Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(desktop): confirm before adding accounts

xd-bobo6fd9ce816bc1
diff --git a/apps/desktop/src/renderer/components/sidebar/AccountSwitcherDialog.tsx b/apps/desktop/src/renderer/components/sidebar/AccountSwitcherDialog.tsx
index 6eb2d2245f..57d925afcf 100644
--- a/apps/desktop/src/renderer/components/sidebar/AccountSwitcherDialog.tsx
+++ b/apps/desktop/src/renderer/components/sidebar/AccountSwitcherDialog.tsx
@@ -59,6 +59,7 @@ export function AccountSwitcherDialog({
const [mutationAllowed, setMutationAllowed] = useState(true);
const [syncing, setSyncing] = useState(false);
const [switchingKey, setSwitchingKey] = useState<string | null>(null);
+ const [addingAccount, setAddingAccount] = useState(false);
const contentRef = useRef<HTMLDivElement>(null);
useEffect(() => {
@@ -90,30 +91,32 @@ export function AccountSwitcherDialog({
};
}, [listAccounts, open, syncAccounts, t]);
+ const confirmRunningTaskInterruption = async (): Promise<boolean> => {
+ // Lazy-load the task store only when the user requests an account boundary
+ // change. The dialog is mounted with the sidebar shell, while chat runtime is not.
+ const { makerChatStore } = await import('@/lib/makerChatStore');
+ const hasRunningTask = [...makerChatStore.getRunningSnapshot().values()].some(
+ (status) => status.isRunning,
+ );
+ if (!hasRunningTask) return true;
+ // Full app windows always provide the shared confirmation host. Standalone
+ // story/test mounts fail closed instead of changing accounts without consent.
+ if (!confirmDialog) return false;
+ return confirmDialog.confirm({
+ title: t('sidebar.accountSwitcher.runningTaskTitle'),
+ description: t('sidebar.accountSwitcher.runningTaskDescription'),
+ confirmText: t('sidebar.accountSwitcher.runningTaskConfirm'),
+ cancelText: t('logic.confirm.cancel'),
+ confirmVariant: 'destructive',
+ });
+ };
+
const handleSwitch = async (account: DesktopSavedAccount) => {
- if (account.isCurrent || switchingKey || !mutationAllowed) return;
+ if (account.isCurrent || switchingKey || addingAccount || !mutationAllowed) return;
setSwitchingKey(account.accountKey);
try {
- // Lazy-load the task store only when the user requests a switch. The account
- // dialog is mounted with the sidebar shell, while the chat runtime is not.
- const { makerChatStore } = await import('@/lib/makerChatStore');
- const hasRunningTask = [...makerChatStore.getRunningSnapshot().values()].some(
- (status) => status.isRunning,
- );
- if (hasRunningTask) {
- // Full app windows always provide the shared confirmation host. Standalone
- // story/test mounts fail closed instead of switching without consent.
- if (!confirmDialog) return;
- const confirmed = await confirmDialog.confirm({
- title: t('sidebar.accountSwitcher.runningTaskTitle'),
- description: t('sidebar.accountSwitcher.runningTaskDescription'),
- confirmText: t('sidebar.accountSwitcher.runningTaskConfirm'),
- cancelText: t('logic.confirm.cancel'),
- confirmVariant: 'destructive',
- });
- if (!confirmed) return;
- }
+ if (!(await confirmRunningTaskInterruption())) return;
// Main owns the account boundary: it stops and drains the outgoing runtime
// before committing the selected account, so this await is also the stop barrier.
@@ -126,6 +129,19 @@ export function AccountSwitcherDialog({
}
};
+ const handleAddAccount = async () => {
+ if (switchingKey || addingAccount || !mutationAllowed) return;
+ setAddingAccount(true);
+ try {
+ if (!(await confirmRunningTaskInterruption())) return;
+ onAddAccount();
+ } catch {
+ toast.error(t('sidebar.accountSwitcher.switchFailed'));
+ } finally {
+ setAddingAccount(false);
+ }
+ };
+
return (
<Dialog.Root open={open} onOpenChange={onOpenChange}>
<Dialog.Portal>
@@ -187,7 +203,12 @@ export function AccountSwitcherDialog({
<button
key={account.accountKey}
type="button"
- disabled={account.isCurrent || switchingKey !== null || !mutationAllowed}
+ disabled={
+ account.isCurrent ||
+ switchingKey !== null ||
+ addingAccount ||
+ !mutationAllowed
+ }
onClick={() => void handleSwitch(account)}
className={cn(
'flex w-full items-center gap-3 rounded-lg px-3 py-2.5 text-left transition-colors',
@@ -228,8 +249,8 @@ export function AccountSwitcherDialog({
<div className="mt-4 border-t border-[var(--border-default)] pt-4">
<button
type="button"
- disabled={!mutationAllowed || switchingKey !== null}
- onClick={onAddAccount}
+ disabled={!mutationAllowed || switchingKey !== null || addingAccount}
+ onClick={() => void handleAddAccount()}
className="flex w-full items-center justify-center gap-2 rounded-lg border border-[var(--border-default)] bg-[var(--surface-elevated)] px-4 py-2.5 text-14 font-medium text-[var(--text-primary)] transition-colors hover:bg-[var(--surface-hover)] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--focus-ring)] disabled:cursor-not-allowed disabled:opacity-60"
>
<Plus className="h-4 w-4" aria-hidden="true" />
diff --git a/apps/desktop/src/renderer/components/sidebar/__tests__/AccountSwitcherDialog.test.tsx b/apps/desktop/src/renderer/components/sidebar/__tests__/AccountSwitcherDialog.test.tsx
index 8ca372f779..910e1b755e 100644
--- a/apps/desktop/src/renderer/components/sidebar/__tests__/AccountSwitcherDialog.test.tsx
+++ b/apps/desktop/src/renderer/components/sidebar/__tests__/AccountSwitcherDialog.test.tsx
@@ -125,7 +125,7 @@ describe('AccountSwitcherDialog', () => {
await waitFor(() => expect(syncAccounts).toHaveBeenCalledOnce());
fireEvent.click(screen.getByRole('button', { name: 'sidebar.accountSwitcher.addAccount' }));
- expect(onAddAccount).toHaveBeenCalledOnce();
+ await waitFor(() => expect(onAddAccount).toHaveBeenCalledOnce());
});
it('keeps switching and add-account actions available during background sync', async () => {
@@ -150,7 +150,52 @@ describe('AccountSwitcherDialog', () => {
expect((addButton as HTMLButtonElement).disabled).toBe(false);
fireEvent.click(addButton);
- expect(onAddAccount).toHaveBeenCalledOnce();
+ await waitFor(() => expect(onAddAccount).toHaveBeenCalledOnce());
+ });
+
+ it('asks before adding an account when a task is running and stays put on cancel', async () => {
+ const onAddAccount = vi.fn();
+ runningSnapshot.set('running-session', { isRunning: true });
+ confirm.mockResolvedValue(false);
+ render(
+ <AccountSwitcherDialog
+ open
+ onOpenChange={vi.fn()}
+ onAddAccount={onAddAccount}
+ triggerRef={createRef<HTMLButtonElement>()}
+ />,
+ );
+
+ fireEvent.click(
+ await screen.findByRole('button', {
+ name: 'sidebar.accountSwitcher.addAccount',
+ }),
+ );
+
+ await waitFor(() => expect(confirm).toHaveBeenCalledOnce());
+ expect(onAddAccount).not.toHaveBeenCalled();
+ });
+
+ it('starts adding an account only after confirming interruption of running tasks', async () => {
+ const onAddAccount = vi.fn();
+ runningSnapshot.set('running-session', { isRunning: true });
+ render(
+ <AccountSwitcherDialog
+ open
+ onOpenChange={vi.fn()}
+ onAddAccount={onAddAccount}
+ triggerRef={createRef<HTMLButtonElement>()}
+ />,
+ );
+
+ fireEvent.click(
+ await screen.findByRole('button', {
+ name: 'sidebar.accountSwitcher.addAccount',
+ }),
+ );
+
+ await waitFor(() => expect(confirm).toHaveBeenCalledOnce());
+ await waitFor(() => expect(onAddAccount).toHaveBeenCalledOnce());
});
it('asks before switching when a task is running and keeps the account dialog open on cancel', async () => {