|
1 | 1 | import { ToolImpl } from "."; |
| 2 | +import { ContextItem } from "../.."; |
2 | 3 | import { formatGrepSearchResults } from "../../util/grepSearch"; |
3 | 4 |
|
| 5 | +const DEFAULT_GREP_SEARCH_RESULTS_LIMIT = 100; |
| 6 | +const DEFAULT_GREP_SEARCH_CHAR_LIMIT = 5000; // ~1000 tokens, will keep truncation simply for now |
| 7 | + |
4 | 8 | export const grepSearchImpl: ToolImpl = async (args, extras) => { |
5 | | - const results = await extras.ide.getSearchResults(args.query); |
6 | | - return [ |
| 9 | + const results = await extras.ide.getSearchResults( |
| 10 | + args.query, |
| 11 | + DEFAULT_GREP_SEARCH_RESULTS_LIMIT, |
| 12 | + ); |
| 13 | + const { formatted, numResults, truncated } = formatGrepSearchResults( |
| 14 | + results, |
| 15 | + DEFAULT_GREP_SEARCH_CHAR_LIMIT, |
| 16 | + ); |
| 17 | + const truncationReasons: string[] = []; |
| 18 | + if (numResults === DEFAULT_GREP_SEARCH_RESULTS_LIMIT) { |
| 19 | + truncationReasons.push( |
| 20 | + `the number of results exceeded ${DEFAULT_GREP_SEARCH_RESULTS_LIMIT}`, |
| 21 | + ); |
| 22 | + } |
| 23 | + if (truncated) { |
| 24 | + truncationReasons.push( |
| 25 | + `the number of characters exceeded ${DEFAULT_GREP_SEARCH_CHAR_LIMIT}`, |
| 26 | + ); |
| 27 | + } |
| 28 | + |
| 29 | + const contextItems: ContextItem[] = [ |
7 | 30 | { |
8 | 31 | name: "Search results", |
9 | 32 | description: "Results from grep search", |
10 | | - content: formatGrepSearchResults(results), |
| 33 | + content: formatted, |
11 | 34 | }, |
12 | 35 | ]; |
| 36 | + if (truncationReasons.length > 0) { |
| 37 | + contextItems.push({ |
| 38 | + name: "Search truncation warning", |
| 39 | + description: "Informs the model that search results were truncated", |
| 40 | + content: `The above search results were truncated because ${truncationReasons.join(" and ")}. If the results are not satisfactory, try refining your search query.`, |
| 41 | + }); |
| 42 | + } |
| 43 | + return contextItems; |
13 | 44 | }; |
0 commit comments