chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
# OpenCode Hooks
|
||||
|
||||
> Part of [`hooks/`](../README.md) — see also [`src/hooks/`](../../src/hooks/README.md) for installation code
|
||||
|
||||
## Specifics
|
||||
|
||||
- TypeScript plugin using the zx library (not a shell hook)
|
||||
- Intercepts `tool.execute.before` events, calls `rtk rewrite` as a subprocess
|
||||
- Uses `.quiet().nothrow()` to silently ignore failures
|
||||
- Mutates `args.command` in-place if rewrite differs from original
|
||||
- Installed to `~/.config/opencode/plugins/rtk.ts` by `rtk init -g --opencode`
|
||||
@@ -0,0 +1,39 @@
|
||||
import type { Plugin } from "@opencode-ai/plugin"
|
||||
|
||||
// RTK OpenCode plugin — rewrites commands to use rtk for token savings.
|
||||
// Requires: rtk >= 0.23.0 in PATH.
|
||||
//
|
||||
// This is a thin delegating plugin: all rewrite logic lives in `rtk rewrite`,
|
||||
// which is the single source of truth (src/discover/registry.rs).
|
||||
// To add or change rewrite rules, edit the Rust registry — not this file.
|
||||
|
||||
export const RtkOpenCodePlugin: Plugin = async ({ $ }) => {
|
||||
try {
|
||||
await $`which rtk`.quiet()
|
||||
} catch {
|
||||
console.warn("[rtk] rtk binary not found in PATH — plugin disabled")
|
||||
return {}
|
||||
}
|
||||
|
||||
return {
|
||||
"tool.execute.before": async (input, output) => {
|
||||
const tool = String(input?.tool ?? "").toLowerCase()
|
||||
if (tool !== "bash" && tool !== "shell") return
|
||||
const args = output?.args
|
||||
if (!args || typeof args !== "object") return
|
||||
|
||||
const command = (args as Record<string, unknown>).command
|
||||
if (typeof command !== "string" || !command) return
|
||||
|
||||
try {
|
||||
const result = await $`rtk rewrite ${command}`.quiet().nothrow()
|
||||
const rewritten = String(result.stdout).trim()
|
||||
if (rewritten && rewritten !== command) {
|
||||
;(args as Record<string, unknown>).command = rewritten
|
||||
}
|
||||
} catch {
|
||||
// rtk rewrite failed — pass through unchanged
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user