rs-adk 0.5.0

Agent runtime for Gemini Live — tools, streaming, agent transfer, middleware
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
//! Integration tests for rs-adk.
//!
//! These tests exercise multiple components working together through the
//! public API only.  They use mock SessionWriters so no real WebSocket or
//! Gemini Live connection is required.

use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::Arc;
use std::time::Duration;

use async_trait::async_trait;
use serde_json::json;
use tokio::sync::broadcast;

use rs_genai::prelude::{Content, FunctionCall, FunctionResponse};
use rs_genai::session::{SessionError, SessionEvent, SessionWriter};

use rs_adk::agent::Agent;
use rs_adk::agent_session::AgentSession;
use rs_adk::agent_tool::AgentTool;
use rs_adk::context::{AgentEvent, InvocationContext};
use rs_adk::error::AgentError;
use rs_adk::middleware::{Middleware, MiddlewareChain};
use rs_adk::runner::Runner;
use rs_adk::tool::{SimpleTool, ToolFunction};
use rs_adk::LlmAgent;

// ---------------------------------------------------------------------------
// Test helpers
// ---------------------------------------------------------------------------

/// A SessionWriter that accepts all commands without error.
struct MockWriter;

#[async_trait]
impl SessionWriter for MockWriter {
    async fn send_audio(&self, _data: Vec<u8>) -> Result<(), SessionError> {
        Ok(())
    }
    async fn send_text(&self, _text: String) -> Result<(), SessionError> {
        Ok(())
    }
    async fn send_tool_response(
        &self,
        _responses: Vec<FunctionResponse>,
    ) -> Result<(), SessionError> {
        Ok(())
    }
    async fn send_client_content(
        &self,
        _turns: Vec<Content>,
        _turn_complete: bool,
    ) -> Result<(), SessionError> {
        Ok(())
    }
    async fn send_video(&self, _jpeg_data: Vec<u8>) -> Result<(), SessionError> {
        Ok(())
    }
    async fn update_instruction(&self, _instruction: String) -> Result<(), SessionError> {
        Ok(())
    }
    async fn signal_activity_start(&self) -> Result<(), SessionError> {
        Ok(())
    }
    async fn signal_activity_end(&self) -> Result<(), SessionError> {
        Ok(())
    }
    async fn disconnect(&self) -> Result<(), SessionError> {
        Ok(())
    }
}

/// Create a mock AgentSession backed by MockWriter, returning the session
/// and the event sender so tests can inject SessionEvents.
fn mock_agent_session() -> (AgentSession, broadcast::Sender<SessionEvent>) {
    let (evt_tx, _) = broadcast::channel(64);
    let writer: Arc<dyn SessionWriter> = Arc::new(MockWriter);
    let session = AgentSession::from_writer(writer, evt_tx.clone());
    (session, evt_tx)
}

/// A no-op agent that returns immediately.
struct NoopAgent {
    name: String,
}

#[async_trait]
impl Agent for NoopAgent {
    fn name(&self) -> &str {
        &self.name
    }
    async fn run_live(&self, _ctx: &mut InvocationContext) -> Result<(), AgentError> {
        Ok(())
    }
}

/// An agent that transfers to another named agent.
struct TransferAgent {
    name: String,
    target: String,
}

#[async_trait]
impl Agent for TransferAgent {
    fn name(&self) -> &str {
        &self.name
    }
    async fn run_live(&self, _ctx: &mut InvocationContext) -> Result<(), AgentError> {
        Err(AgentError::TransferRequested(self.target.clone()))
    }
}

// ---------------------------------------------------------------------------
// (a) full_tool_call_roundtrip
// ---------------------------------------------------------------------------

