omniference 0.1.8

A multi-protocol inference engine with provider adapters
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
use crate::{
	adapter::{AdapterError, ChatAdapter},
	stream::*,
	types::*,
};
use async_trait::async_trait;
use futures_util::StreamExt;
use std::collections::HashMap;
use tokio_util::sync::CancellationToken;

pub struct GeminiAdapter;

// ChatAdapter Implementation
// ============================================================================

#[async_trait]
impl ChatAdapter for GeminiAdapter {
	fn provider_kind(&self) -> ProviderKind {
		ProviderKind::Google
	}

	async fn discover_models(&self, provider_name: &str, endpoint: &ProviderEndpoint) -> Result<Vec<DiscoveredModel>, AdapterError> {
		let client = reqwest::Client::new();

		let base_url = endpoint.base_url.trim_end_matches('/');
		let mut url = format!("{}/v1beta/models", base_url);

		if let Some(api_key) = &endpoint.api_key {
			url = format!("{}?key={}", url, api_key);
		}

		let mut request = client.get(&url);

		if let Some(timeout) = endpoint.timeout {
			request = request.timeout(std::time::Duration::from_millis(timeout));
		}

		for (key, value) in &endpoint.extra_headers {
			request = request.header(key, value);
		}

		let resp = request.send().await.map_err(|e| AdapterError::Http(format!("Failed to fetch models: {}", e)))?;

		if !resp.status().is_success() {
			let status = resp.status();
			let text = resp.text().await.unwrap_or_else(|_| "Unknown error".to_string());

			if let Ok(error_response) = serde_json::from_str::<GeminiErrorResponse>(&text) {
				return Err(AdapterError::Provider {
					code: error_response.error.code.to_string(),
					message: error_response.error.message,
				});
			}

			return Err(AdapterError::Provider {
				code: status.as_u16().to_string(),
				message: text,
			});
		}

		let models_response: GeminiModelsResponse = resp.json().await.map_err(|e| AdapterError::Http(format!("Failed to parse models response: {}", e)))?;

		let discovered_models: Vec<DiscoveredModel> = models_response
			.models
			.into_iter()
			.filter(|model| model.supported_generation_methods.iter().any(|m| m == "generateContent"))
			.map(|model| {
				let parsed = self.live_model_facts(&model);
				let model_id = model.name.strip_prefix("models/").unwrap_or(&model.name);
				DiscoveredModel {
					id: format!("{}/{}", provider_name.to_lowercase(), model_id),
					name: model.display_name.unwrap_or_else(|| model_id.to_string()),
					provider_name: provider_name.to_string(),
					provider_kind: ProviderKind::Google,
					input_modalities: parsed.input_modalities,
					output_modalities: parsed.output_modalities,
					capabilities: parsed.capabilities,
					context_length: parsed.context_length,
					max_tokens: parsed.max_tokens,
					pricing: None,
				}
			})
			.collect();

		Ok(discovered_models)
	}

