- Wrap the external
ast-grepCLI so the broader system can invoke AST-aware search and replace without caring about binary discovery or argument details (cli.ts,tools.ts). - Provide well-typed tooling primitives (
types.ts) plus formatted user output hints/summary helpers (utils.ts) that can be re-used by CLI commands or plugin UI layers. - Manage the brittle parts of CLI usage: locating a binary from caches, npm packages, or homebrew, downloading platform-specific releases when needed, and surfacing environment status/limits (
constants.ts,downloader.ts).
- Singleton initialization with retries:
getAstGrepPathcaches an init promise so concurrent requests share discovery/download work and fallback from local binaries to downloads (cli.ts). - Tool definition as declarative metadata:
tools.tsexportsast_grep_searchandast_grep_replacevia the OpenCode tool registry, which keeps descriptions, schemas, and execution logic centralized. - Separation of concerns:
cli.tsfocuses on process spawning and JSON parsing,constants.tsowns binary path resolution plus environment checks/formatting,utils.tsformats results whiledownloader.tshandles platform maps, cache directories, and fetch/extraction. - Fail fast with hints: Empty-match hints tailored per language (e.g., help removing trailing colons in Python) make search UX better while keeping AST requirements explicit.
- Tools (
ast_grep_search,ast_grep_replace) callrunSg, populating CLI arguments (pattern, rewrite, globs, context) and routing output throughformatSearchResult/formatReplaceResultbefore reporting viashowOutputToUser(tools.ts). runSgconstructs the command, ensures the CLI binary exists (resetting viagetAstGrepPathwhich may callfindSgCliPathSyncor trigger a download), spawns the process with timeout handling, and parses compact JSON while guarding against truncated output and CLI errors (cli.ts).- Binary resolution uses
constants.tshelpers to detect cached binaries, installed packages, platform-specific packages, or Homebrew paths, and exposes environment checks/formatting to upstream callers (constants.ts). downloader.tsis the fallback path: it infers the platform key, downloads the matching GitHub release, extractssg, sets executable bits, and caches it under~/.cache/oh-my-opencode-slim/bin(or Windows AppData) so subsequent commands reuse the binary.
index.tsre-exportsast_grep_search,ast_grep_replace, runtime helpers (ensureCliAvailable,checkEnvironment, etc.), and downloader utilities so other modules can plug into the tooling layer while sharing diagnostics (index.ts).- The OpenCode plugin layer imports
builtinToolsfromsrc/tools/ast-grep/index.tsto surface search/replace capabilities through the CLI tool registry. constants.tsanddownloader.tsare used bycli.tsto decide where to executesg, while environment helpers inform onboarding UIs or setup scripts about missing binaries.types.tsdefines the sharedCliLanguage,CliMatch, andSgResultshapes that drive type safety across CLI invocation, formatting utilities, and tooling schemas.