| 1 | import { ToolImpl } from "."; |
| 2 | import { ContextItem } from "../.."; |
| 3 | import { formatGrepSearchResults } from "../../util/grepSearch"; |
| 4 | import { prepareQueryForRipgrep } from "../../util/regexValidator"; |
| 5 | import { getStringArg } from "../parseArgs"; |
| 6 | |
| 7 | const DEFAULT_GREP_SEARCH_RESULTS_LIMIT = 100; |
| 8 | const DEFAULT_GREP_SEARCH_CHAR_LIMIT = 7500; // ~1500 tokens, will keep truncation simply for now |
| 9 | |
| 10 | function splitGrepResultsByFile(content: string): ContextItem[] { |
| 11 | const matches = [...content.matchAll(/^\.\/([^\n]+)$/gm)]; |
| 12 | |
| 13 | const contextItems: ContextItem[] = []; |
| 14 | |
| 15 | for (let i = 0; i < matches.length; i++) { |
| 16 | const match = matches[i]; |
| 17 | const filepath = match[1]; |
| 18 | const startIndex = match.index!; |
| 19 | const endIndex = |
| 20 | i < matches.length - 1 ? matches[i + 1].index! : content.length; |
| 21 | |
| 22 | // Extract grepped content for this file |
| 23 | const fileContent = content |
| 24 | .substring(startIndex, endIndex) |
| 25 | .replace(/^\.\/[^\n]+\n/, "") // remove the line with file path |
| 26 | .trim(); |
| 27 | |
| 28 | if (fileContent) { |
| 29 | contextItems.push({ |
| 30 | name: `Search results in ${filepath}`, |
| 31 | description: `Grep search results from ${filepath}`, |
| 32 | content: fileContent, |
| 33 | uri: { type: "file", value: filepath }, |
| 34 | }); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | return contextItems; |
| 39 | } |
| 40 | |
| 41 | export const grepSearchImpl: ToolImpl = async (args, extras) => { |
| 42 | const rawQuery = getStringArg(args, "query"); |
| 43 | |
| 44 | const { query, warning } = prepareQueryForRipgrep(rawQuery); |
| 45 | |
| 46 | let results: string; |
| 47 | try { |
| 48 | results = await extras.ide.getSearchResults( |
| 49 | query, |
| 50 | DEFAULT_GREP_SEARCH_RESULTS_LIMIT, |
| 51 | ); |
| 52 | } catch (error) { |
| 53 | const errorMessage = error instanceof Error ? error.message : String(error); |
| 54 | |
| 55 | // Helpful error for common ripgrep exit code |
| 56 | if (errorMessage.includes("Process exited with code 2")) { |
| 57 | return [ |
| 58 | { |
| 59 | name: "Search error", |
| 60 | description: "The search query could not be processed", |
| 61 | 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.`, |
| 62 | }, |
| 63 | ]; |
| 64 | } |
| 65 | |
| 66 | throw error; |
| 67 | } |
| 68 | |
| 69 | const { formatted, numResults, truncated } = formatGrepSearchResults( |
| 70 | results, |
| 71 | DEFAULT_GREP_SEARCH_CHAR_LIMIT, |
| 72 | ); |
| 73 | |
| 74 | if (numResults === 0) { |
| 75 | return [ |
| 76 | { |
| 77 | name: "Search results", |
| 78 | description: "Results from grep search", |
| 79 | content: "The search returned no results.", |
| 80 | }, |
| 81 | ]; |
| 82 | } |
| 83 | |
| 84 | const truncationReasons: string[] = []; |
| 85 | if (numResults === DEFAULT_GREP_SEARCH_RESULTS_LIMIT) { |
| 86 | truncationReasons.push( |
| 87 | `the number of results exceeded ${DEFAULT_GREP_SEARCH_RESULTS_LIMIT}`, |
| 88 | ); |
| 89 | } |
| 90 | if (truncated) { |
| 91 | truncationReasons.push( |
| 92 | `the number of characters exceeded ${DEFAULT_GREP_SEARCH_CHAR_LIMIT}`, |
| 93 | ); |
| 94 | } |
| 95 | |
| 96 | let contextItems: ContextItem[]; |
| 97 | |
| 98 | const splitByFile: boolean = args?.splitByFile || false; |
| 99 | if (splitByFile) { |
| 100 | contextItems = splitGrepResultsByFile(formatted); |
| 101 | } else { |
| 102 | contextItems = [ |
| 103 | { |
| 104 | name: "Search results", |
| 105 | description: "Results from grep search", |
| 106 | content: formatted, |
| 107 | }, |
| 108 | ]; |
| 109 | } |
| 110 | |
| 111 | // Add warnings about query modifications or truncation |
| 112 | const warnings: string[] = []; |
| 113 | if (warning) { |
| 114 | warnings.push(warning); |
| 115 | } |
| 116 | if (truncationReasons.length > 0) { |
| 117 | warnings.push( |
| 118 | `Results were truncated because ${truncationReasons.join(" and ")}`, |
| 119 | ); |
| 120 | } |
| 121 | |
| 122 | if (truncationReasons.length > 0) { |
| 123 | contextItems.push({ |
| 124 | name: "Truncation warning", |
| 125 | description: "", |
| 126 | content: `The above search results were truncated because ${truncationReasons.join(" and ")}. If the results are not satisfactory, try refining your search query.`, |
| 127 | }); |
| 128 | } |
| 129 | return contextItems; |
| 130 | }; |