From a858596fa2e8f6e54f94c9473c441949fa611ff7 Mon Sep 17 00:00:00 2001 From: Jason Date: Thu, 4 Sep 2025 16:34:47 +0800 Subject: [PATCH] feat(edit-current): allow editing current provider and sync to live on save\n\n- Enable Edit button for current provider in UI\n- On update_provider, if updating current provider, write changes to live config (Claude/Codex)\n- Maintain SSOT in cc-switch/config.json with atomic writes --- src-tauri/src/commands.rs | 43 +++++++++++++++++++++++++++++++-- src/components/ProviderList.tsx | 1 - 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index ec25458..b9f5569 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -85,13 +85,52 @@ pub async fn add_provider( // 根据应用类型保存配置文件 // 不再写入供应商副本文件,仅更新内存配置(SSOT) - - manager.providers.insert(provider.id.clone(), provider); + let is_current = manager.current == provider.id; + manager.providers.insert(provider.id.clone(), provider.clone()); // 保存配置 drop(config); // 释放锁 state.save()?; + // 若更新的是当前供应商,则同步写入 live 主配置 + if is_current { + match app_type { + AppType::Claude => { + let settings_path = crate::config::get_claude_settings_path(); + crate::config::write_json_file(&settings_path, &provider.settings_config)?; + } + AppType::Codex => { + let auth_path = crate::codex_config::get_codex_auth_path(); + let config_path = crate::codex_config::get_codex_config_path(); + if let Some(parent) = auth_path.parent() { + std::fs::create_dir_all(parent) + .map_err(|e| format!("创建 Codex 目录失败: {}", e))?; + } + let auth = provider + .settings_config + .get("auth") + .ok_or_else(|| "目标供应商缺少 auth 配置".to_string())?; + crate::config::write_json_file(&auth_path, auth)?; + if let Some(cfg) = provider.settings_config.get("config") { + if let Some(cfg_str) = cfg.as_str() { + if !cfg_str.trim().is_empty() { + toml::from_str::(cfg_str) + .map_err(|e| format!("config.toml 格式错误: {}", e))?; + } + crate::config::write_text_file(&config_path, cfg_str) + .map_err(|e| format!("写入 config.toml 失败: {}", e))?; + } else { + crate::config::write_text_file(&config_path, "") + .map_err(|e| format!("写入空的 config.toml 失败: {}", e))?; + } + } else { + crate::config::write_text_file(&config_path, "") + .map_err(|e| format!("写入空的 config.toml 失败: {}", e))?; + } + } + } + } + Ok(true) } diff --git a/src/components/ProviderList.tsx b/src/components/ProviderList.tsx index 2a76ebf..09c60f4 100644 --- a/src/components/ProviderList.tsx +++ b/src/components/ProviderList.tsx @@ -94,7 +94,6 @@ const ProviderList: React.FC = ({