ic-rig 0.2.0

A lean, modular library for building LLM applications. Bring your own HTTP client.
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
//! Google Gemini `generateContent` API provider.
//!
//! # Usage
//!
//! ```rust,ignore
//! use irig::providers::gemini::{Client, GEMINI_3_5_FLASH};
//!
//! let client = Client::new(my_http, "AIza...");
//! let model  = client.model(GEMINI_3_5_FLASH);
//!
//! let agent = irig::Agent::builder(model)
//!     .preamble("You are a helpful assistant.")
//!     .build();
//!
//! let reply = agent.prompt("Hello!").await?;
//! ```

use crate::{
    completion::{CompletionError, CompletionModel, CompletionRequest, CompletionResponse, ModelChoice, Usage},
    embeddings::{Embedding, EmbeddingError, EmbeddingModel},
    http::{HttpClient, HttpRequest},
    message::{AssistantContent, Message, ToolCall, UserContent},
    tool::ToolDefinition,
};
use serde::{Deserialize, Serialize};

// ── Model constants ───────────────────────────────────────────────────────────

// ── Completion model constants ────────────────────────────────────────────────

// Gemini 3 (current generation, recommended).
pub const GEMINI_3_1_PRO_PREVIEW: &str = "gemini-3.1-pro-preview";
pub const GEMINI_3_7_FLASH: &str = "gemini-3.7-flash";
pub const GEMINI_3_6_FLASH: &str = "gemini-3.6-flash";
pub const GEMINI_3_5_FLASH: &str = "gemini-3.5-flash";
pub const GEMINI_3_5_FLASH_LITE: &str = "gemini-3.5-flash-lite";
pub const GEMINI_3_1_FLASH_LITE: &str = "gemini-3.1-flash-lite";

// Gemini 2.5 (previous generation; GA-stable, Google's safe fallback baseline
// until it goes off GA on 2026-10-16).
/// Was previously pinned to the dated preview ID `gemini-2.5-pro-preview-05-06`,
/// which Google now redirects to this stable ID — updated 2026-08-27.
pub const GEMINI_2_5_PRO: &str = "gemini-2.5-pro";
pub const GEMINI_2_5_FLASH: &str = "gemini-2.5-flash";
pub const GEMINI_2_5_FLASH_LITE: &str = "gemini-2.5-flash-lite";

/// Shut down by Google on 2026-06-01; kept only so old code still compiles.
#[deprecated(note = "shut down by Google on 2026-06-01; use GEMINI_3_5_FLASH or GEMINI_2_5_FLASH instead")]
pub const GEMINI_2_0_FLASH: &str = "gemini-2.0-flash";
/// Shut down by Google on 2026-06-01; kept only so old code still compiles.
#[deprecated(note = "shut down by Google on 2026-06-01; use GEMINI_3_1_FLASH_LITE or GEMINI_2_5_FLASH_LITE instead")]
pub const GEMINI_2_0_FLASH_LITE: &str = "gemini-2.0-flash-lite";
/// Shut down by Google on 2025-09-29; kept only so old code still compiles.
#[deprecated(note = "shut down by Google on 2025-09-29; use GEMINI_3_1_PRO_PREVIEW or GEMINI_2_5_PRO instead")]
pub const GEMINI_1_5_PRO: &str = "gemini-1.5-pro";
/// Shut down by Google on 2025-09-29; kept only so old code still compiles.
#[deprecated(note = "shut down by Google on 2025-09-29; use GEMINI_3_5_FLASH or GEMINI_2_5_FLASH instead")]
pub const GEMINI_1_5_FLASH: &str = "gemini-1.5-flash";

// ── Embedding model constants ─────────────────────────────────────────────────

pub const GEMINI_EMBEDDING_001: &str = "gemini-embedding-001";
/// Multimodal embedding model.
pub const GEMINI_EMBEDDING_2_PREVIEW: &str = "gemini-embedding-2-preview";

/// Shut down by Google on 2026-01-14; kept only so old code still compiles.
#[deprecated(note = "shut down by Google on 2026-01-14; use GEMINI_EMBEDDING_001 instead")]
pub const TEXT_EMBEDDING_004: &str = "text-embedding-004";
/// Shut down by Google in October 2025; kept only so old code still compiles.
#[deprecated(note = "shut down by Google in October 2025; use GEMINI_EMBEDDING_001 instead")]
pub const EMBEDDING_001: &str = "embedding-001";

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

