messageTaskControlToolCallBlock.test.ts 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. import { createElement, type ComponentType } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { describe, expect, it } from "vitest";
  4. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  5. import type { TaskChatToolCallTreeNode } from "@/lib/toolCallTree.js";
  6. import { ToolCallBlock } from "@/ToolCallBlocks.js";
  7. import { RespondToCoordinatorToolCallBlock } from "@/ToolCallBlocks/renderers/respond-to-coordinator.js";
  8. import { SendMessageToolCallBlock } from "@/ToolCallBlocks/renderers/send-message.js";
  9. import { TaskOutputToolCallBlock } from "@/ToolCallBlocks/renderers/task-output.js";
  10. import { TaskStopToolCallBlock } from "@/ToolCallBlocks/renderers/task-stop.js";
  11. import type { ToolCallBlockRenderContext } from "@/ToolCallBlocks/shared.js";
  12. const displayModel = {
  13. inlinePreview: { type: "none" as const },
  14. planResult: null,
  15. viewerSource: null,
  16. viewerLabelId: "codeViewer.viewCode" as const,
  17. showSummaryFileLink: false,
  18. showInput: false,
  19. showOutput: true,
  20. showKind: false,
  21. };
  22. function renderToolCall(toolCall: TaskChatToolCallTreeNode["toolCall"]) {
  23. return renderToStaticMarkup(
  24. createElement(
  25. ZCodeIntlProvider,
  26. { initialLocale: "zh-CN" },
  27. createElement(ToolCallBlock, {
  28. toolCallNode: {
  29. toolCall,
  30. childToolCalls: [],
  31. },
  32. workspacePath: "/workspace",
  33. }),
  34. ),
  35. );
  36. }
  37. function renderDedicatedToolCall(
  38. Renderer: ComponentType<ToolCallBlockRenderContext>,
  39. toolCall: TaskChatToolCallTreeNode["toolCall"],
  40. overrides: Partial<ToolCallBlockRenderContext> = {},
  41. ) {
  42. return renderToStaticMarkup(
  43. createElement(
  44. ZCodeIntlProvider,
  45. { initialLocale: "zh-CN" },
  46. createElement(Renderer, {
  47. toolCallNode: {
  48. toolCall,
  49. childToolCalls: [],
  50. },
  51. workspacePath: "/workspace",
  52. displayModel,
  53. viewerSource: null,
  54. rawFileSummaries: [],
  55. isRunning: false,
  56. statusLabel: "已执行",
  57. childToolList: null,
  58. showIcon: true,
  59. forceOpen: true,
  60. canToggle: true,
  61. ...overrides,
  62. }),
  63. ),
  64. );
  65. }
  66. describe("message and task control tool call routing", () => {
  67. it("routes SendMessage to a semantic summary", () => {
  68. const html = renderToolCall({
  69. toolId: "send-1",
  70. toolName: "SendMessage",
  71. kind: "SendMessage",
  72. title: "SendMessage",
  73. input: {
  74. to: "agent_alpha",
  75. summary: "同步结论",
  76. message: "请复核结果",
  77. },
  78. output: {
  79. status: "success",
  80. messageId: "msg_1",
  81. delivery: "queued",
  82. },
  83. status: "completed",
  84. raw: {},
  85. });
  86. const sentIndex = html.indexOf("消息");
  87. const summaryIndex = html.indexOf("同步结论");
  88. const connectorIndex = html.indexOf("给");
  89. const targetIndex = html.indexOf("agent_alpha");
  90. expect(sentIndex).toBeGreaterThanOrEqual(0);
  91. expect(summaryIndex).toBeGreaterThan(sentIndex);
  92. expect(connectorIndex).toBeGreaterThan(summaryIndex);
  93. expect(targetIndex).toBeGreaterThan(connectorIndex);
  94. expect(html).not.toContain("已发送");
  95. expect(html).toContain("同步结论");
  96. expect(html).toContain("agent_alpha");
  97. });
  98. it("routes TaskStop to a semantic summary", () => {
  99. const html = renderToolCall({
  100. toolId: "stop-1",
  101. toolName: "TaskStop",
  102. kind: "TaskStop",
  103. title: "TaskStop",
  104. input: {
  105. task_id: "task_alpha",
  106. },
  107. output: JSON.stringify({
  108. message: "Successfully stopped task: task_alpha",
  109. task_id: "task_alpha",
  110. task_type: "local_agent",
  111. }),
  112. status: "completed",
  113. raw: {},
  114. });
  115. expect(html).toContain("停止任务");
  116. expect(html).not.toContain("已停止任务");
  117. expect(html).toContain("task_alpha");
  118. expect(html).not.toContain("local_agent");
  119. });
  120. it("routes TaskOutput to its task status summary", () => {
  121. const html = renderToolCall({
  122. toolId: "output-1",
  123. toolName: "TaskOutput",
  124. kind: "TaskOutput",
  125. title: "TaskOutput",
  126. input: {
  127. task_id: "task_alpha",
  128. block: false,
  129. timeout: 0,
  130. },
  131. output: "<retrieval_status>success</retrieval_status>",
  132. status: "completed",
  133. raw: {
  134. display: {
  135. kind: "task_output",
  136. retrievalStatus: "success",
  137. taskStatus: "completed",
  138. },
  139. },
  140. });
  141. expect(html).toContain("任务输出");
  142. expect(html).toContain("task_alpha");
  143. expect(html).not.toContain("&lt;retrieval_status&gt;");
  144. });
  145. it("routes RespondToCoordinator to its queued summary", () => {
  146. const html = renderToolCall({
  147. toolId: "respond-1",
  148. toolName: "RespondToCoordinator",
  149. kind: "RespondToCoordinator",
  150. title: "RespondToCoordinator",
  151. input: {
  152. summary: "已找到根因",
  153. message: "完整回复正文",
  154. },
  155. output: "Response response-1 was queued for the coordinator.",
  156. status: "completed",
  157. raw: {
  158. display: {
  159. kind: "respond_to_coordinator",
  160. status: "success",
  161. },
  162. },
  163. });
  164. expect(html).toContain("回复已排队");
  165. expect(html).toContain("已找到根因");
  166. expect(html).not.toContain("完整回复正文");
  167. expect(html).not.toContain("response-1");
  168. });
  169. });
  170. describe("message and task control tool call details", () => {
  171. it("renders only SendMessage target, summary, and message details", () => {
  172. const html = renderDedicatedToolCall(SendMessageToolCallBlock, {
  173. toolId: "send-details",
  174. toolName: "SendMessage",
  175. kind: "SendMessage",
  176. title: "SendMessage",
  177. input: {
  178. to: "agent_alpha",
  179. summary: "同步结论",
  180. message: "请复核结果并返回风险项。",
  181. },
  182. status: "completed",
  183. raw: {
  184. rawOutput: {
  185. status: "success",
  186. messageId: "msg_alpha",
  187. agentId: "agent_alpha",
  188. delivery: "queued",
  189. taskId: "task_alpha",
  190. outputFile: "/tmp/agent-alpha.txt",
  191. message: "Message queued for local agent.",
  192. },
  193. },
  194. });
  195. expect(html).toContain("目标");
  196. expect(html).toContain("agent_alpha");
  197. expect(html).toContain("摘要");
  198. expect(html).toContain("同步结论");
  199. expect(html).toContain("消息");
  200. expect(html).toContain("请复核结果并返回风险项。");
  201. expect(html).not.toContain("投递状态");
  202. expect(html).not.toContain("已排队");
  203. expect(html).not.toContain("消息 ID");
  204. expect(html).not.toContain("msg_alpha");
  205. expect(html).not.toContain("Agent ID");
  206. expect(html).not.toContain("任务 ID");
  207. expect(html).not.toContain("输出文件");
  208. expect(html).not.toContain("/tmp/agent-alpha.txt");
  209. expect(html).not.toContain("Message queued for local agent.");
  210. expect(html).not.toContain("&quot;messageId&quot;");
  211. expect(html).not.toContain("&quot;delivery&quot;");
  212. });
  213. it("does not make a streaming SendMessage expandable before details arrive", () => {
  214. const html = renderDedicatedToolCall(
  215. SendMessageToolCallBlock,
  216. {
  217. toolId: "send-streaming-empty",
  218. toolName: "SendMessage",
  219. kind: "SendMessage",
  220. title: "SendMessage",
  221. input: {},
  222. status: "pending",
  223. raw: {
  224. kind: "tool_input_start",
  225. },
  226. },
  227. {
  228. isRunning: true,
  229. },
  230. );
  231. expect(html).not.toContain('role="button"');
  232. expect(html).not.toContain("rounded-lg border border-border bg-panel");
  233. });
  234. it("does not render SendMessage plain-text model content as success details", () => {
  235. const resultText = "Message msg_beta was steered for local agent agent_beta.";
  236. const html = renderDedicatedToolCall(SendMessageToolCallBlock, {
  237. toolId: "send-text-result",
  238. toolName: "SendMessage",
  239. kind: "SendMessage",
  240. title: "SendMessage",
  241. input: {
  242. to: "agent_beta",
  243. summary: "补充上下文",
  244. message: "请继续检查当前改动。",
  245. },
  246. output: resultText,
  247. status: "completed",
  248. raw: {
  249. result: {
  250. content: resultText,
  251. },
  252. },
  253. });
  254. expect(html).not.toContain("结果");
  255. expect(html).not.toContain(resultText);
  256. expect(html).not.toContain("&quot;content&quot;");
  257. });
  258. it("renders a structured SendMessage business failure as failed", () => {
  259. const html = renderDedicatedToolCall(SendMessageToolCallBlock, {
  260. toolId: "send-business-failure",
  261. toolName: "SendMessage",
  262. kind: "SendMessage",
  263. title: "SendMessage",
  264. input: {
  265. to: "agent_missing",
  266. summary: "继续检查",
  267. message: "请继续检查当前改动。",
  268. },
  269. output: {
  270. status: "failed",
  271. messageId: "msg_failed",
  272. agentId: "agent_missing",
  273. error: "No active local_agent task found for target agent_missing.",
  274. message: "No active local_agent task found for target agent_missing.",
  275. },
  276. status: "completed",
  277. raw: {},
  278. });
  279. expect(html).toContain("消息");
  280. expect(html).toContain("执行失败");
  281. expect(html).not.toContain("No active local_agent task found");
  282. expect(html).not.toContain("已发送");
  283. });
  284. it.each([
  285. {
  286. name: "a live tool result",
  287. raw: {
  288. result: {
  289. display: {
  290. kind: "local_agent_message",
  291. status: "failed",
  292. error: "No active local_agent task found for target agent_missing.",
  293. message: "The target agent is no longer running.",
  294. },
  295. },
  296. },
  297. },
  298. {
  299. name: "persisted tool metadata",
  300. raw: {
  301. display: {
  302. kind: "local_agent_message",
  303. status: "failed",
  304. error: "No active local_agent task found for target agent_missing.",
  305. message: "The target agent is no longer running.",
  306. },
  307. },
  308. },
  309. {
  310. name: "a provider raw output",
  311. raw: {
  312. rawOutput: {
  313. display: {
  314. kind: "local_agent_message",
  315. status: "failed",
  316. error: "No active local_agent task found for target agent_missing.",
  317. message: "The target agent is no longer running.",
  318. },
  319. },
  320. },
  321. },
  322. ])("renders a projected SendMessage failure from $name", ({ raw }) => {
  323. const html = renderDedicatedToolCall(SendMessageToolCallBlock, {
  324. toolId: "send-projected-failure",
  325. toolName: "SendMessage",
  326. kind: "SendMessage",
  327. title: "SendMessage",
  328. input: {
  329. to: "agent_missing",
  330. summary: "继续检查",
  331. message: "请继续检查当前改动。",
  332. },
  333. output: "The target agent is no longer running.",
  334. status: "completed",
  335. raw,
  336. });
  337. expect(html).toContain("消息");
  338. expect(html).toContain("执行失败");
  339. expect(html).not.toContain("已发送");
  340. });
  341. it("renders TaskStop fields from JSON output without raw JSON", () => {
  342. const html = renderDedicatedToolCall(TaskStopToolCallBlock, {
  343. toolId: "stop-details",
  344. toolName: "TaskStop",
  345. kind: "TaskStop",
  346. title: "TaskStop",
  347. input: {
  348. shell_id: "legacy_shell_alpha",
  349. },
  350. output: JSON.stringify({
  351. message: "Successfully stopped task: task_alpha (pnpm test)",
  352. task_id: "task_alpha",
  353. task_type: "background_shell",
  354. command: "pnpm test",
  355. }),
  356. status: "completed",
  357. raw: {
  358. rawOutput: {
  359. message: "raw fallback should not render",
  360. },
  361. },
  362. });
  363. expect(html).not.toContain("任务 ID");
  364. expect(html).toContain("task_alpha");
  365. expect(html).toContain("任务类型");
  366. expect(html).toContain("background_shell");
  367. expect(html).toContain("命令");
  368. expect(html).toContain("pnpm test");
  369. expect(html).toMatch(/<dd class="[^"]*font-mono[^"]*">pnpm test<\/dd>/u);
  370. expect(html).toContain("结果");
  371. expect(html).toContain("Successfully stopped task: task_alpha");
  372. expect(html).not.toContain("Successfully stopped task: task_alpha (pnpm test)");
  373. expect(html.match(/pnpm test/gu)).toHaveLength(1);
  374. expect(html).not.toContain("raw fallback should not render");
  375. expect(html).not.toContain("&quot;task_id&quot;");
  376. expect(html).not.toContain("&quot;task_type&quot;");
  377. });
  378. it("renders a projected local agent command as a task description", () => {
  379. const html = renderDedicatedToolCall(TaskStopToolCallBlock, {
  380. toolId: "stop-local-agent-description",
  381. toolName: "TaskStop",
  382. kind: "TaskStop",
  383. title: "TaskStop",
  384. input: {
  385. task_id: "agent_alpha",
  386. },
  387. output: "Successfully stopped task: agent_alpha (检查赔率来源)",
  388. status: "completed",
  389. raw: {
  390. result: {
  391. display: {
  392. kind: "task_stop",
  393. taskId: "agent_alpha",
  394. taskType: "local_agent",
  395. command: "检查赔率来源",
  396. message: "Successfully stopped task: agent_alpha (检查赔率来源)",
  397. },
  398. },
  399. },
  400. });
  401. expect(html).toContain("任务描述");
  402. expect(html).not.toContain(">命令<");
  403. expect(html).toMatch(
  404. /<dt[^>]*>任务描述<\/dt><dd class="[^"]*leading-5[^"]*">检查赔率来源<\/dd>/u,
  405. );
  406. expect(html).not.toMatch(/font-mono[^>]*>检查赔率来源/u);
  407. });
  408. it("does not expose a legacy local agent prompt as a task description", () => {
  409. const legacyPrompt = "请搜索多个来源并输出完整的世界杯赔率研究报告";
  410. const html = renderDedicatedToolCall(TaskStopToolCallBlock, {
  411. toolId: "stop-legacy-local-agent",
  412. toolName: "TaskStop",
  413. kind: "TaskStop",
  414. title: "TaskStop",
  415. input: {
  416. task_id: "agent_legacy",
  417. },
  418. output: JSON.stringify({
  419. message: `Successfully stopped task: agent_legacy (${legacyPrompt})`,
  420. task_id: "agent_legacy",
  421. task_type: "local_agent",
  422. command: legacyPrompt,
  423. }),
  424. status: "completed",
  425. raw: {},
  426. });
  427. expect(html).toContain("Successfully stopped task: agent_legacy");
  428. expect(html).not.toContain(legacyPrompt);
  429. expect(html).not.toContain("任务描述");
  430. });
  431. it("keeps a custom TaskStop result unchanged", () => {
  432. const html = renderDedicatedToolCall(TaskStopToolCallBlock, {
  433. toolId: "stop-custom-result",
  434. toolName: "TaskStop",
  435. kind: "TaskStop",
  436. title: "TaskStop",
  437. input: {
  438. task_id: "task_custom",
  439. },
  440. output: JSON.stringify({
  441. message: "Task stopped after cleanup completed.",
  442. task_id: "task_custom",
  443. task_type: "background_shell",
  444. command: "pnpm test",
  445. }),
  446. status: "completed",
  447. raw: {},
  448. });
  449. expect(html).toContain("Task stopped after cleanup completed.");
  450. });
  451. it.each([
  452. {
  453. name: "a live tool result",
  454. raw: {
  455. result: {
  456. display: {
  457. kind: "task_stop",
  458. taskId: "task_display",
  459. taskType: "background_shell",
  460. command: "pnpm test",
  461. message: "Successfully stopped task: task_display (pnpm test)",
  462. },
  463. },
  464. },
  465. },
  466. {
  467. name: "persisted tool metadata",
  468. raw: {
  469. display: {
  470. kind: "task_stop",
  471. taskId: "task_display",
  472. taskType: "background_shell",
  473. command: "pnpm test",
  474. message: "Successfully stopped task: task_display (pnpm test)",
  475. },
  476. },
  477. },
  478. {
  479. name: "a provider raw output",
  480. raw: {
  481. rawOutput: {
  482. display: {
  483. kind: "task_stop",
  484. taskId: "task_display",
  485. taskType: "background_shell",
  486. command: "pnpm test",
  487. message: "Successfully stopped task: task_display (pnpm test)",
  488. },
  489. },
  490. },
  491. },
  492. ])("renders projected TaskStop details from $name", ({ raw }) => {
  493. const html = renderDedicatedToolCall(TaskStopToolCallBlock, {
  494. toolId: "stop-projected-details",
  495. toolName: "TaskStop",
  496. kind: "TaskStop",
  497. title: "TaskStop",
  498. input: {
  499. task_id: "task_input",
  500. },
  501. output: '{"message":"model content"}\n\n[Hook additional context]\n#1\nextra context',
  502. status: "completed",
  503. raw,
  504. });
  505. expect(html).toContain("task_display");
  506. expect(html).toContain("background_shell");
  507. expect(html).toContain("pnpm test");
  508. expect(html).toContain("Successfully stopped task: task_display");
  509. expect(html).not.toContain("Successfully stopped task: task_display (pnpm test)");
  510. expect(html.match(/pnpm test/gu)).toHaveLength(1);
  511. expect(html).not.toContain("extra context");
  512. });
  513. it("shows when projected TaskStop details were truncated", () => {
  514. const html = renderDedicatedToolCall(TaskStopToolCallBlock, {
  515. toolId: "stop-truncated-details",
  516. toolName: "TaskStop",
  517. kind: "TaskStop",
  518. title: "TaskStop",
  519. input: {
  520. task_id: "task_large",
  521. },
  522. status: "completed",
  523. raw: {
  524. result: {
  525. display: {
  526. kind: "task_stop",
  527. taskId: "task_large",
  528. taskType: "background_shell",
  529. command: "long command\n...[truncated]",
  530. message: "Successfully stopped task\n...[truncated]",
  531. truncated: true,
  532. },
  533. },
  534. },
  535. });
  536. expect(html).toContain("部分任务详情已截断");
  537. });
  538. it("parses the explicit hook envelope for a legacy TaskStop result", () => {
  539. const html = renderDedicatedToolCall(TaskStopToolCallBlock, {
  540. toolId: "stop-legacy-hook-details",
  541. toolName: "TaskStop",
  542. kind: "TaskStop",
  543. title: "TaskStop",
  544. input: {
  545. task_id: "task_legacy",
  546. },
  547. output:
  548. '{"message":"Successfully stopped task: task_legacy (pnpm test)","task_id":"task_legacy","task_type":"background_shell","command":"pnpm test"}\n\n[Hook additional context]\n#1\nextra context',
  549. status: "completed",
  550. raw: {},
  551. });
  552. expect(html).toContain("background_shell");
  553. expect(html).toContain("pnpm test");
  554. expect(html).toContain("Successfully stopped task: task_legacy");
  555. expect(html).not.toContain("Successfully stopped task: task_legacy (pnpm test)");
  556. expect(html.match(/pnpm test/gu)).toHaveLength(1);
  557. expect(html).not.toContain("extra context");
  558. });
  559. it("uses failure wording when TaskStop execution fails", () => {
  560. const html = renderDedicatedToolCall(
  561. TaskStopToolCallBlock,
  562. {
  563. toolId: "stop-failure",
  564. toolName: "TaskStop",
  565. kind: "TaskStop",
  566. title: "TaskStop",
  567. input: {
  568. task_id: "task_missing",
  569. },
  570. status: "failed",
  571. raw: {},
  572. },
  573. {
  574. statusLabel: "执行失败",
  575. errorText: "No task found with ID: task_missing",
  576. },
  577. );
  578. expect(html).toContain("停止任务");
  579. expect(html).toContain("执行失败");
  580. expect(html).not.toContain("已停止任务");
  581. });
  582. it("renders a denied TaskStop reason as the expanded result", () => {
  583. const html = renderDedicatedToolCall(
  584. TaskStopToolCallBlock,
  585. {
  586. toolId: "stop-denied-details",
  587. toolName: "TaskStop",
  588. kind: "TaskStop",
  589. title: "TaskStop",
  590. input: {
  591. task_id: "task_denied",
  592. },
  593. status: "denied",
  594. raw: {},
  595. },
  596. {
  597. errorText: "TaskStop is disabled by policy",
  598. },
  599. );
  600. expect(html).toContain("结果");
  601. expect(html).toContain("TaskStop is disabled by policy");
  602. });
  603. it("does not make a stopped TaskStop expandable when no details exist", () => {
  604. const html = renderDedicatedToolCall(TaskStopToolCallBlock, {
  605. toolId: "stop-empty-details",
  606. toolName: "TaskStop",
  607. kind: "TaskStop",
  608. title: "TaskStop",
  609. input: {
  610. task_id: "task_stopped",
  611. },
  612. status: "stopped",
  613. raw: {},
  614. });
  615. expect(html).not.toContain('role="button"');
  616. expect(html).not.toContain("rounded-lg border border-border bg-panel");
  617. });
  618. it("renders only bounded TaskOutput output and its truncation notice", () => {
  619. const output = "x".repeat(2_000);
  620. const html = renderDedicatedToolCall(TaskOutputToolCallBlock, {
  621. toolId: "output-details",
  622. toolName: "TaskOutput",
  623. kind: "TaskOutput",
  624. title: "TaskOutput",
  625. input: {
  626. task_id: "task_large",
  627. block: false,
  628. timeout: 0,
  629. },
  630. output: "<retrieval_status>success</retrieval_status>",
  631. status: "completed",
  632. raw: {
  633. result: {
  634. display: {
  635. kind: "task_output",
  636. retrievalStatus: "success",
  637. output,
  638. truncated: true,
  639. },
  640. description: "must stay hidden",
  641. error: "must stay hidden",
  642. outputFile: "/tmp/task.output",
  643. },
  644. },
  645. });
  646. expect(html).toContain("任务输出");
  647. expect(html).toContain("已获取");
  648. expect(html).toContain("task_large");
  649. expect(html).toContain(output);
  650. expect(html).toContain("后续内容已省略");
  651. expect(html).not.toContain("&lt;retrieval_status&gt;");
  652. expect(html).not.toContain("must stay hidden");
  653. expect(html).not.toContain("/tmp/task.output");
  654. });
  655. it("does not make TaskOutput expandable without projected output", () => {
  656. const html = renderDedicatedToolCall(TaskOutputToolCallBlock, {
  657. toolId: "output-empty",
  658. toolName: "TaskOutput",
  659. kind: "TaskOutput",
  660. title: "TaskOutput",
  661. input: {
  662. task_id: "task_empty",
  663. block: false,
  664. timeout: 0,
  665. },
  666. status: "completed",
  667. raw: {
  668. display: {
  669. kind: "task_output",
  670. retrievalStatus: "success",
  671. },
  672. },
  673. });
  674. expect(html).toContain("已获取");
  675. expect(html).not.toContain('role="button"');
  676. expect(html).not.toContain("rounded-lg border border-border bg-panel");
  677. });
  678. it.each([
  679. {
  680. name: "running retrieval",
  681. status: "completed",
  682. isRunning: false,
  683. display: { retrievalStatus: "not_ready", taskStatus: "running" },
  684. expected: "任务运行中",
  685. },
  686. {
  687. name: "timed out retrieval",
  688. status: "completed",
  689. isRunning: false,
  690. display: { retrievalStatus: "timeout", taskStatus: "running" },
  691. expected: "等待超时",
  692. },
  693. {
  694. name: "failed task",
  695. status: "completed",
  696. isRunning: false,
  697. display: { retrievalStatus: "success", taskStatus: "failed" },
  698. expected: "任务失败",
  699. },
  700. {
  701. name: "stopped task",
  702. status: "completed",
  703. isRunning: false,
  704. display: { retrievalStatus: "success", taskStatus: "killed" },
  705. expected: "任务已停止",
  706. },
  707. {
  708. name: "tool execution",
  709. status: "in_progress",
  710. isRunning: true,
  711. display: undefined,
  712. expected: "正在获取任务输出",
  713. },
  714. {
  715. name: "tool failure",
  716. status: "failed",
  717. isRunning: false,
  718. display: undefined,
  719. expected: "执行失败",
  720. },
  721. {
  722. name: "denied tool",
  723. status: "denied",
  724. isRunning: false,
  725. display: undefined,
  726. expected: "已拒绝",
  727. },
  728. {
  729. name: "stopped tool",
  730. status: "stopped",
  731. isRunning: false,
  732. display: undefined,
  733. expected: "已停止",
  734. },
  735. ])("renders TaskOutput $name status", ({ status, isRunning, display, expected }) => {
  736. const html = renderDedicatedToolCall(
  737. TaskOutputToolCallBlock,
  738. {
  739. toolId: `output-${status}`,
  740. toolName: "TaskOutput",
  741. kind: "TaskOutput",
  742. title: "TaskOutput",
  743. input: { task_id: "task_status", block: false, timeout: 0 },
  744. status,
  745. raw: display
  746. ? {
  747. display: {
  748. kind: "task_output",
  749. ...display,
  750. },
  751. }
  752. : {},
  753. },
  754. {
  755. isRunning,
  756. errorText: status === "failed" ? "TaskOutput execution failed" : undefined,
  757. },
  758. );
  759. expect(html).toContain(expected);
  760. });
  761. it("renders RespondToCoordinator summary and queue status without details", () => {
  762. const html = renderDedicatedToolCall(RespondToCoordinatorToolCallBlock, {
  763. toolId: "respond-details",
  764. toolName: "RespondToCoordinator",
  765. kind: "RespondToCoordinator",
  766. title: "RespondToCoordinator",
  767. input: {
  768. summary: "根因已经确认",
  769. message: "不应出现在卡片中的完整回复",
  770. },
  771. output: "Response response-1 was queued for the coordinator.",
  772. status: "completed",
  773. raw: {
  774. result: {
  775. display: {
  776. kind: "respond_to_coordinator",
  777. status: "success",
  778. },
  779. responseId: "response-1",
  780. error: "must stay hidden",
  781. },
  782. },
  783. });
  784. expect(html).toContain("回复已排队");
  785. expect(html).toContain("根因已经确认");
  786. expect(html).not.toContain('role="button"');
  787. expect(html).not.toContain("不应出现在卡片中的完整回复");
  788. expect(html).not.toContain("response-1");
  789. expect(html).not.toContain("must stay hidden");
  790. });
  791. it.each([
  792. {
  793. name: "running",
  794. status: "in_progress",
  795. isRunning: true,
  796. displayStatus: undefined,
  797. expected: "正在回复",
  798. },
  799. {
  800. name: "queued",
  801. status: "completed",
  802. isRunning: false,
  803. displayStatus: "success",
  804. expected: "回复已排队",
  805. },
  806. {
  807. name: "business failure",
  808. status: "completed",
  809. isRunning: false,
  810. displayStatus: "failed",
  811. expected: "执行失败",
  812. },
  813. {
  814. name: "denied",
  815. status: "denied",
  816. isRunning: false,
  817. displayStatus: undefined,
  818. expected: "已拒绝",
  819. },
  820. {
  821. name: "stopped",
  822. status: "stopped",
  823. isRunning: false,
  824. displayStatus: undefined,
  825. expected: "已停止",
  826. },
  827. ])(
  828. "renders RespondToCoordinator $name status",
  829. ({ status, isRunning, displayStatus, expected }) => {
  830. const html = renderDedicatedToolCall(
  831. RespondToCoordinatorToolCallBlock,
  832. {
  833. toolId: `respond-${status}`,
  834. toolName: "RespondToCoordinator",
  835. kind: "RespondToCoordinator",
  836. title: "RespondToCoordinator",
  837. input: { summary: "回复摘要", message: "完整回复" },
  838. status,
  839. raw: displayStatus
  840. ? {
  841. display: {
  842. kind: "respond_to_coordinator",
  843. status: displayStatus,
  844. },
  845. }
  846. : {},
  847. },
  848. {
  849. isRunning,
  850. },
  851. );
  852. expect(html).toContain(expected);
  853. if (displayStatus === "failed") {
  854. expect(html).toContain("执行失败");
  855. }
  856. },
  857. );
  858. });
  859. describe("message and task control terminal states", () => {
  860. it.each([
  861. { status: "stopped", expected: "已停止" },
  862. { status: "denied", expected: "已拒绝" },
  863. ])("does not present SendMessage $status as sent", ({ status, expected }) => {
  864. const html = renderToolCall({
  865. toolId: `send-${status}`,
  866. toolName: "SendMessage",
  867. kind: "SendMessage",
  868. title: "SendMessage",
  869. input: {
  870. to: "agent_alpha",
  871. summary: "同步结论",
  872. message: "请复核结果",
  873. },
  874. status,
  875. raw: {},
  876. });
  877. expect(html).toContain(expected);
  878. expect(html).not.toContain("已发送");
  879. });
  880. it.each([
  881. { status: "stopped", expected: "已停止" },
  882. { status: "denied", expected: "已拒绝" },
  883. ])("does not present TaskStop $status as stopped successfully", ({ status, expected }) => {
  884. const html = renderToolCall({
  885. toolId: `stop-${status}`,
  886. toolName: "TaskStop",
  887. kind: "TaskStop",
  888. title: "TaskStop",
  889. input: {
  890. task_id: "task_alpha",
  891. },
  892. status,
  893. raw: {},
  894. });
  895. expect(html).toContain(expected);
  896. expect(html).toContain("停止任务");
  897. });
  898. });