Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
CR14: 到达提示的去重在 dismiss 与刷新完成后结束
nanae6b662bc107a
diff --git a/apps/desktop/src/renderer/features/bots/__tests__/useCompanionArrivalNotice.test.tsx b/apps/desktop/src/renderer/features/bots/__tests__/useCompanionArrivalNotice.test.tsxindex 920208ede1..a28d58bcdb 100644--- a/apps/desktop/src/renderer/features/bots/__tests__/useCompanionArrivalNotice.test.tsx+++ b/apps/desktop/src/renderer/features/bots/__tests__/useCompanionArrivalNotice.test.tsx@@ -1,50 +1,78 @@// @vitest-environment jsdom-import { renderHook } from '@testing-library/react';+import { act, renderHook } from '@testing-library/react';import { beforeEach, describe, expect, it, vi } from 'vitest';+import { __testing, setDataOwnerGeneration } from '@/contexts/dataOwnerGeneration';const h = vi.hoisted(() => ({notices: undefined as string[] | undefined,toast: vi.fn(),- dismiss: vi.fn(async (_botId: string) => undefined),- refresh: vi.fn(),+ dismiss: vi.fn((_botId: string): Promise<void> => Promise.resolve()),+ refresh: vi.fn(async () => undefined),}));vi.mock('react-i18next', () => ({ useTranslation: () => ({ t: (key: string) => key }) }));vi.mock('@/lib/toast', () => ({ toast: { info: h.toast } }));vi.mock('../companionCloudStore', () => ({useCompanionCloud: () => ({ actors: [{ externalId: 'mia', arrivalNotices: h.notices }] }),companionCloudApi: () => ({ dismissCompanionArrivalNotices: h.dismiss }),- refreshCompanionCloud: h.refresh,+ refreshCompanionCloudSettled: h.refresh,}));import { useCompanionArrivalNotice } from '../useCompanionArrivalNotice';+const deferred = () => {+ let resolve!: () => void; let reject!: (error: Error) => void;+ const promise = new Promise<void>((ok, fail) => { resolve = ok; reject = fail; });+ return { promise, resolve, reject };+};+const settle = () => act(async () => { await new Promise(done => setTimeout(done, 0)); });+describe('useCompanionArrivalNotice', () => {beforeEach(() => {- h.toast.mockClear(); h.dismiss.mockClear(); h.refresh.mockClear();- h.notices = undefined;+ h.toast.mockClear(); h.dismiss.mockReset().mockResolvedValue(undefined); h.refresh.mockClear();+ h.notices = ['permissions'];+ __testing.reset();});- it('shows a notice once and dismisses it, and shows and dismisses a later arrival of the same companion again', () => {- const view = renderHook(() => useCompanionArrivalNotice('mia', 'Mia'));- h.notices = ['permissions'];- view.rerender();+ it('does not repeat a notice while its dismissal is still being read back, then shows the next arrival of the same notice again even when the chat was closed meanwhile', async () => {+ const pending = deferred();+ h.dismiss.mockReturnValueOnce(pending.promise);+ const first = renderHook(() => useCompanionArrivalNotice('mia', 'Mia'));expect(h.toast).toHaveBeenCalledTimes(1);expect(h.dismiss).toHaveBeenCalledTimes(1);- // The snapshot has not been read back yet: a re-render does not repeat it.- view.rerender();+ // A stale snapshot comes round again before main's dismissal is read back.+ first.rerender();expect(h.toast).toHaveBeenCalledTimes(1);- expect(h.dismiss).toHaveBeenCalledTimes(1);- // Main dismissed it; the same companion arrives again in this window.- h.notices = [];- view.rerender();- h.notices = ['permissions'];- view.rerender();+ // The chat page closes, the dismissal lands and the snapshot is read back (now empty).+ first.unmount();+ pending.resolve();+ await settle();+ expect(h.refresh).toHaveBeenCalledTimes(1);+ // The same companion arrives again with the same notice: new toast, new dismissal.+ renderHook(() => useCompanionArrivalNotice('mia', 'Mia'));expect(h.toast).toHaveBeenCalledTimes(2);expect(h.dismiss).toHaveBeenCalledTimes(2);- // A different set of notices (arrival that adds the model line) is a new batch too.- h.notices = ['permissions', 'model'];- view.rerender();- expect(h.toast).toHaveBeenCalledTimes(4);- expect(h.dismiss).toHaveBeenCalledTimes(3);+ });++ it('ends the dedupe when the dismissal fails, so a later arrival can show it again', async () => {+ const pending = deferred();+ h.dismiss.mockReturnValueOnce(pending.promise);+ const view = renderHook(() => useCompanionArrivalNotice('mia', 'Mia'));+ pending.reject(new Error('IPC'));+ await settle();+ expect(h.refresh).not.toHaveBeenCalled();+ view.unmount();+ renderHook(() => useCompanionArrivalNotice('mia', 'Mia'));+ expect(h.toast).toHaveBeenCalledTimes(2);+ });++ it('does not carry what was shown for one account over to the next', () => {+ h.dismiss.mockReturnValue(new Promise<void>(() => undefined));+ setDataOwnerGeneration('owner-a');+ const view = renderHook(() => useCompanionArrivalNotice('mia', 'Mia'));+ expect(h.toast).toHaveBeenCalledTimes(1);+ view.unmount();+ setDataOwnerGeneration('owner-b');+ renderHook(() => useCompanionArrivalNotice('mia', 'Mia'));+ expect(h.toast).toHaveBeenCalledTimes(2);});});diff --git a/apps/desktop/src/renderer/features/bots/companionCloudStore.ts b/apps/desktop/src/renderer/features/bots/companionCloudStore.tsindex ffe6e37492..f5b85c754a 100644--- a/apps/desktop/src/renderer/features/bots/companionCloudStore.ts+++ b/apps/desktop/src/renderer/features/bots/companionCloudStore.ts@@ -43,17 +43,22 @@ function ensureOwner(): void {snapshot = EMPTY_COMPANION_CLOUD_SNAPSHOT;}-export function refreshCompanionCloud(): void {+/** Re-reads the snapshot; resolves once the answer is applied (or dropped as superseded), never rejects. */+export function refreshCompanionCloudSettled(): Promise<void> {ensureOwner();const api = companionCloudApi();- if (!api) return;+ if (!api) return Promise.resolve();const requestOwner = owner;const requestPushes = pushes;- void Promise.resolve().then(() => api.getCompanionCloudState()).then((next) => {+ return Promise.resolve().then(() => api.getCompanionCloudState()).then((next) => {if (isDataOwnerGenerationCurrent(requestOwner) && pushes === requestPushes && next) apply(next);}).catch(() => undefined);}+export function refreshCompanionCloud(): void {+ void refreshCompanionCloudSettled();+}+function start(): () => void {users += 1;if (users === 1) {diff --git a/apps/desktop/src/renderer/features/bots/useCompanionArrivalNotice.ts b/apps/desktop/src/renderer/features/bots/useCompanionArrivalNotice.tsindex ff0b15eb16..6574a0ae0a 100644--- a/apps/desktop/src/renderer/features/bots/useCompanionArrivalNotice.ts+++ b/apps/desktop/src/renderer/features/bots/useCompanionArrivalNotice.ts@@ -1,15 +1,17 @@import { useEffect } from 'react';import { useTranslation } from 'react-i18next';+import { getDataOwnerGeneration, isDataOwnerGenerationCurrent, type DataOwnerGeneration } from '@/contexts/dataOwnerGeneration';import { toast } from '@/lib/toast';-import { companionCloudApi, refreshCompanionCloud, useCompanionCloud } from './companionCloudStore';+import { companionCloudApi, refreshCompanionCloudSettled, useCompanionCloud } from './companionCloudStore';/**- * The notices shown per bot in this window, so a snapshot still carrying them (before main's- * dismissal is read back) never repeats the toast. An empty snapshot forgets them: the next arrival- * of the same companion is a new one and shows again.+ * The notices shown per bot while main's dismissal is not yet read back, so a snapshot that still+ * carries them never repeats the toast. An entry ends as soon as that dismissal and refresh are+ * done (or failed), so the next arrival of the same companion is a new one and shows again; an+ * entry of another account is never honoured.*/-const shown = new Map<string, string>();+const shown = new Map<string, { owner: DataOwnerGeneration; batch: string }>();/*** One line after a companion arrived on this computer for what its old setup could not keep (the@@ -21,17 +23,18 @@ export function useCompanionArrivalNotice(botId: string | undefined, name: strinconst { t } = useTranslation();const notices = useCompanionCloud().actors.find((actor) => actor.externalId === botId)?.arrivalNotices;useEffect(() => {- if (!botId) return;- if (!notices?.length) {- shown.delete(botId);- return;- }+ if (!botId || !notices?.length) return;const batch = notices.join(',');- if (shown.get(botId) === batch) return;- shown.set(botId, batch);+ const known = shown.get(botId);+ if (known && known.batch === batch && isDataOwnerGenerationCurrent(known.owner)) return;+ const entry = { owner: getDataOwnerGeneration(), batch };+ shown.set(botId, entry);for (const notice of notices) {toast.info(t(notice === 'model' ? 'bots.cloud.arrivalModel' : 'bots.cloud.arrivalPermissions', { name: name ?? '' }));}- void companionCloudApi()?.dismissCompanionArrivalNotices?.(botId).then(refreshCompanionCloud, () => undefined);+ // Whether main dismissed it or not, the dedupe ends here; a failed dismissal may show again next time.+ void Promise.resolve(companionCloudApi()?.dismissCompanionArrivalNotices?.(botId))+ .then(() => refreshCompanionCloudSettled(), () => undefined)+ .finally(() => { if (shown.get(botId) === entry) shown.delete(botId); });}, [botId, name, notices, t]);}