siumai 0.10.3

A unified LLM interface library for Rust
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
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
//! Google Gemini Types
//!
//! This module contains type definitions for Google Gemini API requests and responses.
//! Based on the Gemini `OpenAPI` specification.

use serde::{Deserialize, Serialize};

/// Gemini Generate Content Request
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GenerateContentRequest {
    /// Required. The name of the Model to use for generating the completion.
    pub model: String,
    /// Required. The content of the current conversation with the model.
    pub contents: Vec<Content>,
    /// Optional. Developer set system instructions.
    #[serde(skip_serializing_if = "Option::is_none", rename = "systemInstruction")]
    pub system_instruction: Option<Content>,
    /// Optional. A list of Tools the Model may use to generate the next response.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tools: Option<Vec<GeminiTool>>,
    /// Optional. Tool configuration for any Tool specified in the request.
    #[serde(skip_serializing_if = "Option::is_none", rename = "toolConfig")]
    pub tool_config: Option<ToolConfig>,
    /// Optional. A list of unique `SafetySetting` instances for blocking unsafe content.
    #[serde(skip_serializing_if = "Option::is_none", rename = "safetySettings")]
    pub safety_settings: Option<Vec<SafetySetting>>,
    /// Optional. Configuration options for model generation and outputs.
    #[serde(skip_serializing_if = "Option::is_none", rename = "generationConfig")]
    pub generation_config: Option<GenerationConfig>,
    /// Optional. The name of the content cached to use as context.
    #[serde(skip_serializing_if = "Option::is_none", rename = "cachedContent")]
    pub cached_content: Option<String>,
}

/// Gemini Generate Content Response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GenerateContentResponse {
    /// Candidate responses from the model.
    #[serde(default)]
    pub candidates: Vec<Candidate>,
    /// Returns the prompt's feedback related to the content filters.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub prompt_feedback: Option<PromptFeedback>,
    /// Output only. Metadata on the generation requests' token usage.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub usage_metadata: Option<UsageMetadata>,
    /// Output only. The model version used to generate the response.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub model_version: Option<String>,
    /// Output only. `response_id` is used to identify each response.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub response_id: Option<String>,
}

/// The base structured datatype containing multi-part content of a message.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Content {
    /// Optional. The producer of the content. Must be either 'user' or 'model'.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub role: Option<String>,
    /// Ordered Parts that constitute a single message.
    pub parts: Vec<Part>,
}

/// A datatype containing media that is part of a multi-part Content message.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Part {
    /// Text content
    Text {
        text: String,
        /// Optional. Whether this is a thought summary (for thinking models)
        #[serde(skip_serializing_if = "Option::is_none")]
        thought: Option<bool>,
    },
    /// Inline data (images, audio, etc.)
    InlineData {
        #[serde(rename = "inlineData")]
        inline_data: Blob,
    },
    /// File data
    FileData {
        #[serde(rename = "fileData")]
        file_data: FileData,
    },
    /// Function call
    FunctionCall {
        #[serde(rename = "functionCall")]
        function_call: FunctionCall,
    },
    /// Function response
    FunctionResponse {
        #[serde(rename = "functionResponse")]
        function_response: FunctionResponse,
    },
    /// Executable code
    ExecutableCode {
        #[serde(rename = "executableCode")]
        executable_code: ExecutableCode,
    },
    /// Code execution result
    CodeExecutionResult {
        #[serde(rename = "codeExecutionResult")]
        code_execution_result: CodeExecutionResult,
    },
}

/// Raw media bytes with MIME type.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Blob {
    /// The IANA standard MIME type of the source data.
    pub mime_type: String,
    /// Raw bytes for media formats.
    pub data: String, // Base64 encoded
}

/// URI based data.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileData {
    /// Required. URI.
    pub file_uri: String,
    /// Optional. The IANA standard MIME type of the source data.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mime_type: Option<String>,
}

/// A predicted `FunctionCall` returned from the model.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FunctionCall {
    /// Required. The name of the function to call.
    pub name: String,
    /// Optional. The function parameters and values in JSON object format.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub args: Option<serde_json::Value>,
}

