{
  "schemaVersion": 1,
  "target": "react",
  "name": "subagent",
  "version": "0.1.0",
  "category": "ai-elements",
  "title": "Subagent",
  "description": "A nested agent run with its name, task, live state, its own tool calls as Tool Call Cards, and its result, collapsible on a hairline rail.",
  "tags": [
    "ai",
    "agent",
    "subagent",
    "nested",
    "delegation",
    "tool calls"
  ],
  "requires": {
    "projectTypes": [
      "next",
      "vite-react"
    ],
    "styling": "tailwind-v4"
  },
  "npmDependencies": {
    "@base-ui/react": "^1.9.0"
  },
  "registryDependencies": [
    {
      "name": "agent-state",
      "version": "0.1.0"
    },
    {
      "name": "tool-call-card",
      "version": "0.3.0"
    }
  ],
  "docsPath": "/components/subagent",
  "props": [
    {
      "name": "name",
      "type": "string",
      "description": "The subagent's name, in monospace."
    },
    {
      "name": "task",
      "type": "ReactNode",
      "description": "What it was asked to do."
    },
    {
      "name": "status",
      "type": "\"queued\" | \"running\" | \"waiting\" | \"success\" | \"error\" | \"cancelled\"",
      "description": "Drives the Agent State pill."
    },
    {
      "name": "activity / activityLabel",
      "type": "AgentActivity / string",
      "default": "\"working\"",
      "description": "The mark and verb while running."
    },
    {
      "name": "toolCalls",
      "type": "(ToolCallCardProps & { id? })[]",
      "description": "Its own tool calls, rendered as Tool Call Cards on the rail."
    },
    {
      "name": "result",
      "type": "ReactNode",
      "description": "What it returned to the parent agent."
    },
    {
      "name": "error",
      "type": "ReactNode",
      "description": "Why it failed; always visible when status is error."
    },
    {
      "name": "durationMs",
      "type": "number",
      "description": "Elapsed time, shown once it ends."
    },
    {
      "name": "defaultOpen / open / onOpenChange",
      "type": "boolean / (open) => void",
      "description": "Base UI Collapsible open state."
    }
  ],
  "accessibility": [
    "The header is a Base UI Collapsible trigger (a button with aria-expanded) whose hit area and focus ring cover the whole row.",
    "The state pill sits outside the button as a live region, so Running, Waiting for approval, Done, and Failed are announced as they change.",
    "aria-busy is set while running; tool calls are an ordered list labelled with the subagent's name.",
    "The panel height animates in 220ms; the marks hold still frames and disclosures are instant with reduced motion."
  ],
  "states": [
    "queued",
    "running",
    "waiting",
    "success",
    "error",
    "cancelled"
  ],
  "files": [
    {
      "path": "agent/subagent.tsx",
      "content": "\"use client\";\n\nimport { Collapsible } from \"@base-ui/react/collapsible\";\nimport type { ReactNode } from \"react\";\nimport { type AgentActivity, type AgentPhase, AgentState } from \"./agent-state\";\nimport { ToolCallCard, type ToolCallCardProps } from \"./tool-call-card\";\n\nfunction cx(...classes: (string | false | null | undefined)[]) {\n  return classes.filter(Boolean).join(\" \");\n}\n\nconst ease = \"ease-[cubic-bezier(0.23,1,0.32,1)]\";\n\nexport type SubagentStatus = \"queued\" | \"running\" | \"waiting\" | \"success\" | \"error\" | \"cancelled\";\n\nconst phases: Record<SubagentStatus, { phase: AgentPhase; label?: string }> = {\n  queued: { phase: \"idle\", label: \"Queued\" },\n  running: { phase: \"active\" },\n  waiting: { phase: \"waiting\", label: \"Waiting for approval\" },\n  success: { phase: \"done\", label: \"Done\" },\n  error: { phase: \"failed\", label: \"Failed\" },\n  cancelled: { phase: \"cancelled\", label: \"Cancelled\" },\n};\n\nexport interface SubagentToolCall extends ToolCallCardProps {\n  /** Stable key for the call. Defaults to its position. */\n  id?: string;\n}\n\nexport interface SubagentProps extends Omit<Collapsible.Root.Props, \"className\" | \"children\"> {\n  /** The subagent's name, for example `researcher`. */\n  name: string;\n  /** What it was asked to do. */\n  task: ReactNode;\n  status: SubagentStatus;\n  /** Which Agent State mark animates while it runs, and the default verb. */\n  activity?: AgentActivity;\n  /** Replaces the running verb, e.g. \"Reading the docs\". */\n  activityLabel?: string;\n  /** Its own tool calls, in order, rendered as Tool Call Cards. */\n  toolCalls?: SubagentToolCall[];\n  /** What it returned to the parent agent. */\n  result?: ReactNode;\n  /** Why it failed. Always visible when the status is error. */\n  error?: ReactNode;\n  /** Elapsed time, shown once it ends. */\n  durationMs?: number;\n  /** Extra content inside the run, after the tool calls. */\n  children?: ReactNode;\n  className?: string;\n}\n\n/**\n * A nested agent run: its name, task, and live state in a header row, and underneath, on an\n * indented hairline rail, its own tool calls and the result it handed back. Collapsible.\n */\nexport function Subagent({\n  name,\n  task,\n  status,\n  activity = \"working\",\n  activityLabel,\n  toolCalls = [],\n  result,\n  error,\n  durationMs,\n  children,\n  className,\n  ...props\n}: SubagentProps) {\n  const meta = phases[status];\n  const finished = status === \"success\" || status === \"error\" || status === \"cancelled\";\n  const calls = toolCalls.length;\n\n  return (\n    <Collapsible.Root\n      data-status={status}\n      aria-busy={status === \"running\" || undefined}\n      className={cx(\"@container w-full text-left text-zinc-950 dark:text-white\", className)}\n      {...props}\n    >\n      <div className=\"relative flex flex-wrap items-start gap-x-3 gap-y-1.5 py-2 pr-9 pl-2\">\n        <Collapsible.Trigger\n          className={cx(\n            \"group flex min-w-0 flex-1 cursor-pointer items-start gap-3 text-left\",\n            // The trigger's hit area and hover tint cover the whole header row.\n            \"before:absolute before:inset-0 before:rounded-lg before:transition-colors before:duration-150 before:ease-out motion-reduce:before:transition-none\",\n            \"hover:before:bg-zinc-950/[0.03] dark:hover:before:bg-white/[0.04]\",\n            \"outline-none focus-visible:before:outline-2 focus-visible:before:-outline-offset-2 focus-visible:before:outline-sky-500\",\n          )}\n        >\n          <span\n            aria-hidden=\"true\"\n            className={cx(\n              \"relative mt-px flex size-6 shrink-0 items-center justify-center rounded-full text-zinc-600 dark:text-zinc-300\",\n              \"shadow-[inset_0_0_0_1px_rgb(9_9_11/0.1)] dark:shadow-[inset_0_0_0_1px_rgb(255_255_255/0.16)]\",\n            )}\n          >\n            <svg viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.5\" strokeLinecap=\"round\" className=\"size-3.5\">\n              <circle cx=\"5\" cy=\"4.5\" r=\"1.75\" />\n              <circle cx=\"11\" cy=\"11.5\" r=\"1.75\" />\n              <path d=\"M5 6.25v2.5c0 1.38 1.12 2.5 2.5 2.5h1.75\" />\n            </svg>\n          </span>\n          <span className=\"relative min-w-0 flex-1\">\n            <span className=\"flex items-baseline gap-2\">\n              <span className=\"truncate font-mono text-[13px] leading-6 font-medium\">{name}</span>\n              <span className=\"shrink-0 text-xs text-zinc-500\">\n                Subagent{calls ? `, ${calls} tool ${calls === 1 ? \"call\" : \"calls\"}` : \"\"}\n              </span>\n            </span>\n            <span className={cx(\"block text-sm\", status === \"queued\" ? \"text-zinc-500\" : \"text-zinc-600 dark:text-zinc-400\")}>{task}</span>\n          </span>\n          <svg\n            aria-hidden=\"true\"\n            viewBox=\"0 0 16 16\"\n            className={`absolute top-3.5 right-2.5 size-4 text-zinc-500 transition-transform duration-200 ${ease} group-data-panel-open:rotate-180 motion-reduce:transition-none`}\n          >\n            <path d=\"m4.5 6.25 3.5 3.5 3.5-3.5\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.5\" strokeLinecap=\"round\" strokeLinejoin=\"round\" />\n          </svg>\n        </Collapsible.Trigger>\n        {/* In a narrow container the state wraps under the task so the name keeps its room. */}\n        <div className=\"pointer-events-none relative flex shrink-0 items-center gap-2.5 @max-md:basis-full @max-md:pl-9\">\n          {finished && durationMs != null ? (\n            <span className={`font-mono text-xs text-zinc-500 tabular-nums transition-opacity duration-300 ${ease} starting:opacity-0 motion-reduce:transition-none`}>\n              {formatDuration(durationMs)}\n            </span>\n          ) : null}\n          <AgentState status size=\"sm\" activity={activity} phase={meta.phase} label={meta.label ?? activityLabel} />\n        </div>\n      </div>\n      {status === \"error\" && error != null ? (\n        <p\n          className={`mt-1 mb-2 ml-11 rounded-lg border border-red-500/35 bg-red-500/[0.08] px-3 py-2 text-sm text-red-700 transition-[opacity,translate] duration-200 ${ease} starting:-translate-y-1 starting:opacity-0 motion-reduce:transition-none dark:text-red-300`}\n        >\n          {error}\n        </p>\n      ) : null}\n\n      <Collapsible.Panel\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        <div className=\"ml-5 border-l border-zinc-950/10 pt-1 pb-2 pl-6 @max-md:pl-3 dark:border-white/10\">\n          {calls ? (\n            <ol aria-label={`${name} tool calls`} className=\"grid gap-2\">\n              {toolCalls.map(({ id, ...call }, index) => (\n                <li key={id ?? index}>\n                  <ToolCallCard {...call} />\n                </li>\n              ))}\n            </ol>\n          ) : status === \"running\" || status === \"queued\" ? (\n            <p className=\"py-1 text-sm text-zinc-500\">No tool calls yet</p>\n          ) : null}\n          {children}\n          {result != null ? (\n            <div className={cx(calls || children ? \"mt-4\" : \"\", `transition-[opacity,translate] duration-200 ${ease} starting:translate-y-1 starting:opacity-0 motion-reduce:transition-none`)}>\n              <p className=\"mb-1 text-xs font-medium text-zinc-500\">Result</p>\n              <div className=\"text-sm leading-relaxed text-zinc-800 dark:text-zinc-200\">{result}</div>\n            </div>\n          ) : null}\n        </div>\n      </Collapsible.Panel>\n    </Collapsible.Root>\n  );\n}\n\nfunction formatDuration(ms: number): string {\n  if (ms < 1000) return `${Math.round(ms)} ms`;\n  if (ms < 60_000) return `${(ms / 1000).toFixed(1)} s`;\n  return `${Math.floor(ms / 60_000)}m ${Math.round((ms % 60_000) / 1000)}s`;\n}\n"
    }
  ],
  "example": {
    "content": "import { Subagent } from \"@/components/shaken/agent/subagent\";\n\nexport default function SubagentExample() {\n  return (\n    <div className=\"grid w-full max-w-xl gap-1\">\n      <Subagent\n        name=\"researcher\"\n        task=\"Find how the billing service rounds prorated charges\"\n        status=\"success\"\n        activity=\"searching\"\n        durationMs={14200}\n        defaultOpen\n        toolCalls={[\n          {\n            name: \"grep\",\n            status: \"success\",\n            summary: \"Search for “Math.round” in src/billing\",\n            input: { pattern: \"Math.round\", path: \"src/billing\" },\n            output: \"src/billing/proration.ts:61\\nsrc/billing/invoice.ts:132\",\n            durationMs: 180,\n          },\n          {\n            name: \"read_file\",\n            status: \"success\",\n            summary: \"Read src/billing/proration.ts\",\n            input: { path: \"src/billing/proration.ts\", lines: \"40-80\" },\n            durationMs: 95,\n          },\n        ]}\n        result={\n          <p>\n            Proration rounds each line with <code className=\"font-mono text-[13px]\">Math.round</code> before summing, so\n            two half cents can round down together. Rounding once after the sum fixes the failing test.\n          </p>\n        }\n      />\n      <Subagent\n        name=\"test-runner\"\n        task=\"Run the billing tests after the fix\"\n        status=\"running\"\n        activity=\"working\"\n        activityLabel=\"Running tests\"\n        toolCalls={[{ name: \"run_command\", status: \"running\", summary: \"pnpm vitest run src/billing\" }]}\n      />\n    </div>\n  );\n}\n"
  }
}
