feat: refactor multiedit

This commit is contained in:
Dallin Romney 2025-09-19 22:05:58 -07:00
parent 7716712f21
commit b28ce7094d
4 changed files with 56 additions and 131 deletions

View file

@ -1,4 +1,3 @@
import { EditOperation } from "../../tools/definitions/multiEdit";
import { ContinueError, ContinueErrorReason } from "../../util/errors";
export const FOUND_MULTIPLE_FIND_STRINGS_ERROR =
@ -87,32 +86,6 @@ 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) {
if (isCreating) {
throw new ContinueError(
ContinueErrorReason.MultiEditSubsequentEditsOnCreation,
"cannot make subsequent edits on a file you are creating",
);
} else {
for (let i = 1; i < edits.length; i++) {
if (edits[i].old_string === "") {
throw new ContinueError(
ContinueErrorReason.MultiEditEmptyOldStringNotFirst,
`edit at index ${i}: ${EMPTY_NON_FIRST_EDIT_MESSAGE}`,
);
}
}
}
}
return isCreating;
}
export function trimEmptyLines({
lines,
fromEnd,

View file

@ -50,12 +50,6 @@ CRITICAL REQUIREMENTS:
- Only use emojis if the user explicitly requests it. Avoid adding emojis to files unless asked
- Use replace_all for replacing and renaming all matches for a string across the file. This parameter is useful if you want to rename a variable, for instance
If you want to create a new file, use:
- A new file path, including new directory if needed
- First edit: empty old_string and the new file's contents as new_string
- Subsequent edits are not allowed - there is no need since you are creating
- ALWAYS prefer editing existing files in the codebase. NEVER write new files unless explicitly required
WARNINGS:
- If earlier edits affect the text that later edits are trying to find, files can become mangled
- The tool will fail if edits.old_string doesn't match the file contents exactly (including whitespace)

View file

@ -86,34 +86,19 @@ function validateEdits(edits: EditOperation[]): void {
}
}
function validateFileAccess(
resolvedPath: string,
isCreatingNewFile: boolean,
): void {
if (isCreatingNewFile) {
// For new file creation, check if parent directory exists
const parentDir = path.dirname(resolvedPath);
if (parentDir && !fs.existsSync(parentDir)) {
throw new ContinueError(
ContinueErrorReason.ParentDirectoryNotFound,
`Parent directory does not exist: ${parentDir}`,
);
}
} else {
// For existing files, check if file has been read
// Check with the original path first, then with absolute path
if (!readFilesSet.has(resolvedPath)) {
throw new ContinueError(
ContinueErrorReason.EditToolFileNotRead,
`You must use the ${readFileTool.name} tool to read ${resolvedPath} before editing it.`,
);
}
if (!fs.existsSync(resolvedPath)) {
throw new ContinueError(
ContinueErrorReason.FileNotFound,
`File ${resolvedPath} does not exist`,
);
}
function validateFileAccess(resolvedPath: string): void {
// Check if file has been read
if (!readFilesSet.has(resolvedPath)) {
throw new ContinueError(
ContinueErrorReason.EditToolFileNotRead,
`You must use the ${readFileTool.name} tool to read ${resolvedPath} before editing it.`,
);
}
if (!fs.existsSync(resolvedPath)) {
throw new ContinueError(
ContinueErrorReason.FileNotFound,
`File ${resolvedPath} does not exist`,
);
}
}
@ -121,13 +106,15 @@ function applyEdit(
content: string,
edit: EditOperation,
editIndex: number,
isFirstEditOfNewFile: boolean,
): string {
const { old_string, new_string, replace_all = false } = edit;
// For new file creation, the first edit can have empty old_string
if (isFirstEditOfNewFile && old_string === "") {
return new_string;
// Validate that old_string is not empty (no file creation allowed)
if (old_string === "") {
throw new ContinueError(
ContinueErrorReason.FindAndReplaceMissingOldString,
`Edit ${editIndex + 1}: old_string cannot be empty. File creation is not allowed.`,
);
}
// Check if old_string exists in current content
@ -184,12 +171,6 @@ CRITICAL REQUIREMENTS:
- Only use emojis if the user explicitly requests it. Avoid adding emojis to files unless asked.
- Use replace_all for replacing and renaming strings across the file. This parameter is useful if you want to rename a variable for instance.
If you want to create a new file, use:
- A new file path, including new directory if needed
- First edit: empty old_string and the new file's contents as new_string
- Subsequent edits: normal edit operations on the created content
- ALWAYS prefer editing existing files in the codebase. NEVER write new files unless explicitly required
WARNINGS:
- If earlier edits affect the text that later edits are trying to find, files can become mangled
- The tool will fail if edits.old_string doesn't match the file contents exactly (including whitespace)
@ -220,25 +201,17 @@ WARNINGS:
// Validate each edit operation
validateEdits(edits);
// Check if this is creating a new file (first edit has empty old_string)
const isCreatingNewFile = edits[0].old_string === "";
// Validate file access
validateFileAccess(resolved_path, isCreatingNewFile);
// Read current file content (or start with empty for new files)
let currentContent = "";
if (!isCreatingNewFile) {
currentContent = fs.readFileSync(resolved_path, "utf-8");
}
validateFileAccess(resolved_path);
// Read current file content
const currentContent = fs.readFileSync(resolved_path, "utf-8");
const originalContent = currentContent;
let newContent = currentContent;
// Apply all edits sequentially
for (let i = 0; i < edits.length; i++) {
const isFirstEditOfNewFile = i === 0 && isCreatingNewFile;
newContent = applyEdit(newContent, edits[i], i, isFirstEditOfNewFile);
newContent = applyEdit(newContent, edits[i], i);
}
// Generate diff for preview
@ -249,13 +222,12 @@ WARNINGS:
file_path: resolved_path,
newContent,
originalContent,
isCreatingNewFile,
editCount: edits.length,
},
preview: [
{
type: "text",
content: `Will apply ${edits.length} edit${edits.length === 1 ? "" : "s"} to ${isCreatingNewFile ? "create" : "modify"} ${resolved_path}:`,
content: `Will apply ${edits.length} edit${edits.length === 1 ? "" : "s"} to ${resolved_path}:`,
},
{
type: "diff",
@ -268,7 +240,6 @@ WARNINGS:
file_path: string;
newContent: string;
originalContent: string;
isCreatingNewFile: boolean;
editCount: number;
}) => {
try {
@ -299,8 +270,7 @@ WARNINGS:
args.file_path,
);
const action = args.isCreatingNewFile ? "created" : "edited";
return `Successfully ${action} ${args.file_path} with ${args.editCount} edit${args.editCount === 1 ? "" : "s"}\nDiff:\n${diff}`;
return `Successfully edited ${args.file_path} with ${args.editCount} edit${args.editCount === 1 ? "" : "s"}\nDiff:\n${diff}`;
} catch (error) {
if (error instanceof ContinueError) {
throw error;

View file

@ -1,13 +1,9 @@
import {
performFindAndReplace,
validateCreatingForMultiEdit,
validateSingleEdit,
} from "core/edit/searchAndReplace/findAndReplaceUtils";
import { ContinueError, ContinueErrorReason } from "core/util/errors";
import {
inferResolvedUriFromRelativePath,
resolveRelativePathInDir,
} from "core/util/ideUtils";
import { resolveRelativePathInDir } from "core/util/ideUtils";
import { v4 as uuid } from "uuid";
import { applyForEditTool } from "../../redux/thunks/handleApplyStateUpdate";
import { ClientToolImpl } from "./callClientTool";
@ -47,50 +43,42 @@ export const multiEditImpl: ClientToolImpl = async (
validateSingleEdit(edit.old_string, edit.new_string, i);
}
// Check if this is creating a new file (first edit has empty old_string)
const isCreatingNewFile = validateCreatingForMultiEdit(edits);
// Validate that no edit has empty old_string (file creation not allowed)
for (let i = 0; i < edits.length; i++) {
if (edits[i].old_string === "") {
throw new ContinueError(
ContinueErrorReason.FindAndReplaceMissingOldString,
`edit at index ${i}: old_string cannot be empty. File creation is not allowed.`,
);
}
}
const resolvedUri = await resolveRelativePathInDir(
filepath,
extras.ideMessenger.ide,
);
let newContent: string;
let fileUri: string;
if (isCreatingNewFile) {
if (resolvedUri) {
throw new ContinueError(
ContinueErrorReason.FileAlreadyExists,
`file ${filepath} already exists, cannot create new file`,
);
}
newContent = edits[0].new_string;
const dirs = await extras.ideMessenger.ide.getWorkspaceDirs();
fileUri = await inferResolvedUriFromRelativePath(
filepath,
extras.ideMessenger.ide,
dirs,
if (!resolvedUri) {
throw new ContinueError(
ContinueErrorReason.FileNotFound,
`file ${filepath} does not exist`,
);
}
let newContent =
editingFileContents ??
(await extras.ideMessenger.ide.readFile(resolvedUri));
const fileUri = resolvedUri;
for (let i = 0; i < edits.length; i++) {
const { old_string, new_string, replace_all } = edits[i];
newContent = performFindAndReplace(
newContent,
old_string,
new_string,
replace_all,
i,
);
} else {
if (!resolvedUri) {
throw new ContinueError(
ContinueErrorReason.FileNotFound,
`file ${filepath} does not exist. If you are trying to edit it, correct the filepath. If you are trying to create it, you must pass old_string=""`,
);
}
newContent =
editingFileContents ??
(await extras.ideMessenger.ide.readFile(resolvedUri));
fileUri = resolvedUri;
for (let i = 0; i < edits.length; i++) {
const { old_string, new_string, replace_all } = edits[i];
newContent = performFindAndReplace(
newContent,
old_string,
new_string,
replace_all,
i,
);
}
}
// Apply the changes to the file