continue-custom/core/tools/implementations/readFile.ts
2025-07-20 14:13:15 +02:00

40 lines
986 B
TypeScript

import { resolveRelativePathInDir } from "../../util/ideUtils";
import { getUriPathBasename } from "../../util/uri";
import { ToolImpl } from ".";
import { throwIfFileExceedsHalfOfContext } from "./readFileLimit";
export const readFileImpl: ToolImpl = async (args, extras) => {
if (!args?.filepath) {
throw new Error(
"`filepath` argument is required to read a file, and cannot be empty",
);
}
const firstUriMatch = await resolveRelativePathInDir(
args.filepath,
extras.ide,
);
if (!firstUriMatch) {
throw new Error(`Could not find file ${args.filepath}`);
}
const content = await extras.ide.readFile(firstUriMatch);
await throwIfFileExceedsHalfOfContext(
args.filepath,
content,
extras.config.selectedModelByRole.chat,
);
return [
{
name: getUriPathBasename(args.filepath),
description: args.filepath,
content,
uri: {
type: "file",
value: firstUriMatch,
},
},
];
};