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
//! Responses API client (`POST /v1/responses`): the stateful surface.
//!
//! Unlike Chat Completions (stateless, no server memory), Responses is the only
//! endpoint carrying reasoning across turns: `previous_response_id` chains turns,
//! `store` persists them server-side, and `background` runs long generations
//! asynchronously. This is the loop the legal-agent-workshop pattern is built on.
//!
//! The workshop loop, in ADK terms: one [`MuseResponsesClient::create`] per turn
//! with the previous response id, one tool at a time, progress saved between turns.

use super::config::MuseConfig;
use adk_core::{AdkError, ErrorComponent};
use reqwest::Client;
use serde::{Deserialize, Serialize};
use serde_json::Value;

/// One Responses input item (`input_text`, `input_image`, `input_file`,
/// `function_call`, `function_call_output`, ...). Untagged JSON, API-shaped.
pub type ResponseItem = Value;

/// A Responses generation request.
#[derive(Debug, Clone, Serialize, Default)]
pub struct ResponseRequest {
    /// Model (for example `muse-spark-1.3`).
    pub model: String,
    /// Text input, or structured input items via [`ResponseRequest::with_items`].
    #[serde(skip_serializing_if = "Option::is_none")]
    pub input: Option<Value>,
    /// Per-request instructions (not persisted).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub instructions: Option<String>,
    /// Chain onto a stored response (carries reasoning across turns).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub previous_response_id: Option<String>,
    /// Persist the response server-side (default true server-side).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub store: Option<bool>,
    /// Run asynchronously (poll with [`MuseResponsesClient::get`]).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub background: Option<bool>,
    /// Sampling temperature.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub temperature: Option<f32>,
    /// Nucleus sampling threshold.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub top_p: Option<f32>,
    /// Output cap (minimum 16 server-side).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_output_tokens: Option<u32>,
    /// Function/custom tools (caller-executed) or server-side built-ins.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tools: Option<Vec<Value>>,
    /// Tool choice.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_choice: Option<Value>,
    /// Whether tools may run in parallel in one turn.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parallel_tool_calls: Option<bool>,
    /// Output format, for example strict JSON Schema via `text.format`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub text: Option<Value>,
    /// Include values, for example `reasoning.encrypted_content`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub include: Option<Vec<String>>,
}

impl ResponseRequest {
    /// A request with plain-text input.
    pub fn text(model: impl Into<String>, input: impl Into<String>) -> Self {
        Self { model: model.into(), input: Some(Value::String(input.into())), ..Default::default() }
    }

    /// Attach structured input items (images, files, function outputs).
    pub fn with_items(mut self, items: Vec<ResponseItem>) -> Self {
        self.input = Some(Value::Array(items));
        self
    }

    /// Chain onto a stored response.
    pub fn with_previous(mut self, previous_response_id: impl Into<String>) -> Self {
        self.previous_response_id = Some(previous_response_id.into());
        self
    }

    /// Constrain output to a strict JSON Schema (Responses `text.format`).
    pub fn with_json_schema(mut self, name: &str, schema: Value) -> Self {
        self.text = Some(serde_json::json!({
            "format": {"type": "json_schema", "name": name, "schema": schema, "strict": true},
        }));
        self
    }
}

/// One function call the model wants executed.
#[derive(Debug, Clone)]
pub struct FunctionCallItem {
    /// Call id to answer with `function_call_output`.
    pub call_id: String,
    /// Function name.
    pub name: String,
    /// Raw JSON arguments.
    pub arguments: String,
}

/// A Responses generation.
#[derive(Debug, Clone, Deserialize)]
pub struct ResponseObject {
    /// Response id (`resp-..`), chainable via `previous_response_id`.
    pub id: String,
    /// Lifecycle status (`completed`, `failed`, `in_progress`, ...).
    #[serde(default)]
    pub status: Option<String>,
    /// Error object when the generation failed.
    #[serde(default)]
    pub error: Option<Value>,
    /// Output items (messages, function calls, reasoning).
    #[serde(default)]
    pub output: Vec<Value>,
    /// Usage object as received.
    #[serde(default)]
    pub usage: Option<Value>,
}

impl ResponseObject {
    /// All `output_text` content, joined.
    pub fn text(&self) -> String {
        let mut texts = Vec::new();
        for item in &self.output {
            if item.get("type").and_then(Value::as_str) == Some("message")
                && let Some(content) = item.get("content").and_then(Value::as_array)
            {
                for part in content {
                    if part.get("type").and_then(Value::as_str) == Some("output_text")
                        && let Some(text) = part.get("text").and_then(Value::as_str)
                    {
                        texts.push(text);
                    }
                }
            }
        }
        texts.join("\n")
    }

