message-segments.spec.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import { hasProcess, isToolActivityMessage, parseSegments, splitMessage, stepCountLabel, toolActivitySummary } from './message-segments';
  2. /**
  3. * 过程行拆分(用于「工具调用折叠」)。
  4. *
  5. * 设计原则:**宁可漏判,不可误判** —— 把用户要看的结果折进去,
  6. * 比多留一行过程文本糟糕得多。
  7. */
  8. describe('message-segments', () => {
  9. describe('parseSegments 判定', () => {
  10. it('圆点 + 工具名 → tool 步', () => {
  11. const [s] = parseSegments('· bash');
  12. expect(s).toEqual({ kind: 'tool', label: 'bash' } as never);
  13. });
  14. it('圆点 + 工具名 + 参数 → detail 分离', () => {
  15. const [s] = parseSegments('· bash: git status --short');
  16. expect(s).toEqual({ kind: 'tool', label: 'bash', detail: 'git status --short' });
  17. });
  18. it('圆点后接中文短语 → 不算工具(交回正文)', () => {
  19. const [s] = parseSegments('· 已完成');
  20. expect(s.kind).toBe('text');
  21. });
  22. it('「正在…」短句 → status 步', () => {
  23. const [s] = parseSegments('正在读取配置文件…');
  24. expect(s.kind).toBe('status');
  25. });
  26. it('✅ 完成行 → status 步', () => {
  27. const [s] = parseSegments('✅ 3 个文件已处理');
  28. expect(s.kind).toBe('status');
  29. });
  30. it('🔴 长句即使以「正在」开头也不折叠(避免吃掉正文)', () => {
  31. const long = '正在说明一下:这次的问题是上报器把自己的心跳写进了 currentTask 字段,' +
  32. '导致用户看到永远正在执行,需要改成从 state.db 判断。';
  33. const [s] = parseSegments(long);
  34. expect(s.kind).toBe('text');
  35. });
  36. it('普通正文 → text', () => {
  37. const [s] = parseSegments('我看了下环境,没问题。');
  38. expect(s.kind).toBe('text');
  39. });
  40. it('空行 → blank', () => {
  41. const [s] = parseSegments(' ');
  42. expect(s.kind).toBe('blank');
  43. });
  44. });
  45. describe('toBlocks / splitMessage 分组', () => {
  46. it('连续过程行合成一个 block', () => {
  47. const blocks = splitMessage('先看环境。\n· bash\n· read_file\n· edit_file\n改好了。');
  48. const kinds = blocks.map((b) => b.kind);
  49. expect(kinds).toEqual(['text', 'process', 'text']);
  50. const proc = blocks[1] as { steps: unknown[] };
  51. expect(proc.steps.length).toBe(3);
  52. });
  53. it('过程行之间的空行不打断分组', () => {
  54. const blocks = splitMessage('· bash\n\n· read_file\n\n结论如下。');
  55. expect(blocks[0].kind).toBe('process');
  56. expect((blocks[0] as { steps: unknown[] }).steps.length).toBe(2);
  57. });
  58. it('孤立单步不折叠(内联成一行,少点一次)', () => {
  59. const blocks = splitMessage('正文一\n· bash\n正文二');
  60. expect(blocks.every((b) => b.kind !== 'process')).toBe(true);
  61. expect(blocks.some((b) => b.kind === 'text' && b.text.includes('bash'))).toBe(true);
  62. });
  63. it('没有过程行的消息 → hasProcess 为 false(走整段 markdown)', () => {
  64. expect(hasProcess({ text: '这是一段普通回复,含 **加粗**。\n\n第二段。' })).toBe(false);
  65. });
  66. it('含过程行 → hasProcess 为 true', () => {
  67. expect(hasProcess({ text: '· bash\n· read_file\n好的。' })).toBe(true);
  68. });
  69. it('步骤数文案', () => {
  70. expect(stepCountLabel(3)).toBe('运行了 3 步');
  71. });
  72. });
  73. describe("容器终端活动消息(实测格式)", () => {
  74. it("💻 terminal + 代码块 → 是终端活动", () => {
  75. expect(isToolActivityMessage("💻 terminal\\n```\\ncd /opt/data && chmod +x x.sh\\n```")).toBeTrue();
  76. });
  77. it("✍️ Writing 路径 + 命令 → 是终端活动", () => {
  78. expect(isToolActivityMessage("✍️ Writing /opt/data/work/i.html 💻 terminal\\n```\\nnode -e x\\n```")).toBeTrue();
  79. });
  80. it("带 emoji 的单行状态(攒经验)→ 是终端活动", () => {
  81. expect(isToolActivityMessage("💾 攒经验中,已入账:🔧 「ops」强化成功")).toBeTrue();
  82. });
  83. it("🔴 正常回复带一个 emoji → 不算终端活动(不能误折)", () => {
  84. expect(isToolActivityMessage("修复验证通过 ✅ 字节数一致,本地与云端 MD5 相同。")).toBeFalse();
  85. });
  86. it("🔴 长段落即使含命令片段 → 不算终端活动", () => {
  87. const long = "我执行了 curl 命令做验证,返回的是 200,说明整条链路是通的,这一步之后就可以把结果整理成报告了,目前没有未完成的事项需要处理。";
  88. expect(isToolActivityMessage(long)).toBeFalse();
  89. });
  90. it("空文本 → false", () => {
  91. expect(isToolActivityMessage("")).toBeFalse();
  92. expect(isToolActivityMessage(undefined)).toBeFalse();
  93. });
  94. it("摘要取第一行动作名", () => {
  95. expect(toolActivitySummary("💻 terminal\\n```\\nls\\n```")).toContain("terminal");
  96. });
  97. });
  98. /*
  99. * 2026-11-10 实测原文(lj-test 容器在临时房间里的真实输出)。
  100. *
  101. * 容器的进度消息是 `m.text`,且**同一个气泡里堆了多行工具行**,
  102. * 部分还带 `(×2)`、`(×3)` 这种折叠计数后缀。旧实现在「每一行都是工具行」
  103. * 时才整条折叠,只要混进一行中文叙述就整条进正文 —— 用户看到满屏 `🐍 Running code…`。
  104. */
  105. describe('容器进度消息(2026-11-10 实测原文)', () => {
  106. it('多行 🐍 Running code 堆叠 → 是终端活动', () => {
  107. const txt = [
  108. '* 🐍 Running code from hermes_tools import terminal # H...',
  109. '🐍 Running code from hermes_tools import terminal r=t...',
  110. ].join('\n');
  111. expect(isToolActivityMessage(txt)).toBeTrue();
  112. });
  113. it('带折叠计数 (×2) (×3) 仍识别为终端活动', () => {
  114. const txt = [
  115. '* 🐍 Running code from hermes_tools import terminal r=t...',
  116. '🐍 Running code from hermes_tools import search_files...',
  117. '🐍 Running code from hermes_tools import read_file, s...',
  118. '🐍 Running code from hermes_tools import search_files...',
  119. ].join('\n');
  120. expect(isToolActivityMessage(txt)).toBeTrue();
  121. });
  122. it('🔴 工具行 + 一句中文叙述混排 → 仍是终端活动(整条进过程区,不刷屏正文)', () => {
  123. const txt = [
  124. '🐍 Running code from hermes_tools import terminal r=t...',
  125. '现在我来检查 drain 脚本、配置里与日志和消息类型有关的部分。',
  126. '🐍 Running code from hermes_tools import terminal, re...',
  127. ].join('\n');
  128. expect(isToolActivityMessage(txt)).toBeTrue();
  129. });
  130. it('🔴 纯粹的中文分析仍是正文(不能因为常和工具混排就被误折)', () => {
  131. const txt = '我发现 ArtifactSyncService 这条链路其实是通用的,只不过之前从来没有容器把 artifacts 写进来过,所以右栏一直是空的。';
  132. expect(isToolActivityMessage(txt)).toBeFalse();
  133. });
  134. });
  135. });
  136. describe('emoji 工具行(2026-11-06 补齐)', () => {
  137. it('🔴 💻 terminal 被识别为过程行', () => {
  138. const seg = parseSegments('💻 terminal\n```\ncd /opt/data\n```');
  139. expect(seg[0].kind).toBe('tool');
  140. });
  141. it('🌐 browser_exec 带说明也算过程', () => {
  142. const seg = parseSegments('🌐 browser_exec: "打开本地表格页面并验证渲染"');
  143. expect(seg[0].kind).toBe('tool');
  144. });
  145. it('✍️ Writing 路径算过程', () => {
  146. expect(parseSegments('✍️ Writing /opt/data/work/index.html')[0].kind).toBe('tool');
  147. });
  148. it('🔴 ✅ 完成行判为 status(既有规则,不被 emoji 工具行抢占)', () => {
  149. // 既有设计:✅ 开头是「完成状态」过程行,会被折叠 —— 与 tool 区分开
  150. expect(parseSegments('✅ 全部测试通过')[0].kind).toBe('status');
  151. });
  152. it('❌ 失败说明按既有规则判为 status(不是 tool)', () => {
  153. const seg = parseSegments('❌ 依赖安装失败')[0];
  154. expect(seg.kind === 'status' || seg.kind === 'text').toBe(true);
  155. });
  156. it('正文段落不会被误判', () => {
  157. expect(parseSegments('我先看一下环境再决定怎么做。')[0].kind).toBe('text');
  158. });
  159. it('🔴 混合消息:正文 + 工具行 + 正文 → 中间被折叠', () => {
  160. const blocks = splitMessage(
  161. ['好,我先做一个页面。', '💻 terminal', '```', 'cd /opt/data', '```', '静态自检通过。'].join('\n'),
  162. );
  163. const kinds = blocks.map((b) => b.kind);
  164. expect(kinds).toContain('process');
  165. expect(kinds[0]).toBe('text');
  166. expect(kinds[kinds.length - 1]).toBe('text');
  167. });
  168. });