kcode-gemini-api 0.2.0

Gemini inference, multimodal generation, and session spending limits
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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
use std::fmt;

use base64::{Engine as _, engine::general_purpose::STANDARD};
use serde_json::{Value, json};

use crate::{Error, GEMINI_31_FLASH_LITE, GEMINI_31_PRO, Result};

pub(crate) const MAX_PROMPT_BYTES: usize = 4 * 1024 * 1024;
pub(crate) const MAX_INLINE_MEDIA_BYTES: usize = 64 * 1024 * 1024;
pub(crate) const MAX_NANO_BANANA_IMAGES: usize = 14;
pub(crate) const MAX_TEXT_OUTPUT_TOKENS: u32 = 65_536;
pub(crate) const MAX_IMAGE_OUTPUT_TOKENS: u32 = 32_768;
const MAX_STRUCTURED_OUTPUT_SCHEMA_BYTES: usize = 1024 * 1024;

/// A non-negative USD amount represented exactly in billionths of one dollar.
#[derive(Clone, Copy, Debug, Default, Eq, Ord, PartialEq, PartialOrd)]
pub struct Money(u64);

impl Money {
    /// Zero dollars.
    pub const ZERO: Self = Self(0);
    /// Constructs an amount from billionths of one US dollar.
    pub const fn from_usd_nanos(value: u64) -> Self {
        Self(value)
    }
    /// Constructs an amount from millionths of one US dollar.
    pub const fn from_usd_micros(value: u64) -> Self {
        Self(value.saturating_mul(1_000))
    }
    /// Returns the amount in billionths of one US dollar.
    pub const fn usd_nanos(self) -> u64 {
        self.0
    }
    /// Returns a display-oriented floating-point dollar value.
    pub fn as_usd(self) -> f64 {
        self.0 as f64 / 1_000_000_000.0
    }
    pub(crate) fn saturating_add(self, other: Self) -> Self {
        Self(self.0.saturating_add(other.0))
    }
}

impl fmt::Display for Money {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "${:.9}", self.as_usd())
    }
}

/// Text inference models intentionally supported by this crate.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum TextModel {
    /// Gemini 3.1 Flash-Lite.
    FlashLite,
    /// Gemini 3.1 Pro, currently exposed as a preview model.
    Pro,
}

impl TextModel {
    /// Returns the exact API model identifier.
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::FlashLite => GEMINI_31_FLASH_LITE,
            Self::Pro => GEMINI_31_PRO,
        }
    }
}

impl fmt::Display for TextModel {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Gemini inference service tier.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
pub enum ServiceTier {
    /// Standard synchronous pricing and scheduling.
    #[default]
    Standard,
    /// Priority scheduling at Google's higher published prices.
    Priority,
}

impl ServiceTier {
    pub(crate) const fn as_str(self) -> &'static str {
        match self {
            Self::Standard => "standard",
            Self::Priority => "priority",
        }
    }
}

/// Maximum model reasoning depth.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum ThinkingLevel {
    /// Little to no reasoning; supported by Flash-Lite but not Pro.
    Minimal,
    /// Low reasoning depth.
    Low,
    /// Medium reasoning depth.
    Medium,
    /// High reasoning depth.
    High,
}

impl ThinkingLevel {
    pub(crate) const fn as_str(self) -> &'static str {
        match self {
            Self::Minimal => "minimal",
            Self::Low => "low",
            Self::Medium => "medium",
            Self::High => "high",
        }
    }
}

/// Shared generation controls.
#[derive(Clone, Debug, PartialEq)]
pub struct GenerationOptions {
    /// Optional maximum generated token budget, including thinking tokens.
    /// Omission lets Gemini select its model default.
    pub max_output_tokens: Option<u32>,
    /// Optional sampling temperature in the inclusive range 0 through 2.
    pub temperature: Option<f32>,
    /// Optional maximum thinking depth; omission uses the model default.
    pub thinking_level: Option<ThinkingLevel>,
    /// Gemini scheduling and pricing tier.
    pub service_tier: ServiceTier,
}

impl Default for GenerationOptions {
    fn default() -> Self {
        Self {
            max_output_tokens: Some(8_192),
            temperature: None,
            thinking_level: None,
            service_tier: ServiceTier::Standard,
        }
    }
}

