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(desktop): 群聊发件箱后发的消息始终排在已确认的先发消息之后

Dasha2c961c81c78
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 6e105fc4fa..1c00d25c1d 100644
--- a/apps/desktop/src/renderer/features/bots/__tests__/BotGroupChatView.test.tsx
+++ b/apps/desktop/src/renderer/features/bots/__tests__/BotGroupChatView.test.tsx
@@ -1274,6 +1274,31 @@ describe('BotGroupChatView', () => {
expect(order).toEqual(['先发的', '后发的']);
});
+ it('keeps a later failed message below an earlier one that went out', async () => {
+ const first = pendingSend();
+ renderView();
+ const input = (await screen.findByRole('textbox')) as HTMLTextAreaElement;
+ fireEvent.change(input, { target: { value: '先发的' } });
+ fireEvent.keyDown(input, { key: 'Enter' });
+ await waitFor(() => expect(mocks.sendBotGroupMessage).toHaveBeenCalledTimes(1));
+ fireEvent.change(input, { target: { value: '后发的' } });
+ fireEvent.keyDown(input, { key: 'Enter' });
+ // New text in the input keeps the failed message on the timeline.
+ fireEvent.change(input, { target: { value: '还在写' } });
+
+ mocks.sendBotGroupMessage.mockResolvedValueOnce({ ok: false, errorCode: 'SERVICE_ERROR', message: '' });
+ mocks.getBotGroup.mockResolvedValue({ ok: true, group: withSelf('u2', 5, '先发的') });
+ await first.resolve({ ok: true, messageId: 'u2' });
+ await waitFor(() => expect(mocks.sendBotGroupMessage).toHaveBeenCalledTimes(2));
+ await waitFor(() => expect(screen.getAllByText('先发的')).toHaveLength(1));
+ await waitFor(() => expect(outboxItems().map((item) => item.getAttribute('data-status'))).toEqual(['failed']));
+ const order = [...document.querySelectorAll('article')]
+ .map((article) => article.textContent ?? '')
+ .filter((text) => text.includes('先发的') || text.includes('后发的'))
+ .map((text) => (text.includes('先发的') ? '先发的' : '后发的'));
+ expect(order).toEqual(['先发的', '后发的']);
+ });
+
it('takes the next message while the first is still out, and sends them in order', async () => {
const first = pendingSend();
renderView();
diff --git a/apps/desktop/src/renderer/features/bots/__tests__/botGroupOutbox.test.ts b/apps/desktop/src/renderer/features/bots/__tests__/botGroupOutbox.test.ts
index a369edea2c..1b87a3bd70 100644
--- a/apps/desktop/src/renderer/features/bots/__tests__/botGroupOutbox.test.ts
+++ b/apps/desktop/src/renderer/features/bots/__tests__/botGroupOutbox.test.ts
@@ -469,6 +469,18 @@ describe('botGroupOutbox', () => {
expect(getBotGroupOutboxPreviews('g1').size).toBe(0);
});
+ it('keeps a later message below the copy a re-read shows of an earlier one', async () => {
+ const pending = deferred();
+ mocks.send.mockReturnValueOnce(pending.promise);
+ noteBotGroupTimeline('g1', [], beginBotGroupTimelineRead());
+ enqueueBotGroupSend(draft({ text: '先发的', clientId: 'a' }));
+ enqueueBotGroupSend(draft({ text: '后发的', clientId: 'b' }));
+ await flush();
+ // Both were sent on the same timeline; the copy of the first lands before its receipt.
+ const projected = project('g1', [msg({ id: 'm1', sequence: 1, content: '先发的' })]);
+ expect(projected.pending.map((entry) => [entry.input.clientId, entry.after])).toEqual([['b', 1]]);
+ });
+
it('lays the previews over the copy a re-read shows before the receipt', async () => {
const pending = deferred();
mocks.send.mockReturnValueOnce(pending.promise);
diff --git a/apps/desktop/src/renderer/features/bots/botGroupOutbox.ts b/apps/desktop/src/renderer/features/bots/botGroupOutbox.ts
index f0db99f3cf..683d4be507 100644
--- a/apps/desktop/src/renderer/features/bots/botGroupOutbox.ts
+++ b/apps/desktop/src/renderer/features/bots/botGroupOutbox.ts
@@ -442,7 +442,18 @@ export function noteBotGroupTimeline(groupId: string, messages: readonly BotGrou
changed = true;
}
if (group.entries.some(settled)) {
- group.entries = group.entries.filter((entry) => !settled(entry));
+ // Messages sent later stay below the settled ones: their anchors move past the real message.
+ let floor = -Infinity;
+ const kept: BotGroupOutboxEntry[] = [];
+ for (const entry of group.entries) {
+ if (!settled(entry)) {
+ kept.push(floor > entry.after ? { ...entry, after: floor } : entry);
+ continue;
+ }
+ const sequence = entry.messageId ? byId.get(entry.messageId)?.sequence : undefined;
+ if (sequence !== undefined) floor = Math.max(floor, sequence);
+ }
+ group.entries = kept;
changed = true;
}
if (changed) emit();
@@ -600,15 +611,28 @@ export function projectBotGroupOutbox(
): BotGroupOutboxProjection {
const localAttachments = new Map(previews);
if (entries.length === 0) return { pending: [], localAttachments };
- const ids = new Set(messages.map((message) => message.id));
const claimedIds = new Set(claimed);
- const pending = entries.filter((entry) => {
+ const sequenceOf = new Map(messages.map((message) => [message.id, message.sequence]));
+ // A message sent later never shows above an earlier one the timeline already has.
+ let floor = -Infinity;
+ const pending: BotGroupOutboxEntry[] = [];
+ const shown = (entry: BotGroupOutboxEntry) => {
+ pending.push(floor > entry.after ? { ...entry, after: floor } : entry);
+ };
+ const replaced = (messageId: string, entry: BotGroupOutboxEntry) => {
+ floor = Math.max(floor, sequenceOf.get(messageId) ?? -Infinity);
+ if (entry.attachments.length > 0) localAttachments.set(messageId, entry.attachments);
+ };
+ for (const entry of entries) {
if (entry.status === 'sent') {
- if (!(entry.messageId && ids.has(entry.messageId))) return true;
- if (entry.attachments.length > 0) localAttachments.set(entry.messageId, entry.attachments);
- return false;
+ if (entry.messageId && sequenceOf.has(entry.messageId)) replaced(entry.messageId, entry);
+ else shown(entry);
+ continue;
+ }
+ if (entry.status !== 'sending' || entry.baseline === undefined) {
+ shown(entry);
+ continue;
}
- if (entry.status !== 'sending' || entry.baseline === undefined) return true;
const baseline = entry.baseline;
const copy = messages.find(
(message) =>
@@ -620,10 +644,9 @@ export function projectBotGroupOutbox(
message.content.trim() === entry.input.text &&
message.attachments.length === entry.files.length,
);
- if (!copy) return true;
- if (entry.attachments.length > 0) localAttachments.set(copy.id, entry.attachments);
- return false;
- });
+ if (copy) replaced(copy.id, entry);
+ else shown(entry);
+ }
return { pending, localAttachments };
}
diff --git a/docs/product-rules/bot-group-chat.md b/docs/product-rules/bot-group-chat.md
index 74c9d96a80..202b872152 100644
--- a/docs/product-rules/bot-group-chat.md
+++ b/docs/product-rules/bot-group-chat.md
@@ -353,7 +353,7 @@ Chris 已明确扩展到多人各带伙伴,并选择「主人按群预先授
同一草稿的点名目标 ID、顺序或所有人标记变化,即使显示名字与正文没变,也必须使用新的 `clientId`。只有正文、点名目标、分工标记和附件均不变时,失败重试才复用原发送尝试。
-Desktop 主群输入框发送即乐观显示:点发送后消息立即接在时间线末尾,输入框与托盘马上空出,可以接着发下一条;同一个群的提交按发出顺序逐条交给 main,慢于 0.6 秒才在气泡下显示「发送中…」。回执的 `messageId` 是替换依据:时间线读到该消息即换成真实消息,回执之后开始的重读没读到也视为已进历史;推送重读先于回执到达时,只对正在提交的那一条按「基线之后、未被回执认领、正文与附件数一致的自己的消息」做显示去重,最终仍以 `messageId` 为准。附件按输入生成临时列表;带手绘标注的图片入队即烧录,完成后气泡改用烧录后的本地预览,提交与重试复用同一份烧录结果,放回输入框的仍是带笔迹的原件;入队烧录失败即这条消息失败,轮到它时不自动再烧,用户「重试」或编辑后重新发送才重新烧录。真实消息到达而服务器副本尚未可用(无 url / path,或旧版尚未列出)时,在同一 `messageId` 内配对:数量一致按发送顺序;副本按下载完成先后逐个列出、数量不一致时,只有名字(烧录后的名字)在这条消息里唯一的附件按名字配对,重名的附件分不出谁是谁,继续显示各自的本地预览、对应副本暂不单独显示,直到数量一致再按顺序替换;不退成缺失、不重复、同名文件不互换。离开输入框、尚未确认的图片(含标注原图与烧录副本)登记进存储清理的保护清单直到条目释放,已确认消息的本地预览不登记(发送时 main 已在群的 `bot-group-attachment` 引用下固定);本地预览按已加载的全部时间线(含更早的页)结算,不设条数上限,消息被删除(含在其他设备上删除)即撤下其本地预览;尚未进入消息的暂存文件按归属回收:还没开始提交的条目(排队中、已失败)归发件箱,群被删除(包括不在当前页、从列表消失且读取确认 `NOT_FOUND` 的群;读取不确定时在 5 秒、30 秒、2 分钟后各重试一次,群回到列表或读到仍存在即停止)时立即回收;已开始提交的条目归它的那次请求——烧录期间被丢弃的不再发出并回收,已交给 main 的等 main 答复,成功(已进入消息)不回收,失败或调用异常才回收(main 在答复前读完本地文件,失败时不留引用暂存路径的本地记录)。切换账号时发件箱同样发出回收,但 main 只删当前账号暂存目录里的副本,renderer 看到切换时 main 已在新账号(或边界)下,所以前一账号的暂存副本留在它自己的目录里,不在发件箱范围内。发送失败绝不自动重发:输入框为空(无文字、附件、分工标签)时把正文、点名、标签与附件原样放回并提示原因,再发沿用原 `clientId`;输入框已有新内容时消息留在时间线上显示「没发出去」(待发与失败的消息都排在按下发送时时间线已有的消息之后,之后到达的消息排在它下面,不会被挤到末尾),可「重试」(同一 `clientId`)或在输入框清空后「编辑」放回;main 拒绝的是消息内容本身时(引用原消息或点名对象已不可用、字段或附件无效:`QUOTE_UNAVAILABLE` / `MENTION_UNAVAILABLE` / `INVALID_PARAMS` / `INVALID_ATTACHMENT` / `ATTACHMENT_TOO_LARGE`),原样重发只会再次失败,只提供「编辑」;失败时输入框不在(切到别的群、分享模式),回到该群时同样放回空输入框并提示原因。「引用回复」随消息进入发件箱:待发气泡上方显示同一个引用块,引用是发送输入的一部分(换引用即换 `clientId`);失败放回时引用只随它自己的正文放回(期间已另选引用的以新选为准),原消息不可用(`QUOTE_UNAVAILABLE`)时只放回正文。发件箱原样保存并发出完整发送输入(附件入队即开始烧录标注,提交时序列化),首发与重试不挑字段;除 `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 在答复前读完本地文件,失败时不留引用暂存路径的本地记录)。切换账号时发件箱同样发出回收,但 main 只删当前账号暂存目录里的副本,renderer 看到切换时 main 已在新账号(或边界)下,所以前一账号的暂存副本留在它自己的目录里,不在发件箱范围内。发送失败绝不自动重发:输入框为空(无文字、附件、分工标签)时把正文、点名、标签与附件原样放回并提示原因,再发沿用原 `clientId`;输入框已有新内容时消息留在时间线上显示「没发出去」(待发与失败的消息都排在按下发送时时间线已有的消息之后,之后到达的消息排在它下面,不会被挤到末尾;比它先发出、已出现在时间线上的自己的消息始终排在它上面),可「重试」(同一 `clientId`)或在输入框清空后「编辑」放回;main 拒绝的是消息内容本身时(引用原消息或点名对象已不可用、字段或附件无效:`QUOTE_UNAVAILABLE` / `MENTION_UNAVAILABLE` / `INVALID_PARAMS` / `INVALID_ATTACHMENT` / `ATTACHMENT_TOO_LARGE`),原样重发只会再次失败,只提供「编辑」;失败时输入框不在(切到别的群、分享模式),回到该群时同样放回空输入框并提示原因。「引用回复」随消息进入发件箱:待发气泡上方显示同一个引用块,引用是发送输入的一部分(换引用即换 `clientId`);失败放回时引用只随它自己的正文放回(期间已另选引用的以新选为准),原消息不可用(`QUOTE_UNAVAILABLE`)时只放回正文。发件箱原样保存并发出完整发送输入(附件入队即开始烧录标注,提交时序列化),首发与重试不挑字段;除 `clientId` 外的全部字段与附件共同决定能否复用原 `clientId`,新增发送字段无需改动发件箱。发件箱只在 renderer 内存中保留,切群后仍按群隔离,切换到另一个账号时清空(同账号的 generation 修复、认证边界短暂的 null 不清空)、删群丢弃,不持久化。Thread 回复与手机端仍是等待回执后显示。实现见 `renderer/features/bots/botGroupOutbox.ts`,回归见 `botGroupOutbox.test.ts` 与 `BotGroupChatView.test.tsx`。
完成全部显式目标校验后,最终点名列表继续沿用排除当前用户自己的既有规则;主群、Thread 与手机直连均一致,不因显式列表或所有人标记改变。