	async fn execute_chat(&self, ir: ChatRequestIR, cancel: CancellationToken) -> Result<Box<dyn futures_util::Stream<Item = StreamEvent> + Send + Unpin>, AdapterError> {
		let payload = Self::build_gemini_request(&ir)?;

		let client = reqwest::Client::new();
		let base_url = ir.model.provider.endpoint.base_url.trim_end_matches('/');

		let endpoint_suffix = if ir.stream { "streamGenerateContent" } else { "generateContent" };

		let mut url = format!(
			"{}/v1beta/models/{}:{}",
			base_url,
			self.resolve_adapter_model_id(&ir.model.model_id, &ir.model.provider.name),
			endpoint_suffix
		);

		if let Some(api_key) = &ir.model.provider.endpoint.api_key {
			if ir.stream {
				url = format!("{}?key={}&alt=sse", url, api_key);
			} else {
				url = format!("{}?key={}", url, api_key);
			}
		}

		let mut request = client.post(&url).header("content-type", "application/json").json(&payload);

		if let Some(timeout) = ir.model.provider.endpoint.timeout {
			request = request.timeout(std::time::Duration::from_millis(timeout));
		}

		for (key, value) in &ir.model.provider.endpoint.extra_headers {
			request = request.header(key, value);
		}

		let mut resp = request.send().await.map_err(|e| AdapterError::Http(format!("Failed to send request: {}", e)))?;

		if !resp.status().is_success() {
			let status = resp.status();
			let text = resp.text().await.unwrap_or_else(|_| "Unknown error".to_string());

			if let Ok(error_response) = serde_json::from_str::<GeminiErrorResponse>(&text) {
				return Err(AdapterError::Provider {
					code: error_response.error.code.to_string(),
					message: error_response.error.message,
				});
			}

			return Err(AdapterError::Provider {
				code: status.as_u16().to_string(),
				message: text,
			});
		}

		if ir.stream {
			let s = async_stream::try_stream! {
				use crate::sse::SseParser;

				let mut tool_calls_buffer: HashMap<String, (String, String)> = HashMap::new();
				let mut current_tool_id: Option<String> = None;
				let mut input_tokens = 0u32;
				let mut output_tokens = 0u32;
				let mut cached_input_tokens = 0u32;
				let mut reasoning_tokens = 0u32;
				let mut tool_call_counter = 0u32;
				let mut sse_parser = SseParser::new();

				while let Some(chunk) = resp.chunk().await
					.map_err(|e| AdapterError::Http(format!("Failed to read chunk: {}", e)))?
				{
					if cancel.is_cancelled() {
						yield StreamEvent::Error {
							code: "cancelled".to_string(),
							message: "Request was cancelled".to_string(),
						};
						break;
					}

					let chunk_str = String::from_utf8_lossy(&chunk);

					// Feed the chunk to the SSE parser - it will buffer incomplete events
					let events = sse_parser.feed(&chunk_str);

					for sse_event in events {
						let event_data = &sse_event.data;

						if let Ok(response) = serde_json::from_str::<GeminiGenerateContentResponse>(event_data) {
							if let Some(usage) = &response.usage_metadata {
								input_tokens = usage.prompt_token_count;
								output_tokens = usage.candidates_token_count;
								cached_input_tokens = usage.cached_content_token_count;
								reasoning_tokens = usage.thoughts_token_count;
							}

							for candidate in &response.candidates {
								if let Some(content) = &candidate.content {
									for part in &content.parts {
										match part {
											GeminiPart::ThoughtText { text, thought: true } => {
												yield StreamEvent::ReasoningDelta { content: text.clone() };
											}
											GeminiPart::ThoughtText { text, thought: false } => {
												yield StreamEvent::TextDelta { content: text.clone() };
											}
											GeminiPart::Text { text } => {
												yield StreamEvent::TextDelta { content: text.clone() };
											}
											GeminiPart::FunctionCall { function_call } => {
												let tool_id = format!("call_{}", tool_call_counter);
												tool_call_counter += 1;

												tool_calls_buffer.insert(
													tool_id.clone(),
													(function_call.name.clone(), function_call.args.to_string())
												);

												yield StreamEvent::ToolCallStart {
													id: tool_id.clone(),
													name: function_call.name.clone(),
													args_json: serde_json::Value::Object(serde_json::Map::new()),
												};

												yield StreamEvent::ToolCallDelta {
													id: tool_id.clone(),
													args_delta_json: function_call.args.clone(),
												};

												yield StreamEvent::ToolCallEnd {
													id: tool_id,
													args_json: function_call.args.clone(),
												};
											}
											_ => {}
										}
									}
								}

								// Check for completion
								if let Some(finish_reason) = &candidate.finish_reason {
									match finish_reason {
										GeminiFinishReason::Stop | GeminiFinishReason::MaxTokens => {
											// Normal completion
										}
										GeminiFinishReason::Safety => {
											yield StreamEvent::Error {
												code: "safety".to_string(),
												message: "Response blocked due to safety settings".to_string(),
											};
										}
										_ => {}
									}
								}
							}
						}
					}
				}

				yield StreamEvent::Tokens {
					input: input_tokens,
					output: output_tokens,
				};
				yield StreamEvent::OpenAIMetadata {
					system_fingerprint: None,
					service_tier: None,
					prompt_tokens_details: Some(PromptTokensDetails {
						cached_tokens: cached_input_tokens,
						audio_tokens: 0,
						cache_write_tokens: 0,
					}),
					completion_tokens_details: Some(CompletionTokensDetails {
						reasoning_tokens,
						audio_tokens: 0,
						accepted_prediction_tokens: 0,
						rejected_prediction_tokens: 0,
					}),
				};
				yield StreamEvent::Done;
			};

			Ok(Box::new(Box::pin(s.map(|r: Result<StreamEvent, AdapterError>| match r {
				Ok(ev) => ev,
				Err(e) => StreamEvent::Error {
					code: "stream_error".to_string(),
					message: e.to_string(),
				},
			}))))
		} else {
			let response: GeminiGenerateContentResponse = resp.json().await.map_err(|e| AdapterError::Http(format!("Failed to parse response: {}", e)))?;

			let s = async_stream::try_stream! {
				let mut tool_call_counter = 0u32;

				for candidate in &response.candidates {
					if let Some(content) = &candidate.content {
						for part in &content.parts {
							match part {
								GeminiPart::ThoughtText { text, thought: true } => {
									yield StreamEvent::ReasoningDelta { content: text.clone() };
								}
								GeminiPart::ThoughtText { text, thought: false } => {
									yield StreamEvent::TextDelta { content: text.clone() };
								}
								GeminiPart::Text { text } => {
									yield StreamEvent::TextDelta { content: text.clone() };
								}
								GeminiPart::FunctionCall { function_call } => {
									let tool_id = format!("call_{}", tool_call_counter);
									tool_call_counter += 1;

									yield StreamEvent::ToolCallStart {
										id: tool_id.clone(),
										name: function_call.name.clone(),
										args_json: serde_json::Value::Object(serde_json::Map::new()),
									};
									yield StreamEvent::ToolCallDelta {
										id: tool_id.clone(),
										args_delta_json: function_call.args.clone(),
									};
									yield StreamEvent::ToolCallEnd {
										id: tool_id,
										args_json: function_call.args.clone(),
									};
								}
								_ => {}
							}
						}
					}
				}

				if let Some(usage) = &response.usage_metadata {
					yield StreamEvent::Tokens {
						input: usage.prompt_token_count,
						output: usage.candidates_token_count,
					};
					yield StreamEvent::OpenAIMetadata {
						system_fingerprint: None,
						service_tier: None,
						prompt_tokens_details: Some(PromptTokensDetails {
							cached_tokens: usage.cached_content_token_count,
							audio_tokens: 0,
							cache_write_tokens: 0,
						}),
						completion_tokens_details: Some(CompletionTokensDetails {
							reasoning_tokens: usage.thoughts_token_count,
							audio_tokens: 0,
							accepted_prediction_tokens: 0,
							rejected_prediction_tokens: 0,
						}),
					};
				}

				yield StreamEvent::Done;
			};

			Ok(Box::new(Box::pin(s.map(|r: Result<StreamEvent, AdapterError>| match r {
				Ok(ev) => ev,
				Err(e) => StreamEvent::Error {
					code: "response_error".to_string(),
					message: e.to_string(),
				},
			}))))
		}
	}
}

