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(bots): 开始跟进确认防连点,账号切换后丢弃旧确认结果

zqchrisc27e75c3fa80
diff --git a/apps/desktop/src/renderer/features/bots/BotFollowUpInvitation.tsx b/apps/desktop/src/renderer/features/bots/BotFollowUpInvitation.tsx
index 84df2bbde0..fccc0f0396 100644
--- a/apps/desktop/src/renderer/features/bots/BotFollowUpInvitation.tsx
+++ b/apps/desktop/src/renderer/features/bots/BotFollowUpInvitation.tsx
@@ -1,4 +1,4 @@
-import { useRef, useState } from 'react';
+import { useEffect, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { Button } from '@/components/ui/button';
@@ -41,15 +41,30 @@ export function BotFollowUpInvitationCard({ pending, onChoose, consent }: {
const { confirm } = useConfirmDialog();
// 卡片只占一行;读什么、会用模型额度在开启前的确认框里完整说明(与设置页同一确认),确认后才保存。
const scope = consent ?? t('bots.autonomy.consent');
+ // 确认框在账号切换后仍可能开着(它挂在账号层之上),回来的结果只认发起时的账号和仍在的卡片;
+ // 连点只排一个确认框。
+ const asking = useRef(false);
+ const mounted = useRef(true);
+ useEffect(() => {
+ mounted.current = true;
+ return () => { mounted.current = false; };
+ }, []);
const start = async () => {
- const accepted = await confirm({
- presentation: 'standard',
- title: t('bots.autonomy.invitation.title'),
- description: scope,
- confirmText: t('bots.autonomy.invitation.start'),
- cancelText: t('bots.autonomy.invitation.notNow'),
- });
- if (accepted) await onChoose(true);
+ if (asking.current || pending) return;
+ asking.current = true;
+ const owner = getDataOwnerGeneration();
+ try {
+ const accepted = await confirm({
+ presentation: 'standard',
+ title: t('bots.autonomy.invitation.title'),
+ description: scope,
+ confirmText: t('bots.autonomy.invitation.start'),
+ cancelText: t('bots.autonomy.invitation.notNow'),
+ });
+ if (accepted && mounted.current && isDataOwnerGenerationCurrent(owner)) await onChoose(true);
+ } finally {
+ asking.current = false;
+ }
};
return (
<section
diff --git a/apps/desktop/src/renderer/features/bots/__tests__/botAutonomySettings.test.tsx b/apps/desktop/src/renderer/features/bots/__tests__/botAutonomySettings.test.tsx
index d9e953aac0..11af99ae10 100644
--- a/apps/desktop/src/renderer/features/bots/__tests__/botAutonomySettings.test.tsx
+++ b/apps/desktop/src/renderer/features/bots/__tests__/botAutonomySettings.test.tsx
@@ -29,6 +29,7 @@ vi.mock('../botStore', async (importOriginal) => ({
],
}));
+import { __testing as dataOwnerTesting, setDataOwnerGeneration } from '@/contexts/dataOwnerGeneration';
import { BotAutonomySettings } from '../BotAutonomySettings';
import { BotFollowUpInvitation } from '../BotFollowUpInvitation';
import { RemoteBotFollowUpInvitation } from '../RemoteBotFollowUpInvitation';
@@ -168,6 +169,20 @@ it('keeps the invitation and saves nothing when the confirmation is cancelled',
expect(screen.getByRole('region')).toBeTruthy();
});
+it('asks once for a double click and drops a confirmation that returns after the account changed', async () => {
+ render(<BotFollowUpInvitation botId="cindy-default" engaged />);
+ let answer!: (value: boolean) => void;
+ m.confirm.mockImplementationOnce(() => new Promise<boolean>((done) => { answer = done; }));
+ const button = await screen.findByRole('button', { name: 'bots.autonomy.invitation.start' });
+ fireEvent.click(button);
+ fireEvent.click(button);
+ expect(m.confirm).toHaveBeenCalledOnce();
+ setDataOwnerGeneration('account-b');
+ await act(async () => answer(true));
+ expect(host.setSettings).not.toHaveBeenCalled();
+ dataOwnerTesting.reset();
+});
+
it('keeps the invitation available when saving fails', async () => {
render(<BotFollowUpInvitation botId="cindy-default" engaged />);
const button = await screen.findByRole('button', { name: 'bots.autonomy.invitation.start' });