gemini-rust 2.0.0

Rust client for Google Gemini API
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
use std::sync::Arc;
use tracing::{instrument, Span};

use crate::client::{Error as ClientError, GeminiClient};
use crate::interactions::model::*;
use crate::interactions::stream::InteractionStream;

/// Fluent builder for constructing and executing interaction requests.
#[derive(Clone)]
pub struct InteractionBuilder {
    client: Arc<GeminiClient>,
    model: Option<String>,
    agent: Option<String>,
    input: Option<InteractionInput>,
    system_instruction: Option<String>,
    tools: Vec<InteractionTool>,
    response_format: Option<ResponseFormat>,
    store: Option<bool>,
    background: bool,
    generation_config: Option<InteractionGenerationConfig>,
    agent_config: Option<AgentConfig>,
    previous_interaction_id: Option<String>,
    environment: Option<EnvironmentConfigOrString>,
    cached_content: Option<String>,
    response_modalities: Vec<ResponseModality>,
    service_tier: Option<ServiceTier>,
    webhook_config: Option<WebhookConfig>,
}

impl InteractionBuilder {
    pub(crate) fn new(client: Arc<GeminiClient>) -> Self {
        Self {
            client,
            model: None,
            agent: None,
            input: None,
            system_instruction: None,
            tools: Vec::new(),
            response_format: None,
            store: None,
            background: false,
            generation_config: None,
            agent_config: None,
            previous_interaction_id: None,
            environment: None,
            cached_content: None,
            response_modalities: Vec::new(),
            service_tier: None,
            webhook_config: None,
        }
    }

    // ===== Model / Agent =====

    /// Set the model (mutually exclusive with agent).
    pub fn with_model(mut self, model: impl Into<String>) -> Self {
        self.model = Some(model.into());
        self.agent = None;
        self
    }

    /// Set the agent (mutually exclusive with model).
    pub fn with_agent(mut self, agent: impl Into<String>) -> Self {
        self.agent = Some(agent.into());
        self.model = None;
        self
    }

    // ===== Input =====

    /// Set input from anything that implements `Into<InteractionInput>`.
    pub fn with_input(mut self, input: impl Into<InteractionInput>) -> Self {
        self.input = Some(input.into());
        self
    }

    /// Set input as plain text.
    pub fn with_text(mut self, text: impl Into<String>) -> Self {
        self.input = Some(InteractionInput::Text(text.into()));
        self
    }

    /// Set input as a content array (multimodal).
    pub fn with_content_input(mut self, content: Vec<InteractionContent>) -> Self {
        self.input = Some(InteractionInput::ContentArray(content));
        self
    }

    /// Set input as a step array (stateless multi-turn).
    pub fn with_step_input(mut self, steps: Vec<Step>) -> Self {
        self.input = Some(InteractionInput::StepArray(steps));
        self
    }

    /// Add image input.
    pub fn with_image(mut self, data: impl Into<String>, mime_type: ImageMimeType) -> Self {
        let content = InteractionContent::image(data, mime_type);
        self.input = match self.input {
            Some(InteractionInput::ContentArray(mut arr)) => {
                arr.push(content);
                Some(InteractionInput::ContentArray(arr))
            }
            _ => Some(InteractionInput::ContentArray(vec![content])),
        };
        self
    }

    /// Add image input from a URI.
    pub fn with_image_uri(mut self, uri: impl Into<String>, mime_type: ImageMimeType) -> Self {
        let content = InteractionContent::image_uri(uri, mime_type);
        self.input = match self.input {
            Some(InteractionInput::ContentArray(mut arr)) => {
                arr.push(content);
                Some(InteractionInput::ContentArray(arr))
            }
            _ => Some(InteractionInput::ContentArray(vec![content])),
        };
        self
    }

    /// Add audio input.
    pub fn with_audio(mut self, data: impl Into<String>, mime_type: AudioMimeType) -> Self {
        let content = InteractionContent::audio(data, mime_type);
        self.input = match self.input {
            Some(InteractionInput::ContentArray(mut arr)) => {
                arr.push(content);
                Some(InteractionInput::ContentArray(arr))
            }
            _ => Some(InteractionInput::ContentArray(vec![content])),
        };
        self
    }

    /// Add video input from a URI.
    pub fn with_video(mut self, uri: impl Into<String>) -> Self {
        let content = InteractionContent::video_uri(uri);
        self.input = match self.input {
            Some(InteractionInput::ContentArray(mut arr)) => {
                arr.push(content);
                Some(InteractionInput::ContentArray(arr))
            }
            _ => Some(InteractionInput::ContentArray(vec![content])),
        };
        self
    }

