adk-ui 2.1.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
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
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";

#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct AgUiIdentityCapabilities {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
    pub agent_type: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub version: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub provider: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub documentation_url: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<Value>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct AgUiTransportCapabilities {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub streaming: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub websocket: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub http_binary: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub push_notifications: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub resumable: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct AgUiToolsCapabilities {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub supported: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub items: Option<Vec<Value>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parallel_calls: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub client_provided: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct AgUiOutputCapabilities {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub structured_output: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub supported_mime_types: Option<Vec<String>>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct AgUiStateCapabilities {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub snapshots: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub deltas: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub memory: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub persistent_state: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct AgUiSubAgentInfo {
    pub name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct AgUiMultiAgentCapabilities {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub supported: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub delegation: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub handoffs: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sub_agents: Option<Vec<AgUiSubAgentInfo>>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct AgUiReasoningCapabilities {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub supported: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub streaming: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub encrypted: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct AgUiMultimodalInputCapabilities {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub image: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub audio: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub video: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pdf: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub file: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct AgUiMultimodalOutputCapabilities {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub image: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub audio: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct AgUiMultimodalCapabilities {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub input: Option<AgUiMultimodalInputCapabilities>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub output: Option<AgUiMultimodalOutputCapabilities>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct AgUiExecutionCapabilities {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub code_execution: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sandboxed: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_iterations: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_execution_time: Option<u64>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct AgUiHumanInTheLoopCapabilities {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub supported: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub approvals: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub interventions: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub feedback: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub interrupts: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub approve_with_edits: Option<bool>,
}

/// Current AG-UI capability snapshot. All fields are optional by design:
/// absence means unknown rather than unsupported.
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct AgUiAgentCapabilities {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub identity: Option<AgUiIdentityCapabilities>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub transport: Option<AgUiTransportCapabilities>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tools: Option<AgUiToolsCapabilities>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub output: Option<AgUiOutputCapabilities>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub state: Option<AgUiStateCapabilities>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub multi_agent: Option<AgUiMultiAgentCapabilities>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reasoning: Option<AgUiReasoningCapabilities>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub multimodal: Option<AgUiMultimodalCapabilities>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub execution: Option<AgUiExecutionCapabilities>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub human_in_the_loop: Option<AgUiHumanInTheLoopCapabilities>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub custom: Option<Value>,
}

pub fn default_ag_ui_agent_capabilities() -> AgUiAgentCapabilities {
    AgUiAgentCapabilities {
        identity: Some(AgUiIdentityCapabilities {
            name: Some("ADK UI example agent".to_string()),
            agent_type: Some("adk-rust".to_string()),
            description: Some("Protocol-aware generative UI agent".to_string()),
            version: Some(env!("CARGO_PKG_VERSION").to_string()),
            provider: Some("Zavora AI".to_string()),
            documentation_url: Some("https://github.com/zavora-ai/adk-ui".to_string()),
            metadata: None,
        }),
        transport: Some(AgUiTransportCapabilities {
            streaming: Some(true),
            websocket: Some(false),
            http_binary: Some(false),
            push_notifications: Some(false),
            resumable: Some(false),
        }),
        tools: Some(AgUiToolsCapabilities {
            supported: Some(true),
            items: None,
            parallel_calls: None,
            client_provided: Some(false),
        }),
        output: Some(AgUiOutputCapabilities {
            structured_output: Some(true),
            supported_mime_types: Some(vec![
                "text/event-stream".to_string(),
                "application/json".to_string(),
            ]),
        }),
        state: Some(AgUiStateCapabilities {
            snapshots: Some(true),
            deltas: Some(true),
            memory: None,
            persistent_state: Some(true),
        }),
        reasoning: Some(AgUiReasoningCapabilities {
            supported: Some(true),
            streaming: Some(true),
            encrypted: Some(false),
        }),
        human_in_the_loop: Some(AgUiHumanInTheLoopCapabilities {
            supported: Some(true),
            approvals: None,
            interventions: None,
            feedback: Some(true),
            interrupts: Some(false),
            approve_with_edits: Some(false),
        }),
        custom: Some(json!({
            "adkUiSurfaceEvents": ["ACTIVITY_SNAPSHOT", "CUSTOM"],
            "interruptWireTypes": true,
            "durableInterruptRuntime": false,
        })),
        ..Default::default()
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct AgUiInterrupt {
    pub id: String,
    pub reason: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_call_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub response_schema: Option<Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub expires_at: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<Value>,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum AgUiResumeStatus {
    Resolved,
    Cancelled,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct AgUiResumeEntry {
    pub interrupt_id: String,
    pub status: AgUiResumeStatus,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub payload: Option<Value>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct AgUiRunAgentInput {
    pub thread_id: String,
    pub run_id: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parent_run_id: Option<String>,
    pub state: Value,
    pub messages: Vec<Value>,
    pub tools: Vec<Value>,
    pub context: Vec<Value>,
    pub forwarded_props: Value,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub resume: Option<Vec<AgUiResumeEntry>>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum AgUiRunFinishedOutcome {
    Success,
    Interrupt { interrupts: Vec<AgUiInterrupt> },
}

/// AG-UI event types from the protocol event model (current stable + reasoning).
#[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,
    ReasoningStart,
    ReasoningMessageStart,
    ReasoningMessageContent,
    ReasoningMessageEnd,
    ReasoningMessageChunk,
    ReasoningEnd,
    ReasoningEncryptedValue,
    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,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parent_run_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub input: Option<Value>,
}

#[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>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub outcome: Option<AgUiRunFinishedOutcome>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AgUiRunErrorEvent {
    #[serde(rename = "type")]
    pub event_type: AgUiEventType,
    pub message: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub code: Option<String>,
    /// Optional lineage fields retained for multi-run hosts.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thread_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub run_id: 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,
    /// Official AG-UI field (`stepName`).
    pub step_name: String,
    /// Optional run lineage for multi-thread hosts (not required by core AG-UI).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thread_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub run_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub step_id: 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,
    /// Official AG-UI field name.
    pub snapshot: Value,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thread_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub run_id: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AgUiStateDeltaEvent {
    #[serde(rename = "type")]
    pub event_type: AgUiEventType,
    /// JSON Patch operations (RFC 6902).
    pub delta: Value,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thread_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub run_id: Option<String>,
}

#[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 message_id: String,
    pub activity_type: String,
    pub content: Value,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub replace: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thread_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub run_id: Option<String>,
}

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

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

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

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

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

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AgUiReasoningMessageChunkEvent {
    #[serde(rename = "type")]
    pub event_type: AgUiEventType,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub 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 AgUiReasoningEndEvent {
    #[serde(rename = "type")]
    pub event_type: AgUiEventType,
    pub message_id: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AgUiReasoningEncryptedValueEvent {
    #[serde(rename = "type")]
    pub event_type: AgUiEventType,
    pub subtype: String,
    pub entity_id: String,
    pub encrypted_value: String,
}

#[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),
    ReasoningStart(AgUiReasoningStartEvent),
    ReasoningMessageStart(AgUiReasoningMessageStartEvent),
    ReasoningMessageContent(AgUiReasoningMessageContentEvent),
    ReasoningMessageEnd(AgUiReasoningMessageEndEvent),
    ReasoningMessageChunk(AgUiReasoningMessageChunkEvent),
    ReasoningEnd(AgUiReasoningEndEvent),
    ReasoningEncryptedValue(AgUiReasoningEncryptedValueEvent),
    Error(AgUiErrorEvent),
    Raw(AgUiRawEvent),
    Custom(AgUiCustomEvent),
    RunFinished(AgUiRunFinishedEvent),
}

pub fn run_started_event(thread_id: impl Into<String>, run_id: impl Into<String>) -> AgUiEvent {
    AgUiEvent::RunStarted(AgUiRunStartedEvent {
        event_type: AgUiEventType::RunStarted,
        thread_id: thread_id.into(),
        run_id: run_id.into(),
        parent_run_id: None,
        input: None,
    })
}

pub fn run_finished_event(
    thread_id: impl Into<String>,
    run_id: impl Into<String>,
    result: Option<Value>,
) -> AgUiEvent {
    AgUiEvent::RunFinished(AgUiRunFinishedEvent {
        event_type: AgUiEventType::RunFinished,
        thread_id: thread_id.into(),
        run_id: run_id.into(),
        result,
        outcome: None,
    })
}

pub fn run_interrupted_event(
    thread_id: impl Into<String>,
    run_id: impl Into<String>,
    interrupts: Vec<AgUiInterrupt>,
) -> AgUiEvent {
    AgUiEvent::RunFinished(AgUiRunFinishedEvent {
        event_type: AgUiEventType::RunFinished,
        thread_id: thread_id.into(),
        run_id: run_id.into(),
        result: None,
        outcome: Some(AgUiRunFinishedOutcome::Interrupt { interrupts }),
    })
}

pub fn step_started_event(
    thread_id: impl Into<String>,
    run_id: impl Into<String>,
    step_id: impl Into<String>,
    name: Option<String>,
) -> AgUiEvent {
    let step_id = step_id.into();
    let step_name = name.unwrap_or_else(|| step_id.clone());
    AgUiEvent::StepStarted(AgUiStepEvent {
        event_type: AgUiEventType::StepStarted,
        step_name,
        thread_id: Some(thread_id.into()),
        run_id: Some(run_id.into()),
        step_id: Some(step_id),
    })
}

pub fn step_finished_event(
    thread_id: impl Into<String>,
    run_id: impl Into<String>,
    step_id: impl Into<String>,
    name: Option<String>,
) -> AgUiEvent {
    let step_id = step_id.into();
    let step_name = name.unwrap_or_else(|| step_id.clone());
    AgUiEvent::StepFinished(AgUiStepEvent {
        event_type: AgUiEventType::StepFinished,
        step_name,
        thread_id: Some(thread_id.into()),
        run_id: Some(run_id.into()),
        step_id: Some(step_id),
    })
}

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,
        snapshot: state,
        thread_id: Some(thread_id.into()),
        run_id: Some(run_id.into()),
    })
}

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,
        delta,
        thread_id: Some(thread_id.into()),
        run_id: Some(run_id.into()),
    })
}

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,
        message: message.into(),
        code,
        thread_id: Some(thread_id.into()),
        run_id: Some(run_id.into()),
    })
}

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,
        message_id: message_id.into(),
        activity_type: activity_type.into(),
        content,
        replace,
        thread_id: Some(thread_id.into()),
        run_id: Some(run_id.into()),
    })
}

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,
        message_id: message_id.into(),
        activity_type: activity_type.into(),
        patch,
        thread_id: Some(thread_id.into()),
        run_id: Some(run_id.into()),
    })
}

/// Emit a full reasoning message triad (start → content → end).
pub fn reasoning_message_events(
    message_id: impl Into<String>,
    delta: impl Into<String>,
) -> Vec<AgUiEvent> {
    let message_id = message_id.into();
    let delta = delta.into();
    vec![
        AgUiEvent::ReasoningStart(AgUiReasoningStartEvent {
            event_type: AgUiEventType::ReasoningStart,
            message_id: message_id.clone(),
        }),
        AgUiEvent::ReasoningMessageStart(AgUiReasoningMessageStartEvent {
            event_type: AgUiEventType::ReasoningMessageStart,
            message_id: message_id.clone(),
            role: "reasoning".to_string(),
        }),
        AgUiEvent::ReasoningMessageContent(AgUiReasoningMessageContentEvent {
            event_type: AgUiEventType::ReasoningMessageContent,
            message_id: message_id.clone(),
            delta,
        }),
        AgUiEvent::ReasoningMessageEnd(AgUiReasoningMessageEndEvent {
            event_type: AgUiEventType::ReasoningMessageEnd,
            message_id: message_id.clone(),
        }),
        AgUiEvent::ReasoningEnd(AgUiReasoningEndEvent {
            event_type: AgUiEventType::ReasoningEnd,
            message_id,
        }),
    ]
}

pub fn reasoning_message_chunk_event(
    message_id: Option<String>,
    delta: Option<String>,
) -> AgUiEvent {
    AgUiEvent::ReasoningMessageChunk(AgUiReasoningMessageChunkEvent {
        event_type: AgUiEventType::ReasoningMessageChunk,
        message_id,
        delta,
    })
}

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(),
            parent_run_id: None,
            input: None,
        }),
        // Prefer native activity for surface lifecycle; keep CUSTOM for legacy clients.
        activity_snapshot_event(
            thread_id.clone(),
            run_id.clone(),
            format!("activity-{}", surface.surface_id),
            "ADK_UI_SURFACE",
            json!({
                "format": "adk-ui-surface-v1",
                "surface": surface,
            }),
            Some(true),
        ),
        AgUiEvent::Custom(surface_to_custom_event(surface)),
        AgUiEvent::RunFinished(AgUiRunFinishedEvent {
            event_type: AgUiEventType::RunFinished,
            thread_id,
            run_id,
            result: None,
            outcome: Some(AgUiRunFinishedOutcome::Success),
        }),
    ]
}

