{
  "schemaVersion": 1,
  "target": "react",
  "name": "reasoning",
  "version": "0.1.0",
  "category": "ai-elements",
  "title": "Reasoning",
  "description": "The agent's thinking as it streams, in a quiet collapsible block that reads Thinking with the live mark, then the time it took, and folds itself away when done.",
  "tags": [
    "ai",
    "agent",
    "reasoning",
    "thinking",
    "streaming",
    "collapsible"
  ],
  "requires": {
    "projectTypes": [
      "next",
      "vite-react"
    ],
    "styling": "tailwind-v4"
  },
  "npmDependencies": {
    "@base-ui/react": "^1.9.0"
  },
  "registryDependencies": [
    {
      "name": "agent-state",
      "version": "0.1.0"
    }
  ],
  "docsPath": "/components/reasoning",
  "props": [
    {
      "name": "children",
      "type": "ReactNode",
      "description": "The reasoning text or any content. Plain strings keep their line breaks."
    },
    {
      "name": "streaming",
      "type": "boolean",
      "default": "false",
      "description": "True while reasoning arrives. The trigger reads \"Thinking…\" with the animated thinking mark; afterwards the mark holds still."
    },
    {
      "name": "duration",
      "type": "number",
      "description": "Seconds spent thinking, shown as \"Thought for 6 s\". Measured from streaming on to off when omitted."
    },
    {
      "name": "autoCollapse",
      "type": "boolean | number",
      "default": "true",
      "description": "Closes the block after streaming ends: true waits 1000ms, a number sets the delay, false keeps it open. Skipped if the reader toggled it during the stream."
    },
    {
      "name": "open / defaultOpen / onOpenChange",
      "type": "boolean / boolean / (open) => void",
      "description": "Controlled or uncontrolled open state. defaultOpen falls back to streaming, so a live block starts open."
    },
    {
      "name": "label",
      "type": "ReactNode",
      "description": "Replaces the trigger text."
    },
    {
      "name": "formatThinkingTime",
      "type": "(seconds) => string",
      "description": "The trigger's wording on its own: \"Thought for 6 s\", \"Thought for 1 min 12 s\", \"Thought for a moment\"."
    }
  ],
  "accessibility": [
    "Built on Base UI Collapsible: the trigger is a button with aria-expanded and aria-controls, operable with Enter and Space.",
    "A polite status region announces \"Thinking\" and then \"Thought for 6 seconds\"; the streamed text itself is not announced word by word.",
    "The panel is aria-busy while streaming. The mark is decorative.",
    "With reduced motion the mark renders a still frame and the panel opens and closes without a height animation."
  ],
  "states": [
    "streaming",
    "done",
    "collapsed",
    "expanded"
  ],
  "files": [
    {
      "path": "agent/reasoning.tsx",
      "content": "\"use client\";\n\nimport { Collapsible } from \"@base-ui/react/collapsible\";\nimport { type ReactNode, useEffect, useRef, useState } from \"react\";\nimport { AgentMark } from \"./agent-state\";\n\nfunction cx(...classes: Array<string | false | null | undefined>) {\n  return classes.filter(Boolean).join(\" \");\n}\n\nconst ease = \"ease-[cubic-bezier(0.23,1,0.32,1)]\";\n\n/**\n * \"Thought for 6 s\", \"Thought for 1 min 12 s\", or \"Thought for a moment\" under a second.\n * Exported so a message footer or a trace can use the same wording.\n */\nexport function formatThinkingTime(seconds: number): string {\n  if (!Number.isFinite(seconds) || seconds < 1) return \"Thought for a moment\";\n  const total = Math.round(seconds);\n  if (total < 60) return `Thought for ${total} s`;\n  const minutes = Math.floor(total / 60);\n  const rest = total % 60;\n  return rest ? `Thought for ${minutes} min ${rest} s` : `Thought for ${minutes} min`;\n}\n\n/** The same duration spelled out for screen readers (\"6 seconds\" rather than \"6 s\"). */\nfunction spokenTime(seconds: number): string {\n  if (!Number.isFinite(seconds) || seconds < 1) return \"Thought for a moment\";\n  const total = Math.round(seconds);\n  const minutes = Math.floor(total / 60);\n  const rest = total % 60;\n  const parts = [\n    minutes ? `${minutes} ${minutes === 1 ? \"minute\" : \"minutes\"}` : \"\",\n    rest ? `${rest} ${rest === 1 ? \"second\" : \"seconds\"}` : \"\",\n  ].filter(Boolean);\n  return `Thought for ${parts.join(\" \")}`;\n}\n\nexport interface ReasoningProps {\n  /** The reasoning text, or any content. Plain strings keep their line breaks. */\n  children?: ReactNode;\n  /** True while reasoning tokens are arriving. The trigger reads \"Thinking…\" with the live mark. */\n  streaming?: boolean;\n  /**\n   * Seconds spent thinking, shown once streaming ends. When omitted, the block measures the\n   * time between `streaming` turning on and off itself.\n   */\n  duration?: number;\n  /**\n   * Closes the block after streaming ends. `true` waits 1000ms, a number sets the delay in ms,\n   * `false` leaves it open. Skipped when the reader opened or closed the block during the stream.\n   * @default true\n   */\n  autoCollapse?: boolean | number;\n  /** Controlled open state. Pair with `onOpenChange`. */\n  open?: boolean;\n  /** Initial open state when uncontrolled. Defaults to `streaming`, so a live block starts open. */\n  defaultOpen?: boolean;\n  /** Called with the next open state, from the trigger or from auto open and collapse. */\n  onOpenChange?: (open: boolean) => void;\n  /** Replaces the trigger text, for example \"Reasoning hidden by policy\". */\n  label?: ReactNode;\n  /** Classes for the outer element. */\n  className?: string;\n  /** Classes for the content wrapper inside the panel. */\n  contentClassName?: string;\n}\n\n/**\n * The agent's thinking as it streams: a quiet disclosure that reads \"Thinking…\" next to the\n * animated thinking mark, then \"Thought for 6 s\" with the mark held still. It opens while the\n * stream runs and folds itself away shortly after, unless the reader took control of it.\n */\nexport function Reasoning({\n  children,\n  streaming = false,\n  duration,\n  autoCollapse = true,\n  open: openProp,\n  defaultOpen,\n  onOpenChange,\n  label,\n  className,\n  contentClassName,\n}: ReasoningProps) {\n  const [uncontrolledOpen, setUncontrolledOpen] = useState(defaultOpen ?? streaming);\n  const controlled = openProp !== undefined;\n  const open = controlled ? openProp : uncontrolledOpen;\n\n  const [measured, setMeasured] = useState<number | undefined>(undefined);\n  const startedAt = useRef<number | null>(streaming ? performance.now() : null);\n  const touched = useRef(false);\n  const wasStreaming = useRef(streaming);\n\n  // Keep the latest handlers in refs so the streaming effect only depends on `streaming`.\n  const setOpenRef = useRef<(next: boolean) => void>(() => {});\n  setOpenRef.current = (next: boolean) => {\n    if (!controlled) setUncontrolledOpen(next);\n    onOpenChange?.(next);\n  };\n  const delay = autoCollapse === false ? null : autoCollapse === true ? 1000 : Math.max(0, autoCollapse);\n  const delayRef = useRef(delay);\n  delayRef.current = delay;\n\n  // Bumped when a stream ends; the timer lives in its own effect so it survives Strict Mode.\n  const [collapseRequest, setCollapseRequest] = useState(0);\n\n  useEffect(() => {\n    if (streaming === wasStreaming.current) return;\n    wasStreaming.current = streaming;\n    if (streaming) {\n      startedAt.current = performance.now();\n      touched.current = false;\n      setMeasured(undefined);\n      setOpenRef.current(true);\n      return;\n    }\n    if (startedAt.current != null) setMeasured((performance.now() - startedAt.current) / 1000);\n    startedAt.current = null;\n    if (!touched.current) setCollapseRequest((count) => count + 1);\n  }, [streaming]);\n\n  useEffect(() => {\n    if (!collapseRequest || delayRef.current == null) return;\n    const timer = setTimeout(() => {\n      if (!touched.current && !wasStreaming.current) setOpenRef.current(false);\n    }, delayRef.current);\n    return () => clearTimeout(timer);\n  }, [collapseRequest]);\n\n  const seconds = duration ?? measured;\n  const text = label ?? (streaming ? \"Thinking\" : seconds != null ? formatThinkingTime(seconds) : \"Reasoning\");\n  const announcement = streaming ? \"Thinking\" : seconds != null ? spokenTime(seconds) : \"\";\n\n  return (\n    <Collapsible.Root\n      open={open}\n      onOpenChange={(next) => {\n        touched.current = true;\n        setOpenRef.current(next);\n      }}\n      data-streaming={streaming || undefined}\n      className={cx(\"w-full min-w-0\", className)}\n    >\n      <Collapsible.Trigger\n        className={cx(\n          \"group -mx-1.5 inline-flex h-7 max-w-[calc(100%+0.75rem)] cursor-pointer items-center gap-2 rounded-md px-1.5 text-sm select-none\",\n          \"text-zinc-600 transition-[background-color,color] duration-150 ease-out motion-reduce:transition-none\",\n          \"hover:bg-zinc-950/[0.04] hover:text-zinc-950 data-[panel-open]:text-zinc-950\",\n          \"dark:text-zinc-400 dark:hover:bg-white/[0.06] dark:hover:text-white dark:data-[panel-open]:text-white\",\n          \"focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-sky-500\",\n        )}\n      >\n        <span aria-hidden=\"true\" className=\"inline-flex shrink-0\">\n          {/* One mark throughout: it moves while the model thinks and holds still afterwards. */}\n          <AgentMark\n            activity=\"thinking\"\n            size={16}\n            paused={!streaming}\n            className={cx(\n              \"transition-colors duration-300 motion-reduce:transition-none\",\n              streaming ? \"text-zinc-950 dark:text-white\" : \"text-zinc-950/40 dark:text-white/40\",\n            )}\n          />\n        </span>\n        <span\n          key={typeof text === \"string\" ? text : undefined}\n          className={cx(\n            \"min-w-0 truncate\",\n            streaming && label == null ? \"after:content-['…']\" : \"\",\n            `transition-[opacity,filter,translate] duration-300 ${ease} starting:translate-y-[3px] starting:opacity-0 starting:blur-[2px] motion-reduce:transition-none`,\n          )}\n        >\n          {text}\n        </span>\n        <svg\n          aria-hidden=\"true\"\n          viewBox=\"0 0 16 16\"\n          fill=\"none\"\n          className={cx(\n            \"size-3.5 shrink-0 text-zinc-400 dark:text-zinc-500\",\n            `transition-[rotate,color] duration-220 ${ease} motion-reduce:transition-none`,\n            \"group-hover:text-current group-data-[panel-open]:rotate-180 group-data-[panel-open]:text-current\",\n          )}\n        >\n          <path d=\"m4.5 6.25 3.5 3.5 3.5-3.5\" stroke=\"currentColor\" strokeWidth=\"1.5\" strokeLinecap=\"round\" strokeLinejoin=\"round\" />\n        </svg>\n      </Collapsible.Trigger>\n\n      {/* Announces the switch from \"Thinking\" to the elapsed time, not the streamed text. */}\n      <span role=\"status\" className=\"sr-only\">\n        {announcement}\n      </span>\n\n      <Collapsible.Panel\n        aria-busy={streaming || undefined}\n        className={cx(\n          \"h-(--collapsible-panel-height) overflow-hidden\",\n          `transition-[height,opacity] duration-220 ${ease} motion-reduce:transition-none`,\n          \"data-[starting-style]:h-0 data-[starting-style]:opacity-0\",\n          \"data-[ending-style]:h-0 data-[ending-style]:opacity-0 data-[ending-style]:duration-180\",\n        )}\n      >\n        {/* The rail lines up with the center of the mark above it. */}\n        <div\n          className={cx(\n            \"mt-1.5 ml-[7.5px] border-l border-zinc-950/10 pl-[18px] text-sm leading-relaxed whitespace-pre-wrap text-zinc-600 dark:border-white/10 dark:text-zinc-400\",\n            contentClassName,\n          )}\n        >\n          {children}\n        </div>\n      </Collapsible.Panel>\n    </Collapsible.Root>\n  );\n}\n"
    }
  ],
  "example": {
    "content": "import { Reasoning } from \"@/components/shaken/agent/reasoning\";\n\nexport default function ReasoningExample() {\n  return (\n    <div className=\"grid w-full max-w-lg gap-3\">\n      <Reasoning duration={6} defaultOpen>\n        {\n          \"The user wants to know why the nightly export job failed. The log shows the job started at 02:00 and stopped after 41 seconds with a 403 from the storage bucket.\\n\\nA 403 rather than a 404 means the bucket exists but the service account cannot write to it. The credentials were rotated yesterday, so the new key probably lacks the storage writer role.\"\n        }\n      </Reasoning>\n      <p className=\"text-sm leading-relaxed text-zinc-950 dark:text-white\">\n        The export failed because the rotated service account key cannot write to the bucket. Grant it the storage writer role, then rerun the job.\n      </p>\n    </div>\n  );\n}\n"
  }
}
