( readFile: (path: string) => Promise<string>, writeFile: (path: string, content: string) => Promise<void>, )
| 39 | * Replicates the functionality of anthropic.tools.textEditor_20250728 |
| 40 | */ |
| 41 | export function createGenericTextEditorTool( |
| 42 | readFile: (path: string) => Promise<string>, |
| 43 | writeFile: (path: string, content: string) => Promise<void>, |
| 44 | ) { |
| 45 | return tool({ |
| 46 | description: `Edit files using text editor commands. Supports viewing, creating, and modifying files. |
| 47 | |
| 48 | Commands: |
| 49 | - view: Read a file or a specific range of lines |
| 50 | - create: Create a new file with content |
| 51 | - str_replace: Replace a string in a file |
| 52 | |
| 53 | Use view_range as [start_line, end_line] to read specific sections.`, |
| 54 | inputSchema: z.object({ |
| 55 | command: z |
| 56 | .enum(["view", "create", "str_replace"]) |
| 57 | .describe("The editor command to execute"), |
| 58 | path: z.string().describe("Path to the file"), |
| 59 | file_text: z.string().optional().describe("Content for create command"), |
| 60 | old_str: z |
| 61 | .string() |
| 62 | .optional() |
| 63 | .describe("String to find for str_replace command"), |
| 64 | new_str: z |
| 65 | .string() |
| 66 | .optional() |
| 67 | .describe("String to replace with for str_replace command"), |
| 68 | insert_line: z |
| 69 | .number() |
| 70 | .optional() |
| 71 | .describe("Line number for insert operation (not commonly used)"), |
| 72 | view_range: z |
| 73 | .tuple([z.number(), z.number()]) |
| 74 | .optional() |
| 75 | .describe("Range of lines to view [start, end]"), |
| 76 | }), |
| 77 | execute: async ({ |
| 78 | command, |
| 79 | path, |
| 80 | file_text, |
| 81 | old_str, |
| 82 | new_str, |
| 83 | view_range, |
| 84 | }) => { |
| 85 | if (command === "view") { |
| 86 | const content = await readFile(path); |
| 87 | const lines = content.split("\n"); |
| 88 | |
| 89 | if (view_range) { |
| 90 | const [start, end] = view_range; |
| 91 | const selectedLines = lines.slice(start - 1, end); |
| 92 | return selectedLines.join("\n"); |
| 93 | } |
| 94 | |
| 95 | return content; |
| 96 | } |
| 97 | |
| 98 | if (command === "create") { |
no test coverage detected