agent86-ui 1.3.0

A2UI 1.0 for Rust: message types, catalogs, and validation of agent-generated UI
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//! The A2UI 1.0 messages, as typed Rust values.
//!
//! Every message on the wire is one JSON object carrying `"version": "1.0"` and
//! exactly one key naming its kind. [`AgentMessage`] and [`RendererMessage`]
//! keep that shape: a `version` and a flattened kind. Component properties stay
//! JSON ([`Component::properties`]), since what they may hold is the catalog's
//! business, not the protocol's.
//!
//! Deserializing is lenient about what the schema is strict about (unknown keys
//! are kept or ignored); [`crate::Validator`] runs the schema first, so a value
//! that reached these types through it is well-formed.

use serde::{Deserialize, Deserializer, Serialize};
use serde_json::{Map, Value};

/// An explicit `null` stays `Some(Value::Null)`; only an absent field is `None`.
/// (`value: null` in `updateDataModel` deletes, and a function may return
/// `null`, so the two must not collapse.)
fn present<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Option<Value>, D::Error> {
    Value::deserialize(deserializer).map(Some)
}

/// A message from the agent to the renderer.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct AgentMessage {
    /// The protocol version, `"1.0"`.
    pub version: String,
    /// What the message does.
    #[serde(flatten)]
    pub kind: AgentMessageKind,
}

impl AgentMessage {
    /// A 1.0 message of this kind.
    pub fn new(kind: AgentMessageKind) -> Self {
        Self {
            version: crate::PROTOCOL_VERSION.to_string(),
            kind,
        }
    }

    /// The surface the message is about, when it names one.
    pub fn surface_id(&self) -> Option<&str> {
        match &self.kind {
            AgentMessageKind::CreateSurface(m) => Some(&m.surface_id),
            AgentMessageKind::UpdateComponents(m) => Some(&m.surface_id),
            AgentMessageKind::UpdateDataModel(m) => Some(&m.surface_id),
            AgentMessageKind::DeleteSurface(m) => Some(&m.surface_id),
            AgentMessageKind::CallRendererFunction(m) => m.surface_id.as_deref(),
            AgentMessageKind::AgentFunctionResponse(_) => None,
        }
    }
}

/// The kinds of agent-to-renderer message.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub enum AgentMessageKind {
    /// Create a surface, optionally with its components and data model.
    CreateSurface(CreateSurface),
    /// Add or replace components on a surface.
    UpdateComponents(UpdateComponents),
    /// Set or delete a value in a surface's data model.
    UpdateDataModel(UpdateDataModel),
    /// Remove a surface.
    DeleteSurface(DeleteSurface),
    /// Ask the renderer to run one of its functions.
    CallRendererFunction(CallRendererFunction),
    /// Answer a `callAgentFunction` from the renderer.
    AgentFunctionResponse(FunctionResponse),
}

/// `createSurface`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct CreateSurface {
    /// Unique for the renderer's lifetime.
    pub surface_id: String,
    /// The surface's default catalog.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub catalog_id: Option<String>,
    /// Whether the renderer sends the data model back with each message.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub send_data_model: Option<bool>,
    /// Components to start with.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub components: Option<Vec<Component>>,
    /// The initial data model, applied before the components.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub data_model: Option<Map<String, Value>>,
    /// Surface metadata (`extensions`).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub metadata: Option<Value>,
}

/// `updateComponents`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct UpdateComponents {
    /// The surface to update.
    pub surface_id: String,
    /// Components to add, or to replace by id.
    pub components: Vec<Component>,
}

/// `updateDataModel`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct UpdateDataModel {
    /// The surface whose data model changes.
    pub surface_id: String,
    /// A JSON Pointer; absent or `/` means the whole model.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub path: Option<String>,
    /// The new value; `null` deletes the key at `path`.
    pub value: Value,
}

/// `deleteSurface`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct DeleteSurface {
    /// The surface to remove.
    pub surface_id: String,
}

/// `callRendererFunction`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct CallRendererFunction {
    /// The surface whose data model the call reads, if any.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub surface_id: Option<String>,
    /// Echoed in the renderer's response.
    pub function_call_id: String,
    /// The call (its `catalogId` is required here).
    pub call_function: FunctionCall,
}

