{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "tool-call",
  "title": "Tool call",
  "description": "Collapsible card for one AI agent tool call: the arguments stream in, a status pill shimmers while it runs, then the result slides in. Takes a Vercel AI SDK tool part as is.",
  "dependencies": [
    "lucide-react",
    "motion"
  ],
  "registryDependencies": [
    "utils"
  ],
  "files": [
    {
      "path": "registry/components/tool-call.tsx",
      "content": "\"use client\";\n\nimport { useEffect, useId, useRef, useState } from \"react\";\nimport { Check, ChevronDown, TriangleAlert, Wrench } from \"lucide-react\";\nimport { AnimatePresence, motion, useReducedMotion } from \"motion/react\";\nimport { cn } from \"@/lib/utils\";\n\n/** The states of a tool part in the Vercel AI SDK (`part.state`). */\nexport type ToolCallState = \"input-streaming\" | \"input-available\" | \"output-available\" | \"output-error\";\n\n/** The fields this card reads from an AI SDK tool part (`message.parts` entries whose type starts with \"tool-\"). */\nexport type ToolPartLike = {\n  type: string;\n  state: ToolCallState;\n  input?: unknown;\n  output?: unknown;\n  errorText?: string;\n};\n\nexport type ToolCallProps = {\n  /** Tool name shown in the header. Taken from `part.type` (\"tool-search_web\" → \"search_web\") when you pass `part`. */\n  name?: string;\n  state?: ToolCallState;\n  /** Arguments the model sent. Objects are shown as formatted JSON. */\n  input?: unknown;\n  /** What the tool returned. Objects are shown as formatted JSON; strings and elements as they are. */\n  output?: unknown;\n  errorText?: string;\n  /** An AI SDK tool part; fills name, state, input, output and errorText. */\n  part?: ToolPartLike;\n  /** Time the call took, in ms, shown beside \"Done\". */\n  duration?: number;\n  /** Loop a scripted call while on screen. Turn off to drive the props yourself. */\n  autoPlay?: boolean;\n  open?: boolean;\n  defaultOpen?: boolean;\n  onOpenChange?: (open: boolean) => void;\n  className?: string;\n};\n\nconst DEMO_NAME = \"search_web\";\nconst DEMO_INPUT = { query: \"agent UI patterns 2026\", maxResults: 3 };\nconst DEMO_OUTPUT = [\n  { title: \"Tool calls the user can read\", url: \"nextui.dev/components\" },\n  { title: \"Streaming without layout shift\", url: \"nextui.dev/docs\" },\n  { title: \"Approvals before side effects\", url: \"nextui.dev/blocks\" },\n];\n\n// One pass of the demo, in ms.\nconst TYPE_START = 600;\nconst CHAR_MS = 28;\nconst TICK = 50;\n\nfunction format(value: unknown): string {\n  if (value === undefined) return \"\";\n  if (typeof value === \"string\") return value;\n  try {\n    return JSON.stringify(value, null, 2);\n  } catch {\n    return String(value);\n  }\n}\n\n/** A collapsible card for one agent tool call: arguments stream in, a status pill runs, then the result lands. */\nexport function ToolCall({\n  name: nameProp,\n  state: stateProp,\n  input: inputProp,\n  output: outputProp,\n  errorText: errorProp,\n  part,\n  duration,\n  autoPlay = false,\n  open: openProp,\n  defaultOpen = true,\n  onOpenChange,\n  className,\n}: ToolCallProps) {\n  const ref = useRef<HTMLDivElement>(null);\n  const bodyId = useId();\n  const reduce = useReducedMotion();\n  const [visible, setVisible] = useState(false);\n  const [innerOpen, setInnerOpen] = useState(defaultOpen);\n  const open = openProp ?? innerOpen;\n  const [tick, setTick] = useState(0);\n\n  const demoInput = format(DEMO_INPUT);\n  const typed = TYPE_START + demoInput.length * CHAR_MS;\n  const RUN = typed + 1400;\n  const LOOP = RUN + 3600;\n\n  useEffect(() => {\n    const el = ref.current;\n    if (!el || !autoPlay) return;\n    const io = new IntersectionObserver(([entry]) => setVisible(entry.isIntersecting));\n    io.observe(el);\n    return () => io.disconnect();\n  }, [autoPlay]);\n\n  useEffect(() => {\n    if (!autoPlay || !visible || reduce) return;\n    const id = setInterval(() => setTick((t) => (t + TICK) % LOOP), TICK);\n    return () => clearInterval(id);\n  }, [autoPlay, visible, reduce, LOOP]);\n\n  let name = nameProp ?? (part ? part.type.replace(/^tool-/, \"\") : DEMO_NAME);\n  let state: ToolCallState = stateProp ?? part?.state ?? \"output-available\";\n  let inputText = format(inputProp ?? part?.input);\n  let output: unknown = outputProp ?? part?.output;\n  const errorText = errorProp ?? part?.errorText;\n  let ms = duration;\n\n  if (autoPlay) {\n    const t = reduce ? LOOP - 1 : tick;\n    name = nameProp ?? DEMO_NAME;\n    const chars = Math.max(0, Math.floor((t - TYPE_START) / CHAR_MS));\n    inputText = demoInput.slice(0, chars);\n    output = <DemoResults />;\n    ms = 1240;\n    state = t < typed ? \"input-streaming\" : t < RUN ? \"input-available\" : \"output-available\";\n  }\n\n  const running = state === \"input-streaming\" || state === \"input-available\";\n  const failed = state === \"output-error\";\n  const hasOutput = state === \"output-available\" && output !== undefined;\n  const summary = summarize(autoPlay ? DEMO_INPUT : (inputProp ?? part?.input));\n\n  return (\n    <div ref={ref} className={cn(\"w-full max-w-md rounded-xl bg-muted p-1.5 text-foreground\", className)}>\n      <div className=\"rounded-lg border bg-card shadow-xs\">\n        <button\n          type=\"button\"\n          aria-expanded={open}\n          aria-controls={bodyId}\n          onClick={() => {\n            if (openProp === undefined) setInnerOpen(!open);\n            onOpenChange?.(!open);\n          }}\n          className=\"flex w-full items-center gap-2.5 rounded-lg px-3 py-2.5 text-left text-sm outline-none focus-visible:ring-2 focus-visible:ring-ring\"\n        >\n          <StateIcon state={state} still={!!reduce} />\n          <span className=\"font-mono text-[13px] font-medium\">{name}</span>\n          {summary && <span className=\"min-w-0 flex-1 truncate text-xs text-muted-foreground\">{summary}</span>}\n          {!summary && <span className=\"flex-1\" />}\n          <StatusPill state={state} ms={ms} still={!!reduce} />\n          <ChevronDown\n            className={cn(\"size-3.5 shrink-0 text-muted-foreground transition-transform duration-300\", !open && \"-rotate-90\")}\n            aria-hidden\n          />\n        </button>\n\n        <div\n          id={bodyId}\n          className={cn(\n            \"grid transition-[grid-template-rows,opacity] duration-300 ease-[cubic-bezier(0.22,1,0.36,1)]\",\n            open ? \"grid-rows-[1fr] opacity-100\" : \"grid-rows-[0fr] opacity-0\",\n          )}\n        >\n          <div className=\"overflow-hidden\">\n            <div className=\"space-y-3 border-t px-3 pt-3 pb-3\">\n              <Section label=\"Input\">\n                <pre className=\"max-h-48 overflow-auto rounded-md bg-muted px-3 py-2 font-mono text-xs leading-relaxed whitespace-pre-wrap text-foreground/90\">\n                  {inputText}\n                  {state === \"input-streaming\" && !reduce && (\n                    <span aria-hidden className=\"ml-px inline-block h-3.5 w-1.5 translate-y-0.5 animate-pulse bg-foreground/60\" />\n                  )}\n                </pre>\n              </Section>\n\n              <AnimatePresence initial={false}>\n                {(hasOutput || failed) && (\n                  <motion.div\n                    key={failed ? \"error\" : \"output\"}\n                    initial={reduce ? false : { opacity: 0, y: 6, filter: \"blur(4px)\" }}\n                    animate={{ opacity: 1, y: 0, filter: \"blur(0px)\" }}\n                    exit={reduce ? undefined : { opacity: 0 }}\n                    transition={{ duration: 0.4, ease: [0.22, 1, 0.36, 1] }}\n                  >\n                    {failed ? (\n                      <Section label=\"Error\">\n                        <p className=\"rounded-md bg-destructive/10 px-3 py-2 text-xs text-destructive\">{errorText ?? \"The tool call failed.\"}</p>\n                      </Section>\n                    ) : (\n                      <Section label=\"Output\">\n                        {isElement(output) ? (\n                          output\n                        ) : (\n                          <pre className=\"max-h-56 overflow-auto rounded-md bg-muted px-3 py-2 font-mono text-xs leading-relaxed whitespace-pre-wrap text-foreground/90\">\n                            {format(output)}\n                          </pre>\n                        )}\n                      </Section>\n                    )}\n                  </motion.div>\n                )}\n              </AnimatePresence>\n            </div>\n          </div>\n        </div>\n      </div>\n      <p className=\"sr-only\" role=\"status\">\n        {running ? `Running ${name}` : failed ? `${name} failed` : `${name} finished`}\n      </p>\n      <style>{`\n        .tool-call-shimmer {\n          color: transparent;\n          background: linear-gradient(90deg, var(--muted-foreground) 40%, var(--foreground) 50%, var(--muted-foreground) 60%) 0 0 / 250% 100%;\n          background-clip: text;\n          -webkit-background-clip: text;\n          animation: tool-call-shimmer 2s ease-in-out infinite;\n        }\n        @keyframes tool-call-shimmer {\n          from { background-position: 100% 0; }\n          to { background-position: 0% 0; }\n        }\n        @media (prefers-reduced-motion: reduce) {\n          .tool-call-shimmer { animation: none; color: var(--muted-foreground); }\n        }\n      `}</style>\n    </div>\n  );\n}\n\nexport default function ToolCallDemo() {\n  return <ToolCall autoPlay />;\n}\n\n/** The demo's result, rendered as a list to show that output can be any element. */\nfunction DemoResults() {\n  return (\n    <ul className=\"divide-y rounded-md border text-xs\">\n      {DEMO_OUTPUT.map((r, i) => (\n        <li key={r.title} className=\"flex items-center gap-2.5 px-3 py-2\">\n          <span className=\"grid size-4 shrink-0 place-items-center rounded bg-muted font-mono text-[10px] text-muted-foreground\">{i + 1}</span>\n          <span className=\"min-w-0 flex-1 truncate font-medium\">{r.title}</span>\n          <span className=\"shrink-0 text-muted-foreground\">{r.url}</span>\n        </li>\n      ))}\n    </ul>\n  );\n}\n\nfunction isElement(value: unknown): value is React.ReactElement {\n  return typeof value === \"object\" && value !== null && \"$$typeof\" in value;\n}\n\n/** The first short string or number in the input, as a hint next to the tool name. */\nfunction summarize(input: unknown): string {\n  if (typeof input === \"string\") return input;\n  if (!input || typeof input !== \"object\") return \"\";\n  for (const value of Object.values(input)) {\n    if (typeof value === \"string\" || typeof value === \"number\") return String(value);\n  }\n  return \"\";\n}\n\nfunction Section({ label, children }: { label: string; children: React.ReactNode }) {\n  return (\n    <div>\n      <p className=\"mb-1.5 text-[11px] font-medium tracking-wide text-muted-foreground uppercase\">{label}</p>\n      {children}\n    </div>\n  );\n}\n\nfunction StatusPill({ state, ms, still }: { state: ToolCallState; ms?: number; still: boolean }) {\n  const label =\n    state === \"input-streaming\"\n      ? \"Preparing\"\n      : state === \"input-available\"\n        ? \"Running\"\n        : state === \"output-error\"\n          ? \"Failed\"\n          : ms !== undefined\n            ? `Done · ${(ms / 1000).toFixed(1)}s`\n            : \"Done\";\n  const running = state === \"input-streaming\" || state === \"input-available\";\n\n  return (\n    <span\n      className={cn(\n        \"relative inline-flex h-5 shrink-0 items-center overflow-hidden rounded-md px-1.5 text-[11px] font-medium tabular-nums\",\n        state === \"output-error\" ? \"bg-destructive/10 text-destructive\" : \"bg-muted text-muted-foreground\",\n      )}\n    >\n      <AnimatePresence mode=\"popLayout\" initial={false}>\n        <motion.span\n          key={label}\n          initial={still ? false : { y: \"70%\", opacity: 0, filter: \"blur(3px)\" }}\n          animate={{ y: \"0%\", opacity: 1, filter: \"blur(0px)\" }}\n          exit={still ? undefined : { y: \"-70%\", opacity: 0, filter: \"blur(3px)\" }}\n          transition={{ duration: 0.35, ease: [0.22, 1, 0.36, 1] }}\n          className={cn(\"inline-block\", running && \"tool-call-shimmer\")}\n        >\n          {label}\n        </motion.span>\n      </AnimatePresence>\n    </span>\n  );\n}\n\n/** A wrench that spins gently while running, a filled check when done, a warning when it failed. */\nfunction StateIcon({ state, still }: { state: ToolCallState; still: boolean }) {\n  const running = state === \"input-streaming\" || state === \"input-available\";\n  return (\n    <span className=\"relative grid size-5 shrink-0 place-items-center\" aria-hidden>\n      <motion.span\n        animate={running && !still ? { rotate: [0, -18, 14, 0] } : { rotate: 0 }}\n        transition={running && !still ? { duration: 1.2, repeat: Infinity, ease: \"easeInOut\" } : { duration: 0.2 }}\n        className={cn(\n          \"absolute grid size-5 place-items-center rounded-md border bg-background text-muted-foreground transition-opacity duration-300\",\n          running ? \"opacity-100\" : \"opacity-0\",\n        )}\n      >\n        <Wrench className=\"size-3\" />\n      </motion.span>\n      <motion.span\n        initial={false}\n        animate={state === \"output-available\" ? { scale: 1, opacity: 1 } : { scale: 0.4, opacity: 0 }}\n        transition={still ? { duration: 0 } : { type: \"spring\", stiffness: 520, damping: 22 }}\n        className=\"absolute grid size-5 place-items-center rounded-md bg-foreground text-background\"\n      >\n        <Check className=\"size-3\" strokeWidth={3} />\n      </motion.span>\n      <motion.span\n        initial={false}\n        animate={state === \"output-error\" ? { scale: 1, opacity: 1 } : { scale: 0.4, opacity: 0 }}\n        transition={still ? { duration: 0 } : { type: \"spring\", stiffness: 520, damping: 22 }}\n        className=\"absolute grid size-5 place-items-center rounded-md bg-destructive text-white\"\n      >\n        <TriangleAlert className=\"size-3\" strokeWidth={2.5} />\n      </motion.span>\n    </span>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/nextui/tool-call.tsx"
    }
  ],
  "meta": {
    "kind": "component",
    "category": "ai",
    "previewHeight": 420,
    "new": true,
    "free": true,
    "openSource": true,
    "added": "2026-10-31"
  },
  "categories": [
    "ai"
  ],
  "type": "registry:component"
}