    /// Add document input.
    pub fn with_document(mut self, data: impl Into<String>, mime_type: DocumentMimeType) -> Self {
        let content = InteractionContent::document(data, mime_type);
        self.input = match self.input {
            Some(InteractionInput::ContentArray(mut arr)) => {
                arr.push(content);
                Some(InteractionInput::ContentArray(arr))
            }
            _ => Some(InteractionInput::ContentArray(vec![content])),
        };
        self
    }

    // ===== System Instruction =====

    /// Set the system instruction.
    pub fn with_system_instruction(mut self, instruction: impl Into<String>) -> Self {
        self.system_instruction = Some(instruction.into());
        self
    }

    // ===== Tools =====

    /// Add a tool.
    pub fn with_tool(mut self, tool: InteractionTool) -> Self {
        self.tools.push(tool);
        self
    }

    /// Add tools.
    pub fn with_tools(mut self, tools: Vec<InteractionTool>) -> Self {
        self.tools.extend(tools);
        self
    }

    /// Add a function declaration tool.
    pub fn with_function(
        mut self,
        name: impl Into<String>,
        description: impl Into<String>,
        parameters: serde_json::Value,
    ) -> Self {
        self.tools
            .push(InteractionTool::function(name, description, parameters));
        self
    }

    /// Enable Google Search.
    pub fn with_google_search(mut self) -> Self {
        self.tools.push(InteractionTool::google_search());
        self
    }

    /// Enable code execution.
    pub fn with_code_execution(mut self) -> Self {
        self.tools.push(InteractionTool::code_execution());
        self
    }

    /// Enable URL context.
    pub fn with_url_context(mut self) -> Self {
        self.tools.push(InteractionTool::url_context());
        self
    }

    /// Enable Google Maps.
    pub fn with_google_maps(mut self) -> Self {
        self.tools.push(InteractionTool::google_maps());
        self
    }

    /// Enable file search.
    pub fn with_file_search(mut self, store_names: Vec<String>) -> Self {
        self.tools.push(InteractionTool::file_search(store_names));
        self
    }

    /// Enable MCP Server.
    pub fn with_mcp_server(mut self, name: impl Into<String>, url: impl Into<String>) -> Self {
        self.tools.push(InteractionTool::mcp_server(name, url));
        self
    }

    /// Enable computer use.
    pub fn with_computer_use(mut self, environment: ComputerUseEnvironment) -> Self {
        self.tools.push(InteractionTool::computer_use(environment));
        self
    }

    /// Enable retrieval tool.
    pub fn with_retrieval(mut self, retrieval_types: Vec<RetrievalType>) -> Self {
        self.tools.push(InteractionTool::retrieval(retrieval_types));
        self
    }

    // ===== Response Format =====

    /// Set the response format.
    pub fn with_response_format(mut self, format: ResponseFormat) -> Self {
        self.response_format = Some(format);
        self
    }

    /// Set JSON structured output with a schema.
    pub fn with_json_schema(mut self, schema: serde_json::Value) -> Self {
        self.response_format = Some(ResponseFormat::json_schema(schema));
        self
    }

    // ===== Generation Config =====

    pub fn with_temperature(mut self, temperature: f64) -> Self {
        self.generation_config
            .get_or_insert_with(Default::default)
            .temperature = Some(temperature);
        self
    }

    pub fn with_top_p(mut self, top_p: f64) -> Self {
        self.generation_config
            .get_or_insert_with(Default::default)
            .top_p = Some(top_p);
        self
    }

    pub fn with_seed(mut self, seed: i64) -> Self {
        self.generation_config
            .get_or_insert_with(Default::default)
            .seed = Some(seed);
        self
    }

    pub fn with_stop_sequences(mut self, sequences: Vec<String>) -> Self {
        self.generation_config
            .get_or_insert_with(Default::default)
            .stop_sequences = sequences;
        self
    }

    pub fn with_max_output_tokens(mut self, tokens: i64) -> Self {
        self.generation_config
            .get_or_insert_with(Default::default)
            .max_output_tokens = Some(tokens);
        self
    }

    pub fn with_thinking_level(mut self, level: InteractionThinkingLevel) -> Self {
        self.generation_config
            .get_or_insert_with(Default::default)
            .thinking_level = Some(level);
        self
    }

    pub fn with_thinking_summaries(mut self, summaries: ThinkingSummaries) -> Self {
        self.generation_config
            .get_or_insert_with(Default::default)
            .thinking_summaries = Some(summaries);
        self
    }

    pub fn with_presence_penalty(mut self, penalty: f64) -> Self {
        self.generation_config
            .get_or_insert_with(Default::default)
            .presence_penalty = Some(penalty);
        self
    }