impl GeminiAdapter {
	fn build_gemini_request(ir: &ChatRequestIR) -> Result<GeminiGenerateContentRequest, AdapterError> {
		let mut system_instruction: Option<GeminiContent> = None;
		let mut contents: Vec<GeminiContent> = Vec::new();

		for msg in &ir.messages {
			match msg.role {
				Role::System | Role::Developer => {
					let parts = Self::build_parts(&msg.parts);
					if system_instruction.is_none() {
						system_instruction = Some(GeminiContent { role: None, parts });
					} else {
						if let Some(ref mut si) = system_instruction {
							si.parts.extend(parts);
						}
					}
				}
				Role::User => {
					let parts = Self::build_parts(&msg.parts);
					contents.push(GeminiContent {
						role: Some("user".to_string()),
						parts,
					});
				}
				Role::Assistant => {
					let parts = Self::build_parts(&msg.parts);
					contents.push(GeminiContent {
						role: Some("model".to_string()),
						parts,
					});
				}
				Role::Tool => {
					let mut parts = Vec::new();
					for part in &msg.parts {
						if let ContentPart::Text(text) = part {
							let response_value = serde_json::from_str(text).unwrap_or_else(|_| serde_json::json!({ "result": text }));

							parts.push(GeminiPart::FunctionResponse {
								function_response: GeminiFunctionResponse {
									name: msg.name.clone().unwrap_or_default(),
									response: response_value,
								},
							});
						}
					}
					if !parts.is_empty() {
						contents.push(GeminiContent {
							role: Some("function".to_string()),
							parts,
						});
					}
				}
			}
		}

		let tools = if ir.tools.is_empty() {
			None
		} else {
			let function_declarations: Vec<GeminiFunctionDeclaration> = ir
				.tools
				.iter()
				.map(|tool| match tool {
					ToolSpec::JsonSchema {
						name,
						description,
						schema,
						strict: _,
					} => GeminiFunctionDeclaration {
						name: name.clone(),
						description: description.clone(),
						parameters: Some(schema.clone()),
					},
				})
				.collect();

			Some(vec![GeminiTool { function_declarations }])
		};

		let tool_config = match &ir.tool_choice {
			ToolChoice::Auto => None,
			ToolChoice::None => Some(GeminiToolConfig {
				function_calling_config: GeminiFunctionCallingConfig {
					mode: GeminiFunctionCallingMode::None,
					allowed_function_names: None,
				},
			}),
			ToolChoice::Required => Some(GeminiToolConfig {
				function_calling_config: GeminiFunctionCallingConfig {
					mode: GeminiFunctionCallingMode::Any,
					allowed_function_names: None,
				},
			}),
			ToolChoice::Named(name) => Some(GeminiToolConfig {
				function_calling_config: GeminiFunctionCallingConfig {
					mode: GeminiFunctionCallingMode::Any,
					allowed_function_names: Some(vec![name.clone()]),
				},
			}),
			ToolChoice::Allowed { tools, .. } => Some(GeminiToolConfig {
				function_calling_config: GeminiFunctionCallingConfig {
					mode: GeminiFunctionCallingMode::Auto,
					allowed_function_names: Some(tools.clone()),
				},
			}),
		};

		let thinking_config = ir.reasoning.as_ref().map(|r| GeminiThinkingConfig {
			thinking_level: r.effort.clone(),
			thinking_budget: r.budget_tokens.map(|t| t as i32),
			include_thoughts: r.summary.as_ref().map(|_| true),
		});

		let generation_config = Some(GeminiGenerationConfig {
			max_output_tokens: ir.sampling.max_tokens,
			temperature: ir.sampling.temperature,
			top_p: ir.sampling.top_p,
			top_k: ir.sampling.top_k,
			stop_sequences: if ir.sampling.stop.is_empty() { None } else { Some(ir.sampling.stop.clone()) },
			presence_penalty: ir.sampling.presence_penalty,
			frequency_penalty: ir.sampling.frequency_penalty,
			seed: ir.sampling.seed.map(|s| s as i64),
			response_mime_type: None,
			response_schema: None,
			candidate_count: None,
			thinking_config,
		});

		Ok(GeminiGenerateContentRequest {
			contents,
			tools,
			tool_config,
			system_instruction,
			generation_config,
			safety_settings: None,
		})
	}

