diff options
| author | Taylor Mullen <[email protected]> | 2025-04-18 13:37:51 -0400 |
|---|---|---|
| committer | N. Taylor Mullen <[email protected]> | 2025-04-18 14:02:09 -0400 |
| commit | 7cd3b95317c4d9263e514f33589cb359766d463b (patch) | |
| tree | ccca1f6d7e67e91c9a3603dd5251fa2c4c577274 /packages/cli/src | |
| parent | 93fd6a9160d4654baf2f10269ce9689c553bb8cf (diff) | |
Fix linting errors in a number of core and tool files (partial)
- As part of this work I also started building out errors.ts which will be a cumulation of error helpers to better handle the challenging `catch (error: unknown)` requirement.
- More changes are to come, this is truly a partial change in order to not disrupt as many people as possible.
Part of https://b.corp.google.com/issues/411384603
Diffstat (limited to 'packages/cli/src')
| -rw-r--r-- | packages/cli/src/core/gemini-client.ts | 14 | ||||
| -rw-r--r-- | packages/cli/src/core/gemini-stream.ts | 4 | ||||
| -rw-r--r-- | packages/cli/src/core/history-updater.ts | 2 | ||||
| -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 | ||||
| -rw-r--r-- | packages/cli/src/ui/types.ts | 2 | ||||
| -rw-r--r-- | packages/cli/src/utils/errors.ts | 18 |
9 files changed, 62 insertions, 62 deletions
diff --git a/packages/cli/src/core/gemini-client.ts b/packages/cli/src/core/gemini-client.ts index 0b79a2ad..21cc7188 100644 --- a/packages/cli/src/core/gemini-client.ts +++ b/packages/cli/src/core/gemini-client.ts @@ -25,9 +25,9 @@ import { GeminiEventType, GeminiStream } from './gemini-stream.js'; type ToolExecutionOutcome = { callId: string; name: string; - args: Record<string, any>; + args: Record<string, never>; result?: ToolResult; - error?: any; + error?: Error; confirmationDetails?: ToolCallConfirmationDetails; }; @@ -126,7 +126,7 @@ ${folderStructure} let pendingToolCalls: Array<{ callId: string; name: string; - args: Record<string, any>; + args: Record<string, never>; }> = []; let yieldedTextInTurn = false; const chunksForDebug = []; @@ -148,7 +148,7 @@ ${folderStructure} call.id ?? `${call.name}-${Date.now()}-${Math.random().toString(16).slice(2)}`; const name = call.name || 'undefined_tool_name'; - const args = (call.args || {}) as Record<string, any>; + const args = (call.args || {}) as Record<string, never>; pendingToolCalls.push({ callId, name, args }); const evtValue: ToolCallEvent = { @@ -281,7 +281,7 @@ ${folderStructure} (executedTool: ToolExecutionOutcome): Part => { const { name, result, error } = executedTool; const output = { output: result?.llmContent }; - let toolOutcomePayload: any; + let toolOutcomePayload: Record<string, unknown>; if (error) { const errorMessage = error?.message || String(error); @@ -445,11 +445,11 @@ Respond *only* in JSON format according to the following schema. Do not include async generateJson( contents: Content[], schema: SchemaUnion, - ): Promise<any> { + ): Promise<Record<string, unknown>> { const model = getModel(); try { const result = await this.ai.models.generateContent({ - model: model, + model, config: { ...this.defaultHyperParameters, systemInstruction: CoreSystemPrompt, diff --git a/packages/cli/src/core/gemini-stream.ts b/packages/cli/src/core/gemini-stream.ts index 314a829d..70361f10 100644 --- a/packages/cli/src/core/gemini-stream.ts +++ b/packages/cli/src/core/gemini-stream.ts @@ -154,14 +154,14 @@ export const processGeminiStream = async ({ if (signal.aborted) { throw new Error('Request cancelled by user'); } - } catch (error: any) { + } catch (error: unknown) { if (renderTimeoutId) { // Ensure render loop stops on error clearTimeout(renderTimeoutId); renderTimeoutId = null; } // Delegate history update for error message - addErrorMessageToHistory(error, setHistory, getNextMessageId); + addErrorMessageToHistory(error as (Error | DOMException), setHistory, getNextMessageId); } finally { isStreamComplete = true; // Signal stream end for render loop completion if (renderTimeoutId) { diff --git a/packages/cli/src/core/history-updater.ts b/packages/cli/src/core/history-updater.ts index a97c99a1..465b7d49 100644 --- a/packages/cli/src/core/history-updater.ts +++ b/packages/cli/src/core/history-updater.ts @@ -178,7 +178,7 @@ export const handleToolCallChunk = ( * it to the last non-user message or creating a new entry. */ export const addErrorMessageToHistory = ( - error: any, + error: DOMException | Error, setHistory: React.Dispatch<React.SetStateAction<HistoryItem[]>>, getNextMessageId: () => number, ): void => { 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) { diff --git a/packages/cli/src/ui/types.ts b/packages/cli/src/ui/types.ts index 320f355b..78031a25 100644 --- a/packages/cli/src/ui/types.ts +++ b/packages/cli/src/ui/types.ts @@ -12,7 +12,7 @@ export interface ToolCallEvent { status: ToolCallStatus; callId: string; name: string; - args: Record<string, any>; + args: Record<string, never>; resultDisplay: ToolResultDisplay | undefined; confirmationDetails: ToolCallConfirmationDetails | undefined; } diff --git a/packages/cli/src/utils/errors.ts b/packages/cli/src/utils/errors.ts new file mode 100644 index 00000000..7708895a --- /dev/null +++ b/packages/cli/src/utils/errors.ts @@ -0,0 +1,18 @@ +export function isNodeError(error: unknown): error is NodeJS.ErrnoException { + return error instanceof Error && 'code' in error; +} + +export function getErrorMessage(error: unknown): string { + if (error instanceof Error) { + return error.message; + } else { + // Attempt to convert the non-Error value to a string for logging + try { + const errorMessage = String(error); + return errorMessage; + } catch { + // If String() itself fails (highly unlikely) + return 'Failed to get error details'; + } + } +}
\ No newline at end of file |
