diff --git a/packages/opencode/src/tool/write.ts b/packages/opencode/src/tool/write.ts index 6b8fd3dd1..8b69aa2af 100644 --- a/packages/opencode/src/tool/write.ts +++ b/packages/opencode/src/tool/write.ts @@ -17,7 +17,25 @@ 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"), + content: z + .preprocess((val) => { + if (typeof val === "string") { + return val + } + if (Array.isArray(val)) { + // Join array elements with newlines + // Handle edge cases: null/undefined -> "", objects -> JSON.stringify, others -> String + return val + .map((item) => { + if (item === null || item === undefined) return "" + if (typeof item === "object") return JSON.stringify(item) + return String(item) + }) + .join("\n") + } + return String(val) + }, 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)"), }), async execute(params, ctx) {