adk-realtime 2.0.0

Real-time bidirectional audio/video streaming for Rust Agent Development Kit (ADK-Rust) agents
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
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
//! RealtimeAgent - an Agent implementation for real-time voice interactions.
//!
//! This module provides `RealtimeAgent`, which implements the `adk_core::Agent` trait
//! and provides the same callback/tool/instruction features as `LlmAgent`, but uses
//! real-time bidirectional audio streaming instead of text-based LLM calls.
//!
//! # Architecture
//!
//! ```text
//!                     ┌─────────────────────────────────────────┐
//!                     │              Agent Trait                │
//!                     │  (name, description, run, sub_agents)   │
//!                     └────────────────┬────────────────────────┘
//!//!              ┌───────────────────────┼───────────────────────┐
//!              │                       │                       │
//!     ┌────────▼────────┐    ┌─────────▼─────────┐   ┌─────────▼─────────┐
//!     │    LlmAgent     │    │  RealtimeAgent    │   │  SequentialAgent  │
//!     │  (text-based)   │    │  (voice-based)    │   │   (workflow)      │
//!     └─────────────────┘    └───────────────────┘   └───────────────────┘
//! ```
//!
//! # Shared Features with LlmAgent
//!
//! - **Tools**: Function tools that can be called during conversation
//! - **Callbacks**: before_agent, after_agent, before_tool, after_tool
//! - **Instructions**: Static or dynamic instruction providers
//! - **Sub-agents**: Agent handoff/transfer support
//! - **Context**: Full access to InvocationContext (session, memory, artifacts)
//!
//! # Example
//!
//! ```rust,ignore
//! use adk_realtime::RealtimeAgent;
//! use adk_realtime::openai::OpenAIRealtimeModel;
//!
//! let model = OpenAIRealtimeModel::new(api_key, "gpt-realtime");
//!
//! let agent = RealtimeAgent::builder("voice_assistant")
//!     .model(Box::new(model))
//!     .instruction("You are a helpful voice assistant.")
//!     .voice("alloy")
//!     .tool(Arc::new(weather_tool))
//!     .before_agent_callback(|ctx| async move {
//!         println!("Starting voice session for user: {}", ctx.user_id());
//!         Ok(None)
//!     })
//!     .build()?;
//!
//! // Run through standard ADK runner
//! let runner = Runner::new(agent);
//! runner.run(session, user_content).await?;
//! ```

use crate::config::{RealtimeConfig, ToolDefinition, VadConfig, VadMode};
use crate::events::{ServerEvent, ToolResponse};
use adk_core::{
    AdkError, AfterAgentCallback, AfterToolCallback, Agent, BeforeAgentCallback,
    BeforeToolCallback, CallbackContext, Content, Event, EventActions, EventStream,
    GlobalInstructionProvider, InstructionProvider, InvocationContext, MemoryEntry, Part,
    ReadonlyContext, Result, Tool, ToolCallbackContext, ToolContext, Toolset,
};
use async_stream::stream;
use async_trait::async_trait;

use std::sync::{Arc, Mutex};

/// Shared realtime model type (thread-safe for async usage).
pub type BoxedRealtimeModel = Arc<dyn crate::model::RealtimeModel>;

/// A real-time voice agent that implements the ADK Agent trait.
///
/// `RealtimeAgent` provides bidirectional audio streaming while maintaining
/// compatibility with the standard ADK agent ecosystem. It supports the same
/// callbacks, tools, and instruction patterns as `LlmAgent`.
pub struct RealtimeAgent {
    name: String,
    description: String,
    model: BoxedRealtimeModel,

    // Instructions (same as LlmAgent)
    instruction: Option<String>,
    instruction_provider: Option<Arc<InstructionProvider>>,
    global_instruction: Option<String>,
    global_instruction_provider: Option<Arc<GlobalInstructionProvider>>,

    // Voice-specific settings
    voice: Option<String>,
    vad_config: Option<VadConfig>,
    modalities: Vec<String>,

    // Tools (same as LlmAgent)
    tools: Vec<Arc<dyn Tool>>,
    toolsets: Vec<Arc<dyn Toolset>>,
    sub_agents: Vec<Arc<dyn Agent>>,

    // Callbacks (same as LlmAgent)
    before_callbacks: Arc<Vec<BeforeAgentCallback>>,
    after_callbacks: Arc<Vec<AfterAgentCallback>>,
    before_tool_callbacks: Arc<Vec<BeforeToolCallback>>,
    after_tool_callbacks: Arc<Vec<AfterToolCallback>>,

    // Realtime-specific callbacks
    on_audio: Option<AudioCallback>,
    on_transcript: Option<TranscriptCallback>,
    on_speech_started: Option<SpeechCallback>,
    on_speech_stopped: Option<SpeechCallback>,

    // Video avatar configuration
    #[cfg(feature = "video-avatar")]
    avatar_config: Option<crate::avatar::AvatarConfig>,

    // Video avatar provider instance
    #[cfg(feature = "video-avatar")]
    avatar_provider: Option<std::sync::Arc<dyn crate::avatar::AvatarProvider>>,
}

/// Callback for audio output events (receives raw PCM bytes).
pub type AudioCallback = Arc<
    dyn Fn(&[u8], &str) -> std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send>>
        + Send
        + Sync,
>;

/// Callback for transcript events.
pub type TranscriptCallback = Arc<
    dyn Fn(&str, &str) -> std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send>>
        + Send
        + Sync,
>;

/// Callback for speech detection events.
pub type SpeechCallback = Arc<
    dyn Fn(u64) -> std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send>> + Send + Sync,
>;

impl std::fmt::Debug for RealtimeAgent {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RealtimeAgent")
            .field("name", &self.name)
            .field("description", &self.description)
            .field("model", &self.model.model_id())
            .field("voice", &self.voice)
            .field("tools_count", &self.tools.len())
            .field("toolsets_count", &self.toolsets.len())
            .field("sub_agents_count", &self.sub_agents.len())
            .finish()
    }
}

