diff --git a/core/tools/parseArgs.ts b/core/tools/parseArgs.ts index 543e6ba53..f919ddc92 100644 --- a/core/tools/parseArgs.ts +++ b/core/tools/parseArgs.ts @@ -18,15 +18,38 @@ 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 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 arguments. + // If that has happened, convert back to string. + if (typeof value === "object" && value !== null) { + try { + value = JSON.stringify(value); + return value; + } catch (e) { + //Swallow this, because it might be fine later. + } + } + + 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 args[argName]; + + return value; } export function getOptionalStringArg( diff --git a/core/tools/parseArgs.vitest.ts b/core/tools/parseArgs.vitest.ts index 71bfc0cc5..ed0463259 100644 --- a/core/tools/parseArgs.vitest.ts +++ b/core/tools/parseArgs.vitest.ts @@ -144,6 +144,50 @@ 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 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", () => {