cersei-provider 0.1.9

Provider trait and built-in LLM providers for the Cersei 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
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
//! Google Gemini provider: native Gemini API client with streaming support.
//!
//! Uses Google's `generateContent` API directly rather than the OpenAI-compatible
//! shim, enabling access to native Gemini features like safety settings,
//! grounding, and proper multimodal support.

use crate::*;
use cersei_types::*;
use futures::StreamExt;
use tokio::sync::mpsc;

const GEMINI_API_BASE: &str = "https://generativelanguage.googleapis.com/v1beta";

// ─── Gemini provider ────────────────────────────────────────────────────────

pub struct Gemini {
    api_key: String,
    base_url: String,
    default_model: String,
    client: reqwest::Client,
}

impl Gemini {
    pub fn new(api_key: impl Into<String>) -> Self {
        let base_url = std::env::var("GEMINI_BASE_URL")
            .ok()
            .filter(|u| !u.is_empty())
            .unwrap_or_else(|| GEMINI_API_BASE.to_string());
        Self {
            api_key: api_key.into(),
            base_url,
            default_model: "gemini-3.1-pro-preview".to_string(),
            client: reqwest::Client::new(),
        }
    }

    /// Create from `GOOGLE_API_KEY` or `GEMINI_API_KEY` environment variable.
    pub fn from_env() -> Result<Self> {
        let key = std::env::var("GOOGLE_API_KEY")
            .or_else(|_| std::env::var("GEMINI_API_KEY"))
            .map_err(|_| CerseiError::Auth("GOOGLE_API_KEY or GEMINI_API_KEY not set".into()))?;
        Ok(Self::new(key))
    }

    pub fn builder() -> GeminiBuilder {
        GeminiBuilder::default()
    }
}

#[async_trait::async_trait]
impl Provider for Gemini {
    fn name(&self) -> &str {
        "google"
    }

    fn context_window(&self, model: &str) -> u64 {
        match model {
            m if m.contains("gemini-3.1") => 2_000_000,
            m if m.contains("gemini-3.0") => 1_000_000,
            m if m.contains("gemini-2.0") => 1_000_000,
            m if m.contains("gemini-1.5-pro") => 2_000_000,
            m if m.contains("gemini-1.5-flash") => 1_000_000,
            _ => 1_000_000,
        }
    }

    fn capabilities(&self, _model: &str) -> ProviderCapabilities {
        ProviderCapabilities {
            streaming: true,
            tool_use: true,
            vision: true,
            thinking: false,
            system_prompt: true,
            caching: false,
        }
    }

