mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-06-16 13:34:04 +08:00
5376ea042b
* i18n: update cache terminology across all languages
- Change 'Cache Read' to 'Cache Hit' in all languages
- Change 'Cache Write' to 'Cache Creation' in all languages
- Update zh: 缓存读取 → 缓存命中, 缓存写入 → 缓存创建
- Update en: Cache Read → Cache Hit, Cache Write → Cache Creation
- Update ja: キャッシュ読取 → キャッシュヒット, キャッシュ書込 → キャッシュ作成
Affected keys: cacheReadTokens, cacheCreationTokens, cacheReadCost,
cacheWriteCost, cacheRead, cacheWrite
* feat(usage): add cache metrics to trend chart
- Add cache creation tokens visualization (orange line)
- Add cache hit tokens visualization (purple line)
- Add gradient definitions for new cache metrics
- Include cache data in hourly aggregation
- Display cache metrics alongside input/output tokens
This provides better visibility into cache usage patterns over time.
* fix(usage): fix timezone handling in datetime picker
- Add timestampToLocalDatetime() to convert Unix timestamp to local datetime
- Add localDatetimeToTimestamp() with validation for incomplete input
- Fix issue where typing hours/minutes would jump to previous day
- Validate datetime format completeness before conversion
- Use local timezone instead of UTC for datetime-local input
This resolves the issue where users couldn't fine-tune time selection
and the input would jump unexpectedly when editing hours or minutes.
* feat(usage): add auto-refresh for usage statistics
- Add 30-second auto-refresh interval for all usage queries
- Disable background refresh to save resources
- Apply to: summary, trends, provider stats, model stats, request logs
- Queries automatically update when tab is active
- Pause refresh when user switches to another tab
This keeps usage data fresh without manual refresh.
* fix(proxy): improve usage logging and cache token parsing
- Log requests even when usage parsing fails (with default values)
- Add detailed debug logging for usage metrics
- Support cache_read_input_tokens field in Codex responses
- Fallback to input_tokens_details.cached_tokens if needed
- Add test case for cached_tokens in input_tokens_details
- Ensure all requests are tracked in database for analytics
This fixes missing request logs when API responses lack usage data
and improves cache token detection across different response formats.
* style(rust): use inline format args in format! macros
- Replace format!("...", var) with format!("...{var}")
- Update universal provider ID formatting
- Update error message formatting
- Update config.toml generation in Codex provider
Fixes clippy::uninlined_format_args warnings.
* feat(proxy): enhance provider router logging
- Add debug logs for failover queue provider count
- Log circuit breaker state for each provider check
- Add logs for missing current provider scenarios
- Log when no current provider is configured
- Use inline format args for better readability
This improves debugging of provider selection and failover behavior.
* feat(database): update model pricing data
- Update Claude models to full version format (e.g. claude-opus-4-5-20251101)
- Add GPT-5.2 series model pricing (10 models)
- Add GPT-5.1 series model pricing (10 models)
- Add GPT-5 series model pricing (12 models)
- Add Gemini 3 series model pricing (2 models)
- Update Gemini 2.5 series model ID format (use dot separator)
- Unify display names by removing thinking level suffixes
* fix(usage): correct Gemini output token calculation
Fix Gemini API output token parsing to use totalTokenCount - promptTokenCount
instead of candidatesTokenCount alone. This ensures thoughtsTokenCount is
included in output statistics.
- Update from_gemini_response to calculate output from total - input
- Update from_gemini_stream_chunks with same logic for consistency
- Fix from_codex_stream_events to use adjusted token calculation
- Add test case for responses with thoughtsTokenCount
- Update existing tests to match new calculation logic
* fix(usage): correct cache token billing and add Codex format auto-detection
- Avoid double-billing cache tokens by subtracting from input before calculation
- Add smart Codex parser that auto-detects OpenAI vs Codex API format
- Extract model name from Codex responses for accurate tracking
* fix(proxy): improve takeover detection with live config check
- Add live config takeover detection for hot-switch decision
- Rebuild takeover when backup is missing or placeholder remains
- Make detect_takeover_in_live_config_for_app public
- Fix is_takeover_active to use actual takeover status
* refactor(usage): simplify model pricing lookup by removing suffix fallback
Replace complex suffix-stripping fallback with direct prefix/suffix cleanup.
Model IDs are now cleaned by removing vendor prefix (before /) and colon
suffix (after :), then matched exactly against pricing table.
* feat(database): add Chinese AI model pricing data
Add pricing for domestic AI models (CNY/1M tokens):
- Doubao-Seed-Code (ByteDance)
- DeepSeek V3/V3.1/V3.2
- Kimi K2/K2-Thinking/K2-Turbo (Moonshot)
- MiniMax M2/M2.1/M2.1-Lightning
- GLM-4.6/4.7 (Zhipu)
- Mimo V2 Flash (Xiaomi)
Also fix test case to use correct model ID and remove invalid currency column.
* refactor(proxy): improve header forwarding with blacklist approach
Change from whitelist to blacklist mode for request header forwarding.
Only skip headers that will be overridden (auth, host, content-length).
This preserves client's original headers and improves compatibility.
* fix(proxy): bypass timeout and retry configs when failover is disabled
When auto_failover_enabled is false, timeout and retry configurations
should not affect normal request flow. This change ensures:
- create_forwarder: passes 0 for all timeout/retry params when failover
is disabled, effectively bypassing these checks
- streaming_timeout_config: returns 0 for both first_byte_timeout and
idle_timeout when failover is disabled
This prevents unnecessary timeout errors and retry attempts when users
have explicitly disabled the failover feature.
* fix(proxy): handle zero value input in failover config fields
* refactor(proxy): remove retry logic and add enabled check for failover
* refactor(proxy): distinguish circuit-open from no-provider errors
* Align usage stats to sliding windows
* feat(proxy): add body and header filtering for upstream requests
* feat(proxy): enable transparent passthrough for headers
- Passthrough anthropic-beta header as-is from client
- Passthrough anthropic-version header from client
- Passthrough client IP headers (x-forwarded-for, x-real-ip) by default
- Filter private params (underscore-prefixed fields) from request body
- No database changes required
* feat(proxy): extract session ID from client requests for logging
- Add SessionIdExtractor to parse session ID from Claude/Codex requests
- Support extraction from metadata.user_id, headers, previous_response_id
- Pass session_id through RequestContext to usage logger
- Enable request correlation by session in proxy_request_logs
119 lines
4.0 KiB
Rust
119 lines
4.0 KiB
Rust
//! 错误类型到 HTTP 状态码的映射
|
||
//!
|
||
//! 将 ProxyError 映射到合适的 HTTP 状态码,用于日志记录
|
||
|
||
use super::ProxyError;
|
||
|
||
/// 将 ProxyError 映射到 HTTP 状态码
|
||
///
|
||
/// 映射规则:
|
||
/// - 上游错误:直接使用上游返回的状态码
|
||
/// - 超时:504 Gateway Timeout
|
||
/// - 连接失败:502 Bad Gateway
|
||
/// - 无可用 Provider:503 Service Unavailable
|
||
/// - 重试耗尽:503 Service Unavailable
|
||
/// - 其他错误:500 Internal Server Error
|
||
pub fn map_proxy_error_to_status(error: &ProxyError) -> u16 {
|
||
match error {
|
||
// 上游错误:使用实际状态码
|
||
ProxyError::UpstreamError { status, .. } => *status,
|
||
|
||
// 超时错误:504 Gateway Timeout
|
||
ProxyError::Timeout(_) => 504,
|
||
|
||
// 转发失败/连接失败:502 Bad Gateway
|
||
ProxyError::ForwardFailed(_) => 502,
|
||
|
||
// 无可用 Provider:503 Service Unavailable
|
||
ProxyError::NoAvailableProvider => 503,
|
||
|
||
// 所有供应商已熔断:503 Service Unavailable
|
||
ProxyError::AllProvidersCircuitOpen => 503,
|
||
|
||
// 未配置供应商:503 Service Unavailable
|
||
ProxyError::NoProvidersConfigured => 503,
|
||
|
||
// 重试耗尽:503 Service Unavailable
|
||
ProxyError::MaxRetriesExceeded => 503,
|
||
|
||
// Provider 不健康:503 Service Unavailable
|
||
ProxyError::ProviderUnhealthy(_) => 503,
|
||
|
||
// 数据库错误:500 Internal Server Error
|
||
ProxyError::DatabaseError(_) => 500,
|
||
|
||
// 转换错误:500 Internal Server Error
|
||
ProxyError::TransformError(_) => 500,
|
||
|
||
// 其他未知错误:500 Internal Server Error
|
||
_ => 500,
|
||
}
|
||
}
|
||
|
||
/// 将 ProxyError 转换为用户友好的错误消息
|
||
pub fn get_error_message(error: &ProxyError) -> String {
|
||
match error {
|
||
ProxyError::UpstreamError { status, body } => {
|
||
if let Some(body) = body {
|
||
format!("上游错误 ({status}): {body}")
|
||
} else {
|
||
format!("上游错误 ({status})")
|
||
}
|
||
}
|
||
ProxyError::Timeout(msg) => format!("请求超时: {msg}"),
|
||
ProxyError::ForwardFailed(msg) => format!("转发失败: {msg}"),
|
||
ProxyError::NoAvailableProvider => "无可用 Provider".to_string(),
|
||
ProxyError::AllProvidersCircuitOpen => "所有供应商已熔断,无可用渠道".to_string(),
|
||
ProxyError::NoProvidersConfigured => "未配置供应商".to_string(),
|
||
ProxyError::MaxRetriesExceeded => "所有 Provider 都失败,重试耗尽".to_string(),
|
||
ProxyError::ProviderUnhealthy(msg) => format!("Provider 不健康: {msg}"),
|
||
ProxyError::DatabaseError(msg) => format!("数据库错误: {msg}"),
|
||
ProxyError::TransformError(msg) => format!("请求/响应转换错误: {msg}"),
|
||
_ => error.to_string(),
|
||
}
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
|
||
#[test]
|
||
fn test_map_upstream_error() {
|
||
let error = ProxyError::UpstreamError {
|
||
status: 401,
|
||
body: Some("Unauthorized".to_string()),
|
||
};
|
||
assert_eq!(map_proxy_error_to_status(&error), 401);
|
||
}
|
||
|
||
#[test]
|
||
fn test_map_timeout_error() {
|
||
let error = ProxyError::Timeout("Request timeout".to_string());
|
||
assert_eq!(map_proxy_error_to_status(&error), 504);
|
||
}
|
||
|
||
#[test]
|
||
fn test_map_connection_error() {
|
||
let error = ProxyError::ForwardFailed("Connection refused".to_string());
|
||
assert_eq!(map_proxy_error_to_status(&error), 502);
|
||
}
|
||
|
||
#[test]
|
||
fn test_map_no_provider_error() {
|
||
let error = ProxyError::NoAvailableProvider;
|
||
assert_eq!(map_proxy_error_to_status(&error), 503);
|
||
}
|
||
|
||
#[test]
|
||
fn test_get_error_message() {
|
||
let error = ProxyError::UpstreamError {
|
||
status: 500,
|
||
body: Some("Internal Server Error".to_string()),
|
||
};
|
||
let msg = get_error_message(&error);
|
||
assert!(msg.contains("上游错误"));
|
||
assert!(msg.contains("500"));
|
||
assert!(msg.contains("Internal Server Error"));
|
||
}
|
||
}
|