Files
rtk-ai__rtk/hooks/opencode-rtk.ts
José Almeida 064cb3ea65 feat: add OpenCode plugin support (#300)
* feat(opencode): add OpenCode plugin support

Add `--opencode` flag to `rtk init` for installing a global OpenCode
plugin that rewrites Bash/shell commands through `rtk rewrite`.

- New plugin: hooks/opencode-rtk.ts (thin delegator to rtk rewrite)
- New init modes: --opencode (OpenCode only), combinable with Claude modes
- Plugin install/update/remove lifecycle with idempotent writes
- Uninstall cleans up OpenCode plugin alongside Claude Code artifacts
- `rtk init --show` reports OpenCode plugin status
- Replace unreachable!() with bail!() in match exhaustiveness guard

* docs: add OpenCode plugin documentation

- README: OpenCode plugin section, install flags, troubleshooting
- TROUBLESHOOTING: OpenCode-specific checklist
- Update init mode table to reflect Claude Code default
2026-03-11 22:13:03 +01:00

40 lines
1.3 KiB
TypeScript

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
}
},
}
}