strands-agents 0.1.0

A Rust implementation of the Strands AI Agents SDK
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
//! Google Gemini model provider.
//!
//! Docs: https://ai.google.dev/api

use std::collections::HashMap;

use async_trait::async_trait;
use serde::{Deserialize, Serialize};

use crate::models::{Model, ModelConfig, StreamEventStream};
use crate::types::content::{Message, SystemContentBlock};
use crate::types::errors::StrandsError;
use crate::types::streaming::StreamEvent;
use crate::types::tools::{ToolChoice, ToolSpec};

/// Configuration for Gemini models.
#[derive(Debug, Clone, Default)]
pub struct GeminiConfig {
    /// Gemini model ID (e.g., "gemini-2.5-flash").
    pub model_id: String,
    /// Additional model parameters (e.g., temperature).
    pub params: HashMap<String, serde_json::Value>,
    /// API key for authentication.
    pub api_key: Option<String>,
    /// Base URL for the API.
    pub base_url: Option<String>,
}

impl GeminiConfig {
    pub fn new(model_id: impl Into<String>) -> Self {
        Self {
            model_id: model_id.into(),
            ..Default::default()
        }
    }

    pub fn with_api_key(mut self, api_key: impl Into<String>) -> Self {
        self.api_key = Some(api_key.into());
        self
    }

    pub fn with_param(mut self, key: impl Into<String>, value: serde_json::Value) -> Self {
        self.params.insert(key.into(), value);
        self
    }
}

/// Gemini API request format.
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct GeminiRequest {
    contents: Vec<GeminiContent>,
    #[serde(skip_serializing_if = "Option::is_none")]
    system_instruction: Option<GeminiContent>,
    #[serde(skip_serializing_if = "Option::is_none")]
    tools: Option<Vec<GeminiTool>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    generation_config: Option<serde_json::Value>,
}

#[derive(Debug, Serialize, Deserialize)]
struct GeminiContent {
    role: String,
    parts: Vec<GeminiPart>,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
enum GeminiPart {
    Text { text: String },
    FunctionCall { function_call: GeminiFunctionCall },
    FunctionResponse { function_response: GeminiFunctionResponse },
}

#[derive(Debug, Serialize, Deserialize)]
struct GeminiFunctionCall {
    name: String,
    args: serde_json::Value,
}

#[derive(Debug, Serialize, Deserialize)]
struct GeminiFunctionResponse {
    name: String,
    response: serde_json::Value,
}

#[derive(Debug, Serialize)]
struct GeminiTool {
    function_declarations: Vec<GeminiFunctionDeclaration>,
}

#[derive(Debug, Serialize)]
struct GeminiFunctionDeclaration {
    name: String,
    description: String,
    parameters: serde_json::Value,
}

/// Google Gemini model provider.
pub struct GeminiModel {
    config: ModelConfig,
    gemini_config: GeminiConfig,
    client: reqwest::Client,
}

impl GeminiModel {
    const DEFAULT_BASE_URL: &'static str = "https://generativelanguage.googleapis.com/v1beta";

    pub fn new(config: GeminiConfig) -> Self {
        let model_config = ModelConfig::new(&config.model_id);

        Self {
            config: model_config,
            gemini_config: config,
            client: reqwest::Client::new(),
        }
    }

    fn base_url(&self) -> &str {
        self.gemini_config
            .base_url
            .as_deref()
            .unwrap_or(Self::DEFAULT_BASE_URL)
    }

    fn api_key(&self) -> Result<&str, StrandsError> {
        self.gemini_config
            .api_key
            .as_deref()
            .or_else(|| std::env::var("GOOGLE_API_KEY").ok().as_deref().map(|_| ""))
            .ok_or_else(|| StrandsError::ConfigurationError {
                message: "Gemini API key not configured. Set GOOGLE_API_KEY or provide api_key".into(),
            })
    }

