conversation-session-tool-cross-product.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import { mkdir, rm, writeFile } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import { resolveE2ERuntimePath } from "./e2e-runtime-paths.js";
  4. import { getV4ConfigProjection, switchV4Mode } from "#e2e/helpers/v4-conversation";
  5. export const TOOL_CROSS_PRODUCT_ROOT = resolveE2ERuntimePath(
  6. "conversation-session-tool-cross-product",
  7. );
  8. export const TOOL_CROSS_PRODUCT_MARKER_PREFIX = "E2E_TOOL_CROSS_PRODUCT";
  9. export const TOOL_CROSS_PRODUCT_READ_FILE = join(TOOL_CROSS_PRODUCT_ROOT, "read-fixture.txt");
  10. export const TOOL_CROSS_PRODUCT_GLOB_FILE = join(TOOL_CROSS_PRODUCT_ROOT, "glob-fixture.e2e");
  11. export const TOOL_CROSS_PRODUCT_GREP_FILE = join(TOOL_CROSS_PRODUCT_ROOT, "grep-fixture.txt");
  12. export type ToolCrossProductScenario = "compact" | "fork" | "goal";
  13. export interface ToolCrossProductCase {
  14. markerName: string;
  15. toolName: string;
  16. historyAssertion?: "visibleToolBlock" | "providerRequest";
  17. providerHistoryMarker?: string;
  18. }
  19. export const TOOL_CROSS_PRODUCT_CASES: ToolCrossProductCase[] = [
  20. { markerName: "READ", toolName: "Read" },
  21. { markerName: "WRITE", toolName: "Write" },
  22. { markerName: "EDIT", toolName: "Edit" },
  23. { markerName: "BASH", toolName: "Bash" },
  24. { markerName: "GLOB", toolName: "Glob" },
  25. { markerName: "GREP", toolName: "Grep" },
  26. { markerName: "WEBFETCH", toolName: "WebFetch" },
  27. {
  28. markerName: "TODO_READ",
  29. toolName: "TodoRead",
  30. historyAssertion: "providerRequest",
  31. providerHistoryMarker: "toolu_e2e_tool_cross_product_todo_read",
  32. },
  33. { markerName: "TODO_WRITE", toolName: "TodoWrite" },
  34. { markerName: "GOAL_READ", toolName: "GoalRead" },
  35. {
  36. markerName: "ENTER_PLAN_MODE",
  37. toolName: "EnterPlanMode",
  38. historyAssertion: "providerRequest",
  39. providerHistoryMarker: "toolu_e2e_tool_cross_product_enter_plan_mode",
  40. },
  41. { markerName: "EXIT_PLAN_MODE", toolName: "ExitPlanMode" },
  42. { markerName: "ASK_USER_QUESTION", toolName: "AskUserQuestion" },
  43. { markerName: "SEND_MESSAGE", toolName: "SendMessage" },
  44. { markerName: "AGENT", toolName: "Agent" },
  45. { markerName: "SKILL", toolName: "Skill" },
  46. // 修复原因:当前 desktop E2E 的 agent runtime 没有注册 Workflow 工具。
  47. // replay fixture 若强制调用 Workflow 会稳定得到 "Tool not found",后续 compact/fork/goal
  48. // 断言验证的是失败工具卡而不是工具历史能力,应该从正式叉乘集合剪掉。
  49. ];
  50. export function markerForToolCase(toolCase: ToolCrossProductCase) {
  51. return `${TOOL_CROSS_PRODUCT_MARKER_PREFIX}_${toolCase.markerName}`;
  52. }
  53. export function buildToolCrossProductPrompt(
  54. toolCase: ToolCrossProductCase,
  55. scenario: ToolCrossProductScenario,
  56. runId: string,
  57. ) {
  58. return [
  59. `${markerForToolCase(toolCase)}_${scenario.toUpperCase()}_${runId}:`,
  60. `Trigger the ${toolCase.toolName} tool exactly once.`,
  61. 'Then reply with exactly "upstream-e2e-ok" and no other text.',
  62. ].join(" ");
  63. }
  64. export async function prepareToolCrossProductFixtures() {
  65. await cleanupToolCrossProductFixtures();
  66. await mkdir(TOOL_CROSS_PRODUCT_ROOT, { recursive: true });
  67. await writeFile(
  68. TOOL_CROSS_PRODUCT_READ_FILE,
  69. [
  70. "E2E_TOOL_RESULT_READ",
  71. "E2E_TOOL_RESULT_GREP",
  72. "This file is read by the conversation tool cross product e2e.",
  73. "",
  74. ].join("\n"),
  75. "utf-8",
  76. );
  77. await writeFile(TOOL_CROSS_PRODUCT_GLOB_FILE, "E2E_TOOL_RESULT_GLOB\n", "utf-8");
  78. await writeFile(
  79. TOOL_CROSS_PRODUCT_GREP_FILE,
  80. "E2E_TOOL_RESULT_GREP content search fixture\n",
  81. "utf-8",
  82. );
  83. }
  84. export async function cleanupToolCrossProductFixtures() {
  85. await rm(TOOL_CROSS_PRODUCT_ROOT, { recursive: true, force: true });
  86. }
  87. export async function ensureToolCrossProductFullAccessMode() {
  88. if ((await getV4ConfigProjection()).mode === "yolo") {
  89. return;
  90. }
  91. // Bug 根因:模式菜单已迁移为 DropdownMenu,旧 Select option 查询永远找不到 yolo。
  92. // 复用当前 V4 控件,并只以当前 composer 的模式投影确认,不读取其他工作区配置。
  93. await switchV4Mode("yolo");
  94. await browser.waitUntil(async () => (await getV4ConfigProjection()).mode === "yolo", {
  95. timeout: 30000,
  96. timeoutMsg: "工具叉乘 e2e 没有切换到 Full access/yolo mode",
  97. });
  98. }
  99. export async function respondToToolCrossProductBlockers() {
  100. const result = (await browser.execute(() => {
  101. const normalizeText = (value: string | null | undefined) =>
  102. (value ?? "").replace(/\u00a0/g, " ").trim();
  103. const clickFirstPermissionOption = () => {
  104. const listbox = Array.from(document.querySelectorAll<HTMLElement>('[role="listbox"]')).find(
  105. (element) => {
  106. const label = normalizeText(element.getAttribute("aria-label"));
  107. return label === "Permission required" || label === "需要权限";
  108. },
  109. );
  110. const option = listbox?.querySelector<HTMLButtonElement>('button[role="option"]');
  111. option?.click();
  112. return Boolean(option);
  113. };
  114. const clickFirstQuestionOption = () => {
  115. const listbox = Array.from(document.querySelectorAll<HTMLElement>('[role="listbox"]')).find(
  116. (element) =>
  117. normalizeText(element.getAttribute("aria-label")).includes(
  118. "E2E_TOOL_CROSS_PRODUCT_ASK_USER_QUESTION",
  119. ),
  120. );
  121. const option = listbox?.querySelector<HTMLButtonElement>('button[role="option"]');
  122. option?.click();
  123. return Boolean(option);
  124. };
  125. const clickPlanApprovalSubmit = () => {
  126. const normalizeText = (value: string | null | undefined) =>
  127. (value ?? "").replace(/\u00a0/g, " ").trim();
  128. const body = document.querySelector<HTMLElement>('[data-elicitation-dialog-body="true"]');
  129. const footer = document.querySelector<HTMLElement>('[data-elicitation-dialog-footer="true"]');
  130. const hasApproveOption = Array.from(
  131. body?.querySelectorAll<HTMLButtonElement>('button[role="option"]') ?? [],
  132. ).some((button) =>
  133. ["Approve", "批准"].some((label) => normalizeText(button.textContent).includes(label)),
  134. );
  135. const submit = Array.from(footer?.querySelectorAll<HTMLButtonElement>("button") ?? []).find(
  136. (button) => ["Submit", "提交"].includes(normalizeText(button.textContent)),
  137. );
  138. if (!hasApproveOption || !submit || submit.disabled) {
  139. return false;
  140. }
  141. submit.click();
  142. return true;
  143. };
  144. return {
  145. elicitationClicked: clickFirstQuestionOption(),
  146. planApprovalClicked: clickPlanApprovalSubmit(),
  147. permissionClicked: clickFirstPermissionOption(),
  148. };
  149. })) as {
  150. elicitationClicked: boolean;
  151. planApprovalClicked: boolean;
  152. permissionClicked: boolean;
  153. };
  154. if (result.elicitationClicked || result.planApprovalClicked || result.permissionClicked) {
  155. // Bug 根因:ExitPlanMode 在 V4 中走独立计划审批弹窗,不再是 legacy
  156. // PermissionDialog;只点击 permission option 会让整轮永久停在 interaction。
  157. await browser.pause(250);
  158. }
  159. return result;
  160. }