From 612c73cab12282e029a4759e9a40f3d0e9a14db2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=B7=E5=B8=83=E5=8A=B3=E5=A4=96=20=C2=B7=20=E8=B4=BE?= =?UTF-8?q?=E8=B4=B5?= <472285740@qq.com> Date: Mon, 21 Jul 2025 11:38:21 +0800 Subject: [PATCH] fix(core): code-edit param maybe json string (#13278) ## Summary by CodeRabbit * **Bug Fixes** * Improved input handling for code editing by allowing the tool to accept both arrays and JSON string representations for the `code_edit` parameter, ensuring more robust and flexible input validation. --- .../src/plugins/copilot/tools/doc-edit.ts | 45 +++++++++++-------- 1 file changed, 27 insertions(+), 18 deletions(-) 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 {