System Architecture
The A2UI lifecycle begins with an agent generating a JSON payload containing anA2uiDocument or a PatchDocument. The frontend receives these updates, applies security sanitization, processes any incremental patches, and renders the final state using a set of React components.
Natural Language to Code Entity Mapping
The following diagram maps high-level UI concepts to the specific code entities that implement them. UI Component Mapping Sources: apps/web/src/a2ui/renderer.tsx#90-93, apps/web/src/a2ui/patch.ts#47-52, apps/web/src/a2ui/types.ts#4-11JSON Patch Application
A2UI supports incremental updates via a versioned JSON Patch implementation (v1). This allows agents to modify existing UI surfaces (e.g., updating a progress bar or adding a row to a table) without re-sending the entire document.Patch Processing Pipeline
TheapplyPatchDocument function apps/web/src/a2ui/patch.ts#47-89 executes the following steps:
- Validation: Ensures the patch uses version
v: 1and contains at least one operation apps/web/src/a2ui/patch.ts#53-58. - Budget Check: Validates that the number of operations and path lengths are within the
DEFAULT_PATCH_BUDGETto prevent DoS attacks apps/web/src/a2ui/patch.ts#59-64. - Cloning: Creates a deep clone of the current state using
cloneJsonValueto maintain immutability apps/web/src/a2ui/patch.ts#66. - Sequential Application: Iterates through
ops(add,replace,remove) and resolves JSON pointers to target containers apps/web/src/a2ui/patch.ts#69-86.
Security Sanitization
Security is enforced at the patch level to prevent Prototype Pollution.- Pointer Validation: The
FORBIDDEN_POINTER_TOKENSset (containing__proto__,prototype, andconstructor) is checked during path resolution apps/web/src/a2ui/patch.ts#16-16. - Path Resolution: If a patch attempts to access a forbidden property, an
A2uiErrorof typeinvalid_patchis thrown apps/web/src/a2ui/patch.ts#245-251. - Markdown Sanitization: Text components using Markdown are rendered via
SanitizedMarkdownto prevent XSS apps/web/src/a2ui/renderer.tsx#73-73.
Renderer Implementation
TheA2uiRenderer component apps/web/src/a2ui/renderer.tsx#33-59 acts as the entry point for the UI surface. It iterates over the components array in an A2uiDocument and renders the appropriate React component based on the type field.
Component Types
| Type | React Implementation | Description |
|---|---|---|
text | ComponentBody | Renders plain text with optional semantic tone (e.g., success, error) apps/web/src/a2ui/renderer.tsx#68-71. |
markdown | SanitizedMarkdown | Renders safe HTML from Markdown strings apps/web/src/a2ui/renderer.tsx#72-73. |
table | A2uiTable | Uses EntityTable to display sortable, structured data apps/web/src/a2ui/renderer.tsx#99-123. |
form | A2uiForm | Renders interactive inputs (text, select, checkbox, number) apps/web/src/a2ui/renderer.tsx#130-177. |
chart | A2uiBarChart | Visualizes numeric series as bar charts apps/web/src/a2ui/renderer.tsx#92-93. |
Form Interaction and Data Flow
When an agent renders a form, theA2uiForm component manages local state for all fields. Upon submission, it triggers the onFormSubmit callback, which packages the values into an A2uiFormSubmitEvent to be sent back to the agent.
A2UI Form Data Flow
Sources: apps/web/src/a2ui/renderer.tsx#148-154, apps/web/src/a2ui/renderer.tsx#157-175, apps/web/src/a2ui/patch.ts#47-52
Testing and Quality Assurance
A2UI employs two primary testing strategies to ensure stability and security.Snapshot Tests
TheA2uiRenderer is verified using snapshot-style tests in renderer.snapshot.test.tsx. These tests:
- Render a baseline “demo” document and verify the presence of key elements (e.g., buttons, grids) apps/web/src/a2ui/tests/renderer.snapshot.test.tsx#11-21.
- Apply a
PatchDocumentand verify that the UI updates deterministically (e.g., verifying that areplaceoperation updated a text node) apps/web/src/a2ui/tests/renderer.snapshot.test.tsx#23-51.
Security Regression Tests
Thepatch.security.test.tsx file contains a suite of “bad patches” designed to test the robustness of the pointer resolution logic against prototype pollution apps/web/src/a2ui/tests/patch.security.test.tsx#9-30.
Sources: apps/web/src/a2ui/tests/renderer.snapshot.test.tsx#11-51, apps/web/src/a2ui/tests/patch.security.test.tsx#8-31