| 1 | import { Tool } from "../.."; |
| 2 | import { BUILT_IN_GROUP_NAME, BuiltInToolNames } from "../builtIn"; |
| 3 | |
| 4 | export interface SearchAndReplaceInFileArgs { |
| 5 | filepath: string; |
| 6 | diff: string; |
| 7 | } |
| 8 | |
| 9 | /** |
| 10 | * This tool is in an experimental state. |
| 11 | * Our starting point is heavily inspired by Cline's `replace_in_file` tool: https://github.com/cline/cline/blob/2709ccefcddc616e89a70962f017bcbbca1f17bf/src/core/prompts/system.ts#L87-L121 |
| 12 | */ |
| 13 | export const searchAndReplaceInFileTool: Tool = { |
| 14 | type: "function", |
| 15 | displayTitle: "Edit File", |
| 16 | wouldLikeTo: "edit {{{ filepath }}}", |
| 17 | isCurrently: "editing {{{ filepath }}}", |
| 18 | hasAlready: "edited {{{ filepath }}}", |
| 19 | group: BUILT_IN_GROUP_NAME, |
| 20 | readonly: false, |
| 21 | isInstant: false, |
| 22 | function: { |
| 23 | name: BuiltInToolNames.SearchAndReplaceInFile, |
| 24 | description: |
| 25 | "Request to replace sections of content in an existing file using SEARCH/REPLACE blocks that define exact changes to specific parts of the file. This tool should be used when you need to make targeted changes to specific parts of a file.", |
| 26 | parameters: { |
| 27 | type: "object", |
| 28 | required: ["filepath", "diff"], |
| 29 | properties: { |
| 30 | filepath: { |
| 31 | type: "string", |
| 32 | description: |
| 33 | "The path of the file to modify, relative to the root of the workspace.", |
| 34 | }, |
| 35 | diff: { |
| 36 | type: "string", |
| 37 | description: `One or more SEARCH/REPLACE blocks following this exact format: |
| 38 | \`\`\` |
| 39 | ------- SEARCH |
| 40 | [exact content to find] |
| 41 | ======= |
| 42 | [new content to replace with] |
| 43 | +++++++ REPLACE |
| 44 | \`\`\` |
| 45 | |
| 46 | Critical rules: |
| 47 | 1. SEARCH content must match the associated file section to find EXACTLY: |
| 48 | * Match character-for-character including whitespace, indentation, line endings |
| 49 | * Include all comments, docstrings, etc. |
| 50 | 2. SEARCH/REPLACE blocks will ONLY replace the first match occurrence. |
| 51 | * Including multiple unique SEARCH/REPLACE blocks if you need to make multiple changes. |
| 52 | * Include *just* enough lines in each SEARCH section to uniquely match each set of lines that need to change. |
| 53 | * When using multiple SEARCH/REPLACE blocks, list them in the order they appear in the file. |
| 54 | 3. Keep SEARCH/REPLACE blocks concise: |
| 55 | * Break large SEARCH/REPLACE blocks into a series of smaller blocks that each change a small portion of the file. |
| 56 | * Include just the changing lines, and a few surrounding lines if needed for uniqueness. |
| 57 | * Do not include long runs of unchanging lines in SEARCH/REPLACE blocks. |
| 58 | * Each line must be complete. Never truncate lines mid-way through as this can cause matching failures. |
| 59 | 4. Splitting up tool calls: |
| 60 | * When making multiple closely related edits to the same file, you should try to use multiple SEARCH/REPLACE blocks in a single tool call, rather than making multiple separate tool calls |
| 61 | * If you need to make follow up edits or group your work into logical segments, it is okay to perform additional tool calls |
| 62 | * DO NOT make back-to-back tool calls. Instead interleave with brief explanation of what each will do. For example, instead of [explanation, tool call, tool call] you should do [explanation, tool call, explanation, tool call] |
| 63 | 5. Special operations: |
| 64 | * To move code: Use two SEARCH/REPLACE blocks (one to delete from original + one to insert at new location) |
| 65 | * To delete code: Use empty REPLACE section |
| 66 | |
| 67 | Usage: |
| 68 | \`\`\` |
| 69 | ------- SEARCH |
| 70 | [exact content to find] |
| 71 | ======= |
| 72 | [new content to replace with] |
| 73 | +++++++ REPLACE |
| 74 | |
| 75 | ------- SEARCH |
| 76 | [exact content to find] |
| 77 | ======= |
| 78 | [new content to replace with] |
| 79 | +++++++ REPLACE |
| 80 | \`\`\` |
| 81 | `, |
| 82 | }, |
| 83 | }, |
| 84 | }, |
| 85 | }, |
| 86 | }; |