botsReplyFormatter.test.ts 9.8 KB

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