    /// Function calls the caller must execute and answer.
    pub fn function_calls(&self) -> Vec<FunctionCallItem> {
        self.output
            .iter()
            .filter(|item| item.get("type").and_then(Value::as_str) == Some("function_call"))
            .filter_map(|item| {
                Some(FunctionCallItem {
                    call_id: item.get("call_id")?.as_str()?.to_string(),
                    name: item.get("name")?.as_str()?.to_string(),
                    arguments: item
                        .get("arguments")
                        .and_then(Value::as_str)
                        .unwrap_or_default()
                        .to_string(),
                })
            })
            .collect()
    }

    /// Normalized usage, when present.
    pub fn usage_metadata(&self) -> Option<adk_core::UsageMetadata> {
        use crate::usage_json::count;

        // Responses counts `input_tokens`/`output_tokens`, not Chat Completions'
        // `prompt_tokens`/`completion_tokens`.
        let usage = self.usage.as_ref().filter(|usage| usage.is_object())?;
        let input = count(usage, "/input_tokens").unwrap_or_default();
        let output = count(usage, "/output_tokens").unwrap_or_default();
        Some(adk_core::UsageMetadata {
            prompt_token_count: input,
            candidates_token_count: output,
            total_token_count: count(usage, "/total_tokens")
                .unwrap_or_else(|| input.saturating_add(output)),
            cache_read_input_token_count: count(usage, "/input_tokens_details/cached_tokens"),
            thinking_token_count: count(usage, "/output_tokens_details/reasoning_tokens"),
            provider_usage: Some(usage.clone()),
            ..Default::default()
        })
    }

    /// Whether the generation finished successfully.
    pub fn is_completed(&self) -> bool {
        self.status.as_deref() == Some("completed")
    }
}

/// Responses API client.
///
/// # Example
///
/// ```rust,ignore
/// use adk_model::muse::{MuseConfig, MuseResponsesClient, ResponseRequest};
///
/// let responses = MuseResponsesClient::new(MuseConfig::spark_1_3(api_key))?;
/// let first = responses.create(ResponseRequest::text("muse-spark-1.3", "Plan the memo.")).await?;
/// let next = responses
///     .create(ResponseRequest::text("muse-spark-1.3", "Now execute step one.").with_previous(&first.id))
///     .await?;
/// ```
pub struct MuseResponsesClient {
    client: Client,
    config: MuseConfig,
}

impl MuseResponsesClient {
    /// Create a Responses client.
    pub fn new(config: MuseConfig) -> Result<Self, AdkError> {
        let client = crate::HttpTimeouts::default()
            .apply(Client::builder())
            .build()
            .map_err(|e| AdkError::model(format!("Failed to create HTTP client: {e}")))?;
        Ok(Self { client, config })
    }

    /// Create a response (one turn).
    ///
    /// # Errors
    ///
    /// Returns the API error (HTTP status, code `model.muse.api_error`) on rejection.
    pub async fn create(&self, request: ResponseRequest) -> Result<ResponseObject, AdkError> {
        let response = self
            .client
            .post(format!("{}/responses", self.base()))
            .header("Authorization", self.auth())
            .header("Content-Type", "application/json")
            .json(&request)
            .send()
            .await
            .map_err(|e| self.transport(e))?;
        self.read_json(response).await
    }

    /// Retrieve a stored response (poll background generations here).
    ///
    /// # Errors
    ///
    /// Returns the API error on rejection.
    pub async fn get(&self, response_id: &str) -> Result<ResponseObject, AdkError> {
        let response = self
            .client
            .get(format!("{}/responses/{response_id}", self.base()))
            .header("Authorization", self.auth())
            .send()
            .await
            .map_err(|e| self.transport(e))?;
        self.read_json(response).await
    }

    /// Delete a stored response.
    ///
    /// # Errors
    ///
    /// Returns the API error on rejection.
    pub async fn delete(&self, response_id: &str) -> Result<(), AdkError> {
        let response = self
            .client
            .delete(format!("{}/responses/{response_id}", self.base()))
            .header("Authorization", self.auth())
            .send()
            .await
            .map_err(|e| self.transport(e))?;
        if !response.status().is_success() {
            return Err(self.api_error(response).await);
        }
        Ok(())
    }

