Explorar el Código

refactor: update postMessage to use getPageBody for markdown retrieval

Yuki Takei hace 9 meses
padre
commit
8012d5cff1

+ 6 - 13
apps/app/src/features/openai/client/services/editor-assistant/use-editor-assistant.tsx

@@ -156,18 +156,10 @@ export const useEditorAssistant: UseEditorAssistant = () => {
   }, [selectedAiAssistant?._id]);
   }, [selectedAiAssistant?._id]);
 
 
   const postMessage: PostMessage = useCallback(async(threadId, formData) => {
   const postMessage: PostMessage = useCallback(async(threadId, formData) => {
-    const getMarkdown = (): string | undefined => {
-      if (formData.markdownType === 'none') {
-        return undefined;
-      }
-
-      if (formData.markdownType === 'selected') {
-        return selectedText;
-      }
-
-      if (formData.markdownType === 'full') {
-        return codeMirrorEditor?.getDoc();
-      }
+    const getPageBody = (): string | undefined => {
+      // TODO: Reduce to character limit
+      // refs: https://redmine.weseek.co.jp/issues/167688
+      return codeMirrorEditor?.getDoc();
     };
     };
 
 
     // Disable UnifiedMergeView when a Form is submitted with UnifiedMergeView enabled
     // Disable UnifiedMergeView when a Form is submitted with UnifiedMergeView enabled
@@ -179,7 +171,8 @@ export const useEditorAssistant: UseEditorAssistant = () => {
       body: JSON.stringify({
       body: JSON.stringify({
         threadId,
         threadId,
         userMessage: formData.input,
         userMessage: formData.input,
-        markdown: getMarkdown(),
+        selectedText,
+        pageBody: getPageBody(),
       }),
       }),
     });
     });
 
 

+ 18 - 6
apps/app/src/features/openai/server/routes/edit/index.ts

@@ -42,7 +42,8 @@ const LlmEditorAssistantResponseSchema = z.object({
 
 
 type ReqBody = {
 type ReqBody = {
   userMessage: string,
   userMessage: string,
-  markdown?: string,
+  pageBody: string,
+  selectedText?: string,
   threadId?: string,
   threadId?: string,
 }
 }
 
 
@@ -153,10 +154,13 @@ export const postMessageToEditHandlersFactory: PostMessageHandlersFactory = (cro
       .withMessage('userMessage must be string')
       .withMessage('userMessage must be string')
       .notEmpty()
       .notEmpty()
       .withMessage('userMessage must be set'),
       .withMessage('userMessage must be set'),
-    body('markdown')
+    body('pageBody')
+      .isString()
+      .withMessage('pageBody must be string and not empty'),
+    body('selectedText')
       .optional()
       .optional()
       .isString()
       .isString()
-      .withMessage('markdown must be string'),
+      .withMessage('selectedText must be string'),
     body('threadId').optional().isString().withMessage('threadId must be string'),
     body('threadId').optional().isString().withMessage('threadId must be string'),
   ];
   ];
 
 
@@ -164,7 +168,7 @@ export const postMessageToEditHandlersFactory: PostMessageHandlersFactory = (cro
     accessTokenParser, loginRequiredStrictly, certifyAiService, validator, apiV3FormValidator,
     accessTokenParser, loginRequiredStrictly, certifyAiService, validator, apiV3FormValidator,
     async(req: Req, res: ApiV3Response) => {
     async(req: Req, res: ApiV3Response) => {
       const {
       const {
-        userMessage, markdown, threadId,
+        userMessage, pageBody, selectedText, threadId,
       } = req.body;
       } = req.body;
 
 
       // Parameter check
       // Parameter check
@@ -225,11 +229,19 @@ export const postMessageToEditHandlersFactory: PostMessageHandlersFactory = (cro
           additional_messages: [
           additional_messages: [
             {
             {
               role: 'assistant',
               role: 'assistant',
-              content: instruction(markdown != null),
+              content: instruction(pageBody != null),
             },
             },
             {
             {
               role: 'user',
               role: 'user',
-              content: `Current markdown content:\n\`\`\`markdown\n${markdown}\n\`\`\`\n\nUser request: ${userMessage}`,
+              content: `Current markdown content:
+\`\`\`markdown
+${pageBody}
+\`\`\`
+${selectedText != null
+    ? `Current selected text by user:\`\`\`markdown\n${selectedText}\n\`\`\``
+    : ''
+}
+User request: ${userMessage}`,
             },
             },
           ],
           ],
           response_format: zodResponseFormat(LlmEditorAssistantResponseSchema, 'editor_assistant_response'),
           response_format: zodResponseFormat(LlmEditorAssistantResponseSchema, 'editor_assistant_response'),