    fn convert_messages(&self, messages: &[Message]) -> Vec<GeminiContent> {
        messages
            .iter()
            .map(|msg| {
                let role = match msg.role {
                    crate::types::content::Role::User => "user",
                    crate::types::content::Role::Assistant => "model",
                };

                let parts: Vec<GeminiPart> = msg
                    .content
                    .iter()
                    .filter_map(|block| {
                        if let Some(text) = &block.text {
                            Some(GeminiPart::Text { text: text.clone() })
                        } else if let Some(tool_use) = &block.tool_use {
                            Some(GeminiPart::FunctionCall {
                                function_call: GeminiFunctionCall {
                                    name: tool_use.name.clone(),
                                    args: tool_use.input.clone(),
                                },
                            })
                        } else if let Some(tool_result) = &block.tool_result {
                            Some(GeminiPart::FunctionResponse {
                                function_response: GeminiFunctionResponse {
                                    name: tool_result.tool_use_id.clone(),
                                    response: serde_json::json!({
                                        "content": tool_result.content
                                    }),
                                },
                            })
                        } else {
                            None
                        }
                    })
                    .collect();

                GeminiContent {
                    role: role.to_string(),
                    parts,
                }
            })
            .collect()
    }

    fn convert_tools(&self, tool_specs: &[ToolSpec]) -> Vec<GeminiTool> {
        let declarations: Vec<GeminiFunctionDeclaration> = tool_specs
            .iter()
            .map(|spec| GeminiFunctionDeclaration {
                name: spec.name.clone(),
                description: spec.description.clone(),
                parameters: serde_json::to_value(&spec.input_schema).unwrap_or_default(),
            })
            .collect();

        vec![GeminiTool {
            function_declarations: declarations,
        }]
    }
}

#[async_trait]
impl Model for GeminiModel {
    fn config(&self) -> &ModelConfig {
        &self.config
    }

    fn update_config(&mut self, config: ModelConfig) {
        self.config = config;
    }

    fn stream<'a>(
        &'a self,
        messages: &'a [Message],
        tool_specs: Option<&'a [ToolSpec]>,
        system_prompt: Option<&'a str>,
        _tool_choice: Option<ToolChoice>,
        _system_prompt_content: Option<&'a [SystemContentBlock]>,
    ) -> StreamEventStream<'a> {
        let messages = messages.to_vec();
        let tool_specs = tool_specs.map(|t| t.to_vec());
        let system_prompt = system_prompt.map(|s| s.to_string());

