Przeglądaj źródła

impl handler with zod

Yuki Takei 1 rok temu
rodzic
commit
5ef7d3573e

+ 24 - 5
apps/app/src/features/openai/client/components/AiAssistant/AiAssistantChatSidebar/AiAssistantChatSidebar.tsx

@@ -7,15 +7,17 @@ import { useForm, Controller } from 'react-hook-form';
 import { useTranslation } from 'react-i18next';
 import { useTranslation } from 'react-i18next';
 import { Collapse, UncontrolledTooltip } from 'reactstrap';
 import { Collapse, UncontrolledTooltip } from 'reactstrap';
 import SimpleBar from 'simplebar-react';
 import SimpleBar from 'simplebar-react';
+import type { z } from 'zod';
 
 
 import { apiv3Post } from '~/client/util/apiv3-client';
 import { apiv3Post } from '~/client/util/apiv3-client';
 import { toastError } from '~/client/util/toastr';
 import { toastError } from '~/client/util/toastr';
-import { MessageErrorCode, StreamErrorCode } from '~/features/openai/interfaces/message-error';
-import type { IThreadRelationHasId } from '~/features/openai/interfaces/thread-relation';
 import { useGrowiCloudUri } from '~/stores-universal/context';
 import { useGrowiCloudUri } from '~/stores-universal/context';
 import loggerFactory from '~/utils/logger';
 import loggerFactory from '~/utils/logger';
 
 
 import type { AiAssistantHasId } from '../../../../interfaces/ai-assistant';
 import type { AiAssistantHasId } from '../../../../interfaces/ai-assistant';
+import { SseMessageSchema, SseDetectedDiffSchema, SseFinalizedSchema } from '../../../../interfaces/editor-assistant/sse-schemas';
+import { MessageErrorCode, StreamErrorCode } from '../../../../interfaces/message-error';
+import type { IThreadRelationHasId } from '../../../../interfaces/thread-relation';
 import { useAiAssistantChatSidebar } from '../../../stores/ai-assistant';
 import { useAiAssistantChatSidebar } from '../../../stores/ai-assistant';
 import { useSWRMUTxMessages } from '../../../stores/message';
 import { useSWRMUTxMessages } from '../../../stores/message';
 import { useSWRMUTxThreads } from '../../../stores/thread';
 import { useSWRMUTxThreads } from '../../../stores/thread';
@@ -31,6 +33,16 @@ const moduleClass = styles['grw-ai-assistant-chat-sidebar'] ?? '';
 
 
 const RIGHT_SIDEBAR_WIDTH = 500;
 const RIGHT_SIDEBAR_WIDTH = 500;
 
 
+
+const handleIfSuccessfullyParsed = <T, >(data: T, zSchema: z.ZodSchema<T>,
+  callback: (data: T) => void,
+): void => {
+  const parsed = zSchema.safeParse(data);
+  if (parsed.success) {
+    callback(data);
+  }
+};
+
 type Message = {
 type Message = {
   id: string,
   id: string,
   content: string,
   content: string,
@@ -201,9 +213,16 @@ const AiAssistantChatSidebarSubstance: React.FC<AiAssistantChatSidebarSubstanceP
             if (data.content != null) {
             if (data.content != null) {
               textValues.push(data.content[0].text.value);
               textValues.push(data.content[0].text.value);
             }
             }
-            if (data.editorResponse != null) {
-              console.log('replace editor', { editorResponse: data.editorResponse });
-            }
+
+            handleIfSuccessfullyParsed(data, SseMessageSchema, (data) => {
+              console.log('sse message', { data });
+            });
+            handleIfSuccessfullyParsed(data, SseDetectedDiffSchema, (data) => {
+              console.log('sse diff', { data });
+            });
+            handleIfSuccessfullyParsed(data, SseFinalizedSchema, (data) => {
+              console.log('sse finalized', { data });
+            });
           }
           }
           else if (trimedLine.startsWith('error:')) {
           else if (trimedLine.startsWith('error:')) {
             const error = JSON.parse(line.replace('error: ', ''));
             const error = JSON.parse(line.replace('error: ', ''));