mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-07-29 18:05:37 +08:00
67e074c0a7
* style(frontend): reformat provider forms, constants and hooks
Apply prettier formatting across 5 frontend files. No logic changes.
Changed files:
- AddProviderDialog.tsx: reformat generic type annotation and callback
- ClaudeFormFields.tsx: consolidate multi-line useState and Collapsible props
- CodexConfigSections.tsx: expand single-line React imports to multi-line,
collapse removeCodexTopLevelField() call
- constants.ts: merge TemplateType into single line
- useSkills.ts: expand single-line TanStack Query imports to multi-line,
reformat uninstallSkill mutationFn chain
* deps(proxy): add hyper ecosystem crates and manual decompression libs
reqwest internally normalizes all header names to lowercase and does not
preserve insertion order, causing proxied requests to differ from the
original client requests. To achieve transparent header forwarding with
original casing and order, introduce lower-level hyper HTTP client libs.
New dependencies:
- hyper-util 0.1: TokioExecutor + legacy Client with
preserve_header_case support for HTTP/1.1
- hyper-rustls 0.27: rustls-based TLS connector for hyper
- http 1 / http-body 1 / http-body-util 0.1: HTTP type crates for
hyper 1.x request/response construction
- flate2 1: manual gzip/deflate decompression (replaces reqwest auto)
- brotli 7: manual brotli decompression
Changed dependencies:
- serde_json: enable preserve_order feature to keep JSON field order
- reqwest: drop gzip feature to prevent reqwest from overriding the
client's original accept-encoding header
* refactor(proxy): use hyper client for header-case preserving forwarding
Previously the proxy used reqwest for all upstream requests. reqwest
normalizes header names to lowercase and reorders them internally,
making proxied requests distinguishable from direct CLI requests.
Some upstream providers are sensitive to these differences.
This commit replaces reqwest with a hyper-based HTTP client on the
default (non-proxy) path, achieving wire-level header fidelity:
Server layer (server.rs):
- Replace axum::serve with a manual hyper HTTP/1.1 accept loop
- Enable preserve_header_case(true) so incoming header casing is
captured in a HeaderCaseMap extension on each request
- Bridge hyper requests to axum Router via tower::Service
New hyper client module (hyper_client.rs):
- Lazy-initialized hyper-util Client with preserve_header_case
- ProxyResponse enum wrapping both hyper::Response and reqwest::Response
behind a unified interface (status, headers, bytes, bytes_stream)
- send_request() builds requests with ordered HeaderMap + case map
Request handlers (handlers.rs):
- Switch from (HeaderMap, Json<Value>) extractors to raw
axum::extract::Request to preserve Extensions (containing the
HeaderCaseMap from the accept loop)
- Pass extensions through the forwarding chain
Forwarder (forwarder.rs):
- Remove HEADER_BLACKLIST array; replace with ordered header iteration
that preserves original header sequence and casing
- Build ordered_headers by iterating client headers, skipping only
auth/host/content-length, and inserting auth headers at the original
authorization position to maintain order
- Handle anthropic-beta (ensure claude-code-20250219 tag) and
anthropic-version (passthrough or default) inline during iteration
- Remove should_force_identity_encoding() — accept-encoding is now
transparently forwarded to upstream
- Use hyper client by default; fall back to reqwest only when an
HTTP/SOCKS5 proxy tunnel is configured
Provider adapters (adapter.rs, claude.rs, codex.rs, gemini.rs):
- Replace add_auth_headers(RequestBuilder) -> RequestBuilder with
get_auth_headers(AuthInfo) -> Vec<(HeaderName, HeaderValue)>
- Adapters now return header pairs instead of mutating a reqwest builder
- Claude adapter: merge Anthropic/ClaudeAuth/Bearer into single branch;
move Copilot fingerprint headers into get_auth_headers
Response processing (response_processor.rs):
- Add manual decompression (gzip/deflate/brotli via flate2 + brotli)
for non-streaming responses, since reqwest auto-decompression is now
disabled to allow accept-encoding passthrough
- Add compressed-SSE warning log for streaming responses
- Accept ProxyResponse instead of reqwest::Response
HTTP client (http_client.rs):
- Disable reqwest auto-decompression (.no_gzip/.no_brotli/.no_deflate)
on both global and per-provider clients
Streaming adapters (streaming.rs, streaming_responses.rs):
- Generalize stream error type from reqwest::Error to generic E: Error
Misc:
- log_codes.rs: add SRV-005 (ACCEPT_ERR) and SRV-006 (CONN_ERR)
- stream_check.rs: reformat copilot header lines
- transform.rs: fix trailing whitespace alignment
* fix(lint): resolve 35 clippy warnings across Rust codebase
Fix all clippy warnings reported by `cargo clippy --lib`:
- codex_config.rs: fix doc_overindented_list_items (3 spaces -> 2)
- commands/copilot.rs: inline format args in 2 log::error! calls
- commands/provider.rs: inline format args in 3 map_err closures
- proxy/hyper_client.rs: inline format arg in log::debug! call
- proxy/providers/copilot_auth.rs: inline format args in 16 locations
(log macros, format! in headers, error constructors)
- proxy/thinking_optimizer.rs: inline format args in 2 log::info! calls
- services/skill.rs: inline format args in log::debug! call
- services/webdav_sync.rs: inline format args in 6 format! calls
(version compat messages, download limit messages)
- services/webdav_sync/archive.rs: inline format args in 2 format! calls
- session_manager/providers/opencode.rs: inline format args in
source_path format!
All fixes use the clippy::uninlined_format_args suggestion pattern:
format!("msg: {}", var) -> format!("msg: {var}")
* deps(proxy): add raw HTTP write and native TLS cert dependencies
Add crates required for the raw TCP/TLS write path that bypasses
hyper's header encoder to preserve original header name casing:
- httparse: parse raw TCP peek bytes to capture header casings
- tokio-rustls + rustls: direct TLS connections for raw write path
- webpki-roots: Mozilla CA bundle baseline
- rustls-native-certs: load system keychain CAs (trusts proxy MITM
certificates from Clash, mitmproxy, etc.)
* fix(proxy): address code review feedback on response handling
Fixes from PR #1714 code review:
- Extract `read_decoded_body()` and `strip_entity_headers_for_rebuilt_body()`
in response_processor to properly clean content-encoding/content-length
headers after decompression
- Reuse `read_decoded_body()` in handlers.rs for Claude transform path,
ensuring compressed responses are decoded before format conversion
- Make `build_proxy_url_from_config()` public so forwarder can pass proxy
URL to the hyper raw write path
- Add `has_system_proxy_env()` utility with test coverage
- Add 50ms backoff after accept() failures in server.rs to prevent
tight-loop CPU spin on transient socket errors
* feat(proxy): implement raw TCP/TLS write with HTTP CONNECT tunnel
Rewrite hyper_client with a two-tier strategy for header case preservation:
Primary path (raw write):
- Peek raw TCP bytes in server.rs to capture OriginalHeaderCases before
hyper lowercases them
- Build raw HTTP/1.1 request bytes with exact original header name casing
- Write directly to TLS stream, then use WriteFilter to let hyper parse
the response while discarding its duplicate request writes
- Support HTTP CONNECT tunneling through upstream proxies, so header case
is preserved even when a proxy (Clash, V2Ray) is configured
Fallback path (hyper-util Client):
- Used when OriginalHeaderCases is empty or raw write fails
- Configured with title_case_headers(true) for best-effort casing
TLS improvements:
- Load native system certificates alongside webpki roots so proxy MITM
CAs (installed in system keychain) are trusted through CONNECT tunnels
Key types added:
- OriginalHeaderCases: maps lowercase name → original wire-casing bytes
- WriteFilter<S>: AsyncRead+AsyncWrite wrapper that discards writes
- connect_via_proxy(): HTTP CONNECT tunnel establishment
- ExtensionDebugMarker: diagnostic marker for extension chain debugging
* refactor(proxy): route requests through hyper with proxy-aware forwarding
Rework forwarder request dispatch to always prefer the hyper raw write
path (header case preservation) over reqwest:
Request routing:
- HTTP/HTTPS proxy: hyper raw write through CONNECT tunnel (case preserved)
- SOCKS5 proxy: reqwest fallback (CONNECT not supported for SOCKS5)
- No proxy: hyper raw write direct connection
Header handling improvements:
- Replace host header in-place at original position instead of
skip-and-append, preserving client's header ordering
- Preserve client's original accept-encoding for transparent passthrough;
only force identity encoding when transform path needs decompression
- Add should_force_identity_encoding() to centralize the decision
- Remove hardcoded 'br, gzip, deflate' override that masked client values
Proxy URL resolution (priority order):
1. Provider-specific proxy config (if enabled)
2. Global proxy URL configured in CC Switch
3. Direct connection (no proxy)
* chore(proxy): remove dead code, redundant tests and debug scaffolding
- Inline should_force_identity_encoding() (was just `needs_transform`)
and delete its 5 test cases
- Remove ExtensionDebugMarker diagnostic type
- Remove unused has_system_proxy_env() and its test
- Remove strip_entity_headers test
- Simplify hyper path: remove redundant is_socks_proxy ternary
- Update hyper_client module doc to reflect CONNECT tunnel support
* fix(proxy): block direct-connect fallback and complete CONNECT tunnel support
* feat(hooks): improve proxy requirement warnings with specific reasons
- Remove redundant OpenAI format hint toast messages
- Add detailed reason detection for proxy requirements (OpenAI Chat, OpenAI Responses, full URL mode)
- Update i18n files with new reason-specific keys
* style(*): format code with prettier
- Remove extra whitespace in http_client.rs
- Fix formatting issues in useProviderActions.ts
* fix(proxy): post-merge fixes for forward return type and clippy warnings
- Restore forward() return type to (ProxyResponse, Option<String>)
to pass claude_api_format through to callers
- Inline format args in log::warn! macro (clippy::uninlined_format_args)
- Suppress too_many_arguments for check_claude_stream
* refactor(proxy): preserve original header wire order and add non-streaming body timeout
- Rewrite build_raw_request to emit headers in original
client-sent sequence instead of hash-map order
- Remove unused OriginalHeaderCases::get_all method
- Add body_timeout to read_decoded_body to prevent
requests hanging when upstream stalls after headers
835 lines
28 KiB
Rust
835 lines
28 KiB
Rust
//! Claude (Anthropic) Provider Adapter
|
||
//!
|
||
//! 支持透传模式和 OpenAI 格式转换模式
|
||
//!
|
||
//! ## API 格式
|
||
//! - **anthropic** (默认): Anthropic Messages API 格式,直接透传
|
||
//! - **openai_chat**: OpenAI Chat Completions 格式,需要 Anthropic ↔ OpenAI 转换
|
||
//! - **openai_responses**: OpenAI Responses API 格式,需要 Anthropic ↔ Responses 转换
|
||
//!
|
||
//! ## 认证模式
|
||
//! - **Claude**: Anthropic 官方 API (x-api-key + anthropic-version)
|
||
//! - **ClaudeAuth**: 中转服务 (仅 Bearer 认证,无 x-api-key)
|
||
//! - **OpenRouter**: 已支持 Claude Code 兼容接口,默认透传
|
||
//! - **GitHubCopilot**: GitHub Copilot (OAuth + Copilot Token)
|
||
|
||
use super::{AuthInfo, AuthStrategy, ProviderAdapter, ProviderType};
|
||
use crate::provider::Provider;
|
||
use crate::proxy::error::ProxyError;
|
||
|
||
/// 获取 Claude 供应商的 API 格式
|
||
///
|
||
/// 供 handler/forwarder 外部使用的公开函数。
|
||
/// 优先级:meta.apiFormat > settings_config.api_format > openrouter_compat_mode > 默认 "anthropic"
|
||
pub fn get_claude_api_format(provider: &Provider) -> &'static str {
|
||
// 1) Preferred: meta.apiFormat (SSOT, never written to Claude Code config)
|
||
if let Some(meta) = provider.meta.as_ref() {
|
||
if let Some(api_format) = meta.api_format.as_deref() {
|
||
return match api_format {
|
||
"openai_chat" => "openai_chat",
|
||
"openai_responses" => "openai_responses",
|
||
_ => "anthropic",
|
||
};
|
||
}
|
||
}
|
||
|
||
// 2) Backward compatibility: legacy settings_config.api_format
|
||
if let Some(api_format) = provider
|
||
.settings_config
|
||
.get("api_format")
|
||
.and_then(|v| v.as_str())
|
||
{
|
||
return match api_format {
|
||
"openai_chat" => "openai_chat",
|
||
"openai_responses" => "openai_responses",
|
||
_ => "anthropic",
|
||
};
|
||
}
|
||
|
||
// 3) Backward compatibility: legacy openrouter_compat_mode (bool/number/string)
|
||
let raw = provider.settings_config.get("openrouter_compat_mode");
|
||
let enabled = match raw {
|
||
Some(serde_json::Value::Bool(v)) => *v,
|
||
Some(serde_json::Value::Number(num)) => num.as_i64().unwrap_or(0) != 0,
|
||
Some(serde_json::Value::String(value)) => {
|
||
let normalized = value.trim().to_lowercase();
|
||
normalized == "true" || normalized == "1"
|
||
}
|
||
_ => false,
|
||
};
|
||
|
||
if enabled {
|
||
"openai_chat"
|
||
} else {
|
||
"anthropic"
|
||
}
|
||
}
|
||
|
||
pub fn claude_api_format_needs_transform(api_format: &str) -> bool {
|
||
matches!(api_format, "openai_chat" | "openai_responses")
|
||
}
|
||
|
||
pub fn transform_claude_request_for_api_format(
|
||
body: serde_json::Value,
|
||
provider: &Provider,
|
||
api_format: &str,
|
||
) -> Result<serde_json::Value, ProxyError> {
|
||
let cache_key = provider
|
||
.meta
|
||
.as_ref()
|
||
.and_then(|m| m.prompt_cache_key.as_deref())
|
||
.unwrap_or(&provider.id);
|
||
|
||
match api_format {
|
||
"openai_responses" => {
|
||
super::transform_responses::anthropic_to_responses(body, Some(cache_key))
|
||
}
|
||
"openai_chat" => super::transform::anthropic_to_openai(body, Some(cache_key)),
|
||
_ => Ok(body),
|
||
}
|
||
}
|
||
|
||
/// Claude 适配器
|
||
pub struct ClaudeAdapter;
|
||
|
||
impl ClaudeAdapter {
|
||
pub fn new() -> Self {
|
||
Self
|
||
}
|
||
|
||
/// 获取供应商类型
|
||
///
|
||
/// 根据 base_url 和 auth_mode 检测具体的供应商类型:
|
||
/// - GitHubCopilot: meta.provider_type 为 github_copilot 或 base_url 包含 githubcopilot.com
|
||
/// - OpenRouter: base_url 包含 openrouter.ai
|
||
/// - ClaudeAuth: auth_mode 为 bearer_only
|
||
/// - Claude: 默认 Anthropic 官方
|
||
pub fn provider_type(&self, provider: &Provider) -> ProviderType {
|
||
// 检测 GitHub Copilot
|
||
if self.is_github_copilot(provider) {
|
||
return ProviderType::GitHubCopilot;
|
||
}
|
||
|
||
// 检测 OpenRouter
|
||
if self.is_openrouter(provider) {
|
||
return ProviderType::OpenRouter;
|
||
}
|
||
|
||
// 检测 ClaudeAuth (仅 Bearer 认证)
|
||
if self.is_bearer_only_mode(provider) {
|
||
return ProviderType::ClaudeAuth;
|
||
}
|
||
|
||
ProviderType::Claude
|
||
}
|
||
|
||
/// 检测是否为 GitHub Copilot 供应商
|
||
fn is_github_copilot(&self, provider: &Provider) -> bool {
|
||
// 方式1: 检查 meta.provider_type
|
||
if let Some(meta) = provider.meta.as_ref() {
|
||
if meta.provider_type.as_deref() == Some("github_copilot") {
|
||
return true;
|
||
}
|
||
}
|
||
|
||
// 方式2: 检查 base_url(兼容旧数据的 fallback,后续应优先依赖 providerType)
|
||
if let Ok(base_url) = self.extract_base_url(provider) {
|
||
if base_url.contains("githubcopilot.com") {
|
||
return true;
|
||
}
|
||
}
|
||
|
||
false
|
||
}
|
||
|
||
/// 检测是否使用 OpenRouter
|
||
fn is_openrouter(&self, provider: &Provider) -> bool {
|
||
if let Ok(base_url) = self.extract_base_url(provider) {
|
||
return base_url.contains("openrouter.ai");
|
||
}
|
||
false
|
||
}
|
||
|
||
/// 获取 API 格式
|
||
///
|
||
/// 从 provider.meta.api_format 读取格式设置:
|
||
/// - "anthropic" (默认): Anthropic Messages API 格式,直接透传
|
||
/// - "openai_chat": OpenAI Chat Completions 格式,需要格式转换
|
||
/// - "openai_responses": OpenAI Responses API 格式,需要格式转换
|
||
fn get_api_format(&self, provider: &Provider) -> &'static str {
|
||
get_claude_api_format(provider)
|
||
}
|
||
|
||
/// 检测是否为仅 Bearer 认证模式
|
||
fn is_bearer_only_mode(&self, provider: &Provider) -> bool {
|
||
// 检查 settings_config 中的 auth_mode
|
||
if let Some(auth_mode) = provider
|
||
.settings_config
|
||
.get("auth_mode")
|
||
.and_then(|v| v.as_str())
|
||
{
|
||
if auth_mode == "bearer_only" {
|
||
return true;
|
||
}
|
||
}
|
||
|
||
// 检查 env 中的 AUTH_MODE
|
||
if let Some(env) = provider.settings_config.get("env") {
|
||
if let Some(auth_mode) = env.get("AUTH_MODE").and_then(|v| v.as_str()) {
|
||
if auth_mode == "bearer_only" {
|
||
return true;
|
||
}
|
||
}
|
||
}
|
||
|
||
false
|
||
}
|
||
|
||
/// 从 Provider 配置中提取 API Key
|
||
fn extract_key(&self, provider: &Provider) -> Option<String> {
|
||
if let Some(env) = provider.settings_config.get("env") {
|
||
// Anthropic 标准 key
|
||
if let Some(key) = env
|
||
.get("ANTHROPIC_AUTH_TOKEN")
|
||
.and_then(|v| v.as_str())
|
||
.filter(|s| !s.is_empty())
|
||
{
|
||
log::debug!("[Claude] 使用 ANTHROPIC_AUTH_TOKEN");
|
||
return Some(key.to_string());
|
||
}
|
||
if let Some(key) = env
|
||
.get("ANTHROPIC_API_KEY")
|
||
.and_then(|v| v.as_str())
|
||
.filter(|s| !s.is_empty())
|
||
{
|
||
log::debug!("[Claude] 使用 ANTHROPIC_API_KEY");
|
||
return Some(key.to_string());
|
||
}
|
||
// OpenRouter key
|
||
if let Some(key) = env
|
||
.get("OPENROUTER_API_KEY")
|
||
.and_then(|v| v.as_str())
|
||
.filter(|s| !s.is_empty())
|
||
{
|
||
log::debug!("[Claude] 使用 OPENROUTER_API_KEY");
|
||
return Some(key.to_string());
|
||
}
|
||
// 备选 OpenAI key (用于 OpenRouter)
|
||
if let Some(key) = env
|
||
.get("OPENAI_API_KEY")
|
||
.and_then(|v| v.as_str())
|
||
.filter(|s| !s.is_empty())
|
||
{
|
||
log::debug!("[Claude] 使用 OPENAI_API_KEY");
|
||
return Some(key.to_string());
|
||
}
|
||
}
|
||
|
||
// 尝试直接获取
|
||
if let Some(key) = provider
|
||
.settings_config
|
||
.get("apiKey")
|
||
.or_else(|| provider.settings_config.get("api_key"))
|
||
.and_then(|v| v.as_str())
|
||
.filter(|s| !s.is_empty())
|
||
{
|
||
log::debug!("[Claude] 使用 apiKey/api_key");
|
||
return Some(key.to_string());
|
||
}
|
||
|
||
log::warn!("[Claude] 未找到有效的 API Key");
|
||
None
|
||
}
|
||
}
|
||
|
||
impl Default for ClaudeAdapter {
|
||
fn default() -> Self {
|
||
Self::new()
|
||
}
|
||
}
|
||
|
||
impl ProviderAdapter for ClaudeAdapter {
|
||
fn name(&self) -> &'static str {
|
||
"Claude"
|
||
}
|
||
|
||
fn extract_base_url(&self, provider: &Provider) -> Result<String, ProxyError> {
|
||
// 1. 从 env 中获取
|
||
if let Some(env) = provider.settings_config.get("env") {
|
||
if let Some(url) = env.get("ANTHROPIC_BASE_URL").and_then(|v| v.as_str()) {
|
||
return Ok(url.trim_end_matches('/').to_string());
|
||
}
|
||
}
|
||
|
||
// 2. 尝试直接获取
|
||
if let Some(url) = provider
|
||
.settings_config
|
||
.get("base_url")
|
||
.and_then(|v| v.as_str())
|
||
{
|
||
return Ok(url.trim_end_matches('/').to_string());
|
||
}
|
||
|
||
if let Some(url) = provider
|
||
.settings_config
|
||
.get("baseURL")
|
||
.and_then(|v| v.as_str())
|
||
{
|
||
return Ok(url.trim_end_matches('/').to_string());
|
||
}
|
||
|
||
if let Some(url) = provider
|
||
.settings_config
|
||
.get("apiEndpoint")
|
||
.and_then(|v| v.as_str())
|
||
{
|
||
return Ok(url.trim_end_matches('/').to_string());
|
||
}
|
||
|
||
Err(ProxyError::ConfigError(
|
||
"Claude Provider 缺少 base_url 配置".to_string(),
|
||
))
|
||
}
|
||
|
||
fn extract_auth(&self, provider: &Provider) -> Option<AuthInfo> {
|
||
let provider_type = self.provider_type(provider);
|
||
|
||
// GitHub Copilot 使用特殊的认证策略
|
||
// 实际的 token 会在代理请求时动态获取
|
||
if provider_type == ProviderType::GitHubCopilot {
|
||
// 返回一个占位符,实际 token 由 CopilotAuthManager 动态提供
|
||
return Some(AuthInfo::new(
|
||
"copilot_placeholder".to_string(),
|
||
AuthStrategy::GitHubCopilot,
|
||
));
|
||
}
|
||
|
||
let strategy = match provider_type {
|
||
ProviderType::OpenRouter => AuthStrategy::Bearer,
|
||
ProviderType::ClaudeAuth => AuthStrategy::ClaudeAuth,
|
||
_ => AuthStrategy::Anthropic,
|
||
};
|
||
|
||
self.extract_key(provider)
|
||
.map(|key| AuthInfo::new(key, strategy))
|
||
}
|
||
|
||
fn build_url(&self, base_url: &str, endpoint: &str) -> String {
|
||
// NOTE:
|
||
// 过去 OpenRouter 只有 OpenAI Chat Completions 兼容接口,需要把 Claude 的 `/v1/messages`
|
||
// 映射到 `/v1/chat/completions`,并做 Anthropic ↔ OpenAI 的格式转换。
|
||
//
|
||
// 现在 OpenRouter 已推出 Claude Code 兼容接口,因此默认直接透传 endpoint。
|
||
// 如需回退旧逻辑,可在 forwarder 中根据 needs_transform 改写 endpoint。
|
||
//
|
||
let mut base = format!(
|
||
"{}/{}",
|
||
base_url.trim_end_matches('/'),
|
||
endpoint.trim_start_matches('/')
|
||
);
|
||
|
||
// 去除重复的 /v1/v1(可能由 base_url 与 endpoint 都带版本导致)
|
||
while base.contains("/v1/v1") {
|
||
base = base.replace("/v1/v1", "/v1");
|
||
}
|
||
|
||
base
|
||
}
|
||
|
||
fn get_auth_headers(&self, auth: &AuthInfo) -> Vec<(http::HeaderName, http::HeaderValue)> {
|
||
use http::{HeaderName, HeaderValue};
|
||
// 注意:anthropic-version 由 forwarder.rs 统一处理(透传客户端值或设置默认值)
|
||
let bearer = format!("Bearer {}", auth.api_key);
|
||
match auth.strategy {
|
||
AuthStrategy::Anthropic | AuthStrategy::ClaudeAuth | AuthStrategy::Bearer => {
|
||
vec![(
|
||
HeaderName::from_static("authorization"),
|
||
HeaderValue::from_str(&bearer).unwrap(),
|
||
)]
|
||
}
|
||
AuthStrategy::GitHubCopilot => {
|
||
vec![
|
||
(
|
||
HeaderName::from_static("authorization"),
|
||
HeaderValue::from_str(&bearer).unwrap(),
|
||
),
|
||
(
|
||
HeaderName::from_static("editor-version"),
|
||
HeaderValue::from_static(super::copilot_auth::COPILOT_EDITOR_VERSION),
|
||
),
|
||
(
|
||
HeaderName::from_static("editor-plugin-version"),
|
||
HeaderValue::from_static(super::copilot_auth::COPILOT_PLUGIN_VERSION),
|
||
),
|
||
(
|
||
HeaderName::from_static("copilot-integration-id"),
|
||
HeaderValue::from_static(super::copilot_auth::COPILOT_INTEGRATION_ID),
|
||
),
|
||
(
|
||
HeaderName::from_static("user-agent"),
|
||
HeaderValue::from_static(super::copilot_auth::COPILOT_USER_AGENT),
|
||
),
|
||
(
|
||
HeaderName::from_static("x-github-api-version"),
|
||
HeaderValue::from_static(super::copilot_auth::COPILOT_API_VERSION),
|
||
),
|
||
(
|
||
HeaderName::from_static("openai-intent"),
|
||
HeaderValue::from_static("conversation-panel"),
|
||
),
|
||
]
|
||
}
|
||
_ => vec![],
|
||
}
|
||
}
|
||
|
||
fn needs_transform(&self, provider: &Provider) -> bool {
|
||
// GitHub Copilot 总是需要格式转换 (Anthropic → OpenAI)
|
||
if self.is_github_copilot(provider) {
|
||
return true;
|
||
}
|
||
|
||
// 根据 api_format 配置决定是否需要格式转换
|
||
// - "anthropic" (默认): 直接透传,无需转换
|
||
// - "openai_chat": 需要 Anthropic ↔ OpenAI Chat Completions 格式转换
|
||
// - "openai_responses": 需要 Anthropic ↔ OpenAI Responses API 格式转换
|
||
matches!(
|
||
self.get_api_format(provider),
|
||
"openai_chat" | "openai_responses"
|
||
)
|
||
}
|
||
|
||
fn transform_request(
|
||
&self,
|
||
body: serde_json::Value,
|
||
provider: &Provider,
|
||
) -> Result<serde_json::Value, ProxyError> {
|
||
transform_claude_request_for_api_format(body, provider, self.get_api_format(provider))
|
||
}
|
||
|
||
fn transform_response(&self, body: serde_json::Value) -> Result<serde_json::Value, ProxyError> {
|
||
// Heuristic: detect response format by presence of top-level fields.
|
||
// The ProviderAdapter trait's transform_response doesn't receive the Provider
|
||
// config, so we can't check api_format here. Instead we rely on the fact that
|
||
// Responses API always returns "output" while Chat Completions returns "choices".
|
||
// This is safe because the two formats are structurally disjoint.
|
||
if body.get("output").is_some() {
|
||
super::transform_responses::responses_to_anthropic(body)
|
||
} else {
|
||
super::transform::openai_to_anthropic(body)
|
||
}
|
||
}
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
use crate::provider::ProviderMeta;
|
||
use serde_json::json;
|
||
|
||
fn create_provider(config: serde_json::Value) -> Provider {
|
||
Provider {
|
||
id: "test".to_string(),
|
||
name: "Test Claude".to_string(),
|
||
settings_config: config,
|
||
website_url: None,
|
||
category: Some("claude".to_string()),
|
||
created_at: None,
|
||
sort_index: None,
|
||
notes: None,
|
||
meta: None,
|
||
icon: None,
|
||
icon_color: None,
|
||
in_failover_queue: false,
|
||
}
|
||
}
|
||
|
||
fn create_provider_with_meta(config: serde_json::Value, meta: ProviderMeta) -> Provider {
|
||
Provider {
|
||
id: "test".to_string(),
|
||
name: "Test Claude".to_string(),
|
||
settings_config: config,
|
||
website_url: None,
|
||
category: Some("claude".to_string()),
|
||
created_at: None,
|
||
sort_index: None,
|
||
notes: None,
|
||
meta: Some(meta),
|
||
icon: None,
|
||
icon_color: None,
|
||
in_failover_queue: false,
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn test_extract_base_url_from_env() {
|
||
let adapter = ClaudeAdapter::new();
|
||
let provider = create_provider(json!({
|
||
"env": {
|
||
"ANTHROPIC_BASE_URL": "https://api.anthropic.com"
|
||
}
|
||
}));
|
||
|
||
let url = adapter.extract_base_url(&provider).unwrap();
|
||
assert_eq!(url, "https://api.anthropic.com");
|
||
}
|
||
|
||
#[test]
|
||
fn test_extract_auth_anthropic() {
|
||
let adapter = ClaudeAdapter::new();
|
||
let provider = create_provider(json!({
|
||
"env": {
|
||
"ANTHROPIC_BASE_URL": "https://api.anthropic.com",
|
||
"ANTHROPIC_AUTH_TOKEN": "sk-ant-test-key"
|
||
}
|
||
}));
|
||
|
||
let auth = adapter.extract_auth(&provider).unwrap();
|
||
assert_eq!(auth.api_key, "sk-ant-test-key");
|
||
assert_eq!(auth.strategy, AuthStrategy::Anthropic);
|
||
}
|
||
|
||
#[test]
|
||
fn test_extract_auth_anthropic_api_key() {
|
||
let adapter = ClaudeAdapter::new();
|
||
let provider = create_provider(json!({
|
||
"env": {
|
||
"ANTHROPIC_BASE_URL": "https://api.anthropic.com",
|
||
"ANTHROPIC_API_KEY": "sk-ant-test-key"
|
||
}
|
||
}));
|
||
|
||
let auth = adapter.extract_auth(&provider).unwrap();
|
||
assert_eq!(auth.api_key, "sk-ant-test-key");
|
||
assert_eq!(auth.strategy, AuthStrategy::Anthropic);
|
||
}
|
||
|
||
#[test]
|
||
fn test_extract_auth_openrouter() {
|
||
let adapter = ClaudeAdapter::new();
|
||
let provider = create_provider(json!({
|
||
"env": {
|
||
"ANTHROPIC_BASE_URL": "https://openrouter.ai/api",
|
||
"OPENROUTER_API_KEY": "sk-or-test-key"
|
||
}
|
||
}));
|
||
|
||
let auth = adapter.extract_auth(&provider).unwrap();
|
||
assert_eq!(auth.api_key, "sk-or-test-key");
|
||
assert_eq!(auth.strategy, AuthStrategy::Bearer);
|
||
}
|
||
|
||
#[test]
|
||
fn test_extract_auth_claude_auth_mode() {
|
||
let adapter = ClaudeAdapter::new();
|
||
let provider = create_provider(json!({
|
||
"env": {
|
||
"ANTHROPIC_BASE_URL": "https://some-proxy.com",
|
||
"ANTHROPIC_AUTH_TOKEN": "sk-proxy-key"
|
||
},
|
||
"auth_mode": "bearer_only"
|
||
}));
|
||
|
||
let auth = adapter.extract_auth(&provider).unwrap();
|
||
assert_eq!(auth.api_key, "sk-proxy-key");
|
||
assert_eq!(auth.strategy, AuthStrategy::ClaudeAuth);
|
||
}
|
||
|
||
#[test]
|
||
fn test_extract_auth_claude_auth_env_mode() {
|
||
let adapter = ClaudeAdapter::new();
|
||
let provider = create_provider(json!({
|
||
"env": {
|
||
"ANTHROPIC_BASE_URL": "https://some-proxy.com",
|
||
"ANTHROPIC_AUTH_TOKEN": "sk-proxy-key",
|
||
"AUTH_MODE": "bearer_only"
|
||
}
|
||
}));
|
||
|
||
let auth = adapter.extract_auth(&provider).unwrap();
|
||
assert_eq!(auth.api_key, "sk-proxy-key");
|
||
assert_eq!(auth.strategy, AuthStrategy::ClaudeAuth);
|
||
}
|
||
|
||
#[test]
|
||
fn test_provider_type_detection() {
|
||
let adapter = ClaudeAdapter::new();
|
||
|
||
// Anthropic 官方
|
||
let anthropic = create_provider(json!({
|
||
"env": {
|
||
"ANTHROPIC_BASE_URL": "https://api.anthropic.com",
|
||
"ANTHROPIC_AUTH_TOKEN": "sk-ant-test"
|
||
}
|
||
}));
|
||
assert_eq!(adapter.provider_type(&anthropic), ProviderType::Claude);
|
||
|
||
// OpenRouter
|
||
let openrouter = create_provider(json!({
|
||
"env": {
|
||
"ANTHROPIC_BASE_URL": "https://openrouter.ai/api",
|
||
"OPENROUTER_API_KEY": "sk-or-test"
|
||
}
|
||
}));
|
||
assert_eq!(adapter.provider_type(&openrouter), ProviderType::OpenRouter);
|
||
|
||
// ClaudeAuth
|
||
let claude_auth = create_provider(json!({
|
||
"env": {
|
||
"ANTHROPIC_BASE_URL": "https://some-proxy.com",
|
||
"ANTHROPIC_AUTH_TOKEN": "sk-test"
|
||
},
|
||
"auth_mode": "bearer_only"
|
||
}));
|
||
assert_eq!(
|
||
adapter.provider_type(&claude_auth),
|
||
ProviderType::ClaudeAuth
|
||
);
|
||
}
|
||
|
||
#[test]
|
||
fn test_build_url_anthropic() {
|
||
let adapter = ClaudeAdapter::new();
|
||
let url = adapter.build_url("https://api.anthropic.com", "/v1/messages");
|
||
assert_eq!(url, "https://api.anthropic.com/v1/messages");
|
||
}
|
||
|
||
#[test]
|
||
fn test_build_url_openrouter() {
|
||
let adapter = ClaudeAdapter::new();
|
||
let url = adapter.build_url("https://openrouter.ai/api", "/v1/messages");
|
||
assert_eq!(url, "https://openrouter.ai/api/v1/messages");
|
||
}
|
||
|
||
#[test]
|
||
fn test_build_url_no_beta_for_other_endpoints() {
|
||
let adapter = ClaudeAdapter::new();
|
||
let url = adapter.build_url("https://api.anthropic.com", "/v1/complete");
|
||
assert_eq!(url, "https://api.anthropic.com/v1/complete");
|
||
}
|
||
|
||
#[test]
|
||
fn test_build_url_preserve_existing_query() {
|
||
let adapter = ClaudeAdapter::new();
|
||
let url = adapter.build_url("https://api.anthropic.com", "/v1/messages?foo=bar");
|
||
assert_eq!(url, "https://api.anthropic.com/v1/messages?foo=bar");
|
||
}
|
||
|
||
#[test]
|
||
fn test_build_url_no_beta_for_github_copilot() {
|
||
let adapter = ClaudeAdapter::new();
|
||
let url = adapter.build_url("https://api.githubcopilot.com", "/v1/messages");
|
||
assert_eq!(url, "https://api.githubcopilot.com/v1/messages");
|
||
}
|
||
|
||
#[test]
|
||
fn test_build_url_no_beta_for_openai_chat_completions() {
|
||
let adapter = ClaudeAdapter::new();
|
||
let url = adapter.build_url("https://integrate.api.nvidia.com", "/v1/chat/completions");
|
||
assert_eq!(url, "https://integrate.api.nvidia.com/v1/chat/completions");
|
||
}
|
||
|
||
#[test]
|
||
fn test_needs_transform() {
|
||
let adapter = ClaudeAdapter::new();
|
||
|
||
// Default: no transform (anthropic format) - no meta
|
||
let anthropic_provider = create_provider(json!({
|
||
"env": {
|
||
"ANTHROPIC_BASE_URL": "https://api.anthropic.com"
|
||
}
|
||
}));
|
||
assert!(!adapter.needs_transform(&anthropic_provider));
|
||
|
||
// Explicit anthropic format in meta: no transform
|
||
let explicit_anthropic = create_provider_with_meta(
|
||
json!({
|
||
"env": {
|
||
"ANTHROPIC_BASE_URL": "https://api.example.com"
|
||
}
|
||
}),
|
||
ProviderMeta {
|
||
api_format: Some("anthropic".to_string()),
|
||
..Default::default()
|
||
},
|
||
);
|
||
assert!(!adapter.needs_transform(&explicit_anthropic));
|
||
|
||
// Legacy settings_config.api_format: openai_chat should enable transform
|
||
let legacy_settings_api_format = create_provider(json!({
|
||
"env": {
|
||
"ANTHROPIC_BASE_URL": "https://api.example.com"
|
||
},
|
||
"api_format": "openai_chat"
|
||
}));
|
||
assert!(adapter.needs_transform(&legacy_settings_api_format));
|
||
|
||
// Legacy openrouter_compat_mode: bool/number/string should enable transform
|
||
let legacy_openrouter_bool = create_provider(json!({
|
||
"env": {
|
||
"ANTHROPIC_BASE_URL": "https://api.example.com"
|
||
},
|
||
"openrouter_compat_mode": true
|
||
}));
|
||
assert!(adapter.needs_transform(&legacy_openrouter_bool));
|
||
|
||
let legacy_openrouter_num = create_provider(json!({
|
||
"env": {
|
||
"ANTHROPIC_BASE_URL": "https://api.example.com"
|
||
},
|
||
"openrouter_compat_mode": 1
|
||
}));
|
||
assert!(adapter.needs_transform(&legacy_openrouter_num));
|
||
|
||
let legacy_openrouter_str = create_provider(json!({
|
||
"env": {
|
||
"ANTHROPIC_BASE_URL": "https://api.example.com"
|
||
},
|
||
"openrouter_compat_mode": "true"
|
||
}));
|
||
assert!(adapter.needs_transform(&legacy_openrouter_str));
|
||
|
||
// OpenAI Chat format in meta: needs transform
|
||
let openai_chat_provider = create_provider_with_meta(
|
||
json!({
|
||
"env": {
|
||
"ANTHROPIC_BASE_URL": "https://api.example.com"
|
||
}
|
||
}),
|
||
ProviderMeta {
|
||
api_format: Some("openai_chat".to_string()),
|
||
..Default::default()
|
||
},
|
||
);
|
||
assert!(adapter.needs_transform(&openai_chat_provider));
|
||
|
||
// OpenAI Responses format in meta: needs transform
|
||
let openai_responses_provider = create_provider_with_meta(
|
||
json!({
|
||
"env": {
|
||
"ANTHROPIC_BASE_URL": "https://api.example.com"
|
||
}
|
||
}),
|
||
ProviderMeta {
|
||
api_format: Some("openai_responses".to_string()),
|
||
..Default::default()
|
||
},
|
||
);
|
||
assert!(adapter.needs_transform(&openai_responses_provider));
|
||
|
||
// meta takes precedence over legacy settings_config fields
|
||
let meta_precedence_over_settings = create_provider_with_meta(
|
||
json!({
|
||
"env": {
|
||
"ANTHROPIC_BASE_URL": "https://api.example.com"
|
||
},
|
||
"api_format": "openai_chat",
|
||
"openrouter_compat_mode": true
|
||
}),
|
||
ProviderMeta {
|
||
api_format: Some("anthropic".to_string()),
|
||
..Default::default()
|
||
},
|
||
);
|
||
assert!(!adapter.needs_transform(&meta_precedence_over_settings));
|
||
|
||
// Unknown format in meta: default to anthropic (no transform)
|
||
let unknown_format = create_provider_with_meta(
|
||
json!({
|
||
"env": {
|
||
"ANTHROPIC_BASE_URL": "https://api.example.com"
|
||
}
|
||
}),
|
||
ProviderMeta {
|
||
api_format: Some("unknown".to_string()),
|
||
..Default::default()
|
||
},
|
||
);
|
||
assert!(!adapter.needs_transform(&unknown_format));
|
||
}
|
||
|
||
#[test]
|
||
fn test_github_copilot_detection_by_url() {
|
||
let adapter = ClaudeAdapter::new();
|
||
|
||
// GitHub Copilot by base_url
|
||
let copilot = create_provider(json!({
|
||
"env": {
|
||
"ANTHROPIC_BASE_URL": "https://api.githubcopilot.com"
|
||
}
|
||
}));
|
||
assert_eq!(adapter.provider_type(&copilot), ProviderType::GitHubCopilot);
|
||
}
|
||
|
||
#[test]
|
||
fn test_github_copilot_detection_by_meta() {
|
||
let adapter = ClaudeAdapter::new();
|
||
|
||
// GitHub Copilot by meta.provider_type
|
||
let copilot_meta = create_provider_with_meta(
|
||
json!({
|
||
"env": {
|
||
"ANTHROPIC_BASE_URL": "https://api.example.com"
|
||
}
|
||
}),
|
||
ProviderMeta {
|
||
provider_type: Some("github_copilot".to_string()),
|
||
..Default::default()
|
||
},
|
||
);
|
||
assert_eq!(
|
||
adapter.provider_type(&copilot_meta),
|
||
ProviderType::GitHubCopilot
|
||
);
|
||
}
|
||
|
||
#[test]
|
||
fn test_github_copilot_auth() {
|
||
let adapter = ClaudeAdapter::new();
|
||
|
||
let copilot = create_provider(json!({
|
||
"env": {
|
||
"ANTHROPIC_BASE_URL": "https://api.githubcopilot.com"
|
||
}
|
||
}));
|
||
|
||
let auth = adapter.extract_auth(&copilot).unwrap();
|
||
assert_eq!(auth.strategy, AuthStrategy::GitHubCopilot);
|
||
}
|
||
|
||
#[test]
|
||
fn test_github_copilot_needs_transform() {
|
||
let adapter = ClaudeAdapter::new();
|
||
|
||
let copilot = create_provider(json!({
|
||
"env": {
|
||
"ANTHROPIC_BASE_URL": "https://api.githubcopilot.com"
|
||
}
|
||
}));
|
||
|
||
// GitHub Copilot always needs transform
|
||
assert!(adapter.needs_transform(&copilot));
|
||
}
|
||
|
||
#[test]
|
||
fn test_transform_claude_request_for_api_format_responses() {
|
||
let provider = create_provider(json!({
|
||
"env": {
|
||
"ANTHROPIC_BASE_URL": "https://api.githubcopilot.com"
|
||
}
|
||
}));
|
||
let body = json!({
|
||
"model": "gpt-5.4",
|
||
"messages": [{ "role": "user", "content": "hello" }],
|
||
"max_tokens": 128
|
||
});
|
||
|
||
let transformed =
|
||
transform_claude_request_for_api_format(body, &provider, "openai_responses").unwrap();
|
||
|
||
assert_eq!(transformed["model"], "gpt-5.4");
|
||
assert!(transformed.get("input").is_some());
|
||
assert!(transformed.get("max_output_tokens").is_some());
|
||
}
|
||
}
|