From 4a5c3b9982d36340457f3f549485dd9f15fc2c87 Mon Sep 17 00:00:00 2001 From: Miguel Palhas Date: Sat, 5 Sep 2026 08:59:20 +0100 Subject: [PATCH] fix(web): restore tool expansion directly --- web/src/structured.test.ts | 6 +++++- web/src/structured.ts | 3 +-- web/src/structuredRender.ts | 23 +++++++++++++++-------- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/web/src/structured.test.ts b/web/src/structured.test.ts index 870100a..336edb8 100644 --- a/web/src/structured.test.ts +++ b/web/src/structured.test.ts @@ -116,7 +116,7 @@ describe("structured connection: data flow", () => { conn.dispose(); }); - it("keeps an expanded running tool open when its stable row is patched", () => { + it("keeps an expanded running tool open without emitting a click when patched", () => { const ws = new FakeSocket(); const conn = mount({ sockets: [ws] }); ws.open(); @@ -138,6 +138,8 @@ describe("structured connection: data flow", () => { const originalHead = conn.el.querySelector(".st-tool-head")!; originalHead.click(); expect(originalHead.getAttribute("aria-expanded")).toBe("true"); + const clickListener = vi.fn(); + window.addEventListener("click", clickListener, { capture: true, once: true }); ws.emit({ type: "projection_update", @@ -152,6 +154,8 @@ describe("structured connection: data flow", () => { state: idleState, }); + expect(clickListener).not.toHaveBeenCalled(); + window.removeEventListener("click", clickListener, true); const patchedHead = conn.el.querySelector(".st-tool-head")!; expect(patchedHead).not.toBe(originalHead); expect(patchedHead.getAttribute("aria-expanded")).toBe("true"); diff --git a/web/src/structured.ts b/web/src/structured.ts index 531601d..56b81ce 100644 --- a/web/src/structured.ts +++ b/web/src/structured.ts @@ -162,12 +162,11 @@ export function createStructuredConnection( const oldToolHead = old?.querySelector(".st-tool-head") ?? null; const wasExpanded = oldToolHead?.getAttribute("aria-expanded") === "true"; const restoreFocus = document.activeElement === oldToolHead; - const el = buildTranscriptRow(row, approvalStateFor(row)); + const el = buildTranscriptRow(row, approvalStateFor(row), wasExpanded); rowEls.set(row.id, el); if (old) old.replaceWith(el); else rowsEl.appendChild(el); const newToolHead = el.querySelector(".st-tool-head"); - if (wasExpanded) newToolHead?.click(); if (restoreFocus) newToolHead?.focus(); } diff --git a/web/src/structuredRender.ts b/web/src/structuredRender.ts index 6faae2f..ca51efc 100644 --- a/web/src/structuredRender.ts +++ b/web/src/structuredRender.ts @@ -187,7 +187,7 @@ function statusMeta(status: string | undefined, hasDiff: boolean): { text: strin return null; } -function buildToolRow(row: TranscriptRow): HTMLElement { +function buildToolRow(row: TranscriptRow, expanded: boolean): HTMLElement { const tool = (row.data ?? {}) as AcpToolCallLike; const content = tool.content ?? []; const hasDiff = content.some((c) => c.type === "diff"); @@ -251,13 +251,16 @@ function buildToolRow(row: TranscriptRow): HTMLElement { }; card.appendChild(out); + const setExpanded = (nextExpanded: boolean) => { + if (nextExpanded) buildOut(); + if (out.children.length === 0) nextExpanded = false; + head.setAttribute("aria-expanded", String(nextExpanded)); + out.hidden = !nextExpanded; + }; head.addEventListener("click", () => { - const expanded = head.getAttribute("aria-expanded") === "true"; - if (!expanded) buildOut(); - if (out.children.length === 0) return; // nothing to show — stay collapsed - head.setAttribute("aria-expanded", String(!expanded)); - out.hidden = expanded; + setExpanded(head.getAttribute("aria-expanded") !== "true"); }); + setExpanded(expanded); return wrap; } @@ -361,7 +364,11 @@ function buildApprovalRow(row: TranscriptRow, approval: ApprovalRowState): HTMLE /** Build the DOM for one transcript row. `approval` is consulted only for an * `approval` row; pass it whenever the row might be that kind. */ -export function buildTranscriptRow(row: TranscriptRow, approval?: ApprovalRowState): HTMLElement { +export function buildTranscriptRow( + row: TranscriptRow, + approval?: ApprovalRowState, + toolExpanded = false, +): HTMLElement { let built: HTMLElement; switch (row.kind) { case "user": @@ -375,7 +382,7 @@ export function buildTranscriptRow(row: TranscriptRow, approval?: ApprovalRowSta break; case "tool_call": case "tool_diff": - built = buildToolRow(row); + built = buildToolRow(row, toolExpanded); break; case "approval": built = buildApprovalRow(row, approval ?? { pending: false, onDecide: () => Promise.resolve() });