    /// Cancel a background response.
    ///
    /// # Errors
    ///
    /// Returns the API error on rejection.
    pub async fn cancel(&self, response_id: &str) -> Result<ResponseObject, AdkError> {
        let response = self
            .client
            .post(format!("{}/responses/{response_id}/cancel", self.base()))
            .header("Authorization", self.auth())
            .send()
            .await
            .map_err(|e| self.transport(e))?;
        self.read_json(response).await
    }

    /// Count input tokens for a request without generating.
    ///
    /// # Errors
    ///
    /// Returns the API error on rejection.
    pub async fn input_tokens(&self, request: &ResponseRequest) -> Result<u64, AdkError> {
        let response = self
            .client
            .post(format!("{}/responses/input_tokens", self.base()))
            .header("Authorization", self.auth())
            .header("Content-Type", "application/json")
            .json(request)
            .send()
            .await
            .map_err(|e| self.transport(e))?;
        let body: Value = self.read_json(response).await?;
        body.get("input_tokens")
            .and_then(Value::as_u64)
            .ok_or_else(|| AdkError::model("input_tokens response carried no count"))
    }

    fn base(&self) -> String {
        self.config.effective_base_url().trim_end_matches('/').to_string()
    }

    fn auth(&self) -> String {
        format!("Bearer {}", self.config.api_key)
    }

    fn transport(&self, error: reqwest::Error) -> AdkError {
        AdkError::new(
            ErrorComponent::Model,
            crate::http_timeouts::transport_category(&error),
            "model.muse.request",
            format!("Muse responses request failed: {error}"),
        )
        .with_provider("muse")
    }

    async fn api_error(&self, response: reqwest::Response) -> AdkError {
        let status = response.status();
        let status_code = status.as_u16();
        let error_text = response.text().await.unwrap_or_default();
        AdkError::new(
            ErrorComponent::Model,
            crate::retry::category_for_status_code(status_code),
            "model.muse.api_error",
            format!(
                "Muse responses API error (HTTP {status}): {}",
                adk_core::upstream_excerpt(&error_text)
            ),
        )
        .with_upstream_status(status_code)
        .with_provider("muse")
    }

    async fn read_json<T: serde::de::DeserializeOwned>(
        &self,
        response: reqwest::Response,
    ) -> Result<T, AdkError> {
        if !response.status().is_success() {
            return Err(self.api_error(response).await);
        }
        let text = response
            .text()
            .await
            .map_err(|e| AdkError::model(format!("Failed to read responses body: {e}")))?;
        serde_json::from_str(&text).map_err(|e| {
            AdkError::model(format!(
                "Failed to parse responses body: {}",
                adk_core::json_error_summary(&e)
            ))
        })
    }
}

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

    #[test]
    fn response_text_and_calls_are_extracted() {
        let response: ResponseObject = serde_json::from_value(serde_json::json!({
            "id": "resp-1",
            "status": "completed",
            "output": [
                {"type": "message", "content": [{"type": "output_text", "text": "Memo plan ready."}]},
                {"type": "function_call", "call_id": "call-1", "name": "analyze_document", "arguments": "{\"id\":\"D1\"}"},
            ],
            "usage": {
                "input_tokens": 10,
                "input_tokens_details": {"cached_tokens": 4},
                "output_tokens": 5,
                "output_tokens_details": {"reasoning_tokens": 3},
                "total_tokens": 15,
            },
        }))
        .unwrap();
        assert!(response.is_completed());
        assert_eq!(response.text(), "Memo plan ready.");
        let usage = response.usage_metadata().unwrap();
        assert_eq!(
            (
                usage.prompt_token_count,
                usage.candidates_token_count,
                usage.total_token_count,
                usage.cache_read_input_token_count,
                usage.thinking_token_count,
            ),
            (10, 5, 15, Some(4), Some(3))
        );
        let calls = response.function_calls();
        assert_eq!(calls.len(), 1);
        assert_eq!(calls[0].name, "analyze_document");
    }

    #[test]
    fn request_chains_and_constrains() {
        let request = ResponseRequest::text("muse-spark-1.3", "next step")
            .with_previous("resp-1")
            .with_json_schema("memo", serde_json::json!({"type": "object"}));
        let wire = serde_json::to_value(&request).unwrap();
        assert_eq!(wire["previous_response_id"], "resp-1");
        assert_eq!(wire["text"]["format"]["type"], "json_schema");
    }
}