|
@@ -57,6 +57,65 @@ type Req = Request<undefined, Response, ReqBody> & {
|
|
|
|
|
|
|
|
type PostMessageHandlersFactory = (crowi: Crowi) => RequestHandler[];
|
|
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
|
|
* Create endpoint handlers for editor assistant
|
|
|
*/
|
|
*/
|
|
@@ -137,40 +196,14 @@ export const postMessageToEditHandlersFactory: PostMessageHandlersFactory = (cro
|
|
|
const thread = await openaiClient.beta.threads.retrieve(threadId);
|
|
const thread = await openaiClient.beta.threads.retrieve(threadId);
|
|
|
|
|
|
|
|
// Create stream
|
|
// Create stream
|
|
|
- /* eslint-disable max-len */
|
|
|
|
|
const stream = openaiClient.beta.threads.runs.stream(thread.id, {
|
|
const stream = openaiClient.beta.threads.runs.stream(thread.id, {
|
|
|
assistant_id: assistant.id,
|
|
assistant_id: assistant.id,
|
|
|
additional_messages: [
|
|
additional_messages: [
|
|
|
{
|
|
{
|
|
|
role: 'assistant',
|
|
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',
|
|
// role: 'assistant',
|
|
@@ -209,7 +242,6 @@ export const postMessageToEditHandlersFactory: PostMessageHandlersFactory = (cro
|
|
|
],
|
|
],
|
|
|
response_format: zodResponseFormat(LlmEditorAssistantResponseSchema, 'editor_assistant_response'),
|
|
response_format: zodResponseFormat(LlmEditorAssistantResponseSchema, 'editor_assistant_response'),
|
|
|
});
|
|
});
|
|
|
- /* eslint-disable max-len */
|
|
|
|
|
|
|
|
|
|
// Message delta handler
|
|
// Message delta handler
|
|
|
const messageDeltaHandler = async(delta: MessageDelta) => {
|
|
const messageDeltaHandler = async(delta: MessageDelta) => {
|