Skip to content

Working hook for OpenCode Desktop #2650

Closed as duplicate
Closed as duplicate

Description

@wauxhall

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 RtkOpenCodePlugin

Activity

  1. added
    enhancement
    help wanted
    on Jun 27, 2026
  2. pszymkowiak commented on Jun 27, 2026

    @pszymkowiak
    Collaborator

    wshm · Automated triage by AI

    🔍 Automated Triage

    ✨ Category feature
    🟢 Priority low
    🎯 Confidence 68%

    Summary

    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.

  3. mvanhorn commented on Jun 28, 2026

    @mvanhorn
    Contributor

    Submitted a fix in #2705.

  4. pegasusearl commented on Aug 26, 2026

    @pegasusearl

    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.

    I'm on Windows 11, changing .config\opencode\plugins\rtk.ts content to your script still doesn't work. It still output without compaction.
    I also had busybox installed with scoop so which should 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.

  5. KuSh commented on Sep 13, 2026

    @KuSh
    Collaborator

    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 about rtk rewrite exiting 3 on success deserves its own issue if it still bites.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:docsenhancementhelp wantedpriority:low

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions