adk-ui 0.6.0

Dynamic UI generation for ADK-Rust agents - render forms, cards, tables, charts and more
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
439
440
441
442
443
444
445
446
447
use adk_ui::compat::{Content, EventActions, ReadonlyContext, ToolContext};
use adk_ui::{UiToolset, column, text};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use std::collections::BTreeMap;
use std::fs;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};

struct TestContext {
    content: Content,
    actions: Mutex<EventActions>,
}

impl TestContext {
    fn new() -> Self {
        Self {
            content: Content::new("user"),
            actions: Mutex::new(EventActions::default()),
        }
    }
}

#[async_trait]
impl ReadonlyContext for TestContext {
    fn invocation_id(&self) -> &str {
        "matrix-test"
    }

    fn agent_name(&self) -> &str {
        "matrix-agent"
    }

    fn user_id(&self) -> &str {
        "user"
    }

    fn app_name(&self) -> &str {
        "app"
    }

    fn session_id(&self) -> &str {
        "session"
    }

    fn branch(&self) -> &str {
        ""
    }

    fn user_content(&self) -> &Content {
        &self.content
    }
}

#[async_trait]
impl adk_ui::compat::CallbackContext for TestContext {
    fn artifacts(&self) -> Option<Arc<dyn adk_ui::compat::Artifacts>> {
        None
    }
}

#[async_trait]
impl ToolContext for TestContext {
    fn function_call_id(&self) -> &str {
        "call-matrix"
    }

    fn actions(&self) -> EventActions {
        self.actions.lock().expect("actions lock").clone()
    }

    fn set_actions(&self, actions: EventActions) {
        *self.actions.lock().expect("actions lock") = actions;
    }

    async fn search_memory(
        &self,
        _query: &str,
    ) -> adk_ui::compat::Result<Vec<adk_ui::compat::MemoryEntry>> {
        Ok(vec![])
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
struct MatrixSnapshot {
    tool: String,
    protocol: String,
    value_kind: String,
    top_level_keys: Vec<String>,
    jsonl_line_count: Option<usize>,
    event_types: Vec<String>,
    resource_uri: Option<String>,
    has_version: bool,
}

fn fixture_path() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("tests")
        .join("fixtures")
        .join("tool_protocol_matrix.snap.json")
}

fn templates_by_tool() -> BTreeMap<&'static str, Value> {
    let mut templates = BTreeMap::new();
    templates.insert(
        "render_screen",
        json!({
            "surface_id": "matrix-screen",
            "components": [
                text("title", "Matrix Screen", Some("h1")),
                text("body", "Coverage run", None),
                column("root", vec!["title", "body"])
            ],
            "data_model": { "status": "ok" }
        }),
    );
    templates.insert(
        "render_page",
        json!({
            "surface_id": "matrix-page",
            "title": "Matrix Page",
            "description": "Protocol coverage",
            "sections": [
                {
                    "heading": "Summary",
                    "body": "All adapters should emit valid outputs.",
                    "bullets": ["a2ui", "ag_ui", "mcp_apps"],
                    "actions": [{ "label": "Continue", "action": "continue" }]
                }
            ]
        }),
    );
    templates.insert(
        "render_kit",
        json!({
            "name": "Matrix Kit",
            "version": "0.1.0",
            "brand": { "vibe": "clean", "industry": "platform" },
            "colors": { "primary": "#2F6BFF" },
            "typography": { "family": "Source Sans 3" }
        }),
    );
    templates.insert(
        "render_form",
        json!({
            "title": "Matrix Form",
            "fields": [
                { "name": "email", "label": "Email", "type": "email", "required": true },
                { "name": "notes", "label": "Notes", "type": "textarea" }
            ],
            "submit_action": "matrix_submit"
        }),
    );
    templates.insert(
        "render_card",
        json!({
            "title": "Matrix Card",
            "content": "Card content for protocol matrix.",
            "actions": [{ "label": "Open", "action_id": "open_card" }]
        }),
    );
    templates.insert(
        "render_alert",
        json!({
            "title": "Matrix Alert",
            "description": "Protocol check",
            "variant": "warning"
        }),
    );
    templates.insert(
        "render_confirm",
        json!({
            "title": "Confirm Matrix",
            "message": "Proceed with protocol checks?",
            "confirm_action": "confirm_matrix",
            "destructive": false
        }),
    );
    templates.insert(
        "render_table",
        json!({
            "title": "Matrix Table",
            "columns": [
                { "header": "Name", "accessor_key": "name" },
                { "header": "Status", "accessor_key": "status" }
            ],
            "data": [
                { "name": "a2ui", "status": "ok" },
                { "name": "ag_ui", "status": "ok" },
                { "name": "mcp_apps", "status": "ok" }
            ]
        }),
    );
    templates.insert(
        "render_chart",
        json!({
            "title": "Matrix Chart",
            "type": "line",
            "data": [
                { "phase": "a2ui", "score": 1 },
                { "phase": "ag_ui", "score": 1 },
                { "phase": "mcp_apps", "score": 1 }
            ],
            "x_key": "phase",
            "y_keys": ["score"]
        }),
    );
    templates.insert(
        "render_layout",
        json!({
            "title": "Matrix Layout",
            "sections": [
                {
                    "title": "Protocol Status",
                    "type": "stats",
                    "stats": [
                        { "label": "A2UI", "value": "ok", "status": "success" },
                        { "label": "AG-UI", "value": "ok", "status": "success" }
                    ]
                }
            ]
        }),
    );
    templates.insert(
        "render_progress",
        json!({
            "title": "Matrix Progress",
            "value": 66,
            "steps": [
                { "label": "A2UI", "completed": true },
                { "label": "AG-UI", "completed": true },
                { "label": "MCP Apps", "current": true }
            ]
        }),
    );
    templates.insert(
        "render_modal",
        json!({
            "title": "Matrix Modal",
            "message": "Protocol rendering in progress.",
            "confirm_label": "OK",
            "cancel_label": "Close"
        }),
    );
    templates.insert(
        "render_toast",
        json!({
            "message": "Matrix toast",
            "variant": "success",
            "duration": 1200
        }),
    );
    templates
}

