knowledge-assistant.ts 863 B

12345678910111213141516171819202122232425
  1. import { SseMessageSchema, type SseMessage } from '~/features/openai/interfaces/knowledge-assistant/sse-schemas';
  2. import { handleIfSuccessfullyParsed } from '~/features/openai/utils/handle-if-successfully-parsed';
  3. export const postMessage = async(
  4. aiAssistantId: string, threadId: string, userMessage: string, summaryMode?: boolean,
  5. ): Promise<Response> => {
  6. const response = await fetch('/_api/v3/openai/message', {
  7. method: 'POST',
  8. headers: { 'Content-Type': 'application/json' },
  9. body: JSON.stringify({
  10. aiAssistantId,
  11. threadId,
  12. userMessage,
  13. summaryMode,
  14. }),
  15. });
  16. return response;
  17. };
  18. export const processMessage = (data: unknown,
  19. handler: {onMessage: (data: SseMessage) => void}) : void => {
  20. handleIfSuccessfullyParsed(data, SseMessageSchema, (data: SseMessage) => {
  21. handler.onMessage(data);
  22. });
  23. };