adk-model 3.0.0

LLM model integrations for Rust Agent Development Kit (ADK-Rust) (Gemini, OpenAI, Claude, DeepSeek, etc.)
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
//! A scripted [`Llm`](adk_core::Llm) for tests: one scripted reply per call, with every request recorded.

use std::collections::VecDeque;
use std::sync::Mutex;

use adk_core::{
    AdkError, Content, ErrorCategory, ErrorComponent, FinishReason, Llm, LlmRequest, LlmResponse,
    LlmResponseStream, NativeWebSearch, Part, Result, UsageMetadata,
};
use async_trait::async_trait;
use serde_json::Value;

/// The error code [`ScriptedLlm`] fails a call with once its script is used up.
pub const SCRIPT_EXHAUSTED_CODE: &str = "model.scripted.exhausted";

/// What a [`ScriptedLlm`] answers to one `generate_content` call.
///
/// A reply is a stream of responses, optionally followed by a stream error, or a call that
/// fails outright. Build one with [`text`](Self::text), [`function_call`](Self::function_call),
/// [`stream`](Self::stream) or [`error`](Self::error); an [`LlmResponse`] converts into a
/// one-response reply.
///
/// # Example
///
/// ```rust
/// use adk_core::{AdkError, UsageMetadata};
/// use adk_model::ScriptedResponse;
/// use serde_json::json;
///
/// let call = ScriptedResponse::function_call("get_weather", json!({ "city": "Nairobi" }));
/// let answer = ScriptedResponse::text("It is sunny.")
///     .with_usage(UsageMetadata { prompt_token_count: 12, ..Default::default() });
/// let cut = ScriptedResponse::text("It is").then_error(AdkError::model("stream reset"));
/// let refused = ScriptedResponse::error(AdkError::model("rate limited"));
/// # let _ = (call, answer, cut, refused);
/// ```
#[derive(Debug)]
pub struct ScriptedResponse {
    responses: Vec<LlmResponse>,
    stream_error: Option<AdkError>,
    call_error: Option<AdkError>,
}

impl ScriptedResponse {
    /// A final model response with one text part.
    pub fn text(text: impl Into<String>) -> Self {
        Self::response(final_response(vec![Part::Text { text: text.into() }]))
    }

    /// A final model response with one function call.
    ///
    /// The call has no ID; [`ScriptedLlm`] assigns one when it replies.
    pub fn function_call(name: impl Into<String>, args: Value) -> Self {
        Self::function_calls([(name, args)])
    }

    /// A final model response with several function calls, in order.
    pub fn function_calls<N: Into<String>>(calls: impl IntoIterator<Item = (N, Value)>) -> Self {
        let parts = calls
            .into_iter()
            .map(|(name, args)| Part::FunctionCall {
                name: name.into(),
                args,
                id: None,
                thought_signature: None,
            })
            .collect();
        Self::response(final_response(parts))
    }

    /// A reply of exactly `response`.
    pub fn response(response: LlmResponse) -> Self {
        Self::stream([response])
    }

    /// A reply that streams `responses` in order, as given.
    pub fn stream(responses: impl IntoIterator<Item = LlmResponse>) -> Self {
        Self { responses: responses.into_iter().collect(), stream_error: None, call_error: None }
    }

    /// A call that fails: `generate_content` returns `error` instead of a stream.
    pub fn error(error: AdkError) -> Self {
        Self { responses: Vec::new(), stream_error: None, call_error: Some(error) }
    }

    /// Ends the stream with `error` after its responses, as a provider whose stream breaks.
    pub fn then_error(mut self, error: AdkError) -> Self {
        self.stream_error = Some(error);
        self
    }

    /// Reports `usage` on the reply's last response.
    pub fn with_usage(mut self, usage: UsageMetadata) -> Self {
        if let Some(last) = self.responses.last_mut() {
            last.usage_metadata = Some(usage);
        }
        self
    }
}

impl From<LlmResponse> for ScriptedResponse {
    fn from(response: LlmResponse) -> Self {
        Self::response(response)
    }
}

impl From<AdkError> for ScriptedResponse {
    fn from(error: AdkError) -> Self {
        Self::error(error)
    }
}

