| 1 | import { Tool } from "../.."; |
| 2 | import { BUILT_IN_GROUP_NAME, BuiltInToolNames } from "../builtIn"; |
| 3 | import { NO_PARALLEL_TOOL_CALLING_INSTRUCTION } from "./editFile"; |
| 4 | |
| 5 | export interface SingleFindAndReplaceArgs { |
| 6 | filepath: string; |
| 7 | old_string: string; |
| 8 | new_string: string; |
| 9 | replace_all?: boolean; |
| 10 | } |
| 11 | |
| 12 | export const singleFindAndReplaceTool: Tool = { |
| 13 | type: "function", |
| 14 | displayTitle: "Find and Replace", |
| 15 | wouldLikeTo: "edit {{{ filepath }}}", |
| 16 | isCurrently: "editing {{{ filepath }}}", |
| 17 | hasAlready: "edited {{{ filepath }}}", |
| 18 | group: BUILT_IN_GROUP_NAME, |
| 19 | readonly: false, |
| 20 | isInstant: false, |
| 21 | function: { |
| 22 | name: BuiltInToolNames.SingleFindAndReplace, |
| 23 | description: `Performs exact string replacements in a file. |
| 24 | |
| 25 | IMPORTANT: |
| 26 | - ALWAYS use the \`${BuiltInToolNames.ReadFile}\` tool just before making edits, to understand the file's up-to-date contents and context. The user can also edit the file while you are working with it. |
| 27 | - ${NO_PARALLEL_TOOL_CALLING_INSTRUCTION} |
| 28 | - When editing text from \`${BuiltInToolNames.ReadFile}\` tool output, ensure you preserve exact whitespace/indentation. |
| 29 | - Only use emojis if the user explicitly requests it. Avoid adding emojis to files unless asked. |
| 30 | - Use \`replace_all\` for replacing and renaming strings across the file. This parameter is useful if you want to rename a variable, for instance. |
| 31 | |
| 32 | WARNINGS: |
| 33 | - When not using \`replace_all\`, the edit will FAIL if \`old_string\` is not unique in the file. Either provide a larger string with more surrounding context to make it unique or use \`replace_all\` to change every instance of \`old_string\`. |
| 34 | - The edit will likely fail if you have not recently used the \`${BuiltInToolNames.ReadFile}\` tool to view up-to-date file contents.`, |
| 35 | parameters: { |
| 36 | type: "object", |
| 37 | required: ["filepath", "old_string", "new_string"], |
| 38 | properties: { |
| 39 | filepath: { |
| 40 | type: "string", |
| 41 | description: |
| 42 | "The path to the file to modify, relative to the root of the workspace", |
| 43 | }, |
| 44 | old_string: { |
| 45 | type: "string", |
| 46 | description: |
| 47 | "The text to replace - must be exact including whitespace/indentation", |
| 48 | }, |
| 49 | new_string: { |
| 50 | type: "string", |
| 51 | description: |
| 52 | "The text to replace it with (MUST be different from old_string)", |
| 53 | }, |
| 54 | replace_all: { |
| 55 | type: "boolean", |
| 56 | description: "Replace all occurrences of old_string (default false)", |
| 57 | }, |
| 58 | }, |
| 59 | }, |
| 60 | }, |
| 61 | systemMessageDescription: { |
| 62 | 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. |
| 63 | |
| 64 | For example, you could respond with:`, |
| 65 | exampleArgs: [ |
| 66 | ["filepath", "path/to/file.ts"], |
| 67 | ["old_string", "const oldVariable = 'value'"], |
| 68 | ["new_string", "const newVariable = 'updated'"], |
| 69 | ["replace_all", "false"], |
| 70 | ], |
| 71 | }, |
| 72 | defaultToolPolicy: "allowedWithPermission", |
| 73 | }; |