workflowEscalationToolCards.test.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. // escalate / ResolveWorkflowQuestion 专用工具卡(apps/zcode-cli/packages/dynamic-workflow/docs/execution-engine.md)。
  2. //
  3. // 文件名刻意是 `.test.ts` 而不是 `.test.tsx`:根 vitest 配置只收
  4. // `packages/*/test/**/*.test.ts`(vitest.config.ts),`.test.tsx` 一个用例都不会被采集。
  5. // 所以这里和 submitResultToolCallBlock.test.ts 一样用 createElement + renderToStaticMarkup,不写 JSX。
  6. //
  7. // 主会话与子代理侧板共用同一条 ToolCallBlocks 管线(侧板挂的是嵌套只读 SessionPane),所以
  8. // 「从 ToolCallBlock 入口整链走下来就是专用卡」这一条同时钉住两个面。
  9. import { createElement, type ReactNode } from "react";
  10. import { renderToStaticMarkup } from "react-dom/server";
  11. import { describe, expect, it, vi } from "vitest";
  12. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  13. // CodeBlock 依赖 shiki / CodeViewer 根 provider,静态渲染跑不动;照 submit-result 的先例替换为
  14. // 桩(本卡不用 CodeBlock,但整链入口可能间接触达,留桩最省心)。
  15. vi.mock("@/components/ai-elements/code-block.js", () => ({
  16. CodeBlock: ({ code, language }: { code: string; language?: string; children?: ReactNode }) =>
  17. createElement("pre", { "data-testid": "code-block", "data-language": language }, code),
  18. }));
  19. // eslint-disable-next-line import/first -- 必须在 code-block mock 之后再引入被测 renderer。
  20. import { EscalateToolCallBlock } from "../src/ToolCallBlocks/renderers/escalate.js";
  21. // eslint-disable-next-line import/first -- 同上。
  22. import { ResolveWorkflowQuestionToolCallBlock } from "../src/ToolCallBlocks/renderers/resolve-workflow-question.js";
  23. // eslint-disable-next-line import/first -- 同上;路由断言要拿到 workflow family 的兜底 renderer。
  24. import { CreateWorkflowToolCallBlock } from "../src/ToolCallBlocks/renderers/create-workflow.js";
  25. // eslint-disable-next-line import/first -- 同上。
  26. import { resolveToolCallRenderer } from "../src/ToolCallBlocks/resolveRenderer.js";
  27. // eslint-disable-next-line import/first -- 同上;整链路由断言要走真实入口组件。
  28. import { ToolCallBlock } from "@/ToolCallBlocks.js";
  29. interface ToolCallOverrides {
  30. input?: unknown;
  31. output?: unknown;
  32. raw?: unknown;
  33. status?: string;
  34. toolName?: string;
  35. snapshotRefs?: readonly { field: string; previewBytes: number; fullBytes: number }[];
  36. }
  37. interface ContextOverrides {
  38. isRunning?: boolean;
  39. errorText?: string;
  40. forceOpen?: boolean;
  41. onLoadFullToolCallFields?: (toolId: string) => void;
  42. }
  43. function buildContext(
  44. toolCall: ToolCallOverrides,
  45. contextOverrides: ContextOverrides = {},
  46. defaultName = "escalate",
  47. ) {
  48. const name = toolCall.toolName ?? defaultName;
  49. return {
  50. toolCallNode: {
  51. toolCall: {
  52. toolId: "tool-escalation",
  53. toolName: name,
  54. kind: name,
  55. title: name,
  56. input: toolCall.input,
  57. output: toolCall.output,
  58. status: toolCall.status ?? "completed",
  59. raw: toolCall.raw ?? {},
  60. snapshotRefs: toolCall.snapshotRefs,
  61. },
  62. childToolCalls: [],
  63. // eslint-disable-next-line @typescript-eslint/no-explicit-any -- 测试上下文只填 renderer 读取的字段。
  64. } as any,
  65. workspacePath: "/workspace",
  66. displayModel: {
  67. inlinePreview: { type: "none" },
  68. planResult: null,
  69. viewerSource: null,
  70. viewerLabelId: "codeViewer.viewCode",
  71. showSummaryFileLink: false,
  72. showInput: false,
  73. showOutput: true,
  74. showKind: false,
  75. // eslint-disable-next-line @typescript-eslint/no-explicit-any -- 同上。
  76. } as any,
  77. viewerSource: null,
  78. rawFileSummaries: [],
  79. isRunning: contextOverrides.isRunning ?? false,
  80. statusLabel: "已执行",
  81. errorText: contextOverrides.errorText,
  82. childToolList: null,
  83. showIcon: true,
  84. canToggle: true,
  85. forceOpen: contextOverrides.forceOpen ?? true,
  86. onLoadFullToolCallFields: contextOverrides.onLoadFullToolCallFields,
  87. };
  88. }
  89. function renderEscalate(
  90. toolCall: ToolCallOverrides,
  91. contextOverrides: ContextOverrides = {},
  92. locale: "en-US" | "zh-CN" = "zh-CN",
  93. ): string {
  94. return renderToStaticMarkup(
  95. createElement(
  96. ZCodeIntlProvider,
  97. { initialLocale: locale },
  98. createElement(EscalateToolCallBlock, buildContext(toolCall, contextOverrides, "escalate")),
  99. ),
  100. );
  101. }
  102. function renderResolve(
  103. toolCall: ToolCallOverrides,
  104. contextOverrides: ContextOverrides = {},
  105. locale: "en-US" | "zh-CN" = "zh-CN",
  106. ): string {
  107. return renderToStaticMarkup(
  108. createElement(
  109. ZCodeIntlProvider,
  110. { initialLocale: locale },
  111. createElement(
  112. ResolveWorkflowQuestionToolCallBlock,
  113. buildContext(toolCall, contextOverrides, "ResolveWorkflowQuestion"),
  114. ),
  115. ),
  116. );
  117. }
  118. describe("升级问答工具卡路由", () => {
  119. it("escalate 按名分流到专用卡,不误伤 CreateWorkflow", () => {
  120. expect(resolveToolCallRenderer(buildContext({}, {}, "escalate") as never)).toBe(
  121. EscalateToolCallBlock,
  122. );
  123. expect(resolveToolCallRenderer(buildContext({ toolName: "CreateWorkflow" }) as never)).toBe(
  124. CreateWorkflowToolCallBlock,
  125. );
  126. });
  127. it("ResolveWorkflowQuestion 按名分流,snake_case wire 写法也命中", () => {
  128. for (const toolName of ["ResolveWorkflowQuestion", "resolve_workflow_question"]) {
  129. expect(resolveToolCallRenderer(buildContext({ toolName }) as never)).toBe(
  130. ResolveWorkflowQuestionToolCallBlock,
  131. );
  132. }
  133. });
  134. it("从 ToolCallBlock 入口整链走下来就是专用卡,不再是 fallback 的裸 JSON dump", () => {
  135. const escalateHtml = renderToStaticMarkup(
  136. createElement(
  137. ZCodeIntlProvider,
  138. { initialLocale: "zh-CN" },
  139. createElement(ToolCallBlock, {
  140. toolCallNode: {
  141. toolCall: {
  142. toolId: "escalate-e2e",
  143. toolName: "escalate",
  144. kind: "escalate",
  145. title: "escalate",
  146. input: { question: "门坏了吗?" },
  147. output: "跳过它。",
  148. status: "completed",
  149. raw: {},
  150. },
  151. childToolCalls: [],
  152. },
  153. workspacePath: "/workspace",
  154. }),
  155. ),
  156. );
  157. expect(escalateHtml).toContain("已询问主代理");
  158. expect(escalateHtml).not.toContain(""toolId"");
  159. const resolveHtml = renderToStaticMarkup(
  160. createElement(
  161. ZCodeIntlProvider,
  162. { initialLocale: "zh-CN" },
  163. createElement(ToolCallBlock, {
  164. toolCallNode: {
  165. toolCall: {
  166. toolId: "resolve-e2e",
  167. toolName: "ResolveWorkflowQuestion",
  168. kind: "ResolveWorkflowQuestion",
  169. title: "ResolveWorkflowQuestion",
  170. input: { question_id: "dwfq-run1-1", answer: "跳过它。" },
  171. output: "Answer delivered.",
  172. status: "completed",
  173. raw: {},
  174. },
  175. childToolCalls: [],
  176. },
  177. workspacePath: "/workspace",
  178. }),
  179. ),
  180. );
  181. expect(resolveHtml).toContain("已回答问题");
  182. expect(resolveHtml).not.toContain(""toolId"");
  183. });
  184. });
  185. describe("EscalateToolCallBlock 相位文案", () => {
  186. it.each([
  187. { name: "in_progress", status: "in_progress", isRunning: true, expected: "正在询问主代理" },
  188. { name: "pending", status: "pending", isRunning: true, expected: "正在询问主代理" },
  189. { name: "completed", status: "completed", isRunning: false, expected: "已询问主代理" },
  190. ])("$name 相写「$expected」", ({ status, isRunning, expected }) => {
  191. const html = renderEscalate({ input: { question: "门坏了吗?" }, status }, { isRunning });
  192. expect(html).toContain(expected);
  193. });
  194. it("en-US 下相位文案同样本地化", () => {
  195. const asking = renderEscalate(
  196. { input: { question: "Is the gate broken?" }, status: "in_progress" },
  197. { isRunning: true },
  198. "en-US",
  199. );
  200. expect(asking).toContain("Asking the main agent");
  201. const asked = renderEscalate(
  202. { input: { question: "Is the gate broken?" }, output: "Yes, skip it." },
  203. {},
  204. "en-US",
  205. );
  206. expect(asked).toContain("Asked the main agent");
  207. expect(asked).not.toContain("正在询问主代理");
  208. });
  209. });
  210. describe("EscalateToolCallBlock body 分支", () => {
  211. it("问题 + 背景 + 答案三段齐备", () => {
  212. const html = renderEscalate({
  213. input: { question: "门坏了吗?", context: "阈值 96,实际封顶 95。" },
  214. output: "对,跳过它,用旁路。",
  215. });
  216. expect(html).toContain("门坏了吗?");
  217. expect(html).toContain("阈值 96,实际封顶 95。");
  218. expect(html).toContain("对,跳过它,用旁路。");
  219. // 三个小标题都在场。
  220. expect(html).toContain("问题");
  221. expect(html).toContain("背景");
  222. expect(html).toContain("答复");
  223. });
  224. it("停驻中(running)只有问题、没有答案区,卡不显失败", () => {
  225. const html = renderEscalate(
  226. { input: { question: "门坏了吗?" }, status: "in_progress" },
  227. { isRunning: true },
  228. );
  229. expect(html).toContain("门坏了吗?");
  230. expect(html).toContain("正在询问主代理");
  231. // 答案小标题不该出现;停驻不是失败。
  232. expect(html).not.toContain("执行失败");
  233. expect(html).not.toContain("text-destructive");
  234. });
  235. it("预算已尽的驳回是普通结果——照常渲染成答案,绝不当失败", () => {
  236. // handler 的 formatModelContent 只返回 message;预算已尽的文案走的是普通 output(status
  237. // 仍是 completed),卡片绝不因文本内容把它样式成 error。
  238. const refusal =
  239. "You have used all 3 escalations for this ask. Proceed on your own best judgement.";
  240. const html = renderEscalate({ input: { question: "还能再问吗?" }, output: refusal });
  241. expect(html).toContain(refusal);
  242. expect(html).not.toContain("执行失败");
  243. expect(html).not.toContain("border-destructive");
  244. });
  245. it("首帧 input 还是空对象时不提供展开入口,也不渲染空面板", () => {
  246. const html = renderEscalate(
  247. { input: {}, status: "pending" },
  248. { isRunning: true, forceOpen: true },
  249. );
  250. expect(html).toContain("正在询问主代理");
  251. expect(html).not.toContain('role="button"');
  252. });
  253. });
  254. describe("ResolveWorkflowQuestionToolCallBlock 相位文案", () => {
  255. it.each([
  256. { name: "in_progress", status: "in_progress", isRunning: true, expected: "正在回答问题" },
  257. { name: "completed", status: "completed", isRunning: false, expected: "已回答问题" },
  258. ])("$name 相写「$expected」", ({ status, isRunning, expected }) => {
  259. const html = renderResolve(
  260. { input: { question_id: "dwfq-run1-1", answer: "跳过它。" }, status },
  261. { isRunning },
  262. );
  263. expect(html).toContain(expected);
  264. });
  265. it("en-US 下相位文案同样本地化", () => {
  266. const html = renderResolve(
  267. { input: { question_id: "dwfq-run1-1", answer: "Skip it." }, output: "Answer delivered." },
  268. {},
  269. "en-US",
  270. );
  271. expect(html).toContain("Answered question");
  272. expect(html).not.toContain("正在回答问题");
  273. });
  274. });
  275. describe("ResolveWorkflowQuestionToolCallBlock body 分支", () => {
  276. it("qid mono 行 + 完整答案 + 成功确认三段齐备", () => {
  277. const html = renderResolve({
  278. input: { question_id: "dwfq-run1-1", answer: "跳过那道门,用旁路。" },
  279. output: "Answer delivered for question dwfq-run1-1. The run keeps going.",
  280. });
  281. expect(html).toContain("dwfq-run1-1");
  282. // qid 是标识键 → mono。
  283. expect(html).toMatch(/font-mono[^>]*>dwfq-run1-1/u);
  284. expect(html).toContain("跳过那道门,用旁路。");
  285. expect(html).toContain("The run keeps going.");
  286. expect(html).toContain("问题 ID");
  287. expect(html).toContain("答复");
  288. expect(html).toContain("结果");
  289. });
  290. it("结构化拒绝走失败样式(status failed)", () => {
  291. const refusal = "workflow_question_already_resolved: this question was already answered.";
  292. const html = renderResolve(
  293. { input: { question_id: "dwfq-run1-1", answer: "跳过它。" }, status: "failed" },
  294. { errorText: refusal },
  295. );
  296. expect(html).toContain("执行失败");
  297. expect(html).toContain(refusal);
  298. expect(html).toContain("border-destructive");
  299. });
  300. it("成功确认不带任何失败样式", () => {
  301. const html = renderResolve({
  302. input: { question_id: "dwfq-run1-1", answer: "跳过它。" },
  303. output: "Answer delivered for question dwfq-run1-1.",
  304. });
  305. expect(html).not.toContain("执行失败");
  306. expect(html).not.toContain("border-destructive");
  307. });
  308. });
  309. describe("升级问答工具卡快照字段提示", () => {
  310. it("escalate 卡后仍挂着快照字段提示", () => {
  311. const html = renderEscalate(
  312. {
  313. input: { question: "门坏了吗?" },
  314. snapshotRefs: [{ field: "input", previewBytes: 100, fullBytes: 5_000 }],
  315. },
  316. { onLoadFullToolCallFields: () => {} },
  317. );
  318. expect(html).toContain("加载完整工具数据");
  319. });
  320. });