impl GenerationOptions {
    pub(crate) fn validate(&self, model: TextModel) -> Result<()> {
        if let Some(maximum) = self.max_output_tokens {
            validate_output_tokens(maximum, MAX_TEXT_OUTPUT_TOKENS)?;
        }
        if self
            .temperature
            .is_some_and(|value| !value.is_finite() || !(0.0..=2.0).contains(&value))
        {
            return Err(Error::InvalidInput(
                "temperature must be finite and between 0 and 2".into(),
            ));
        }
        if model == TextModel::Pro && self.thinking_level == Some(ThinkingLevel::Minimal) {
            return Err(Error::InvalidInput(
                "Gemini 3.1 Pro does not support minimal thinking".into(),
            ));
        }
        Ok(())
    }

    pub(crate) fn generation_config(&self) -> Value {
        let mut value = json!({});
        let object = value.as_object_mut().expect("configuration is an object");
        if let Some(maximum) = self.max_output_tokens {
            object.insert("max_output_tokens".into(), json!(maximum));
        }
        if let Some(temperature) = self.temperature {
            object.insert("temperature".into(), json!(temperature));
        }
        if let Some(level) = self.thinking_level {
            object.insert("thinking_level".into(), json!(level.as_str()));
        }
        value
    }
}

/// A validated JSON Schema requesting JSON structured output.
///
/// Gemini receives this as an `application/json` text response format. The
/// provider remains responsible for validating which JSON Schema features the
/// selected model supports.
#[derive(Clone, Debug, PartialEq)]
pub struct StructuredOutput {
    schema: Value,
}

impl StructuredOutput {
    /// Constructs a structured-output request from a JSON Schema object.
    pub fn new(schema: Value) -> Result<Self> {
        let object = schema.as_object().ok_or_else(|| {
            Error::InvalidInput("structured-output schema must be a JSON object".into())
        })?;
        if object.is_empty() {
            return Err(Error::InvalidInput(
                "structured-output schema must not be empty".into(),
            ));
        }
        if serde_json::to_vec(&schema)
            .map_err(|error| Error::InvalidInput(format!("invalid JSON Schema: {error}")))?
            .len()
            > MAX_STRUCTURED_OUTPUT_SCHEMA_BYTES
        {
            return Err(Error::InvalidInput(format!(
                "structured-output schema exceeds the {MAX_STRUCTURED_OUTPUT_SCHEMA_BYTES}-byte safety limit"
            )));
        }
        Ok(Self { schema })
    }

    /// Returns the JSON Schema sent to Gemini.
    pub fn schema(&self) -> &Value {
        &self.schema
    }

    pub(crate) fn response_format(&self) -> Value {
        json!({
            "type":"text",
            "mime_type":"application/json",
            "schema":self.schema,
        })
    }
}

/// One text-only inference request.
#[derive(Clone, Debug, PartialEq)]
pub struct InferenceRequest {
    /// User prompt.
    pub prompt: String,
    /// Optional system instruction.
    pub system_instruction: Option<String>,
    /// Optional JSON structured-output contract.
    pub structured_output: Option<StructuredOutput>,
    /// Generation controls.
    pub options: GenerationOptions,
}

impl InferenceRequest {
    /// Constructs a request with default generation controls.
    pub fn new(prompt: impl Into<String>) -> Self {
        Self {
            prompt: prompt.into(),
            system_instruction: None,
            structured_output: None,
            options: GenerationOptions::default(),
        }
    }
}

/// Inline media category accepted by Gemini 3.1 Pro.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum MediaKind {
    /// Image input.
    Image,
    /// Audio input.
    Audio,
    /// Video input.
    Video,
}

impl MediaKind {
    pub(crate) const fn as_str(self) -> &'static str {
        match self {
            Self::Image => "image",
            Self::Audio => "audio",
            Self::Video => "video",
        }
    }
}

/// One image, audio, or video input held in memory for an inference request.
#[derive(Clone, Eq, PartialEq)]
pub struct MediaInput {
    kind: MediaKind,
    mime_type: String,
    data: Vec<u8>,
}

impl MediaInput {
    /// Constructs an image input after validation.
    pub fn image(mime_type: impl Into<String>, data: Vec<u8>) -> Result<Self> {
        Self::new(MediaKind::Image, mime_type.into(), data)
    }
    /// Constructs an audio input after validation.
    pub fn audio(mime_type: impl Into<String>, data: Vec<u8>) -> Result<Self> {
        Self::new(MediaKind::Audio, mime_type.into(), data)
    }
    /// Constructs a video input after validation.
    pub fn video(mime_type: impl Into<String>, data: Vec<u8>) -> Result<Self> {
        Self::new(MediaKind::Video, mime_type.into(), data)
    }
    /// Returns whether this is image, audio, or video data.
    pub const fn kind(&self) -> MediaKind {
        self.kind
    }
    /// Returns the validated MIME type.
    pub fn mime_type(&self) -> &str {
        &self.mime_type
    }
    /// Returns the raw byte count.
    pub fn len(&self) -> usize {
        self.data.len()
    }
    /// Returns true when the input has no bytes.
    pub fn is_empty(&self) -> bool {
        self.data.is_empty()
    }

