Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
34 changes: 34 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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 };
},
});
},
});
62 changes: 62 additions & 0 deletions src/rpc.ts
Original file line number Diff line number Diff line change
@@ -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;
16 changes: 16 additions & 0 deletions src/tui.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<typeof setInterval> | undefined;
Expand All @@ -250,6 +264,8 @@ export default Plugin.define({

return () => {
stopTicker();
stopOpen();
stopClose();
if (autoplay) clearInterval(autoplay);
unregister();
};
Expand Down