// ── Client ────────────────────────────────────────────────────────────────────

/// Gemini API client. Use [`model`](Client::model) to get a [`CompletionModel`].
pub struct Client<H> {
    http: H,
    api_key: String,
    base_url: String,
}

impl<H: HttpClient + Clone> Client<H> {
    pub fn new(http: H, api_key: impl Into<String>) -> Self {
        Self { http, api_key: api_key.into(), base_url: BASE_URL.to_owned() }
    }

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

    pub fn model(&self, model: impl Into<String>) -> Model<H> {
        Model {
            http: self.http.clone(),
            api_key: self.api_key.clone(),
            base_url: self.base_url.clone(),
            model: model.into(),
        }
    }

    /// Produce a Gemini [`GeminiEmbeddingModel`].
    pub fn embedding_model(&self, model: impl Into<String>) -> GeminiEmbeddingModel<H> {
        GeminiEmbeddingModel {
            http: self.http.clone(),
            api_key: self.api_key.clone(),
            base_url: self.base_url.clone(),
            model: model.into(),
        }
    }
}

// ── Model ─────────────────────────────────────────────────────────────────────

pub struct Model<H> {
    http: H,
    api_key: String,
    base_url: String,
    model: String,
}

impl<H: HttpClient> CompletionModel for Model<H> {
    type Error = CompletionError;

    async fn complete(&self, request: CompletionRequest) -> Result<CompletionResponse, CompletionError> {
        let body = build_request(request)?;
        let bytes = serde_json::to_vec(&body)?;

        // Gemini passes the API key as a query parameter, not a header.
        let url = format!(
            "{}/{}:generateContent?key={}",
            self.base_url, self.model, self.api_key
        );

        let http_req = HttpRequest::new(url).json_body(bytes);

        let resp = self.http.post(http_req).await
            .map_err(|e| CompletionError::Http(e.to_string()))?;

        if !resp.is_success() {
            let message = String::from_utf8_lossy(&resp.body).into_owned();
            return Err(CompletionError::Provider { status: resp.status, message });
        }

        let api_resp: ApiResponse = resp.json()?;
        parse_response(api_resp)
    }
}

// ── Wire types (Gemini JSON format) ──────────────────────────────────────────

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct ApiRequest {
    contents: Vec<ApiContent>,
    #[serde(skip_serializing_if = "Option::is_none")]
    system_instruction: Option<ApiSystemInstruction>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    tools: Vec<ApiTools>,
    #[serde(skip_serializing_if = "Option::is_none")]
    generation_config: Option<GenerationConfig>,
}

#[derive(Serialize)]
struct ApiSystemInstruction {
    parts: Vec<ApiPart>,
}

/// Gemini groups all tool declarations under a single `tools` object.
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct ApiTools {
    function_declarations: Vec<ApiFunctionDeclaration>,
}

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

#[derive(Serialize, Deserialize)]
struct ApiContent {
    role: String,
    parts: Vec<ApiPart>,
}

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(try_from = "RawPart")]
enum ApiPart {
    /// Plain text.
    Text(String),
    /// Thinking / chain-of-thought trace — a text part with `"thought":
    /// true` alongside it. Only appears when thinking is enabled. Never
    /// treated as the answer — collected into
    /// [`CompletionResponse::reasoning`](crate::completion::CompletionResponse::reasoning)
    /// instead.
    Thought(String),
    /// A tool call emitted by the model (`role: "model"`).
    FunctionCall(ApiFunctionCall),
    /// The result of a tool call (`role: "user"`).
    FunctionResponse(ApiFunctionResponse),
}

/// Incoming Gemini parts carry `text` alongside a sibling `thought: bool`
/// flag rather than a distinct wire "type", which doesn't fit a plain
/// externally-tagged enum — so we deserialize this loose shape first and
/// resolve it into an [`ApiPart`] via [`TryFrom`].
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct RawPart {
    #[serde(default)]
    text: Option<String>,
    #[serde(default)]
    thought: bool,
    #[serde(default)]
    function_call: Option<ApiFunctionCall>,
    #[serde(default)]
    function_response: Option<ApiFunctionResponse>,
}