/// Builder for RealtimeAgent.
pub struct RealtimeAgentBuilder {
    name: String,
    description: Option<String>,
    model: Option<BoxedRealtimeModel>,
    instruction: Option<String>,
    instruction_provider: Option<Arc<InstructionProvider>>,
    global_instruction: Option<String>,
    global_instruction_provider: Option<Arc<GlobalInstructionProvider>>,
    voice: Option<String>,
    vad_config: Option<VadConfig>,
    modalities: Vec<String>,
    tools: Vec<Arc<dyn Tool>>,
    toolsets: Vec<Arc<dyn Toolset>>,
    sub_agents: Vec<Arc<dyn Agent>>,
    before_callbacks: Vec<BeforeAgentCallback>,
    after_callbacks: Vec<AfterAgentCallback>,
    before_tool_callbacks: Vec<BeforeToolCallback>,
    after_tool_callbacks: Vec<AfterToolCallback>,
    on_audio: Option<AudioCallback>,
    on_transcript: Option<TranscriptCallback>,
    on_speech_started: Option<SpeechCallback>,
    on_speech_stopped: Option<SpeechCallback>,

    #[cfg(feature = "video-avatar")]
    avatar_config: Option<crate::avatar::AvatarConfig>,

    #[cfg(feature = "video-avatar")]
    avatar_provider: Option<std::sync::Arc<dyn crate::avatar::AvatarProvider>>,
}

impl RealtimeAgentBuilder {
    /// Create a new builder with the given agent name.
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            description: None,
            model: None,
            instruction: None,
            instruction_provider: None,
            global_instruction: None,
            global_instruction_provider: None,
            voice: None,
            vad_config: None,
            modalities: vec!["text".to_string(), "audio".to_string()],
            tools: Vec::new(),
            toolsets: Vec::new(),
            sub_agents: Vec::new(),
            before_callbacks: Vec::new(),
            after_callbacks: Vec::new(),
            before_tool_callbacks: Vec::new(),
            after_tool_callbacks: Vec::new(),
            on_audio: None,
            on_transcript: None,
            on_speech_started: None,
            on_speech_stopped: None,
            #[cfg(feature = "video-avatar")]
            avatar_config: None,
            #[cfg(feature = "video-avatar")]
            avatar_provider: None,
        }
    }

    /// Set the agent description.
    pub fn description(mut self, desc: impl Into<String>) -> Self {
        self.description = Some(desc.into());
        self
    }

    /// Set the realtime model.
    pub fn model(mut self, model: BoxedRealtimeModel) -> Self {
        self.model = Some(model);
        self
    }

    /// Set a static instruction.
    pub fn instruction(mut self, instruction: impl Into<String>) -> Self {
        self.instruction = Some(instruction.into());
        self
    }

    /// Set a dynamic instruction provider.
    pub fn instruction_provider(mut self, provider: InstructionProvider) -> Self {
        self.instruction_provider = Some(Arc::new(provider));
        self
    }

    /// Set a static global instruction.
    pub fn global_instruction(mut self, instruction: impl Into<String>) -> Self {
        self.global_instruction = Some(instruction.into());
        self
    }

    /// Set a dynamic global instruction provider.
    pub fn global_instruction_provider(mut self, provider: GlobalInstructionProvider) -> Self {
        self.global_instruction_provider = Some(Arc::new(provider));
        self
    }

    /// Set the voice for audio output.
    pub fn voice(mut self, voice: impl Into<String>) -> Self {
        self.voice = Some(voice.into());
        self
    }

    /// Set voice activity detection configuration.
    pub fn vad(mut self, config: VadConfig) -> Self {
        self.vad_config = Some(config);
        self
    }

    /// Enable server-side VAD with default settings.
    pub fn server_vad(mut self) -> Self {
        self.vad_config = Some(VadConfig {
            mode: VadMode::ServerVad,
            threshold: Some(0.5),
            prefix_padding_ms: Some(300),
            silence_duration_ms: Some(500),
            interrupt_response: Some(true),
            eagerness: None,
        });
        self
    }

    /// Set output modalities (e.g., ["text", "audio"]).
    pub fn modalities(mut self, modalities: Vec<String>) -> Self {
        self.modalities = modalities;
        self
    }

    /// Add a tool.
    pub fn tool(mut self, tool: Arc<dyn Tool>) -> Self {
        self.tools.push(tool);
        self
    }

    /// Register a dynamic toolset for per-invocation tool resolution.
    ///
    /// Toolsets are resolved at the start of each `run()` call using the
    /// invocation's `ReadonlyContext`. This enables context-dependent tools
    /// like per-user browser sessions from a pool.
    pub fn toolset(mut self, toolset: Arc<dyn Toolset>) -> Self {
        self.toolsets.push(toolset);
        self
    }

    /// Add a sub-agent for handoffs.
    pub fn sub_agent(mut self, agent: Arc<dyn Agent>) -> Self {
        self.sub_agents.push(agent);
        self
    }

    /// Add a before-agent callback.
    pub fn before_agent_callback(mut self, callback: BeforeAgentCallback) -> Self {
        self.before_callbacks.push(callback);
        self
    }

    /// Add an after-agent callback.
    pub fn after_agent_callback(mut self, callback: AfterAgentCallback) -> Self {
        self.after_callbacks.push(callback);
        self
    }

    /// Add a before-tool callback.
    pub fn before_tool_callback(mut self, callback: BeforeToolCallback) -> Self {
        self.before_tool_callbacks.push(callback);
        self
    }

    /// Add an after-tool callback.
    pub fn after_tool_callback(mut self, callback: AfterToolCallback) -> Self {
        self.after_tool_callbacks.push(callback);
        self
    }

    /// Set callback for audio output events.
    pub fn on_audio(mut self, callback: AudioCallback) -> Self {
        self.on_audio = Some(callback);
        self
    }

    /// Set callback for transcript events.
    pub fn on_transcript(mut self, callback: TranscriptCallback) -> Self {
        self.on_transcript = Some(callback);
        self
    }

    /// Set callback for speech started events.
    pub fn on_speech_started(mut self, callback: SpeechCallback) -> Self {
        self.on_speech_started = Some(callback);
        self
    }

    /// Set callback for speech stopped events.
    pub fn on_speech_stopped(mut self, callback: SpeechCallback) -> Self {
        self.on_speech_stopped = Some(callback);
        self
    }

    /// Set the video avatar configuration for this agent.
    ///
    /// When set, the avatar configuration is included in the session setup
    /// payload sent to the realtime provider. If the provider does not support
    /// video avatars, a warning is logged and the session proceeds audio-only.
    ///
    /// Requires the `video-avatar` feature flag.
    #[cfg(feature = "video-avatar")]
    pub fn avatar(mut self, config: crate::avatar::AvatarConfig) -> Self {
        self.avatar_config = Some(config);
        self
    }

    /// Set the video avatar provider for this agent.
    ///
    /// When both an `AvatarConfig` (with a provider kind) and an `AvatarProvider`
    /// instance are set, the runner routes audio through the avatar provider
    /// for lip-sync rendering instead of sending raw audio to the client.
    ///
    /// Requires the `video-avatar` feature flag.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use std::sync::Arc;
    /// use adk_realtime::avatar::heygen::{HeyGenConfig, HeyGenProvider};
    ///
    /// let provider = Arc::new(HeyGenProvider::new(HeyGenConfig::new("key")));
    /// let agent = RealtimeAgentBuilder::new("assistant")
    ///     .avatar(avatar_config)
    ///     .avatar_provider(provider)
    ///     .build()?;
    /// ```
    #[cfg(feature = "video-avatar")]
    pub fn avatar_provider(
        mut self,
        provider: std::sync::Arc<dyn crate::avatar::AvatarProvider>,
    ) -> Self {
        self.avatar_provider = Some(provider);
        self
    }

    /// Build the RealtimeAgent.
    pub fn build(self) -> Result<RealtimeAgent> {
        let model =
            self.model.ok_or_else(|| AdkError::agent("RealtimeModel is required".to_string()))?;

        Ok(RealtimeAgent {
            name: self.name,
            description: self.description.unwrap_or_default(),
            model,
            instruction: self.instruction,
            instruction_provider: self.instruction_provider,
            global_instruction: self.global_instruction,
            global_instruction_provider: self.global_instruction_provider,
            voice: self.voice,
            vad_config: self.vad_config,
            modalities: self.modalities,
            tools: self.tools,
            toolsets: self.toolsets,
            sub_agents: self.sub_agents,
            before_callbacks: Arc::new(self.before_callbacks),
            after_callbacks: Arc::new(self.after_callbacks),
            before_tool_callbacks: Arc::new(self.before_tool_callbacks),
            after_tool_callbacks: Arc::new(self.after_tool_callbacks),
            on_audio: self.on_audio,
            on_transcript: self.on_transcript,
            on_speech_started: self.on_speech_started,
            on_speech_stopped: self.on_speech_stopped,
            #[cfg(feature = "video-avatar")]
            avatar_config: self.avatar_config,
            #[cfg(feature = "video-avatar")]
            avatar_provider: self.avatar_provider,
        })
    }
}

impl RealtimeAgent {
    /// Create a new builder.
    pub fn builder(name: impl Into<String>) -> RealtimeAgentBuilder {
        RealtimeAgentBuilder::new(name)
    }

    /// Get the static instruction, if set.
    pub fn instruction(&self) -> Option<&String> {
        self.instruction.as_ref()
    }

    /// Get the voice setting, if set.
    pub fn voice(&self) -> Option<&String> {
        self.voice.as_ref()
    }

    /// Get the VAD configuration, if set.
    pub fn vad_config(&self) -> Option<&VadConfig> {
        self.vad_config.as_ref()
    }

    /// Get the list of tools.
    pub fn tools(&self) -> &[Arc<dyn Tool>] {
        &self.tools
    }

    /// Get the avatar configuration, if set.
    ///
    /// Requires the `video-avatar` feature flag.
    #[cfg(feature = "video-avatar")]
    pub fn avatar_config(&self) -> Option<&crate::avatar::AvatarConfig> {
        self.avatar_config.as_ref()
    }

    /// Get the avatar provider, if set.
    ///
    /// Requires the `video-avatar` feature flag.
    #[cfg(feature = "video-avatar")]
    pub fn avatar_provider(&self) -> Option<&std::sync::Arc<dyn crate::avatar::AvatarProvider>> {
        self.avatar_provider.as_ref()
    }

    /// Build the realtime configuration from agent settings.
    async fn build_config(
        &self,
        ctx: &Arc<dyn InvocationContext>,
        resolved_tools: &[Arc<dyn Tool>],
    ) -> Result<RealtimeConfig> {
        let mut config = RealtimeConfig::default();

        // Build instruction from providers or static value
        if let Some(provider) = &self.global_instruction_provider {
            let global_inst = provider(ctx.clone() as Arc<dyn ReadonlyContext>).await?;
            if !global_inst.is_empty() {
                config.instruction = Some(global_inst);
            }
        } else if let Some(ref template) = self.global_instruction {
            let processed = adk_core::inject_session_state(ctx.as_ref(), template).await?;
            config.instruction = Some(processed);
        }

        // Add agent-specific instruction
        if let Some(provider) = &self.instruction_provider {
            let inst = provider(ctx.clone() as Arc<dyn ReadonlyContext>).await?;
            if !inst.is_empty() {
                if let Some(existing) = &mut config.instruction {
                    existing.push_str("\n\n");
                    existing.push_str(&inst);
                } else {
                    config.instruction = Some(inst);
                }
            }
        } else if let Some(ref template) = self.instruction {
            let processed = adk_core::inject_session_state(ctx.as_ref(), template).await?;
            if let Some(existing) = &mut config.instruction {
                existing.push_str("\n\n");
                existing.push_str(&processed);
            } else {
                config.instruction = Some(processed);
            }
        }

        // Voice settings
        config.voice = self.voice.clone();
        config.turn_detection = self.vad_config.clone();
        config.modalities = Some(self.modalities.clone());

        // Convert ADK tools to realtime tool definitions
        let tool_defs: Vec<ToolDefinition> = resolved_tools
            .iter()
            .map(|t| ToolDefinition {
                name: t.name().to_string(),
                description: Some(t.enhanced_description().to_string()),
                parameters: t.parameters_schema(),
            })
            .collect();

        if !tool_defs.is_empty() {
            config.tools = Some(tool_defs);
        }

        // Add transfer_to_agent tool if sub-agents exist
        if !self.sub_agents.is_empty() {
            let mut tools = config.tools.unwrap_or_default();
            tools.push(ToolDefinition {
                name: "transfer_to_agent".to_string(),
                description: Some("Transfer execution to another agent.".to_string()),
                parameters: Some(serde_json::json!({
                    "type": "object",
                    "properties": {
                        "agent_name": {
                            "type": "string",
                            "description": "The name of the agent to transfer to."
                        }
                    },
                    "required": ["agent_name"]
                })),
            });
            config.tools = Some(tools);
        }

        // Include avatar configuration in session setup if present.
        // Currently no realtime provider supports video avatars natively,
        // so we log a warning and proceed audio-only. The config is still
        // placed in `extra` so future provider implementations can read it.
        #[cfg(feature = "video-avatar")]
        if let Some(ref avatar) = self.avatar_config {
            tracing::warn!(
                agent = %self.name,
                source_url = %avatar.source_url,
                "video avatar configured but the current realtime provider does not support video avatars; proceeding audio-only"
            );
            let avatar_json = serde_json::to_value(avatar).unwrap_or_else(|e| {
                tracing::warn!("failed to serialize avatar config: {e}");
                serde_json::Value::Null
            });
            let extra = config.extra.get_or_insert_with(|| serde_json::json!({}));
            if let Some(obj) = extra.as_object_mut() {
                obj.insert("avatarConfig".to_string(), avatar_json);
            }
        }

        Ok(config)
    }

