rtk.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import type { Plugin } from "@opencode-ai/plugin"
  2. // RTK OpenCode plugin — rewrites commands to use rtk for token savings.
  3. // Requires: rtk >= 0.23.0 in PATH.
  4. //
  5. // This is a thin delegating plugin: all rewrite logic lives in `rtk rewrite`,
  6. // which is the single source of truth (src/discover/registry.rs).
  7. // To add or change rewrite rules, edit the Rust registry — not this file.
  8. export const RtkOpenCodePlugin: Plugin = async ({ $ }) => {
  9. try {
  10. await $`which rtk`.quiet()
  11. } catch {
  12. console.warn("[rtk] rtk binary not found in PATH — plugin disabled")
  13. return {}
  14. }
  15. return {
  16. "tool.execute.before": async (input, output) => {
  17. const tool = String(input?.tool ?? "").toLowerCase()
  18. if (tool !== "bash" && tool !== "shell") return
  19. const args = output?.args
  20. if (!args || typeof args !== "object") return
  21. const command = (args as Record<string, unknown>).command
  22. if (typeof command !== "string" || !command) return
  23. try {
  24. const result = await $`rtk rewrite ${command}`.quiet().nothrow()
  25. const rewritten = String(result.stdout).trim()
  26. if (rewritten && rewritten !== command) {
  27. ;(args as Record<string, unknown>).command = rewritten
  28. }
  29. } catch {
  30. // rtk rewrite failed — pass through unchanged
  31. }
  32. },
  33. }
  34. }