The failing check looks like a stale edge cache rather than a code regression. I should invalidate the tag and re-check the preview.

"use client"

import {
  createContext,
  useCallback,
  useContext,
  useEffect,
  useRef,
  useState,
  type PropsWithChildren,
} from "react"
import { BrainIcon, ChevronDownIcon } from "lucide-react"

import { cn } from "@/lib/utils"
import { ShimmerLabel } from "@/registry/tonic/lib/surfaces"

type ReasoningContextValue = {
  isOpen: boolean
  isPreview: boolean
  onOpenChange: (open: boolean) => void
}

const ReasoningContext = createContext<ReasoningContextValue | null>(null)

function useReasoningContext() {
  const context = useContext(ReasoningContext)
  if (!context) {
    throw new Error("Reasoning components must be nested in ReasoningRoot")
  }
  return context
}

export type ReasoningRootProps = Omit<
  React.ComponentProps<"div">,
  "children"
> &
  PropsWithChildren<{
    open?: boolean
    onOpenChange?: (open: boolean) => void
    defaultOpen?: boolean
    streaming?: boolean
  }>

export function ReasoningRoot({
  open: controlledOpen,
  onOpenChange: controlledOnOpenChange,
  defaultOpen = false,
  streaming,
  className,
  children,
  ...props
}: ReasoningRootProps) {
  const [initialOpen] = useState(defaultOpen)
  const [userOpen, setUserOpen] = useState<boolean | null>(null)
  const isControlled = controlledOpen !== undefined
  const isOpen = isControlled
    ? controlledOpen
    : (userOpen ?? (streaming || initialOpen))
  const isPreview = streaming === true && isOpen
  const onOpenChange = useCallback(
    (open: boolean) => {
      if (!isControlled) setUserOpen(open)
      controlledOnOpenChange?.(open)
    },
    [controlledOnOpenChange, isControlled]
  )

  return (
    <ReasoningContext.Provider value={{ isOpen, isPreview, onOpenChange }}>
      <div
        data-slot="reasoning-root"
        className={cn("w-full", className)}
        {...props}
      >
        {children}
      </div>
    </ReasoningContext.Provider>
  )
}

export type ReasoningTriggerProps = Omit<
  React.ComponentProps<"button">,
  "children"
> & {
  active?: boolean
  duration?: number
}

export function ReasoningTrigger({
  active,
  duration,
  className,
  onClick,
  ...props
}: ReasoningTriggerProps) {
  const { isOpen, onOpenChange } = useReasoningContext()
  const label = `Reasoning${duration ? ` (${duration}s)` : ""}`

  return (
    <button
      type="button"
      data-slot="reasoning-trigger"
      aria-expanded={isOpen}
      aria-busy={active === true}
      className={cn(
        "flex min-h-7 items-center gap-1.5 py-1 outline-none",
        className
      )}
      onClick={(event) => {
        onClick?.(event)
        onOpenChange(!isOpen)
      }}
      {...props}
    >
      <BrainIcon className="text-muted-foreground size-3.5" />
      <ShimmerLabel
        active={active}
        className="text-muted-foreground text-[15px] leading-6 tabular-nums"
      >
        {label}
      </ShimmerLabel>
      <ChevronDownIcon
        className={cn(
          "text-muted-foreground/55 size-3.5 transition-transform duration-200 motion-reduce:transition-none",
          !isOpen && "-rotate-90"
        )}
      />
    </button>
  )
}

export type ReasoningContentProps = Omit<
  React.ComponentProps<"div">,
  "children"
> &
  PropsWithChildren

export function ReasoningContent({
  className,
  children,
  ...props
}: ReasoningContentProps) {
  const { isOpen } = useReasoningContext()
  if (!isOpen) return null
  return (
    <div
      data-slot="reasoning-content"
      className={cn("overflow-hidden", className)}
      {...props}
    >
      {children}
    </div>
  )
}

export type ReasoningTextProps = Omit<
  React.ComponentProps<"div">,
  "children"
> &
  PropsWithChildren

export function ReasoningText({
  className,
  children,
  ...props
}: ReasoningTextProps) {
  const { isPreview } = useReasoningContext()
  const scrollRef = useRef<HTMLDivElement>(null)

  useEffect(() => {
    if (!isPreview || !scrollRef.current) return
    scrollRef.current.scrollTop = scrollRef.current.scrollHeight
  }, [isPreview, children])

  return (
    <div
      ref={scrollRef}
      data-slot="reasoning-text"
      className={cn("max-h-64 overflow-y-auto py-2 ps-6", className)}
      {...props}
    >
      {typeof children === "string" || typeof children === "number" ? (
        <p className="text-muted-foreground text-sm whitespace-pre-wrap">
          {children}
        </p>
      ) : (
        children
      )}
    </div>
  )
}

export const Reasoning = {
  Root: ReasoningRoot,
  Trigger: ReasoningTrigger,
  Content: ReasoningContent,
  Text: ReasoningText,
}