v4ToolCallRowAdapter.test.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // M5④ 阶段2:v4 ToolCallRow → 旧 ToolCallBlocks 输入形态适配单测。
  2. import { describe, expect, it } from "vitest";
  3. import type { ToolCallRow } from "@zcode/shared/zcode-protocol-v4";
  4. import { buildToolDisplayModel } from "@/lib/toolDisplay.js";
  5. import { toolCallRowToLegacyNode } from "@/v4/toolCallRowAdapter.js";
  6. function makeRow(overrides: Partial<ToolCallRow> = {}): ToolCallRow {
  7. return {
  8. rowId: 7,
  9. turnId: "turn-1",
  10. createdAt: 1000,
  11. createdAtSeq: 3,
  12. kind: "toolCall",
  13. toolCallId: "call-abc",
  14. toolName: "Bash",
  15. status: "running",
  16. inputText: '{"command":"ls -la"}',
  17. input: { command: "ls -la" },
  18. ...overrides,
  19. };
  20. }
  21. describe("toolCallRowToLegacyNode", () => {
  22. it("字段映射:toolId/toolName/kind/input/startedAt,无子树", () => {
  23. const node = toolCallRowToLegacyNode(makeRow({ startedAt: 1234 }));
  24. expect(node.toolCall.toolId).toBe("call-abc");
  25. expect(node.toolCall.toolName).toBe("Bash");
  26. expect(node.toolCall.kind).toBe("Bash");
  27. expect(node.toolCall.input).toEqual({ command: "ls -la" });
  28. expect(node.toolCall.startedAt).toBe(1234);
  29. expect(node.childToolCalls).toEqual([]);
  30. });
  31. it("把投影时固化的 CUA App 身份桥接给摘要 renderer", () => {
  32. const cuaApp = { pid: 42, name: "Zed", bundleId: "dev.zed.Zed" };
  33. const node = toolCallRowToLegacyNode(makeRow({ cuaApp }));
  34. expect(node.toolCall.raw).toMatchObject({ cuaApp });
  35. });
  36. it("status 全量映射到 mapToolStatus 词表", () => {
  37. const cases: Array<[ToolCallRow["status"], string]> = [
  38. ["inputStreaming", "pending"],
  39. ["pendingApproval", "pending"],
  40. ["running", "in_progress"],
  41. ["success", "completed"],
  42. ["error", "failed"],
  43. ["cancelled", "stopped"],
  44. ];
  45. for (const [v4Status, legacy] of cases) {
  46. expect(toolCallRowToLegacyNode(makeRow({ status: v4Status })).toolCall.status).toBe(legacy);
  47. }
  48. });
  49. it("output/error 映射:终态文本与错误信息透传", () => {
  50. const node = toolCallRowToLegacyNode(
  51. makeRow({
  52. status: "error",
  53. output: { text: "partial output" },
  54. error: { code: "toolFailed", message: "command not found" },
  55. }),
  56. );
  57. expect(node.toolCall.output).toBe("partial output");
  58. expect(node.toolCall.error).toBe("command not found");
  59. });
  60. it.each([
  61. {
  62. kind: "task_output",
  63. retrievalStatus: "not_ready",
  64. taskStatus: "running",
  65. output: "partial output",
  66. },
  67. {
  68. kind: "respond_to_coordinator",
  69. status: "success",
  70. },
  71. ] as const)("保留 $kind display 给专属 renderer", (display) => {
  72. const node = toolCallRowToLegacyNode(
  73. makeRow({
  74. status: "success",
  75. display,
  76. }),
  77. );
  78. expect(node.toolCall.raw).toMatchObject({ display });
  79. });
  80. it("可信 MCP discovery 展示元数据透传到 legacy raw,不反拆合成工具名", () => {
  81. const node = toolCallRowToLegacyNode(
  82. makeRow({
  83. toolName: "custom-provider-visible-name",
  84. status: "success",
  85. display: {
  86. kind: "mcp_tool",
  87. serverName: "company-github-prod",
  88. toolName: "issue_query_v2",
  89. description: "Query issues visible to the current user.",
  90. },
  91. }),
  92. );
  93. expect(node.toolCall.raw).toMatchObject({
  94. display: {
  95. kind: "mcp_tool",
  96. serverName: "company-github-prod",
  97. toolName: "issue_query_v2",
  98. },
  99. });
  100. });
  101. it("把 output.display 中的 CUA 截图桥接到 legacy raw", () => {
  102. const node = toolCallRowToLegacyNode(
  103. makeRow({
  104. toolName: "mcp__computer-use__computer-use",
  105. status: "success",
  106. output: {
  107. text: "[Attached image/jpeg: MCP image]",
  108. display: {
  109. kind: "cua",
  110. schemaVersion: 1,
  111. toolName: "computer-use",
  112. status: "success",
  113. input: '{"action":"screenshot"}',
  114. media: [{ mimeType: "image/jpeg", data: "AAAA" }],
  115. },
  116. },
  117. }),
  118. );
  119. expect(node.toolCall.raw).toMatchObject({
  120. display: {
  121. kind: "cua",
  122. media: [{ mimeType: "image/jpeg", data: "AAAA" }],
  123. },
  124. });
  125. expect(node.toolCall.raw?.display).not.toHaveProperty("input");
  126. });
  127. it("继续兼容顶层 Node REPL 图片 display", () => {
  128. const node = toolCallRowToLegacyNode(
  129. makeRow({
  130. display: {
  131. kind: "node_repl_images",
  132. images: [{ base64: "AAAA", mimeType: "image/png" }],
  133. },
  134. }),
  135. );
  136. expect(node.toolCall.raw).toMatchObject({
  137. display: {
  138. kind: "node_repl_images",
  139. images: [{ base64: "AAAA", mimeType: "image/png" }],
  140. },
  141. });
  142. });
  143. it("background Agent 的终态 output 同时桥接为可展开活动正文", () => {
  144. const node = toolCallRowToLegacyNode(
  145. makeRow({
  146. toolName: "Agent",
  147. status: "error",
  148. output: { text: "Agent task failed. Requests are too frequent." },
  149. error: {
  150. code: "fault.runtime.backgroundTaskFailed",
  151. message: "Requests are too frequent.",
  152. },
  153. }),
  154. );
  155. expect(node.toolCall.content).toBe("Agent task failed. Requests are too frequent.");
  156. expect(node.toolCall.status).toBe("failed");
  157. expect(node.toolCall.error).toBe("Requests are too frequent.");
  158. });
  159. it("status=error 但 error.message 缺席时,从 tagged output 还原错误正文", () => {
  160. const node = toolCallRowToLegacyNode(
  161. makeRow({
  162. status: "error",
  163. output: {
  164. text: "<tool_use_error>Permission denied: /workspace/secret.txt</tool_use_error>",
  165. },
  166. }),
  167. );
  168. expect(node.toolCall.status).toBe("failed");
  169. expect(node.toolCall.error).toBe("Permission denied: /workspace/secret.txt");
  170. expect(node.toolCall.raw).toMatchObject({
  171. rawOutput: "<tool_use_error>Permission denied: /workspace/secret.txt</tool_use_error>",
  172. status: "failed",
  173. v4Status: "error",
  174. });
  175. });
  176. it("status=error 但没有结构化 error 时,用普通 output 文本兜出错误", () => {
  177. const node = toolCallRowToLegacyNode(
  178. makeRow({
  179. status: "error",
  180. output: { text: "Permission denied while reading /workspace/secret.txt" },
  181. }),
  182. );
  183. expect(node.toolCall.error).toBe("Permission denied while reading /workspace/secret.txt");
  184. });
  185. it("status=error 且 message 为空时,用 error code 作为最后可见错误", () => {
  186. const node = toolCallRowToLegacyNode(
  187. makeRow({
  188. status: "error",
  189. error: { code: "tool_use_failed", message: "" },
  190. }),
  191. );
  192. expect(node.toolCall.error).toBe("tool_use_failed");
  193. });
  194. it("非失败状态不会把 output 误投成错误", () => {
  195. const success = toolCallRowToLegacyNode(makeRow({ status: "success", output: { text: "ok" } }));
  196. const cancelled = toolCallRowToLegacyNode(
  197. makeRow({ status: "cancelled", output: { text: "stopped by user" } }),
  198. );
  199. expect(success.toolCall.error).toBeUndefined();
  200. expect(cancelled.toolCall.error).toBeUndefined();
  201. });
  202. it("input 缺席时从 inputText 还原;半截 JSON 也生成流式预览 input", () => {
  203. const parsed = toolCallRowToLegacyNode(
  204. makeRow({ input: undefined, inputText: '{"file_path":"/a.ts"}' }),
  205. );
  206. expect(parsed.toolCall.input).toEqual({ file_path: "/a.ts" });
  207. const streaming = toolCallRowToLegacyNode(
  208. makeRow({
  209. toolName: "Write",
  210. status: "inputStreaming",
  211. input: undefined,
  212. inputText: '{"file_path":"src/app.ts","content":"line 1\\nline 2',
  213. }),
  214. );
  215. expect(streaming.toolCall.input).toMatchObject({
  216. file_path: "src/app.ts",
  217. content: "line 1\nline 2",
  218. });
  219. expect(streaming.toolCall.raw).toMatchObject({
  220. inputPreviewComplete: false,
  221. streamingRawInputLength: '{"file_path":"src/app.ts","content":"line 1\\nline 2'.length,
  222. });
  223. const displayModel = buildToolDisplayModel(streaming.toolCall, "/workspace");
  224. expect(displayModel.inlinePreview.type).toBe("text");
  225. if (displayModel.inlinePreview.type === "text") {
  226. expect(displayModel.inlinePreview.source.path).toBe("/workspace/src/app.ts");
  227. expect(displayModel.inlinePreview.source.content).toBe("line 1\nline 2");
  228. }
  229. const empty = toolCallRowToLegacyNode(makeRow({ input: undefined, inputText: "" }));
  230. expect(empty.toolCall.input).toBeUndefined();
  231. });
  232. });