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(chat): refresh reaction avatars when who reacted changes at the same count

Dashd6683d910e9f
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/chatServer.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/chatServer.test.ts
index 14db5638b3..7e4ef8008f 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/chatServer.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/chatServer.test.ts
@@ -1091,6 +1091,23 @@ describe('Chat Server result delivery and refresh', () => {
const recovered = await service.getGroup(roomId);
expect(recovered.ok && recovered.group.messages[0].attachments[0].name).toBe('report.pdf');
});
+ it('versions a message’s reactions on every realtime reaction change, even when the count is unchanged', async () => {
+ const message = { id: execution.source_message_id, seq: '1', authorId: selfId, author: { kind: 'human', name: 'Me' },
+ content: [{ type: 'text', text: 'Hi' }], createdAt: new Date().toISOString(), deleted: false, threadRootId: null,
+ reactions: [{ emoji: '👍', count: 1, me: false }] };
+ fixture.handle.mockImplementation(route => route.includes('/messages?') ? { body: [message] } : response(route));
+ const revision = async () => { const r = await service.getGroup(roomId); return r.ok ? r.group.messages[0].reactionRevision : undefined; };
+ expect(await revision()).toBe(0);
+ await vi.advanceTimersByTimeAsync(2000);
+ const socket = fixture.sockets[0];
+ // One member removes 👍 and another adds it in one batch: same count, different reactor.
+ socket.emit('message', JSON.stringify({ type: 'changes', scope: `conversation:${roomId}`, cursor: '5', changes: [
+ { type: 'reaction.changed', entityId: message.id, data: { emoji: '👍', present: false } },
+ { type: 'reaction.changed', entityId: message.id, data: { emoji: '👍', present: true } },
+ { type: 'message.updated', entityId: message.id },
+ ] }));
+ expect(await revision()).toBe(2);
+ });
it('refreshes a reset room and resumes realtime from the authorized snapshot cursor', async () => {
await service.getGroup(roomId);
await vi.advanceTimersByTimeAsync(2000);
diff --git a/apps/desktop/src/main/maker-ipc/chatServer.ts b/apps/desktop/src/main/maker-ipc/chatServer.ts
index 74421eb773..44baf3c551 100644
--- a/apps/desktop/src/main/maker-ipc/chatServer.ts
+++ b/apps/desktop/src/main/maker-ipc/chatServer.ts
@@ -181,6 +181,8 @@ function createChatServer(local: BotGroupChatService, deps: BotGroupChatServiceD
let companion: CompanionCloudRuntime | undefined;
let companionSelf: string | null = null;
let actorsStale = false;
+ // Who reacted can change while the count stays the same; each realtime reaction change versions the message.
+ const reactionRevisions = new Map<string, number>();
let profiles: Array<typeof botProfiles.$inferSelect> = [];
const workspaces = () => chatServerWorkspaces(config.baseUrl, selfId, current);
const planning = new Map<string, { botId: string; controller: AbortController }>();
@@ -557,6 +559,9 @@ function createChatServer(local: BotGroupChatService, deps: BotGroupChatServiceD
if (event.scope.startsWith('conversation:')) {
const roomId = event.scope.slice(13);
if (rooms.has(roomId)) advanceRoom(roomId, event.cursor);
+ for (const change of event.changes ?? []) if (change.type === 'reaction.changed' && typeof change.entityId === 'string') {
+ reactionRevisions.set(change.entityId, (reactionRevisions.get(change.entityId) ?? 0) + 1);
+ }
changed(roomId);
for (const run of running.values()) if (run.execution.conversation_id === roomId) void checkLease(run);
}
@@ -636,6 +641,7 @@ function createChatServer(local: BotGroupChatService, deps: BotGroupChatServiceD
const quote = m.deleted || planCard ? null : readBotGroupQuote(m.content);
return { id: m.id, sequence: Number(m.seq), deleted: m.deleted, kind: planCard ? 'plan' : 'message', authorKind: legacy?.authorKind === 'system' ? 'system' : m.author.kind === 'human' ? 'user' : 'bot',
isSelf: m.authorId === selfId, threadRootId: m.threadRootId, replyCount: m.replyCount ?? 0, reactions: m.reactions ?? [], ...(quote ? { quote } : {}),
+ reactionRevision: reactionRevisions.get(m.id) ?? 0,
authorBotId: localBot(m.authorId)?.id ?? m.authorId, authorName: legacy?.authorKind === 'system' && typeof legacy.authorName === 'string' ? legacy.authorName : member ? memberName(member) : m.author.name, content: bodyText(m),
mentions: { all: false, botIds: [] }, noticeCode: null, planId: typeof planCard?.data?.planId === 'string' ? planCard.data.planId : null, files: [], attachments: [], createdAt: Date.parse(m.createdAt) };
}
diff --git a/apps/desktop/src/renderer/features/bots/ChatReactionButton.tsx b/apps/desktop/src/renderer/features/bots/ChatReactionButton.tsx
index a7007ef617..0982c77b9f 100644
--- a/apps/desktop/src/renderer/features/bots/ChatReactionButton.tsx
+++ b/apps/desktop/src/renderer/features/bots/ChatReactionButton.tsx
@@ -35,13 +35,14 @@ function reactorNames(reactors: ChatReactor[], t: TFunction, language: string) {
* badge shows their avatars instead of a number; avatars come from the group member projection,
* and actors without one fall back to an initial, never an invented identity.
*/
-export function ChatReactionButton({ groupId, messageId, reaction, members, disabled, onToggle }: {
- groupId?: string; messageId: string; reaction: Reaction; members?: BotGroupMemberView[]; disabled: boolean; onToggle: () => void;
+export function ChatReactionButton({ groupId, messageId, reaction, revision = 0, members, disabled, onToggle }: {
+ groupId?: string; messageId: string; reaction: Reaction; revision?: number; members?: BotGroupMemberView[]; disabled: boolean; onToggle: () => void;
}) {
const { t, i18n } = useTranslation();
const [loaded, setLoaded] = useState<Loaded | null>(null);
- // The authoritative summary changes on every add/remove, local or remote.
- const summary = `${reaction.count}:${reaction.me}`;
+ // Changes on every add/remove, local or remote — including a same-count swap of who reacted,
+ // which only the realtime reaction revision reveals.
+ const summary = `${reaction.count}:${reaction.me}:${revision}`;
const wantsAvatars = !!groupId && reaction.count > 0 && reaction.count <= REACTION_AVATAR_LIMIT;
// Only badges near the viewport read, so opening a long history does not fire one read per badge.
const badgeRef = useRef<HTMLButtonElement>(null);
diff --git a/apps/desktop/src/renderer/features/bots/ChatServerControls.tsx b/apps/desktop/src/renderer/features/bots/ChatServerControls.tsx
index ba4e00e3d9..9155a1cb9f 100644
--- a/apps/desktop/src/renderer/features/bots/ChatServerControls.tsx
+++ b/apps/desktop/src/renderer/features/bots/ChatServerControls.tsx
@@ -46,7 +46,7 @@ export function ChatMessageActions({ groupId, shareScope, message, members, alig
<div {...{ [SHARE_EXCLUDE_ATTR]: '' }} className="mt-1 flex flex-col gap-1">
{!!message.reactions?.length && <div className={`flex flex-wrap items-center gap-1 ${align === 'right' ? 'justify-end' : ''}`}>
{message.reactions.map(reaction => (
- <ChatReactionButton key={reaction.emoji} groupId={groupId} messageId={message.id} reaction={reaction} members={members}
+ <ChatReactionButton key={reaction.emoji} groupId={groupId} messageId={message.id} reaction={reaction} revision={message.reactionRevision} members={members}
disabled={busy || !groupId} onToggle={() => void react(reaction.emoji, !reaction.me)} />
))}
</div>}
diff --git a/apps/desktop/src/renderer/features/bots/__tests__/ChatReactionButton.test.tsx b/apps/desktop/src/renderer/features/bots/__tests__/ChatReactionButton.test.tsx
index 6fc5d04d76..6cba2c4480 100644
--- a/apps/desktop/src/renderer/features/bots/__tests__/ChatReactionButton.test.tsx
+++ b/apps/desktop/src/renderer/features/bots/__tests__/ChatReactionButton.test.tsx
@@ -138,6 +138,19 @@ describe('reaction avatars', () => {
expect(mocks.reactors).toHaveBeenCalledTimes(2);
});
+ it('re-reads when who reacted changes but the count does not', async () => {
+ mocks.reactors.mockResolvedValueOnce({ ok: true, reactors: [reactor('z', 'Zoe')] })
+ .mockResolvedValueOnce({ ok: true, reactors: [reactor('c1', 'Cindy (Chris)')] });
+ const view = render(<ChatMessageActions shareScope="group:room" groupId="room" members={members} onChanged={vi.fn()}
+ message={{ ...message([{ emoji: '👍', count: 1, me: false }]), reactionRevision: 3 }} />);
+ await waitFor(() => expect(badge().getAttribute('aria-label')).toBe('👍 · 1: Zoe'));
+ // Zoe removed 👍 and Chris's companion added it: same count and `me`, a new realtime revision.
+ view.rerender(<ChatMessageActions shareScope="group:room" groupId="room" members={members} onChanged={vi.fn()}
+ message={{ ...message([{ emoji: '👍', count: 1, me: false }]), reactionRevision: 4 }} />);
+ await waitFor(() => expect(badge().getAttribute('aria-label')).toBe('👍 · 1: Cindy (Chris)'));
+ expect(images()).toEqual(['https://example.test/cindy-chris.png']);
+ });
+
it('keeps click as the toggle', async () => {
mocks.reactors.mockResolvedValue({ ok: true, reactors: [reactor('a', 'Dash', { isSelf: true })] });
render(actions([{ emoji: '👍', count: 1, me: true }]));
diff --git a/docs/product-rules/bot-group-chat.md b/docs/product-rules/bot-group-chat.md
index 876f428f0d..9d978e8877 100644
--- a/docs/product-rules/bot-group-chat.md
+++ b/docs/product-rules/bot-group-chat.md
@@ -365,7 +365,8 @@ Desktop 主群与 Thread 的表情回应徽标参考 Telegram:某个表情的
不额外读取名单。只有接近可视区域的徽标才读取回应者,打开长历史不会逐个徽标发请求。名单按 Actor ID 去重,由主进程读取
Chat Server 既有的回应者接口直到最后一页(离群成员保留回应,总数可超过群人数上限)并按群成员投影命名;
读取只读,不触发伙伴资料同步等任何写入。加载期间按人数预留头像位,不撑动消息;读取失败或名单与人数暂不一致时
-如实显示数字。回应数或自己的状态变化(本地或远端)时重新读取。点击仍是添加/取消回应。回归见 `ChatReactionButton.test.tsx`
+如实显示数字。回应数、自己的状态或实时回应事件(含人数不变、只换了人的情况)变化时重新读取;实时连接断开期间,
+只换人不换数的变化要到重连回放后才更新。点击仍是添加/取消回应。回归见 `ChatReactionButton.test.tsx`
与 `chatServer.test.ts`;手机端暂不显示回应。
当前本地验收与正式切源边界见 [Chat Server 本地联调](../dev-rules/chat-server-local-test.md)。
diff --git a/packages/maker-shared/src/botGroupChat.ts b/packages/maker-shared/src/botGroupChat.ts
index 9302285426..b59096cd36 100644
--- a/packages/maker-shared/src/botGroupChat.ts
+++ b/packages/maker-shared/src/botGroupChat.ts
@@ -145,6 +145,8 @@ export interface BotGroupMessageView {
/** 引用回复: the earlier main-timeline message this one quotes; not a Thread parent. */
quote?: BotGroupQuoteView;
reactions?: Array<{ emoji: string; count: number; me: boolean }>;
+ /** Bumped by realtime reaction changes, so a same-count change of who reacted is still noticed. */
+ reactionRevision?: number;
id: string;
sequence: number;
kind: BotGroupMessageKind;