        Box::pin(async_stream::stream! {
            let api_key = match self.api_key() {
                Ok(key) => key.to_string(),
                Err(e) => {
                    yield Err(e);
                    return;
                }
            };

            let api_key = if api_key.is_empty() {
                match std::env::var("GOOGLE_API_KEY") {
                    Ok(key) => key,
                    Err(_) => {
                        yield Err(StrandsError::ConfigurationError {
                            message: "GOOGLE_API_KEY not set".into(),
                        });
                        return;
                    }
                }
            } else {
                api_key
            };

            let contents = self.convert_messages(&messages);

            let system_instruction = system_prompt.map(|prompt| GeminiContent {
                role: "user".to_string(),
                parts: vec![GeminiPart::Text { text: prompt }],
            });

            let tools = tool_specs.as_ref().map(|specs| self.convert_tools(specs));

            let request = GeminiRequest {
                contents,
                system_instruction,
                tools,
                generation_config: if self.gemini_config.params.is_empty() {
                    None
                } else {
                    Some(serde_json::to_value(&self.gemini_config.params).unwrap_or_default())
                },
            };

            let url = format!(
                "{}/models/{}:streamGenerateContent?key={}&alt=sse",
                self.base_url(),
                self.config.model_id,
                api_key
            );

            let response = match self.client
                .post(&url)
                .json(&request)
                .send()
                .await
            {
                Ok(resp) => resp,
                Err(e) => {
                    yield Err(StrandsError::NetworkError(e.to_string()));
                    return;
                }
            };

            if !response.status().is_success() {
                let status = response.status();
                let body = response.text().await.unwrap_or_default();

                if status.as_u16() == 429 {
                    yield Err(StrandsError::ModelThrottled {
                        message: "Gemini rate limit exceeded".into(),
                    });
                } else {
                    yield Err(StrandsError::ModelError {
                        message: format!("Gemini API error {}: {}", status, body),
                        source: None,
                    });
                }
                return;
            }

            yield Ok(StreamEvent::message_start(crate::types::content::Role::Assistant));
            yield Ok(StreamEvent::content_block_start(0, None));

            let body = match response.text().await {
                Ok(b) => b,
                Err(e) => {
                    yield Err(StrandsError::NetworkError(e.to_string()));
                    return;
                }
            };

            let mut tool_used = false;
            let mut finish_reason = "STOP";
            let mut input_tokens = 0u64;
            let mut output_tokens = 0u64;

            for line in body.lines() {
                let line = line.trim();

                if line.is_empty() || line.starts_with(':') {
                    continue;
                }

                if let Some(data) = line.strip_prefix("data: ") {
                    if data.trim() == "[DONE]" {
                        continue;
                    }

                    if let Ok(parsed) = serde_json::from_str::<serde_json::Value>(data) {
                        if let Some(usage) = parsed.get("usageMetadata") {
                            if let Some(prompt_tokens) = usage.get("promptTokenCount").and_then(|v| v.as_u64()) {
                                input_tokens = prompt_tokens;
                            }
                            if let Some(candidates_tokens) = usage.get("candidatesTokenCount").and_then(|v| v.as_u64()) {
                                output_tokens = candidates_tokens;
                            }
                        }

                        if let Some(candidates) = parsed.get("candidates").and_then(|c| c.as_array()) {
                            for candidate in candidates {
                                if let Some(reason) = candidate.get("finishReason").and_then(|r| r.as_str()) {
                                    finish_reason = match reason {
                                        "MAX_TOKENS" => "MAX_TOKENS",
                                        "SAFETY" => "SAFETY",
                                        "STOP" | _ => "STOP",
                                    };
                                }

                                if let Some(content) = candidate.get("content") {
                                    if let Some(parts) = content.get("parts").and_then(|p| p.as_array()) {
                                        for part in parts {
                                            if let Some(text) = part.get("text").and_then(|t| t.as_str()) {
                                                let is_thought = part.get("thought").and_then(|t| t.as_bool()).unwrap_or(false);
                                                if is_thought {
                                                    yield Ok(StreamEvent::reasoning_delta(0, text));
                                                } else {
                                                    yield Ok(StreamEvent::text_delta(0, text));
                                                }
                                            }

                                            if let Some(function_call) = part.get("functionCall") {
                                                if let (Some(name), Some(args)) = (
                                                    function_call.get("name").and_then(|n| n.as_str()),
                                                    function_call.get("args"),
                                                ) {
                                                    tool_used = true;
                                                    yield Ok(StreamEvent::tool_use_start(
                                                        1,
                                                        name,
                                                        name,
                                                    ));
                                                    yield Ok(StreamEvent::tool_use_delta(
                                                        1,
                                                        &serde_json::to_string(args).unwrap_or_default(),
                                                    ));
                                                    yield Ok(StreamEvent::content_block_stop(1));
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            yield Ok(StreamEvent::content_block_stop(0));

            let stop_reason = if tool_used {
                crate::types::streaming::StopReason::ToolUse
            } else {
                match finish_reason {
                    "MAX_TOKENS" => crate::types::streaming::StopReason::MaxTokens,
                    _ => crate::types::streaming::StopReason::EndTurn,
                }
            };

            yield Ok(StreamEvent::message_stop(stop_reason));

            yield Ok(StreamEvent::metadata(
                crate::types::streaming::Usage::new(input_tokens as u32, output_tokens as u32),
                crate::types::streaming::Metrics::default(),
            ));
        })
    }
}

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

    #[test]
    fn test_gemini_config() {
        let config = GeminiConfig::new("gemini-2.5-flash")
            .with_api_key("test-key")
            .with_param("temperature", serde_json::json!(0.7));

        assert_eq!(config.model_id, "gemini-2.5-flash");
        assert_eq!(config.api_key, Some("test-key".to_string()));
        assert!(config.params.contains_key("temperature"));
    }
}