| 1 | import { Tool } from "../.."; |
| 2 | import { BUILT_IN_GROUP_NAME, BuiltInToolNames } from "../builtIn"; |
| 3 | |
| 4 | export interface SingleFindAndReplaceArgs { |
| 5 | filepath: string; |
| 6 | old_string: string; |
| 7 | new_string: string; |
| 8 | replace_all?: boolean; |
| 9 | } |
| 10 | |
| 11 | export const singleFindAndReplaceTool: Tool = { |
| 12 | type: "function", |
| 13 | displayTitle: "Find and Replace", |
| 14 | wouldLikeTo: "find and replace in {{{ filepath }}}", |
| 15 | isCurrently: "finding and replacing in {{{ filepath }}}", |
| 16 | hasAlready: "found and replaced in {{{ filepath }}}", |
| 17 | group: BUILT_IN_GROUP_NAME, |
| 18 | readonly: false, |
| 19 | isInstant: false, |
| 20 | function: { |
| 21 | name: BuiltInToolNames.SingleFindAndReplace, |
| 22 | description: `Performs exact string replacements in files. |
| 23 | |
| 24 | Usage: |
| 25 | |
| 26 | - You must use your \`read_file\` tool at least once in the conversation before |
| 27 | editing. This tool will error if you attempt an edit without reading the file. |
| 28 | |
| 29 | - When editing text from read_file tool output, ensure you preserve the exact |
| 30 | indentation (tabs/spaces) as it appears AFTER the line number prefix. The line |
| 31 | number prefix format is: spaces + line number + tab. Everything after that tab |
| 32 | is the actual file content to match. Never include any part of the line number |
| 33 | prefix in the old_string or new_string. |
| 34 | |
| 35 | - ALWAYS prefer editing existing files in the codebase. NEVER write new files |
| 36 | unless explicitly required. |
| 37 | |
| 38 | - Only use emojis if the user explicitly requests it. Avoid adding emojis to |
| 39 | files unless asked. |
| 40 | |
| 41 | - The edit will FAIL if \`old_string\` is not unique in the file. Either provide |
| 42 | a larger string with more surrounding context to make it unique or use |
| 43 | \`replace_all\` to change every instance of \`old_string\`. |
| 44 | |
| 45 | - Use \`replace_all\` for replacing and renaming strings across the file. This |
| 46 | parameter is useful if you want to rename a variable for instance.`, |
| 47 | parameters: { |
| 48 | type: "object", |
| 49 | required: ["filepath", "old_string", "new_string"], |
| 50 | properties: { |
| 51 | filepath: { |
| 52 | type: "string", |
| 53 | description: |
| 54 | "The path to the file to modify, relative to the root of the workspace", |
| 55 | }, |
| 56 | old_string: { |
| 57 | type: "string", |
| 58 | description: "The text to replace", |
| 59 | }, |
| 60 | new_string: { |
| 61 | type: "string", |
| 62 | description: |
| 63 | "The text to replace it with (must be different from old_string)", |
| 64 | }, |
| 65 | replace_all: { |
| 66 | type: "boolean", |
| 67 | description: "Replace all occurrences of old_string (default false)", |
| 68 | }, |
| 69 | }, |
| 70 | }, |
| 71 | }, |
| 72 | systemMessageDescription: { |
| 73 | prefix: `To perform exact string replacements in files, use the ${BuiltInToolNames.SingleFindAndReplace} tool with a filepath (relative to the root of the workspace) and the strings to find and replace. |
| 74 | |
| 75 | For example, you could respond with:`, |
| 76 | exampleArgs: [ |
| 77 | ["filepath", "path/to/file.ts"], |
| 78 | ["old_string", "const oldVariable = 'value'"], |
| 79 | ["new_string", "const newVariable = 'updated'"], |
| 80 | ["replace_all", "false"], |
| 81 | ], |
| 82 | }, |
| 83 | defaultToolPolicy: "allowedWithPermission", |
| 84 | }; |