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): 待贴回队列先写再同步界面;本人贴回优先显示;过期旧贴回优先核对

Dash462f828407e4
diff --git a/apps/desktop/src/renderer/features/bots/BotGroupChatView.tsx b/apps/desktop/src/renderer/features/bots/BotGroupChatView.tsx
index a4ccccdd81..fc5e301663 100644
--- a/apps/desktop/src/renderer/features/bots/BotGroupChatView.tsx
+++ b/apps/desktop/src/renderer/features/bots/BotGroupChatView.tsx
@@ -824,13 +824,14 @@ function staleBotGroupShares(older: readonly BotGroupMessageView[], version: num
}
/**
- * Which older shares one refresh re-checks: stale or still-loading ones first, then the
- * newest, up to the per-read cap. The rest are checked by later refreshes.
+ * Which older shares one refresh re-checks, up to the per-read cap: stale ones first, so
+ * every capped batch advances; then still-loading ones, which the main process is already
+ * retrying; then the newest. The rest are checked by later refreshes.
*/
function botGroupShownShares(older: readonly BotGroupMessageView[], version: number | undefined) {
const withShares = older.filter(message => message.threadShare).reverse();
- const first = new Set(withShares.filter(message => message.threadShare!.status === 'loading'
- || (version !== undefined && (message.threadShare!.version ?? 0) < version)));
+ const stale = staleBotGroupShares(withShares, version);
+ const first = new Set([...stale, ...withShares.filter(message => message.threadShare!.status === 'loading')]);
return [...first, ...withShares.filter(message => !first.has(message))].slice(0, BOT_GROUP_SHOWN_SHARES_MAX).map(message => ({
id: message.id, messageId: message.threadShare!.messageId, threadRootId: message.threadShare!.threadRootId,
...(message.authorBotId ? { sharerId: message.authorBotId } : {}) }));
diff --git a/apps/desktop/src/renderer/features/bots/ChatThreadPanel.tsx b/apps/desktop/src/renderer/features/bots/ChatThreadPanel.tsx
index e922db7384..6074c3fbb8 100644
--- a/apps/desktop/src/renderer/features/bots/ChatThreadPanel.tsx
+++ b/apps/desktop/src/renderer/features/bots/ChatThreadPanel.tsx
@@ -126,11 +126,14 @@ export function ChatThreadPanel({ group, rootId, focus, sharedReplies, onRevealS
const [alsoSend, setAlsoSend] = useState(false);
const unpostedKey = `${group.id}:${rootId}`;
const [unposted, setUnpostedState] = useState<ReadonlySet<string>>(() => unpostedReplies().get(unpostedKey) ?? new Set());
- const setUnposted = (update: (previous: ReadonlySet<string>) => ReadonlySet<string>) => setUnpostedState(previous => {
- const next = update(previous);
- if (next.size) unpostedReplies().set(unpostedKey, next); else unpostedReplies().delete(unpostedKey);
- return next;
- });
+ // The queue is written first: a share may finish after this panel has closed, and the
+ // state below only mirrors it while the panel is open.
+ const setUnposted = (update: (previous: ReadonlySet<string>) => ReadonlySet<string>) => {
+ const queue = unpostedReplies();
+ const next = update(queue.get(unpostedKey) ?? new Set());
+ if (next.size) queue.set(unpostedKey, next); else queue.delete(unpostedKey);
+ setUnpostedState(next);
+ };
const [posting, setPosting] = useState<ReadonlySet<string>>(new Set());
const postingRef = useRef(new Set<string>());
/** Shares confirmed in this panel (reply → main-chat message, null when unknown). */
@@ -219,10 +222,12 @@ export function ChatThreadPanel({ group, rootId, focus, sharedReplies, onRevealS
} catch { if (isDataOwnerGenerationCurrent(owner)) setError(t(key('requestFailed'))); }
finally { sending.current = false; setBusy(false); }
}
+ /** The reader's own share wins over another member's, even when only this panel knows it. */
const sharedState = (messageId: string) => {
const known = sharedReplies?.get(messageId);
- if (known) return { bySelf: known.bySelf, shareId: known.messageId };
- return posted.has(messageId) ? { bySelf: true, shareId: posted.get(messageId) ?? null } : null;
+ if (known?.bySelf) return { bySelf: true, shareId: known.messageId };
+ if (posted.has(messageId)) return { bySelf: true, shareId: posted.get(messageId) ?? null };
+ return known ? { bySelf: false, shareId: known.messageId } : null;
};
/** The share may sit on an older page the main chat has not loaded. */
const revealShare = (shareId: string) => {
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 de41c79e5e..ef1bd5b480 100644
--- a/apps/desktop/src/renderer/features/bots/__tests__/BotGroupChatView.test.tsx
+++ b/apps/desktop/src/renderer/features/bots/__tests__/BotGroupChatView.test.tsx
@@ -2046,6 +2046,29 @@ describe('thread share follow-ups', () => {
expect(mocks.shareToMain).toHaveBeenCalledTimes(1);
});
+ it("shows the reader's own share over another member's that the main chat has loaded", async () => {
+ const bobShare = msg({ id: 'bob-share', sequence: 2, authorKind: 'user', isSelf: false, authorBotId: 'bob', authorName: 'Bob', content: '↪ 卡片',
+ threadShare: { messageId: 'alice-reply', threadRootId: 'u1', status: 'available', authorKind: 'user', authorBotId: null, authorName: 'Alice',
+ sharedByAuthor: false, content: 'Alice says hi', attachments: [], createdAt: 1, edited: false } });
+ mocks.getBotGroup.mockResolvedValue({ ok: true, group: detail({ serverBacked: true, messages: [msg({ id: 'u1', content: '去哪玩' }), bobShare] }) });
+ mocks.thread.mockResolvedValue({ ok: true, root: msg({ id: 'u1', content: '去哪玩' }), hasMore: false,
+ replies: [msg({ id: 'alice-reply', authorKind: 'user', isSelf: false, authorName: 'Alice', content: 'Alice says hi' })] });
+ // The reader's earlier share of the same reply sits on a page the main chat has not loaded.
+ mocks.shareToMain.mockResolvedValue({ ok: true, messageId: 'my-old-share', alreadyShared: true });
+ const user = userEvent.setup();
+ renderView();
+ await user.click((await screen.findAllByRole('button', { name: 'bots.groupChat.server.reply' }))[0]!);
+ const panel = await screen.findByRole('complementary');
+ const reply = (await within(panel).findByText('Alice says hi')).closest('article')!;
+ expect(await within(reply).findByRole('button', { name: t('postedToMain') })).toBeTruthy();
+ fireEvent.pointerDown(within(reply).getByRole('button', { name: 'chat.messageActionBar.moreActions' }), { button: 0, ctrlKey: false });
+ await user.click(await screen.findByRole('menuitem', { name: t('postToMain') }));
+ expect(await within(reply).findByRole('button', { name: t('postedBySelf') })).toBeTruthy();
+ fireEvent.pointerDown(within(reply).getByRole('button', { name: 'chat.messageActionBar.moreActions' }), { button: 0, ctrlKey: false });
+ expect(await screen.findByRole('menuitem', { name: t('viewInMain') })).toBeTruthy();
+ expect(screen.queryByRole('menuitem', { name: t('postToMain') })).toBeNull();
+ });
+
it('keeps every failed main-chat copy retryable, across switching threads', async () => {
mocks.getBotGroup.mockResolvedValue({ ok: true, group: detail({ serverBacked: true, messages: [msg({ id: 'u1', content: 'A' }), msg({ id: 'u2', sequence: 2, content: 'B' })] }) });
mocks.reply.mockResolvedValueOnce({ ok: true, messageId: 'first' }).mockResolvedValueOnce({ ok: true, messageId: 'second' });
@@ -2073,6 +2096,27 @@ describe('thread share follow-ups', () => {
expect(mocks.shareToMain.mock.calls.map(([input]) => input.messageId)).toEqual(['first', 'second']);
expect(mocks.reply).toHaveBeenCalledTimes(2);
});
+
+ it('keeps a main-chat copy retryable when it fails after leaving the thread', async () => {
+ mocks.getBotGroup.mockResolvedValue({ ok: true, group: detail({ serverBacked: true, messages: [msg({ id: 'u1', content: 'A' }), msg({ id: 'u2', sequence: 2, content: 'B' })] }) });
+ mocks.reply.mockResolvedValue({ ok: true, messageId: 'first' });
+ let fail!: () => void;
+ mocks.shareToMain.mockReturnValueOnce(new Promise(resolve => { fail = () => resolve({ ok: false, errorCode: 'SERVICE_ERROR' }); }));
+ const user = userEvent.setup();
+ renderView();
+ const [first, second] = await screen.findAllByRole('button', { name: 'bots.groupChat.server.reply' });
+ await user.click(first!);
+ const panel = await screen.findByRole('complementary');
+ await user.click(within(panel).getByRole('checkbox', { name: t('alsoSend') }));
+ await user.type(within(panel).getByRole('textbox'), 'one{Enter}');
+ await waitFor(() => expect(mocks.shareToMain).toHaveBeenCalledTimes(1));
+ // The copy is still on its way when the reader moves to another thread.
+ await user.click(second!);
+ await screen.findByText('Reply to u2');
+ await act(async () => fail());
+ await user.click(first!);
+ expect(await within(await screen.findByRole('complementary')).findByRole('button', { name: t('retryPost') })).toBeTruthy();
+ });
});
describe('shares on older pages', () => {
@@ -2171,6 +2215,31 @@ describe('more older shares than one refresh re-reads', () => {
expect(refreshes.every(ids => ids.length <= 200)).toBe(true);
expect(screen.queryByText('↪ 卡片 0')).toBeNull();
});
+
+ it('checks a stale share even when a full batch of newer ones is still loading', { timeout: 20_000 }, async () => {
+ type Share = NonNullable<BotGroupMessageView['threadShare']>;
+ const shareOf = (i: number, extra: Partial<Share> = {}): Share => ({ messageId: `reply-${i}`, threadRootId: 'root', status: 'available', authorKind: 'bot',
+ authorBotId: 'xiaoman', authorName: '小满', sharedByAuthor: false, content: `原文 ${i}`, attachments: [], createdAt: 1, edited: false, version: 2, ...extra });
+ // The newest 200 are still being read by the main process; the oldest predates an edit.
+ const older = Array.from({ length: 201 }, (_, i) => msg({ id: `share-${i}`, sequence: i + 1, authorKind: 'user', isSelf: false, authorBotId: 'alice',
+ authorName: 'Alice', content: `↪ 卡片 ${i}`, threadShare: i === 0 ? shareOf(0, { version: 1 }) : shareOf(i, { status: 'loading', authorName: '', content: '' }) }));
+ const latest = detail({ serverBacked: true, hasMoreBefore: true, threadShareVersion: 2, messages: [msg({ id: 'new', sequence: 500, content: '最新' })] });
+ const refreshes: Array<string[]> = [];
+ mocks.getBotGroup.mockImplementation(async (_groupId: string, options?: { beforeSequence?: number; shownShares?: Array<{ id: string; messageId: string }> }) => {
+ if (options?.beforeSequence) return { ok: true, group: { ...latest, hasMoreBefore: false, messages: older } };
+ refreshes.push((options?.shownShares ?? []).map(share => share.id));
+ const threadShareUpdates = Object.fromEntries((options?.shownShares ?? []).map(share => [share.id, share.messageId === 'reply-0'
+ ? shareOf(0, { content: '核对后的原文' }) : shareOf(Number(share.messageId.slice(6)), { status: 'loading', authorName: '', content: '' })]));
+ return { ok: true, group: { ...latest, threadShareUpdates } };
+ });
+ const user = userEvent.setup();
+ renderView();
+ await user.click(await screen.findByRole('button', { name: 'bots.groupChat.timeline.loadEarlier' }));
+ expect(await screen.findByText('核对后的原文')).toBeTruthy();
+ // The first refresh that checks older shares already takes the stale one.
+ expect(refreshes.find(ids => ids.length)).toContain('share-0');
+ expect(refreshes.every(ids => ids.length <= 200)).toBe(true);
+ });
});
describe('pending main-chat copies across accounts', () => {