From ec9d3157bb3d4413db982fd84eb3bb3d85301eab Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Thu, 24 Jul 2025 19:06:42 +0700 Subject: [PATCH] fix: system message tools array args for search and replace tool --- .../definitions/searchAndReplaceInFile.ts | 2 +- .../systemMessageTools/parseSystemToolCall.ts | 21 ++++++++++- .../parseSystemToolCall.vitest.ts | 32 ++++++++++++++++ manual-testing-sandbox/test.js | 37 ------------------- 4 files changed, 53 insertions(+), 39 deletions(-) diff --git a/core/tools/definitions/searchAndReplaceInFile.ts b/core/tools/definitions/searchAndReplaceInFile.ts index aa12e7c72..96dac65da 100644 --- a/core/tools/definitions/searchAndReplaceInFile.ts +++ b/core/tools/definitions/searchAndReplaceInFile.ts @@ -104,7 +104,7 @@ ${SEARCH_AND_REPLACE_RULES} [ ["filepath", "path/to/file.ts"], [ - "diff", + "diffs", `[ "------- SEARCH [exact content to find] diff --git a/core/tools/systemMessageTools/parseSystemToolCall.ts b/core/tools/systemMessageTools/parseSystemToolCall.ts index 74dc6f470..0300654d9 100644 --- a/core/tools/systemMessageTools/parseSystemToolCall.ts +++ b/core/tools/systemMessageTools/parseSystemToolCall.ts @@ -87,12 +87,31 @@ export function handleToolCallBuffer( if (isNewLine) { const isEndArgTag = line.match(/end_?arg/i); if (isEndArgTag) { - const trimmedValue = state.currentArgLines.join("").trim(); + let trimmedValue = state.currentArgLines.join("").trim(); state.currentArgLines.length = 0; state.processedArgNames.add(state.currentArgName); state.currentArgName = undefined; try { + if ( + trimmedValue.startsWith("[") || + trimmedValue.startsWith("{") + ) { + trimmedValue = trimmedValue.replace( + /"((?:\\[\s\S]|[^"\\])*?)"/g, + (match) => { + const content = match.slice(1, -1); + // Replace unescaped newlines + return ( + '"' + + content + .replace(/([^\\])\n/g, "$1\\n") + .replace(/^\n/g, "\\n") + + '"' + ); + }, + ); + } const parsed = JSON.parse(trimmedValue); const stringifiedArg = JSON.stringify(parsed); return createDelta("", stringifiedArg, state.toolCallId); diff --git a/core/tools/systemMessageTools/parseSystemToolCall.vitest.ts b/core/tools/systemMessageTools/parseSystemToolCall.vitest.ts index 436e3fa6e..2aeac2ce8 100644 --- a/core/tools/systemMessageTools/parseSystemToolCall.vitest.ts +++ b/core/tools/systemMessageTools/parseSystemToolCall.vitest.ts @@ -266,4 +266,36 @@ describe("handleToolCallBuffer", () => { }); expect(state.done).toBe(true); }); + + it("handles JSON array args", () => { + state.currentLineIndex = 3; + state.currentArgName = "test_arg"; + state.currentArgLines = [ + "[\n", + '"------- SEARCH\n', + " subtract(number) {\n", + " return this;\n", + " }\n", + "=======\n", + " subtract(number) {\n", + " this -= number\n", + " return this;\n", + " }\n", + '+++++++ REPLACE"\n', + "]\n", + ]; + + handleToolCallBuffer("END_ARG", state); + const newLineResult = handleToolCallBuffer("\n", state); + + expect(newLineResult).toEqual({ + type: "function", + function: { + name: "", + arguments: + '["------- SEARCH\\n subtract(number) {\\n return this;\\n }\\n=======\\n subtract(number) {\\n this -= number\\n return this;\\n }\\n+++++++ REPLACE"]', + }, + id: expect.any(String), + }); + }); }); diff --git a/manual-testing-sandbox/test.js b/manual-testing-sandbox/test.js index 0e60af4e0..d2920d6af 100644 --- a/manual-testing-sandbox/test.js +++ b/manual-testing-sandbox/test.js @@ -1,50 +1,21 @@ -/** - * Calculator class for performing basic arithmetic operations - * Uses method chaining to allow consecutive operations - */ class Calculator { - /** - * Initialize calculator with result set to 0 - */ constructor() { this.result = 0; } - /** - * Add a number to the current result - * @param {number} number - The number to add - * @returns {Calculator} - Returns this instance for method chaining - */ add(number) { this.result += number; return this; } - - /** - * Subtract a number from the current result - * @param {number} number - The number to subtract - * @returns {Calculator} - Returns this instance for method chaining - */ subtract(number) { return this; } - /** - * Multiply the current result by a number - * @param {number} number - The number to multiply by - * @returns {Calculator} - Returns this instance for method chaining - */ multiply(number) { this.result *= number; return this; } - /** - * Divide the current result by a number - * @param {number} number - The number to divide by - * @returns {Calculator} - Returns this instance for method chaining - * @throws {Error} - Throws error if attempting to divide by zero - */ divide(number) { if (number === 0) { throw new Error("Cannot divide by zero"); @@ -53,18 +24,10 @@ class Calculator { return this; } - /** - * Get the current result value - * @returns {number} - The current result - */ getResult() { return this.result; } - /** - * Reset the result to 0 - * @returns {Calculator} - Returns this instance for method chaining - */ reset() { this.result = 0; return this;