	fn build_parts(parts: &[ContentPart]) -> Vec<GeminiPart> {
		parts
			.iter()
			.filter_map(|part| match part {
				ContentPart::Text(text) => Some(GeminiPart::Text { text: text.clone() }),
				ContentPart::ImageUrl { url, mime } => {
					if url.starts_with("data:") {
						if let Some(comma_pos) = url.find(',') {
							let data = &url[comma_pos + 1..];
							let mime_part = &url[5..comma_pos]; // Skip "data:"
							let mime_type = mime_part.split(';').next().unwrap_or("image/png");

							Some(GeminiPart::InlineData {
								inline_data: GeminiBlob {
									mime_type: mime_type.to_string(),
									data: data.to_string(),
								},
							})
						} else {
							None
						}
					} else {
						Some(GeminiPart::FileData {
							file_data: GeminiFileData {
								mime_type: mime.clone(),
								file_uri: url.clone(),
							},
						})
					}
				}
				ContentPart::ToolCall { id: _, name, arguments } => {
					// Convert tool call to Gemini's function call format
					let args = serde_json::from_str(arguments).unwrap_or(serde_json::json!({}));
					Some(GeminiPart::FunctionCall {
						function_call: GeminiFunctionCall { name: name.clone(), args },
					})
				}
				ContentPart::BlobRef { .. } => None,
				ContentPart::Audio { .. } => None,
				ContentPart::File { .. } => None,
			})
			.collect()
	}

	pub fn live_model_facts(&self, model: &GeminiModelInfo) -> ModelCapabilitiesWithModalities {
		let capabilities = ModelCapabilitiesWithModalities {
			context_length: model.input_token_limit,
			max_tokens: model.output_token_limit,
			capabilities: vec![],
			input_modalities: vec![Modality::Text],
			output_modalities: vec![Modality::Text],
		};

		capabilities
	}
}