Skip to content

feat(core): agent widgets - #65261

Draft
kommander wants to merge 166 commits into
devfrom
widgets
Draft

kommander wants to merge 166 commits into
devfrom
widgets

Conversation

@kommander

@kommander kommander commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Issue for this PR

Closes #

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Agents can show interactive widgets in a session: PsychoScript templates plus JSON data, run and rendered natively by each client. Contract: specs/v2/widgets.md, specs/v2/widgets-live.md.

  • @opencode/plugin-widgets: internal plugin on the public plugin API: widget / widget_template tools, widgets skill, template + state storage, RPC opencode.widget. Behind the opt-in widgets flag.
  • @opencode/widget: prelude, step ABI, node schema (incl. canvas, shader), built-in templates.
  • TUI renders widget tool calls.

Live widgets and diagram templates are in progress on this branch.

How did you verify your code works?

Plugin, widget and TUI tests (incl. end-to-end against the real server); live runs with Rune desktop.

Screenshots / recordings

See anomalyco/opencode-rune#313.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

opencode-agent Bot and others added 30 commits November 23, 2026 17:49
widgets-input.md specifies { kind: "wheel", x, y, dx, dy } in logical pixels (lines 40 px, pages
the canvas's size), summed per frame, delivered as move is, and { kind, dx, dy } under capture.
The prelude passes it to onPointer and leaves widget.pointer alone. Two wire fixtures (wheel,
wheel-captured) take the TypeScript host's outcome, as protocol 1's prelude doesn't know the event;
four fixtures' prelude line numbers move by 5.
Without capture the wheel scrolls the page or transcript and is never delivered; under capture it
is { kind: "wheel", dx, dy }, where captured moves go, summed per frame. The Wheel type and the
wheel wire fixture lose x and y; the prelude is unchanged.
…ate here

A new Rust workspace, packages/widgets-rs, with crates/widget-web: psychoscript-web from PsychoScript's web branch
(e16ce7e), renamed (lib widget_web), on psychoscript 0.1 from crates.io instead of a path to a patched copy.

psychoscript 0.1 always has typed arrays, so the two Runtime::typed_arrays(true) calls go, and the test that a plain
run has none now says every run has them. Otherwise the code is as it was.

The tests read the fixtures (packages/widget/test/fixtures) and the preludes (packages/widget/src) in place instead of
copies. The views they compare byte for byte, the copies' <name>.wire files (JSON.stringify(view)), are now written
from each fixture's view in the test, the same bytes for all 46.
packages/widgets-rs/web: PsychoScript's web/ (web branch e16ce7e), the TypeScript host that runs widgets in Web
Workers on widget-web's WebAssembly, as package widget-web.

The module is widget_web_bg.wasm (was psychoscript_web_bg.wasm): scripts/build-wasm.sh builds crate widget-web in the
workspace one level up, with the opencode checkout's root remapped to /opencode, and the worker, the host's default
module URL, build.ts and the package's exports follow. dist/ is no longer committed (it holds the ~10 MB module):
bash scripts/build-wasm.sh && bun run build makes it. The scripts read the fixtures and preludes in place from
packages/widget, and fixtures.ts takes it by default. The bundle is otherwise byte for byte the web branch's.
… apps ship

A Rust workspace for the widget hosts' native code, on PsychoScript's crates by git rev (psychoscript-gpu and
psychoscript-mobile aren't on crates.io yet; psychoscript patched to the same rev, and wgpu-hal to its vendored Metal
fix).

- widget-wire: the binary wire format, from psychoscript's mobile branch; GPU batches are psychoscript-gpu's `Arg`
  and `Batch`, and the `wgsl` shorthand moved here with it. Its tests read packages/widget/test/fixtures in place:
  all 58 wire fixtures and draw.json.
- widget-mobile: the one native library each app ships, psychoscript-mobile's UniFFI API plus the widget encoder
  (`widgetEncoder()`, on psychoscript-mobile's LiveGpu) and `ShaderCanvas`; built by psychoscript-mobile's
  build-ios.sh and build-android.sh. Its widget and GPU tests moved here, on packages/widget's preludes and fixtures
  in place (the 58 wire and 53 GPU fixtures).
…wn in the PsychoScript module

uniffi-bindgen-swift names a crate's Swift file after its module_name: with psychoscript-mobile's name it overwrote
PsychoScript.swift. WidgetMobile.swift is compiled into the same `PsychoScript` target (its helpers are fileprivate).
# Conflicts:
#	packages/widgets-rs/.gitignore
#	packages/widgets-rs/Cargo.lock
#	packages/widgets-rs/Cargo.toml
packages/widgets-rs/README.md: the crates, how PsychoScript's crates are pinned and bumped, the toolchain and the
commands. widget-mobile/README.md replaces the copied psychoscript-mobile README: what the crate adds (the widget
encoder's behaviour, ShaderCanvas, OpenCode widgets on this API with widgetEncoder()), its build outputs and its tests;
psychoscript-mobile's README covers the rest.
The batch format, canvas size, limits, host checks and the generic errors and device loss rules moved to
anomalyco/psychoscript rust/crates/psychoscript-gpu/README.md; widgets-gpu.md keeps the program side, the widget
messages, the wgsl shorthand and conformance. The mobile clients are widget-mobile (packages/widgets-rs) on
psychoscript-mobile and psychoscript-gpu.
psychoscript now comes from PsychoScript's git rev (patched, as the other crates use it): without the remap the
module carried this machine's checkout paths in its panic messages.
…ipt-mobile 0.1.0 from crates.io

No git dependency on PsychoScript; wgpu-hal stays patched to its vendored Metal fix, now at PsychoScript dev 2ef493f (a
published crate can't carry it).
…et.fetch rethrows the host's error

The prelude rethrows a fetch rejection that has a kind (the same { kind, message }) instead of a copy, so a host's
location and kind for it stay. widgets-rs takes psychoscript 0.1.2 and psychoscript-mobile 0.2.0 (program_lines,
fetch kinds, GPU errors at their post).
…ate and keeps fetch kinds

LiveOptions::program_lines (startLive's programLines) declares the template's lines (Runtime::program_lines), so an
error raised in the preludes is at the template's nearest line. A fetch rejection is a located error with its kind,
and uncaught it is the run's error of that kind (CapabilityDenied, ...), raw or rethrown by widget.fetch. A GPU batch's
post carries its line and column (`at`) for the client's errors in it, and an invalid draw fails at its post
(Error::Draw has a line and column). Tests: the five cases natively, on the real preludes.
…with the template's line

The host passes the template's lines to the run (programLines), reports a fetch rejection the program let escape in
phase fetch (by the run's error's kind), and reports a GPU batch's errors at its post's line and column (the frame
request that carried it). scripts/report-check.ts checks the six cases' reports in headless Chromium.
…template

A live widget template that declares capabilities.storage gets widget.storage: get and keys read the client's copy of
the space widget.<name>, set and delete write through the client's spaces sync, and widget.on("storage") hears
changes from elsewhere. The run starts with the copy in __input().storage; null means the template didn't declare it
(CapabilityDenied), and a client without spaces sync passes none (Unsupported). The prelude checks the limits (256-char
keys, 64 KiB values, 1,000 keys and 1 MiB per template) so errors land at the template's line; the TypeScript host and
widget-wire's encoder check posts again. widget-wire adds the storage post kind 0B. The server is unchanged.
…torage

The fixture keeps one storage key per entry and draws its history on a 2D canvas: weight as a line, each day's
calories as bars, a week or month range, and tap or drag to inspect a day. Its test follows a change from another
device through on("storage"), pointer inspection and the range switch. The skill lists storage among widget.on's kinds.
…hart.js's chart

Inputs and buttons log a meal or a weight, a select picks the last 7 or 30 days, and recent entries are a list. The
chart is a plain 2D canvas with the built-in chart template's drawing (nice ranges, axes, bars growing in, the line
drawing in, legend, tooltip), with the weight on a second axis, animated in on frames; press or drag reads a day.
A render per keystroke sent the input back a value behind what was typed, and a slow client reset the field to it.
Typing now only keeps the text, and a new key after each entry gives an empty input.
ui.table(columns, rows, props) is a header and rows of text: columns with a label and an align (end for numbers),
rows of strings and numbers, an optional sort indicator and selected row. onPress(state, row) takes a row's press and
onSort(state, column) a header's (as change); the host walk sets selectable and sortable. widget-wire writes it as
type 17 with props 39 to 45, and widget-web walks it too. The web host draws an HTML table that scrolls sideways; the
TUI writes aligned text. Clients draw it like their markdown tables.
…its own

Today logs a meal or a weight, with an optional note, and charts the week or the month; History lists every entry in
a table (date, weight, calories, note), newest first, sorted by a header press, a row press selecting one to delete.
The pages are tabs: a carousel would page on the swipes that read the chart.
Same data and behaviour: columns (numbers aligned to the end, with their unit and digits), sorting by a header press
(numbers largest first, text A to Z, again the other way), choose submitting { index, row }. A table's cells hold
text, so each row's trend is a text sparkline (▁▂▃▅▇) in a column of its own instead of a canvas. The TUI writes it
as aligned text.
…ion (instances)

A new opencode.widget RPC method, instances: every widget across Sessions except superseded ones, last updated
first, each with its state, its tool call's data and its Session's title and Location, so the apps can show
widgets outside the timeline (the Mini apps dashboard). The plugin reads the data from the widget's message through
the plugin API's session.message.get, now part of the plugin session domain.
…ment (replaced)

An agent updates a shown widget by posting a new one with replaces. Each listed instance now carries the superseded
widgets it replaced, following chains, so a client holding an old key (a dashboard tile, a window) resolves it to
the current widget at its version: one widget everywhere.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant