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): 最新页重置后丢弃晚到的更早一页

Chris29e5d51c0585
diff --git a/apps/desktop/src/renderer/features/bots/BotGroupChatView.tsx b/apps/desktop/src/renderer/features/bots/BotGroupChatView.tsx
index 1d01559aad..6a2f75150a 100644
--- a/apps/desktop/src/renderer/features/bots/BotGroupChatView.tsx
+++ b/apps/desktop/src/renderer/features/bots/BotGroupChatView.tsx
@@ -617,6 +617,7 @@ function BotGroupChatContent({ groupId }: { groupId: string }) {
if (base.kind !== 'ready' || loadingOlderRef.current) return null;
const loaded = mergeBotGroupMessages(base.older, base.group.messages);
const before = beforeSequence ?? loaded[0]?.sequence;
+ const anchorId = loaded[0]?.id;
const api = botGroupApi();
if (!before || !api) return null;
const owner = getDataOwnerGeneration();
@@ -630,6 +631,11 @@ function BotGroupChatContent({ groupId }: { groupId: string }) {
toast.error(t('bots.groupChat.timeline.loadEarlierFailed'));
return null;
}
+ // The latest page started over meanwhile (more than a page arrived): this page was read
+ // below a timeline that is gone and would sit under the new one with the gap still in it.
+ const stillLoaded = (timeline: typeof base) => !anchorId || (timeline.kind === 'ready'
+ && (timeline.group.messages.some(message => message.id === anchorId) || timeline.older.some(message => message.id === anchorId)));
+ if (!stillLoaded(stateRef.current)) return null;
const element = scrollRef.current;
const current = stateRef.current;
const shownFirst = current.kind === 'ready' ? mergeBotGroupMessages(current.older, current.group.messages)[0]?.sequence : undefined;
@@ -638,7 +644,7 @@ function BotGroupChatContent({ groupId }: { groupId: string }) {
if (element && prepends) prependAnchorRef.current = { height: element.scrollHeight, top: element.scrollTop };
const newerRefresh = current.kind === 'ready' && current.group !== base.group;
setState((previous) => {
- if (previous.kind !== 'ready') return previous;
+ if (previous.kind !== 'ready' || !stillLoaded(previous)) return previous;
// A newer push refresh wins over an older in-flight pagination request.
const executionFailures = previous.group === base.group
? result.group.executionFailures ?? previous.group.executionFailures
diff --git a/apps/desktop/src/renderer/features/bots/ChatThreadPanel.tsx b/apps/desktop/src/renderer/features/bots/ChatThreadPanel.tsx
index 14fc14c75e..41f59e0be0 100644
--- a/apps/desktop/src/renderer/features/bots/ChatThreadPanel.tsx
+++ b/apps/desktop/src/renderer/features/bots/ChatThreadPanel.tsx
@@ -92,6 +92,10 @@ export function ChatThreadPanel({ group, rootId, quoteSources = group.messages,
const owner = getDataOwnerGeneration();
const current = () => aliveRef.current && isDataOwnerGenerationCurrent(owner);
const shown = repliesRef.current;
+ // An older page belongs below the reply it was read from; a latest page that started over
+ // meanwhile dropped that reply, and the page would sit under the new one with the gap still in it.
+ const anchorId = before ? shown[0]?.id : undefined;
+ const anchored = (replies: BotGroupMessageView[]) => !anchorId || replies.some(reply => reply.id === anchorId);
try {
const result = await api().thread({ groupId: group.id, rootId, before, sourceMessageIds: shown.map(message => message.id) });
if (!current()) return;
@@ -104,7 +108,10 @@ export function ChatThreadPanel({ group, rootId, quoteSources = group.messages,
// start over from it, as on first load, so 「查看更早」 reads on from right below it.
const gap = !before && result.hasMore && result.replies.length > 0 && shown.length > 0
&& floor > shown.reduce((max, reply) => Math.max(max, reply.sequence), Number.NEGATIVE_INFINITY);
- setReplies(previous => stabilizeById(previous, gap ? result.replies : mergeBotGroupMessages(previous, result.replies)));
+ if (!anchored(repliesRef.current)) return;
+ setReplies(previous => (anchored(previous)
+ ? stabilizeById(previous, gap ? result.replies : mergeBotGroupMessages(previous, result.replies))
+ : previous));
if (!before) latestFloorRef.current = floor;
if (!rereadOnly && (before || shown.length === 0 || gap)) setHasMore(result.hasMore);
} catch { if (current()) setError(t(key('requestFailed'))); }
diff --git a/apps/desktop/src/renderer/features/bots/__tests__/BotGroupChatView.test.tsx b/apps/desktop/src/renderer/features/bots/__tests__/BotGroupChatView.test.tsx
index 45afef56a3..a8ea3af6ec 100644
--- a/apps/desktop/src/renderer/features/bots/__tests__/BotGroupChatView.test.tsx
+++ b/apps/desktop/src/renderer/features/bots/__tests__/BotGroupChatView.test.tsx
@@ -740,6 +740,26 @@ describe('BotGroupChatView', () => {
expect(screen.queryByText('bots.groupChat.notice.runtimeFailure.AUTH_REQUIRED:Bot')).toBeNull();
});
+ it('drops an older page that answers after the latest page started over', async () => {
+ const latest = msg({ id: 'latest', sequence: 100, content: 'Latest question' });
+ const source = msg({ id: 'older', sequence: 4, content: 'Earlier question' });
+ const far = msg({ id: 'far', sequence: 300, content: 'Far question' });
+ let finish: ((result: { ok: true; group: BotGroupDetail }) => void) | undefined;
+ const pending = new Promise<{ ok: true; group: BotGroupDetail }>(resolve => { finish = resolve; });
+ mocks.getBotGroup.mockResolvedValueOnce({ ok: true, group: detail({ messages: [latest], hasMoreBefore: true }) })
+ .mockReturnValueOnce(pending)
+ .mockResolvedValue({ ok: true, group: detail({ messages: [far], hasMoreBefore: true }) });
+ renderView();
+ fireEvent.click(await screen.findByRole('button', { name: 'bots.groupChat.timeline.loadEarlier' }));
+ // More than a page arrives while the older page is still on its way: the timeline starts over.
+ await act(async () => mocks.pushes.forEach(push => push({ groupId: 'g1', change: 'round' })));
+ await screen.findByText('Far question');
+ await act(async () => finish?.({ ok: true, group: detail({ messages: [source], hasMoreBefore: false }) }));
+ expect(screen.queryByText('Earlier question')).toBeNull();
+ fireEvent.click(await screen.findByRole('button', { name: 'bots.groupChat.timeline.loadEarlier' }));
+ await waitFor(() => expect(mocks.getBotGroup.mock.calls.at(-1)?.[1]).toMatchObject({ beforeSequence: 300 }));
+ });
+
it.each([false, true])('clears retried failures on an older timeline page when the newest page is refreshed (initial failure: %s)', async initialFailure => {
const source = msg({ id: 'older-source', sequence: 4, content: 'Earlier question' });
const failure = { executionId: 'older', epoch: 1, sourceMessageId: source.id, botId: 'bot', botName: 'Bot', code: 'AUTH_REQUIRED' as const, planId: null };
diff --git a/apps/desktop/src/renderer/features/bots/__tests__/ChatServerControls.test.tsx b/apps/desktop/src/renderer/features/bots/__tests__/ChatServerControls.test.tsx
index 1c48a6e640..73a7db27c5 100644
--- a/apps/desktop/src/renderer/features/bots/__tests__/ChatServerControls.test.tsx
+++ b/apps/desktop/src/renderer/features/bots/__tests__/ChatServerControls.test.tsx
@@ -166,6 +166,26 @@ describe('chat interaction controls', () => {
await waitFor(() => expect(mocks.thread.mock.calls.at(-1)?.[0]).toMatchObject({ before: 200 }));
});
+ it('drops an older Thread page that answers after the latest page started over', async () => {
+ const latest = { ...message, id: 'latest', sequence: 100, content: 'Latest reply' };
+ const older = { ...message, id: 'older', sequence: 4, content: 'Earlier reply' };
+ const far = { ...message, id: 'far', sequence: 300, content: 'Far reply' };
+ let finish!: (value: unknown) => void;
+ mocks.thread.mockResolvedValueOnce({ ok: true, root: message, replies: [latest], hasMore: true })
+ .mockReturnValueOnce(new Promise(resolve => { finish = resolve; }))
+ .mockResolvedValue({ ok: true, root: message, replies: [far], hasMore: true });
+ render(<ChatThreadPanel group={group} rootId="root" onClose={vi.fn()} />);
+ fireEvent.click(await screen.findByRole('button', { name: 'bots.groupChat.timeline.loadEarlier' }));
+ // More than a page arrives while the older page is still on its way.
+ const root = screen.getByText('Root message').closest('article')!;
+ fireEvent.click(within(root).getByRole('button', { name: k('reactionCount') }));
+ await screen.findByText('Far reply');
+ await act(async () => finish({ ok: true, root: message, replies: [older], hasMore: false }));
+ expect(screen.queryByText('Earlier reply')).toBeNull();
+ fireEvent.click(await screen.findByRole('button', { name: 'bots.groupChat.timeline.loadEarlier' }));
+ await waitFor(() => expect(mocks.thread.mock.calls.at(-1)?.[0]).toMatchObject({ before: 300 }));
+ });
+
it('clears a retried failure on an older loaded Thread page without dropping its reply', async () => {
const latest = { ...message, id: 'latest', sequence: 100, content: 'Latest reply' };
const older = { ...message, id: 'older', sequence: 4, content: 'Earlier reply' };