Просмотр исходного кода

Merge pull request #9843 from weseek/feat/164617-apply-llm-responses-to-unified-merge-view

feat: Apply LLM responses to UnifiedMergeView (without Markdown)
Yuki Takei 11 месяцев назад
Родитель
Сommit
5c99abd2e0

+ 3 - 1
apps/app/src/features/openai/client/services/editor-assistant.ts

@@ -119,7 +119,9 @@ export const useEditorAssistant: UseEditorAssistant = () => {
       body: JSON.stringify({
         threadId,
         userMessage,
-        markdown: selectedText,
+        markdown: selectedText != null && selectedText.length !== 0
+          ? selectedText
+          : undefined,
       }),
     });
 

+ 62 - 30
apps/app/src/features/openai/server/routes/edit/index.ts

@@ -57,6 +57,65 @@ type Req = Request<undefined, Response, ReqBody> & {
 
 type PostMessageHandlersFactory = (crowi: Crowi) => RequestHandler[];
 
+
+// -----------------------------------------------------------------------------
+// Instructions
+// -----------------------------------------------------------------------------
+/* eslint-disable max-len */
+const instructionWithMarkdown = `You are an Editor Assistant for GROWI, a markdown wiki system.
+    Your task is to help users edit their markdown content based on their requests.
+    Spaces and line breaks are also counted as individual characters.
+
+    RESPONSE FORMAT:
+    You must respond with a JSON object in the following format example:
+    {
+      "contents": [
+        { "message": "Your brief message about the upcoming change or proposal.\n\n" },
+        { "replace": "New text 1" },
+        { "message": "Additional explanation if needed" },
+        { "replace": "New text 2" },
+        ...more items if needed
+        { "message": "Your friendly message explaining what changes were made or suggested." }
+      ]
+    }
+
+    The array should contain:
+    - [At the beginning of the list] A "message" object that has your brief message about the upcoming change or proposal. Be sure to add two consecutive line feeds ('\n\n') at the end.
+    - Objects with a "message" key for explanatory text to the user if needed.
+    - Edit markdown according to user instructions and include it line by line in the 'replace' object. Return original text for lines that do not need editing.
+    - [At the end of the list] A "message" object that contains your friendly message explaining that the operation was completed and what changes were made.
+
+    IMPORTANT:
+    - The text for lines that do not need correction must be returned exactly as in the original text.
+    - Include original text in the replace object even if it contains only spaces or line breaks
+
+    Always provide messages in the same language as the user's request.`;
+
+const instructionWithoutMarkdown = `You are an Editor Assistant for GROWI, a markdown wiki system.
+    Your task is to help users edit their markdown content based on their requests.
+
+    RESPONSE FORMAT:
+    You must respond with a JSON object in the following format example:
+    {
+      "contents": [
+        { "message": "Your brief message about the upcoming change or proposal.\n\n" },
+        { "replace": "New text 1" },
+        { "message": "Additional explanation if needed" },
+        { "replace": "New text 2" },
+        ...more items if needed
+        { "message": "Your friendly message explaining what changes were made or suggested." }
+      ]
+    }
+
+    The array should contain:
+    - [At the beginning of the list] A "message" object that has your brief message about the upcoming change or proposal. Be sure to add two consecutive line feeds ('\n\n') at the end.
+    - Objects with a "message" key for explanatory text to the user if needed.
+    - Edit markdown according to user instructions and include it line by line in the 'replace' object.
+    - [At the end of the list] A "message" object that contains your friendly message explaining that the operation was completed and what changes were made.
+
+    Always provide messages in the same language as the user's request.`;
+/* eslint-disable max-len */
+
 /**
  * Create endpoint handlers for editor assistant
  */
@@ -137,40 +196,14 @@ export const postMessageToEditHandlersFactory: PostMessageHandlersFactory = (cro
         const thread = await openaiClient.beta.threads.retrieve(threadId);
 
         // Create stream
-        /* eslint-disable max-len */
         const stream = openaiClient.beta.threads.runs.stream(thread.id, {
           assistant_id: assistant.id,
           additional_messages: [
             {
               role: 'assistant',
-              content: `You are an Editor Assistant for GROWI, a markdown wiki system.
-              Your task is to help users edit their markdown content based on their requests.
-              Spaces and line breaks are also counted as individual characters.
-
-              RESPONSE FORMAT:
-              You must respond with a JSON object in the following format example:
-              {
-                "contents": [
-                  { "message": "Your brief message about the upcoming change or proposal.\n\n" },
-                  { "replace": New text 1 },,
-                  { "message": "Additional explanation if needed" },
-                  { "replace": "New text 2" },
-                  ...more items if needed
-                  { "message": "Your friendly message explaining what changes were made or suggested." }
-                ]
-              }
-
-              The array should contain:
-              - [At the beginning of the list] A "message" object that has your brief message about the upcoming change or proposal. Be sure to add two consecutive line feeds ('\n\n') at the end.
-              - Objects with a "message" key for explanatory text to the user if needed.
-              - Edit markdown according to user instructions and include it line by line in the 'replace' object. Return original text for lines that do not need editing.
-              - [At the end of the list] A "message" object that contains your friendly message explaining that the operation was completed and what changes were made.
-
-              IMPORTANT:
-              - The text for lines that do not need correction must be returned exactly as in the original text.
-              - Include original text in the replace object even if it contains only spaces or line breaks
-
-              Always provide messages in the same language as the user's request.`,
+              content: markdown != null
+                ? instructionWithMarkdown
+                : instructionWithoutMarkdown,
             },
             // {
             //   role: 'assistant',
@@ -209,7 +242,6 @@ export const postMessageToEditHandlersFactory: PostMessageHandlersFactory = (cro
           ],
           response_format: zodResponseFormat(LlmEditorAssistantResponseSchema, 'editor_assistant_response'),
         });
-        /* eslint-disable max-len */
 
         // Message delta handler
         const messageDeltaHandler = async(delta: MessageDelta) => {