| 1 | import * as fs from "fs"; |
| 2 | |
| 3 | import { markFileAsRead } from "./edit.js"; |
| 4 | import { formatToolArgument } from "./formatters.js"; |
| 5 | import { Tool } from "./types.js"; |
| 6 | |
| 7 | export const readFileTool: Tool = { |
| 8 | name: "Read", |
| 9 | displayName: "Read", |
| 10 | description: "Read the contents of a file at the specified path", |
| 11 | parameters: { |
| 12 | filepath: { |
| 13 | type: "string", |
| 14 | description: "The path to the file to read", |
| 15 | required: true, |
| 16 | }, |
| 17 | }, |
| 18 | readonly: true, |
| 19 | isBuiltIn: true, |
| 20 | preprocess: async (args) => { |
| 21 | return { |
| 22 | args, |
| 23 | preview: [ |
| 24 | { |
| 25 | type: "text", |
| 26 | content: `Will read ${formatToolArgument(args.filepath)}`, |
| 27 | }, |
| 28 | ], |
| 29 | }; |
| 30 | }, |
| 31 | run: async (args: { filepath: string }): Promise<string> => { |
| 32 | try { |
| 33 | if (!fs.existsSync(args.filepath)) { |
| 34 | return `Error: File does not exist: ${args.filepath}`; |
| 35 | } |
| 36 | const content = fs.readFileSync(args.filepath, "utf-8"); |
| 37 | // Mark this file as read for the edit tool |
| 38 | markFileAsRead(args.filepath); |
| 39 | return `Content of ${args.filepath}:\n${content}`; |
| 40 | } catch (error) { |
| 41 | return `Error reading file: ${ |
| 42 | error instanceof Error ? error.message : String(error) |
| 43 | }`; |
| 44 | } |
| 45 | }, |
| 46 | }; |