agentToolCallBlock.test.ts 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
  1. import { ToolCallBlock } from "@/ToolCallBlocks.js";
  2. import type { IBroadcastService, IServiceAccessor } from "@zcode/services";
  3. import { createElement } from "react";
  4. import { renderToStaticMarkup } from "react-dom/server";
  5. import { testId, TID_TOOL_SUMMARY_TRIGGER } from "@zcode/shared/test-ids";
  6. import { afterEach, describe, expect, it, vi } from "vitest";
  7. import { ServiceProvider } from "@/hooks/useServices.js";
  8. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  9. import { StoreProvider } from "@/store/StoreProvider.js";
  10. import { useSubagentsStore } from "@/store/subagentsStore.js";
  11. import { AgentToolCallBlock } from "../src/ToolCallBlocks/renderers/agent.js";
  12. const localStorageState = new Map<string, string>();
  13. Object.defineProperty(globalThis, "localStorage", {
  14. configurable: true,
  15. value: {
  16. getItem: (key: string) => localStorageState.get(key) ?? null,
  17. setItem: (key: string, value: string) => {
  18. localStorageState.set(key, value);
  19. },
  20. removeItem: (key: string) => {
  21. localStorageState.delete(key);
  22. },
  23. clear: () => {
  24. localStorageState.clear();
  25. },
  26. },
  27. });
  28. Object.defineProperty(globalThis, "document", {
  29. configurable: true,
  30. value: {
  31. documentElement: {
  32. classList: {
  33. contains: () => false,
  34. toggle: () => {},
  35. },
  36. },
  37. },
  38. });
  39. const mockBroadcastService: IBroadcastService = {
  40. send: async () => {},
  41. onMessage: () => ({ dispose: () => {} }),
  42. };
  43. const mockServices = {
  44. fileService: {
  45. readMediaPreview: async () => ({
  46. path: "/workspace/assets/logo.png",
  47. mediaType: "image/png",
  48. dataBase64: "",
  49. totalBytes: 0,
  50. }),
  51. },
  52. } as IServiceAccessor;
  53. describe("AgentToolCallBlock", () => {
  54. afterEach(() => {
  55. useSubagentsStore.setState({ agents: [] });
  56. });
  57. it("keeps an Agent without a persisted child as a static one-line summary", () => {
  58. const html = renderToStaticMarkup(
  59. createElement(
  60. ZCodeIntlProvider,
  61. { initialLocale: "zh-CN" },
  62. createElement(
  63. ServiceProvider,
  64. { services: mockServices },
  65. createElement(
  66. StoreProvider,
  67. { broadcastService: mockBroadcastService },
  68. createElement(AgentToolCallBlock, {
  69. toolCallNode: {
  70. toolCall: {
  71. toolId: "tool-agent-running",
  72. kind: "think",
  73. title: "Inspect workspace",
  74. input: {
  75. prompt: "分析当前工作区并继续拆分任务",
  76. subagent_type: "Explore",
  77. },
  78. output: [],
  79. status: "running",
  80. raw: {
  81. _meta: {
  82. zcode: {
  83. toolName: "Agent",
  84. },
  85. },
  86. },
  87. },
  88. childToolCalls: [],
  89. },
  90. workspacePath: "/workspace",
  91. displayModel: {
  92. inlinePreview: { type: "none" },
  93. planResult: null,
  94. viewerSource: null,
  95. viewerLabelId: "codeViewer.viewCode",
  96. showSummaryFileLink: false,
  97. showInput: false,
  98. showOutput: false,
  99. showKind: false,
  100. },
  101. viewerSource: null,
  102. rawFileSummaries: [],
  103. isRunning: true,
  104. statusLabel: "执行中",
  105. errorText: undefined,
  106. childToolList: null,
  107. showIcon: true,
  108. forceOpen: false,
  109. onOpenCodeViewer: undefined,
  110. onOpenBrowserUrl: undefined,
  111. }),
  112. ),
  113. ),
  114. ),
  115. );
  116. expect(html).toContain(testId(TID_TOOL_SUMMARY_TRIGGER, "tool-agent-running"));
  117. expect(html).toContain("Inspect workspace");
  118. expect(html).not.toContain('role="button"');
  119. expect(html).not.toContain("aria-expanded");
  120. expect(html).not.toMatch(/提示词|Prompt/);
  121. expect(html).not.toContain("分析当前工作区并继续拆分任务");
  122. });
  123. it("uses a side-pane summary action instead of expandable details", () => {
  124. const html = renderToStaticMarkup(
  125. createElement(
  126. ZCodeIntlProvider,
  127. { initialLocale: "zh-CN" },
  128. createElement(
  129. ServiceProvider,
  130. { services: mockServices },
  131. createElement(
  132. StoreProvider,
  133. { broadcastService: mockBroadcastService },
  134. createElement(AgentToolCallBlock, {
  135. toolCallNode: {
  136. toolCall: {
  137. toolId: "tool-agent-summary-action",
  138. kind: "think",
  139. title: "Inspect workspace",
  140. input: {
  141. prompt: "分析当前工作区并继续拆分任务",
  142. subagent_type: "Explore",
  143. },
  144. output: [],
  145. status: "running",
  146. raw: {
  147. _meta: {
  148. zcode: {
  149. toolName: "Agent",
  150. },
  151. },
  152. },
  153. },
  154. childToolCalls: [],
  155. },
  156. workspacePath: "/workspace",
  157. displayModel: {
  158. inlinePreview: { type: "none" },
  159. planResult: null,
  160. viewerSource: null,
  161. viewerLabelId: "codeViewer.viewCode",
  162. showSummaryFileLink: false,
  163. showInput: false,
  164. showOutput: false,
  165. showKind: false,
  166. },
  167. viewerSource: null,
  168. rawFileSummaries: [],
  169. isRunning: true,
  170. statusLabel: "执行中",
  171. errorText: undefined,
  172. childToolList: null,
  173. showIcon: true,
  174. agentSummaryAction: {
  175. onActivate: vi.fn(),
  176. testId: "v4-subagent-open-side-pane-child-session",
  177. },
  178. onOpenCodeViewer: undefined,
  179. onOpenBrowserUrl: undefined,
  180. }),
  181. ),
  182. ),
  183. ),
  184. );
  185. expect(html).toContain('data-testid="v4-subagent-open-side-pane-child-session"');
  186. expect(html).toContain('aria-label="在右侧打开"');
  187. expect(html).not.toContain("aria-expanded");
  188. expect(html).not.toContain("subagent-output-preview");
  189. expect(html).not.toContain("分析当前工作区并继续拆分任务");
  190. });
  191. it("never falls back to inline prompt or child output", () => {
  192. const html = renderToStaticMarkup(
  193. createElement(
  194. ZCodeIntlProvider,
  195. { initialLocale: "zh-CN" },
  196. createElement(
  197. ServiceProvider,
  198. { services: mockServices },
  199. createElement(
  200. StoreProvider,
  201. { broadcastService: mockBroadcastService },
  202. createElement(AgentToolCallBlock, {
  203. toolCallNode: {
  204. toolCall: {
  205. toolId: "tool-agent-drilldown-slot",
  206. kind: "think",
  207. title: "Inspect workspace",
  208. input: {
  209. prompt: "分析当前工作区并继续拆分任务",
  210. subagent_type: "Explore",
  211. },
  212. output: [],
  213. status: "running",
  214. raw: {
  215. _meta: {
  216. zcode: {
  217. toolName: "Agent",
  218. },
  219. },
  220. },
  221. },
  222. childToolCalls: [],
  223. },
  224. workspacePath: "/workspace",
  225. displayModel: {
  226. inlinePreview: { type: "none" },
  227. planResult: null,
  228. viewerSource: null,
  229. viewerLabelId: "codeViewer.viewCode",
  230. showSummaryFileLink: false,
  231. showInput: false,
  232. showOutput: false,
  233. showKind: false,
  234. },
  235. viewerSource: null,
  236. rawFileSummaries: [],
  237. isRunning: true,
  238. statusLabel: "执行中",
  239. errorText: undefined,
  240. childToolList: null,
  241. showIcon: true,
  242. forceOpen: true,
  243. onOpenCodeViewer: undefined,
  244. onOpenBrowserUrl: undefined,
  245. }),
  246. ),
  247. ),
  248. ),
  249. );
  250. expect(html).not.toMatch(/提示词|Prompt/);
  251. expect(html).not.toContain("分析当前工作区并继续拆分任务");
  252. expect(html).not.toContain("subagent-output-preview");
  253. expect(html).not.toContain("aria-expanded");
  254. expect(html).not.toContain("在右侧打开");
  255. expect(html).not.toContain("子会话");
  256. });
  257. it("colors the subagent name from the configured agent color", () => {
  258. useSubagentsStore.setState({
  259. agents: [
  260. {
  261. id: "agent-explore",
  262. name: "Explore",
  263. description: "Explore workspace",
  264. systemPrompt: "Explore",
  265. color: "red",
  266. path: "/workspace/.zcode/agents/explore.md",
  267. scope: "user",
  268. source: "user",
  269. enabled: true,
  270. },
  271. ],
  272. });
  273. const html = renderToStaticMarkup(
  274. createElement(
  275. ZCodeIntlProvider,
  276. { initialLocale: "zh-CN" },
  277. createElement(
  278. ServiceProvider,
  279. { services: mockServices },
  280. createElement(
  281. StoreProvider,
  282. { broadcastService: mockBroadcastService },
  283. createElement(ToolCallBlock, {
  284. toolCallNode: {
  285. toolCall: {
  286. toolId: "tool-agent-configured-color",
  287. kind: "think",
  288. title: "Agent",
  289. input: {
  290. prompt: "检查工作区",
  291. subagent_type: "Explore",
  292. },
  293. output: [],
  294. status: "completed",
  295. raw: {},
  296. },
  297. childToolCalls: [],
  298. },
  299. workspacePath: "/workspace",
  300. displayModel: {
  301. inlinePreview: { type: "none" },
  302. planResult: null,
  303. viewerSource: null,
  304. viewerLabelId: "codeViewer.viewCode",
  305. showSummaryFileLink: false,
  306. showInput: false,
  307. showOutput: false,
  308. showKind: false,
  309. },
  310. viewerSource: null,
  311. rawFileSummaries: [],
  312. isRunning: false,
  313. statusLabel: "完成",
  314. errorText: undefined,
  315. childToolList: null,
  316. showIcon: true,
  317. forceOpen: false,
  318. onOpenCodeViewer: undefined,
  319. onOpenBrowserUrl: undefined,
  320. }),
  321. ),
  322. ),
  323. ),
  324. );
  325. expect(html).toContain("Explore");
  326. expect(html).toContain("text-rose-700");
  327. });
  328. it("keeps the agent type blank while subagent_type may still be streaming", () => {
  329. const html = renderToStaticMarkup(
  330. createElement(
  331. ZCodeIntlProvider,
  332. { initialLocale: "zh-CN" },
  333. createElement(
  334. ServiceProvider,
  335. { services: mockServices },
  336. createElement(
  337. StoreProvider,
  338. { broadcastService: mockBroadcastService },
  339. createElement(AgentToolCallBlock, {
  340. toolCallNode: {
  341. toolCall: {
  342. toolId: "tool-agent-streaming-type",
  343. kind: "Agent",
  344. title: "Agent",
  345. input: {
  346. description: "检查流式参数",
  347. prompt: "等待完整输入",
  348. },
  349. output: "",
  350. status: "pending",
  351. raw: {
  352. toolName: "Agent",
  353. inputPreviewComplete: false,
  354. },
  355. },
  356. childToolCalls: [],
  357. },
  358. workspacePath: "/workspace",
  359. displayModel: {
  360. inlinePreview: { type: "none" },
  361. planResult: null,
  362. viewerSource: null,
  363. viewerLabelId: "codeViewer.viewCode",
  364. showSummaryFileLink: false,
  365. showInput: false,
  366. showOutput: false,
  367. showKind: false,
  368. },
  369. viewerSource: null,
  370. rawFileSummaries: [],
  371. isRunning: true,
  372. statusLabel: "等待中",
  373. errorText: undefined,
  374. childToolList: null,
  375. showIcon: true,
  376. forceOpen: false,
  377. onOpenCodeViewer: undefined,
  378. onOpenBrowserUrl: undefined,
  379. }),
  380. ),
  381. ),
  382. ),
  383. );
  384. expect(html).toContain("检查流式参数");
  385. expect(html).not.toContain("general-purpose");
  386. });
  387. it("falls back to the default general-purpose agent type only after complete input omits subagent_type", () => {
  388. const html = renderToStaticMarkup(
  389. createElement(
  390. ZCodeIntlProvider,
  391. { initialLocale: "zh-CN" },
  392. createElement(
  393. ServiceProvider,
  394. { services: mockServices },
  395. createElement(
  396. StoreProvider,
  397. { broadcastService: mockBroadcastService },
  398. createElement(AgentToolCallBlock, {
  399. toolCallNode: {
  400. toolCall: {
  401. toolId: "tool-agent-default-type",
  402. kind: "Agent",
  403. title: "Agent",
  404. input: {
  405. description: "抓取 bettingexpert 赔率",
  406. prompt: "获取赔率信息",
  407. run_in_background: true,
  408. },
  409. output: "Async agent launched successfully.",
  410. status: "running",
  411. raw: {
  412. inputPreviewComplete: true,
  413. _meta: {
  414. zcode: {
  415. toolName: "Agent",
  416. },
  417. },
  418. },
  419. },
  420. childToolCalls: [],
  421. },
  422. workspacePath: "/workspace",
  423. displayModel: {
  424. inlinePreview: { type: "none" },
  425. planResult: null,
  426. viewerSource: null,
  427. viewerLabelId: "codeViewer.viewCode",
  428. showSummaryFileLink: false,
  429. showInput: false,
  430. showOutput: false,
  431. showKind: false,
  432. },
  433. viewerSource: null,
  434. rawFileSummaries: [],
  435. isRunning: true,
  436. statusLabel: "执行中",
  437. errorText: undefined,
  438. childToolList: null,
  439. showIcon: true,
  440. forceOpen: false,
  441. onOpenCodeViewer: undefined,
  442. onOpenBrowserUrl: undefined,
  443. }),
  444. ),
  445. ),
  446. ),
  447. );
  448. expect(html).toContain("general-purpose");
  449. });
  450. it("uses the latest child tool summary for a collapsed running subagent", () => {
  451. const html = renderToStaticMarkup(
  452. createElement(
  453. ZCodeIntlProvider,
  454. { initialLocale: "zh-CN" },
  455. createElement(
  456. ServiceProvider,
  457. { services: mockServices },
  458. createElement(
  459. StoreProvider,
  460. { broadcastService: mockBroadcastService },
  461. createElement(AgentToolCallBlock, {
  462. toolCallNode: {
  463. toolCall: {
  464. toolId: "tool-agent-child-summary",
  465. kind: "Agent",
  466. title: "Agent",
  467. input: {
  468. prompt: "读取核心入口文件",
  469. subagent_type: "Explore",
  470. },
  471. output: "",
  472. status: "running",
  473. raw: {
  474. _meta: {
  475. zcode: {
  476. toolName: "Agent",
  477. },
  478. },
  479. },
  480. },
  481. childToolCalls: [
  482. {
  483. toolCall: {
  484. toolId: "tool-agent-child-read",
  485. kind: "read",
  486. title: "Read",
  487. input: {
  488. path: "/workspace/src/App.tsx",
  489. },
  490. output: "",
  491. status: "running",
  492. raw: {},
  493. },
  494. childToolCalls: [],
  495. },
  496. ],
  497. },
  498. workspacePath: "/workspace",
  499. displayModel: {
  500. inlinePreview: { type: "none" },
  501. planResult: null,
  502. viewerSource: null,
  503. viewerLabelId: "codeViewer.viewCode",
  504. showSummaryFileLink: false,
  505. showInput: false,
  506. showOutput: false,
  507. showKind: false,
  508. },
  509. viewerSource: null,
  510. rawFileSummaries: [],
  511. isRunning: true,
  512. statusLabel: "执行中",
  513. errorText: undefined,
  514. childToolList: null,
  515. showIcon: true,
  516. forceOpen: false,
  517. onOpenCodeViewer: undefined,
  518. onOpenBrowserUrl: undefined,
  519. }),
  520. ),
  521. ),
  522. ),
  523. );
  524. // Bugfix: Agent 收起态使用最新子工具摘要,外层仍展示子智能体。
  525. expect(html).toMatch(/>(SubAgent|子智能体)</);
  526. expect(html).toContain("·");
  527. expect(html).toContain("正在读取");
  528. expect(html).toContain("App.tsx");
  529. expect(html).toContain("/workspace/src");
  530. expect(html).not.toContain("读取核心入口文件");
  531. });
  532. it("does not keep a completed subagent visually running while a child tool is running", () => {
  533. const html = renderToStaticMarkup(
  534. createElement(
  535. ZCodeIntlProvider,
  536. { initialLocale: "zh-CN" },
  537. createElement(
  538. ServiceProvider,
  539. { services: mockServices },
  540. createElement(
  541. StoreProvider,
  542. { broadcastService: mockBroadcastService },
  543. createElement(AgentToolCallBlock, {
  544. toolCallNode: {
  545. toolCall: {
  546. toolId: "tool-agent-completed-child-running",
  547. kind: "Agent",
  548. title: "Agent",
  549. input: {
  550. prompt: "读取核心入口文件",
  551. subagent_type: "Explore",
  552. },
  553. output: "",
  554. status: "completed",
  555. raw: {
  556. _meta: {
  557. zcode: {
  558. toolName: "Agent",
  559. },
  560. },
  561. },
  562. },
  563. childToolCalls: [
  564. {
  565. toolCall: {
  566. toolId: "tool-agent-child-read-still-running",
  567. kind: "read",
  568. title: "Read",
  569. input: {
  570. path: "/workspace/src/App.tsx",
  571. },
  572. output: "",
  573. status: "in_progress",
  574. raw: {},
  575. },
  576. childToolCalls: [],
  577. },
  578. ],
  579. },
  580. workspacePath: "/workspace",
  581. displayModel: {
  582. inlinePreview: { type: "none" },
  583. planResult: null,
  584. viewerSource: null,
  585. viewerLabelId: "codeViewer.viewCode",
  586. showSummaryFileLink: false,
  587. showInput: false,
  588. showOutput: false,
  589. showKind: false,
  590. },
  591. viewerSource: null,
  592. rawFileSummaries: [],
  593. isRunning: false,
  594. statusLabel: "已执行",
  595. errorText: undefined,
  596. childToolList: null,
  597. showIcon: true,
  598. forceOpen: false,
  599. onOpenCodeViewer: undefined,
  600. onOpenBrowserUrl: undefined,
  601. }),
  602. ),
  603. ),
  604. ),
  605. );
  606. // Bugfix: 父 Agent 的完成态只看父 tool 生命周期;
  607. // 子工具属于展开区明细,不能让 completed 父块继续显示 running 渐变。
  608. expect(html).toMatch(/>(SubAgent|子智能体)</);
  609. expect(html).toContain("Explore");
  610. expect(html).not.toContain("正在读取");
  611. expect(html).not.toContain("App.tsx");
  612. expect(html).not.toContain("animated-gradient-text");
  613. expect(html).not.toContain("读取核心入口文件");
  614. });
  615. it("keeps the child action label for completed subagent child summaries", () => {
  616. const html = renderToStaticMarkup(
  617. createElement(
  618. ZCodeIntlProvider,
  619. { initialLocale: "zh-CN" },
  620. createElement(
  621. ServiceProvider,
  622. { services: mockServices },
  623. createElement(
  624. StoreProvider,
  625. { broadcastService: mockBroadcastService },
  626. createElement(AgentToolCallBlock, {
  627. toolCallNode: {
  628. toolCall: {
  629. toolId: "tool-agent-completed-child-summary",
  630. kind: "Agent",
  631. title: "Agent",
  632. input: {
  633. prompt: "读取核心入口文件",
  634. subagent_type: "Explore",
  635. },
  636. output: "",
  637. status: "running",
  638. raw: {
  639. _meta: {
  640. zcode: {
  641. toolName: "Agent",
  642. },
  643. },
  644. },
  645. },
  646. childToolCalls: [
  647. {
  648. toolCall: {
  649. toolId: "tool-agent-completed-child-read",
  650. kind: "read",
  651. title: "Read",
  652. input: {
  653. path: "/workspace/src/App.tsx",
  654. },
  655. output: "",
  656. status: "completed",
  657. raw: {},
  658. },
  659. childToolCalls: [],
  660. },
  661. ],
  662. },
  663. workspacePath: "/workspace",
  664. displayModel: {
  665. inlinePreview: { type: "none" },
  666. planResult: null,
  667. viewerSource: null,
  668. viewerLabelId: "codeViewer.viewCode",
  669. showSummaryFileLink: false,
  670. showInput: false,
  671. showOutput: false,
  672. showKind: false,
  673. },
  674. viewerSource: null,
  675. rawFileSummaries: [],
  676. isRunning: true,
  677. statusLabel: "执行中",
  678. errorText: undefined,
  679. childToolList: null,
  680. showIcon: true,
  681. forceOpen: false,
  682. onOpenCodeViewer: undefined,
  683. onOpenBrowserUrl: undefined,
  684. }),
  685. ),
  686. ),
  687. ),
  688. );
  689. expect(html).toContain("·");
  690. expect(html).toContain("App.tsx");
  691. expect(html).toContain("正在读取");
  692. });
  693. it("summarizes TodoWrite child tools with todo progress and active content", () => {
  694. const html = renderToStaticMarkup(
  695. createElement(
  696. ZCodeIntlProvider,
  697. { initialLocale: "zh-CN" },
  698. createElement(
  699. ServiceProvider,
  700. { services: mockServices },
  701. createElement(
  702. StoreProvider,
  703. { broadcastService: mockBroadcastService },
  704. createElement(AgentToolCallBlock, {
  705. toolCallNode: {
  706. toolCall: {
  707. toolId: "tool-agent-todo-child-summary",
  708. kind: "Agent",
  709. title: "Agent",
  710. input: {
  711. prompt: "实现 toolcall 摘要优化",
  712. subagent_type: "Explore",
  713. },
  714. output: "",
  715. status: "running",
  716. raw: {
  717. _meta: {
  718. zcode: {
  719. toolName: "Agent",
  720. },
  721. },
  722. },
  723. },
  724. childToolCalls: [
  725. {
  726. toolCall: {
  727. toolId: "tool-agent-child-todo",
  728. toolName: "TodoWrite",
  729. kind: "TodoWrite",
  730. title: "TodoWrite",
  731. input: {
  732. todos: [
  733. {
  734. content: "确认现有 Agent 摘要行为",
  735. status: "completed",
  736. },
  737. {
  738. content: "补齐待办子工具摘要",
  739. status: "in_progress",
  740. },
  741. {
  742. content: "跑回归测试",
  743. status: "pending",
  744. },
  745. ],
  746. },
  747. output: "",
  748. status: "running",
  749. raw: {},
  750. },
  751. childToolCalls: [],
  752. },
  753. ],
  754. },
  755. workspacePath: "/workspace",
  756. displayModel: {
  757. inlinePreview: { type: "none" },
  758. planResult: null,
  759. viewerSource: null,
  760. viewerLabelId: "codeViewer.viewCode",
  761. showSummaryFileLink: false,
  762. showInput: false,
  763. showOutput: false,
  764. showKind: false,
  765. },
  766. viewerSource: null,
  767. rawFileSummaries: [],
  768. isRunning: true,
  769. statusLabel: "执行中",
  770. errorText: undefined,
  771. childToolList: null,
  772. showIcon: true,
  773. forceOpen: false,
  774. onOpenCodeViewer: undefined,
  775. onOpenBrowserUrl: undefined,
  776. }),
  777. ),
  778. ),
  779. ),
  780. );
  781. expect(html).toContain("·");
  782. expect(html).toContain("更新待办");
  783. expect(html).toContain("1/3");
  784. expect(html).toContain("补齐待办子工具摘要");
  785. expect(html).not.toContain("Running TodoWrite");
  786. });
  787. it("uses SubAgent as the outer kind instead of the resolved agent type", () => {
  788. const html = renderToStaticMarkup(
  789. createElement(
  790. ZCodeIntlProvider,
  791. { initialLocale: "zh-CN" },
  792. createElement(
  793. ServiceProvider,
  794. { services: mockServices },
  795. createElement(
  796. StoreProvider,
  797. { broadcastService: mockBroadcastService },
  798. createElement(AgentToolCallBlock, {
  799. toolCallNode: {
  800. toolCall: {
  801. toolId: "tool-agent-numbered",
  802. kind: "think",
  803. title: "Agent",
  804. input: {
  805. description: "Explore project structure",
  806. prompt: "搜索国际与商业新闻",
  807. },
  808. output: JSON.stringify({
  809. status: "completed",
  810. agentId: "agent_6e358228-a52f-427a-b1ae-d30b5cd9899e",
  811. agentType: "Explore",
  812. description: "Explore project structure",
  813. }),
  814. status: "completed",
  815. raw: {
  816. _meta: {
  817. zcode: {
  818. toolName: "Agent",
  819. },
  820. },
  821. },
  822. },
  823. childToolCalls: [],
  824. },
  825. workspacePath: "/workspace",
  826. displayModel: {
  827. inlinePreview: { type: "none" },
  828. planResult: null,
  829. viewerSource: null,
  830. viewerLabelId: "codeViewer.viewCode",
  831. showSummaryFileLink: false,
  832. showInput: false,
  833. showOutput: false,
  834. showKind: false,
  835. },
  836. viewerSource: null,
  837. rawFileSummaries: [],
  838. isRunning: false,
  839. statusLabel: "已执行",
  840. errorText: undefined,
  841. childToolList: null,
  842. showIcon: true,
  843. forceOpen: false,
  844. sourceLabel: "SubAgent",
  845. onOpenCodeViewer: undefined,
  846. onOpenBrowserUrl: undefined,
  847. }),
  848. ),
  849. ),
  850. ),
  851. );
  852. // 外层 kind 固定显示 SubAgent;真实子智能体类型作为彩色名称展示。
  853. expect(html).toMatch(/>(SubAgent|子智能体)</);
  854. expect(html).toContain("·");
  855. expect(html).toContain("Explore project structure");
  856. expect(html).not.toContain("子智能体 2");
  857. expect(html).not.toMatch(/>Agent</);
  858. expect(html).toMatch(/>Explore</);
  859. });
  860. it("uses spawn_agent subagent nickname as the summary primary text", () => {
  861. const html = renderToStaticMarkup(
  862. createElement(
  863. ZCodeIntlProvider,
  864. { initialLocale: "zh-CN" },
  865. createElement(
  866. ServiceProvider,
  867. { services: mockServices },
  868. createElement(
  869. StoreProvider,
  870. { broadcastService: mockBroadcastService },
  871. createElement(AgentToolCallBlock, {
  872. toolCallNode: {
  873. toolCall: {
  874. toolId: "tool-spawn-agent-locke",
  875. kind: "spawn_agent",
  876. title: "spawn_agent",
  877. input: {
  878. agent_type: "explorer",
  879. message: "在当前项目里做只读结构检查",
  880. },
  881. output: "",
  882. status: "completed",
  883. raw: {
  884. name: "spawn_agent",
  885. _meta: {
  886. zcode: {
  887. toolName: "spawn_agent",
  888. nickname: "Locke",
  889. },
  890. },
  891. },
  892. },
  893. childToolCalls: [],
  894. },
  895. workspacePath: "/workspace",
  896. displayModel: {
  897. inlinePreview: { type: "none" },
  898. planResult: null,
  899. viewerSource: null,
  900. viewerLabelId: "codeViewer.viewCode",
  901. showSummaryFileLink: false,
  902. showInput: false,
  903. showOutput: false,
  904. showKind: false,
  905. },
  906. viewerSource: null,
  907. rawFileSummaries: [],
  908. isRunning: false,
  909. statusLabel: "已执行",
  910. errorText: undefined,
  911. childToolList: null,
  912. showIcon: true,
  913. forceOpen: false,
  914. onOpenCodeViewer: undefined,
  915. onOpenBrowserUrl: undefined,
  916. }),
  917. ),
  918. ),
  919. ),
  920. );
  921. // Bugfix: provider 原生子 agent 工具名 spawn_agent 直接展示会暴露实现细节;
  922. // 外层 kind 固定为 SubAgent,nickname 作为彩色名称和摘要正文展示。
  923. expect(html).not.toMatch(/>explorer</);
  924. expect(html).toMatch(/>(SubAgent|子智能体)</);
  925. expect(html).toContain("Locke");
  926. expect(html).not.toContain("子智能体 1");
  927. expect(html).not.toContain("spawn_agent");
  928. expect(html).not.toContain("在当前项目里做只读结构检查");
  929. });
  930. it("falls back to spawn_agent agent_type when nickname is not available", () => {
  931. const html = renderToStaticMarkup(
  932. createElement(
  933. ZCodeIntlProvider,
  934. { initialLocale: "zh-CN" },
  935. createElement(
  936. ServiceProvider,
  937. { services: mockServices },
  938. createElement(
  939. StoreProvider,
  940. { broadcastService: mockBroadcastService },
  941. createElement(AgentToolCallBlock, {
  942. toolCallNode: {
  943. toolCall: {
  944. toolId: "tool-spawn-agent-explorer",
  945. kind: "spawn_agent",
  946. title: "spawn_agent",
  947. input: {
  948. agent_type: "explorer",
  949. message: "在当前项目里做只读结构检查",
  950. },
  951. output: "",
  952. status: "pending",
  953. raw: {
  954. name: "spawn_agent",
  955. },
  956. },
  957. childToolCalls: [],
  958. },
  959. workspacePath: "/workspace",
  960. displayModel: {
  961. inlinePreview: { type: "none" },
  962. planResult: null,
  963. viewerSource: null,
  964. viewerLabelId: "codeViewer.viewCode",
  965. showSummaryFileLink: false,
  966. showInput: false,
  967. showOutput: false,
  968. showKind: false,
  969. },
  970. viewerSource: null,
  971. rawFileSummaries: [],
  972. isRunning: true,
  973. statusLabel: "执行中",
  974. errorText: undefined,
  975. childToolList: null,
  976. showIcon: true,
  977. forceOpen: false,
  978. onOpenCodeViewer: undefined,
  979. onOpenBrowserUrl: undefined,
  980. }),
  981. ),
  982. ),
  983. ),
  984. );
  985. expect(html).toContain("explorer");
  986. expect(html).toMatch(/>(SubAgent|子智能体)</);
  987. expect(html).not.toContain("子智能体 1");
  988. expect(html).not.toContain("spawn_agent");
  989. });
  990. it("renders prompt and child tool calls without result content", () => {
  991. const html = renderToStaticMarkup(
  992. createElement(
  993. ZCodeIntlProvider,
  994. { initialLocale: "zh-CN" },
  995. createElement(
  996. ServiceProvider,
  997. { services: mockServices },
  998. createElement(
  999. StoreProvider,
  1000. { broadcastService: mockBroadcastService },
  1001. createElement(AgentToolCallBlock, {
  1002. toolCallNode: {
  1003. toolCall: {
  1004. toolId: "tool-agent-render",
  1005. kind: "think",
  1006. title: "Explore project structure",
  1007. input: {
  1008. prompt: "请分析项目结构并列出入口点",
  1009. subagent_type: "Explore",
  1010. },
  1011. output: [
  1012. {
  1013. type: "text",
  1014. text: "这段结果不应该直接展示在 Agent 展开区",
  1015. },
  1016. ],
  1017. status: "completed",
  1018. raw: {
  1019. _meta: {
  1020. zcode: {
  1021. toolName: "Agent",
  1022. },
  1023. },
  1024. },
  1025. },
  1026. childToolCalls: [
  1027. {
  1028. toolCall: {
  1029. toolId: "tool-agent-child",
  1030. kind: "search",
  1031. title: "Find package.json",
  1032. input: {
  1033. query: "package.json",
  1034. },
  1035. output: "...",
  1036. status: "completed",
  1037. raw: {},
  1038. },
  1039. childToolCalls: [],
  1040. },
  1041. ],
  1042. },
  1043. workspacePath: "/workspace",
  1044. displayModel: {
  1045. inlinePreview: { type: "none" },
  1046. planResult: null,
  1047. viewerSource: null,
  1048. viewerLabelId: "codeViewer.viewCode",
  1049. showSummaryFileLink: false,
  1050. showInput: false,
  1051. showOutput: false,
  1052. showKind: false,
  1053. },
  1054. viewerSource: null,
  1055. rawFileSummaries: [],
  1056. isRunning: false,
  1057. statusLabel: "已执行",
  1058. errorText: undefined,
  1059. childToolList: null,
  1060. showIcon: true,
  1061. forceOpen: true,
  1062. onOpenCodeViewer: undefined,
  1063. onOpenBrowserUrl: undefined,
  1064. }),
  1065. ),
  1066. ),
  1067. ),
  1068. );
  1069. // Agent/Task 在消息流中固定为单行;详情只通过持久化 child session 打开。
  1070. expect(html).toMatch(/>(SubAgent|子智能体)</);
  1071. expect(html).toMatch(/>Explore</);
  1072. expect(html).toContain("Explore project structure");
  1073. expect(html).not.toMatch(/提示词|Prompt/);
  1074. expect(html).not.toContain("请分析项目结构并列出入口点");
  1075. expect(html).not.toContain("Find package.json");
  1076. expect(html).not.toContain("aria-expanded");
  1077. expect(html).not.toContain("这段结果不应该直接展示在 Agent 展开区");
  1078. });
  1079. it("keeps background agent internals out of the static summary", () => {
  1080. const html = renderToStaticMarkup(
  1081. createElement(
  1082. ZCodeIntlProvider,
  1083. { initialLocale: "zh-CN" },
  1084. createElement(
  1085. ServiceProvider,
  1086. { services: mockServices },
  1087. createElement(
  1088. StoreProvider,
  1089. { broadcastService: mockBroadcastService },
  1090. createElement(AgentToolCallBlock, {
  1091. toolCallNode: {
  1092. toolCall: {
  1093. toolId: "tool-background-agent",
  1094. kind: "think",
  1095. title: "Task",
  1096. input: {
  1097. prompt: "搜索今日新闻",
  1098. run_in_background: true,
  1099. },
  1100. output: [
  1101. {
  1102. type: "text",
  1103. text: "Async agent launched successfully.\nagentId: internal-agent-id (internal ID - do not mention to user.)\noutput_file: /tmp/background-agent.output\n",
  1104. },
  1105. ],
  1106. status: "completed",
  1107. raw: {
  1108. _meta: {
  1109. zcode: {
  1110. toolName: "Agent",
  1111. backgroundAgent: {
  1112. runInBackground: true,
  1113. agentId: "internal-agent-id",
  1114. outputFile: "/tmp/background-agent.output",
  1115. },
  1116. },
  1117. },
  1118. },
  1119. },
  1120. childToolCalls: [],
  1121. },
  1122. workspacePath: "/workspace",
  1123. displayModel: {
  1124. inlinePreview: { type: "none" },
  1125. planResult: null,
  1126. viewerSource: null,
  1127. viewerLabelId: "codeViewer.viewCode",
  1128. showSummaryFileLink: false,
  1129. showInput: false,
  1130. showOutput: false,
  1131. showKind: false,
  1132. },
  1133. viewerSource: null,
  1134. rawFileSummaries: [],
  1135. isRunning: false,
  1136. statusLabel: "已执行",
  1137. errorText: undefined,
  1138. childToolList: null,
  1139. showIcon: true,
  1140. forceOpen: true,
  1141. onOpenCodeViewer: undefined,
  1142. onOpenBrowserUrl: undefined,
  1143. }),
  1144. ),
  1145. ),
  1146. ),
  1147. );
  1148. expect(html).toMatch(/子智能体|SubAgent/);
  1149. expect(html).not.toContain("/tmp/background-agent.output");
  1150. expect(html).not.toContain("internal-agent-id");
  1151. });
  1152. it("does not inline background child activity", () => {
  1153. const html = renderToStaticMarkup(
  1154. createElement(
  1155. ZCodeIntlProvider,
  1156. { initialLocale: "zh-CN" },
  1157. createElement(
  1158. ServiceProvider,
  1159. { services: mockServices },
  1160. createElement(
  1161. StoreProvider,
  1162. { broadcastService: mockBroadcastService },
  1163. createElement(AgentToolCallBlock, {
  1164. toolCallNode: {
  1165. toolCall: {
  1166. toolId: "tool-background-agent-activity",
  1167. kind: "think",
  1168. title: "Task",
  1169. input: {
  1170. prompt: "后台调研项目结构",
  1171. run_in_background: true,
  1172. },
  1173. output: "",
  1174. content: "已读取 package.json,继续分析入口文件。",
  1175. thought: "先确认项目结构。",
  1176. status: "completed",
  1177. raw: {
  1178. _meta: {
  1179. zcode: {
  1180. toolName: "Agent",
  1181. backgroundAgent: {
  1182. runInBackground: true,
  1183. },
  1184. },
  1185. },
  1186. },
  1187. },
  1188. childToolCalls: [],
  1189. },
  1190. workspacePath: "/workspace",
  1191. displayModel: {
  1192. inlinePreview: { type: "none" },
  1193. planResult: null,
  1194. viewerSource: null,
  1195. viewerLabelId: "codeViewer.viewCode",
  1196. showSummaryFileLink: false,
  1197. showInput: false,
  1198. showOutput: false,
  1199. showKind: false,
  1200. },
  1201. viewerSource: null,
  1202. rawFileSummaries: [],
  1203. isRunning: false,
  1204. statusLabel: "已执行",
  1205. errorText: undefined,
  1206. childToolList: null,
  1207. showIcon: true,
  1208. forceOpen: true,
  1209. onOpenCodeViewer: undefined,
  1210. onOpenBrowserUrl: undefined,
  1211. }),
  1212. ),
  1213. ),
  1214. ),
  1215. );
  1216. expect(html).not.toContain("先确认项目结构。");
  1217. expect(html).not.toContain("已读取 package.json");
  1218. expect(html).not.toContain("data-markdown-table-sticky-scrollbar");
  1219. });
  1220. it("does not inline either task notification or raw background transcript", () => {
  1221. const html = renderToStaticMarkup(
  1222. createElement(
  1223. ZCodeIntlProvider,
  1224. { initialLocale: "zh-CN" },
  1225. createElement(
  1226. ServiceProvider,
  1227. { services: mockServices },
  1228. createElement(
  1229. StoreProvider,
  1230. { broadcastService: mockBroadcastService },
  1231. createElement(AgentToolCallBlock, {
  1232. toolCallNode: {
  1233. toolCall: {
  1234. toolId: "tool-background-agent-summary",
  1235. kind: "think",
  1236. title: "Implement Todo types and components",
  1237. input: {
  1238. prompt: "实现 Todo 类型和组件",
  1239. run_in_background: true,
  1240. },
  1241. output: "",
  1242. content: '{"isSidechain":true,"message":"raw transcript"}',
  1243. status: "completed",
  1244. raw: {
  1245. _meta: {
  1246. zcode: {
  1247. toolName: "Agent",
  1248. backgroundAgent: {
  1249. runInBackground: true,
  1250. },
  1251. taskNotification: {
  1252. outputFile: "/tmp/background-agent.output",
  1253. result: "All files are created and the project builds successfully.",
  1254. },
  1255. },
  1256. },
  1257. },
  1258. },
  1259. childToolCalls: [],
  1260. },
  1261. workspacePath: "/workspace",
  1262. displayModel: {
  1263. inlinePreview: { type: "none" },
  1264. planResult: null,
  1265. viewerSource: null,
  1266. viewerLabelId: "codeViewer.viewCode",
  1267. showSummaryFileLink: false,
  1268. showInput: false,
  1269. showOutput: false,
  1270. showKind: false,
  1271. },
  1272. viewerSource: null,
  1273. rawFileSummaries: [],
  1274. isRunning: false,
  1275. statusLabel: "已执行",
  1276. errorText: undefined,
  1277. childToolList: null,
  1278. showIcon: true,
  1279. forceOpen: true,
  1280. onOpenCodeViewer: undefined,
  1281. onOpenBrowserUrl: undefined,
  1282. }),
  1283. ),
  1284. ),
  1285. ),
  1286. );
  1287. expect(html).not.toContain("All files are created and the project builds successfully.");
  1288. expect(html).not.toContain("raw transcript");
  1289. });
  1290. });