    fn new(kind: MediaKind, mime_type: String, data: Vec<u8>) -> Result<Self> {
        if data.is_empty() {
            return Err(Error::InvalidInput("media data must not be empty".into()));
        }
        let valid = match kind {
            MediaKind::Image => matches!(
                mime_type.as_str(),
                "image/png"
                    | "image/jpeg"
                    | "image/webp"
                    | "image/heic"
                    | "image/heif"
                    | "image/gif"
                    | "image/bmp"
                    | "image/tiff"
            ),
            MediaKind::Audio => matches!(
                mime_type.as_str(),
                "audio/wav"
                    | "audio/mp3"
                    | "audio/aiff"
                    | "audio/aac"
                    | "audio/ogg"
                    | "audio/flac"
                    | "audio/mpeg"
                    | "audio/m4a"
                    | "audio/l16"
                    | "audio/opus"
                    | "audio/alaw"
                    | "audio/mulaw"
            ),
            MediaKind::Video => matches!(
                mime_type.as_str(),
                "video/mp4"
                    | "video/mpeg"
                    | "video/mov"
                    | "video/quicktime"
                    | "video/avi"
                    | "video/x-flv"
                    | "video/mpg"
                    | "video/webm"
                    | "video/wmv"
                    | "video/3gpp"
            ),
        };
        if !valid {
            return Err(Error::InvalidInput(format!(
                "unsupported {} MIME type {mime_type:?}",
                kind.as_str()
            )));
        }
        Ok(Self {
            kind,
            mime_type,
            data,
        })
    }

    pub(crate) fn interaction_value(&self) -> Value {
        json!({
            "type": self.kind.as_str(),
            "mime_type": self.mime_type,
            "data": STANDARD.encode(&self.data),
        })
    }
}

impl fmt::Debug for MediaInput {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("MediaInput")
            .field("kind", &self.kind)
            .field("mime_type", &self.mime_type)
            .field("bytes", &self.data.len())
            .finish()
    }
}

/// Image, audio, and/or video inference request for Gemini 3.1 Pro.
#[derive(Clone, Debug, PartialEq)]
pub struct MultimodalRequest {
    /// User prompt accompanying the media.
    pub prompt: String,
    /// Image, audio, and video inputs; at least one is required.
    pub media: Vec<MediaInput>,
    /// Optional system instruction.
    pub system_instruction: Option<String>,
    /// Optional JSON structured-output contract.
    pub structured_output: Option<StructuredOutput>,
    /// Generation controls.
    pub options: GenerationOptions,
}

impl MultimodalRequest {
    /// Constructs a request with default generation controls.
    pub fn new(prompt: impl Into<String>, media: Vec<MediaInput>) -> Self {
        Self {
            prompt: prompt.into(),
            media,
            system_instruction: None,
            structured_output: None,
            options: GenerationOptions::default(),
        }
    }
}

/// Native-image aspect ratio supported by Nano Banana Pro at 2K.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
pub enum AspectRatio {
    /// 1:1 square output.
    #[default]
    Square,
    /// 3:2 landscape output.
    ThreeTwo,
    /// 2:3 portrait output.
    TwoThree,
    /// 4:3 landscape output.
    FourThree,
    /// 3:4 portrait output.
    ThreeFour,
    /// 5:4 landscape output.
    FiveFour,
    /// 4:5 portrait output.
    FourFive,
    /// 16:9 landscape output.
    SixteenNine,
    /// 9:16 portrait output.
    NineSixteen,
    /// 21:9 ultrawide output.
    TwentyOneNine,
}

impl AspectRatio {
    pub(crate) const fn as_str(self) -> &'static str {
        match self {
            Self::Square => "1:1",
            Self::ThreeTwo => "3:2",
            Self::TwoThree => "2:3",
            Self::FourThree => "4:3",
            Self::ThreeFour => "3:4",
            Self::FiveFour => "5:4",
            Self::FourFive => "4:5",
            Self::SixteenNine => "16:9",
            Self::NineSixteen => "9:16",
            Self::TwentyOneNine => "21:9",
        }
    }
}