    /// Execute a tool call.
    #[allow(dead_code)]
    async fn execute_tool(
        &self,
        ctx: &Arc<dyn InvocationContext>,
        call_id: &str,
        name: &str,
        arguments: &str,
    ) -> (serde_json::Value, EventActions) {
        // Find the tool
        let tool = self.tools.iter().find(|t| t.name() == name);

        if let Some(tool) = tool {
            let args: serde_json::Value =
                serde_json::from_str(arguments).unwrap_or(serde_json::json!({}));

            // Create tool context
            let tool_ctx: Arc<dyn ToolContext> =
                Arc::new(RealtimeToolContext::new(ctx.clone(), call_id.to_string()));

            // Execute before_tool callbacks
            let tool_cb_ctx =
                Arc::new(ToolCallbackContext::new(ctx.clone(), name.to_string(), args.clone()));
            for callback in self.before_tool_callbacks.as_ref() {
                if let Err(e) = callback(tool_cb_ctx.clone() as Arc<dyn CallbackContext>).await {
                    return (
                        serde_json::json!({ "error": e.to_string() }),
                        EventActions::default(),
                    );
                }
            }

            // Execute the tool
            let result = match tool.execute(tool_ctx.clone(), args.clone()).await {
                Ok(result) => result,
                Err(e) => serde_json::json!({ "error": e.to_string() }),
            };

            let actions = tool_ctx.actions();

            // Execute after_tool callbacks
            let tool_cb_ctx =
                Arc::new(ToolCallbackContext::new(ctx.clone(), name.to_string(), args.clone()));
            for callback in self.after_tool_callbacks.as_ref() {
                if let Err(e) = callback(tool_cb_ctx.clone() as Arc<dyn CallbackContext>).await {
                    return (serde_json::json!({ "error": e.to_string() }), actions);
                }
            }

            (result, actions)
        } else {
            (
                serde_json::json!({ "error": format!("Tool {} not found", name) }),
                EventActions::default(),
            )
        }
    }
}

#[async_trait]
impl Agent for RealtimeAgent {
    fn name(&self) -> &str {
        &self.name
    }

    fn description(&self) -> &str {
        &self.description
    }

    fn sub_agents(&self) -> &[Arc<dyn Agent>] {
        &self.sub_agents
    }

