| 1 | import * as child_process from "child_process"; |
| 2 | import * as fs from "fs"; |
| 3 | import * as util from "util"; |
| 4 | |
| 5 | import { Tool } from "./types.js"; |
| 6 | |
| 7 | const execPromise = util.promisify(child_process.exec); |
| 8 | |
| 9 | // Default maximum number of results to display |
| 10 | const DEFAULT_MAX_RESULTS = 100; |
| 11 | |
| 12 | export const searchCodeTool: Tool = { |
| 13 | name: "Search", |
| 14 | displayName: "Search", |
| 15 | description: "Search the codebase using ripgrep (rg) for a specific pattern", |
| 16 | parameters: { |
| 17 | type: "object", |
| 18 | required: ["pattern"], |
| 19 | properties: { |
| 20 | pattern: { |
| 21 | type: "string", |
| 22 | description: "The search pattern to look for", |
| 23 | }, |
| 24 | path: { |
| 25 | type: "string", |
| 26 | description: "The path to search in (defaults to current directory)", |
| 27 | }, |
| 28 | file_pattern: { |
| 29 | type: "string", |
| 30 | description: "Optional file pattern to filter results (e.g., '*.ts')", |
| 31 | }, |
| 32 | }, |
| 33 | }, |
| 34 | readonly: true, |
| 35 | isBuiltIn: true, |
| 36 | preprocess: async (args) => { |
| 37 | const truncatedPattern = |
| 38 | args.pattern.length > 50 |
| 39 | ? args.pattern.substring(0, 50) + "..." |
| 40 | : args.pattern; |
| 41 | return { |
| 42 | args, |
| 43 | preview: [ |
| 44 | { |
| 45 | type: "text", |
| 46 | content: `Will search for: "${truncatedPattern}"`, |
| 47 | }, |
| 48 | ], |
| 49 | }; |
| 50 | }, |
| 51 | run: async (args: { |
| 52 | pattern: string; |
| 53 | path?: string; |
| 54 | file_pattern?: string; |
| 55 | }): Promise<string> => { |
| 56 | try { |
| 57 | const searchPath = args.path || process.cwd(); |
| 58 | if (!fs.existsSync(searchPath)) { |
| 59 | return `Error: Path does not exist: ${searchPath}`; |
| 60 | } |
| 61 | |
| 62 | let command = `rg --line-number --with-filename --color never "${args.pattern}"`; |
| 63 | |
| 64 | if (args.file_pattern) { |
| 65 | command += ` -g "${args.file_pattern}"`; |
| 66 | } |
| 67 | |
| 68 | command += ` "${searchPath}"`; |
| 69 | try { |
| 70 | const { stdout, stderr } = await execPromise(command); |
| 71 | |
| 72 | if (stderr) { |
| 73 | return `Warning during search: ${stderr}\n\n${stdout}`; |
| 74 | } |
| 75 | |
| 76 | if (!stdout.trim()) { |
| 77 | return `No matches found for pattern "${args.pattern}"${ |
| 78 | args.file_pattern ? ` in files matching "${args.file_pattern}"` : "" |
| 79 | }.`; |
| 80 | } |
| 81 | |
| 82 | // Split the results into lines and limit the number of results |
| 83 | const lines = stdout.split("\n"); |
| 84 | const truncated = lines.length > DEFAULT_MAX_RESULTS; |
| 85 | const limitedLines = lines.slice(0, DEFAULT_MAX_RESULTS); |
| 86 | const resultText = limitedLines.join("\n"); |
| 87 | |
| 88 | const truncationMessage = truncated |
| 89 | ? `\n\n[Results truncated: showing ${DEFAULT_MAX_RESULTS} of ${lines.length} matches]` |
| 90 | : ""; |
| 91 | |
| 92 | return `Search results for pattern "${args.pattern}"${ |
| 93 | args.file_pattern ? ` in files matching "${args.file_pattern}"` : "" |
| 94 | }:\n\n${resultText}${truncationMessage}`; |
| 95 | } catch (error: any) { |
| 96 | if (error.code === 1) { |
| 97 | return `No matches found for pattern "${args.pattern}"${ |
| 98 | args.file_pattern ? ` in files matching "${args.file_pattern}"` : "" |
| 99 | }.`; |
| 100 | } |
| 101 | if (error instanceof Error) { |
| 102 | if (error.message.includes("command not found")) { |
| 103 | return `Error: ripgrep is not installed.`; |
| 104 | } |
| 105 | } |
| 106 | return `Error executing ripgrep: ${ |
| 107 | error instanceof Error ? error.message : String(error) |
| 108 | }`; |
| 109 | } |
| 110 | } catch (error) { |
| 111 | return `Error searching code: ${ |
| 112 | error instanceof Error ? error.message : String(error) |
| 113 | }`; |
| 114 | } |
| 115 | }, |
| 116 | }; |