From 19133466260ad2875a32c1ff91a246fbc05e84fa Mon Sep 17 00:00:00 2001 From: Nabheet Sandhu Date: Fri, 20 Nov 2026 14:29:42 -0700 Subject: [PATCH] feat: open the panel from outside the TUI over plugin RPC The panel lives in the TUI process, which no client can reach: `panel.open` is client-side, and the API cannot publish events (GET /api/event is subscribe only). A server part makes the plugin addressable from outside. - `src/rpc.ts` defines the portable contract: `open`/`close`, events `open` and `close`. - `src/index.ts` registers it and forwards each call as an event. No state. - `src/tui.tsx` subscribes over the connection it already holds and calls the same `open()` the slash command uses. - Scoped to the viewed session: nothing is deferred for another session. Verified end to end on a standalone instance: the call opened the panel and it animated (45/48). `npm run check` green; exports are `.`, `./tui`, `./rpc`. --- CHANGELOG.md | 24 ++++++++++++++++++++ README.md | 19 ++++++++++++++++ package.json | 4 +++- src/index.ts | 34 ++++++++++++++++++++++++++++ src/rpc.ts | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/tui.tsx | 16 ++++++++++++++ 6 files changed, 158 insertions(+), 1 deletion(-) create mode 100644 src/index.ts create mode 100644 src/rpc.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index c3e58be..becf230 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,29 @@ # Changelog +## 0.1.10 + +Open the panel from outside the TUI, for the session you are viewing. + +- Add: a server part (package root) exposing the RPC methods + `POST /api/rpc/media/open` and `POST /api/rpc/media/close`; the TUI part + subscribes to the emitted events and routes them to the same open/close the + slash command uses. A script or an agent can open a clip without typing and + without enabling autoplay. +- Scoped to the current session on purpose: `panel.open` only opens the session + being viewed, so a request has no way to appear in another one. Nothing is + queued for later. +- Add: `./rpc` export with the contract, for callers using + `client.rpc(Media).open({ source })`. +- Packaging: exports `.`, `./tui` and `./rpc` from `dist/`, so the server part + loads beside the TUI part from one entry. +- Note: events are live only. A call made while no TUI is running is missed. + +## 0.1.9 + +- Fix: the panel did not take focus when it opened, so `escape`, `space` and + `f` did nothing until it was clicked. Keymap layers are routed to the focused + renderable; `MediaPanel` now focuses the panel on mount. + ## 0.1.8 Documentation correction. No code change. diff --git a/README.md b/README.md index 6ab5001..99bac30 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,25 @@ The panel belongs to a session, so opening a file from the home screen reports Paths with spaces are fine: the whole argument is the path. Remote URLs work too, as long as ffmpeg can read the protocol. +### Opening it from outside the TUI + +A script, an agent, or another plugin can open the panel without typing. The +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 +``` + +Any client with the server address works the same way, including +`client.rpc(Media).open({ source })` from `@opencode/client`. + +The panel opens in the session you are viewing, and there is no way to target +another one: `panel.open` is session-scoped, and a request is not queued for +later. Events are live only, so a call made while no TUI is running is missed, +exactly like typing the command before the panel exists. + ## Options | Option | Default | Meaning | diff --git a/package.json b/package.json index de045f2..c2fabfa 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,9 @@ "description": "OpenCode v2 TUI plugin: play GIFs and video inside a session panel.", "type": "module", "exports": { - "./tui": "./dist/tui.js" + ".": "./dist/index.js", + "./tui": "./dist/tui.js", + "./rpc": "./dist/rpc.js" }, "files": [ "dist", diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..620849a --- /dev/null +++ b/src/index.ts @@ -0,0 +1,34 @@ +import { Plugin } from "@opencode/plugin"; +import { Media } from "./rpc.js"; + +/** + * The reachable half of the panel. + * + * A TUI plugin cannot be called from outside its process, but a server plugin + * can: `POST /api/rpc/media/open` (or `client.rpc(Media).open(...)`) lands here. + * Each call is forwarded as an event, and every subscribed TUI opens the panel + * from it. The server part holds no panel state on purpose, so restarting the + * TUI needs no re-registration. + */ +export default Plugin.define({ + id: "media.panel.server", + async setup(context) { + const registration = await context.rpc.register(Media, { + open: async (input) => { + const source = + typeof input === "object" && input !== null + ? (input as { source?: unknown }).source + : undefined; + if (typeof source !== "string" || source.trim() === "") { + throw new Error("A source path is required"); + } + await registration.events.emit("open", { source }); + return { queued: true }; + }, + close: async () => { + await registration.events.emit("close", {}); + return { queued: true }; + }, + }); + }, +}); diff --git a/src/rpc.ts b/src/rpc.ts new file mode 100644 index 0000000..09947f4 --- /dev/null +++ b/src/rpc.ts @@ -0,0 +1,62 @@ +import { Rpc } from "@opencode/plugin/rpc"; + +/** + * Contract between the server part, the TUI part, and any external caller. + * + * The panel lives in the TUI process, which no client can reach directly. The + * server part is the reachable side: a method call emits an event that every + * subscribed TUI opens from. Both are stateless, so a TUI that was not running + * when the call happened simply misses it, which is the same as typing the + * command late. + * + * Schemas are JSON Schema so the definition stays portable across the server, + * the TUI, and an HTTP caller that only knows the shapes. `Rpc.define` infers + * the non-portable union in this version, so the definition is narrowed once + * here instead of at every call site. + */ +const definition = Rpc.define({ + id: "media", + methods: { + open: { + input: { + type: "object", + properties: { source: { type: "string" } }, + required: ["source"], + additionalProperties: false, + }, + output: { + type: "object", + properties: { queued: { type: "boolean" } }, + required: ["queued"], + additionalProperties: false, + }, + }, + close: { + input: { + type: "object", + additionalProperties: false, + }, + output: { + type: "object", + properties: { queued: { type: "boolean" } }, + required: ["queued"], + additionalProperties: false, + }, + }, + }, + events: { + open: { + schema: { + type: "object", + properties: { source: { type: "string" } }, + required: ["source"], + additionalProperties: false, + }, + }, + close: { + schema: { type: "object", additionalProperties: false }, + }, + }, +}); + +export const Media = definition as unknown as Rpc.PortableDefinition; diff --git a/src/tui.tsx b/src/tui.tsx index 1f7199f..2caf195 100644 --- a/src/tui.tsx +++ b/src/tui.tsx @@ -4,6 +4,7 @@ import type { PanelInput } from "@opencode/plugin/tui/context"; import { createSignal, onCleanup, onMount, Show } from "solid-js"; import { extractFrames } from "./frames"; import { cellsFor, normalizeOptions } from "./options"; +import { Media } from "./rpc"; /** Opening name for this plugin's panel. Shared selection value, not a registry key. */ const PANEL = "media.panel"; @@ -226,6 +227,19 @@ export default Plugin.define({ ), }); + // Remote control: the server part exposes `POST /api/rpc/media/open`, which + // 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. + 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); + }); + const stopClose = remote.events.on("close", () => { + context.ui.panel.close(); + }); + const remembered = options.remember ? saved.source.trim() : ""; const initial = options.source ?? (remembered === "" ? undefined : remembered); let autoplay: ReturnType | undefined; @@ -250,6 +264,8 @@ export default Plugin.define({ return () => { stopTicker(); + stopOpen(); + stopClose(); if (autoplay) clearInterval(autoplay); unregister(); };