2025-12-04 22:40:25 +09:00
|
|
|
import { tool } from "@opencode-ai/plugin/tool"
|
2025-12-09 11:49:00 +09:00
|
|
|
import { CLI_LANGUAGES } from "./constants"
|
2025-12-04 22:40:25 +09:00
|
|
|
import { runSg } from "./cli"
|
2025-12-09 11:49:00 +09:00
|
|
|
import { formatSearchResult, formatReplaceResult } from "./utils"
|
|
|
|
|
import type { CliLanguage } from "./types"
|
2025-12-04 22:40:25 +09:00
|
|
|
|
|
|
|
|
function showOutputToUser(context: unknown, output: string): void {
|
|
|
|
|
const ctx = context as { metadata?: (input: { metadata: { output: string } }) => void }
|
|
|
|
|
ctx.metadata?.({ metadata: { output } })
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-05 20:59:05 +09:00
|
|
|
function getEmptyResultHint(pattern: string, lang: CliLanguage): string | null {
|
2025-12-05 15:17:42 +09:00
|
|
|
const src = pattern.trim()
|
|
|
|
|
|
2025-12-05 20:59:05 +09:00
|
|
|
if (lang === "python") {
|
|
|
|
|
if (src.startsWith("class ") && src.endsWith(":")) {
|
2025-12-05 21:57:58 +09:00
|
|
|
const withoutColon = src.slice(0, -1)
|
|
|
|
|
return `💡 Hint: Remove trailing colon. Try: "${withoutColon}"`
|
2025-12-05 20:59:05 +09:00
|
|
|
}
|
|
|
|
|
if ((src.startsWith("def ") || src.startsWith("async def ")) && src.endsWith(":")) {
|
2025-12-05 21:57:58 +09:00
|
|
|
const withoutColon = src.slice(0, -1)
|
|
|
|
|
return `💡 Hint: Remove trailing colon. Try: "${withoutColon}"`
|
2025-12-05 20:59:05 +09:00
|
|
|
}
|
|
|
|
|
}
|
2025-12-05 15:17:42 +09:00
|
|
|
|
2025-12-05 20:59:05 +09:00
|
|
|
if (["javascript", "typescript", "tsx"].includes(lang)) {
|
|
|
|
|
if (/^(export\s+)?(async\s+)?function\s+\$[A-Z_]+\s*$/i.test(src)) {
|
|
|
|
|
return `💡 Hint: Function patterns need params and body. Try "function $NAME($$$) { $$$ }"`
|
|
|
|
|
}
|
2025-12-05 15:17:42 +09:00
|
|
|
}
|
2025-12-05 20:59:05 +09:00
|
|
|
|
|
|
|
|
return null
|
2025-12-05 15:17:42 +09:00
|
|
|
}
|
|
|
|
|
|
2025-12-04 22:40:25 +09:00
|
|
|
export const ast_grep_search = tool({
|
|
|
|
|
description:
|
|
|
|
|
"Search code patterns across filesystem using AST-aware matching. Supports 25 languages. " +
|
|
|
|
|
"Use meta-variables: $VAR (single node), $$$ (multiple nodes). " +
|
2025-12-05 15:17:42 +09:00
|
|
|
"IMPORTANT: Patterns must be complete AST nodes (valid code). " +
|
|
|
|
|
"For functions, include params and body: 'export async function $NAME($$$) { $$$ }' not 'export async function $NAME'. " +
|
2025-12-04 22:40:25 +09:00
|
|
|
"Examples: 'console.log($MSG)', 'def $FUNC($$$):', 'async function $NAME($$$)'",
|
|
|
|
|
args: {
|
2025-12-05 15:17:42 +09:00
|
|
|
pattern: tool.schema.string().describe("AST pattern with meta-variables ($VAR, $$$). Must be complete AST node."),
|
2025-12-04 22:40:25 +09:00
|
|
|
lang: tool.schema.enum(CLI_LANGUAGES).describe("Target language"),
|
|
|
|
|
paths: tool.schema.array(tool.schema.string()).optional().describe("Paths to search (default: ['.'])"),
|
|
|
|
|
globs: tool.schema.array(tool.schema.string()).optional().describe("Include/exclude globs (prefix ! to exclude)"),
|
|
|
|
|
context: tool.schema.number().optional().describe("Context lines around match"),
|
|
|
|
|
},
|
|
|
|
|
execute: async (args, context) => {
|
|
|
|
|
try {
|
2025-12-05 22:48:54 +09:00
|
|
|
const result = await runSg({
|
2025-12-04 22:40:25 +09:00
|
|
|
pattern: args.pattern,
|
|
|
|
|
lang: args.lang as CliLanguage,
|
|
|
|
|
paths: args.paths,
|
|
|
|
|
globs: args.globs,
|
|
|
|
|
context: args.context,
|
|
|
|
|
})
|
2025-12-05 20:59:05 +09:00
|
|
|
|
2025-12-05 22:48:54 +09:00
|
|
|
let output = formatSearchResult(result)
|
2025-12-05 20:59:05 +09:00
|
|
|
|
2025-12-05 22:48:54 +09:00
|
|
|
if (result.matches.length === 0 && !result.error) {
|
2025-12-05 20:59:05 +09:00
|
|
|
const hint = getEmptyResultHint(args.pattern, args.lang as CliLanguage)
|
|
|
|
|
if (hint) {
|
|
|
|
|
output += `\n\n${hint}`
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-04 22:40:25 +09:00
|
|
|
showOutputToUser(context, output)
|
|
|
|
|
return output
|
|
|
|
|
} catch (e) {
|
|
|
|
|
const output = `Error: ${e instanceof Error ? e.message : String(e)}`
|
|
|
|
|
showOutputToUser(context, output)
|
|
|
|
|
return output
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
export const ast_grep_replace = tool({
|
|
|
|
|
description:
|
|
|
|
|
"Replace code patterns across filesystem with AST-aware rewriting. " +
|
|
|
|
|
"Dry-run by default. Use meta-variables in rewrite to preserve matched content. " +
|
|
|
|
|
"Example: pattern='console.log($MSG)' rewrite='logger.info($MSG)'",
|
|
|
|
|
args: {
|
|
|
|
|
pattern: tool.schema.string().describe("AST pattern to match"),
|
|
|
|
|
rewrite: tool.schema.string().describe("Replacement pattern (can use $VAR from pattern)"),
|
|
|
|
|
lang: tool.schema.enum(CLI_LANGUAGES).describe("Target language"),
|
|
|
|
|
paths: tool.schema.array(tool.schema.string()).optional().describe("Paths to search"),
|
|
|
|
|
globs: tool.schema.array(tool.schema.string()).optional().describe("Include/exclude globs"),
|
|
|
|
|
dryRun: tool.schema.boolean().optional().describe("Preview changes without applying (default: true)"),
|
|
|
|
|
},
|
|
|
|
|
execute: async (args, context) => {
|
|
|
|
|
try {
|
2025-12-05 22:48:54 +09:00
|
|
|
const result = await runSg({
|
2025-12-04 22:40:25 +09:00
|
|
|
pattern: args.pattern,
|
|
|
|
|
rewrite: args.rewrite,
|
|
|
|
|
lang: args.lang as CliLanguage,
|
|
|
|
|
paths: args.paths,
|
|
|
|
|
globs: args.globs,
|
|
|
|
|
updateAll: args.dryRun === false,
|
|
|
|
|
})
|
2025-12-05 22:48:54 +09:00
|
|
|
const output = formatReplaceResult(result, args.dryRun !== false)
|
2025-12-04 22:40:25 +09:00
|
|
|
showOutputToUser(context, output)
|
|
|
|
|
return output
|
|
|
|
|
} catch (e) {
|
|
|
|
|
const output = `Error: ${e instanceof Error ? e.message : String(e)}`
|
|
|
|
|
showOutputToUser(context, output)
|
|
|
|
|
return output
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|