Browse Source

fix: Improve CLI tools with better error handling and options (#122)

- ast-grep: Log background init errors instead of silently swallowing them
- grep: Add caseSensitive, wholeWord, fixedStrings options to tool definition

These fixes improve debuggability and expose more functionality to users.
mister-test 2 months ago
parent
commit
c5c9683115
2 changed files with 22 additions and 2 deletions
  1. 3 1
      src/tools/ast-grep/cli.ts
  2. 19 1
      src/tools/grep/tools.ts

+ 3 - 1
src/tools/ast-grep/cli.ts

@@ -56,7 +56,9 @@ export async function getAstGrepPath(): Promise<string | null> {
 export function startBackgroundInit(): void {
   if (!initPromise) {
     initPromise = getAstGrepPath();
-    initPromise.catch(() => {});
+    initPromise.catch((err) => {
+      console.warn('[ast-grep] Background initialization failed:', err?.message ?? err);
+    });
   }
 }
 

+ 19 - 1
src/tools/grep/tools.ts

@@ -7,7 +7,7 @@ export const grep: ToolDefinition = tool({
     'Fast content search tool with safety limits (60s timeout, 10MB output). ' +
     'Searches file contents using regular expressions. ' +
     'Supports full regex syntax (eg. "log.*Error", "function\\s+\\w+", etc.). ' +
-    'Filter files by pattern with the include parameter (eg. "*.js", "*.{ts,tsx}"). ' +
+    'Filter files by pattern with the include parameter (e.g. "*.js", "*.{ts,tsx}"). ' +
     'Returns file paths with matches sorted by modification time.',
   args: {
     pattern: tool.schema
@@ -25,6 +25,21 @@ export const grep: ToolDefinition = tool({
       .describe(
         'The directory to search in. Defaults to the current working directory.',
       ),
+    caseSensitive: tool.schema
+      .boolean()
+      .optional()
+      .default(false)
+      .describe('Perform case-sensitive search (default: false)'),
+    wholeWord: tool.schema
+      .boolean()
+      .optional()
+      .default(false)
+      .describe('Match whole words only (default: false)'),
+    fixedStrings: tool.schema
+      .boolean()
+      .optional()
+      .default(false)
+      .describe('Treat pattern as literal string (default: false)'),
   },
   execute: async (args) => {
     try {
@@ -36,6 +51,9 @@ export const grep: ToolDefinition = tool({
         paths,
         globs,
         context: 0,
+        caseSensitive: args.caseSensitive ?? false,
+        wholeWord: args.wholeWord ?? false,
+        fixedStrings: args.fixedStrings ?? false,
       });
 
       return formatGrepResult(result);