Open-source SEO, GEO, and marketing skills for AI agents.
fix(chat): preserve tool activity disclosure state
Yuting Zhong103ec5031df6
diff --git a/notfair/src/components/chat/messages.test.tsx b/notfair/src/components/chat/messages.test.tsxnew file mode 100644index 00000000..54656773--- /dev/null+++ b/notfair/src/components/chat/messages.test.tsx@@ -0,0 +1,44 @@+// @vitest-environment jsdom+import { fireEvent, render } from "@testing-library/react";+import { describe, expect, it } from "vitest";++import { ToolGroup } from "./messages";+import type { ToolEntry } from "./transcript-model";++const runningTool: ToolEntry = {+ toolCallId: "tool-1",+ name: "shell",+ label: "pwd",+ result: null,+ ok: true,+ done: false,+};++describe("ToolGroup", () => {+ it("stays collapsed by default and preserves a user's expansion when the tool finishes", () => {+ const { container, rerender } = render(+ <ToolGroup tools={[runningTool]} />,+ );+ const details = container.querySelector("details");+ const summary = container.querySelector("summary");++ expect(details).not.toHaveAttribute("open");+ fireEvent.click(summary!);+ expect(details).toHaveAttribute("open");++ rerender(+ <ToolGroup+ tools={[+ {+ ...runningTool,+ result: "done",+ done: true,+ },+ ]}+ />,+ );++ expect(container.querySelector("details")).toBe(details);+ expect(details).toHaveAttribute("open");+ });+});diff --git a/notfair/src/components/chat/messages.tsx b/notfair/src/components/chat/messages.tsxindex ca433219..c2daaa8b 100644--- a/notfair/src/components/chat/messages.tsx+++ b/notfair/src/components/chat/messages.tsx@@ -140,8 +140,6 @@ export function ToolGroup({return (<details- key={isLive ? "live" : "done"}- open={isLive}data-activity-kind={headMcp ? "mcp" : "tool"}className="group">diff --git a/notfair/src/components/chat/transcript-model.test.ts b/notfair/src/components/chat/transcript-model.test.tsindex 34c4212b..b1441edc 100644--- a/notfair/src/components/chat/transcript-model.test.ts+++ b/notfair/src/components/chat/transcript-model.test.ts@@ -57,6 +57,7 @@ describe("collapseEvents", () => {ReturnType<typeof collapseEvents>[number],{ kind: "tool_group" }>;+ expect(group.key).toBe("tg:t1");expect(group.tools).toHaveLength(2);expect(group.tools[0]).toMatchObject({ toolCallId: "t1", done: true });expect(group.tools[1]).toMatchObject({ toolCallId: "t2", done: false });diff --git a/notfair/src/components/chat/transcript-model.ts b/notfair/src/components/chat/transcript-model.tsindex 051d01dd..9a0af5f9 100644--- a/notfair/src/components/chat/transcript-model.ts+++ b/notfair/src/components/chat/transcript-model.ts@@ -21,6 +21,11 @@ export type RenderedItem =| { kind: "tool_group"; key: string; tools: ToolEntry[] }| { kind: "system_unknown"; key: string; raw_type: string };+/** Stable across the optimistic SSE row and its committed transcript copy. */+export function toolGroupKey(toolCallId: string): string {+ return `tg:${toolCallId}`;+}+/*** Content signature for cross-writer dedup. The id-based set in the* stream hook catches in-channel dups (e.g. polling racing itself);@@ -58,7 +63,7 @@ export function eventSignature(e: TranscriptEvent): string {*/export function collapseEvents(events: TranscriptEvent[]): RenderedItem[] {type Step =- | { tag: "tool"; key: string; entry: ToolEntry }+ | { tag: "tool"; entry: ToolEntry }| { tag: "msg"; item: RenderedItem };const steps: Step[] = [];const callIndex = new Map<string, number>();@@ -67,7 +72,6 @@ export function collapseEvents(events: TranscriptEvent[]): RenderedItem[] {callIndex.set(e.tool_call_id, steps.length);steps.push({tag: "tool",- key: e.id,entry: {toolCallId: e.tool_call_id,name: e.name,@@ -93,7 +97,6 @@ export function collapseEvents(events: TranscriptEvent[]): RenderedItem[] {}steps.push({tag: "tool",- key: e.id,entry: {toolCallId: e.tool_call_id,name: e.name,@@ -129,16 +132,17 @@ export function collapseEvents(events: TranscriptEvent[]): RenderedItem[] {}const out: RenderedItem[] = [];let buffer: ToolEntry[] = [];- let bufferKey: string | null = null;const flush = () => {if (buffer.length === 0) return;- out.push({ kind: "tool_group", key: `tg:${bufferKey}`, tools: buffer });+ out.push({+ kind: "tool_group",+ key: toolGroupKey(buffer[0]!.toolCallId),+ tools: buffer,+ });buffer = [];- bufferKey = null;};for (const step of steps) {if (step.tag === "tool") {- if (bufferKey === null) bufferKey = step.key;buffer.push(step.entry);} else {flush();diff --git a/notfair/src/components/live-transcript.tsx b/notfair/src/components/live-transcript.tsxindex a0854ce6..ed13c733 100644--- a/notfair/src/components/live-transcript.tsx+++ b/notfair/src/components/live-transcript.tsx@@ -14,7 +14,10 @@ import {TranscriptEmptyState,UserBubble,} from "@/components/chat/messages";-import { collapseEvents } from "@/components/chat/transcript-model";+import {+ collapseEvents,+ toolGroupKey,+} from "@/components/chat/transcript-model";import { useChatStream } from "@/components/chat/use-chat-stream";import type { McpCatalogEntryLite } from "@/components/chat/tool-intent";import {@@ -242,7 +245,7 @@ export function LiveTranscript({</li>)}{pendingTools.length > 0 && (- <li>+ <li key={toolGroupKey(pendingTools[0]!.toolCallId)}><ToolGroup tools={pendingTools} mcpCatalog={mcpCatalog} /></li>)}