| 1 | import { Tool } from "../.."; |
| 2 | import { EDIT_CODE_INSTRUCTIONS } from "../../llm/defaultSystemMessages"; |
| 3 | import { BUILT_IN_GROUP_NAME, BuiltInToolNames } from "../builtIn"; |
| 4 | |
| 5 | export interface EditToolArgs { |
| 6 | filepath: string; |
| 7 | changes: string; |
| 8 | } |
| 9 | |
| 10 | export const NO_PARALLEL_TOOL_CALLING_INSTRUCTION = |
| 11 | "This tool CANNOT be called in parallel with other tools."; |
| 12 | |
| 13 | const CHANGES_DESCRIPTION = |
| 14 | "Any modifications to the file, showing only needed changes. Do NOT wrap this in a codeblock or write anything besides the code changes. In larger files, use brief language-appropriate placeholders for large unmodified sections, e.g. '// ... existing code ...'"; |
| 15 | |
| 16 | export const editFileTool: Tool = { |
| 17 | type: "function", |
| 18 | displayTitle: "Edit File", |
| 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.EditExistingFile, |
| 27 | description: `Use this tool to edit an existing file. If you don't know the contents of the file, read it first.\n${EDIT_CODE_INSTRUCTIONS}\n${NO_PARALLEL_TOOL_CALLING_INSTRUCTION}`, |
| 28 | parameters: { |
| 29 | type: "object", |
| 30 | required: ["filepath", "changes"], |
| 31 | properties: { |
| 32 | filepath: { |
| 33 | type: "string", |
| 34 | description: |
| 35 | "The path of the file to edit, relative to the root of the workspace.", |
| 36 | }, |
| 37 | changes: { |
| 38 | type: "string", |
| 39 | description: CHANGES_DESCRIPTION, |
| 40 | }, |
| 41 | }, |
| 42 | }, |
| 43 | }, |
| 44 | defaultToolPolicy: "allowedWithPermission", |
| 45 | systemMessageDescription: { |
| 46 | prefix: `To edit an EXISTING file, use the ${BuiltInToolNames.EditExistingFile} tool with |
| 47 | - filepath: the relative filepath to the file. |
| 48 | - changes: ${CHANGES_DESCRIPTION} |
| 49 | Only use this tool if you already know the contents of the file. Otherwise, use the ${BuiltInToolNames.ReadFile} or ${BuiltInToolNames.ReadCurrentlyOpenFile} tool to read it first. |
| 50 | For example:`, |
| 51 | exampleArgs: [ |
| 52 | ["filepath", "path/to/the_file.ts"], |
| 53 | [ |
| 54 | "changes", |
| 55 | "// ... existing code ...\nfunction subtract(a: number, b: number): number {\n return a - b;\n}\n// ... rest of code ...", |
| 56 | ], |
| 57 | ], |
| 58 | }, |
| 59 | }; |