mirror of
https://github.com/outbackdingo/optimclaw.git
synced 2026-08-27 16:10:09 +00:00
feat(gemini_oauth): full Cloud Code API integration with project discovery
- Register gemini_oauth as a dedicated backend in config/llm.rs (skip registry fallback, preserve backend name, suppress unknown-backend warning) - Fix app.rs credential guard to exclude backends with dedicated configs (gemini_oauth, bedrock) from the provider.is_none() check - Auto-discover Cloud Code project_id via loadCodeAssist when credentials lack it (e.g. created by the original Gemini CLI) - Persist discovered project_id to credentials file for subsequent runs - Add safety settings (BLOCK_NONE), gated behind GEMINI_SAFETY_BLOCK_NONE env - Add thinkingConfig: budget-based for Gemini 2.5, level-based for Gemini 3.x (without includeThoughts to avoid empty responses from reasoning.rs stripping) - Add thought signature injection for Gemini 3.x preview APIs - Add history curation to filter invalid model outputs before re-sending - Add extended generationConfig env vars (topP, topK, seed, penalties, responseMimeType, responseJsonSchema, cachedContent) - Add custom headers support via GEMINI_CLI_CUSTOM_HEADERS - Add API key auth mode (GEMINI_API_KEY + GEMINI_API_KEY_AUTH_MECHANISM) - Add SSE metadata extraction (modelVersion, credits, promptFeedback, groundingMetadata, citationMetadata, cachedContentTokenCount) - Add countTokens API support - Add new models to wizard (gemini-3.1-pro-preview-customtools, gemini-3-pro-preview, gemini-3.1-flash-lite-preview) - Update docs/LLM_PROVIDERS.md with new models and routing rules - Rewrite regression tests with comprehensive coverage (23 unit tests pass)
This commit is contained in:
@@ -88,15 +88,19 @@ GEMINI_MODEL=gemini-2.5-flash
|
||||
| Model | ID | Notes |
|
||||
|---|---|---|
|
||||
| Gemini 3.1 Pro | `gemini-3.1-pro-preview` | Latest, strongest reasoning |
|
||||
| Gemini 3.1 Pro Custom Tools | `gemini-3.1-pro-preview-customtools` | Enhanced tool use |
|
||||
| Gemini 3 Pro | `gemini-3-pro-preview` | Preview |
|
||||
| Gemini 3 Flash | `gemini-3-flash-preview` | Fast preview with thinking |
|
||||
| Gemini 3.1 Flash Lite | `gemini-3.1-flash-lite-preview` | Preview, lightweight |
|
||||
| Gemini 2.5 Pro | `gemini-2.5-pro` | Stable, strong reasoning |
|
||||
| Gemini 2.5 Flash | `gemini-2.5-flash` | Fast, good quality |
|
||||
| Gemini 2.5 Flash Lite | `gemini-2.5-flash-lite` | Fastest, lightweight |
|
||||
|
||||
### Cloud Code API vs standard API
|
||||
|
||||
Models containing `preview` or `gemini-3` in the name route through the
|
||||
Cloud Code API (`cloudcode-pa.googleapis.com`) which supports SSE streaming
|
||||
Models containing `-preview` (with hyphen) or `gemini-3` in the name, as well
|
||||
as any `gemini-` model with major version >= 2, route through the Cloud Code
|
||||
API (`cloudcode-pa.googleapis.com`) which supports SSE streaming
|
||||
and project-scoped access. Other models use the standard Generative Language
|
||||
API (`generativelanguage.googleapis.com`).
|
||||
|
||||
|
||||
+8
-4
@@ -683,10 +683,14 @@ impl AppBuilder {
|
||||
self.init_database().await?;
|
||||
self.init_secrets().await?;
|
||||
|
||||
// Post-init validation: if a non-nearai backend was selected but
|
||||
// credentials were never resolved (deferred resolution found no keys),
|
||||
// fail early with a clear error instead of a confusing runtime failure.
|
||||
if self.config.llm.backend != "nearai" && self.config.llm.provider.is_none() {
|
||||
// Post-init validation: backends with dedicated config (nearai, gemini_oauth,
|
||||
// bedrock) handle their own credential resolution. For registry-based backends,
|
||||
// fail early if no provider config was resolved.
|
||||
if self.config.llm.backend != "nearai"
|
||||
&& self.config.llm.backend != "gemini_oauth"
|
||||
&& self.config.llm.backend != "bedrock"
|
||||
&& self.config.llm.provider.is_none()
|
||||
{
|
||||
let backend = &self.config.llm.backend;
|
||||
anyhow::bail!(
|
||||
"LLM_BACKEND={backend} is configured but no credentials were found. \
|
||||
|
||||
+6
-3
@@ -10,7 +10,6 @@ use crate::llm::registry::{ProviderProtocol, ProviderRegistry};
|
||||
use crate::llm::session::SessionConfig;
|
||||
use crate::settings::Settings;
|
||||
|
||||
|
||||
impl LlmConfig {
|
||||
/// Create a test-friendly config without reading env vars.
|
||||
#[cfg(feature = "libsql")]
|
||||
@@ -75,8 +74,10 @@ impl LlmConfig {
|
||||
backend_lower == "nearai" || backend_lower == "near_ai" || backend_lower == "near";
|
||||
let is_bedrock =
|
||||
backend_lower == "bedrock" || backend_lower == "aws_bedrock" || backend_lower == "aws";
|
||||
let is_gemini_oauth = backend_lower == "gemini_oauth" || backend_lower == "gemini-oauth";
|
||||
|
||||
if !is_nearai && !is_bedrock && registry.find(&backend_lower).is_none() {
|
||||
if !is_nearai && !is_bedrock && !is_gemini_oauth && registry.find(&backend_lower).is_none()
|
||||
{
|
||||
tracing::warn!(
|
||||
"Unknown LLM backend '{}'. Will attempt as openai_compatible fallback.",
|
||||
backend
|
||||
@@ -124,7 +125,7 @@ impl LlmConfig {
|
||||
};
|
||||
|
||||
// Resolve registry provider config (for non-NearAI, non-Bedrock backends)
|
||||
let provider = if is_nearai || is_bedrock {
|
||||
let provider = if is_nearai || is_bedrock || is_gemini_oauth {
|
||||
None
|
||||
} else {
|
||||
Some(Self::resolve_registry_provider(
|
||||
@@ -199,6 +200,8 @@ impl LlmConfig {
|
||||
"nearai".to_string()
|
||||
} else if is_bedrock {
|
||||
"bedrock".to_string()
|
||||
} else if is_gemini_oauth {
|
||||
"gemini_oauth".to_string()
|
||||
} else if let Some(ref p) = provider {
|
||||
p.provider_id.clone()
|
||||
} else {
|
||||
|
||||
@@ -239,6 +239,21 @@ impl NearAiConfig {
|
||||
}
|
||||
|
||||
/// Configuration for Gemini OAuth integration.
|
||||
///
|
||||
/// Extended generation config parameters (topP, topK, seed, etc.) are read from
|
||||
/// environment variables at request time:
|
||||
/// - `GEMINI_TOP_P` — nucleus sampling (0.0–1.0)
|
||||
/// - `GEMINI_TOP_K` — top-k sampling (integer)
|
||||
/// - `GEMINI_SEED` — deterministic generation seed
|
||||
/// - `GEMINI_PRESENCE_PENALTY` — presence penalty (-2.0–2.0)
|
||||
/// - `GEMINI_FREQUENCY_PENALTY` — frequency penalty (-2.0–2.0)
|
||||
/// - `GEMINI_RESPONSE_MIME_TYPE` — e.g. "application/json"
|
||||
/// - `GEMINI_RESPONSE_JSON_SCHEMA` — JSON schema string for structured output
|
||||
/// - `GEMINI_CACHED_CONTENT` — cached content resource name
|
||||
/// - `GEMINI_CLI_CUSTOM_HEADERS` — custom headers (key:value,key:value)
|
||||
/// - `GOOGLE_GENAI_API_VERSION` — API version (default: v1beta)
|
||||
/// - `GEMINI_API_KEY` — optional API key for non-OAuth auth mode
|
||||
/// - `GEMINI_API_KEY_AUTH_MECHANISM` — "x-goog-api-key" (default) or "bearer"
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct GeminiOauthConfig {
|
||||
pub model: String,
|
||||
|
||||
+811
-38
File diff suppressed because it is too large
Load Diff
@@ -1708,10 +1708,22 @@ impl SetupWizard {
|
||||
"gemini-3.1-pro-preview".into(),
|
||||
"Gemini 3.1 Pro (Latest, strongest reasoning)".into(),
|
||||
),
|
||||
(
|
||||
"gemini-3.1-pro-preview-customtools".into(),
|
||||
"Gemini 3.1 Pro Custom Tools (Enhanced tool use)".into(),
|
||||
),
|
||||
(
|
||||
"gemini-3-pro-preview".into(),
|
||||
"Gemini 3 Pro (Preview)".into(),
|
||||
),
|
||||
(
|
||||
"gemini-3-flash-preview".into(),
|
||||
"Gemini 3 Flash (Fast preview with thinking)".into(),
|
||||
),
|
||||
(
|
||||
"gemini-3.1-flash-lite-preview".into(),
|
||||
"Gemini 3.1 Flash Lite (Preview, lightweight)".into(),
|
||||
),
|
||||
(
|
||||
"gemini-2.5-pro".into(),
|
||||
"Gemini 2.5 Pro (Stable, strong reasoning)".into(),
|
||||
|
||||
@@ -1,28 +1,96 @@
|
||||
use ironclaw::llm::ChatMessage;
|
||||
use ironclaw::llm::gemini_oauth::GeminiOauthProvider;
|
||||
|
||||
/// Regression: Cloud Code API routing for Gemini 2.0+ models.
|
||||
/// Gemini 1.x → legacy generativelanguage.googleapis.com
|
||||
/// Gemini 2.0+ → Cloud Code API (cloudcode-pa.googleapis.com)
|
||||
#[test]
|
||||
fn test_regression_gemini_oauth_fields() {
|
||||
// This test ensures that the CompletionResponse and ToolCompletionResponse
|
||||
// include the newly added caching fields, which was a critical compilation fix.
|
||||
// Since we are using the public API, if it compiles and runs, the fields are present.
|
||||
fn test_regression_cloud_code_api_routing() {
|
||||
// Legacy models (1.x) → false
|
||||
assert!(!GeminiOauthProvider::model_uses_cloud_code_api(
|
||||
"gemini-1.5-pro"
|
||||
));
|
||||
assert!(!GeminiOauthProvider::model_uses_cloud_code_api(
|
||||
"gemini-1.5-flash"
|
||||
));
|
||||
|
||||
// Test model metadata logic (which we updated)
|
||||
assert!(
|
||||
!ironclaw::llm::gemini_oauth::GeminiOauthProvider::model_uses_cloud_code_api(
|
||||
"gemini-1.5-pro"
|
||||
)
|
||||
);
|
||||
assert!(
|
||||
ironclaw::llm::gemini_oauth::GeminiOauthProvider::model_uses_cloud_code_api(
|
||||
"gemini-2.0-flash"
|
||||
)
|
||||
);
|
||||
// 2.0+ models → true
|
||||
assert!(GeminiOauthProvider::model_uses_cloud_code_api(
|
||||
"gemini-2.0-flash"
|
||||
));
|
||||
assert!(GeminiOauthProvider::model_uses_cloud_code_api(
|
||||
"gemini-2.5-pro"
|
||||
));
|
||||
assert!(GeminiOauthProvider::model_uses_cloud_code_api(
|
||||
"gemini-2.5-flash"
|
||||
));
|
||||
|
||||
// Preview models with hyphen → true
|
||||
assert!(GeminiOauthProvider::model_uses_cloud_code_api(
|
||||
"gemini-3.1-pro-preview"
|
||||
));
|
||||
assert!(GeminiOauthProvider::model_uses_cloud_code_api(
|
||||
"gemini-3-flash-preview"
|
||||
));
|
||||
|
||||
// Gemini 3 family → true
|
||||
assert!(GeminiOauthProvider::model_uses_cloud_code_api(
|
||||
"gemini-3-pro"
|
||||
));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_regression_chat_message_helpers() {
|
||||
// Verify ChatMessage helper methods which were used to fix tests
|
||||
let msg = ChatMessage::user("test");
|
||||
assert_eq!(msg.role, ironclaw::llm::Role::User);
|
||||
assert_eq!(msg.content, "test");
|
||||
/// Regression: "preview" false-positive fix.
|
||||
/// `model.contains("-preview")` (with hyphen) prevents models whose name
|
||||
/// happens to include "preview" without a hyphen prefix from being
|
||||
/// mis-routed to Cloud Code API.
|
||||
#[test]
|
||||
fn test_regression_preview_false_positive_fix() {
|
||||
// "my-preview-custom" still matches (contains "-preview")
|
||||
assert!(GeminiOauthProvider::model_uses_cloud_code_api(
|
||||
"my-preview-custom"
|
||||
));
|
||||
|
||||
// "mypreviewcustom" does NOT match (no hyphen before "preview")
|
||||
assert!(!GeminiOauthProvider::model_uses_cloud_code_api(
|
||||
"mypreviewcustom"
|
||||
));
|
||||
|
||||
// Non-Gemini models without "-preview" → false
|
||||
assert!(!GeminiOauthProvider::model_uses_cloud_code_api(
|
||||
"not-a-gemini-model"
|
||||
));
|
||||
}
|
||||
|
||||
/// Regression: model list consistency.
|
||||
/// Wizard, list_models(), and LLM_PROVIDERS.md all return the same 5 models.
|
||||
#[test]
|
||||
fn test_regression_standardized_model_list() {
|
||||
let expected_models = [
|
||||
"gemini-3.1-pro-preview",
|
||||
"gemini-3-flash-preview",
|
||||
"gemini-2.5-pro",
|
||||
"gemini-2.5-flash",
|
||||
"gemini-2.5-flash-lite",
|
||||
];
|
||||
|
||||
// All standardized models must route to Cloud Code API (all are >= 2.0)
|
||||
for model in &expected_models {
|
||||
assert!(
|
||||
GeminiOauthProvider::model_uses_cloud_code_api(model),
|
||||
"Standardized model '{}' should route to Cloud Code API",
|
||||
model
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Regression: ChatMessage helper constructors.
|
||||
#[test]
|
||||
fn test_regression_chat_message_helpers() {
|
||||
let user_msg = ChatMessage::user("hello");
|
||||
assert_eq!(user_msg.role, ironclaw::llm::Role::User);
|
||||
assert_eq!(user_msg.content, "hello");
|
||||
|
||||
let system_msg = ChatMessage::system("you are helpful");
|
||||
assert_eq!(system_msg.role, ironclaw::llm::Role::System);
|
||||
assert_eq!(system_msg.content, "you are helpful");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user