1import { spawn, spawnSync } from "node:child_process";
2import { createInterface } from "node:readline";
3import { readFileSync, statSync } from "node:fs";
4import { relative, basename } from "node:path";
5import { Type } from "@sinclair/typebox";
6import type { AgentTool } from "@mariozechner/pi-ai";
7import { resolveToCwd } from "./path-utils.js";
8import {
9 DEFAULT_MAX_BYTES,
10 formatSize,
11 GREP_MAX_LINE_LENGTH,
12 truncateHead,
13 truncateLine,
14} from "../../util/truncate.js";
15import { ToolInputError } from "../../util/errors.js";
16
17const DEFAULT_LIMIT = 100;
18
19const GrepSchema = Type.Object({
20 pattern: Type.String({ description: "Search pattern (regex or literal string)" }),
21 path: Type.Optional(Type.String({ description: "Directory or file to search (default: current directory)" })),
22 glob: Type.Optional(Type.String({ description: "Filter files by glob pattern, e.g. '*.ts'" })),
23 ignoreCase: Type.Optional(Type.Boolean({ description: "Case-insensitive search (default: false)" })),
24 literal: Type.Optional(
25 Type.Boolean({ description: "Treat pattern as literal string instead of regex (default: false)" }),
26 ),
27 context: Type.Optional(Type.Number({ description: "Lines of context around each match (default: 0)" })),
28 limit: Type.Optional(Type.Number({ description: "Maximum matches to return (default: 100)" })),
29});
30
31export const createGrepTool = (workspacePath: string): AgentTool => {
32 const rgResult = spawnSync("which", ["rg"], { encoding: "utf-8" });
33 if (rgResult.status !== 0) {
34 throw new ToolInputError("ripgrep (rg) is not available");
35 }
36 const rgPath = rgResult.stdout.trim();
37
38 return {
39 name: "grep",
40 label: "Grep",
41 description: `Search file contents for a pattern using ripgrep. Returns up to ${DEFAULT_LIMIT} matches or ${DEFAULT_MAX_BYTES / 1024}KB (whichever is hit first). Lines truncated to ${GREP_MAX_LINE_LENGTH} chars. Respects .gitignore.`,
42 parameters: GrepSchema as any,
43 execute: async (_toolCallId: string, params: any) => {
44 const searchDir: string | undefined = params.path;
45 const searchPath = resolveToCwd(searchDir || ".", workspacePath);
46 let isDirectory = false;
47 try {
48 isDirectory = statSync(searchPath).isDirectory();
49 } catch {
50 throw new ToolInputError(`Path does not exist or is not accessible: ${searchDir || "."}`);
51 }
52
53 const effectiveLimit: number = Math.max(1, Math.floor(params.limit ?? DEFAULT_LIMIT));
54 const contextLines: number = Math.max(0, Math.floor(params.context ?? 0));
55
56 const args: string[] = ["--json", "--line-number", "--color=never", "--hidden"];
57 if (params.ignoreCase) args.push("--ignore-case");
58 if (params.literal) args.push("--fixed-strings");
59 if (params.glob) args.push("--glob", params.glob);
60 args.push(params.pattern, searchPath);
61
62 return new Promise((resolve, reject) => {
63 const child = spawn(rgPath, args, { stdio: ["ignore", "pipe", "pipe"] });
64
65 const rl = createInterface({ input: child.stdout! });
66 const fileCache = new Map<string, string[]>();
67
68 interface MatchEntry {
69 filePath: string;
70 lineNumber: number;
71 }
72
73 const matches: MatchEntry[] = [];
74 let stderrData = "";
75 let limitReached = false;
76
77 child.stderr!.on("data", (chunk: Buffer) => {
78 stderrData += chunk.toString();
79 });
80
81 rl.on("line", (line: string) => {
82 if (matches.length >= effectiveLimit) {
83 if (!limitReached) {
84 limitReached = true;
85 child.kill();
86 }
87 return;
88 }
89
90 try {
91 const parsed = JSON.parse(line);
92 if (parsed.type === "match") {
93 const filePath: string = parsed.data.path.text;
94 const lineNumber: number = parsed.data.line_number;
95 matches.push({ filePath, lineNumber });
96 }
97 } catch {
98 // ignore malformed JSON lines
99 }
100 });
101
102 child.on("close", (code: number | null) => {
103 if (code !== 0 && code !== 1 && !limitReached) {
104 reject(new Error(`rg exited with code ${code}: ${stderrData.trim()}`));
105 return;
106 }
107
108 if (matches.length === 0) {
109 resolve({
110 content: [{ type: "text", text: "No matches found" }],
111 details: { matches: 0 },
112 });
113 return;
114 }
115
116 const outputLines: string[] = [];
117 let anyLineTruncated = false;
118
119 for (const match of matches) {
120 let lines: string[];
121 if (fileCache.has(match.filePath)) {
122 lines = fileCache.get(match.filePath)!;
123 } else {
124 try {
125 lines = readFileSync(match.filePath, "utf-8").split(/\r?\n/);
126 fileCache.set(match.filePath, lines);
127 } catch {
128 lines = [];
129 }
130 }
131
132 const relPath = isDirectory
133 ? relative(searchPath, match.filePath)
134 : basename(match.filePath);
135
136 const startLine = Math.max(0, match.lineNumber - 1 - contextLines);
137 const endLine = Math.min(lines.length, match.lineNumber + contextLines);
138
139 for (let i = startLine; i < endLine; i++) {
140 const lineContent = lines[i] ?? "";
141 const { text: truncatedContent, wasTruncated } = truncateLine(lineContent);
142 if (wasTruncated) anyLineTruncated = true;
143
144 const lineNum = i + 1;
145 if (lineNum === match.lineNumber) {
146 outputLines.push(`${relPath}:${lineNum}: ${truncatedContent}`);
147 } else {
148 outputLines.push(`${relPath}-${lineNum}- ${truncatedContent}`);
149 }
150 }
151 }
152
153 const rawOutput = outputLines.join("\n");
154 const truncation = truncateHead(rawOutput, { maxLines: Number.MAX_SAFE_INTEGER });
155
156 const notices: string[] = [];
157 if (limitReached) {
158 notices.push(
159 `${effectiveLimit} matches limit reached. Use limit=${effectiveLimit * 2} for more, or refine pattern`,
160 );
161 }
162 if (truncation.truncated) {
163 notices.push(`${formatSize(DEFAULT_MAX_BYTES)} limit reached`);
164 }
165 if (anyLineTruncated) {
166 notices.push(
167 `Some lines truncated to ${GREP_MAX_LINE_LENGTH} chars. Use read tool to see full lines`,
168 );
169 }
170
171 let output = truncation.content;
172 if (notices.length > 0) {
173 output += `\n\n[${notices.join(". ")}]`;
174 }
175
176 resolve({
177 content: [{ type: "text", text: output }],
178 details: {
179 matches: matches.length,
180 ...(limitReached ? { matchLimitReached: effectiveLimit } : {}),
181 ...(truncation.truncated ? { truncation } : {}),
182 ...(anyLineTruncated ? { linesTruncated: true } : {}),
183 },
184 });
185 });
186 });
187 },
188 };
189};