A2UI is a JSON protocol for agent-driven interfaces: an agent emits declarative component trees and data models, a client renderer displays them, and structured user actions flow back to update the same interface.
Moon A2UI implements the A2UI 1.0 protocol and client/agent runtime in pure MoonBit. The same library compiles to Native and JavaScript (native and js targets), enabling applications to build messages, validate catalogs, track surface state, evaluate reactive bindings, and route bidirectional function calls without coupling to a UI framework, model provider, or npm runtime.
中文说明 · Live Demo · Mooncakes API · Runtime Guide · Scenarios · Development
Your Agent / App (MoonBit Native or JS)
│ constructs Message / handles Action
▼
Moon A2UI Core (Messages · Catalogs · SurfaceStore · Runtime)
│ JSON wire messages ("version": "1.0")
▼
Your Transport (HTTP · WebSocket · JS Bridge)
▼
A2UI 1.0 Renderer (e.g. @a2ui/lit) ──► user Action / RPC JSON back to MoonBit
| Layer | Responsibility |
|---|---|
| Moon A2UI | Wire schema & catalog validation, atomic surface state, JSON Pointer updates, dynamic bindings, Basic functions, and bidirectional RPC correlation |
| Your Application | Transport framing, authentication, business authorization, model calls, and platform side effects (such as openUrl) |
| A2UI Renderer | Rendering components from A2UI 1.0 catalogs and emitting user actions or renderer function responses |
Versions & targets: A2UI wire messages carry
"version": "1.0", independent of the SDK package version (0.1.2). Verified compilation targets arenativeandjs.
Tested with MoonBit 0.1.20261117 (moonc v0.10.21+06da1ed44) and CI 1.0.0-rc1+3c0520448.
Add the package in your application directory:
moon add Delthin/moon_a2ui@0.1.2
Import the root package in your moon.pkg:
import { "Delthin/moon_a2ui" @a2ui }
For local source development, place this checkout next to your application and run moon work init app moon-a2ui from their parent directory.
This executable example builds an initial surface with a bound Text component, serializes the wire message, parses an incoming user Action, applies a data-model update, and verifies the resolved component property:
///|
test {
let runtime = @a2ui.Runtime::new()
let create = @a2ui.Message::create_surface(
"greeting",
catalog_id=@a2ui.basic_catalog_id,
components=[
@a2ui.Component::new("root", "Text", properties={
"text": { "@path": "/greeting" },
}),
],
data_model={ "greeting": "Hello" },
)
runtime.apply(create)
let wire_out = create.to_json().stringify()
assert_true(wire_out.contains("\"createSurface\""))
let wire_in =
#|{"version":"1.0","action":{"surfaceId":"greeting","sourceComponentId":"save","name":"rename","timestamp":"2026-12-01T12:00:00Z","context":{"greeting":"你好"}}}
let action = @a2ui.Action::parse(wire_in)
guard action.name == "rename" &&
action.context() is { "greeting": String(next), .. } else {
fail("unexpected action")
}
runtime.apply(
@a2ui.Message::update_data(
action.surface_id,
Json::string(next),
path="/greeting",
),
)
assert_true(
runtime.component_properties("greeting", "root") ==
({ "text": "你好" } : Json),
)
}
Send create.to_json().stringify() over your transport to an A2UI 1.0 renderer, and pass incoming payloads to Action::parse or RendererMessage::parse. See the Runtime Guide for reactive bindings, watchers, Basic functions, and bidirectional RPC.
| Area | Key Types & Entry Points | What It Handles |
|---|---|---|
| Wire Messages | AgentMessage, RendererMessage, Message, Action, FunctionCall, FunctionResponse, RendererError |
Decodes and constructs all 6 agent-to-renderer and 4 renderer-to-agent A2UI 1.0 message families |
| Surface State | SurfaceStore |
Multi-surface isolation, atomic rollback on invalid updates, JSON Pointer updates, progressive component refs with validate_complete, metadata/extensions, and renderer_data_model |
| Catalogs & Capabilities | Catalog, CatalogRegistry, AgentCapabilities |
Embedded Basic Catalog (18 components), registered and consented inline catalogs, cross-catalog references, Unicode UAX #31 identifiers, and schema validation without network fetches |
| Reactive Runtime & RPC | Runtime, EvaluationContext, Evaluation |
Absolute/scoped @path bindings, template @index, Basic functions, property watchers, bidirectional callAgentFunction/callRendererFunction, resumable actions, and host activation boundaries |
Use Message + Action + SurfaceStore when an agent only needs typed message construction, catalog validation, and surface state snapshots; use Runtime + AgentMessage / RendererMessage when you also evaluate bindings, run checks, watch properties, or handle bidirectional function calls.
Key protocol defaults and bounds:
- JSON text parsing rejects duplicate keys, caps nesting depth at 64, and preserves number literals across wire round-trips (numeric Basic functions compute with
Double). - Data updates use path replacement rather than deep object merge;
nulldeletes an object field, keeps an array slotnull, or resets the root to{}. JSON Pointer supports~0/~1and bounds array growth at index 10,000. - Basic formatting uses
en-US; regular expressions use the bounded A2UI safe subset.
Open the Live Demo to run three shared MoonBit scenarios rendered with the official @a2ui/lit renderer (the demo UI is in Simplified Chinese; no model service or API key is required):
| Scenario | UI Tab | What It Demonstrates |
|---|---|---|
| Project Name Form | 信息填写 |
Structured input binding, empty-name validation, and in-place surface update |
| Deployment Budget | 方案选择 |
ChoicePicker + Slider action context computing an example monthly estimate |
| Protocol Checks | 任务跟踪 |
Template list + @checks executing message round-trip, catalog rejection, and cycle rejection |
Clone the repository and build both targets from the repository root:
git clone https://github.com/Delthin/moon-a2ui.git
cd moon-a2ui
moon update
moon build examples/browser --target js --release
moon build examples/native --target native --release
Start the Native HTTP service (127.0.0.1:4180) in one terminal (runs without Node.js):
./_build/native/release/build/examples/native/native.exe
In another terminal, build and preview the browser UI from examples/browser (Node.js is used only for the browser UI):
cd examples/browser
npm ci
npm run build
npm run preview
Open http://127.0.0.1:4178 and switch between JavaScript (runs the compiled MoonBit JS module in-browser) and Native (proxies requests to 127.0.0.1:4180). Open http://127.0.0.1:4178/?backend=native to start directly on Native without downloading the MoonBit JS engine. See the Scenario Walkthrough for expected inputs and outputs.
| Path | Role |
|---|---|
/ (*.mbt) |
Core A2UI 1.0 protocol, catalog validation, SurfaceStore, and Runtime library |
examples/form |
Shared form-session example consumed by the scenario runner |
examples/scenarios |
Shared three-scenario session engine compiled for both Native and JS |
examples/native |
Standalone MoonBit Native HTTP service (127.0.0.1:4180) |
examples/browser |
Vite + @a2ui/lit browser demo and JS ESM bridge |
bench |
Micro-benchmark comparing MoonBit Native, MoonBit JS, and @a2ui/web_core |
conformance |
Runner checking pinned official A2UI 1.0 fixture groups |
internal/* |
Vendored jsonschema, bounded safe_regex, interpolation, and formatting |
docs/ |
Runtime Guide, Scenarios, and Development Guide |
- The browser example pins
@a2ui/lit 0.16.0/@a2ui/web_core 0.16.1, which depend transitively on Zod 3 (GHSA-mrhx-c7xc-m3w5). - To contain this in the example,
examples/nativebinds to loopback (127.0.0.1:4180), checks request origins, and caps request bodies at 64 KiB, whileexamples/browserserves only fixed scenarios and validates response size, depth, collection length, and node count before handing packets to the renderer. - Accepting arbitrary remote A2UI messages or catalogs in a browser application requires resolving the upstream Zod 3 dependency first. The MoonBit core library itself has no npm dependencies.
- Wire contract and Basic Catalog follow A2UI 1.0 (
Apache-2.0). - JSON Schema validation vendors and adapts
colmugx/json-schema 0.2.0(Apache-2.0; see internal/jsonschema/README.md). - Safe regex, expression interpolation, and
en-USformatting follow the pinned A2UI reference; currency symbols derive from Unicode CLDR 48.0 (Unicode-3.0). - Full attribution is in NOTICE. Moon A2UI is licensed under Apache-2.0.