    async fn run(&self, ctx: Arc<dyn InvocationContext>) -> Result<EventStream> {
        let agent_name = self.name.clone();
        let invocation_id = ctx.invocation_id().to_string();
        let model = self.model.clone();
        let _sub_agents = self.sub_agents.clone();

        // Clone callback refs
        let before_callbacks = self.before_callbacks.clone();
        let after_callbacks = self.after_callbacks.clone();
        let before_tool_callbacks = self.before_tool_callbacks.clone();
        let after_tool_callbacks = self.after_tool_callbacks.clone();
        let tools = self.tools.clone();
        let toolsets = self.toolsets.clone();

        // Clone realtime callbacks
        let on_audio = self.on_audio.clone();
        let on_transcript = self.on_transcript.clone();
        let on_speech_started = self.on_speech_started.clone();
        let on_speech_stopped = self.on_speech_stopped.clone();

        // Clone avatar provider for the stream closure
        #[cfg(feature = "video-avatar")]
        let avatar_provider = self.avatar_provider.clone();
        #[cfg(feature = "video-avatar")]
        let avatar_config_for_session = self.avatar_config.clone();

        // ===== RESOLVE TOOLSETS =====
        let mut resolved_tools: Vec<Arc<dyn Tool>> = tools.clone();
        let static_tool_names: std::collections::HashSet<String> =
            tools.iter().map(|t| t.name().to_string()).collect();
        let mut toolset_source: std::collections::HashMap<String, String> =
            std::collections::HashMap::new();

        for toolset in &toolsets {
            let toolset_tools = toolset.tools(ctx.clone() as Arc<dyn ReadonlyContext>).await?;
            for tool in &toolset_tools {
                let name = tool.name().to_string();
                if static_tool_names.contains(&name) {
                    return Err(AdkError::agent(format!(
                        "Duplicate tool name '{}': conflict between static tool and toolset '{}'",
                        name,
                        toolset.name()
                    )));
                }
                if let Some(other_toolset_name) = toolset_source.get(&name) {
                    return Err(AdkError::agent(format!(
                        "Duplicate tool name '{}': conflict between toolset '{}' and toolset '{}'",
                        name,
                        other_toolset_name,
                        toolset.name()
                    )));
                }
                toolset_source.insert(name, toolset.name().to_string());
                resolved_tools.push(tool.clone());
            }
        }

        // Build config with resolved tools
        let config = self.build_config(&ctx, &resolved_tools).await?;

        let s = stream! {
            // ===== BEFORE AGENT CALLBACKS =====
            for callback in before_callbacks.as_ref() {
                match callback(ctx.clone() as Arc<dyn CallbackContext>).await {
                    Ok(Some(content)) => {
                        let mut early_event = Event::new(&invocation_id);
                        early_event.author = agent_name.clone();
                        early_event.llm_response.content = Some(content);
                        yield Ok(early_event);
                        return;
                    }
                    Ok(None) => continue,
                    Err(e) => {
                        yield Err(e);
                        return;
                    }
                }
            }

            // ===== CONNECT TO REALTIME SESSION =====
            let session = match model.connect(config).await {
                Ok(s) => s,
                Err(e) => {
                    yield Err(AdkError::model(format!("Failed to connect: {}", e)));
                    return;
                }
            };

            // Yield session started event
            let mut start_event = Event::new(&invocation_id);
            start_event.author = agent_name.clone();
            start_event.llm_response.content = Some(Content {
                role: "system".to_string(),
                parts: vec![Part::Text {
                    text: format!("Realtime session started: {}", session.session_id()),
                }],
            });
            yield Ok(start_event);

            // ===== START AVATAR SESSION (if configured) =====
            #[cfg(feature = "video-avatar")]
            let avatar_session_id: Option<String> = {
                if let (Some(provider), Some(config)) = (&avatar_provider, &avatar_config_for_session) {
                    match provider.start_session(config).await {
                        Ok(session_info) => {
                            tracing::info!(
                                provider = %session_info.provider,
                                session_id = %session_info.session_id,
                                "avatar session started"
                            );
                            // Emit avatar session info as an event for the client
                            let mut avatar_event = Event::new(&invocation_id);
                            avatar_event.author = agent_name.clone();
                            avatar_event.llm_response.content = Some(Content {
                                role: "system".to_string(),
                                parts: vec![Part::Text {
                                    text: serde_json::to_string(&session_info).unwrap_or_default(),
                                }],
                            });
                            yield Ok(avatar_event);
                            Some(session_info.session_id)
                        }
                        Err(e) => {
                            // Graceful degradation: log warning, continue audio-only
                            tracing::warn!(
                                error = %e,
                                "avatar session creation failed, falling back to audio-only"
                            );
                            None
                        }
                    }
                } else {
                    None
                }
            };
            #[cfg(not(feature = "video-avatar"))]
            let _avatar_session_id: Option<String> = None;

            // Spawn keep-alive task for avatar session
            #[cfg(feature = "video-avatar")]
            let _avatar_keep_alive_handle: Option<tokio::task::JoinHandle<()>> = {
                if let (Some(provider), Some(sess_id)) = (&avatar_provider, &avatar_session_id) {
                    Some(crate::avatar::spawn_keep_alive(
                        provider.clone(),
                        sess_id.clone(),
                        std::time::Duration::from_secs(30),
                    ))
                } else {
                    None
                }
            };

            // ===== SEND INITIAL USER CONTENT =====
            // If user provided text input, send it to start the conversation
            let user_content = ctx.user_content();
            for part in &user_content.parts {
                if let Part::Text { text } = part {
                    if let Err(e) = session.send_text(text).await {
                        yield Err(AdkError::model(format!("Failed to send text: {}", e)));
                        return;
                    }
                    // Request a response
                    if let Err(e) = session.create_response().await {
                        yield Err(AdkError::model(format!("Failed to create response: {}", e)));
                        return;
                    }
                }
            }

            // ===== PROCESS REALTIME EVENTS =====
            loop {
                let event = session.next_event().await;

                match event {
                    Some(Ok(server_event)) => {
                        match server_event {
                            ServerEvent::AudioDelta { delta, item_id, .. } => {
                                // Route audio through avatar provider if active
                                #[cfg(feature = "video-avatar")]
                                if let (Some(provider), Some(sess_id)) = (&avatar_provider, &avatar_session_id) {
                                    if let Err(e) = provider.send_audio(sess_id, &delta).await {
                                        tracing::warn!(error = %e, "avatar send_audio failed");
                                    }
                                    // Don't yield raw audio to client — avatar provides video+audio
                                    // Still call the on_audio callback for monitoring
                                    if let Some(ref cb) = on_audio {
                                        cb(&delta, &item_id).await;
                                    }
                                    continue;
                                }

                                // No avatar provider — send raw audio to client
                                if let Some(ref cb) = on_audio {
                                    cb(&delta, &item_id).await;
                                }

                                // Yield audio event (delta is already raw bytes)
                                let mut audio_event = Event::new(&invocation_id);
                                audio_event.author = agent_name.clone();
                                audio_event.llm_response.content = Some(Content {
                                    role: "model".to_string(),
                                    parts: vec![Part::InlineData {
                                        mime_type: "audio/pcm".to_string(),
                                        data: delta,
                                        uri: None,
                                        annotations: None,
                                    }],
                                });
                                yield Ok(audio_event);
                            }

                            ServerEvent::TextDelta { delta, .. } => {
                                let mut text_event = Event::new(&invocation_id);
                                text_event.author = agent_name.clone();
                                text_event.llm_response.content = Some(Content {
                                    role: "model".to_string(),
                                    parts: vec![Part::Text { text: delta.clone() }],
                                });
                                yield Ok(text_event);
                            }

                            ServerEvent::TranscriptDelta { delta, item_id, .. } => {
                                if let Some(ref cb) = on_transcript {
                                    cb(&delta, &item_id).await;
                                }
                            }

                            ServerEvent::SpeechStarted { audio_start_ms, .. } => {
                                if let Some(ref cb) = on_speech_started {
                                    cb(audio_start_ms).await;
                                }
                            }

                            ServerEvent::SpeechStopped { audio_end_ms, .. } => {
                                if let Some(ref cb) = on_speech_stopped {
                                    cb(audio_end_ms).await;
                                }
                            }

                            ServerEvent::FunctionCallDone {
                                call_id,
                                name,
                                arguments,
                                ..
                            } => {
                                // Handle transfer_to_agent
                                if name == "transfer_to_agent" {
                                    let args: serde_json::Value = serde_json::from_str(&arguments)
                                        .unwrap_or(serde_json::json!({}));
                                    let target = args.get("agent_name")
                                        .and_then(|v| v.as_str())
                                        .unwrap_or_default()
                                        .to_string();

                                    let mut transfer_event = Event::new(&invocation_id);
                                    transfer_event.author = agent_name.clone();
                                    transfer_event.actions.transfer_to_agent = Some(target);
                                    yield Ok(transfer_event);

                                    let _ = session.close().await;
                                    return;
                                }

                                // Execute tool
                                let tool = resolved_tools.iter().find(|t| t.name() == name);

                                let (result, actions) = if let Some(tool) = tool {
                                    let args: serde_json::Value = serde_json::from_str(&arguments)
                                        .unwrap_or(serde_json::json!({}));

                                    let tool_ctx: Arc<dyn ToolContext> = Arc::new(
                                        RealtimeToolContext::new(ctx.clone(), call_id.clone())
                                    );

                                    let cb_ctx: Arc<dyn CallbackContext> =
                                        Arc::new(ToolCallbackContext::new(
                                            ctx.clone(),
                                            name.clone(),
                                            args.clone(),
                                        ));

                                    let result = execute_tool_with_callbacks(
                                        tool.as_ref(),
                                        tool_ctx.clone(),
                                        cb_ctx,
                                        args.clone(),
                                        before_tool_callbacks.as_ref(),
                                        after_tool_callbacks.as_ref(),
                                    )
                                    .await;

                                    (result, tool_ctx.actions())
                                } else {
                                    (
                                        serde_json::json!({ "error": format!("Tool {} not found", name) }),
                                        EventActions::default(),
                                    )
                                };

                                // Yield tool event
                                let mut tool_event = Event::new(&invocation_id);
                                tool_event.author = agent_name.clone();
                                tool_event.actions = actions.clone();
                                tool_event.llm_response.content = Some(Content {
                                    role: "function".to_string(),
                                    parts: vec![Part::FunctionResponse {
                                        function_response: adk_core::FunctionResponseData::new(name.clone(), result.clone()),
                                        id: Some(call_id.clone()),
                                        annotations: None,
                                    }],
                                });
                                yield Ok(tool_event);

                                // Check for escalation
                                if actions.escalate || actions.skip_summarization {
                                    let _ = session.close().await;
                                    return;
                                }

                                // Send tool response back to session
                                let response = ToolResponse {
                                    call_id,
                                    output: result,
                                };
                                if let Err(e) = session.send_tool_response(response).await {
                                    yield Err(AdkError::model(format!("Failed to send tool response: {}", e)));
                                    let _ = session.close().await;
                                    return;
                                }
                            }

                            ServerEvent::ResponseDone { .. } => {
                                // Response complete, continue listening
                            }

                            ServerEvent::Error { error, .. } => {
                                yield Err(AdkError::model(format!(
                                    "Realtime error: {} - {}",
                                    error.code.unwrap_or_default(),
                                    error.message
                                )));
                            }


                            _ => {
                                // Ignore other events
                            }
                        }
                    }
                    Some(Err(e)) => {
                        yield Err(AdkError::model(format!("Session error: {}", e)));
                        break;
                    }
                    None => {
                        // Session closed
                        break;
                    }
                }
            }

            // ===== STOP AVATAR SESSION (cleanup) =====
            #[cfg(feature = "video-avatar")]
            {
                // Abort keep-alive task
                if let Some(handle) = _avatar_keep_alive_handle {
                    handle.abort();
                }
                // Stop the avatar session
                if let (Some(provider), Some(sess_id)) = (&avatar_provider, &avatar_session_id) {
                    if let Err(e) = provider.stop_session(sess_id).await {
                        tracing::warn!(error = %e, "avatar session cleanup failed");
                    }
                }
            }

            // ===== AFTER AGENT CALLBACKS =====
            for callback in after_callbacks.as_ref() {
                match callback(ctx.clone() as Arc<dyn CallbackContext>).await {
                    Ok(Some(content)) => {
                        let mut after_event = Event::new(&invocation_id);
                        after_event.author = agent_name.clone();
                        after_event.llm_response.content = Some(content);
                        yield Ok(after_event);
                        break;
                    }
                    Ok(None) => continue,
                    Err(e) => {
                        yield Err(e);
                        return;
                    }
                }
            }
        };

        Ok(Box::pin(s))
    }
}

