Compare commits

..

10 Commits

Author SHA1 Message Date
YoVinchen b32960cf5d fix(skill): separate source path validation and add canonicalization for symlink safety 2026-02-15 16:50:21 +08:00
YoVinchen dc32f8f7a7 fix(skill): resolve symlink and path traversal issues in ZIP skill import 2026-02-15 13:02:17 +08:00
YoVinchen 9e2d7fe4ac Merge branch 'main' into fix/skill-zip-symlink-resolution 2026-02-15 12:48:41 +08:00
YoVinchen f2934daa14 merge: integrate main with OpenClaw support and resolve skill conflicts 2026-02-14 17:21:08 +08:00
YoVinchen 1ffbb32484 refactor(skill): unify scan/import logic with lock file skillPath and repo saving
- Deduplicate scan_unmanaged and import_from_apps using shared source list
- Replace hand-written AppType match with as_str() and AppType::all()
- Extract read_skill_name_desc, build_repo_info_from_lock, save_repos_from_lock helpers
- Add SkillApps::from_labels for building enable state from source labels
- Parse skillPath from .skill-lock.json for correct readme URLs
- Save skill repos to skill_repos table in both import and migration paths
2026-02-14 17:08:36 +08:00
YoVinchen 74e84be1dd feat(skill): scan ~/.cc-switch/skills/ for unmanaged skill discovery and import 2026-02-14 15:55:16 +08:00
YoVinchen c3ef4b4660 fix(skill): use metadata name or ZIP filename for root-level SKILL.md imports (#1000)
When a ZIP contains SKILL.md at the root without a wrapper directory,
the install name was derived from the temp directory name (e.g. .tmpDZKGpF).
Now falls back to SKILL.md frontmatter name, then ZIP filename stem.
2026-02-14 14:44:27 +08:00
YoVinchen 74dcc0bb3e feat(skill): support importing skills from ~/.agents/skills/ directory
- Scan ~/.agents/skills/ in scan_unmanaged() for skill discovery
- Parse ~/.agents/.skill-lock.json to extract repo owner/name metadata
- Auto-add discovered repos to skill_repos management on import
- Add path field to UnmanagedSkill to show discovered location in UI

Closes #980
2026-02-14 14:17:53 +08:00
YoVinchen 688b310021 fix(skill): change search to match name and repo instead of description 2026-02-14 11:14:04 +08:00
YoVinchen 6d5cb9407d fix(skill): resolve symlinks in ZIP extraction for GitHub repos (#1001)
- detect symlink entries via is_symlink() during ZIP extraction and collect target paths

- add resolve_symlinks_in_dir() to copy symlink target content into link location

- canonicalize base_dir to fix macOS /tmp → /private/tmp path comparison issue

- add path traversal safety check to block symlinks pointing outside repo boundary

- apply symlink resolution to both download_and_extract and extract_local_zip paths

Closes https://github.com/farion1231/cc-switch/issues/1001
2026-02-14 10:54:31 +08:00
48 changed files with 298 additions and 2218 deletions
+2 -7
View File
@@ -61,13 +61,8 @@ Claude Code / Codex / Gemini official channels at 38% / 2% / 9% of original pric
</tr>
<tr>
<td width="180"><a href="https://crazyrouter.com/register?aff=OZcm&ref=cc-switch"><img src="assets/partners/logos/crazyrouter.jpg" alt="AICoding" width="150"></a></td>
<td>Thanks to Crazyrouter for sponsoring this project! Crazyrouter is a high-performance AI API aggregation platform — one API key for 300+ models including Claude Code, Codex, Gemini CLI, and more. All models at 55% of official pricing with auto-failover, smart routing, and unlimited concurrency. Crazyrouter offers an exclusive deal for CC Switch users: register via <a href="https://crazyrouter.com/register?aff=OZcm&ref=cc-switch">this link</a> to get <strong>$2 free credit</strong> instantly, plus enter promo code `CCSWITCH` on your first top-up for an extra <strong>30% bonus credit</strong>! </td>
</tr>
<tr>
<td width="180"><a href="https://www.sssaicode.com/register?ref=DCP0SM"><img src="assets/partners/logos/sssaicode.png" alt="SSSAiCode" width="150"></a></td>
<td>Thanks to SSSAiCode for sponsoring this project! SSSAiCode is a stable and reliable API relay service, dedicated to providing stable, reliable, and affordable Claude and Codex model services, <strong>offering high cost-effective official Claude service at just ¥0.5/$ equivalent</strong>, supporting monthly and pay-as-you-go billing plans with same-day fast invoicing. SSSAiCode offers a special deal for CC Switch users: register via <a href="https://www.sssaicode.com/register?ref=DCP0SM">this link</a> to enjoy $10 extra credit on every top-up!</td>
<td width="180"><a href="https://crazyrouter.com/register?ref=cc-switch"><img src="assets/partners/logos/crazyrouter.jpg" alt="AICoding" width="150"></a></td>
<td>Thanks to Crazyrouter for sponsoring this project! Crazyrouter is a high-performance AI API aggregation platform — one API key for 300+ models including Claude Code, Codex, Gemini CLI, GPT, and more. All through a single OpenAI-compatible endpoint with zero code changes. Features include auto-failover, smart routing, unlimited concurrency, and global low-latency access. <a href="https://crazyrouter.com/register?ref=cc-switch">Register here</a> to get started.</td>
</tr>
</table>
+2 -7
View File
@@ -61,13 +61,8 @@ Claude Code / Codex / Gemini 公式チャンネルが最安で元価格の 38% /
</tr>
<tr>
<td width="180"><a href="https://crazyrouter.com/register?aff=OZcm&ref=cc-switch"><img src="assets/partners/logos/crazyrouter.jpg" alt="AICoding" width="150"></a></td>
<td>Crazyrouter のご支援に感謝します!Crazyrouter は高性能 AI API アグリゲーションプラットフォームです。1 つの API キーで Claude Code、Codex、Gemini CLI など 300 以上のモデルにアクセス可能。全モデルが公式価格の 55% で利用でき、自動フェイルオーバー、スマートルーティング、無制限同時接続に対応。CC Switch ユーザー向けの限定特典:<a href="https://crazyrouter.com/register?aff=OZcm&ref=cc-switch">こちらのリンク</a>から登録すると <strong>$2 の無料クレジット</strong> を即時進呈。さらに初回チャージ時にプロモコード `CCSWITCH` を入力すると <strong>30% のボーナスクレジット</strong> が追加されます</td>
</tr>
<tr>
<td width="180"><a href="https://www.sssaicode.com/register?ref=DCP0SM"><img src="assets/partners/logos/sssaicode.png" alt="SSSAiCode" width="150"></a></td>
<td>SSSAiCode のご支援に感謝します!SSSAiCode は安定性と信頼性に優れた API 中継サービスで、安定的で信頼性が高く、手頃な価格の Claude・Codex モデルサービスを提供しています。<strong>高コストパフォーマンスの公式 Claude サービスを 0.5¥/$ 換算で提供</strong>、月額制・Paygo など多様な課金方式に対応し、当日の迅速な請求書発行をサポート。CC Switch ユーザー向けの特別特典:<a href="https://www.sssaicode.com/register?ref=DCP0SM">こちらのリンク</a>から登録すると、毎回のチャージで $10 の追加ボーナスを受けられます!</td>
<td width="180"><a href="https://crazyrouter.com/register?ref=cc-switch"><img src="assets/partners/logos/crazyrouter.jpg" alt="AICoding" width="150"></a></td>
<td>Crazyrouter のご支援に感謝します!Crazyrouter は高性能 AI API アグリゲーションプラットフォームです。1 つの API キーで Claude Code、Codex、Gemini CLI、GPT など 300 以上のモデルにアクセス可能。OpenAI 互換エンドポイントでコード変更不要。自動フェイルオーバー、スマートルーティング、無制限同時接続、グローバル低遅延アクセスに対応。<a href="https://crazyrouter.com/register?ref=cc-switch">こちらから登録</a>してすぐにご利用いただけます</td>
</tr>
</table>
+2 -7
View File
@@ -62,13 +62,8 @@ Claude Code / Codex / Gemini 官方渠道低至 3.8 / 0.2 / 0.9 折,充值更
</tr>
<tr>
<td width="180"><a href="https://crazyrouter.com/register?aff=OZcm&ref=cc-switch"><img src="assets/partners/logos/crazyrouter.jpg" alt="AICoding" width="150"></a></td>
<td>感谢 Crazyrouter 赞助了本项目!Crazyrouter 是一个高性能 AI API 聚合平台——一个 API Key 即可访问 300+ 模型,包括 Claude Code、Codex、Gemini CLI 等。全部模型低至官方定价的 55%支持自动故障转移、智能路由无限并发。Crazyrouter 为 CC Switch 用户提供了专属优惠:通过<a href="https://crazyrouter.com/register?aff=OZcm&ref=cc-switch">此链接</a>注册即可获得 <strong>$2 免费额度</strong>,首次充值时输入优惠码 `CCSWITCH` 还可获得额外 <strong>30% 奖励额度</strong></td>
</tr>
<tr>
<td width="180"><a href="https://www.sssaicode.com/register?ref=DCP0SM"><img src="assets/partners/logos/sssaicode.png" alt="SSSAiCode" width="150"></a></td>
<td>感谢 SSSAiCode 赞助了本项目!SSSAiCode 是一家稳定可靠的API中转站,致力于提供稳定、可靠、平价的Claude、CodeX模型服务,<strong>提供高性价比折合0.5¥/$的官方Claude服务</strong>,支持包月、Paygo多种计费方式、支持当日快速开票,SSSAiCode为本软件的用户提供特别优惠,使用<a href="https://www.sssaicode.com/register?ref=DCP0SM">此链接</a>注册每次充值均可享受10$的额外奖励!</td>
<td width="180"><a href="https://crazyrouter.com/register?ref=cc-switch"><img src="assets/partners/logos/crazyrouter.jpg" alt="AICoding" width="150"></a></td>
<td>感谢 Crazyrouter 赞助了本项目!Crazyrouter 是一个高性能 AI API 聚合平台——一个 API Key 即可访问 300+ 模型,包括 Claude Code、Codex、Gemini CLI、GPT 等。通过单一 OpenAI 兼容端点实现零代码改动接入。支持自动故障转移、智能路由无限并发和全球低延迟访问。<a href="https://crazyrouter.com/register?ref=cc-switch">点击这里注册</a>即可开始使用</td>
</tr>
</table>
Binary file not shown.

Before

Width:  |  Height:  |  Size: 447 KiB

+1 -1
View File
@@ -57,7 +57,7 @@ url = "2.5"
auto-launch = "0.5"
once_cell = "1.21.3"
base64 = "0.22"
rusqlite = { version = "0.31", features = ["bundled", "backup", "hooks"] }
rusqlite = { version = "0.31", features = ["bundled", "backup"] }
indexmap = { version = "2", features = ["serde"] }
rust_decimal = "1.33"
uuid = { version = "1.11", features = ["v4"] }
-40
View File
@@ -407,43 +407,3 @@ pub async fn get_circuit_breaker_stats(
let _ = (state, provider_id, app_type);
Ok(None)
}
// ==================== URL 预览相关命令 ====================
use crate::app_config::AppType;
use crate::proxy::url_builder::{self, UrlPreview};
/// 构建 URL 预览
///
/// 根据 app_type、base_url 和 api_format 计算直连和代理模式的请求地址。
/// 这个命令与后端代理的 URL 构建逻辑保持一致。
#[tauri::command]
pub fn build_url_preview(
app_type: String,
base_url: String,
api_format: Option<String>,
) -> Result<UrlPreview, String> {
let app = app_type.parse::<AppType>().map_err(|e| e.to_string())?;
Ok(url_builder::build_url_preview(
&app,
&base_url,
api_format.as_deref(),
))
}
/// 检查是否需要代理
///
/// 返回需要代理的原因(openai_chat_format, full_url, url_mismatch),
/// 或 null 表示不需要代理。
#[tauri::command]
pub fn check_proxy_requirement(
app_type: String,
base_url: String,
api_format: Option<String>,
) -> Result<Option<String>, String> {
let app = app_type.parse::<AppType>().map_err(|e| e.to_string())?;
Ok(
url_builder::check_proxy_requirement(&app, &base_url, api_format.as_deref())
.map(|s| s.to_string()),
)
}
+15 -15
View File
@@ -1,6 +1,8 @@
#![allow(non_snake_case)]
use serde_json::{json, Value};
use serde_json::{Value, json};
use std::future::Future;
use std::sync::OnceLock;
use tauri::State;
use crate::commands::sync_support::{
@@ -11,9 +13,8 @@ use crate::services::webdav_sync as webdav_sync_service;
use crate::settings::{self, WebDavSyncSettings};
use crate::store::AppState;
fn persist_sync_error(settings: &mut WebDavSyncSettings, error: &AppError, source: &str) {
fn persist_sync_error(settings: &mut WebDavSyncSettings, error: &AppError) {
settings.status.last_error = Some(error.to_string());
settings.status.last_error_source = Some(source.to_string());
let _ = settings::update_webdav_sync_status(settings.status.clone());
}
@@ -56,16 +57,20 @@ fn resolve_password_for_request(
incoming
}
#[cfg(test)]
fn webdav_sync_mutex() -> &'static tokio::sync::Mutex<()> {
webdav_sync_service::sync_mutex()
static LOCK: OnceLock<tokio::sync::Mutex<()>> = OnceLock::new();
LOCK.get_or_init(|| tokio::sync::Mutex::new(()))
}
async fn run_with_webdav_lock<T, Fut>(operation: Fut) -> Result<T, AppError>
where
Fut: std::future::Future<Output = Result<T, AppError>>,
Fut: Future<Output = Result<T, AppError>>,
{
webdav_sync_service::run_with_sync_lock(operation).await
let result = {
let _guard = webdav_sync_mutex().lock().await;
operation.await
};
result
}
fn map_sync_result<T, F>(result: Result<T, AppError>, on_error: F) -> Result<T, String>
@@ -107,9 +112,7 @@ pub async fn webdav_sync_upload(state: State<'_, AppState>) -> Result<Value, Str
let mut settings = require_enabled_webdav_settings()?;
let result = run_with_webdav_lock(webdav_sync_service::upload(&db, &mut settings)).await;
map_sync_result(result, |error| {
persist_sync_error(&mut settings, error, "manual")
})
map_sync_result(result, |error| persist_sync_error(&mut settings, error))
}
#[tauri::command]
@@ -117,11 +120,10 @@ pub async fn webdav_sync_download(state: State<'_, AppState>) -> Result<Value, S
let db = state.db.clone();
let db_for_sync = db.clone();
let mut settings = require_enabled_webdav_settings()?;
let _auto_sync_suppression = crate::services::webdav_auto_sync::AutoSyncSuppressionGuard::new();
let sync_result = run_with_webdav_lock(webdav_sync_service::download(&db, &mut settings)).await;
let mut result = map_sync_result(sync_result, |error| {
persist_sync_error(&mut settings, error, "manual")
persist_sync_error(&mut settings, error)
})?;
// Post-download sync is best-effort: snapshot restore has already succeeded.
@@ -177,8 +179,8 @@ mod tests {
use crate::error::AppError;
use crate::settings::{AppSettings, WebDavSyncSettings};
use serial_test::serial;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;
#[tokio::test]
@@ -285,7 +287,6 @@ mod tests {
persist_sync_error(
&mut current,
&crate::error::AppError::Config("boom".to_string()),
"manual",
);
let after = crate::settings::get_webdav_sync_settings().expect("read webdav settings");
@@ -303,7 +304,6 @@ mod tests {
.contains("boom"),
"status error should be updated"
);
assert_eq!(after.status.last_error_source.as_deref(), Some("manual"));
}
#[test]
+1 -14
View File
@@ -37,7 +37,7 @@ pub use dao::OmoGlobalConfig;
use crate::config::get_app_config_dir;
use crate::error::AppError;
use rusqlite::{hooks::Action, Connection};
use rusqlite::Connection;
use serde::Serialize;
use std::sync::Mutex;
@@ -76,17 +76,6 @@ pub struct Database {
pub(crate) conn: Mutex<Connection>,
}
fn register_db_change_hook(conn: &Connection) {
conn.update_hook(Some(
|action: Action, _database: &str, table: &str, _row_id: i64| match action {
Action::SQLITE_INSERT | Action::SQLITE_UPDATE | Action::SQLITE_DELETE => {
crate::services::webdav_auto_sync::notify_db_changed(table);
}
_ => {}
},
));
}
impl Database {
/// 初始化数据库连接并创建表
///
@@ -104,7 +93,6 @@ impl Database {
// 启用外键约束
conn.execute("PRAGMA foreign_keys = ON;", [])
.map_err(|e| AppError::Database(e.to_string()))?;
register_db_change_hook(&conn);
let db = Self {
conn: Mutex::new(conn),
@@ -123,7 +111,6 @@ impl Database {
// 启用外键约束
conn.execute("PRAGMA foreign_keys = ON;", [])
.map_err(|e| AppError::Database(e.to_string()))?;
register_db_change_hook(&conn);
let db = Self {
conn: Mutex::new(conn),
-7
View File
@@ -702,10 +702,6 @@ pub fn run() {
}
let _tray = tray_builder.build(app)?;
crate::services::webdav_auto_sync::start_worker(
app_state.db.clone(),
app.handle().clone(),
);
// 将同一个实例注入到全局状态,避免重复创建导致的不一致
app.manage(app_state);
@@ -971,9 +967,6 @@ pub fn run() {
commands::get_circuit_breaker_config,
commands::update_circuit_breaker_config,
commands::get_circuit_breaker_stats,
// URL preview (for endpoint field)
commands::build_url_preview,
commands::check_proxy_requirement,
// Failover queue management
commands::get_failover_queue,
commands::get_available_providers_for_failover,
+7 -63
View File
@@ -749,17 +749,15 @@ impl RequestForwarder {
// 检查是否需要格式转换
let needs_transform = adapter.needs_transform(provider);
let effective_endpoint = if needs_transform
&& adapter.name() == "Claude"
&& endpoint.starts_with("/v1/messages")
{
transform_claude_messages_endpoint(endpoint)
} else {
endpoint.to_string()
};
let effective_endpoint =
if needs_transform && adapter.name() == "Claude" && endpoint == "/v1/messages" {
"/v1/chat/completions"
} else {
endpoint
};
// 使用适配器构建 URL
let url = adapter.build_url(&base_url, &effective_endpoint);
let url = adapter.build_url(&base_url, effective_endpoint);
// 应用模型映射(独立于格式转换)
let (mapped_body, _original_model, _mapped_model) =
@@ -929,57 +927,3 @@ fn extract_error_message(error: &ProxyError) -> Option<String> {
_ => Some(error.to_string()),
}
}
fn transform_claude_messages_endpoint(endpoint: &str) -> String {
let transformed = endpoint.replacen("/v1/messages", "/v1/chat/completions", 1);
let Some((path, query)) = transformed.split_once('?') else {
return transformed;
};
// 转换到 chat/completions 时,显式移除 beta=true,避免把旧接口参数带到新接口。
let filtered_params: Vec<&str> = query
.split('&')
.filter(|segment| !segment.is_empty())
.filter(|segment| !is_beta_true_query_pair(segment))
.collect();
if filtered_params.is_empty() {
path.to_string()
} else {
format!("{path}?{}", filtered_params.join("&"))
}
}
fn is_beta_true_query_pair(segment: &str) -> bool {
let Some((key, value)) = segment.split_once('=') else {
return false;
};
key.eq_ignore_ascii_case("beta") && value.eq_ignore_ascii_case("true")
}
#[cfg(test)]
mod tests {
use super::transform_claude_messages_endpoint;
#[test]
fn transform_claude_messages_endpoint_strips_beta_true_only() {
let endpoint = "/v1/messages?beta=true&foo=1";
let transformed = transform_claude_messages_endpoint(endpoint);
assert_eq!(transformed, "/v1/chat/completions?foo=1");
}
#[test]
fn transform_claude_messages_endpoint_drops_empty_query_after_strip() {
let endpoint = "/v1/messages?beta=true";
let transformed = transform_claude_messages_endpoint(endpoint);
assert_eq!(transformed, "/v1/chat/completions");
}
#[test]
fn transform_claude_messages_endpoint_keeps_other_query_values() {
let endpoint = "/v1/messages?beta=false&foo=1";
let transformed = transform_claude_messages_endpoint(endpoint);
assert_eq!(transformed, "/v1/chat/completions?beta=false&foo=1");
}
}
+33
View File
@@ -42,6 +42,22 @@ fn claude_model_extractor(events: &[Value], request_model: &str) -> String {
request_model.to_string()
}
/// OpenAI Chat Completions 流式响应模型提取(优先使用 usage.model
fn openai_model_extractor(events: &[Value], request_model: &str) -> String {
// 首先尝试从解析的 usage 中获取模型
if let Some(usage) = TokenUsage::from_openai_stream_events(events) {
if let Some(model) = usage.model {
return model;
}
}
// 回退:从事件中直接提取
events
.iter()
.find_map(|e| e.get("model")?.as_str())
.unwrap_or(request_model)
.to_string()
}
/// Codex 智能流式响应模型提取(自动检测格式)
fn codex_auto_model_extractor(events: &[Value], request_model: &str) -> String {
// 首先尝试从解析的 usage 中获取模型
@@ -91,6 +107,14 @@ pub const CLAUDE_PARSER_CONFIG: UsageParserConfig = UsageParserConfig {
app_type_str: "claude",
};
/// OpenAI Chat Completions API 解析配置(用于 Codex /v1/chat/completions
pub const OPENAI_PARSER_CONFIG: UsageParserConfig = UsageParserConfig {
stream_parser: TokenUsage::from_openai_stream_events,
response_parser: TokenUsage::from_openai_response,
model_extractor: openai_model_extractor,
app_type_str: "codex",
};
/// Codex 智能解析配置(自动检测 OpenAI 或 Codex 格式)
pub const CODEX_PARSER_CONFIG: UsageParserConfig = UsageParserConfig {
stream_parser: TokenUsage::from_codex_stream_events_auto,
@@ -136,6 +160,15 @@ pub const CLAUDE_HANDLER_CONFIG: HandlerConfig = HandlerConfig {
parser_config: &CLAUDE_PARSER_CONFIG,
};
/// Codex Chat Completions Handler 配置
#[allow(dead_code)]
pub const CODEX_CHAT_HANDLER_CONFIG: HandlerConfig = HandlerConfig {
app_type: AppType::Codex,
tag: "Codex",
app_type_str: "codex",
parser_config: &OPENAI_PARSER_CONFIG,
};
/// Codex Responses Handler 配置
#[allow(dead_code)]
pub const CODEX_RESPONSES_HANDLER_CONFIG: HandlerConfig = HandlerConfig {
+45 -9
View File
@@ -9,7 +9,9 @@
use super::{
error_mapper::{get_error_message, map_proxy_error_to_status},
handler_config::{CLAUDE_PARSER_CONFIG, CODEX_PARSER_CONFIG, GEMINI_PARSER_CONFIG},
handler_config::{
CLAUDE_PARSER_CONFIG, CODEX_PARSER_CONFIG, GEMINI_PARSER_CONFIG, OPENAI_PARSER_CONFIG,
},
handler_context::RequestContext,
providers::{get_adapter, streaming::create_anthropic_sse_stream, transform},
response_processor::{create_logged_passthrough_stream, process_response, SseUsageCollector},
@@ -54,7 +56,6 @@ pub async fn get_status(State(state): State<ProxyState>) -> Result<Json<ProxySta
/// - 现在 OpenRouter 已推出 Claude Code 兼容接口,默认不再启用该转换(逻辑保留以备回退)
pub async fn handle_messages(
State(state): State<ProxyState>,
uri: axum::http::Uri,
headers: axum::http::HeaderMap,
Json(body): Json<Value>,
) -> Result<axum::response::Response, ProxyError> {
@@ -66,18 +67,12 @@ pub async fn handle_messages(
.and_then(|s| s.as_bool())
.unwrap_or(false);
// 透传客户端 query 参数(例如 ?beta=true),路径统一为 /v1/messages
let endpoint = uri
.query()
.map(|q| format!("/v1/messages?{q}"))
.unwrap_or_else(|| "/v1/messages".to_string());
// 转发请求
let forwarder = ctx.create_forwarder(&state);
let result = match forwarder
.forward_with_retry(
&AppType::Claude,
&endpoint,
"/v1/messages",
body.clone(),
headers,
ctx.get_providers(),
@@ -271,6 +266,47 @@ async fn handle_claude_transform(
// Codex API 处理器
// ============================================================================
/// 处理 /v1/chat/completions 请求(OpenAI Chat Completions API - Codex CLI
pub async fn handle_chat_completions(
State(state): State<ProxyState>,
headers: axum::http::HeaderMap,
Json(body): Json<Value>,
) -> Result<axum::response::Response, ProxyError> {
let mut ctx =
RequestContext::new(&state, &body, &headers, AppType::Codex, "Codex", "codex").await?;
let is_stream = body
.get("stream")
.and_then(|v| v.as_bool())
.unwrap_or(false);
let forwarder = ctx.create_forwarder(&state);
let result = match forwarder
.forward_with_retry(
&AppType::Codex,
"/chat/completions",
body,
headers,
ctx.get_providers(),
)
.await
{
Ok(result) => result,
Err(mut err) => {
if let Some(provider) = err.provider.take() {
ctx.provider = provider;
}
log_forward_error(&state, &ctx, is_stream, &err.error);
return Err(err.error);
}
};
ctx.provider = result.provider;
let response = result.response;
process_response(response, &ctx, &state, &OPENAI_PARSER_CONFIG).await
}
/// 处理 /v1/responses 请求(OpenAI Responses API - Codex CLI 透传)
pub async fn handle_responses(
State(state): State<ProxyState>,
-2
View File
@@ -24,8 +24,6 @@ pub mod session;
pub mod thinking_budget_rectifier;
pub mod thinking_rectifier;
pub(crate) mod types;
pub mod url_builder;
pub(crate) mod url_utils;
pub mod usage;
// 公开导出给外部使用(commands, services等模块需要)
+16 -96
View File
@@ -14,7 +14,6 @@
use super::{AuthInfo, AuthStrategy, ProviderAdapter, ProviderType};
use crate::provider::Provider;
use crate::proxy::error::ProxyError;
use crate::proxy::url_utils::{dedup_v1_v1_boundary_safe, split_url_suffix};
use reqwest::RequestBuilder;
/// Claude 适配器
@@ -253,48 +252,26 @@ impl ProviderAdapter for ClaudeAdapter {
// 现在 OpenRouter 已推出 Claude Code 兼容接口,因此默认直接透传 endpoint。
// 如需回退旧逻辑,可在 forwarder 中根据 needs_transform 改写 endpoint。
let (base, suffix) = split_url_suffix(base_url);
let base_trimmed = base.trim_end_matches('/');
let endpoint_trimmed = endpoint.trim_start_matches('/');
// 检测 base_url 是否已经以 API 路径结尾(用户填写了完整路径)
// 支持的 API 路径模式:/v1/messages, /messages, /v1/chat/completions, /chat/completions
let api_path_patterns = [
"/v1/messages",
"/messages",
"/v1/chat/completions",
"/chat/completions",
];
let base_ends_with_api_path = api_path_patterns
.iter()
.any(|pattern| base_trimmed.to_lowercase().ends_with(pattern));
// 如果 base_url 已经以 API 路径结尾,直接使用 base_url,不再追加 endpoint
let base = if base_ends_with_api_path {
base_trimmed.to_string()
} else {
format!("{base_trimmed}/{endpoint_trimmed}")
};
let mut base = format!(
"{}/{}",
base_url.trim_end_matches('/'),
endpoint.trim_start_matches('/')
);
// 去除重复的 /v1/v1(可能由 base_url 与 endpoint 都带版本导致)
let base = dedup_v1_v1_boundary_safe(base);
while base.contains("/v1/v1") {
base = base.replace("/v1/v1", "/v1");
}
let url = format!("{base}{suffix}");
// 为 Claude 原生 /v1/messages 端点添加 ?beta=true 参数
// 为 Claude 相关端点添加 ?beta=true 参数
// 这是某些上游服务(如 DuckCoding)验证请求来源的关键参数
// 注意:不要为 OpenAI Chat Completions (/v1/chat/completions) 添加此参数
// 当 apiFormat="openai_chat" 时,请求会转发到 /v1/chat/completions
// 但该端点是 OpenAI 标准,不支持 ?beta=true 参数
if endpoint.contains("/v1/messages")
&& !endpoint.contains("/v1/chat/completions")
// 注openai_chat 模式下会转发到 /v1/chat/completions,此处也需要保持一致
if (endpoint.contains("/v1/messages") || endpoint.contains("/v1/chat/completions"))
&& !endpoint.contains('?')
&& !url.contains('?')
{
format!("{url}?beta=true")
format!("{base}?beta=true")
} else {
url
base
}
}
@@ -507,6 +484,7 @@ mod tests {
#[test]
fn test_build_url_anthropic() {
let adapter = ClaudeAdapter::new();
// /v1/messages 端点会自动添加 ?beta=true 参数
let url = adapter.build_url("https://api.anthropic.com", "/v1/messages");
assert_eq!(url, "https://api.anthropic.com/v1/messages?beta=true");
}
@@ -514,6 +492,7 @@ mod tests {
#[test]
fn test_build_url_openrouter() {
let adapter = ClaudeAdapter::new();
// /v1/messages 端点会自动添加 ?beta=true 参数
let url = adapter.build_url("https://openrouter.ai/api", "/v1/messages");
assert_eq!(url, "https://openrouter.ai/api/v1/messages?beta=true");
}
@@ -521,7 +500,7 @@ mod tests {
#[test]
fn test_build_url_no_beta_for_other_endpoints() {
let adapter = ClaudeAdapter::new();
// 非 /v1/messages 端点保持原样
// 非 /v1/messages 端点不添加 ?beta=true
let url = adapter.build_url("https://api.anthropic.com", "/v1/complete");
assert_eq!(url, "https://api.anthropic.com/v1/complete");
}
@@ -534,65 +513,6 @@ mod tests {
assert_eq!(url, "https://api.anthropic.com/v1/messages?foo=bar");
}
#[test]
fn test_build_url_full_path_messages() {
let adapter = ClaudeAdapter::new();
// base_url 已包含完整路径 /v1/messages,不再追加
let url = adapter.build_url("https://example.com/api/v1/messages", "/v1/messages");
assert_eq!(url, "https://example.com/api/v1/messages?beta=true");
}
#[test]
fn test_build_url_full_path_chat_completions() {
let adapter = ClaudeAdapter::new();
// base_url 已包含完整路径 /v1/chat/completions,不再追加
let url = adapter.build_url(
"https://opencode.ai/zen/v1/chat/completions",
"/v1/chat/completions",
);
assert_eq!(url, "https://opencode.ai/zen/v1/chat/completions");
}
#[test]
fn test_build_url_full_path_short_suffix() {
let adapter = ClaudeAdapter::new();
// base_url 以 /messages 结尾(无 /v1 前缀)
let url = adapter.build_url("https://example.com/api/messages", "/v1/messages");
assert_eq!(url, "https://example.com/api/messages?beta=true");
// base_url 以 /chat/completions 结尾(无 /v1 前缀)
let url2 = adapter.build_url(
"https://example.com/api/chat/completions",
"/v1/chat/completions",
);
assert_eq!(url2, "https://example.com/api/chat/completions");
}
#[test]
fn test_build_url_v1_base_dedup() {
let adapter = ClaudeAdapter::new();
// base_url 以 /v1 结尾,endpoint 也以 /v1 开头,应该去重
// 场景:https://integrate.api.nvidia.com/v1 + /v1/chat/completions
let url = adapter.build_url(
"https://integrate.api.nvidia.com/v1",
"/v1/chat/completions",
);
assert_eq!(url, "https://integrate.api.nvidia.com/v1/chat/completions");
// 另一个场景:/v1 + /v1/messages
let url2 = adapter.build_url("https://api.example.com/v1", "/v1/messages");
assert_eq!(url2, "https://api.example.com/v1/messages?beta=true");
}
#[test]
fn test_build_url_no_beta_for_openai_chat_completions() {
let adapter = ClaudeAdapter::new();
// OpenAI Chat Completions 端点不添加 ?beta=true
// 这是 Nvidia 等 apiFormat="openai_chat" 供应商使用的端点
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();
+6 -43
View File
@@ -8,7 +8,6 @@
use super::{AuthInfo, AuthStrategy, ProviderAdapter};
use crate::provider::Provider;
use crate::proxy::error::ProxyError;
use crate::proxy::url_utils::{dedup_v1_v1_boundary_safe, split_url_suffix};
use regex::Regex;
use reqwest::RequestBuilder;
use std::sync::LazyLock;
@@ -139,23 +138,9 @@ impl ProviderAdapter for CodexAdapter {
}
fn build_url(&self, base_url: &str, endpoint: &str) -> String {
let (base, suffix) = split_url_suffix(base_url);
let base_trimmed = base.trim_end_matches('/');
let base_trimmed = base_url.trim_end_matches('/');
let endpoint_trimmed = endpoint.trim_start_matches('/');
// 检测 base_url 是否已经以 API 路径结尾(用户填写了完整路径)
// 仅支持 Responses API 路径模式:/v1/responses, /responses
let api_path_patterns = ["/v1/responses", "/responses"];
let base_ends_with_api_path = api_path_patterns
.iter()
.any(|pattern| base_trimmed.to_lowercase().ends_with(pattern));
// 如果 base_url 已经以 API 路径结尾,直接使用 base_url,不再追加 endpoint
if base_ends_with_api_path {
return format!("{base_trimmed}{suffix}");
}
// OpenAI/Codex 的 base_url 可能是:
// - 纯 origin: https://api.openai.com (需要自动补 /v1)
// - 已含 /v1: https://api.openai.com/v1 (直接拼接)
@@ -182,8 +167,11 @@ impl ProviderAdapter for CodexAdapter {
};
// 去除重复的 /v1/v1(可能由 base_url 与 endpoint 都带版本导致)
url = dedup_v1_v1_boundary_safe(url);
format!("{url}{suffix}")
while url.contains("/v1/v1") {
url = url.replace("/v1/v1", "/v1");
}
url
}
fn add_auth_headers(&self, request: RequestBuilder, auth: &AuthInfo) -> RequestBuilder {
@@ -280,31 +268,6 @@ mod tests {
assert_eq!(url, "https://www.packyapi.com/v1/responses");
}
#[test]
fn test_build_url_full_path_responses() {
let adapter = CodexAdapter::new();
// base_url 已包含完整路径 /v1/responses,不再追加
let url = adapter.build_url("https://example.com/v1/responses", "/responses");
assert_eq!(url, "https://example.com/v1/responses");
}
#[test]
fn test_build_url_full_path_short_suffix() {
let adapter = CodexAdapter::new();
// base_url 以 /responses 结尾(无 /v1 前缀)
let url = adapter.build_url("https://example.com/api/responses", "/responses");
assert_eq!(url, "https://example.com/api/responses");
}
#[test]
fn test_build_url_v1_base_dedup() {
let adapter = CodexAdapter::new();
// base_url 以 /v1 结尾,endpoint 也以 /v1 开头,应该去重
// 场景:https://integrate.api.nvidia.com/v1 + /v1/responses
let url = adapter.build_url("https://integrate.api.nvidia.com/v1", "/v1/responses");
assert_eq!(url, "https://integrate.api.nvidia.com/v1/responses");
}
// 官方客户端检测测试
#[test]
fn test_is_official_client_vscode() {
+2 -4
View File
@@ -9,7 +9,6 @@
use super::{AuthInfo, AuthStrategy, ProviderAdapter, ProviderType};
use crate::provider::Provider;
use crate::proxy::error::ProxyError;
use crate::proxy::url_utils::split_url_suffix;
use reqwest::RequestBuilder;
/// Gemini 适配器
@@ -201,8 +200,7 @@ impl ProviderAdapter for GeminiAdapter {
}
fn build_url(&self, base_url: &str, endpoint: &str) -> String {
let (base, suffix) = split_url_suffix(base_url);
let base_trimmed = base.trim_end_matches('/');
let base_trimmed = base_url.trim_end_matches('/');
let endpoint_trimmed = endpoint.trim_start_matches('/');
let mut url = format!("{base_trimmed}/{endpoint_trimmed}");
@@ -216,7 +214,7 @@ impl ProviderAdapter for GeminiAdapter {
}
}
format!("{url}{suffix}")
url
}
fn add_auth_headers(&self, request: RequestBuilder, auth: &AuthInfo) -> RequestBuilder {
+14
View File
@@ -218,6 +218,20 @@ impl ProxyServer {
// Claude API (支持带前缀和不带前缀两种格式)
.route("/v1/messages", post(handlers::handle_messages))
.route("/claude/v1/messages", post(handlers::handle_messages))
// OpenAI Chat Completions API (Codex CLI,支持带前缀和不带前缀)
.route("/chat/completions", post(handlers::handle_chat_completions))
.route(
"/v1/chat/completions",
post(handlers::handle_chat_completions),
)
.route(
"/v1/v1/chat/completions",
post(handlers::handle_chat_completions),
)
.route(
"/codex/v1/chat/completions",
post(handlers::handle_chat_completions),
)
// OpenAI Responses API (Codex CLI,支持带前缀和不带前缀)
.route("/responses", post(handlers::handle_responses))
.route("/v1/responses", post(handlers::handle_responses))
-437
View File
@@ -1,437 +0,0 @@
//! URL 构建工具模块
//!
//! 提供统一的 URL 构建逻辑,供前端预览和后端代理使用。
use crate::app_config::AppType;
use crate::proxy::providers::{ClaudeAdapter, CodexAdapter, ProviderAdapter};
use crate::proxy::url_utils::{dedup_v1_v1_boundary_safe, split_url_suffix};
use serde::{Deserialize, Serialize};
/// URL 预览结果
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UrlPreview {
/// 直连模式请求地址
pub direct_url: String,
/// 代理模式请求地址
pub proxy_url: String,
/// 是否为全链接(base_url 已包含 API 路径)
pub is_full_url: bool,
}
/// API 路径模式
struct ApiPathPatterns {
/// 直连模式默认端点
direct_endpoint: &'static str,
/// 代理模式端点(根据 api_format 可能不同)
proxy_endpoint: &'static str,
/// 识别为全链接的路径后缀
full_url_patterns: &'static [&'static str],
}
impl ApiPathPatterns {
fn for_claude(api_format: Option<&str>) -> Self {
// 根据 API 格式决定端点和全链接检测模式
if api_format == Some("openai_chat") {
Self {
direct_endpoint: "/v1/messages",
proxy_endpoint: "/v1/chat/completions",
// 与运行时 ClaudeAdapter 保持一致:同时识别 messages/chat 两类完整路径
full_url_patterns: &[
"/v1/messages",
"/messages",
"/v1/chat/completions",
"/chat/completions",
],
}
} else {
Self {
direct_endpoint: "/v1/messages",
proxy_endpoint: "/v1/messages",
// 与运行时 ClaudeAdapter 保持一致:同时识别 messages/chat 两类完整路径
full_url_patterns: &[
"/v1/messages",
"/messages",
"/v1/chat/completions",
"/chat/completions",
],
}
}
}
fn for_codex(api_format: Option<&str>) -> Self {
let _ = api_format;
Self {
direct_endpoint: "/responses",
proxy_endpoint: "/responses",
full_url_patterns: &["/v1/responses", "/responses"],
}
}
fn for_gemini() -> Self {
Self {
direct_endpoint: "/v1beta/models",
proxy_endpoint: "/v1beta/models",
full_url_patterns: &["/v1beta/models"],
}
}
}
/// 检测 URL 是否以指定的 API 路径结尾
fn url_ends_with_api_path(url: &str, patterns: &[&str]) -> bool {
let (base, _) = split_url_suffix(url);
let path_part = base.trim_end_matches('/').to_lowercase();
patterns
.iter()
.any(|pattern| path_part.ends_with(&pattern.to_lowercase()))
}
/// 硬拼接 URL(用于直连地址)
///
/// 始终将 endpoint 拼接到 base_url 后面,不做任何智能检测或去重。
fn build_direct_url(base_url: &str, endpoint: &str) -> String {
let (base, suffix) = split_url_suffix(base_url);
let base_trimmed = base.trim_end_matches('/');
let endpoint_trimmed = endpoint.trim_start_matches('/');
// 直接拼接,不做任何去重
format!("{base_trimmed}/{endpoint_trimmed}{suffix}")
}
/// 智能构建 URL(用于代理地址)
///
/// 如果 base_url 已经以 API 路径结尾,直接返回;否则追加 endpoint。
pub fn build_smart_url(base_url: &str, endpoint: &str, full_url_patterns: &[&str]) -> String {
let (base, suffix) = split_url_suffix(base_url);
let base_trimmed = base.trim_end_matches('/');
let endpoint_trimmed = endpoint.trim_start_matches('/');
// 检测 base_url 是否已经以 API 路径结尾
if url_ends_with_api_path(base_trimmed, full_url_patterns) {
return format!("{base_trimmed}{suffix}");
}
// 拼接 URL
let url = format!("{base_trimmed}/{endpoint_trimmed}");
let url = dedup_v1_v1_boundary_safe(url);
format!("{url}{suffix}")
}
fn build_runtime_like_url(
app_type: &AppType,
base_url: &str,
endpoint: &str,
is_proxy: bool,
) -> String {
match app_type {
// Claude 代理预览需要展示与运行时一致的 URL 归一化结果
AppType::Claude if is_proxy => ClaudeAdapter::new().build_url(base_url, endpoint),
// Codex/OpenCode 预览复用运行时 /v1 归一化规则
AppType::Codex | AppType::OpenCode | AppType::OpenClaw => {
CodexAdapter::new().build_url(base_url, endpoint)
}
_ => build_direct_url(base_url, endpoint),
}
}
/// 构建 URL 预览
///
/// 根据 app_type、base_url 和 api_format 计算直连和代理模式的请求地址。
/// - 直连地址:始终硬拼接默认后缀
/// - 代理地址:智能检测,如果已包含 API 路径则不重复拼接
pub fn build_url_preview(
app_type: &AppType,
base_url: &str,
api_format: Option<&str>,
) -> UrlPreview {
let patterns = match app_type {
AppType::Claude => ApiPathPatterns::for_claude(api_format),
AppType::Codex => ApiPathPatterns::for_codex(api_format),
AppType::Gemini => ApiPathPatterns::for_gemini(),
AppType::OpenCode => ApiPathPatterns::for_codex(api_format), // OpenCode 使用 Codex 逻辑
AppType::OpenClaw => ApiPathPatterns::for_codex(api_format), // OpenClaw 使用 Codex 逻辑
};
let is_full_url = url_ends_with_api_path(base_url, patterns.full_url_patterns);
// 直连地址:默认硬拼接;Codex/OpenCode 复用运行时规则(含 origin-only /v1 归一化)
let direct_url = build_runtime_like_url(app_type, base_url, patterns.direct_endpoint, false);
// 代理地址:Claude/Codex/OpenCode 复用运行时规则;Gemini 继续使用通用智能拼接
let proxy_url = match app_type {
AppType::Claude | AppType::Codex | AppType::OpenCode | AppType::OpenClaw => {
build_runtime_like_url(app_type, base_url, patterns.proxy_endpoint, true)
}
_ => build_smart_url(
base_url,
patterns.proxy_endpoint,
patterns.full_url_patterns,
),
};
UrlPreview {
direct_url,
proxy_url,
is_full_url,
}
}
/// 检查是否需要代理
///
/// 返回需要代理的原因,None 表示不需要代理
pub fn check_proxy_requirement(
app_type: &AppType,
base_url: &str,
api_format: Option<&str>,
) -> Option<&'static str> {
// Claude OpenAI Chat 格式必须开启代理(需要格式转换)
if matches!(app_type, AppType::Claude) && api_format == Some("openai_chat") {
return Some("openai_chat_format");
}
// base_url 缺失时无法做 full_url / url_mismatch 判断,避免误判
if base_url.trim().is_empty() {
return None;
}
let preview = build_url_preview(app_type, base_url, api_format);
// 如果是全链接且以直连后缀结尾,需要代理
if preview.is_full_url {
// 检查是否以直连后缀结尾
let direct_suffixes: &[&str] = match app_type {
AppType::Claude => &[
"/v1/messages",
"/messages",
"/v1/chat/completions",
"/chat/completions",
],
AppType::Codex | AppType::OpenCode | AppType::OpenClaw => &["/v1/responses", "/responses"],
_ => return None,
};
if url_ends_with_api_path(base_url, direct_suffixes) {
return Some("full_url");
}
}
// 如果直连地址和代理地址路径不同,需要代理(忽略查询参数差异)
let (direct_base, _) = split_url_suffix(&preview.direct_url);
let (proxy_base, _) = split_url_suffix(&preview.proxy_url);
if direct_base != proxy_base {
return Some("url_mismatch");
}
None
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_build_url_preview_claude_anthropic() {
let preview = build_url_preview(
&AppType::Claude,
"https://api.example.com",
Some("anthropic"),
);
assert_eq!(preview.direct_url, "https://api.example.com/v1/messages");
assert_eq!(preview.proxy_url, "https://api.example.com/v1/messages");
assert!(!preview.is_full_url);
}
#[test]
fn test_build_url_preview_claude_openai_chat() {
let preview = build_url_preview(
&AppType::Claude,
"https://api.example.com",
Some("openai_chat"),
);
assert_eq!(preview.direct_url, "https://api.example.com/v1/messages");
assert_eq!(
preview.proxy_url,
"https://api.example.com/v1/chat/completions"
);
assert!(!preview.is_full_url);
}
#[test]
fn test_build_url_preview_claude_full_url() {
// 全链接时:直连会硬拼接后缀,代理保持原地址
let preview = build_url_preview(
&AppType::Claude,
"https://api.example.com/v1/messages",
Some("anthropic"),
);
assert_eq!(
preview.direct_url,
"https://api.example.com/v1/messages/v1/messages"
);
assert_eq!(preview.proxy_url, "https://api.example.com/v1/messages");
assert!(preview.is_full_url);
}
#[test]
fn test_build_url_preview_codex_responses() {
let preview = build_url_preview(
&AppType::Codex,
"https://api.openai.com/v1",
Some("responses"),
);
assert_eq!(preview.direct_url, "https://api.openai.com/v1/responses");
assert_eq!(preview.proxy_url, "https://api.openai.com/v1/responses");
assert!(!preview.is_full_url);
}
#[test]
fn test_build_url_preview_codex_origin_normalizes_v1() {
let preview =
build_url_preview(&AppType::Codex, "https://api.openai.com", Some("responses"));
assert_eq!(preview.direct_url, "https://api.openai.com/v1/responses");
assert_eq!(preview.proxy_url, "https://api.openai.com/v1/responses");
assert!(!preview.is_full_url);
}
#[test]
fn test_build_url_preview_codex_full_url() {
// 全链接时:直连/代理均保持原地址(运行时适配器规则)
let preview = build_url_preview(
&AppType::Codex,
"https://api.example.com/v1/responses",
Some("responses"),
);
assert_eq!(preview.direct_url, "https://api.example.com/v1/responses");
assert_eq!(preview.proxy_url, "https://api.example.com/v1/responses");
assert!(preview.is_full_url);
}
#[test]
fn test_check_proxy_requirement_claude_openai_chat() {
let result = check_proxy_requirement(
&AppType::Claude,
"https://api.example.com",
Some("openai_chat"),
);
assert_eq!(result, Some("openai_chat_format"));
}
#[test]
fn test_check_proxy_requirement_claude_full_url() {
let result = check_proxy_requirement(
&AppType::Claude,
"https://api.example.com/v1/messages",
Some("anthropic"),
);
assert_eq!(result, Some("full_url"));
}
#[test]
fn test_check_proxy_requirement_codex_full_url() {
let result = check_proxy_requirement(
&AppType::Codex,
"https://api.example.com/v1/responses",
Some("responses"),
);
assert_eq!(result, Some("full_url"));
}
#[test]
fn test_check_proxy_requirement_codex_origin_none() {
let result =
check_proxy_requirement(&AppType::Codex, "https://api.openai.com", Some("responses"));
assert_eq!(result, None);
}
#[test]
fn test_check_proxy_requirement_none() {
let result = check_proxy_requirement(
&AppType::Claude,
"https://api.example.com",
Some("anthropic"),
);
assert_eq!(result, None);
}
#[test]
fn test_v1_dedup() {
// 代理地址使用 build_smart_url,会去重 /v1/v1
let url = build_smart_url("https://api.example.com/v1", "/v1/messages", &[]);
assert_eq!(url, "https://api.example.com/v1/messages");
}
#[test]
fn test_direct_url_no_dedup() {
// 直连地址硬拼接,不做任何去重
let preview = build_url_preview(
&AppType::Claude,
"https://api.example.com/v1",
Some("anthropic"),
);
assert_eq!(preview.direct_url, "https://api.example.com/v1/v1/messages");
// 代理地址按运行时规则构建,会进行路径去重
assert_eq!(preview.proxy_url, "https://api.example.com/v1/messages");
}
#[test]
fn test_query_suffix_preserved_in_preview() {
let preview = build_url_preview(
&AppType::Claude,
"https://api.example.com?beta=true",
Some("anthropic"),
);
assert_eq!(
preview.direct_url,
"https://api.example.com/v1/messages?beta=true"
);
assert_eq!(
preview.proxy_url,
"https://api.example.com/v1/messages?beta=true"
);
assert!(!preview.is_full_url);
}
#[test]
fn test_fragment_suffix_preserved_and_full_url_detected() {
let preview = build_url_preview(
&AppType::Claude,
"https://api.example.com/v1/messages#frag",
Some("anthropic"),
);
assert_eq!(
preview.proxy_url,
"https://api.example.com/v1/messages#frag"
);
assert!(preview.is_full_url);
}
#[test]
fn test_claude_full_url_detection_is_api_format_agnostic() {
// 与运行时 ClaudeAdapter 一致:/messages 与 /chat/completions 都视为全链接
let preview = build_url_preview(
&AppType::Claude,
"https://api.example.com/v1/messages",
Some("anthropic"),
);
assert!(preview.is_full_url);
let preview = build_url_preview(
&AppType::Claude,
"https://api.example.com/v1/chat/completions",
Some("anthropic"),
);
assert!(preview.is_full_url);
let preview = build_url_preview(
&AppType::Claude,
"https://api.example.com/v1/chat/completions",
Some("openai_chat"),
);
assert!(preview.is_full_url);
let preview = build_url_preview(
&AppType::Claude,
"https://api.example.com/v1/messages",
Some("openai_chat"),
);
assert!(preview.is_full_url);
}
}
-96
View File
@@ -1,96 +0,0 @@
//! URL utilities shared across proxy modules.
//!
//! This module intentionally avoids full URL parsing to keep behavior aligned with
//! existing "stringly-typed" base_url configurations while fixing common edge cases
//! (query/fragment handling and safe path de-duplication).
/// Split a URL-like string into `(base, suffix)` where suffix starts with `?` or `#`.
///
/// Example:
/// - `https://x/v1?token=1` => (`https://x/v1`, `?token=1`)
/// - `https://x/v1#frag` => (`https://x/v1`, `#frag`)
pub(crate) fn split_url_suffix(input: &str) -> (&str, &str) {
match input.find(['?', '#']) {
Some(idx) => (&input[..idx], &input[idx..]),
None => (input, ""),
}
}
/// De-duplicate repeated `/v1/v1` only when it occurs on a segment boundary.
///
/// This avoids corrupting valid paths such as `/v1/v1beta/...`.
pub(crate) fn dedup_v1_v1_boundary_safe(mut url: String) -> String {
const NEEDLE: &str = "/v1/v1";
let mut search_start = 0usize;
loop {
let Some(rel_pos) = url[search_start..].find(NEEDLE) else {
break;
};
let pos = search_start + rel_pos;
let after = pos + NEEDLE.len();
let boundary_ok = after == url.len()
|| matches!(
url.as_bytes().get(after),
Some(b'/') | Some(b'?') | Some(b'#')
);
if boundary_ok {
url.replace_range(pos..after, "/v1");
// Continue searching from the same position in case we created a new boundary match.
search_start = pos;
} else {
// Skip forward to find later occurrences that might be valid boundary matches.
search_start = pos + 1;
}
}
url
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn split_url_suffix_handles_query() {
let (base, suffix) = split_url_suffix("https://example.com/v1?token=1");
assert_eq!(base, "https://example.com/v1");
assert_eq!(suffix, "?token=1");
}
#[test]
fn split_url_suffix_handles_fragment() {
let (base, suffix) = split_url_suffix("https://example.com/v1#frag");
assert_eq!(base, "https://example.com/v1");
assert_eq!(suffix, "#frag");
}
#[test]
fn dedup_v1_v1_only_on_boundary() {
let url = "https://example.com/v1/v1/messages".to_string();
assert_eq!(
dedup_v1_v1_boundary_safe(url),
"https://example.com/v1/messages"
);
}
#[test]
fn dedup_v1_v1_does_not_corrupt_v1beta() {
let url = "https://example.com/v1/v1beta/models".to_string();
assert_eq!(
dedup_v1_v1_boundary_safe(url.clone()),
"https://example.com/v1/v1beta/models"
);
}
#[test]
fn dedup_v1_v1_skips_v1beta_but_dedups_later_occurrence() {
let url = "https://example.com/v1/v1beta/v1/v1/messages".to_string();
assert_eq!(
dedup_v1_v1_boundary_safe(url),
"https://example.com/v1/v1beta/v1/messages"
);
}
}
-1
View File
@@ -11,7 +11,6 @@ pub mod speedtest;
pub mod stream_check;
pub mod usage_stats;
pub mod webdav;
pub mod webdav_auto_sync;
pub mod webdav_sync;
pub use config::ConfigService;
+1 -1
View File
@@ -830,7 +830,7 @@ impl ProxyService {
///
/// 代理服务器的路由已经根据 API 端点自动区分应用类型:
/// - `/v1/messages` → Claude
/// - `/v1/responses` → Codex
/// - `/v1/chat/completions`, `/v1/responses` → Codex
/// - `/v1beta/*` → Gemini
///
/// 因此不需要在 URL 中添加应用前缀。
+3 -3
View File
@@ -293,11 +293,11 @@ impl StreamCheckService {
timeout: std::time::Duration,
) -> Result<(u16, String), AppError> {
let base = base_url.trim_end_matches('/');
// 健康检查不强制附加查询参数,保持与默认 Claude 路径一致
// URL 必须包含 ?beta=true 参数(某些中转服务依赖此参数验证请求来源)
let url = if base.ends_with("/v1") {
format!("{base}/messages")
format!("{base}/messages?beta=true")
} else {
format!("{base}/v1/messages")
format!("{base}/v1/messages?beta=true")
};
let body = json!({
+40 -85
View File
@@ -8,7 +8,6 @@ use std::time::Duration;
use crate::error::AppError;
use crate::proxy::http_client;
use futures::StreamExt;
const DEFAULT_TIMEOUT_SECS: u64 = 30;
/// Timeout for large file transfers (PUT/GET of db.sql, skills.zip).
@@ -238,7 +237,15 @@ pub async fn put_bytes(
)
.send()
.await
.map_err(|e| webdav_transport_error("webdav.put_failed", "PUT 请求", "PUT request", url, &e))?;
.map_err(|e| {
webdav_transport_error(
"webdav.put_failed",
"PUT 请求",
"PUT request",
url,
&e,
)
})?;
if resp.status().is_success() {
return Ok(());
@@ -252,7 +259,6 @@ pub async fn put_bytes(
pub async fn get_bytes(
url: &str,
auth: &WebDavAuth,
max_bytes: usize,
) -> Result<Option<(Vec<u8>, Option<String>)>, AppError> {
let client = http_client::get();
let resp = apply_auth(
@@ -263,7 +269,15 @@ pub async fn get_bytes(
)
.send()
.await
.map_err(|e| webdav_transport_error("webdav.get_failed", "GET 请求", "GET request", url, &e))?;
.map_err(|e| {
webdav_transport_error(
"webdav.get_failed",
"GET 请求",
"GET request",
url,
&e,
)
})?;
if resp.status() == StatusCode::NOT_FOUND {
return Ok(None);
@@ -271,29 +285,22 @@ pub async fn get_bytes(
if !resp.status().is_success() {
return Err(webdav_status_error("GET", resp.status(), url));
}
ensure_content_length_within_limit(resp.headers(), max_bytes, url)?;
let etag = resp
.headers()
.get("etag")
.and_then(|v| v.to_str().ok())
.map(|s| s.to_string());
let mut bytes = Vec::new();
let mut stream = resp.bytes_stream();
while let Some(chunk) = stream.next().await {
let chunk = chunk.map_err(|e| {
let bytes = resp
.bytes()
.await
.map_err(|e| {
AppError::localized(
"webdav.response_read_failed",
format!("读取 WebDAV 响应失败: {e}"),
format!("Failed to read WebDAV response: {e}"),
)
})?;
if bytes.len().saturating_add(chunk.len()) > max_bytes {
return Err(response_too_large_error(url, max_bytes));
}
bytes.extend_from_slice(&chunk);
}
Ok(Some((bytes, etag)))
Ok(Some((bytes.to_vec(), etag)))
}
/// HEAD request to retrieve the ETag. Returns `None` on 404.
@@ -308,7 +315,13 @@ pub async fn head_etag(url: &str, auth: &WebDavAuth) -> Result<Option<String>, A
.send()
.await
.map_err(|e| {
webdav_transport_error("webdav.head_failed", "HEAD 请求", "HEAD request", url, &e)
webdav_transport_error(
"webdav.head_failed",
"HEAD 请求",
"HEAD request",
url,
&e,
)
})?;
if resp.status() == StatusCode::NOT_FOUND {
@@ -373,7 +386,9 @@ pub fn webdav_status_error(op: &str, status: StatusCode, url: &str) -> AppError
if matches!(status, StatusCode::UNAUTHORIZED | StatusCode::FORBIDDEN) {
if jgy {
zh.push_str("。坚果云请使用「第三方应用密码」,并确认地址指向 /dav/ 下的目录。");
zh.push_str(
"。坚果云请使用「第三方应用密码」,并确认地址指向 /dav/ 下的目录。",
);
en.push_str(
". For Jianguoyun, use an app-specific password and ensure the URL points under /dav/.",
);
@@ -386,7 +401,9 @@ pub fn webdav_status_error(op: &str, status: StatusCode, url: &str) -> AppError
en.push_str(". Common Jianguoyun cause: URL is outside a writable /dav/ directory.");
} else if op == "MKCOL" && status == StatusCode::CONFLICT {
if jgy {
zh.push_str("。坚果云不允许自动创建顶层文件夹,请先在网页端手动创建后重试。");
zh.push_str(
"。坚果云不允许自动创建顶层文件夹,请先在网页端手动创建后重试。",
);
en.push_str(
". Jianguoyun does not allow creating top-level folders automatically; create it manually first.",
);
@@ -429,47 +446,9 @@ fn redact_url(raw: &str) -> String {
}
}
fn response_too_large_error(url: &str, max_bytes: usize) -> AppError {
let max_mb = max_bytes / 1024 / 1024;
AppError::localized(
"webdav.response_too_large",
format!(
"WebDAV 响应体超过上限({} MB: {}",
max_mb,
redact_url(url)
),
format!(
"WebDAV response body exceeds limit ({} MB): {}",
max_mb,
redact_url(url)
),
)
}
fn ensure_content_length_within_limit(
headers: &reqwest::header::HeaderMap,
max_bytes: usize,
url: &str,
) -> Result<(), AppError> {
let Some(content_length) = headers.get(reqwest::header::CONTENT_LENGTH) else {
return Ok(());
};
let Ok(raw) = content_length.to_str() else {
return Ok(());
};
let Ok(value) = raw.parse::<u64>() else {
return Ok(());
};
if value > max_bytes as u64 {
return Err(response_too_large_error(url, max_bytes));
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use reqwest::header::{HeaderMap, HeaderValue, CONTENT_LENGTH};
#[test]
fn build_remote_url_encodes_path_segments() {
@@ -519,34 +498,10 @@ mod tests {
#[test]
fn redact_url_hides_credentials_and_query_values() {
let redacted = redact_url("https://alice:secret@example.com:8443/dav?token=abc&foo=1");
assert_eq!(redacted, "https://example.com:8443/dav?[keys:foo,token]");
assert_eq!(
redacted,
"https://example.com:8443/dav?[keys:foo,token]"
);
assert!(!redacted.contains("secret"));
}
#[test]
fn ensure_content_length_within_limit_accepts_missing_or_small_values() {
let empty = HeaderMap::new();
assert!(
ensure_content_length_within_limit(&empty, 1024, "https://dav.example.com").is_ok()
);
let mut small = HeaderMap::new();
small.insert(CONTENT_LENGTH, HeaderValue::from_static("1024"));
assert!(
ensure_content_length_within_limit(&small, 1024, "https://dav.example.com").is_ok()
);
}
#[test]
fn ensure_content_length_within_limit_rejects_oversized_values() {
let mut large = HeaderMap::new();
large.insert(CONTENT_LENGTH, HeaderValue::from_static("2048"));
let err = ensure_content_length_within_limit(&large, 1024, "https://dav.example.com")
.expect_err("oversized response should be rejected");
assert!(
err.to_string().contains("too large") || err.to_string().contains("超过"),
"unexpected error: {err}"
);
}
}
-277
View File
@@ -1,277 +0,0 @@
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::sync::OnceLock;
use std::time::{Duration, Instant};
use serde_json::json;
use tauri::{AppHandle, Emitter};
use tokio::sync::mpsc::error::TrySendError;
use tokio::sync::mpsc::{channel, Receiver, Sender};
use crate::error::AppError;
use crate::services::webdav_sync as webdav_sync_service;
use crate::settings::{self, WebDavSyncSettings};
const AUTO_SYNC_DEBOUNCE_MS: u64 = 1000;
pub(crate) const MAX_AUTO_SYNC_WAIT_MS: u64 = 10_000;
static DB_CHANGE_TX: OnceLock<Sender<String>> = OnceLock::new();
static AUTO_SYNC_SUPPRESS_DEPTH: AtomicUsize = AtomicUsize::new(0);
pub(crate) struct AutoSyncSuppressionGuard;
impl AutoSyncSuppressionGuard {
pub fn new() -> Self {
AUTO_SYNC_SUPPRESS_DEPTH.fetch_add(1, Ordering::SeqCst);
Self
}
}
impl Drop for AutoSyncSuppressionGuard {
fn drop(&mut self) {
let _ =
AUTO_SYNC_SUPPRESS_DEPTH.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |value| {
Some(value.saturating_sub(1))
});
}
}
pub(crate) fn is_auto_sync_suppressed() -> bool {
AUTO_SYNC_SUPPRESS_DEPTH.load(Ordering::SeqCst) > 0
}
pub fn should_trigger_for_table(table: &str) -> bool {
let normalized = table.trim().to_ascii_lowercase();
matches!(
normalized.as_str(),
"providers"
| "provider_endpoints"
| "mcp_servers"
| "prompts"
| "skills"
| "skill_repos"
| "settings"
| "proxy_config"
)
}
pub(crate) fn enqueue_change_signal(tx: &Sender<String>, table: &str) -> bool {
match tx.try_send(table.to_string()) {
Ok(()) => true,
Err(TrySendError::Full(_)) | Err(TrySendError::Closed(_)) => false,
}
}
pub(crate) fn auto_sync_wait_duration(started_at: Instant, now: Instant) -> Option<Duration> {
let max_wait = Duration::from_millis(MAX_AUTO_SYNC_WAIT_MS);
let debounce = Duration::from_millis(AUTO_SYNC_DEBOUNCE_MS);
let elapsed = now.saturating_duration_since(started_at);
if elapsed >= max_wait {
return None;
}
Some(debounce.min(max_wait - elapsed))
}
fn should_run_auto_sync(settings: Option<&WebDavSyncSettings>) -> bool {
let Some(sync) = settings else {
return false;
};
sync.enabled && sync.auto_sync
}
fn persist_auto_sync_error(settings: &mut WebDavSyncSettings, error: &AppError) {
settings.status.last_error = Some(error.to_string());
settings.status.last_error_source = Some("auto".to_string());
let _ = settings::update_webdav_sync_status(settings.status.clone());
}
fn emit_auto_sync_status_updated(app: &AppHandle, status: &str, error: Option<&str>) {
let payload = match error {
Some(message) => json!({
"source": "auto",
"status": status,
"error": message,
}),
None => json!({
"source": "auto",
"status": status,
}),
};
if let Err(err) = app.emit("webdav-sync-status-updated", payload) {
log::debug!("[WebDAV] failed to emit sync status update event: {err}");
}
}
async fn run_auto_sync_upload(
db: &crate::database::Database,
app: &AppHandle,
) -> Result<(), AppError> {
let mut settings = settings::get_webdav_sync_settings();
if !should_run_auto_sync(settings.as_ref()) {
return Ok(());
}
let mut sync_settings = match settings.take() {
Some(value) => value,
None => return Ok(()),
};
let result = webdav_sync_service::run_with_sync_lock(webdav_sync_service::upload(
db,
&mut sync_settings,
))
.await;
match result {
Ok(_) => {
emit_auto_sync_status_updated(app, "success", None);
Ok(())
}
Err(err) => {
persist_auto_sync_error(&mut sync_settings, &err);
emit_auto_sync_status_updated(app, "error", Some(&err.to_string()));
Err(err)
}
}
}
pub fn notify_db_changed(table: &str) {
if is_auto_sync_suppressed() {
return;
}
if !should_trigger_for_table(table) {
return;
}
let Some(tx) = DB_CHANGE_TX.get() else {
return;
};
let _ = enqueue_change_signal(tx, table);
}
pub fn start_worker(db: Arc<crate::database::Database>, app: tauri::AppHandle) {
if DB_CHANGE_TX.get().is_some() {
return;
}
// Buffer size 1 is enough: we only need "dirty" signals, not every event.
let (tx, rx) = channel::<String>(1);
if DB_CHANGE_TX.set(tx).is_err() {
return;
}
tauri::async_runtime::spawn(async move {
run_worker_loop(db, rx, app).await;
});
}
async fn run_worker_loop(
db: Arc<crate::database::Database>,
mut rx: Receiver<String>,
app: tauri::AppHandle,
) {
while let Some(first_table) = rx.recv().await {
let started_at = Instant::now();
let mut merged_count = 1usize;
loop {
let Some(wait_for) = auto_sync_wait_duration(started_at, Instant::now()) else {
break;
};
let timeout = tokio::time::timeout(wait_for, rx.recv()).await;
match timeout {
Ok(Some(_)) => merged_count += 1,
Ok(None) => return,
Err(_) => break,
}
}
log::debug!(
"[WebDAV][AutoSync] Triggered by table={first_table}, merged_changes={merged_count}"
);
if let Err(err) = run_auto_sync_upload(&db, &app).await {
log::warn!("[WebDAV][AutoSync] Upload failed: {err}");
}
}
}
#[cfg(test)]
mod tests {
use super::{
auto_sync_wait_duration, enqueue_change_signal, is_auto_sync_suppressed,
should_run_auto_sync, should_trigger_for_table, AutoSyncSuppressionGuard,
MAX_AUTO_SYNC_WAIT_MS,
};
use crate::settings::WebDavSyncSettings;
use std::time::{Duration, Instant};
use tokio::sync::mpsc::channel;
#[test]
fn should_trigger_sync_for_config_tables_only() {
assert!(should_trigger_for_table("providers"));
assert!(should_trigger_for_table("settings"));
assert!(!should_trigger_for_table("proxy_request_logs"));
assert!(!should_trigger_for_table("provider_health"));
}
#[test]
fn suppression_guard_enables_and_restores_state() {
assert!(!is_auto_sync_suppressed());
{
let _guard = AutoSyncSuppressionGuard::new();
assert!(is_auto_sync_suppressed());
}
assert!(!is_auto_sync_suppressed());
}
#[test]
fn max_wait_caps_flush_latency_for_continuous_events() {
let started = Instant::now();
let later = started + Duration::from_millis(MAX_AUTO_SYNC_WAIT_MS + 1);
assert!(auto_sync_wait_duration(started, later).is_none());
}
#[tokio::test]
async fn enqueue_change_signal_drops_when_channel_is_full() {
let (tx, _rx) = channel::<String>(1);
assert!(enqueue_change_signal(&tx, "providers"));
assert!(!enqueue_change_signal(&tx, "providers"));
}
#[test]
fn should_run_auto_sync_requires_enabled_and_auto_sync_flag() {
assert!(!should_run_auto_sync(None));
let disabled = WebDavSyncSettings {
enabled: false,
auto_sync: true,
..WebDavSyncSettings::default()
};
assert!(!should_run_auto_sync(Some(&disabled)));
let auto_sync_off = WebDavSyncSettings {
enabled: true,
auto_sync: false,
..WebDavSyncSettings::default()
};
assert!(!should_run_auto_sync(Some(&auto_sync_off)));
let enabled = WebDavSyncSettings {
enabled: true,
auto_sync: true,
..WebDavSyncSettings::default()
};
assert!(should_run_auto_sync(Some(&enabled)));
}
#[test]
fn service_layer_does_not_depend_on_commands_layer() {
let source = include_str!("webdav_auto_sync.rs");
let needle = ["crate", "commands", ""].join("::");
assert!(
!source.contains(&needle),
"services layer should not depend on commands layer"
);
}
}
+32 -87
View File
@@ -5,9 +5,7 @@
use std::collections::BTreeMap;
use std::fs;
use std::future::Future;
use std::process::Command;
use std::sync::OnceLock;
use chrono::Utc;
use serde::{Deserialize, Serialize};
@@ -35,21 +33,6 @@ const REMOTE_DB_SQL: &str = "db.sql";
const REMOTE_SKILLS_ZIP: &str = "skills.zip";
const REMOTE_MANIFEST: &str = "manifest.json";
const MAX_DEVICE_NAME_LEN: usize = 64;
const MAX_MANIFEST_BYTES: usize = 1024 * 1024;
pub(super) const MAX_SYNC_ARTIFACT_BYTES: u64 = 512 * 1024 * 1024;
pub fn sync_mutex() -> &'static tokio::sync::Mutex<()> {
static LOCK: OnceLock<tokio::sync::Mutex<()>> = OnceLock::new();
LOCK.get_or_init(|| tokio::sync::Mutex::new(()))
}
pub async fn run_with_sync_lock<T, Fut>(operation: Fut) -> Result<T, AppError>
where
Fut: Future<Output = Result<T, AppError>>,
{
let _guard = sync_mutex().lock().await;
operation.await
}
fn localized(key: &'static str, zh: impl Into<String>, en: impl Into<String>) -> AppError {
AppError::localized(key, zh, en)
@@ -162,15 +145,13 @@ pub async fn download(
let auth = auth_for(settings);
let manifest_url = remote_file_url(settings, REMOTE_MANIFEST)?;
let (manifest_bytes, etag) = get_bytes(&manifest_url, &auth, MAX_MANIFEST_BYTES)
.await?
.ok_or_else(|| {
localized(
"webdav.sync.remote_empty",
"远端没有可下载的同步数据",
"No downloadable sync data found on the remote.",
)
})?;
let (manifest_bytes, etag) = get_bytes(&manifest_url, &auth).await?.ok_or_else(|| {
localized(
"webdav.sync.remote_empty",
"远端没有可下载的同步数据",
"No downloadable sync data found on the remote.",
)
})?;
let manifest: SyncManifest =
serde_json::from_slice(&manifest_bytes).map_err(|e| AppError::Json {
@@ -200,7 +181,7 @@ pub async fn fetch_remote_info(settings: &WebDavSyncSettings) -> Result<Option<V
let auth = auth_for(settings);
let manifest_url = remote_file_url(settings, REMOTE_MANIFEST)?;
let Some((bytes, _)) = get_bytes(&manifest_url, &auth, MAX_MANIFEST_BYTES).await? else {
let Some((bytes, _)) = get_bytes(&manifest_url, &auth).await? else {
return Ok(None);
};
@@ -233,7 +214,6 @@ fn persist_sync_success(
let status = WebDavSyncStatus {
last_sync_at: Some(Utc::now().timestamp()),
last_error: None,
last_error_source: None,
last_local_manifest_hash: Some(manifest_hash.clone()),
last_remote_manifest_hash: Some(manifest_hash),
last_remote_etag: etag,
@@ -339,10 +319,14 @@ fn sha256_hex(bytes: &[u8]) -> String {
}
fn detect_system_device_name() -> Option<String> {
let env_name = ["CC_SWITCH_DEVICE_NAME", "COMPUTERNAME", "HOSTNAME"]
.iter()
.filter_map(|key| std::env::var(key).ok())
.find_map(|value| normalize_device_name(&value));
let env_name = [
"CC_SWITCH_DEVICE_NAME",
"COMPUTERNAME",
"HOSTNAME",
]
.iter()
.filter_map(|key| std::env::var(key).ok())
.find_map(|value| normalize_device_name(&value));
if env_name.is_some() {
return env_name;
@@ -357,26 +341,21 @@ fn detect_system_device_name() -> Option<String> {
}
fn normalize_device_name(raw: &str) -> Option<String> {
let compact = raw
.chars()
.fold(String::with_capacity(raw.len()), |mut acc, ch| {
if ch.is_whitespace() {
acc.push(' ');
} else if !ch.is_control() {
acc.push(ch);
}
acc
});
let compact = raw.chars().fold(String::with_capacity(raw.len()), |mut acc, ch| {
if ch.is_whitespace() {
acc.push(' ');
} else if !ch.is_control() {
acc.push(ch);
}
acc
});
let normalized = compact.split_whitespace().collect::<Vec<_>>().join(" ");
let trimmed = normalized.trim();
if trimmed.is_empty() {
return None;
}
let limited = trimmed
.chars()
.take(MAX_DEVICE_NAME_LEN)
.collect::<String>();
let limited = trimmed.chars().take(MAX_DEVICE_NAME_LEN).collect::<String>();
if limited.is_empty() {
None
} else {
@@ -426,18 +405,14 @@ async fn download_and_verify(
format!("Manifest missing artifact: {artifact_name}"),
)
})?;
validate_artifact_size_limit(artifact_name, meta.size)?;
let url = remote_file_url(settings, artifact_name)?;
let (bytes, _) = get_bytes(&url, auth, MAX_SYNC_ARTIFACT_BYTES as usize)
.await?
.ok_or_else(|| {
localized(
"webdav.sync.remote_missing_artifact",
format!("远端缺少 artifact 文件: {artifact_name}"),
format!("Remote artifact file missing: {artifact_name}"),
)
})?;
let (bytes, _) = get_bytes(&url, auth).await?.ok_or_else(|| {
localized(
"webdav.sync.remote_missing_artifact",
format!("远端缺少 artifact 文件: {artifact_name}"),
format!("Remote artifact file missing: {artifact_name}"),
)
})?;
// Quick size check before expensive hash
if bytes.len() as u64 != meta.size {
@@ -528,21 +503,6 @@ fn auth_for(settings: &WebDavSyncSettings) -> WebDavAuth {
auth_from_credentials(&settings.username, &settings.password)
}
fn validate_artifact_size_limit(artifact_name: &str, size: u64) -> Result<(), AppError> {
if size > MAX_SYNC_ARTIFACT_BYTES {
let max_mb = MAX_SYNC_ARTIFACT_BYTES / 1024 / 1024;
return Err(localized(
"webdav.sync.artifact_too_large",
format!("artifact {artifact_name} 超过下载上限({} MB", max_mb),
format!(
"Artifact {artifact_name} exceeds download limit ({} MB)",
max_mb
),
));
}
Ok(())
}
// ─── Tests ───────────────────────────────────────────────────
#[cfg(test)]
@@ -686,19 +646,4 @@ mod tests {
"manifest should not contain deviceId"
);
}
#[test]
fn validate_artifact_size_limit_rejects_oversized_artifacts() {
let err = validate_artifact_size_limit("skills.zip", MAX_SYNC_ARTIFACT_BYTES + 1)
.expect_err("artifact larger than limit should be rejected");
assert!(
err.to_string().contains("too large") || err.to_string().contains("超过"),
"unexpected error: {err}"
);
}
#[test]
fn validate_artifact_size_limit_accepts_limit_boundary() {
assert!(validate_artifact_size_limit("skills.zip", MAX_SYNC_ARTIFACT_BYTES).is_ok());
}
}
+15 -79
View File
@@ -10,8 +10,10 @@ use zip::DateTime;
use crate::error::AppError;
use crate::services::skill::SkillService;
use super::{io_context_localized, localized, MAX_SYNC_ARTIFACT_BYTES, REMOTE_SKILLS_ZIP};
use super::{io_context_localized, localized, REMOTE_SKILLS_ZIP};
/// Maximum total bytes allowed during zip extraction (512 MB).
const MAX_EXTRACT_BYTES: u64 = 512 * 1024 * 1024;
/// Maximum number of entries allowed in a zip archive.
const MAX_EXTRACT_ENTRIES: usize = 10_000;
@@ -90,14 +92,8 @@ pub(super) fn restore_skills_zip(raw: &[u8]) -> Result<(), AppError> {
if archive.len() > MAX_EXTRACT_ENTRIES {
return Err(localized(
"webdav.sync.skills_zip_too_many_entries",
format!(
"skills.zip 条目数过多({}),上限 {MAX_EXTRACT_ENTRIES}",
archive.len()
),
format!(
"skills.zip has too many entries ({}), limit is {MAX_EXTRACT_ENTRIES}",
archive.len()
),
format!("skills.zip 条目数过多({}),上限 {MAX_EXTRACT_ENTRIES}", archive.len()),
format!("skills.zip has too many entries ({}), limit is {MAX_EXTRACT_ENTRIES}", archive.len()),
));
}
@@ -122,13 +118,15 @@ pub(super) fn restore_skills_zip(raw: &[u8]) -> Result<(), AppError> {
fs::create_dir_all(parent).map_err(|e| AppError::io(parent, e))?;
}
let mut out = fs::File::create(&out_path).map_err(|e| AppError::io(&out_path, e))?;
let _written = copy_entry_with_total_limit(
&mut entry,
&mut out,
&mut total_bytes,
MAX_SYNC_ARTIFACT_BYTES,
&out_path,
)?;
let written = std::io::copy(&mut entry, &mut out).map_err(|e| AppError::io(&out_path, e))?;
total_bytes += written;
if total_bytes > MAX_EXTRACT_BYTES {
return Err(localized(
"webdav.sync.skills_zip_too_large",
format!("skills.zip 解压后体积超过上限({} MB", MAX_EXTRACT_BYTES / 1024 / 1024),
format!("skills.zip extracted size exceeds limit ({} MB)", MAX_EXTRACT_BYTES / 1024 / 1024),
));
}
}
let ssot = SkillService::get_ssot_dir().map_err(|e| {
@@ -329,47 +327,10 @@ fn mark_visited_dir(path: &Path, visited: &mut HashSet<PathBuf>) -> Result<bool,
Ok(visited.insert(canonical))
}
fn copy_entry_with_total_limit<R: Read, W: Write>(
reader: &mut R,
writer: &mut W,
total_bytes: &mut u64,
max_total_bytes: u64,
out_path: &Path,
) -> Result<u64, AppError> {
let mut buffer = [0u8; 16 * 1024];
let mut written = 0u64;
loop {
let n = reader
.read(&mut buffer)
.map_err(|e| AppError::io(out_path, e))?;
if n == 0 {
break;
}
if total_bytes.saturating_add(n as u64) > max_total_bytes {
let max_mb = max_total_bytes / 1024 / 1024;
return Err(localized(
"webdav.sync.skills_zip_too_large",
format!("skills.zip 解压后体积超过上限({} MB", max_mb),
format!("skills.zip extracted size exceeds limit ({} MB)", max_mb),
));
}
writer
.write_all(&buffer[..n])
.map_err(|e| AppError::io(out_path, e))?;
*total_bytes += n as u64;
written += n as u64;
}
Ok(written)
}
#[cfg(test)]
mod tests {
use super::{copy_entry_with_total_limit, mark_visited_dir};
use super::mark_visited_dir;
use std::collections::HashSet;
use std::io::Cursor;
use std::path::Path;
use tempfile::tempdir;
#[test]
@@ -382,29 +343,4 @@ mod tests {
assert!(mark_visited_dir(&dir, &mut visited).expect("first visit"));
assert!(!mark_visited_dir(&dir, &mut visited).expect("second visit"));
}
#[test]
fn copy_entry_with_total_limit_rejects_oversized_stream_before_write() {
let mut reader = Cursor::new(vec![1u8; 16]);
let mut writer = Vec::new();
let mut total_bytes = 0u64;
let err = copy_entry_with_total_limit(
&mut reader,
&mut writer,
&mut total_bytes,
8,
Path::new("skills-extracted/file.bin"),
)
.expect_err("stream larger than limit should be rejected");
assert!(
err.to_string().contains("too large") || err.to_string().contains("超过"),
"unexpected error: {err}"
);
assert_eq!(
writer.len(),
0,
"should not write when the first chunk exceeds limit"
);
}
}
-5
View File
@@ -72,8 +72,6 @@ pub struct WebDavSyncStatus {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub last_error: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub last_error_source: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub last_remote_etag: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub last_local_manifest_hash: Option<String>,
@@ -95,8 +93,6 @@ pub struct WebDavSyncSettings {
#[serde(default)]
pub enabled: bool,
#[serde(default)]
pub auto_sync: bool,
#[serde(default)]
pub base_url: String,
#[serde(default)]
pub username: String,
@@ -114,7 +110,6 @@ impl Default for WebDavSyncSettings {
fn default() -> Self {
Self {
enabled: false,
auto_sync: false,
base_url: String::new(),
username: String::new(),
password: String::new(),
+2 -58
View File
@@ -3,7 +3,6 @@ import { useTranslation } from "react-i18next";
import { motion, AnimatePresence } from "framer-motion";
import { toast } from "sonner";
import { invoke } from "@tauri-apps/api/core";
import { listen } from "@tauri-apps/api/event";
import { useQueryClient } from "@tanstack/react-query";
import {
Plus,
@@ -82,12 +81,6 @@ type View =
| "openclawTools"
| "openclawAgents";
interface WebDavSyncStatusUpdatedPayload {
source?: string;
status?: string;
error?: string;
}
const DRAG_BAR_HEIGHT = isWindows() || isLinux() ? 0 : 28; // px
const HEADER_HEIGHT = 64; // px
const CONTENT_TOP_OFFSET = DRAG_BAR_HEIGHT + HEADER_HEIGHT;
@@ -229,10 +222,7 @@ function App() {
deleteProvider,
saveUsageScript,
setAsDefaultModel,
} = useProviderActions(activeApp, {
isProxyRunning,
isTakeoverActive: isCurrentAppTakeoverActive,
});
} = useProviderActions(activeApp);
const disableOmoMutation = useDisableCurrentOmo();
const handleDisableOmo = () => {
@@ -302,49 +292,6 @@ function App() {
};
}, [queryClient]);
useEffect(() => {
let unsubscribe: (() => void) | undefined;
let active = true;
const setupListener = async () => {
try {
const off = await listen(
"webdav-sync-status-updated",
async (event) => {
const payload = (event.payload ?? {}) as WebDavSyncStatusUpdatedPayload;
await queryClient.invalidateQueries({ queryKey: ["settings"] });
if (payload.source !== "auto" || payload.status !== "error") {
return;
}
toast.error(
t("settings.webdavSync.autoSyncFailedToast", {
error: payload.error || t("common.unknown"),
}),
);
},
);
if (!active) {
off();
return;
}
unsubscribe = off;
} catch (error) {
console.error(
"[App] Failed to subscribe webdav-sync-status-updated event",
error,
);
}
};
void setupListener();
return () => {
active = false;
unsubscribe?.();
};
}, [queryClient, t]);
useEffect(() => {
const checkEnvOnStartup = async () => {
try {
@@ -1025,10 +972,7 @@ function App() {
<>
{activeApp !== "opencode" && activeApp !== "openclaw" && (
<>
<ProxyToggle
activeApp={activeApp}
currentProvider={providers[currentProviderId] || null}
/>
<ProxyToggle activeApp={activeApp} />
<div
className={cn(
"transition-all duration-300 ease-in-out overflow-hidden",
@@ -169,8 +169,6 @@ export function ClaudeFormFields({
: t("providerForm.apiHint")
}
onManageClick={() => onEndpointModalToggle(true)}
appType="claude"
apiFormat={apiFormat}
/>
)}
@@ -94,8 +94,6 @@ export function CodexFormFields({
placeholder={t("providerForm.codexApiEndpointPlaceholder")}
hint={t("providerForm.codexApiHint")}
onManageClick={() => onEndpointModalToggle(true)}
appType="codex"
apiFormat="responses"
/>
)}
@@ -1,5 +1,5 @@
import { useTranslation } from "react-i18next";
import { useEffect, useState, useCallback, useMemo } from "react";
import { useEffect, useState } from "react";
import { FullScreenPanel } from "@/components/common/FullScreenPanel";
import { Label } from "@/components/ui/label";
import { Button } from "@/components/ui/button";
@@ -53,81 +53,6 @@ export function CommonConfigEditor({
return () => observer.disconnect();
}, []);
// Mirror value prop to local state so checkbox toggles and JsonEditor stay in sync
// (parent uses form.getValues which doesn't trigger re-renders)
const [localValue, setLocalValue] = useState(value);
useEffect(() => {
setLocalValue(value);
}, [value]);
const handleLocalChange = useCallback(
(newValue: string) => {
setLocalValue(newValue);
onChange(newValue);
},
[onChange],
);
const toggleStates = useMemo(() => {
try {
const config = JSON.parse(localValue);
return {
hideAttribution:
config?.attribution?.commit === "" && config?.attribution?.pr === "",
alwaysThinking: config?.alwaysThinkingEnabled === true,
teammates:
config?.env?.CLAUDE_CODE_EXPERIMENTAL_AGENT_TEAMS === "1" ||
config?.env?.CLAUDE_CODE_EXPERIMENTAL_AGENT_TEAMS === 1,
};
} catch {
return {
hideAttribution: false,
alwaysThinking: false,
teammates: false,
};
}
}, [localValue]);
const handleToggle = useCallback(
(toggleKey: string, checked: boolean) => {
try {
const config = JSON.parse(localValue || "{}");
switch (toggleKey) {
case "hideAttribution":
if (checked) {
config.attribution = { commit: "", pr: "" };
} else {
delete config.attribution;
}
break;
case "alwaysThinking":
if (checked) {
config.alwaysThinkingEnabled = true;
} else {
delete config.alwaysThinkingEnabled;
}
break;
case "teammates":
if (!config.env) config.env = {};
if (checked) {
config.env.CLAUDE_CODE_EXPERIMENTAL_AGENT_TEAMS = "1";
} else {
delete config.env.CLAUDE_CODE_EXPERIMENTAL_AGENT_TEAMS;
if (Object.keys(config.env).length === 0) delete config.env;
}
break;
}
handleLocalChange(JSON.stringify(config, null, 2));
} catch {
// Don't modify if JSON is invalid
}
},
[localValue, handleLocalChange],
);
return (
<>
<div className="space-y-2">
@@ -166,40 +91,9 @@ export function CommonConfigEditor({
{commonConfigError}
</p>
)}
<div className="flex flex-wrap items-center gap-x-4 gap-y-1">
<label className="inline-flex items-center gap-2 text-sm text-muted-foreground cursor-pointer">
<input
type="checkbox"
checked={toggleStates.hideAttribution}
onChange={(e) =>
handleToggle("hideAttribution", e.target.checked)
}
className="w-4 h-4 text-blue-500 bg-white dark:bg-gray-800 border-border-default rounded focus:ring-blue-500 dark:focus:ring-blue-400 focus:ring-2"
/>
<span>{t("claudeConfig.hideAttribution")}</span>
</label>
<label className="inline-flex items-center gap-2 text-sm text-muted-foreground cursor-pointer">
<input
type="checkbox"
checked={toggleStates.alwaysThinking}
onChange={(e) => handleToggle("alwaysThinking", e.target.checked)}
className="w-4 h-4 text-blue-500 bg-white dark:bg-gray-800 border-border-default rounded focus:ring-blue-500 dark:focus:ring-blue-400 focus:ring-2"
/>
<span>{t("claudeConfig.alwaysThinking")}</span>
</label>
<label className="inline-flex items-center gap-2 text-sm text-muted-foreground cursor-pointer">
<input
type="checkbox"
checked={toggleStates.teammates}
onChange={(e) => handleToggle("teammates", e.target.checked)}
className="w-4 h-4 text-blue-500 bg-white dark:bg-gray-800 border-border-default rounded focus:ring-blue-500 dark:focus:ring-blue-400 focus:ring-2"
/>
<span>{t("claudeConfig.enableTeammates")}</span>
</label>
</div>
<JsonEditor
value={localValue}
onChange={handleLocalChange}
value={value}
onChange={onChange}
placeholder={`{
"env": {
"ANTHROPIC_BASE_URL": "https://your-api-endpoint.com",
+18 -26
View File
@@ -43,10 +43,7 @@ import {
import { OpenCodeFormFields } from "./OpenCodeFormFields";
import { OpenClawFormFields } from "./OpenClawFormFields";
import type { UniversalProviderPreset } from "@/config/universalProviderPresets";
import {
applyTemplateValues,
setCodexWireApi,
} from "@/utils/providerConfigUtils";
import { applyTemplateValues } from "@/utils/providerConfigUtils";
import { mergeProviderMeta } from "@/utils/providerMetaUtils";
import { getCodexCustomTemplate } from "@/config/codexTemplates";
import CodexConfigEditor from "./CodexConfigEditor";
@@ -408,13 +405,14 @@ export function ProviderForm({
onConfigChange: (config) => form.setValue("settingsConfig", config),
});
const [localClaudeApiFormat, setLocalClaudeApiFormat] =
useState<ClaudeApiFormat>(() => {
if (appId !== "claude") return "anthropic";
return initialData?.meta?.apiFormat === "openai_chat"
? "openai_chat"
: "anthropic";
});
const [localApiFormat, setLocalApiFormat] = useState<ClaudeApiFormat>(() => {
if (appId !== "claude") return "anthropic";
return initialData?.meta?.apiFormat ?? "anthropic";
});
const handleApiFormatChange = useCallback((format: ClaudeApiFormat) => {
setLocalApiFormat(format);
}, []);
const {
codexAuth,
@@ -442,10 +440,6 @@ export function ProviderForm({
[originalHandleCodexConfigChange, debouncedValidate],
);
const handleClaudeApiFormatChange = useCallback((format: ClaudeApiFormat) => {
setLocalClaudeApiFormat(format);
}, []);
useEffect(() => {
if (appId === "codex" && !initialData && selectedPresetId === "custom") {
const template = getCodexCustomTemplate();
@@ -1367,7 +1361,7 @@ export function ProviderForm({
const authJson = JSON.parse(codexAuth);
const configObj = {
auth: authJson,
config: setCodexWireApi(codexConfig ?? "", "responses"),
config: codexConfig ?? "",
};
settingsConfig = JSON.stringify(configObj);
} catch (err) {
@@ -1502,11 +1496,6 @@ export function ProviderForm({
}
}
const providerApiFormat =
appId === "claude" && category !== "official"
? localClaudeApiFormat
: undefined;
const baseMeta: ProviderMeta | undefined =
payload.meta ?? (initialData?.meta ? { ...initialData.meta } : undefined);
payload.meta = {
@@ -1521,7 +1510,10 @@ export function ProviderForm({
pricingConfig.enabled && pricingConfig.pricingModelSource !== "inherit"
? pricingConfig.pricingModelSource
: undefined,
apiFormat: providerApiFormat,
apiFormat:
appId === "claude" && category !== "official"
? localApiFormat
: undefined,
};
onSubmit(payload);
@@ -1776,9 +1768,9 @@ export function ProviderForm({
);
if (preset.apiFormat) {
setLocalClaudeApiFormat(preset.apiFormat);
setLocalApiFormat(preset.apiFormat);
} else {
setLocalClaudeApiFormat("anthropic");
setLocalApiFormat("anthropic");
}
form.reset({
@@ -1961,8 +1953,8 @@ export function ProviderForm({
defaultOpusModel={defaultOpusModel}
onModelChange={handleModelChange}
speedTestEndpoints={speedTestEndpoints}
apiFormat={localClaudeApiFormat}
onApiFormatChange={handleClaudeApiFormatChange}
apiFormat={localApiFormat}
onApiFormatChange={handleApiFormatChange}
/>
)}
@@ -1,11 +1,7 @@
import { useState, useEffect, useRef } from "react";
import { useTranslation } from "react-i18next";
import { FormLabel } from "@/components/ui/form";
import { Input } from "@/components/ui/input";
import { Zap, AlertTriangle, Server, Unplug } from "lucide-react";
import { proxyApi, type UrlPreview } from "@/lib/api/proxy";
type AppType = "claude" | "codex" | "gemini";
import { Zap } from "lucide-react";
interface EndpointFieldProps {
id: string;
@@ -17,11 +13,6 @@ interface EndpointFieldProps {
showManageButton?: boolean;
onManageClick?: () => void;
manageButtonLabel?: string;
// 应用类型和 API 格式
appType?: AppType;
apiFormat?: string;
// 是否显示请求地址预览
showUrlPreview?: boolean;
}
export function EndpointField({
@@ -34,48 +25,13 @@ export function EndpointField({
showManageButton = true,
onManageClick,
manageButtonLabel,
appType,
apiFormat,
showUrlPreview = true,
}: EndpointFieldProps) {
const { t } = useTranslation();
const [urlPreview, setUrlPreview] = useState<UrlPreview | null>(null);
const lastRequestIdRef = useRef(0);
const defaultManageLabel = t("providerForm.manageAndTest", {
defaultValue: "管理和测速",
});
// 调用后端 API 获取 URL 预览
useEffect(() => {
if (!value || !appType || !showUrlPreview) {
// 标记当前所有已发出的请求为过期,避免旧请求回写
lastRequestIdRef.current += 1;
setUrlPreview(null);
return;
}
// 防抖:延迟 300ms 后请求
const timer = setTimeout(async () => {
const requestId = ++lastRequestIdRef.current;
try {
const preview = await proxyApi.buildUrlPreview(
appType,
value,
apiFormat,
);
if (requestId !== lastRequestIdRef.current) return;
setUrlPreview(preview);
} catch (error) {
console.error("Failed to build URL preview:", error);
if (requestId !== lastRequestIdRef.current) return;
setUrlPreview(null);
}
}, 300);
return () => clearTimeout(timer);
}, [value, appType, apiFormat, showUrlPreview]);
return (
<div className="space-y-2">
<div className="flex items-center justify-between">
@@ -99,68 +55,6 @@ export function EndpointField({
placeholder={placeholder}
autoComplete="off"
/>
{/* 请求地址预览 */}
{showUrlPreview && urlPreview && (
<div className="p-2 bg-muted/50 border border-border rounded-md space-y-2">
{/* CLI 直连请求地址 */}
<div>
<p className="text-xs text-muted-foreground mb-0.5 flex items-center gap-1">
<Unplug className="h-3 w-3" />
{t("providerForm.directRequestUrl", {
defaultValue: "CLI 直连请求地址:",
})}
</p>
<p className="text-xs font-mono text-foreground break-all pl-4">
{urlPreview.direct_url}
</p>
<p className="text-xs text-muted-foreground/70 mt-0.5 pl-4">
{t("providerForm.directRequestUrlDesc", {
defaultValue: "CLI 直连模式下的实际请求地址",
})}
</p>
</div>
{/* CCS 代理请求地址 */}
<div className="pt-1.5 border-t border-border/50">
<p className="text-xs text-muted-foreground mb-0.5 flex items-center gap-1">
<Server className="h-3 w-3" />
{t("providerForm.proxyRequestUrl", {
defaultValue: "CCS 代理请求地址:",
})}
</p>
<p className="text-xs font-mono text-foreground break-all pl-4">
{urlPreview.proxy_url}
</p>
<p className="text-xs text-muted-foreground/70 mt-0.5 pl-4">
{t("providerForm.proxyRequestUrlDesc", {
defaultValue: "CCS 智能拼接后转发到上游的地址",
})}
</p>
</div>
</div>
)}
{/* 全链接警告 */}
{urlPreview?.is_full_url && (
<div className="flex items-start gap-2 p-2 bg-orange-50 dark:bg-orange-950/30 border border-orange-200 dark:border-orange-800 rounded-md">
<AlertTriangle className="h-4 w-4 text-orange-500 mt-0.5 flex-shrink-0" />
<div className="flex-1">
<p className="text-xs text-orange-600 dark:text-orange-400 font-medium">
{t("providerForm.fullUrlWarningTitle", {
defaultValue: "检测到完整 API 路径",
})}
</p>
<p className="text-xs text-orange-600/80 dark:text-orange-400/80 mt-0.5">
{t("providerForm.fullUrlWarning", {
defaultValue:
"填写了包含 API 路径的完整地址,此配置仅在代理模式下生效。直连模式下请只填写基础地址。",
})}
</p>
</div>
</div>
)}
{hint ? (
<div className="p-3 bg-amber-50 dark:bg-amber-900/20 border border-amber-200 dark:border-amber-700 rounded-lg">
<p className="text-xs text-amber-600 dark:text-amber-400">{hint}</p>
+3 -67
View File
@@ -6,91 +6,27 @@
*/
import { Radio, Loader2 } from "lucide-react";
import { useState } from "react";
import { Switch } from "@/components/ui/switch";
import { useProxyStatus } from "@/hooks/useProxyStatus";
import { cn } from "@/lib/utils";
import { useTranslation } from "react-i18next";
import { toast } from "sonner";
import type { AppId } from "@/lib/api";
import { proxyApi } from "@/lib/api/proxy";
import type { Provider } from "@/types";
import { extractProviderBaseUrl } from "@/utils/providerBaseUrl";
interface ProxyToggleProps {
className?: string;
activeApp: AppId;
currentProvider?: Provider | null;
}
export function ProxyToggle({
className,
activeApp,
currentProvider,
}: ProxyToggleProps) {
export function ProxyToggle({ className, activeApp }: ProxyToggleProps) {
const { t } = useTranslation();
const [isCheckingRequirement, setIsCheckingRequirement] = useState(false);
const { isRunning, takeoverStatus, setTakeoverForApp, isPending, status } =
useProxyStatus();
const handleToggle = async (checked: boolean) => {
// 关闭代理时,检查当前供应商是否是全链接配置
if (
!checked &&
currentProvider &&
currentProvider.category !== "official"
) {
const baseUrl = extractProviderBaseUrl(currentProvider, activeApp);
const apiFormat = currentProvider.meta?.apiFormat;
setIsCheckingRequirement(true);
try {
let proxyRequirement: string | null = null;
if (baseUrl || apiFormat) {
proxyRequirement = await proxyApi.checkProxyRequirement(
activeApp,
baseUrl || "",
apiFormat,
);
}
if (proxyRequirement) {
const warningKey =
proxyRequirement === "openai_chat_format"
? "notifications.openAIChatFormatWarningOnDisable"
: proxyRequirement === "url_mismatch"
? "notifications.urlMismatchWarningOnDisable"
: "notifications.fullUrlWarningOnDisable";
toast.warning(
t(warningKey, {
defaultValue:
"当前供应商配置可能依赖代理模式,关闭代理后可能无法正常工作。建议更换为基础地址配置或保持代理开启。",
}),
{ duration: 6000, closeButton: true },
);
}
} catch (error) {
console.error("Failed to check proxy requirement:", error);
} finally {
setIsCheckingRequirement(false);
}
}
try {
await setTakeoverForApp({ appType: activeApp, enabled: checked });
} catch (error) {
console.error("[ProxyToggle] Toggle takeover failed:", error);
toast.error(
t("proxy.takeover.toggleFailed", {
defaultValue: "切换接管状态失败",
}),
{
description: t("proxy.takeover.toggleFailedDesc", {
defaultValue: "请检查代理服务状态与权限,然后重试。",
}),
closeButton: true,
},
);
}
};
@@ -125,7 +61,7 @@ export function ProxyToggle({
)}
title={tooltipText}
>
{isPending || isCheckingRequirement ? (
{isPending ? (
<Loader2 className="h-4 w-4 animate-spin text-muted-foreground" />
) : (
<Radio
@@ -140,7 +76,7 @@ export function ProxyToggle({
<Switch
checked={takeoverEnabled}
onCheckedChange={handleToggle}
disabled={isPending || isCheckingRequirement}
disabled={isPending}
/>
</div>
);
@@ -38,7 +38,7 @@ export function DirectorySettings({
const { t } = useTranslation();
return (
<div className="space-y-6">
<>
{/* CC Switch 配置目录 - 独立区块 */}
<section className="space-y-4">
<header className="space-y-1">
@@ -131,7 +131,7 @@ export function DirectorySettings({
onReset={() => onResetDirectory("opencode")}
/>
</section>
</div>
</>
);
}
@@ -53,7 +53,7 @@ export function ImportExportSection({
</p>
</header>
<div className="space-y-4 rounded-lg border border-border bg-muted/40 p-6">
<div className="space-y-4 rounded-xl glass-card p-6 border border-white/10">
{/* Import and Export Buttons Side by Side */}
<div className="grid grid-cols-2 gap-4 items-stretch">
{/* Import Button */}
@@ -16,7 +16,6 @@ import { useQueryClient } from "@tanstack/react-query";
import { toast } from "sonner";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Switch } from "@/components/ui/switch";
import {
Select,
SelectContent,
@@ -163,7 +162,6 @@ export function WebdavSyncSection({ config }: WebdavSyncSectionProps) {
password: config?.password ?? "",
remoteRoot: config?.remoteRoot ?? "cc-switch-sync",
profile: config?.profile ?? "default",
autoSync: config?.autoSync ?? false,
}));
// Preset selector — derived from initial URL, updated on user selection
@@ -198,7 +196,6 @@ export function WebdavSyncSection({ config }: WebdavSyncSectionProps) {
password: config.password ?? "",
remoteRoot: config.remoteRoot ?? "cc-switch-sync",
profile: config.profile ?? "default",
autoSync: config.autoSync ?? false,
});
setPasswordTouched(false);
setPresetId(detectPreset(config.baseUrl ?? ""));
@@ -240,16 +237,6 @@ export function WebdavSyncSection({ config }: WebdavSyncSectionProps) {
}
}, [form.baseUrl, presetId]);
const handleAutoSyncChange = useCallback((checked: boolean) => {
setForm((prev) => ({ ...prev, autoSync: checked }));
setDirty(true);
setJustSaved(false);
if (justSavedTimerRef.current) {
clearTimeout(justSavedTimerRef.current);
justSavedTimerRef.current = null;
}
}, []);
const buildSettings = useCallback((): WebDavSyncSettings | null => {
const baseUrl = form.baseUrl.trim();
if (!baseUrl) return null;
@@ -260,7 +247,6 @@ export function WebdavSyncSection({ config }: WebdavSyncSectionProps) {
password: form.password,
remoteRoot: form.remoteRoot.trim() || "cc-switch-sync",
profile: form.profile.trim() || "default",
autoSync: form.autoSync,
};
}, [form]);
@@ -447,9 +433,6 @@ export function WebdavSyncSection({ config }: WebdavSyncSectionProps) {
const lastSyncDisplay = lastSyncAt
? new Date(lastSyncAt * 1000).toLocaleString()
: null;
const lastError = config?.status?.lastError?.trim();
const showAutoSyncError =
!!lastError && config?.status?.lastErrorSource === "auto";
// ─── Render ─────────────────────────────────────────────
@@ -576,23 +559,6 @@ export function WebdavSyncSection({ config }: WebdavSyncSectionProps) {
disabled={isLoading}
/>
</div>
<div className="flex items-start gap-4">
<label className="w-40 text-xs font-medium text-foreground shrink-0">
{t("settings.webdavSync.autoSync")}
<span className="block text-[10px] font-normal text-muted-foreground">
{t("settings.webdavSync.autoSyncHint")}
</span>
</label>
<div className="pt-1">
<Switch
checked={form.autoSync}
onCheckedChange={handleAutoSyncChange}
aria-label={t("settings.webdavSync.autoSync")}
disabled={isLoading}
/>
</div>
</div>
</div>
{/* Last sync time */}
@@ -601,17 +567,6 @@ export function WebdavSyncSection({ config }: WebdavSyncSectionProps) {
{t("settings.webdavSync.lastSync", { time: lastSyncDisplay })}
</p>
)}
{showAutoSyncError && (
<div className="rounded-lg border border-red-300/70 bg-red-50/80 px-3 py-2 text-xs text-red-900 dark:border-red-500/50 dark:bg-red-950/30 dark:text-red-200">
<p className="font-medium">
{t("settings.webdavSync.autoSyncLastErrorTitle")}
</p>
<p className="mt-1 break-all whitespace-pre-wrap">{lastError}</p>
<p className="mt-1 text-[11px] text-red-700/90 dark:text-red-300/80">
{t("settings.webdavSync.autoSyncLastErrorHint")}
</p>
</div>
)}
{/* Config buttons + save status */}
<div className="flex flex-wrap items-center gap-3 pt-2">
+26 -114
View File
@@ -3,7 +3,6 @@ import { useQueryClient } from "@tanstack/react-query";
import { toast } from "sonner";
import { useTranslation } from "react-i18next";
import { providersApi, settingsApi, openclawApi, type AppId } from "@/lib/api";
import { proxyApi } from "@/lib/api/proxy";
import type {
Provider,
UsageScript,
@@ -18,25 +17,13 @@ import {
useSwitchProviderMutation,
} from "@/lib/query";
import { extractErrorMessage } from "@/utils/errorUtils";
import { extractProviderBaseUrl } from "@/utils/providerBaseUrl";
import { openclawKeys } from "@/hooks/useOpenClaw";
interface UseProviderActionsOptions {
/** 代理服务是否正在运行 */
isProxyRunning?: boolean;
/** 当前应用的代理接管是否激活 */
isTakeoverActive?: boolean;
}
/**
* Hook for managing provider actions (add, update, delete, switch)
* Extracts business logic from App.tsx
*/
export function useProviderActions(
activeApp: AppId,
options: UseProviderActionsOptions = {},
) {
const { isProxyRunning = false, isTakeoverActive = false } = options;
export function useProviderActions(activeApp: AppId) {
const { t } = useTranslation();
const queryClient = useQueryClient();
@@ -146,11 +133,30 @@ export function useProviderActions(
// 切换供应商
const switchProvider = useCallback(
async (provider: Provider) => {
// 官方供应商不需要检查
if (provider.category === "official") {
try {
await switchProviderMutation.mutateAsync(provider.id);
await syncClaudePlugin(provider);
try {
await switchProviderMutation.mutateAsync(provider.id);
await syncClaudePlugin(provider);
// 根据供应商类型显示不同的成功提示
if (
activeApp === "claude" &&
provider.category !== "official" &&
provider.meta?.apiFormat === "openai_chat"
) {
// OpenAI Chat 格式供应商:显示代理提示
toast.info(
t("notifications.openAIChatFormatHint", {
defaultValue:
"此供应商使用 OpenAI Chat 格式,需要开启代理服务才能正常使用",
}),
{
duration: 5000,
closeButton: true,
},
);
} else {
// 普通供应商:显示切换成功
// OpenCode/OpenClaw: show "added to config" message instead of "switched"
const isMultiProviderApp =
activeApp === "opencode" || activeApp === "openclaw";
const messageKey = isMultiProviderApp
@@ -159,94 +165,7 @@ export function useProviderActions(
const defaultMessage = isMultiProviderApp
? "已添加到配置"
: "切换成功!";
toast.success(
t(messageKey, { defaultValue: defaultMessage }),
{
closeButton: true,
},
);
} catch {
// 错误提示由 mutation 处理
}
return;
}
// 提取 base URL 和 API 格式
const baseUrl = extractProviderBaseUrl(provider, activeApp);
const apiFormat = provider.meta?.apiFormat;
// 调用后端 API 检查是否需要代理(统一由后端控制)
let proxyRequirement: string | null = null;
let proxyRequirementCheckFailed = false;
if (baseUrl || apiFormat) {
try {
proxyRequirement = await proxyApi.checkProxyRequirement(
activeApp,
baseUrl || "",
apiFormat,
);
} catch (error) {
console.error("Failed to check proxy requirement:", error);
proxyRequirementCheckFailed = true;
}
}
// 如果需要代理但代理未激活,阻止切换并提示
if (proxyRequirement && !(isProxyRunning && isTakeoverActive)) {
let message: string;
if (proxyRequirement === "openai_chat_format") {
message = t("notifications.openAIChatFormatRequiresProxy", {
defaultValue:
"此供应商使用 OpenAI Chat 格式,需要开启代理服务进行格式转换才能正常使用。请先开启代理并接管当前应用。",
});
} else if (proxyRequirement === "full_url") {
// 用户填了全链接(如 /v1/messages 结尾)
message = t("notifications.fullUrlRequiresProxy", {
defaultValue:
"此供应商配置了完整 API 路径,直连模式下客户端可能会重复追加路径。请先开启代理并接管当前应用。",
});
} else {
// url_mismatch: 直连地址和代理地址不匹配
message = t("notifications.urlMismatchRequiresProxy", {
defaultValue:
"此供应商的请求地址配置与 API 格式不匹配,直连模式下无法正常工作。请先开启代理并接管当前应用。",
});
}
toast.warning(message, {
duration: 6000,
closeButton: true,
});
return; // 阻止切换
}
try {
await switchProviderMutation.mutateAsync(provider.id);
await syncClaudePlugin(provider);
const isMultiProviderApp =
activeApp === "opencode" || activeApp === "openclaw";
const messageKey = isMultiProviderApp
? "notifications.addToConfigSuccess"
: "notifications.switchSuccess";
const defaultMessage = isMultiProviderApp ? "已添加到配置" : "切换成功!";
if (proxyRequirementCheckFailed && baseUrl) {
toast.success(
t("notifications.switchAppliedUnverified", {
defaultValue: "切换已应用(未验证直连兼容性)",
}),
{
description: t("notifications.switchAppliedUnverifiedDesc", {
defaultValue:
"未能验证该端点是否需要代理。如果切换后无法正常使用,请开启代理并接管当前应用。",
}),
closeButton: true,
duration: 6000,
},
);
} else {
toast.success(t(messageKey, { defaultValue: defaultMessage }), {
closeButton: true,
});
@@ -255,14 +174,7 @@ export function useProviderActions(
// 错误提示由 mutation 处理
}
},
[
switchProviderMutation,
syncClaudePlugin,
activeApp,
t,
isProxyRunning,
isTakeoverActive,
],
[switchProviderMutation, syncClaudePlugin, activeApp, t],
);
// 删除供应商
+1 -27
View File
@@ -58,10 +58,7 @@
"extractFromCurrent": "Extract from Editor",
"extractNoCommonConfig": "No common config available to extract from editor",
"extractFailed": "Extract failed: {{error}}",
"saveFailed": "Save failed: {{error}}",
"hideAttribution": "Hide AI Attribution",
"alwaysThinking": "Extended Thinking",
"enableTeammates": "Teammates Mode"
"saveFailed": "Save failed: {{error}}"
},
"header": {
"viewOnGithub": "View on GitHub",
@@ -167,14 +164,6 @@
"settingsSaved": "Settings saved",
"settingsSaveFailed": "Failed to save settings: {{error}}",
"openAIChatFormatHint": "This provider uses OpenAI Chat format and requires the proxy service to be enabled",
"openAIChatFormatRequiresProxy": "This provider uses OpenAI Chat format and requires the proxy service for format conversion. Please enable the proxy and takeover the current app first.",
"fullUrlRequiresProxy": "This provider is configured with a full API path. The client may append duplicate paths in direct connection mode. Please enable the proxy and takeover the current app first.",
"urlMismatchRequiresProxy": "This provider's request URL configuration does not match the API format. It cannot work properly in direct connection mode. Please enable the proxy and takeover the current app first.",
"fullUrlWarningOnDisable": "The current provider is configured with a full API path or special format. It may not work properly after disabling the proxy. Consider switching to a base URL configuration or keep the proxy enabled.",
"openAIChatFormatWarningOnDisable": "The current provider uses OpenAI Chat format. It may not work properly after disabling the proxy. Consider keeping the proxy enabled.",
"urlMismatchWarningOnDisable": "The current provider's request URL configuration may rely on the proxy. It may not work properly after disabling the proxy. Consider switching to a base URL configuration or keep the proxy enabled.",
"switchAppliedUnverified": "Switch applied (direct mode not verified)",
"switchAppliedUnverifiedDesc": "Unable to verify whether this endpoint requires the proxy. If it doesn't work after switching, enable the proxy and takeover the current app.",
"openLinkFailed": "Failed to open link",
"openclawModelsRegistered": "Models have been registered to /model list",
"openclawDefaultModelSet": "Set as default model",
@@ -294,8 +283,6 @@
"passwordPlaceholder": "App password",
"remoteRoot": "Remote Root Directory",
"profile": "Sync Profile Name",
"autoSync": "Auto Sync",
"autoSyncHint": "When enabled, each database change triggers an automatic WebDAV upload.",
"test": "Test Connection",
"testing": "Testing...",
"testSuccess": "Connection successful",
@@ -307,14 +294,11 @@
"uploading": "Uploading...",
"uploadSuccess": "Uploaded to WebDAV",
"uploadFailed": "Upload failed: {{error}}",
"autoSyncFailedToast": "Auto sync failed: {{error}}",
"download": "Download from Cloud",
"downloading": "Downloading...",
"downloadSuccess": "Downloaded and restored from WebDAV",
"downloadFailed": "Download failed: {{error}}",
"lastSync": "Last sync: {{time}}",
"autoSyncLastErrorTitle": "Last auto sync failed",
"autoSyncLastErrorHint": "Please check network or WebDAV settings. Auto sync will retry on future changes.",
"missingUrl": "Please enter the WebDAV server URL",
"presets": {
"label": "Provider",
@@ -602,12 +586,6 @@
"apiHint": "💡 Fill in Claude API compatible service endpoint, avoid trailing slash",
"apiHintOAI": "💡 Fill in OpenAI Chat Completions compatible service endpoint, avoid trailing slash",
"codexApiHint": "💡 Fill in service endpoint compatible with OpenAI Response format",
"directRequestUrl": "CLI Direct Request URL:",
"directRequestUrlDesc": "Actual request URL after CLI appends default suffix",
"proxyRequestUrl": "CCS Proxy Request URL:",
"proxyRequestUrlDesc": "URL forwarded to upstream after CCS smart path detection",
"fullUrlWarningTitle": "Full API path detected",
"fullUrlWarning": "The URL contains a full API path. This configuration only works in proxy mode. For direct connection mode, please enter only the base URL.",
"fillSupplierName": "Please fill in provider name",
"fillConfigContent": "Please fill in configuration content",
"fillParameter": "Please fill in {{label}}",
@@ -1502,10 +1480,6 @@
}
},
"proxy": {
"takeover": {
"toggleFailed": "Failed to toggle takeover",
"toggleFailedDesc": "Check proxy service status and permissions, then try again."
},
"panel": {
"serviceAddress": "Service Address",
"addressCopied": "Address copied",
+1 -27
View File
@@ -58,10 +58,7 @@
"extractFromCurrent": "編集内容から抽出",
"extractNoCommonConfig": "編集内容から抽出できる共通設定がありません",
"extractFailed": "抽出に失敗しました: {{error}}",
"saveFailed": "保存に失敗しました: {{error}}",
"hideAttribution": "AI署名を非表示",
"alwaysThinking": "拡張思考",
"enableTeammates": "Teammates モード"
"saveFailed": "保存に失敗しました: {{error}}"
},
"header": {
"viewOnGithub": "GitHub で見る",
@@ -167,14 +164,6 @@
"settingsSaved": "設定を保存しました",
"settingsSaveFailed": "設定の保存に失敗しました: {{error}}",
"openAIChatFormatHint": "このプロバイダーは OpenAI Chat フォーマットを使用しており、プロキシサービスの有効化が必要です",
"openAIChatFormatRequiresProxy": "このプロバイダーは OpenAI Chat フォーマットを使用しており、フォーマット変換のためにプロキシサービスが必要です。先にプロキシを有効にして、現在のアプリをテイクオーバーしてください。",
"fullUrlRequiresProxy": "このプロバイダーは完全な API パスで構成されています。直接接続モードではクライアントがパスを重複追加する可能性があります。先にプロキシを有効にして、現在のアプリをテイクオーバーしてください。",
"urlMismatchRequiresProxy": "このプロバイダーのリクエスト URL 設定が API フォーマットと一致しません。直接接続モードでは正常に動作しません。先にプロキシを有効にして、現在のアプリをテイクオーバーしてください。",
"fullUrlWarningOnDisable": "現在のプロバイダーは完全な API パスまたは特殊なフォーマットで構成されています。プロキシを無効にすると正常に動作しない可能性があります。ベース URL 設定に変更するか、プロキシを有効のままにすることをお勧めします。",
"openAIChatFormatWarningOnDisable": "現在のプロバイダーは OpenAI Chat フォーマットを使用しています。プロキシを無効にすると正常に動作しない可能性があります。プロキシを有効のままにすることをお勧めします。",
"urlMismatchWarningOnDisable": "現在のプロバイダーのリクエスト URL 設定はプロキシに依存している可能性があります。プロキシを無効にすると正常に動作しない可能性があります。ベース URL 設定に変更するか、プロキシを有効のままにすることをお勧めします。",
"switchAppliedUnverified": "切り替えを適用しました(直接接続は未検証)",
"switchAppliedUnverifiedDesc": "このエンドポイントがプロキシを必要とするか検証できませんでした。切り替え後に動作しない場合は、プロキシを有効にして現在のアプリをテイクオーバーしてください。",
"openLinkFailed": "リンクを開けませんでした",
"openclawModelsRegistered": "モデルが /model リストに登録されました",
"openclawDefaultModelSet": "デフォルトモデルに設定しました",
@@ -294,8 +283,6 @@
"passwordPlaceholder": "アプリパスワード",
"remoteRoot": "リモートルートディレクトリ",
"profile": "同期プロファイル名",
"autoSync": "自動同期",
"autoSyncHint": "有効にすると、データベース変更のたびに WebDAV へ自動アップロードします。",
"test": "接続テスト",
"testing": "テスト中...",
"testSuccess": "接続成功",
@@ -307,14 +294,11 @@
"uploading": "アップロード中...",
"uploadSuccess": "WebDAV にアップロードしました",
"uploadFailed": "アップロードに失敗しました:{{error}}",
"autoSyncFailedToast": "自動同期に失敗しました:{{error}}",
"download": "クラウドからダウンロード",
"downloading": "ダウンロード中...",
"downloadSuccess": "WebDAV からダウンロード・復元しました",
"downloadFailed": "ダウンロードに失敗しました:{{error}}",
"lastSync": "前回の同期:{{time}}",
"autoSyncLastErrorTitle": "前回の自動同期に失敗しました",
"autoSyncLastErrorHint": "ネットワークまたは WebDAV 設定を確認してください。次回の変更時に自動再試行されます。",
"missingUrl": "WebDAV サーバー URL を入力してください",
"presets": {
"label": "サービス",
@@ -602,12 +586,6 @@
"apiHint": "💡 Claude API 互換サービスのエンドポイントを入力してください。末尾にスラッシュを付けないでください",
"apiHintOAI": "💡 OpenAI Chat Completions 互換サービスのエンドポイントを入力してください。末尾にスラッシュを付けないでください",
"codexApiHint": "💡 OpenAI Response 互換のサービスエンドポイントを入力してください",
"directRequestUrl": "CLI 直接リクエスト URL",
"directRequestUrlDesc": "CLI がデフォルトサフィックスを追加した後の実際のリクエスト URL",
"proxyRequestUrl": "CCS プロキシリクエスト URL",
"proxyRequestUrlDesc": "CCS がスマートパス検出後にアップストリームへ転送する URL",
"fullUrlWarningTitle": "完全な API パスを検出",
"fullUrlWarning": "URL には完全な API パスが含まれています。この設定はプロキシモードでのみ有効です。直接接続モードではベース URL のみを入力してください。",
"fillSupplierName": "プロバイダー名を入力してください",
"fillConfigContent": "設定内容を入力してください",
"fillParameter": "{{label}} を入力してください",
@@ -1500,10 +1478,6 @@
}
},
"proxy": {
"takeover": {
"toggleFailed": "テイクオーバーの切り替えに失敗しました",
"toggleFailedDesc": "プロキシサービスの状態と権限を確認して、もう一度お試しください。"
},
"panel": {
"serviceAddress": "サービスアドレス",
"addressCopied": "アドレスをコピーしました",
+1 -27
View File
@@ -58,10 +58,7 @@
"extractFromCurrent": "从编辑内容提取",
"extractNoCommonConfig": "当前编辑内容没有可提取的通用配置",
"extractFailed": "提取失败: {{error}}",
"saveFailed": "保存失败: {{error}}",
"hideAttribution": "隐藏 AI 署名",
"alwaysThinking": "扩展思考",
"enableTeammates": "Teammates 模式"
"saveFailed": "保存失败: {{error}}"
},
"header": {
"viewOnGithub": "在 GitHub 上查看",
@@ -167,14 +164,6 @@
"settingsSaved": "设置已保存",
"settingsSaveFailed": "保存设置失败:{{error}}",
"openAIChatFormatHint": "此供应商使用 OpenAI Chat 格式,需要开启代理服务才能正常使用",
"openAIChatFormatRequiresProxy": "此供应商使用 OpenAI Chat 格式,需要开启代理服务进行格式转换才能正常使用。请先开启代理并接管当前应用。",
"fullUrlRequiresProxy": "此供应商配置了完整 API 路径,直连模式下客户端可能会重复追加路径。请先开启代理并接管当前应用。",
"urlMismatchRequiresProxy": "此供应商的请求地址配置与 API 格式不匹配,直连模式下无法正常工作。请先开启代理并接管当前应用。",
"fullUrlWarningOnDisable": "当前供应商配置了完整 API 路径或特殊格式,关闭代理后可能无法正常工作。建议更换为基础地址配置或保持代理开启。",
"openAIChatFormatWarningOnDisable": "当前供应商使用 OpenAI Chat 格式,关闭代理后可能无法正常工作。建议保持代理开启。",
"urlMismatchWarningOnDisable": "当前供应商的请求地址配置可能依赖代理模式,关闭代理后可能无法正常工作。建议更换为基础地址配置或保持代理开启。",
"switchAppliedUnverified": "切换已应用(未验证直连兼容性)",
"switchAppliedUnverifiedDesc": "未能验证该端点是否需要代理。如果切换后无法正常使用,请开启代理并接管当前应用。",
"openLinkFailed": "链接打开失败",
"openclawModelsRegistered": "模型已注册到 /model 列表",
"openclawDefaultModelSet": "已设为默认模型",
@@ -294,8 +283,6 @@
"passwordPlaceholder": "应用密码(坚果云请使用「第三方应用密码」)",
"remoteRoot": "远程根目录",
"profile": "同步配置名",
"autoSync": "自动同步",
"autoSyncHint": "开启后每次数据库变更都会自动上传到 WebDAV。",
"test": "测试连接",
"testing": "测试中...",
"testSuccess": "连接成功",
@@ -307,14 +294,11 @@
"uploading": "上传中...",
"uploadSuccess": "已上传到 WebDAV",
"uploadFailed": "上传失败:{{error}}",
"autoSyncFailedToast": "自动同步失败:{{error}}",
"download": "从云端下载",
"downloading": "下载中...",
"downloadSuccess": "已从 WebDAV 下载并恢复",
"downloadFailed": "下载失败:{{error}}",
"lastSync": "上次同步:{{time}}",
"autoSyncLastErrorTitle": "上次自动同步失败",
"autoSyncLastErrorHint": "请检查网络或 WebDAV 配置,系统会在后续变更时继续自动重试。",
"missingUrl": "请填写 WebDAV 服务器地址",
"presets": {
"label": "服务商",
@@ -602,12 +586,6 @@
"apiHint": "💡 填写兼容 Claude API 的服务端点地址,不要以斜杠结尾",
"apiHintOAI": "💡 填写兼容 OpenAI Chat Completions 的服务端点地址,不要以斜杠结尾",
"codexApiHint": "💡 填写兼容 OpenAI Response 格式的服务端点地址",
"directRequestUrl": "CLI 直连请求地址:",
"directRequestUrlDesc": "CLI 硬拼接默认后缀后的实际请求地址",
"proxyRequestUrl": "CCS 代理请求地址:",
"proxyRequestUrlDesc": "CCS 智能拼接后转发到上游的地址",
"fullUrlWarningTitle": "检测到完整 API 路径",
"fullUrlWarning": "填写了包含 API 路径的完整地址,此配置仅在代理模式下生效。直连模式下请只填写基础地址。",
"fillSupplierName": "请填写供应商名称",
"fillConfigContent": "请填写配置内容",
"fillParameter": "请填写 {{label}}",
@@ -1502,10 +1480,6 @@
}
},
"proxy": {
"takeover": {
"toggleFailed": "切换接管状态失败",
"toggleFailedDesc": "请检查代理服务状态与权限,然后重试。"
},
"panel": {
"serviceAddress": "服务地址",
"addressCopied": "地址已复制",
-36
View File
@@ -117,40 +117,4 @@ export const proxyApi = {
async setPricingModelSource(appType: string, value: string): Promise<void> {
return invoke("set_pricing_model_source", { appType, value });
},
// ========== URL 预览 API ==========
/**
* URL
* app_typebase_url api_format
*/
async buildUrlPreview(
appType: string,
baseUrl: string,
apiFormat?: string,
): Promise<UrlPreview> {
return invoke("build_url_preview", { appType, baseUrl, apiFormat });
},
/**
*
* null
*/
async checkProxyRequirement(
appType: string,
baseUrl: string,
apiFormat?: string,
): Promise<string | null> {
return invoke("check_proxy_requirement", { appType, baseUrl, apiFormat });
},
};
/** URL 预览结果 */
export interface UrlPreview {
/** 直连模式请求地址 */
direct_url: string;
/** 代理模式请求地址 */
proxy_url: string;
/** 是否为全链接(base_url 已包含 API 路径) */
is_full_url: boolean;
}
-2
View File
@@ -33,7 +33,6 @@ export const settingsSchema = z.object({
webdavSync: z
.object({
enabled: z.boolean().optional(),
autoSync: z.boolean().optional(),
baseUrl: z.string().trim().optional().or(z.literal("")),
username: z.string().trim().optional().or(z.literal("")),
password: z.string().optional(),
@@ -43,7 +42,6 @@ export const settingsSchema = z.object({
.object({
lastSyncAt: z.number().nullable().optional(),
lastError: z.string().nullable().optional(),
lastErrorSource: z.string().nullable().optional(),
lastRemoteEtag: z.string().nullable().optional(),
lastLocalManifestHash: z.string().nullable().optional(),
lastRemoteManifestHash: z.string().nullable().optional(),
+2 -7
View File
@@ -140,13 +140,10 @@ export interface ProviderMeta {
costMultiplier?: string;
// 供应商计费模式来源
pricingModelSource?: string;
// 供应商 API 格式
// Claude:
// Claude API 格式(仅 Claude 供应商使用)
// - "anthropic": 原生 Anthropic Messages API 格式,直接透传
// - "openai_chat": OpenAI Chat Completions 格式,需要格式转换
// Codex:
// - "responses": OpenAI Responses API
apiFormat?: "anthropic" | "openai_chat" | "responses";
apiFormat?: "anthropic" | "openai_chat";
}
// Skill 同步方式
@@ -170,7 +167,6 @@ export interface VisibleApps {
export interface WebDavSyncStatus {
lastSyncAt?: number | null;
lastError?: string | null;
lastErrorSource?: string | null;
lastRemoteEtag?: string | null;
lastLocalManifestHash?: string | null;
lastRemoteManifestHash?: string | null;
@@ -179,7 +175,6 @@ export interface WebDavSyncStatus {
// WebDAV v2 同步配置
export interface WebDavSyncSettings {
enabled?: boolean;
autoSync?: boolean;
baseUrl?: string;
username?: string;
password?: string;
-43
View File
@@ -1,43 +0,0 @@
import type { AppId } from "@/lib/api";
import type { Provider } from "@/types";
import { extractCodexBaseUrl } from "@/utils/providerConfigUtils";
/**
* provider base URL/
*/
export function extractProviderBaseUrl(
provider: Provider,
appId: AppId,
): string | null {
try {
const config = provider.settingsConfig;
if (!config) return null;
if (appId === "claude") {
const envUrl = config?.env?.ANTHROPIC_BASE_URL;
return typeof envUrl === "string" ? envUrl.trim() : null;
}
if (appId === "codex" || appId === "opencode") {
const tomlConfig = config?.config;
if (typeof tomlConfig === "string") {
return extractCodexBaseUrl(tomlConfig) ?? null;
}
const baseUrl = config?.base_url;
return typeof baseUrl === "string" ? baseUrl.trim() : null;
}
if (appId === "gemini") {
const envUrl = config?.env?.GOOGLE_GEMINI_BASE_URL;
if (typeof envUrl === "string") return envUrl.trim();
const baseUrl = config?.GEMINI_API_BASE || config?.base_url;
return typeof baseUrl === "string" ? baseUrl.trim() : null;
}
return null;
} catch {
return null;
}
}
-35
View File
@@ -468,41 +468,6 @@ export const setCodexBaseUrl = (
return `${prefix}${replacementLine}\n`;
};
// 在 Codex 的 TOML 配置文本中写入或更新 wire_api 字段(仅保留 responses
export const setCodexWireApi = (
configText: string,
wireApi: "responses" = "responses",
): string => {
const normalizedText = normalizeQuotes(configText);
const replacementLine = `wire_api = "${wireApi}"`;
const pattern = /^wire_api\s*=\s*(["'])(responses|chat)\1/m;
if (pattern.test(normalizedText)) {
return normalizedText.replace(pattern, replacementLine);
}
// 优先插入到 base_url 后,提升可读性
const baseUrlPattern = /^base_url\s*=\s*["'][^"']+["']/m;
const match = normalizedText.match(baseUrlPattern);
if (match && match.index !== undefined) {
const endOfLine = normalizedText.indexOf("\n", match.index);
if (endOfLine !== -1) {
return (
normalizedText.slice(0, endOfLine + 1) +
replacementLine +
"\n" +
normalizedText.slice(endOfLine + 1)
);
}
}
const prefix =
normalizedText && !normalizedText.endsWith("\n")
? `${normalizedText}\n`
: normalizedText;
return `${prefix}${replacementLine}\n`;
};
// ========== Codex model name utils ==========
// 从 Codex 的 TOML 配置文本中提取 model 字段(支持单/双引号)
@@ -34,17 +34,6 @@ vi.mock("@/components/ui/input", () => ({
Input: (props: any) => <input {...props} />,
}));
vi.mock("@/components/ui/switch", () => ({
Switch: ({ checked, onCheckedChange, ...props }: any) => (
<button
role="switch"
aria-checked={checked}
onClick={() => onCheckedChange?.(!checked)}
{...props}
/>
),
}));
vi.mock("@/components/ui/select", () => ({
Select: ({ value, onValueChange, children }: any) => (
<select
@@ -93,7 +82,6 @@ const baseConfig: WebDavSyncSettings = {
password: "secret",
remoteRoot: "cc-switch-sync",
profile: "default",
autoSync: false,
status: {},
};
@@ -140,49 +128,6 @@ describe("WebdavSyncSection", () => {
settingsApiMock.webdavSyncDownload.mockResolvedValue({ status: "downloaded" });
});
it("shows auto sync error callout when last auto sync failed", () => {
renderSection({
...baseConfig,
status: {
lastError: "network timeout",
lastErrorSource: "auto",
},
});
expect(
screen.getByText("settings.webdavSync.autoSyncLastErrorTitle"),
).toBeInTheDocument();
expect(screen.getByText("network timeout")).toBeInTheDocument();
});
it("does not show auto sync error callout for manual sync errors", () => {
renderSection({
...baseConfig,
status: {
lastError: "manual upload failed",
lastErrorSource: "manual",
},
});
expect(
screen.queryByText("settings.webdavSync.autoSyncLastErrorTitle"),
).not.toBeInTheDocument();
});
it("does not show auto sync error callout when source is missing", () => {
renderSection({
...baseConfig,
autoSync: true,
status: {
lastError: "legacy error without source",
},
});
expect(
screen.queryByText("settings.webdavSync.autoSyncLastErrorTitle"),
).not.toBeInTheDocument();
});
it("shows validation error when saving without base url", async () => {
renderSection({ ...baseConfig, baseUrl: "" });
@@ -205,7 +150,6 @@ describe("WebdavSyncSection", () => {
baseUrl: "https://dav.example.com/dav/",
username: "alice",
password: "secret",
autoSync: false,
}),
false,
);
@@ -222,24 +166,6 @@ describe("WebdavSyncSection", () => {
);
});
it("saves auto sync as true after toggle", async () => {
renderSection(baseConfig);
fireEvent.click(
screen.getByRole("switch", { name: "settings.webdavSync.autoSync" }),
);
fireEvent.click(screen.getByRole("button", { name: "settings.webdavSync.save" }));
await waitFor(() => {
expect(settingsApiMock.webdavSyncSaveSettings).toHaveBeenCalledWith(
expect.objectContaining({
autoSync: true,
}),
false,
);
});
});
it("blocks upload when there are unsaved changes", async () => {
renderSection(baseConfig);
-21
View File
@@ -209,25 +209,4 @@ describe("App integration with MSW", () => {
expect(toastErrorMock).not.toHaveBeenCalled();
expect(toastSuccessMock).toHaveBeenCalled();
});
it("shows toast when auto sync fails in background", async () => {
const { default: App } = await import("@/App");
renderApp(App);
await waitFor(() =>
expect(screen.getByTestId("provider-list").textContent).toContain(
"claude-1",
),
);
emitTauriEvent("webdav-sync-status-updated", {
source: "auto",
status: "error",
error: "network timeout",
});
await waitFor(() => {
expect(toastErrorMock).toHaveBeenCalled();
});
});
});