| 1 | use crate::provider; |
| 2 | use crate::provider_defaults::*; |
| 3 | use crate::provider_kind::ProviderKind; |
| 4 | use crate::runtime_options::base_url_uses_local_host; |
| 5 | use codewhale_secrets::{SecretSource, Secrets}; |
| 6 | |
| 7 | pub(crate) fn default_base_url_for_provider(provider: ProviderKind) -> &'static str { |
| 8 | provider.provider().default_base_url() |
| 9 | } |
| 10 | |
| 11 | pub(crate) fn moonshot_base_url_uses_kimi_code(base_url: &str) -> bool { |
| 12 | let normalized = base_url.trim_end_matches('/').to_ascii_lowercase(); |
| 13 | normalized == DEFAULT_KIMI_CODE_BASE_URL |
| 14 | || normalized == "https://api.kimi.com/coding" |
| 15 | || normalized.starts_with("https://api.kimi.com/coding/") |
| 16 | } |
| 17 | |
| 18 | /// Dual-wire vendors: dialect is config (`wire`), not a separate ProviderKind. |
| 19 | pub(crate) fn wire_prefers_anthropic(kind: ProviderKind, wire: Option<&str>) -> bool { |
| 20 | if matches!( |
| 21 | kind, |
| 22 | ProviderKind::DeepseekAnthropic |
| 23 | | ProviderKind::MinimaxAnthropic |
| 24 | | ProviderKind::ModelstudioTokenPlanAnthropic |
| 25 | | ProviderKind::ModelstudioCodingPlanAnthropic |
| 26 | ) { |
| 27 | return true; |
| 28 | } |
| 29 | provider::wire_dialect_prefers_anthropic(wire) |
| 30 | } |
| 31 | |
| 32 | pub(crate) fn modelstudio_mode_is_coding_plan(kind: ProviderKind, mode: Option<&str>) -> bool { |
| 33 | if matches!( |
| 34 | kind, |
| 35 | ProviderKind::ModelstudioCodingPlan | ProviderKind::ModelstudioCodingPlanAnthropic |
| 36 | ) { |
| 37 | return true; |
| 38 | } |
| 39 | let Some(raw) = mode.map(str::trim).filter(|value| !value.is_empty()) else { |
| 40 | return false; |
| 41 | }; |
| 42 | let normalized = raw.to_ascii_lowercase().replace(['_', ' '], "-"); |
| 43 | matches!( |
| 44 | normalized.as_str(), |
| 45 | "coding-plan" | "coding" | "codingplan" | "dashscope-coding" | "code" |
| 46 | ) |
| 47 | } |
| 48 | |
| 49 | pub(crate) fn is_modelstudio_family(kind: ProviderKind) -> bool { |
| 50 | matches!( |
| 51 | kind, |
| 52 | ProviderKind::ModelstudioTokenPlan |
| 53 | | ProviderKind::ModelstudioTokenPlanAnthropic |
| 54 | | ProviderKind::ModelstudioCodingPlan |
| 55 | | ProviderKind::ModelstudioCodingPlanAnthropic |
| 56 | ) |
| 57 | } |
| 58 | |
| 59 | pub(crate) fn resolve_modelstudio_base_url( |
| 60 | configured: Option<String>, |
| 61 | kind: ProviderKind, |
| 62 | mode: Option<&str>, |
| 63 | wire: Option<&str>, |
| 64 | ) -> String { |
| 65 | if let Some(url) = configured.filter(|value| !value.trim().is_empty()) { |
| 66 | return url; |
| 67 | } |
| 68 | let coding = modelstudio_mode_is_coding_plan(kind, mode); |
| 69 | let anthropic = wire_prefers_anthropic(kind, wire); |
| 70 | match (coding, anthropic) { |
| 71 | (true, true) => MODELSTUDIO_CODING_PLAN_ANTHROPIC_BASE_URL.to_string(), |
| 72 | (true, false) => DEFAULT_MODELSTUDIO_CODING_PLAN_BASE_URL.to_string(), |
| 73 | (false, true) => MODELSTUDIO_TOKEN_PLAN_ANTHROPIC_BASE_URL.to_string(), |
| 74 | (false, false) => DEFAULT_MODELSTUDIO_TOKEN_PLAN_BASE_URL.to_string(), |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | pub(crate) fn resolve_minimax_base_url( |
| 79 | configured: Option<String>, |
| 80 | kind: ProviderKind, |
| 81 | wire: Option<&str>, |
| 82 | ) -> String { |
| 83 | if let Some(url) = configured.filter(|value| !value.trim().is_empty()) { |
| 84 | return url; |
| 85 | } |
| 86 | if wire_prefers_anthropic(kind, wire) { |
| 87 | DEFAULT_MINIMAX_ANTHROPIC_BASE_URL.to_string() |
| 88 | } else { |
| 89 | DEFAULT_MINIMAX_BASE_URL.to_string() |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | pub(crate) fn resolve_deepseek_base_url( |
| 94 | configured: Option<String>, |
| 95 | kind: ProviderKind, |
| 96 | wire: Option<&str>, |
| 97 | ) -> String { |
| 98 | if let Some(url) = configured.filter(|value| !value.trim().is_empty()) { |
| 99 | return url; |
| 100 | } |
| 101 | if wire_prefers_anthropic(kind, wire) { |
| 102 | DEFAULT_DEEPSEEK_ANTHROPIC_BASE_URL.to_string() |
| 103 | } else { |
| 104 | DEFAULT_DEEPSEEK_BASE_URL.to_string() |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | pub fn xiaomi_mimo_base_url_for_mode(mode: &str) -> Option<&'static str> { |
| 109 | let normalized = mode.trim().to_ascii_lowercase().replace(['_', ' '], "-"); |
| 110 | if normalized.is_empty() || xiaomi_mimo_mode_uses_standard_endpoint(&normalized) { |
| 111 | return None; |
| 112 | } |
| 113 | Some(match normalized.as_str() { |
| 114 | "token-plan" | "tokenplan" | "subscription" | "subscribed" | "plan" => { |
| 115 | DEFAULT_XIAOMI_MIMO_BASE_URL |
| 116 | } |
| 117 | "token-plan-cn" |
| 118 | | "token-plan-china" |
| 119 | | "token-plan-mainland" |
| 120 | | "token-plan-mainland-china" |
| 121 | | "cn" |
| 122 | | "china" => XIAOMI_MIMO_TOKEN_PLAN_CN_BASE_URL, |
| 123 | "token-plan-sgp" |
| 124 | | "token-plan-sg" |
| 125 | | "token-plan-singapore" |
| 126 | | "sgp" |
| 127 | | "sg" |
| 128 | | "singapore" => XIAOMI_MIMO_TOKEN_PLAN_SGP_BASE_URL, |
| 129 | "token-plan-ams" |
| 130 | | "token-plan-eu" |
| 131 | | "token-plan-europe" |
| 132 | | "token-plan-amsterdam" |
| 133 | | "ams" |
| 134 | | "eu" |
| 135 | | "europe" |
| 136 | | "amsterdam" => XIAOMI_MIMO_TOKEN_PLAN_AMS_BASE_URL, |
| 137 | _ => DEFAULT_XIAOMI_MIMO_BASE_URL, |
| 138 | }) |
| 139 | } |
| 140 | |
| 141 | pub fn xiaomi_mimo_mode_uses_standard_endpoint(normalized_mode: &str) -> bool { |
| 142 | matches!( |
| 143 | normalized_mode, |
| 144 | "standard" | "default" | "payg" | "paygo" | "pay-as-you-go" | "pay-as-go" |
| 145 | ) |
| 146 | } |
| 147 | |
| 148 | pub(crate) fn xiaomi_mimo_base_url_uses_token_plan(base_url: &str) -> bool { |
| 149 | let normalized = base_url.trim_end_matches('/').to_ascii_lowercase(); |
| 150 | normalized == XIAOMI_MIMO_TOKEN_PLAN_CN_BASE_URL |
| 151 | || normalized == XIAOMI_MIMO_TOKEN_PLAN_SGP_BASE_URL |
| 152 | || normalized == XIAOMI_MIMO_TOKEN_PLAN_AMS_BASE_URL |
| 153 | } |
| 154 | |
| 155 | pub(crate) const XIAOMI_MIMO_TOKEN_PLAN_ENV_VARS: &[&str] = |
| 156 | &["XIAOMI_MIMO_TOKEN_PLAN_API_KEY", "MIMO_TOKEN_PLAN_API_KEY"]; |
| 157 | |
| 158 | pub(crate) const XIAOMI_MIMO_STANDARD_ENV_VARS: &[&str] = |
| 159 | &["XIAOMI_MIMO_API_KEY", "XIAOMI_API_KEY", "MIMO_API_KEY"]; |
| 160 | |
| 161 | pub(crate) fn xiaomi_mimo_env_api_key_for_runtime( |
| 162 | mode: Option<&str>, |
| 163 | base_url: Option<&str>, |
| 164 | ) -> Option<String> { |
| 165 | let env_value = |vars: &[&str]| codewhale_secrets::env_first(vars).map(|(_, value)| value); |
| 166 | |
| 167 | let normalized_mode = |
| 168 | mode.map(|value| value.trim().to_ascii_lowercase().replace(['_', ' '], "-")); |
| 169 | let standard_selected = normalized_mode |
| 170 | .as_deref() |
| 171 | .is_some_and(xiaomi_mimo_mode_uses_standard_endpoint) |
| 172 | || base_url.is_some_and(xiaomi_mimo_base_url_is_pay_as_you_go); |
| 173 | if standard_selected { |
| 174 | return env_value(XIAOMI_MIMO_STANDARD_ENV_VARS); |
| 175 | } |
| 176 | |
| 177 | let token_plan_selected = normalized_mode |
| 178 | .as_deref() |
| 179 | .and_then(xiaomi_mimo_base_url_for_mode) |
| 180 | .is_some() |
| 181 | || base_url.is_some_and(xiaomi_mimo_base_url_uses_token_plan); |
| 182 | if token_plan_selected { |
| 183 | return env_value(XIAOMI_MIMO_TOKEN_PLAN_ENV_VARS); |
| 184 | } |
| 185 | |
| 186 | env_value(XIAOMI_MIMO_TOKEN_PLAN_ENV_VARS).or_else(|| env_value(XIAOMI_MIMO_STANDARD_ENV_VARS)) |
| 187 | } |
| 188 | |
| 189 | pub fn resolve_xiaomi_mimo_base_url( |
| 190 | configured: Option<String>, |
| 191 | api_key: Option<&str>, |
| 192 | mode: Option<&str>, |
| 193 | ) -> String { |
| 194 | let normalized_mode = |
| 195 | mode.map(|value| value.trim().to_ascii_lowercase().replace(['_', ' '], "-")); |
| 196 | let uses_standard_mode = normalized_mode |
| 197 | .as_deref() |
| 198 | .is_some_and(xiaomi_mimo_mode_uses_standard_endpoint); |
| 199 | let mode_base_url = normalized_mode |
| 200 | .as_deref() |
| 201 | .and_then(xiaomi_mimo_base_url_for_mode); |
| 202 | let uses_token_plan = xiaomi_mimo_api_key_uses_token_plan(api_key); |
| 203 | match configured { |
| 204 | Some(base_url) if uses_standard_mode => base_url, |
| 205 | Some(base_url) if uses_token_plan && xiaomi_mimo_base_url_is_pay_as_you_go(&base_url) => { |
| 206 | mode_base_url |
| 207 | .unwrap_or(DEFAULT_XIAOMI_MIMO_BASE_URL) |
| 208 | .to_string() |
| 209 | } |
| 210 | Some(base_url) => base_url, |
| 211 | None => { |
| 212 | if let Some(base_url) = mode_base_url { |
| 213 | base_url.to_string() |
| 214 | } else if uses_standard_mode { |
| 215 | XIAOMI_MIMO_PAY_AS_YOU_GO_BASE_URL.to_string() |
| 216 | } else if uses_token_plan || api_key.is_none() { |
| 217 | DEFAULT_XIAOMI_MIMO_BASE_URL.to_string() |
| 218 | } else { |
| 219 | XIAOMI_MIMO_PAY_AS_YOU_GO_BASE_URL.to_string() |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | pub fn xiaomi_mimo_api_key_uses_token_plan(api_key: Option<&str>) -> bool { |
| 226 | api_key.is_some_and(|key| key.trim_start().starts_with("tp-")) |
| 227 | } |
| 228 | |
| 229 | pub(crate) fn xiaomi_mimo_base_url_is_pay_as_you_go(base_url: &str) -> bool { |
| 230 | matches!( |
| 231 | base_url.trim_end_matches('/').to_ascii_lowercase().as_str(), |
| 232 | "https://api.xiaomimimo.com" | "https://api.xiaomimimo.com/v1" |
| 233 | ) |
| 234 | } |
| 235 | |
| 236 | /// Whether `base_url` belongs to the provider's official endpoint family. |
| 237 | /// |
| 238 | /// Some providers publish multiple stable paths for the same credential and |
| 239 | /// model namespace. Keep that family definition centralized so route |
| 240 | /// canonicalization and credential scoping cannot disagree. |
| 241 | #[must_use] |
| 242 | pub fn provider_base_url_is_official(provider: ProviderKind, base_url: &str) -> bool { |
| 243 | let normalized = base_url.trim().trim_end_matches('/').to_ascii_lowercase(); |
| 244 | match provider { |
| 245 | ProviderKind::Deepseek | ProviderKind::DeepseekAnthropic => { |
| 246 | provider_published_pricing_base_urls(provider) |
| 247 | .iter() |
| 248 | .any(|candidate| normalized == *candidate) |
| 249 | } |
| 250 | ProviderKind::Siliconflow | ProviderKind::SiliconflowCN => matches!( |
| 251 | normalized.as_str(), |
| 252 | "https://api.siliconflow.com/v1" | "https://api.siliconflow.cn/v1" |
| 253 | ), |
| 254 | ProviderKind::Moonshot => { |
| 255 | matches!( |
| 256 | normalized.as_str(), |
| 257 | DEFAULT_MOONSHOT_BASE_URL | MOONSHOT_CN_BASE_URL |
| 258 | ) || moonshot_base_url_uses_kimi_code(base_url) |
| 259 | } |
| 260 | ProviderKind::Zai => matches!( |
| 261 | normalized.as_str(), |
| 262 | "https://api.z.ai/api/coding/paas/v4" |
| 263 | | "https://api.z.ai/api/paas/v4" |
| 264 | | "https://api.z.ai/api/anthropic" |
| 265 | | "https://open.bigmodel.cn/api/paas/v4" |
| 266 | | "https://open.bigmodel.cn/api/coding/paas/v4" |
| 267 | | "https://open.bigmodel.cn/api/anthropic" |
| 268 | ), |
| 269 | ProviderKind::XiaomiMimo => { |
| 270 | xiaomi_mimo_base_url_uses_token_plan(base_url) |
| 271 | || xiaomi_mimo_base_url_is_pay_as_you_go(base_url) |
| 272 | } |
| 273 | // StepFun publishes one vendor over four hosts: global (.ai) and |
| 274 | // China (.com), each with a pay-as-you-go `/v1` and a Step Plan |
| 275 | // `/step_plan/v1` surface. All four are StepFun's own, documented in |
| 276 | // its console. Recognising only the global PAYG default made a Step |
| 277 | // Plan subscriber's route read as a custom endpoint, and |
| 278 | // `catalog_models_for_route` withholds the catalog from a custom |
| 279 | // endpoint — so the picker reported `0 bundled`, offered no model |
| 280 | // list, and fell back to a guessed context window on a route whose |
| 281 | // console lists `step-5-preview` at 1M context. |
| 282 | // |
| 283 | // Note the plan hosts are listed from StepFun's console, not from |
| 284 | // models.dev, whose `stepfun-ai-step-plan` entry is missing |
| 285 | // `step-5-preview` that the vendor itself advertises. An aggregator |
| 286 | // is a secondary source for what a host serves; the vendor is not. |
| 287 | ProviderKind::Stepfun => matches!( |
| 288 | normalized.as_str(), |
| 289 | "https://api.stepfun.ai/v1" |
| 290 | | "https://api.stepfun.ai/step_plan/v1" |
| 291 | | "https://api.stepfun.com/v1" |
| 292 | | "https://api.stepfun.com/step_plan/v1" |
| 293 | ), |
| 294 | ProviderKind::Ollama => { |
| 295 | normalized == DEFAULT_OLLAMA_BASE_URL |
| 296 | || provider::is_exact_ollama_cloud_route(provider, base_url) |
| 297 | } |
| 298 | ProviderKind::OllamaCloud => provider::is_exact_ollama_cloud_route(provider, base_url), |
| 299 | ProviderKind::Edenai => matches!( |
| 300 | normalized.as_str(), |
| 301 | "https://api.edenai.run/v3" | "https://api.eu.edenai.run/v3" |
| 302 | ), |
| 303 | ProviderKind::Zenmux => normalized == DEFAULT_ZENMUX_BASE_URL, |
| 304 | ProviderKind::Csdn => normalized == DEFAULT_CSDN_BASE_URL, |
| 305 | ProviderKind::Concentrate => normalized == DEFAULT_CONCENTRATE_BASE_URL, |
| 306 | // The Codewhale API's official endpoint family is its default base |
| 307 | // plus whatever the operator declared in `CODEWHALE_API_BASE` — the |
| 308 | // route's own documented override, already validated as HTTPS or |
| 309 | // loopback, and the exact shape `CODEWHALE_CLOUD_API_BASE` has for the |
| 310 | // account surface. Treating that declared origin as "custom" is what |
| 311 | // silently stripped the account bearer and dispatched unauthenticated. |
| 312 | // A base URL from anywhere else stays custom and keyless. |
| 313 | ProviderKind::Codewhale => { |
| 314 | normalized == DEFAULT_CODEWHALE_BASE_URL |
| 315 | || provider::codewhale_api_base_from_env().is_some_and(|declared| { |
| 316 | normalized == declared.trim_end_matches('/').to_ascii_lowercase() |
| 317 | }) |
| 318 | } |
| 319 | // Custom routes have no Codewhale-owned official endpoint. The |
| 320 | // descriptor URL is a schema placeholder, never a credential scope. |
| 321 | ProviderKind::Custom => false, |
| 322 | _ => { |
| 323 | normalized |
| 324 | == default_base_url_for_provider(provider) |
| 325 | .trim() |
| 326 | .trim_end_matches('/') |
| 327 | .to_ascii_lowercase() |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | pub fn provider_published_pricing_base_urls(provider: ProviderKind) -> Vec<&'static str> { |
| 333 | match provider { |
| 334 | ProviderKind::Deepseek => vec![ |
| 335 | "https://api.deepseek.com", |
| 336 | "https://api.deepseek.com/v1", |
| 337 | "https://api.deepseek.com/beta", |
| 338 | ], |
| 339 | ProviderKind::DeepseekAnthropic => vec![ |
| 340 | "https://api.deepseek.com/anthropic", |
| 341 | "https://api.deepseek.com/anthropic/v1", |
| 342 | ], |
| 343 | ProviderKind::Custom => vec![], |
| 344 | _ => { |
| 345 | let extra: &[&'static str] = match provider { |
| 346 | ProviderKind::Anthropic => &["https://api.anthropic.com"], |
| 347 | ProviderKind::Openai => &["https://api.openai.com", "https://api.openai.com/v1"], |
| 348 | ProviderKind::Zai => &[ |
| 349 | "https://api.z.ai", |
| 350 | "https://api.z.ai/v1", |
| 351 | "https://api.z.ai/api/paas/v4", |
| 352 | "https://api.z.ai/api/anthropic", |
| 353 | "https://open.bigmodel.cn/api/paas/v4", |
| 354 | "https://open.bigmodel.cn/api/coding/paas/v4", |
| 355 | "https://open.bigmodel.cn/api/anthropic", |
| 356 | ], |
| 357 | ProviderKind::Moonshot => &[ |
| 358 | "https://api.moonshot.ai", |
| 359 | "https://api.moonshot.ai/v1", |
| 360 | "https://api.moonshot.ai/anthropic", |
| 361 | "https://api.moonshot.cn", |
| 362 | "https://api.moonshot.cn/v1", |
| 363 | "https://api.moonshot.cn/anthropic", |
| 364 | ], |
| 365 | ProviderKind::Minimax => &[ |
| 366 | "https://api.minimax.io", |
| 367 | "https://api.minimax.io/v1", |
| 368 | "https://api.minimaxi.com", |
| 369 | "https://api.minimaxi.com/v1", |
| 370 | "https://api.minimax.chat", |
| 371 | "https://api.minimax.chat/v1", |
| 372 | ], |
| 373 | ProviderKind::MinimaxAnthropic => &[ |
| 374 | "https://api.minimax.io/anthropic", |
| 375 | "https://api.minimax.io/anthropic/v1", |
| 376 | "https://api.minimaxi.com/anthropic", |
| 377 | "https://api.minimaxi.com/anthropic/v1", |
| 378 | "https://api.minimax.chat/anthropic", |
| 379 | "https://api.minimax.chat/anthropic/v1", |
| 380 | ], |
| 381 | ProviderKind::Stepfun => &[ |
| 382 | "https://api.stepfun.ai", |
| 383 | "https://api.stepfun.ai/v1", |
| 384 | "https://api.stepfun.com", |
| 385 | "https://api.stepfun.com/v1", |
| 386 | ], |
| 387 | ProviderKind::XiaomiMimo => &["https://api.xiaomimimo.com/v1"], |
| 388 | ProviderKind::Volcengine => &["https://ark.cn-beijing.volces.com/api/v3"], |
| 389 | ProviderKind::Siliconflow => &[ |
| 390 | "https://api.siliconflow.com/v1", |
| 391 | "https://api.siliconflow.cn/v1", |
| 392 | ], |
| 393 | ProviderKind::Edenai => { |
| 394 | &["https://api.edenai.run/v3", "https://api.eu.edenai.run/v3"] |
| 395 | } |
| 396 | ProviderKind::ModelstudioTokenPlan |
| 397 | | ProviderKind::ModelstudioTokenPlanAnthropic |
| 398 | | ProviderKind::ModelstudioCodingPlan |
| 399 | | ProviderKind::ModelstudioCodingPlanAnthropic => &[ |
| 400 | "https://dashscope-intl.aliyuncs.com/compatible-mode/v1", |
| 401 | "https://dashscope.aliyuncs.com/compatible-mode/v1", |
| 402 | ], |
| 403 | _ => &[], |
| 404 | }; |
| 405 | let default = default_base_url_for_provider(provider); |
| 406 | let mut urls = vec![default]; |
| 407 | urls.extend(extra.iter().copied().filter(|url| *url != default)); |
| 408 | urls |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | pub(crate) fn base_url_is_custom_for_provider(provider: ProviderKind, base_url: &str) -> bool { |
| 414 | !provider_base_url_is_official(provider, base_url) |
| 415 | } |
| 416 | |
| 417 | /// Whether `base_url` is outside the provider's official endpoint family and |
| 418 | /// therefore owns its model-id namespace. |
| 419 | /// |
| 420 | /// Custom OpenAI-compatible endpoints must receive the exact model selector |
| 421 | /// the user supplied. Official endpoints may safely canonicalize known aliases |
| 422 | /// to their provider wire ids. |
| 423 | #[must_use] |
| 424 | pub fn provider_preserves_custom_base_url_model(provider: ProviderKind, base_url: &str) -> bool { |
| 425 | base_url_is_custom_for_provider(provider, base_url) |
| 426 | } |
| 427 | |
| 428 | pub(crate) fn should_skip_secret_store_for_provider( |
| 429 | provider: ProviderKind, |
| 430 | base_url: &str, |
| 431 | auth_mode: Option<&str>, |
| 432 | ) -> bool { |
| 433 | if auth_mode_disables_api_key(auth_mode) { |
| 434 | return true; |
| 435 | } |
| 436 | if base_url_is_custom_for_provider(provider, base_url) { |
| 437 | return true; |
| 438 | } |
| 439 | if auth_mode_requires_api_key(auth_mode) { |
| 440 | return false; |
| 441 | } |
| 442 | // The Codewhale API authenticates on every origin it is allowed to reach, |
| 443 | // including the loopback test origin `CODEWHALE_API_BASE` may name. It is |
| 444 | // never a keyless local runtime. |
| 445 | if provider == ProviderKind::Codewhale { |
| 446 | return false; |
| 447 | } |
| 448 | |
| 449 | matches!(provider, ProviderKind::Sglang | ProviderKind::Vllm) |
| 450 | || (provider == ProviderKind::Ollama |
| 451 | && !provider::is_exact_ollama_cloud_route(provider, base_url)) |
| 452 | || base_url_uses_local_host(base_url) |
| 453 | } |
| 454 | |
| 455 | /// Read the durable provider slot without allowing environment fallback to |
| 456 | /// jump ahead of the bounded legacy slot. The old `ollama` slot is consulted |
| 457 | /// only for the exact route tuple migrated above; selecting `ollama-cloud` |
| 458 | /// directly never consumes a local provider credential. |
| 459 | pub(crate) fn stored_api_key_for_provider( |
| 460 | secrets: &Secrets, |
| 461 | provider: ProviderKind, |
| 462 | legacy_ollama_cloud: bool, |
| 463 | ) -> Option<(String, SecretSource)> { |
| 464 | let mut slots = vec![provider.secret_store_slot()]; |
| 465 | if provider == ProviderKind::OllamaCloud && legacy_ollama_cloud { |
| 466 | slots.push(ProviderKind::Ollama.secret_store_slot()); |
| 467 | } |
| 468 | slots.into_iter().find_map(|slot| { |
| 469 | secrets |
| 470 | .get(slot) |
| 471 | .ok() |
| 472 | .flatten() |
| 473 | .filter(|value| !value.trim().is_empty()) |
| 474 | .map(|value| (value, SecretSource::Keyring)) |
| 475 | }) |
| 476 | } |
| 477 | |
| 478 | /// The provider's API key from its own environment variables, the single |
| 479 | /// list on its descriptor ([`provider::Provider::env_vars`]). |
| 480 | /// |
| 481 | /// Xiaomi MiMo is the exception: its token-plan variables belong to the |
| 482 | /// token-plan endpoints, and `xiaomi_mimo_env_api_key_for_runtime` (which the |
| 483 | /// resolver tries first) is the only reader that knows the selected mode. This |
| 484 | /// fallback reads the standard variables only, so a token-plan key is never |
| 485 | /// sent to the pay-as-you-go endpoint. |
| 486 | pub(crate) fn env_api_key_for_provider(provider: ProviderKind) -> Option<String> { |
| 487 | let env_vars = if provider == ProviderKind::XiaomiMimo { |
| 488 | XIAOMI_MIMO_STANDARD_ENV_VARS |
| 489 | } else { |
| 490 | provider.provider().env_vars() |
| 491 | }; |
| 492 | codewhale_secrets::env_first(env_vars).map(|(_, value)| value) |
| 493 | } |
| 494 | |
| 495 | /// Whether an authentication mode requires API-key material. |
| 496 | #[must_use] |
| 497 | pub fn auth_mode_requires_api_key(auth_mode: Option<&str>) -> bool { |
| 498 | matches!( |
| 499 | auth_mode |
| 500 | .map(str::trim) |
| 501 | .filter(|value| !value.is_empty()) |
| 502 | .map(|value| value.to_ascii_lowercase()), |
| 503 | Some(value) |
| 504 | if matches!( |
| 505 | value.as_str(), |
| 506 | "api_key" | "api-key" | "apikey" | "bearer" | "bearer-token" |
| 507 | ) |
| 508 | ) |
| 509 | } |
| 510 | |
| 511 | /// Whether an authentication mode explicitly disables upstream provider auth. |
| 512 | #[must_use] |
| 513 | pub fn auth_mode_disables_api_key(auth_mode: Option<&str>) -> bool { |
| 514 | matches!( |
| 515 | auth_mode |
| 516 | .map(str::trim) |
| 517 | .filter(|value| !value.is_empty()) |
| 518 | .map(|value| value.to_ascii_lowercase()), |
| 519 | Some(value) |
| 520 | if matches!( |
| 521 | value.as_str(), |
| 522 | "none" | "off" | "disabled" | "no_auth" | "no-auth" | "anonymous" |
| 523 | ) |
| 524 | ) |
| 525 | } |
| 526 | |
| 527 | /// Whether an authentication mode selects Kimi's imported bearer token. |
| 528 | #[must_use] |
| 529 | pub fn auth_mode_uses_kimi_imported_token(auth_mode: &str) -> bool { |
| 530 | matches!( |
| 531 | auth_mode |
| 532 | .trim() |
| 533 | .to_ascii_lowercase() |
| 534 | .replace('-', "_") |
| 535 | .as_str(), |
| 536 | "kimi" | "kimi_oauth" | "kimi_cli" | "oauth" |
| 537 | ) |
| 538 | } |
| 539 |