toolCallBlocks.test.ts 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364
  1. // @vitest-environment jsdom
  2. import { createElement } from "react";
  3. import { renderToStaticMarkup } from "react-dom/server";
  4. import { cleanup, fireEvent, render, screen } from "@testing-library/react";
  5. import { afterEach, describe, expect, it, vi } from "vitest";
  6. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  7. import { TooltipProvider } from "@/components/ui/tooltip.js";
  8. import type { ToolCallRow } from "@zcode/shared/zcode-protocol-v4";
  9. import { ToolCallBlock } from "../src/ToolCallBlocks.js";
  10. import { ToolCallBody } from "../src/ToolCallBlocks/ToolCallBody.js";
  11. import { readRawToolCallFileSummaries } from "../src/ToolCallBlocks/shared.js";
  12. import { isExploreToolCall } from "../src/lib/exploreToolCall.js";
  13. import { buildToolDisplayModel } from "../src/lib/toolDisplay.js";
  14. import { toolCallRowToLegacyNode } from "../src/v4/toolCallRowAdapter.js";
  15. vi.mock("@/ToolCallBlocks/renderers/switch-mode.js", () => ({
  16. SwitchModeToolCallBlock: ({
  17. toolCallNode,
  18. }: {
  19. toolCallNode: {
  20. toolCall: {
  21. toolId: string;
  22. title?: string;
  23. kind: string;
  24. };
  25. };
  26. }) =>
  27. createElement(
  28. "div",
  29. { "data-testid": "mock-switch-mode-panel" },
  30. `switch-mode:${toolCallNode.toolCall.toolId}:${toolCallNode.toolCall.title ?? ""}:${toolCallNode.toolCall.kind}`,
  31. ),
  32. }));
  33. // CreateWorkflow renderer 只回显它**收到的上下文**:这条 mock 存在的理由是一个真实 bug——
  34. // onOpenWorkflowRun 曾经被透传给子工具卡却从未进 renderContext,于是聊天卡永远拿不到
  35. // run 详情入口(表现是「入口找不到」,成因却在这层装配里)。
  36. vi.mock("@/ToolCallBlocks/renderers/create-workflow.js", () => ({
  37. CreateWorkflowToolCallBlock: ({
  38. onOpenWorkflowRun,
  39. workflowRun,
  40. }: {
  41. onOpenWorkflowRun?: () => void;
  42. workflowRun?: { runId: string };
  43. }) =>
  44. createElement("div", {
  45. "data-testid": "mock-create-workflow-panel",
  46. "data-has-open-run": String(typeof onOpenWorkflowRun === "function"),
  47. "data-run-id": workflowRun?.runId ?? "",
  48. }),
  49. }));
  50. vi.mock("@/ToolCallBlocks/renderers/plan-guidance.js", () => ({
  51. PlanGuidanceToolCallBlock: ({
  52. toolCallNode,
  53. }: {
  54. toolCallNode: {
  55. toolCall: {
  56. toolId: string;
  57. title?: string;
  58. kind: string;
  59. };
  60. };
  61. }) =>
  62. createElement(
  63. "div",
  64. { "data-testid": "mock-plan-guidance-panel" },
  65. `plan-guidance:${toolCallNode.toolCall.toolId}:${toolCallNode.toolCall.title ?? ""}:${toolCallNode.toolCall.kind}`,
  66. ),
  67. }));
  68. vi.mock("@/ToolCallBlocks/renderers/agent.js", () => ({
  69. AgentToolCallBlock: ({
  70. toolCallNode,
  71. sourceLabel,
  72. }: {
  73. toolCallNode: {
  74. toolCall: {
  75. toolId: string;
  76. title?: string;
  77. kind: string;
  78. };
  79. };
  80. sourceLabel?: string;
  81. }) =>
  82. createElement(
  83. "div",
  84. { "data-testid": "mock-agent-panel" },
  85. `agent:${toolCallNode.toolCall.toolId}:${toolCallNode.toolCall.title ?? ""}:${toolCallNode.toolCall.kind}:source:${sourceLabel ?? "none"}`,
  86. ),
  87. }));
  88. afterEach(cleanup);
  89. describe("isExploreToolCall", () => {
  90. it("does not classify redirect write commands as explore", () => {
  91. const result = isExploreToolCall({
  92. kind: "execute",
  93. input: {
  94. command: ["/bin/zsh", "-lc", "cat > index.html <<'EOF' <!doctype html>"],
  95. },
  96. });
  97. expect(result).toBe(false);
  98. });
  99. });
  100. describe("ToolCallBlock agent routing", () => {
  101. it("renders compact trusted MCP labels from the real plugin-scoped payload", () => {
  102. const row: ToolCallRow = {
  103. rowId: 3,
  104. turnId: "turn-1",
  105. createdAt: 1_700_000_000_002,
  106. createdAtSeq: 3,
  107. kind: "toolCall",
  108. toolCallId: "call-mcp",
  109. toolName: "mcp__plugin_firebase_firebase__firebase_get_environment",
  110. status: "success",
  111. inputText: '{"query":"is:open"}',
  112. input: { query: "is:open" },
  113. output: { text: "12 issues" },
  114. display: {
  115. kind: "mcp_tool",
  116. serverName: "plugin:firebase:firebase",
  117. toolName: "firebase_get_environment",
  118. description: "Retrieve the current Firebase environment configuration.",
  119. },
  120. };
  121. const html = renderToStaticMarkup(
  122. createElement(
  123. ZCodeIntlProvider,
  124. { initialLocale: "zh-CN" },
  125. createElement(
  126. TooltipProvider,
  127. null,
  128. createElement(ToolCallBlock, {
  129. toolCallNode: toolCallRowToLegacyNode(row),
  130. workspacePath: "/workspace",
  131. }),
  132. ),
  133. ),
  134. );
  135. expect(html).toContain(">MCP<");
  136. expect(html).toContain("Get environment");
  137. expect(html).toContain('class="text-foreground-subtle">Firebase</span>');
  138. expect(html).not.toContain("Plugin:firebase:firebase");
  139. expect(html).not.toContain("Firebase get environment");
  140. expect(html).not.toContain(">mcp__plugin_firebase_firebase__firebase_get_environment<");
  141. expect(html).not.toContain("&quot;toolId&quot;");
  142. expect(html).not.toContain("已执行");
  143. });
  144. it("renders historical MCP rows without display through the MCP summary and detail", () => {
  145. const syntheticName = "mcp__plugin_firebase_firebase__firebase_get_environment";
  146. const row: ToolCallRow = {
  147. rowId: 31,
  148. turnId: "turn-legacy",
  149. createdAt: 1_700_000_000_031,
  150. createdAtSeq: 31,
  151. kind: "toolCall",
  152. toolCallId: "call-mcp-legacy-stopped",
  153. toolName: syntheticName,
  154. status: "cancelled",
  155. inputText: "{}",
  156. input: {},
  157. };
  158. render(
  159. createElement(
  160. ZCodeIntlProvider,
  161. { initialLocale: "zh-CN" },
  162. createElement(ToolCallBlock, {
  163. toolCallNode: toolCallRowToLegacyNode(row),
  164. workspacePath: "/workspace",
  165. }),
  166. ),
  167. );
  168. const summary = screen.getByTestId("tool-summary-trigger-call-mcp-legacy-stopped");
  169. expect(summary.textContent).toContain("MCP");
  170. expect(summary.textContent).toContain("Firebase");
  171. expect(summary.textContent).toContain("Get environment");
  172. expect(summary.textContent).toContain("已停止");
  173. expect(summary.innerHTML).toContain(">·</span>");
  174. expect(summary.textContent).not.toContain(syntheticName);
  175. fireEvent.click(summary);
  176. expect(screen.queryByTestId("mcp-expanded-content")).toBeNull();
  177. expect(screen.queryByRole("button", { name: "查看调用详情" })).toBeNull();
  178. expect(screen.queryByText("PARAMETERS")).toBeNull();
  179. expect(screen.queryByText(syntheticName)).toBeNull();
  180. expect(screen.queryByText(/toolId/u)).toBeNull();
  181. });
  182. it("shows only the result first and keeps MCP metadata in secondary call details", () => {
  183. const description = "Retrieve the current Firebase environment configuration.";
  184. const row: ToolCallRow = {
  185. rowId: 4,
  186. turnId: "turn-1",
  187. createdAt: 1_700_000_000_003,
  188. createdAtSeq: 4,
  189. kind: "toolCall",
  190. toolCallId: "call-mcp-detail",
  191. toolName: "mcp__plugin_firebase_firebase__firebase_get_environment",
  192. status: "success",
  193. inputText: '{"projectId":"demo"}',
  194. input: { projectId: "demo" },
  195. output: { text: "No firebase.json file was found." },
  196. display: {
  197. kind: "mcp_tool",
  198. serverName: "plugin:firebase:firebase",
  199. toolName: "firebase_get_environment",
  200. description,
  201. },
  202. };
  203. render(
  204. createElement(
  205. ZCodeIntlProvider,
  206. { initialLocale: "zh-CN" },
  207. createElement(ToolCallBlock, {
  208. toolCallNode: toolCallRowToLegacyNode(row),
  209. workspacePath: "/workspace",
  210. }),
  211. ),
  212. );
  213. expect(screen.queryByText(description)).toBeNull();
  214. const summary = screen.getByTestId("tool-summary-trigger-call-mcp-detail");
  215. fireEvent.click(summary);
  216. expect(screen.queryByText("Result")).toBeNull();
  217. expect(screen.queryByText("Parameters")).toBeNull();
  218. expect(screen.queryByText(description)).toBeNull();
  219. fireEvent.click(screen.getByRole("button", { name: "查看调用详情" }));
  220. expect(screen.getByText(description)).toBeTruthy();
  221. expect(screen.getByText("调用参数")).toBeTruthy();
  222. });
  223. it.each([
  224. ["inputStreaming", "等待中", false],
  225. ["pendingApproval", "等待中", false],
  226. ["running", "执行中", true],
  227. ["cancelled", "已停止", false],
  228. ] as const)("controls %s MCP detail availability", (status, label, canExpand) => {
  229. const row: ToolCallRow = {
  230. rowId: 5,
  231. turnId: "turn-1",
  232. createdAt: 1_700_000_000_004,
  233. createdAtSeq: 5,
  234. kind: "toolCall",
  235. toolCallId: `call-mcp-${status}`,
  236. toolName: "opaque-provider-name",
  237. status,
  238. inputText: "{}",
  239. input: {},
  240. display: {
  241. kind: "mcp_tool",
  242. serverName: "firebase",
  243. toolName: "get_environment",
  244. },
  245. };
  246. render(
  247. createElement(
  248. ZCodeIntlProvider,
  249. { initialLocale: "zh-CN" },
  250. createElement(ToolCallBlock, {
  251. toolCallNode: toolCallRowToLegacyNode(row),
  252. workspacePath: "/workspace",
  253. }),
  254. ),
  255. );
  256. const summary = screen.getByTestId(`tool-summary-trigger-call-mcp-${status}`);
  257. if (status === "inputStreaming" || status === "pendingApproval" || status === "running") {
  258. expect(summary.textContent).not.toContain(label);
  259. } else {
  260. expect(summary.textContent).toContain(label);
  261. expect(
  262. Array.from(summary.querySelectorAll("span")).filter(
  263. (element) => element.textContent === "·",
  264. ),
  265. ).toHaveLength(2);
  266. }
  267. fireEvent.click(summary);
  268. if (canExpand) {
  269. expect(screen.getByTestId("mcp-expanded-content").textContent).toContain(label);
  270. } else {
  271. expect(screen.queryByTestId("mcp-expanded-content")).toBeNull();
  272. }
  273. });
  274. it("shows the MCP failure result before call details", () => {
  275. const row: ToolCallRow = {
  276. rowId: 6,
  277. turnId: "turn-1",
  278. createdAt: 1_700_000_000_005,
  279. createdAtSeq: 6,
  280. kind: "toolCall",
  281. toolCallId: "call-mcp-failed",
  282. toolName: "opaque-provider-name",
  283. status: "error",
  284. inputText: "{}",
  285. input: {},
  286. error: { code: "tool_failed", message: "Firebase authentication failed." },
  287. display: {
  288. kind: "mcp_tool",
  289. serverName: "firebase",
  290. toolName: "get_environment",
  291. },
  292. };
  293. render(
  294. createElement(
  295. ZCodeIntlProvider,
  296. { initialLocale: "zh-CN" },
  297. createElement(ToolCallBlock, {
  298. toolCallNode: toolCallRowToLegacyNode(row),
  299. workspacePath: "/workspace",
  300. }),
  301. ),
  302. );
  303. const summary = screen.getByTestId("tool-summary-trigger-call-mcp-failed");
  304. expect(
  305. Array.from(summary.querySelectorAll("span")).filter((element) => element.textContent === "·"),
  306. ).toHaveLength(2);
  307. fireEvent.click(summary);
  308. const detail = screen.getByTestId("mcp-expanded-content");
  309. expect(detail.textContent).toContain("Firebase authentication failed.");
  310. expect(detail.textContent).toContain("查看调用详情");
  311. expect(screen.getByTestId("mcp-error-surface").className).toContain("bg-destructive/10");
  312. });
  313. it("renders multiline MCP result actions", () => {
  314. const row: ToolCallRow = {
  315. rowId: 7,
  316. turnId: "turn-1",
  317. createdAt: 1_700_000_000_006,
  318. createdAtSeq: 7,
  319. kind: "toolCall",
  320. toolCallId: "call-mcp-long-result",
  321. toolName: "opaque-provider-name",
  322. status: "success",
  323. inputText: "{}",
  324. input: {},
  325. output: { text: '{\n "project": "demo"\n}' },
  326. display: {
  327. kind: "mcp_tool",
  328. serverName: "firebase",
  329. toolName: "get_environment",
  330. },
  331. };
  332. render(
  333. createElement(
  334. ZCodeIntlProvider,
  335. { initialLocale: "zh-CN" },
  336. createElement(
  337. TooltipProvider,
  338. null,
  339. createElement(ToolCallBlock, {
  340. toolCallNode: toolCallRowToLegacyNode(row),
  341. workspacePath: "/workspace",
  342. }),
  343. ),
  344. ),
  345. );
  346. fireEvent.click(screen.getByTestId("tool-summary-trigger-call-mcp-long-result"));
  347. screen.getByTestId("mcp-result-surface");
  348. expect(screen.getByRole("button", { name: "复制结果" })).toBeTruthy();
  349. expect(screen.getByRole("button", { name: "切换自动换行" })).toBeTruthy();
  350. });
  351. it("renders adapted v4 output-only tool failures as an error surface", () => {
  352. const row: ToolCallRow = {
  353. rowId: 1,
  354. turnId: "turn-1",
  355. createdAt: 1_700_000_000_000,
  356. createdAtSeq: 1,
  357. kind: "toolCall",
  358. toolCallId: "call-failed",
  359. toolName: "Bash",
  360. status: "error",
  361. inputText: '{"command":"cat /workspace/secret.txt"}',
  362. input: { command: "cat /workspace/secret.txt" },
  363. output: {
  364. text: "<tool_use_error>Permission denied: /workspace/secret.txt</tool_use_error>",
  365. },
  366. };
  367. const toolCall = toolCallRowToLegacyNode(row).toolCall;
  368. const html = renderToStaticMarkup(
  369. createElement(
  370. ZCodeIntlProvider,
  371. { initialLocale: "zh-CN" },
  372. createElement(ToolCallBody, {
  373. childToolList: null,
  374. displayModel: buildToolDisplayModel(toolCall),
  375. toolCall,
  376. workspacePath: "/workspace",
  377. }),
  378. ),
  379. );
  380. expect(html).toContain("Error");
  381. expect(html).toContain("Permission denied: /workspace/secret.txt");
  382. expect(html).not.toContain("&lt;tool_use_error&gt;");
  383. });
  384. it("keeps adapted v4 successful tool output on the normal result surface", () => {
  385. const row: ToolCallRow = {
  386. rowId: 2,
  387. turnId: "turn-1",
  388. createdAt: 1_700_000_000_001,
  389. createdAtSeq: 2,
  390. kind: "toolCall",
  391. toolCallId: "call-success",
  392. toolName: "Bash",
  393. status: "success",
  394. inputText: '{"command":"echo ok"}',
  395. input: { command: "echo ok" },
  396. output: { text: "ok" },
  397. };
  398. const toolCall = toolCallRowToLegacyNode(row).toolCall;
  399. const html = renderToStaticMarkup(
  400. createElement(
  401. ZCodeIntlProvider,
  402. { initialLocale: "zh-CN" },
  403. createElement(ToolCallBody, {
  404. childToolList: null,
  405. displayModel: buildToolDisplayModel(toolCall),
  406. toolCall,
  407. workspacePath: "/workspace",
  408. }),
  409. ),
  410. );
  411. expect(html).toContain("Result");
  412. expect(html).not.toContain("Error");
  413. });
  414. it("routes Task tool calls to the agent renderer", () => {
  415. const html = renderToStaticMarkup(
  416. createElement(
  417. ZCodeIntlProvider,
  418. { initialLocale: "zh-CN" },
  419. createElement(ToolCallBlock, {
  420. toolCallNode: {
  421. toolCall: {
  422. toolId: "tool-task-agent",
  423. kind: "think",
  424. title: "Task",
  425. input: {
  426. prompt: "搜索今日新闻",
  427. run_in_background: true,
  428. },
  429. output: [],
  430. status: "completed",
  431. raw: {},
  432. },
  433. childToolCalls: [],
  434. },
  435. workspacePath: "/workspace",
  436. }),
  437. ),
  438. );
  439. expect(html).toContain("agent:tool-task-agent:Task:think:source:none");
  440. });
  441. it("routes GLM Agent tool calls to the agent renderer", () => {
  442. const html = renderToStaticMarkup(
  443. createElement(
  444. ZCodeIntlProvider,
  445. { initialLocale: "zh-CN" },
  446. createElement(ToolCallBlock, {
  447. toolCallNode: {
  448. toolCall: {
  449. toolId: "tool-glm-agent",
  450. kind: "think",
  451. title: "Agent",
  452. input: {
  453. description: "Find runtime loop",
  454. prompt: "Find where the runtime injects tool results.",
  455. },
  456. output: {
  457. status: "completed",
  458. agentId: "agent-glm-explore",
  459. agentType: "Explore",
  460. },
  461. status: "completed",
  462. raw: {},
  463. },
  464. childToolCalls: [],
  465. },
  466. workspacePath: "/workspace",
  467. }),
  468. ),
  469. );
  470. expect(html).toContain("agent:tool-glm-agent:Agent:think:source:none");
  471. });
  472. it("routes spawn_agent tool calls to the agent renderer", () => {
  473. const html = renderToStaticMarkup(
  474. createElement(
  475. ZCodeIntlProvider,
  476. { initialLocale: "zh-CN" },
  477. createElement(ToolCallBlock, {
  478. toolCallNode: {
  479. toolCall: {
  480. toolId: "tool-spawn-agent",
  481. kind: "spawn_agent",
  482. title: "spawn_agent",
  483. input: {
  484. agent_type: "explorer",
  485. message: "检查当前项目结构",
  486. },
  487. output: {
  488. agent_id: "agent-1",
  489. nickname: "Locke",
  490. },
  491. status: "completed",
  492. raw: {
  493. name: "spawn_agent",
  494. _meta: {
  495. zcode: {
  496. toolName: "spawn_agent",
  497. nickname: "Locke",
  498. },
  499. },
  500. },
  501. },
  502. childToolCalls: [],
  503. },
  504. workspacePath: "/workspace",
  505. }),
  506. ),
  507. );
  508. expect(html).toContain("agent:tool-spawn-agent:spawn_agent:spawn_agent:source:none");
  509. });
  510. });
  511. describe("readRawToolCallFileSummaries", () => {
  512. it("does not infer edit summaries from read tool content keywords", () => {
  513. const summaries = readRawToolCallFileSummaries(
  514. {
  515. kind: "read",
  516. status: "completed",
  517. rawInput: {
  518. file_path: "/Users/dev/ZCodeProject/demo/snake/index.html",
  519. },
  520. content: [
  521. {
  522. type: "content",
  523. content: {
  524. type: "text",
  525. text: "function deleteFood() { return writeScore(); }",
  526. },
  527. },
  528. ],
  529. },
  530. {
  531. kind: "read",
  532. title: "Read snake/index.html",
  533. input: {
  534. file_path: "/Users/dev/ZCodeProject/demo/snake/index.html",
  535. },
  536. },
  537. );
  538. expect(summaries).toEqual([]);
  539. });
  540. it("routes read tools to the read renderer even if raw summaries exist", () => {
  541. const html = renderToStaticMarkup(
  542. createElement(
  543. ZCodeIntlProvider,
  544. { initialLocale: "zh-CN" },
  545. createElement(ToolCallBlock, {
  546. toolCallNode: {
  547. toolCall: {
  548. toolId: "tool-read-weird",
  549. kind: "read",
  550. title: "Read snake/index.html",
  551. input: {
  552. file_path: "/Users/dev/ZCodeProject/demo/snake/index.html",
  553. },
  554. output: {
  555. content: "function deleteFood() { return writeScore(); }",
  556. },
  557. status: "completed",
  558. raw: {
  559. kind: "read",
  560. changes: {
  561. "/Users/dev/ZCodeProject/demo/snake/index.html": {
  562. type: "add",
  563. content: "unexpected",
  564. },
  565. },
  566. rawInput: {
  567. file_path: "/Users/dev/ZCodeProject/demo/snake/index.html",
  568. },
  569. },
  570. },
  571. childToolCalls: [],
  572. },
  573. workspacePath: "/workspace",
  574. }),
  575. ),
  576. );
  577. expect(html).toContain("读取");
  578. expect(html).not.toContain("主 Agent");
  579. expect(html).not.toContain("SubAgent");
  580. expect(html).toContain("index.html");
  581. expect(html).not.toContain("已写入");
  582. expect(html).not.toContain("已删除");
  583. expect(html).not.toContain("已编辑");
  584. });
  585. it("does not infer edit summaries for explore nodes backed by Read tool metadata", () => {
  586. const summaries = readRawToolCallFileSummaries(
  587. {
  588. _meta: {
  589. claudeCode: {
  590. toolName: "Read",
  591. },
  592. },
  593. content: [
  594. {
  595. type: "content",
  596. content: {
  597. type: "text",
  598. text: "function deleteFood() { return writeScore(); }",
  599. },
  600. },
  601. ],
  602. rawOutput: "...",
  603. status: "completed",
  604. sessionUpdate: "tool_call_update",
  605. },
  606. {
  607. kind: "explore",
  608. title: "Read snake/game.ts",
  609. input: {
  610. file_path: "/Users/dev/ZCodeProject/demo/snake/game.ts",
  611. },
  612. },
  613. );
  614. expect(summaries).toEqual([]);
  615. });
  616. it("reads multi-file ZCode tool display summaries", () => {
  617. const summaries = readRawToolCallFileSummaries(
  618. {
  619. display: {
  620. kind: "file_diffs",
  621. files: [
  622. {
  623. filePath: "/workspace/src/a.ts",
  624. additions: 2,
  625. deletions: 1,
  626. structuredPatch: [
  627. {
  628. oldStart: 1,
  629. oldLines: 1,
  630. newStart: 1,
  631. newLines: 1,
  632. lines: ["-old", "+new"],
  633. },
  634. ],
  635. },
  636. {
  637. filePath: "/workspace/src/b.ts",
  638. additions: 3,
  639. deletions: 0,
  640. structuredPatch: [
  641. {
  642. oldStart: 1,
  643. oldLines: 0,
  644. newStart: 1,
  645. newLines: 1,
  646. lines: ["+created"],
  647. },
  648. ],
  649. },
  650. ],
  651. },
  652. },
  653. {
  654. kind: "ApplyPatch",
  655. title: "Edited 2 files",
  656. input: {},
  657. },
  658. );
  659. expect(summaries).toHaveLength(2);
  660. expect(summaries.map((summary) => summary.changeStat)).toEqual([
  661. { added: 2, removed: 1 },
  662. { added: 3, removed: 0 },
  663. ]);
  664. expect(summaries[1]?.patch).toContain("+created");
  665. });
  666. it("does not build delete summaries for non-writable execute tools even with raw changes", () => {
  667. const summaries = readRawToolCallFileSummaries(
  668. {
  669. kind: "execute",
  670. changes: {
  671. "/Users/dev/ZCodeProject/demo/snake/game.ts": {
  672. type: "delete",
  673. oldText: "console.log('legacy');",
  674. },
  675. },
  676. rawInput: {
  677. path: "/Users/dev/ZCodeProject/demo/snake/game.ts",
  678. },
  679. },
  680. {
  681. kind: "execute",
  682. title: "Run cleanup command",
  683. input: {
  684. command: "rm game.ts",
  685. },
  686. },
  687. );
  688. expect(summaries).toEqual([]);
  689. });
  690. it("does not infer delete from explore text without writable tool kind", () => {
  691. const summaries = readRawToolCallFileSummaries(
  692. {
  693. status: "completed",
  694. rawInput: {
  695. file_path: "/Users/dev/ZCodeProject/demo/snake/game.ts",
  696. description: "Delete legacy fallback after inspection",
  697. },
  698. },
  699. {
  700. kind: "explore",
  701. title: "Inspect game.ts",
  702. input: {
  703. file_path: "/Users/dev/ZCodeProject/demo/snake/game.ts",
  704. description: "Delete legacy fallback after inspection",
  705. },
  706. },
  707. );
  708. expect(summaries).toEqual([]);
  709. });
  710. it("normalizes hunk-only add patches as newly created files", () => {
  711. const summaries = readRawToolCallFileSummaries(
  712. {
  713. changes: {
  714. "src/demo.ts": {
  715. type: "add",
  716. unified_diff: "@@ -0,0 +1,2 @@\n+export const value = 1;\n+export { value };",
  717. },
  718. },
  719. },
  720. {
  721. kind: "write",
  722. title: "Write src/demo.ts",
  723. input: {
  724. path: "src/demo.ts",
  725. },
  726. },
  727. );
  728. expect(summaries).toHaveLength(1);
  729. expect(summaries[0]?.patch).toContain("--- /dev/null");
  730. expect(summaries[0]?.patch).toContain("+++ b/demo.ts");
  731. });
  732. it("builds write summaries from ZCode protocol input payloads", () => {
  733. const summaries = readRawToolCallFileSummaries(
  734. {
  735. toolName: "Write",
  736. input: {
  737. file_path: "src/quicksort.js",
  738. content: "export const quicksort = () => [];\n",
  739. },
  740. },
  741. {
  742. kind: "Write",
  743. title: "Write",
  744. raw: {
  745. toolName: "Write",
  746. input: {
  747. file_path: "src/quicksort.js",
  748. content: "export const quicksort = () => [];\n",
  749. },
  750. },
  751. },
  752. );
  753. expect(summaries).toHaveLength(1);
  754. expect(summaries[0]?.path).toBe("src/quicksort.js");
  755. expect(summaries[0]?.operationKind).toBe("write");
  756. expect(summaries[0]?.changeStat).toEqual({ added: 1, removed: 0 });
  757. });
  758. it("builds edit summaries from ZCode protocol input payloads", () => {
  759. const summaries = readRawToolCallFileSummaries(
  760. {
  761. toolName: "Edit",
  762. input: {
  763. file_path: "src/quicksort.js",
  764. old_string: "return left.concat(pivot, right);",
  765. new_string: "return [...left, pivot, ...right];",
  766. },
  767. },
  768. {
  769. kind: "Edit",
  770. title: "Edit",
  771. raw: {
  772. toolName: "Edit",
  773. input: {
  774. file_path: "src/quicksort.js",
  775. old_string: "return left.concat(pivot, right);",
  776. new_string: "return [...left, pivot, ...right];",
  777. },
  778. },
  779. },
  780. );
  781. expect(summaries).toHaveLength(1);
  782. expect(summaries[0]?.path).toBe("src/quicksort.js");
  783. expect(summaries[0]?.operationKind).toBe("edit");
  784. expect(summaries[0]?.changeStat).toEqual({ added: 1, removed: 1 });
  785. });
  786. it("drops multi-file unified diff payloads and falls back to single-file patch", () => {
  787. const summaries = readRawToolCallFileSummaries(
  788. {
  789. changes: {
  790. "src/demo.ts": {
  791. type: "update",
  792. oldText: "const value = 1;\n",
  793. newText: "const value = 2;\n",
  794. unified_diff:
  795. "diff --git a/src/demo.ts b/src/demo.ts\n--- a/src/demo.ts\n+++ b/src/demo.ts\n@@ -1 +1 @@\n-const value = 1;\n+const value = 2;\n" +
  796. "diff --git a/src/other.ts b/src/other.ts\n--- a/src/other.ts\n+++ b/src/other.ts\n@@ -1 +1 @@\n-console.log('a');\n+console.log('b');",
  797. },
  798. },
  799. },
  800. {
  801. kind: "edit",
  802. title: "Edit src/demo.ts",
  803. input: {
  804. path: "src/demo.ts",
  805. },
  806. },
  807. );
  808. expect(summaries).toHaveLength(1);
  809. expect(summaries[0]?.patch).toContain("--- a/demo.ts");
  810. expect(summaries[0]?.patch).toContain("+++ b/demo.ts");
  811. expect(summaries[0]?.patch).not.toContain("src/other.ts");
  812. });
  813. it("drops truncated second-file hunks in multi-file unified diff payloads", () => {
  814. const summaries = readRawToolCallFileSummaries(
  815. {
  816. changes: {
  817. "src/demo.ts": {
  818. type: "update",
  819. oldText: "const value = 1;\n",
  820. newText: "const value = 2;\n",
  821. // 回归场景:第二个文件 hunk 被流式截断时,仍应识别为多文件并触发 fallback。
  822. unified_diff:
  823. "--- a/src/demo.ts\n+++ b/src/demo.ts\n@@ -1 +1 @@\n-const value = 1;\n+const value = 2;\n" +
  824. "--- a/src/other.ts\n+++ b/src/other.ts\n@@ -1 +1 @@\n-console.log('a');",
  825. },
  826. },
  827. },
  828. {
  829. kind: "edit",
  830. title: "Edit src/demo.ts",
  831. input: {
  832. path: "src/demo.ts",
  833. },
  834. },
  835. );
  836. expect(summaries).toHaveLength(1);
  837. expect(summaries[0]?.patch).toContain("--- a/demo.ts");
  838. expect(summaries[0]?.patch).toContain("+++ b/demo.ts");
  839. expect(summaries[0]?.patch).not.toContain("src/other.ts");
  840. });
  841. it("keeps single-file patches when hunk content contains header-like lines", () => {
  842. const summaries = readRawToolCallFileSummaries(
  843. {
  844. changes: {
  845. "src/demo.ts": {
  846. type: "update",
  847. // 回归场景:正文里以 --- / +++ 开头的文本不应被识别成额外文件头。
  848. unified_diff:
  849. "--- a/src/demo.ts\n+++ b/src/demo.ts\n@@ -1,2 +1,2 @@\n const marker = true;\n--- header-like-old-line\n+++ header-like-new-line",
  850. },
  851. },
  852. },
  853. {
  854. kind: "edit",
  855. title: "Edit src/demo.ts",
  856. input: {
  857. path: "src/demo.ts",
  858. },
  859. },
  860. );
  861. expect(summaries).toHaveLength(1);
  862. expect(summaries[0]?.patch).toContain("--- a/src/demo.ts");
  863. expect(summaries[0]?.patch).toContain("+++ b/src/demo.ts");
  864. expect(summaries[0]?.patch).toContain("--- header-like-old-line");
  865. expect(summaries[0]?.patch).toContain("+++ header-like-new-line");
  866. });
  867. it("keeps read-like explore nodes out of edit renderer", () => {
  868. const html = renderToStaticMarkup(
  869. createElement(
  870. ZCodeIntlProvider,
  871. { initialLocale: "zh-CN" },
  872. createElement(ToolCallBlock, {
  873. toolCallNode: {
  874. toolCall: {
  875. toolId: "tool-explore-read-like",
  876. kind: "explore",
  877. title: "Read snake/game.ts",
  878. input: {
  879. file_path: "/Users/dev/ZCodeProject/demo/snake/game.ts",
  880. },
  881. output: "...",
  882. status: "completed",
  883. raw: {
  884. _meta: {
  885. claudeCode: {
  886. toolName: "Read",
  887. },
  888. },
  889. content: [
  890. {
  891. type: "content",
  892. content: {
  893. type: "text",
  894. text: "function deleteFood() { return writeScore(); }",
  895. },
  896. },
  897. ],
  898. rawOutput: "...",
  899. status: "completed",
  900. toolCallId: "call_bcbf3d3258d34b44b3dbc0b4",
  901. sessionUpdate: "tool_call_update",
  902. },
  903. },
  904. childToolCalls: [],
  905. },
  906. workspacePath: "/workspace",
  907. }),
  908. ),
  909. );
  910. expect(html).not.toContain("已写入");
  911. expect(html).not.toContain("已删除");
  912. expect(html).not.toContain("已编辑");
  913. });
  914. it("keeps execute tools with delete-like raw changes out of edit renderer", () => {
  915. const html = renderToStaticMarkup(
  916. createElement(
  917. ZCodeIntlProvider,
  918. { initialLocale: "zh-CN" },
  919. createElement(ToolCallBlock, {
  920. toolCallNode: {
  921. toolCall: {
  922. toolId: "tool-execute-delete-like",
  923. kind: "execute",
  924. title: "Run cleanup",
  925. input: {
  926. command: "rm snake/game.ts",
  927. },
  928. output: {
  929. stdout: "cleanup finished",
  930. },
  931. status: "completed",
  932. raw: {
  933. kind: "execute",
  934. changes: {
  935. "/Users/dev/ZCodeProject/demo/snake/game.ts": {
  936. type: "delete",
  937. oldText: "console.log('legacy');",
  938. },
  939. },
  940. },
  941. },
  942. childToolCalls: [],
  943. },
  944. workspacePath: "/workspace",
  945. }),
  946. ),
  947. );
  948. expect(html).toContain("终端");
  949. expect(html).not.toContain("已删除");
  950. expect(html).not.toContain("已编辑");
  951. expect(html).not.toContain("Deleting");
  952. expect(html).not.toContain("Deleted");
  953. });
  954. it("把 dwf run 入口与 run 摘要交到 CreateWorkflow renderer 手里", () => {
  955. // 回归:这两个 prop 之前只被透传给**子**工具卡,从未进过 renderContext,于是 run 态卡片
  956. // 和它的详情页入口在生产里根本不存在。装配这一层必须自己被钉住。
  957. const html = renderToStaticMarkup(
  958. createElement(
  959. ZCodeIntlProvider,
  960. { initialLocale: "zh-CN" },
  961. createElement(ToolCallBlock, {
  962. toolCallNode: {
  963. toolCall: {
  964. toolId: "tool-create-workflow",
  965. kind: "CreateWorkflow",
  966. title: "CreateWorkflow",
  967. input: { name: "Fan-out review", script: "const x = 1;" },
  968. status: "completed",
  969. },
  970. childToolCalls: [],
  971. },
  972. workspacePath: "/workspace",
  973. onOpenWorkflowRun: () => {},
  974. workflowRun: {
  975. runId: "dwfrun-42",
  976. status: "running" as const,
  977. nodesSettled: 3,
  978. nodesTotal: 7,
  979. },
  980. }),
  981. ),
  982. );
  983. expect(html).toContain("mock-create-workflow-panel");
  984. expect(html).toContain('data-has-open-run="true"');
  985. expect(html).toContain('data-run-id="dwfrun-42"');
  986. });
  987. it("routes switch_mode tool calls to the dedicated renderer", () => {
  988. const html = renderToStaticMarkup(
  989. createElement(
  990. ZCodeIntlProvider,
  991. { initialLocale: "zh-CN" },
  992. createElement(ToolCallBlock, {
  993. toolCallNode: {
  994. toolCall: {
  995. toolId: "tool-switch-mode",
  996. kind: "switch_mode",
  997. title: "Exited Plan Mode",
  998. input: {
  999. plan: "# 输出计划\n\n- 渲染 markdown",
  1000. },
  1001. output: "...",
  1002. status: "completed",
  1003. raw: {
  1004. rawOutput: "...",
  1005. },
  1006. },
  1007. childToolCalls: [],
  1008. },
  1009. workspacePath: "/workspace",
  1010. }),
  1011. ),
  1012. );
  1013. expect(html).toContain("mock-switch-mode-panel");
  1014. expect(html).toContain("switch-mode:tool-switch-mode:Exited Plan Mode:switch_mode");
  1015. });
  1016. it("routes Exited Plan Mode titles to the switch mode renderer even when kind is other", () => {
  1017. const html = renderToStaticMarkup(
  1018. createElement(
  1019. ZCodeIntlProvider,
  1020. { initialLocale: "zh-CN" },
  1021. createElement(ToolCallBlock, {
  1022. toolCallNode: {
  1023. toolCall: {
  1024. toolId: "tool-exited-plan-title",
  1025. kind: "other",
  1026. title: "Exited Plan Mode",
  1027. input: {
  1028. plan: "# 输出计划\n\n- 渲染 markdown",
  1029. },
  1030. output: "...",
  1031. status: "completed",
  1032. raw: {
  1033. rawOutput: "...",
  1034. },
  1035. },
  1036. childToolCalls: [],
  1037. },
  1038. workspacePath: "/workspace",
  1039. }),
  1040. ),
  1041. );
  1042. expect(html).toContain("mock-switch-mode-panel");
  1043. expect(html).toContain("switch-mode:tool-exited-plan-title:Exited Plan Mode:other");
  1044. });
  1045. it("routes current ExitPlanMode tool calls to the switch mode renderer", () => {
  1046. const html = renderToStaticMarkup(
  1047. createElement(
  1048. ZCodeIntlProvider,
  1049. { initialLocale: "zh-CN" },
  1050. createElement(ToolCallBlock, {
  1051. toolCallNode: {
  1052. toolCall: {
  1053. toolId: "tool-exit-plan-current",
  1054. kind: "ExitPlanMode",
  1055. title: "ExitPlanMode",
  1056. input: {
  1057. plan: "# 输出计划\n\n- 渲染 markdown",
  1058. },
  1059. output: "...",
  1060. status: "completed",
  1061. raw: {
  1062. toolName: "ExitPlanMode",
  1063. rawOutput: "...",
  1064. },
  1065. },
  1066. childToolCalls: [],
  1067. },
  1068. workspacePath: "/workspace",
  1069. }),
  1070. ),
  1071. );
  1072. expect(html).toContain("mock-switch-mode-panel");
  1073. expect(html).toContain("switch-mode:tool-exit-plan-current:ExitPlanMode:ExitPlanMode");
  1074. });
  1075. it("routes EnterPlanMode titles to the plan guidance renderer", () => {
  1076. const html = renderToStaticMarkup(
  1077. createElement(
  1078. ZCodeIntlProvider,
  1079. { initialLocale: "zh-CN" },
  1080. createElement(ToolCallBlock, {
  1081. toolCallNode: {
  1082. toolCall: {
  1083. toolId: "tool-enter-plan-guidance",
  1084. kind: "other",
  1085. title: "EnterPlanMode",
  1086. input: {},
  1087. output: "Entered plan mode.",
  1088. status: "completed",
  1089. raw: {
  1090. _meta: {
  1091. claudeCode: {
  1092. toolName: "EnterPlanMode",
  1093. },
  1094. },
  1095. rawOutput: "Entered plan mode.",
  1096. },
  1097. },
  1098. childToolCalls: [],
  1099. },
  1100. workspacePath: "/workspace",
  1101. }),
  1102. ),
  1103. );
  1104. expect(html).toContain("mock-plan-guidance-panel");
  1105. expect(html).toContain("plan-guidance:tool-enter-plan-guidance:EnterPlanMode:other");
  1106. });
  1107. it("routes TodoWrite to the todo renderer instead of the file write renderer", () => {
  1108. const html = renderToStaticMarkup(
  1109. createElement(
  1110. ZCodeIntlProvider,
  1111. { initialLocale: "zh-CN" },
  1112. createElement(ToolCallBlock, {
  1113. toolCallNode: {
  1114. toolCall: {
  1115. toolId: "tool-todo-write",
  1116. kind: "TodoWrite",
  1117. title: "TodoWrite",
  1118. input: {
  1119. todos: [
  1120. {
  1121. content: "更新 index.html 标题",
  1122. priority: "high",
  1123. status: "in_progress",
  1124. },
  1125. {
  1126. content: "创建番茄时钟组件",
  1127. priority: "medium",
  1128. status: "pending",
  1129. },
  1130. ],
  1131. },
  1132. output: JSON.stringify({
  1133. oldTodos: [],
  1134. todos: [
  1135. {
  1136. content: "更新 index.html 标题",
  1137. priority: "high",
  1138. status: "in_progress",
  1139. },
  1140. {
  1141. content: "创建番茄时钟组件",
  1142. priority: "medium",
  1143. status: "pending",
  1144. },
  1145. ],
  1146. }),
  1147. status: "completed",
  1148. raw: {
  1149. toolCallId: "tool-todo-write",
  1150. toolName: "TodoWrite",
  1151. },
  1152. },
  1153. childToolCalls: [],
  1154. },
  1155. workspacePath: "/workspace",
  1156. }),
  1157. ),
  1158. );
  1159. expect(html).toContain("待办");
  1160. expect(html).toContain("更新 index.html 标题");
  1161. expect(html).toContain("0/2");
  1162. expect(html).not.toContain("已写入");
  1163. });
  1164. it("routes fixed AskUserQuestion tools without renaming the tool identity", () => {
  1165. const html = renderToStaticMarkup(
  1166. createElement(
  1167. ZCodeIntlProvider,
  1168. { initialLocale: "zh-CN" },
  1169. createElement(ToolCallBlock, {
  1170. toolCallNode: {
  1171. toolCall: {
  1172. toolId: "tool-ask-user-question",
  1173. toolName: "AskUserQuestion",
  1174. kind: "AskUserQuestion",
  1175. title: "AskUserQuestion",
  1176. input: {
  1177. questions: [
  1178. {
  1179. id: "q1",
  1180. question: "选择实现方向?",
  1181. options: [
  1182. { id: "safe", label: "稳妥" },
  1183. { id: "fast", label: "快速" },
  1184. ],
  1185. },
  1186. ],
  1187. answers: {
  1188. "选择实现方向?": "稳妥",
  1189. },
  1190. },
  1191. status: "completed",
  1192. raw: {
  1193. toolName: "AskUserQuestion",
  1194. },
  1195. },
  1196. childToolCalls: [],
  1197. },
  1198. workspacePath: "/workspace",
  1199. }),
  1200. ),
  1201. );
  1202. expect(html).toContain("已询问");
  1203. expect(html).toContain("1 个问题");
  1204. expect(html).toContain("AskUserQuestion");
  1205. expect(html).not.toContain("AskQuestion");
  1206. expect(html).not.toContain("已写入");
  1207. });
  1208. it("shows an automatic-continuation history label for explicit empty answers", () => {
  1209. const html = renderToStaticMarkup(
  1210. createElement(
  1211. ZCodeIntlProvider,
  1212. { initialLocale: "zh-CN" },
  1213. createElement(ToolCallBlock, {
  1214. toolCallNode: {
  1215. toolCall: {
  1216. toolId: "tool-ask-auto-continued",
  1217. toolName: "AskUserQuestion",
  1218. kind: "AskUserQuestion",
  1219. title: "AskUserQuestion",
  1220. input: {
  1221. questions: [
  1222. {
  1223. id: "q1",
  1224. question: "选择实现方向?",
  1225. options: [
  1226. { id: "safe", label: "稳妥" },
  1227. { id: "fast", label: "快速" },
  1228. ],
  1229. },
  1230. ],
  1231. },
  1232. output: { questions: [], answers: {} },
  1233. status: "completed",
  1234. raw: { toolName: "AskUserQuestion" },
  1235. },
  1236. childToolCalls: [],
  1237. },
  1238. workspacePath: "/workspace",
  1239. }),
  1240. ),
  1241. );
  1242. expect(html).toContain("未回答,已自动继续");
  1243. expect(html).not.toContain("未提供回答");
  1244. });
  1245. it("routes Agent tool calls to the dedicated renderer", () => {
  1246. const html = renderToStaticMarkup(
  1247. createElement(
  1248. ZCodeIntlProvider,
  1249. { initialLocale: "zh-CN" },
  1250. createElement(ToolCallBlock, {
  1251. toolCallNode: {
  1252. toolCall: {
  1253. toolId: "tool-agent",
  1254. kind: "think",
  1255. title: "Explore project structure",
  1256. input: {
  1257. description: "Explore project structure",
  1258. subagent_type: "Explore",
  1259. },
  1260. output: "...",
  1261. status: "completed",
  1262. raw: {
  1263. _meta: {
  1264. claudeCode: {
  1265. toolName: "Agent",
  1266. },
  1267. },
  1268. },
  1269. },
  1270. childToolCalls: [],
  1271. },
  1272. workspacePath: "/workspace",
  1273. }),
  1274. ),
  1275. );
  1276. expect(html).toContain("mock-agent-panel");
  1277. expect(html).toContain("agent:tool-agent:Explore project structure:think");
  1278. });
  1279. });