fix: system message tools array args for search and replace tool

This commit is contained in:
Dallin Romney 2025-07-24 19:06:42 +07:00
parent 954d816885
commit ec9d3157bb
4 changed files with 53 additions and 39 deletions

View file

@ -104,7 +104,7 @@ ${SEARCH_AND_REPLACE_RULES}
[
["filepath", "path/to/file.ts"],
[
"diff",
"diffs",
`[
"------- SEARCH
[exact content to find]

View file

@ -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);

View file

@ -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),
});
});
});

View file

@ -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;