conversation-session-tool-groups.ts 3.3 KB

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