#[tokio::test]
async fn full_tool_call_roundtrip() {
    // Build an LlmAgent with a SimpleTool.
    let tool = SimpleTool::new("get_weather", "Get weather info", None, |_args| async {
        Ok(json!({"temp": 22, "unit": "celsius"}))
    });
    let agent = LlmAgent::builder("weather_agent").tool(tool).build();

    // Create a mock session and context.
    let (session, evt_tx) = mock_agent_session();
    let mut ctx = InvocationContext::new(session);

    // Subscribe to AgentEvents BEFORE running.
    let mut agent_events = ctx.subscribe();

    // Inject a ToolCall event followed by TurnComplete from a background task.
    tokio::spawn(async move {
        tokio::time::sleep(Duration::from_millis(10)).await;
        let _ = evt_tx.send(SessionEvent::ToolCall(vec![FunctionCall {
            name: "get_weather".to_string(),
            args: json!({"city": "London"}),
            id: Some("call-1".to_string()),
        }]));
        // Give the agent time to dispatch the tool and send the response.
        tokio::time::sleep(Duration::from_millis(50)).await;
        let _ = evt_tx.send(SessionEvent::TurnComplete);
    });

    // Run the agent — it should process the tool call and terminate on TurnComplete.
    let result = agent.run_live(&mut ctx).await;
    assert!(result.is_ok(), "agent should complete without error");

    // Verify we got ToolCallStarted and ToolCallCompleted events.
    let mut saw_tool_started = false;
    let mut saw_tool_completed = false;
    let mut completed_result = None;

    while let Ok(event) = agent_events.try_recv() {
        match event {
            AgentEvent::ToolCallStarted { name, .. } if name == "get_weather" => {
                saw_tool_started = true;
            }
            AgentEvent::ToolCallCompleted {
                name,
                result,
                duration,
                ..
            } if name == "get_weather" => {
                saw_tool_completed = true;
                completed_result = Some(result);
                assert!(duration.as_millis() < 5000, "tool should complete quickly");
            }
            _ => {}
        }
    }

    assert!(saw_tool_started, "should have emitted ToolCallStarted");
    assert!(saw_tool_completed, "should have emitted ToolCallCompleted");

    // Verify the tool result content.
    let result_val = completed_result.expect("should have a result");
    assert_eq!(result_val["temp"], 22);
    assert_eq!(result_val["unit"], "celsius");
}

// ---------------------------------------------------------------------------
// (b) agent_tool_in_llm_agent
// ---------------------------------------------------------------------------

#[tokio::test]
async fn agent_tool_in_llm_agent() {
    // Create a simple inner agent.
    let inner_agent = NoopAgent {
        name: "summarizer".to_string(),
    };

    // Wrap it as an AgentTool.
    let agent_tool = AgentTool::new(inner_agent);

    // Register it in an LlmAgent via the .tool() builder method.
    let agent = LlmAgent::builder("orchestrator").tool(agent_tool).build();

    // Verify the AgentTool appears in tool declarations.
    let tools = agent.tools();
    assert!(!tools.is_empty(), "should have tool declarations");

    // The dispatcher should have the "summarizer" tool registered.
    assert!(
        agent.dispatcher().classify("summarizer").is_some(),
        "should have 'summarizer' tool registered via AgentTool"
    );
}

// ---------------------------------------------------------------------------
// (c) runner_transfer_cycle
// ---------------------------------------------------------------------------

#[tokio::test]
async fn runner_transfer_cycle() {
    // Root agent transfers to "sub_agent".
    let root = TransferAgent {
        name: "root".to_string(),
        target: "sub_agent".to_string(),
    };

    let sub = NoopAgent {
        name: "sub_agent".to_string(),
    };

    let mut runner = Runner::new(root);
    runner.register(Arc::new(sub));

    let connect_count = Arc::new(AtomicU32::new(0));
    let count = connect_count.clone();

    let result = runner
        .run(move |_agent| {
            let c = count.clone();
            async move {
                c.fetch_add(1, Ordering::SeqCst);
                let (session, _evt_tx) = mock_agent_session();
                Ok(session)
            }
        })
        .await;

    assert!(result.is_ok(), "runner should complete successfully");
    // Should have connected twice: once for root, once for sub_agent.
    assert_eq!(
        connect_count.load(Ordering::SeqCst),
        2,
        "runner should connect once per agent (root + sub_agent)"
    );
}

// ---------------------------------------------------------------------------
// (d) middleware_chain_called_in_order
// ---------------------------------------------------------------------------

/// Middleware that increments a shared counter in `before_agent`.
struct CountingMiddleware {
    label: String,
    order_log: Arc<parking_lot::Mutex<Vec<String>>>,
}

#[async_trait]
impl Middleware for CountingMiddleware {
    fn name(&self) -> &str {
        &self.label
    }
    async fn before_agent(&self, _ctx: &InvocationContext) -> Result<(), AgentError> {
        self.order_log.lock().push(self.label.clone());
        Ok(())
    }
}

