{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "ai-branch",
  "type": "registry:ui",
  "description": "AI conversation branch component for displaying branched conversations",
  "files": [
    {
      "path": "packages/ai/branch.tsx",
      "content": "'use client';\n\nimport { ChevronLeft, ChevronRight } from 'lucide-react';\nimport type { HTMLAttributes, ReactElement, ReactNode } from 'react';\nimport { createContext, useContext, useEffect, useState } from 'react';\nimport { Button } from '@repo/shadcn-ui/components/ui/button';\nimport { cn } from '@repo/shadcn-ui/lib/utils';\n\ntype AIBranchContextType = {\n  currentBranch: number;\n  totalBranches: number;\n  goToPrevious: () => void;\n  goToNext: () => void;\n  branches: ReactElement[];\n  setBranches: (branches: ReactElement[]) => void;\n};\n\nconst AIBranchContext = createContext<AIBranchContextType | null>(null);\n\nconst useAIBranch = () => {\n  const context = useContext(AIBranchContext);\n\n  if (!context) {\n    throw new Error('AIBranch components must be used within AIBranch');\n  }\n\n  return context;\n};\n\nexport type AIBranchProps = HTMLAttributes<HTMLDivElement> & {\n  defaultBranch?: number;\n  onBranchChange?: (branchIndex: number) => void;\n};\n\nexport const AIBranch = ({\n  defaultBranch = 0,\n  onBranchChange,\n  className,\n  ...props\n}: AIBranchProps) => {\n  const [currentBranch, setCurrentBranch] = useState(defaultBranch);\n  const [branches, setBranches] = useState<ReactElement[]>([]);\n\n  const handleBranchChange = (newBranch: number) => {\n    setCurrentBranch(newBranch);\n    onBranchChange?.(newBranch);\n  };\n\n  const goToPrevious = () => {\n    const newBranch =\n      currentBranch > 0 ? currentBranch - 1 : branches.length - 1;\n    handleBranchChange(newBranch);\n  };\n\n  const goToNext = () => {\n    const newBranch =\n      currentBranch < branches.length - 1 ? currentBranch + 1 : 0;\n    handleBranchChange(newBranch);\n  };\n\n  const contextValue: AIBranchContextType = {\n    currentBranch,\n    totalBranches: branches.length,\n    goToPrevious,\n    goToNext,\n    branches,\n    setBranches,\n  };\n\n  return (\n    <AIBranchContext.Provider value={contextValue}>\n      <div\n        className={cn('grid w-full gap-2 [&>div]:pb-0', className)}\n        {...props}\n      />\n    </AIBranchContext.Provider>\n  );\n};\n\nexport type AIBranchMessagesProps = {\n  children: ReactElement | ReactElement[];\n};\n\nexport const AIBranchMessages = ({ children }: AIBranchMessagesProps) => {\n  const { currentBranch, setBranches, branches } = useAIBranch();\n  const childrenArray = Array.isArray(children) ? children : [children];\n\n  // Use useEffect to update branches when they change\n  useEffect(() => {\n    if (branches.length !== childrenArray.length) {\n      setBranches(childrenArray);\n    }\n  }, [childrenArray, branches, setBranches]);\n\n  return childrenArray.map((branch, index) => (\n    <div\n      className={cn(\n        'grid gap-2 [&>div]:pb-0',\n        index === currentBranch ? 'block' : 'hidden'\n      )}\n      key={index}\n    >\n      {branch}\n    </div>\n  ));\n};\n\nexport type AIBranchSelectorProps = HTMLAttributes<HTMLDivElement> & {\n  from: 'user' | 'assistant';\n};\n\nexport const AIBranchSelector = ({\n  className,\n  from,\n  ...props\n}: AIBranchSelectorProps) => {\n  const { totalBranches } = useAIBranch();\n\n  // Don't render if there's only one branch\n  if (totalBranches <= 1) {\n    return null;\n  }\n\n  return (\n    <div\n      className={cn(\n        'flex items-center gap-2 self-end px-10',\n        from === 'assistant' ? 'justify-start' : 'justify-end',\n        className\n      )}\n      {...props}\n    />\n  );\n};\n\nexport type AIBranchPreviousProps = {\n  className?: string;\n  children?: ReactNode;\n};\n\nexport const AIBranchPrevious = ({\n  className,\n  children,\n}: AIBranchPreviousProps) => {\n  const { goToPrevious, totalBranches } = useAIBranch();\n\n  return (\n    <Button\n      aria-label=\"Previous branch\"\n      className={cn(\n        'size-7 shrink-0 rounded-full text-muted-foreground transition-colors',\n        'hover:bg-accent hover:text-foreground',\n        'disabled:pointer-events-none disabled:opacity-50',\n        className\n      )}\n      disabled={totalBranches <= 1}\n      onClick={goToPrevious}\n      size=\"icon\"\n      type=\"button\"\n      variant=\"ghost\"\n    >\n      {children ?? <ChevronLeft size={14} />}\n    </Button>\n  );\n};\n\nexport type AIBranchNextProps = {\n  className?: string;\n  children?: ReactNode;\n};\n\nexport const AIBranchNext = ({ className, children }: AIBranchNextProps) => {\n  const { goToNext, totalBranches } = useAIBranch();\n\n  return (\n    <Button\n      aria-label=\"Next branch\"\n      className={cn(\n        'size-7 shrink-0 rounded-full text-muted-foreground transition-colors',\n        'hover:bg-accent hover:text-foreground',\n        'disabled:pointer-events-none disabled:opacity-50',\n        className\n      )}\n      disabled={totalBranches <= 1}\n      onClick={goToNext}\n      size=\"icon\"\n      type=\"button\"\n      variant=\"ghost\"\n    >\n      {children ?? <ChevronRight size={14} />}\n    </Button>\n  );\n};\n\nexport type AIBranchPageProps = {\n  className?: string;\n};\n\nexport const AIBranchPage = ({ className }: AIBranchPageProps) => {\n  const { currentBranch, totalBranches } = useAIBranch();\n\n  return (\n    <span\n      className={cn(\n        'font-medium text-muted-foreground text-xs tabular-nums',\n        className\n      )}\n    >\n      {currentBranch + 1} of {totalBranches}\n    </span>\n  );\n};\n",
      "type": "registry:component"
    }
  ]
}