mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-08-03 11:01:22 +08:00
feat(profiles): autosave previous profile state on switch
When switching to a different project, the current configuration of the previously active project is automatically captured into that project's snapshot before the target project's snapshot is applied. Scope is limited to the active group (Claude/Codex); autosave failures are collected as warnings and do not block the switch. - Move autosave logic into ProfileService::apply for consistent behavior across UI, tray, and tests - Add integration test covering bidirectional autosave roundtrip Refs: profile switch B方案
This commit is contained in:
@@ -311,6 +311,12 @@ impl ProfileService {
|
||||
/// 只作用于发起页所属分组内的应用,不碰其他分组的配置与 current 标记。
|
||||
/// 该分组从未拍过快照时不改动任何配置,仅标记 current 并返回提示
|
||||
/// (用户可先绑定项目、再"以当前状态更新"补拍该侧快照)。
|
||||
///
|
||||
/// **切换前会自动保存旧项目**:若当前分组已绑定到另一个项目,先把当前
|
||||
/// 状态写入那个旧项目(仅当前分组槽位),再加载目标项目。这样切走后
|
||||
/// 旧项目仍保留离开时的配置,回来时状态一致。自动保存失败时作为 warning
|
||||
/// 继续,不阻塞切换。
|
||||
///
|
||||
/// 顺序不可换:供应商切换(switch_normal 内部会按 DB 当前标志跑 MCP
|
||||
/// sync_all_enabled)必须先于 MCP diff,否则 profile 的 MCP 目标态会被冲掉。
|
||||
pub fn apply(
|
||||
@@ -318,6 +324,19 @@ impl ProfileService {
|
||||
profile_id: &str,
|
||||
scope: ProfileScope,
|
||||
) -> Result<Vec<String>, AppError> {
|
||||
let mut warnings = Vec::new();
|
||||
|
||||
// 自动保存旧项目当前状态(仅当前分组),失败不阻塞切换
|
||||
if let Some(current_id) = state.db.get_current_profile_id(scope.as_str())? {
|
||||
if current_id != profile_id {
|
||||
if let Err(e) = Self::update(state, ¤t_id, None, true, Some(scope)) {
|
||||
warnings.push(format!(
|
||||
"autosave profile '{current_id}' before switch failed: {e}"
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let profile = state
|
||||
.db
|
||||
.get_profile(profile_id)?
|
||||
|
||||
Reference in New Issue
Block a user