/// The result output from a `FunctionCall`.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FunctionResponse {
    /// Required. The name of the function to call.
    pub name: String,
    /// Required. The function response in JSON object format.
    pub response: serde_json::Value,
}

/// Code generated by the model that is meant to be executed.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExecutableCode {
    /// Required. Programming language of the code.
    pub language: CodeLanguage,
    /// Required. The code to be executed.
    pub code: String,
}

/// Programming language enumeration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum CodeLanguage {
    #[serde(rename = "LANGUAGE_UNSPECIFIED")]
    Unspecified,
    #[serde(rename = "PYTHON")]
    Python,
}

/// Result of executing the `ExecutableCode`.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CodeExecutionResult {
    /// Required. Outcome of the code execution.
    pub outcome: CodeExecutionOutcome,
    /// Optional. Contains stdout when code execution is successful, stderr or other description otherwise.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub output: Option<String>,
}

/// Code execution outcome enumeration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum CodeExecutionOutcome {
    #[serde(rename = "OUTCOME_UNSPECIFIED")]
    Unspecified,
    #[serde(rename = "OUTCOME_OK")]
    Ok,
    #[serde(rename = "OUTCOME_FAILED")]
    Failed,
    #[serde(rename = "OUTCOME_DEADLINE_EXCEEDED")]
    DeadlineExceeded,
}

/// A candidate response generated by the model.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Candidate {
    /// Output only. Generated content returned from the model.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub content: Option<Content>,
    /// Optional. Output only. The reason why the model stopped generating tokens.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub finish_reason: Option<FinishReason>,
    /// List of ratings for the safety of a response candidate.
    #[serde(default)]
    pub safety_ratings: Vec<SafetyRating>,
    /// Output only. Citation information for model-generated candidate.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub citation_metadata: Option<CitationMetadata>,
    /// Output only. Token count for this candidate.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub token_count: Option<i32>,
    /// Output only. Index of the candidate in the list of candidates.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub index: Option<i32>,
}

/// Defines the reason why the model stopped generating tokens.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum FinishReason {
    #[serde(rename = "FINISH_REASON_UNSPECIFIED")]
    Unspecified,
    #[serde(rename = "STOP")]
    Stop,
    #[serde(rename = "MAX_TOKENS")]
    MaxTokens,
    #[serde(rename = "SAFETY")]
    Safety,
    #[serde(rename = "RECITATION")]
    Recitation,
    #[serde(rename = "LANGUAGE")]
    Language,
    #[serde(rename = "OTHER")]
    Other,
    #[serde(rename = "BLOCKLIST")]
    Blocklist,
    #[serde(rename = "PROHIBITED_CONTENT")]
    ProhibitedContent,
    #[serde(rename = "SPII")]
    Spii,
    #[serde(rename = "MALFORMED_FUNCTION_CALL")]
    MalformedFunctionCall,
}

/// A collection of source attributions for a piece of content.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CitationMetadata {
    /// Citations to sources for a specific response.
    #[serde(default)]
    pub citation_sources: Vec<CitationSource>,
}

/// A citation to a source for a portion of a specific response.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CitationSource {
    /// Optional. Start of segment of the response that is attributed to this source.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub start_index: Option<i32>,
    /// Optional. End of the attributed segment, exclusive.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub end_index: Option<i32>,
    /// Optional. URI that is attributed as a source for a portion of the text.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub uri: Option<String>,
    /// Optional. License for the GitHub project that is attributed as a source for segment.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub license: Option<String>,
}

/// Safety rating for a piece of content.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SafetyRating {
    /// Required. The category for this rating.
    pub category: HarmCategory,
    /// Required. The probability of harm for this content.
    pub probability: HarmProbability,
    /// Was this content blocked because of this rating?
    #[serde(skip_serializing_if = "Option::is_none")]
    pub blocked: Option<bool>,
}

