| 1 | import * as fs from "fs"; |
| 2 | |
| 3 | import { telemetryService } from "../telemetry/telemetryService.js"; |
| 4 | import { |
| 5 | calculateLinesOfCodeDiff, |
| 6 | getLanguageFromFilePath, |
| 7 | } from "../telemetry/utils.js"; |
| 8 | |
| 9 | import { Tool } from "./types.js"; |
| 10 | import { generateDiff } from "./writeFile.js"; |
| 11 | |
| 12 | export interface EditArgs { |
| 13 | file_path: string; |
| 14 | old_string: string; |
| 15 | new_string: string; |
| 16 | replace_all?: boolean; |
| 17 | } |
| 18 | |
| 19 | // Track files that have been read in the current session |
| 20 | export const readFilesSet = new Set<string>(); |
| 21 | |
| 22 | export const editTool: Tool = { |
| 23 | name: "Edit", |
| 24 | displayName: "Edit", |
| 25 | readonly: false, |
| 26 | isBuiltIn: true, |
| 27 | description: `Performs exact string replacements in files. |
| 28 | |
| 29 | |
| 30 | Usage: |
| 31 | |
| 32 | - You must use your \`Read\` tool at least once in the conversation before |
| 33 | editing. This tool will error if you attempt an edit without reading the file. |
| 34 | |
| 35 | - When editing text from Read tool output, ensure you preserve the exact |
| 36 | indentation (tabs/spaces) as it appears AFTER the line number prefix. The line |
| 37 | number prefix format is: spaces + line number + tab. Everything after that tab |
| 38 | is the actual file content to match. Never include any part of the line number |
| 39 | prefix in the old_string or new_string. |
| 40 | |
| 41 | - ALWAYS prefer editing existing files in the codebase. NEVER write new files |
| 42 | unless explicitly required. |
| 43 | |
| 44 | - Only use emojis if the user explicitly requests it. Avoid adding emojis to |
| 45 | files unless asked. |
| 46 | |
| 47 | - The edit will FAIL if \`old_string\` is not unique in the file. Either provide |
| 48 | a larger string with more surrounding context to make it unique or use |
| 49 | \`replace_all\` to change every instance of \`old_string\`. |
| 50 | |
| 51 | - Use \`replace_all\` for replacing and renaming strings across the file. This |
| 52 | parameter is useful if you want to rename a variable for instance.`, |
| 53 | parameters: { |
| 54 | file_path: { |
| 55 | type: "string", |
| 56 | description: "The absolute path to the file to modify", |
| 57 | required: true, |
| 58 | }, |
| 59 | old_string: { |
| 60 | type: "string", |
| 61 | description: "The text to replace", |
| 62 | required: true, |
| 63 | }, |
| 64 | new_string: { |
| 65 | type: "string", |
| 66 | description: |
| 67 | "The text to replace it with (must be different from old_string)", |
| 68 | required: true, |
| 69 | }, |
| 70 | replace_all: { |
| 71 | type: "boolean", |
| 72 | description: "Replace all occurences of old_string (default false)", |
| 73 | required: false, |
| 74 | }, |
| 75 | }, |
| 76 | preprocess: async (args) => { |
| 77 | const { |
| 78 | file_path, |
| 79 | old_string, |
| 80 | new_string, |
| 81 | replace_all = false, |
| 82 | } = args as EditArgs; |
| 83 | |
| 84 | // Check if file has been read |
| 85 | if (!readFilesSet.has(file_path)) { |
| 86 | throw new Error( |
| 87 | `You must use the Read tool to read ${file_path} before editing it.`, |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | // Validate arguments |
| 92 | if (!file_path) { |
| 93 | throw new Error("file_path is required"); |
| 94 | } |
| 95 | if (!old_string) { |
| 96 | throw new Error("old_string is required"); |
| 97 | } |
| 98 | if (new_string === undefined) { |
| 99 | throw new Error("new_string is required"); |
| 100 | } |
| 101 | if (old_string === new_string) { |
| 102 | throw new Error("old_string and new_string must be different"); |
| 103 | } |
| 104 | |
| 105 | // Check if file exists |
| 106 | if (!fs.existsSync(file_path)) { |
| 107 | throw new Error(`File ${file_path} does not exist`); |
| 108 | } |
| 109 | |
| 110 | // Read current file content |
| 111 | const oldContent = fs.readFileSync(file_path, "utf-8"); |
| 112 | |
| 113 | // Check if old_string exists in the file |
| 114 | if (!oldContent.includes(old_string)) { |
| 115 | throw new Error(`String not found in file: ${old_string}`); |
| 116 | } |
| 117 | |
| 118 | let newContent: string; |
| 119 | |
| 120 | if (replace_all) { |
| 121 | // Replace all occurrences |
| 122 | newContent = oldContent.split(old_string).join(new_string); |
| 123 | } else { |
| 124 | // Replace only the first occurrence |
| 125 | const occurrences = oldContent.split(old_string).length - 1; |
| 126 | if (occurrences > 1) { |
| 127 | throw new Error( |
| 128 | `String "${old_string}" appears ${occurrences} 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.`, |
| 129 | ); |
| 130 | } |
| 131 | newContent = oldContent.replace(old_string, new_string); |
| 132 | } |
| 133 | |
| 134 | // Generate diff for preview |
| 135 | const diff = generateDiff(oldContent, newContent, file_path); |
| 136 | |
| 137 | return { |
| 138 | args: { |
| 139 | file_path, |
| 140 | newContent, |
| 141 | oldContent, |
| 142 | }, |
| 143 | preview: [ |
| 144 | { |
| 145 | type: "text", |
| 146 | content: "Will make the following changes:", |
| 147 | }, |
| 148 | { |
| 149 | type: "diff", |
| 150 | content: diff, |
| 151 | }, |
| 152 | ], |
| 153 | }; |
| 154 | }, |
| 155 | run: async (args: { |
| 156 | file_path: string; |
| 157 | newContent: string; |
| 158 | oldContent: string; |
| 159 | }) => { |
| 160 | try { |
| 161 | fs.writeFileSync(args.file_path, args.newContent, "utf-8"); |
| 162 | |
| 163 | // Get lines for telemetry |
| 164 | const { added, removed } = calculateLinesOfCodeDiff( |
| 165 | args.oldContent, |
| 166 | args.newContent, |
| 167 | ); |
| 168 | const language = getLanguageFromFilePath(args.file_path); |
| 169 | |
| 170 | if (added > 0) { |
| 171 | telemetryService.recordLinesOfCodeModified("added", added, language); |
| 172 | } |
| 173 | if (removed > 0) { |
| 174 | telemetryService.recordLinesOfCodeModified( |
| 175 | "removed", |
| 176 | removed, |
| 177 | language, |
| 178 | ); |
| 179 | } |
| 180 | |
| 181 | // Generate diff for result display |
| 182 | const diff = generateDiff( |
| 183 | args.oldContent, |
| 184 | args.newContent, |
| 185 | args.file_path, |
| 186 | ); |
| 187 | |
| 188 | return `Successfully edited ${args.file_path}\nDiff:\n${diff}`; |
| 189 | } catch (error) { |
| 190 | throw new Error( |
| 191 | `Error: failed to edit ${args.file_path}: ${ |
| 192 | error instanceof Error ? error.message : String(error) |
| 193 | }`, |
| 194 | ); |
| 195 | } |
| 196 | }, |
| 197 | }; |
| 198 | |
| 199 | // Function to mark a file as read (to be called from the read tool) |
| 200 | export function markFileAsRead(filePath: string) { |
| 201 | readFilesSet.add(filePath); |
| 202 | } |