-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathripGrep.ts
More file actions
404 lines (358 loc) · 12.6 KB
/
ripGrep.ts
File metadata and controls
404 lines (358 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import fs from 'node:fs';
import path from 'node:path';
import type { ToolInvocation, ToolResult } from './tools.js';
import { BaseDeclarativeTool, BaseToolInvocation, Kind } from './tools.js';
import { ToolNames } from './tool-names.js';
import { resolveAndValidatePath } from '../utils/paths.js';
import { getErrorMessage } from '../utils/errors.js';
import type { Config } from '../config/config.js';
import { runRipgrep } from '../utils/ripgrepUtils.js';
import { SchemaValidator } from '../utils/schemaValidator.js';
import type { FileFilteringOptions } from '../config/constants.js';
import { DEFAULT_FILE_FILTERING_OPTIONS } from '../config/constants.js';
import { createDebugLogger } from '../utils/debugLogger.js';
import type { PermissionDecision } from '../permissions/types.js';
const debugLogger = createDebugLogger('RIPGREP');
/**
* Parameters for the GrepTool (Simplified)
*/
export interface RipGrepToolParams {
/**
* The regular expression pattern to search for in file contents
*/
pattern: string;
/**
* The directory to search in (optional, defaults to current directory relative to root)
*/
path?: string;
/**
* Glob pattern to filter files (e.g. "*.js", "*.{ts,tsx}")
*/
glob?: string;
/**
* Maximum number of matching lines to return (optional, shows all if not specified)
*/
limit?: number;
}
class GrepToolInvocation extends BaseToolInvocation<
RipGrepToolParams,
ToolResult
> {
constructor(
private readonly config: Config,
params: RipGrepToolParams,
) {
super(params);
}
/**
* Returns 'ask' for paths outside the workspace, so that external grep
* searches require user confirmation.
*/
override async getDefaultPermission(): Promise<PermissionDecision> {
if (!this.params.path) {
return 'allow'; // Default workspace directory
}
const workspaceContext = this.config.getWorkspaceContext();
const resolvedPath = path.resolve(
this.config.getTargetDir(),
this.params.path,
);
if (workspaceContext.isPathWithinWorkspace(resolvedPath)) {
return 'allow';
}
return 'ask';
}
async execute(signal: AbortSignal): Promise<ToolResult> {
try {
// Determine which paths to search
const searchPaths: string[] = [];
let searchDirDisplay: string;
if (this.params.path) {
// User specified a path — search only that path
const searchDirAbs = resolveAndValidatePath(
this.config,
this.params.path,
{ allowFiles: true, allowExternalPaths: true },
);
searchPaths.push(searchDirAbs);
searchDirDisplay = this.params.path;
} else {
// No path specified — search all workspace directories
const workspaceDirs = this.config
.getWorkspaceContext()
.getDirectories();
searchPaths.push(...workspaceDirs);
searchDirDisplay = '.';
}
// Get raw ripgrep output
const rawOutput = await this.performRipgrepSearch({
pattern: this.params.pattern,
paths: searchPaths,
glob: this.params.glob,
signal,
});
// Build search description
const searchLocationDescription = this.params.path
? `in path "${searchDirDisplay}"`
: searchPaths.length > 1
? `across ${searchPaths.length} workspace directories`
: `in the workspace directory`;
const filterDescription = this.params.glob
? ` (filter: "${this.params.glob}")`
: '';
// Check if we have any matches
if (!rawOutput.trim()) {
const noMatchMsg = `No matches found for pattern "${this.params.pattern}" ${searchLocationDescription}${filterDescription}.`;
return { llmContent: noMatchMsg, returnDisplay: `No matches found` };
}
// Split into lines and count total matches
let allLines = rawOutput.split('\n').filter((line) => line.trim());
// Deduplicate lines from potentially overlapping workspace directories.
// ripgrep reports the same file twice when given paths like /a and /a/sub.
if (searchPaths.length > 1) {
const seen = new Set<string>();
allLines = allLines.filter((line) => {
// ripgrep output format: filepath:linenum:content
const firstColon = line.indexOf(':');
if (firstColon !== -1) {
const secondColon = line.indexOf(':', firstColon + 1);
if (secondColon !== -1) {
const key = line.substring(0, secondColon);
if (seen.has(key)) return false;
seen.add(key);
}
}
return true;
});
}
const totalMatches = allLines.length;
const matchTerm = totalMatches === 1 ? 'match' : 'matches';
// Build header early to calculate available space
const header = `Found ${totalMatches} ${matchTerm} for pattern "${this.params.pattern}" ${searchLocationDescription}${filterDescription}:\n---\n`;
const charLimit = this.config.getTruncateToolOutputThreshold();
const lineLimit = Math.min(
this.config.getTruncateToolOutputLines(),
this.params.limit ?? Number.POSITIVE_INFINITY,
);
// Apply line limit first (if specified)
let truncatedByLineLimit = false;
let linesToInclude = allLines;
if (allLines.length > lineLimit) {
linesToInclude = allLines.slice(0, lineLimit);
truncatedByLineLimit = true;
}
// Build output and track how many lines we include, respecting character limit
let grepOutput = '';
let truncatedByCharLimit = false;
let includedLines = 0;
if (Number.isFinite(charLimit)) {
const parts: string[] = [];
let currentLength = 0;
for (const line of linesToInclude) {
const sep = includedLines > 0 ? 1 : 0;
includedLines++;
const projectedLength = currentLength + line.length + sep;
if (projectedLength <= charLimit) {
parts.push(line);
currentLength = projectedLength;
} else {
const remaining = Math.max(charLimit - currentLength - sep, 10);
parts.push(line.slice(0, remaining) + '...');
truncatedByCharLimit = true;
break;
}
}
grepOutput = parts.join('\n');
} else {
grepOutput = linesToInclude.join('\n');
includedLines = linesToInclude.length;
}
// Build result
let llmContent = header + grepOutput;
// Add truncation notice if needed
if (truncatedByLineLimit || truncatedByCharLimit) {
const omittedMatches = totalMatches - includedLines;
llmContent += `\n---\n[${omittedMatches} ${omittedMatches === 1 ? 'line' : 'lines'} truncated] ...`;
}
// Build display message (show real count, not truncated)
let displayMessage = `Found ${totalMatches} ${matchTerm}`;
if (truncatedByLineLimit || truncatedByCharLimit) {
displayMessage += ` (truncated)`;
}
return {
llmContent: llmContent.trim(),
returnDisplay: displayMessage,
};
} catch (error) {
debugLogger.error('Error during ripgrep search operation:', error);
const errorMessage = getErrorMessage(error);
return {
llmContent: `Error during grep search operation: ${errorMessage}`,
returnDisplay: `Error: ${errorMessage}`,
};
}
}
private async performRipgrepSearch(options: {
pattern: string;
paths: string[]; // Can be files or directories
glob?: string;
signal: AbortSignal;
}): Promise<string> {
const { pattern, paths, glob } = options;
const rgArgs: string[] = [
'--line-number',
'--no-heading',
'--with-filename',
'--ignore-case',
'--regexp',
pattern,
];
// Add file exclusions from .gitignore and .qwenignore
const filteringOptions = this.getFileFilteringOptions();
if (!filteringOptions.respectGitIgnore) {
rgArgs.push('--no-ignore-vcs');
}
if (filteringOptions.respectQwenIgnore) {
// Load .qwenignore from each workspace directory, not just the primary one
const seenIgnoreFiles = new Set<string>();
for (const searchPath of paths) {
const dir =
fs.existsSync(searchPath) && fs.statSync(searchPath).isDirectory()
? searchPath
: path.dirname(searchPath);
const qwenIgnorePath = path.join(dir, '.qwenignore');
if (
!seenIgnoreFiles.has(qwenIgnorePath) &&
fs.existsSync(qwenIgnorePath)
) {
rgArgs.push('--ignore-file', qwenIgnorePath);
seenIgnoreFiles.add(qwenIgnorePath);
}
}
}
// Add glob pattern if provided
if (glob) {
rgArgs.push('--glob', glob);
}
rgArgs.push('--threads', '4');
// Pass all search paths to ripgrep (it supports multiple paths natively)
rgArgs.push(...paths);
const result = await runRipgrep(rgArgs, options.signal);
if (result.error && !result.stdout) {
throw result.error;
}
return result.stdout;
}
private getFileFilteringOptions(): FileFilteringOptions {
const options = this.config.getFileFilteringOptions?.();
return {
respectGitIgnore:
options?.respectGitIgnore ??
DEFAULT_FILE_FILTERING_OPTIONS.respectGitIgnore,
respectQwenIgnore:
options?.respectQwenIgnore ??
DEFAULT_FILE_FILTERING_OPTIONS.respectQwenIgnore,
};
}
/**
* Gets a description of the grep operation
* @returns A string describing the grep
*/
getDescription(): string {
let description = `'${this.params.pattern}'`;
if (this.params.path) {
description += ` in path '${this.params.path}'`;
}
if (this.params.glob) {
description += ` (filter: '${this.params.glob}')`;
}
return description;
}
}
/**
* Implementation of the Grep tool logic
*/
export class RipGrepTool extends BaseDeclarativeTool<
RipGrepToolParams,
ToolResult
> {
static readonly Name = ToolNames.GREP;
constructor(private readonly config: Config) {
super(
RipGrepTool.Name,
'Grep',
'A powerful search tool built on ripgrep\n\n Usage:\n - ALWAYS use Grep for search tasks. NEVER invoke `grep` or `rg` as a Bash command. The Grep tool has been optimized for correct permissions and access.\n - Supports full regex syntax (e.g., "log.*Error", "function\\s+\\w+")\n - Filter files with glob parameter (e.g., "*.js", "**/*.tsx")\n - Use Agent tool for open-ended searches requiring multiple rounds\n - Pattern syntax: Uses ripgrep (not grep) - special regex characters need escaping (use `interface\\{\\}` to find `interface{}` in Go code)\n',
Kind.Search,
{
properties: {
pattern: {
type: 'string',
description:
'The regular expression pattern to search for in file contents',
},
glob: {
type: 'string',
description:
'Glob pattern to filter files (e.g. "*.js", "*.{ts,tsx}") - maps to rg --glob',
},
path: {
type: 'string',
description:
'File or directory to search in (rg PATH). Defaults to current working directory.',
},
limit: {
type: 'number',
description:
'Limit output to first N lines/entries. Optional - shows all matches if not specified.',
},
},
required: ['pattern'],
type: 'object',
},
);
}
/**
* Validates the parameters for the tool
* @param params Parameters to validate
* @returns An error message string if invalid, null otherwise
*/
protected override validateToolParamValues(
params: RipGrepToolParams,
): string | null {
const errors = SchemaValidator.validate(
this.schema.parametersJsonSchema,
params,
);
if (errors) {
return errors;
}
// Validate pattern is a valid regex
try {
new RegExp(params.pattern);
} catch (error) {
return `Invalid regular expression pattern: ${params.pattern}. Error: ${getErrorMessage(error)}`;
}
// Only validate path if one is provided
if (params.path) {
try {
resolveAndValidatePath(this.config, params.path, {
allowFiles: true,
allowExternalPaths: true,
});
} catch (error) {
return getErrorMessage(error);
}
}
return null; // Parameters are valid
}
protected createInvocation(
params: RipGrepToolParams,
): ToolInvocation<RipGrepToolParams, ToolResult> {
return new GrepToolInvocation(this.config, params);
}
}