Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions docs/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ packages/copilotkit_flutter Flutter, re-exports copilotkit
activities with `a2ui_flutter` and its catalogs
(A2uiActivityRenderer), kept per thread in an
A2uiSurfaceRegistry with their lifecycle statuses
(A2uiSurfaceView), and hides its plumbing rows from
the chat
(A2uiSurfaceView), sends the user's actions back to
the agent (A2uiActionDispatcher), and hides its
plumbing rows from the chat
agent CopilotAgentScope, AgentStateBuilder
tools CopilotTool, CopilotHumanInTheLoop, ToolRenderer,
ToolCallWidget
Expand Down Expand Up @@ -336,6 +337,17 @@ calls and the `a2ui_action_context_*` messages; `CopilotChatProvider` maps
rows with the filters of the scopes above it, so `chat` learns names to
hide without importing `a2ui`.

The actions a surface emits go back to the agent
([ADR 0022](adr/0022-a2ui-user-actions.md)). The registry listens to its
processor's `onAction` and hands each action, as an `A2uiUserAction`, to an
`A2uiActionDispatcher`, which `A2uiActivityRenderer` builds with the
nearest `CopilotKit` client. The dispatcher calls `runAgent` with no message
and the action as run-scoped `forwardedProps` (`a2uiAction.userAction`),
and reads the end of the turn into an `A2uiActionStatus` the registry keeps
per surface: pending, failed, or dropped when `runAgent` returned `false`.
With `reportErrorsToAgent`, the registry also batches its errors into one
`a2ui_error` action per turn.

An agent opened on an existing thread, or switched to one, gets its
conversation back through `AgentRunner`'s `ThreadConnector`: it clears the
agent, asks the transport to `connect` to the thread, and applies the replayed
Expand Down
97 changes: 97 additions & 0 deletions docs/adr/0022-a2ui-user-actions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# 0022. A2UI user actions

**Status:** Accepted

## Context

[#96](https://github.com/VGVentures/copilotkit_flutter_sdk/issues/96)
asks for the actions a user takes on an A2UI surface, like a button tap,
to reach the agent, with loading and failed states.
[#246](https://github.com/VGVentures/copilotkit_flutter_sdk/issues/246)
adds an opt-in `reportErrorsToAgent` that sends the surfaces' errors the
same way. What the runtime and the renderers do, checked in
`@ag-ui/a2ui-middleware` 0.0.10 and 0.0.16, CopilotKit React 1.79 and
`a2ui_core` 0.10.0:

- The middleware reads the action from
`forwardedProps.a2uiAction.userAction`: `{name, surfaceId,
sourceComponentId?, context?, timestamp?}`. It writes the history entry
itself: 0.0.10 appends a `log_a2ui_event` tool call and its result, and
0.0.16 a user message named `a2ui_action_context_<43 base64url chars>`
whose content is `A2UI user action:` and the action's JSON, which it
filters out of `RUN_STARTED` and `MESSAGES_SNAPSHOT`. Both serialize the
whole `userAction`, so extra fields reach the agent.
- React's `runA2UIAction` sets the prop on the client, calls `runAgent`
without a message and removes the prop in `finally`. Its provider builds
the `userAction` from the renderer's action: `name`, `surfaceId`,
`sourceComponentId`, `context`, `timestamp`.
- `a2ui_core` emits an `A2uiRendererAction` on the surface group's
`onAction`, with the `context` resolved against the surface's data. It is
the same in v0.9 and v1.0: only the binding syntax of the component
differs (`{"path": ...}` against `{"@path": ...}`), and v1.0 may add
`metadata`.
- `runAgent` returns `false`, and sends nothing, while the agent is in a
turn ([ADR 0014](0014-run-agent-during-a-turn.md)).

The synthetic fixtures `a2ui_action_round_trip_0_0_10` and
`a2ui_action_round_trip_0_0_16` pin the outgoing props and the update that
comes back until #247 records real ones.

## Decision

- **The wire.** `A2uiActionDispatcher` sends an `A2uiUserAction` with
`runAgent(agentId, forwardedProps: action.forwardedProps)` and no
message ([ADR 0017](0017-run-scoped-forwarded-props.md)), so the action
reaches one run and never leaks into the next. The `userAction` is the
A2UI `action` body (`A2uiRendererAction.toJson` without the null
fields), React's five fields plus `userMessage`, `catalogId` and
`metadata` when the action has them: the spec defines them for the agent,
and both middleware versions pass them through.
- **Statuses next to the surface statuses.** The registry that owns the
processor also listens to its actions, and keeps an `A2uiActionStatus`
per surface in `actionStatuses`, with `actionStatusChanges`, the way it
keeps `statuses`. `A2uiSurfaceView` shows an `action` builder's row under
the painted surface; `A2uiActivityRenderer` and `CopilotA2ui` take the
same builder, like `building`, `failed` and `rejected`. A turn that ends
well clears the status.
- **A busy agent drops, and says so.** An action taken while the agent is
in a turn is `A2uiActionDropped`: nothing is sent and nothing is queued.
A surface ignores its own actions while one of them is pending, so a
double tap sends one action and the pending row stays.
- **Error reports are batched and bounded.** With `reportErrorsToAgent`,
the registry reports errors as an action named `a2ui_error` whose
`context.errors` lists each error's `code`, `message`, `surfaceId`,
`messageId` and `path` when known:
- Errors raised during a turn wait for it to end and go in one action,
because an action sent during a turn would be dropped.
- Errors of activities applied out of a turn, like a thread's replay,
are history and are not reported.
- After a report, nothing more is reported until the user acts again,
by an action or a message, so an agent that keeps sending a bad
surface is not asked again and again.

## Alternatives

- **Disable the surface while the agent runs.** Rejected: `a2ui_flutter`
has no way to disable components from outside, and graying out every
surface during every turn hides the cause. The dropped status says why
nothing happened.
- **Queue the action for the end of the turn.** Rejected for the reasons
ADR 0014 gives for messages; it can be added on top of the dropped
status.
- **Set the prop client-wide, like React.** Rejected in ADR 0017.
- **Send React's five fields only.** Rejected: it drops the
`userMessage` the A2UI spec defines for the agent, and the middleware
takes the whole object.
- **One error report per error.** Rejected: each would start a turn, and
all but the first would be dropped.

## Consequences

- An action shows up in the chat as whatever the agent answers; the
middleware's history entry stays hidden by `hiddenChatRows`, whose
`a2ui_action_context_` prefix matches what 0.0.16 writes.
- An action needs a `CopilotKit` client above the renderer; without one it
goes nowhere.
- An error raised just as the user sends a message can be dropped with its
report; the report is logged with the error either way.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{"fixture":{"source":"synthetic","basedOn":"@ag-ui/a2ui-middleware 0.0.10","note":"An A2UI user action round trip. The first run renders a list surface whose cards are bound to /items/N and whose refresh button's action context reads local state: a literal itemIndex, pinnedIds bound to /pinnedIds and filterMax bound to /filter/max. The user taps refresh: the second run starts with no user message and carries the action in forwardedProps.a2uiAction.userAction, as the client sends it (the timestamp is the only field that differs per tap). processUserAction appends a synthetic log_a2ui_event assistant tool call and its tool result to the agent's messages, which the agent's RUN_STARTED input echoes; the agent then calls refresh_item, whose result updates only /items/2. A closing MESSAGES_SNAPSHOT exposes the synthetic log_a2ui_event pair."}}
{"type":"RUN_STARTED","threadId":"thread-a2ui-action","runId":"run-items-1","protocolVersion":"1.0","input":{"threadId":"thread-a2ui-action","runId":"run-items-1","state":{},"messages":[{"id":"user-1","role":"user","content":"Three suggestions under 30."}],"tools":[],"context":[],"forwardedProps":{"a2uiCatalogAvailable":true,"injectA2UITool":true}}}
{"type":"TOOL_CALL_START","toolCallId":"call-1","toolCallName":"render_a2ui","parentMessageId":"assistant-1"}
{"type":"ACTIVITY_SNAPSHOT","messageId":"a2ui-surface-call-1","activityType":"a2ui-surface","content":{"status":"building"},"replace":true}
{"type":"TOOL_CALL_ARGS","toolCallId":"call-1","delta":"{\"surfaceId\":\"items\",\"components\":[{\"id\":\"root\",\"component\":\"Column\",\"children\":[\"card0\",\"card1\",\"card2\",\"refresh-2\"]},{\"id\":\"card0\",\"component\":\"Text\",\"text\":{\"path\":\"/items/0/title\"}},{\"id\":\"card1\",\"component\":\"Text\",\"text\":{\"path\":\"/items/1/title\"}},{\"id\":\"card2\",\"component\":\"Text\",\"text\":{\"path\":\"/items/2/title\"}},{\"id\":\"refresh-2\",\"component\":\"Button\",\"child\":\"refresh-2-label\",\"action\":{\"event\":{\"name\":\"refresh_item\",\"context\":{\"itemIndex\":2,\"pinnedIds\":{\"path\":\"/pinnedIds\"},\"filterMax\":{\"path\":\"/filter/max\"}}}}},{\"id\":\"refresh-2-label\",\"component\":\"Text\",\"text\":\"Refresh\"}],\"data\":{\"items\":[{\"id\":\"a\",\"title\":\"Lamp\",\"price\":24},{\"id\":\"b\",\"title\":\"Mug\",\"price\":12},{\"id\":\"c\",\"title\":\"Plant\",\"price\":28}],\"pinnedIds\":[\"a\",\"b\"],\"filter\":{\"max\":30}}}"}
{"type":"ACTIVITY_SNAPSHOT","messageId":"a2ui-surface-call-1","activityType":"a2ui-surface","content":{"a2ui_operations":[{"version":"v0.9","createSurface":{"surfaceId":"items","catalogId":"https://a2ui.org/specification/v0_9/basic_catalog.json"}},{"version":"v0.9","updateComponents":{"surfaceId":"items","components":[{"id":"root","component":"Column","children":["card0","card1","card2","refresh-2"]},{"id":"card0","component":"Text","text":{"path":"/items/0/title"}},{"id":"card1","component":"Text","text":{"path":"/items/1/title"}},{"id":"card2","component":"Text","text":{"path":"/items/2/title"}},{"id":"refresh-2","component":"Button","child":"refresh-2-label","action":{"event":{"name":"refresh_item","context":{"itemIndex":2,"pinnedIds":{"path":"/pinnedIds"},"filterMax":{"path":"/filter/max"}}}}},{"id":"refresh-2-label","component":"Text","text":"Refresh"}]}},{"version":"v0.9","updateDataModel":{"surfaceId":"items","path":"/","value":{"items":[{"id":"a","title":"Lamp","price":24},{"id":"b","title":"Mug","price":12},{"id":"c","title":"Plant","price":28}],"pinnedIds":["a","b"],"filter":{"max":30}}}}]},"replace":true}
{"type":"TOOL_CALL_END","toolCallId":"call-1"}
{"type":"TOOL_CALL_RESULT","messageId":"tool-1","toolCallId":"call-1","content":"{\"status\":\"rendered\"}","role":"tool"}
{"type":"RUN_FINISHED","threadId":"thread-a2ui-action","runId":"run-items-1"}
{"type":"RUN_STARTED","threadId":"thread-a2ui-action","runId":"run-items-2","protocolVersion":"1.0","input":{"threadId":"thread-a2ui-action","runId":"run-items-2","state":{},"messages":[{"id":"user-1","role":"user","content":"Three suggestions under 30."},{"id":"assistant-1","role":"assistant","content":"","toolCalls":[{"id":"call-1","type":"function","function":{"name":"render_a2ui","arguments":"{\"surfaceId\":\"items\",\"components\":[{\"id\":\"root\",\"component\":\"Column\",\"children\":[\"card0\",\"card1\",\"card2\",\"refresh-2\"]},{\"id\":\"card0\",\"component\":\"Text\",\"text\":{\"path\":\"/items/0/title\"}},{\"id\":\"card1\",\"component\":\"Text\",\"text\":{\"path\":\"/items/1/title\"}},{\"id\":\"card2\",\"component\":\"Text\",\"text\":{\"path\":\"/items/2/title\"}},{\"id\":\"refresh-2\",\"component\":\"Button\",\"child\":\"refresh-2-label\",\"action\":{\"event\":{\"name\":\"refresh_item\",\"context\":{\"itemIndex\":2,\"pinnedIds\":{\"path\":\"/pinnedIds\"},\"filterMax\":{\"path\":\"/filter/max\"}}}}},{\"id\":\"refresh-2-label\",\"component\":\"Text\",\"text\":\"Refresh\"}],\"data\":{\"items\":[{\"id\":\"a\",\"title\":\"Lamp\",\"price\":24},{\"id\":\"b\",\"title\":\"Mug\",\"price\":12},{\"id\":\"c\",\"title\":\"Plant\",\"price\":28}],\"pinnedIds\":[\"a\",\"b\"],\"filter\":{\"max\":30}}}"}}]},{"id":"tool-1","role":"tool","toolCallId":"call-1","content":"{\"status\":\"rendered\"}"},{"id":"assistant-log","role":"assistant","content":"","toolCalls":[{"id":"call-log","type":"function","function":{"name":"log_a2ui_event","arguments":"{\"name\":\"refresh_item\",\"surfaceId\":\"items\",\"sourceComponentId\":\"refresh-2\",\"timestamp\":\"2026-11-30T12:00:00.000Z\",\"context\":{\"itemIndex\":2,\"pinnedIds\":[\"a\",\"b\"],\"filterMax\":30}}"}}]},{"id":"tool-log","role":"tool","toolCallId":"call-log","content":"User performed action \"refresh_item\" on surface \"items\" (component: refresh-2). Context: {\"itemIndex\":2,\"pinnedIds\":[\"a\",\"b\"],\"filterMax\":30}"}],"tools":[],"context":[],"forwardedProps":{"a2uiCatalogAvailable":true,"a2uiAction":{"userAction":{"name":"refresh_item","surfaceId":"items","sourceComponentId":"refresh-2","timestamp":"2026-11-30T12:00:00.000Z","context":{"itemIndex":2,"pinnedIds":["a","b"],"filterMax":30}}},"injectA2UITool":true}}}
{"type":"TOOL_CALL_START","toolCallId":"call-2","toolCallName":"refresh_item","parentMessageId":"assistant-2"}
{"type":"TOOL_CALL_ARGS","toolCallId":"call-2","delta":"{\"index\":2}"}
{"type":"TOOL_CALL_END","toolCallId":"call-2"}
{"type":"TOOL_CALL_RESULT","messageId":"tool-2","toolCallId":"call-2","content":"{\"a2ui_operations\":[{\"version\":\"v0.9\",\"updateDataModel\":{\"surfaceId\":\"items\",\"path\":\"/items/2\",\"value\":{\"id\":\"c\",\"title\":\"Fern\",\"price\":22}}}]}","role":"tool"}
{"type":"ACTIVITY_SNAPSHOT","messageId":"a2ui-surface-call-2","activityType":"a2ui-surface","content":{"a2ui_operations":[{"version":"v0.9","updateDataModel":{"surfaceId":"items","path":"/items/2","value":{"id":"c","title":"Fern","price":22}}}]},"replace":true}
{"type":"MESSAGES_SNAPSHOT","messages":[{"id":"user-1","role":"user","content":"Three suggestions under 30."},{"id":"assistant-1","role":"assistant","content":"","toolCalls":[{"id":"call-1","type":"function","function":{"name":"render_a2ui","arguments":"{\"surfaceId\":\"items\",\"components\":[{\"id\":\"root\",\"component\":\"Column\",\"children\":[\"card0\",\"card1\",\"card2\",\"refresh-2\"]},{\"id\":\"card0\",\"component\":\"Text\",\"text\":{\"path\":\"/items/0/title\"}},{\"id\":\"card1\",\"component\":\"Text\",\"text\":{\"path\":\"/items/1/title\"}},{\"id\":\"card2\",\"component\":\"Text\",\"text\":{\"path\":\"/items/2/title\"}},{\"id\":\"refresh-2\",\"component\":\"Button\",\"child\":\"refresh-2-label\",\"action\":{\"event\":{\"name\":\"refresh_item\",\"context\":{\"itemIndex\":2,\"pinnedIds\":{\"path\":\"/pinnedIds\"},\"filterMax\":{\"path\":\"/filter/max\"}}}}},{\"id\":\"refresh-2-label\",\"component\":\"Text\",\"text\":\"Refresh\"}],\"data\":{\"items\":[{\"id\":\"a\",\"title\":\"Lamp\",\"price\":24},{\"id\":\"b\",\"title\":\"Mug\",\"price\":12},{\"id\":\"c\",\"title\":\"Plant\",\"price\":28}],\"pinnedIds\":[\"a\",\"b\"],\"filter\":{\"max\":30}}}"}}]},{"id":"tool-1","role":"tool","toolCallId":"call-1","content":"{\"status\":\"rendered\"}"},{"id":"assistant-log","role":"assistant","content":"","toolCalls":[{"id":"call-log","type":"function","function":{"name":"log_a2ui_event","arguments":"{\"name\":\"refresh_item\",\"surfaceId\":\"items\",\"sourceComponentId\":\"refresh-2\",\"timestamp\":\"2026-11-30T12:00:00.000Z\",\"context\":{\"itemIndex\":2,\"pinnedIds\":[\"a\",\"b\"],\"filterMax\":30}}"}}]},{"id":"tool-log","role":"tool","toolCallId":"call-log","content":"User performed action \"refresh_item\" on surface \"items\" (component: refresh-2). Context: {\"itemIndex\":2,\"pinnedIds\":[\"a\",\"b\"],\"filterMax\":30}"},{"id":"assistant-2","role":"assistant","content":"","toolCalls":[{"id":"call-2","type":"function","function":{"name":"refresh_item","arguments":"{\"index\":2}"}}]},{"id":"tool-2","role":"tool","toolCallId":"call-2","content":"{\"a2ui_operations\":[{\"version\":\"v0.9\",\"updateDataModel\":{\"surfaceId\":\"items\",\"path\":\"/items/2\",\"value\":{\"id\":\"c\",\"title\":\"Fern\",\"price\":22}}}]}"},{"id":"a2ui-surface-call-1","role":"activity","activityType":"a2ui-surface","content":{"a2ui_operations":[{"version":"v0.9","createSurface":{"surfaceId":"items","catalogId":"https://a2ui.org/specification/v0_9/basic_catalog.json"}},{"version":"v0.9","updateComponents":{"surfaceId":"items","components":[{"id":"root","component":"Column","children":["card0","card1","card2","refresh-2"]},{"id":"card0","component":"Text","text":{"path":"/items/0/title"}},{"id":"card1","component":"Text","text":{"path":"/items/1/title"}},{"id":"card2","component":"Text","text":{"path":"/items/2/title"}},{"id":"refresh-2","component":"Button","child":"refresh-2-label","action":{"event":{"name":"refresh_item","context":{"itemIndex":2,"pinnedIds":{"path":"/pinnedIds"},"filterMax":{"path":"/filter/max"}}}}},{"id":"refresh-2-label","component":"Text","text":"Refresh"}]}},{"version":"v0.9","updateDataModel":{"surfaceId":"items","path":"/","value":{"items":[{"id":"a","title":"Lamp","price":24},{"id":"b","title":"Mug","price":12},{"id":"c","title":"Plant","price":28}],"pinnedIds":["a","b"],"filter":{"max":30}}}}]}},{"id":"a2ui-surface-call-2","role":"activity","activityType":"a2ui-surface","content":{"a2ui_operations":[{"version":"v0.9","updateDataModel":{"surfaceId":"items","path":"/items/2","value":{"id":"c","title":"Fern","price":22}}}]}}]}
{"type":"RUN_FINISHED","threadId":"thread-a2ui-action","runId":"run-items-2"}
Loading