conversation-session-tool-groups.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { TID_TOOL_SUMMARY_TRIGGER } from "@zcode/shared/test-ids";
  2. export type ConversationToolGroupName = "Explore" | "ExecuteGroup" | "ChangesGroup";
  3. export interface ConversationToolGroupSnapshot {
  4. childCount: number;
  5. expanded: boolean;
  6. status: string | null;
  7. summaryText: string;
  8. text: string;
  9. toolCallId: string;
  10. toolName: ConversationToolGroupName;
  11. }
  12. export function listConversationToolGroups(): Promise<ConversationToolGroupSnapshot[]> {
  13. return browser.execute((summaryPrefix) => {
  14. const groupNames = new Set(["Explore", "ExecuteGroup", "ChangesGroup"]);
  15. return Array.from(document.querySelectorAll<HTMLElement>("[data-tool-call-id][data-tool-name]"))
  16. .filter((block) => groupNames.has(block.dataset.toolName ?? ""))
  17. .map((block) => {
  18. const trigger = block.querySelector<HTMLElement>(`[data-testid^="${summaryPrefix}-"]`);
  19. const childCount = Array.from(
  20. block.querySelectorAll<HTMLElement>("[data-tool-call-id]"),
  21. ).filter((child) => child !== block).length;
  22. return {
  23. childCount,
  24. expanded: trigger?.getAttribute("aria-expanded") === "true",
  25. status: block.dataset.status ?? null,
  26. summaryText: (trigger?.innerText ?? "").replace(/\u00a0/g, " ").trim(),
  27. text: block.innerText.replace(/\u00a0/g, " ").trim(),
  28. toolCallId: block.dataset.toolCallId ?? "",
  29. toolName: block.dataset.toolName as ConversationToolGroupName,
  30. };
  31. });
  32. }, TID_TOOL_SUMMARY_TRIGGER);
  33. }
  34. export async function waitForConversationToolGroup(
  35. toolName: ConversationToolGroupName,
  36. predicate: (snapshot: ConversationToolGroupSnapshot) => boolean,
  37. failureMessage: string,
  38. timeout = 30000,
  39. ): Promise<ConversationToolGroupSnapshot> {
  40. let latest: ConversationToolGroupSnapshot | null = null;
  41. try {
  42. await browser.waitUntil(
  43. async () => {
  44. latest =
  45. (await listConversationToolGroups())
  46. .filter((group) => group.toolName === toolName)
  47. .at(-1) ?? null;
  48. return latest !== null && predicate(latest);
  49. },
  50. {
  51. timeout,
  52. timeoutMsg: failureMessage,
  53. },
  54. );
  55. } catch (cause) {
  56. // waitUntil 的 options 在轮询前构造;失败后再读取 latest 才能保留真实现场。
  57. throw new Error(`${failureMessage}; latest=${JSON.stringify(latest)}`, { cause });
  58. }
  59. if (!latest) throw new Error(`${failureMessage}; group missing`);
  60. return latest;
  61. }
  62. export async function toggleLatestConversationToolGroup(
  63. toolName: ConversationToolGroupName,
  64. targetExpanded: boolean,
  65. ): Promise<ConversationToolGroupSnapshot> {
  66. const clicked = await browser.execute(
  67. (summaryPrefix, expectedToolName, shouldExpand) => {
  68. const blocks = Array.from(
  69. document.querySelectorAll<HTMLElement>(`[data-tool-name="${expectedToolName}"]`),
  70. );
  71. const block = blocks.at(-1);
  72. const trigger = block?.querySelector<HTMLElement>(`[data-testid^="${summaryPrefix}-"]`);
  73. if (!trigger) return false;
  74. const expanded = trigger.getAttribute("aria-expanded") === "true";
  75. if (expanded !== shouldExpand) trigger.click();
  76. return true;
  77. },
  78. TID_TOOL_SUMMARY_TRIGGER,
  79. toolName,
  80. targetExpanded,
  81. );
  82. if (!clicked) throw new Error(`没有找到 ${toolName} summary trigger`);
  83. return waitForConversationToolGroup(
  84. toolName,
  85. (snapshot) => snapshot.expanded === targetExpanded,
  86. `${toolName} 没有切换到 expanded=${targetExpanded}`,
  87. );
  88. }