{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "ai-context",
  "type": "registry:item",
  "title": "Ai Context",
  "description": "A ai-context item.",
  "dependencies": [
    "@mui/material",
    "ai",
    "tokenlens",
    "@emotion/react",
    "@emotion/styled"
  ],
  "registryDependencies": [],
  "files": [
    {
      "path": "components/ai-context/ai-context.tsx",
      "target": "src/mui-treasury/components/ai-context/ai-context.tsx",
      "content": "'use client';\n\nimport {\n  type ComponentProps,\n  type ReactNode,\n  createContext,\n  useContext,\n} from 'react';\n\nimport Box from '@mui/material/Box';\nimport Button from '@mui/material/Button';\nimport LinearProgress from '@mui/material/LinearProgress';\nimport Typography from '@mui/material/Typography';\nimport type { LanguageModelUsage } from 'ai';\nimport { type ModelId, estimateCost } from 'tokenlens';\n\nconst PERCENT_MAX = 100;\nconst ICON_RADIUS = 10;\nconst ICON_VIEWBOX = 24;\nconst ICON_CENTER = 12;\nconst ICON_STROKE_WIDTH = 2;\n\ntype ContextSchema = {\n  usedTokens: number;\n  maxTokens: number;\n  usage?: LanguageModelUsage;\n  modelId?: ModelId;\n};\n\nconst ContextContext = createContext<ContextSchema | null>(null);\n\nconst useContextValue = () => {\n  const context = useContext(ContextContext);\n\n  if (!context) {\n    throw new Error('Context components must be used within Context');\n  }\n\n  return context;\n};\n\nexport type ContextProps = {\n  children: ReactNode;\n} & ContextSchema;\n\nexport const Context = ({\n  usedTokens,\n  maxTokens,\n  usage,\n  modelId,\n  children,\n}: ContextProps) => (\n  <ContextContext.Provider\n    value={{\n      usedTokens,\n      maxTokens,\n      usage,\n      modelId,\n    }}\n  >\n    {children}\n  </ContextContext.Provider>\n);\n\nconst ContextIcon = () => {\n  const { usedTokens, maxTokens } = useContextValue();\n  const circumference = 2 * Math.PI * ICON_RADIUS;\n  const usedPercent = usedTokens / maxTokens;\n  const dashOffset = circumference * (1 - usedPercent);\n\n  return (\n    <svg\n      aria-label=\"Model context usage\"\n      height=\"20\"\n      role=\"img\"\n      style={{ color: 'currentcolor' }}\n      viewBox={`0 0 ${ICON_VIEWBOX} ${ICON_VIEWBOX}`}\n      width=\"20\"\n    >\n      <circle\n        cx={ICON_CENTER}\n        cy={ICON_CENTER}\n        fill=\"none\"\n        opacity=\"0.25\"\n        r={ICON_RADIUS}\n        stroke=\"currentColor\"\n        strokeWidth={ICON_STROKE_WIDTH}\n      />\n      <circle\n        cx={ICON_CENTER}\n        cy={ICON_CENTER}\n        fill=\"none\"\n        opacity=\"0.7\"\n        r={ICON_RADIUS}\n        stroke=\"currentColor\"\n        strokeDasharray={`${circumference} ${circumference}`}\n        strokeDashoffset={dashOffset}\n        strokeLinecap=\"round\"\n        strokeWidth={ICON_STROKE_WIDTH}\n        style={{ transformOrigin: 'center', transform: 'rotate(-90deg)' }}\n      />\n    </svg>\n  );\n};\n\nexport type ContextTriggerProps = ComponentProps<typeof Button> & {\n  onOpen?: (event: React.MouseEvent<HTMLElement>) => void;\n};\n\nexport const ContextTrigger = ({\n  children,\n  onOpen,\n  sx,\n  ...props\n}: ContextTriggerProps) => {\n  const { usedTokens, maxTokens } = useContextValue();\n  const usedPercent = usedTokens / maxTokens;\n  const renderedPercent = new Intl.NumberFormat('en-US', {\n    style: 'percent',\n    maximumFractionDigits: 1,\n  }).format(usedPercent);\n\n  return (\n    children ?? (\n      <Button\n        type=\"button\"\n        variant=\"text\"\n        onClick={onOpen}\n        sx={{\n          minWidth: 'auto',\n          p: 1,\n          color: 'text.secondary',\n          '&:hover': {\n            bgcolor: 'action.hover',\n          },\n          ...sx,\n        }}\n        {...props}\n      >\n        <Typography\n          component=\"span\"\n          sx={{ fontWeight: 500, mr: 1, color: 'text.secondary' }}\n        >\n          {renderedPercent}\n        </Typography>\n        <ContextIcon />\n      </Button>\n    )\n  );\n};\n\nexport type ContextContentProps = ComponentProps<typeof Box>;\n\nexport const ContextContent = ({\n  sx,\n  children,\n  ...props\n}: ContextContentProps) => (\n  <Box\n    sx={{\n      minWidth: 240,\n      '& > *:not(:last-child)': {\n        borderBottom: 1,\n        borderColor: 'divider',\n      },\n      ...sx,\n    }}\n    {...props}\n  >\n    {children}\n  </Box>\n);\n\nexport type ContextContentHeaderProps = ComponentProps<typeof Box>;\n\nexport const ContextContentHeader = ({\n  children,\n  sx,\n  ...props\n}: ContextContentHeaderProps) => {\n  const { usedTokens, maxTokens } = useContextValue();\n  const usedPercent = usedTokens / maxTokens;\n  const displayPct = new Intl.NumberFormat('en-US', {\n    style: 'percent',\n    maximumFractionDigits: 1,\n  }).format(usedPercent);\n  const used = new Intl.NumberFormat('en-US', {\n    notation: 'compact',\n  }).format(usedTokens);\n  const total = new Intl.NumberFormat('en-US', {\n    notation: 'compact',\n  }).format(maxTokens);\n\n  return (\n    <Box sx={{ width: '100%', p: 1.5, ...sx }} {...props}>\n      {children ?? (\n        <>\n          <Box\n            sx={{\n              display: 'flex',\n              alignItems: 'center',\n              justifyContent: 'space-between',\n              gap: 1.5,\n              mb: 1,\n            }}\n          >\n            <Typography variant=\"caption\">{displayPct}</Typography>\n            <Typography\n              variant=\"caption\"\n              sx={{ fontFamily: 'monospace', color: 'text.secondary' }}\n            >\n              {used} / {total}\n            </Typography>\n          </Box>\n          <LinearProgress\n            variant=\"determinate\"\n            value={usedPercent * PERCENT_MAX}\n            sx={{ height: 6, borderRadius: 1, bgcolor: 'action.hover' }}\n          />\n        </>\n      )}\n    </Box>\n  );\n};\n\nexport type ContextContentBodyProps = ComponentProps<typeof Box>;\n\nexport const ContextContentBody = ({\n  children,\n  sx,\n  ...props\n}: ContextContentBodyProps) => (\n  <Box sx={{ width: '100%', p: 1.5, ...sx }} {...props}>\n    {children}\n  </Box>\n);\n\nexport type ContextContentFooterProps = ComponentProps<typeof Box>;\n\nexport const ContextContentFooter = ({\n  children,\n  sx,\n  ...props\n}: ContextContentFooterProps) => {\n  const { modelId, usage } = useContextValue();\n  const costUSD = modelId\n    ? estimateCost({\n        modelId,\n        usage: {\n          input: usage?.inputTokens ?? 0,\n          output: usage?.outputTokens ?? 0,\n        },\n      }).totalUSD\n    : undefined;\n  const totalCost = new Intl.NumberFormat('en-US', {\n    style: 'currency',\n    currency: 'USD',\n  }).format(costUSD ?? 0);\n\n  return (\n    <Box\n      sx={{\n        display: 'flex',\n        width: '100%',\n        alignItems: 'center',\n        justifyContent: 'space-between',\n        gap: 1.5,\n        bgcolor: 'action.hover',\n        p: 1.5,\n        ...sx,\n      }}\n      {...props}\n    >\n      {children ?? (\n        <>\n          <Typography variant=\"caption\" sx={{ color: 'text.secondary' }}>\n            Total cost\n          </Typography>\n          <Typography variant=\"caption\">{totalCost}</Typography>\n        </>\n      )}\n    </Box>\n  );\n};\n\nexport type ContextInputUsageProps = ComponentProps<typeof Box>;\n\nexport const ContextInputUsage = ({\n  sx,\n  children,\n  ...props\n}: ContextInputUsageProps) => {\n  const { usage, modelId } = useContextValue();\n  const inputTokens = usage?.inputTokens ?? 0;\n\n  if (children) {\n    return <>{children}</>;\n  }\n\n  if (!inputTokens) {\n    return null;\n  }\n\n  const inputCost = modelId\n    ? estimateCost({\n        modelId,\n        usage: { input: inputTokens, output: 0 },\n      }).totalUSD\n    : undefined;\n  const inputCostText = new Intl.NumberFormat('en-US', {\n    style: 'currency',\n    currency: 'USD',\n  }).format(inputCost ?? 0);\n\n  return (\n    <Box\n      sx={{\n        display: 'flex',\n        alignItems: 'center',\n        justifyContent: 'space-between',\n        ...sx,\n      }}\n      {...props}\n    >\n      <Typography variant=\"caption\" sx={{ color: 'text.secondary' }}>\n        Input\n      </Typography>\n      <TokensWithCost costText={inputCostText} tokens={inputTokens} />\n    </Box>\n  );\n};\n\nexport type ContextOutputUsageProps = ComponentProps<typeof Box>;\n\nexport const ContextOutputUsage = ({\n  sx,\n  children,\n  ...props\n}: ContextOutputUsageProps) => {\n  const { usage, modelId } = useContextValue();\n  const outputTokens = usage?.outputTokens ?? 0;\n\n  if (children) {\n    return <>{children}</>;\n  }\n\n  if (!outputTokens) {\n    return null;\n  }\n\n  const outputCost = modelId\n    ? estimateCost({\n        modelId,\n        usage: { input: 0, output: outputTokens },\n      }).totalUSD\n    : undefined;\n  const outputCostText = new Intl.NumberFormat('en-US', {\n    style: 'currency',\n    currency: 'USD',\n  }).format(outputCost ?? 0);\n\n  return (\n    <Box\n      sx={{\n        display: 'flex',\n        alignItems: 'center',\n        justifyContent: 'space-between',\n        ...sx,\n      }}\n      {...props}\n    >\n      <Typography variant=\"caption\" sx={{ color: 'text.secondary' }}>\n        Output\n      </Typography>\n      <TokensWithCost costText={outputCostText} tokens={outputTokens} />\n    </Box>\n  );\n};\n\nexport type ContextReasoningUsageProps = ComponentProps<typeof Box>;\n\nexport const ContextReasoningUsage = ({\n  sx,\n  children,\n  ...props\n}: ContextReasoningUsageProps) => {\n  const { usage, modelId } = useContextValue();\n  const reasoningTokens = usage?.reasoningTokens ?? 0;\n\n  if (children) {\n    return <>{children}</>;\n  }\n\n  if (!reasoningTokens) {\n    return null;\n  }\n\n  const reasoningCost = modelId\n    ? estimateCost({\n        modelId,\n        usage: { reasoningTokens },\n      }).totalUSD\n    : undefined;\n  const reasoningCostText = new Intl.NumberFormat('en-US', {\n    style: 'currency',\n    currency: 'USD',\n  }).format(reasoningCost ?? 0);\n\n  return (\n    <Box\n      sx={{\n        display: 'flex',\n        alignItems: 'center',\n        justifyContent: 'space-between',\n        ...sx,\n      }}\n      {...props}\n    >\n      <Typography variant=\"caption\" sx={{ color: 'text.secondary' }}>\n        Reasoning\n      </Typography>\n      <TokensWithCost costText={reasoningCostText} tokens={reasoningTokens} />\n    </Box>\n  );\n};\n\nexport type ContextCacheUsageProps = ComponentProps<typeof Box>;\n\nexport const ContextCacheUsage = ({\n  sx,\n  children,\n  ...props\n}: ContextCacheUsageProps) => {\n  const { usage, modelId } = useContextValue();\n  const cacheTokens = usage?.cachedInputTokens ?? 0;\n\n  if (children) {\n    return <>{children}</>;\n  }\n\n  if (!cacheTokens) {\n    return null;\n  }\n\n  const cacheCost = modelId\n    ? estimateCost({\n        modelId,\n        usage: { cacheReads: cacheTokens, input: 0, output: 0 },\n      }).totalUSD\n    : undefined;\n  const cacheCostText = new Intl.NumberFormat('en-US', {\n    style: 'currency',\n    currency: 'USD',\n  }).format(cacheCost ?? 0);\n\n  return (\n    <Box\n      sx={{\n        display: 'flex',\n        alignItems: 'center',\n        justifyContent: 'space-between',\n        ...sx,\n      }}\n      {...props}\n    >\n      <Typography variant=\"caption\" sx={{ color: 'text.secondary' }}>\n        Cache\n      </Typography>\n      <TokensWithCost costText={cacheCostText} tokens={cacheTokens} />\n    </Box>\n  );\n};\n\nconst TokensWithCost = ({\n  tokens,\n  costText,\n}: {\n  tokens?: number;\n  costText?: string;\n}) => (\n  <Typography component=\"span\" variant=\"caption\">\n    {tokens === undefined\n      ? '—'\n      : new Intl.NumberFormat('en-US', {\n          notation: 'compact',\n        }).format(tokens)}\n    {costText ? (\n      <Typography\n        component=\"span\"\n        variant=\"caption\"\n        sx={{ ml: 1, color: 'text.secondary' }}\n      >\n        • {costText}\n      </Typography>\n    ) : null}\n  </Typography>\n);\n",
      "type": "registry:item"
    },
    {
      "path": "components/ai-context/index.ts",
      "target": "src/mui-treasury/components/ai-context/index.ts",
      "content": "export * from './ai-context';\n",
      "type": "registry:item"
    }
  ],
  "meta": {
    "category": "ai",
    "subcategory": "elements"
  }
}