Elffuss BraidHub

kikocis/continue public

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