fn final_response(parts: Vec<Part>) -> LlmResponse {
    let mut response = LlmResponse::new(Content { role: "model".to_string(), parts });
    response.finish_reason = Some(FinishReason::Stop);
    response.turn_complete = true;
    response
}

/// A test double for [`Llm`] that answers each call with the next scripted reply and records
/// every request.
///
/// Script one [`ScriptedResponse`] per model call: a tool loop that calls one tool and then
/// answers is two replies, a function call and a text. [`requests`](Self::requests) returns
/// what the agent sent on each call, so a test can assert on the history, tool declarations,
/// and configuration the model received. A call past the end of the script fails with
/// [`SCRIPT_EXHAUSTED_CODE`], so a test that runs more model turns than it expects fails
/// instead of passing on a made-up reply.
///
/// A function call scripted without an ID is given `scripted_tc_{call}_{index}`, where `call`
/// counts `generate_content` calls from 0 and `index` counts the reply's function calls.
///
/// [`MockLlm`](crate::MockLlm) replays its whole response list on every call; use
/// `ScriptedLlm` to script more than one turn.
///
/// # Example
///
/// ```rust
/// use adk_core::Llm;
/// use adk_model::{ScriptedLlm, ScriptedResponse};
/// use futures::StreamExt;
/// use serde_json::json;
///
/// # #[tokio::main(flavor = "current_thread")]
/// # async fn main() -> Result<(), adk_core::AdkError> {
/// let llm = ScriptedLlm::new(
///     "scripted",
///     [
///         ScriptedResponse::function_call("get_weather", json!({ "city": "Nairobi" })),
///         ScriptedResponse::text("It is sunny in Nairobi."),
///     ],
/// );
///
/// let request = adk_core::LlmRequest::new("scripted", vec![]);
/// let first = llm.generate_content(request, false).await?.next().await.unwrap()?;
/// assert!(first.content.unwrap().has_function_calls());
/// assert_eq!(llm.requests().len(), 1);
/// assert_eq!(llm.remaining(), 1);
/// # Ok(())
/// # }
/// ```
pub struct ScriptedLlm {
    name: String,
    script: Mutex<VecDeque<ScriptedResponse>>,
    requests: Mutex<Vec<LlmRequest>>,
    native_web_search: Option<NativeWebSearch>,
    data_locality: adk_core::DataLocality,
    data_destinations: Vec<adk_core::DataDestination>,
}

impl ScriptedLlm {
    /// Creates a model named `name` that answers its calls with `script`, in order.
    ///
    /// Each item is anything that converts into a [`ScriptedResponse`], such as an
    /// [`LlmResponse`].
    pub fn new<R: Into<ScriptedResponse>>(
        name: impl Into<String>,
        script: impl IntoIterator<Item = R>,
    ) -> Self {
        Self {
            name: name.into(),
            script: Mutex::new(script.into_iter().map(Into::into).collect()),
            requests: Mutex::new(Vec::new()),
            native_web_search: None,
            data_locality: adk_core::DataLocality::Remote,
            data_destinations: Vec::new(),
        }
    }

    /// Reports `search` from [`Llm::native_web_search`], so a test stands in for a client
    /// whose transport serves that provider's built-in search (spec 3.1 §7.20).
    ///
    /// # Example
    ///
    /// ```rust
    /// use adk_core::{Llm, NativeWebSearch, NativeWebSearchTool};
    /// use adk_model::{ScriptedLlm, ScriptedResponse};
    ///
    /// let search = NativeWebSearch::new("gemini", NativeWebSearchTool::GoogleSearch, false);
    /// let llm = ScriptedLlm::new("gemini-3.5-flash-lite", Vec::<ScriptedResponse>::new())
    ///     .with_native_web_search(search);
    /// assert_eq!(llm.native_web_search(), Some(search));
    /// ```
    pub fn with_native_web_search(mut self, search: NativeWebSearch) -> Self {
        self.native_web_search = Some(search);
        self
    }

