A cross-platform desktop All-in-One assistant for Claude Code, Codex, OpenCode,DeepSeek Harness and more. Only official website: ccswitch.io
Rust ai-toolsclaude-codecodexdeepseek-harnessdesktop-appgrokgrokbuildhermeskimi-codemcpopen-sourceopencodepiprovider-managementrustskillsskills-managementtauriwsl-support
fix(agy): sync the active provider when switching accounts (#10312)
Allen Xu867a7f7c64d4
diff --git a/src-tauri/src/commands/auth.rs b/src-tauri/src/commands/auth.rsindex f90780036..3038a3566 100644--- a/src-tauri/src/commands/auth.rs+++ b/src-tauri/src/commands/auth.rs@@ -17,6 +17,7 @@ use crate::proxy::providers::xai_oauth_auth::{XaiOAuthAccount, XaiOAuthError};use crate::services::agy_account::AgyAccount;use crate::services::claude_account::ClaudeAccount;use crate::services::codebuddy_account::CodebuddyAccount;+use crate::services::provider::agy_direct;use crate::services::workbuddy_account::WorkBuddyAccount;use crate::store::AppState;@@ -817,6 +818,7 @@ pub async fn auth_set_default_account(app: tauri::AppHandle,auth_provider: String,account_id: String,+ app_state: State<'_, AppState>,copilot_state: State<'_, CopilotAuthState>,codex_state: State<'_, CodexOAuthState>,xai_state: State<'_, XaiOAuthState>,@@ -848,12 +850,21 @@ pub async fn auth_set_default_account(.await.map_err(|e| e.to_string())}- // 切 agy 的全局登录(写钥匙串)- AUTH_PROVIDER_AGY_OAUTH => agy_state- .0- .switch_to(&account_id)+ AUTH_PROVIDER_AGY_OAUTH => {+ let state = app_state.inner().clone();+ let accounts = agy_state.0.clone();+ let handle = app.clone();+ tauri::async_runtime::spawn_blocking(move || {+ let provider_changed = agy_direct::switch_account(&state, &accounts, &account_id)+ .map_err(|e| e.to_string())?;+ if provider_changed {+ crate::tray::refresh_tray_menu(&handle);+ }+ Ok(())+ }).await- .map_err(|e| e.to_string()),+ .map_err(|e| e.to_string())?+ }// 还原这个账号的那组凭据文件(要求 WorkBuddy 已退出)AUTH_PROVIDER_WORKBUDDY_ACCOUNT => {run_workbuddy(&workbuddy_state.0, move |manager| {diff --git a/src-tauri/src/services/agy_account/tests.rs b/src-tauri/src/services/agy_account/tests.rsindex 2eb2a4abf..77c9c6ffd 100644--- a/src-tauri/src/services/agy_account/tests.rs+++ b/src-tauri/src/services/agy_account/tests.rs@@ -93,7 +93,7 @@ mod switching {use crate::app_config::AppType;use crate::database::Database;use crate::provider::{AuthBinding, AuthBindingSource, Provider, ProviderMeta};- use crate::services::provider::agy_direct::save_from_editor;+ use crate::services::provider::agy_direct::{save_from_editor, switch_account};use crate::store::AppState;use crate::test_support::TestHome;use serial_test::serial;@@ -144,6 +144,42 @@ mod switching {assert_eq!(live_sub(&login).as_deref(), Some("alice"));}+ #[test]+ #[serial]+ fn switching_accounts_enables_the_matching_direct_provider() {+ let _home = TestHome::new();+ let (_dir, login, manager) = manager();+ let state = AppState::new(Arc::new(Database::memory().unwrap()));+ set_live(&login, &credential("alice", "a1"));+ tauri::async_runtime::block_on(manager.import_current()).unwrap();+ tauri::async_runtime::block_on(add(&manager, "bob"));+ tauri::async_runtime::block_on(add(&manager, "carol"));++ let alice = official_card("alice");+ save_from_editor(&state, &manager, alice.clone(), None).unwrap();+ let mut bob = official_card("bob");+ bob.id = "official-bob".into();+ state.db.save_provider(&AppType::Agy, &bob).unwrap();++ assert!(switch_account(&state, &manager, "bob").unwrap());+ assert_eq!(live_sub(&login).as_deref(), Some("bob"));+ let current_provider = || {+ crate::mode::current::direct_provider(&state.db, &AppType::Agy)+ .unwrap()+ .unwrap()+ .id+ };+ assert_eq!(current_provider(), bob.id);++ assert!(!switch_account(&state, &manager, "carol").unwrap());+ assert_eq!(live_sub(&login).as_deref(), Some("carol"));+ assert_eq!(current_provider(), bob.id);++ assert!(switch_account(&state, &manager, "alice").unwrap());+ assert_eq!(live_sub(&login).as_deref(), Some("alice"));+ assert_eq!(current_provider(), alice.id);+ }+#[test]#[serial]fn a_missing_bound_account_leaves_the_login_and_provider_unchanged() {diff --git a/src-tauri/src/services/provider/agy_direct.rs b/src-tauri/src/services/provider/agy_direct.rsindex 279861698..f6af09285 100644--- a/src-tauri/src/services/provider/agy_direct.rs+++ b/src-tauri/src/services/provider/agy_direct.rs@@ -229,6 +229,37 @@ pub(crate) fn switch_with_login(switch(state, Some(accounts), id)}+/// 授权中心切号时,若当前直连官方卡绑了另一个号,就启用目标账号对应的官方卡。+pub(crate) fn switch_account(+ state: &AppState,+ accounts: &AgyAccountManager,+ account_id: &str,+) -> Result<bool, AppError> {+ let app = AppType::Agy;+ let _guard = lock_native_provider_operations(state, &app);+ crate::mode::controller::settle_locked(state, &app)?;+ if !crate::mode::current::is_proxy(&app) {+ let current_binding = crate::mode::current::direct_provider(&state.db, &app)?+ .as_ref()+ .and_then(bound_account_id);+ if current_binding.is_some_and(|id| id != account_id) {+ let providers = state+ .db+ .get_provider_rows(&app)?+ .into_readable("切换 Antigravity 登录");+ if let Some(target) = providers+ .values()+ .find(|provider| bound_account_id(provider).as_deref() == Some(account_id))+ {+ switch(state, Some(accounts), &target.id)?;+ return Ok(true);+ }+ }+ }+ tauri::async_runtime::block_on(accounts.switch_to(account_id))?;+ Ok(false)+}+/// 这张卡能不能当路由目标:官方登录、没填 Key、用了自定义 CA 证书的都只能直连(代理连上游/// 不带用户的 CA;拒绝的原因见 `mode::controller::agy_contract`)。托盘据此把卡置灰。pub(crate) fn can_route(provider: &Provider) -> bool {diff --git a/src/components/providers/useAgyOfficial.ts b/src/components/providers/useAgyOfficial.tsindex a8569030f..14f67aa23 100644--- a/src/components/providers/useAgyOfficial.ts+++ b/src/components/providers/useAgyOfficial.ts@@ -48,7 +48,10 @@ export function useAgyAccountSwitch() {} finally {busy.current = false;if (attemptedSwitch) {- await invalidateAgyLoginCaches(queryClient);+ await Promise.all([+ queryClient.invalidateQueries({ queryKey: ["providers", "agy"] }),+ invalidateAgyLoginCaches(queryClient),+ ]);}}},diff --git a/tests/components/AgyOAuthSection.test.tsx b/tests/components/AgyOAuthSection.test.tsxindex 676fbe6d5..efb1e0426 100644--- a/tests/components/AgyOAuthSection.test.tsx+++ b/tests/components/AgyOAuthSection.test.tsx@@ -128,7 +128,10 @@ describe("AgyOAuthSection", () => {];auth.defaultAccountId = "a";mocks.useManagedAuth.mockReturnValue(auth);- renderSection();+ const client = createTestQueryClient();+ const providersKey = ["providers", "agy"];+ client.setQueryData(providersKey, { currentProviderId: "provider-a" });+ renderSection(client);expect(mocks.useManagedAuth).toHaveBeenCalledWith("agy_oauth");const current = screen.getByText("[email protected]").parentElement!;@@ -148,6 +151,7 @@ describe("AgyOAuthSection", () => {"agy_oauth","b",);+ expect(client.getQueryState(providersKey)?.isInvalidated).toBe(true);});expect(mocks.toastSuccess).toHaveBeenCalledTimes(1);});