    pub fn with_frequency_penalty(mut self, penalty: f64) -> Self {
        self.generation_config
            .get_or_insert_with(Default::default)
            .frequency_penalty = Some(penalty);
        self
    }

    pub fn with_tool_choice(mut self, choice: ToolChoiceConfig) -> Self {
        self.generation_config
            .get_or_insert_with(Default::default)
            .tool_choice = Some(choice);
        self
    }

    pub fn with_speech_config(mut self, config: InteractionSpeechConfig) -> Self {
        self.generation_config
            .get_or_insert_with(Default::default)
            .speech_config
            .push(config);
        self
    }

    pub fn with_video_config(mut self, config: VideoConfig) -> Self {
        self.generation_config
            .get_or_insert_with(Default::default)
            .video_config = Some(config);
        self
    }

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

    // ===== Agent Config =====

    /// Set the agent config.
    pub fn with_agent_config(mut self, config: AgentConfig) -> Self {
        self.agent_config = Some(config);
        self
    }

    // ===== Interaction Options =====

    /// Set `previous_interaction_id` for server-side state management.
    pub fn with_previous_interaction(mut self, id: impl Into<String>) -> Self {
        self.previous_interaction_id = Some(id.into());
        self
    }

    /// Set `store` flag. Use `store(false)` for stateless mode.
    pub fn with_store(mut self, store: bool) -> Self {
        self.store = Some(store);
        self
    }

    /// Enable background execution.
    pub fn with_background(mut self) -> Self {
        self.background = true;
        self
    }

    /// Set the environment configuration.
    pub fn with_environment(mut self, env: EnvironmentConfig) -> Self {
        self.environment = Some(EnvironmentConfigOrString::Config(env));
        self
    }

    /// Set the environment by ID (reuse an existing environment).
    pub fn with_environment_id(mut self, env_id: impl Into<String>) -> Self {
        self.environment = Some(EnvironmentConfigOrString::Id(env_id.into()));
        self
    }

    /// Set cached content.
    pub fn with_cached_content(mut self, cached_content: impl Into<String>) -> Self {
        self.cached_content = Some(cached_content.into());
        self
    }

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

    /// Set the service tier.
    pub fn with_service_tier(mut self, tier: ServiceTier) -> Self {
        self.service_tier = Some(tier);
        self
    }

    /// Set the webhook configuration.
    pub fn with_webhook_config(mut self, config: WebhookConfig) -> Self {
        self.webhook_config = Some(config);
        self
    }

    // ===== Build & Execute =====

    /// Build the request.
    pub fn build(self) -> Result<CreateInteractionRequest, ClientError> {
        let input = self.input.ok_or_else(|| ClientError::InvalidResourceName {
            name: "input is required for interaction".to_string(),
        })?;

        let model = if self.model.is_none() && self.agent.is_none() {
            Some(
                self.client
                    .model
                    .as_str()
                    .trim_start_matches("models/")
                    .to_string(),
            )
        } else {
            self.model
        };

        Ok(CreateInteractionRequest {
            model,
            agent: self.agent,
            input,
            system_instruction: self.system_instruction,
            tools: self.tools,
            response_format: self.response_format,
            stream: None,
            store: self.store,
            background: if self.background { Some(true) } else { None },
            generation_config: self.generation_config,
            agent_config: self.agent_config,
            previous_interaction_id: self.previous_interaction_id,
            environment: self.environment,
            cached_content: self.cached_content,
            response_modalities: self.response_modalities,
            service_tier: self.service_tier,
            webhook_config: self.webhook_config,
        })
    }

    /// Execute the interaction (non-streaming).
    #[instrument(skip_all, fields(
        model = self.model.as_deref().unwrap_or(""),
        agent = self.agent.as_deref().unwrap_or(""),
        tools.count = self.tools.len(),
        system.instruction.present = self.system_instruction.is_some(),
        background = self.background,
        previous.interaction.present = self.previous_interaction_id.is_some(),
        status.code,
        usage.total_tokens,
    ))]
    pub async fn execute(self) -> Result<Interaction, ClientError> {
        let client = self.client.clone();
        let request = self.build()?;
        let response = client.create_interaction(request).await?;

        Span::current().record("status.code", response.status.as_ref());

        if let Some(usage) = &response.usage {
            Span::current().record("usage.total_tokens", usage.total_tokens);
        }

        Ok(response)
    }

    /// Execute the interaction (streaming).
    #[instrument(skip_all, fields(
        model = self.model.as_deref().unwrap_or(""),
        agent = self.agent.as_deref().unwrap_or(""),
        tools.count = self.tools.len(),
    ))]
    pub async fn execute_stream(self) -> Result<InteractionStream, ClientError> {
        let client = self.client.clone();
        let request = self.build()?;
        client.create_interaction_stream(request).await
    }
}