adk-ui 0.8.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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
use super::surface::UiSurface;
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};

/// Event name used for surface payload transport via AG-UI custom events.
pub const ADK_UI_SURFACE_EVENT_NAME: &str = "adk.ui.surface";

/// AG-UI event types from the protocol event model.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum AgUiEventType {
    RunStarted,
    RunFinished,
    RunError,
    StepStarted,
    StepFinished,
    TextMessageStart,
    TextMessageContent,
    TextMessageDelta,
    TextMessageEnd,
    TextMessageChunk,
    ToolCallStart,
    ToolCallArgs,
    ToolCallEnd,
    ToolCallResult,
    ToolCallChunk,
    StateSnapshot,
    StateDelta,
    MessagesSnapshot,
    ActivitySnapshot,
    ActivityDelta,
    Error,
    Raw,
    Custom,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AgUiRunStartedEvent {
    #[serde(rename = "type")]
    pub event_type: AgUiEventType,
    pub thread_id: String,
    pub run_id: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AgUiRunFinishedEvent {
    #[serde(rename = "type")]
    pub event_type: AgUiEventType,
    pub thread_id: String,
    pub run_id: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub result: Option<Value>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AgUiRunErrorEvent {
    #[serde(rename = "type")]
    pub event_type: AgUiEventType,
    pub thread_id: String,
    pub run_id: String,
    pub message: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub code: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AgUiCustomEvent {
    #[serde(rename = "type")]
    pub event_type: AgUiEventType,
    pub name: String,
    pub value: Value,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub timestamp: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub raw_event: Option<Value>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AgUiStepEvent {
    #[serde(rename = "type")]
    pub event_type: AgUiEventType,
    pub thread_id: String,
    pub run_id: String,
    pub step_id: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AgUiTextMessageStartEvent {
    #[serde(rename = "type")]
    pub event_type: AgUiEventType,
    pub thread_id: String,
    pub run_id: String,
    pub message_id: String,
    pub role: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AgUiTextMessageDeltaEvent {
    #[serde(rename = "type")]
    pub event_type: AgUiEventType,
    pub thread_id: String,
    pub run_id: String,
    pub message_id: String,
    pub delta: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AgUiTextMessageChunkEvent {
    #[serde(rename = "type")]
    pub event_type: AgUiEventType,
    pub thread_id: String,
    pub run_id: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub role: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub delta: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AgUiTextMessageEndEvent {
    #[serde(rename = "type")]
    pub event_type: AgUiEventType,
    pub thread_id: String,
    pub run_id: String,
    pub message_id: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AgUiToolCallStartEvent {
    #[serde(rename = "type")]
    pub event_type: AgUiEventType,
    pub thread_id: String,
    pub run_id: String,
    pub tool_call_id: String,
    pub tool_call_name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parent_message_id: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AgUiToolCallArgsEvent {
    #[serde(rename = "type")]
    pub event_type: AgUiEventType,
    pub thread_id: String,
    pub run_id: String,
    pub tool_call_id: String,
    pub delta: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AgUiToolCallEndEvent {
    #[serde(rename = "type")]
    pub event_type: AgUiEventType,
    pub thread_id: String,
    pub run_id: String,
    pub tool_call_id: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AgUiToolCallResultEvent {
    #[serde(rename = "type")]
    pub event_type: AgUiEventType,
    pub thread_id: String,
    pub run_id: String,
    pub tool_call_id: String,
    pub message_id: String,
    pub content: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub role: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AgUiToolCallChunkEvent {
    #[serde(rename = "type")]
    pub event_type: AgUiEventType,
    pub thread_id: String,
    pub run_id: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_call_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_call_name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parent_message_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub delta: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AgUiStateSnapshotEvent {
    #[serde(rename = "type")]
    pub event_type: AgUiEventType,
    pub thread_id: String,
    pub run_id: String,
    pub state: Value,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AgUiStateDeltaEvent {
    #[serde(rename = "type")]
    pub event_type: AgUiEventType,
    pub thread_id: String,
    pub run_id: String,
    pub delta: Value,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AgUiMessagesSnapshotEvent {
    #[serde(rename = "type")]
    pub event_type: AgUiEventType,
    pub thread_id: String,
    pub run_id: String,
    pub messages: Vec<Value>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AgUiActivitySnapshotEvent {
    #[serde(rename = "type")]
    pub event_type: AgUiEventType,
    pub thread_id: String,
    pub run_id: String,
    pub message_id: String,
    pub activity_type: String,
    pub content: Value,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub replace: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AgUiActivityDeltaEvent {
    #[serde(rename = "type")]
    pub event_type: AgUiEventType,
    pub thread_id: String,
    pub run_id: String,
    pub message_id: String,
    pub activity_type: String,
    pub patch: Value,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AgUiErrorEvent {
    #[serde(rename = "type")]
    pub event_type: AgUiEventType,
    pub thread_id: String,
    pub run_id: String,
    pub message: String,
    pub recoverable: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub code: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AgUiRawEvent {
    #[serde(rename = "type")]
    pub event_type: AgUiEventType,
    pub event: Value,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum AgUiEvent {
    RunStarted(AgUiRunStartedEvent),
    RunError(AgUiRunErrorEvent),
    StepStarted(AgUiStepEvent),
    StepFinished(AgUiStepEvent),
    TextMessageStart(AgUiTextMessageStartEvent),
    TextMessageContent(AgUiTextMessageDeltaEvent),
    TextMessageDelta(AgUiTextMessageDeltaEvent),
    TextMessageChunk(AgUiTextMessageChunkEvent),
    TextMessageEnd(AgUiTextMessageEndEvent),
    ToolCallStart(AgUiToolCallStartEvent),
    ToolCallArgs(AgUiToolCallArgsEvent),
    ToolCallEnd(AgUiToolCallEndEvent),
    ToolCallResult(AgUiToolCallResultEvent),
    ToolCallChunk(AgUiToolCallChunkEvent),
    StateSnapshot(AgUiStateSnapshotEvent),
    StateDelta(AgUiStateDeltaEvent),
    MessagesSnapshot(AgUiMessagesSnapshotEvent),
    ActivitySnapshot(AgUiActivitySnapshotEvent),
    ActivityDelta(AgUiActivityDeltaEvent),
    Error(AgUiErrorEvent),
    Raw(AgUiRawEvent),
    Custom(AgUiCustomEvent),
    RunFinished(AgUiRunFinishedEvent),
}

pub fn step_started_event(
    thread_id: impl Into<String>,
    run_id: impl Into<String>,
    step_id: impl Into<String>,
    name: Option<String>,
) -> AgUiEvent {
    AgUiEvent::StepStarted(AgUiStepEvent {
        event_type: AgUiEventType::StepStarted,
        thread_id: thread_id.into(),
        run_id: run_id.into(),
        step_id: step_id.into(),
        name,
    })
}

pub fn step_finished_event(
    thread_id: impl Into<String>,
    run_id: impl Into<String>,
    step_id: impl Into<String>,
    name: Option<String>,
) -> AgUiEvent {
    AgUiEvent::StepFinished(AgUiStepEvent {
        event_type: AgUiEventType::StepFinished,
        thread_id: thread_id.into(),
        run_id: run_id.into(),
        step_id: step_id.into(),
        name,
    })
}

pub fn text_message_events(
    thread_id: impl Into<String>,
    run_id: impl Into<String>,
    message_id: impl Into<String>,
    role: impl Into<String>,
    delta: impl Into<String>,
) -> Vec<AgUiEvent> {
    let thread_id = thread_id.into();
    let run_id = run_id.into();
    let message_id = message_id.into();
    let role = role.into();
    let delta = delta.into();

    vec![
        AgUiEvent::TextMessageStart(AgUiTextMessageStartEvent {
            event_type: AgUiEventType::TextMessageStart,
            thread_id: thread_id.clone(),
            run_id: run_id.clone(),
            message_id: message_id.clone(),
            role,
        }),
        AgUiEvent::TextMessageContent(AgUiTextMessageDeltaEvent {
            event_type: AgUiEventType::TextMessageContent,
            thread_id: thread_id.clone(),
            run_id: run_id.clone(),
            message_id: message_id.clone(),
            delta,
        }),
        AgUiEvent::TextMessageEnd(AgUiTextMessageEndEvent {
            event_type: AgUiEventType::TextMessageEnd,
            thread_id,
            run_id,
            message_id,
        }),
    ]
}

pub fn text_message_chunk_event(
    thread_id: impl Into<String>,
    run_id: impl Into<String>,
    message_id: Option<String>,
    role: Option<String>,
    delta: Option<String>,
) -> AgUiEvent {
    AgUiEvent::TextMessageChunk(AgUiTextMessageChunkEvent {
        event_type: AgUiEventType::TextMessageChunk,
        thread_id: thread_id.into(),
        run_id: run_id.into(),
        message_id,
        role,
        delta,
    })
}

pub fn tool_call_events(
    thread_id: impl Into<String>,
    run_id: impl Into<String>,
    tool_call_id: impl Into<String>,
    name: impl Into<String>,
    args: Value,
    result: Value,
    is_error: bool,
) -> Vec<AgUiEvent> {
    let thread_id = thread_id.into();
    let run_id = run_id.into();
    let tool_call_id = tool_call_id.into();
    let name = name.into();
    let args_delta = serde_json::to_string(&args).unwrap_or_else(|_| args.to_string());
    let result_content = serde_json::to_string(&if is_error {
        json!({
            "is_error": true,
            "result": result,
        })
    } else {
        result
    })
    .unwrap_or_else(|_| "\"\"".to_string());
    let message_id = format!("msg-{}", tool_call_id);

    vec![
        AgUiEvent::ToolCallStart(AgUiToolCallStartEvent {
            event_type: AgUiEventType::ToolCallStart,
            thread_id: thread_id.clone(),
            run_id: run_id.clone(),
            tool_call_id: tool_call_id.clone(),
            tool_call_name: name,
            parent_message_id: None,
        }),
        AgUiEvent::ToolCallArgs(AgUiToolCallArgsEvent {
            event_type: AgUiEventType::ToolCallArgs,
            thread_id: thread_id.clone(),
            run_id: run_id.clone(),
            tool_call_id: tool_call_id.clone(),
            delta: args_delta,
        }),
        AgUiEvent::ToolCallEnd(AgUiToolCallEndEvent {
            event_type: AgUiEventType::ToolCallEnd,
            thread_id: thread_id.clone(),
            run_id: run_id.clone(),
            tool_call_id: tool_call_id.clone(),
        }),
        AgUiEvent::ToolCallResult(AgUiToolCallResultEvent {
            event_type: AgUiEventType::ToolCallResult,
            thread_id,
            run_id,
            tool_call_id,
            message_id,
            content: result_content,
            role: Some("tool".to_string()),
        }),
    ]
}

pub fn tool_call_chunk_event(
    thread_id: impl Into<String>,
    run_id: impl Into<String>,
    tool_call_id: Option<String>,
    tool_call_name: Option<String>,
    parent_message_id: Option<String>,
    delta: Option<String>,
) -> AgUiEvent {
    AgUiEvent::ToolCallChunk(AgUiToolCallChunkEvent {
        event_type: AgUiEventType::ToolCallChunk,
        thread_id: thread_id.into(),
        run_id: run_id.into(),
        tool_call_id,
        tool_call_name,
        parent_message_id,
        delta,
    })
}

pub fn state_snapshot_event(
    thread_id: impl Into<String>,
    run_id: impl Into<String>,
    state: Value,
) -> AgUiEvent {
    AgUiEvent::StateSnapshot(AgUiStateSnapshotEvent {
        event_type: AgUiEventType::StateSnapshot,
        thread_id: thread_id.into(),
        run_id: run_id.into(),
        state,
    })
}

pub fn state_delta_event(
    thread_id: impl Into<String>,
    run_id: impl Into<String>,
    delta: Value,
) -> AgUiEvent {
    AgUiEvent::StateDelta(AgUiStateDeltaEvent {
        event_type: AgUiEventType::StateDelta,
        thread_id: thread_id.into(),
        run_id: run_id.into(),
        delta,
    })
}

pub fn error_event(
    thread_id: impl Into<String>,
    run_id: impl Into<String>,
    message: impl Into<String>,
    code: Option<String>,
    recoverable: bool,
) -> AgUiEvent {
    AgUiEvent::Error(AgUiErrorEvent {
        event_type: AgUiEventType::Error,
        thread_id: thread_id.into(),
        run_id: run_id.into(),
        message: message.into(),
        recoverable,
        code,
    })
}

pub fn run_error_event(
    thread_id: impl Into<String>,
    run_id: impl Into<String>,
    message: impl Into<String>,
    code: Option<String>,
) -> AgUiEvent {
    AgUiEvent::RunError(AgUiRunErrorEvent {
        event_type: AgUiEventType::RunError,
        thread_id: thread_id.into(),
        run_id: run_id.into(),
        message: message.into(),
        code,
    })
}

pub fn messages_snapshot_event(
    thread_id: impl Into<String>,
    run_id: impl Into<String>,
    messages: Vec<Value>,
) -> AgUiEvent {
    AgUiEvent::MessagesSnapshot(AgUiMessagesSnapshotEvent {
        event_type: AgUiEventType::MessagesSnapshot,
        thread_id: thread_id.into(),
        run_id: run_id.into(),
        messages,
    })
}

pub fn activity_snapshot_event(
    thread_id: impl Into<String>,
    run_id: impl Into<String>,
    message_id: impl Into<String>,
    activity_type: impl Into<String>,
    content: Value,
    replace: Option<bool>,
) -> AgUiEvent {
    AgUiEvent::ActivitySnapshot(AgUiActivitySnapshotEvent {
        event_type: AgUiEventType::ActivitySnapshot,
        thread_id: thread_id.into(),
        run_id: run_id.into(),
        message_id: message_id.into(),
        activity_type: activity_type.into(),
        content,
        replace,
    })
}

pub fn activity_delta_event(
    thread_id: impl Into<String>,
    run_id: impl Into<String>,
    message_id: impl Into<String>,
    activity_type: impl Into<String>,
    patch: Value,
) -> AgUiEvent {
    AgUiEvent::ActivityDelta(AgUiActivityDeltaEvent {
        event_type: AgUiEventType::ActivityDelta,
        thread_id: thread_id.into(),
        run_id: run_id.into(),
        message_id: message_id.into(),
        activity_type: activity_type.into(),
        patch,
    })
}

pub fn raw_event(event: Value, source: Option<String>) -> AgUiEvent {
    AgUiEvent::Raw(AgUiRawEvent {
        event_type: AgUiEventType::Raw,
        event,
        source,
    })
}

pub fn surface_to_custom_event(surface: &UiSurface) -> AgUiCustomEvent {
    AgUiCustomEvent {
        event_type: AgUiEventType::Custom,
        name: ADK_UI_SURFACE_EVENT_NAME.to_string(),
        value: json!({
            "format": "adk-ui-surface-v1",
            "surface": surface
        }),
        timestamp: None,
        raw_event: None,
    }
}

pub fn surface_to_event_stream(
    surface: &UiSurface,
    thread_id: impl Into<String>,
    run_id: impl Into<String>,
) -> Vec<AgUiEvent> {
    let thread_id = thread_id.into();
    let run_id = run_id.into();

    vec![
        AgUiEvent::RunStarted(AgUiRunStartedEvent {
            event_type: AgUiEventType::RunStarted,
            thread_id: thread_id.clone(),
            run_id: run_id.clone(),
        }),
        AgUiEvent::Custom(surface_to_custom_event(surface)),
        AgUiEvent::RunFinished(AgUiRunFinishedEvent {
            event_type: AgUiEventType::RunFinished,
            thread_id,
            run_id,
            result: None,
        }),
    ]
}

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

    #[test]
    fn surface_custom_event_is_well_formed() {
        let surface = UiSurface::new(
            "main",
            "catalog",
            vec![json!({"id":"root","component":{"Column":{"children":[]}}})],
        );
        let event = surface_to_custom_event(&surface);
        assert_eq!(event.event_type, AgUiEventType::Custom);
        assert_eq!(event.name, ADK_UI_SURFACE_EVENT_NAME);
        assert!(event.value.get("surface").is_some());
    }

    #[test]
    fn event_stream_wraps_custom_event_with_lifecycle() {
        let surface = UiSurface::new(
            "main",
            "catalog",
            vec![json!({"id":"root","component":{"Column":{"children":[]}}})],
        );
        let stream = surface_to_event_stream(&surface, "thread-1", "run-1");
        assert_eq!(stream.len(), 3);

        let first = serde_json::to_value(&stream[0]).unwrap();
        let second = serde_json::to_value(&stream[1]).unwrap();
        let third = serde_json::to_value(&stream[2]).unwrap();

        assert_eq!(first["type"], "RUN_STARTED");
        assert_eq!(second["type"], "CUSTOM");
        assert_eq!(third["type"], "RUN_FINISHED");
    }

    #[test]
    fn text_message_helpers_emit_start_content_end() {
        let events = text_message_events("thread-1", "run-1", "msg-1", "assistant", "hello");
        assert_eq!(events.len(), 3);

        let start = serde_json::to_value(&events[0]).unwrap();
        let content = serde_json::to_value(&events[1]).unwrap();
        let end = serde_json::to_value(&events[2]).unwrap();

        assert_eq!(start["type"], "TEXT_MESSAGE_START");
        assert_eq!(content["type"], "TEXT_MESSAGE_CONTENT");
        assert_eq!(content["delta"], "hello");
        assert_eq!(end["type"], "TEXT_MESSAGE_END");
    }

    #[test]
    fn tool_call_helpers_emit_lifecycle_and_result() {
        let events = tool_call_events(
            "thread-1",
            "run-1",
            "tool-1",
            "lookup_weather",
            json!({"city": "Nairobi"}),
            json!({"temp": 23}),
            false,
        );

        assert_eq!(events.len(), 4);
        let start = serde_json::to_value(&events[0]).unwrap();
        let args = serde_json::to_value(&events[1]).unwrap();
        let end = serde_json::to_value(&events[2]).unwrap();
        let result = serde_json::to_value(&events[3]).unwrap();

        assert_eq!(start["type"], "TOOL_CALL_START");
        assert_eq!(start["toolCallName"], "lookup_weather");
        assert_eq!(args["type"], "TOOL_CALL_ARGS");
        assert_eq!(args["delta"], "{\"city\":\"Nairobi\"}");
        assert_eq!(end["type"], "TOOL_CALL_END");
        assert_eq!(result["type"], "TOOL_CALL_RESULT");
        assert_eq!(result["content"], "{\"temp\":23}");
        assert_eq!(result["messageId"], "msg-tool-1");
        assert_eq!(result["role"], "tool");
    }

    #[test]
    fn state_and_error_helpers_emit_expected_shapes() {
        let snapshot = state_snapshot_event("thread-1", "run-1", json!({"phase": "planning"}));
        let delta = state_delta_event("thread-1", "run-1", json!({"phase": "acting"}));
        let error = error_event(
            "thread-1",
            "run-1",
            "tool timeout",
            Some("TIMEOUT".to_string()),
            true,
        );

        let snapshot_json = serde_json::to_value(snapshot).unwrap();
        let delta_json = serde_json::to_value(delta).unwrap();
        let error_json = serde_json::to_value(error).unwrap();

        assert_eq!(snapshot_json["type"], "STATE_SNAPSHOT");
        assert_eq!(snapshot_json["state"]["phase"], "planning");
        assert_eq!(delta_json["type"], "STATE_DELTA");
        assert_eq!(delta_json["delta"]["phase"], "acting");
        assert_eq!(error_json["type"], "ERROR");
        assert_eq!(error_json["code"], "TIMEOUT");
        assert_eq!(error_json["recoverable"], true);
    }

    #[test]
    fn stable_ag_ui_helper_events_emit_expected_shapes() {
        let run_error = run_error_event("thread-1", "run-1", "boom", Some("FAIL".to_string()));
        let text_chunk = text_message_chunk_event(
            "thread-1",
            "run-1",
            Some("msg-1".to_string()),
            Some("assistant".to_string()),
            Some("partial".to_string()),
        );
        let tool_chunk = tool_call_chunk_event(
            "thread-1",
            "run-1",
            Some("tool-1".to_string()),
            Some("lookup_weather".to_string()),
            Some("msg-1".to_string()),
            Some("{\"city\":\"Nairobi\"}".to_string()),
        );
        let messages_snapshot = messages_snapshot_event(
            "thread-1",
            "run-1",
            vec![json!({"role":"assistant","content":"hello"})],
        );
        let activity_snapshot = activity_snapshot_event(
            "thread-1",
            "run-1",
            "activity-1",
            "PLAN",
            json!({"steps":[{"title":"Research"}]}),
            Some(true),
        );
        let activity_delta = activity_delta_event(
            "thread-1",
            "run-1",
            "activity-1",
            "PLAN",
            json!([{"op":"add","path":"/steps/1","value":{"title":"Implement"}}]),
        );
        let raw = raw_event(
            json!({"source":"legacy"}),
            Some("legacy-system".to_string()),
        );

        let run_error_json = serde_json::to_value(run_error).unwrap();
        let text_chunk_json = serde_json::to_value(text_chunk).unwrap();
        let tool_chunk_json = serde_json::to_value(tool_chunk).unwrap();
        let messages_snapshot_json = serde_json::to_value(messages_snapshot).unwrap();
        let activity_snapshot_json = serde_json::to_value(activity_snapshot).unwrap();
        let activity_delta_json = serde_json::to_value(activity_delta).unwrap();
        let raw_json = serde_json::to_value(raw).unwrap();

        assert_eq!(run_error_json["type"], "RUN_ERROR");
        assert_eq!(run_error_json["message"], "boom");
        assert_eq!(text_chunk_json["type"], "TEXT_MESSAGE_CHUNK");
        assert_eq!(tool_chunk_json["type"], "TOOL_CALL_CHUNK");
        assert_eq!(messages_snapshot_json["type"], "MESSAGES_SNAPSHOT");
        assert_eq!(activity_snapshot_json["type"], "ACTIVITY_SNAPSHOT");
        assert_eq!(activity_delta_json["type"], "ACTIVITY_DELTA");
        assert_eq!(raw_json["type"], "RAW");
    }
}