diff --git a/packages/opencode/src/tool/edit.ts b/packages/opencode/src/tool/edit.ts index 9505dd9eab96..3711a9234105 100644 --- a/packages/opencode/src/tool/edit.ts +++ b/packages/opencode/src/tool/edit.ts @@ -38,8 +38,18 @@ export const EditTool = Tool.define("edit", { description: DESCRIPTION, parameters: z.object({ filePath: z.string().describe("The absolute path to the file to modify"), - oldString: z.string().describe("The text to replace"), - newString: z.string().describe("The text to replace it with (must be different from oldString)"), + oldString: z + .preprocess( + (v) => (v !== null && typeof v === "object" ? JSON.stringify(v, null, 2) : v), + z.string(), + ) + .describe("The text to replace"), + newString: z + .preprocess( + (v) => (v !== null && typeof v === "object" ? JSON.stringify(v, null, 2) : v), + z.string(), + ) + .describe("The text to replace it with (must be different from oldString)"), replaceAll: z.boolean().optional().describe("Replace all occurrences of oldString (default false)"), }), async execute(params, ctx) { diff --git a/packages/opencode/src/tool/write.ts b/packages/opencode/src/tool/write.ts index 6b134e5253de..aa331624d386 100644 --- a/packages/opencode/src/tool/write.ts +++ b/packages/opencode/src/tool/write.ts @@ -20,8 +20,13 @@ const MAX_PROJECT_DIAGNOSTICS_FILES = 5 export const WriteTool = Tool.define("write", { description: DESCRIPTION, parameters: z.object({ - content: z.string().describe("The content to write to the file"), filePath: z.string().describe("The absolute path to the file to write (must be absolute, not relative)"), + content: z + .preprocess( + (v) => (v !== null && typeof v === "object" ? JSON.stringify(v, null, 2) : v), + z.string(), + ) + .describe("The content to write to the file"), }), async execute(params, ctx) { const filepath = path.isAbsolute(params.filePath) ? params.filePath : path.join(Instance.directory, params.filePath)