import { TID_TOOL_SUMMARY_TRIGGER } from "@zcode/shared/test-ids"; export type ConversationToolGroupName = "Explore" | "ExecuteGroup" | "ChangesGroup"; export interface ConversationToolGroupSnapshot { childCount: number; expanded: boolean; status: string | null; summaryText: string; text: string; toolCallId: string; toolName: ConversationToolGroupName; } export function listConversationToolGroups(): Promise { return browser.execute((summaryPrefix) => { const groupNames = new Set(["Explore", "ExecuteGroup", "ChangesGroup"]); return Array.from(document.querySelectorAll("[data-tool-call-id][data-tool-name]")) .filter((block) => groupNames.has(block.dataset.toolName ?? "")) .map((block) => { const trigger = block.querySelector(`[data-testid^="${summaryPrefix}-"]`); const childCount = Array.from( block.querySelectorAll("[data-tool-call-id]"), ).filter((child) => child !== block).length; return { childCount, expanded: trigger?.getAttribute("aria-expanded") === "true", status: block.dataset.status ?? null, summaryText: (trigger?.innerText ?? "").replace(/\u00a0/g, " ").trim(), text: block.innerText.replace(/\u00a0/g, " ").trim(), toolCallId: block.dataset.toolCallId ?? "", toolName: block.dataset.toolName as ConversationToolGroupName, }; }); }, TID_TOOL_SUMMARY_TRIGGER); } export async function waitForConversationToolGroup( toolName: ConversationToolGroupName, predicate: (snapshot: ConversationToolGroupSnapshot) => boolean, failureMessage: string, timeout = 30000, ): Promise { let latest: ConversationToolGroupSnapshot | null = null; try { await browser.waitUntil( async () => { latest = (await listConversationToolGroups()) .filter((group) => group.toolName === toolName) .at(-1) ?? null; return latest !== null && predicate(latest); }, { timeout, timeoutMsg: failureMessage, }, ); } catch (cause) { // waitUntil 的 options 在轮询前构造;失败后再读取 latest 才能保留真实现场。 throw new Error(`${failureMessage}; latest=${JSON.stringify(latest)}`, { cause }); } if (!latest) throw new Error(`${failureMessage}; group missing`); return latest; } export async function toggleLatestConversationToolGroup( toolName: ConversationToolGroupName, targetExpanded: boolean, ): Promise { const clicked = await browser.execute( (summaryPrefix, expectedToolName, shouldExpand) => { const blocks = Array.from( document.querySelectorAll(`[data-tool-name="${expectedToolName}"]`), ); const block = blocks.at(-1); const trigger = block?.querySelector(`[data-testid^="${summaryPrefix}-"]`); if (!trigger) return false; const expanded = trigger.getAttribute("aria-expanded") === "true"; if (expanded !== shouldExpand) trigger.click(); return true; }, TID_TOOL_SUMMARY_TRIGGER, toolName, targetExpanded, ); if (!clicked) throw new Error(`没有找到 ${toolName} summary trigger`); return waitForConversationToolGroup( toolName, (snapshot) => snapshot.expanded === targetExpanded, `${toolName} 没有切换到 expanded=${targetExpanded}`, ); }