| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- import { Message } from './api/models';
- /**
- * 抢答检测 —— 「我 @ 的是 A,回话的却是 B」。
- *
- * ## 实测背景(2026-10-28)
- *
- * 在群里 @ 了 `lj-test` 派活,结果**执行的是 agent-lj**,`lj-test` 完全没响应。
- * 查容器配置:`agent-lj` 的 `MATRIX_REQUIRE_MENTION=false` →
- * @ 判定门直接短路 → **群里任何消息它都会处理,不管 @ 的是谁**。
- *
- * 另有隐蔽坑:容器的 @ 判定是**子串/正则匹配**,
- * `@lj-test` 的文本里含 `lj`,可能被 `agent-lj` 误认成在叫自己。
- *
- * ## 这个模块做什么
- *
- * App 改不了容器的 @ 行为,但**必须让用户看见**这个错位 ——
- * 否则用户会以为「组员干了活」,实际是组长抢着干了,组内根本没有分工。
- */
- export interface ReplyOwner {
- /** 回复者 Matrix ID */
- senderId: string;
- /** 回复者展示名 */
- senderName?: string;
- /** 回复时间 */
- ts: number;
- }
- export interface HijackReport {
- /** 用户 @ 了谁(Matrix ID) */
- expected: string[];
- /** 实际回话的人 */
- replied: ReplyOwner[];
- /** @ 了却没回的人 */
- silent: string[];
- /** 没被 @ 却回了话的人(抢答者) */
- intruders: ReplyOwner[];
- /** 是否存在抢答 */
- hasHijack: boolean;
- }
- /**
- * 分析一轮派活:谁被叫了、谁回应了、谁抢了。
- *
- * @param expectedIds 用户 @ 的 Matrix ID 列表
- * @param replies 该轮之后收到的、来自 agent 的回复
- */
- export function detectHijack(expectedIds: string[], replies: ReplyOwner[]): HijackReport {
- const expected = [...new Set(expectedIds.filter(Boolean))];
- if (expected.length === 0) {
- return { expected, replied: replies, silent: [], intruders: [], hasHijack: false };
- }
- const expectedSet = new Set(expected);
- const repliedIds = new Set(replies.map((r) => r.senderId));
- const intruders = replies.filter((r) => !expectedSet.has(r.senderId));
- // 用 id 去重,同一个人回多条只报一次
- const seen = new Set<string>();
- const uniqueIntruders = intruders.filter((r) => {
- if (seen.has(r.senderId)) return false;
- seen.add(r.senderId);
- return true;
- });
- return {
- expected,
- replied: replies,
- silent: expected.filter((id) => !repliedIds.has(id)),
- intruders: uniqueIntruders,
- hasHijack: uniqueIntruders.length > 0,
- };
- }
- /**
- * 从消息列表里挑出「某条消息之后、来自 agent 的回复」。
- *
- * @param all 群消息(时间升序)
- * @param afterTs 起点时间(毫秒),通常是被 @ 的那条消息的时间
- * @param myId 当前用户的 Matrix ID(排除自己)
- */
- export function repliesAfter(all: Message[], afterTs: number, myId: string): ReplyOwner[] {
- return all
- .filter((m) => m.sender === 'agent' && Date.parse(m.ts) > afterTs)
- .map((m) => ({ senderId: m.senderId ?? '', senderName: m.senderName, ts: Date.parse(m.ts) }))
- .filter((r) => r.senderId && r.senderId !== myId);
- }
- /** 给用户看的提示文案(无抢答时返回 null) */
- export function hijackHint(report: HijackReport, nameOf: (id: string) => string): string | null {
- if (!report.hasHijack) return null;
- const intruders = report.intruders.map((r) => r.senderName || nameOf(r.senderId)).join('、');
- const expected = report.expected.map(nameOf).join('、');
- if (report.silent.length > 0) {
- return (
- `你 @ 的是 ${expected},但实际回应的是 ${intruders},${expected} 没有响应。` +
- '这通常是对方容器未开启「只在被 @ 时响应」(MATRIX_REQUIRE_MENTION),导致它在群里处理了所有消息。'
- );
- }
- return `你 @ 的是 ${expected},${intruders} 也一并响应了 —— 检查它的 @ 判定配置。`;
- }
|