/// Tool context for realtime agent tool execution.
/// Runs one tool through its before- and after-tool callbacks.
///
/// The callback contract matches the standard agent loop, which the realtime path did not
/// honour: a before-callback returning `Ok(Some(content))` substitutes a result and the tool
/// does **not** run, `Ok(None)` allows it, and an error refuses it and skips the after
/// callbacks. Previously the loop evaluated `(error_result, EventActions::default())` as a
/// discarded expression statement and fell through to `tool.execute`, so a gate could neither
/// deny nor substitute — it reported a decision while the tool ran regardless. After-callback
/// results were dropped by `let _ =`.
async fn execute_tool_with_callbacks(
    tool: &dyn Tool,
    tool_ctx: Arc<dyn ToolContext>,
    cb_ctx: Arc<dyn CallbackContext>,
    args: serde_json::Value,
    before_tool_callbacks: &[BeforeToolCallback],
    after_tool_callbacks: &[AfterToolCallback],
) -> serde_json::Value {
    let mut short_circuit: Option<serde_json::Value> = None;
    let mut run_after_tool_callbacks = true;

    for callback in before_tool_callbacks {
        match callback(cb_ctx.clone()).await {
            Ok(Some(content)) => {
                short_circuit = Some(content_to_tool_result(&content));
                break;
            }
            Ok(None) => continue,
            Err(e) => {
                short_circuit = Some(serde_json::json!({ "error": e.to_string() }));
                run_after_tool_callbacks = false;
                break;
            }
        }
    }

    let mut result = match short_circuit {
        Some(result) => result,
        None => match tool.execute(tool_ctx, args).await {
            Ok(value) => value,
            Err(e) => serde_json::json!({ "error": e.to_string() }),
        },
    };

    if run_after_tool_callbacks {
        for callback in after_tool_callbacks {
            match callback(cb_ctx.clone()).await {
                Ok(Some(modified)) => {
                    result = content_to_tool_result(&modified);
                    break;
                }
                Ok(None) => continue,
                Err(e) => {
                    result = serde_json::json!({ "error": e.to_string() });
                    break;
                }
            }
        }
    }

    result
}