impl TryFrom<RawPart> for ApiPart {
    type Error = String;

    fn try_from(raw: RawPart) -> Result<Self, Self::Error> {
        if let Some(fc) = raw.function_call {
            Ok(ApiPart::FunctionCall(fc))
        } else if let Some(fr) = raw.function_response {
            Ok(ApiPart::FunctionResponse(fr))
        } else if let Some(text) = raw.text {
            Ok(if raw.thought { ApiPart::Thought(text) } else { ApiPart::Text(text) })
        } else {
            Err("part has none of text, functionCall, functionResponse".into())
        }
    }
}

#[derive(Serialize, Deserialize)]
struct ApiFunctionCall {
    name: String,
    /// Gemini delivers arguments as a JSON **object** (not a string).
    args: serde_json::Value,
}

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

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct GenerationConfig {
    #[serde(skip_serializing_if = "Option::is_none")]
    temperature: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    max_output_tokens: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    thinking_config: Option<ThinkingConfig>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct ThinkingConfig {
    /// `-1` = dynamic (model decides), `0` = disabled. Some Gemini 3 models
    /// (e.g. `gemini-3.1-pro-preview`) can't fully disable thinking — Google's
    /// own docs note this — so `0` may not be honored on every model.
    thinking_budget: i32,
    /// Whether to return thought-summary parts (`"thought": true`) at all;
    /// without this, thinking may still happen internally but won't be
    /// visible in `CompletionResponse::reasoning`.
    include_thoughts: bool,
}

#[derive(Deserialize)]
struct ApiResponse {
    candidates: Vec<ApiCandidate>,
    #[serde(rename = "usageMetadata")]
    usage_metadata: Option<ApiUsageMetadata>,
}

#[derive(Deserialize)]
struct ApiCandidate {
    content: ApiContent,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct ApiUsageMetadata {
    prompt_token_count: u32,
    candidates_token_count: u32,
}

// ── Conversion helpers ────────────────────────────────────────────────────────

fn build_request(req: CompletionRequest) -> Result<ApiRequest, CompletionError> {
    let mut system_instruction: Option<ApiSystemInstruction> = None;
    let mut chat_messages: Vec<Message> = Vec::new();

    for msg in req.messages {
        match msg {
            Message::System { content } => {
                system_instruction = Some(ApiSystemInstruction {
                    parts: vec![ApiPart::Text(content)],
                });
            }
            other => chat_messages.push(other),
        }
    }

    let contents = convert_messages(chat_messages)?;

    let tools = if req.tools.is_empty() {
        Vec::new()
    } else {
        vec![ApiTools {
            function_declarations: req.tools.into_iter().map(convert_tool).collect(),
        }]
    };

    let thinking_config = req.thinking.map(|enabled| ThinkingConfig {
        thinking_budget: if enabled { -1 } else { 0 },
        include_thoughts: enabled,
    });

    let generation_config =
        if req.temperature.is_some() || req.max_tokens.is_some() || thinking_config.is_some() {
            Some(GenerationConfig {
                temperature: req.temperature,
                max_output_tokens: req.max_tokens,
                thinking_config,
            })
        } else {
            None
        };

    Ok(ApiRequest { contents, system_instruction, tools, generation_config })
}

/// Convert irig messages to Gemini's `contents` format.
///
/// Key differences:
/// - Gemini uses `"user"` and `"model"` roles (not `"assistant"`).
/// - Tool calls are `functionCall` parts inside a `"model"` turn.
/// - Tool results are `functionResponse` parts inside a `"user"` turn.
fn convert_messages(messages: Vec<Message>) -> Result<Vec<ApiContent>, CompletionError> {
    let mut out = Vec::new();

    for msg in messages {
        match msg {
            Message::System { .. } => {
                // Extracted above; ignore stragglers.
            }

            Message::User { content } => {
                let parts: Vec<ApiPart> = content
                    .into_iter()
                    .map(|part| match part {
                        UserContent::Text(t) => ApiPart::Text(t.text),
                        UserContent::ToolResult(r) => {
                            ApiPart::FunctionResponse(ApiFunctionResponse {
                                name: r.name,
                                // Wrap the content string in a JSON object so
                                // Gemini receives a structured response field.
                                response: serde_json::json!({ "content": r.content }),
                            })
                        }
                    })
                    .collect();

                out.push(ApiContent { role: "user".into(), parts });
            }

            Message::Assistant { content } => {
                let parts: Vec<ApiPart> = content
                    .into_iter()
                    .map(|part| match part {
                        AssistantContent::Text(t) => ApiPart::Text(t.text),
                        AssistantContent::ToolCall(c) => {
                            ApiPart::FunctionCall(ApiFunctionCall {
                                name: c.name,
                                args: c.arguments,
                            })
                        }
                    })
                    .collect();

                out.push(ApiContent { role: "model".into(), parts });
            }
        }
    }

    Ok(out)
}

fn convert_tool(def: ToolDefinition) -> ApiFunctionDeclaration {
    ApiFunctionDeclaration {
        name: def.name,
        description: def.description,
        parameters: def.parameters,
    }
}

fn parse_response(resp: ApiResponse) -> Result<CompletionResponse, CompletionError> {
    let candidate = resp
        .candidates
        .into_iter()
        .next()
        .ok_or_else(|| CompletionError::Response("no candidates in response".into()))?;

    let usage = resp.usage_metadata.map(|u| Usage {
        prompt_tokens: u.prompt_token_count,
        completion_tokens: u.candidates_token_count,
    });

    let mut text: Option<String> = None;
    let mut reasoning: Option<String> = None;
    let mut tool_calls: Vec<ToolCall> = Vec::new();

    for part in candidate.content.parts {
        match part {
            ApiPart::Text(t) => text = Some(t),
            ApiPart::Thought(t) => {
                reasoning = Some(match reasoning {
                    Some(existing) => format!("{existing}\n{t}"),
                    None => t,
                });
            }
            ApiPart::FunctionCall(fc) => {
                // Gemini doesn't supply a persistent call id; synthesise one
                // from the name so tool results can be correlated.
                let id = format!("call_{}", fc.name);
                tool_calls.push(ToolCall { id, name: fc.name, arguments: fc.args });
            }
            ApiPart::FunctionResponse(_) => {
                // Function responses only appear in user turns; skip if echoed.
            }
        }
    }

    let choice = if !tool_calls.is_empty() {
        ModelChoice::ToolCall(tool_calls)
    } else {
        let t = text.ok_or_else(|| CompletionError::Response("empty parts in candidate".into()))?;
        ModelChoice::Message(t)
    };

    Ok(CompletionResponse { choice, reasoning, usage })
}

// ═════════════════════════════════════════════════════════════════════════════
// Embeddings
// ═════════════════════════════════════════════════════════════════════════════

/// A Gemini embedding model that implements [`EmbeddingModel`].
pub struct GeminiEmbeddingModel<H> {
    http: H,
    api_key: String,
    base_url: String,
    model: String,
}

// ── Wire types ────────────────────────────────────────────────────────────────

/// Gemini `batchEmbedContents` request.
#[derive(Serialize)]
struct BatchEmbedRequest<'a> {
    requests: Vec<EmbedContentRequest<'a>>,
}

#[derive(Serialize)]
struct EmbedContentRequest<'a> {
    model: &'a str,
    content: EmbedContent<'a>,
}

