|
| 1 | +// Coffee CLI — OpenCode Plugin |
| 2 | +// |
| 3 | +// Auto-loaded by OpenCode from ~/.config/opencode/plugins/ on every session. |
| 4 | +// Subscribes to OpenCode's bus events via the catch-all `event` hook and |
| 5 | +// forwards a normalized 3-state payload to the Coffee CLI hook server over |
| 6 | +// loopback TCP — same protocol the Claude/Codex forwarders speak. |
| 7 | +// |
| 8 | +// Event-to-state mapping (verified against |
| 9 | +// packages/opencode/src/session/status.ts and |
| 10 | +// packages/sdk/js/src/gen/types.gen.ts on the dev branch): |
| 11 | +// |
| 12 | +// working — session.status with status.type === "busy" | "retry" |
| 13 | +// permission.replied (user just answered, agent resumes) |
| 14 | +// wait_input — permission.updated (a permission record exists awaiting |
| 15 | +// response — Permission has no status |
| 16 | +// field, so the event itself signals it) |
| 17 | +// idle — session.idle / session.error |
| 18 | +// session.status with status.type === "idle" |
| 19 | +// |
| 20 | +// What is NOT mapped (intentionally): |
| 21 | +// message.updated / message.part.updated — too noisy, fires per chunk |
| 22 | +// during streaming AND after stream ends (cleanup writes), which trapped |
| 23 | +// the dot in "working" for ~30s past actual idle (auto-idle fallback). |
| 24 | +// Working is already covered by session.status=busy; we don't need a |
| 25 | +// second source. |
| 26 | +// |
| 27 | +// tool.execute.before / tool.execute.after — these are NAMED hooks in the |
| 28 | +// plugin Hooks interface, not bus events; the catch-all `event` handler |
| 29 | +// never receives them. |
| 30 | +// |
| 31 | +// Env vars (injected by Coffee CLI when spawning opencode): |
| 32 | +// COFFEE_CLI_TAB_ID, COFFEE_CLI_HOOK_PORT, COFFEE_CLI_TOOL=opencode |
| 33 | +// Plugin auto-loads for ALL OpenCode sessions, including those started |
| 34 | +// outside Coffee CLI — must no-op silently when env vars are missing. |
| 35 | + |
| 36 | +import net from "node:net"; |
| 37 | + |
| 38 | +const COFFEE_DEBUG = process.env.COFFEE_CLI_DEBUG === "1"; |
| 39 | + |
| 40 | +function debug(...args) { |
| 41 | + if (COFFEE_DEBUG) { |
| 42 | + try { process.stderr.write(`[coffee-cli-island] ${args.join(" ")}\n`); } catch (_) {} |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +function send(payload) { |
| 47 | + const tab_id = process.env.COFFEE_CLI_TAB_ID; |
| 48 | + const port = process.env.COFFEE_CLI_HOOK_PORT; |
| 49 | + const tool = process.env.COFFEE_CLI_TOOL || ""; |
| 50 | + if (!tab_id || !port || tool !== "opencode") return; |
| 51 | + |
| 52 | + try { |
| 53 | + const s = net.createConnection({ host: "127.0.0.1", port: parseInt(port, 10) }); |
| 54 | + s.setTimeout(800); |
| 55 | + const cleanup = () => { try { s.destroy(); } catch (_) {} }; |
| 56 | + s.on("error", (err) => { debug("send err", err && err.message); cleanup(); }); |
| 57 | + s.on("timeout", () => { debug("send timeout"); cleanup(); }); |
| 58 | + s.on("connect", () => { |
| 59 | + const body = JSON.stringify({ tab_id, tool, ...payload }) + "\n"; |
| 60 | + s.write(body); |
| 61 | + // Server acks with `{}\n` then closes. Give it 150ms to round-trip. |
| 62 | + setTimeout(cleanup, 150); |
| 63 | + }); |
| 64 | + } catch (err) { |
| 65 | + debug("send caught", err && err.message); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +function mapEvent(evt) { |
| 70 | + if (!evt || typeof evt !== "object") return null; |
| 71 | + const type = evt.type || ""; |
| 72 | + const props = evt.properties || {}; |
| 73 | + |
| 74 | + switch (type) { |
| 75 | + case "session.idle": |
| 76 | + case "session.error": |
| 77 | + return { status: "idle", event: type }; |
| 78 | + |
| 79 | + case "session.status": { |
| 80 | + // properties.status is a SessionStatus object: {type: "idle"|"retry"|"busy", ...}. |
| 81 | + // Old version of this plugin compared the whole object to "busy" — never |
| 82 | + // matched, which is why idle was missed. |
| 83 | + const inner = props.status || {}; |
| 84 | + const t = inner.type; |
| 85 | + if (t === "busy" || t === "retry") return { status: "working", event: type }; |
| 86 | + if (t === "idle") return { status: "idle", event: type }; |
| 87 | + return null; |
| 88 | + } |
| 89 | + |
| 90 | + case "permission.updated": |
| 91 | + // Permission record was created or changed. Easiest 3-state read: any |
| 92 | + // permission.updated means the agent is blocked waiting for the user. |
| 93 | + // (permission.replied below transitions us back out.) |
| 94 | + return { status: "wait_input", event: type }; |
| 95 | + |
| 96 | + case "permission.replied": |
| 97 | + // User answered — back to working until the next status flip. |
| 98 | + return { status: "working", event: type }; |
| 99 | + |
| 100 | + default: |
| 101 | + return null; |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +// OpenCode loader (packages/opencode/src/plugin/index.ts) iterates Object.values |
| 106 | +// of the loaded module and treats every function as a Plugin in legacy mode. |
| 107 | +// We export a single named Plugin function; default-export is the same |
| 108 | +// reference and is deduped by the loader's identity Set. |
| 109 | +export const CoffeeCliIslandPlugin = async () => { |
| 110 | + debug("plugin loaded; tab=", process.env.COFFEE_CLI_TAB_ID || "<unset>", |
| 111 | + "port=", process.env.COFFEE_CLI_HOOK_PORT || "<unset>"); |
| 112 | + return { |
| 113 | + event: async ({ event }) => { |
| 114 | + const mapped = mapEvent(event); |
| 115 | + if (mapped) { |
| 116 | + debug("→", event && event.type, "=>", mapped.status); |
| 117 | + send(mapped); |
| 118 | + } |
| 119 | + }, |
| 120 | + }; |
| 121 | +}; |
| 122 | + |
| 123 | +export default CoffeeCliIslandPlugin; |
0 commit comments