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): 群聊发件箱的消息留在发出时的位置,入队烧录失败不再自动重烧

Dashe919341afe2b
diff --git a/apps/desktop/src/renderer/features/bots/BotGroupChatView.tsx b/apps/desktop/src/renderer/features/bots/BotGroupChatView.tsx
index 28a5b517a1..648bf51e61 100644
--- a/apps/desktop/src/renderer/features/bots/BotGroupChatView.tsx
+++ b/apps/desktop/src/renderer/features/bots/BotGroupChatView.tsx
@@ -111,6 +111,7 @@ import {
editBotGroupSend,
getBotGroupOutboxClaims,
getBotGroupOutboxPreviews,
+ interleaveBotGroupOutbox,
mergeBotGroupLocalAttachments,
noteBotGroupTimeline,
projectBotGroupOutbox,
@@ -778,11 +779,29 @@ function BotGroupChatContent({ groupId }: { groupId: string }) {
const openPlan = openBotGroupPlan({ openPlan: group.openPlan, plans });
const followUp = botGroupPlanFollowUp(openPlan);
const continueId = continuableRoundEndId(messages, group.round);
- const timeGroups = collectBotMessageTimeGroups([
- ...messages.map((message) => ({ clientId: message.id, createdAt: message.createdAt })),
- ...pendingSends.map((entry) => ({ clientId: outboxKey(entry), createdAt: entry.createdAt })),
- ]);
const mentionLabels = [t('bots.groupChat.mention.all'), ...group.members.flatMap((member) => [member.name, member.nickname || member.displayName || member.name])];
+ // Messages still in the outbox stay where they were sent, between the timeline's own.
+ const rows = interleaveBotGroupOutbox(messages, pendingSends);
+ const timeGroups = collectBotMessageTimeGroups(rows.map((row) => row.entry
+ ? { clientId: outboxKey(row.entry), createdAt: row.entry.createdAt }
+ : { clientId: row.message.id, createdAt: row.message.createdAt }));
+ const renderOutboxEntry = (entry: BotGroupOutboxEntry) => {
+ const groupTime = timeGroups.get(outboxKey(entry));
+ return (
+ <div key={outboxKey(entry)} className="flex flex-col gap-4">
+ {groupTime !== undefined ? (
+ <p className="select-none text-center text-12 text-[var(--text-tertiary)]">
+ {formatBotMessageGroupTime(groupTime, i18n.language)}
+ </p>
+ ) : null}
+ <BotGroupOutboxItem entry={entry} mentionLabels={mentionLabels}
+ quote={entry.quote ? (
+ <BotGroupQuoteBlock display={resolveBotGroupQuote(entry.quote, quoteSources)}
+ onLocate={() => void locateQuoted(entry.quote!)} align="right" />
+ ) : null} />
+ </div>
+ );
+ };
const running = group.round.status === 'running';
const speakers = running
? group.round.speakers.flatMap((speaker) => {
@@ -855,7 +874,9 @@ function BotGroupChatContent({ groupId }: { groupId: string }) {
</p>
</div>
) : null}
- {messages.map((timelineMessage) => {
+ {rows.map((row) => {
+ if (row.entry) return renderOutboxEntry(row.entry);
+ const timelineMessage = row.message;
const groupTime = timeGroups.get(timelineMessage.id);
// My own message just confirmed: keep the local attachment previews until the server copies are usable.
const local = localAttachments.get(timelineMessage.id);
@@ -934,23 +955,6 @@ function BotGroupChatContent({ groupId }: { groupId: string }) {
</div>
);
})}
- {pendingSends.map((entry) => {
- const groupTime = timeGroups.get(outboxKey(entry));
- return (
- <div key={outboxKey(entry)} className="flex flex-col gap-4">
- {groupTime !== undefined ? (
- <p className="select-none text-center text-12 text-[var(--text-tertiary)]">
- {formatBotMessageGroupTime(groupTime, i18n.language)}
- </p>
- ) : null}
- <BotGroupOutboxItem entry={entry} mentionLabels={mentionLabels}
- quote={entry.quote ? (
- <BotGroupQuoteBlock display={resolveBotGroupQuote(entry.quote, quoteSources)}
- onLocate={() => void locateQuoted(entry.quote!)} align="right" />
- ) : null} />
- </div>
- );
- })}
{openPlan && followUp ? (
<BotGroupPlanFollowUpRow
followUp={followUp}
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 9114b463e6..d85e49a032 100644
--- a/apps/desktop/src/renderer/features/bots/__tests__/BotGroupChatView.test.tsx
+++ b/apps/desktop/src/renderer/features/bots/__tests__/BotGroupChatView.test.tsx
@@ -1249,6 +1249,29 @@ describe('BotGroupChatView', () => {
expect(outboxItems()).toHaveLength(0);
});
+ it('keeps a failed message where it was sent, above messages that came after it', 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: '后发的' } });
+ await first.resolve({ ok: false, errorCode: 'SERVICE_ERROR', message: '' });
+ expect(outboxItems()[0]!.getAttribute('data-status')).toBe('failed');
+
+ mocks.sendBotGroupMessage.mockResolvedValueOnce({ ok: true, messageId: 'u2' });
+ mocks.getBotGroup.mockResolvedValue({ ok: true, group: withSelf('u2', 5, '后发的') });
+ fireEvent.keyDown(input, { key: 'Enter' });
+ await waitFor(() => expect(screen.getAllByText('后发的')).toHaveLength(1));
+ await waitFor(() => expect(outboxItems()).toHaveLength(1));
+ 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 eb600bc51d..0426fa9eda 100644
--- a/apps/desktop/src/renderer/features/bots/__tests__/botGroupOutbox.test.ts
+++ b/apps/desktop/src/renderer/features/bots/__tests__/botGroupOutbox.test.ts
@@ -539,6 +539,26 @@ describe('botGroupOutbox', () => {
expect(restore).toHaveBeenCalledWith(expect.objectContaining({ files: [original] }));
});
+ it('never sends a queued message whose early burn failed until 重试', async () => {
+ const slow = deferred();
+ mocks.send.mockReturnValueOnce(slow.promise).mockResolvedValueOnce({ ok: true, messageId: 'm2' });
+ // The early burn fails; a second burn would succeed.
+ mocks.materialize.mockRejectedValueOnce(new Error('burn')).mockResolvedValue([burned]);
+ enqueueBotGroupSend(draft({ clientId: 'first' }));
+ enqueueBotGroupSend(draft({ clientId: 'drawn' }, { files: [original] }));
+ await flush();
+ slow.resolve({ ok: true, messageId: 'm1' });
+ await flush();
+ // Its turn came: the failure is the message's, not a cue to burn again on its own.
+ expect(getBotGroupOutbox('g1').find((entry) => entry.input.clientId === 'drawn')?.status).toBe('failed');
+ expect(mocks.send).toHaveBeenCalledTimes(1);
+ expect(mocks.materialize).toHaveBeenCalledTimes(1);
+ retryBotGroupSend('g1', 'drawn');
+ await flush();
+ expect(mocks.materialize).toHaveBeenCalledTimes(2);
+ expect(mocks.send.mock.calls[1]![0].attachments?.[0]?.url).toBe('cindy-media://blobs/burned.png');
+ });
+
it('tries the burn again on 重试 after it failed', async () => {
mocks.materialize.mockRejectedValueOnce(new Error('burn')).mockResolvedValueOnce([burned]);
mocks.send.mockResolvedValueOnce({ ok: true, messageId: 'm1' });
diff --git a/apps/desktop/src/renderer/features/bots/botGroupOutbox.ts b/apps/desktop/src/renderer/features/bots/botGroupOutbox.ts
index 847b434ee7..e2b580c524 100644
--- a/apps/desktop/src/renderer/features/bots/botGroupOutbox.ts
+++ b/apps/desktop/src/renderer/features/bots/botGroupOutbox.ts
@@ -85,6 +85,8 @@ export function botGroupSendSignature(input: BotGroupOutboxSendInput, attachment
export interface BotGroupOutboxEntry extends BotGroupOutboxDraft {
status: BotGroupOutboxStatus;
createdAt: number;
+ /** Highest timeline sequence known when the user pressed send: the message shows after it. */
+ after: number;
/** The attachments as the timeline shows them (the burned-in copies once `prepared`). */
attachments: readonly BotGroupAttachment[];
/** `files` with drawn annotations burned in; reused by every (re)try. */
@@ -259,15 +261,21 @@ export function enqueueBotGroupSend(draft: BotGroupOutboxDraft): void {
attachments: displayAttachments(draft.files),
status: 'sending',
createdAt: Date.now(),
+ after: group.latestSequence,
};
group.entries = [...group.entries.filter((existing) => existing.input.clientId !== clientId), entry];
+ // Sent again after 编辑 (same clientId): a burn that failed before is tried afresh.
+ preparing.delete(entryKey(groupId, clientId));
emit();
schedule(groupId, clientId);
// Burn annotations in now, not when the message's turn comes: the bubble shows the result.
if (entry.files.length > 0) void prepare(groupId, clientId).catch(() => undefined);
}
-/** The message's files with annotations burned in, once; a failure is retried by the next call. */
+/**
+ * The message's files with annotations burned in, once. A failure stays until 重试 clears it:
+ * the early burn's failure is the message's, it never goes out on a second try nobody asked for.
+ */
function prepare(groupId: string, clientId: string): Promise<readonly AttachedFile[]> {
const entry = groups.get(groupId)?.entries.find((candidate) => candidate.input.clientId === clientId);
if (!entry) return Promise.reject(new Error('OUTBOX_ENTRY_GONE'));
@@ -283,10 +291,8 @@ function prepare(groupId: string, clientId: string): Promise<readonly AttachedFi
if (requestGeneration === generation) {
update(groupId, clientId, { prepared, attachments: displayAttachments(prepared) });
}
- return prepared;
- })
- .finally(() => {
if (preparing.get(key) === pending) preparing.delete(key);
+ return prepared;
});
preparing.set(key, pending);
return pending;
@@ -297,6 +303,8 @@ export function retryBotGroupSend(groupId: string, clientId: string): void {
ensureOwner();
const entry = groups.get(groupId)?.entries.find((candidate) => candidate.input.clientId === clientId);
if (!entry || entry.status !== 'failed' || entry.retryable === false) return;
+ // A burn that failed is tried again now, on the user's 重试.
+ preparing.delete(entryKey(groupId, clientId));
update(groupId, clientId, { status: 'sending', errorKey: undefined });
schedule(groupId, clientId);
}
@@ -618,6 +626,29 @@ export function projectBotGroupOutbox(
return { pending, localAttachments };
}
+export type BotGroupTimelineRow =
+ | { message: BotGroupMessageView; entry?: undefined }
+ | { entry: BotGroupOutboxEntry; message?: undefined };
+
+/**
+ * The timeline with the messages still in the outbox where they were sent: each after the
+ * messages the timeline had then, so a failed message never moves below later ones. Both lists
+ * are in order (messages by sequence, entries as sent), so the anchors only grow.
+ */
+export function interleaveBotGroupOutbox(
+ messages: readonly BotGroupMessageView[],
+ pending: readonly BotGroupOutboxEntry[],
+): BotGroupTimelineRow[] {
+ const rows: BotGroupTimelineRow[] = [];
+ let at = 0;
+ for (const entry of pending) {
+ while (at < messages.length && messages[at]!.sequence <= entry.after) rows.push({ message: messages[at++]! });
+ rows.push({ entry });
+ }
+ while (at < messages.length) rows.push({ message: messages[at++]! });
+ return rows;
+}
+
function subscribe(listener: () => void): () => void {
listeners.add(listener);
return () => listeners.delete(listener);
diff --git a/docs/product-rules/bot-group-chat.md b/docs/product-rules/bot-group-chat.md
index ec209a8304..5efa98c97c 100644
--- a/docs/product-rules/bot-group-chat.md
+++ b/docs/product-rules/bot-group-chat.md
@@ -352,7 +352,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 与手机直连均一致,不因显式列表或所有人标记改变。