/// Nano Banana Pro generation or editing request with fixed 2K output.
#[derive(Clone, Debug, PartialEq)]
pub struct NanoBananaProRequest {
    /// Text generation or editing prompt.
    pub prompt: String,
    /// Optional image inputs for editing or visual reference.
    pub images: Vec<MediaInput>,
    /// Requested aspect ratio.
    pub aspect_ratio: AspectRatio,
    /// Generation controls; Nano Banana Pro supports Standard service only here.
    pub options: GenerationOptions,
}

impl NanoBananaProRequest {
    /// Constructs a 2K text-to-image request with defaults.
    pub fn new(prompt: impl Into<String>) -> Self {
        Self {
            prompt: prompt.into(),
            images: Vec::new(),
            aspect_ratio: AspectRatio::default(),
            options: GenerationOptions::default(),
        }
    }
}

/// Grounded Flash-Lite search request matching Kennedy fast search.
#[derive(Clone, Debug, PartialEq)]
pub struct GroundedSearchRequest {
    /// Research question.
    pub question: String,
    /// Generation controls.
    pub options: GenerationOptions,
}

impl GroundedSearchRequest {
    /// Constructs a low-thinking Priority request with Gemini's output limit.
    pub fn new(question: impl Into<String>) -> Self {
        Self {
            question: question.into(),
            options: GenerationOptions {
                max_output_tokens: None,
                temperature: None,
                thinking_level: Some(ThinkingLevel::Low),
                service_tier: ServiceTier::Priority,
            },
        }
    }
}

/// Final interaction state returned by Gemini.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum CompletionStatus {
    /// The turn completed normally.
    Completed,
    /// The turn returned usable partial output.
    Incomplete,
}

/// A generated image returned by Nano Banana Pro.
#[derive(Clone, Eq, PartialEq)]
pub struct GeneratedImage {
    /// Returned image MIME type.
    pub mime_type: String,
    /// Decoded image bytes.
    pub data: Vec<u8>,
}

impl fmt::Debug for GeneratedImage {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("GeneratedImage")
            .field("mime_type", &self.mime_type)
            .field("bytes", &self.data.len())
            .finish()
    }
}

/// Provider modality used for token accounting.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum Modality {
    /// Text tokens.
    Text,
    /// Image tokens.
    Image,
    /// Audio tokens.
    Audio,
    /// Video tokens.
    Video,
    /// Document tokens, billed at image rates.
    Document,
    /// A provider modality unknown to this version.
    Unknown,
}

impl Modality {
    pub(crate) fn parse(value: &str) -> Self {
        match value {
            "text" => Self::Text,
            "image" => Self::Image,
            "audio" => Self::Audio,
            "video" => Self::Video,
            "document" => Self::Document,
            _ => Self::Unknown,
        }
    }
}

/// One modality-specific token count.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ModalityTokens {
    /// Token modality.
    pub modality: Modality,
    /// Token count.
    pub tokens: u64,
}

/// Token usage reported for one interaction.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct TokenUsage {
    /// Total effective input tokens.
    pub input_tokens: u64,
    /// Cached input tokens.
    pub cached_tokens: u64,
    /// Visible output tokens.
    pub output_tokens: u64,
    /// Internal thinking tokens billed at output rates.
    pub thought_tokens: u64,
    /// Tool-use prompt tokens reported separately.
    pub tool_use_tokens: u64,
    /// Total tokens reported by Gemini.
    pub total_tokens: u64,
    /// Input counts by modality.
    pub input_by_modality: Vec<ModalityTokens>,
    /// Cached input counts by modality.
    pub cached_by_modality: Vec<ModalityTokens>,
    /// Output counts by modality.
    pub output_by_modality: Vec<ModalityTokens>,
    /// Tool-use prompt counts by modality.
    pub tool_use_by_modality: Vec<ModalityTokens>,
    /// Reported Google Search query count.
    pub grounding_search_queries: u64,
}

impl TokenUsage {
    pub(crate) fn modality_total(values: &[ModalityTokens], modality: Modality) -> u64 {
        values
            .iter()
            .filter(|entry| entry.modality == modality)
            .map(|entry| entry.tokens)
            .sum()
    }

