fix(tools): address PR review feedback from cubic

- Use tool.schema.enum() for output_mode instead of generic string()
- Remove unsafe type assertion for output_mode
- Fix files_with_matches mode returning empty results by adding
  filesOnly flag to parseOutput for --files-with-matches rg output
This commit is contained in:
JiHongKim98
2026-02-21 03:17:48 +09:00
parent dafdca217b
commit 02017a1b70
2 changed files with 14 additions and 4 deletions

View File

@@ -95,7 +95,7 @@ function buildArgs(options: GrepOptions, backend: GrepBackend): string[] {
return backend === "rg" ? buildRgArgs(options) : buildGrepArgs(options)
}
function parseOutput(output: string): GrepMatch[] {
function parseOutput(output: string, filesOnly = false): GrepMatch[] {
if (!output.trim()) return []
const matches: GrepMatch[] = []
@@ -104,6 +104,16 @@ function parseOutput(output: string): GrepMatch[] {
for (const line of lines) {
if (!line.trim()) continue
if (filesOnly) {
// --files-with-matches outputs only file paths, one per line
matches.push({
file: line.trim(),
line: 0,
text: "",
})
continue
}
const match = line.match(/^(.+?):(\d+):(.*)$/)
if (match) {
matches.push({
@@ -191,7 +201,7 @@ async function runRgInternal(options: GrepOptions): Promise<GrepResult> {
}
}
const matches = parseOutput(outputToProcess)
const matches = parseOutput(outputToProcess, options.outputMode === "files_with_matches")
const limited = options.headLimit && options.headLimit > 0
? matches.slice(0, options.headLimit)
: matches

View File

@@ -22,7 +22,7 @@ export function createGrepTools(ctx: PluginInput): Record<string, ToolDefinition
.optional()
.describe("The directory to search in. Defaults to the current working directory."),
output_mode: tool.schema
.string()
.enum(["content", "files_with_matches", "count"])
.optional()
.describe(
"Output mode: \"content\" shows matching lines, \"files_with_matches\" shows only file paths (default), \"count\" shows match counts per file."
@@ -37,7 +37,7 @@ export function createGrepTools(ctx: PluginInput): Record<string, ToolDefinition
const globs = args.include ? [args.include] : undefined
const searchPath = args.path ?? ctx.directory
const paths = [searchPath]
const outputMode = (args.output_mode as "content" | "files_with_matches" | "count") ?? "files_with_matches"
const outputMode = args.output_mode ?? "files_with_matches"
const headLimit = args.head_limit ?? 0
if (outputMode === "count") {