| 1 | import { resolveRelativePathInDir } from "../../util/ideUtils"; |
| 2 | import { getUriPathBasename } from "../../util/uri"; |
| 3 | |
| 4 | import { ToolImpl } from "."; |
| 5 | import { throwIfFileExceedsHalfOfContext } from "./readFileLimit"; |
| 6 | |
| 7 | export const readFileImpl: ToolImpl = async (args, extras) => { |
| 8 | if (!args?.filepath) { |
| 9 | throw new Error( |
| 10 | "`filepath` argument is required to read a file, and cannot be empty", |
| 11 | ); |
| 12 | } |
| 13 | |
| 14 | const firstUriMatch = await resolveRelativePathInDir( |
| 15 | args.filepath, |
| 16 | extras.ide, |
| 17 | ); |
| 18 | if (!firstUriMatch) { |
| 19 | throw new Error(`Could not find file ${args.filepath}`); |
| 20 | } |
| 21 | const content = await extras.ide.readFile(firstUriMatch); |
| 22 | |
| 23 | await throwIfFileExceedsHalfOfContext( |
| 24 | args.filepath, |
| 25 | content, |
| 26 | extras.config.selectedModelByRole.chat, |
| 27 | ); |
| 28 | |
| 29 | return [ |
| 30 | { |
| 31 | name: getUriPathBasename(args.filepath), |
| 32 | description: args.filepath, |
| 33 | content, |
| 34 | uri: { |
| 35 | type: "file", |
| 36 | value: firstUriMatch, |
| 37 | }, |
| 38 | }, |
| 39 | ]; |
| 40 | }; |