#[tokio::test]
async fn middleware_chain_called_in_order() {
    let order_log = Arc::new(parking_lot::Mutex::new(Vec::<String>::new()));

    // Build a simple agent (no middleware on the agent itself).
    let agent = LlmAgent::builder("mw_test").build();

    // Build a MiddlewareChain with two counting middleware and pass it
    // to the InvocationContext.  (This mirrors what Runner does: middleware
    // is applied at the context level, not baked into the agent.)
    let mut chain = MiddlewareChain::new();
    chain.add(Arc::new(CountingMiddleware {
        label: "first".to_string(),
        order_log: order_log.clone(),
    }));
    chain.add(Arc::new(CountingMiddleware {
        label: "second".to_string(),
        order_log: order_log.clone(),
    }));

    let (session, evt_tx) = mock_agent_session();
    let mut ctx = InvocationContext::with_middleware(session, chain);

    // End the turn immediately so the agent completes.
    tokio::spawn(async move {
        tokio::time::sleep(Duration::from_millis(10)).await;
        let _ = evt_tx.send(SessionEvent::TurnComplete);
    });

    agent.run_live(&mut ctx).await.unwrap();

    // The middleware chain should have been called: first, then second.
    let log = order_log.lock().clone();
    assert!(
        log.len() >= 2,
        "both counting middleware should have been called, got {:?}",
        log
    );
    let first_idx = log.iter().position(|s| s == "first").unwrap();
    let second_idx = log.iter().position(|s| s == "second").unwrap();
    assert!(
        first_idx < second_idx,
        "'first' middleware should run before 'second'"
    );
}

// ---------------------------------------------------------------------------
// (e) events_emitted_in_order
// ---------------------------------------------------------------------------

#[tokio::test]
async fn events_emitted_in_order() {
    let agent = LlmAgent::builder("ordered_agent").build();

    let (session, evt_tx) = mock_agent_session();
    let mut ctx = InvocationContext::new(session);
    let mut agent_events = ctx.subscribe();

    // Send TurnComplete after a short delay.
    tokio::spawn(async move {
        tokio::time::sleep(Duration::from_millis(10)).await;
        let _ = evt_tx.send(SessionEvent::TurnComplete);
    });

    agent.run_live(&mut ctx).await.unwrap();

    // Collect all events.
    let mut events = Vec::new();
    while let Ok(event) = agent_events.try_recv() {
        events.push(event);
    }

    assert!(
        events.len() >= 3,
        "should have at least AgentStarted, TurnComplete, AgentCompleted; got {} events",
        events.len()
    );

    // Verify ordering: AgentStarted must come first.
    assert!(
        matches!(&events[0], AgentEvent::AgentStarted { name } if name == "ordered_agent"),
        "first event should be AgentStarted, got: {:?}",
        events[0]
    );

    // TurnComplete should come before AgentCompleted.
    let turn_idx = events
        .iter()
        .position(|e| matches!(e, AgentEvent::Session(SessionEvent::TurnComplete)))
        .expect("should have TurnComplete event");

    let completed_idx = events
        .iter()
        .position(|e| matches!(e, AgentEvent::AgentCompleted { .. }))
        .expect("should have AgentCompleted event");

    assert!(
        turn_idx < completed_idx,
        "TurnComplete (idx={}) should come before AgentCompleted (idx={})",
        turn_idx,
        completed_idx
    );

    // AgentCompleted should be the last event.
    assert!(
        matches!(events.last().unwrap(), AgentEvent::AgentCompleted { name } if name == "ordered_agent"),
        "last event should be AgentCompleted, got: {:?}",
        events.last().unwrap()
    );
}

// ---------------------------------------------------------------------------
// (f) state_preserved_across_agent_tool
// ---------------------------------------------------------------------------

/// An agent that reads state["parent_data"] and emits it as text.
struct StateReadingAgent;

#[async_trait]
impl Agent for StateReadingAgent {
    fn name(&self) -> &str {
        "state_reader"
    }
    async fn run_live(&self, ctx: &mut InvocationContext) -> Result<(), AgentError> {
        // Read the request from state (injected by AgentTool).
        let request = ctx
            .state()
            .get::<String>("request_text")
            .unwrap_or_else(|| "none".to_string());

        // Emit the text back as output.
        ctx.emit(AgentEvent::Session(SessionEvent::TextDelta(format!(
            "got: {}",
            request
        ))));
        Ok(())
    }
}

#[tokio::test]
async fn state_preserved_across_agent_tool() {
    // The AgentTool wraps an agent that reads state injected via args.
    let agent_tool = AgentTool::new(StateReadingAgent);

    // Call the tool with a request containing data the parent would set.
    let result = agent_tool
        .call(json!({"request": "parent_value_42"}))
        .await
        .unwrap();

    // The wrapped agent should have read the injected state and echoed it.
    assert_eq!(
        result["result"], "got: parent_value_42",
        "AgentTool should inject args into state and the wrapped agent should read them"
    );
}