#[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(), 4);

        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();
        let fourth = serde_json::to_value(&stream[3]).unwrap();

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

    #[test]
    fn state_snapshot_uses_official_snapshot_field() {
        let event = state_snapshot_event("t", "r", json!({"ui": "ok"}));
        let value = serde_json::to_value(event).unwrap();
        assert_eq!(value["type"], "STATE_SNAPSHOT");
        assert_eq!(value["snapshot"]["ui"], "ok");
        assert!(value.get("state").is_none());
    }

    #[test]
    fn reasoning_helpers_emit_official_event_types() {
        let events = reasoning_message_events("msg-r1", "thinking...");
        let types: Vec<_> = events
            .iter()
            .map(|e| {
                serde_json::to_value(e).unwrap()["type"]
                    .as_str()
                    .unwrap()
                    .to_string()
            })
            .collect();
        assert!(types.contains(&"REASONING_START".to_string()));
        assert!(types.contains(&"REASONING_MESSAGE_CONTENT".to_string()));
        assert!(types.contains(&"REASONING_END".to_string()));
    }

    #[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["snapshot"]["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");
    }

    #[test]
    fn capability_snapshot_uses_current_typed_categories() {
        let capabilities = serde_json::to_value(default_ag_ui_agent_capabilities()).unwrap();

        assert_eq!(capabilities["transport"]["streaming"], true);
        assert_eq!(capabilities["transport"]["resumable"], false);
        assert_eq!(capabilities["state"]["snapshots"], true);
        assert_eq!(capabilities["reasoning"]["encrypted"], false);
        assert_eq!(capabilities["humanInTheLoop"]["interrupts"], false);
        assert_eq!(capabilities["custom"]["interruptWireTypes"], true);
    }

    #[test]
    fn interrupt_outcome_and_resume_entry_match_ag_ui_wire_shape() {
        let event = run_interrupted_event(
            "thread-1",
            "run-1",
            vec![AgUiInterrupt {
                id: "interrupt-1".to_string(),
                reason: "confirmation".to_string(),
                message: Some("Continue?".to_string()),
                tool_call_id: None,
                response_schema: Some(json!({"type":"boolean"})),
                expires_at: None,
                metadata: None,
            }],
        );
        let event_json = serde_json::to_value(event).unwrap();
        assert_eq!(event_json["type"], "RUN_FINISHED");
        assert_eq!(event_json["outcome"]["type"], "interrupt");
        assert_eq!(event_json["outcome"]["interrupts"][0]["id"], "interrupt-1");

        let resume: AgUiResumeEntry = serde_json::from_value(json!({
            "interruptId": "interrupt-1",
            "status": "resolved",
            "payload": {"approved": true}
        }))
        .unwrap();
        assert_eq!(resume.status, AgUiResumeStatus::Resolved);
    }
}