use specific ConitnueError for tool calls
This commit is contained in:
parent
1d7da38a66
commit
77a8db7f63
10 changed files with 56 additions and 13 deletions
|
|
@ -3,6 +3,7 @@ import { inferResolvedUriFromRelativePath } from "../../util/ideUtils";
|
|||
import { ToolImpl } from ".";
|
||||
import { getCleanUriPath, getUriPathBasename } from "../../util/uri";
|
||||
import { getStringArg } from "../parseArgs";
|
||||
import { ContinueError, ContinueErrorReason } from "../../util/errors";
|
||||
|
||||
export const createNewFileImpl: ToolImpl = async (args, extras) => {
|
||||
const filepath = getStringArg(args, "filepath");
|
||||
|
|
@ -15,7 +16,8 @@ export const createNewFileImpl: ToolImpl = async (args, extras) => {
|
|||
if (resolvedFileUri) {
|
||||
const exists = await extras.ide.fileExists(resolvedFileUri);
|
||||
if (exists) {
|
||||
throw new Error(
|
||||
throw new ContinueError(
|
||||
ContinueErrorReason.FileAlreadyExists,
|
||||
`File ${filepath} already exists. Use the edit tool to edit this file`,
|
||||
);
|
||||
}
|
||||
|
|
@ -37,6 +39,9 @@ export const createNewFileImpl: ToolImpl = async (args, extras) => {
|
|||
},
|
||||
];
|
||||
} else {
|
||||
throw new Error("Failed to resolve path");
|
||||
throw new ContinueError(
|
||||
ContinueErrorReason.PathResolutionFailed,
|
||||
"Failed to resolve path",
|
||||
);
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ import { ContextItem } from "../..";
|
|||
import { formatGrepSearchResults } from "../../util/grepSearch";
|
||||
import { prepareQueryForRipgrep } from "../../util/regexValidator";
|
||||
import { getStringArg } from "../parseArgs";
|
||||
import { ContinueError, ContinueErrorReason } from "../../util/errors";
|
||||
|
||||
const DEFAULT_GREP_SEARCH_RESULTS_LIMIT = 100;
|
||||
const DEFAULT_GREP_SEARCH_CHAR_LIMIT = 7500; // ~1500 tokens, will keep truncation simply for now
|
||||
|
|
@ -63,7 +64,10 @@ export const grepSearchImpl: ToolImpl = async (args, extras) => {
|
|||
];
|
||||
}
|
||||
|
||||
throw error;
|
||||
throw new ContinueError(
|
||||
ContinueErrorReason.SearchExecutionFailed,
|
||||
errorMessage
|
||||
);
|
||||
}
|
||||
|
||||
const { formatted, numResults, truncated } = formatGrepSearchResults(
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ import ignore from "ignore";
|
|||
import { ToolImpl } from ".";
|
||||
import { walkDir } from "../../indexing/walkDir";
|
||||
import { resolveRelativePathInDir } from "../../util/ideUtils";
|
||||
import { ContinueError, ContinueErrorReason } from "../../util/errors";
|
||||
|
||||
export function resolveLsToolDirPath(dirPath: string | undefined) {
|
||||
if (!dirPath || dirPath === ".") {
|
||||
|
|
@ -20,7 +21,8 @@ export const lsToolImpl: ToolImpl = async (args, extras) => {
|
|||
const dirPath = resolveLsToolDirPath(args?.dirPath);
|
||||
const uri = await resolveRelativePathInDir(dirPath, extras.ide);
|
||||
if (!uri) {
|
||||
throw new Error(
|
||||
throw new ContinueError(
|
||||
ContinueErrorReason.DirectoryNotFound,
|
||||
`Directory ${args.dirPath} not found. Make sure to use forward-slash paths`,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,7 +13,8 @@ export const readFileImpl: ToolImpl = async (args, extras) => {
|
|||
|
||||
const firstUriMatch = await resolveRelativePathInDir(filepath, extras.ide);
|
||||
if (!firstUriMatch) {
|
||||
throw new Error(
|
||||
throw new ContinueError(
|
||||
ContinueErrorReason.FileNotFound,
|
||||
`File "${filepath}" does not exist. You might want to check the path and try again.`,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import { ILLM } from "../..";
|
||||
import { countTokensAsync } from "../../llm/countTokens";
|
||||
import { ContinueError, ContinueErrorReason } from "../../util/errors";
|
||||
|
||||
export async function throwIfFileExceedsHalfOfContext(
|
||||
filepath: string,
|
||||
|
|
@ -10,7 +11,8 @@ export async function throwIfFileExceedsHalfOfContext(
|
|||
const tokens = await countTokensAsync(content, model.title);
|
||||
const tokenLimit = model.contextLength / 2;
|
||||
if (tokens > tokenLimit) {
|
||||
throw new Error(
|
||||
throw new ContinueError(
|
||||
ContinueErrorReason.FileTooLarge,
|
||||
`File ${filepath} is too large (${tokens} tokens vs ${tokenLimit} token limit). Try another approach`,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import { getUriPathBasename } from "../../util/uri";
|
|||
import { ToolImpl } from ".";
|
||||
import { getNumberArg, getStringArg } from "../parseArgs";
|
||||
import { throwIfFileExceedsHalfOfContext } from "./readFileLimit";
|
||||
import { ContinueError, ContinueErrorReason } from "../../util/errors";
|
||||
|
||||
export const readFileRangeImpl: ToolImpl = async (args, extras) => {
|
||||
const filepath = getStringArg(args, "filepath");
|
||||
|
|
@ -12,24 +13,28 @@ export const readFileRangeImpl: ToolImpl = async (args, extras) => {
|
|||
|
||||
// Validate that line numbers are positive integers
|
||||
if (startLine < 1) {
|
||||
throw new Error(
|
||||
throw new ContinueError(
|
||||
ContinueErrorReason.InvalidLineNumber,
|
||||
"startLine must be 1 or greater. Negative line numbers are not supported - use the terminal tool with 'tail' command for reading from file end.",
|
||||
);
|
||||
}
|
||||
if (endLine < 1) {
|
||||
throw new Error(
|
||||
throw new ContinueError(
|
||||
ContinueErrorReason.InvalidLineNumber,
|
||||
"endLine must be 1 or greater. Negative line numbers are not supported - use the terminal tool with 'tail' command for reading from file end.",
|
||||
);
|
||||
}
|
||||
if (endLine < startLine) {
|
||||
throw new Error(
|
||||
throw new ContinueError(
|
||||
ContinueErrorReason.InvalidLineNumber,
|
||||
`endLine (${endLine}) must be greater than or equal to startLine (${startLine})`,
|
||||
);
|
||||
}
|
||||
|
||||
const firstUriMatch = await resolveRelativePathInDir(filepath, extras.ide);
|
||||
if (!firstUriMatch) {
|
||||
throw new Error(
|
||||
throw new ContinueError(
|
||||
ContinueErrorReason.FileNotFound,
|
||||
`File "${filepath}" does not exist. You might want to check the path and try again.`,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import { ToolImpl } from ".";
|
||||
import { getStringArg } from "../parseArgs";
|
||||
import { ContinueError, ContinueErrorReason } from "../../util/errors";
|
||||
|
||||
export const requestRuleImpl: ToolImpl = async (args, extras) => {
|
||||
const name = getStringArg(args, "name");
|
||||
|
|
@ -8,7 +9,10 @@ export const requestRuleImpl: ToolImpl = async (args, extras) => {
|
|||
const rule = extras.config.rules.find((r) => r.name === name);
|
||||
|
||||
if (!rule || !rule.sourceFile) {
|
||||
throw new Error(`Rule with name "${name}" not found or has no file path`);
|
||||
throw new ContinueError(
|
||||
ContinueErrorReason.RuleNotFound,
|
||||
`Rule with name "${name}" not found or has no file path`
|
||||
);
|
||||
}
|
||||
|
||||
return [
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ import iconv from "iconv-lite";
|
|||
import childProcess from "node:child_process";
|
||||
import os from "node:os";
|
||||
import util from "node:util";
|
||||
import { ContinueError, ContinueErrorReason } from "../../util/errors";
|
||||
// Automatically decode the buffer according to the platform to avoid garbled Chinese
|
||||
function getDecodedOutput(data: Buffer): string {
|
||||
if (process.platform === "win32") {
|
||||
|
|
@ -337,7 +338,8 @@ export const runTerminalCommandImpl: ToolImpl = async (args, extras) => {
|
|||
if (code === 0) {
|
||||
resolve({ stdout, stderr });
|
||||
} else {
|
||||
const error = new Error(
|
||||
const error = new ContinueError(
|
||||
ContinueErrorReason.CommandExecutionFailed,
|
||||
`Command failed with exit code ${code}`,
|
||||
);
|
||||
(error as any).stderr = stderr;
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ import { resolveRelativePathInDir } from "../../util/ideUtils";
|
|||
|
||||
import { ToolImpl } from ".";
|
||||
import { getStringArg } from "../parseArgs";
|
||||
import { ContinueError, ContinueErrorReason } from "../../util/errors";
|
||||
|
||||
export const viewSubdirectoryImpl: ToolImpl = async (args: any, extras) => {
|
||||
const directory_path = getStringArg(args, "directory_path");
|
||||
|
|
@ -10,7 +11,10 @@ export const viewSubdirectoryImpl: ToolImpl = async (args: any, extras) => {
|
|||
const uri = await resolveRelativePathInDir(directory_path, extras.ide);
|
||||
|
||||
if (!uri) {
|
||||
throw new Error(`Directory path "${directory_path}" does not exist.`);
|
||||
throw new ContinueError(
|
||||
ContinueErrorReason.DirectoryNotFound,
|
||||
`Directory path "${directory_path}" does not exist.`
|
||||
);
|
||||
}
|
||||
|
||||
const repoMap = await generateRepoMap(extras.llm, extras.ide, {
|
||||
|
|
|
|||
|
|
@ -47,6 +47,20 @@ export enum ContinueErrorReason {
|
|||
FileWriteError = "file_write_error",
|
||||
FileIsSecurityConcern = "file_is_security_concern",
|
||||
ParentDirectoryNotFound = "parent_directory_not_found",
|
||||
FileTooLarge = "file_too_large",
|
||||
PathResolutionFailed = "path_resolution_failed",
|
||||
InvalidLineNumber = "invalid_line_number",
|
||||
DirectoryNotFound = "directory_not_found",
|
||||
|
||||
// Terminal/Command execution
|
||||
CommandExecutionFailed = "command_execution_failed",
|
||||
CommandNotAvailableInRemote = "command_not_available_in_remote",
|
||||
|
||||
// Search
|
||||
SearchExecutionFailed = "search_execution_failed",
|
||||
|
||||
// Rules
|
||||
RuleNotFound = "rule_not_found",
|
||||
|
||||
// Other
|
||||
Unspecified = "unspecified", // I.e. a known error but no specific code for it
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue