MessageStore behavior
MessageStore connects your transport adapter to <A2UI>. Use push for
incremental protocol messages, replace for a complete final response, and
clear when you only want to discard the buffered messages.
The store holds raw messages. <A2UI> owns the live surfaces, component
resources, and data model produced by processing them. These are separate:
clearing the buffer does not clear the UI, and user input can change the live
data model without adding a message to the buffer.
Operations at a glance
push, replace, and clear return void. They change the buffer synchronously;
returning from a call does not mean that rendering has completed or that a
replacement passed protocol checks. Subscribers are notified once for a nonempty
push, once for every replace, and once for clear when the buffer was
nonempty. push([]) and clearing an already-empty buffer are no-ops.
getSnapshot() keeps the same array reference between mutations. The store
copies and freezes the array, but does not deep-copy or freeze the message
objects. Treat submitted messages and nested JSON as immutable; create new
values for subsequent updates. A raw snapshot does not include local input edits
and should not be treated as an export of all live UI state.
Streaming and a complete final response
This example appends a streaming update, then replaces the history with a complete final snapshot:
In an application, these calls happen as transport responses arrive. For REST,
use replace when the response is a complete snapshot, or push when it is an
update to an existing surface. For SSE, append validated incremental envelopes
and use the final validated done payload as the replacement source.
Keep cancellation in the transport adapter: cancel obsolete prompt and action
requests with AbortController and ignore late responses from a previous turn.
Do not expose provider credentials in the client or accept untrusted endpoint
overrides. The store does not own requests or decide which response is current.
What push means
push appends protocol operations. A surface must be created before its
component or data updates. Repeating createSurface for an existing surface
without first deleting it is rejected.
For updateComponents, supplied component IDs are inserted or updated; omitted
IDs remain in the surface. Each supplied component is its new description, not
a patch of selected props. The current push path does not deep-deduplicate
component descriptions: repeating an identical component can notify its resource
and execute the component again. ReactLynx's later rendering diff may avoid
native node changes even though component code ran.
updateDataModel replaces the value at its JSON Pointer path; an omitted or
empty path targets the root, and null deletes the targeted value. These are
explicit server writes, so they can overwrite local input. The local-input merge
policy below applies to replace.
Multiple messages passed in one push generate one buffer notification.
replace(fullMessages) followed by push(updates) in the same batch processes
the replacement first, then its appended tail. If another replace arrives
before consumption, the latest full buffer takes precedence over discarded
messages.
What counts as a complete replacement
The replacement must describe the state independently, including createSurface
before updates for each surface. It may be a complete operation history or a
compacted snapshot. A full components array without surface creation is still
only an update; use push for that payload.
The live UI can already contain a surface with the same ID. A single
createSurface in the replacement is valid because target state is built
separately. Repeated creation of the same surface within the replacement, without
an intervening deletion, is an error.
The resulting state covers the entire store. Surfaces absent from it are removed,
as are component IDs absent from a retained surface's resulting component map.
With a full history, earlier definitions still count unless later operations
remove their state. Omission from just the last updateComponents message does
not delete an earlier component.
Diff and component reuse
Replacement compares the resulting state rather than message count, object identity alone, or the original streaming batch boundaries:
- Components are matched by surface ID and component ID, then compared recursively as JSON. Object property order is ignored; array order matters.
- Unchanged component descriptions and resources are reused. When the entire rendered state and active surface are unchanged, no component-resource update is published.
- Changed components and relevant parents are notified. Data bindings update their consumers; child membership changes may also refresh the root.
- Parent updates can execute ordinary descendants too. Incremental resource updates do not guarantee that only changed component functions run.
The current surface and root resource remain mounted while their identities are retained. Replacing the active surface or recreating its root resource resets the rendered subtree. Component type changes or changes to parent structure can also reset component state. Keep IDs stable when they represent the same UI entity.
There is still a cost to building and comparing the complete target state.
replace is intended for full responses, not as a guarantee of work proportional
only to the changed fields.
Parents that read child definitions are detected using catalog ComponentId /
ChildList reference schemas. Without a schema, the built-in child, children,
trigger, content, and tabs[].child fields are recognized. Declare custom
child props in the catalog schema. Ordinary text or metadata equal to an ID is
not a reference.
Local input and arrays
Replacement compares the previous server data, the new server data, and the live data containing local edits:
When an array changes, matching follows these rules:
- If all three arrays have unique string or finite-number
idfields, match items byid. Otherwise try uniquekeyfields with the same requirements. - For matched items, preserve local fields unchanged by the server. The new server array determines ordering and membership, including inserted and removed items.
- Without usable identities, merge by index only when all three lengths agree. If lengths differ, replace with the server array.
For example, if the server changes item A but leaves item B's label unchanged,
an edited local label on B survives when B can be matched. Provide stable
identities for reorderable collections; index matching cannot identify which
business item moved. These rules preserve data-model values, not component
instances across every possible list reorder.
Rejected replacement and recovery
Before touching live state, the processor checks the replacement in a separate state. Any reported protocol error rejects the whole replacement. Common causes:
Rejection preserves the live UI and its consumption position. The raw buffer
already contains the rejected snapshot, so getSnapshot() can differ from what
is on screen. Its appended tail is not applied to the old live state. A later
push can cause another attempt and another error; it does not repair the
rejected replacement by itself.
The renderer logs:
Inspect message.error in <A2UI onMessage={...}> for code, message, and,
when present, surfaceId / path. The warning itself is only a summary. Keep
forwarding other protocol events through your transport as needed.
Recover in one of two ways:
Choose one recovery path. clear() does not reset protocol state: repeating
createSurface for a retained surface is still invalid. Use replace for a
complete response.
store.replace() returns void and does not synchronously validate messages.
Direct MessageProcessor.replaceMessages() consumers receive true on success
and false on protocol rejection. All incoming data should still be validated
by the transport; these protocol checks are not a general malformed-JSON guard.
RPC, empty states, and session lifetime
Replacement ignores callRendererFunction and agentFunctionResponse envelopes
so historical calls are not executed again. New RPC calls and responses must use
push, even if they arrive in the final transport frame. A response delivered
only through replace will not resolve its pending call.
renderEmpty is a render function, not a one-time event. renderFallback is
different: it renders while an existing resource is pending.
Passing a non-null store after null starts a fresh session and consumes that
store's buffered messages. Directly swapping one non-null store for another does
not reset the session. For a new turn, use a fresh store with a turn-specific
<A2UI key={turnId}>, as described in the overview.

