Skip to content

feat(example): A2UI screen with a custom catalog, in-place refresh and actions - #258

Open
pietroid wants to merge 1 commit into
test/a2ui-recorded-fixturesfrom
feat/a2ui-example
Open

pietroid wants to merge 1 commit into
test/a2ui-recorded-fixturesfrom
feat/a2ui-example

Conversation

@pietroid

@pietroid pietroid commented Dec 3, 2026

Copy link
Copy Markdown
Collaborator

Status

READY

Description

Closes #248. Last PR of the A2UI stack (#71). It builds on #257 (test/a2ui-recorded-fixtures).

The example gets an A2UI screen. An app bar button in the chat opens it. The screen chats with a new a2ui agent of the example runtime.

App (example/lib/a2ui/)

  • ItemCatalog is an A2UI v0.9 catalog with ItemList, ItemCard and RangeFilter. It also holds the basic Column, Row, Text and Divider, because a v0.9 surface draws on one catalog. Each component is a class that holds its schema (CommonSchemas) and its builder. Its widget is plain Flutter, in widgets/.
  • A2uiPage wraps the chat in CopilotA2ui(protocol: A2uiProtocol.v0_9, catalogs: ...). It reuses the chat's MessageList and Composer. An empty chat offers the "three suggestions under $30" prompt.
  • Per-card rebuilds. The agent lists the cards by id. Each card is bound to its own /items/<index>. The refresh_item update of /items/2 therefore rebuilds that card and no other. The replay test checks this: the ItemCard widgets of cards 1 and 2 are the same instances before and after the refresh.
  • Local state in the action. Pins live in the surface's data at /pinnedIds, and the card toggles its own itemId there. RangeFilter writes /filter/max. The refresh action's context, {itemIndex, pinnedIds, filterMax}, reads both.

Runtime (example/tool/runtime/)

  • server.ts sets a2ui: { injectA2UITool: true, agents: ["a2ui"] }, so the default and document agents are unchanged.
  • a2ui_agent.ts adds the a2ui agent and the fixed-schema server tool refresh_item. The tool returns {"a2ui_operations": [updateDataModel{path: "/items/<i>"}]}. It has tests in a2ui_agent.test.ts, which npm test runs, and it adds zod 3.25.76 as a direct dependency (already in the lockfile).
  • The recorder has a new item_refresh case for this flow. It records with the example's agent id and catalog context into example/test/fixtures/a2ui/{1.79.0,1.91.0}/. Its fixtures are kept apart from the SDK's, so copilotkit_flutter's fixture test still covers the same cases.

Tests

  • test/a2ui/widgets/a2ui_page_test.dart replays both recordings through the real client and page (test/helpers/a2ui_wire_replay.dart is copied from copilotkit_flutter). It checks that:
    • the list renders;
    • two pins and a filter move send context: {itemIndex: 2, pinnedIds: ["a","b"], filterMax: 25};
    • only card 3 rebuilds, and the pins stay.
  • test/a2ui_live_test.dart runs the same flow against a real runtime and model. It is skipped unless COPILOTKIT_LIVE_RUNTIME_URL is set (ADR 0015), so it never runs on CI.
  • Unit and widget tests cover each component, widget and the catalog. The example is at 100% line coverage.

Docs. docs/flutter-quickstart.mdx does not exist in this repo. The quickstart went where the earlier A2UI docs are: "Quickstart: a custom catalog" in the copilotkit_flutter README (catalog, attach, runtime, actions). The example README has an "A2UI" section (files, flow, how to switch to v1.0, tests) and a checklist entry. ARCHITECTURE.md, CONTRIBUTING.md and the root README's demo list are updated.

Deviations from the issue, for review

  • ItemCard props. The issue's pinned and onTogglePin became itemId and pinnedIds, a list bound to /pinnedIds that the card adds itself to or removes itself from. In v0.9, a component's action can only start an agent turn or call a basic function. A pin sent to the agent would start one turn per pin, and a second pin during the first turn would be dropped. Keeping the pins in the surface's data is what lets the action's context carry them.
  • ItemList takes card ids, not a template. The cards of a template read paths relative to the list, so the agent can't give each card's action its own itemIndex. A template still renders.
  • A pushed screen, not a tab. The app has no tabs. A2UI opens from the app bar, like Document and Memories.
  • Protocol: v0.9. The runtime pins 1.79 (middleware 0.0.10, v0.9 only). The example README says how to switch to v1.0 on runtime 1.91.

Checks: flutter analyze, dart format, cspell and dart doc (0 warnings) are clean. Tests of all three packages pass at 100% coverage, and npm test in example/tool/runtime passes (14 tests). flutter build web --wasm --dart-define=RUNTIME_URL=http://localhost:8200/api/copilotkit builds.

Reviewer step: the live flow. It needs a model key, so it was not run.

# terminal 1
export OPENAI_API_KEY=<your key>
./example/tool/start_runtime.sh
# terminal 2
cd example && flutter run --dart-define=RUNTIME_URL=http://localhost:8200/api/copilotkit
  1. Tap the A2UI button in the app bar, then tap "Show me three suggestions under $30."
  2. Pin two cards, then tap refresh on the third.
  3. Only the third card changes, and the pinned cards keep their pins.

To run the same flow as a test against that runtime:

cd example
COPILOTKIT_LIVE_RUNTIME_URL=http://localhost:8200/api/copilotkit flutter test test/a2ui_live_test.dart

Type of Change

  • ✨ New feature (non-breaking change which adds functionality)
  • 🛠️ Bug fix (non-breaking change which fixes an issue)
  • ❌ Breaking change (fix or feature that would cause existing functionality to change)
  • 🧹 Code refactor
  • ✅ Build configuration change
  • 📝 Documentation
  • 🗑️ Chore

🤖 Generated with Claude Code

…d actions

Adds an A2UI screen to the example, opened from the chat's app bar, that
chats with a new `a2ui` agent of the example runtime:

- An A2UI v0.9 catalog (ItemCatalog) with ItemList, ItemCard and
  RangeFilter next to the basic Column, Row, Text and Divider, advertised
  and rendered with CopilotA2ui.
- Each ItemCard is bound to its own /items/<index>, so the refresh_item
  server tool's single updateDataModel of /items/2 rebuilds that card only.
  Pins live in the surface's data (/pinnedIds) and RangeFilter writes
  /filter/max, so the refresh action's context carries local state.
- The runtime turns the A2UI middleware on for the a2ui agent
  (injectA2UITool) and adds the fixed-schema refresh_item tool.
- The recorder gains an item_refresh case, recorded from runtimes 1.79 and
  1.91 into example/test/fixtures, which the page test replays end to end.
  A live test runs the flow against a real runtime, skipped unless
  COPILOTKIT_LIVE_RUNTIME_URL is set.
- Docs: an A2UI quickstart in the copilotkit_flutter README, an A2UI
  section and checklist in the example README.

Closes #248

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant