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