    async fn complete(&self, request: CompletionRequest) -> Result<CompletionStream> {
        let model = if request.model.is_empty() {
            self.default_model.clone()
        } else {
            request.model.clone()
        };

        // Build a map of tool_use_id → tool_name from conversation history
        let tool_name_map: std::collections::HashMap<String, String> = request
            .messages
            .iter()
            .flat_map(|m| match &m.content {
                MessageContent::Blocks(blocks) => blocks
                    .iter()
                    .filter_map(|b| {
                        if let ContentBlock::ToolUse { id, name, .. } = b {
                            Some((id.clone(), name.clone()))
                        } else {
                            None
                        }
                    })
                    .collect::<Vec<_>>(),
                _ => vec![],
            })
            .collect();

        // Build Gemini-native contents array
        let mut contents: Vec<serde_json::Value> = Vec::new();

        for msg in &request.messages {
            match msg.role {
                Role::User => {
                    let mut parts: Vec<serde_json::Value> = Vec::new();

                    if let MessageContent::Blocks(blocks) = &msg.content {
                        for block in blocks {
                            match block {
                                ContentBlock::Text { text } => {
                                    parts.push(serde_json::json!({ "text": text }));
                                }
                                ContentBlock::ToolResult {
                                    tool_use_id,
                                    content,
                                    ..
                                } => {
                                    // Gemini requires the function NAME, not the call ID
                                    let func_name = tool_name_map
                                        .get(tool_use_id)
                                        .cloned()
                                        .unwrap_or_else(|| tool_use_id.clone());
                                    let content_str = match content {
                                        ToolResultContent::Text(s) => s.clone(),
                                        ToolResultContent::Blocks(blocks) => blocks
                                            .iter()
                                            .filter_map(|b| {
                                                if let ContentBlock::Text { text } = b {
                                                    Some(text.as_str())
                                                } else {
                                                    None
                                                }
                                            })
                                            .collect::<Vec<_>>()
                                            .join("\n"),
                                    };
                                    parts.push(serde_json::json!({
                                        "functionResponse": {
                                            "name": func_name,
                                            "response": { "content": content_str },
                                        }
                                    }));
                                }
                                _ => {}
                            }
                        }
                    } else {
                        parts.push(serde_json::json!({ "text": msg.get_all_text() }));
                    }

                    if !parts.is_empty() {
                        contents.push(serde_json::json!({
                            "role": "user",
                            "parts": parts,
                        }));
                    }
                }
                Role::Assistant => {
                    let mut parts: Vec<serde_json::Value> = Vec::new();

                    if let MessageContent::Blocks(blocks) = &msg.content {
                        for block in blocks {
                            match block {
                                ContentBlock::Text { text } => {
                                    parts.push(serde_json::json!({ "text": text }));
                                }
                                ContentBlock::ToolUse { id, name, input } => {
                                    // Extract fc_id and thoughtSignature from encoded tool_id
                                    // Format: "gemini-tool-N::fc_id::thoughtSignature" or "gemini-tool-N"
                                    let segments: Vec<&str> = id.splitn(3, "::").collect();
                                    let mut fc = serde_json::json!({
                                        "name": name,
                                        "args": input,
                                    });
                                    let mut part_obj = serde_json::Map::new();
                                    if segments.len() >= 3 {
                                        // Has fc_id and thoughtSignature
                                        fc["id"] =
                                            serde_json::Value::String(segments[1].to_string());
                                        part_obj.insert("functionCall".to_string(), fc);
                                        part_obj.insert(
                                            "thoughtSignature".to_string(),
                                            serde_json::Value::String(segments[2].to_string()),
                                        );
                                    } else {
                                        part_obj.insert("functionCall".to_string(), fc);
                                    }
                                    parts.push(serde_json::Value::Object(part_obj));
                                }
                                _ => {}
                            }
                        }
                    } else {
                        parts.push(serde_json::json!({ "text": msg.get_all_text() }));
                    }

                    if !parts.is_empty() {
                        contents.push(serde_json::json!({
                            "role": "model",
                            "parts": parts,
                        }));
                    }
                }
                Role::System => {
                    // System messages handled separately via systemInstruction
                }
            }
        }

        // Build request body
        let mut body = serde_json::json!({
            "contents": contents,
            "generationConfig": {
                "maxOutputTokens": request.max_tokens,
            },
        });

        // System instruction (Gemini's equivalent of system prompt)
        if let Some(system) = &request.system {
            body["systemInstruction"] = serde_json::json!({
                "parts": [{ "text": system }],
            });
        }

        if let Some(temp) = request.temperature {
            body["generationConfig"]["temperature"] = serde_json::json!(temp);
        }

        if !request.stop_sequences.is_empty() {
            body["generationConfig"]["stopSequences"] = serde_json::json!(request.stop_sequences);
        }

        // Tool declarations
        if !request.tools.is_empty() {
            let function_declarations: Vec<serde_json::Value> = request
                .tools
                .iter()
                .map(|t| {
                    serde_json::json!({
                        "name": t.name,
                        "description": t.description,
                        "parameters": t.input_schema,
                    })
                })
                .collect();
            body["tools"] = serde_json::json!([{
                "functionDeclarations": function_declarations,
            }]);
        }

        // Safety settings: use least restrictive defaults to avoid unexpected blocks
        body["safetySettings"] = serde_json::json!([
            { "category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_ONLY_HIGH" },
            { "category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_ONLY_HIGH" },
            { "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_ONLY_HIGH" },
            { "category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_ONLY_HIGH" },
        ]);

        // SECURITY: never put the API key in the URL. Use the
        // `x-goog-api-key` header so that reqwest's error `Display` (which
        // prints the URL) cannot leak the secret into logs or error-wrapped
        // output.
        let url = format!(
            "{}/models/{}:streamGenerateContent?alt=sse",
            self.base_url, model
        );

        let (tx, rx) = mpsc::channel(256);

        let req = self
            .client
            .post(&url)
            .header("x-goog-api-key", &self.api_key)
            .header("content-type", "application/json")
            .json(&body)
            .build()
            .map_err(CerseiError::Http)?;

        let client = self.client.clone();

        tokio::spawn(async move {
            match client.execute(req).await {
                Ok(response) => {
                    if !response.status().is_success() {
                        let status = response.status().as_u16();
                        let body = response.text().await.unwrap_or_default();
                        let _ = tx
                            .send(StreamEvent::Error {
                                message: format!("HTTP {}: {}", status, body),
                            })
                            .await;
                        return;
                    }

                    let _ = tx
                        .send(StreamEvent::MessageStart {
                            id: String::new(),
                            model: String::new(),
                        })
                        .await;

                    let mut stream = response.bytes_stream();
                    let mut buffer = String::new();
                    let mut block_index: usize = 0;
                    let mut total_input_tokens: u64 = 0;
                    let mut total_output_tokens: u64 = 0;
                    let mut saw_function_calls = false;

                    while let Some(chunk) = stream.next().await {
                        match chunk {
                            Ok(bytes) => {
                                buffer.push_str(&String::from_utf8_lossy(&bytes));

                                while let Some(pos) = buffer.find("\n") {
                                    let line = buffer[..pos].to_string();
                                    buffer = buffer[pos + 1..].to_string();

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

                                        if let Ok(json) =
                                            serde_json::from_str::<serde_json::Value>(data)
                                        {
                                            // Extract usage metadata
                                            if let Some(metadata) = json.get("usageMetadata") {
                                                total_input_tokens = metadata
                                                    .get("promptTokenCount")
                                                    .and_then(|v| v.as_u64())
                                                    .unwrap_or(total_input_tokens);
                                                total_output_tokens = metadata
                                                    .get("candidatesTokenCount")
                                                    .and_then(|v| v.as_u64())
                                                    .unwrap_or(total_output_tokens);
                                            }

                                            // Process candidates
                                            if let Some(candidates) =
                                                json.get("candidates").and_then(|c| c.as_array())
                                            {
                                                for candidate in candidates {
                                                    if let Some(parts) = candidate
                                                        .get("content")
                                                        .and_then(|c| c.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 _ = tx
                                                                    .send(StreamEvent::ContentBlockStart {
                                                                        index: block_index,
                                                                        block_type: "text".into(),
                                                                        id: None,
                                                                        name: None,
                                                                    })
                                                                    .await;
                                                                let _ = tx
                                                                    .send(StreamEvent::TextDelta {
                                                                        index: block_index,
                                                                        text: text.to_string(),
                                                                    })
                                                                    .await;
                                                                let _ = tx
                                                                    .send(StreamEvent::ContentBlockStop {
                                                                        index: block_index,
                                                                    })
                                                                    .await;
                                                                block_index += 1;
                                                            }

                                                            if let Some(fc) =
                                                                part.get("functionCall")
                                                            {
                                                                saw_function_calls = true;
                                                                let name = fc
                                                                    .get("name")
                                                                    .and_then(|n| n.as_str())
                                                                    .unwrap_or("")
                                                                    .to_string();
                                                                let args = fc
                                                                    .get("args")
                                                                    .cloned()
                                                                    .unwrap_or(
                                                                        serde_json::Value::Object(
                                                                            Default::default(),
                                                                        ),
                                                                    );
                                                                // Capture thoughtSignature (sibling of functionCall at part level, Gemini 3.1+)
                                                                let thought_sig = part
                                                                    .get("thoughtSignature")
                                                                    .and_then(|s| s.as_str())
                                                                    .unwrap_or("");
                                                                // Capture functionCall.id if present
                                                                let fc_id = fc
                                                                    .get("id")
                                                                    .and_then(|s| s.as_str())
                                                                    .unwrap_or("");
                                                                // Encode both in tool_id for roundtrip
                                                                let tool_id =
                                                                    if thought_sig.is_empty() {
                                                                        format!(
                                                                            "gemini-tool-{}",
                                                                            block_index
                                                                        )
                                                                    } else {
                                                                        format!(
                                                                        "gemini-tool-{}::{}::{}",
                                                                        block_index,
                                                                        fc_id,
                                                                        thought_sig
                                                                    )
                                                                    };

                                                                let _ = tx
                                                                    .send(StreamEvent::ContentBlockStart {
                                                                        index: block_index,
                                                                        block_type: "tool_use".into(),
                                                                        id: Some(tool_id),
                                                                        name: Some(name),
                                                                    })
                                                                    .await;
                                                                let _ = tx
                                                                    .send(StreamEvent::InputJsonDelta {
                                                                        index: block_index,
                                                                        partial_json: serde_json::to_string(&args)
                                                                            .unwrap_or_default(),
                                                                    })
                                                                    .await;
                                                                let _ = tx
                                                                    .send(StreamEvent::ContentBlockStop {
                                                                        index: block_index,
                                                                    })
                                                                    .await;
                                                                block_index += 1;
                                                            }
                                                        }
                                                    }

                                                    // Check finish reason
                                                    let finish_reason = candidate
                                                        .get("finishReason")
                                                        .and_then(|r| r.as_str());
                                                    if let Some(reason) = finish_reason {
                                                        let stop = if saw_function_calls {
                                                            StopReason::ToolUse
                                                        } else {
                                                            match reason {
                                                                "STOP" => StopReason::EndTurn,
                                                                "MAX_TOKENS" => {
                                                                    StopReason::MaxTokens
                                                                }
                                                                "SAFETY" => StopReason::EndTurn,
                                                                _ => StopReason::EndTurn,
                                                            }
                                                        };
                                                        let _ = tx
                                                            .send(StreamEvent::MessageDelta {
                                                                stop_reason: Some(stop),
                                                                usage: Some(Usage {
                                                                    input_tokens:
                                                                        total_input_tokens,
                                                                    output_tokens:
                                                                        total_output_tokens,
                                                                    ..Default::default()
                                                                }),
                                                            })
                                                            .await;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            Err(e) => {
                                let _ = tx
                                    .send(StreamEvent::Error {
                                        message: e.to_string(),
                                    })
                                    .await;
                                return;
                            }
                        }
                    }

                    let _ = tx.send(StreamEvent::MessageStop).await;
                }
                Err(e) => {
                    let _ = tx
                        .send(StreamEvent::Error {
                            message: e.to_string(),
                        })
                        .await;
                }
            }
        });

        Ok(CompletionStream::new(rx))
    }
}

// ─── Builder ─────────────────────────────────────────────────────────────────

#[derive(Default)]
pub struct GeminiBuilder {
    api_key: Option<String>,
    base_url: Option<String>,
    model: Option<String>,
}

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

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

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

    pub fn build(self) -> Result<Gemini> {
        let api_key = if let Some(key) = self.api_key {
            key
        } else {
            return Err(CerseiError::Auth(
                "No API key provided. Set GOOGLE_API_KEY or GEMINI_API_KEY or use .api_key()"
                    .into(),
            ));
        };

        Ok(Gemini {
            api_key,
            base_url: self.base_url.unwrap_or_else(|| GEMINI_API_BASE.to_string()),
            default_model: self
                .model
                .unwrap_or_else(|| "gemini-3.1-pro-preview".to_string()),
            client: reqwest::Client::new(),
        })
    }
}