    /// Reports `locality` from [`Llm::data_locality`], so a test stands in for a local or
    /// loopback model. The default is [`DataLocality::Remote`](adk_core::DataLocality::Remote),
    /// as for any hosted provider.
    ///
    /// # Example
    ///
    /// ```rust
    /// use adk_core::{DataLocality, Llm};
    /// use adk_model::{ScriptedLlm, ScriptedResponse};
    ///
    /// let remote = ScriptedLlm::new("claude-sonnet-5-5", Vec::<ScriptedResponse>::new());
    /// assert_eq!(remote.data_locality(), DataLocality::Remote);
    /// let local = ScriptedLlm::new("qwen3-vl-8b", Vec::<ScriptedResponse>::new())
    ///     .with_data_locality(DataLocality::InProcess);
    /// assert_eq!(local.data_locality(), DataLocality::InProcess);
    /// ```
    pub fn with_data_locality(mut self, locality: adk_core::DataLocality) -> Self {
        self.data_locality = locality;
        self
    }

    /// Reports `destination` from [`Llm::data_destinations`], so a test stands in for a
    /// hosted provider's client. None by default.
    ///
    /// # Example
    ///
    /// ```rust
    /// use adk_core::{DataDestination, Llm};
    /// use adk_model::{ScriptedLlm, ScriptedResponse};
    ///
    /// let claude = ScriptedLlm::new("claude-sonnet-5-5", Vec::<ScriptedResponse>::new())
    ///     .with_data_destination(DataDestination::new("anthropic", ["api.anthropic.com"]));
    /// assert_eq!(claude.data_destinations()[0].hosts, ["api.anthropic.com"]);
    /// ```
    pub fn with_data_destination(mut self, destination: adk_core::DataDestination) -> Self {
        self.data_destinations.push(destination);
        self
    }

    /// Appends a reply to the end of the script.
    pub fn push(&self, reply: impl Into<ScriptedResponse>) {
        lock(&self.script).push_back(reply.into());
    }

    /// Every request received so far, in call order.
    pub fn requests(&self) -> Vec<LlmRequest> {
        lock(&self.requests).clone()
    }

    /// The most recent request, if any.
    pub fn last_request(&self) -> Option<LlmRequest> {
        lock(&self.requests).last().cloned()
    }

    /// How many scripted replies have not been used yet.
    pub fn remaining(&self) -> usize {
        lock(&self.script).len()
    }
}

/// A poisoned lock only means another test thread panicked; the data is still usable.
fn lock<T>(mutex: &Mutex<T>) -> std::sync::MutexGuard<'_, T> {
    mutex.lock().unwrap_or_else(std::sync::PoisonError::into_inner)
}

