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
This commit is contained in:
@@ -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::<toml::Table>(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)
|
||||
}
|
||||
|
||||
|
||||
@@ -94,7 +94,6 @@ const ProviderList: React.FC<ProviderListProps> = ({
|
||||
<button
|
||||
className="edit-btn"
|
||||
onClick={() => onEdit(provider.id)}
|
||||
disabled={isCurrent}
|
||||
>
|
||||
编辑
|
||||
</button>
|
||||
|
||||
Reference in New Issue
Block a user