botsReplyFormatter.test.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. extractBotAssistantResponseMessages,
  4. formatBotAssistantReplyBlocks,
  5. formatBotPermissionRequestSummary,
  6. formatBotToolCallReply,
  7. isBotToolCallReplyTerminal,
  8. updateBotReplyToolCalls,
  9. type BotReplyToolCallState,
  10. } from "../src/bots/replyFormatter.js";
  11. import type { ZCodeStreamEvent, ZCodeTaskChangeSummary } from "@zcode/shared";
  12. const changeSummary: ZCodeTaskChangeSummary = {
  13. fileCount: 2,
  14. added: 12,
  15. removed: 4,
  16. files: [
  17. {
  18. path: "packages/ui/src/App.tsx",
  19. added: 10,
  20. removed: 2,
  21. writeCount: 1,
  22. lastTurnIndex: 1,
  23. },
  24. {
  25. path: "packages/services/src/bots/botsService.ts",
  26. added: 2,
  27. removed: 2,
  28. writeCount: 1,
  29. lastTurnIndex: 1,
  30. },
  31. ],
  32. };
  33. describe("bot reply formatter", () => {
  34. it("formats assistant response blocks with change summary", () => {
  35. const messages = formatBotAssistantReplyBlocks([
  36. { type: "content", content: "已经完成。" },
  37. { type: "change-summary", changeSummary },
  38. ]);
  39. expect(messages).toContain("已经完成。");
  40. expect(messages).toContain("变更摘要:2 个文件,🟢 `+12` 🔴 `-4`\n- `packages/ui/src/App.tsx` (🟢 `+10` 🔴 `-2`)\n- `packages/services/src/bots/botsService.ts` (🟢 `+2` 🔴 `-2`)");
  41. expect(messages.join("\n")).not.toContain("工具调用:");
  42. });
  43. it("formats assistant response blocks with tool block and change summary", () => {
  44. const toolCall: BotReplyToolCallState =
  45. {
  46. toolId: "tool-1",
  47. title: "Edit file",
  48. kind: "edit",
  49. input: { file_path: "/repo/project/packages/ui/src/App.tsx" },
  50. status: "completed",
  51. };
  52. const messages = formatBotAssistantReplyBlocks(
  53. [
  54. { type: "content", content: "已经完成。" },
  55. { type: "tool-call", toolCall },
  56. { type: "change-summary", changeSummary },
  57. ],
  58. { workspacePath: "/repo/project" },
  59. );
  60. expect(messages).toContain("已经完成。");
  61. expect(messages).toContain("工具调用:\n- 完成 · Edit file · `packages/ui/src/App.tsx`");
  62. expect(messages.join("\n")).toContain("变更摘要:2 个文件,🟢 `+12` 🔴 `-4`");
  63. });
  64. it("keeps selected assistant response paragraphs in one message", () => {
  65. const messages = formatBotAssistantReplyBlocks([
  66. { type: "content", content: "第一句。第二句!\n\n第三段" },
  67. { type: "change-summary", changeSummary },
  68. ]);
  69. expect(messages[0]).toBe("第一句。第二句!\n\n第三段");
  70. expect(messages.at(-1)).toContain("变更摘要:2 个文件,🟢 `+12` 🔴 `-4`");
  71. });
  72. it("buffers streaming assistant responses until terminal flush", () => {
  73. expect(
  74. extractBotAssistantResponseMessages("这是第一句。第二句还没换行", false),
  75. ).toEqual({
  76. messages: [],
  77. rest: "这是第一句。第二句还没换行",
  78. });
  79. expect(
  80. extractBotAssistantResponseMessages("第一段。\n\n第二段还在流式输出", false),
  81. ).toEqual({
  82. messages: [],
  83. rest: "第一段。\n\n第二段还在流式输出",
  84. });
  85. expect(
  86. extractBotAssistantResponseMessages("第一段。\n\n第二段", true),
  87. ).toEqual({
  88. messages: ["第一段。\n\n第二段"],
  89. rest: "",
  90. });
  91. });
  92. it("splits the whole selected latest part instead of taking only its last paragraph", () => {
  93. const messages = formatBotAssistantReplyBlocks([
  94. {
  95. type: "content",
  96. content:
  97. "已把 index.html 改成单文件贪吃蛇,直接打开就能玩。包含键盘方向键 / WASD 控制、空格暂停、开始/重开按钮、分数与最高分记录,以及手机端方向按钮。\n\n" +
  98. "我顺手修了一个常见判定问题:蛇在不吃食物时允许走到“尾巴刚离开的位置”,不会被误判成撞到自己,逻辑在 index.html。这次没法实际开浏览器联调,只做了静态检查;如果你要,我可以继续帮你加“加速模式”或“穿墙模式”。",
  99. },
  100. ]);
  101. expect(messages).toEqual([
  102. "已把 index.html 改成单文件贪吃蛇,直接打开就能玩。包含键盘方向键 / WASD 控制、空格暂停、开始/重开按钮、分数与最高分记录,以及手机端方向按钮。\n\n" +
  103. "我顺手修了一个常见判定问题:蛇在不吃食物时允许走到“尾巴刚离开的位置”,不会被误判成撞到自己,逻辑在 index.html。这次没法实际开浏览器联调,只做了静态检查;如果你要,我可以继续帮你加“加速模式”或“穿墙模式”。",
  104. ]);
  105. });
  106. it("collects tool call updates from stream events", () => {
  107. const toolCalls = new Map<string, BotReplyToolCallState>();
  108. updateBotReplyToolCalls(toolCalls, {
  109. type: "tool_call",
  110. taskId: "task-1",
  111. traceId: "trace-1",
  112. toolId: "tool-1",
  113. title: "Run command",
  114. kind: "execute",
  115. input: { command: "pnpm test" },
  116. raw: {},
  117. } as ZCodeStreamEvent);
  118. updateBotReplyToolCalls(toolCalls, {
  119. type: "tool_call_update",
  120. taskId: "task-1",
  121. traceId: "trace-1",
  122. toolId: "tool-1",
  123. status: "completed",
  124. content: "ok",
  125. raw: {},
  126. } as ZCodeStreamEvent);
  127. expect([...toolCalls.values()]).toMatchObject([
  128. {
  129. toolId: "tool-1",
  130. title: "Run command",
  131. kind: "execute",
  132. input: { command: "pnpm test" },
  133. output: "ok",
  134. status: "completed",
  135. },
  136. ]);
  137. });
  138. it("formats individual terminal tool call replies", () => {
  139. expect(isBotToolCallReplyTerminal("completed")).toBe(true);
  140. expect(isBotToolCallReplyTerminal("in_progress")).toBe(false);
  141. expect(
  142. formatBotToolCallReply({
  143. toolId: "tool-1",
  144. title: "Run command",
  145. input: { command: "pnpm test" },
  146. status: "completed",
  147. }),
  148. ).toBe("工具调用:\n- 完成 · Run command · `pnpm test`");
  149. });
  150. it("shortens long run commands in tool call replies", () => {
  151. const longCommand =
  152. "pnpm vitest run packages/services/test/botsReplyFormatter.test.ts packages/services/test/botsService.test.ts --reporter verbose --runInBand";
  153. const text = formatBotToolCallReply({
  154. toolId: "tool-1",
  155. title: "Run command",
  156. input: { command: longCommand },
  157. status: "completed",
  158. });
  159. expect(text).toBe(
  160. "工具调用:\n- 完成 · Run command · `pnpm vitest run packages/services/test/botsReplyFormatter.te ... --reporter verbose --runInBand`",
  161. );
  162. expect(text).not.toContain("botsService.test.ts");
  163. });
  164. it("shortens long run commands in permission request summaries", () => {
  165. const longCommand =
  166. "npm exec very-long-script -- --workspace /Users/me/project --target packages/services/src/bots/replyFormatter.ts --include-generated false";
  167. const text = formatBotPermissionRequestSummary({
  168. title: "Permission Required",
  169. kind: "execute",
  170. raw: { rawInput: { command: longCommand } },
  171. });
  172. expect(text).toBe(
  173. "需要权限:\nPermission Required\n`npm exec very-long-script -- --workspace /Users/me/project - ... er.ts --include-generated false`",
  174. );
  175. expect(text).not.toContain("packages/services/src/bots");
  176. });
  177. it("uses edit kind labels in permission request titles", () => {
  178. const text = formatBotPermissionRequestSummary(
  179. {
  180. title: "Edit /Users/dev/ZCodeProject/Gomoku/index.html",
  181. kind: "edit",
  182. raw: { rawInput: { file_path: "/Users/dev/ZCodeProject/Gomoku/index.html" } },
  183. },
  184. { locale: "en-US" },
  185. );
  186. expect(text).toBe(
  187. "Permission required:\nEditing /Users/dev/ZCodeProject/Gomoku/index.html\n`/Users/dev/ZCodeProject/Gomoku/index.html`",
  188. );
  189. });
  190. it("specializes edit permission kind labels from file changes", () => {
  191. const text = formatBotPermissionRequestSummary(
  192. {
  193. title: "Edit /Users/dev/ZCodeProject/Gomoku/index.html",
  194. kind: "edit",
  195. raw: {
  196. changes: {
  197. "/Users/dev/ZCodeProject/Gomoku/index.html": {
  198. type: "add",
  199. content: "<html></html>",
  200. },
  201. },
  202. },
  203. },
  204. { locale: "en-US" },
  205. );
  206. expect(text).toBe(
  207. "Permission required:\nWriting /Users/dev/ZCodeProject/Gomoku/index.html\n`/Users/dev/ZCodeProject/Gomoku/index.html`",
  208. );
  209. });
  210. it("uses editing for update file change permission labels", () => {
  211. const text = formatBotPermissionRequestSummary(
  212. {
  213. title: "Edit /Users/dev/ZCodeProject/Gomoku/index.html",
  214. kind: "edit",
  215. raw: {
  216. changes: {
  217. "/Users/dev/ZCodeProject/Gomoku/index.html": {
  218. type: "update",
  219. content: "<html></html>",
  220. },
  221. },
  222. },
  223. },
  224. { locale: "en-US" },
  225. );
  226. expect(text).toBe(
  227. "Permission required:\nEditing /Users/dev/ZCodeProject/Gomoku/index.html\n`/Users/dev/ZCodeProject/Gomoku/index.html`",
  228. );
  229. });
  230. it("formats edit tool file paths relative to the workspace", () => {
  231. expect(
  232. formatBotToolCallReply(
  233. {
  234. toolId: "tool-1",
  235. title: "Edit file",
  236. kind: "edit",
  237. input: { file_path: "/Users/me/project/src/demo.md" },
  238. status: "completed",
  239. },
  240. { workspacePath: "/Users/me/project" },
  241. ),
  242. ).toBe("工具调用:\n- 完成 · Edit file · `src/demo.md`");
  243. });
  244. it("escapes inline code markers in change summary paths", () => {
  245. const messages = formatBotAssistantReplyBlocks([
  246. {
  247. type: "change-summary",
  248. changeSummary: {
  249. fileCount: 1,
  250. added: 1,
  251. removed: 0,
  252. files: [
  253. {
  254. path: "src/weird`file.ts",
  255. added: 1,
  256. removed: 0,
  257. writeCount: 1,
  258. lastTurnIndex: 1,
  259. },
  260. ],
  261. },
  262. },
  263. ]);
  264. expect(messages.join("\n")).toContain("- `src/weird\\`file.ts` (🟢 `+1`)");
  265. });
  266. });