mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-07-26 14:35:22 +08:00
8b925c2f2f
Extract a shared `parse_custom_user_agent` helper in provider.rs returning `Result<Option<HeaderValue>>`, and reuse it in the forwarder, stream check, and model fetch paths so detection, forwarding, and model listing all apply the same provider-level User-Agent. Previously only the forwarder honored it, so stream check could fail (or model listing 403) on UA-gated upstreams that the proxy itself handled fine. - stream_check injects the provider's custom UA on the claude/codex paths and still skips the GitHub Copilot fingerprint UA. - model_fetch service + command and the model-fetch.ts wrapper thread an optional UA through to GET /v1/models. - runtime callers silently ignore invalid values via `.ok().flatten()` (no save-time block, so deeplink imports stay lenient).
32 lines
1.1 KiB
Rust
32 lines
1.1 KiB
Rust
//! 模型列表获取命令
|
|
//!
|
|
//! 提供 Tauri 命令,供前端在供应商表单中获取可用模型列表。
|
|
|
|
use crate::services::model_fetch::{self, FetchedModel};
|
|
|
|
/// 获取供应商的可用模型列表
|
|
///
|
|
/// 使用 OpenAI 兼容的 GET /v1/models 端点。优先使用 `models_url` 精确覆写;
|
|
/// 否则对 baseURL 生成候选列表(含「剥离 Anthropic 兼容子路径」兜底),按序尝试。
|
|
#[tauri::command(rename_all = "camelCase")]
|
|
pub async fn fetch_models_for_config(
|
|
base_url: String,
|
|
api_key: String,
|
|
is_full_url: Option<bool>,
|
|
models_url: Option<String>,
|
|
custom_user_agent: Option<String>,
|
|
) -> Result<Vec<FetchedModel>, String> {
|
|
// 与转发 / 检测路径共用 parse_custom_user_agent:非法 UA 静默忽略(不阻断取模型)。
|
|
let user_agent = crate::provider::parse_custom_user_agent(custom_user_agent.as_deref())
|
|
.ok()
|
|
.flatten();
|
|
model_fetch::fetch_models(
|
|
&base_url,
|
|
&api_key,
|
|
is_full_url.unwrap_or(false),
|
|
models_url.as_deref(),
|
|
user_agent,
|
|
)
|
|
.await
|
|
}
|