| 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 | const SEARCH_AND_REPLACE_EXAMPLE_BLOCK = `------- SEARCH |
| 14 | [exact content to find] |
| 15 | ======= |
| 16 | [new content to replace with] |
| 17 | +++++++ REPLACE`; |
| 18 | |
| 19 | const SEARCH_AND_REPLACE_RULES = `Critical rules: |
| 20 | 1. SEARCH content must match the associated file section to find EXACTLY: |
| 21 | * Match character-for-character including whitespace, indentation, line endings |
| 22 | * Include all comments, docstrings, etc. |
| 23 | 2. SEARCH/REPLACE blocks will ONLY replace the first match occurrence. |
| 24 | * Including multiple unique SEARCH/REPLACE blocks if you need to make multiple changes. |
| 25 | * Include *just* enough lines in each SEARCH section to uniquely match each set of lines that need to change. |
| 26 | * When using multiple SEARCH/REPLACE blocks, list them in the order they appear in the file. |
| 27 | 3. **Order matters**: DIFFs in the array should be ordered from top to bottom of the file to ensure correct application. |
| 28 | 4. Keep SEARCH/REPLACE blocks concise: |
| 29 | * Break large SEARCH/REPLACE blocks into a series of smaller blocks that each change a small portion of the file. |
| 30 | * Include just the changing lines, and a few surrounding lines if needed for uniqueness. |
| 31 | * Do not include long runs of unchanging lines in SEARCH/REPLACE blocks. |
| 32 | * Each line must be complete. Never truncate lines mid-way through as this can cause matching failures. |
| 33 | 5. Splitting up tool calls: |
| 34 | * 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 |
| 35 | * If you need to make follow up edits or group your work into logical segments, it is okay to perform additional tool calls |
| 36 | * 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] |
| 37 | 6. Special operations: |
| 38 | * To move code: Use two SEARCH/REPLACE blocks (one to delete from original + one to insert at new location) |
| 39 | * To delete code: Use empty REPLACE section |
| 40 | 7. You should always read the file before trying to edit it, to ensure you know the up-to-date file contents`; |
| 41 | |
| 42 | const SEARCH_AND_REPLACE_DIFFS_DESCRIPTION = `An array of strings, each containing one or more SEARCH/REPLACE blocks following this exact format: |
| 43 | \`\`\` |
| 44 | ${SEARCH_AND_REPLACE_EXAMPLE_BLOCK} |
| 45 | \`\`\` |
| 46 | |
| 47 | ${SEARCH_AND_REPLACE_RULES} |
| 48 | |
| 49 | Usage: |
| 50 | \`\`\` |
| 51 | ${SEARCH_AND_REPLACE_EXAMPLE_BLOCK} |
| 52 | |
| 53 | ${SEARCH_AND_REPLACE_EXAMPLE_BLOCK} |
| 54 | \`\`\` |
| 55 | |
| 56 | Each string in the diffs array can contain multiple SEARCH/REPLACE blocks, and all will be applied sequentially in the order they appear.`; |
| 57 | |
| 58 | /** |
| 59 | * This tool is in an experimental state. |
| 60 | * 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 |
| 61 | */ |
| 62 | export const searchAndReplaceInFileTool: Tool = { |
| 63 | type: "function", |
| 64 | displayTitle: "Edit File", |
| 65 | wouldLikeTo: "edit {{{ filepath }}}", |
| 66 | isCurrently: "editing {{{ filepath }}}", |
| 67 | hasAlready: "edited {{{ filepath }}}", |
| 68 | group: BUILT_IN_GROUP_NAME, |
| 69 | readonly: false, |
| 70 | isInstant: false, |
| 71 | function: { |
| 72 | name: BuiltInToolNames.SearchAndReplaceInFile, |
| 73 | 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}`, |
| 74 | parameters: { |
| 75 | type: "object", |
| 76 | required: ["filepath", "diffs"], |
| 77 | properties: { |
| 78 | filepath: { |
| 79 | type: "string", |
| 80 | description: `The path of the file to modify, relative to the root of the workspace.`, |
| 81 | }, |
| 82 | diffs: { |
| 83 | type: "array", |
| 84 | items: { |
| 85 | type: "string", |
| 86 | }, |
| 87 | description: SEARCH_AND_REPLACE_DIFFS_DESCRIPTION, |
| 88 | }, |
| 89 | }, |
| 90 | }, |
| 91 | }, |
| 92 | systemMessageDescription: { |
| 93 | prefix: `To make targeted edits by replacing sections of content in an existing file, use the ${BuiltInToolNames.SearchAndReplaceInFile} tool with a filepath (relative to the root of the workspace) and a "diffs" argument containing an array of SEARCH/REPLACE blocks that define exact changes to specific parts of the file. |
| 94 | Each block should follow this format: |
| 95 | ${SEARCH_AND_REPLACE_EXAMPLE_BLOCK} |
| 96 | |
| 97 | ${SEARCH_AND_REPLACE_RULES} |
| 98 | |
| 99 | For example, you could respond with:`, |
| 100 | exampleArgs: [ |
| 101 | ["filepath", "path/to/file.ts"], |
| 102 | [ |
| 103 | "diffs", |
| 104 | `[ |
| 105 | "${SEARCH_AND_REPLACE_EXAMPLE_BLOCK}", |
| 106 | "${SEARCH_AND_REPLACE_EXAMPLE_BLOCK}" |
| 107 | ]`, |
| 108 | ], |
| 109 | ], |
| 110 | }, |
| 111 | defaultToolPolicy: "allowedWithPermission", |
| 112 | }; |