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