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