Browse Source

Refactoring of chank processing

Shun Miyazawa 1 year ago
parent
commit
0a25807e14

+ 21 - 21
apps/app/src/features/openai/chat/components/AiChatModal/AiChatModal.tsx

@@ -144,28 +144,28 @@ const AiChatModalSubstance = (): JSX.Element => {
 
 
         const chunk = decoder.decode(value);
         const chunk = decoder.decode(value);
 
 
-        if (chunk.startsWith('error:')) {
-          const error = JSON.parse(chunk.replace('error: ', ''));
-          logger.error(error.errorMessage);
-          form.setError('input', { type: 'manual', message: error.message });
-          if (error.code === StreamErrorCode.RATE_LIMIT_EXCEEDED) {
-
-            const toastErrorMessage = growiCloudUri != null
-              ? 'modal_aichat.rate_limit_exceeded_for_growi_cloud'
-              : 'modal_aichat.rate_limit_exceeded';
-
-            toastError(t(toastErrorMessage));
+        const textValues: string[] = [];
+        const lines = chunk.split('\n\n');
+        lines.forEach((line) => {
+          const trimedLine = line.trim();
+          if (trimedLine.startsWith('data:')) {
+            const data = JSON.parse(line.replace('data: ', ''));
+            textValues.push(data.content[0].text.value);
           }
           }
-        }
+          else if (trimedLine.startsWith('error:')) {
+            const error = JSON.parse(line.replace('error: ', ''));
+            logger.error(error.errorMessage);
+            form.setError('input', { type: 'manual', message: error.message });
+
+            if (error.code === StreamErrorCode.RATE_LIMIT_EXCEEDED) {
+              const toastErrorMessage = growiCloudUri != null
+                ? 'modal_aichat.rate_limit_exceeded_for_growi_cloud'
+                : 'modal_aichat.rate_limit_exceeded';
+              toastError(t(toastErrorMessage));
+            }
+          }
+        });
 
 
-        // Extract text values from the chunk
-        const textValues = chunk
-          .split('\n\n')
-          .filter(line => line.trim().startsWith('data:'))
-          .map((line) => {
-            const data = JSON.parse(line.replace('data: ', ''));
-            return data.content[0].text.value;
-          });
 
 
         // append text values to the assistant message
         // append text values to the assistant message
         setGeneratingAnswerMessage((prevMessage) => {
         setGeneratingAnswerMessage((prevMessage) => {
@@ -185,7 +185,7 @@ const AiChatModalSubstance = (): JSX.Element => {
       form.setError('input', { type: 'manual', message: err.toString() });
       form.setError('input', { type: 'manual', message: err.toString() });
     }
     }
 
 
-  }, [form, isGenerating, messageLogs, t, threadId]);
+  }, [form, growiCloudUri, isGenerating, messageLogs, t, threadId]);
 
 
   const keyDownHandler = (event: KeyboardEvent<HTMLTextAreaElement>) => {
   const keyDownHandler = (event: KeyboardEvent<HTMLTextAreaElement>) => {
     if (event.key === 'Enter' && (event.ctrlKey || event.metaKey)) {
     if (event.key === 'Enter' && (event.ctrlKey || event.metaKey)) {

+ 1 - 1
apps/app/src/features/openai/server/routes/message.ts

@@ -81,7 +81,7 @@ export const postMessageHandlersFactory: PostMessageHandlersFactory = (crowi) =>
       };
       };
 
 
       const sendError = (code: StreamErrorCode, message: string) => {
       const sendError = (code: StreamErrorCode, message: string) => {
-        res.write(`error: ${JSON.stringify({ code, message })}`);
+        res.write(`error: ${JSON.stringify({ code, message })}\n\n`);
       };
       };
 
 
       stream.on('event', (delta) => {
       stream.on('event', (delta) => {