| 1 | import { Tool } from "../.."; |
| 2 | import { BUILT_IN_GROUP_NAME, BuiltInToolNames } from "../builtIn"; |
| 3 | |
| 4 | export interface EditOperation { |
| 5 | old_string: string; |
| 6 | new_string: string; |
| 7 | replace_all?: boolean; |
| 8 | } |
| 9 | |
| 10 | export interface MultiEditArgs { |
| 11 | filepath: string; |
| 12 | edits: EditOperation[]; |
| 13 | } |
| 14 | |
| 15 | export const multiEditTool: Tool = { |
| 16 | type: "function", |
| 17 | displayTitle: "Multi Edit", |
| 18 | wouldLikeTo: "edit {{{ filepath }}}", |
| 19 | isCurrently: "editing {{{ filepath }}}", |
| 20 | hasAlready: "edited {{{ filepath }}}", |
| 21 | group: BUILT_IN_GROUP_NAME, |
| 22 | readonly: false, |
| 23 | isInstant: false, |
| 24 | function: { |
| 25 | name: BuiltInToolNames.MultiEdit, |
| 26 | description: `This is a tool for making multiple edits to a single file in one operation. It |
| 27 | is built on top of the single find and replace tool and allows you to perform multiple |
| 28 | find-and-replace operations efficiently. Prefer this tool over the single find and replace tool |
| 29 | when you need to make multiple edits to the same file. |
| 30 | |
| 31 | Before using this tool: |
| 32 | |
| 33 | 1. Use the read_file tool to understand the file's contents and context |
| 34 | 2. Verify the directory path is correct |
| 35 | |
| 36 | To make multiple file edits, provide the following: |
| 37 | |
| 38 | 1. filepath: The path to the file to modify (relative to the root of the workspace) |
| 39 | 2. edits: An array of edit operations to perform, where each edit contains: |
| 40 | - old_string: The text to replace (must match the file contents exactly, including all whitespace and indentation) |
| 41 | - new_string: The edited text to replace the old_string |
| 42 | - replace_all: Replace all occurrences of old_string. This parameter is optional and defaults to false. |
| 43 | |
| 44 | IMPORTANT: |
| 45 | - All edits are applied in sequence, in the order they are provided |
| 46 | - Each edit operates on the result of the previous edit |
| 47 | - All edits must be valid for the operation to succeed - if any edit fails, |
| 48 | none will be applied |
| 49 | - This tool is ideal when you need to make several changes to different parts |
| 50 | of the same file |
| 51 | |
| 52 | CRITICAL REQUIREMENTS: |
| 53 | |
| 54 | 1. All edits follow the same requirements as the single find and replace tool |
| 55 | 2. The edits are atomic - either all succeed or none are applied |
| 56 | 3. Plan your edits carefully to avoid conflicts between sequential operations |
| 57 | |
| 58 | WARNING: |
| 59 | |
| 60 | - The tool will fail if edits.old_string doesn't match the file contents |
| 61 | exactly (including whitespace) |
| 62 | - The tool will fail if edits.old_string and edits.new_string are the same |
| 63 | - Since edits are applied in sequence, ensure that earlier edits don't affect |
| 64 | the text that later edits are trying to find |
| 65 | |
| 66 | When making edits: |
| 67 | |
| 68 | - Ensure all edits result in idiomatic, correct code |
| 69 | - Do not leave the code in a broken state |
| 70 | - Only use emojis if the user explicitly requests it. Avoid adding emojis to |
| 71 | files unless asked. |
| 72 | - Use replace_all for replacing and renaming strings across the file. This |
| 73 | parameter is useful if you want to rename a variable for instance. |
| 74 | |
| 75 | If you want to create a new file, use: |
| 76 | - A new file path |
| 77 | - First edit: empty old_string and the new file's contents as new_string |
| 78 | - Subsequent edits are not allowed - there is no need since you are creating`, |
| 79 | parameters: { |
| 80 | type: "object", |
| 81 | required: ["filepath", "edits"], |
| 82 | properties: { |
| 83 | filepath: { |
| 84 | type: "string", |
| 85 | description: |
| 86 | "The path to the file to modify, relative to the root of the workspace", |
| 87 | }, |
| 88 | edits: { |
| 89 | type: "array", |
| 90 | description: |
| 91 | "Array of edit operations to perform sequentially on the file", |
| 92 | items: { |
| 93 | type: "object", |
| 94 | required: ["old_string", "new_string"], |
| 95 | properties: { |
| 96 | old_string: { |
| 97 | type: "string", |
| 98 | description: "The text to replace", |
| 99 | }, |
| 100 | new_string: { |
| 101 | type: "string", |
| 102 | description: "The text to replace it with", |
| 103 | }, |
| 104 | replace_all: { |
| 105 | type: "boolean", |
| 106 | description: |
| 107 | "Replace all occurrences of old_string (default false)", |
| 108 | }, |
| 109 | }, |
| 110 | }, |
| 111 | }, |
| 112 | }, |
| 113 | }, |
| 114 | }, |
| 115 | systemMessageDescription: { |
| 116 | prefix: `To make multiple edits to a single file, use the ${BuiltInToolNames.MultiEdit} tool with a filepath (relative to the root of the workspace) and an array of edit operations. |
| 117 | |
| 118 | For example, you could respond with:`, |
| 119 | exampleArgs: [ |
| 120 | ["filepath", "path/to/file.ts"], |
| 121 | [ |
| 122 | "edits", |
| 123 | `[ |
| 124 | { "old_string": "const oldVar = 'value'", "new_string": "const newVar = 'updated'" }, |
| 125 | { "old_string": "oldFunction()", "new_string": "newFunction()", "replace_all": true } |
| 126 | ]`, |
| 127 | ], |
| 128 | ], |
| 129 | }, |
| 130 | defaultToolPolicy: "allowedWithPermission", |
| 131 | }; |