#[derive(Serialize)]
struct EmbedContent<'a> {
    parts: [EmbedPart<'a>; 1],
}

#[derive(Serialize)]
struct EmbedPart<'a> {
    text: &'a str,
}

#[derive(Deserialize)]
struct BatchEmbedResponse {
    embeddings: Vec<GeminiEmbedding>,
}

#[derive(Deserialize)]
struct GeminiEmbedding {
    values: Vec<f64>,
}

// ── Trait impl ────────────────────────────────────────────────────────────────

impl<H: HttpClient> EmbeddingModel for GeminiEmbeddingModel<H> {
    /// Gemini's batch endpoint accepts up to 100 requests at a time.
    const MAX_DOCUMENTS: usize = 100;

    type Error = EmbeddingError;

    fn ndims(&self) -> usize {
        // text-embedding-004 produces 768-dim vectors.
        768
    }

    async fn embed_texts(&self, texts: Vec<String>) -> Result<Vec<Embedding>, EmbeddingError> {
        let model_path = format!("models/{}", self.model);

        let requests: Vec<EmbedContentRequest> = texts
            .iter()
            .map(|t| EmbedContentRequest {
                model: &model_path,
                content: EmbedContent { parts: [EmbedPart { text: t }] },
            })
            .collect();

        let body = serde_json::to_vec(&BatchEmbedRequest { requests })?;

        let url = format!(
            "{}/{}:batchEmbedContents?key={}",
            self.base_url, model_path, self.api_key
        );

        let req = HttpRequest::new(url).json_body(body);

        let resp = self.http.post(req).await
            .map_err(|e| EmbeddingError::Http(e.to_string()))?;

        if !resp.is_success() {
            let message = String::from_utf8_lossy(&resp.body).into_owned();
            return Err(EmbeddingError::Provider { status: resp.status, message });
        }

        let api_resp: BatchEmbedResponse = resp.json()?;

        if api_resp.embeddings.len() != texts.len() {
            return Err(EmbeddingError::Response(format!(
                "expected {} embeddings, got {}",
                texts.len(),
                api_resp.embeddings.len(),
            )));
        }

        Ok(api_resp
            .embeddings
            .into_iter()
            .zip(texts)
            .map(|(e, document)| Embedding { document, vec: e.values })
            .collect())
    }
}

