hijack-detect.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { Message } from './api/models';
  2. /**
  3. * 抢答检测 —— 「我 @ 的是 A,回话的却是 B」。
  4. *
  5. * ## 实测背景(2026-10-28)
  6. *
  7. * 在群里 @ 了 `lj-test` 派活,结果**执行的是 agent-lj**,`lj-test` 完全没响应。
  8. * 查容器配置:`agent-lj` 的 `MATRIX_REQUIRE_MENTION=false` →
  9. * @ 判定门直接短路 → **群里任何消息它都会处理,不管 @ 的是谁**。
  10. *
  11. * 另有隐蔽坑:容器的 @ 判定是**子串/正则匹配**,
  12. * `@lj-test` 的文本里含 `lj`,可能被 `agent-lj` 误认成在叫自己。
  13. *
  14. * ## 这个模块做什么
  15. *
  16. * App 改不了容器的 @ 行为,但**必须让用户看见**这个错位 ——
  17. * 否则用户会以为「组员干了活」,实际是组长抢着干了,组内根本没有分工。
  18. */
  19. export interface ReplyOwner {
  20. /** 回复者 Matrix ID */
  21. senderId: string;
  22. /** 回复者展示名 */
  23. senderName?: string;
  24. /** 回复时间 */
  25. ts: number;
  26. }
  27. export interface HijackReport {
  28. /** 用户 @ 了谁(Matrix ID) */
  29. expected: string[];
  30. /** 实际回话的人 */
  31. replied: ReplyOwner[];
  32. /** @ 了却没回的人 */
  33. silent: string[];
  34. /** 没被 @ 却回了话的人(抢答者) */
  35. intruders: ReplyOwner[];
  36. /** 是否存在抢答 */
  37. hasHijack: boolean;
  38. }
  39. /**
  40. * 分析一轮派活:谁被叫了、谁回应了、谁抢了。
  41. *
  42. * @param expectedIds 用户 @ 的 Matrix ID 列表
  43. * @param replies 该轮之后收到的、来自 agent 的回复
  44. */
  45. export function detectHijack(expectedIds: string[], replies: ReplyOwner[]): HijackReport {
  46. const expected = [...new Set(expectedIds.filter(Boolean))];
  47. if (expected.length === 0) {
  48. return { expected, replied: replies, silent: [], intruders: [], hasHijack: false };
  49. }
  50. const expectedSet = new Set(expected);
  51. const repliedIds = new Set(replies.map((r) => r.senderId));
  52. const intruders = replies.filter((r) => !expectedSet.has(r.senderId));
  53. // 用 id 去重,同一个人回多条只报一次
  54. const seen = new Set<string>();
  55. const uniqueIntruders = intruders.filter((r) => {
  56. if (seen.has(r.senderId)) return false;
  57. seen.add(r.senderId);
  58. return true;
  59. });
  60. return {
  61. expected,
  62. replied: replies,
  63. silent: expected.filter((id) => !repliedIds.has(id)),
  64. intruders: uniqueIntruders,
  65. hasHijack: uniqueIntruders.length > 0,
  66. };
  67. }
  68. /**
  69. * 从消息列表里挑出「某条消息之后、来自 agent 的回复」。
  70. *
  71. * @param all 群消息(时间升序)
  72. * @param afterTs 起点时间(毫秒),通常是被 @ 的那条消息的时间
  73. * @param myId 当前用户的 Matrix ID(排除自己)
  74. */
  75. export function repliesAfter(all: Message[], afterTs: number, myId: string): ReplyOwner[] {
  76. return all
  77. .filter((m) => m.sender === 'agent' && Date.parse(m.ts) > afterTs)
  78. .map((m) => ({ senderId: m.senderId ?? '', senderName: m.senderName, ts: Date.parse(m.ts) }))
  79. .filter((r) => r.senderId && r.senderId !== myId);
  80. }
  81. /** 给用户看的提示文案(无抢答时返回 null) */
  82. export function hijackHint(report: HijackReport, nameOf: (id: string) => string): string | null {
  83. if (!report.hasHijack) return null;
  84. const intruders = report.intruders.map((r) => r.senderName || nameOf(r.senderId)).join('、');
  85. const expected = report.expected.map(nameOf).join('、');
  86. if (report.silent.length > 0) {
  87. return (
  88. `你 @ 的是 ${expected},但实际回应的是 ${intruders},${expected} 没有响应。` +
  89. '这通常是对方容器未开启「只在被 @ 时响应」(MATRIX_REQUIRE_MENTION),导致它在群里处理了所有消息。'
  90. );
  91. }
  92. return `你 @ 的是 ${expected},${intruders} 也一并响应了 —— 检查它的 @ 判定配置。`;
  93. }