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): 定位贴回回复时只按真正合入的旧页计数

Dash0bed6169ff25
diff --git a/apps/desktop/src/renderer/features/bots/ChatThreadPanel.tsx b/apps/desktop/src/renderer/features/bots/ChatThreadPanel.tsx
index b76d4418ca..4df1d417a1 100644
--- a/apps/desktop/src/renderer/features/bots/ChatThreadPanel.tsx
+++ b/apps/desktop/src/renderer/features/bots/ChatThreadPanel.tsx
@@ -154,13 +154,15 @@ export function ChatThreadPanel({ group, rootId, quoteSources = group.messages,
const request = useRef(0);
const attempt = useRef<{ text: string; id: string; mentions: BotGroupMention } | null>(null);
const sending = useRef(false);
- const load = useCallback(async (before?: number) => {
+ /** `onAccepted` runs only when this read's result is the one shown. */
+ const load = useCallback(async (before?: number, onAccepted?: () => void) => {
const version = ++request.current, owner = getDataOwnerGeneration();
const current = () => version === request.current && isDataOwnerGenerationCurrent(owner);
try {
const result = await api().thread({ groupId: group.id, rootId, before, sourceMessageIds: replies.map(message => message.id) });
if (!current()) return;
if (!result.ok) { setError(t(chatErrorKey(result.errorCode))); return; }
+ onAccepted?.();
setRoot(result.root); setError('');
setExecutionFailures(result.executionFailures ?? []);
setReplies(previous => mergeBotGroupMessages(previous, result.replies));
@@ -185,8 +187,9 @@ export function ChatThreadPanel({ group, rootId, quoteSources = group.messages,
const target = findThreadMessageElement(contentRef.current, focus.messageId);
if (target) { search.pages = -1; flashThreadTarget(target); return; }
if (!hasMore || search.pages >= FOCUS_PAGE_LIMIT) { search.pages = -1; return; }
- search.pages += 1;
- void loadRef.current(replies[0]?.sequence);
+ // A push refresh may overtake this page; then its replies trigger the next attempt, and
+ // only a page that was merged counts against the limit.
+ void loadRef.current(replies[0]?.sequence, () => { search.pages += 1; });
}, [focus, replies, hasMore]);
/** One server operation per reply and person, so a retry never posts twice. */
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 f9daa71c6c..aaa6417064 100644
--- a/apps/desktop/src/renderer/features/bots/__tests__/ChatServerControls.test.tsx
+++ b/apps/desktop/src/renderer/features/bots/__tests__/ChatServerControls.test.tsx
@@ -2,7 +2,7 @@
import { StrictMode, type ReactNode } from 'react';
import { cleanup, fireEvent, render as renderUI, screen, waitFor, within } from '@testing-library/react';
import { Tooltip } from '@/components/ui/tooltip';
-import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import { afterEach, beforeEach, describe, expect, it, onTestFinished, vi } from 'vitest';
import { ChatInviteButton, ChatJoinButton, ChatMessageActions } from '../ChatServerControls';
import { ChatThreadPanel } from '../ChatThreadPanel';
import type { BotGroupDetail, BotGroupMessageView } from '../../../../shared/botGroupChat';
@@ -99,6 +99,33 @@ describe('chat interaction controls', () => {
expect(mocks.thread.mock.calls.at(-1)?.[0]).toMatchObject({ sourceMessageIds: [older.id, latest.id] });
});
+ it('keeps looking for a shared reply when group pushes overtake its older-page reads', async () => {
+ const latest = { ...message, id: 'latest', sequence: 100, content: 'Latest reply' };
+ const target = { ...message, id: 'target', sequence: 4, content: 'Target reply' };
+ const olderReads: Array<(value: unknown) => void> = [];
+ mocks.thread.mockImplementation(({ before }: { before?: number }) => before
+ ? new Promise(resolve => { olderReads.push(resolve); })
+ : Promise.resolve({ ok: true, root: message, replies: [latest], hasMore: true }));
+ let push!: (event: { groupId: string }, stamp: unknown) => void;
+ Object.defineProperty(window, 'electronAPI', { configurable: true, value: { maker: {
+ chatServer: mocks, onBotGroupChanged: (listener: typeof push) => { push = listener; return () => {}; },
+ } } });
+ const scrollIntoView = vi.fn();
+ Element.prototype.scrollIntoView = scrollIntoView;
+ onTestFinished(() => { delete (Element.prototype as { scrollIntoView?: unknown }).scrollIntoView; });
+ render(<ChatThreadPanel group={group} rootId="root" focus={{ messageId: target.id, nonce: 1 }} onClose={vi.fn()} />);
+ await waitFor(() => expect(olderReads).toHaveLength(1));
+ // Each push refresh lands while an older page is in flight, so that page is discarded.
+ for (let overtaken = 1; overtaken <= 6; overtaken += 1) {
+ push({ groupId: 'room' }, null);
+ await waitFor(() => expect(olderReads).toHaveLength(overtaken + 1));
+ olderReads[overtaken - 1]!({ ok: true, root: message, replies: [], hasMore: true });
+ }
+ olderReads.at(-1)!({ ok: true, root: message, replies: [target], hasMore: false });
+ expect(await screen.findByText('Target reply')).toBeTruthy();
+ await waitFor(() => expect(scrollIntoView).toHaveBeenCalled());
+ });
+
it('preserves copy and image sharing beside thread replies and reactions in one toolbar', async () => {
const writeText = vi.fn().mockResolvedValue(undefined), reply = vi.fn();
Object.defineProperty(navigator, 'clipboard', { configurable: true, value: { writeText } });