/// The category of a rating.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum HarmCategory {
    #[serde(rename = "HARM_CATEGORY_UNSPECIFIED")]
    Unspecified,
    #[serde(rename = "HARM_CATEGORY_DEROGATORY")]
    Derogatory,
    #[serde(rename = "HARM_CATEGORY_TOXICITY")]
    Toxicity,
    #[serde(rename = "HARM_CATEGORY_VIOLENCE")]
    Violence,
    #[serde(rename = "HARM_CATEGORY_SEXUAL")]
    Sexual,
    #[serde(rename = "HARM_CATEGORY_MEDICAL")]
    Medical,
    #[serde(rename = "HARM_CATEGORY_DANGEROUS")]
    Dangerous,
    #[serde(rename = "HARM_CATEGORY_HARASSMENT")]
    Harassment,
    #[serde(rename = "HARM_CATEGORY_HATE_SPEECH")]
    HateSpeech,
    #[serde(rename = "HARM_CATEGORY_SEXUALLY_EXPLICIT")]
    SexuallyExplicit,
    #[serde(rename = "HARM_CATEGORY_DANGEROUS_CONTENT")]
    DangerousContent,
    #[serde(rename = "HARM_CATEGORY_CIVIC_INTEGRITY")]
    CivicIntegrity,
}

/// The probability that a piece of content is harmful.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum HarmProbability {
    #[serde(rename = "HARM_PROBABILITY_UNSPECIFIED")]
    Unspecified,
    #[serde(rename = "NEGLIGIBLE")]
    Negligible,
    #[serde(rename = "LOW")]
    Low,
    #[serde(rename = "MEDIUM")]
    Medium,
    #[serde(rename = "HIGH")]
    High,
}

/// Safety setting, affecting the safety-blocking behavior.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SafetySetting {
    /// Required. The category for this setting.
    pub category: HarmCategory,
    /// Required. Controls the probability threshold at which harm is blocked.
    pub threshold: HarmBlockThreshold,
}

/// Block at and beyond a specified harm probability.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum HarmBlockThreshold {
    #[serde(rename = "HARM_BLOCK_THRESHOLD_UNSPECIFIED")]
    Unspecified,
    #[serde(rename = "BLOCK_LOW_AND_ABOVE")]
    BlockLowAndAbove,
    #[serde(rename = "BLOCK_MEDIUM_AND_ABOVE")]
    BlockMediumAndAbove,
    #[serde(rename = "BLOCK_ONLY_HIGH")]
    BlockOnlyHigh,
    #[serde(rename = "BLOCK_NONE")]
    BlockNone,
}

/// Configuration options for model generation and outputs.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GenerationConfig {
    /// Optional. Number of generated responses to return.
    #[serde(skip_serializing_if = "Option::is_none", rename = "candidateCount")]
    pub candidate_count: Option<i32>,
    /// Optional. The set of character sequences that will stop output generation.
    #[serde(skip_serializing_if = "Option::is_none", rename = "stopSequences")]
    pub stop_sequences: Option<Vec<String>>,
    /// Optional. The maximum number of tokens to include in a candidate.
    #[serde(skip_serializing_if = "Option::is_none", rename = "maxOutputTokens")]
    pub max_output_tokens: Option<i32>,
    /// Optional. Controls the randomness of the output.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub temperature: Option<f32>,
    /// Optional. The maximum cumulative probability of tokens to consider when sampling.
    #[serde(skip_serializing_if = "Option::is_none", rename = "topP")]
    pub top_p: Option<f32>,
    /// Optional. The maximum number of tokens to consider when sampling.
    #[serde(skip_serializing_if = "Option::is_none", rename = "topK")]
    pub top_k: Option<i32>,
    /// Optional. Output response mimetype of the generated candidate text.
    #[serde(skip_serializing_if = "Option::is_none", rename = "responseMimeType")]
    pub response_mime_type: Option<String>,
    /// Optional. Output response schema of the generated candidate text.
    #[serde(skip_serializing_if = "Option::is_none", rename = "responseSchema")]
    pub response_schema: Option<serde_json::Value>,
    /// Optional. Configuration for thinking behavior.
    #[serde(skip_serializing_if = "Option::is_none", rename = "thinkingConfig")]
    pub thinking_config: Option<ThinkingConfig>,
    /// Optional. Output response modalities (e.g., ["TEXT", "IMAGE"]).
    #[serde(skip_serializing_if = "Option::is_none", rename = "responseModalities")]
    pub response_modalities: Option<Vec<String>>,
}

