(args, extras)
| 40 | } |
| 41 | |
| 42 | export const grepSearchImpl: ToolImpl = async (args, extras) => { |
| 43 | const rawQuery = getStringArg(args, "query"); |
| 44 | |
| 45 | const { query, warning } = prepareQueryForRipgrep(rawQuery); |
| 46 | |
| 47 | let results: string; |
| 48 | try { |
| 49 | results = await extras.ide.getSearchResults( |
| 50 | query, |
| 51 | DEFAULT_GREP_SEARCH_RESULTS_LIMIT, |
| 52 | ); |
| 53 | } catch (error) { |
| 54 | const errorMessage = error instanceof Error ? error.message : String(error); |
| 55 | |
| 56 | // Helpful error for common ripgrep exit code |
| 57 | if (errorMessage.includes("Process exited with code 2")) { |
| 58 | return [ |
| 59 | { |
| 60 | name: "Search error", |
| 61 | description: "The search query could not be processed", |
| 62 | content: `The search failed due to an invalid regex pattern.\n\nOriginal query: ${rawQuery}\nProcessed query: ${query}\n\nError: ${errorMessage}\n\nTip: If you're searching for literal text with special characters, the query was automatically escaped. If you need regex patterns, ensure they use proper regex syntax.`, |
| 63 | }, |
| 64 | ]; |
| 65 | } |
| 66 | |
| 67 | throw new ContinueError( |
| 68 | ContinueErrorReason.SearchExecutionFailed, |
| 69 | errorMessage, |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | const { formatted, numResults, truncated } = formatGrepSearchResults( |
| 74 | results, |
| 75 | DEFAULT_GREP_SEARCH_CHAR_LIMIT, |
| 76 | ); |
| 77 | |
| 78 | if (numResults === 0) { |
| 79 | return [ |
| 80 | { |
| 81 | name: "Search results", |
| 82 | description: "Results from grep search", |
| 83 | content: "The search returned no results.", |
| 84 | }, |
| 85 | ]; |
| 86 | } |
| 87 | |
| 88 | const truncationReasons: string[] = []; |
| 89 | if (numResults === DEFAULT_GREP_SEARCH_RESULTS_LIMIT) { |
| 90 | truncationReasons.push( |
| 91 | `the number of results exceeded ${DEFAULT_GREP_SEARCH_RESULTS_LIMIT}`, |
| 92 | ); |
| 93 | } |
| 94 | if (truncated) { |
| 95 | truncationReasons.push( |
| 96 | `the number of characters exceeded ${DEFAULT_GREP_SEARCH_CHAR_LIMIT}`, |
| 97 | ); |
| 98 | } |
| 99 |
no test coverage detected