    pub(crate) fn saturating_add(&mut self, other: &Self) {
        self.input_tokens = self.input_tokens.saturating_add(other.input_tokens);
        self.cached_tokens = self.cached_tokens.saturating_add(other.cached_tokens);
        self.output_tokens = self.output_tokens.saturating_add(other.output_tokens);
        self.thought_tokens = self.thought_tokens.saturating_add(other.thought_tokens);
        self.tool_use_tokens = self.tool_use_tokens.saturating_add(other.tool_use_tokens);
        self.total_tokens = self.total_tokens.saturating_add(other.total_tokens);
        self.grounding_search_queries = self
            .grounding_search_queries
            .saturating_add(other.grounding_search_queries);
        add_modalities(&mut self.input_by_modality, &other.input_by_modality);
        add_modalities(&mut self.cached_by_modality, &other.cached_by_modality);
        add_modalities(&mut self.output_by_modality, &other.output_by_modality);
        add_modalities(&mut self.tool_use_by_modality, &other.tool_use_by_modality);
    }
}

fn add_modalities(target: &mut Vec<ModalityTokens>, source: &[ModalityTokens]) {
    for entry in source {
        if let Some(existing) = target
            .iter_mut()
            .find(|value| value.modality == entry.modality)
        {
            existing.tokens = existing.tokens.saturating_add(entry.tokens);
        } else {
            target.push(entry.clone());
        }
    }
}

/// Quality of a locally calculated cost.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum CostAccuracy {
    /// Complete provider usage mapped directly to published rates.
    Exact,
    /// Missing modality detail required a documented fallback.
    Estimated,
    /// Grounding cost assumes every reported query is chargeable.
    Conservative,
}

/// Locally calculated cost for one request or aggregate.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CostBreakdown {
    /// Non-cached input cost.
    pub input: Money,
    /// Cached input cost.
    pub cached_input: Money,
    /// Visible text output plus thinking cost.
    pub text_output_and_thinking: Money,
    /// Native image output cost.
    pub image_output: Money,
    /// Conservative Search grounding cost.
    pub grounding: Money,
    /// Sum of cost components.
    pub total: Money,
    /// Cost calculation quality.
    pub accuracy: CostAccuracy,
    /// Compiled pricing-table identifier.
    pub pricing_version: String,
}

impl Default for CostBreakdown {
    fn default() -> Self {
        Self {
            input: Money::ZERO,
            cached_input: Money::ZERO,
            text_output_and_thinking: Money::ZERO,
            image_output: Money::ZERO,
            grounding: Money::ZERO,
            total: Money::ZERO,
            accuracy: CostAccuracy::Exact,
            pricing_version: "google-gemini-2026-07-20".into(),
        }
    }
}

impl CostBreakdown {
    pub(crate) fn saturating_add(&mut self, other: &Self) {
        self.input = self.input.saturating_add(other.input);
        self.cached_input = self.cached_input.saturating_add(other.cached_input);
        self.text_output_and_thinking = self
            .text_output_and_thinking
            .saturating_add(other.text_output_and_thinking);
        self.image_output = self.image_output.saturating_add(other.image_output);
        self.grounding = self.grounding.saturating_add(other.grounding);
        self.total = self.total.saturating_add(other.total);
        if other.accuracy == CostAccuracy::Conservative {
            self.accuracy = CostAccuracy::Conservative;
        } else if other.accuracy == CostAccuracy::Estimated && self.accuracy == CostAccuracy::Exact
        {
            self.accuracy = CostAccuracy::Estimated;
        }
    }
}

/// A successful text or image interaction.
#[derive(Clone, Debug, PartialEq)]
pub struct InferenceResponse {
    /// Gemini interaction identifier.
    pub id: String,
    /// Actual model identifier returned by Gemini.
    pub model: String,
    /// Completion state.
    pub status: CompletionStatus,
    /// Concatenated text output, when present.
    pub text: Option<String>,
    /// Decoded native image output.
    pub images: Vec<GeneratedImage>,
    /// Provider-reported usage.
    pub usage: TokenUsage,
    /// Locally calculated cost.
    pub cost: CostBreakdown,
    /// In-memory session usage-record identifier.
    pub usage_record_id: u64,
}

/// One public source returned by grounded search.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct WebSource {
    /// Sanitized source title.
    pub title: String,
    /// Public HTTP(S) URL without a fragment.
    pub url: String,
}

/// Successful grounded-search output.
#[derive(Clone, Debug, PartialEq)]
pub struct GroundedSearchResponse {
    /// Normalized interaction response.
    pub interaction: InferenceResponse,
    /// Deduplicated citations and search results.
    pub sources: Vec<WebSource>,
}

