| 1 | import { ToolImpl } from "."; |
| 2 | import { ContextItem } from "../.."; |
| 3 | import { formatGrepSearchResults } from "../../util/grepSearch"; |
| 4 | import { getStringArg } from "../parseArgs"; |
| 5 | |
| 6 | const DEFAULT_GREP_SEARCH_RESULTS_LIMIT = 100; |
| 7 | const DEFAULT_GREP_SEARCH_CHAR_LIMIT = 5000; // ~1000 tokens, will keep truncation simply for now |
| 8 | |
| 9 | function splitGrepResultsByFile(content: string): ContextItem[] { |
| 10 | const matches = [...content.matchAll(/^\.\/([^\n]+)$/gm)]; |
| 11 | |
| 12 | if (matches.length === 0) { |
| 13 | return []; |
| 14 | } |
| 15 | |
| 16 | const contextItems: ContextItem[] = []; |
| 17 | |
| 18 | for (let i = 0; i < matches.length; i++) { |
| 19 | const match = matches[i]; |
| 20 | const filepath = match[1]; |
| 21 | const startIndex = match.index!; |
| 22 | const endIndex = |
| 23 | i < matches.length - 1 ? matches[i + 1].index! : content.length; |
| 24 | |
| 25 | // Extract grepped content for this file |
| 26 | const fileContent = content |
| 27 | .substring(startIndex, endIndex) |
| 28 | .replace(/^\.\/[^\n]+\n/, "") // remove the line with file path |
| 29 | .trim(); |
| 30 | |
| 31 | if (fileContent) { |
| 32 | contextItems.push({ |
| 33 | name: `Search results in ${filepath}`, |
| 34 | description: `Grep search results from ${filepath}`, |
| 35 | content: fileContent, |
| 36 | uri: { type: "file", value: filepath }, |
| 37 | }); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | return contextItems; |
| 42 | } |
| 43 | |
| 44 | export const grepSearchImpl: ToolImpl = async (args, extras) => { |
| 45 | const query = getStringArg(args, "query"); |
| 46 | |
| 47 | const results = await extras.ide.getSearchResults( |
| 48 | query, |
| 49 | DEFAULT_GREP_SEARCH_RESULTS_LIMIT, |
| 50 | ); |
| 51 | const { formatted, numResults, truncated } = formatGrepSearchResults( |
| 52 | results, |
| 53 | DEFAULT_GREP_SEARCH_CHAR_LIMIT, |
| 54 | ); |
| 55 | const truncationReasons: string[] = []; |
| 56 | if (numResults === DEFAULT_GREP_SEARCH_RESULTS_LIMIT) { |
| 57 | truncationReasons.push( |
| 58 | `the number of results exceeded ${DEFAULT_GREP_SEARCH_RESULTS_LIMIT}`, |
| 59 | ); |
| 60 | } |
| 61 | if (truncated) { |
| 62 | truncationReasons.push( |
| 63 | `the number of characters exceeded ${DEFAULT_GREP_SEARCH_CHAR_LIMIT}`, |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | let contextItems: ContextItem[] = []; |
| 68 | |
| 69 | const splitByFile: boolean = args?.splitByFile || false; |
| 70 | if (splitByFile) { |
| 71 | contextItems = splitGrepResultsByFile(formatted); |
| 72 | } else { |
| 73 | contextItems = [ |
| 74 | { |
| 75 | name: "Search results", |
| 76 | description: "Results from grep search", |
| 77 | content: formatted, |
| 78 | }, |
| 79 | ]; |
| 80 | } |
| 81 | |
| 82 | if (truncationReasons.length > 0) { |
| 83 | contextItems.push({ |
| 84 | name: "Search truncation warning", |
| 85 | description: "Informs the model that search results were truncated", |
| 86 | content: `The above search results were truncated because ${truncationReasons.join(" and ")}. If the results are not satisfactory, try refining your search query.`, |
| 87 | }); |
| 88 | } |
| 89 | return contextItems; |
| 90 | }; |