fn args_with_protocol(template: &Value, protocol: &str) -> Value {
    let mut args = template.clone();
    let object = args.as_object_mut().expect("tool args must be object");
    object.insert("protocol".to_string(), Value::String(protocol.to_string()));
    args
}

fn extract_event_types(value: &Value) -> Vec<String> {
    let events = value
        .get("events")
        .and_then(Value::as_array)
        .or_else(|| value.pointer("/payload/events").and_then(Value::as_array));
    let Some(events) = events else {
        return vec![];
    };

    events
        .iter()
        .filter_map(|event| {
            event
                .get("type")
                .and_then(Value::as_str)
                .map(ToString::to_string)
        })
        .collect()
}

fn extract_resource_uri(value: &Value) -> Option<String> {
    value
        .pointer("/payload/resource/uri")
        .and_then(Value::as_str)
        .map(ToString::to_string)
        .or_else(|| {
            value
                .pointer("/payload/payload/resource/uri")
                .and_then(Value::as_str)
                .map(ToString::to_string)
        })
}

fn extract_jsonl_line_count(value: &Value) -> Option<usize> {
    if let Some(s) = value.as_str() {
        return Some(s.lines().filter(|line| !line.trim().is_empty()).count());
    }
    if let Some(s) = value.get("jsonl").and_then(Value::as_str) {
        return Some(s.lines().filter(|line| !line.trim().is_empty()).count());
    }
    value
        .pointer("/payload/jsonl")
        .and_then(Value::as_str)
        .map(|s| s.lines().filter(|line| !line.trim().is_empty()).count())
}