/// Turns a callback's substitute `Content` into the result sent back to the provider.
///
/// A callback returns `Content` because that is what the standard agent loop puts on the
/// event stream. The realtime transport wants a JSON tool result, so a function response is
/// unwrapped to its payload and anything else is carried as its text.
fn content_to_tool_result(content: &Content) -> serde_json::Value {
    for part in &content.parts {
        if let Part::FunctionResponse { function_response, .. } = part {
            return function_response.response.clone();
        }
    }

    let text: String = content.parts.iter().filter_map(|part| part.text()).collect();
    serde_json::json!({ "result": text })
}

struct RealtimeToolContext {
    parent_ctx: Arc<dyn InvocationContext>,
    function_call_id: String,
    actions: Mutex<EventActions>,
}

impl RealtimeToolContext {
    fn new(parent_ctx: Arc<dyn InvocationContext>, function_call_id: String) -> Self {
        Self { parent_ctx, function_call_id, actions: Mutex::new(EventActions::default()) }
    }
}

#[async_trait]
impl ReadonlyContext for RealtimeToolContext {
    fn invocation_id(&self) -> &str {
        self.parent_ctx.invocation_id()
    }

    fn agent_name(&self) -> &str {
        self.parent_ctx.agent_name()
    }

    fn user_id(&self) -> &str {
        self.parent_ctx.user_id()
    }

    fn app_name(&self) -> &str {
        self.parent_ctx.app_name()
    }

    fn session_id(&self) -> &str {
        self.parent_ctx.session_id()
    }

    fn branch(&self) -> &str {
        self.parent_ctx.branch()
    }

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

#[async_trait]
impl CallbackContext for RealtimeToolContext {
    fn artifacts(&self) -> Option<Arc<dyn adk_core::Artifacts>> {
        self.parent_ctx.artifacts()
    }

    /// Shared state from the parent context, so realtime tools coordinate with the rest of
    /// the run rather than seeing `None`.
    fn shared_state(&self) -> Option<Arc<adk_core::SharedState>> {
        self.parent_ctx.shared_state()
    }
}

#[async_trait]
impl ToolContext for RealtimeToolContext {
    fn function_call_id(&self) -> &str {
        &self.function_call_id
    }

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

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

    async fn search_memory(&self, query: &str) -> Result<Vec<MemoryEntry>> {
        if let Some(memory) = self.parent_ctx.memory() {
            memory.search(query).await
        } else {
            Ok(vec![])
        }
    }

    /// The caller's scopes, from the parent context.
    ///
    /// Without this the trait default returned an empty list, so a scope-checking tool saw an
    /// unauthenticated caller in realtime and behaved differently than in the standard loop.
    fn user_scopes(&self) -> Vec<String> {
        self.parent_ctx.user_scopes()
    }

    /// Secrets resolved through the parent context.
    ///
    /// The trait default returns `None`, which a tool cannot distinguish from a secret that is
    /// genuinely absent.
    async fn get_secret(&self, name: &str) -> Result<Option<String>> {
        self.parent_ctx.get_secret(name).await
    }
}

#[cfg(test)]
mod tool_safety_tests {
    //! A before-tool callback must be able to stop a tool, and a realtime tool must see the
    //! same capabilities it sees in the standard loop.
    //!
    //! The dispatch loop built `(error_result, EventActions::default())` as a discarded
    //! expression statement and then fell through to `tool.execute`, so a denying callback
    //! reported a decision that had no effect. After-callback results were dropped with
    //! `let _ =`. `RealtimeToolContext` implemented only the required methods, inheriting
    //! `user_scopes() -> vec![]`, `get_secret() -> None`, and `shared_state() -> None`, so a
    //! scope- or secret-checking tool behaved differently in realtime than under a Runner.

    use super::*;
    use adk_core::{RunConfig, SharedState, State};
    use std::collections::HashMap;
    use std::sync::atomic::{AtomicUsize, Ordering};

    /// Counts how many times it is executed.
    struct CountingTool {
        executions: Arc<AtomicUsize>,
    }

    #[async_trait]
    impl Tool for CountingTool {
        fn name(&self) -> &str {
            "counting"
        }
        fn description(&self) -> &str {
            "counts executions"
        }
        async fn execute(
            &self,
            _ctx: Arc<dyn ToolContext>,
            _args: serde_json::Value,
        ) -> Result<serde_json::Value> {
            self.executions.fetch_add(1, Ordering::SeqCst);
            Ok(serde_json::json!({ "ran": true }))
        }
    }

    /// The minimum context the callback path needs.
    struct TestToolContext {
        actions: Mutex<EventActions>,
        content: Content,
    }

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

    #[async_trait]
    impl ReadonlyContext for TestToolContext {
        fn invocation_id(&self) -> &str {
            "inv"
        }
        fn agent_name(&self) -> &str {
            "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 CallbackContext for TestToolContext {
        fn artifacts(&self) -> Option<Arc<dyn adk_core::Artifacts>> {
            None
        }
    }

    #[async_trait]
    impl ToolContext for TestToolContext {
        fn function_call_id(&self) -> &str {
            "call-1"
        }
        fn actions(&self) -> EventActions {
            self.actions.lock().unwrap().clone()
        }
        fn set_actions(&self, actions: EventActions) {
            *self.actions.lock().unwrap() = actions;
        }
        async fn search_memory(&self, _query: &str) -> Result<Vec<MemoryEntry>> {
            Ok(vec![])
        }
    }

    /// Runs the tool through the callback gate with the supplied callbacks.
    async fn dispatch(
        before: Vec<BeforeToolCallback>,
        after: Vec<AfterToolCallback>,
        executions: Arc<AtomicUsize>,
    ) -> serde_json::Value {
        let tool = CountingTool { executions };
        let ctx = Arc::new(TestToolContext::new());
        execute_tool_with_callbacks(
            &tool,
            ctx.clone() as Arc<dyn ToolContext>,
            ctx as Arc<dyn CallbackContext>,
            serde_json::json!({}),
            &before,
            &after,
        )
        .await
    }

    #[tokio::test]
    async fn a_before_callback_error_prevents_execution() {
        let executions = Arc::new(AtomicUsize::new(0));
        let before: Vec<BeforeToolCallback> =
            vec![Box::new(|_ctx| Box::pin(async { Err(AdkError::tool("denied by policy")) }))];

        let result = dispatch(before, vec![], Arc::clone(&executions)).await;

        assert_eq!(executions.load(Ordering::SeqCst), 0, "a refused tool must not run: {result}");
        assert!(
            result["error"].as_str().unwrap_or_default().contains("denied by policy"),
            "the refusal reason must reach the provider: {result}"
        );
    }

