diff options
Diffstat (limited to 'packages/cli/src/tools')
| -rw-r--r-- | packages/cli/src/tools/edit.tool.ts | 14 | ||||
| -rw-r--r-- | packages/cli/src/tools/glob.tool.ts | 13 | ||||
| -rw-r--r-- | packages/cli/src/tools/grep.tool.ts | 46 | ||||
| -rw-r--r-- | packages/cli/src/tools/read-file.tool.ts | 11 |
4 files changed, 33 insertions, 51 deletions
diff --git a/packages/cli/src/tools/edit.tool.ts b/packages/cli/src/tools/edit.tool.ts index a98b9861..de8ccd5b 100644 --- a/packages/cli/src/tools/edit.tool.ts +++ b/packages/cli/src/tools/edit.tool.ts @@ -11,6 +11,7 @@ import { import { makeRelative, shortenPath } from '../utils/paths.js'; import { ReadFileTool } from './read-file.tool.js'; import { WriteFileTool } from './write-file.tool.js'; +import { isNodeError } from '../utils/errors.js'; /** * Parameters for the Edit tool @@ -37,11 +38,6 @@ export interface EditToolParams { expected_replacements?: number; } -/** - * Result from the Edit tool - */ -export interface EditToolResult extends ToolResult {} - interface CalculatedEdit { currentContent: string | null; newContent: string; @@ -54,7 +50,7 @@ interface CalculatedEdit { * Implementation of the Edit tool that modifies files. * This tool maintains state for the "Always Edit" confirmation preference. */ -export class EditTool extends BaseTool<EditToolParams, EditToolResult> { +export class EditTool extends BaseTool<EditToolParams, ToolResult> { private shouldAlwaysEdit = false; private readonly rootDirectory: string; @@ -174,8 +170,8 @@ export class EditTool extends BaseTool<EditToolParams, EditToolResult> { try { currentContent = fs.readFileSync(params.file_path, 'utf8'); fileExists = true; - } catch (err: any) { - if (err.code !== 'ENOENT') { + } catch (err: unknown) { + if (!isNodeError(err) || err.code !== 'ENOENT') { throw err; } fileExists = false; @@ -300,7 +296,7 @@ export class EditTool extends BaseTool<EditToolParams, EditToolResult> { * @param params Parameters for the edit operation * @returns Result of the edit operation */ - async execute(params: EditToolParams): Promise<EditToolResult> { + async execute(params: EditToolParams): Promise<ToolResult> { if (!this.validateParams(params)) { return { llmContent: 'Invalid parameters for file edit operation', diff --git a/packages/cli/src/tools/glob.tool.ts b/packages/cli/src/tools/glob.tool.ts index 6c14b7d1..d9d82210 100644 --- a/packages/cli/src/tools/glob.tool.ts +++ b/packages/cli/src/tools/glob.tool.ts @@ -21,15 +21,10 @@ export interface GlobToolParams { } /** - * Result from the GlobTool - */ -export interface GlobToolResult extends ToolResult {} - -/** * Implementation of the GlobTool that finds files matching patterns, * sorted by modification time (newest first). */ -export class GlobTool extends BaseTool<GlobToolParams, GlobToolResult> { +export class GlobTool extends BaseTool<GlobToolParams, ToolResult> { /** * The root directory that this tool is grounded in. * All file operations will be restricted to this directory. @@ -125,9 +120,9 @@ export class GlobTool extends BaseTool<GlobToolParams, GlobToolResult> { if (!fs.statSync(searchDirAbsolute).isDirectory()) { return `Search path is not a directory: ${shortenPath(makeRelative(searchDirAbsolute, this.rootDirectory))} (absolute: ${searchDirAbsolute})`; } - } catch (e: any) { + } catch (e: unknown) { // Catch potential permission errors during sync checks - return `Error accessing search path: ${e.message}`; + return `Error accessing search path: ${e}`; } // Validate glob pattern (basic non-empty check) @@ -165,7 +160,7 @@ export class GlobTool extends BaseTool<GlobToolParams, GlobToolResult> { * @param params Parameters for the glob search * @returns Result of the glob search */ - async execute(params: GlobToolParams): Promise<GlobToolResult> { + async execute(params: GlobToolParams): Promise<ToolResult> { const validationError = this.invalidParams(params); if (validationError) { return { diff --git a/packages/cli/src/tools/grep.tool.ts b/packages/cli/src/tools/grep.tool.ts index 788fc76d..1c334a09 100644 --- a/packages/cli/src/tools/grep.tool.ts +++ b/packages/cli/src/tools/grep.tool.ts @@ -7,6 +7,7 @@ import fastGlob from 'fast-glob'; // Used for JS fallback file searching import { BaseTool, ToolResult } from './tools.js'; import { SchemaValidator } from '../utils/schemaValidator.js'; import { makeRelative, shortenPath } from '../utils/paths.js'; +import { getErrorMessage, isNodeError } from '../utils/errors.js'; // --- Interfaces (kept separate for clarity) --- @@ -39,17 +40,12 @@ interface GrepMatch { line: string; } -/** - * Result from the GrepTool - */ -export interface GrepToolResult extends ToolResult {} - // --- GrepTool Class --- /** * Implementation of the GrepTool that searches file contents using git grep, system grep, or JS fallback. */ -export class GrepTool extends BaseTool<GrepToolParams, GrepToolResult> { +export class GrepTool extends BaseTool<GrepToolParams, ToolResult> { private rootDirectory: string; /** @@ -114,12 +110,12 @@ export class GrepTool extends BaseTool<GrepToolParams, GrepToolResult> { if (!stats.isDirectory()) { throw new Error(`Path is not a directory: ${targetPath}`); } - } catch (err: any) { - if (err.code === 'ENOENT') { + } catch (error: unknown) { + if (isNodeError(error) && error.code !== 'ENOENT') { throw new Error(`Path does not exist: ${targetPath}`); } throw new Error( - `Failed to access path stats for ${targetPath}: ${err.message}`, + `Failed to access path stats for ${targetPath}: ${error}`, ); } @@ -164,7 +160,7 @@ export class GrepTool extends BaseTool<GrepToolParams, GrepToolResult> { * @param params Parameters for the grep search * @returns Result of the grep search */ - async execute(params: GrepToolParams): Promise<GrepToolResult> { + async execute(params: GrepToolParams): Promise<ToolResult> { const validationError = this.invalidParams(params); if (validationError) { console.error(`GrepTool Parameter Validation Failed: ${validationError}`); @@ -253,7 +249,7 @@ export class GrepTool extends BaseTool<GrepToolParams, GrepToolResult> { }); child.on('close', (code) => resolve(code === 0)); child.on('error', () => resolve(false)); - } catch (e) { + } catch { resolve(false); } }); @@ -277,10 +273,10 @@ export class GrepTool extends BaseTool<GrepToolParams, GrepToolResult> { return true; } return false; - } catch (err: any) { - if (err.code !== 'ENOENT') { + } catch (error: unknown) { + if (!isNodeError(error) || error.code !== 'ENOENT') { console.error( - `Error checking for .git in ${currentPath}: ${err.message}`, + `Error checking for .git in ${currentPath}: ${error}`, ); return false; } @@ -291,9 +287,9 @@ export class GrepTool extends BaseTool<GrepToolParams, GrepToolResult> { } currentPath = path.dirname(currentPath); } - } catch (err: any) { + } catch (error: unknown) { console.error( - `Error traversing directory structure upwards from ${dirPath}: ${err instanceof Error ? err.message : String(err)}`, + `Error traversing directory structure upwards from ${dirPath}: ${error instanceof Error ? error.message : error}`, ); } return false; @@ -446,9 +442,9 @@ export class GrepTool extends BaseTool<GrepToolParams, GrepToolResult> { }); }); return this.parseGrepOutput(output, absolutePath); - } catch (gitError: any) { + } catch (gitError: unknown) { console.error( - `GrepTool: git grep strategy failed: ${gitError.message}. Falling back...`, + `GrepTool: git grep strategy failed: ${getErrorMessage(gitError)}. Falling back...`, ); } } @@ -512,9 +508,9 @@ export class GrepTool extends BaseTool<GrepToolParams, GrepToolResult> { }); }); return this.parseGrepOutput(output, absolutePath); - } catch (grepError: any) { + } catch (grepError: unknown) { console.error( - `GrepTool: System grep strategy failed: ${grepError.message}. Falling back...`, + `GrepTool: System grep strategy failed: ${getErrorMessage(grepError)}. Falling back...`, ); } } @@ -559,19 +555,19 @@ export class GrepTool extends BaseTool<GrepToolParams, GrepToolResult> { }); } }); - } catch (readError: any) { - if (readError.code !== 'ENOENT') { + } catch (readError: unknown) { + if (!isNodeError(readError) || readError.code !== 'ENOENT') { console.error( - `GrepTool: Could not read or process file ${fileAbsolutePath}: ${readError.message}`, + `GrepTool: Could not read or process file ${fileAbsolutePath}: ${getErrorMessage(readError)}`, ); } } } return allMatches; - } catch (error: any) { + } catch (error: unknown) { console.error( - `GrepTool: Error during performGrepSearch (Strategy: ${strategyUsed}): ${error.message}`, + `GrepTool: Error during performGrepSearch (Strategy: ${strategyUsed}): ${getErrorMessage(error)}`, ); throw error; // Re-throw to be caught by the execute method's handler } diff --git a/packages/cli/src/tools/read-file.tool.ts b/packages/cli/src/tools/read-file.tool.ts index 98df7c12..9737e75d 100644 --- a/packages/cli/src/tools/read-file.tool.ts +++ b/packages/cli/src/tools/read-file.tool.ts @@ -25,16 +25,11 @@ export interface ReadFileToolParams { } /** - * Standardized result from the ReadFile tool - */ -export interface ReadFileToolResult extends ToolResult {} - -/** * Implementation of the ReadFile tool that reads files from the filesystem */ export class ReadFileTool extends BaseTool< ReadFileToolParams, - ReadFileToolResult + ToolResult > { static readonly Name: string = 'read_file'; @@ -166,7 +161,7 @@ export class ReadFileTool extends BaseTool< // If more than 30% are non-printable, likely binary return nonPrintableCount / bytesRead > 0.3; - } catch (error) { + } catch { return false; } } @@ -214,7 +209,7 @@ export class ReadFileTool extends BaseTool< * @param params Parameters for the file reading * @returns Result with file contents */ - async execute(params: ReadFileToolParams): Promise<ReadFileToolResult> { + async execute(params: ReadFileToolParams): Promise<ToolResult> { const validationError = this.invalidParams(params); const filePath = params.file_path; if (validationError) { |
