diff options
| author | Taylor Mullen <[email protected]> | 2025-05-18 23:13:57 -0700 |
|---|---|---|
| committer | N. Taylor Mullen <[email protected]> | 2025-05-18 23:19:15 -0700 |
| commit | 3d74a7061e2e64d7d8f3850e913acf31b493bcf9 (patch) | |
| tree | 27314f5526383186b71a139e1c0fa4c5a396fc80 /packages/server/src/tools/grep.ts | |
| parent | cd1dc7ec5918f3e3867ba1101b6228c815b7bc31 (diff) | |
fix(server): Use console.debug in GrepTool for less verbose logging
- Replaces `console.warn` and `console.error` calls with `console.debug` in `packages/server/src/tools/grep.ts`. This change reduces noise for the user, as `warn` and `error` messages are
displayed directly, while `debug` messages are not.
- Adds a comprehensive test suite for the GrepTool (`packages/server/src/tools/grep.test.ts`) to ensure its functionality remains robust after these changes and to cover various usage
scenarios.
- Improves error message consistency in `GrepTool`'s parameter validation and execution.
Fixes https://b.corp.google.com/issues/418648813
Diffstat (limited to 'packages/server/src/tools/grep.ts')
| -rw-r--r-- | packages/server/src/tools/grep.ts | 34 |
1 files changed, 17 insertions, 17 deletions
diff --git a/packages/server/src/tools/grep.ts b/packages/server/src/tools/grep.ts index 54391832..acdf0bc8 100644 --- a/packages/server/src/tools/grep.ts +++ b/packages/server/src/tools/grep.ts @@ -147,13 +147,13 @@ export class GrepTool extends BaseTool<GrepToolParams, ToolResult> { try { new RegExp(params.pattern); } catch (error) { - return `Invalid regular expression pattern provided: ${params.pattern}. Error: ${error instanceof Error ? error.message : String(error)}`; + return `Invalid regular expression pattern provided: ${params.pattern}. Error: ${getErrorMessage(error)}`; } try { this.resolveAndValidatePath(params.path); } catch (error) { - return error instanceof Error ? error.message : String(error); + return getErrorMessage(error); } return null; // Parameters are valid @@ -172,12 +172,9 @@ export class GrepTool extends BaseTool<GrepToolParams, ToolResult> { ): Promise<ToolResult> { const validationError = this.validateToolParams(params); if (validationError) { - console.error( - `GrepLogic Parameter Validation Failed: ${validationError}`, - ); return { llmContent: `Error: Invalid parameters provided. Reason: ${validationError}`, - returnDisplay: `Error: Failed to execute tool.`, + returnDisplay: `Model provided invalid parameters. Error: ${validationError}`, }; } @@ -231,8 +228,7 @@ export class GrepTool extends BaseTool<GrepToolParams, ToolResult> { }; } catch (error) { console.error(`Error during GrepLogic execution: ${error}`); - const errorMessage = - error instanceof Error ? error.message : String(error); + const errorMessage = getErrorMessage(error); return { llmContent: `Error during grep search operation: ${errorMessage}`, returnDisplay: `Error: ${errorMessage}`, @@ -286,7 +282,7 @@ export class GrepTool extends BaseTool<GrepToolParams, ToolResult> { return false; } catch (error: unknown) { if (!isNodeError(error) || error.code !== 'ENOENT') { - console.error( + console.debug( `Error checking for .git in ${currentPath}: ${error}`, ); return false; @@ -299,7 +295,7 @@ export class GrepTool extends BaseTool<GrepToolParams, ToolResult> { currentPath = path.dirname(currentPath); } } catch (error: unknown) { - console.error( + console.debug( `Error traversing directory structure upwards from ${dirPath}: ${getErrorMessage(error)}`, ); } @@ -366,9 +362,13 @@ export class GrepTool extends BaseTool<GrepToolParams, ToolResult> { description += ` in ${params.include}`; } if (params.path) { - const searchDir = params.path || this.rootDirectory; - const relativePath = makeRelative(searchDir, this.rootDirectory); - description += ` within ${shortenPath(relativePath || './')}`; + const resolvedPath = path.resolve(this.rootDirectory, params.path); + if (resolvedPath === this.rootDirectory || params.path === '.') { + description += ` within ./`; + } else { + const relativePath = makeRelative(resolvedPath, this.rootDirectory); + description += ` within ${shortenPath(relativePath)}`; + } } return description; } @@ -433,7 +433,7 @@ export class GrepTool extends BaseTool<GrepToolParams, ToolResult> { }); return this.parseGrepOutput(output, absolutePath); } catch (gitError: unknown) { - console.warn( + console.debug( `GrepLogic: git grep failed: ${getErrorMessage(gitError)}. Falling back...`, ); } @@ -496,14 +496,14 @@ export class GrepTool extends BaseTool<GrepToolParams, ToolResult> { }); return this.parseGrepOutput(output, absolutePath); } catch (grepError: unknown) { - console.warn( + console.debug( `GrepLogic: System grep failed: ${getErrorMessage(grepError)}. Falling back...`, ); } } // --- Strategy 3: Pure JavaScript Fallback --- - console.warn( + console.debug( 'GrepLogic: Falling back to JavaScript grep implementation.', ); strategyUsed = 'javascript fallback'; @@ -548,7 +548,7 @@ export class GrepTool extends BaseTool<GrepToolParams, ToolResult> { } catch (readError: unknown) { // Ignore errors like permission denied or file gone during read if (!isNodeError(readError) || readError.code !== 'ENOENT') { - console.warn( + console.debug( `GrepLogic: Could not read/process ${fileAbsolutePath}: ${getErrorMessage(readError)}`, ); } |
