import { Message } from './api/models'; /** * 群聊回合聚合(2026-11-05 重做) * * ## 为什么不用 `turn-aggregator.ts` * * `buildTurns` 是为**单聊**写的:一个房间、一个 typing 信号。 * 群聊是多房间合并 —— 直接套用会出现两个问题(2026-11-05 实测): * * 1. 初始 `/sync` 把各房间的历史全部回填进来,被并成一个回合, * 耗时显示成 `189848s`(约 2.2 天)。 * 2. 两个容器的回复逐句交错,用户分不清谁在说什么。 * * ## 本文件的切分口径 * * 回合锚点由**存储层**保证(`GroupEntry.turnId`),不靠时间戳猜。 * 回复到达时就知道「它属于哪次提问、来自哪个容器」。 * * ``` * 用户:@A @B 各自汇报 * └─ 回合(turnId = 用户消息 id) * ├─ 块 A:A 的全部回复(连续、可展开) * └─ 块 B:B 的全部回复 * ``` */ /** 一个成员在某个回合里的输出块 */ export interface GroupBlock { agentId: string; /** 该块内的消息,按到达顺序 */ messages: Message[]; /** 块内首条时间(ms)。**不取用户消息时间** —— 它表达「容器干了多久」 */ startedAt: number; /** 块内末条时间(ms) */ updatedAt: number; /** 该成员此刻仍在输出 */ typing: boolean; /** 该块是否已关闭(不再新增) */ closed: boolean; } /** 群聊回合:一条用户消息 + 各成员对它的全部回复 */ export interface GroupTurn { /** = 触发它的用户消息 id */ id: string; userMessage: Message; /** 按成员分组;顺序 = 目标成员的声明顺序 */ blocks: GroupBlock[]; /** 该回合被发给的成员 id(`@` 命中的目标) */ targets: string[]; /** 用户消息时间(ms)—— 空块是否该放弃的计时起点 */ userMessageTs: number; /** * 同一回合里用户额外发的附件(2026-11-07)。 * * 用户气泡只承载一条消息;群聊里「文字 + 多个文件」会产生多条用户条目, * 第一个是文字(`userMessage`),其余文件收集在这里并列渲染。 * 没有时为空 —— 调用方无需判空。 */ userFiles?: Message[]; /** * 是否已结束:所有目标成员都不再 typing,且静默超过 `quietMs`。 * * 🔴 只影响「是否显示转圈」,不参与切分 —— 切分由 `turnId` 决定。 */ closed: boolean; } /** 输入:一条群内条目(用户消息或成员回复) */ export interface GroupEntry { /** 全局唯一 id:用户消息 `me-<毫秒>`,回复为 Matrix eventId */ id: string; /** 所属回合 = 用户消息 id */ turnId: string; /** 用户消息为 null;回复为该成员容器 id */ fromAgentId: string | null; message: Message; /** 毫秒时间戳 */ at: number; } /** 回合聚合所需的运行时输入 */ export interface GroupTurnInput { /** 该群会话的全部条目(顺序不限,内部按 at 排序) */ entries: GroupEntry[]; /** * 每条用户消息分发给了哪些成员。 * * 用于在回复尚未到达时也能先渲染出「空块」, * 让用户立刻看到「我已经派给 A 和 B 了」。 */ targetsOf: (turnId: string) => string[]; /** agentId → 是否正在 typing */ typingOf: (agentId: string) => boolean; /** 当前时间(注入便于单测) */ now: number; /** 回合关闭所需的静默时长(ms),缺省 3000 */ quietMs?: number; } /** * 默认静默时长,与单聊 `turn-aggregator.ts` 的 `QUIET_MS` 保持一致。 * * 🔴 2026-11-08 由 3 秒放宽到 25 秒,与单聊同步 —— 群成员跑工具时 * 分段之间同样有十几秒空档,3 秒会把「正在输出」提前判成「已结束」。 */ export const GROUP_QUIET_MS = 25_000; /** * 把群内条目切成回合序列。 * * ## 规则 * * 1. 每个用户消息开一个回合,id = 用户消息 id * 2. 回复按 `turnId` 归入对应回合,再按 `fromAgentId` 分块 * 3. `turnId` 找不到对应用户消息的回复**丢弃**(脏数据兜底) * 4. 块内 `startedAt` / `updatedAt` 只在本块内取 min/max * —— 这是「189848s」不再复发的根本原因 */ export function buildGroupTurns({ entries, targetsOf, typingOf, now, quietMs = GROUP_QUIET_MS, }: GroupTurnInput): GroupTurn[] { /** turnId → 回合(保持用户消息首次出现顺序) */ const byTurn = new Map(); const order: string[] = []; /* * 🔴 先建全部回合,再挂回复(2026-11-05 修正)。 * * 此前是「按时间顺序单遍遍历」—— 一旦某条回复的时间早于用户消息 * (同步回填的历史、或跨设备时钟偏移),它会被当成「找不到锚点」丢弃。 * 两遍处理:第一遍只认用户消息建回合,第二遍挂回复,与时间顺序无关。 */ for (const e of entries) { if (e.fromAgentId !== null) continue; if (byTurn.has(e.turnId)) { /* * 同一回合的第二条用户条目 = 本回合用户额外发的附件(2026-11-07)。 * 主气泡只认第一条(文本),附件挂到 userFiles 上并列渲染。 */ const t = byTurn.get(e.turnId)!; if (e.message.file && e.id !== e.turnId) { (t.userFiles ??= []).push(e.message); } continue; // 重复的用户消息(回显)→ 只认第一条 } byTurn.set(e.turnId, { id: e.turnId, userMessage: e.message, blocks: [], targets: targetsOf(e.turnId), userMessageTs: e.at, closed: false, }); order.push(e.turnId); } // 回合按用户消息时间升序 const msgAt = new Map(); for (const e of entries) { if (e.fromAgentId === null && !msgAt.has(e.turnId)) msgAt.set(e.turnId, e.at); } order.sort((a, b) => (msgAt.get(a) ?? 0) - (msgAt.get(b) ?? 0)); // 第二遍:挂回复(回复块内仍按时间升序) const replies = entries .filter((e) => e.fromAgentId !== null) .sort((a, b) => a.at - b.at); for (const e of replies) { const turn = byTurn.get(e.turnId); // 脏数据:回复找不到锚点回合 → 不展示(不能凭空造一个「用户消息」) if (!turn) continue; let block = turn.blocks.find((b) => b.agentId === e.fromAgentId); if (!block) { block = { agentId: e.fromAgentId!, messages: [], startedAt: e.at, updatedAt: e.at, typing: false, closed: false, }; turn.blocks.push(block); } block.messages.push(e.message); // 🔴 只在本块内取 min/max —— 跨块会重新引入「189848s」 block.startedAt = Math.min(block.startedAt, e.at); block.updatedAt = Math.max(block.updatedAt, e.at); } for (const id of order) { const turn = byTurn.get(id)!; // 补齐未响应成员的占位块(让用户看到「我派给它了,它还没回」) for (const agentId of turn.targets) { if (!turn.blocks.some((b) => b.agentId === agentId)) { turn.blocks.push({ agentId, messages: [], startedAt: 0, updatedAt: 0, typing: false, closed: false, }); } } // 块按目标声明顺序排列,未在 targets 里的(历史脏数据)追加在后 const rank = (id2: string): number => { const i = turn.targets.indexOf(id2); return i < 0 ? Number.MAX_SAFE_INTEGER : i; }; turn.blocks.sort((a, b) => rank(a.agentId) - rank(b.agentId)); for (const b of turn.blocks) { b.typing = typingOf(b.agentId); /* * 🔴 空块必须可以关闭(2026-11-05 评审修复)。 * `updatedAt` 为 0 时 `now - 0` 恒大于 quietMs,看似会关闭; * 但若成员一直不回复,旧逻辑在 `typing` 抖动时会反复显示「执行中」。 * 这里改为:空块以**用户消息时间**起算,超过 quietMs 即视为放弃。 */ if (b.messages.length === 0) { b.closed = !b.typing && now - turn.userMessageTs >= quietMs; } else { b.closed = !b.typing && now - b.updatedAt >= quietMs; } } turn.closed = turn.blocks.every((b) => b.closed); } return order.map((id) => byTurn.get(id)!); } /** * 块耗时(ms):块内首条到末条。 * * 空块(尚未回复)返回 0,不显示。 */ export function blockDuration(b: GroupBlock): number { if (b.messages.length === 0 || b.startedAt === 0) return 0; return Math.max(0, b.updatedAt - b.startedAt); }