40 lines
1.2 KiB
TypeScript
40 lines
1.2 KiB
TypeScript
import { resolveRelativePathInDir } from "../../util/ideUtils";
|
|
import { getUriPathBasename } from "../../util/uri";
|
|
|
|
import { ToolImpl } from ".";
|
|
import { throwIfFileIsSecurityConcern } from "../../indexing/ignore";
|
|
import { getStringArg } from "../parseArgs";
|
|
import { throwIfFileExceedsHalfOfContext } from "./readFileLimit";
|
|
import { ContinueError, ContinueErrorReason } from "../../util/errors";
|
|
|
|
export const readFileImpl: ToolImpl = async (args, extras) => {
|
|
const filepath = getStringArg(args, "filepath");
|
|
throwIfFileIsSecurityConcern(filepath);
|
|
|
|
const firstUriMatch = await resolveRelativePathInDir(filepath, extras.ide);
|
|
if (!firstUriMatch) {
|
|
throw new ContinueError(
|
|
ContinueErrorReason.FileNotFound,
|
|
`File "${filepath}" does not exist. You might want to check the path and try again.`,
|
|
);
|
|
}
|
|
const content = await extras.ide.readFile(firstUriMatch);
|
|
|
|
await throwIfFileExceedsHalfOfContext(
|
|
filepath,
|
|
content,
|
|
extras.config.selectedModelByRole.chat,
|
|
);
|
|
|
|
return [
|
|
{
|
|
name: getUriPathBasename(firstUriMatch),
|
|
description: filepath,
|
|
content,
|
|
uri: {
|
|
type: "file",
|
|
value: firstUriMatch,
|
|
},
|
|
},
|
|
];
|
|
};
|