The Open Standard for Generative UI
Improve tool tray behaviour (#822)
ABa631afcf329a
diff --git a/packages/react-ui/src/components/OpenUIChat/GenUIAssistantMessage.tsx b/packages/react-ui/src/components/OpenUIChat/GenUIAssistantMessage.tsxindex 7f57b7d76..1bd95c6c3 100644--- a/packages/react-ui/src/components/OpenUIChat/GenUIAssistantMessage.tsx+++ b/packages/react-ui/src/components/OpenUIChat/GenUIAssistantMessage.tsx@@ -126,7 +126,12 @@ export const GenUIAssistantMessage = ({<AssistantMessageContainer>{activities.length > 0 && (// Raw request/response for ALL tool calls, collapsed by default.- <ToolCallTimeline activities={activities} isLast={isStreaming} forceDefault />+ <ToolCallTimeline+ activities={activities}+ isLast={isStreaming}+ forceDefault+ awaitingResponse={isStreaming && !message.content}+ />)}{matchedActivities.map((activity) => (// Matched renderers (artifact previews, web search) — always visible.diff --git a/packages/react-ui/src/components/ToolCall/ToolCallTimeline.tsx b/packages/react-ui/src/components/ToolCall/ToolCallTimeline.tsxindex dc58df620..405330f06 100644--- a/packages/react-ui/src/components/ToolCall/ToolCallTimeline.tsx+++ b/packages/react-ui/src/components/ToolCall/ToolCallTimeline.tsx@@ -1,4 +1,5 @@import { useThread, type ToolActivity } from "@openuidev/react-headless";+import clsx from "clsx";import { ChevronDown, ChevronUp } from "lucide-react";import { useEffect, useRef, useState } from "react";import { TimelineEntry } from "../_shared/tool-renderer/TimelineEntry";@@ -25,13 +26,12 @@ const isRunning = (a: ToolActivity) => a.status === "streaming" || a.status ===/*** The "Working… / Behind the scenes" timeline wrapper, driven by- * {@link ToolActivity}[] from `useToolActivities` instead of the branch's- * `ThinkItem[]`. Keeps the staggered reveal + toggle; "running" is read from the- * activity status (not an `isThinking` prop), and "current item ready" asks the- * status union instead of peeking at magic `_request`/`_response` keys.+ * {@link ToolActivity}[] from `useToolActivities`. Reveals the run's activities+ * one-by-one, holds the compact card open across the tool-result → first-token+ * gap (`awaitingResponse`), and lets a manual close stick for the rest of the+ * run. "Running" is read from each activity's status, not an `isThinking` prop.*- * (The compact reveal uses a CSS fade — framer-motion is intentionally not a- * dependency of this package.)+ * (Animations are CSS-only — framer-motion is intentionally not a dependency.)** @category Components*/@@ -40,6 +40,7 @@ export function ToolCallTimeline({isLast = false,detailedViewPanel,forceDefault = false,+ awaitingResponse = false,}: {activities: ToolActivity[];isLast?: boolean;@@ -47,46 +48,50 @@ export function ToolCallTimeline({/** Render every row as the raw default card (e.g. so matched tools' raw* request/response stay inspectable here while their rich preview renders elsewhere). */forceDefault?: boolean;+ /** Hold the compact "Working…" tray open across the tool-result → first-token+ * gap instead of collapsing the instant the last result lands. */+ awaitingResponse?: boolean;}) {- // The timeline is "thinking" while its own last activity is still running, it's- // the live message, AND the thread is actually running — so a closed-args call- // that never received a result stops showing "Working..." once the run ends.+ // "Thinking" while the last activity is still running (or its results are in+ // but the response hasn't started), on the live message, and the thread is+ // actually running — so a closed-args call with no result stops showing+ // "Working..." once the run ends.const isThreadRunning = useThread((s) => s.isRunning);const thinking =isThreadRunning &&isLast &&activities.length > 0 &&- isRunning(activities[activities.length - 1]!);+ (isRunning(activities[activities.length - 1]!) || awaitingResponse);const [expanded, setExpanded] = useState(false);- // Live message → reveal one-by-one from the first; historical (not live) →- // everything already revealed so it never animates "Working…" on mount.+ // A user close mid-run sticks until the next run.+ const [userCollapsed, setUserCollapsed] = useState(false);+ // Live message reveals from the first activity; historical reveals them all so+ // it never animates "Working…" on mount.const [revealedCount, setRevealedCount] = useState(() =>isLast ? 1 : Math.max(activities.length, 1),);- const prevThinking = useRef(thinking);+ // Reset on each run edge: a rising `thinking` restarts the reveal and clears+ // any prior expand/collapse; a falling `thinking` reveals everything.+ const prevThinking = useRef(thinking);useEffect(() => {if (!prevThinking.current && thinking) {setRevealedCount(1);setExpanded(false);- }- if (prevThinking.current && !thinking) {+ setUserCollapsed(false);+ } else if (prevThinking.current && !thinking) {setExpanded(false);setRevealedCount(activities.length);}prevThinking.current = thinking;}, [thinking, activities.length]);- // Advance only when the current activity has left "streaming" (args closed) —- // replaces the branch's `!!toolRequest || !!toolResponse` peek with the union.- const currentReady = (() => {- const a = activities[revealedCount - 1];- return a ? a.status !== "streaming" : true;- })();-+ // Advance the reveal once the current activity's args have closed (left+ // "streaming"); only the live message reveals incrementally.+ const revealingActivity = activities[revealedCount - 1];+ const currentReady = revealingActivity ? revealingActivity.status !== "streaming" : true;useEffect(() => {- // Only the live message reveals incrementally; historical messages show all.if (isLast && revealedCount < activities.length && currentReady) {const t = setTimeout(() => setRevealedCount((c) => c + 1), REVEAL_INTERVAL);return () => clearTimeout(t);@@ -97,7 +102,8 @@ export function ToolCallTimeline({if (activities.length === 0) return null;const revealing = revealedCount < activities.length;- const showCompact = (thinking || revealing) && !expanded;+ const showCompact = (thinking || revealing) && !expanded && !userCollapsed;+ const isOpen = expanded || showCompact;const current = activities[Math.min(revealedCount - 1, activities.length - 1)]!;// Persistent live announcement reflecting the current step's status — driven by@@ -125,10 +131,19 @@ export function ToolCallTimeline({<buttonclassName="openui-behind-the-scenes__toggle"type="button"- aria-expanded={expanded}- onClick={() => setExpanded((v) => !v)}+ aria-expanded={isOpen}+ onClick={() => {+ // Anything open → close it (sticky for the rest of the run via+ // userCollapsed); otherwise reopen the full list.+ if (isOpen) {+ setExpanded(false);+ setUserCollapsed(true);+ } else {+ setExpanded(true);+ }+ }}>- {expanded ? (+ {isOpen ? (<ChevronUp size={14} className="openui-behind-the-scenes__toggle-icon" />) : (<ChevronDown size={14} className="openui-behind-the-scenes__toggle-icon" />@@ -136,20 +151,29 @@ export function ToolCallTimeline({{toggleLabel}</button>- {showCompact && (- <div className="openui-behind-the-scenes__items">- {/* key changes per reveal → remounts → re-triggers the CSS fade-in */}- <div- key={revealedCount}- className="openui-behind-the-scenes__reveal-item"- style={{ width: "100%" }}- >- <TimelineEntry- activity={current}- isLast- detailedViewPanel={detailedViewPanel}- forceDefault={forceDefault}- />+ {isLast && !expanded && (+ <div+ className={clsx("openui-behind-the-scenes__compact", {+ "openui-behind-the-scenes__compact--closed": !showCompact,+ })}+ inert={!showCompact}+ >+ <div className="openui-behind-the-scenes__compact-inner">+ <div className="openui-behind-the-scenes__items">+ {/* key changes per reveal → remounts → re-triggers the CSS fade-in */}+ <div+ key={revealedCount}+ className="openui-behind-the-scenes__reveal-item"+ style={{ width: "100%" }}+ >+ <TimelineEntry+ activity={current}+ isLast+ detailedViewPanel={detailedViewPanel}+ forceDefault={forceDefault}+ />+ </div>+ </div></div></div>)}diff --git a/packages/react-ui/src/components/ToolCall/toolCall.scss b/packages/react-ui/src/components/ToolCall/toolCall.scssindex e9fe85047..1d730d324 100644--- a/packages/react-ui/src/components/ToolCall/toolCall.scss+++ b/packages/react-ui/src/components/ToolCall/toolCall.scss@@ -49,6 +49,26 @@width: 100%;}+ // Graceful auto-close of the compact "Working…" tray+ &__compact {+ display: grid;+ grid-template-rows: 1fr;+ width: 100%;+ transition:+ grid-template-rows 0.2s ease,+ opacity 0.2s ease;++ &--closed {+ grid-template-rows: 0fr;+ opacity: 0;+ }+ }++ &__compact-inner {+ overflow: hidden;+ min-height: 0;+ }+// Compact staggered reveal (CSS fade-in; remounted per step via React key).&__reveal-item {animation: openui-behind-the-scenes-reveal 0.2s ease;@@ -481,6 +501,7 @@}.openui-behind-the-scenes__toggle-icon,+ .openui-behind-the-scenes__compact,.openui-tool-call__chevron,.openui-tool-call__source,.openui-tool-code-block,