TUI Extensions (@hya/tui-sdk v2)
1. Introduction
Bundles customize Hya’s terminal UI and browser WebUI in TypeScript through @hya/tui-sdk v2 (api_version: 2). Extensions can declare the minimum compatible 2.x SDK version. Under Hya’s modular v2 architecture, domain UI surfaces—such as subagent panes and viewers, workflow dashboards, channel renderers, and workspace sidebar panels—live in bundles rather than being hardcoded into packages/hya-tui.
@hya/tui-sdk v2 adds four core capabilities on top of the isolated execution model:
- Bundle-confined
BundleApiaccess (tui.bundle_api): Call your own bundle’s backend/v2/bundles/<bundleId>/api/...(or session-scoped/v2/sessions/<sessionId>/bundles/<bundleId>/...) endpoints viaapi.bundleApior{ command: "bundle.api", ... }, strictly confined to your bundle ID. - Bundle-confined extension state (
tui.extension_state.read): Read folded session projection state (Projection::extensions[bundleId]) synchronously fromcontext.extension_state/api.extensionState.get()or asynchronously viaapi.extensionState.read(), strictly confined to your bundle ID. - Dynamic named panes & slash commands: Register multi-instance named panes (
api.registerPane) with default layout slots/sizes, control them viapane.open/pane.pin/pane.close/pane.focus, and contribute slash commands (api.registerCommand). - Host-virtualized streaming transcripts (
tui.transcript.embed): Embed{ type: "transcript", sessionId }nodes inside extension trees so the host renders live streaming subsession transcripts directly without cloning transcript trees over IPC.
Every TUI extension runs inside an isolated QuickJS WebAssembly VM (64 MiB hard memory cap, 2-second per-call execution deadline, plus OS-level Landlock/seccomp on Linux or sandbox_init on macOS) with zero access to the network, environment variables, or shell. Trusted local extensions (such as first-party bundles) run on JavaScriptCore’s JIT inside a null-prototype realm and the same OS sandbox.
2. Usage
Declaring a v2 TUI extension in bundle.yaml
Add a tui: block with api_version: 2 and sdk: "2.0.0" to your schema_version: 2 bundle.yaml and list your TypeScript sources under assets::
kind: Bundleschema_version: 2identity: id: acme/subagent-ui version: 1.0.0 publisher: acmeassets: - { id: tui-entry, path: tui/main.ts }tui: api_version: 2 entry: tui/main.ts sdk: "2.0.0" permissions: - tui.panel - tui.action - tui.keys - tui.bundle_api - tui.extension_state.read - tui.transcript.embedWorked example
Import defineTuiExtension and transcriptNode from @hya/tui-sdk (bundled automatically by the extension host—no npm install needed at runtime):
import { defineTuiExtension, transcriptNode } from "@hya/tui-sdk"
export default defineTuiExtension({ activate(api) { // 1. Dynamic named pane with a host-virtualized streaming transcript api.registerPane({ id: "viewer", title: "Worker Viewer", defaultSlot: "right", defaultSize: { width: "40%", minWidth: 32 }, pinnable: true, closable: true, render: ({ instance }) => { const state = api.extensionState.get<{ workers?: { id: string; handle: string }[] }>() const sessionId = instance ?? state?.workers?.[0]?.id if (!sessionId) return { kind: "text", text: "No active worker", style: { color: "muted" } } return { kind: "column", children: [ { kind: "text", text: `Worker ${sessionId}`, style: { color: "heading", bold: true } }, transcriptNode(sessionId, { follow: true, compact: true }), ], } }, onKey: (key, _ctx, meta) => { if (key.name === "p") return { commands: [{ command: "pane.pin", pane: "viewer", instance: meta?.instance, pinned: true }] } if (key.name === "q") return { commands: [{ command: "pane.close", pane: "viewer", instance: meta?.instance }] } }, })
// 2. Slash command that queries the bundle's own API and opens the dynamic pane api.registerCommand({ name: "worker", description: "Inspect a worker session", argumentHint: "<session-id>", execute: async ({ args }) => { const id = args.trim() if (!id) return "Usage: /worker <session-id>" await api.bundleApi.post("/inspect", { sessionId: id }) return { notice: `Opened worker ${id}`, commands: [{ command: "pane.open", pane: "viewer", instance: id, slot: "right", focus: true }], } }, }) },})Package and install the bundle (hya bundle install ...), or place the folder under <project>/.hya/bundles/<name>/ and run /reload-bundles.
Tool call cards
Use api.registerToolRenderer to present a tool your bundle supplies. The
TUI shows its default argument and result previews while the extension host
computes the card. The same renderer runs in the sandboxed VM and trusted JIT.
Set tui.sdk: "2.1.0" and include tui.render in tui.permissions. Add
tui.transcript.read to receive the arguments and result. For a bundle that
ships an inspect tool:
import { asRecord, defineTuiExtension, unwrapToolOutput } from "@hya/tui-sdk"
export default defineTuiExtension({ activate(api) { api.registerToolRenderer({ id: "inspect-card", tools: ["inspect"], render(call) { const args = asRecord(call.input) const output = unwrapToolOutput(call.output) return { summary: `Inspection · ${typeof args.target === "string" ? args.target : "workspace"}`, statusChip: call.status === "completed" ? "Ready" : "Inspecting", body: { kind: "text", text: typeof output === "string" ? output : "Waiting for results" }, } }, }) },})Click the card or use /tools on to expand the body. The tool name,
execution state, elapsed time, and errors are drawn by the host.
Pure tool-renderer caching and streaming behavior
Tool-call renderers are treated as pure functions of (ToolCallInput, resultJson, width):
- Stable per-call slot (stale-while-revalidate): Each tool call is cached under a stable slot
renderer:<renderer-id>:<tool-call-id>keyed by the hash of(input, resultJson, width). While a streaming delta’s updatedtui/renderRPC is in flight, the host continues displaying the previously renderedToolCardPresentationso cards never flicker back to raw JSON. - Decoupled from periodic
tui/contextupdates:tui/contextticks (every 200 ms) invalidate context-driven surfaces (panels, panes, status items, composer decorations, and formatters), not tool-call renderers. Completed tool cards trigger zerotui/renderRPCs acrosstui/contextticks. Only extension start/reload/disable or aninvalidate: truereply from a renderer’s ownonActioninvalidates tool-call cards. - Per-card reactivity and bounded LRU cache: Tool-card renders notify only the affected tool call’s reactive slot rather than re-evaluating every card in scrollback. Cached tool cards are bounded per extension with LRU eviction (default 500 entries) so active and recently-used cards are retained while older scrollback entries evict incrementally.
Managing extensions, trust tiers, and --bare
/extensions//bundles: Inspect loaded extensions, warnings, permissions, and logs.- Default VM tier: Third-party extensions run in QuickJS WebAssembly (64 MiB memory cap). Extensions from a remote backend start
blockeduntil explicitly enabled with/extensions enable <bundle-id>. - Trusted JIT tier (
/extensions trust <bundle-id>): Runs trusted code on JavaScriptCore’s JIT inside a null-prototype realm and OS sandbox (default for local first-party bundles). - Manual panel placement: In the
workspacelayout preset, place any registered panel or dynamic pane with/layout split left extension <bundle-id>#<pane-or-panel-id>(or assign/wrap it in/layout tree). - Core vs. bundle panes (
packages/hya-tui): The TUI host owns only five core pane kinds (conversation,composer,activity,status,layout), five bundle-replaced workspace slots (projects,sessions,todos,context,jobsfromhya/basic-tui-components), and the genericextensionleaf (<bundle-id>#<pane-id>). Savedtui-state.jsonlayouts strictly requireversion: 4; legacy layout upgrades are handled bycrates/hya-migration. - Live extension state updates: Whenever a bundle emits an
extensionEventon the session or global/v2stream, the TUI host refreshes the open session’sSessionInfo.extensionsprojection and pushes the updatedcontext.extension_stateslice to the owning bundle’s surfaces. - Bare mode (
hya --bare): When the backend advertises zero TUI extensions,ExtensionManager.settled()resolves immediately, no extension host process runs, and the TUI uses the minimal single-column chat layout while keeping core overlays (/key,/mcp,/skills,/bundles,/rules,/diff) available. - Browser-safe keybindings: Keep custom
onKeybindings browser-safe—do not rely exclusively on shortcuts reserved by browsers (Ctrl/Cmd+W,T,N,L,Tab,Ctrl+Tab).
3. Interface Definitions (api_version: 2)
Version constants
| Constant | Value | Meaning |
|---|---|---|
TUI_EXTENSION_API_VERSION |
2 |
Wire protocol major version enforced by tui/initialize and catalog validation. |
TUI_EXTENSION_SDK_VERSION |
"2.1.0" |
Independent @hya/tui-sdk semantic version. |
TUI_EXTENSION_SDK_MAJOR |
2 |
SDK contract major version validated by sdkCompatible. |
MAX_FRAME_BYTES |
2_097_152 |
Maximum UTF-8 byte length of one LF-delimited JSON-RPC frame. |
MAX_CONTRIBUTIONS |
32 |
Maximum contributions of each kind per extension activation. |
RENDER_LIMITS |
{ maxDepth: 32, maxNodes: 2000, maxStringBytes: 32768 } |
Validation caps per render tree. |
Permissions (TuiExtensionPermission) & Confinement
An extension only receives context sections, contributions, render nodes, and host commands whose permissions are declared in tui.permissions:
| Permission | Scope & behavior |
|---|---|
tui.panel |
Register panels and dynamic panes; emit pane.open, pane.pin, pane.close, pane.focus. |
tui.status_item |
Register status_items shown in the Context box and bottom status line. |
tui.render |
Register named tool cards, renderers (tool_call, composer), and output formatters. |
tui.action |
Emit clickable NodeActions, register interceptors, and register slash commands. |
tui.keys |
Receive tui/key events on registered panels and dynamic panes (onKey). |
tui.session.read |
Read context.session (active session ID, title, agent, model, mode, workdir, busy). |
tui.transcript.read |
Read context.transcript (up to 50 recent messages) and tool call input/output/error. |
tui.transcript.embed |
Return host-virtualized { type: "transcript", sessionId, ... } nodes in render trees. |
tui.extension_state.read |
Read Projection::extensions[bundleId] via context.extension_state, api.extensionState.get(), and api.extensionState.read(). Strictly confined to caller’s bundleId unless explicitly authorized by the host. |
tui.bundle_api |
Invoke bundle endpoints via api.bundleApi.* or { command: "bundle.api", ... }. Strictly confined to caller’s bundleId (/v2/bundles/<encodeURIComponent(bundleId)>/api<path> or /v2/sessions/<sessionId>/bundles/<encodeURIComponent(bundleId)><path>) unless explicitly authorized by the host. |
tui.workspace.read |
Read context.workspace (directory and active Project ID/name). |
workspace.git.read |
Read context.git (branch, HEAD, dirty, ahead, behind). |
tui.sessions.read / tui.projects.read |
Read context.sessions / context.projects. |
tui.todos.read / tui.status.read |
Read context.todos / context.status. |
tui.session.control / tui.project.control |
Emit session.* / project.* and fs.complete host commands. |
fs.read |
Read-only api.fs.{read, list, stat, watch} strictly inside active Project roots (read <= 1 MiB, list <= 2,000 entries, <= 16 watches). |
Bundle-ID confinement invariant: Both the SDK runner and the TUI host enforce bundle-ID confinement independently. The shared host process overwrites params.ext with the authoritative extension ID on every host-bound RPC (bundle/api, extension_state/read, fs/*), so VM code cannot spoof another bundle’s identity. Path traversal (.., %2e%2e, %2f, \, ?, #, control characters, or empty segments) is rejected on bundle.api paths.
TuiExtensionApi
export interface TuiExtensionApi { readonly bundleId: string readonly registerPanel: (panel: PanelOptions) => void readonly registerPane: (pane: PaneOptions) => void readonly registerCommand: (command: SlashCommandOptions) => void readonly registerStatusItem: (item: StatusItemOptions) => void readonly registerToolRenderer: (renderer: ToolRendererOptions) => void readonly registerRenderer: (renderer: RendererOptions) => void readonly registerFormatter: (formatter: FormatterOptions) => void readonly registerInterceptor: (interceptor: InterceptorOptions) => void readonly onContext: (handler: (context: ExtensionContext) => MaybePromise<boolean | void>) => void readonly fs: TuiFsApi readonly bundleApi: TuiBundleApi readonly extensionState: TuiExtensionStateApi}api.registerPane(pane: PaneOptions): Registers a dynamic named pane with{ id, title, defaultSlot?, defaultSize?, pinnable?, closable?, refreshMs?, render, onAction?, onKey?, onResult? }.defaultSlotis one of"left" | "right" | "top" | "bottom" | "main" | "overlay".defaultSizeaccepts{ width?: number | "${1..100}%", height?: number | "${1..100}%", minWidth?: number, minHeight?: number }.renderreceives{ context, width, height, now, instance?, params? }.api.registerCommand(command: SlashCommandOptions): Registers a slash command{ name, description, argumentHint?, execute, onResult? }.namemust match/^[a-z0-9][a-z0-9._:-]{0,63}$/and must not start withskill:.api.bundleApi: Providesrequest({ method, path, body?, query?, sessionId?, bundleId? })and shorthand methodsget,post,put,patch,delete.methodis"GET" | "POST" | "PUT" | "PATCH" | "DELETE".api.extensionState: Providesget<T>(bundleId?): T | undefined(synchronous read from the latest pushedcontext.extension_state) andread<T>({ sessionId?, bundleId? }?): Promise<T | undefined>(asynchronous host query, optionally targeting a specific session).
Payload helpers (@hya/tui-sdk)
@hya/tui-sdk exports two pure helpers for decoding tool-call inputs and result envelopes across tool renderers:
/** Return `value` as a plain string-keyed record, or `{}` if it is null, primitive, or an array. */export function asRecord(value: unknown): Record<string, unknown>
/** Unwrap a tool result's `{ output: ... }` envelope when present, returning the raw value otherwise. */export function unwrapToolOutput(output: unknown): unknownasRecord(call.input)safely narrows JSON tool arguments or nested metadata without repeating null/array guards in every bundle.unwrapToolOutput(call.output)returnsoutput.outputwhenoutputis a plain object carrying a definedoutputfield, oroutputunchanged for raw strings, arrays, primitives, or un-enveloped objects.
ToolRendererOptions
interface ToolRendererOptions { readonly id: string readonly tools: readonly string[] readonly priority?: number readonly render: ( input: ToolCallInput, context: RenderContext, ) => MaybePromise<ToolCardPresentation | null | undefined> readonly onAction?: ActionHandler}
interface ToolCallInput { readonly id: string readonly tool: string readonly status: "pending" | "running" | "completed" | "error" readonly summary: string readonly input?: JsonValue readonly output?: JsonValue readonly error?: string}
interface ToolCardPresentation { readonly summary: string readonly body?: RenderNode readonly statusChip?: string readonly expandedByDefault?: boolean}tools accepts 1–32 exact names of up to 128 characters each. The host
validates the registration ID and integer priority using the same rules as
other renderers. The highest priority replacement with a valid result wins.
Equal priorities are ordered by bundle ID, then registration order within
the bundle, with the last result winning. The default priority is 0.
The host decodes JSON arguments and results, passing incomplete or non-JSON
values as strings. Without tui.transcript.read, the summary is empty and
the arguments, result, and error are omitted. RenderContext supplies
{ context, width, height, now } using the same permission-scoped context
as other renderers. MaybePromise<T> accepts T or Promise<T>.
Return null or undefined to leave the default card in place. Summary
and chip text appear on one line, capped at 512 and 64 characters respectively.
The body accepts normal render nodes and supports clicks through onAction
with tui.action. The handler receives (action, context, meta?),
where action is { name: string; data: JsonValue; button: "left" | "right" }
and meta, when present, is { instance?: string }. It returns a notice
string, void, or { notice?: string; commands?: readonly HostCommand[]; invalidate?: boolean }, synchronously or as a promise. Expansion defaults to collapsed unless the renderer sets
expandedByDefault or the user has set an expansion preference.
On the wire, named cards are tool_call replacement renderers with a
tools list. tui/render returns { root, card? }; root is the body
or null and card contains the remaining ToolCardPresentation fields.
Existing SDK 2.0 extensions and registerRenderer continue to work.
Contributions wire schema (tui/activate result)
export interface Contributions { readonly panels: readonly { readonly id: string readonly title: string readonly placement: "sidebar" | "pane" readonly replaces?: ReplaceableSurface readonly default_slot?: PaneSlot readonly default_size?: PaneSize readonly refresh_ms?: number readonly keys?: true }[] readonly panes: readonly { readonly id: string readonly title: string readonly default_slot?: PaneSlot readonly default_size?: PaneSize readonly pinnable?: boolean readonly closable?: boolean readonly refresh_ms?: number readonly keys?: true }[] readonly commands: readonly { readonly name: string readonly description: string readonly argument_hint?: string }[] readonly status_items: readonly { readonly id: string; readonly label: string; readonly priority: number }[] readonly renderers: readonly { readonly id: string; readonly target: "tool_call" | "composer"; readonly mode: "replace" | "decorate"; readonly priority: number; readonly tools?: readonly string[] }[] readonly formatters: readonly { readonly id: string; readonly priority: number }[] readonly interceptors: readonly { readonly id: string; readonly target: "submit"; readonly priority: number }[]}Declarative RenderNode schema
export type RenderNode = | { readonly kind: "text"; readonly text: string; readonly style?: TextStyle; readonly action?: NodeAction } | { readonly kind: "row" | "column"; readonly children: readonly RenderNode[]; readonly gap?: number } | { readonly kind: "box"; readonly children: readonly RenderNode[]; readonly title?: string; readonly border?: boolean; readonly padding?: number } | { readonly kind: "table"; readonly columns: readonly string[]; readonly rows: readonly (readonly string[])[] } | { readonly kind: "progress"; readonly value: number; readonly total: number; readonly label?: string } | { readonly kind: "slot" } | { readonly type: "transcript" // (also accepts `kind: "transcript"`) readonly sessionId: string readonly follow?: boolean // default true readonly scrollOffset?: number // non-negative integer, used when follow is false readonly maxMessages?: number // integer 1..1000 readonly compact?: boolean }Supported theme color tokens: fg, muted, focus, heading, link, activity, success, warning, error, border (or hex #rrggbb).
HostCommand union
Returned in { commands?: HostCommand[] } from onAction, onKey, onResult, or slash command execute:
export type HostCommand = | { readonly command: "bundle.api"; readonly method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE"; readonly path: string; readonly body?: JsonValue; readonly query?: Readonly<Record<string, string>>; readonly sessionId?: string; readonly bundleId?: string; readonly token?: string } | { readonly command: "pane.open"; readonly pane: string; readonly instance?: string; readonly title?: string; readonly slot?: PaneSlot; readonly size?: PaneSize; readonly params?: JsonValue; readonly focus?: boolean; readonly token?: string } | { readonly command: "pane.pin"; readonly pane: string; readonly instance?: string; readonly pinned?: boolean; readonly token?: string } | { readonly command: "pane.close"; readonly pane: string; readonly instance?: string; readonly token?: string } | { readonly command: "pane.focus"; readonly pane: string; readonly instance?: string; readonly token?: string } | { readonly command: "session.open"; readonly id: string; readonly exact?: boolean; readonly token?: string } | { readonly command: "session.menu"; readonly id: string } | { readonly command: "session.new_temporary"; readonly token?: string } | { readonly command: "project.switch"; readonly id: string; readonly token?: string } | { readonly command: "project.menu"; readonly id: string } | { readonly command: "project.create"; readonly name: string; readonly roots: readonly string[]; readonly token?: string } | { readonly command: "project.rename"; readonly id: string; readonly name: string; readonly token?: string } | { readonly command: "project.set_roots"; readonly id: string; readonly roots: readonly string[]; readonly token?: string } | { readonly command: "project.delete"; readonly id: string; readonly token?: string } | { readonly command: "fs.complete"; readonly input: string; readonly token: string } | { readonly command: "ui.release" } | { readonly command: "ui.close" } | { readonly command: "ui.open" }session.open normally opens the root of the requested session. Set exact: true to open the requested session directly, including read-only child sessions.
Commands carrying a token deliver their outcome back to the originating surface or slash command via tui/command_result ({ token, ok, value?, error? }).
4. Official TUI Extensions
The official default registry profile includes five bundles with @hya/tui-sdk v2 extensions (api_version: 2). Hybrid native + tui bundles carry their compiled ABI v2 dynamic library and tui/*.ts assets in one .hyabundle archive. GET /v2/tui-extensions serves the TUI assets. See the registry protocol for installation.
| Bundle | Runtime | Contributed Surfaces & Commands | Required Permissions |
|---|---|---|---|
hya/basic-tui-components |
tui |
Panels replacing projects, sessions, todos, context, context_line, project_view, and jobs (hya/basic-tui-components#jobs) |
tui.panel, tui.action, tui.keys, tui.session.read, tui.sessions.read, tui.projects.read, tui.todos.read, tui.status.read, tui.session.control, tui.project.control |
hya/multi-agent |
native + tui |
Dynamic panes hya/multi-agent#subagents (SubagentsPane) and hya/multi-agent#subagent-viewer (SubagentViewer, embedding { type: "transcript", sessionId }), named cards for task, report, list_agents, search_agent, archive, and wait, plus /subagents calling GET /members, POST /archive, POST /steer |
tui.panel, tui.action, tui.keys, tui.session.read, tui.sessions.read, tui.bundle_api, tui.extension_state.read, tui.transcript.embed, tui.render, tui.transcript.read, tui.session.control |
hya/workflow |
native + tui |
Dynamic pane hya/workflow#workflows (WorkflowsPane) a workflow tool card, plus /workflow and /workflows slash commands calling GET /workflows, GET /state, POST /command |
tui.panel, tui.action, tui.keys, tui.session.read, tui.workspace.read, tui.bundle_api, tui.extension_state.read, tui.render, tui.transcript.read |
hya/plan |
native + tui |
plan_exit tool-call card renderer (plan-exit-card) and Plan status badge (PLAN (read-only) while active session runs hya-plan) |
tui.render, tui.status_item, tui.session.read, tui.transcript.read |
hya/agent-channels |
native + tui |
send / list_channel tool-call renderers (channel-tools) and Mail unread status item (unread-mail, reading extension_state["hya/agent-channels"] with GET /inbox fallback) |
tui.render, tui.status_item, tui.session.read, tui.transcript.read, tui.bundle_api, tui.extension_state.read |
When any bundle is disabled (via /extensions disable <bundle-id>, bundles.builtin.disable, or --bare), its dynamic panes, status badges, slash commands, and custom tool-call renderers disappear cleanly from the live TUI.
JSON-RPC methods
- Host → Extension (
TuiMethodMap):tui/initialize({ api_version: 2, sdk_version, extension_id, permissions, authorized_bundles? }),tui/activate,tui/render(surface.kind: "panel" | "pane" | "status" | "renderer", optionalsurface.instance),tui/command({ name, args, raw }),tui/action,tui/key,tui/command_result,tui/context,tui/format,tui/intercept,tui/fs_event,tui/shutdown. - Extension Host → TUI (
HostChannel):fs/read,fs/list,fs/stat,fs/watch,fs/unwatch(fs.read),bundle/api(tui.bundle_api), andextension_state/read(tui.extension_state.read). Every request is tagged with the host’s authoritativeextbundle ID.