Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): 群聊发件箱本地预览只作显示,删群核验跨 owner 变化不作定论
Dash2f24e0f736a9
diff --git a/apps/desktop/src/renderer/features/bots/BotGroupChatView.tsx b/apps/desktop/src/renderer/features/bots/BotGroupChatView.tsxindex 5182d4c788..181b6f0dfe 100644--- a/apps/desktop/src/renderer/features/bots/BotGroupChatView.tsx+++ b/apps/desktop/src/renderer/features/bots/BotGroupChatView.tsx@@ -272,9 +272,10 @@ function BotGroupChatContent({ groupId }: { groupId: string }) {[state],);// Settle receipts only once the read that shows them is on screen, so a pending bubble- // never disappears a frame before its real message appears.+ // never disappears a frame before its real message appears. Older pages count too: a+ // message pushed off the latest page settles its previews once 查看更早的消息 shows it.useLayoutEffect(() => {- if (state.kind === 'ready') noteBotGroupTimeline(groupId, state.group.messages, state.readToken);+ if (state.kind === 'ready') noteBotGroupTimeline(groupId, mergeBotGroupMessages(state.older, state.group.messages), state.readToken);}, [groupId, state]);const outbox = useBotGroupOutbox(groupId);const { pending: pendingSends, localAttachments } = useMemo(diff --git a/apps/desktop/src/renderer/features/bots/__tests__/BotGroupChatView.test.tsx b/apps/desktop/src/renderer/features/bots/__tests__/BotGroupChatView.test.tsxindex 9b780a3001..000f096d1e 100644--- a/apps/desktop/src/renderer/features/bots/__tests__/BotGroupChatView.test.tsx+++ b/apps/desktop/src/renderer/features/bots/__tests__/BotGroupChatView.test.tsx@@ -13,7 +13,7 @@ import { FeatureSidebarSlotProvider, useFeatureContentHeader } from '../../featuimport { markBotRead, getBotLastReadAt, botGroupReadKey, resetBotReadStateForTests } from '../botReadState';import { botGroupAttachmentScope } from '../botGroupAttachments';import { getBotGroupListSnapshot, refreshBotGroups, resetBotGroupStoreForTests } from '../botGroupStore';-import { getBotGroupOutbox, resetBotGroupOutboxForTests } from '../botGroupOutbox';+import { getBotGroupOutbox, getBotGroupOutboxPreviews, resetBotGroupOutboxForTests } from '../botGroupOutbox';import { BotGroupChatView } from '../BotGroupChatView';import { ControlledBanner, __resetControlledBannerForTests } from '../../remote-device/ControlledBanner';import type {@@ -1382,6 +1382,30 @@ describe('BotGroupChatView', () => {expect(mocks.sendBotGroupMessage.mock.calls[1]![0].clientId).toBe(mocks.sendBotGroupMessage.mock.calls[0]![0].clientId);});+ it('settles the local preview of a message pushed off the latest page once an older page shows it', async () => {+ const send = pendingSend();+ renderView();+ await screen.findByRole('textbox');+ pickFiles([diskFile('photo.png', 'image/png')]);+ await screen.findByAltText('photo.png');+ fireEvent.click(screen.getByRole('button', { name: 'bots.send' }));+ await waitFor(() => expect(mocks.sendBotGroupMessage).toHaveBeenCalledTimes(1));+ // The receipt comes back; the next read's latest page no longer holds the message.+ mocks.getBotGroup.mockResolvedValue({ ok: true, group: detail({ hasMoreBefore: true }) });+ await send.resolve({ ok: true, messageId: 'u-old' });+ pushMessages();+ await waitFor(() => expect(getBotGroupOutbox('g1')).toEqual([]));+ expect(getBotGroupOutboxPreviews('g1').size).toBe(1);++ const image = { id: 's1', name: 'photo.png', category: 'image' as const, mimeType: 'image/png', size: 7, url: 'cindy-media://blobs/server.png', path: null };+ mocks.getBotGroup.mockResolvedValueOnce({+ ok: true,+ group: detail({ messages: [msg({ id: 'u-old', sequence: 0, attachments: [image] })], hasMoreBefore: false }),+ });+ fireEvent.click(await screen.findByRole('button', { name: 'bots.groupChat.timeline.loadEarlier' }));+ await waitFor(() => expect(getBotGroupOutboxPreviews('g1').size).toBe(0));+ });+it('drops the outbox of a group that reads as gone', async () => {renderView();const input = (await screen.findByRole('textbox')) as HTMLTextAreaElement;diff --git a/apps/desktop/src/renderer/features/bots/__tests__/botGroupOutbox.test.ts b/apps/desktop/src/renderer/features/bots/__tests__/botGroupOutbox.test.tsindex 2612829880..55ed1ae97b 100644--- a/apps/desktop/src/renderer/features/bots/__tests__/botGroupOutbox.test.ts+++ b/apps/desktop/src/renderer/features/bots/__tests__/botGroupOutbox.test.ts@@ -42,6 +42,8 @@ const mocks = vi.hoisted(() => ({vi.mock('@/contexts/dataOwnerGeneration', () => ({getDataOwnerGeneration: () => ({ dataOwnerId: mocks.owner === null ? null : `account-${mocks.owner}`, generation: mocks.generation }),+ isDataOwnerGenerationCurrent: (owner: { dataOwnerId: string | null; generation: number }) =>+ owner.dataOwnerId === (mocks.owner === null ? null : `account-${mocks.owner}`) && owner.generation === mocks.generation,}));vi.mock('../botGroupStore', () => ({botGroupApi: () => ({ sendBotGroupMessage: mocks.send, getBotGroup: mocks.getBotGroup }),@@ -796,4 +798,55 @@ describe('botGroupOutbox', () => {expect(mocks.cleanupStaged).toHaveBeenCalledWith([staged('failed')]);});});++ describe('previews of confirmed messages', () => {+ const image = (id: string) =>+ ({ id, name: `${id}.png`, path: `/tmp/${id}.png`, size: 1, category: 'image', mimeType: 'image/png', type: 'image', url: `cindy-media://blobs/${id}.png` }) as unknown as AttachedFile;+ const loading = (messageId: string, name: string) => msg({ id: messageId, sequence: 1, attachments: [+ { id: `media-${messageId}`, name, category: 'image', mimeType: 'image/png', size: 1, url: null, path: null },+ ] });++ it('are not listed for storage cleanup protection: main pinned them when the message was sent', async () => {+ mocks.send.mockResolvedValueOnce({ ok: true, messageId: 'm1' });+ enqueueBotGroupSend(draft({ text: '' }, { files: [image('a')] }));+ await flush();+ noteBotGroupTimeline('g1', [loading('m1', 'a.png')], beginBotGroupTimelineRead());+ expect(getBotGroupOutboxPreviews('g1').size).toBe(1);+ expect(mocks.protectedUrls).toEqual([]);+ });++ it('are never evicted while their server copies are not usable, however many there are', async () => {+ const count = 40;+ for (let i = 0; i < count; i += 1) mocks.send.mockResolvedValueOnce({ ok: true, messageId: `m${i}` });+ for (let i = 0; i < count; i += 1) enqueueBotGroupSend(draft({ clientId: `c${i}`, text: '' }, { files: [image(`p${i}`)] }));+ await flush();+ noteBotGroupTimeline('g1', Array.from({ length: count }, (_, i) => ({ ...loading(`m${i}`, `p${i}.png`), sequence: i + 1 })), beginBotGroupTimelineRead());+ expect(getBotGroupOutboxPreviews('g1').size).toBe(count);+ expect(getBotGroupOutbox('g1')).toEqual([]);+ });++ it('a gone-check read that spans an owner change is no answer; a later read under a stable owner decides', async () => {+ mocks.send.mockResolvedValueOnce({ ok: false, errorCode: 'INTERNAL', message: '' });+ enqueueBotGroupSend(draft({ clientId: 'failed' }, { files: [image('kept')] }));+ await flush();+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] });+ try {+ let answer!: (value: unknown) => void;+ mocks.getBotGroup.mockImplementationOnce(() => new Promise((resolve) => { answer = resolve; }))+ .mockResolvedValueOnce({ ok: false, errorCode: 'NOT_FOUND', message: '' });+ listGroups([]);+ await vi.advanceTimersByTimeAsync(0);+ // Same account, repaired generation while the read was out.+ mocks.generation += 1;+ answer({ ok: false, errorCode: 'NOT_FOUND', message: '' });+ await vi.advanceTimersByTimeAsync(0);+ expect(getBotGroupOutbox('g1')).toHaveLength(1);+ expect(mocks.cleanupStaged).not.toHaveBeenCalled();+ await vi.advanceTimersByTimeAsync(BOT_GROUP_GONE_RETRY_MS[0]);+ expect(getBotGroupOutbox('g1')).toEqual([]);+ } finally {+ vi.useRealTimers();+ }+ });+ });});diff --git a/apps/desktop/src/renderer/features/bots/botGroupOutbox.ts b/apps/desktop/src/renderer/features/bots/botGroupOutbox.tsindex 004cd3a8db..4d8272bcdb 100644--- a/apps/desktop/src/renderer/features/bots/botGroupOutbox.ts+++ b/apps/desktop/src/renderer/features/bots/botGroupOutbox.ts@@ -17,8 +17,9 @@* (同一份烧录结果也用于提交与重试,不重复烧录;放回输入框的仍是带笔迹的原件)。真实消息到了* 而服务器副本还没下载好(没有 url / path,或旧版干脆还没出现在列表里)时,按同一 messageId* 把本地预览接到真实消息上,直到副本可用(见 `mergeBotGroupLocalAttachments`)。- * - 离开输入框的附件不再受草稿保护:发件箱持有的图片地址(含标注原图与本地预览)登记进存储- * 清理的保护清单,条目移除时同步撤下。尚未进入消息的暂存文件按归属回收:还没开始提交的条目+ * - 离开输入框的附件不再受草稿保护:发件箱条目持有的图片地址(含标注原图与烧录副本)登记进+ * 存储清理的保护清单,条目移除时同步撤下;已确认消息的本地预览不登记——发送时 main 已在群的+ * `bot-group-attachment` 引用下固定这些图片。尚未进入消息的暂存文件按归属回收:还没开始提交的条目* (排队中、已失败)归发件箱,群被删除(含不在当前页的群:列表里消失且读取确认已不存在)或* 切换账号时立即回收;已开始提交的条目归它的那次请求,条目在烧录期间被丢弃就不再发出并回收,* 已交给 main 的等结果——成功(已进入消息)不碰,失败才回收。main 的发送在 IPC 返回前读完@@ -30,7 +31,7 @@*/import { useCallback, useSyncExternalStore } from 'react';-import { getDataOwnerGeneration } from '@/contexts/dataOwnerGeneration';+import { getDataOwnerGeneration, isDataOwnerGenerationCurrent } from '@/contexts/dataOwnerGeneration';import { isAnnotationBurnInError, materializeAnnotatedAttachmentsForSend } from '@/lib/annotationBurnIn';import { cleanupStagedChatAttachmentFiles } from '@/lib/chatAttachmentStageCleanup';import { setBotGroupOutboxAttachmentUrls } from '@/lib/composerDraftStore';@@ -108,7 +109,6 @@ interface GroupOutbox {}const CLAIMED_LIMIT = 64;-const PREVIEW_LIMIT = 32;const NO_PREVIEWS: ReadonlyMap<string, readonly BotGroupAttachment[]> = new Map();const EMPTY: readonly BotGroupOutboxEntry[] = [];@@ -131,7 +131,11 @@ const listeners = new Set<() => void>();/** One burn-in per message, shared by the bubble and its submission. */const preparing = new Map<string, Promise<readonly AttachedFile[]>>();-/** Media only this outbox still refers to; storage cleanup must not sweep it. */+/**+ * Media only this outbox still refers to; storage cleanup must not sweep it. Previews of+ * confirmed messages are not listed: the send pinned those images under the group's+ * `bot-group-attachment` reference in main.+ */function protectHeldMedia(): void {const urls: string[] = [];const add = (file: { url?: string | null; annotationSourceUrl?: string }) => {@@ -143,7 +147,6 @@ function protectHeldMedia(): void {entry.files.forEach(add);entry.prepared?.forEach(add);}- for (const preview of group.previews.values()) preview.forEach(add);}setBotGroupOutboxAttachmentUrls(urls);}@@ -399,7 +402,6 @@ export function noteBotGroupTimeline(groupId: string, messages: readonly BotGrouconst message = byId.get(messageId);if (message && botGroupLocalAttachmentsSettled(message.attachments, local)) previews.delete(messageId);}- while (previews.size > PREVIEW_LIMIT) previews.delete(previews.keys().next().value!);if (previews.size !== group.previews.size || [...previews].some(([id, value]) => group.previews.get(id) !== value)) {group.previews = previews;changed = true;@@ -508,11 +510,14 @@ function verifyGroupGone(groupId: string, attempt: number): void {const api = botGroupApi();if (!api) return;const requestGeneration = generation;+ // A read that spans an owner change (same-account repair, an auth boundary's null) is no answer.+ const requestOwner = getDataOwnerGeneration();let definite = false;verifying.add(groupId);void api.getBotGroup(groupId).then((result) => {+ if (!isDataOwnerGenerationCurrent(requestOwner)) return;definite = result.ok || result.errorCode === 'NOT_FOUND';if (requestGeneration === generation && !result.ok && result.errorCode === 'NOT_FOUND') discardBotGroupOutbox(groupId);})diff --git a/docs/product-rules/bot-group-chat.md b/docs/product-rules/bot-group-chat.mdindex 8ed0060b4b..11bc39f93a 100644--- a/docs/product-rules/bot-group-chat.md+++ b/docs/product-rules/bot-group-chat.md@@ -351,7 +351,7 @@ Chris 已明确扩展到多人各带伙伴,并选择「主人按群预先授同一草稿的点名目标 ID、顺序或所有人标记变化,即使显示名字与正文没变,也必须使用新的 `clientId`。只有正文、点名目标、分工标记和附件均不变时,失败重试才复用原发送尝试。-Desktop 主群输入框发送即乐观显示:点发送后消息立即接在时间线末尾,输入框与托盘马上空出,可以接着发下一条;同一个群的提交按发出顺序逐条交给 main,慢于 0.6 秒才在气泡下显示「发送中…」。回执的 `messageId` 是替换依据:时间线读到该消息即换成真实消息,回执之后开始的重读没读到也视为已进历史;推送重读先于回执到达时,只对正在提交的那一条按「基线之后、未被回执认领、正文与附件数一致的自己的消息」做显示去重,最终仍以 `messageId` 为准。附件按输入生成临时列表;带手绘标注的图片入队即烧录,完成后气泡改用烧录后的本地预览,提交与重试复用同一份烧录结果,放回输入框的仍是带笔迹的原件。真实消息到达而服务器副本尚未可用(无 url / path,或旧版尚未列出)时,在同一 `messageId` 内配对:数量一致按发送顺序,旧版逐个列出时按顺序同名(烧录后的名字)配对,继续显示本地预览直到副本可用,不退成缺失、不重复、同名文件不互换。离开输入框的图片(含标注原图与本地预览)登记进存储清理的保护清单直到条目释放;尚未进入消息的暂存文件按归属回收:还没开始提交的条目(排队中、已失败)归发件箱,群被删除(包括不在当前页、从列表消失且读取确认 `NOT_FOUND` 的群;读取不确定时在 5 秒、30 秒、2 分钟后各重试一次,群回到列表或读到仍存在即停止)或切换账号时立即回收;已开始提交的条目归它的那次请求——烧录期间被丢弃的不再发出并回收,已交给 main 的等 main 答复,成功(已进入消息)不回收,失败或调用异常才回收(main 在答复前读完本地文件,失败时不留引用暂存路径的本地记录)。发送失败绝不自动重发:输入框为空(无文字、附件、分工标签)时把正文、点名、标签与附件原样放回并提示原因,再发沿用原 `clientId`;输入框已有新内容时消息留在时间线上显示「没发出去」,可「重试」(同一 `clientId`)或在输入框清空后「编辑」放回;失败时输入框不在(切到别的群、分享模式),回到该群时同样放回空输入框并提示原因。发件箱原样保存并发出完整发送输入(附件入队即开始烧录标注,提交时序列化),首发与重试不挑字段;除 `clientId` 外的全部字段与附件共同决定能否复用原 `clientId`,新增发送字段无需改动发件箱。发件箱只在 renderer 内存中保留,切群后仍按群隔离,切换到另一个账号时清空(同账号的 generation 修复、认证边界短暂的 null 不清空)、删群丢弃,不持久化。Thread 回复与手机端仍是等待回执后显示。实现见 `renderer/features/bots/botGroupOutbox.ts`,回归见 `botGroupOutbox.test.ts` 与 `BotGroupChatView.test.tsx`。+Desktop 主群输入框发送即乐观显示:点发送后消息立即接在时间线末尾,输入框与托盘马上空出,可以接着发下一条;同一个群的提交按发出顺序逐条交给 main,慢于 0.6 秒才在气泡下显示「发送中…」。回执的 `messageId` 是替换依据:时间线读到该消息即换成真实消息,回执之后开始的重读没读到也视为已进历史;推送重读先于回执到达时,只对正在提交的那一条按「基线之后、未被回执认领、正文与附件数一致的自己的消息」做显示去重,最终仍以 `messageId` 为准。附件按输入生成临时列表;带手绘标注的图片入队即烧录,完成后气泡改用烧录后的本地预览,提交与重试复用同一份烧录结果,放回输入框的仍是带笔迹的原件。真实消息到达而服务器副本尚未可用(无 url / path,或旧版尚未列出)时,在同一 `messageId` 内配对:数量一致按发送顺序,旧版逐个列出时按顺序同名(烧录后的名字)配对,继续显示本地预览直到副本可用,不退成缺失、不重复、同名文件不互换。离开输入框、尚未确认的图片(含标注原图与烧录副本)登记进存储清理的保护清单直到条目释放,已确认消息的本地预览不登记(发送时 main 已在群的 `bot-group-attachment` 引用下固定);本地预览按已加载的全部时间线(含更早的页)结算,不设条数上限;尚未进入消息的暂存文件按归属回收:还没开始提交的条目(排队中、已失败)归发件箱,群被删除(包括不在当前页、从列表消失且读取确认 `NOT_FOUND` 的群;读取不确定时在 5 秒、30 秒、2 分钟后各重试一次,群回到列表或读到仍存在即停止)或切换账号时立即回收;已开始提交的条目归它的那次请求——烧录期间被丢弃的不再发出并回收,已交给 main 的等 main 答复,成功(已进入消息)不回收,失败或调用异常才回收(main 在答复前读完本地文件,失败时不留引用暂存路径的本地记录)。发送失败绝不自动重发:输入框为空(无文字、附件、分工标签)时把正文、点名、标签与附件原样放回并提示原因,再发沿用原 `clientId`;输入框已有新内容时消息留在时间线上显示「没发出去」,可「重试」(同一 `clientId`)或在输入框清空后「编辑」放回;失败时输入框不在(切到别的群、分享模式),回到该群时同样放回空输入框并提示原因。发件箱原样保存并发出完整发送输入(附件入队即开始烧录标注,提交时序列化),首发与重试不挑字段;除 `clientId` 外的全部字段与附件共同决定能否复用原 `clientId`,新增发送字段无需改动发件箱。发件箱只在 renderer 内存中保留,切群后仍按群隔离,切换到另一个账号时清空(同账号的 generation 修复、认证边界短暂的 null 不清空)、删群丢弃,不持久化。Thread 回复与手机端仍是等待回执后显示。实现见 `renderer/features/bots/botGroupOutbox.ts`,回归见 `botGroupOutbox.test.ts` 与 `BotGroupChatView.test.tsx`。完成全部显式目标校验后,最终点名列表继续沿用排除当前用户自己的既有规则;主群、Thread 与手机直连均一致,不因显式列表或所有人标记改变。