← All Labs Lab 13 / 28 A plain tool returns a ui:// view
Lab 13 · Tools & UI · Folded back

A plain tool returns a ui:// view

A plain tool passed to DurableHabitat.open({ tools }) that returns withUIResources gives the model a short summary and the user a ui:// view, and the view stays with the conversation.

Folded back Needs: tool-calling Smoke: pnpm exec tsx examples/13-ui-views-from-tools/run.ts --smoke Run: pnpm exec tsx examples/13-ui-views-from-tools/run.ts Deck ↗ Source: examples/13-ui-views-from-tools

A pi-durable DurableHabitat that answers Acme finance questions from the Acme FinOps ontology. Each answer goes two ways: the model gets a one-line summary, and the user gets a ui:// view of the facts. The ontology's display hints pick that view's default layout; the model never writes HTML.

zsh
pnpm exec tsx examples/13-ui-views-from-tools/run.ts            # agent :7704 + chat UI :7740, offline demo model
pnpm exec tsx examples/13-ui-views-from-tools/run.ts --smoke    # offline, 7 checks, exit 0/1
MYCEL_URL=https://mycel.thefocus.ai pnpm exec tsx examples/13-ui-views-from-tools/run.ts --model mycel:z-ai/glm-5.3-flash [--smoke]
pnpm exec tsx examples/13-ui-views-from-tools/agent.ts --port 7704 [--work-dir dir]   # the agent alone

Try "Which invoices count as Overdue A/R?", "Who owes us the most?" and "Why isn't INV-1051 overdue?".

  • ontology.ts: classes with display hints, example QuickBooks data, the Overdue A/R rule, the three queries, and chooseView.
  • views.ts: one self-contained HTML page per result, with table, by-customer, graph and facts views and a "Show excluded" toggle.
  • agent.ts: the DurableHabitat, the query_ontology tool (a plain tool() returning withUIResources), an offline demo model, the host API, and A2A with views as artifacts.
  • run.ts: starts the agent behind the existing chat client examples/_chat/chat-ui.ts, or runs the smoke test.

What the smoke checks

CheckWhat it shows
split: <question> (×3)query_ontology ran; the user got a ui://ontology/… HTML view; the model's tool result has no HTML and is under 120 tokens
answered-from-summary: <question> (×3)The answer has the fact it needed from the summary alone
views-keptAll three views are in ConversationUIResourcesDoc and still in the transcript after reopening habitat.sqlite

Break it: in packages/habitat/src/durable/extensions.ts, give the model the whole result (const forModel = result). The split checks fail with about 3,000 tokens of HTML in the model's context.

The demo does more than the smoke checks. The ontology's display hints pick each view's layout, the chat client hides its MCP sidebar and turns a view's Why? buttons into the next turn, and the agent serves A2A. Those aren't this lab's claim. A2A views have their own lab, Lab 23; the chat client is checked by examples/_chat.

Findings: FINDINGS.md.

presentation.html (published copy) is a 8-slide deck in the style of Lab 01's deck, covering the tool bridge, how the ontology picks a view, and the two-way home lights panel. Open it in a browser and use the arrow keys, N for speaker notes, G for the slide grid.

Prompt

Coding-Agent Prompt · Ready to Paste
Build a lab under `examples/13-ui-views-from-tools/`: a `DurableHabitat` agent that answers finance questions from a small ontology and returns each answer twice, a one-line summary for the model and a `ui://` HTML view of the facts for the user. The model must never write HTML.

Read first: `packages/habitat/src/ui-resources.ts` (`buildHabitatUIResource`, `withUIResources`), `packages/habitat/src/durable/extensions.ts` (how tool results carry `details.uiResources` and `ConversationUIResourcesDoc`), `packages/habitat/src/durable/durable-habitat.ts`, and the chat client in `examples/_chat/` (`chat-ui.ts`, `ui/index.html`).

Requirements:
1. `ontology.ts`: classes with display hints (key property, money and date properties, relations to follow), example QuickBooks-style invoices and customers, an "Overdue A/R" rule (more than 30 days past due, excluding GL 6410 pass-through), three queries (overdue-ar, by-customer, why <invoice>), and `chooseView(result)` picking table, grouped or graph from the result's shape.
2. `views.ts`: one self-contained HTML page per result with table, by-customer, graph and facts views, a "Show excluded" toggle, and "Why?" buttons on excluded rows that send an MCP-UI `prompt` action.
3. `agent.ts`: `DurableHabitat.open({ tools: { query_ontology } })` where `query_ontology` is an ordinary @umwelten/pi `tool()` returning `withUIResources(summary, view)`; an offline demo model; the host API the chat client expects (`/api/state`, `/api/chat`, views in the transcript, `ui` settings to hide MCP sources).
4. `run.ts` starts the agent behind `examples/_chat/chat-ui.ts`. `--smoke` asks the three questions and checks, one PASS/FAIL line each, that each called query_ontology, that the user got a ui:// HTML view while the model's tool result has no HTML and stays under 120 tokens, that the answer has the fact it needed from that summary, and that all three views are in ConversationUIResourcesDoc and still in the transcript after reopening habitat.sqlite. `--model mycel:<id>` runs live.
Strict TypeScript, no `any`.
Works in Pi, Claude Code, Codex & Antigravity Offline mock first → --live opt-in

What we learned

Smoke: 7/7 offline (it was 29 checks across five claims before it was narrowed; see the last bullet). Live on mycel:z-ai/glm-5.3-flash, the model picked the right query for all three questions.

  • The model sees about 45 tokens; the user sees about 3,100. Each query_ontology result is a 41–48 token summary for the model and a 2,500–3,100 token HTML view for the user. Live on Mycel, GLM-5.3-Flash picked the right query for all three questions and answered from the summary alone.
  • The ontology can pick the view. Three rules on the result's shape (single focus → graph, grouping relation → grouped, key + money → table) chose a sensible default every time. The model only picks the query.
  • Plain tools can return views. DurableHabitat.open({ tools }) used to JSON-stringify a whole tool result into the model's context. Its bridge now splits withUIResources(...) results: the ui:// view goes to details.uiResources and ConversationUIResourcesDoc, and the model gets only the rest. query_ontology is an ordinary @umwelten/pi tool().
  • Views are durable. They are kept in the tool result entry and in ConversationUIResourcesDoc, so reopening habitat.sqlite restores the transcript with its views.
  • A2A clients get the view too. With createA2ASurface({ uiResources: true }) the card advertises text/html, and each completed task carries its turn's views as text/html URL artifacts under /a2a/ui/…. pi-a2a only accepts URL artifacts on the agent's own origin, so the views are served there, behind the same authentication, with an unguessable MAC in the URL and a sandbox CSP.
  • The chat client is now agent-neutral. An agent's /api/state can carry ui: { sources: false, examplePrompt, empty } to hide the MCP sources sidebar and set its own placeholder and empty state. Views can grow to 2,400px. MCP-UI prompt, tool and intent messages from a view become the user's next turn (the table's Why? buttons use this), and link opens a new tab.
  • The lab tested five things. Its 29 checks covered the Overdue A/R rule, the summary/view split, display hints picking the layout, the chat client, and A2A view artifacts. The smoke now checks the split and that views stay with the conversation. The A2A checks moved to Lab 23, where they run with a bearer token: here they ran with unauthenticated loopback, so they couldn't catch a view server that ignored authentication. Giving the model the whole tool result fails the narrowed smoke 3/7.