fn validate_contract(tool: &str, protocol: &str, value: &Value) {
    match (tool, protocol) {
        ("render_page", "a2ui") => {
            let jsonl = value
                .as_str()
                .expect("render_page a2ui should return JSONL string");
            assert!(jsonl.contains("\"createSurface\""));
            assert!(jsonl.contains("\"updateComponents\""));
        }
        ("render_kit", _) => {
            assert_eq!(value["protocol"], protocol);
            assert!(
                value.get("payload").is_some(),
                "render_kit should include payload"
            );
        }
        (_, "a2ui") => {
            assert_eq!(value["protocol"], "a2ui");
            assert!(
                value.get("jsonl").and_then(Value::as_str).is_some()
                    || value
                        .pointer("/payload/jsonl")
                        .and_then(Value::as_str)
                        .is_some(),
                "{tool} a2ui output should carry jsonl",
            );
        }
        (_, "ag_ui") => {
            assert_eq!(value["protocol"], "ag_ui");
            let has_events = value.get("events").and_then(Value::as_array).is_some()
                || value
                    .pointer("/payload/events")
                    .and_then(Value::as_array)
                    .is_some();
            assert!(has_events, "{tool} ag_ui output should include events");
        }
        (_, "mcp_apps") => {
            assert_eq!(value["protocol"], "mcp_apps");
            let has_uri = extract_resource_uri(value).is_some();
            assert!(
                has_uri,
                "{tool} mcp_apps output should include resource uri"
            );
        }
        _ => {}
    }
}

fn snapshot_record(tool: &str, protocol: &str, value: &Value) -> MatrixSnapshot {
    let mut top_level_keys = value
        .as_object()
        .map(|object| object.keys().cloned().collect::<Vec<_>>())
        .unwrap_or_default();
    top_level_keys.sort();

    MatrixSnapshot {
        tool: tool.to_string(),
        protocol: protocol.to_string(),
        value_kind: if value.is_string() {
            "string".to_string()
        } else {
            "object".to_string()
        },
        top_level_keys,
        jsonl_line_count: extract_jsonl_line_count(value),
        event_types: extract_event_types(value),
        resource_uri: extract_resource_uri(value),
        has_version: value.get("version").is_some(),
    }
}

async fn collect_matrix_snapshots() -> Vec<MatrixSnapshot> {
    let templates = templates_by_tool();
    let tools = UiToolset::all_tools();
    let protocols = ["a2ui", "ag_ui", "mcp_apps"];
    let mut snapshots = Vec::new();

    assert_eq!(tools.len(), 13, "toolset should expose 13 tools");

    for protocol in protocols {
        for tool in &tools {
            let template = templates
                .get(tool.name())
                .unwrap_or_else(|| panic!("missing args for {}", tool.name()));
            let args = args_with_protocol(template, protocol);
            let ctx: Arc<dyn ToolContext> = Arc::new(TestContext::new());
            let value = tool
                .execute(ctx, args)
                .await
                .unwrap_or_else(|e| panic!("{} {} failed: {}", tool.name(), protocol, e));
            validate_contract(tool.name(), protocol, &value);
            snapshots.push(snapshot_record(tool.name(), protocol, &value));
        }
    }

    snapshots
}

#[tokio::test]
async fn tool_protocol_matrix_covers_all_tools_and_protocols() {
    let snapshots = collect_matrix_snapshots().await;
    assert_eq!(snapshots.len(), 39, "expected 13 tools x 3 protocols");
}

#[tokio::test]
async fn tool_protocol_matrix_is_deterministic() {
    let first = collect_matrix_snapshots().await;
    let second = collect_matrix_snapshots().await;
    assert_eq!(
        first, second,
        "matrix outputs should be deterministic across runs"
    );
}

#[tokio::test]
async fn tool_protocol_matrix_snapshot_matches_fixture() {
    let snapshots = collect_matrix_snapshots().await;
    let path = fixture_path();

    if std::env::var("UPDATE_SNAPSHOTS").ok().as_deref() == Some("1") {
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent).expect("create fixture directory");
        }
        let fixture = serde_json::to_string_pretty(&snapshots).expect("serialize snapshots");
        fs::write(&path, fixture).expect("write fixture file");
    }

    let expected_raw = fs::read_to_string(&path).unwrap_or_else(|_| {
        panic!(
            "missing fixture at {}. Run with UPDATE_SNAPSHOTS=1",
            path.display()
        )
    });
    let expected: Vec<MatrixSnapshot> =
        serde_json::from_str(&expected_raw).expect("parse fixture JSON");

    assert_eq!(snapshots, expected, "matrix snapshots changed");
}