A2UI protocol messages and surface state for MoonBit.
moon add Delthin/moon_a2ui@0.1.1import { "Delthin/moon_a2ui" @a2ui }///|
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),
)
}
git clone https://github.com/Delthin/moon-a2ui.git
cd moon-a2ui
moon updatemoon build examples/browser --target js --release
moon build examples/native --target native --release./_build/native/release/build/examples/native/native.exenpm ci
npm run build
npm run previewpub(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)impl Show for ProtocolErrorpub struct Action {
name : String
surface_id : String
source_component_id : String
timestamp : String
// private fields
}pub struct AgentCapabilities {
accepts_inline_catalogs : Bool
// private fields
}fn AgentCapabilities::new(supported_catalog_ids : Array[String], accepts_inline_catalogs? : Bool) -> AgentCapabilities raise ProtocolErrorpub(all) enum AgentMessage {
SurfaceUpdate(Message)
CallRenderer(FunctionCall)
AgentFunctionResponse(FunctionResponse)
}fn AgentMessage::call_renderer(id : String, call : Json, surface_id? : String) -> AgentMessage raise ProtocolErrorpub struct Catalog {
id : String
// private fields
}pub struct CatalogRegistry {
// private fields
}fn CatalogRegistry::negotiate(self : CatalogRegistry, agent : AgentCapabilities, renderer : Json) -> Array[String] raise ProtocolErrorfn CatalogRegistry::validate_call(self : CatalogRegistry, call : Json, catalog_id~ : String, caller? : Caller) -> Unit raise ProtocolErrorfn CatalogRegistry::validate_component(self : CatalogRegistry, component : Component, catalog_id~ : String) -> Unit raise ProtocolErrorpub struct Component {
// private fields
}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?
}fn EvaluationContext::root(surface_id : String, catalog_id? : String, user_activation? : Bool) -> EvaluationContextpub struct FunctionCall {
id : String
surface_id : String?
// private fields
}pub struct FunctionResponse {
id : String
// private fields
}fn FunctionResponse::failure(id : String, code : String, message : String) -> FunctionResponse raise ProtocolErrorpub struct Message {
// private fields
}fn Message::update_components(surface_id : String, components : Array[Component]) -> Message raise ProtocolErrorfn Message::update_data(surface_id : String, value : Json, path? : String) -> Message raise ProtocolErrorpub struct RendererError {
code : String
message : String
// private fields
}fn RendererError::new(code : String, message : String, surface_id? : String, function_call_id? : String, path? : String) -> RendererError raise ProtocolErrorpub(all) enum RendererMessage {
UserAction(Action)
CallAgent(FunctionCall)
RendererFunctionResponse(FunctionResponse)
Error(RendererError)
}fn RendererMessage::call_agent(id : String, surface_id : String, call : Json) -> RendererMessage raise ProtocolErrorpub struct Runtime {
// private fields
}fn Runtime::activate(self : Runtime, surface_id : String, component_id : String, timestamp : String, metadata? : Json, scope? : String, index? : Int) -> RendererMessage? raise ProtocolErrorfn Runtime::begin_renderer_call(self : Runtime, id : String, call : Json, surface_id? : String) -> AgentMessage raise ProtocolErrorfn Runtime::cancel_call(self : Runtime, call_id : String, reason : String) -> Unit raise ProtocolErrorfn Runtime::check(self : Runtime, rules : Array[Json], context : EvaluationContext) -> Array[Json] raise ProtocolErrorfn Runtime::component_properties(self : Runtime, surface_id : String, component_id : String, scope? : String, index? : Int) -> Json raise ProtocolErrorfn Runtime::evaluate(self : Runtime, value : Json, context : EvaluationContext) -> Json raise ProtocolErrorfn Runtime::handle_agent_call(self : Runtime, message : RendererMessage) -> AgentMessage raise ProtocolErrorfn Runtime::receive_agent(self : Runtime, message : AgentMessage) -> Array[RendererMessage] raise ProtocolErrorfn Runtime::receive_renderer_response(self : Runtime, message : RendererMessage) -> Unit raise ProtocolErrorfn Runtime::register_function(self : Runtime, catalog_id : String, name : String, handler : (Json, EvaluationContext) -> Json raise) -> Unit raise ProtocolErrorfn Runtime::template_contexts(self : Runtime, context : EvaluationContext, path : String) -> Array[EvaluationContext] raise ProtocolErrorfn Runtime::watch_component(self : Runtime, surface_id : String, component_id : String, callback : (Json?) -> Unit, scope? : String, index? : Int) -> Int raise ProtocolErrorfn Runtime::write_binding(self : Runtime, context : EvaluationContext, binding : Json, value : Json) -> Unit raise ProtocolErrorpub struct SurfaceStore {
// private fields
}fn SurfaceStore::data_at(self : SurfaceStore, surface_id : String, path : String) -> Json? raise ProtocolErrorfn SurfaceStore::metadata(self : SurfaceStore, surface_id : String, component_id? : String) -> Json raise ProtocolErrorfn SurfaceStore::on_metadata_changed(self : SurfaceStore, listener : (MetadataChange) -> Unit) -> Intfn SurfaceStore::remove_extension(self : SurfaceStore, surface_id : String, key : String, component_id? : String) -> Unit raise ProtocolErrorfn SurfaceStore::renderer_data_model(self : SurfaceStore, surface_id : String) -> Json? raise ProtocolErrorfn SurfaceStore::set_extension(self : SurfaceStore, surface_id : String, key : String, value : Json, component_id? : String) -> Unit raise ProtocolErrorfn SurfaceStore::set_metadata(self : SurfaceStore, surface_id : String, metadata : Json, component_id? : String) -> Unit raise ProtocolErrorfn SurfaceStore::validate_complete(self : SurfaceStore, surface_id : String) -> Unit raise ProtocolError