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:
parent
dafdca217b
commit
02017a1b70
@ -95,7 +95,7 @@ function buildArgs(options: GrepOptions, backend: GrepBackend): string[] {
|
|||||||
return backend === "rg" ? buildRgArgs(options) : buildGrepArgs(options)
|
return backend === "rg" ? buildRgArgs(options) : buildGrepArgs(options)
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseOutput(output: string): GrepMatch[] {
|
function parseOutput(output: string, filesOnly = false): GrepMatch[] {
|
||||||
if (!output.trim()) return []
|
if (!output.trim()) return []
|
||||||
|
|
||||||
const matches: GrepMatch[] = []
|
const matches: GrepMatch[] = []
|
||||||
@ -104,6 +104,16 @@ function parseOutput(output: string): GrepMatch[] {
|
|||||||
for (const line of lines) {
|
for (const line of lines) {
|
||||||
if (!line.trim()) continue
|
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+):(.*)$/)
|
const match = line.match(/^(.+?):(\d+):(.*)$/)
|
||||||
if (match) {
|
if (match) {
|
||||||
matches.push({
|
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
|
const limited = options.headLimit && options.headLimit > 0
|
||||||
? matches.slice(0, options.headLimit)
|
? matches.slice(0, options.headLimit)
|
||||||
: matches
|
: matches
|
||||||
|
|||||||
@ -22,7 +22,7 @@ export function createGrepTools(ctx: PluginInput): Record<string, ToolDefinition
|
|||||||
.optional()
|
.optional()
|
||||||
.describe("The directory to search in. Defaults to the current working directory."),
|
.describe("The directory to search in. Defaults to the current working directory."),
|
||||||
output_mode: tool.schema
|
output_mode: tool.schema
|
||||||
.string()
|
.enum(["content", "files_with_matches", "count"])
|
||||||
.optional()
|
.optional()
|
||||||
.describe(
|
.describe(
|
||||||
"Output mode: \"content\" shows matching lines, \"files_with_matches\" shows only file paths (default), \"count\" shows match counts per file."
|
"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 globs = args.include ? [args.include] : undefined
|
||||||
const searchPath = args.path ?? ctx.directory
|
const searchPath = args.path ?? ctx.directory
|
||||||
const paths = [searchPath]
|
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
|
const headLimit = args.head_limit ?? 0
|
||||||
|
|
||||||
if (outputMode === "count") {
|
if (outputMode === "count") {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user