    #[tokio::test]
    async fn a_before_callback_substitution_prevents_execution() {
        let executions = Arc::new(AtomicUsize::new(0));
        let before: Vec<BeforeToolCallback> = vec![Box::new(|_ctx| {
            Box::pin(async {
                Ok(Some(Content {
                    role: "function".to_string(),
                    parts: vec![Part::FunctionResponse {
                        function_response: adk_core::FunctionResponseData::new(
                            "counting",
                            serde_json::json!({ "cached": true }),
                        ),
                        id: None,
                        annotations: None,
                    }],
                }))
            })
        })];

        let result = dispatch(before, vec![], Arc::clone(&executions)).await;

        assert_eq!(
            executions.load(Ordering::SeqCst),
            0,
            "a substituted result must not run the tool"
        );
        assert_eq!(result, serde_json::json!({ "cached": true }));
    }

    #[tokio::test]
    async fn a_permitting_callback_lets_the_tool_run() {
        let executions = Arc::new(AtomicUsize::new(0));
        let before: Vec<BeforeToolCallback> = vec![Box::new(|_ctx| Box::pin(async { Ok(None) }))];

        let result = dispatch(before, vec![], Arc::clone(&executions)).await;

        assert_eq!(executions.load(Ordering::SeqCst), 1);
        assert_eq!(result, serde_json::json!({ "ran": true }));
    }

    #[tokio::test]
    async fn an_after_callback_error_becomes_the_result() {
        let executions = Arc::new(AtomicUsize::new(0));
        let after: Vec<AfterToolCallback> =
            vec![Box::new(|_ctx| Box::pin(async { Err(AdkError::tool("post-check failed")) }))];

        let result = dispatch(vec![], after, Arc::clone(&executions)).await;

        assert_eq!(executions.load(Ordering::SeqCst), 1, "the tool ran, as it should have");
        assert!(
            result["error"].as_str().unwrap_or_default().contains("post-check failed"),
            "an after-callback failure must not be dropped: {result}"
        );
    }

    #[tokio::test]
    async fn after_callbacks_are_skipped_when_a_before_callback_refuses() {
        let executions = Arc::new(AtomicUsize::new(0));
        let after_ran = Arc::new(AtomicUsize::new(0));
        let counter = Arc::clone(&after_ran);

        let before: Vec<BeforeToolCallback> =
            vec![Box::new(|_ctx| Box::pin(async { Err(AdkError::tool("refused")) }))];
        let after: Vec<AfterToolCallback> = vec![Box::new(move |_ctx| {
            let counter = Arc::clone(&counter);
            Box::pin(async move {
                counter.fetch_add(1, Ordering::SeqCst);
                Ok(None)
            })
        })];

        let result = dispatch(before, after, Arc::clone(&executions)).await;

        assert_eq!(executions.load(Ordering::SeqCst), 0);
        assert_eq!(after_ran.load(Ordering::SeqCst), 0, "matching the standard loop's ordering");
        assert!(result["error"].as_str().unwrap_or_default().contains("refused"), "{result}");
    }

    // ── Context capabilities ──────────────────────────────────────────

    struct TestState;
    impl State for TestState {
        fn get(&self, _key: &str) -> Option<serde_json::Value> {
            None
        }
        fn set(&mut self, _key: String, _value: serde_json::Value) {}
        fn all(&self) -> HashMap<String, serde_json::Value> {
            HashMap::new()
        }
    }

    struct TestSession;
    impl adk_core::Session for TestSession {
        fn id(&self) -> &str {
            "session"
        }
        fn app_name(&self) -> &str {
            "app"
        }
        fn user_id(&self) -> &str {
            "user"
        }
        fn state(&self) -> &dyn State {
            &TestState
        }
        fn conversation_history(&self) -> Vec<Content> {
            Vec::new()
        }
    }

    /// A parent context carrying the capabilities a tool should still see in realtime.
    struct CapableParent {
        content: Content,
        config: RunConfig,
        session: TestSession,
        shared: Arc<SharedState>,
    }

    #[async_trait]
    impl ReadonlyContext for CapableParent {
        fn invocation_id(&self) -> &str {
            "inv"
        }
        fn agent_name(&self) -> &str {
            "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 CallbackContext for CapableParent {
        fn artifacts(&self) -> Option<Arc<dyn adk_core::Artifacts>> {
            None
        }

        fn shared_state(&self) -> Option<Arc<SharedState>> {
            Some(Arc::clone(&self.shared))
        }
    }

    #[async_trait]
    impl InvocationContext for CapableParent {
        fn agent(&self) -> Arc<dyn Agent> {
            unreachable!("not used by these tests")
        }
        fn memory(&self) -> Option<Arc<dyn adk_core::Memory>> {
            None
        }
        fn session(&self) -> &dyn adk_core::Session {
            &self.session
        }
        fn run_config(&self) -> &RunConfig {
            &self.config
        }
        fn end_invocation(&self) {}
        fn ended(&self) -> bool {
            false
        }
        fn user_scopes(&self) -> Vec<String> {
            vec!["repo:write".to_string()]
        }
        async fn get_secret(&self, name: &str) -> Result<Option<String>> {
            Ok(Some(format!("secret-for-{name}")))
        }
    }

    #[tokio::test]
    async fn the_realtime_tool_context_preserves_parent_capabilities() {
        let parent = Arc::new(CapableParent {
            content: Content::new("user"),
            config: RunConfig::default(),
            session: TestSession,
            shared: Arc::new(SharedState::new()),
        }) as Arc<dyn InvocationContext>;

        let ctx = RealtimeToolContext::new(parent, "call-1".to_string());

        assert_eq!(
            ctx.user_scopes(),
            vec!["repo:write".to_string()],
            "an empty scope list makes an authenticated caller look anonymous"
        );
        assert_eq!(ctx.get_secret("api_key").await.unwrap().as_deref(), Some("secret-for-api_key"));
        assert!(ctx.shared_state().is_some(), "shared state must reach realtime tools");
        assert_eq!(ctx.app_name(), "app", "identity still delegates");
    }
}