"use client"; import { createContext, useCallback, useContext, useEffect, useLayoutEffect, useRef, useState, } from "react"; import { cva, type VariantProps } from "class-variance-authority"; import { BrainIcon, ChevronDownIcon } from "lucide-react"; import { Collapsible, CollapsibleContent, CollapsibleTrigger, } from '../../ui/collapsible'; import { cn } from '../../lib/utils'; export const ANIMATION_DURATION = 200; const ReasoningPreviewContext = createContext(false); const reasoningVariants = cva("aui-reasoning-root mb-4 w-full", { variants: { variant: { outline: "rounded-lg border px-3 py-2", ghost: "", muted: "bg-muted/50 rounded-lg px-3 py-2", }, }, defaultVariants: { variant: "outline", }, }); export type ReasoningRootProps = Omit< React.ComponentProps, "open" | "onOpenChange" > & VariantProps & { open?: boolean; onOpenChange?: (open: boolean) => void; defaultOpen?: boolean; /** * Whether the reasoning is currently streaming. While `true` the * disclosure is held open with a bottom-pinned live preview; when * streaming ends it returns to `defaultOpen`, and the first manual * toggle takes over the open/close state permanently. The live preview * keeps following the newest tokens while the disclosure is open during * streaming, even after a manual toggle, and pauses while the reader is * scrolled up. */ streaming?: boolean; /** Called right before the disclosure animates, on toggle and on streaming transitions. */ onAnimationStart?: () => void; }; function ReasoningRoot({ className, variant, open: controlledOpen, onOpenChange: controlledOnOpenChange, defaultOpen = false, streaming, onAnimationStart, children, ...props }: ReasoningRootProps) { const initialOpenRef = useRef(defaultOpen); const [userOpen, setUserOpen] = useState(null); const isControlled = controlledOpen !== undefined; const isOpen = isControlled ? controlledOpen : (userOpen ?? (streaming || initialOpenRef.current)); const isPreview = streaming === true && isOpen; const prevStreamingRef = useRef(streaming); useLayoutEffect(() => { if (prevStreamingRef.current === streaming) return; prevStreamingRef.current = streaming; // A streaming transition only animates the panel when the resting state // is collapsed; with `defaultOpen` the disclosure stays open across it. if (!isControlled && userOpen === null && !initialOpenRef.current) { onAnimationStart?.(); } }, [streaming, isControlled, userOpen, onAnimationStart]); const handleOpenChange = useCallback( (open: boolean) => { onAnimationStart?.(); if (!isControlled) { setUserOpen(open); } controlledOnOpenChange?.(open); }, [onAnimationStart, isControlled, controlledOnOpenChange], ); return ( {children} ); } function ReasoningFade({ side = "bottom", className, ...props }: React.ComponentProps<"div"> & { side?: "top" | "bottom" }) { if (side === "top") { return (
); } return (
); } function ReasoningTrigger({ active, duration, className, ...props }: React.ComponentProps & { active?: boolean; duration?: number; }) { const durationText = duration ? ` (${duration}s)` : ""; return ( Reasoning{durationText} ); } function ReasoningContent({ className, children, ...props }: React.ComponentProps) { const isPreview = useContext(ReasoningPreviewContext); return ( {children} {isPreview ? : null} ); } function ReasoningText({ className, children, ...props }: React.ComponentProps<"div">) { const isPreview = useContext(ReasoningPreviewContext); const scrollRef = useRef(null); const contentRef = useRef(null); useEffect(() => { if (!isPreview) return; const scrollEl = scrollRef.current; const contentEl = contentRef.current; if (!scrollEl || !contentEl) return; let pinned = true; let lastScrollTop = scrollEl.scrollTop; let lastScrollHeight = scrollEl.scrollHeight; const isAtBottom = () => Math.abs( scrollEl.scrollHeight - scrollEl.scrollTop - scrollEl.clientHeight, ) <= 1 || scrollEl.scrollHeight <= scrollEl.clientHeight; const pin = () => { if (!pinned) return; scrollEl.scrollTop = scrollEl.scrollHeight; }; // A pin's own scroll event can arrive after new content grew the scroll // height and read as "not at bottom"; only an upward move at unchanged // scroll height is user intent. const onScroll = () => { if (isAtBottom()) { pinned = true; } else if ( scrollEl.scrollTop < lastScrollTop && scrollEl.scrollHeight === lastScrollHeight ) { pinned = false; } lastScrollTop = scrollEl.scrollTop; lastScrollHeight = scrollEl.scrollHeight; }; pin(); scrollEl.addEventListener("scroll", onScroll); const observer = new ResizeObserver(pin); observer.observe(contentEl); return () => { scrollEl.removeEventListener("scroll", onScroll); observer.disconnect(); }; }, [isPreview]); return (
{children}
); } export { ReasoningRoot, ReasoningTrigger, ReasoningContent, ReasoningText, ReasoningFade, reasoningVariants, };