GeezerVison is showing github.com — refactor(tui): setup-time keymap layer, typed setup, reactive target · rphang/opencode-workflows@c9755c2
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: docs/PARITY.md +1-1Lines changed: 1 addition & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -147,7 +147,7 @@ Design: [`docs/design/steering-and-live-tree.md`](design/steering-and-live-tree. | 147 | 147 |
| 148 | 148 | ## Test evidence | 149 | 149 |
| 150 | | -`npm run test:parity` (`bun test tests/parity`) runs 334 tests in 15 files, and all pass. They | | 150 | +`npm run test:parity` (`bun test tests/parity`) runs 335 tests in 15 files, and all pass. They | 151 | 151 | enter at the highest level possible: the plugin's `setup()` over a fake opencode v2 Context | 152 | 152 | (`tests/helpers/plugin-harness.ts`), then the `workflow` and `workflow_control` tools | 153 | 153 | (`execute`), the slash commands, and the `<task-notification>` delivered through | |
Collapse file Copy file name to clipboardExpand all lines: src/tui/index.tsx +52-43Lines changed: 52 additions & 43 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 | // events delta / finished → one global stream, filtered here by Location and session | 8 | 8 | // | 9 | 9 | // Surfaces: the prompt footer (`wf <name> 7/12 · 81.2k · $0.42`), the home footer (`wf 2 running`), a | 10 | | -// session panel "workflows" with the tree (<leader>o, /wf), <leader>j to message an agent, and a | 11 | | -// desktop notification (or a toast) when a run finishes. If the RPC is unavailable (plugin disabled, | 12 | | -// older opencode) it stays silent; /workflows keeps working. | | 10 | +// session panel "workflows" with the tree (<leader>o, /wf), the agent dialog (<leader>j, X25), a | | 11 | +// sidebar summary (X26), and a desktop notification (or a toast) when a run finishes. If the RPC is | | 12 | +// unavailable (plugin disabled) it retries a few times (X27), then stays quiet; /workflows keeps working. | | 13 | +// | | 14 | +// The host-agnostic state and sync live in src/client/ (shared with a GUI companion); this file only | | 15 | +// wires them to the opencode 2.3 TUI plugin API (@opencode/plugin/tui, types only: the host provides it). | 13 | 16 |
| 14 | | -import { createMemo, createSignal, Show } from "solid-js" | | 17 | +import type { Plugin } from "@opencode/plugin/tui" | | 18 | +import { createEffect, createMemo, createRoot, createSignal, Show, untrack } from "solid-js" | 15 | 19 | import { TreeSync } from "../client/sync.ts" | 16 | 20 | import { definition } from "../plugin/rpc-def.ts" | 17 | 21 | import { Panel } from "./panel.tsx" | @@ -55,7 +59,7 @@ function clip(s: string, n = 400): string { | 55 | 59 |
| 56 | 60 | const plugin = { | 57 | 61 | id: "dynamic-workflows.tui", | 58 | | - setup(ctx: any) { | | 62 | + setup(ctx: Plugin.Context) { | 59 | 63 | const rpc = ctx.client.rpc(definition) | 60 | 64 | const tree = new TreeStore() | 61 | 65 | const [version, setVersion] = createSignal(0) | @@ -98,18 +102,25 @@ const plugin = { | 98 | 102 | const offPlugins = ctx.data?.on?.("plugin.updated", () => void sync.refresh()) | 99 | 103 | if (typeof offPlugins === "function") cleanups.push(offPlugins) | 100 | 104 |
| 101 | | - // Resync when the viewed session (and so the target) or the scope changes, and periodically | 102 | | - // while a run is active (missed events, reconnects). | 103 | | - const watchTarget = setInterval(() => sync.checkTarget(), 500) | | 105 | + // Resync when the viewed session (and so the target) or the scope changes: the router's route and | | 106 | + // the session data are Solid stores in opencode 2.3 (route.tsx createStore/reconcile, the client's | | 107 | + // data store), so an effect over filter() sees every change. Its own root: setup has no owner. | | 108 | + createRoot((dispose) => { | | 109 | + cleanups.push(dispose) | | 110 | + createEffect(() => { | | 111 | + filter() | | 112 | + untrack(() => sync.checkTarget()) | | 113 | + }) | | 114 | + }) | | 115 | + // Periodically while a run is active (missed events, reconnects). | 104 | 116 | const periodic = setInterval(() => { | 105 | 117 | if ([...tree.runs.values()].some(isActive)) void resync() | 106 | 118 | }, RESYNC_MS) | 107 | 119 | const ticker = setInterval(() => { | 108 | 120 | if ([...tree.runs.values()].some(isActive)) setNow(Date.now()) | 109 | 121 | }, TICK_MS) | 110 | | - for (const t of [watchTarget, periodic, ticker]) (t as any).unref?.() | | 122 | + for (const t of [periodic, ticker]) (t as { unref?: () => void }).unref?.() | 111 | 123 | cleanups.push(() => { | 112 | | - clearInterval(watchTarget) | 113 | 124 | clearInterval(periodic) | 114 | 125 | clearInterval(ticker) | 115 | 126 | }) | @@ -130,7 +141,8 @@ const plugin = { | 130 | 141 |
| 131 | 142 | const control = async (req: ControlRequest) => { | 132 | 143 | try { | 133 | | - const res = await rpc.control(req, location()) | | 144 | + // The output schema is a plain object; the handler answers {ok, message} (src/plugin/rpc-def.ts). | | 145 | + const res = (await rpc.control(req, location())) as { ok?: boolean; message?: string } | undefined | 134 | 146 | toast(String(res?.message ?? "done"), res?.ok ? "success" : "warning", "Workflows") | 135 | 147 | } catch (e) { | 136 | 148 | toast(errText(e), "error", "Workflows") | @@ -222,6 +234,33 @@ const plugin = { | 222 | 234 | if (picked) await runAction(pickerAction(tree, picked, "steer")) | 223 | 235 | } | 224 | 236 |
| | 237 | + // ---- global commands ---------------------------------------------------------------------- | | 238 | + | | 239 | + // A setup-time layer (opencode 2.3 registers it for the plugin's lifetime; it used to need an | | 240 | + // `app` slot render that returned null). | | 241 | + ctx.keymap.layer(() => ({ | | 242 | + mode: "global", | | 243 | + commands: [ | | 244 | + { | | 245 | + id: "workflows.panel", | | 246 | + title: "Workflows: toggle the live progress tree", | | 247 | + group: "Workflows", | | 248 | + bind: "<leader>o", | | 249 | + palette: true, | | 250 | + slash: { name: "wf" }, | | 251 | + run: () => openPanel(), | | 252 | + }, | | 253 | + { | | 254 | + id: "workflows.message", | | 255 | + title: "Workflows: agents (steer, stop, open)", | | 256 | + group: "Workflows", | | 257 | + bind: "<leader>j", | | 258 | + palette: true, | | 259 | + run: () => void agentDialog(), | | 260 | + }, | | 261 | + ], | | 262 | + })) | | 263 | + | 225 | 264 | // ---- surfaces ----------------------------------------------------------------------------- | 226 | 265 |
| 227 | 266 | const muted = () => ctx.theme?.text?.muted | @@ -272,7 +311,7 @@ const plugin = { | 272 | 311 | cleanups.push( | 273 | 312 | ctx.ui.slot({ | 274 | 313 | append: "session.panel", | 275 | | - render: (input: any) => ( | | 314 | + render: (input) => ( | 276 | 315 | <Show when={input.name === PANEL}> | 277 | 316 | <Panel | 278 | 317 | ctx={ctx} | @@ -302,36 +341,6 @@ const plugin = { | 302 | 341 | ), | 303 | 342 | }), | 304 | 343 | ) | 305 | | - cleanups.push( | 306 | | - ctx.ui.slot({ | 307 | | - append: "app", | 308 | | - render() { | 309 | | - ctx.keymap.layer(() => ({ | 310 | | - mode: "global", | 311 | | - commands: [ | 312 | | - { | 313 | | - id: "workflows.panel", | 314 | | - title: "Workflows: toggle the live progress tree", | 315 | | - group: "Workflows", | 316 | | - bind: "<leader>o", | 317 | | - palette: true, | 318 | | - slash: { name: "wf" }, | 319 | | - run: () => openPanel(), | 320 | | - }, | 321 | | - { | 322 | | - id: "workflows.message", | 323 | | - title: "Workflows: agents (steer, stop, open)", | 324 | | - group: "Workflows", | 325 | | - bind: "<leader>j", | 326 | | - palette: true, | 327 | | - run: () => void agentDialog(), | 328 | | - }, | 329 | | - ], | 330 | | - })) | 331 | | - return null | 332 | | - }, | 333 | | - }), | 334 | | - ) | 335 | 344 |
| 336 | 345 | return () => { | 337 | 346 | for (const c of cleanups.splice(0).reverse()) { | @@ -341,6 +350,6 @@ const plugin = { | 341 | 350 | } | 342 | 351 | } | 343 | 352 | }, | 344 | | -} | | 353 | +} satisfies Plugin.Definition | 345 | 354 |
| 346 | 355 | export default plugin |
Collapse file Copy file name to clipboardExpand all lines: tests/parity/tui-client.test.ts +17Lines changed: 17 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -115,6 +115,23 @@ describe("X25 agent dialog", () => { | 115 | 115 | }) | 116 | 116 | }) | 117 | 117 |
| | 118 | +describe("X15 X25 TUI entry on opencode 2.3", () => { | | 119 | + test("X15 X25 the global commands are a setup-time keymap layer (no `app` slot render), setup is typed against the TUI plugin definition, and the target is watched reactively", () => { | | 120 | + const src = readFileSync(join(root, "src", "tui", "index.tsx"), "utf8") | | 121 | + expect(src).not.toMatch(/append: "app"/) | | 122 | + expect(src).toMatch(/import type \{ Plugin \} from "@opencode\/plugin\/tui"/) | | 123 | + expect(src).toMatch(/satisfies Plugin\.Definition/) | | 124 | + expect(src).not.toMatch(/setup\(ctx: any\)/) | | 125 | + // The keymap layer is created in setup itself, before any slot. | | 126 | + const layer = src.indexOf("ctx.keymap.layer(") | | 127 | + expect(layer).toBeGreaterThan(0) | | 128 | + expect(layer).toBeLessThan(src.indexOf("ctx.ui.slot(")) | | 129 | + // The 500 ms poll of the route is gone: a Solid effect on the (reactive) router and session data. | | 130 | + expect(src).not.toMatch(/setInterval\(\(\) => sync\.checkTarget\(\), 500\)/) | | 131 | + expect(src).toMatch(/createEffect\(/) | | 132 | + }) | | 133 | +}) | | 134 | + | 118 | 135 | describe("X26 sidebar summary", () => { | 119 | 136 | const research = { title: "Research", model: null, agents: 8, done: 5, running: 3, tokens: 60_000, elapsedMs: 1000 } | 120 | 137 | const judge = { title: "Judge", model: null, agents: 4, done: 0, running: 0, tokens: 0, elapsedMs: null } | |
|