/// Configuration for thinking behavior in Gemini models.
///
/// Note: Different models have different thinking capabilities. The API will
/// return appropriate errors if unsupported configurations are used.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ThinkingConfig {
    /// Thinking budget in tokens.
    /// - Set to -1 for dynamic thinking (model decides when and how much to think)
    /// - Set to 0 to attempt to disable thinking (may not work on all models)
    /// - Set to specific value to limit thinking tokens
    ///
    /// The actual supported range depends on the specific model being used.
    #[serde(skip_serializing_if = "Option::is_none", rename = "thinkingBudget")]
    pub thinking_budget: Option<i32>,

    /// Whether to include thought summaries in the response.
    /// This controls the visibility of thinking summaries, not the thinking process itself.
    #[serde(skip_serializing_if = "Option::is_none", rename = "includeThoughts")]
    pub include_thoughts: Option<bool>,
}

/// A set of the feedback metadata the prompt specified in GenerateContentRequest.content.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PromptFeedback {
    /// Optional. If set, the prompt was blocked and no candidates are returned.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub block_reason: Option<BlockReason>,
    /// Ratings for safety of the prompt.
    #[serde(default)]
    pub safety_ratings: Vec<SafetyRating>,
}

/// Specifies what was the reason why prompt was blocked.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum BlockReason {
    #[serde(rename = "BLOCK_REASON_UNSPECIFIED")]
    Unspecified,
    #[serde(rename = "SAFETY")]
    Safety,
    #[serde(rename = "OTHER")]
    Other,
    #[serde(rename = "BLOCKLIST")]
    Blocklist,
    #[serde(rename = "PROHIBITED_CONTENT")]
    ProhibitedContent,
    #[serde(rename = "IMAGE_SAFETY")]
    ImageSafety,
}

/// Metadata on the generation requests' token usage.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UsageMetadata {
    /// Number of tokens in the prompt.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub prompt_token_count: Option<i32>,
    /// Total token count for the generation request (prompt + response candidates).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub total_token_count: Option<i32>,
    /// Number of tokens in the cached part of the prompt.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cached_content_token_count: Option<i32>,
    /// Number of tokens in the response candidate.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub candidates_token_count: Option<i32>,
    /// Number of tokens used for thinking (only for thinking models).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thoughts_token_count: Option<i32>,
}

/// Tool details that the model may use to generate response.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum GeminiTool {
    /// Function calling tool
    FunctionDeclarations {
        function_declarations: Vec<FunctionDeclaration>,
    },
    /// Code execution tool
    CodeExecution { code_execution: CodeExecution },
}

/// Structured representation of a function declaration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FunctionDeclaration {
    /// Required. The name of the function.
    pub name: String,
    /// Required. A brief description of the function.
    pub description: String,
    /// Optional. Describes the parameters to this function.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parameters: Option<serde_json::Value>,
    /// Optional. Describes the output from this function in JSON Schema format.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub response: Option<serde_json::Value>,
}

/// Tool that executes code generated by the model.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CodeExecution {
    // This is an empty object in the API spec
}

/// Tool configuration for any Tool specified in the request.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolConfig {
    /// Optional. Function calling config.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub function_calling_config: Option<FunctionCallingConfig>,
}

/// Configuration for specifying function calling behavior.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FunctionCallingConfig {
    /// Optional. Specifies the mode in which function calling should execute.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mode: Option<FunctionCallingMode>,
    /// Optional. A set of function names that, when provided, limits the functions the model will call.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub allowed_function_names: Option<Vec<String>>,
}

/// Defines the execution behavior for function calling.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum FunctionCallingMode {
    #[serde(rename = "MODE_UNSPECIFIED")]
    Unspecified,
    #[serde(rename = "AUTO")]
    Auto,
    #[serde(rename = "ANY")]
    Any,
    #[serde(rename = "NONE")]
    None,
}

/// Gemini-specific configuration parameters
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GeminiConfig {
    /// API key for authentication
    pub api_key: String,
    /// Base URL for the Gemini API
    pub base_url: String,
    /// Default model to use
    pub model: String,
    /// Default generation configuration
    #[serde(skip_serializing_if = "Option::is_none")]
    pub generation_config: Option<GenerationConfig>,
    /// Default safety settings
    #[serde(skip_serializing_if = "Option::is_none")]
    pub safety_settings: Option<Vec<SafetySetting>>,
    /// HTTP timeout in seconds
    #[serde(skip_serializing_if = "Option::is_none")]
    pub timeout: Option<u64>,
}

