diff --git a/core/tools/implementations/readCurrentlyOpenFile.ts b/core/tools/implementations/readCurrentlyOpenFile.ts index a55aee2bd..bf3b2c713 100644 --- a/core/tools/implementations/readCurrentlyOpenFile.ts +++ b/core/tools/implementations/readCurrentlyOpenFile.ts @@ -1,28 +1,41 @@ import { getUriDescription } from "../../util/uri"; import { ToolImpl } from "."; +import { throwIfFileExceedsHalfOfContext } from "./readFileLimit"; export const readCurrentlyOpenFileImpl: ToolImpl = async (args, extras) => { const result = await extras.ide.getCurrentFile(); - if (!result) { - return []; - } + if (result) { + await throwIfFileExceedsHalfOfContext( + result.path, + result.contents, + extras.config.selectedModelByRole.chat, + ); - const { relativePathOrBasename, last2Parts, baseName } = getUriDescription( - result.path, - await extras.ide.getWorkspaceDirs(), - ); + const { relativePathOrBasename, last2Parts, baseName } = getUriDescription( + result.path, + await extras.ide.getWorkspaceDirs(), + ); - return [ - { - name: `Current file: ${baseName}`, - description: last2Parts, - content: `\`\`\`${relativePathOrBasename}\n${result.contents}\n\`\`\``, - uri: { - type: "file", - value: result.path, + return [ + { + name: `Current file: ${baseName}`, + description: last2Parts, + content: `\`\`\`${relativePathOrBasename}\n${result.contents}\n\`\`\``, + uri: { + type: "file", + value: result.path, + }, }, - }, - ]; + ]; + } else { + return [ + { + name: `No Current File`, + description: "", + content: "There are no files currently open.", + }, + ]; + } }; diff --git a/core/tools/implementations/readFile.ts b/core/tools/implementations/readFile.ts index c08713997..4736d0b4c 100644 --- a/core/tools/implementations/readFile.ts +++ b/core/tools/implementations/readFile.ts @@ -2,6 +2,7 @@ import { resolveRelativePathInDir } from "../../util/ideUtils"; import { getUriPathBasename } from "../../util/uri"; import { ToolImpl } from "."; +import { throwIfFileExceedsHalfOfContext } from "./readFileLimit"; export const readFileImpl: ToolImpl = async (args, extras) => { const firstUriMatch = await resolveRelativePathInDir( @@ -12,6 +13,13 @@ export const readFileImpl: ToolImpl = async (args, extras) => { 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), diff --git a/core/tools/implementations/readFileLimit.ts b/core/tools/implementations/readFileLimit.ts new file mode 100644 index 000000000..e2af14d56 --- /dev/null +++ b/core/tools/implementations/readFileLimit.ts @@ -0,0 +1,18 @@ +import { ILLM } from "../.."; +import { countTokensAsync } from "../../llm/countTokens"; + +export async function throwIfFileExceedsHalfOfContext( + filepath: string, + content: string, + model: ILLM | null, +) { + if (model) { + const tokens = await countTokensAsync(content, model.title); + const tokenLimit = model.contextLength / 2; + if (tokens > tokenLimit) { + throw new Error( + `File ${filepath} is too large (${tokens} tokens vs ${tokenLimit} token limit). Try another approach`, + ); + } + } +}