workflowEscalationToolCards.test.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. // eslint-disable-next-line import/first -- 同上;词条只用于双语存在性断言。
  30. import enUS from "../src/i18n/locales/en-US.js";
  31. // eslint-disable-next-line import/first -- 同上。
  32. import zhCN from "../src/i18n/locales/zh-CN.js";
  33. interface ToolCallOverrides {
  34. input?: unknown;
  35. output?: unknown;
  36. raw?: unknown;
  37. status?: string;
  38. toolName?: string;
  39. snapshotRefs?: readonly { field: string; previewBytes: number; fullBytes: number }[];
  40. }
  41. interface ContextOverrides {
  42. isRunning?: boolean;
  43. errorText?: string;
  44. forceOpen?: boolean;
  45. onLoadFullToolCallFields?: (toolId: string) => void;
  46. }
  47. function buildContext(
  48. toolCall: ToolCallOverrides,
  49. contextOverrides: ContextOverrides = {},
  50. defaultName = "escalate",
  51. ) {
  52. const name = toolCall.toolName ?? defaultName;
  53. return {
  54. toolCallNode: {
  55. toolCall: {
  56. toolId: "tool-escalation",
  57. toolName: name,
  58. kind: name,
  59. title: name,
  60. input: toolCall.input,
  61. output: toolCall.output,
  62. status: toolCall.status ?? "completed",
  63. raw: toolCall.raw ?? {},
  64. snapshotRefs: toolCall.snapshotRefs,
  65. },
  66. childToolCalls: [],
  67. // eslint-disable-next-line @typescript-eslint/no-explicit-any -- 测试上下文只填 renderer 读取的字段。
  68. } as any,
  69. workspacePath: "/workspace",
  70. displayModel: {
  71. inlinePreview: { type: "none" },
  72. planResult: null,
  73. viewerSource: null,
  74. viewerLabelId: "codeViewer.viewCode",
  75. showSummaryFileLink: false,
  76. showInput: false,
  77. showOutput: true,
  78. showKind: false,
  79. // eslint-disable-next-line @typescript-eslint/no-explicit-any -- 同上。
  80. } as any,
  81. viewerSource: null,
  82. rawFileSummaries: [],
  83. isRunning: contextOverrides.isRunning ?? false,
  84. statusLabel: "已执行",
  85. errorText: contextOverrides.errorText,
  86. childToolList: null,
  87. showIcon: true,
  88. canToggle: true,
  89. forceOpen: contextOverrides.forceOpen ?? true,
  90. onLoadFullToolCallFields: contextOverrides.onLoadFullToolCallFields,
  91. };
  92. }
  93. function renderEscalate(
  94. toolCall: ToolCallOverrides,
  95. contextOverrides: ContextOverrides = {},
  96. locale: "en-US" | "zh-CN" = "zh-CN",
  97. ): string {
  98. return renderToStaticMarkup(
  99. createElement(
  100. ZCodeIntlProvider,
  101. { initialLocale: locale },
  102. createElement(EscalateToolCallBlock, buildContext(toolCall, contextOverrides, "escalate")),
  103. ),
  104. );
  105. }
  106. function renderResolve(
  107. toolCall: ToolCallOverrides,
  108. contextOverrides: ContextOverrides = {},
  109. locale: "en-US" | "zh-CN" = "zh-CN",
  110. ): string {
  111. return renderToStaticMarkup(
  112. createElement(
  113. ZCodeIntlProvider,
  114. { initialLocale: locale },
  115. createElement(
  116. ResolveWorkflowQuestionToolCallBlock,
  117. buildContext(toolCall, contextOverrides, "ResolveWorkflowQuestion"),
  118. ),
  119. ),
  120. );
  121. }
  122. describe("升级问答工具卡路由", () => {
  123. it("escalate 按名分流到专用卡,不误伤 CreateWorkflow", () => {
  124. expect(resolveToolCallRenderer(buildContext({}, {}, "escalate") as never)).toBe(
  125. EscalateToolCallBlock,
  126. );
  127. expect(resolveToolCallRenderer(buildContext({ toolName: "CreateWorkflow" }) as never)).toBe(
  128. CreateWorkflowToolCallBlock,
  129. );
  130. });
  131. it("ResolveWorkflowQuestion 按名分流,snake_case wire 写法也命中", () => {
  132. for (const toolName of ["ResolveWorkflowQuestion", "resolve_workflow_question"]) {
  133. expect(resolveToolCallRenderer(buildContext({ toolName }) as never)).toBe(
  134. ResolveWorkflowQuestionToolCallBlock,
  135. );
  136. }
  137. });
  138. it("从 ToolCallBlock 入口整链走下来就是专用卡,不再是 fallback 的裸 JSON dump", () => {
  139. const escalateHtml = renderToStaticMarkup(
  140. createElement(
  141. ZCodeIntlProvider,
  142. { initialLocale: "zh-CN" },
  143. createElement(ToolCallBlock, {
  144. toolCallNode: {
  145. toolCall: {
  146. toolId: "escalate-e2e",
  147. toolName: "escalate",
  148. kind: "escalate",
  149. title: "escalate",
  150. input: { question: "门坏了吗?" },
  151. output: "跳过它。",
  152. status: "completed",
  153. raw: {},
  154. },
  155. childToolCalls: [],
  156. },
  157. workspacePath: "/workspace",
  158. }),
  159. ),
  160. );
  161. expect(escalateHtml).toContain("已询问主代理");
  162. expect(escalateHtml).not.toContain(""toolId"");
  163. const resolveHtml = renderToStaticMarkup(
  164. createElement(
  165. ZCodeIntlProvider,
  166. { initialLocale: "zh-CN" },
  167. createElement(ToolCallBlock, {
  168. toolCallNode: {
  169. toolCall: {
  170. toolId: "resolve-e2e",
  171. toolName: "ResolveWorkflowQuestion",
  172. kind: "ResolveWorkflowQuestion",
  173. title: "ResolveWorkflowQuestion",
  174. input: { question_id: "dwfq-run1-1", answer: "跳过它。" },
  175. output: "Answer delivered.",
  176. status: "completed",
  177. raw: {},
  178. },
  179. childToolCalls: [],
  180. },
  181. workspacePath: "/workspace",
  182. }),
  183. ),
  184. );
  185. expect(resolveHtml).toContain("已回答问题");
  186. expect(resolveHtml).not.toContain(""toolId"");
  187. });
  188. });
  189. describe("EscalateToolCallBlock 相位文案", () => {
  190. it.each([
  191. { name: "in_progress", status: "in_progress", isRunning: true, expected: "正在询问主代理" },
  192. { name: "pending", status: "pending", isRunning: true, expected: "正在询问主代理" },
  193. { name: "completed", status: "completed", isRunning: false, expected: "已询问主代理" },
  194. ])("$name 相写「$expected」", ({ status, isRunning, expected }) => {
  195. const html = renderEscalate({ input: { question: "门坏了吗?" }, status }, { isRunning });
  196. expect(html).toContain(expected);
  197. });
  198. it("en-US 下相位文案同样本地化", () => {
  199. const asking = renderEscalate(
  200. { input: { question: "Is the gate broken?" }, status: "in_progress" },
  201. { isRunning: true },
  202. "en-US",
  203. );
  204. expect(asking).toContain("Asking the main agent");
  205. const asked = renderEscalate(
  206. { input: { question: "Is the gate broken?" }, output: "Yes, skip it." },
  207. {},
  208. "en-US",
  209. );
  210. expect(asked).toContain("Asked the main agent");
  211. expect(asked).not.toContain("正在询问主代理");
  212. });
  213. });
  214. describe("EscalateToolCallBlock body 分支", () => {
  215. it("问题 + 背景 + 答案三段齐备", () => {
  216. const html = renderEscalate({
  217. input: { question: "门坏了吗?", context: "阈值 96,实际封顶 95。" },
  218. output: "对,跳过它,用旁路。",
  219. });
  220. expect(html).toContain("门坏了吗?");
  221. expect(html).toContain("阈值 96,实际封顶 95。");
  222. expect(html).toContain("对,跳过它,用旁路。");
  223. // 三个小标题都在场。
  224. expect(html).toContain("问题");
  225. expect(html).toContain("背景");
  226. expect(html).toContain("答复");
  227. });
  228. it("停驻中(running)只有问题、没有答案区,卡不显失败", () => {
  229. const html = renderEscalate(
  230. { input: { question: "门坏了吗?" }, status: "in_progress" },
  231. { isRunning: true },
  232. );
  233. expect(html).toContain("门坏了吗?");
  234. expect(html).toContain("正在询问主代理");
  235. // 答案小标题不该出现;停驻不是失败。
  236. expect(html).not.toContain("执行失败");
  237. expect(html).not.toContain("text-destructive");
  238. });
  239. it("预算已尽的驳回是普通结果——照常渲染成答案,绝不当失败", () => {
  240. // handler 的 formatModelContent 只返回 message;预算已尽的文案走的是普通 output(status
  241. // 仍是 completed),卡片绝不因文本内容把它样式成 error。
  242. const refusal =
  243. "You have used all 3 escalations for this ask. Proceed on your own best judgement.";
  244. const html = renderEscalate({ input: { question: "还能再问吗?" }, output: refusal });
  245. expect(html).toContain(refusal);
  246. expect(html).not.toContain("执行失败");
  247. expect(html).not.toContain("border-destructive");
  248. });
  249. it("首帧 input 还是空对象时不提供展开入口,也不渲染空面板", () => {
  250. const html = renderEscalate(
  251. { input: {}, status: "pending" },
  252. { isRunning: true, forceOpen: true },
  253. );
  254. expect(html).toContain("正在询问主代理");
  255. expect(html).not.toContain('role="button"');
  256. });
  257. });
  258. describe("ResolveWorkflowQuestionToolCallBlock 相位文案", () => {
  259. it.each([
  260. { name: "in_progress", status: "in_progress", isRunning: true, expected: "正在回答问题" },
  261. { name: "completed", status: "completed", isRunning: false, expected: "已回答问题" },
  262. ])("$name 相写「$expected」", ({ status, isRunning, expected }) => {
  263. const html = renderResolve(
  264. { input: { question_id: "dwfq-run1-1", answer: "跳过它。" }, status },
  265. { isRunning },
  266. );
  267. expect(html).toContain(expected);
  268. });
  269. it("en-US 下相位文案同样本地化", () => {
  270. const html = renderResolve(
  271. { input: { question_id: "dwfq-run1-1", answer: "Skip it." }, output: "Answer delivered." },
  272. {},
  273. "en-US",
  274. );
  275. expect(html).toContain("Answered question");
  276. expect(html).not.toContain("正在回答问题");
  277. });
  278. });
  279. describe("ResolveWorkflowQuestionToolCallBlock body 分支", () => {
  280. it("qid mono 行 + 完整答案 + 成功确认三段齐备", () => {
  281. const html = renderResolve({
  282. input: { question_id: "dwfq-run1-1", answer: "跳过那道门,用旁路。" },
  283. output: "Answer delivered for question dwfq-run1-1. The run keeps going.",
  284. });
  285. expect(html).toContain("dwfq-run1-1");
  286. // qid 是标识键 → mono。
  287. expect(html).toMatch(/font-mono[^>]*>dwfq-run1-1/u);
  288. expect(html).toContain("跳过那道门,用旁路。");
  289. expect(html).toContain("The run keeps going.");
  290. expect(html).toContain("问题 ID");
  291. expect(html).toContain("答复");
  292. expect(html).toContain("结果");
  293. });
  294. it("结构化拒绝走失败样式(status failed)", () => {
  295. const refusal =
  296. "workflow_question_already_resolved: this question was already answered.";
  297. const html = renderResolve(
  298. { input: { question_id: "dwfq-run1-1", answer: "跳过它。" }, status: "failed" },
  299. { errorText: refusal },
  300. );
  301. expect(html).toContain("执行失败");
  302. expect(html).toContain(refusal);
  303. expect(html).toContain("border-destructive");
  304. });
  305. it("成功确认不带任何失败样式", () => {
  306. const html = renderResolve({
  307. input: { question_id: "dwfq-run1-1", answer: "跳过它。" },
  308. output: "Answer delivered for question dwfq-run1-1.",
  309. });
  310. expect(html).not.toContain("执行失败");
  311. expect(html).not.toContain("border-destructive");
  312. });
  313. });
  314. describe("升级问答工具卡快照字段提示", () => {
  315. it("escalate 卡后仍挂着快照字段提示", () => {
  316. const html = renderEscalate(
  317. {
  318. input: { question: "门坏了吗?" },
  319. snapshotRefs: [{ field: "input", previewBytes: 100, fullBytes: 5_000 }],
  320. },
  321. { onLoadFullToolCallFields: () => {} },
  322. );
  323. expect(html).toContain("加载完整工具数据");
  324. });
  325. });
  326. describe("升级问答工具卡词条双语齐备", () => {
  327. it("escalate 与 resolveQuestion 两组 key 在两种语言里都在场,且不是英文占位", () => {
  328. // 缺 key 会静默渲染裸 key(IntlProvider 的 messages[id] ?? id)。
  329. for (const key of [
  330. "chat.toolCall.workflow.escalate.asking",
  331. "chat.toolCall.workflow.escalate.asked",
  332. "chat.toolCall.workflow.escalate.question",
  333. "chat.toolCall.workflow.escalate.context",
  334. "chat.toolCall.workflow.escalate.answer",
  335. "chat.toolCall.workflow.escalate.fallbackName",
  336. "chat.toolCall.workflow.resolveQuestion.answering",
  337. "chat.toolCall.workflow.resolveQuestion.answered",
  338. "chat.toolCall.workflow.resolveQuestion.questionId",
  339. "chat.toolCall.workflow.resolveQuestion.answer",
  340. "chat.toolCall.workflow.resolveQuestion.outcome",
  341. "chat.toolCall.workflow.resolveQuestion.fallbackName",
  342. ]) {
  343. expect(enUS[key]).toBeTruthy();
  344. expect(zhCN[key]).toBeTruthy();
  345. expect(zhCN[key]).not.toBe(enUS[key]);
  346. }
  347. });
  348. it("事件日志两行措辞已与专用卡对齐(旧 Escalated / 升级提问 / 问题已答 不再出现)", () => {
  349. expect(enUS["chat.toolCall.workflow.run.event.escalationRaised"]).toBe("Asked main agent");
  350. expect(enUS["chat.toolCall.workflow.run.event.escalationResolved"]).toBe("Answered");
  351. expect(zhCN["chat.toolCall.workflow.run.event.escalationRaised"]).toBe("询问主代理");
  352. expect(zhCN["chat.toolCall.workflow.run.event.escalationResolved"]).toBe("已回答");
  353. });
  354. });