Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(bots): 旧页贴回是否还要核对改按已提交的时间线判断
Dash4850c41082bd
diff --git a/apps/desktop/src/renderer/features/bots/BotGroupChatView.tsx b/apps/desktop/src/renderer/features/bots/BotGroupChatView.tsxindex 0e09a9407e..a4ccccdd81 100644--- a/apps/desktop/src/renderer/features/bots/BotGroupChatView.tsx+++ b/apps/desktop/src/renderer/features/bots/BotGroupChatView.tsx@@ -166,7 +166,7 @@ function BotGroupChatContent({ groupId }: { groupId: string }) {const [continuing, setContinuing] = useState(false);const [planPending, setPlanPending] = useState<PlanPending | null>(null);const planPendingRef = useRef(false);- const loadRef = useRef<(merging?: readonly BotGroupMessageView[]) => void>(() => {});+ const loadRef = useRef<(additionalSources?: string[]) => void>(() => {});const scrollRef = useRef<HTMLDivElement>(null);const contentRef = useRef<HTMLDivElement>(null);const stickToBottomRef = useRef(true);@@ -180,9 +180,7 @@ function BotGroupChatContent({ groupId }: { groupId: string }) {useEffect(() => {let cancelled = false;let requestVersion = 0;- // Follow-up reads for older shares one refresh could not check; bounded per chain.- let shareFollowUps = 0;- const load = async (merging: readonly BotGroupMessageView[] = []) => {+ const load = async (additionalSources: string[] = []) => {const version = ++requestVersion;const owner = getDataOwnerGeneration();const api = botGroupApi();@@ -195,12 +193,10 @@ function BotGroupChatContent({ groupId }: { groupId: string }) {try {const displayed = stateRef.current;const shown = displayed.kind === 'ready' && displayed.group.id === groupId;- // An older page merged just before this read may not be on `displayed` yet.- const shownOlder = shown ? mergeBotGroupMessages(displayed.older, merging) : [];const sourceMessageIds = [...new Set([...(shown- ? mergeBotGroupMessages(shownOlder, displayed.group.messages).map(message => message.id) : []), ...merging.map(message => message.id)])];+ ? mergeBotGroupMessages(displayed.older, displayed.group.messages).map(message => message.id) : []), ...additionalSources])];// Older pages are not re-read, so their shares ask for the reply's current state.- const shownShares = shown ? botGroupShownShares(shownOlder, displayed.group.threadShareVersion) : [];+ const shownShares = shown ? botGroupShownShares(displayed.older, displayed.group.threadShareVersion) : [];const result = sourceMessageIds.length || shownShares.length? await api.getBotGroup(groupId, { sourceMessageIds, ...(shownShares.length ? { shownShares } : {}) }): await api.getBotGroup(groupId);@@ -218,14 +214,6 @@ function BotGroupChatContent({ groupId }: { groupId: string }) {olderHasMore: result.group.hasMoreBefore,},);- const older = applyBotGroupThreadShareUpdates(shownOlder, result.group);- const unchecked = staleBotGroupShares(older, result.group.threadShareVersion).length;- const total = older.filter(message => message.threadShare).length;- if (!unchecked) shareFollowUps = 0;- else if (shareFollowUps < Math.ceil(total / BOT_GROUP_SHOWN_SHARES_MAX) + 1) {- shareFollowUps += 1;- setTimeout(() => { if (!cancelled) void load(); }, 0);- }} else if (result.errorCode === 'NOT_FOUND') {setState({ kind: 'missing' });} else {@@ -236,7 +224,7 @@ function BotGroupChatContent({ groupId }: { groupId: string }) {if (isCurrent()) setState((previous) => (previous.kind === 'ready' ? previous : { kind: 'error' }));}};- loadRef.current = merging => void load(merging);+ loadRef.current = additionalSources => void load(additionalSources);void load();const unsubscribe =botGroupApi()?.onBotGroupChanged?.((payload, ownerStamp) => {@@ -258,6 +246,19 @@ function BotGroupChatContent({ groupId }: { groupId: string }) {};}, [groupId, reloadVersion]);+ // Older shares checked before the latest edit, deletion or reset are re-checked from the+ // committed timeline, whichever of a refresh and an older page landed last. One refresh+ // checks up to the per-read cap, so a bounded number of follow-ups reaches all of them.+ const shareFollowUps = useRef(0);+ useEffect(() => {+ if (state.kind !== 'ready') { shareFollowUps.current = 0; return; }+ if (!staleBotGroupShares(state.older, state.group.threadShareVersion).length) { shareFollowUps.current = 0; return; }+ const total = state.older.filter(message => message.threadShare).length;+ if (shareFollowUps.current >= Math.ceil(total / BOT_GROUP_SHOWN_SHARES_MAX) + 1) return;+ shareFollowUps.current += 1;+ loadRef.current();+ }, [state]);+const group = state.kind === 'ready' ? state.group : null;// Until the timeline arrives, the header comes from the group's sidebar row (same account).const listed = useBotGroupSummary(state.kind === 'loading' || state.kind === 'error' ? groupId : null);@@ -477,9 +478,8 @@ function BotGroupChatContent({ groupId }: { groupId: string }) {olderHasMore: result.group.hasMoreBefore,};});- // A push may have refreshed before this page was displayed: reconcile its sources and- // shares too. The page goes along because the merge above may not be committed yet.- if (newerRefresh) loadRef.current(result.group.messages);+ // A push may have refreshed before these sources were displayed. Reconcile their current state too.+ if (newerRefresh) loadRef.current(result.group.messages.map(message => message.id));} catch {toast.error(t('bots.groupChat.timeline.loadEarlierFailed'));} finally {diff --git a/apps/desktop/src/renderer/features/bots/__tests__/BotGroupChatView.test.tsx b/apps/desktop/src/renderer/features/bots/__tests__/BotGroupChatView.test.tsxindex 0801def61b..de41c79e5e 100644--- a/apps/desktop/src/renderer/features/bots/__tests__/BotGroupChatView.test.tsx+++ b/apps/desktop/src/renderer/features/bots/__tests__/BotGroupChatView.test.tsx@@ -2100,25 +2100,28 @@ describe('shares on older pages', () => {shownShares: [{ id: 'old-share', messageId: 'old-reply', threadRootId: 'old-root', sharerId: 'alice-actor' }] }));});- it('checks the shares of an older page that lands after an edit refreshed the timeline', async () => {+ // The reply is edited while an older page is on its way; either read may land first.+ it.each(['refresh', 'page'] as const)('checks the shares of an older page that races an edit refresh (%s lands first)', async (first) => {const share = { messageId: 'old-reply', threadRootId: 'old-root', status: 'available' as const, authorKind: 'bot' as const, authorBotId: 'xiaoman', authorName: '小满',sharedByAuthor: false, content: '编辑前的原文', attachments: [], createdAt: 1, edited: false, version: 1 };const oldShare = msg({ id: 'old-share', sequence: 1, authorKind: 'user', isSelf: false, authorBotId: 'alice-actor', authorName: 'Alice', content: '↪ 卡片文字', threadShare: share });const latest = detail({ serverBacked: true, hasMoreBefore: true, threadShareVersion: 1, messages: [msg({ id: 'new', sequence: 50, content: '最新' })] });- let finish!: (result: { ok: true; group: BotGroupDetail }) => void;+ let finishPage!: (result: { ok: true; group: BotGroupDetail }) => void;+ let finishRefresh!: (result: { ok: true; group: BotGroupDetail }) => void;mocks.getBotGroup.mockResolvedValueOnce({ ok: true, group: latest })- .mockReturnValueOnce(new Promise(resolve => { finish = resolve; }))- .mockResolvedValueOnce({ ok: true, group: { ...latest, threadShareVersion: 2 } })+ .mockReturnValueOnce(new Promise(resolve => { finishPage = resolve; }))+ .mockReturnValueOnce(new Promise(resolve => { finishRefresh = resolve; })).mockImplementation(async (_groupId: string, options?: { shownShares?: Array<{ id: string }> }) => ({ ok: true, group: { ...latest, threadShareVersion: 2,...(options?.shownShares?.some(item => item.id === 'old-share')? { threadShareUpdates: { 'old-share': { ...share, content: '编辑后的原文', edited: true, version: 2 } } } : {}) } }));renderView();fireEvent.click(await screen.findByRole('button', { name: 'bots.groupChat.timeline.loadEarlier' }));- // The reply is edited while that page is on its way; the push refresh finishes first.await act(async () => mocks.pushes.forEach(push => push({ groupId: 'g1', change: 'messages' })));await waitFor(() => expect(mocks.getBotGroup).toHaveBeenCalledTimes(3));- await act(async () => finish({ ok: true, group: { ...latest, hasMoreBefore: false, messages: [oldShare] } }));+ const page = () => finishPage({ ok: true, group: { ...latest, hasMoreBefore: false, messages: [oldShare] } });+ const refresh = () => finishRefresh({ ok: true, group: { ...latest, threadShareVersion: 2 } });+ for (const land of first === 'refresh' ? [refresh, page] : [page, refresh]) await act(async () => land());expect(await screen.findByText('编辑后的原文')).toBeTruthy();expect(screen.queryByText('编辑前的原文')).toBeNull();});