/// A call to a catalog function, as it appears in bindings, checks, actions,
/// and the function-call messages.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct FunctionCall {
    /// The function's name.
    #[serde(rename = "@call")]
    pub call: String,
    /// The catalog to resolve it in, overriding the surface's default.
    #[serde(rename = "catalogId", default, skip_serializing_if = "Option::is_none")]
    pub catalog_id: Option<String>,
    /// The arguments, as the function's schema defines them.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub args: Option<Map<String, Value>>,
}

/// `agentFunctionResponse` and `rendererFunctionResponse`: a value or an error.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct FunctionResponse {
    /// The id of the call this answers.
    pub function_call_id: String,
    /// The result, when the call succeeded (`null` is a result).
    #[serde(
        default,
        deserialize_with = "present",
        skip_serializing_if = "Option::is_none"
    )]
    pub value: Option<Value>,
    /// The failure, when it did not.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub error: Option<ErrorBody>,
}

/// A function response's `error`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ErrorBody {
    /// A machine-readable code.
    pub code: String,
    /// What went wrong.
    pub message: String,
}

/// One component on a surface. `id` and `component` are the protocol's; the
/// `@`-prefixed fields are its envelope mixins; everything else is the
/// catalog's, kept as JSON in [`Component::properties`].
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Component {
    /// Unique on its surface; a UAX #31 identifier.
    pub id: String,
    /// The component type, from the catalog.
    pub component: String,
    /// The catalog to resolve the type in, overriding the surface's default.
    #[serde(
        rename = "@catalogId",
        default,
        skip_serializing_if = "Option::is_none"
    )]
    pub catalog_id: Option<String>,
    /// `label`, `description`, `live`, and `hidden`, for assistive technologies.
    #[serde(
        rename = "@accessibility",
        default,
        skip_serializing_if = "Option::is_none"
    )]
    pub accessibility: Option<Value>,
    /// Vendor metadata (`extensions`).
    #[serde(rename = "@metadata", default, skip_serializing_if = "Option::is_none")]
    pub metadata: Option<Value>,
    /// Renderer-side checks, on checkable components.
    #[serde(rename = "@checks", default, skip_serializing_if = "Option::is_none")]
    pub checks: Option<Vec<CheckRule>>,
    /// The catalog's properties for this type (`text`, `children`, `action`, ...).
    #[serde(flatten)]
    pub properties: Map<String, Value>,
}

/// One `@checks` rule: a condition (a binding or a function call) and the
/// message to show when it fails.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct CheckRule {
    /// A `{"@path": ...}` or a function call, evaluating to a boolean or a
    /// validation result.
    pub condition: Value,
    /// The fallback message.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub message: Option<String>,
}

/// A message from the renderer to the agent.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct RendererMessage {
    /// The protocol version, `"1.0"`.
    pub version: String,
    /// What the message reports.
    #[serde(flatten)]
    pub kind: RendererMessageKind,
}

impl RendererMessage {
    /// A 1.0 message of this kind.
    pub fn new(kind: RendererMessageKind) -> Self {
        Self {
            version: crate::PROTOCOL_VERSION.to_string(),
            kind,
        }
    }
}

/// The kinds of renderer-to-agent message.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub enum RendererMessageKind {
    /// The user did something a component's action sends to the agent.
    Action(Action),
    /// The renderer asks the agent to run a function.
    CallAgentFunction(CallAgentFunction),
    /// The answer to a `callRendererFunction`.
    RendererFunctionResponse(FunctionResponse),
    /// A validation, integrity, or evaluation error on the renderer.
    Error(RendererError),
}

/// `action`: a user interaction with a component whose action is an event.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Action {
    /// The event's name, from the component's `action.event.name`.
    pub name: String,
    /// The surface it happened on.
    pub surface_id: String,
    /// The component that triggered it.
    pub source_component_id: String,
    /// When, as an RFC 3339 date-time.
    pub timestamp: String,
    /// The event's `context`, with every binding resolved.
    pub context: Map<String, Value>,
    /// The event's `userMessage`, resolved.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub user_message: Option<String>,
    /// Renderer metadata (`extensions`).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub metadata: Option<Value>,
}