impl Default for GeminiConfig {
    fn default() -> Self {
        Self {
            api_key: String::new(),
            base_url: "https://generativelanguage.googleapis.com/v1beta".to_string(),
            model: "gemini-1.5-flash".to_string(),
            generation_config: None,
            safety_settings: None,
            timeout: Some(30),
        }
    }
}

impl GeminiConfig {
    /// Create a new Gemini configuration with the given API key
    pub fn new(api_key: String) -> Self {
        Self {
            api_key,
            ..Default::default()
        }
    }

    /// Set the model to use
    pub fn with_model(mut self, model: String) -> Self {
        self.model = model;
        self
    }

    /// Set the base URL
    pub fn with_base_url(mut self, base_url: String) -> Self {
        self.base_url = base_url;
        self
    }

    /// Set generation configuration
    pub fn with_generation_config(mut self, config: GenerationConfig) -> Self {
        self.generation_config = Some(config);
        self
    }

    /// Set safety settings
    pub fn with_safety_settings(mut self, settings: Vec<SafetySetting>) -> Self {
        self.safety_settings = Some(settings);
        self
    }

    /// Set HTTP timeout
    pub const fn with_timeout(mut self, timeout: u64) -> Self {
        self.timeout = Some(timeout);
        self
    }
}

impl GenerationConfig {
    /// Create a new generation configuration
    pub const fn new() -> Self {
        Self {
            candidate_count: None,
            stop_sequences: None,
            max_output_tokens: None,
            temperature: None,
            top_p: None,
            top_k: None,
            response_mime_type: None,
            response_schema: None,
            thinking_config: None,
            response_modalities: None,
        }
    }

    /// Set the number of candidates to generate
    pub const fn with_candidate_count(mut self, count: i32) -> Self {
        self.candidate_count = Some(count);
        self
    }

    /// Set stop sequences
    pub fn with_stop_sequences(mut self, sequences: Vec<String>) -> Self {
        self.stop_sequences = Some(sequences);
        self
    }

    /// Set maximum output tokens
    pub const fn with_max_output_tokens(mut self, tokens: i32) -> Self {
        self.max_output_tokens = Some(tokens);
        self
    }

    /// Set temperature
    pub const fn with_temperature(mut self, temperature: f32) -> Self {
        self.temperature = Some(temperature);
        self
    }

    /// Set top-p
    pub const fn with_top_p(mut self, top_p: f32) -> Self {
        self.top_p = Some(top_p);
        self
    }

    /// Set top-k
    pub const fn with_top_k(mut self, top_k: i32) -> Self {
        self.top_k = Some(top_k);
        self
    }

    /// Set response MIME type
    pub fn with_response_mime_type(mut self, mime_type: String) -> Self {
        self.response_mime_type = Some(mime_type);
        self
    }

    /// Set response schema
    pub fn with_response_schema(mut self, schema: serde_json::Value) -> Self {
        self.response_schema = Some(schema);
        self
    }

    /// Set thinking configuration
    pub const fn with_thinking_config(mut self, config: ThinkingConfig) -> Self {
        self.thinking_config = Some(config);
        self
    }

    /// Set response modalities
    pub fn with_response_modalities(mut self, modalities: Vec<String>) -> Self {
        self.response_modalities = Some(modalities);
        self
    }
}

impl Default for GenerationConfig {
    fn default() -> Self {
        Self::new()
    }
}

impl ThinkingConfig {
    /// Create a new thinking configuration
    pub const fn new() -> Self {
        Self {
            thinking_budget: None,
            include_thoughts: None,
        }
    }

    /// Create thinking configuration with specific budget
    pub const fn with_budget(budget: i32) -> Self {
        Self {
            thinking_budget: Some(budget),
            include_thoughts: None,
        }
    }

    /// Create thinking configuration with thought summaries enabled
    pub const fn with_thoughts() -> Self {
        Self {
            thinking_budget: None,
            include_thoughts: Some(true),
        }
    }

    /// Create dynamic thinking configuration (model decides budget)
    pub const fn dynamic() -> Self {
        Self {
            thinking_budget: Some(-1),
            include_thoughts: Some(true),
        }
    }

