{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "token-meter",
  "title": "Token meter",
  "description": "Context window usage bar whose segments (system, history, tools, response) spring to new widths, with rolling numbers, a legend, and amber then rose tones near and over the limit.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "https://goodui.dev/r/goodui-motion.json",
    "https://goodui.dev/r/locale.json",
    "https://goodui.dev/r/number-flow.json"
  ],
  "files": [
    {
      "path": "registry/components/ai/token-meter.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { motion, useReducedMotion } from \"motion/react\"\nimport { cn } from \"@/lib/utils\"\nimport { NumberFlow } from \"@/components/goodui/number-flow\"\nimport { useLabels } from \"@/lib/goodui-locale\"\nimport { spring as springs } from \"@/lib/goodui-motion\"\n\n/*\n * Context window usage: a bar that fills by segment (system prompt, history, tools, response…), rolling\n * numbers for used / limit and a legend. Turns amber near the limit and rose over it.\n */\n\nexport type TokenSegment = {\n  label: string\n  value: number\n  /** Any CSS color; defaults cycle through --chart-1..5. */\n  color?: string\n}\n\nexport type TokenMeterLabels = {\n  used: (used: string, max: string) => string\n  /** Suffix after the remaining count. */\n  left: string\n  warning: string\n  over: string\n}\nconst DEFAULT_LABELS: TokenMeterLabels = { used: (u, m) => `${u} of ${m} tokens used`, left: \"left\", warning: \"Close to the limit\", over: \"Over the limit\" }\n\nexport type TokenMeterProps = Omit<React.ComponentProps<\"div\">, \"children\"> & {\n  /** Tokens used, when there is one number. */\n  used?: number\n  /** Breakdown by part of the context; used = sum of the values. */\n  segments?: TokenSegment[]\n  /** The context window. */\n  max: number\n  label?: React.ReactNode\n  /** Fraction of max where the meter turns amber. */\n  warnAt?: number\n  /** Fraction of max where it turns rose. */\n  dangerAt?: number\n  /** Show the segment legend. */\n  showLegend?: boolean\n  /** Show the remaining count instead of used / max. */\n  showRemaining?: boolean\n  /** Compact (12.4K) or full (12,400) numbers. */\n  format?: \"compact\" | \"full\"\n  size?: \"sm\" | \"md\" | \"lg\"\n  locale?: string\n  /** Text for built-in controls and screen readers; pass translations here. */\n  labels?: Partial<TokenMeterLabels>\n}\n\nconst CHART = [\"var(--chart-1)\", \"var(--chart-2)\", \"var(--chart-3)\", \"var(--chart-4)\", \"var(--chart-5)\"]\nconst HEIGHT = { sm: \"h-1.5\", md: \"h-2.5\", lg: \"h-4\" }\n\n/** Context window usage bar with segments, rolling numbers and a legend. */\nexport function TokenMeter({\n  used: usedProp,\n  segments,\n  max,\n  label,\n  warnAt = 0.8,\n  dangerAt = 0.95,\n  showLegend = true,\n  showRemaining = false,\n  format = \"compact\",\n  size = \"md\",\n  locale = \"en-US\",\n  labels,\n  className,\n  ...props\n}: TokenMeterProps) {\n  const l = useLabels(\"token-meter\", DEFAULT_LABELS, labels)\n  const reduce = useReducedMotion() ?? false\n  const parts = segments ?? []\n  const used = usedProp ?? parts.reduce((s, p) => s + p.value, 0)\n  const ratio = max > 0 ? used / max : 0\n  const state = ratio > 1 ? \"over\" : ratio >= dangerAt ? \"danger\" : ratio >= warnAt ? \"warning\" : \"ok\"\n  const fmtOpts = React.useMemo<Intl.NumberFormatOptions>(() => (format === \"compact\" ? { notation: \"compact\", maximumFractionDigits: 1 } : {}), [format])\n  const formatter = React.useMemo(() => new Intl.NumberFormat(locale, fmtOpts), [locale, fmtOpts])\n  const spring = reduce ? { duration: 0 } : springs.morph\n  const stateTone = state === \"over\" || state === \"danger\" ? \"text-rose-700 dark:text-rose-400\" : state === \"warning\" ? \"text-amber-700 dark:text-amber-400\" : \"text-foreground\"\n  const fill = state === \"over\" || state === \"danger\" ? \"oklch(0.6 0.2 25)\" : state === \"warning\" ? \"oklch(0.72 0.16 70)\" : \"var(--foreground)\"\n\n  // Each segment is a flex item sized by its tokens; motion's layout animation springs them to new widths.\n  const shownParts = parts.length ? parts : [{ label: \"\", value: used, color: fill }]\n  const free = Math.max(0, max - Math.min(used, max))\n\n  return (\n    <div data-state={state} className={cn(\"pc-token-meter flex flex-col gap-1.5\", className)} {...props}>\n      <div className=\"flex items-baseline justify-between gap-3 text-sm\">\n        {label !== undefined && <span className=\"min-w-0 truncate font-medium\">{label}</span>}\n        <span className={cn(\"ms-auto inline-flex items-baseline gap-1 text-xs tabular-nums\", stateTone)} aria-hidden=\"true\">\n          {showRemaining ? (\n            <>\n              <NumberFlow value={Math.max(0, max - used)} locale={locale} format={fmtOpts} className=\"text-sm font-semibold\" />\n              <span className=\"text-muted-foreground\">{l.left}</span>\n            </>\n          ) : (\n            <>\n              <NumberFlow value={used} locale={locale} format={fmtOpts} className=\"text-sm font-semibold\" />\n              <span className=\"text-muted-foreground\">/ {formatter.format(max)}</span>\n            </>\n          )}\n        </span>\n      </div>\n      <div\n        role=\"meter\"\n        aria-label={typeof label === \"string\" ? label : l.used(formatter.format(used), formatter.format(max))}\n        aria-valuemin={0}\n        aria-valuemax={max}\n        aria-valuenow={Math.min(used, max)}\n        aria-valuetext={`${l.used(formatter.format(used), formatter.format(max))}${state === \"over\" ? `. ${l.over}` : state !== \"ok\" ? `. ${l.warning}` : \"\"}`}\n        className={cn(\"flex w-full overflow-hidden rounded-full bg-muted\", HEIGHT[size])}\n      >\n        {shownParts.map((p, i) => (\n          <motion.div\n            key={p.label || i}\n            layout\n            transition={spring}\n            className=\"h-full min-w-0 first:rounded-s-full last:rounded-e-full\"\n            style={{ flex: `${Math.max(0, p.value)} 0 0px`, background: p.color ?? CHART[i % CHART.length], marginInlineEnd: i < shownParts.length - 1 && size !== \"sm\" ? 2 : 0 }}\n          />\n        ))}\n        <motion.div layout transition={spring} className=\"h-full\" style={{ flex: `${free} 0 0px` }} />\n      </div>\n      {showLegend && parts.length > 0 && (\n        <ul className=\"flex flex-wrap gap-x-3 gap-y-1 text-xs text-muted-foreground\">\n          {parts.map((p, i) => (\n            <li key={p.label} className=\"inline-flex items-center gap-1.5\">\n              <span className=\"size-2 rounded-full\" style={{ background: p.color ?? CHART[i % CHART.length] }} aria-hidden=\"true\" />\n              <span>{p.label}</span>\n              <span className=\"text-foreground tabular-nums\">{formatter.format(p.value)}</span>\n            </li>\n          ))}\n        </ul>\n      )}\n    </div>\n  )\n}\n",
      "type": "registry:component",
      "target": "components/goodui/token-meter.tsx"
    }
  ],
  "categories": [
    "components",
    "ai"
  ],
  "type": "registry:component"
}
