From 5f14a2fa664f3c9b0df23eb01c2f774a69cfd764 Mon Sep 17 00:00:00 2001 From: Matt Cowger Date: Tue, 2 Dec 2025 20:08:26 +0000 Subject: [PATCH 1/3] fix: handle JSON contents of create_new_file Handle case where JSON content was parsed into an object by the tool call parser. If the arguments to the tool call are valid JSON (e.g. the model attempts to create a .json file) the earlier call to JSON.parse() will have deeply parsed the returned contents. If that has happened, convert back to string. Fixes: #8972 --- core/tools/parseArgs.ts | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/core/tools/parseArgs.ts b/core/tools/parseArgs.ts index 543e6ba53..14885c979 100644 --- a/core/tools/parseArgs.ts +++ b/core/tools/parseArgs.ts @@ -18,15 +18,41 @@ export function getStringArg( argName: string, allowEmpty = false, ): string { - if (!args || !(argName in args) || typeof args[argName] !== "string") { + if (!args || !(argName in args)) { throw new Error( `\`${argName}\` argument is required${allowEmpty ? "" : " and must not be empty or whitespace-only"}. (type string)`, ); } - if (!allowEmpty && !args[argName].trim()) { + + let value = args[argName]; + + // Handle case where JSON content was parsed into an object by the tool call parser. + // If the arguments to the tool call are valid JSON (e.g. the model attempts to create a .json file) + // the earlier call to JSON.parse() will have deeply parsed the returned contents. + // If that has happened, convert back to string. + if (typeof value === "object" && value !== null) { + // Special handling for contents parameter which should always be a string + if (argName === "contents") { + value = JSON.stringify(value); + return value; + } else { + throw new Error( + `Argument \`${argName}\` must be a string, not an object`, + ); + } + } + + if (!allowEmpty && !value.trim()) { throw new Error(`Argument ${argName} must not be empty or whitespace-only`); } - return args[argName]; + + if (typeof value !== "string") { + throw new Error( + `\`${argName}\` argument is required${allowEmpty ? "" : " and must not be empty or whitespace-only"}. (type string)`, + ); + } + + return value; } export function getOptionalStringArg( From a3810024c697d0e2a1d83ddeeccd6cd5eef8cfb6 Mon Sep 17 00:00:00 2001 From: Matt Cowger Date: Tue, 2 Dec 2025 20:15:10 +0000 Subject: [PATCH 2/3] Add tests to match. --- core/tools/parseArgs.ts | 8 +++--- core/tools/parseArgs.vitest.ts | 51 ++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/core/tools/parseArgs.ts b/core/tools/parseArgs.ts index 14885c979..fa6e25b85 100644 --- a/core/tools/parseArgs.ts +++ b/core/tools/parseArgs.ts @@ -42,16 +42,16 @@ export function getStringArg( } } - if (!allowEmpty && !value.trim()) { - throw new Error(`Argument ${argName} must not be empty or whitespace-only`); - } - if (typeof value !== "string") { throw new Error( `\`${argName}\` argument is required${allowEmpty ? "" : " and must not be empty or whitespace-only"}. (type string)`, ); } + if (!allowEmpty && !value.trim()) { + throw new Error(`Argument ${argName} must not be empty or whitespace-only`); + } + return value; } diff --git a/core/tools/parseArgs.vitest.ts b/core/tools/parseArgs.vitest.ts index 71bfc0cc5..c3d0f6428 100644 --- a/core/tools/parseArgs.vitest.ts +++ b/core/tools/parseArgs.vitest.ts @@ -144,6 +144,57 @@ describe("getStringArg", () => { "`name` argument is required and must not be empty or whitespace-only. (type string)", ); }); + + it("should convert parsed JSON object to string for contents parameter", () => { + // This simulates the case where JSON.parse() has converted a JSON string into an object + const args = { contents: { key: "value", number: 123 } }; + const result = getStringArg(args, "contents"); + expect(result).toBe('{"key":"value","number":123}'); + }); + + it("should convert nested JSON object to string for contents parameter", () => { + const args = { + contents: { + user: { + name: "John", + details: { + age: 30, + preferences: ["coding", "reading"], + }, + }, + }, + }; + const result = getStringArg(args, "contents"); + const expected = + '{"user":{"name":"John","details":{"age":30,"preferences":["coding","reading"]}}}'; + expect(result).toBe(expected); + }); + + it("should convert JSON array to string for contents parameter", () => { + const args = { contents: ["item1", "item2", { key: "value" }] }; + const result = getStringArg(args, "contents"); + expect(result).toBe('["item1","item2",{"key":"value"}]'); + }); + + it("should throw error when non-contents parameter is an object", () => { + const args = { filename: { invalid: "object" } }; + expect(() => getStringArg(args, "filename")).toThrowError( + "Argument `filename` must be a string, not an object", + ); + }); + + it("should handle contents parameter that is already a string", () => { + const args = { contents: "already a string" }; + const result = getStringArg(args, "contents"); + expect(result).toBe("already a string"); + }); + + it("should handle contents parameter that is null", () => { + const args = { contents: null }; + expect(() => getStringArg(args, "contents")).toThrowError( + "`contents` argument is required and must not be empty or whitespace-only. (type string)", + ); + }); }); describe("getOptionalStringArg", () => { From e04c21f9c883ad62d6c15661d715635f1a69c50e Mon Sep 17 00:00:00 2001 From: Matt Cowger Date: Tue, 2 Dec 2025 20:28:45 +0000 Subject: [PATCH 3/3] Address comments from @RomneyDa --- core/tools/parseArgs.ts | 13 +++++-------- core/tools/parseArgs.vitest.ts | 7 ------- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/core/tools/parseArgs.ts b/core/tools/parseArgs.ts index fa6e25b85..f919ddc92 100644 --- a/core/tools/parseArgs.ts +++ b/core/tools/parseArgs.ts @@ -26,19 +26,16 @@ export function getStringArg( let value = args[argName]; - // Handle case where JSON content was parsed into an object by the tool call parser. + // Handle case where JSON was parsed into an object by the tool call parser. // If the arguments to the tool call are valid JSON (e.g. the model attempts to create a .json file) - // the earlier call to JSON.parse() will have deeply parsed the returned contents. + // the earlier call to JSON.parse() will have deeply parsed the returned arguments. // If that has happened, convert back to string. if (typeof value === "object" && value !== null) { - // Special handling for contents parameter which should always be a string - if (argName === "contents") { + try { value = JSON.stringify(value); return value; - } else { - throw new Error( - `Argument \`${argName}\` must be a string, not an object`, - ); + } catch (e) { + //Swallow this, because it might be fine later. } } diff --git a/core/tools/parseArgs.vitest.ts b/core/tools/parseArgs.vitest.ts index c3d0f6428..ed0463259 100644 --- a/core/tools/parseArgs.vitest.ts +++ b/core/tools/parseArgs.vitest.ts @@ -176,13 +176,6 @@ describe("getStringArg", () => { expect(result).toBe('["item1","item2",{"key":"value"}]'); }); - it("should throw error when non-contents parameter is an object", () => { - const args = { filename: { invalid: "object" } }; - expect(() => getStringArg(args, "filename")).toThrowError( - "Argument `filename` must be a string, not an object", - ); - }); - it("should handle contents parameter that is already a string", () => { const args = { contents: "already a string" }; const result = getStringArg(args, "contents");