impl Action {
    /// An action as a renderer reports it.
    pub fn new(
        name: impl Into<String>,
        surface_id: impl Into<String>,
        source_component_id: impl Into<String>,
        timestamp: impl Into<String>,
        context: Map<String, Value>,
    ) -> Self {
        Self {
            name: name.into(),
            surface_id: surface_id.into(),
            source_component_id: source_component_id.into(),
            timestamp: timestamp.into(),
            context,
            user_message: None,
            metadata: None,
        }
    }
}

/// `callAgentFunction`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct CallAgentFunction {
    /// The surface the call came from.
    pub surface_id: String,
    /// To be echoed in the agent's response.
    pub function_call_id: String,
    /// The call.
    pub call_function: FunctionCall,
}

/// A renderer's `error` message.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct RendererError {
    /// `VALIDATION_FAILED`, `UNALLOWED_PARENT`, `INTEGRITY_ERROR`, ...
    pub code: String,
    /// What went wrong.
    pub message: String,
    /// The surface, for surface errors.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub surface_id: Option<String>,
    /// A JSON Pointer to the offending field, for validation errors.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub path: Option<String>,
    /// The function call, for function invocation errors.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub function_call_id: Option<String>,
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn an_agent_message_round_trips_with_its_version_first() {
        let wire = json!({
            "version": "1.0",
            "createSurface": {
                "surfaceId": "s1",
                "catalogId": "c",
                "components": [
                    {"id": "root", "component": "Text", "text": {"@path": "/name"},
                     "@accessibility": {"label": "Name"}}
                ],
                "dataModel": {"name": "Ada"}
            }
        });
        let message: AgentMessage = serde_json::from_value(wire.clone()).unwrap();
        let AgentMessageKind::CreateSurface(create) = &message.kind else {
            panic!("not createSurface")
        };
        let root = &create.components.as_ref().unwrap()[0];
        assert_eq!(root.component, "Text");
        assert_eq!(root.properties["text"], json!({"@path": "/name"}));
        assert_eq!(message.surface_id(), Some("s1"));
        let back = serde_json::to_value(&message).unwrap();
        assert_eq!(back, wire);
        assert_eq!(
            serde_json::to_string(&message).unwrap().find("\"version\""),
            Some(1)
        );
    }

    #[test]
    fn a_null_value_is_kept_apart_from_a_missing_one() {
        let delete: AgentMessage = serde_json::from_value(json!({
            "version": "1.0",
            "updateDataModel": {"surfaceId": "s", "path": "/a", "value": null}
        }))
        .unwrap();
        let AgentMessageKind::UpdateDataModel(update) = delete.kind else {
            panic!()
        };
        assert_eq!(update.value, Value::Null);

        let response: FunctionResponse =
            serde_json::from_value(json!({"functionCallId": "c", "value": null})).unwrap();
        assert_eq!(response.value, Some(Value::Null));
        let error: FunctionResponse = serde_json::from_value(
            json!({"functionCallId": "c", "error": {"code": "X", "message": "m"}}),
        )
        .unwrap();
        assert_eq!(error.value, None);
    }

    #[test]
    fn an_action_serializes_as_the_specification_shows() {
        let mut context = Map::new();
        context.insert("isSubscribed".into(), json!(true));
        let mut action = Action::new(
            "submitForm",
            "contact_form_1",
            "submit_button",
            "2026-06-02T08:57:23Z",
            context,
        );
        action.user_message = Some("Submitted contact form".into());
        let message = RendererMessage::new(RendererMessageKind::Action(action));
        assert_eq!(
            serde_json::to_value(&message).unwrap(),
            json!({
                "version": "1.0",
                "action": {
                    "name": "submitForm",
                    "surfaceId": "contact_form_1",
                    "sourceComponentId": "submit_button",
                    "timestamp": "2026-06-02T08:57:23Z",
                    "context": {"isSubscribed": true},
                    "userMessage": "Submitted contact form"
                }
            })
        );
    }
}