Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): 群聊发件箱只在换到另一个账号时清空
Dashee7ccd3c5f98
diff --git a/apps/desktop/src/renderer/features/bots/__tests__/botGroupOutbox.test.ts b/apps/desktop/src/renderer/features/bots/__tests__/botGroupOutbox.test.tsindex 854e398f8d..2612829880 100644--- a/apps/desktop/src/renderer/features/bots/__tests__/botGroupOutbox.test.ts+++ b/apps/desktop/src/renderer/features/bots/__tests__/botGroupOutbox.test.ts@@ -28,7 +28,9 @@ import {} from '../botGroupOutbox';const mocks = vi.hoisted(() => ({- owner: 1,+ /** Data owner account (null = an auth boundary's fence); `generation` moves on same-owner repairs. */+ owner: 1 as number | null,+ generation: 0,send: vi.fn<(input: BotGroupSendInput) => Promise<BotGroupSendResult>>(),materialize: vi.fn(async (files: readonly AttachedFile[]) => [...files]),protectedUrls: [] as string[],@@ -39,8 +41,7 @@ const mocks = vi.hoisted(() => ({}));vi.mock('@/contexts/dataOwnerGeneration', () => ({- getDataOwnerGeneration: () => mocks.owner,- isDataOwnerGenerationCurrent: (owner: number) => owner === mocks.owner,+ getDataOwnerGeneration: () => ({ dataOwnerId: mocks.owner === null ? null : `account-${mocks.owner}`, generation: mocks.generation }),}));vi.mock('../botGroupStore', () => ({botGroupApi: () => ({ sendBotGroupMessage: mocks.send, getBotGroup: mocks.getBotGroup }),@@ -140,6 +141,7 @@ function project(groupId: string, messages: BotGroupMessageView[]) {beforeEach(() => {mocks.owner = 1;+ mocks.generation = 0;mocks.send.mockReset();mocks.materialize.mockReset().mockImplementation(async (files: readonly AttachedFile[]) => [...files]);mocks.protectedUrls = [];@@ -756,4 +758,42 @@ describe('botGroupOutbox', () => {expect(mocks.getBotGroup).not.toHaveBeenCalled();});});++ describe('the same account', () => {+ const staged = (id: string) =>+ ({ id, name: `${id}.txt`, path: `/staged/${id}.bin`, size: 1, category: 'text', mimeType: 'text/plain', type: 'file' }) as unknown as AttachedFile;++ it('keeps queued, failed and in-flight messages through a same-owner generation repair and a passing null', async () => {+ const inFlight = deferred();+ mocks.send.mockResolvedValueOnce({ ok: false, errorCode: 'INTERNAL', message: '' }).mockReturnValueOnce(inFlight.promise);+ enqueueBotGroupSend(draft({ clientId: 'failed' }, { files: [staged('failed')] }));+ enqueueBotGroupSend(draft({ clientId: 'flying' }, { files: [staged('flying')] }));+ enqueueBotGroupSend(draft({ clientId: 'queued' }, { files: [staged('queued')] }));+ await flush();+ // A repair keeps the account and moves the generation; an auth boundary publishes null for a moment.+ mocks.generation += 1;+ expect(getBotGroupOutbox('g1').map((entry) => entry.input.clientId)).toEqual(['failed', 'flying', 'queued']);+ mocks.owner = null;+ expect(getBotGroupOutbox('g1')).toHaveLength(3);+ inFlight.resolve({ ok: true, messageId: 'm-flying' });+ mocks.owner = 1;+ mocks.send.mockResolvedValueOnce({ ok: true, messageId: 'm-queued' });+ await flush();+ expect(getBotGroupOutbox('g1').map((entry) => `${entry.input.clientId}:${entry.status}`)).toEqual([+ 'failed:failed', 'flying:sent', 'queued:sent',+ ]);+ expect(mocks.cleanupStaged).not.toHaveBeenCalled();+ });++ it('still clears for a different account, even after a null in between', async () => {+ mocks.send.mockResolvedValueOnce({ ok: false, errorCode: 'INTERNAL', message: '' });+ enqueueBotGroupSend(draft({ clientId: 'failed' }, { files: [staged('failed')] }));+ await flush();+ mocks.owner = null;+ expect(getBotGroupOutbox('g1')).toHaveLength(1);+ mocks.owner = 2;+ expect(getBotGroupOutbox('g1')).toEqual([]);+ expect(mocks.cleanupStaged).toHaveBeenCalledWith([staged('failed')]);+ });+ });});diff --git a/apps/desktop/src/renderer/features/bots/botGroupOutbox.ts b/apps/desktop/src/renderer/features/bots/botGroupOutbox.tsindex e3ed4cc3e8..004cd3a8db 100644--- a/apps/desktop/src/renderer/features/bots/botGroupOutbox.ts+++ b/apps/desktop/src/renderer/features/bots/botGroupOutbox.ts@@ -25,11 +25,12 @@* 全部本地文件,失败时不留任何引用暂存路径的本地记录(服务端只引用已上传的副本)。* - 失败时交还输入框(输入框空着才交还,见 `registerBotGroupComposer`),否则留在时间线上* 显示没发出去,可重试或编辑;失败时输入框不在(切群、分享模式),回来注册输入框时同样交还。- * - 切换账号整体清空,旧账号的迟到回执一律丢弃;群被删除时丢弃该群的条目。+ * - 切换到另一个账号时整体清空,旧账号的迟到回执一律丢弃;同账号的 generation 修复、认证边界+ * 短暂发布的 null、退出后重新登录同一账号都不清空。群被删除时丢弃该群的条目。*/import { useCallback, useSyncExternalStore } from 'react';-import { getDataOwnerGeneration, isDataOwnerGenerationCurrent } from '@/contexts/dataOwnerGeneration';+import { getDataOwnerGeneration } from '@/contexts/dataOwnerGeneration';import { isAnnotationBurnInError, materializeAnnotatedAttachmentsForSend } from '@/lib/annotationBurnIn';import { cleanupStagedChatAttachmentFiles } from '@/lib/chatAttachmentStageCleanup';import { setBotGroupOutboxAttachmentUrls } from '@/lib/composerDraftStore';@@ -111,7 +112,17 @@ const PREVIEW_LIMIT = 32;const NO_PREVIEWS: ReadonlyMap<string, readonly BotGroupAttachment[]> = new Map();const EMPTY: readonly BotGroupOutboxEntry[] = [];-let owner = getDataOwnerGeneration();+/**+ * The account the outbox holds messages for: the last non-null data owner. Only a different+ * account replaces it — not a same-owner generation repair, not the null an auth boundary+ * publishes for a moment, not signing back in to the same account.+ */+let account = getDataOwnerGeneration().dataOwnerId;++function accountChanged(): boolean {+ const current = getDataOwnerGeneration().dataOwnerId;+ return current !== null && current !== account;+}let generation = 0;let tick = 0;const groups = new Map<string, GroupOutbox>();@@ -159,8 +170,11 @@ function releaseUnsubmitted(groupId: string, entries: readonly BotGroupOutboxEnt}function ensureOwner(): void {- if (isDataOwnerGenerationCurrent(owner)) return;- owner = getDataOwnerGeneration();+ if (!accountChanged()) return;+ const previous = account;+ account = getDataOwnerGeneration().dataOwnerId;+ // Nothing was held for another account yet: whatever is here was written for this one.+ if (previous === null) return;generation += 1;// The previous account's queued and failed sends go with it; one being submitted settles itself.for (const [groupId, group] of groups) releaseUnsubmitted(groupId, group.entries);@@ -288,7 +302,7 @@ async function submit(requestGeneration: number,files: readonly AttachedFile[],): Promise<void> {- const isCurrent = () => requestGeneration === generation && isDataOwnerGenerationCurrent(owner);+ const isCurrent = () => requestGeneration === generation && !accountChanged();// Dropped before its turn (account switch, deleted group): whoever dropped it released its files.if (!isCurrent()) return;const group = groups.get(groupId);@@ -606,7 +620,7 @@ export function useBotGroupOutbox(groupId: string): readonly BotGroupOutboxEntry/** Test-only reset of the module state. */export function resetBotGroupOutboxForTests(): void {generation += 1;- owner = getDataOwnerGeneration();+ account = getDataOwnerGeneration().dataOwnerId;groups.clear();preparing.clear();verifying.clear();diff --git a/docs/product-rules/bot-group-chat.md b/docs/product-rules/bot-group-chat.mdindex de2b48c364..8ed0060b4b 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 内存中保留,切群后仍按群隔离,切换账号清空、删群丢弃,不持久化。Thread 回复与手机端仍是等待回执后显示。实现见 `renderer/features/bots/botGroupOutbox.ts`,回归见 `botGroupOutbox.test.ts` 与 `BotGroupChatView.test.tsx`。+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`。完成全部显式目标校验后,最终点名列表继续沿用排除当前用户自己的既有规则;主群、Thread 与手机直连均一致,不因显式列表或所有人标记改变。