| 1 | import { resolveRelativePathInDir } from "../../util/ideUtils"; |
| 2 | import { getUriPathBasename } from "../../util/uri"; |
| 3 | |
| 4 | import { ToolImpl } from "."; |
| 5 | import { throwIfFileIsSecurityConcern } from "../../indexing/ignore"; |
| 6 | import { getStringArg } from "../parseArgs"; |
| 7 | import { throwIfFileExceedsHalfOfContext } from "./readFileLimit"; |
| 8 | |
| 9 | export const readFileImpl: ToolImpl = async (args, extras) => { |
| 10 | const filepath = getStringArg(args, "filepath"); |
| 11 | throwIfFileIsSecurityConcern(filepath); |
| 12 | |
| 13 | const firstUriMatch = await resolveRelativePathInDir(filepath, extras.ide); |
| 14 | if (!firstUriMatch) { |
| 15 | throw new Error( |
| 16 | `File "${filepath}" does not exist. You might want to check the path and try again.`, |
| 17 | ); |
| 18 | } |
| 19 | const content = await extras.ide.readFile(firstUriMatch); |
| 20 | |
| 21 | await throwIfFileExceedsHalfOfContext( |
| 22 | filepath, |
| 23 | content, |
| 24 | extras.config.selectedModelByRole.chat, |
| 25 | ); |
| 26 | |
| 27 | return [ |
| 28 | { |
| 29 | name: getUriPathBasename(firstUriMatch), |
| 30 | description: filepath, |
| 31 | content, |
| 32 | uri: { |
| 33 | type: "file", |
| 34 | value: firstUriMatch, |
| 35 | }, |
| 36 | }, |
| 37 | ]; |
| 38 | }; |