fix: make search and replace edit copy less confusing

This commit is contained in:
Dallin Romney 2025-08-25 14:12:54 -07:00
parent ff931ec433
commit fc93a2e922
4 changed files with 35 additions and 27 deletions

View file

@ -1,6 +1,8 @@
import { EditOperation } from "core/tools/definitions/multiEdit";
import { describe, expect, it } from "vitest";
import {
EMPTY_NON_FIRST_EDIT_MESSAGE,
FOUND_MULTIPLE_FIND_STRINGS_ERROR,
performFindAndReplace,
validateCreatingForMultiEdit,
validateSingleEdit,
@ -152,7 +154,7 @@ describe("performFindAndReplace", () => {
expect(() => {
performFindAndReplace(content, "xyz", "abc");
}).toThrow('String not found in file: "xyz"');
}).toThrow('string not found in file: "xyz"');
});
it("should throw error with edit context when string not found", () => {
@ -160,7 +162,7 @@ describe("performFindAndReplace", () => {
expect(() => {
performFindAndReplace(content, "xyz", "abc", false, 2);
}).toThrow('Edit #3: String not found in file: "xyz"');
}).toThrow('edit at index 2: string not found in file: "xyz"');
});
it("should throw error when multiple occurrences exist and replaceAll is false", () => {
@ -169,7 +171,7 @@ describe("performFindAndReplace", () => {
expect(() => {
performFindAndReplace(content, "Hello", "Hi", false);
}).toThrow(
'String "Hello" appears 3 times in the file. Either provide a more specific string with surrounding context to make it unique, or use replace_all=true to replace all occurrences.',
`String "Hello" appears 3 times in the file. ${FOUND_MULTIPLE_FIND_STRINGS_ERROR}`,
);
});
@ -179,7 +181,7 @@ describe("performFindAndReplace", () => {
expect(() => {
performFindAndReplace(content, "test", "exam", false, 1);
}).toThrow(
'Edit #2: String "test" appears 3 times in the file. Either provide a more specific string with surrounding context to make it unique, or use replace_all=true to replace all occurrences.',
`edit at index 1: String "test" appears 3 times in the file. ${FOUND_MULTIPLE_FIND_STRINGS_ERROR}`,
);
});
});
@ -296,19 +298,21 @@ describe("validateSingleEdit", () => {
it("should include edit number in error messages when index provided", () => {
expect(() => {
validateSingleEdit(null as any, "new", 2);
}).toThrow("Edit #3: old_string is required");
}).toThrow("edit at index 2: old_string is required");
});
it("should include edit number for new_string errors", () => {
expect(() => {
validateSingleEdit("old", undefined as any, 0);
}).toThrow("Edit #1: new_string is required");
}).toThrow("edit at index 0: new_string is required");
});
it("should include edit number for identical strings error", () => {
expect(() => {
validateSingleEdit("same", "same", 4);
}).toThrow("Edit #5: old_string and new_string must be different");
}).toThrow(
"edit at index 4: old_string and new_string must be different",
);
});
it("should not include context when index not provided", () => {
@ -382,9 +386,7 @@ describe("validateCreatingForMultiEdit", () => {
expect(() => {
validateCreatingForMultiEdit(edits);
}).toThrow(
"edit #2: only the first edit can contain an empty old_string, which is only used for file creation.",
);
}).toThrow(`edit at index 1: ${EMPTY_NON_FIRST_EDIT_MESSAGE}`);
});
it("should throw error with correct edit number for empty old_string", () => {
@ -396,9 +398,7 @@ describe("validateCreatingForMultiEdit", () => {
expect(() => {
validateCreatingForMultiEdit(edits);
}).toThrow(
"edit #3: only the first edit can contain an empty old_string, which is only used for file creation.",
);
}).toThrow(`edit at index 2: ${EMPTY_NON_FIRST_EDIT_MESSAGE}`);
});
it("should throw error for multiple empty old_strings in subsequent edits", () => {
@ -410,9 +410,7 @@ describe("validateCreatingForMultiEdit", () => {
expect(() => {
validateCreatingForMultiEdit(edits);
}).toThrow(
"edit #2: only the first edit can contain an empty old_string, which is only used for file creation.",
);
}).toThrow(`edit at index 1: ${EMPTY_NON_FIRST_EDIT_MESSAGE}`);
});
});

View file