    /// Create configuration that attempts to disable thinking
    /// Note: Not all models support disabling thinking
    pub const fn disabled() -> Self {
        Self {
            thinking_budget: Some(0),
            include_thoughts: Some(false),
        }
    }

    /// Basic validation (only check for obviously invalid values)
    pub fn validate(&self) -> Result<(), String> {
        if let Some(budget) = self.thinking_budget
            && budget < -1
        {
            return Err("Thinking budget cannot be less than -1".to_string());
        }
        Ok(())
    }
}

impl Default for ThinkingConfig {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn test_thinking_config_json_serialization() {
        let thinking_config = ThinkingConfig {
            thinking_budget: Some(-1),
            include_thoughts: Some(true),
        };

        let json = serde_json::to_string(&thinking_config).unwrap();
        println!("ThinkingConfig JSON: {}", json);

        // Verify the JSON contains the correct field names
        assert!(json.contains("thinkingBudget"));
        assert!(json.contains("includeThoughts"));
        assert!(!json.contains("thinking_budget"));
        assert!(!json.contains("include_thoughts"));
    }

    #[test]
    fn test_generation_config_json_serialization() {
        let thinking_config = ThinkingConfig {
            thinking_budget: Some(-1),
            include_thoughts: Some(true),
        };

        let generation_config = GenerationConfig {
            candidate_count: Some(1),
            stop_sequences: None,
            max_output_tokens: Some(1000),
            temperature: Some(0.7),
            top_p: Some(0.9),
            top_k: Some(40),
            response_mime_type: None,
            response_schema: None,
            thinking_config: Some(thinking_config),
            response_modalities: None,
        };

        let json = serde_json::to_string(&generation_config).unwrap();
        println!("GenerationConfig JSON: {}", json);

        // Verify the JSON contains the correct field names
        assert!(json.contains("candidateCount"));
        assert!(json.contains("maxOutputTokens"));
        assert!(json.contains("topP"));
        assert!(json.contains("topK"));
        assert!(json.contains("thinkingConfig"));
        assert!(json.contains("thinkingBudget"));
        assert!(json.contains("includeThoughts"));
    }
}

impl SafetySetting {
    /// Create a new safety setting
    pub const fn new(category: HarmCategory, threshold: HarmBlockThreshold) -> Self {
        Self {
            category,
            threshold,
        }
    }

    /// Create a safety setting that blocks low and above
    pub const fn block_low_and_above(category: HarmCategory) -> Self {
        Self::new(category, HarmBlockThreshold::BlockLowAndAbove)
    }

    /// Create a safety setting that blocks medium and above
    pub const fn block_medium_and_above(category: HarmCategory) -> Self {
        Self::new(category, HarmBlockThreshold::BlockMediumAndAbove)
    }

    /// Create a safety setting that blocks only high
    pub const fn block_only_high(category: HarmCategory) -> Self {
        Self::new(category, HarmBlockThreshold::BlockOnlyHigh)
    }

    /// Create a safety setting that blocks none
    pub const fn block_none(category: HarmCategory) -> Self {
        Self::new(category, HarmBlockThreshold::BlockNone)
    }
}

impl Content {
    /// Create new content with the given role and parts
    pub const fn new(role: Option<String>, parts: Vec<Part>) -> Self {
        Self { role, parts }
    }

    /// Create user content with text
    pub fn user_text(text: String) -> Self {
        Self {
            role: Some("user".to_string()),
            parts: vec![Part::Text {
                text,
                thought: None,
            }],
        }
    }

    /// Create model content with text
    pub fn model_text(text: String) -> Self {
        Self {
            role: Some("model".to_string()),
            parts: vec![Part::Text {
                text,
                thought: None,
            }],
        }
    }

    /// Create system content with text
    pub fn system_text(text: String) -> Self {
        Self {
            role: None, // System instructions don't have a role
            parts: vec![Part::Text {
                text,
                thought: None,
            }],
        }
    }
}

impl Part {
    /// Create a text part
    pub const fn text(text: String) -> Self {
        Self::Text {
            text,
            thought: None,
        }
    }

