Repository navigation
Closed as duplicate
Labels
Description
I'm using OpenCode Desktop - installed opencode plugin, but it didn't work. Seems it must be done much more different for Desktop version. Here's rewritten plugin that worked for me. Hope will help anyone.
P.s. rtk rewrite returns exit code 3, strange behavior, should be fixed. Crutched it in my script.
import type { Plugin } from "@opencode-ai/plugin"
import { execFile } from "node:child_process"
import { existsSync } from "node:fs"
import { promisify } from "node:util"
// RTK OpenCode Desktop plugin — rewrites commands to use rtk for token savings.
// Requires: rtk >= 0.23.0 installed.
//
// Note: OpenCode Desktop runs plugins under Node.js, where the Bun shell
// helper `$` is NOT available (it's `Bun.$`). So we use node:child_process
// instead, and resolve the rtk binary by absolute path because GUI apps on
// macOS don't inherit the user's shell PATH (~/.local/bin etc. are missing).
const execFileAsync = promisify(execFile)
const RtkOpenCodePlugin: Plugin = async ({ client }) => {
const log = (message: string, extra?: Record<string, unknown>) =>
client.app
.log({ body: { service: "rtk", level: "info", message, extra } })
.catch(() => {})
const home = process.env.HOME ?? ""
const candidates = [
`${home}/.local/bin/rtk`,
`${home}/.cargo/bin/rtk`,
"/opt/homebrew/bin/rtk",
"/usr/local/bin/rtk",
"/usr/bin/rtk",
]
const rtkBin = candidates.find((p) => p && existsSync(p))
if (!rtkBin) {
await log("rtk binary not found — plugin disabled", { candidates })
return {}
}
await log("loaded", { rtkBin })
return {
"tool.execute.before": async (input, output) => {
if (input.tool !== "bash" && input.tool !== "shell") return
const args = output?.args as Record<string, unknown> | undefined
if (!args) return
const command = args.command
if (typeof command !== "string" || !command) return
const { stdout, code, error } = await execFileAsync(rtkBin, ["rewrite", command])
.then((r) => ({ stdout: r.stdout, code: 0, error: undefined as unknown }))
.catch((e) => ({
stdout: String(e?.stdout ?? ""),
code: Number(e?.code),
error: e,
}))
if (code === 0 || code === 3) {
const rewritten = stdout.trim()
if (rewritten && rewritten !== command) {
args.command = rewritten
await log("rewritten", { from: command, to: rewritten, code })
}
} else {
await log("rewrite failed", {
command,
code,
error: String((error as any)?.message ?? error),
stderr: String((error as any)?.stderr ?? ""),
})
}
},
}
}
export default RtkOpenCodePluginReacted by amnesia
Reacted by Erkan Erdem
Activity
pszymkowiak commentedon Jun 27, 2026
🔍 Automated Triage
featurelowSummary
Community contribution: working OpenCode Desktop plugin implementation using node:child_process instead of Bun shell, with absolute-path rtk binary resolution; consider incorporating into official plugin or docs
Triaged automatically by wshm · This is an automated analysis, not a human review.
mvanhorn commentedon Jun 28, 2026
Submitted a fix in #2705.
pegasusearl commentedon Aug 26, 2026
I'm on Windows 11, changing
.config\opencode\plugins\rtk.tscontent to your script still doesn't work. It still output without compaction.I also had busybox installed with scoop sowhichshould work here.Edit: Sorry, looks like I replied in the wrong issues. While I also used opencode desktop, mine might be windows related since it also doesn't work on opencode tui.
KuSh commentedon Sep 13, 2026
Closing as a duplicate of #2039, which tracks the same OpenCode Desktop failure - the GUI app does not inherit the shell PATH, and Desktop runs plugins under Node where Bun's
$is unavailable - and has the longer thread with confirmations on macOS and Linux. Your Node-based plugin is the clearest workaround posted so far, so please continue any follow-up on #2039. The side remark aboutrtk rewriteexiting 3 on success deserves its own issue if it still bites.