pub(crate) fn validate_prompt(prompt: &str) -> Result<()> {
    if prompt.trim().is_empty() {
        return Err(Error::InvalidInput("prompt must not be empty".into()));
    }
    if prompt.len() > MAX_PROMPT_BYTES {
        return Err(Error::InvalidInput(format!(
            "prompt exceeds the {MAX_PROMPT_BYTES}-byte limit"
        )));
    }
    Ok(())
}

pub(crate) fn validate_system_instruction(value: Option<&str>) -> Result<()> {
    if let Some(value) = value {
        if value.trim().is_empty() {
            return Err(Error::InvalidInput(
                "system_instruction must be omitted instead of empty".into(),
            ));
        }
        if value.len() > MAX_PROMPT_BYTES {
            return Err(Error::InvalidInput(format!(
                "system_instruction exceeds the {MAX_PROMPT_BYTES}-byte limit"
            )));
        }
    }
    Ok(())
}

pub(crate) fn validate_media(media: &[MediaInput], required: bool) -> Result<()> {
    if required && media.is_empty() {
        return Err(Error::InvalidInput(
            "multimodal inference requires at least one media input".into(),
        ));
    }
    let total = media.iter().try_fold(0_usize, |total, input| {
        total
            .checked_add(input.len())
            .ok_or_else(|| Error::InvalidInput("aggregate media size overflowed".into()))
    })?;
    if total > MAX_INLINE_MEDIA_BYTES {
        return Err(Error::InvalidInput(format!(
            "aggregate inline media exceeds the {MAX_INLINE_MEDIA_BYTES}-byte safety limit"
        )));
    }
    Ok(())
}

pub(crate) fn validate_output_tokens(value: u32, maximum: u32) -> Result<()> {
    if value == 0 || value > maximum {
        return Err(Error::InvalidInput(format!(
            "max_output_tokens must be between 1 and {maximum}"
        )));
    }
    Ok(())
}

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

    const VIDEO_MIME_TYPES: &[&str] = &[
        "video/mp4",
        "video/mpeg",
        "video/mov",
        "video/quicktime",
        "video/avi",
        "video/x-flv",
        "video/mpg",
        "video/webm",
        "video/wmv",
        "video/3gpp",
    ];

    #[test]
    fn media_debug_redacts_bytes() {
        let media = MediaInput::video("video/mp4", b"sensitive bytes".to_vec()).unwrap();
        let debug = format!("{media:?}");
        assert!(debug.contains("Video"));
        assert!(debug.contains("video/mp4"));
        assert!(debug.contains("bytes: 15"));
        assert!(!debug.contains("sensitive"));
    }

    #[test]
    fn every_supported_video_mime_type_is_accepted() {
        for mime_type in VIDEO_MIME_TYPES {
            let media = MediaInput::video(*mime_type, vec![1]).unwrap();
            assert_eq!(media.kind(), MediaKind::Video);
            assert_eq!(media.mime_type(), *mime_type);
        }
    }

    #[test]
    fn invalid_or_empty_video_is_rejected() {
        assert!(MediaInput::video("application/octet-stream", vec![1]).is_err());
        assert!(MediaInput::video("Video/MP4", vec![1]).is_err());
        assert!(MediaInput::video("video/mp4", Vec::new()).is_err());
    }

    #[test]
    fn pro_rejects_minimal_thinking() {
        let options = GenerationOptions {
            thinking_level: Some(ThinkingLevel::Minimal),
            ..GenerationOptions::default()
        };
        assert!(options.validate(TextModel::Pro).is_err());
        assert!(options.validate(TextModel::FlashLite).is_ok());
    }

    #[test]
    fn video_uses_current_interactions_schema() {
        let value = MediaInput::video("video/mp4", vec![1, 2, 3])
            .unwrap()
            .interaction_value();
        assert_eq!(value["type"], "video");
        assert_eq!(value["mime_type"], "video/mp4");
        assert_eq!(value["data"], "AQID");
    }

    #[test]
    fn nano_banana_aspect_ratios_include_five_four_pair() {
        assert_eq!(AspectRatio::FiveFour.as_str(), "5:4");
        assert_eq!(AspectRatio::FourFive.as_str(), "4:5");
    }

    #[test]
    fn structured_output_requires_a_nonempty_schema_object() {
        assert!(StructuredOutput::new(json!({"type":"object"})).is_ok());
        assert!(StructuredOutput::new(json!({})).is_err());
        assert!(StructuredOutput::new(json!(["not", "a", "schema"])).is_err());
    }
}