    /// Create a thought summary part
    pub const fn thought_summary(text: String) -> Self {
        Self::Text {
            text,
            thought: Some(true),
        }
    }

    /// Create an inline data part
    pub const fn inline_data(mime_type: String, data: String) -> Self {
        Self::InlineData {
            inline_data: Blob { mime_type, data },
        }
    }

    /// Create a file data part
    pub const fn file_data(file_uri: String, mime_type: Option<String>) -> Self {
        Self::FileData {
            file_data: FileData {
                file_uri,
                mime_type,
            },
        }
    }

    /// Create a function call part
    pub const fn function_call(name: String, args: Option<serde_json::Value>) -> Self {
        Self::FunctionCall {
            function_call: FunctionCall { name, args },
        }
    }

    /// Create a function response part
    pub const fn function_response(name: String, response: serde_json::Value) -> Self {
        Self::FunctionResponse {
            function_response: FunctionResponse { name, response },
        }
    }
}

// File management types

/// Gemini File object
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GeminiFile {
    /// Immutable. Identifier. The File resource name.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// Optional. The human-readable display name for the File.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub display_name: Option<String>,
    /// Output only. MIME type of the file.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mime_type: Option<String>,
    /// Output only. Size of the file in bytes.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub size_bytes: Option<String>,
    /// Output only. The timestamp of when the File was created.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub create_time: Option<String>,
    /// Output only. The timestamp of when the File was last updated.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub update_time: Option<String>,
    /// Output only. The timestamp of when the File will be deleted.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub expiration_time: Option<String>,
    /// Output only. SHA-256 hash of the uploaded bytes.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sha256_hash: Option<String>,
    /// Output only. The uri of the File.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub uri: Option<String>,
    /// Output only. Processing state of the File.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub state: Option<GeminiFileState>,
    /// Output only. Error status if File processing failed.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<GeminiStatus>,
    /// Output only. Metadata for a video.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub video_metadata: Option<VideoFileMetadata>,
}

/// Processing state of the File
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum GeminiFileState {
    #[serde(rename = "STATE_UNSPECIFIED")]
    Unspecified,
    #[serde(rename = "PROCESSING")]
    Processing,
    #[serde(rename = "ACTIVE")]
    Active,
    #[serde(rename = "FAILED")]
    Failed,
}

/// Error status for file processing
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GeminiStatus {
    /// The status code
    #[serde(skip_serializing_if = "Option::is_none")]
    pub code: Option<i32>,
    /// A developer-facing error message
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message: Option<String>,
    /// A list of messages that carry the error details
    #[serde(skip_serializing_if = "Option::is_none")]
    pub details: Option<Vec<serde_json::Value>>,
}

/// Metadata for a video file
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VideoFileMetadata {
    /// Duration of the video
    #[serde(skip_serializing_if = "Option::is_none")]
    pub video_duration: Option<String>,
}

/// Request for `CreateFile`
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateFileRequest {
    /// Optional. Metadata for the file to create.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub file: Option<GeminiFile>,
}

/// Response for `CreateFile`
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateFileResponse {
    /// Metadata for the created file.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub file: Option<GeminiFile>,
}

/// Response for `ListFiles`
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ListFilesResponse {
    /// The list of Files.
    #[serde(default)]
    pub files: Vec<GeminiFile>,
    /// A token that can be sent as `page_token` into a subsequent `ListFiles` call.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub next_page_token: Option<String>,
}

/// Response for `DownloadFile`
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DownloadFileResponse {
    // This is typically just raw bytes, but we'll handle it in the implementation
}

// ================================================================================================
// Embedding Types
// ================================================================================================

/// Gemini-specific embedding configuration options
///
/// This struct provides type-safe configuration for Gemini embedding requests,
/// including task type optimization, context titles, and custom dimensions.
///
/// # Example
/// ```rust,no_run
/// use siumai::providers::gemini::GeminiEmbeddingOptions;
/// use siumai::types::EmbeddingTaskType;
///
/// let options = GeminiEmbeddingOptions::new()
///     .with_task_type(EmbeddingTaskType::RetrievalQuery)
///     .with_title("Search Context")
///     .with_output_dimensionality(768);
/// ```
#[derive(Debug, Clone, Default)]
pub struct GeminiEmbeddingOptions {
    /// Task type for optimization (Gemini-specific feature)
    pub task_type: Option<crate::types::EmbeddingTaskType>,
    /// Title for additional context (helps with embedding quality)
    pub title: Option<String>,
    /// Custom output dimensions (128-3072, must be supported by model)
    pub output_dimensionality: Option<u32>,
}

