Agent Thinking Trace
Collapsible AI reasoning accordion with live elapsed telemetry, text shimmer, and sequential tool-call steps.
Inspecting component surface tokens
Comparing Light tonal depth with Dark top-edge specular rays.
read_fileglobals.css
1. Install Dependencies
$npm install framer-motion lucide-react
2. Copy Component Code
components/ui/AgentThinkingTrace.tsx
1"use client";2
3import React, { useEffect, useId, useState } from "react";4import { AnimatePresence, motion, useReducedMotion } from "framer-motion";5import {6 Check,7 ChevronRight,8 FileText,9 Loader2,10 Sparkles,11 Terminal,12} from "lucide-react";13
14const APPLE_EASE = [0.22, 1, 0.36, 1] as const;15const CONTAINER_LAYOUT_SPRING = {16 type: "spring",17 stiffness: 460,18 damping: 36,19 mass: 0.85,20} as const;21
22export interface ThinkingStep {23 id: string;24 title: string;25 detail: string;26 tool?: string;27 file?: string;28 latency: string;29}30
31export const DEFAULT_THINKING_STEPS: ThinkingStep[] = [32 {33 id: "step-tokens",34 title: "Inspecting component surface tokens",35 detail: "Comparing Light tonal depth with Dark top-edge specular rays.",36 tool: "read_file",37 file: "globals.css",38 latency: "120ms",39 },40 {41 id: "step-grid",42 title: "Auditing 4px concentric grid",43 detail: "Verifying parallel inner control radii across action bars.",44 tool: "grep_search",45 file: "Navbar.tsx",46 latency: "340ms",47 },48 {49 id: "step-motion",50 title: "Synthesizing spring transitions",51 detail: "Aligning blur-dissolve timing with APPLE_EASE deceleration.",52 tool: "apply_patch",53 file: "ComponentDetailView.tsx",54 latency: "210ms",55 },56];57
58export interface AgentThinkingTraceProps {59 /** Sequential reasoning steps shown inside the trace timeline. */60 steps?: ThinkingStep[];61 /** Controlled open/collapsed state. */62 open?: boolean;63 /** Initial open/collapsed state when uncontrolled. */64 defaultOpen?: boolean;65 /** Callback fired when accordion header is toggled. */66 onOpenChange?: (open: boolean) => void;67 /** Plays the live cascade when true; shows completed trace immediately when false. */68 autoPlay?: boolean;69 /** Extra classes on outer shell. */70 className?: string;71}72
73/** Collapsible AI reasoning accordion with elapsed timer shimmer and sequential tool-call steps. */74export function AgentThinkingTrace({75 steps = DEFAULT_THINKING_STEPS,76 open,77 defaultOpen = true,78 onOpenChange,79 autoPlay = true,80 className = "",81}: AgentThinkingTraceProps) {82 const prefersReducedMotion = useReducedMotion();83 const tracePanelId = useId();84 const [internalOpen, setInternalOpen] = useState(defaultOpen);85
86 const shouldAnimateStream = autoPlay && !prefersReducedMotion;87
88 const [visibleCount, setVisibleCount] = useState(() =>89 shouldAnimateStream ? Math.min(1, steps.length) : steps.length90 );91 const [completedCount, setCompletedCount] = useState(() =>92 shouldAnimateStream ? 0 : steps.length93 );94 const [elapsedMs, setElapsedMs] = useState(() =>95 shouldAnimateStream ? 0 : 240096 );97
98 const isOpen = open !== undefined ? open : internalOpen;99 const isThinking = completedCount < steps.length;100
101 useEffect(() => {102 if (!shouldAnimateStream || steps.length === 0) return;103
104 const timers: number[] = [];105 const totalDuration = 2400;106 const stepInterval = Math.floor(totalDuration / steps.length);107
108 steps.forEach((_, idx) => {109 if (idx > 0) {110 const revealAt = idx * stepInterval - 50;111 timers.push(112 window.setTimeout(() => {113 setVisibleCount((prev) => Math.max(prev, idx + 1));114 }, revealAt)115 );116 }117
118 const completeAt =119 idx === steps.length - 1 ? totalDuration : (idx + 1) * stepInterval - 50;120 timers.push(121 window.setTimeout(() => {122 setCompletedCount((prev) => Math.max(prev, idx + 1));123 if (idx === steps.length - 1) {124 setElapsedMs(totalDuration);125 }126 }, completeAt)127 );128 });129
130 const startTime = performance.now();131 const tickId = window.setInterval(() => {132 const elapsed = Math.min(totalDuration, Math.round(performance.now() - startTime));133 setElapsedMs(elapsed);134 if (elapsed >= totalDuration) {135 window.clearInterval(tickId);136 }137 }, 100);138
139 return () => {140 window.clearInterval(tickId);141 timers.forEach((id) => window.clearTimeout(id));142 };143 }, [shouldAnimateStream, steps]);144
145 function toggleOpen() {146 const next = !isOpen;147 if (open === undefined) {148 setInternalOpen(next);149 }150 onOpenChange?.(next);151 }152
153 const formattedSeconds = (elapsedMs / 1000).toFixed(1);154 const visibleSteps = steps.slice(0, visibleCount);155 const spineProgress =156 steps.length > 0 ? Math.min(1, visibleCount / steps.length) : 1;157
158 return (159 <motion.div160 layout={!prefersReducedMotion}161 transition={162 prefersReducedMotion ? { duration: 0 } : { layout: CONTAINER_LAYOUT_SPRING }163 }164 className={`w-full max-w-[460px] min-w-0 rounded-[20px] border-0 bg-white p-2 shadow-[0_4px_20px_rgba(0,0,0,0.06)] dark:border-0 dark:bg-[#111113] dark:shadow-[inset_0_1px_0_rgba(255,255,255,0.09),0_12px_32px_rgba(0,0,0,0.65)] ${className}`}165 >166 {/* Accordion Header Trigger (12px inner radius + 8px padding = 20px concentric outer) */}167 <button168 type="button"169 onClick={toggleOpen}170 aria-expanded={isOpen}171 aria-controls={tracePanelId}172 className="flex min-h-10 w-full items-center justify-between gap-3 rounded-[12px] px-3 py-1 text-left transition-colors duration-150 hover:bg-[#F5F5F7] active:scale-[0.99] dark:hover:bg-[#18181C]"173 >174 <div className="flex min-w-0 items-center gap-2">175 <motion.span176 animate={{ rotate: isOpen ? 90 : 0 }}177 transition={178 prefersReducedMotion179 ? { duration: 0 }180 : { duration: 0.18, ease: APPLE_EASE }181 }182 className="inline-flex shrink-0 items-center justify-center text-black/45 dark:text-white/45"183 >184 <ChevronRight185 className="h-3.5 w-3.5"186 strokeWidth={1.75}187 aria-hidden="true"188 />189 </motion.span>190
191 <AnimatePresence mode="popLayout" initial={false}>192 {isThinking ? (193 <motion.div194 key="header-thinking"195 initial={prefersReducedMotion ? false : { opacity: 0, y: 4 }}196 animate={{ opacity: 1, y: 0 }}197 exit={prefersReducedMotion ? undefined : { opacity: 0, y: -4 }}198 transition={199 prefersReducedMotion200 ? { duration: 0 }201 : { duration: 0.18, ease: APPLE_EASE }202 }203 className="flex items-center gap-2"204 >205 <Sparkles206 className="h-3.5 w-3.5 shrink-0 text-black/60 dark:text-white/60"207 strokeWidth={1.75}208 aria-hidden="true"209 />210 <motion.span211 animate={212 prefersReducedMotion213 ? undefined214 : { backgroundPosition: ["200% 0%", "-200% 0%"] }215 }216 transition={{217 duration: 1.8,218 repeat: Infinity,219 ease: "linear",220 }}221 className="bg-[linear-gradient(110deg,#737373_35%,#0A0A0A_50%,#737373_65%)] bg-[length:220%_100%] bg-clip-text text-xs leading-4 font-medium tracking-[-0.01em] text-transparent dark:bg-[linear-gradient(110deg,#737373_35%,#FFFFFF_50%,#737373_65%)]"222 >223 Thinking...224 </motion.span>225 </motion.div>226 ) : (227 <motion.div228 key="header-done"229 initial={prefersReducedMotion ? false : { opacity: 0, y: 4 }}230 animate={{ opacity: 1, y: 0 }}231 exit={prefersReducedMotion ? undefined : { opacity: 0, y: -4 }}232 transition={233 prefersReducedMotion234 ? { duration: 0 }235 : { duration: 0.18, ease: APPLE_EASE }236 }237 className="flex items-center gap-2"238 >239 <Check240 className="h-3.5 w-3.5 shrink-0 text-[#10B981]"241 strokeWidth={1.75}242 aria-hidden="true"243 />244 <span className="text-xs leading-4 font-medium tracking-[-0.01em] text-[#0A0A0A] dark:text-white">245 Thought for {formattedSeconds}s246 </span>247 </motion.div>248 )}249 </AnimatePresence>250 </div>251
252 <span className="shrink-0 font-mono text-xs leading-4 tabular-nums text-black/40 dark:text-white/40">253 {isThinking ? `${formattedSeconds}s` : `${steps.length} steps`}254 </span>255 </button>256
257 {/* Collapsible Reasoning Trace Timeline */}258 <AnimatePresence initial={false}>259 {isOpen && (260 <motion.div261 id={tracePanelId}262 initial={263 prefersReducedMotion ? false : { height: 0, opacity: 0 }264 }265 animate={{ height: "auto", opacity: 1 }}266 exit={267 prefersReducedMotion ? undefined : { height: 0, opacity: 0 }268 }269 transition={270 prefersReducedMotion271 ? { duration: 0 }272 : { duration: 0.22, ease: APPLE_EASE }273 }274 className="overflow-hidden"275 >276 <div className="px-3 pt-2 pb-2">277 <motion.div278 layout={!prefersReducedMotion}279 transition={280 prefersReducedMotion281 ? { duration: 0 }282 : { layout: CONTAINER_LAYOUT_SPRING }283 }284 className="relative"285 >286 {/* 1px Vertical Spine Track + Smooth Progress Line */}287 <div288 aria-hidden="true"289 className="pointer-events-none absolute top-2 bottom-2 left-[7px] w-px bg-black/[0.06] dark:bg-white/[0.06]"290 />291 <motion.div292 aria-hidden="true"293 initial={shouldAnimateStream ? { scaleY: 0 } : false}294 animate={{ scaleY: spineProgress }}295 transition={296 prefersReducedMotion297 ? { duration: 0 }298 : { duration: 0.32, ease: APPLE_EASE }299 }300 style={{ transformOrigin: "top" }}301 className="pointer-events-none absolute top-2 bottom-2 left-[7px] w-px bg-black/[0.18] dark:bg-white/[0.2]"302 />303
304 <AnimatePresence initial={false}>305 {visibleSteps.map((step, idx) => {306 const done = idx < completedCount;307 return (308 <motion.div309 key={step.id}310 initial={311 prefersReducedMotion312 ? false313 : { opacity: 0, height: 0, y: 6 }314 }315 animate={{ opacity: 1, height: "auto", y: 0 }}316 exit={317 prefersReducedMotion318 ? undefined319 : { opacity: 0, height: 0, y: -4 }320 }321 transition={322 prefersReducedMotion323 ? { duration: 0 }324 : { duration: 0.24, ease: APPLE_EASE }325 }326 className="overflow-hidden"327 >328 <div329 className={`relative flex items-start gap-3 ${330 idx > 0 ? "pt-3" : ""331 }`}332 >333 <span className="relative z-10 flex h-4 w-4 shrink-0 items-center justify-center rounded-full bg-[#F3F3F5] dark:bg-[#1A1A1E]">334 <AnimatePresence mode="popLayout" initial={false}>335 {done ? (336 <motion.span337 key="step-done"338 initial={339 prefersReducedMotion340 ? false341 : { opacity: 0, scale: 0.65 }342 }343 animate={{ opacity: 1, scale: 1 }}344 exit={345 prefersReducedMotion346 ? undefined347 : { opacity: 0, scale: 0.65 }348 }349 transition={350 prefersReducedMotion351 ? { duration: 0 }352 : { duration: 0.16, ease: APPLE_EASE }353 }354 className="inline-flex items-center justify-center"355 >356 <Check357 className="h-3 w-3 text-[#10B981]"358 strokeWidth={2}359 aria-hidden="true"360 />361 </motion.span>362 ) : (363 <motion.span364 key="step-loading"365 initial={366 prefersReducedMotion367 ? false368 : { opacity: 0, scale: 0.65 }369 }370 animate={{ opacity: 1, scale: 1 }}371 exit={372 prefersReducedMotion373 ? undefined374 : { opacity: 0, scale: 0.65 }375 }376 transition={377 prefersReducedMotion378 ? { duration: 0 }379 : { duration: 0.16, ease: APPLE_EASE }380 }381 className="inline-flex items-center justify-center"382 >383 <Loader2384 className={`h-3 w-3 text-black/60 dark:text-white/60 ${385 prefersReducedMotion ? "" : "animate-spin"386 }`}387 strokeWidth={1.75}388 aria-hidden="true"389 />390 </motion.span>391 )}392 </AnimatePresence>393 </span>394
395 <div className="min-w-0 flex-1">396 <div className="flex items-center justify-between gap-2">397 <span398 className={`truncate text-xs leading-4 font-medium tracking-[-0.01em] transition-colors duration-200 ${399 done400 ? "text-black/75 dark:text-white/75"401 : "text-black dark:text-white"402 }`}403 >404 {step.title}405 </span>406 {done && (407 <span className="shrink-0 font-mono text-xs leading-4 tabular-nums text-black/40 dark:text-white/40">408 {step.latency}409 </span>410 )}411 </div>412
413 <p className="mt-1 text-xs leading-[18px] font-normal text-black/50 dark:text-white/50">414 {step.detail}415 </p>416
417 {(step.tool || step.file) && (418 <div className="mt-2 flex min-w-0 flex-wrap items-center gap-2">419 {step.tool && (420 <span className="inline-flex min-h-6 max-w-full min-w-0 items-center gap-2 rounded-[8px] bg-[#F3F3F5] px-2 font-mono text-xs leading-4 tabular-nums text-black/70 dark:bg-[#1A1A1E] dark:text-white/75">421 <Terminal422 className="h-3.5 w-3.5 shrink-0 text-black/45 dark:text-white/45"423 strokeWidth={1.75}424 aria-hidden="true"425 />426 <span className="truncate">{step.tool}</span>427 </span>428 )}429 {step.file && (430 <span className="inline-flex min-h-6 max-w-full min-w-0 items-center gap-2 rounded-[8px] bg-[#F3F3F5] px-2 font-mono text-xs leading-4 tabular-nums text-black/70 dark:bg-[#1A1A1E] dark:text-white/75">431 <FileText432 className="h-3.5 w-3.5 shrink-0 text-black/45 dark:text-white/45"433 strokeWidth={1.75}434 aria-hidden="true"435 />436 <span className="truncate">{step.file}</span>437 </span>438 )}439 </div>440 )}441 </div>442 </div>443 </motion.div>444 );445 })}446 </AnimatePresence>447 </motion.div>448 </div>449 </motion.div>450 )}451 </AnimatePresence>452 </motion.div>453 );454}Usage Example
import { AgentThinkingTrace } from "@/components/ui/AgentThinkingTrace";
export default function ReasoningBlock() { return <AgentThinkingTrace defaultOpen={true} autoPlay={true} />;}3. Props Reference
| Prop | Type | Default | Description |
|---|---|---|---|
| steps | ThinkingStep[] | DEFAULT_THINKING_STEPS | Sequential reasoning steps and tool-call metadata. |
| open | boolean | undefined | Controlled expanded/collapsed accordion state. |
| defaultOpen | boolean | true | Initial accordion visibility when uncontrolled. |
| autoPlay | boolean | true | Streams steps sequentially with live elapsed time; false shows completed state immediately. |
| onOpenChange | (open: boolean) => void | undefined | Fired when clicking the accordion header trigger. |