From 23685b4cb3b18d8813fd8769a39988b38e1cc5f8 Mon Sep 17 00:00:00 2001 From: Nabheet Sandhu Date: Fri, 20 Nov 2026 14:52:46 -0700 Subject: [PATCH] feat: route every open through the plugin's own server RPC The open request now originates inside the TUI process. `open()` calls `context.client.rpc(Media).open(...)`, the server part emits `open`, and the subscriber in the same process renders it. `/media`, autoplay and an external caller all take that path; the plugin never shells out. If the server part is not loaded the call rejects and the local opener runs, so a TUI-only install keeps working. Also documents the call's real shape: the body is `{"input": ...}` and the service authenticates with HTTP Basic, `opencode` plus the password from service.json. Verified by driving /media in an isolated instance: the panel opened at 19/48 through the RPC round trip. --- CHANGELOG.md | 12 ++++++++++++ README.md | 13 +++++++++++-- src/tui.tsx | 18 ++++++++++++++++-- 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index becf230..49b3c09 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,17 @@ # Changelog +## 0.1.11 + +- Change: every open now travels through the plugin's own server RPC. `/media` + and autoplay call `context.client.rpc(Media).open(...)`, the server part emits + `open`, and the TUI subscriber renders. One path for the slash command, + autoplay and any external caller, all inside the running client. If the + server part is not loaded the call rejects and the local opener runs, so a + TUI-only install keeps working. +- Fix: the README's RPC example sent the arguments at the top level. The HTTP + endpoint expects `{"input": …}`, and it authenticates with HTTP Basic + (`opencode` plus the service password). Both are now documented. + ## 0.1.10 Open the panel from outside the TUI, for the session you are viewing. diff --git a/README.md b/README.md index 99bac30..1af1f4d 100644 --- a/README.md +++ b/README.md @@ -109,8 +109,17 @@ package ships a server part that exposes an RPC, and the TUI part subscribes to its events: ```bash -opencode api POST /api/rpc/media/open -d '{"source":"/home/user/clips/demo.mp4"}' -opencode api POST /api/rpc/media/close +opencode api POST /api/rpc/media/open -d '{"input":{"source":"/home/user/clips/demo.mp4"}}' +opencode api POST /api/rpc/media/close -d '{"input":{}}' + +The body is an envelope: the method's own arguments go under `input`. A body of +`{"source": …}` is rejected with `rpc.invalid_input: Expected object`. The +service authenticates with HTTP Basic: username `opencode` and the password in +`service.json`, over the channel's port (`49374` for `latest`, `0xc0de`). + +Every open, including `/media` and autoplay, travels through this RPC: the TUI +part calls it, the server part emits, and the TUI renders the event. The plugin +never shells out. ``` Any client with the server address works the same way, including diff --git a/src/tui.tsx b/src/tui.tsx index 2caf195..9a09d0a 100644 --- a/src/tui.tsx +++ b/src/tui.tsx @@ -126,7 +126,7 @@ export default Plugin.define({ }, 1000 / next.fps); } - function open(source: string): boolean { + function show(source: string): boolean { let extracted: ReturnType; try { extracted = extractFrames(source, options); @@ -231,11 +231,25 @@ export default Plugin.define({ // a client (script, agent, another plugin) can call. The panel itself is // unreachable from outside the TUI process, so the call arrives as an event // and this subscriber is what opens it. + // Every open goes through the server: `open` below calls the RPC, the + // server emits, and this subscriber renders. One path for the slash + // command, autoplay and any external caller, and no shell or CLI in it. + // If the server part is not loaded the call rejects and the local opener + // runs instead, so a TUI-only install keeps working. const remote = context.client.rpc(Media); const stopOpen = remote.events.on("open", (event) => { const data = event.data as { source?: unknown }; - if (typeof data.source === "string") open(data.source); + if (typeof data.source === "string") show(data.source); }); + + function open(source: string): void { + const call = remote.open; + if (typeof call !== "function") { + show(source); + return; + } + Promise.resolve(call({ source })).catch(() => show(source)); + } const stopClose = remote.events.on("close", () => { context.ui.panel.close(); });