moon_a2ui

    A2UI protocol messages and surface state for MoonBit.

    a2ui
    agent
    protocol
    ui
    Download zip
    Author
    Version
    0.1.1
    License
    Apache-2.0
    Last updated
    1 hour ago
    Downloads
    5

    #Moon A2UI

    Build A2UI 1.0 interfaces and process their state in MoonBit. The same library compiles to Native and JavaScript, so an agent can construct messages, validate catalogs, evaluate bindings and handle user actions without depending on a UI framework or model provider.

    #Install

    Use MoonBit 0.1.20261117 / moonc v0.10.21+06da1ed44. In your application directory:

    moon add Delthin/moon_a2ui@0.1.1

    Import the root package in your application’s moon.pkg:

    import { "Delthin/moon_a2ui" @a2ui }

    For source development, place this checkout beside your application and run moon work init app moon-a2ui from their parent directory.

    #Create and update an interface

    A catalog defines the component properties that a renderer understands. This example binds a Text component to the surface's data model:

    ///|
    test {
    let runtime = @a2ui.Runtime::new()
    let message = @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(message)
    runtime.apply(
    @a2ui.Message::update_data("greeting", "你好", path="/greeting"),
    )
    assert_true(
    runtime.component_properties("greeting", "root") ==
    ({ "text": "你好" } : Json),
    )
    }

    Send message.to_json().stringify() through your transport to an A2UI renderer. Decode an incoming user action with Action::parse, validate its business inputs, and return further Message values. The runtime guide covers functions, reactive bindings and bidirectional calls.

    #Try Native and JavaScript execution

    Open the live demo to confirm a project name, calculate a deployment budget, or advance a protocol-check task. The hosted version executes in your browser. Follow the scenario walkthrough to run the same operations through Native. Both targets use the shared MoonBit scenario implementation and the official Lit renderer.

    Native deployment-budget scenario

    Clone the source, then build the shared JavaScript engine and the Native service 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 service in one terminal:

    ./_build/native/release/build/examples/native/native.exe

    In another terminal, run these commands from examples/browser:

    npm ci npm run build npm run preview

    Open http://127.0.0.1:4178 and select JavaScript or Native. JavaScript executes the compiled MoonBit module inside the browser. Native sends requests through the local proxy to the MoonBit HTTP service at 127.0.0.1:4180. Open http://127.0.0.1:4178/?backend=native to start directly on Native without downloading the MoonBit JavaScript engine. The Native service runs without Node.js; Node.js is used to build and serve the browser interface. No model service or API key is required.

    #Protocol behavior

    • AgentMessage and RendererMessage decode all six agent and four renderer message families. Message provides constructors for surface lifecycle and state updates; Action provides owned access to user-action context.
    • SurfaceStore validates the selected catalog before applying component changes. Updates replace definitions and data values; failed updates leave existing state intact. validate_complete checks that progressive references have resolved and that the root exists.
    • Runtime evaluates absolute and scoped bindings, template indices, Basic functions and validation rules. Property observers receive changed resolved values; metadata remains separate. Host functions enforce caller and user-activation boundaries.
    • CatalogRegistry supports registered and consented inline catalogs, cross-catalog components and calls, Unicode identifiers, composition constraints and format validation. Catalog identifiers never trigger network fetches.
    • renderer_data_model returns opted-in surface data for a transport metadata channel. It is not inserted into action.context.

    JSON text parsing rejects duplicate keys, limits nesting to 64, and preserves number representations. Pointer paths support ~0 and ~1; null removes an object field, leaves an array slot null, or resets the root to {}. Array expansion is bounded at index 10,000. Basic formatting uses en-US; regular expressions use the bounded A2UI safe subset. See runtime behavior for integration details.

    #Browser dependency security

    The pinned official renderer uses Zod 3, which is affected by GHSA-mrhx-c7xc-m3w5. The example only serves its fixed scenarios, bounds Native request bodies at 64 KiB, and checks response size, collection size, nesting and node count before the renderer validates a packet. Keep the Native service on loopback. The hosted demo runs only the included scenarios inside the browser; accepting arbitrary remote catalogs or messages requires resolving this upstream dependency issue first. The MoonBit library has no npm dependency.

    #Source and license

    The wire contract follows A2UI 1.0, with the official Basic Catalog. JSON Schema evaluation reuses vendored colmugx/json-schema; regex, expression parsing and formatting follow the fixed A2UI reference. See NOTICE and the dependency-local license files for attribution. Moon A2UI is distributed under Apache-2.0.

    ProtocolError

    pub(all) suberror ProtocolError {
    Invalid(path~ : String, reason~ : String)
    UnsupportedMessage(String)
    StateError(String)
    RuntimeError(code~ : String, reason~ : String)
    CallFailed(id~ : String, code~ : String, message~ : String)
    } derive(
    Debug
    )

    Protocol failures include a JSON-pointer-like location in the input.

    Action

    pub struct Action {
    name : String
    surface_id : String
    source_component_id : String
    timestamp : String
    // private fields
    }

    A renderer action with resolved context. Transport metadata is supplied separately by the transport and is not inserted into this envelope.

    Action::context

    Action::from_json

    Action::parse

    fn Action::parse(text : String) -> Action raise

    Action::to_json

    AgentCapabilities

    pub struct AgentCapabilities {
    accepts_inline_catalogs : Bool
    // private fields
    }

    AgentCapabilities::from_json

    AgentCapabilities::new

    fn AgentCapabilities::new(supported_catalog_ids : Array[String], accepts_inline_catalogs? : Bool) -> AgentCapabilities raise ProtocolError

    AgentCapabilities::supported_catalog_ids

    fn AgentCapabilities::supported_catalog_ids(self : AgentCapabilities) -> Array[String]

    AgentCapabilities::to_json

    AgentMessage

    pub(all) enum AgentMessage {
    SurfaceUpdate(Message)
    CallRenderer(FunctionCall)
    AgentFunctionResponse(FunctionResponse)
    }

    AgentMessage::call_renderer

    fn AgentMessage::call_renderer(id : String, call :
    Json
    , surface_id? : String) -> AgentMessage raise ProtocolError

    AgentMessage::from_json

    AgentMessage::parse

    fn AgentMessage::parse(text : String) -> AgentMessage raise

    AgentMessage::to_json

    Caller

    pub(all) enum Caller {
    Renderer
    Agent
    } derive(Eq,
    Debug
    )

    Catalog

    pub struct Catalog {
    id : String
    // private fields
    }

    An owned catalog definition and its compiled component/function validators.

    Catalog::basic

    fn Catalog::basic() -> Catalog raise ProtocolError

    Catalog::from_json

    Catalog::to_json

    CatalogRegistry

    pub struct CatalogRegistry {
    // private fields
    }

    CatalogRegistry::add

    fn CatalogRegistry::add(self : CatalogRegistry, catalog : Catalog) -> Unit raise ProtocolError

    CatalogRegistry::basic

    CatalogRegistry::capabilities

    fn CatalogRegistry::capabilities(self : CatalogRegistry, inline_catalogs? : Bool) ->
    Json

    CatalogRegistry::negotiate

    fn CatalogRegistry::negotiate(self : CatalogRegistry, agent : AgentCapabilities, renderer :
    Json
    ) -> Array[String] raise ProtocolError

    Accepts only explicitly advertised inline catalogs when the agent opted in.

    CatalogRegistry::new

    CatalogRegistry::validate_call

    fn CatalogRegistry::validate_call(self : CatalogRegistry, call :
    Json
    , catalog_id~ : String, caller? : Caller) -> Unit raise ProtocolError

    CatalogRegistry::validate_component

    fn CatalogRegistry::validate_component(self : CatalogRegistry, component : Component, catalog_id~ : String) -> Unit raise ProtocolError

    Component

    pub struct Component {
    // private fields
    }

    A component with a typed identity and catalog-owned JSON properties.

    Component::from_json

    Component::new

    fn Component::new(id : String, kind : String, properties~ :
    Json
    ) -> Component raise ProtocolError

    Component::to_json

    Evaluation

    pub(all) enum Evaluation {
    Ready(
    Json
    )
    Pending(String)
    Failed(code~ : String, message~ : String)
    }

    EvaluationContext

    pub(all) struct EvaluationContext {
    surface_id : String?
    catalog_id : String
    scope : String
    index : Int?
    caller : Caller
    user_activation : Bool
    imperative : Bool
    allow_remote : Bool
    operation_id : String?
    }

    EvaluationContext::root

    fn EvaluationContext::root(surface_id : String, catalog_id? : String, user_activation? : Bool) -> EvaluationContext

    FunctionCall

    pub struct FunctionCall {
    id : String
    surface_id : String?
    // private fields
    }

    A function invocation; direction is supplied by its enclosing message.

    FunctionCall::expression

    FunctionResponse

    pub struct FunctionResponse {
    id : String
    // private fields
    }

    FunctionResponse::error

    FunctionResponse::failure

    fn FunctionResponse::failure(id : String, code : String, message : String) -> FunctionResponse raise ProtocolError

    FunctionResponse::success

    FunctionResponse::value

    Message

    pub struct Message {
    // private fields
    }

    A validated state-changing A2UI 1.0 envelope. Component properties remain catalog-defined; envelope validation does not validate a component catalog.

    Message::create_surface

    fn Message::create_surface(surface_id : String, catalog_id~ : String, components~ : Array[Component], data_model~ :
    Json
    ) -> Message raise ProtocolError

    Message::delete_surface

    fn Message::delete_surface(surface_id : String) -> Message raise ProtocolError

    Message::from_json

    Message::kind

    fn Message::kind(self : Message) -> String

    Message::parse

    fn Message::parse(text : String) -> Message raise

    Decode a single envelope, rejecting duplicate keys and excessive nesting.

    Message::surface_id

    fn Message::surface_id(self : Message) -> String

    Message::to_json

    Message::update_components

    fn Message::update_components(surface_id : String, components : Array[Component]) -> Message raise ProtocolError

    Message::update_data

    fn Message::update_data(surface_id : String, value :
    Json
    , path? : String) -> Message raise ProtocolError

    MetadataChange

    pub struct MetadataChange {
    surface_id : String
    component_id : String?
    previous :
    Json

    current :
    Json

    }

    RendererError

    pub struct RendererError {
    code : String
    message : String
    // private fields
    }

    RendererError::new

    fn RendererError::new(code : String, message : String, surface_id? : String, function_call_id? : String, path? : String) -> RendererError raise ProtocolError

    RendererMessage

    pub(all) enum RendererMessage {
    UserAction(Action)
    CallAgent(FunctionCall)
    RendererFunctionResponse(FunctionResponse)
    Error(RendererError)
    }

    RendererMessage::call_agent

    fn RendererMessage::call_agent(id : String, surface_id : String, call :
    Json
    ) -> RendererMessage raise ProtocolError

    RendererMessage::from_json

    RendererMessage::parse

    fn RendererMessage::parse(text : String) -> RendererMessage raise

    RendererMessage::to_json

    Runtime

    pub struct Runtime {
    // private fields
    }

    Runtime::activate

    fn Runtime::activate(self : Runtime, surface_id : String, component_id : String, timestamp : String, metadata? :
    Json
    , scope? : String, index? : Int) -> RendererMessage? raise ProtocolError

    Activates a declared action and resumes it when dependent RPC results arrive.

    Runtime::apply

    fn Runtime::apply(self : Runtime, message : Message) -> Unit raise ProtocolError

    Runtime::begin_renderer_call

    fn Runtime::begin_renderer_call(self : Runtime, id : String, call :
    Json
    , surface_id? : String) -> AgentMessage raise ProtocolError

    Runtime::cancel_call

    fn Runtime::cancel_call(self : Runtime, call_id : String, reason : String) -> Unit raise ProtocolError

    Runtime::component_properties

    fn Runtime::component_properties(self : Runtime, surface_id : String, component_id : String, scope? : String, index? : Int) ->
    Json
    raise ProtocolError

    Runtime::drain_messages

    fn Runtime::drain_messages(self : Runtime) -> Array[RendererMessage]

    Runtime::handle_agent_call

    fn Runtime::handle_agent_call(self : Runtime, message : RendererMessage) -> AgentMessage raise ProtocolError

    Executes a renderer-originated RPC on an agent host with registered handlers.

    Runtime::new

    fn Runtime::new(catalogs? : CatalogRegistry) -> Runtime raise ProtocolError

    Runtime::receive_agent

    fn Runtime::receive_agent(self : Runtime, message : AgentMessage) -> Array[RendererMessage] raise ProtocolError

    Runtime::receive_renderer_response

    fn Runtime::receive_renderer_response(self : Runtime, message : RendererMessage) -> Unit raise ProtocolError

    Runtime::register_function

    fn Runtime::register_function(self : Runtime, catalog_id : String, name : String, handler : (
    Json
    , EvaluationContext) ->
    Json
    raise) -> Unit raise ProtocolError

    Runtime::renderer_data_model

    fn Runtime::renderer_data_model(self : Runtime, surface_id : String) ->
    Json
    ? raise ProtocolError

    Runtime::resolve

    Runtime::response

    fn Runtime::response(self : Runtime, call_id : String) -> FunctionResponse?

    Runtime::snapshot

    fn Runtime::snapshot(self : Runtime, surface_id : String) ->
    Json
    ?

    Runtime::take_response

    fn Runtime::take_response(self : Runtime, id : String) -> FunctionResponse?

    Runtime::template_contexts

    fn Runtime::template_contexts(self : Runtime, context : EvaluationContext, path : String) -> Array[EvaluationContext] raise ProtocolError

    Runtime::unwatch

    fn Runtime::unwatch(self : Runtime, id : Int) -> Unit

    Runtime::validate_complete

    fn Runtime::validate_complete(self : Runtime, surface_id : String) -> Unit raise ProtocolError

    Runtime::watch_component

    fn Runtime::watch_component(self : Runtime, surface_id : String, component_id : String, callback : (
    Json
    ?) -> Unit, scope? : String, index? : Int) -> Int raise ProtocolError

    Calls the observer only when resolved properties change; None means deletion.

    Runtime::write_binding

    fn Runtime::write_binding(self : Runtime, context : EvaluationContext, binding :
    Json
    , value :
    Json
    ) -> Unit raise ProtocolError

    SurfaceStore

    pub struct SurfaceStore {
    // private fields
    }

    Stores independent surface states without I/O or renderer dependencies.

    SurfaceStore::apply

    fn SurfaceStore::apply(self : SurfaceStore, message : Message) -> Unit raise ProtocolError

    SurfaceStore::data_at

    fn SurfaceStore::data_at(self : SurfaceStore, surface_id : String, path : String) ->
    Json
    ? raise ProtocolError

    SurfaceStore::metadata

    fn SurfaceStore::metadata(self : SurfaceStore, surface_id : String, component_id? : String) ->
    Json
    raise ProtocolError

    SurfaceStore::new

    fn SurfaceStore::new(catalogs? : CatalogRegistry) -> SurfaceStore

    SurfaceStore::on_metadata_changed

    fn SurfaceStore::on_metadata_changed(self : SurfaceStore, listener : (MetadataChange) -> Unit) -> Int

    SurfaceStore::remove_extension

    fn SurfaceStore::remove_extension(self : SurfaceStore, surface_id : String, key : String, component_id? : String) -> Unit raise ProtocolError

    SurfaceStore::remove_metadata_listener

    fn SurfaceStore::remove_metadata_listener(self : SurfaceStore, id : Int) -> Unit

    SurfaceStore::renderer_data_model

    fn SurfaceStore::renderer_data_model(self : SurfaceStore, surface_id : String) ->
    Json
    ? raise ProtocolError

    Carries only the requesting surface's opted-in data in transport metadata.

    SurfaceStore::set_extension

    fn SurfaceStore::set_extension(self : SurfaceStore, surface_id : String, key : String, value :
    Json
    , component_id? : String) -> Unit raise ProtocolError

    SurfaceStore::set_metadata

    fn SurfaceStore::set_metadata(self : SurfaceStore, surface_id : String, metadata :
    Json
    , component_id? : String) -> Unit raise ProtocolError

    SurfaceStore::snapshot

    fn SurfaceStore::snapshot(self : SurfaceStore, surface_id : String) ->
    Json
    ?

    Returns an owned snapshot; changing it cannot mutate the store.

    SurfaceStore::validate_complete

    fn SurfaceStore::validate_complete(self : SurfaceStore, surface_id : String) -> Unit raise ProtocolError

    basic_catalog_id

    let basic_catalog_id : String

    coerce_boolean

    fn coerce_boolean(value :
    Json
    ) -> Bool