2026-02-07 19:04:31 +09:00
|
|
|
import type { PluginInput } from "@opencode-ai/plugin"
|
2025-12-27 17:56:40 +08:00
|
|
|
import { tool, type ToolDefinition } from "@opencode-ai/plugin/tool"
|
2025-12-04 22:54:13 +09:00
|
|
|
import { runRg } from "./cli"
|
2026-02-08 13:57:26 +09:00
|
|
|
import { formatGrepResult } from "./result-formatter"
|
2025-12-04 22:54:13 +09:00
|
|
|
|
2026-02-07 19:04:31 +09:00
|
|
|
export function createGrepTools(ctx: PluginInput): Record<string, ToolDefinition> {
|
|
|
|
|
const grep: ToolDefinition = tool({
|
|
|
|
|
description:
|
|
|
|
|
"Fast content search tool with safety limits (60s timeout, 10MB output). " +
|
|
|
|
|
"Searches file contents using regular expressions. " +
|
|
|
|
|
"Supports full regex syntax (eg. \"log.*Error\", \"function\\s+\\w+\", etc.). " +
|
|
|
|
|
"Filter files by pattern with the include parameter (eg. \"*.js\", \"*.{ts,tsx}\"). " +
|
|
|
|
|
"Returns file paths with matches sorted by modification time.",
|
|
|
|
|
args: {
|
|
|
|
|
pattern: tool.schema.string().describe("The regex pattern to search for in file contents"),
|
|
|
|
|
include: tool.schema
|
|
|
|
|
.string()
|
|
|
|
|
.optional()
|
|
|
|
|
.describe("File pattern to include in the search (e.g. \"*.js\", \"*.{ts,tsx}\")"),
|
|
|
|
|
path: tool.schema
|
|
|
|
|
.string()
|
|
|
|
|
.optional()
|
|
|
|
|
.describe("The directory to search in. Defaults to the current working directory."),
|
|
|
|
|
},
|
|
|
|
|
execute: async (args) => {
|
|
|
|
|
try {
|
|
|
|
|
const globs = args.include ? [args.include] : undefined
|
|
|
|
|
const searchPath = args.path ?? ctx.directory
|
|
|
|
|
const paths = [searchPath]
|
2025-12-04 22:54:13 +09:00
|
|
|
|
2026-02-07 19:04:31 +09:00
|
|
|
const result = await runRg({
|
|
|
|
|
pattern: args.pattern,
|
|
|
|
|
paths,
|
|
|
|
|
globs,
|
|
|
|
|
context: 0,
|
|
|
|
|
})
|
2025-12-04 22:54:13 +09:00
|
|
|
|
2026-02-07 19:04:31 +09:00
|
|
|
return formatGrepResult(result)
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return `Error: ${e instanceof Error ? e.message : String(e)}`
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return { grep }
|
|
|
|
|
}
|