// ── Tests ─────────────────────────────────────────────────────────────────────

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

    #[test]
    fn thinking_toggle_serializes_expected_shape() {
        let mut on = CompletionRequest::new(vec![Message::user("hi")]);
        on.thinking = Some(true);
        let json = serde_json::to_value(build_request(on).unwrap()).unwrap();
        assert_eq!(json["generationConfig"]["thinkingConfig"]["thinkingBudget"], -1);
        assert_eq!(json["generationConfig"]["thinkingConfig"]["includeThoughts"], true);

        let mut off = CompletionRequest::new(vec![Message::user("hi")]);
        off.thinking = Some(false);
        let json = serde_json::to_value(build_request(off).unwrap()).unwrap();
        assert_eq!(json["generationConfig"]["thinkingConfig"]["thinkingBudget"], 0);
        assert_eq!(json["generationConfig"]["thinkingConfig"]["includeThoughts"], false);

        let unset = CompletionRequest::new(vec![Message::user("hi")]);
        let json = serde_json::to_value(build_request(unset).unwrap()).unwrap();
        assert!(json.get("generationConfig").is_none());
    }

    fn make_response(json: &str) -> ApiResponse {
        serde_json::from_str(json).expect("test fixture must deserialise")
    }

    #[test]
    fn thought_part_is_kept_out_of_the_answer() {
        // Regression test: before ApiPart handled the `thought: true`
        // sibling field, a thinking-enabled response would fail to parse
        // entirely (it doesn't fit a plain externally-tagged enum).
        let resp = make_response(
            r#"{
                "candidates": [{
                    "content": {
                        "role": "model",
                        "parts": [
                            {"text": "Reasoning about the problem...", "thought": true},
                            {"text": "Final answer."}
                        ]
                    }
                }],
                "usageMetadata": {"promptTokenCount": 10, "candidatesTokenCount": 5}
            }"#,
        );

        let result = parse_response(resp).unwrap();
        assert!(matches!(result.choice, ModelChoice::Message(ref s) if s == "Final answer."));
        assert_eq!(result.reasoning.as_deref(), Some("Reasoning about the problem..."));
    }

    #[test]
    fn plain_text_response_has_no_reasoning() {
        let resp = make_response(
            r#"{
                "candidates": [{
                    "content": {
                        "role": "model",
                        "parts": [{"text": "Hello, world!"}]
                    }
                }],
                "usageMetadata": {"promptTokenCount": 3, "candidatesTokenCount": 2}
            }"#,
        );

        let result = parse_response(resp).unwrap();
        assert!(matches!(result.choice, ModelChoice::Message(ref s) if s == "Hello, world!"));
        assert_eq!(result.reasoning, None);
    }
}