| 1 | import { Tool } from "../.."; |
| 2 | import { BUILT_IN_GROUP_NAME, BuiltInToolNames } from "../builtIn"; |
| 3 | |
| 4 | export interface SearchAndReplaceInFileArgs { |
| 5 | filepath: string; |
| 6 | diffs: string[]; |
| 7 | } |
| 8 | |
| 9 | // Currently unsupported and filtered out in `gui/src/redux/slices/sessionSlice.ts` |
| 10 | export const NO_PARALLEL_TOOL_CALLING_INSRUCTION = |
| 11 | "Note this tool CANNOT be called in parallel."; |
| 12 | |
| 13 | /** |
| 14 | * This tool is in an experimental state. |
| 15 | * 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 |
| 16 | */ |
| 17 | export const searchAndReplaceInFileTool: Tool = { |
| 18 | type: "function", |
| 19 | displayTitle: "Edit File", |
| 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.SearchAndReplaceInFile, |
| 28 | description: `Request to replace sections of content in an existing file using multiple 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. ${NO_PARALLEL_TOOL_CALLING_INSRUCTION}`, |
| 29 | parameters: { |
| 30 | type: "object", |
| 31 | required: ["filepath", "diffs"], |
| 32 | properties: { |
| 33 | filepath: { |
| 34 | type: "string", |
| 35 | description: |
| 36 | "The path of the file to modify, relative to the root of the workspace.", |
| 37 | }, |
| 38 | diffs: { |
| 39 | type: "array", |
| 40 | items: { |
| 41 | type: "string", |
| 42 | }, |
| 43 | description: `An array of strings, each containing one or more SEARCH/REPLACE blocks following this exact format: |
| 44 | \`\`\` |
| 45 | ------- SEARCH |
| 46 | [exact content to find] |
| 47 | ======= |
| 48 | [new content to replace with] |
| 49 | +++++++ REPLACE |
| 50 | \`\`\` |
| 51 | |
| 52 | Critical rules: |
| 53 | 1. SEARCH content must match the associated file section to find EXACTLY: |
| 54 | * Match character-for-character including whitespace, indentation, line endings |
| 55 | * Include all comments, docstrings, etc. |
| 56 | 2. SEARCH/REPLACE blocks will ONLY replace the first match occurrence. |
| 57 | * Including multiple unique SEARCH/REPLACE blocks if you need to make multiple changes. |
| 58 | * Include *just* enough lines in each SEARCH section to uniquely match each set of lines that need to change. |
| 59 | * When using multiple SEARCH/REPLACE blocks, list them in the order they appear in the file. |
| 60 | 3. **Order matters**: DIFFs in the array should be ordered from top to bottom of the file to ensure correct application. |
| 61 | 4. Keep SEARCH/REPLACE blocks concise: |
| 62 | * Break large SEARCH/REPLACE blocks into a series of smaller blocks that each change a small portion of the file. |
| 63 | * Include just the changing lines, and a few surrounding lines if needed for uniqueness. |
| 64 | * Do not include long runs of unchanging lines in SEARCH/REPLACE blocks. |
| 65 | * Each line must be complete. Never truncate lines mid-way through as this can cause matching failures. |
| 66 | 5. Splitting up tool calls: |
| 67 | * 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 |
| 68 | * If you need to make follow up edits or group your work into logical segments, it is okay to perform additional tool calls |
| 69 | * 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] |
| 70 | 6. Special operations: |
| 71 | * To move code: Use two SEARCH/REPLACE blocks (one to delete from original + one to insert at new location) |
| 72 | * To delete code: Use empty REPLACE section |
| 73 | |
| 74 | Usage: |
| 75 | \`\`\` |
| 76 | ------- SEARCH |
| 77 | [exact content to find] |
| 78 | ======= |
| 79 | [new content to replace with] |
| 80 | +++++++ REPLACE |
| 81 | |
| 82 | ------- SEARCH |
| 83 | [exact content to find] |
| 84 | ======= |
| 85 | [new content to replace with] |
| 86 | +++++++ REPLACE |
| 87 | \`\`\` |
| 88 | |
| 89 | Each string in the diffs array can contain multiple SEARCH/REPLACE blocks, and all will be applied sequentially in the order they appear.`, |
| 90 | }, |
| 91 | }, |
| 92 | }, |
| 93 | }, |
| 94 | }; |