fix(webui): lazily render collapsed tool traces
Keep trace rows unmounted until their group is opened, then release them only after the closing disclosure transition finishes. Hidden historical traces should not allocate a DOM row per tool call merely to support animation. Add a 1,000-row regression test covering initial allocation, exit cleanup, rapid reopening, and subsequent expansion.
This commit is contained in:
1 parent
14f46d21fd
commit
11a03595f4
2 files changed
+46
-5
No files matched your search
@@ -936,12 +936,17 @@ function TraceGroup({ message }: TraceGroupProps) {
|
|||||||
const lines = message.traces ?? [message.content];
|
const lines = message.traces ?? [message.content];
|
||||||
const count = lines.length;
|
const count = lines.length;
|
||||||
const [open, setOpen] = useState(false);
|
const [open, setOpen] = useState(false);
|
||||||
|
const [hasOpened, setHasOpened] = useState(false);
|
||||||
|
const releaseContent = useCallback(() => setHasOpened(false), []);
|
||||||
const contentId = useId();
|
const contentId = useId();
|
||||||
return (
|
return (
|
||||||
<div className="w-full">
|
<div className="w-full">
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
onClick={() => setOpen((v) => !v)}
|
onClick={() => {
|
||||||
|
if (!open) setHasOpened(true);
|
||||||
|
setOpen((value) => !value);
|
||||||
|
}}
|
||||||
className={cn(
|
className={cn(
|
||||||
"group flex w-full items-center gap-2 rounded-md px-2 py-1.5",
|
"group flex w-full items-center gap-2 rounded-md px-2 py-1.5",
|
||||||
"text-xs text-muted-foreground transition-colors hover:bg-muted/45",
|
"text-xs text-muted-foreground transition-colors hover:bg-muted/45",
|
||||||
@@ -963,8 +968,8 @@ function TraceGroup({ message }: TraceGroupProps) {
|
|||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
</button>
|
</button>
|
||||||
<DisclosureContent id={contentId} open={open}>
|
<DisclosureContent id={contentId} open={open} onExitComplete={releaseContent}>
|
||||||
<ul
|
{hasOpened && <ul
|
||||||
className="mt-1 space-y-0.5 border-l border-muted-foreground/20 pl-3"
|
className="mt-1 space-y-0.5 border-l border-muted-foreground/20 pl-3"
|
||||||
>
|
>
|
||||||
{lines.map((line, i) => (
|
{lines.map((line, i) => (
|
||||||
@@ -975,7 +980,7 @@ function TraceGroup({ message }: TraceGroupProps) {
|
|||||||
{line}
|
{line}
|
||||||
</li>
|
</li>
|
||||||
))}
|
))}
|
||||||
</ul>
|
</ul>}
|
||||||
</DisclosureContent>
|
</DisclosureContent>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -852,7 +852,43 @@ describe("MessageBubble", () => {
|
|||||||
fireEvent.click(toggle);
|
fireEvent.click(toggle);
|
||||||
expect(content).toHaveAttribute("data-state", "closed");
|
expect(content).toHaveAttribute("data-state", "closed");
|
||||||
expect(content).toHaveAttribute("aria-hidden", "true");
|
expect(content).toHaveAttribute("aria-hidden", "true");
|
||||||
expect(screen.getByText('weather("get")')).toBeInTheDocument();
|
expect(screen.queryByText('weather("get")')).not.toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("lazily mounts large trace groups and releases them only after an uninterrupted exit", async () => {
|
||||||
|
const traces = Array.from({ length: 1000 }, (_, index) => `tool call ${index}`);
|
||||||
|
render(<MessageBubble message={{
|
||||||
|
id: "large-trace", role: "tool", kind: "trace",
|
||||||
|
content: traces[0], traces, createdAt: Date.now(),
|
||||||
|
}} />);
|
||||||
|
const toggle = screen.getByRole("button", { name: /used 1000 tools/i });
|
||||||
|
const content = document.getElementById(toggle.getAttribute("aria-controls")!)!;
|
||||||
|
// Hidden content must not allocate a DOM node for every historical trace.
|
||||||
|
expect(content.querySelectorAll("li")).toHaveLength(0);
|
||||||
|
fireEvent.click(toggle);
|
||||||
|
expect(content.querySelectorAll("li")).toHaveLength(1000);
|
||||||
|
|
||||||
|
let finishExit!: () => void;
|
||||||
|
const getAnimations = vi.fn(() => [{
|
||||||
|
finished: new Promise<void>((resolve) => { finishExit = resolve; }),
|
||||||
|
}]);
|
||||||
|
Object.defineProperty(content, "getAnimations", { value: getAnimations });
|
||||||
|
fireEvent.click(toggle);
|
||||||
|
expect(content.querySelectorAll("li")).toHaveLength(1000);
|
||||||
|
expect(content).toHaveAttribute("inert");
|
||||||
|
|
||||||
|
// Reopening cancels cleanup of the previous exit, even if it finishes later.
|
||||||
|
fireEvent.click(toggle);
|
||||||
|
await act(async () => { finishExit(); });
|
||||||
|
expect(content.querySelectorAll("li")).toHaveLength(1000);
|
||||||
|
expect(content).not.toHaveAttribute("inert");
|
||||||
|
|
||||||
|
fireEvent.click(toggle);
|
||||||
|
expect(getAnimations).toHaveBeenCalledTimes(2);
|
||||||
|
await act(async () => { finishExit(); });
|
||||||
|
expect(content.querySelectorAll("li")).toHaveLength(0);
|
||||||
|
fireEvent.click(toggle);
|
||||||
|
expect(content.querySelectorAll("li")).toHaveLength(1000);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("renders video media as an inline player", () => {
|
it("renders video media as an inline player", () => {
|
||||||
|
|||||||
Reference in new issue
Block a user