| 1 | use crate::auth_source::ProviderAuthSourceToml; |
| 2 | use crate::display_redaction::is_sensitive_config_key; |
| 3 | use crate::external_credentials::ExternalCredentialConsentToml; |
| 4 | use crate::provider; |
| 5 | use crate::provider_kind::ProviderKind; |
| 6 | use anyhow::{Result, bail}; |
| 7 | use serde::{Deserialize, Serialize}; |
| 8 | use std::collections::BTreeMap; |
| 9 | |
| 10 | pub const CONFIG_FILE_NAME: &str = "config.toml"; |
| 11 | |
| 12 | pub const PERMISSIONS_FILE_NAME: &str = "permissions.toml"; |
| 13 | |
| 14 | pub const LEGACY_ANTIGRAVITY_TOMBSTONE_MESSAGE: &str = "Antigravity is a retired, non-runnable legacy provider. Clear Codewhale-owned legacy state with `codewhale auth clear --provider antigravity`; this does not alter Google or Antigravity sessions. For Gemini use provider `google` with `GEMINI_API_KEY`."; |
| 15 | |
| 16 | /// Secret-store routing metadata; never credential material. |
| 17 | pub const API_KEYRING_SENTINEL: &str = "__KEYRING__"; |
| 18 | |
| 19 | /// Canonical structural classification for configured API-key values. |
| 20 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 21 | pub enum ConfigApiKeyValueKind { |
| 22 | Empty, |
| 23 | SecretStoreSentinel, |
| 24 | Literal, |
| 25 | } |
| 26 | |
| 27 | #[must_use] |
| 28 | pub fn classify_config_api_key_value(value: &str) -> ConfigApiKeyValueKind { |
| 29 | match value.trim() { |
| 30 | "" => ConfigApiKeyValueKind::Empty, |
| 31 | API_KEYRING_SENTINEL => ConfigApiKeyValueKind::SecretStoreSentinel, |
| 32 | _ => ConfigApiKeyValueKind::Literal, |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | pub(crate) fn http_headers_are_effectively_empty(headers: &BTreeMap<String, String>) -> bool { |
| 37 | !headers |
| 38 | .iter() |
| 39 | .any(|(name, value)| !name.trim().is_empty() && !value.trim().is_empty()) |
| 40 | } |
| 41 | |
| 42 | /// Whether an HTTP header can carry the model provider's primary credential. |
| 43 | /// |
| 44 | /// Header names are case-insensitive. Keeping this classifier in shared config |
| 45 | /// prevents `auth_mode = "none"` from disabling a generated bearer token while |
| 46 | /// still leaking the same credential through a configured alternate dialect. |
| 47 | #[must_use] |
| 48 | pub fn is_upstream_auth_header(name: &str) -> bool { |
| 49 | let name = name.trim(); |
| 50 | // Configured gateways use more credential dialects than the three headers |
| 51 | // generated by Codewhale itself. `auth_mode = "none"` is an endpoint |
| 52 | // contract, so suppress every credential-shaped request header instead of |
| 53 | // allowing the same secret through Proxy-Authorization, X-Auth-Token, |
| 54 | // X-Access-Token, X-Goog-Api-Key, or another *-token/*-api-key spelling. |
| 55 | is_sensitive_config_key(name) |
| 56 | } |
| 57 | |
| 58 | /// Preserve OpenRouter endpoint slugs verbatim; an empty value clears a pin. |
| 59 | /// The service owns the vendor catalog, so validation must not freeze one here. |
| 60 | pub fn validate_openrouter_vendor(value: &str) -> Result<Option<&str>> { |
| 61 | if value.trim().is_empty() { |
| 62 | return Ok(None); |
| 63 | } |
| 64 | if value |
| 65 | .chars() |
| 66 | .any(|ch| ch.is_whitespace() || ch.is_control()) |
| 67 | { |
| 68 | bail!( |
| 69 | "providers.openrouter.vendor must be an OpenRouter slug without whitespace or control characters" |
| 70 | ); |
| 71 | } |
| 72 | Ok(Some(value)) |
| 73 | } |
| 74 | |
| 75 | /// Apply a validated pin to an OpenRouter request without dropping unrelated |
| 76 | /// caller policies such as data collection or zero-data-retention constraints. |
| 77 | pub fn apply_openrouter_vendor(body: &mut serde_json::Value, vendor: Option<&str>) { |
| 78 | if let Some(vendor) = vendor { |
| 79 | if !body["provider"].is_object() { |
| 80 | body["provider"] = serde_json::json!({}); |
| 81 | } |
| 82 | body["provider"]["order"] = serde_json::json!([vendor]); |
| 83 | body["provider"]["allow_fallbacks"] = serde_json::json!(false); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | #[derive(Debug, Clone, Serialize, Deserialize, Default)] |
| 88 | pub struct ProviderConfigToml { |
| 89 | /// OpenRouter upstream slug, including an optional endpoint variant. |
| 90 | /// Requests with a vendor pin disable OpenRouter's upstream fallbacks. |
| 91 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 92 | pub vendor: Option<String>, |
| 93 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 94 | pub api_key: Option<String>, |
| 95 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 96 | pub api_key_env: Option<String>, |
| 97 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 98 | pub base_url: Option<String>, |
| 99 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 100 | pub model: Option<String>, |
| 101 | #[serde( |
| 102 | default, |
| 103 | skip_serializing_if = "Option::is_none", |
| 104 | alias = "contextWindow", |
| 105 | alias = "context_window_tokens", |
| 106 | alias = "contextWindowTokens", |
| 107 | alias = "context_length", |
| 108 | alias = "contextLength" |
| 109 | )] |
| 110 | pub context_window: Option<u32>, |
| 111 | /// Per-model context-window overrides keyed by exact wire model id |
| 112 | /// (`[providers.<id>.model_context_windows]`, #6108). A matching entry |
| 113 | /// wins over this provider's `context_window` for that model only, so one |
| 114 | /// gateway can front models with heterogeneous windows. |
| 115 | #[serde( |
| 116 | default, |
| 117 | skip_serializing_if = "BTreeMap::is_empty", |
| 118 | alias = "modelContextWindows" |
| 119 | )] |
| 120 | pub model_context_windows: BTreeMap<String, u32>, |
| 121 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 122 | pub mode: Option<String>, |
| 123 | /// Wire dialect preference for dual-protocol vendors (DeepSeek, MiniMax, |
| 124 | /// Model Studio): `openai` (Chat Completions, default) or `anthropic` |
| 125 | /// (Messages). Not a separate catalog provider — a power-user toggle. |
| 126 | #[serde( |
| 127 | default, |
| 128 | skip_serializing_if = "Option::is_none", |
| 129 | alias = "api_style", |
| 130 | alias = "protocol", |
| 131 | alias = "wire_format", |
| 132 | alias = "dialect" |
| 133 | )] |
| 134 | pub wire: Option<String>, |
| 135 | /// Responses dialect for a custom table whose `wire` is `responses`. |
| 136 | /// Absent keeps the default `standard` profile. Built-in providers reject |
| 137 | /// this field; `codex` is not on the custom allowlist. |
| 138 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 139 | pub responses_profile: Option<String>, |
| 140 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 141 | pub auth_mode: Option<String>, |
| 142 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 143 | pub insecure_skip_tls_verify: Option<bool>, |
| 144 | /// Explicit consent to a plain-HTTP `base_url` for this provider (a |
| 145 | /// llama.cpp box on the LAN, an internal gateway). Loopback hosts are |
| 146 | /// always allowed without it. Distinct from `insecure_skip_tls_verify`, |
| 147 | /// which skips TLS certificate verification on HTTPS URLs and does not |
| 148 | /// permit plain HTTP (#5991). |
| 149 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 150 | pub allow_insecure_http: Option<bool>, |
| 151 | #[serde(default, skip_serializing_if = "http_headers_are_effectively_empty")] |
| 152 | pub http_headers: BTreeMap<String, String>, |
| 153 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 154 | pub path_suffix: Option<String>, |
| 155 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 156 | pub auth: Option<ProviderAuthSourceToml>, |
| 157 | /// Explicit consent for reading one exact credential file owned by |
| 158 | /// another CLI. Absence means disabled and must not trigger discovery. |
| 159 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 160 | pub external_credentials: Option<ExternalCredentialConsentToml>, |
| 161 | /// Codewhale-owned xAI OAuth generation selected by config. The value is a |
| 162 | /// validated basename under `$CODEWHALE_HOME/credentials`, never an |
| 163 | /// arbitrary path. |
| 164 | #[serde(default, skip_serializing_if = "Option::is_none")] |
| 165 | pub oauth_credential_generation: Option<String>, |
| 166 | /// Preserve provider fields introduced by newer Codewhale versions and by |
| 167 | /// custom provider adapters when an older typed writer saves this file. |
| 168 | #[serde(flatten)] |
| 169 | pub extras: BTreeMap<String, toml::Value>, |
| 170 | } |
| 171 | |
| 172 | impl ProviderConfigToml { |
| 173 | #[must_use] |
| 174 | pub fn is_empty(&self) -> bool { |
| 175 | let blank = |value: Option<&String>| value.is_none_or(|value| value.trim().is_empty()); |
| 176 | |
| 177 | blank(self.api_key.as_ref()) |
| 178 | && blank(self.api_key_env.as_ref()) |
| 179 | && self.vendor.is_none() |
| 180 | && blank(self.base_url.as_ref()) |
| 181 | && blank(self.model.as_ref()) |
| 182 | && self.context_window.is_none() |
| 183 | && self.model_context_windows.is_empty() |
| 184 | && blank(self.mode.as_ref()) |
| 185 | && blank(self.wire.as_ref()) |
| 186 | && blank(self.responses_profile.as_ref()) |
| 187 | && blank(self.auth_mode.as_ref()) |
| 188 | && self.insecure_skip_tls_verify.is_none() |
| 189 | && self.allow_insecure_http.is_none() |
| 190 | && http_headers_are_effectively_empty(&self.http_headers) |
| 191 | && blank(self.path_suffix.as_ref()) |
| 192 | && self.auth.is_none() |
| 193 | && self.external_credentials.is_none() |
| 194 | && self.oauth_credential_generation.is_none() |
| 195 | && self.extras.is_empty() |
| 196 | } |
| 197 | |
| 198 | #[must_use] |
| 199 | pub fn api_key_env_name(&self) -> Option<&str> { |
| 200 | self.api_key_env |
| 201 | .as_deref() |
| 202 | .map(str::trim) |
| 203 | .filter(|name| !name.is_empty()) |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | #[derive(Debug, Clone, Serialize, Deserialize, Default)] |
| 208 | pub struct ProvidersToml { |
| 209 | #[serde(default, skip_serializing_if = "ProviderConfigToml::is_empty")] |
| 210 | pub deepseek: ProviderConfigToml, |
| 211 | #[serde( |
| 212 | default, |
| 213 | skip_serializing_if = "ProviderConfigToml::is_empty", |
| 214 | alias = "deepseek-anthropic", |
| 215 | alias = "deepseekAnthropic", |
| 216 | alias = "deepseek-claude", |
| 217 | alias = "deepseek_claude" |
| 218 | )] |
| 219 | pub deepseek_anthropic: ProviderConfigToml, |
| 220 | #[serde( |
| 221 | default, |
| 222 | skip_serializing_if = "ProviderConfigToml::is_empty", |
| 223 | // The canonical provider id is the kebab `nvidia-nim` (see |
| 224 | // `provider.rs`); without these aliases a `[providers.nvidia-nim]` |
| 225 | // TOML section was silently dropped (2026-08-04 review). |
| 226 | alias = "nvidia-nim", |
| 227 | alias = "nvidia", |
| 228 | alias = "nim" |
| 229 | )] |
| 230 | pub nvidia_nim: ProviderConfigToml, |
| 231 | #[serde(default, skip_serializing_if = "ProviderConfigToml::is_empty")] |
| 232 | pub openai: ProviderConfigToml, |
| 233 | #[serde(default, skip_serializing_if = "ProviderConfigToml::is_empty")] |
| 234 | pub atlascloud: ProviderConfigToml, |
| 235 | #[serde( |
| 236 | default, |
| 237 | skip_serializing_if = "ProviderConfigToml::is_empty", |
| 238 | alias = "wanjie-ark", |
| 239 | alias = "wanjie", |
| 240 | alias = "ark-wanjie", |
| 241 | alias = "ark_wanjie" |
| 242 | )] |
| 243 | pub wanjie_ark: ProviderConfigToml, |
| 244 | #[serde(default, skip_serializing_if = "ProviderConfigToml::is_empty")] |
| 245 | pub volcengine: ProviderConfigToml, |
| 246 | #[serde(default, skip_serializing_if = "ProviderConfigToml::is_empty")] |
| 247 | pub openrouter: ProviderConfigToml, |
| 248 | #[serde(default, skip_serializing_if = "ProviderConfigToml::is_empty")] |
| 249 | pub orcarouter: ProviderConfigToml, |
| 250 | #[serde( |
| 251 | default, |
| 252 | skip_serializing_if = "ProviderConfigToml::is_empty", |
| 253 | alias = "xiaomi-mimo", |
| 254 | alias = "xiaomi", |
| 255 | alias = "mimo", |
| 256 | alias = "xiaomimimo" |
| 257 | )] |
| 258 | pub xiaomi_mimo: ProviderConfigToml, |
| 259 | #[serde(default, skip_serializing_if = "ProviderConfigToml::is_empty")] |
| 260 | pub novita: ProviderConfigToml, |
| 261 | #[serde(default, skip_serializing_if = "ProviderConfigToml::is_empty")] |
| 262 | pub fireworks: ProviderConfigToml, |
| 263 | #[serde(default, skip_serializing_if = "ProviderConfigToml::is_empty")] |
| 264 | pub siliconflow: ProviderConfigToml, |
| 265 | #[serde( |
| 266 | default, |
| 267 | skip_serializing_if = "ProviderConfigToml::is_empty", |
| 268 | alias = "siliconflow-CN", |
| 269 | alias = "siliconflow-cn" |
| 270 | )] |
| 271 | pub siliconflow_cn: ProviderConfigToml, |
| 272 | #[serde(default, skip_serializing_if = "ProviderConfigToml::is_empty")] |
| 273 | pub arcee: ProviderConfigToml, |
| 274 | #[serde(default, skip_serializing_if = "ProviderConfigToml::is_empty")] |
| 275 | pub moonshot: ProviderConfigToml, |
| 276 | #[serde(default, skip_serializing_if = "ProviderConfigToml::is_empty")] |
| 277 | pub sglang: ProviderConfigToml, |
| 278 | #[serde(default, skip_serializing_if = "ProviderConfigToml::is_empty")] |
| 279 | pub vllm: ProviderConfigToml, |
| 280 | #[serde(default, skip_serializing_if = "ProviderConfigToml::is_empty")] |
| 281 | pub ollama: ProviderConfigToml, |
| 282 | #[serde( |
| 283 | default, |
| 284 | skip_serializing_if = "ProviderConfigToml::is_empty", |
| 285 | alias = "ollama-cloud" |
| 286 | )] |
| 287 | pub ollama_cloud: ProviderConfigToml, |
| 288 | #[serde(default, skip_serializing_if = "ProviderConfigToml::is_empty")] |
| 289 | pub huggingface: ProviderConfigToml, |
| 290 | #[serde(default, skip_serializing_if = "ProviderConfigToml::is_empty")] |
| 291 | pub modelscope: ProviderConfigToml, |
| 292 | #[serde(default, skip_serializing_if = "ProviderConfigToml::is_empty")] |
| 293 | pub together: ProviderConfigToml, |
| 294 | #[serde( |
| 295 | default, |
| 296 | skip_serializing_if = "ProviderConfigToml::is_empty", |
| 297 | alias = "baidu-qianfan", |
| 298 | alias = "baidu_qianfan", |
| 299 | alias = "baidu" |
| 300 | )] |
| 301 | pub qianfan: ProviderConfigToml, |
| 302 | #[serde( |
| 303 | default, |
| 304 | skip_serializing_if = "ProviderConfigToml::is_empty", |
| 305 | alias = "openai-codex", |
| 306 | alias = "openai_codex", |
| 307 | alias = "codex", |
| 308 | alias = "chatgpt", |
| 309 | alias = "chatgpt-codex" |
| 310 | )] |
| 311 | pub openai_codex: ProviderConfigToml, |
| 312 | #[serde(default, skip_serializing_if = "ProviderConfigToml::is_empty")] |
| 313 | pub anthropic: ProviderConfigToml, |
| 314 | #[serde( |
| 315 | default, |
| 316 | skip_serializing_if = "ProviderConfigToml::is_empty", |
| 317 | alias = "open-model", |
| 318 | alias = "open_model" |
| 319 | )] |
| 320 | pub openmodel: ProviderConfigToml, |
| 321 | #[serde( |
| 322 | default, |
| 323 | skip_serializing_if = "ProviderConfigToml::is_empty", |
| 324 | alias = "z-ai", |
| 325 | alias = "z_ai", |
| 326 | alias = "z.ai", |
| 327 | alias = "zhipu", |
| 328 | alias = "zhipuai", |
| 329 | alias = "bigmodel", |
| 330 | alias = "big-model" |
| 331 | )] |
| 332 | pub zai: ProviderConfigToml, |
| 333 | #[serde( |
| 334 | default, |
| 335 | skip_serializing_if = "ProviderConfigToml::is_empty", |
| 336 | alias = "step-fun", |
| 337 | alias = "step_fun", |
| 338 | alias = "stepfun", |
| 339 | alias = "stepflash", |
| 340 | alias = "step-flash", |
| 341 | alias = "step_flash" |
| 342 | )] |
| 343 | pub stepfun: ProviderConfigToml, |
| 344 | #[serde( |
| 345 | default, |
| 346 | skip_serializing_if = "ProviderConfigToml::is_empty", |
| 347 | alias = "mini-max", |
| 348 | alias = "mini_max", |
| 349 | alias = "minimax" |
| 350 | )] |
| 351 | pub minimax: ProviderConfigToml, |
| 352 | #[serde( |
| 353 | default, |
| 354 | skip_serializing_if = "ProviderConfigToml::is_empty", |
| 355 | alias = "minimax-anthropic", |
| 356 | alias = "minimaxAnthropic", |
| 357 | alias = "mini-max-anthropic", |
| 358 | alias = "mini_max_anthropic" |
| 359 | )] |
| 360 | pub minimax_anthropic: ProviderConfigToml, |
| 361 | #[serde( |
| 362 | default, |
| 363 | skip_serializing_if = "ProviderConfigToml::is_empty", |
| 364 | alias = "deep-infra", |
| 365 | alias = "deep_infra" |
| 366 | )] |
| 367 | pub deepinfra: ProviderConfigToml, |
| 368 | #[serde( |
| 369 | default, |
| 370 | skip_serializing_if = "ProviderConfigToml::is_empty", |
| 371 | alias = "sakana-ai", |
| 372 | alias = "sakana_ai", |
| 373 | alias = "fugu" |
| 374 | )] |
| 375 | pub sakana: ProviderConfigToml, |
| 376 | #[serde( |
| 377 | default, |
| 378 | skip_serializing_if = "ProviderConfigToml::is_empty", |
| 379 | alias = "long-cat", |
| 380 | alias = "meituan-longcat", |
| 381 | alias = "meituan" |
| 382 | )] |
| 383 | pub longcat: ProviderConfigToml, |
| 384 | #[serde( |
| 385 | default, |
| 386 | skip_serializing_if = "ProviderConfigToml::is_empty", |
| 387 | alias = "opencode-go", |
| 388 | alias = "opencodego" |
| 389 | )] |
| 390 | pub opencode_go: ProviderConfigToml, |
| 391 | #[serde( |
| 392 | default, |
| 393 | skip_serializing_if = "ProviderConfigToml::is_empty", |
| 394 | alias = "opencode-zen", |
| 395 | alias = "opencodezen", |
| 396 | alias = "zen", |
| 397 | alias = "opencode" |
| 398 | )] |
| 399 | pub opencode_zen: ProviderConfigToml, |
| 400 | #[serde( |
| 401 | default, |
| 402 | skip_serializing_if = "ProviderConfigToml::is_empty", |
| 403 | alias = "meta-ai", |
| 404 | alias = "meta_ai", |
| 405 | alias = "meta-model-api", |
| 406 | alias = "meta_model_api", |
| 407 | alias = "muse", |
| 408 | alias = "muse-spark" |
| 409 | )] |
| 410 | pub meta: ProviderConfigToml, |
| 411 | #[serde( |
| 412 | default, |
| 413 | skip_serializing_if = "ProviderConfigToml::is_empty", |
| 414 | alias = "x-ai", |
| 415 | alias = "x_ai", |
| 416 | alias = "grok" |
| 417 | )] |
| 418 | pub xai: ProviderConfigToml, |
| 419 | #[serde( |
| 420 | default, |
| 421 | skip_serializing_if = "ProviderConfigToml::is_empty", |
| 422 | alias = "mistral-ai", |
| 423 | alias = "mistral_ai", |
| 424 | alias = "mistralai", |
| 425 | alias = "la-plateforme", |
| 426 | alias = "la_plateforme" |
| 427 | )] |
| 428 | pub mistral: ProviderConfigToml, |
| 429 | /// Google Gemini — official OpenAI-compatible endpoint with thought |
| 430 | /// signatures on tool calls. |
| 431 | #[serde( |
| 432 | default, |
| 433 | skip_serializing_if = "ProviderConfigToml::is_empty", |
| 434 | alias = "google-gemini", |
| 435 | alias = "google_gemini", |
| 436 | alias = "gemini" |
| 437 | )] |
| 438 | pub google: ProviderConfigToml, |
| 439 | /// Retired Antigravity configuration. This table exists only so old |
| 440 | /// Codewhale-owned state can deserialize and be cleared safely. |
| 441 | #[serde( |
| 442 | default, |
| 443 | skip_serializing_if = "ProviderConfigToml::is_empty", |
| 444 | alias = "agy" |
| 445 | )] |
| 446 | pub antigravity: ProviderConfigToml, |
| 447 | /// Jiangsu Telecom TokenHub — OpenAI-compatible AI gateway. |
| 448 | #[serde( |
| 449 | default, |
| 450 | skip_serializing_if = "ProviderConfigToml::is_empty", |
| 451 | alias = "telecom-js", |
| 452 | alias = "telecom_js", |
| 453 | alias = "telecomjs-cn", |
| 454 | alias = "tokenhub" |
| 455 | )] |
| 456 | pub telecomjs: ProviderConfigToml, |
| 457 | /// Eden AI — OpenAI-compatible AI gateway (aggregator). |
| 458 | #[serde( |
| 459 | default, |
| 460 | skip_serializing_if = "ProviderConfigToml::is_empty", |
| 461 | alias = "eden-ai", |
| 462 | alias = "eden_ai" |
| 463 | )] |
| 464 | pub edenai: ProviderConfigToml, |
| 465 | /// ZenMux — OpenAI-compatible AI gateway (aggregator). |
| 466 | #[serde( |
| 467 | default, |
| 468 | skip_serializing_if = "ProviderConfigToml::is_empty", |
| 469 | alias = "zen-mux", |
| 470 | alias = "zen_mux" |
| 471 | )] |
| 472 | pub zenmux: ProviderConfigToml, |
| 473 | /// CSDN 星图 — hosted OpenAI-compatible platform and Coding Plan. |
| 474 | #[serde( |
| 475 | default, |
| 476 | skip_serializing_if = "ProviderConfigToml::is_empty", |
| 477 | alias = "csdn-ai", |
| 478 | alias = "csdn_ai", |
| 479 | alias = "csdn-coding-plan", |
| 480 | alias = "csdn_coding_plan", |
| 481 | alias = "starmap" |
| 482 | )] |
| 483 | pub csdn: ProviderConfigToml, |
| 484 | /// Concentrate — OpenAI Responses-compatible AI gateway (aggregator). |
| 485 | #[serde( |
| 486 | default, |
| 487 | skip_serializing_if = "ProviderConfigToml::is_empty", |
| 488 | alias = "concentrate-ai", |
| 489 | alias = "concentrate_ai", |
| 490 | alias = "concentrateai" |
| 491 | )] |
| 492 | pub concentrate: ProviderConfigToml, |
| 493 | /// Codewhale API — account-backed model access over connected provider keys. |
| 494 | #[serde( |
| 495 | default, |
| 496 | skip_serializing_if = "ProviderConfigToml::is_empty", |
| 497 | alias = "codewhale-api", |
| 498 | alias = "codewhale_api", |
| 499 | alias = "cw-api", |
| 500 | alias = "codewhale-cloud" |
| 501 | )] |
| 502 | pub codewhale: ProviderConfigToml, |
| 503 | /// Alibaba Cloud Model Studio — Token Plan (OpenAI-compatible endpoint). |
| 504 | #[serde( |
| 505 | default, |
| 506 | skip_serializing_if = "ProviderConfigToml::is_empty", |
| 507 | alias = "modelstudio-token-plan", |
| 508 | alias = "modelstudio_token_plan", |
| 509 | alias = "alibaba-token-plan", |
| 510 | alias = "dashscope-token-plan" |
| 511 | )] |
| 512 | pub modelstudio_token_plan: ProviderConfigToml, |
| 513 | /// Alibaba Cloud Model Studio — Token Plan Anthropic-compatible endpoint. |
| 514 | #[serde( |
| 515 | default, |
| 516 | skip_serializing_if = "ProviderConfigToml::is_empty", |
| 517 | alias = "modelstudio-token-plan-anthropic", |
| 518 | alias = "modelstudio_token_plan_anthropic", |
| 519 | alias = "alibaba-token-plan-anthropic" |
| 520 | )] |
| 521 | pub modelstudio_token_plan_anthropic: ProviderConfigToml, |
| 522 | /// Alibaba Cloud Model Studio — Coding Plan (OpenAI-compatible endpoint). |
| 523 | #[serde( |
| 524 | default, |
| 525 | skip_serializing_if = "ProviderConfigToml::is_empty", |
| 526 | alias = "modelstudio-coding-plan", |
| 527 | alias = "modelstudio_coding_plan", |
| 528 | alias = "alibaba-coding-plan", |
| 529 | alias = "dashscope-coding-plan" |
| 530 | )] |
| 531 | pub modelstudio_coding_plan: ProviderConfigToml, |
| 532 | /// Alibaba Cloud Model Studio — Coding Plan Anthropic-compatible endpoint. |
| 533 | #[serde( |
| 534 | default, |
| 535 | skip_serializing_if = "ProviderConfigToml::is_empty", |
| 536 | alias = "modelstudio-coding-plan-anthropic", |
| 537 | alias = "modelstudio_coding_plan_anthropic", |
| 538 | alias = "alibaba-coding-plan-anthropic" |
| 539 | )] |
| 540 | pub modelstudio_coding_plan_anthropic: ProviderConfigToml, |
| 541 | /// Catch-all table for the dynamic OpenAI-compatible custom provider |
| 542 | /// identity (#1519). Arbitrary `[providers.<name>]` tables are handled by |
| 543 | /// the tui-side flatten map; this named slot keeps the canonical |
| 544 | /// `ProviderKind::Custom` lookups total without leaking into another |
| 545 | /// provider's config. |
| 546 | #[serde(default, skip_serializing_if = "ProviderConfigToml::is_empty")] |
| 547 | pub custom: ProviderConfigToml, |
| 548 | /// Preserve dynamically named provider tables and providers added by a |
| 549 | /// newer Codewhale version. |
| 550 | #[serde(flatten)] |
| 551 | pub extras: BTreeMap<String, toml::Value>, |
| 552 | } |
| 553 | |
| 554 | impl ProvidersToml { |
| 555 | #[must_use] |
| 556 | pub fn is_empty(&self) -> bool { |
| 557 | // The full registry, not the selectable catalog: legacy dialect and |
| 558 | // plan tables (`deepseek_anthropic`, `modelstudio_coding_plan`, ...) |
| 559 | // are still fields here, and skipping the whole `[providers]` section |
| 560 | // when only one of them is set would erase it on the next typed save. |
| 561 | self.extras.is_empty() |
| 562 | && provider::all_providers() |
| 563 | .iter() |
| 564 | .all(|provider| self.for_provider(provider.kind()).is_empty()) |
| 565 | && self.antigravity.is_empty() |
| 566 | } |
| 567 | |
| 568 | #[must_use] |
| 569 | pub fn for_provider(&self, provider: ProviderKind) -> &ProviderConfigToml { |
| 570 | match provider { |
| 571 | ProviderKind::Deepseek => &self.deepseek, |
| 572 | ProviderKind::DeepseekAnthropic => &self.deepseek_anthropic, |
| 573 | ProviderKind::NvidiaNim => &self.nvidia_nim, |
| 574 | ProviderKind::Openai => &self.openai, |
| 575 | ProviderKind::Atlascloud => &self.atlascloud, |
| 576 | ProviderKind::WanjieArk => &self.wanjie_ark, |
| 577 | ProviderKind::Volcengine => &self.volcengine, |
| 578 | ProviderKind::Openrouter => &self.openrouter, |
| 579 | ProviderKind::Orcarouter => &self.orcarouter, |
| 580 | ProviderKind::XiaomiMimo => &self.xiaomi_mimo, |
| 581 | ProviderKind::Novita => &self.novita, |
| 582 | ProviderKind::Fireworks => &self.fireworks, |
| 583 | ProviderKind::Siliconflow => &self.siliconflow, |
| 584 | ProviderKind::SiliconflowCN => &self.siliconflow_cn, |
| 585 | ProviderKind::Arcee => &self.arcee, |
| 586 | ProviderKind::Moonshot => &self.moonshot, |
| 587 | ProviderKind::Sglang => &self.sglang, |
| 588 | ProviderKind::Vllm => &self.vllm, |
| 589 | ProviderKind::Ollama => &self.ollama, |
| 590 | ProviderKind::OllamaCloud => &self.ollama_cloud, |
| 591 | ProviderKind::Huggingface => &self.huggingface, |
| 592 | ProviderKind::Modelscope => &self.modelscope, |
| 593 | ProviderKind::Together => &self.together, |
| 594 | ProviderKind::Qianfan => &self.qianfan, |
| 595 | ProviderKind::OpenaiCodex => &self.openai_codex, |
| 596 | ProviderKind::Anthropic => &self.anthropic, |
| 597 | ProviderKind::Openmodel => &self.openmodel, |
| 598 | ProviderKind::Zai => &self.zai, |
| 599 | ProviderKind::Stepfun => &self.stepfun, |
| 600 | ProviderKind::Minimax => &self.minimax, |
| 601 | ProviderKind::MinimaxAnthropic => &self.minimax_anthropic, |
| 602 | ProviderKind::Deepinfra => &self.deepinfra, |
| 603 | ProviderKind::Sakana => &self.sakana, |
| 604 | ProviderKind::LongCat => &self.longcat, |
| 605 | ProviderKind::OpencodeGo => &self.opencode_go, |
| 606 | ProviderKind::OpencodeZen => &self.opencode_zen, |
| 607 | ProviderKind::Meta => &self.meta, |
| 608 | ProviderKind::Xai => &self.xai, |
| 609 | ProviderKind::Mistral => &self.mistral, |
| 610 | ProviderKind::Google => &self.google, |
| 611 | ProviderKind::Antigravity => &self.antigravity, |
| 612 | ProviderKind::Telecomjs => &self.telecomjs, |
| 613 | ProviderKind::Edenai => &self.edenai, |
| 614 | ProviderKind::Zenmux => &self.zenmux, |
| 615 | ProviderKind::Csdn => &self.csdn, |
| 616 | ProviderKind::Concentrate => &self.concentrate, |
| 617 | ProviderKind::Codewhale => &self.codewhale, |
| 618 | ProviderKind::ModelstudioTokenPlan => &self.modelstudio_token_plan, |
| 619 | ProviderKind::ModelstudioTokenPlanAnthropic => &self.modelstudio_token_plan_anthropic, |
| 620 | ProviderKind::ModelstudioCodingPlan => &self.modelstudio_coding_plan, |
| 621 | ProviderKind::ModelstudioCodingPlanAnthropic => &self.modelstudio_coding_plan_anthropic, |
| 622 | ProviderKind::Custom => &self.custom, |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | pub fn for_provider_mut(&mut self, provider: ProviderKind) -> &mut ProviderConfigToml { |
| 627 | match provider { |
| 628 | ProviderKind::Deepseek => &mut self.deepseek, |
| 629 | ProviderKind::DeepseekAnthropic => &mut self.deepseek_anthropic, |
| 630 | ProviderKind::NvidiaNim => &mut self.nvidia_nim, |
| 631 | ProviderKind::Openai => &mut self.openai, |
| 632 | ProviderKind::Atlascloud => &mut self.atlascloud, |
| 633 | ProviderKind::WanjieArk => &mut self.wanjie_ark, |
| 634 | ProviderKind::Volcengine => &mut self.volcengine, |
| 635 | ProviderKind::Openrouter => &mut self.openrouter, |
| 636 | ProviderKind::Orcarouter => &mut self.orcarouter, |
| 637 | ProviderKind::XiaomiMimo => &mut self.xiaomi_mimo, |
| 638 | ProviderKind::Novita => &mut self.novita, |
| 639 | ProviderKind::Fireworks => &mut self.fireworks, |
| 640 | ProviderKind::Siliconflow => &mut self.siliconflow, |
| 641 | ProviderKind::SiliconflowCN => &mut self.siliconflow_cn, |
| 642 | ProviderKind::Arcee => &mut self.arcee, |
| 643 | ProviderKind::Moonshot => &mut self.moonshot, |
| 644 | ProviderKind::Sglang => &mut self.sglang, |
| 645 | ProviderKind::Vllm => &mut self.vllm, |
| 646 | ProviderKind::Ollama => &mut self.ollama, |
| 647 | ProviderKind::OllamaCloud => &mut self.ollama_cloud, |
| 648 | ProviderKind::Huggingface => &mut self.huggingface, |
| 649 | ProviderKind::Modelscope => &mut self.modelscope, |
| 650 | ProviderKind::Together => &mut self.together, |
| 651 | ProviderKind::Qianfan => &mut self.qianfan, |
| 652 | ProviderKind::OpenaiCodex => &mut self.openai_codex, |
| 653 | ProviderKind::Anthropic => &mut self.anthropic, |
| 654 | ProviderKind::Openmodel => &mut self.openmodel, |
| 655 | ProviderKind::Zai => &mut self.zai, |
| 656 | ProviderKind::Stepfun => &mut self.stepfun, |
| 657 | ProviderKind::Minimax => &mut self.minimax, |
| 658 | ProviderKind::MinimaxAnthropic => &mut self.minimax_anthropic, |
| 659 | ProviderKind::Deepinfra => &mut self.deepinfra, |
| 660 | ProviderKind::Sakana => &mut self.sakana, |
| 661 | ProviderKind::LongCat => &mut self.longcat, |
| 662 | ProviderKind::OpencodeGo => &mut self.opencode_go, |
| 663 | ProviderKind::OpencodeZen => &mut self.opencode_zen, |
| 664 | ProviderKind::Meta => &mut self.meta, |
| 665 | ProviderKind::Xai => &mut self.xai, |
| 666 | ProviderKind::Mistral => &mut self.mistral, |
| 667 | ProviderKind::Google => &mut self.google, |
| 668 | ProviderKind::Antigravity => &mut self.antigravity, |
| 669 | ProviderKind::Telecomjs => &mut self.telecomjs, |
| 670 | ProviderKind::Edenai => &mut self.edenai, |
| 671 | ProviderKind::Zenmux => &mut self.zenmux, |
| 672 | ProviderKind::Csdn => &mut self.csdn, |
| 673 | ProviderKind::Concentrate => &mut self.concentrate, |
| 674 | ProviderKind::Codewhale => &mut self.codewhale, |
| 675 | ProviderKind::ModelstudioTokenPlan => &mut self.modelstudio_token_plan, |
| 676 | ProviderKind::ModelstudioTokenPlanAnthropic => { |
| 677 | &mut self.modelstudio_token_plan_anthropic |
| 678 | } |
| 679 | ProviderKind::ModelstudioCodingPlan => &mut self.modelstudio_coding_plan, |
| 680 | ProviderKind::ModelstudioCodingPlanAnthropic => { |
| 681 | &mut self.modelstudio_coding_plan_anthropic |
| 682 | } |
| 683 | ProviderKind::Custom => &mut self.custom, |
| 684 | } |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | pub(crate) fn deserialize_root_provider<'de, D>( |
| 689 | deserializer: D, |
| 690 | ) -> std::result::Result<ProviderKind, D::Error> |
| 691 | where |
| 692 | D: serde::Deserializer<'de>, |
| 693 | { |
| 694 | let value = String::deserialize(deserializer)?; |
| 695 | let strict = serde::de::value::StringDeserializer::<D::Error>::new(value); |
| 696 | Ok(ProviderKind::deserialize(strict).unwrap_or(ProviderKind::Custom)) |
| 697 | } |
| 698 |