impl std::fmt::Debug for ScriptedLlm {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ScriptedLlm")
            .field("name", &self.name)
            .field("remaining", &self.remaining())
            .field("requests", &lock(&self.requests).len())
            .finish()
    }
}

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

    fn native_web_search(&self) -> Option<NativeWebSearch> {
        self.native_web_search
    }

    fn data_locality(&self) -> adk_core::DataLocality {
        self.data_locality
    }

    fn data_destinations(&self) -> Vec<adk_core::DataDestination> {
        self.data_destinations.clone()
    }

    async fn generate_content(
        &self,
        request: LlmRequest,
        _stream: bool,
    ) -> Result<LlmResponseStream> {
        let call = {
            let mut requests = lock(&self.requests);
            requests.push(request);
            requests.len() - 1
        };
        let Some(reply) = lock(&self.script).pop_front() else {
            return Err(AdkError::new(
                ErrorComponent::Model,
                ErrorCategory::Internal,
                SCRIPT_EXHAUSTED_CODE,
                format!(
                    "ScriptedLlm '{}' has no reply scripted for call {}; script one more reply \
                     or expect fewer model calls.",
                    self.name,
                    call + 1
                ),
            ));
        };
        if let Some(error) = reply.call_error {
            return Err(error);
        }

        let mut index = 0;
        let mut items: Vec<Result<LlmResponse>> = reply
            .responses
            .into_iter()
            .map(|mut response| {
                let parts =
                    response.content.iter_mut().flat_map(|content| content.parts.iter_mut());
                for part in parts {
                    if let Part::FunctionCall { id, .. } = part {
                        if id.is_none() {
                            *id = Some(format!("scripted_tc_{call}_{index}"));
                        }
                        index += 1;
                    }
                }
                Ok(response)
            })
            .collect();
        items.extend(reply.stream_error.map(Err));
        Ok(Box::pin(futures::stream::iter(items)))
    }
}

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

    async fn call(llm: &ScriptedLlm, text: &str) -> Result<Vec<Result<LlmResponse>>> {
        let request = LlmRequest::new("m", vec![Content::new("user").with_text(text)]);
        Ok(llm.generate_content(request, true).await?.collect().await)
    }

    #[tokio::test]
    async fn each_call_gets_the_next_reply_and_is_recorded() {
        let llm = ScriptedLlm::new(
            "m",
            [
                ScriptedResponse::function_calls([("a", json!({ "x": 1 })), ("b", json!({}))]),
                ScriptedResponse::text("done"),
            ],
        );

        let first = call(&llm, "one").await.unwrap();
        assert_eq!(first.len(), 1);
        let parts = &first[0].as_ref().unwrap().content.as_ref().unwrap().parts;
        let ids: Vec<_> = parts
            .iter()
            .map(|part| match part {
                Part::FunctionCall { id, .. } => id.clone(),
                other => panic!("expected a function call, got {other:?}"),
            })
            .collect();
        assert_eq!(ids, [Some("scripted_tc_0_0".to_string()), Some("scripted_tc_0_1".to_string())]);

        let second = call(&llm, "two").await.unwrap();
        let reply = second[0].as_ref().unwrap();
        assert_eq!(reply.content.as_ref().unwrap().parts[0].text(), Some("done"));
        assert!(reply.turn_complete);
        assert_eq!(reply.finish_reason, Some(FinishReason::Stop));

        let texts: Vec<_> = llm
            .requests()
            .iter()
            .map(|request| request.contents[0].parts[0].text().unwrap().to_string())
            .collect();
        assert_eq!(texts, ["one", "two"]);
        assert_eq!(llm.remaining(), 0);
    }

    #[tokio::test]
    async fn an_exhausted_script_fails_the_call() {
        let llm = ScriptedLlm::new("m", [ScriptedResponse::text("only")]);
        call(&llm, "one").await.unwrap();
        let err = call(&llm, "two").await.unwrap_err();
        assert_eq!(err.code, SCRIPT_EXHAUSTED_CODE);
        assert!(err.message.contains("call 2"), "{}", err.message);
        assert_eq!(llm.requests().len(), 2, "the unanswered request is still recorded");
    }

    #[tokio::test]
    async fn errors_usage_and_streams_are_scripted() {
        let mut chunk = LlmResponse::new(Content::new("model").with_text("par"));
        chunk.partial = true;
        let llm = ScriptedLlm::new(
            "m",
            [
                ScriptedResponse::error(AdkError::model("rate limited")),
                ScriptedResponse::stream([
                    chunk,
                    final_response(vec![Part::Text { text: "partial".into() }]),
                ])
                .with_usage(UsageMetadata { total_token_count: 7, ..Default::default() })
                .then_error(AdkError::model("stream reset")),
            ],
        );
        assert_eq!(call(&llm, "a").await.unwrap_err().message, "rate limited");

        let items = call(&llm, "b").await.unwrap();
        assert_eq!(items.len(), 3);
        assert!(items[0].as_ref().unwrap().partial);
        assert_eq!(
            items[1].as_ref().unwrap().usage_metadata.as_ref().unwrap().total_token_count,
            7
        );
        assert_eq!(items[2].as_ref().unwrap_err().message, "stream reset");
    }

    #[tokio::test]
    async fn scripted_ids_are_kept_and_replies_can_be_pushed() {
        let mut response = final_response(vec![Part::FunctionCall {
            name: "a".into(),
            args: json!({}),
            id: Some("call_mine".into()),
            thought_signature: None,
        }]);
        response.model = Some("scripted".into());
        let llm = ScriptedLlm::new("m", Vec::<ScriptedResponse>::new());
        llm.push(response);
        let items = call(&llm, "x").await.unwrap();
        let reply = items[0].as_ref().unwrap();
        assert!(matches!(
            &reply.content.as_ref().unwrap().parts[0],
            Part::FunctionCall { id: Some(id), .. } if id == "call_mine"
        ));
        assert_eq!(llm.last_request().unwrap().contents[0].parts[0].text(), Some("x"));
    }
}