diff --git a/packages/backend/server/src/plugins/copilot/tools/doc-edit.ts b/packages/backend/server/src/plugins/copilot/tools/doc-edit.ts index 20ec4b76c..2d61a1e09 100644 --- a/packages/backend/server/src/plugins/copilot/tools/doc-edit.ts +++ b/packages/backend/server/src/plugins/copilot/tools/doc-edit.ts @@ -142,24 +142,33 @@ You should specify the following arguments before the others: [doc_id], [origin_ 'A short, first-person description of the intended edit, clearly summarizing what I will change. For example: "I will translate the steps into English and delete the paragraph explaining the delay." This helps the downstream system understand the purpose of the changes.' ), - code_edit: z - .array( - z.object({ - op: z - .string() - .describe( - 'A short description of the change, such as "Bold intro name"' - ), - updates: z - .string() - .describe( - 'Markdown block fragments that represent the change, including the block_id and type' - ), - }) - ) - .describe( - 'An array of independent semantic changes to apply to the document.' - ), + code_edit: z.preprocess( + val => { + // BACKGROUND: LLM sometimes returns a JSON string instead of an array. + if (typeof val === 'string') { + return JSON.parse(val); + } + return val; + }, + z + .array( + z.object({ + op: z + .string() + .describe( + 'A short description of the change, such as "Bold intro name"' + ), + updates: z + .string() + .describe( + 'Markdown block fragments that represent the change, including the block_id and type' + ), + }) + ) + .describe( + 'An array of independent semantic changes to apply to the document.' + ) + ), }), execute: async ({ doc_id, origin_content, code_edit }) => { try {