use crate::ai::credentials::{
antigravity_auth_available, get_antigravity_token, get_claude_code_token,
get_codex_token_if_valid, get_gemini_access_token_if_valid, invalidate_gemini_creds,
ANTIGRAVITY_ENDPOINTS, ANTIGRAVITY_VERSION, GEMINI_OAUTH_CLIENT_ID, GEMINI_OAUTH_CLIENT_SECRET,
GOOGLE_TOKEN_ENDPOINT,
};
use crate::ai::ollama::OllamaClient;
use crate::ai::providers::{ProviderKind, ProvidersConfig};
use crate::ai::types::AiConfig;
use crate::error::FetchiumError;
use futures::StreamExt;
use serde::Deserialize;
#[derive(Debug)]
pub struct ChatResult {
pub content: String,
pub model_used: String,
pub provider: ProviderKind,
}
pub async fn chat_with_fallback(
messages: &[crate::ai::types::ChatMessage],
model_override: Option<&str>,
ai_config: &AiConfig,
providers: &ProvidersConfig,
streaming: bool,
on_token: &mut dyn FnMut(&str),
) -> Result<ChatResult, FetchiumError> {
let chain = providers.resolved_chain();
if chain.is_empty() {
return Err(FetchiumError::AiUnavailable(
"No AI providers configured. Run `fetchium provider setup` to get started.".into(),
));
}
let mut last_error: Option<FetchiumError> = None;
let chain_len = chain.len();
for (idx, kind) in chain.iter().enumerate() {
match call_provider(
*kind,
messages,
model_override,
ai_config,
providers,
streaming,
on_token,
)
.await
{
Ok(result) => return Ok(result),
Err(e) => {
if idx + 1 < chain_len {
tracing::debug!("Provider {} failed (trying next): {}", kind.slug(), e);
} else {
tracing::warn!("All AI providers exhausted. Last error: {}", e);
}
last_error = Some(e);
}
}
}
Err(last_error.unwrap_or_else(|| {
FetchiumError::AiUnavailable("All configured AI providers failed.".into())
}))
}
async fn call_provider(
kind: ProviderKind,
messages: &[crate::ai::types::ChatMessage],
model_override: Option<&str>,
ai_config: &AiConfig,
providers: &ProvidersConfig,
streaming: bool,
on_token: &mut dyn FnMut(&str),
) -> Result<ChatResult, FetchiumError> {
let entry = providers.entry(kind);
let model = model_override
.map(|s| s.to_string())
.unwrap_or_else(|| entry.resolve_model(kind));
match kind {
ProviderKind::Ollama => call_ollama(messages, &model, ai_config, streaming, on_token).await,
ProviderKind::OpenAi => {
let auth_store_key_openai = crate::ai::credentials::fetchium_auth_get("openai")
.and_then(|a| a.api_key().map(|s| s.to_string()));
let key = if let Some(k) = entry
.resolve_api_key("OPENAI_API_KEY")
.or(auth_store_key_openai)
{
k
} else if let Some(tok) = get_codex_token_if_valid() {
tracing::info!("Using OpenAI Codex CLI OAuth session (ChatGPT subscription)");
tok
} else {
return Err(FetchiumError::AiUnavailable(
"OpenAI: no API key or Codex CLI session found.\n \
Options:\n \
• Set OPENAI_API_KEY env var\n \
• Log in via Codex CLI: codex auth login\n \
• Run `fetchium provider setup openai`"
.into(),
));
};
let base = entry
.base_url
.clone()
.unwrap_or_else(|| "https://api.openai.com".into());
call_openai_compat(
kind, &key, &base, &model, messages, ai_config, streaming, on_token,
)
.await
}
ProviderKind::Anthropic => {
let auth_store_key_anthropic = crate::ai::credentials::fetchium_auth_get("anthropic")
.and_then(|a| a.api_key().map(|s| s.to_string()));
let (token, use_oauth) = if let Some(k) = entry
.resolve_api_key("ANTHROPIC_API_KEY")
.or(auth_store_key_anthropic)
{
(k, false)
} else if let Some(creds) = get_claude_code_token() {
tracing::info!(
"Using Claude Code {} subscription (OAuth)",
creds.subscription_type
);
(creds.access_token, true)
} else {
return Err(FetchiumError::AiUnavailable(
"Anthropic: no API key or Claude Code session found.\n \
Options:\n \
• Set ANTHROPIC_API_KEY env var\n \
• Run `claude` once to log in (uses your Claude Max/Pro subscription)\n \
• Run `fetchium provider setup anthropic`"
.into(),
));
};
call_anthropic(
&token, use_oauth, &model, messages, ai_config, streaming, on_token,
)
.await
}
ProviderKind::Gemini => {
let keys = collect_gemini_keys(entry);
if !keys.is_empty() {
call_gemini_api_key_pool(&keys, &model, messages, ai_config, streaming, on_token)
.await
} else if let Some(access_token) = get_gemini_access_token_if_valid() {
tracing::info!("Using Gemini OAuth (Gemini CLI subscription)");
call_gemini_oauth(
&access_token,
&model,
messages,
ai_config,
streaming,
on_token,
)
.await
} else {
match refresh_gemini_token_async(ai_config).await {
Some(fresh_token) => {
call_gemini_oauth(&fresh_token, &model, messages, ai_config, streaming, on_token).await
}
None => {
Err(FetchiumError::AiUnavailable(
"Gemini: no API keys configured and OAuth session expired.\n \
Fix (choose one):\n \
• fetchium provider set gemini --key AIza... (set primary key)\n \
• fetchium provider set gemini --add-key AIza... (add to key pool)\n \
• export GEMINI_API_KEY=AIza... (env var)\n \
• gemini auth login (re-authenticate OAuth)\n \
Get a free key: https://aistudio.google.com/app/apikey".into(),
))
}
}
}
}
ProviderKind::GeminiCli => call_gemini_cli(&model, messages, on_token).await,
ProviderKind::OpenRouter => {
let key = entry.resolve_api_key("OPENROUTER_API_KEY")
.or_else(crate::ai::credentials::read_openrouter_key)
.ok_or_else(|| FetchiumError::AiUnavailable(
"OpenRouter API key not set. Set OPENROUTER_API_KEY or run `fetchium provider setup openrouter`.".into(),
))?;
let base = entry
.base_url
.clone()
.unwrap_or_else(|| "https://openrouter.ai/api".into());
call_openai_compat(
kind, &key, &base, &model, messages, ai_config, streaming, on_token,
)
.await
}
ProviderKind::Antigravity => {
let token_result = tokio::task::spawn_blocking(get_antigravity_token)
.await
.unwrap_or(None);
let (access_token, project_id) = token_result.ok_or_else(|| {
FetchiumError::AiUnavailable(
"Antigravity: no OpenCode account found.\n \
Install OpenCode and add an account: https://opencode.ai\n \
Then install the plugin: npm i -g opencode-antigravity-auth\n \
Run `fetchium provider setup antigravity` to verify."
.into(),
)
})?;
call_antigravity(
&access_token,
&project_id,
&model,
messages,
ai_config,
streaming,
on_token,
)
.await
}
}
}
async fn call_ollama(
messages: &[crate::ai::types::ChatMessage],
model: &str,
ai_config: &AiConfig,
streaming: bool,
on_token: &mut dyn FnMut(&str),
) -> Result<ChatResult, FetchiumError> {
let ollama = OllamaClient::new(ai_config);
if !ollama.is_available().await {
return Err(FetchiumError::AiUnavailable(
"Ollama is not running. Start it with: ollama serve".into(),
));
}
let available = ollama.list_models().await.unwrap_or_default();
let model_name = if available
.iter()
.any(|m| m.name == model || m.name.starts_with(model))
{
model.to_string()
} else if let Some(first) = available.first() {
tracing::warn!(
"Requested model '{model}' not found; using '{}'",
first.name
);
first.name.clone()
} else {
return Err(FetchiumError::AiUnavailable(format!(
"No models installed in Ollama. Run: ollama pull {model}"
)));
};
let content = if streaming {
ollama
.chat_stream(&model_name, messages, ai_config.temperature, on_token)
.await?
} else {
ollama
.chat(&model_name, messages, ai_config.temperature)
.await?
};
Ok(ChatResult {
content,
model_used: model_name,
provider: ProviderKind::Ollama,
})
}
#[derive(Deserialize)]
struct OaiResponse {
choices: Vec<OaiChoice>,
model: Option<String>,
}
#[derive(Deserialize)]
struct OaiChoice {
message: OaiMessage,
}
#[derive(Deserialize)]
struct OaiMessage {
content: String,
}
#[derive(Deserialize)]
struct OaiStreamChunk {
choices: Vec<OaiStreamChoice>,
}
#[derive(Deserialize)]
struct OaiStreamChoice {
delta: OaiDelta,
}
#[derive(Deserialize, Default)]
struct OaiDelta {
#[serde(default)]
content: Option<String>,
}
#[allow(clippy::too_many_arguments)]
async fn call_openai_compat(
kind: ProviderKind,
api_key: &str,
base_url: &str,
model: &str,
messages: &[crate::ai::types::ChatMessage],
ai_config: &AiConfig,
streaming: bool,
on_token: &mut dyn FnMut(&str),
) -> Result<ChatResult, FetchiumError> {
let url = format!("{}/v1/chat/completions", base_url.trim_end_matches('/'));
let oai_msgs: Vec<serde_json::Value> = messages
.iter()
.map(|m| serde_json::json!({"role": m.role, "content": m.content}))
.collect();
let body = serde_json::json!({
"model": model,
"messages": oai_msgs,
"temperature": ai_config.temperature,
"stream": streaming,
});
let client = reqwest::Client::builder()
.timeout(std::time::Duration::from_secs(ai_config.timeout_secs))
.build()
.map_err(|e| FetchiumError::AiUnavailable(format!("HTTP client build error: {e}")))?;
let mut req = client
.post(&url)
.header("Authorization", format!("Bearer {api_key}"))
.header("Content-Type", "application/json");
if kind == ProviderKind::OpenRouter {
req = req
.header("HTTP-Referer", "https://github.com/fetchium/fetchium")
.header("X-Title", "Fetchium");
}
let resp = req.json(&body).send().await.map_err(|e| {
FetchiumError::AiUnavailable(format!("Request to {} failed: {e}", kind.display_name()))
})?;
if !resp.status().is_success() {
let status = resp.status();
let body_text = resp.text().await.unwrap_or_default();
return Err(FetchiumError::AiUnavailable(format!(
"{} API error {status}: {body_text}",
kind.display_name()
)));
}
if streaming {
let mut stream = resp.bytes_stream();
let mut full = String::new();
let mut buf = Vec::<u8>::new();
while let Some(chunk) = stream.next().await {
let bytes =
chunk.map_err(|e| FetchiumError::AiUnavailable(format!("Stream error: {e}")))?;
buf.extend_from_slice(&bytes);
while let Some(pos) = buf.iter().position(|&b| b == b'\n') {
let line: Vec<u8> = buf.drain(..=pos).collect();
let s = std::str::from_utf8(&line).unwrap_or("").trim();
if let Some(data) = s.strip_prefix("data: ") {
if data == "[DONE]" {
return Ok(ChatResult {
content: full,
model_used: model.to_string(),
provider: kind,
});
}
if let Ok(chunk) = serde_json::from_str::<OaiStreamChunk>(data) {
for choice in &chunk.choices {
if let Some(ref tok) = choice.delta.content {
on_token(tok);
full.push_str(tok);
}
}
}
}
}
}
Ok(ChatResult {
content: full,
model_used: model.to_string(),
provider: kind,
})
} else {
let parsed: OaiResponse = resp
.json()
.await
.map_err(|e| FetchiumError::AiUnavailable(format!("Invalid response: {e}")))?;
let content = parsed
.choices
.into_iter()
.next()
.map(|c| c.message.content)
.unwrap_or_default();
let model_used = parsed.model.unwrap_or_else(|| model.to_string());
Ok(ChatResult {
content,
model_used,
provider: kind,
})
}
}
#[derive(Deserialize)]
struct AnthropicResponse {
content: Vec<AnthropicBlock>,
model: String,
}
#[derive(Deserialize)]
struct AnthropicBlock {
#[serde(rename = "type")]
kind: String,
text: Option<String>,
}
async fn call_anthropic(
token: &str,
use_oauth: bool, model: &str,
messages: &[crate::ai::types::ChatMessage],
ai_config: &AiConfig,
streaming: bool,
on_token: &mut dyn FnMut(&str),
) -> Result<ChatResult, FetchiumError> {
let mut system_text = String::new();
let mut anth_msgs: Vec<serde_json::Value> = Vec::new();
for m in messages {
if m.role == "system" {
system_text = m.content.clone();
} else {
anth_msgs.push(serde_json::json!({"role": m.role, "content": m.content}));
}
}
if anth_msgs.is_empty() {
anth_msgs.push(serde_json::json!({"role": "user", "content": "Hello"}));
}
let mut body = serde_json::json!({
"model": model,
"max_tokens": ai_config.max_context_tokens,
"temperature": ai_config.temperature,
"messages": anth_msgs,
"stream": streaming,
});
if !system_text.is_empty() {
body["system"] = serde_json::Value::String(system_text);
}
let client = reqwest::Client::builder()
.timeout(std::time::Duration::from_secs(ai_config.timeout_secs))
.build()
.map_err(|e| FetchiumError::AiUnavailable(format!("HTTP client build error: {e}")))?;
let mut req = client
.post("https://api.anthropic.com/v1/messages")
.header("anthropic-version", "2023-06-01")
.header("Content-Type", "application/json");
if use_oauth {
req = req.header("Authorization", format!("Bearer {token}"));
} else {
req = req.header("x-api-key", token);
}
let resp = req
.json(&body)
.send()
.await
.map_err(|e| FetchiumError::AiUnavailable(format!("Anthropic request failed: {e}")))?;
if !resp.status().is_success() {
let status = resp.status();
let body_text = resp.text().await.unwrap_or_default();
return Err(FetchiumError::AiUnavailable(format!(
"Anthropic API error {status}: {body_text}"
)));
}
if streaming {
let mut stream = resp.bytes_stream();
let mut full = String::new();
let mut buf = Vec::<u8>::new();
while let Some(chunk) = stream.next().await {
let bytes =
chunk.map_err(|e| FetchiumError::AiUnavailable(format!("Stream error: {e}")))?;
buf.extend_from_slice(&bytes);
while let Some(pos) = buf.iter().position(|&b| b == b'\n') {
let line: Vec<u8> = buf.drain(..=pos).collect();
let s = std::str::from_utf8(&line).unwrap_or("").trim();
if let Some(data) = s.strip_prefix("data: ") {
if let Ok(ev) = serde_json::from_str::<serde_json::Value>(data) {
if ev["type"] == "content_block_delta" {
if let Some(text) = ev["delta"]["text"].as_str() {
on_token(text);
full.push_str(text);
}
}
}
}
}
}
Ok(ChatResult {
content: full,
model_used: model.to_string(),
provider: ProviderKind::Anthropic,
})
} else {
let parsed: AnthropicResponse = resp.json().await.map_err(|e| {
FetchiumError::AiUnavailable(format!("Invalid Anthropic response: {e}"))
})?;
let content = parsed
.content
.into_iter()
.filter(|b| b.kind == "text")
.filter_map(|b| b.text)
.collect::<Vec<_>>()
.join("");
Ok(ChatResult {
content,
model_used: parsed.model,
provider: ProviderKind::Anthropic,
})
}
}
#[derive(Deserialize)]
struct GeminiResponse {
candidates: Vec<GeminiCandidate>,
}
#[derive(Deserialize)]
struct GeminiCandidate {
content: GeminiContent,
}
#[derive(Deserialize)]
struct GeminiContent {
parts: Vec<GeminiPart>,
}
#[derive(Deserialize)]
struct GeminiPart {
text: Option<String>,
}
fn gemini_build_contents(messages: &[crate::ai::types::ChatMessage]) -> Vec<serde_json::Value> {
let mut system_text = String::new();
let mut contents: Vec<serde_json::Value> = Vec::new();
for m in messages {
if m.role == "system" {
system_text = m.content.clone();
} else {
let role = if m.role == "assistant" {
"model"
} else {
"user"
};
contents.push(serde_json::json!({
"role": role,
"parts": [{"text": m.content}],
}));
}
}
if !system_text.is_empty() {
if let Some(first) = contents.first_mut() {
if first["role"] == "user" {
let original = first["parts"][0]["text"].as_str().unwrap_or("").to_string();
first["parts"][0]["text"] =
serde_json::Value::String(format!("{system_text}\n\n{original}"));
}
}
}
if contents.is_empty() {
contents.push(serde_json::json!({"role": "user", "parts": [{"text": "Hello"}]}));
}
contents
}
async fn gemini_read_stream(
resp: reqwest::Response,
model: &str,
on_token: &mut dyn FnMut(&str),
) -> Result<ChatResult, FetchiumError> {
gemini_read_stream_with_provider(resp, model, ProviderKind::Gemini, on_token).await
}
async fn gemini_read_stream_with_provider(
resp: reqwest::Response,
model: &str,
provider: ProviderKind,
on_token: &mut dyn FnMut(&str),
) -> Result<ChatResult, FetchiumError> {
let mut stream = resp.bytes_stream();
let mut full = String::new();
let mut buf = Vec::<u8>::new();
while let Some(chunk) = stream.next().await {
let bytes =
chunk.map_err(|e| FetchiumError::AiUnavailable(format!("Stream error: {e}")))?;
buf.extend_from_slice(&bytes);
while let Some(pos) = buf.iter().position(|&b| b == b'\n') {
let line: Vec<u8> = buf.drain(..=pos).collect();
let s = std::str::from_utf8(&line).unwrap_or("").trim();
if let Some(data) = s.strip_prefix("data: ") {
if let Ok(parsed) = serde_json::from_str::<GeminiResponse>(data) {
for cand in parsed.candidates {
for part in cand.content.parts {
if let Some(text) = part.text {
on_token(&text);
full.push_str(&text);
}
}
}
}
}
}
}
Ok(ChatResult {
content: full,
model_used: model.to_string(),
provider,
})
}
fn mask_key(key: &str) -> String {
if key.len() > 14 {
format!("{}…{}", &key[..8], &key[key.len() - 4..])
} else {
"****".into()
}
}
fn collect_gemini_keys(entry: &crate::ai::providers::ProviderEntry) -> Vec<String> {
let mut keys: Vec<String> = Vec::new();
let mut push = |k: String| {
if !k.is_empty() && !keys.contains(&k) {
keys.push(k);
}
};
if let Ok(multi) = std::env::var("GEMINI_API_KEYS") {
for k in multi.split(',') {
push(k.trim().to_string());
}
}
if let Ok(k) = std::env::var("GEMINI_API_KEY") {
push(k);
}
for k in &entry.api_keys {
let k = k.trim().to_string();
if !k.starts_with("your-") && k != "REPLACE_ME" {
push(k);
}
}
if let Some(ref k) = entry.api_key {
if !k.starts_with("your-") && k != "REPLACE_ME" {
push(k.clone());
}
}
if let Some(auth) = crate::ai::credentials::fetchium_auth_get("gemini") {
for k in auth.api_keys() {
push(k.to_string());
}
}
tracing::debug!(
"Gemini key pool: {} unique key(s) from all sources",
keys.len()
);
keys
}
static GEMINI_KEY_COUNTER: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
static GEMINI_COOLDOWNS: std::sync::OnceLock<
std::sync::Mutex<std::collections::HashMap<usize, std::time::Instant>>,
> = std::sync::OnceLock::new();
fn gemini_cooldowns(
) -> &'static std::sync::Mutex<std::collections::HashMap<usize, std::time::Instant>> {
GEMINI_COOLDOWNS.get_or_init(|| std::sync::Mutex::new(std::collections::HashMap::new()))
}
const RATE_LIMIT_COOLDOWN: std::time::Duration = std::time::Duration::from_secs(60);
async fn call_gemini_api_key_pool(
keys: &[String],
model: &str,
messages: &[crate::ai::types::ChatMessage],
ai_config: &AiConfig,
streaming: bool,
on_token: &mut dyn FnMut(&str),
) -> Result<ChatResult, FetchiumError> {
let pool_size = keys.len();
let start_idx =
GEMINI_KEY_COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed) % pool_size;
let now = std::time::Instant::now();
if let Ok(mut cooldowns) = gemini_cooldowns().lock() {
cooldowns.retain(|_, expires| *expires > now);
}
let mut rate_limited_count = 0usize;
let mut tried = 0usize;
for offset in 0..pool_size {
let idx = (start_idx + offset) % pool_size;
if let Ok(cooldowns) = gemini_cooldowns().lock() {
if let Some(expires) = cooldowns.get(&idx) {
if *expires > now {
tracing::debug!(
"Gemini: key {} in cooldown ({:.0}s left), skipping",
mask_key(&keys[idx]),
expires.duration_since(now).as_secs_f32()
);
continue;
}
}
}
let key = &keys[idx];
let masked = mask_key(key);
tried += 1;
tracing::debug!(
"Gemini: trying key {} (slot {}/{}, round-robin offset {})",
masked,
idx + 1,
pool_size,
start_idx
);
match call_gemini_api_key(key, model, messages, ai_config, streaming, on_token).await {
Ok(result) => {
if rate_limited_count > 0 {
tracing::info!(
"Gemini: succeeded with key {} after {} rate-limited key(s)",
masked,
rate_limited_count
);
}
return Ok(result);
}
Err(FetchiumError::AiUnavailable(ref msg))
if msg.contains("429") || msg.contains("RESOURCE_EXHAUSTED") =>
{
rate_limited_count += 1;
if let Ok(mut cooldowns) = gemini_cooldowns().lock() {
cooldowns.insert(idx, now + RATE_LIMIT_COOLDOWN);
}
tracing::warn!(
"Gemini key {} rate limited → 60s cooldown ({}/{} pool keys exhausted)",
masked,
rate_limited_count,
pool_size
);
}
Err(e) => return Err(e),
}
}
if tried == 0 {
Err(FetchiumError::AiUnavailable(format!(
"All {} Gemini API key(s) are in cooldown (rate limited).\n \
Cooldown expires in ~60s. Add more keys for uninterrupted service:\n \
• Config: [ai.providers.gemini] api_keys = [\"key1\", \"key2\", ...]\n \
• Env: GEMINI_API_KEYS=key1,key2,...\n \
Get free keys: https://aistudio.google.com/app/apikey",
pool_size
)))
} else {
Err(FetchiumError::AiUnavailable(format!(
"All {} Gemini API key(s) are rate limited (HTTP 429).\n \
Add more keys for uninterrupted service:\n \
• Config: [ai.providers.gemini] api_keys = [\"key1\", \"key2\", ...]\n \
• Env: GEMINI_API_KEYS=key1,key2,...\n \
Get free keys: https://aistudio.google.com/app/apikey",
pool_size
)))
}
}
async fn call_gemini_api_key(
api_key: &str,
model: &str,
messages: &[crate::ai::types::ChatMessage],
ai_config: &AiConfig,
streaming: bool,
on_token: &mut dyn FnMut(&str),
) -> Result<ChatResult, FetchiumError> {
let contents = gemini_build_contents(messages);
let is_thinking_model = model.contains("2.5") || model.contains("thinking");
let disable_thinking = is_thinking_model && !ai_config.thinking;
let body = if disable_thinking {
serde_json::json!({
"contents": contents,
"generationConfig": {
"maxOutputTokens": ai_config.max_context_tokens,
"temperature": ai_config.temperature,
"thinkingConfig": { "thinkingBudget": 0 },
},
})
} else {
serde_json::json!({
"contents": contents,
"generationConfig": {
"maxOutputTokens": ai_config.max_context_tokens,
"temperature": ai_config.temperature,
},
})
};
let url = if streaming {
format!(
"https://generativelanguage.googleapis.com/v1beta/models/{model}:streamGenerateContent?alt=sse&key={api_key}"
)
} else {
format!(
"https://generativelanguage.googleapis.com/v1beta/models/{model}:generateContent?key={api_key}"
)
};
let client = reqwest::Client::builder()
.timeout(std::time::Duration::from_secs(ai_config.timeout_secs))
.build()
.map_err(|e| FetchiumError::AiUnavailable(format!("HTTP client build error: {e}")))?;
let resp = client
.post(&url)
.header("Content-Type", "application/json")
.json(&body)
.send()
.await
.map_err(|e| FetchiumError::AiUnavailable(format!("Gemini request failed: {e}")))?;
if !resp.status().is_success() {
let status = resp.status();
let body_text = resp.text().await.unwrap_or_default();
return Err(FetchiumError::AiUnavailable(format!(
"Gemini API error {status}: {body_text}"
)));
}
if streaming {
gemini_read_stream(resp, model, on_token).await
} else {
let parsed: GeminiResponse = resp
.json()
.await
.map_err(|e| FetchiumError::AiUnavailable(format!("Invalid Gemini response: {e}")))?;
let content = parsed
.candidates
.into_iter()
.next()
.map(|c| {
c.content
.parts
.into_iter()
.filter_map(|p| p.text)
.collect::<Vec<_>>()
.join("")
})
.unwrap_or_default();
Ok(ChatResult {
content,
model_used: model.to_string(),
provider: ProviderKind::Gemini,
})
}
}
async fn call_gemini_oauth(
access_token: &str,
model: &str,
messages: &[crate::ai::types::ChatMessage],
ai_config: &AiConfig,
streaming: bool,
on_token: &mut dyn FnMut(&str),
) -> Result<ChatResult, FetchiumError> {
let contents = gemini_build_contents(messages);
let is_thinking_model = model.contains("2.5") || model.contains("thinking");
let body = if is_thinking_model {
serde_json::json!({
"contents": contents,
"generationConfig": {
"maxOutputTokens": ai_config.max_context_tokens,
"temperature": ai_config.temperature,
"thinkingConfig": { "thinkingBudget": 0 },
},
})
} else {
serde_json::json!({
"contents": contents,
"generationConfig": {
"maxOutputTokens": ai_config.max_context_tokens,
"temperature": ai_config.temperature,
},
})
};
let endpoint = if streaming {
"streamGenerateContent?alt=sse"
} else {
"generateContent"
};
let url = format!("https://generativelanguage.googleapis.com/v1beta/models/{model}:{endpoint}");
let client = reqwest::Client::builder()
.timeout(std::time::Duration::from_secs(ai_config.timeout_secs))
.build()
.map_err(|e| FetchiumError::AiUnavailable(format!("HTTP client build error: {e}")))?;
let resp = client
.post(&url)
.header("Authorization", format!("Bearer {access_token}"))
.header("Content-Type", "application/json")
.json(&body)
.send()
.await
.map_err(|e| FetchiumError::AiUnavailable(format!("Gemini OAuth request failed: {e}")))?;
if !resp.status().is_success() {
let status = resp.status();
let body_text = resp.text().await.unwrap_or_default();
if status == reqwest::StatusCode::FORBIDDEN
&& body_text.contains("ACCESS_TOKEN_SCOPE_INSUFFICIENT")
{
return Err(FetchiumError::AiUnavailable(
"Gemini OAuth token has insufficient scopes for the REST API.
Fix (choose one):
• fetchium provider auth gemini (interactive setup — API key or OAuth)
• export GEMINI_API_KEY=AIza... (free key: aistudio.google.com/app/apikey)
• fetchium provider chain gemini_cli (use the local `gemini` CLI instead)"
.into(),
));
}
return Err(FetchiumError::AiUnavailable(format!(
"Gemini OAuth API error {status}: {body_text}"
)));
}
if streaming {
gemini_read_stream(resp, model, on_token).await
} else {
let parsed: GeminiResponse = resp.json().await.map_err(|e| {
FetchiumError::AiUnavailable(format!("Invalid Gemini OAuth response: {e}"))
})?;
let content = parsed
.candidates
.into_iter()
.next()
.map(|c| {
c.content
.parts
.into_iter()
.filter_map(|p| p.text)
.collect::<Vec<_>>()
.join("")
})
.unwrap_or_default();
Ok(ChatResult {
content,
model_used: model.to_string(),
provider: ProviderKind::Gemini,
})
}
}
async fn refresh_gemini_token_async(_ai_config: &AiConfig) -> Option<String> {
use crate::ai::credentials::read_gemini_creds;
let creds = read_gemini_creds()?;
let refresh_token = &creds.refresh_token;
let client = reqwest::Client::builder()
.timeout(std::time::Duration::from_secs(15))
.build()
.ok()?;
let params = [
("client_id", GEMINI_OAUTH_CLIENT_ID),
("client_secret", GEMINI_OAUTH_CLIENT_SECRET),
("refresh_token", refresh_token.as_str()),
("grant_type", "refresh_token"),
];
let resp = client
.post(GOOGLE_TOKEN_ENDPOINT)
.form(¶ms)
.send()
.await
.ok()?;
if !resp.status().is_success() {
let status = resp.status();
if status == reqwest::StatusCode::UNAUTHORIZED || status == reqwest::StatusCode::BAD_REQUEST
{
invalidate_gemini_creds();
tracing::warn!(
"Gemini refresh token revoked (HTTP {status}) — credentials cleared.\n \
Fix: run `gemini auth login` to get a new session, then retry."
);
} else {
tracing::warn!("Gemini token refresh failed: HTTP {status}");
}
return None;
}
let json: serde_json::Value = resp.json().await.ok()?;
let new_token = json["access_token"].as_str()?.to_string();
if let Some(path) = dirs::home_dir().map(|h| h.join(".gemini").join("oauth_creds.json")) {
let expires_in: u64 = json["expires_in"].as_u64().unwrap_or(3600);
let now_ms = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_millis() as u64)
.unwrap_or(0);
let updated = serde_json::json!({
"access_token": new_token,
"refresh_token": refresh_token,
"expiry_date": now_ms + expires_in * 1000,
"token_type": "Bearer",
});
let _ = std::fs::write(
&path,
serde_json::to_string_pretty(&updated).unwrap_or_default(),
);
tracing::debug!("Gemini OAuth token refreshed and saved to {:?}", path);
}
Some(new_token)
}
#[allow(clippy::too_many_arguments)]
async fn call_antigravity(
access_token: &str,
_project_id: &str,
model: &str,
messages: &[crate::ai::types::ChatMessage],
ai_config: &AiConfig,
streaming: bool,
on_token: &mut dyn FnMut(&str),
) -> Result<ChatResult, FetchiumError> {
let contents = gemini_build_contents(messages);
let is_thinking_model = model.contains("2.5") || model.contains("thinking");
let body = if is_thinking_model {
serde_json::json!({
"contents": contents,
"generationConfig": {
"maxOutputTokens": ai_config.max_context_tokens,
"temperature": ai_config.temperature,
"thinkingConfig": { "thinkingBudget": 0 },
},
"model": model,
})
} else {
serde_json::json!({
"contents": contents,
"generationConfig": {
"maxOutputTokens": ai_config.max_context_tokens,
"temperature": ai_config.temperature,
},
"model": model,
})
};
let ua = format!(
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 \
(KHTML, like Gecko) Antigravity/{ANTIGRAVITY_VERSION} \
Chrome/138.0.7204.235 Electron/37.3.1 Safari/537.36"
);
let client = reqwest::Client::builder()
.timeout(std::time::Duration::from_secs(ai_config.timeout_secs))
.user_agent(&ua)
.build()
.map_err(|e| FetchiumError::AiUnavailable(format!("HTTP client build error: {e}")))?;
let action = if streaming {
"generateContent?alt=sse"
} else {
"generateContent"
};
let mut last_err = String::new();
for &endpoint in ANTIGRAVITY_ENDPOINTS {
let url = format!("{endpoint}/v1internal:{action}");
let resp = client
.post(&url)
.header("Authorization", format!("Bearer {access_token}"))
.header("Content-Type", "application/json")
.header("X-Goog-Api-Client", "google-cloud-sdk vscode_cloudshelleditor/0.1")
.header("Client-Metadata", r#"{"ideType":"IDE_UNSPECIFIED","platform":"PLATFORM_UNSPECIFIED","pluginType":"GEMINI"}"#)
.json(&body)
.send()
.await;
let resp = match resp {
Ok(r) => r,
Err(e) => {
last_err = format!("Request to {endpoint} failed: {e}");
tracing::debug!("Antigravity endpoint {endpoint} failed: {e}");
continue;
}
};
let status = resp.status();
if status == reqwest::StatusCode::UNAUTHORIZED || status == reqwest::StatusCode::FORBIDDEN {
return Err(FetchiumError::AiUnavailable(
"Antigravity: authentication failed — re-run OpenCode to refresh your session.\n \
Run `opencode` once, then retry."
.into(),
));
}
if !status.is_success() {
let body_text = resp.text().await.unwrap_or_default();
last_err = format!("Antigravity API error {status} at {endpoint}: {body_text}");
tracing::debug!("{last_err}");
continue;
}
return if streaming {
gemini_read_stream_with_provider(resp, model, ProviderKind::Antigravity, on_token).await
} else {
let parsed: GeminiResponse = resp.json().await.map_err(|e| {
FetchiumError::AiUnavailable(format!("Invalid Antigravity response: {e}"))
})?;
let content = parsed
.candidates
.into_iter()
.next()
.map(|c| {
c.content
.parts
.into_iter()
.filter_map(|p| p.text)
.collect::<Vec<_>>()
.join("")
})
.unwrap_or_default();
Ok(ChatResult {
content,
model_used: model.to_string(),
provider: ProviderKind::Antigravity,
})
};
}
Err(FetchiumError::AiUnavailable(format!(
"Antigravity: all endpoints failed. Last error: {last_err}\n \
Ensure your OpenCode session is active and the antigravity plugin is installed."
)))
}
async fn call_gemini_cli(
model: &str,
messages: &[crate::ai::types::ChatMessage],
on_token: &mut dyn FnMut(&str),
) -> Result<ChatResult, FetchiumError> {
let prompt = messages
.iter()
.map(|m| {
if m.role == "system" {
format!("[System Instructions]: {}", m.content)
} else {
m.content.clone()
}
})
.collect::<Vec<_>>()
.join("\n\n");
const CAPACITY_FALLBACK: &str = "gemini-2.5-flash";
let models_to_try: Vec<String> = if model == CAPACITY_FALLBACK {
vec![model.to_string()]
} else {
vec![model.to_string(), CAPACITY_FALLBACK.to_string()]
};
let mut last_err: Option<FetchiumError> = None;
for try_model in &models_to_try {
let args = vec![
"--model".to_string(),
try_model.clone(),
"--prompt".to_string(),
prompt.clone(),
];
let output = tokio::process::Command::new("gemini")
.args(&args)
.stdin(std::process::Stdio::null())
.output()
.await
.map_err(|e| {
FetchiumError::ExternalTool(format!(
"Gemini CLI not found: {e}\nInstall: npm install -g @google/gemini-cli"
))
})?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr).into_owned();
let is_not_last = try_model != models_to_try.last().unwrap();
let is_capacity = stderr.contains("MODEL_CAPACITY_EXHAUSTED")
|| stderr.contains("No capacity available")
|| stderr.contains("RESOURCE_EXHAUSTED");
if is_capacity && is_not_last {
tracing::warn!(
"GeminiCli: '{try_model}' capacity exhausted, retrying with '{CAPACITY_FALLBACK}'"
);
last_err = Some(FetchiumError::ExternalTool(format!(
"Gemini CLI error: {stderr}"
)));
continue;
}
return Err(FetchiumError::ExternalTool(format!(
"Gemini CLI error: {stderr}"
)));
}
let content = String::from_utf8_lossy(&output.stdout).trim().to_string();
on_token(&content);
return Ok(ChatResult {
content,
model_used: format!("gemini-cli/{try_model}"),
provider: ProviderKind::GeminiCli,
});
}
Err(last_err.unwrap_or_else(|| {
FetchiumError::ExternalTool("GeminiCli: all models capacity-exhausted".into())
}))
}
#[derive(Debug)]
pub enum ProviderStatus {
Available {
model_count: Option<usize>,
},
Unavailable {
reason: String,
},
}
pub async fn check_provider(
kind: ProviderKind,
providers: &ProvidersConfig,
ai_config: &AiConfig,
) -> ProviderStatus {
let entry = providers.entry(kind);
match kind {
ProviderKind::Ollama => {
let ollama = OllamaClient::new(ai_config);
if ollama.is_available().await {
let models = ollama.list_models().await.unwrap_or_default();
ProviderStatus::Available {
model_count: Some(models.len()),
}
} else {
ProviderStatus::Unavailable {
reason: "Ollama not running (start: ollama serve)".into(),
}
}
}
ProviderKind::GeminiCli => {
let found = std::process::Command::new("gemini")
.arg("--version")
.output()
.map(|o| o.status.success())
.unwrap_or(false);
if found {
ProviderStatus::Available { model_count: None }
} else {
ProviderStatus::Unavailable {
reason: "`gemini` not in PATH (npm install -g @google/generative-ai-cli)"
.into(),
}
}
}
ProviderKind::OpenAi => {
if entry.resolve_api_key("OPENAI_API_KEY").is_some()
|| crate::ai::credentials::codex_auth_available()
{
ProviderStatus::Available { model_count: None }
} else {
ProviderStatus::Unavailable {
reason: "No API key or Codex CLI session (set OPENAI_API_KEY or run `codex auth login`)".into(),
}
}
}
ProviderKind::Anthropic => {
if entry.resolve_api_key("ANTHROPIC_API_KEY").is_some()
|| crate::ai::credentials::get_claude_code_token().is_some()
{
ProviderStatus::Available { model_count: None }
} else {
ProviderStatus::Unavailable {
reason: "No API key or Claude Code session (run `claude` once to log in)"
.into(),
}
}
}
ProviderKind::Gemini => {
if entry.resolve_api_key("GEMINI_API_KEY").is_some()
|| crate::ai::credentials::get_gemini_access_token_if_valid().is_some()
|| crate::ai::credentials::read_gemini_creds()
.map(|c| c.is_refreshable())
.unwrap_or(false)
{
ProviderStatus::Available { model_count: None }
} else {
ProviderStatus::Unavailable {
reason: "No API key or valid OAuth session.\n \
Fix: run `gemini auth login` OR \
`fetchium provider set gemini --key AIza...`"
.into(),
}
}
}
ProviderKind::OpenRouter => {
if entry.resolve_api_key("OPENROUTER_API_KEY").is_some()
|| crate::ai::credentials::read_openrouter_key().is_some()
{
ProviderStatus::Available { model_count: None }
} else {
ProviderStatus::Unavailable {
reason:
"No API key (set OPENROUTER_API_KEY or run `fetchium provider setup openrouter`)"
.into(),
}
}
}
ProviderKind::Antigravity => {
if antigravity_auth_available() {
ProviderStatus::Available { model_count: None }
} else {
ProviderStatus::Unavailable {
reason: "No OpenCode Antigravity account found.\n \
Install: npm i -g opencode-antigravity-auth\n \
Then run `opencode` to authenticate."
.into(),
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ai::providers::ProvidersConfig;
#[test]
fn chat_result_has_provider() {
let r = ChatResult {
content: "hello".into(),
model_used: "test-model".into(),
provider: ProviderKind::Gemini,
};
assert_eq!(r.provider, ProviderKind::Gemini);
}
#[tokio::test]
async fn empty_chain_returns_error() {
let mut providers = ProvidersConfig {
fallback_chain: vec!["nonexistent_provider_xyz".into()],
..ProvidersConfig::default()
};
providers.ollama.enabled = false;
providers.gemini.enabled = false;
providers.gemini_cli.enabled = false;
providers.antigravity.enabled = false;
providers.anthropic.enabled = false;
providers.openai.enabled = false;
providers.openrouter.enabled = false;
let chain = providers.resolved_chain();
assert!(
!chain.iter().any(|k| k.slug() == "nonexistent_provider_xyz"),
"Unknown provider slug should be filtered from chain"
);
}
}