@ -1,5 +1,8 @@
import { EditOperation } from "core/tools/definitions/multiEdit";
export const FOUND_MULTIPLE_FIND_STRINGS_ERROR =
"Either provide a more specific string with surrounding context to make it unique, or use replace_all=true to replace all occurrences.";
/**
* Performs a find and replace operation on text content with proper handling of special characters
*/
@ -10,10 +13,10 @@ export function performFindAndReplace(
replaceAll: boolean = false,
index?: number, // For error messages
): string {
const errorContext = index !== undefined ? `Edit #${index + 1}: ` : "";
const errorContext = index !== undefined ? `edit at index ${index}: ` : "";
// Check if old_string exists in current content
if (!content.includes(oldString)) {
throw new Error(`${errorContext}String not found in file: "${oldString}"`);
throw new Error(`${errorContext}string not found in file: "${oldString}"`);
}
if (replaceAll) {
@ -35,7 +38,7 @@ export function performFindAndReplace(
if (count > 1) {
throw new Error(
`${errorContext}String "${oldString}" appears ${count} times in the file. Either provide a more specific string with surrounding context to make it unique, or use replace_all=true to replace all occurrences.`,
`${errorContext}String "${oldString}" appears ${count} times in the file. ${FOUND_MULTIPLE_FIND_STRINGS_ERROR}`,
);
}
@ -57,7 +60,7 @@ export function validateSingleEdit(
newString: string,
index?: number,
): void {
const context = index !== undefined ? `Edit #${index + 1}: ` : "";
const context = index !== undefined ? `edit at index ${index}: ` : "";
if (!oldString && oldString !== "") {
throw new Error(`${context}old_string is required`);
@ -70,6 +73,8 @@ export function validateSingleEdit(
}
}
export const EMPTY_NON_FIRST_EDIT_MESSAGE =
"contains empty old_string. Only the first edit can contain an empty old_string, which is only used for file creation.";
export function validateCreatingForMultiEdit(edits: EditOperation[]) {
const isCreating = edits[0].old_string === "";
if (edits.length > 1) {
@ -81,7 +86,7 @@ export function validateCreatingForMultiEdit(edits: EditOperation[]) {
for (let i = 1; i < edits.length; i++) {
if (edits[i].old_string === "") {
throw new Error(
`edit #${i + 1}: only the first edit can contain an empty old_string, which is only used for file creation.`,
`edit at index ${i}: ${EMPTY_NON_FIRST_EDIT_MESSAGE}`,
);
}
}

View file

@ -2,6 +2,7 @@ import * as ideUtils from "core/util/ideUtils";
import { beforeEach, describe, expect, it, Mock, vi } from "vitest";
import { applyForEditTool } from "../../redux/thunks/handleApplyStateUpdate";
import { ClientToolExtras } from "./callClientTool";
import { FOUND_MULTIPLE_FIND_STRINGS_ERROR } from "./findAndReplaceUtils";
import { multiEditImpl } from "./multiEditImpl";
vi.mock("uuid", () => ({
@ -72,7 +73,7 @@ describe("multiEditImpl", () => {
"id",
mockExtras,
),
).rejects.toThrow("Edit #1: old_string is required");
).rejects.toThrow("edit at index 0: old_string is required");
});
it("should throw if edit has undefined new_string", async () => {
@ -85,7 +86,7 @@ describe("multiEditImpl", () => {
"id",
mockExtras,
),
).rejects.toThrow("Edit #1: new_string is required");
).rejects.toThrow("edit at index 0: new_string is required");
});
it("should throw if old_string equals new_string", async () => {
@ -98,7 +99,9 @@ describe("multiEditImpl", () => {
"id",
mockExtras,
),
).rejects.toThrow("Edit #1: old_string and new_string must be different");
).rejects.toThrow(
"edit at index 0: old_string and new_string must be different",
);
});
});
@ -201,7 +204,9 @@ describe("multiEditImpl", () => {
"id",
mockExtras,
),
).rejects.toThrow('Edit #2: String not found in file: "not found"');
).rejects.toThrow(
'edit at index 1: string not found in file: "not found"',
);
});
it("should throw if multiple occurrences without replace_all", async () => {
@ -219,7 +224,7 @@ describe("multiEditImpl", () => {
mockExtras,
),
).rejects.toThrow(
'Edit #1: String "test" appears 3 times in the file. Either provide a more specific string with surrounding context to make it unique, or use replace_all=true to replace all occurrences.',
`edit at index 0: String "test" appears 3 times in the file. ${FOUND_MULTIPLE_FIND_STRINGS_ERROR}`,
);
});
});

View file

@ -148,7 +148,7 @@ describe("singleFindAndReplaceImpl", () => {
await expect(
singleFindAndReplaceImpl(args, "tool-call-id", mockExtras),
).rejects.toThrow(`String not found in file: "not found"`);
).rejects.toThrow(`string not found in file: "not found"`);
});
it("should replace single occurrence", async () => {