impl GeminiEmbeddingOptions {
    /// Create new Gemini embedding options with default values
    pub fn new() -> Self {
        Self::default()
    }

    /// Set task type for optimization
    ///
    /// Different task types optimize the embedding for specific use cases:
    /// - `RetrievalQuery`: For search queries
    /// - `RetrievalDocument`: For documents to be searched
    /// - `SemanticSimilarity`: For similarity comparison
    /// - `Classification`: For text classification
    /// - `Clustering`: For grouping similar texts
    /// - `QuestionAnswering`: For Q&A systems
    /// - `FactVerification`: For fact checking
    pub fn with_task_type(mut self, task_type: crate::types::EmbeddingTaskType) -> Self {
        self.task_type = Some(task_type);
        self
    }

    /// Set title for additional context
    ///
    /// The title provides additional context that can improve embedding quality.
    /// This is particularly useful for documents or when the text needs context.
    pub fn with_title(mut self, title: impl Into<String>) -> Self {
        self.title = Some(title.into());
        self
    }

    /// Set custom output dimensions
    ///
    /// Allows customizing the embedding vector size. Must be between 128-3072
    /// and supported by the specific model being used.
    pub fn with_output_dimensionality(mut self, dimensions: u32) -> Self {
        self.output_dimensionality = Some(dimensions);
        self
    }

    /// Apply these options to an EmbeddingRequest
    ///
    /// This method modifies the provided EmbeddingRequest to include
    /// Gemini-specific parameters.
    pub fn apply_to_request(
        self,
        mut request: crate::types::EmbeddingRequest,
    ) -> crate::types::EmbeddingRequest {
        if let Some(task_type) = self.task_type {
            request = request.with_task_type(task_type);
        }
        if let Some(title) = self.title {
            request = request.with_provider_param("title", serde_json::Value::String(title));
        }
        if let Some(dims) = self.output_dimensionality {
            request.dimensions = Some(dims);
        }
        request
    }
}

/// Extension trait for EmbeddingRequest to add Gemini-specific configuration
pub trait GeminiEmbeddingRequestExt {
    /// Configure this request with Gemini-specific options
    ///
    /// # Example
    /// ```rust,no_run
    /// use siumai::types::EmbeddingRequest;
    /// use siumai::providers::gemini::{GeminiEmbeddingOptions, GeminiEmbeddingRequestExt};
    /// use siumai::types::EmbeddingTaskType;
    ///
    /// let request = EmbeddingRequest::new(vec!["text".to_string()])
    ///     .with_gemini_config(
    ///         GeminiEmbeddingOptions::new()
    ///             .with_task_type(EmbeddingTaskType::RetrievalQuery)
    ///             .with_title("Search Context")
    ///     );
    /// ```
    fn with_gemini_config(self, config: GeminiEmbeddingOptions) -> Self;

    /// Quick method to set Gemini task type
    fn with_gemini_task_type(self, task_type: crate::types::EmbeddingTaskType) -> Self;

    /// Quick method to set Gemini title
    fn with_gemini_title(self, title: impl Into<String>) -> Self;

    /// Quick method to set Gemini output dimensions
    fn with_gemini_dimensions(self, dimensions: u32) -> Self;
}

impl GeminiEmbeddingRequestExt for crate::types::EmbeddingRequest {
    fn with_gemini_config(self, config: GeminiEmbeddingOptions) -> Self {
        config.apply_to_request(self)
    }

    fn with_gemini_task_type(self, task_type: crate::types::EmbeddingTaskType) -> Self {
        self.with_task_type(task_type)
    }

    fn with_gemini_title(self, title: impl Into<String>) -> Self {
        self.with_provider_param("title", serde_json::Value::String(title.into()))
    }

    fn with_gemini_dimensions(self, dimensions: u32) -> Self {
        let mut request = self;
        request.dimensions = Some(dimensions);
        request
    }
}