Skip to main content

fetchium_core/ai/
provider_client.rs

1//! Provider-agnostic AI chat client with automatic fallback chain.
2//!
3//! Supports both **API key** and **subscription OAuth** authentication:
4//! - Gemini: reads `~/.gemini/oauth_creds.json` (Gemini CLI / "antigravity" auth)
5//! - Anthropic: reads macOS Keychain entry `"Claude Code-credentials"` (Claude Code Max/Pro)
6//! - OpenRouter, OpenAI: API key only
7//! - Ollama, GeminiCli: local, no key needed
8
9use crate::ai::credentials::{
10    antigravity_auth_available, get_antigravity_token, get_claude_code_token,
11    get_codex_token_if_valid, get_gemini_access_token_if_valid, invalidate_gemini_creds,
12    ANTIGRAVITY_ENDPOINTS, ANTIGRAVITY_VERSION, GEMINI_OAUTH_CLIENT_ID, GEMINI_OAUTH_CLIENT_SECRET,
13    GOOGLE_TOKEN_ENDPOINT,
14};
15use crate::ai::ollama::OllamaClient;
16use crate::ai::providers::{ProviderKind, ProvidersConfig};
17use crate::ai::types::AiConfig;
18use crate::error::FetchiumError;
19use futures::StreamExt;
20use serde::Deserialize;
21
22// ─── Public result type ───────────────────────────────────────────────────────
23
24/// The answer returned by whichever provider succeeded.
25#[derive(Debug)]
26pub struct ChatResult {
27    /// Generated text content.
28    pub content: String,
29    /// Identifier of the model that produced the answer (e.g. `"gemini-2.0-flash"`).
30    pub model_used: String,
31    /// Which provider was ultimately used.
32    pub provider: ProviderKind,
33}
34
35// ─── Fallback dispatch ────────────────────────────────────────────────────────
36
37/// Try each provider in the configured fallback chain and return the first success.
38///
39/// `on_token` is called for each streamed token (may be called 0 times for
40/// non-streaming or batch responses).
41pub async fn chat_with_fallback(
42    messages: &[crate::ai::types::ChatMessage],
43    model_override: Option<&str>,
44    ai_config: &AiConfig,
45    providers: &ProvidersConfig,
46    streaming: bool,
47    on_token: &mut dyn FnMut(&str),
48) -> Result<ChatResult, FetchiumError> {
49    let chain = providers.resolved_chain();
50
51    if chain.is_empty() {
52        return Err(FetchiumError::AiUnavailable(
53            "No AI providers configured. Run `fetchium provider setup` to get started.".into(),
54        ));
55    }
56
57    let mut last_error: Option<FetchiumError> = None;
58
59    let chain_len = chain.len();
60    for (idx, kind) in chain.iter().enumerate() {
61        match call_provider(
62            *kind,
63            messages,
64            model_override,
65            ai_config,
66            providers,
67            streaming,
68            on_token,
69        )
70        .await
71        {
72            Ok(result) => return Ok(result),
73            Err(e) => {
74                // Downgrade per-provider failures to DEBUG when a fallback exists.
75                // Only WARN when the entire chain is exhausted (no more fallbacks).
76                if idx + 1 < chain_len {
77                    tracing::debug!("Provider {} failed (trying next): {}", kind.slug(), e);
78                } else {
79                    tracing::warn!("All AI providers exhausted. Last error: {}", e);
80                }
81                last_error = Some(e);
82            }
83        }
84    }
85
86    Err(last_error.unwrap_or_else(|| {
87        FetchiumError::AiUnavailable("All configured AI providers failed.".into())
88    }))
89}
90
91/// Attempt a single provider and return its `ChatResult` on success.
92async fn call_provider(
93    kind: ProviderKind,
94    messages: &[crate::ai::types::ChatMessage],
95    model_override: Option<&str>,
96    ai_config: &AiConfig,
97    providers: &ProvidersConfig,
98    streaming: bool,
99    on_token: &mut dyn FnMut(&str),
100) -> Result<ChatResult, FetchiumError> {
101    let entry = providers.entry(kind);
102    let model = model_override
103        .map(|s| s.to_string())
104        .unwrap_or_else(|| entry.resolve_model(kind));
105
106    match kind {
107        ProviderKind::Ollama => call_ollama(messages, &model, ai_config, streaming, on_token).await,
108
109        ProviderKind::OpenAi => {
110            // Priority: 1) config/env API key  2) auth store API key  3) OpenAI Codex CLI OAuth session
111            let auth_store_key_openai = crate::ai::credentials::fetchium_auth_get("openai")
112                .and_then(|a| a.api_key().map(|s| s.to_string()));
113            let key = if let Some(k) = entry
114                .resolve_api_key("OPENAI_API_KEY")
115                .or(auth_store_key_openai)
116            {
117                k
118            } else if let Some(tok) = get_codex_token_if_valid() {
119                tracing::info!("Using OpenAI Codex CLI OAuth session (ChatGPT subscription)");
120                tok
121            } else {
122                return Err(FetchiumError::AiUnavailable(
123                    "OpenAI: no API key or Codex CLI session found.\n  \
124                     Options:\n  \
125                     • Set OPENAI_API_KEY env var\n  \
126                     • Log in via Codex CLI: codex auth login\n  \
127                     • Run `fetchium provider setup openai`"
128                        .into(),
129                ));
130            };
131            let base = entry
132                .base_url
133                .clone()
134                .unwrap_or_else(|| "https://api.openai.com".into());
135            call_openai_compat(
136                kind, &key, &base, &model, messages, ai_config, streaming, on_token,
137            )
138            .await
139        }
140
141        ProviderKind::Anthropic => {
142            // Priority: 1) config/env API key  2) auth store API key  3) Claude Code OAuth subscription
143            let auth_store_key_anthropic = crate::ai::credentials::fetchium_auth_get("anthropic")
144                .and_then(|a| a.api_key().map(|s| s.to_string()));
145            let (token, use_oauth) = if let Some(k) = entry
146                .resolve_api_key("ANTHROPIC_API_KEY")
147                .or(auth_store_key_anthropic)
148            {
149                (k, false)
150            } else if let Some(creds) = get_claude_code_token() {
151                tracing::info!(
152                    "Using Claude Code {} subscription (OAuth)",
153                    creds.subscription_type
154                );
155                (creds.access_token, true)
156            } else {
157                return Err(FetchiumError::AiUnavailable(
158                    "Anthropic: no API key or Claude Code session found.\n  \
159                     Options:\n  \
160                     • Set ANTHROPIC_API_KEY env var\n  \
161                     • Run `claude` once to log in (uses your Claude Max/Pro subscription)\n  \
162                     • Run `fetchium provider setup anthropic`"
163                        .into(),
164                ));
165            };
166            call_anthropic(
167                &token, use_oauth, &model, messages, ai_config, streaming, on_token,
168            )
169            .await
170        }
171
172        ProviderKind::Gemini => {
173            // Collect full key pool from all sources (env, config, auth store).
174            // Pool is tried in random order; HTTP 429 triggers automatic failover.
175            let keys = collect_gemini_keys(entry);
176            if !keys.is_empty() {
177                call_gemini_api_key_pool(&keys, &model, messages, ai_config, streaming, on_token)
178                    .await
179            } else if let Some(access_token) = get_gemini_access_token_if_valid() {
180                tracing::info!("Using Gemini OAuth (Gemini CLI subscription)");
181                call_gemini_oauth(
182                    &access_token,
183                    &model,
184                    messages,
185                    ai_config,
186                    streaming,
187                    on_token,
188                )
189                .await
190            } else {
191                // Try to refresh the expired OAuth token
192                match refresh_gemini_token_async(ai_config).await {
193                    Some(fresh_token) => {
194                        call_gemini_oauth(&fresh_token, &model, messages, ai_config, streaming, on_token).await
195                    }
196                    None => {
197                        Err(FetchiumError::AiUnavailable(
198                            "Gemini: no API keys configured and OAuth session expired.\n  \
199                             Fix (choose one):\n  \
200                             • fetchium provider set gemini --key AIza...        (set primary key)\n  \
201                             • fetchium provider set gemini --add-key AIza...    (add to key pool)\n  \
202                             • export GEMINI_API_KEY=AIza...                (env var)\n  \
203                             • gemini auth login                            (re-authenticate OAuth)\n  \
204                             Get a free key: https://aistudio.google.com/app/apikey".into(),
205                        ))
206                    }
207                }
208            }
209        }
210
211        ProviderKind::GeminiCli => call_gemini_cli(&model, messages, on_token).await,
212
213        ProviderKind::OpenRouter => {
214            // Priority: 1) config/env  2) ~/.openrouter/config.json
215            let key = entry.resolve_api_key("OPENROUTER_API_KEY")
216                .or_else(crate::ai::credentials::read_openrouter_key)
217                .ok_or_else(|| FetchiumError::AiUnavailable(
218                    "OpenRouter API key not set. Set OPENROUTER_API_KEY or run `fetchium provider setup openrouter`.".into(),
219                ))?;
220            let base = entry
221                .base_url
222                .clone()
223                .unwrap_or_else(|| "https://openrouter.ai/api".into());
224            call_openai_compat(
225                kind, &key, &base, &model, messages, ai_config, streaming, on_token,
226            )
227            .await
228        }
229
230        ProviderKind::Antigravity => {
231            // Refresh the antigravity Google OAuth access token on every call.
232            // refresh_antigravity_token uses reqwest::blocking — must run in spawn_blocking.
233            let token_result = tokio::task::spawn_blocking(get_antigravity_token)
234                .await
235                .unwrap_or(None);
236            let (access_token, project_id) = token_result.ok_or_else(|| {
237                FetchiumError::AiUnavailable(
238                    "Antigravity: no OpenCode account found.\n  \
239                     Install OpenCode and add an account: https://opencode.ai\n  \
240                     Then install the plugin: npm i -g opencode-antigravity-auth\n  \
241                     Run `fetchium provider setup antigravity` to verify."
242                        .into(),
243                )
244            })?;
245            call_antigravity(
246                &access_token,
247                &project_id,
248                &model,
249                messages,
250                ai_config,
251                streaming,
252                on_token,
253            )
254            .await
255        }
256    }
257}
258
259// ─── Ollama ───────────────────────────────────────────────────────────────────
260
261async fn call_ollama(
262    messages: &[crate::ai::types::ChatMessage],
263    model: &str,
264    ai_config: &AiConfig,
265    streaming: bool,
266    on_token: &mut dyn FnMut(&str),
267) -> Result<ChatResult, FetchiumError> {
268    let ollama = OllamaClient::new(ai_config);
269
270    if !ollama.is_available().await {
271        return Err(FetchiumError::AiUnavailable(
272            "Ollama is not running. Start it with: ollama serve".into(),
273        ));
274    }
275
276    let available = ollama.list_models().await.unwrap_or_default();
277    let model_name = if available
278        .iter()
279        .any(|m| m.name == model || m.name.starts_with(model))
280    {
281        model.to_string()
282    } else if let Some(first) = available.first() {
283        tracing::warn!(
284            "Requested model '{model}' not found; using '{}'",
285            first.name
286        );
287        first.name.clone()
288    } else {
289        return Err(FetchiumError::AiUnavailable(format!(
290            "No models installed in Ollama. Run: ollama pull {model}"
291        )));
292    };
293
294    let content = if streaming {
295        ollama
296            .chat_stream(&model_name, messages, ai_config.temperature, on_token)
297            .await?
298    } else {
299        ollama
300            .chat(&model_name, messages, ai_config.temperature)
301            .await?
302    };
303
304    Ok(ChatResult {
305        content,
306        model_used: model_name,
307        provider: ProviderKind::Ollama,
308    })
309}
310
311// ─── OpenAI-compatible (OpenAI + OpenRouter) ──────────────────────────────────
312
313#[derive(Deserialize)]
314struct OaiResponse {
315    choices: Vec<OaiChoice>,
316    model: Option<String>,
317}
318#[derive(Deserialize)]
319struct OaiChoice {
320    message: OaiMessage,
321}
322#[derive(Deserialize)]
323struct OaiMessage {
324    content: String,
325}
326#[derive(Deserialize)]
327struct OaiStreamChunk {
328    choices: Vec<OaiStreamChoice>,
329}
330#[derive(Deserialize)]
331struct OaiStreamChoice {
332    delta: OaiDelta,
333}
334#[derive(Deserialize, Default)]
335struct OaiDelta {
336    #[serde(default)]
337    content: Option<String>,
338}
339
340#[allow(clippy::too_many_arguments)]
341async fn call_openai_compat(
342    kind: ProviderKind,
343    api_key: &str,
344    base_url: &str,
345    model: &str,
346    messages: &[crate::ai::types::ChatMessage],
347    ai_config: &AiConfig,
348    streaming: bool,
349    on_token: &mut dyn FnMut(&str),
350) -> Result<ChatResult, FetchiumError> {
351    let url = format!("{}/v1/chat/completions", base_url.trim_end_matches('/'));
352    let oai_msgs: Vec<serde_json::Value> = messages
353        .iter()
354        .map(|m| serde_json::json!({"role": m.role, "content": m.content}))
355        .collect();
356
357    let body = serde_json::json!({
358        "model": model,
359        "messages": oai_msgs,
360        "temperature": ai_config.temperature,
361        "stream": streaming,
362    });
363
364    let client = reqwest::Client::builder()
365        .timeout(std::time::Duration::from_secs(ai_config.timeout_secs))
366        .build()
367        .map_err(|e| FetchiumError::AiUnavailable(format!("HTTP client build error: {e}")))?;
368
369    let mut req = client
370        .post(&url)
371        .header("Authorization", format!("Bearer {api_key}"))
372        .header("Content-Type", "application/json");
373
374    // OpenRouter requires these headers for proper attribution
375    if kind == ProviderKind::OpenRouter {
376        req = req
377            .header("HTTP-Referer", "https://github.com/fetchium/fetchium")
378            .header("X-Title", "Fetchium");
379    }
380
381    let resp = req.json(&body).send().await.map_err(|e| {
382        FetchiumError::AiUnavailable(format!("Request to {} failed: {e}", kind.display_name()))
383    })?;
384
385    if !resp.status().is_success() {
386        let status = resp.status();
387        let body_text = resp.text().await.unwrap_or_default();
388        return Err(FetchiumError::AiUnavailable(format!(
389            "{} API error {status}: {body_text}",
390            kind.display_name()
391        )));
392    }
393
394    if streaming {
395        let mut stream = resp.bytes_stream();
396        let mut full = String::new();
397        let mut buf = Vec::<u8>::new();
398
399        while let Some(chunk) = stream.next().await {
400            let bytes =
401                chunk.map_err(|e| FetchiumError::AiUnavailable(format!("Stream error: {e}")))?;
402            buf.extend_from_slice(&bytes);
403
404            while let Some(pos) = buf.iter().position(|&b| b == b'\n') {
405                let line: Vec<u8> = buf.drain(..=pos).collect();
406                let s = std::str::from_utf8(&line).unwrap_or("").trim();
407                if let Some(data) = s.strip_prefix("data: ") {
408                    if data == "[DONE]" {
409                        return Ok(ChatResult {
410                            content: full,
411                            model_used: model.to_string(),
412                            provider: kind,
413                        });
414                    }
415                    if let Ok(chunk) = serde_json::from_str::<OaiStreamChunk>(data) {
416                        for choice in &chunk.choices {
417                            if let Some(ref tok) = choice.delta.content {
418                                on_token(tok);
419                                full.push_str(tok);
420                            }
421                        }
422                    }
423                }
424            }
425        }
426        Ok(ChatResult {
427            content: full,
428            model_used: model.to_string(),
429            provider: kind,
430        })
431    } else {
432        let parsed: OaiResponse = resp
433            .json()
434            .await
435            .map_err(|e| FetchiumError::AiUnavailable(format!("Invalid response: {e}")))?;
436        let content = parsed
437            .choices
438            .into_iter()
439            .next()
440            .map(|c| c.message.content)
441            .unwrap_or_default();
442        let model_used = parsed.model.unwrap_or_else(|| model.to_string());
443        Ok(ChatResult {
444            content,
445            model_used,
446            provider: kind,
447        })
448    }
449}
450
451// ─── Anthropic ────────────────────────────────────────────────────────────────
452
453#[derive(Deserialize)]
454struct AnthropicResponse {
455    content: Vec<AnthropicBlock>,
456    model: String,
457}
458#[derive(Deserialize)]
459struct AnthropicBlock {
460    #[serde(rename = "type")]
461    kind: String,
462    text: Option<String>,
463}
464
465async fn call_anthropic(
466    token: &str,
467    use_oauth: bool, // true = Claude Code subscription (Bearer), false = API key (x-api-key)
468    model: &str,
469    messages: &[crate::ai::types::ChatMessage],
470    ai_config: &AiConfig,
471    streaming: bool,
472    on_token: &mut dyn FnMut(&str),
473) -> Result<ChatResult, FetchiumError> {
474    let mut system_text = String::new();
475    let mut anth_msgs: Vec<serde_json::Value> = Vec::new();
476
477    for m in messages {
478        if m.role == "system" {
479            system_text = m.content.clone();
480        } else {
481            anth_msgs.push(serde_json::json!({"role": m.role, "content": m.content}));
482        }
483    }
484    if anth_msgs.is_empty() {
485        anth_msgs.push(serde_json::json!({"role": "user", "content": "Hello"}));
486    }
487
488    let mut body = serde_json::json!({
489        "model": model,
490        "max_tokens": ai_config.max_context_tokens,
491        "temperature": ai_config.temperature,
492        "messages": anth_msgs,
493        "stream": streaming,
494    });
495    if !system_text.is_empty() {
496        body["system"] = serde_json::Value::String(system_text);
497    }
498
499    let client = reqwest::Client::builder()
500        .timeout(std::time::Duration::from_secs(ai_config.timeout_secs))
501        .build()
502        .map_err(|e| FetchiumError::AiUnavailable(format!("HTTP client build error: {e}")))?;
503
504    let mut req = client
505        .post("https://api.anthropic.com/v1/messages")
506        .header("anthropic-version", "2023-06-01")
507        .header("Content-Type", "application/json");
508
509    if use_oauth {
510        // Claude Code subscription: OAuth Bearer token (sk-ant-oat01-…)
511        req = req.header("Authorization", format!("Bearer {token}"));
512    } else {
513        // Direct API key
514        req = req.header("x-api-key", token);
515    }
516
517    let resp = req
518        .json(&body)
519        .send()
520        .await
521        .map_err(|e| FetchiumError::AiUnavailable(format!("Anthropic request failed: {e}")))?;
522
523    if !resp.status().is_success() {
524        let status = resp.status();
525        let body_text = resp.text().await.unwrap_or_default();
526        return Err(FetchiumError::AiUnavailable(format!(
527            "Anthropic API error {status}: {body_text}"
528        )));
529    }
530
531    if streaming {
532        let mut stream = resp.bytes_stream();
533        let mut full = String::new();
534        let mut buf = Vec::<u8>::new();
535
536        while let Some(chunk) = stream.next().await {
537            let bytes =
538                chunk.map_err(|e| FetchiumError::AiUnavailable(format!("Stream error: {e}")))?;
539            buf.extend_from_slice(&bytes);
540
541            while let Some(pos) = buf.iter().position(|&b| b == b'\n') {
542                let line: Vec<u8> = buf.drain(..=pos).collect();
543                let s = std::str::from_utf8(&line).unwrap_or("").trim();
544                if let Some(data) = s.strip_prefix("data: ") {
545                    if let Ok(ev) = serde_json::from_str::<serde_json::Value>(data) {
546                        if ev["type"] == "content_block_delta" {
547                            if let Some(text) = ev["delta"]["text"].as_str() {
548                                on_token(text);
549                                full.push_str(text);
550                            }
551                        }
552                    }
553                }
554            }
555        }
556        Ok(ChatResult {
557            content: full,
558            model_used: model.to_string(),
559            provider: ProviderKind::Anthropic,
560        })
561    } else {
562        let parsed: AnthropicResponse = resp.json().await.map_err(|e| {
563            FetchiumError::AiUnavailable(format!("Invalid Anthropic response: {e}"))
564        })?;
565        let content = parsed
566            .content
567            .into_iter()
568            .filter(|b| b.kind == "text")
569            .filter_map(|b| b.text)
570            .collect::<Vec<_>>()
571            .join("");
572        Ok(ChatResult {
573            content,
574            model_used: parsed.model,
575            provider: ProviderKind::Anthropic,
576        })
577    }
578}
579
580// ─── Google Gemini REST API ───────────────────────────────────────────────────
581
582#[derive(Deserialize)]
583struct GeminiResponse {
584    candidates: Vec<GeminiCandidate>,
585}
586#[derive(Deserialize)]
587struct GeminiCandidate {
588    content: GeminiContent,
589}
590#[derive(Deserialize)]
591struct GeminiContent {
592    parts: Vec<GeminiPart>,
593}
594#[derive(Deserialize)]
595struct GeminiPart {
596    text: Option<String>,
597}
598
599/// Build Gemini request contents from ChatMessage list (system → prepended to first user turn).
600fn gemini_build_contents(messages: &[crate::ai::types::ChatMessage]) -> Vec<serde_json::Value> {
601    let mut system_text = String::new();
602    let mut contents: Vec<serde_json::Value> = Vec::new();
603
604    for m in messages {
605        if m.role == "system" {
606            system_text = m.content.clone();
607        } else {
608            let role = if m.role == "assistant" {
609                "model"
610            } else {
611                "user"
612            };
613            contents.push(serde_json::json!({
614                "role": role,
615                "parts": [{"text": m.content}],
616            }));
617        }
618    }
619
620    if !system_text.is_empty() {
621        if let Some(first) = contents.first_mut() {
622            if first["role"] == "user" {
623                let original = first["parts"][0]["text"].as_str().unwrap_or("").to_string();
624                first["parts"][0]["text"] =
625                    serde_json::Value::String(format!("{system_text}\n\n{original}"));
626            }
627        }
628    }
629    if contents.is_empty() {
630        contents.push(serde_json::json!({"role": "user", "parts": [{"text": "Hello"}]}));
631    }
632    contents
633}
634
635/// Parse a Gemini SSE stream into a complete response string, calling `on_token` for each chunk.
636async fn gemini_read_stream(
637    resp: reqwest::Response,
638    model: &str,
639    on_token: &mut dyn FnMut(&str),
640) -> Result<ChatResult, FetchiumError> {
641    gemini_read_stream_with_provider(resp, model, ProviderKind::Gemini, on_token).await
642}
643
644/// Parse a Gemini SSE stream, tagging the result with the given provider.
645async fn gemini_read_stream_with_provider(
646    resp: reqwest::Response,
647    model: &str,
648    provider: ProviderKind,
649    on_token: &mut dyn FnMut(&str),
650) -> Result<ChatResult, FetchiumError> {
651    let mut stream = resp.bytes_stream();
652    let mut full = String::new();
653    let mut buf = Vec::<u8>::new();
654
655    while let Some(chunk) = stream.next().await {
656        let bytes =
657            chunk.map_err(|e| FetchiumError::AiUnavailable(format!("Stream error: {e}")))?;
658        buf.extend_from_slice(&bytes);
659
660        while let Some(pos) = buf.iter().position(|&b| b == b'\n') {
661            let line: Vec<u8> = buf.drain(..=pos).collect();
662            let s = std::str::from_utf8(&line).unwrap_or("").trim();
663            if let Some(data) = s.strip_prefix("data: ") {
664                if let Ok(parsed) = serde_json::from_str::<GeminiResponse>(data) {
665                    for cand in parsed.candidates {
666                        for part in cand.content.parts {
667                            if let Some(text) = part.text {
668                                on_token(&text);
669                                full.push_str(&text);
670                            }
671                        }
672                    }
673                }
674            }
675        }
676    }
677    Ok(ChatResult {
678        content: full,
679        model_used: model.to_string(),
680        provider,
681    })
682}
683
684// ─── Gemini key pool helpers ─────────────────────────────────────────────────
685
686/// Mask an API key for safe logging: shows first 8 and last 4 characters.
687fn mask_key(key: &str) -> String {
688    if key.len() > 14 {
689        format!("{}…{}", &key[..8], &key[key.len() - 4..])
690    } else {
691        "****".into()
692    }
693}
694
695/// Collect all Gemini API keys from every configured source (deduplicated).
696///
697/// Collect all Gemini API keys from every source into a deduplicated pool.
698///
699/// Sources (in priority order):
700///
701/// 1. `GEMINI_API_KEYS` env var — comma-separated pool
702/// 2. `GEMINI_API_KEY` env var — single key
703/// 3. Config `[ai.providers.gemini].api_keys` — array of keys
704/// 4. Config `[ai.providers.gemini].api_key` — single key
705/// 5. Auth store `~/.fetchium/auth.json` (single `Api` or `ApiPool`)
706fn collect_gemini_keys(entry: &crate::ai::providers::ProviderEntry) -> Vec<String> {
707    let mut keys: Vec<String> = Vec::new();
708
709    let mut push = |k: String| {
710        if !k.is_empty() && !keys.contains(&k) {
711            keys.push(k);
712        }
713    };
714
715    // 1. GEMINI_API_KEYS — comma-separated pool (highest priority for pool users)
716    if let Ok(multi) = std::env::var("GEMINI_API_KEYS") {
717        for k in multi.split(',') {
718            push(k.trim().to_string());
719        }
720    }
721
722    // 2. GEMINI_API_KEY — single key env var
723    if let Ok(k) = std::env::var("GEMINI_API_KEY") {
724        push(k);
725    }
726
727    // 3. Config api_keys array (unlimited keys)
728    for k in &entry.api_keys {
729        let k = k.trim().to_string();
730        if !k.starts_with("your-") && k != "REPLACE_ME" {
731            push(k);
732        }
733    }
734
735    // 4. Config api_key (single)
736    if let Some(ref k) = entry.api_key {
737        if !k.starts_with("your-") && k != "REPLACE_ME" {
738            push(k.clone());
739        }
740    }
741
742    // 5. Auth store (Api single or ApiPool)
743    if let Some(auth) = crate::ai::credentials::fetchium_auth_get("gemini") {
744        for k in auth.api_keys() {
745            push(k.to_string());
746        }
747    }
748
749    tracing::debug!(
750        "Gemini key pool: {} unique key(s) from all sources",
751        keys.len()
752    );
753    keys
754}
755
756/// Global round-robin counter for Gemini key rotation.
757/// Uses atomic increment so concurrent requests spread across keys evenly.
758static GEMINI_KEY_COUNTER: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
759
760/// Per-key rate-limit cooldown tracker.
761/// Maps key index → Instant when cooldown expires. Keys in cooldown are skipped.
762static GEMINI_COOLDOWNS: std::sync::OnceLock<
763    std::sync::Mutex<std::collections::HashMap<usize, std::time::Instant>>,
764> = std::sync::OnceLock::new();
765
766fn gemini_cooldowns(
767) -> &'static std::sync::Mutex<std::collections::HashMap<usize, std::time::Instant>> {
768    GEMINI_COOLDOWNS.get_or_init(|| std::sync::Mutex::new(std::collections::HashMap::new()))
769}
770
771/// Cooldown duration after a 429 — skip key for this long before retrying it.
772const RATE_LIMIT_COOLDOWN: std::time::Duration = std::time::Duration::from_secs(60);
773
774/// Call the Gemini REST API with a **pool of API keys**.
775///
776/// Uses round-robin rotation with rate-limit awareness:
777/// - Keys are tried starting from a global atomic counter (even load distribution)
778/// - Keys that recently got 429'd are in cooldown and skipped
779/// - On 429, the key enters cooldown and the next key is tried
780/// - Supports unlimited keys from env, config array, config single, and auth store
781async fn call_gemini_api_key_pool(
782    keys: &[String],
783    model: &str,
784    messages: &[crate::ai::types::ChatMessage],
785    ai_config: &AiConfig,
786    streaming: bool,
787    on_token: &mut dyn FnMut(&str),
788) -> Result<ChatResult, FetchiumError> {
789    let pool_size = keys.len();
790    let start_idx =
791        GEMINI_KEY_COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed) % pool_size;
792    let now = std::time::Instant::now();
793
794    // Clean expired cooldowns.
795    if let Ok(mut cooldowns) = gemini_cooldowns().lock() {
796        cooldowns.retain(|_, expires| *expires > now);
797    }
798
799    let mut rate_limited_count = 0usize;
800    let mut tried = 0usize;
801
802    // Try all keys starting from round-robin offset, skipping cooled-down keys.
803    for offset in 0..pool_size {
804        let idx = (start_idx + offset) % pool_size;
805
806        // Skip keys in cooldown.
807        if let Ok(cooldowns) = gemini_cooldowns().lock() {
808            if let Some(expires) = cooldowns.get(&idx) {
809                if *expires > now {
810                    tracing::debug!(
811                        "Gemini: key {} in cooldown ({:.0}s left), skipping",
812                        mask_key(&keys[idx]),
813                        expires.duration_since(now).as_secs_f32()
814                    );
815                    continue;
816                }
817            }
818        }
819
820        let key = &keys[idx];
821        let masked = mask_key(key);
822        tried += 1;
823        tracing::debug!(
824            "Gemini: trying key {} (slot {}/{}, round-robin offset {})",
825            masked,
826            idx + 1,
827            pool_size,
828            start_idx
829        );
830
831        match call_gemini_api_key(key, model, messages, ai_config, streaming, on_token).await {
832            Ok(result) => {
833                if rate_limited_count > 0 {
834                    tracing::info!(
835                        "Gemini: succeeded with key {} after {} rate-limited key(s)",
836                        masked,
837                        rate_limited_count
838                    );
839                }
840                return Ok(result);
841            }
842            // 429 / RESOURCE_EXHAUSTED → put key in cooldown, try next
843            Err(FetchiumError::AiUnavailable(ref msg))
844                if msg.contains("429") || msg.contains("RESOURCE_EXHAUSTED") =>
845            {
846                rate_limited_count += 1;
847                if let Ok(mut cooldowns) = gemini_cooldowns().lock() {
848                    cooldowns.insert(idx, now + RATE_LIMIT_COOLDOWN);
849                }
850                tracing::warn!(
851                    "Gemini key {} rate limited → 60s cooldown ({}/{} pool keys exhausted)",
852                    masked,
853                    rate_limited_count,
854                    pool_size
855                );
856            }
857            // Any other error (auth, network, bad request…) propagates immediately.
858            Err(e) => return Err(e),
859        }
860    }
861
862    if tried == 0 {
863        Err(FetchiumError::AiUnavailable(format!(
864            "All {} Gemini API key(s) are in cooldown (rate limited).\n  \
865             Cooldown expires in ~60s. Add more keys for uninterrupted service:\n  \
866             • Config: [ai.providers.gemini] api_keys = [\"key1\", \"key2\", ...]\n  \
867             • Env: GEMINI_API_KEYS=key1,key2,...\n  \
868             Get free keys: https://aistudio.google.com/app/apikey",
869            pool_size
870        )))
871    } else {
872        Err(FetchiumError::AiUnavailable(format!(
873            "All {} Gemini API key(s) are rate limited (HTTP 429).\n  \
874             Add more keys for uninterrupted service:\n  \
875             • Config: [ai.providers.gemini] api_keys = [\"key1\", \"key2\", ...]\n  \
876             • Env: GEMINI_API_KEYS=key1,key2,...\n  \
877             Get free keys: https://aistudio.google.com/app/apikey",
878            pool_size
879        )))
880    }
881}
882
883/// Call Gemini REST API with an **API key** (`?key=` query parameter).
884async fn call_gemini_api_key(
885    api_key: &str,
886    model: &str,
887    messages: &[crate::ai::types::ChatMessage],
888    ai_config: &AiConfig,
889    streaming: bool,
890    on_token: &mut dyn FnMut(&str),
891) -> Result<ChatResult, FetchiumError> {
892    let contents = gemini_build_contents(messages);
893
894    // Gemini 2.5+ models use "thinking" by default, adding 5-10s of latency.
895    // Disable thinking for speed unless explicitly enabled via --think flag.
896    let is_thinking_model = model.contains("2.5") || model.contains("thinking");
897    let disable_thinking = is_thinking_model && !ai_config.thinking;
898    let body = if disable_thinking {
899        serde_json::json!({
900            "contents": contents,
901            "generationConfig": {
902                "maxOutputTokens": ai_config.max_context_tokens,
903                "temperature": ai_config.temperature,
904                "thinkingConfig": { "thinkingBudget": 0 },
905            },
906        })
907    } else {
908        serde_json::json!({
909            "contents": contents,
910            "generationConfig": {
911                "maxOutputTokens": ai_config.max_context_tokens,
912                "temperature": ai_config.temperature,
913            },
914        })
915    };
916
917    let url = if streaming {
918        format!(
919            "https://generativelanguage.googleapis.com/v1beta/models/{model}:streamGenerateContent?alt=sse&key={api_key}"
920        )
921    } else {
922        format!(
923            "https://generativelanguage.googleapis.com/v1beta/models/{model}:generateContent?key={api_key}"
924        )
925    };
926
927    let client = reqwest::Client::builder()
928        .timeout(std::time::Duration::from_secs(ai_config.timeout_secs))
929        .build()
930        .map_err(|e| FetchiumError::AiUnavailable(format!("HTTP client build error: {e}")))?;
931
932    let resp = client
933        .post(&url)
934        .header("Content-Type", "application/json")
935        .json(&body)
936        .send()
937        .await
938        .map_err(|e| FetchiumError::AiUnavailable(format!("Gemini request failed: {e}")))?;
939
940    if !resp.status().is_success() {
941        let status = resp.status();
942        let body_text = resp.text().await.unwrap_or_default();
943        return Err(FetchiumError::AiUnavailable(format!(
944            "Gemini API error {status}: {body_text}"
945        )));
946    }
947
948    if streaming {
949        gemini_read_stream(resp, model, on_token).await
950    } else {
951        let parsed: GeminiResponse = resp
952            .json()
953            .await
954            .map_err(|e| FetchiumError::AiUnavailable(format!("Invalid Gemini response: {e}")))?;
955        let content = parsed
956            .candidates
957            .into_iter()
958            .next()
959            .map(|c| {
960                c.content
961                    .parts
962                    .into_iter()
963                    .filter_map(|p| p.text)
964                    .collect::<Vec<_>>()
965                    .join("")
966            })
967            .unwrap_or_default();
968        Ok(ChatResult {
969            content,
970            model_used: model.to_string(),
971            provider: ProviderKind::Gemini,
972        })
973    }
974}
975
976/// Call Gemini REST API with a **Google OAuth Bearer token** (antigravity / Gemini CLI subscription).
977///
978/// No `?key=` parameter — authentication is via `Authorization: Bearer {access_token}`.
979async fn call_gemini_oauth(
980    access_token: &str,
981    model: &str,
982    messages: &[crate::ai::types::ChatMessage],
983    ai_config: &AiConfig,
984    streaming: bool,
985    on_token: &mut dyn FnMut(&str),
986) -> Result<ChatResult, FetchiumError> {
987    let contents = gemini_build_contents(messages);
988    let is_thinking_model = model.contains("2.5") || model.contains("thinking");
989    let body = if is_thinking_model {
990        serde_json::json!({
991            "contents": contents,
992            "generationConfig": {
993                "maxOutputTokens": ai_config.max_context_tokens,
994                "temperature": ai_config.temperature,
995                "thinkingConfig": { "thinkingBudget": 0 },
996            },
997        })
998    } else {
999        serde_json::json!({
1000            "contents": contents,
1001            "generationConfig": {
1002                "maxOutputTokens": ai_config.max_context_tokens,
1003                "temperature": ai_config.temperature,
1004            },
1005        })
1006    };
1007
1008    // OAuth endpoint: no `?key=` parameter
1009    let endpoint = if streaming {
1010        "streamGenerateContent?alt=sse"
1011    } else {
1012        "generateContent"
1013    };
1014    let url = format!("https://generativelanguage.googleapis.com/v1beta/models/{model}:{endpoint}");
1015
1016    let client = reqwest::Client::builder()
1017        .timeout(std::time::Duration::from_secs(ai_config.timeout_secs))
1018        .build()
1019        .map_err(|e| FetchiumError::AiUnavailable(format!("HTTP client build error: {e}")))?;
1020
1021    let resp = client
1022        .post(&url)
1023        .header("Authorization", format!("Bearer {access_token}"))
1024        .header("Content-Type", "application/json")
1025        .json(&body)
1026        .send()
1027        .await
1028        .map_err(|e| FetchiumError::AiUnavailable(format!("Gemini OAuth request failed: {e}")))?;
1029
1030    if !resp.status().is_success() {
1031        let status = resp.status();
1032        let body_text = resp.text().await.unwrap_or_default();
1033        // 403 SCOPE_INSUFFICIENT means the Gemini CLI OAuth token was obtained with
1034        // limited scopes — it cannot be used for the Generative Language REST API.
1035        // Do NOT clear credentials (they're still valid for the `gemini` CLI subprocess).
1036        if status == reqwest::StatusCode::FORBIDDEN
1037            && body_text.contains("ACCESS_TOKEN_SCOPE_INSUFFICIENT")
1038        {
1039            return Err(FetchiumError::AiUnavailable(
1040                "Gemini OAuth token has insufficient scopes for the REST API.
1041                   Fix (choose one):
1042                   • fetchium provider auth gemini       (interactive setup — API key or OAuth)
1043                   • export GEMINI_API_KEY=AIza...   (free key: aistudio.google.com/app/apikey)
1044                   • fetchium provider chain gemini_cli   (use the local `gemini` CLI instead)"
1045                    .into(),
1046            ));
1047        }
1048        return Err(FetchiumError::AiUnavailable(format!(
1049            "Gemini OAuth API error {status}: {body_text}"
1050        )));
1051    }
1052
1053    if streaming {
1054        gemini_read_stream(resp, model, on_token).await
1055    } else {
1056        let parsed: GeminiResponse = resp.json().await.map_err(|e| {
1057            FetchiumError::AiUnavailable(format!("Invalid Gemini OAuth response: {e}"))
1058        })?;
1059        let content = parsed
1060            .candidates
1061            .into_iter()
1062            .next()
1063            .map(|c| {
1064                c.content
1065                    .parts
1066                    .into_iter()
1067                    .filter_map(|p| p.text)
1068                    .collect::<Vec<_>>()
1069                    .join("")
1070            })
1071            .unwrap_or_default();
1072        Ok(ChatResult {
1073            content,
1074            model_used: model.to_string(),
1075            provider: ProviderKind::Gemini,
1076        })
1077    }
1078}
1079
1080/// Refresh an expired Gemini OAuth token using the stored refresh_token.
1081///
1082/// Uses the same OAuth client ID/secret as the Gemini CLI (installed-app flow).
1083/// Returns the new access token string on success, or `None` if refresh fails.
1084async fn refresh_gemini_token_async(_ai_config: &AiConfig) -> Option<String> {
1085    use crate::ai::credentials::read_gemini_creds;
1086
1087    let creds = read_gemini_creds()?;
1088    let refresh_token = &creds.refresh_token;
1089
1090    let client = reqwest::Client::builder()
1091        .timeout(std::time::Duration::from_secs(15))
1092        .build()
1093        .ok()?;
1094
1095    let params = [
1096        ("client_id", GEMINI_OAUTH_CLIENT_ID),
1097        ("client_secret", GEMINI_OAUTH_CLIENT_SECRET),
1098        ("refresh_token", refresh_token.as_str()),
1099        ("grant_type", "refresh_token"),
1100    ];
1101
1102    let resp = client
1103        .post(GOOGLE_TOKEN_ENDPOINT)
1104        .form(&params)
1105        .send()
1106        .await
1107        .ok()?;
1108
1109    if !resp.status().is_success() {
1110        let status = resp.status();
1111        if status == reqwest::StatusCode::UNAUTHORIZED || status == reqwest::StatusCode::BAD_REQUEST
1112        {
1113            // Refresh token is revoked or invalid — clear stale creds so future
1114            // check_provider calls correctly report Gemini as unavailable.
1115            invalidate_gemini_creds();
1116            tracing::warn!(
1117                "Gemini refresh token revoked (HTTP {status}) — credentials cleared.\n  \
1118                 Fix: run `gemini auth login` to get a new session, then retry."
1119            );
1120        } else {
1121            tracing::warn!("Gemini token refresh failed: HTTP {status}");
1122        }
1123        return None;
1124    }
1125
1126    let json: serde_json::Value = resp.json().await.ok()?;
1127    let new_token = json["access_token"].as_str()?.to_string();
1128
1129    // Persist the refreshed token back to ~/.gemini/oauth_creds.json so subsequent
1130    // calls within this session and future sessions can use it directly.
1131    if let Some(path) = dirs::home_dir().map(|h| h.join(".gemini").join("oauth_creds.json")) {
1132        let expires_in: u64 = json["expires_in"].as_u64().unwrap_or(3600);
1133        let now_ms = std::time::SystemTime::now()
1134            .duration_since(std::time::UNIX_EPOCH)
1135            .map(|d| d.as_millis() as u64)
1136            .unwrap_or(0);
1137        let updated = serde_json::json!({
1138            "access_token":  new_token,
1139            "refresh_token": refresh_token,
1140            "expiry_date":   now_ms + expires_in * 1000,
1141            "token_type":    "Bearer",
1142        });
1143        let _ = std::fs::write(
1144            &path,
1145            serde_json::to_string_pretty(&updated).unwrap_or_default(),
1146        );
1147        tracing::debug!("Gemini OAuth token refreshed and saved to {:?}", path);
1148    }
1149
1150    Some(new_token)
1151}
1152
1153// ─── OpenCode Antigravity (Google Cloud Code Assist) ─────────────────────────
1154//
1155// Antigravity routes Gemini-format requests to Google's internal Cloud Code Assist
1156// API, which proxies them to Gemini 3 (Pro/Flash) and Claude (Sonnet/Opus) models
1157// without requiring any API key — only a Google OAuth access token from the
1158// `opencode-antigravity-auth` plugin.
1159//
1160// Request format: same as Gemini REST API (contents[] body)
1161// Auth:           Authorization: Bearer {access_token}
1162// Extra headers:  User-Agent, X-Goog-Api-Client, Client-Metadata (required for routing)
1163// Endpoint:       {cloudcode-pa}/v1internal:generateContent[?alt=sse]
1164
1165/// Call the Google Cloud Code Assist API via Antigravity OAuth credentials.
1166///
1167/// The body format is identical to the Gemini REST API (`contents[]`).
1168/// The endpoint routes automatically based on the model name prefix:
1169/// - `antigravity-gemini-*`  → Gemini 3 models
1170/// - `antigravity-claude-*`  → Claude models via Vertex AI
1171#[allow(clippy::too_many_arguments)]
1172async fn call_antigravity(
1173    access_token: &str,
1174    _project_id: &str,
1175    model: &str,
1176    messages: &[crate::ai::types::ChatMessage],
1177    ai_config: &AiConfig,
1178    streaming: bool,
1179    on_token: &mut dyn FnMut(&str),
1180) -> Result<ChatResult, FetchiumError> {
1181    let contents = gemini_build_contents(messages);
1182    let is_thinking_model = model.contains("2.5") || model.contains("thinking");
1183    let body = if is_thinking_model {
1184        serde_json::json!({
1185            "contents": contents,
1186            "generationConfig": {
1187                "maxOutputTokens": ai_config.max_context_tokens,
1188                "temperature": ai_config.temperature,
1189                "thinkingConfig": { "thinkingBudget": 0 },
1190            },
1191            "model": model,
1192        })
1193    } else {
1194        serde_json::json!({
1195            "contents": contents,
1196            "generationConfig": {
1197                "maxOutputTokens": ai_config.max_context_tokens,
1198                "temperature": ai_config.temperature,
1199            },
1200            "model": model,
1201        })
1202    };
1203
1204    let ua = format!(
1205        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 \
1206         (KHTML, like Gecko) Antigravity/{ANTIGRAVITY_VERSION} \
1207         Chrome/138.0.7204.235 Electron/37.3.1 Safari/537.36"
1208    );
1209
1210    let client = reqwest::Client::builder()
1211        .timeout(std::time::Duration::from_secs(ai_config.timeout_secs))
1212        .user_agent(&ua)
1213        .build()
1214        .map_err(|e| FetchiumError::AiUnavailable(format!("HTTP client build error: {e}")))?;
1215
1216    let action = if streaming {
1217        "generateContent?alt=sse"
1218    } else {
1219        "generateContent"
1220    };
1221
1222    // Try each endpoint in order; first success wins
1223    let mut last_err = String::new();
1224    for &endpoint in ANTIGRAVITY_ENDPOINTS {
1225        let url = format!("{endpoint}/v1internal:{action}");
1226
1227        let resp = client
1228            .post(&url)
1229            .header("Authorization", format!("Bearer {access_token}"))
1230            .header("Content-Type", "application/json")
1231            .header("X-Goog-Api-Client", "google-cloud-sdk vscode_cloudshelleditor/0.1")
1232            .header("Client-Metadata", r#"{"ideType":"IDE_UNSPECIFIED","platform":"PLATFORM_UNSPECIFIED","pluginType":"GEMINI"}"#)
1233            .json(&body)
1234            .send()
1235            .await;
1236
1237        let resp = match resp {
1238            Ok(r) => r,
1239            Err(e) => {
1240                last_err = format!("Request to {endpoint} failed: {e}");
1241                tracing::debug!("Antigravity endpoint {endpoint} failed: {e}");
1242                continue;
1243            }
1244        };
1245
1246        let status = resp.status();
1247        if status == reqwest::StatusCode::UNAUTHORIZED || status == reqwest::StatusCode::FORBIDDEN {
1248            return Err(FetchiumError::AiUnavailable(
1249                "Antigravity: authentication failed — re-run OpenCode to refresh your session.\n  \
1250                 Run `opencode` once, then retry."
1251                    .into(),
1252            ));
1253        }
1254        if !status.is_success() {
1255            let body_text = resp.text().await.unwrap_or_default();
1256            last_err = format!("Antigravity API error {status} at {endpoint}: {body_text}");
1257            tracing::debug!("{last_err}");
1258            continue;
1259        }
1260
1261        return if streaming {
1262            // Gemini SSE format
1263            gemini_read_stream_with_provider(resp, model, ProviderKind::Antigravity, on_token).await
1264        } else {
1265            let parsed: GeminiResponse = resp.json().await.map_err(|e| {
1266                FetchiumError::AiUnavailable(format!("Invalid Antigravity response: {e}"))
1267            })?;
1268            let content = parsed
1269                .candidates
1270                .into_iter()
1271                .next()
1272                .map(|c| {
1273                    c.content
1274                        .parts
1275                        .into_iter()
1276                        .filter_map(|p| p.text)
1277                        .collect::<Vec<_>>()
1278                        .join("")
1279                })
1280                .unwrap_or_default();
1281            Ok(ChatResult {
1282                content,
1283                model_used: model.to_string(),
1284                provider: ProviderKind::Antigravity,
1285            })
1286        };
1287    }
1288
1289    Err(FetchiumError::AiUnavailable(format!(
1290        "Antigravity: all endpoints failed. Last error: {last_err}\n  \
1291         Ensure your OpenCode session is active and the antigravity plugin is installed."
1292    )))
1293}
1294
1295// ─── Gemini CLI subprocess ────────────────────────────────────────────────────
1296
1297async fn call_gemini_cli(
1298    model: &str,
1299    messages: &[crate::ai::types::ChatMessage],
1300    on_token: &mut dyn FnMut(&str),
1301) -> Result<ChatResult, FetchiumError> {
1302    let prompt = messages
1303        .iter()
1304        .map(|m| {
1305            if m.role == "system" {
1306                format!("[System Instructions]: {}", m.content)
1307            } else {
1308                m.content.clone()
1309            }
1310        })
1311        .collect::<Vec<_>>()
1312        .join("\n\n");
1313
1314    // Gemini CLI v0.30+ sends `thinking_config.include_thoughts` on every request.
1315    // Only models that support thinking (gemini-2.5-flash, gemini-3-flash-preview, etc.) work.
1316    // Always pass `--model` so the CLI uses the explicitly selected model.
1317    //
1318    // Capacity fallback: if `model` is capacity-exhausted (429 MODEL_CAPACITY_EXHAUSTED)
1319    // we automatically retry once with `gemini-2.5-flash` before giving up.
1320    const CAPACITY_FALLBACK: &str = "gemini-2.5-flash";
1321    let models_to_try: Vec<String> = if model == CAPACITY_FALLBACK {
1322        vec![model.to_string()]
1323    } else {
1324        vec![model.to_string(), CAPACITY_FALLBACK.to_string()]
1325    };
1326
1327    let mut last_err: Option<FetchiumError> = None;
1328
1329    for try_model in &models_to_try {
1330        let args = vec![
1331            "--model".to_string(),
1332            try_model.clone(),
1333            "--prompt".to_string(),
1334            prompt.clone(),
1335        ];
1336
1337        // Redirect stdin to /dev/null — gemini CLI hangs waiting for interactive
1338        // input when stdin is inherited from a terminal or pipe.
1339        let output = tokio::process::Command::new("gemini")
1340            .args(&args)
1341            .stdin(std::process::Stdio::null())
1342            .output()
1343            .await
1344            .map_err(|e| {
1345                FetchiumError::ExternalTool(format!(
1346                    "Gemini CLI not found: {e}\nInstall: npm install -g @google/gemini-cli"
1347                ))
1348            })?;
1349
1350        if !output.status.success() {
1351            let stderr = String::from_utf8_lossy(&output.stderr).into_owned();
1352            let is_not_last = try_model != models_to_try.last().unwrap();
1353            let is_capacity = stderr.contains("MODEL_CAPACITY_EXHAUSTED")
1354                || stderr.contains("No capacity available")
1355                || stderr.contains("RESOURCE_EXHAUSTED");
1356            if is_capacity && is_not_last {
1357                tracing::warn!(
1358                    "GeminiCli: '{try_model}' capacity exhausted, retrying with '{CAPACITY_FALLBACK}'"
1359                );
1360                last_err = Some(FetchiumError::ExternalTool(format!(
1361                    "Gemini CLI error: {stderr}"
1362                )));
1363                continue;
1364            }
1365            return Err(FetchiumError::ExternalTool(format!(
1366                "Gemini CLI error: {stderr}"
1367            )));
1368        }
1369
1370        let content = String::from_utf8_lossy(&output.stdout).trim().to_string();
1371        // Gemini CLI doesn't stream — emit the full response as one token.
1372        on_token(&content);
1373        return Ok(ChatResult {
1374            content,
1375            model_used: format!("gemini-cli/{try_model}"),
1376            provider: ProviderKind::GeminiCli,
1377        });
1378    }
1379
1380    Err(last_err.unwrap_or_else(|| {
1381        FetchiumError::ExternalTool("GeminiCli: all models capacity-exhausted".into())
1382    }))
1383}
1384
1385// ─── Provider availability check ─────────────────────────────────────────────
1386
1387/// Status of an AI provider for display in `fetchium provider list` / `fetchium doctor`.
1388#[derive(Debug)]
1389pub enum ProviderStatus {
1390    /// Provider is reachable and configured.
1391    Available {
1392        /// Number of installed models (Ollama only).
1393        model_count: Option<usize>,
1394    },
1395    /// Provider is not reachable or not configured.
1396    Unavailable {
1397        /// Human-readable reason.
1398        reason: String,
1399    },
1400}
1401
1402/// Check whether a single provider is available without making a real LLM call.
1403pub async fn check_provider(
1404    kind: ProviderKind,
1405    providers: &ProvidersConfig,
1406    ai_config: &AiConfig,
1407) -> ProviderStatus {
1408    let entry = providers.entry(kind);
1409
1410    match kind {
1411        ProviderKind::Ollama => {
1412            let ollama = OllamaClient::new(ai_config);
1413            if ollama.is_available().await {
1414                let models = ollama.list_models().await.unwrap_or_default();
1415                ProviderStatus::Available {
1416                    model_count: Some(models.len()),
1417                }
1418            } else {
1419                ProviderStatus::Unavailable {
1420                    reason: "Ollama not running (start: ollama serve)".into(),
1421                }
1422            }
1423        }
1424
1425        ProviderKind::GeminiCli => {
1426            let found = std::process::Command::new("gemini")
1427                .arg("--version")
1428                .output()
1429                .map(|o| o.status.success())
1430                .unwrap_or(false);
1431            if found {
1432                ProviderStatus::Available { model_count: None }
1433            } else {
1434                ProviderStatus::Unavailable {
1435                    reason: "`gemini` not in PATH (npm install -g @google/generative-ai-cli)"
1436                        .into(),
1437                }
1438            }
1439        }
1440
1441        ProviderKind::OpenAi => {
1442            if entry.resolve_api_key("OPENAI_API_KEY").is_some()
1443                || crate::ai::credentials::codex_auth_available()
1444            {
1445                ProviderStatus::Available { model_count: None }
1446            } else {
1447                ProviderStatus::Unavailable {
1448                    reason: "No API key or Codex CLI session (set OPENAI_API_KEY or run `codex auth login`)".into(),
1449                }
1450            }
1451        }
1452
1453        ProviderKind::Anthropic => {
1454            if entry.resolve_api_key("ANTHROPIC_API_KEY").is_some()
1455                || crate::ai::credentials::get_claude_code_token().is_some()
1456            {
1457                ProviderStatus::Available { model_count: None }
1458            } else {
1459                ProviderStatus::Unavailable {
1460                    reason: "No API key or Claude Code session (run `claude` once to log in)"
1461                        .into(),
1462                }
1463            }
1464        }
1465
1466        ProviderKind::Gemini => {
1467            // Creds file "existing" is not enough — the refresh token must be non-empty.
1468            // invalidate_gemini_creds() sets refresh_token="" when a 401 is received,
1469            // so a dead session is correctly reported as unavailable on the next check.
1470            if entry.resolve_api_key("GEMINI_API_KEY").is_some()
1471                || crate::ai::credentials::get_gemini_access_token_if_valid().is_some()
1472                || crate::ai::credentials::read_gemini_creds()
1473                    .map(|c| c.is_refreshable())
1474                    .unwrap_or(false)
1475            {
1476                ProviderStatus::Available { model_count: None }
1477            } else {
1478                ProviderStatus::Unavailable {
1479                    reason: "No API key or valid OAuth session.\n    \
1480                             Fix: run `gemini auth login`  OR  \
1481                             `fetchium provider set gemini --key AIza...`"
1482                        .into(),
1483                }
1484            }
1485        }
1486
1487        ProviderKind::OpenRouter => {
1488            if entry.resolve_api_key("OPENROUTER_API_KEY").is_some()
1489                || crate::ai::credentials::read_openrouter_key().is_some()
1490            {
1491                ProviderStatus::Available { model_count: None }
1492            } else {
1493                ProviderStatus::Unavailable {
1494                    reason:
1495                        "No API key (set OPENROUTER_API_KEY or run `fetchium provider setup openrouter`)"
1496                            .into(),
1497                }
1498            }
1499        }
1500
1501        ProviderKind::Antigravity => {
1502            if antigravity_auth_available() {
1503                ProviderStatus::Available { model_count: None }
1504            } else {
1505                ProviderStatus::Unavailable {
1506                    reason: "No OpenCode Antigravity account found.\n    \
1507                             Install: npm i -g opencode-antigravity-auth\n    \
1508                             Then run `opencode` to authenticate."
1509                        .into(),
1510                }
1511            }
1512        }
1513    }
1514}
1515
1516#[cfg(test)]
1517mod tests {
1518    use super::*;
1519    use crate::ai::providers::ProvidersConfig;
1520
1521    #[test]
1522    fn chat_result_has_provider() {
1523        let r = ChatResult {
1524            content: "hello".into(),
1525            model_used: "test-model".into(),
1526            provider: ProviderKind::Gemini,
1527        };
1528        assert_eq!(r.provider, ProviderKind::Gemini);
1529    }
1530
1531    #[tokio::test]
1532    async fn empty_chain_returns_error() {
1533        // Disable ALL providers so auto-discovery can't add any to the chain.
1534        let mut providers = ProvidersConfig {
1535            fallback_chain: vec!["nonexistent_provider_xyz".into()],
1536            ..ProvidersConfig::default()
1537        };
1538        providers.ollama.enabled = false;
1539        providers.gemini.enabled = false;
1540        providers.gemini_cli.enabled = false;
1541        providers.antigravity.enabled = false;
1542        providers.anthropic.enabled = false;
1543        providers.openai.enabled = false;
1544        providers.openrouter.enabled = false;
1545        // With all providers disabled and an invalid slug, the chain should
1546        // fall back to [Ollama] but Ollama is also disabled, so resolved_chain
1547        // still returns [Ollama]. The actual chat call will fail because Ollama
1548        // is not reachable or the chain effectively has only disabled providers.
1549        //
1550        // Test the chain resolution directly instead of relying on network state.
1551        let chain = providers.resolved_chain();
1552        // The fallback to Ollama is always applied when chain is empty, so
1553        // verify the original nonexistent provider was filtered out.
1554        assert!(
1555            !chain.iter().any(|k| k.slug() == "nonexistent_provider_xyz"),
1556            "Unknown provider slug should be filtered from chain"
1557        );
1558    }
1559}