{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "model-failover-path",
  "title": "Model Failover Path",
  "description": "Show which AI model is answering and what happens when one fails: requests move down a chain of backups, with speed and cost changing as they do.",
  "dependencies": [],
  "files": [
    {
      "path": "registry/new-york/model-failover-path/model-failover-path.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface FailoverModel {\n  id: string;\n  name: string;\n  /** Typical reply time, in milliseconds. */\n  latency: number;\n  /** Dollars per thousand requests. */\n  cost: number;\n  /** Starts unavailable. */\n  down?: boolean;\n}\n\nexport interface ModelFailoverPathProps extends Omit<\n  React.ComponentPropsWithoutRef<\"section\">,\n  \"title\" | \"onChange\"\n> {\n  theme?: \"dark\" | \"light\";\n  title?: string;\n  /** When it moves on, in plain words. */\n  rule?: string;\n  /** In the order they're tried. */\n  models?: FailoverModel[];\n  onChange?: (active: FailoverModel | null, down: string[]) => void;\n}\n\nconst FONT =\n  '\"Inter Variable\", Inter, \"SF Pro Display\", -apple-system, BlinkMacSystemFont, sans-serif';\nconst ACCENT = \"#5E6AD2\";\nconst GREEN = \"#27A644\";\nconst ORANGE = \"#FC7840\";\nconst RED = \"#EB5757\";\nconst H = 112;\nconst LINE_Y = 40;\nconst R = 19;\nconst LOG_ROWS = 3;\n\nconst PALETTES = {\n  dark: {\n    card: \"#08090A\",\n    node: \"#161718\",\n    border: \"rgba(255,255,255,0.08)\",\n    wire: \"rgba(255,255,255,0.12)\",\n    text: \"#F7F8F8\",\n    soft: \"#D0D6E0\",\n    sub: \"#8A8F98\",\n    faint: \"#62666D\",\n    chip: \"rgba(255,255,255,0.05)\",\n  },\n  light: {\n    card: \"#FFFFFF\",\n    node: \"#F7F8F8\",\n    border: \"rgba(8,9,10,0.08)\",\n    wire: \"rgba(8,9,10,0.14)\",\n    text: \"#08090A\",\n    soft: \"#3C3F44\",\n    sub: \"#62666D\",\n    faint: \"#8A8F98\",\n    chip: \"rgba(8,9,10,0.04)\",\n  },\n};\n\nconst DEFAULT_MODELS: FailoverModel[] = [\n  { id: \"large\", name: \"Large\", latency: 840, cost: 12 },\n  { id: \"medium\", name: \"Medium\", latency: 410, cost: 4.5 },\n  { id: \"small\", name: \"Small\", latency: 190, cost: 0.8 },\n  { id: \"local\", name: \"On device\", latency: 95, cost: 0 },\n];\n\nfunction useReducedMotion() {\n  return React.useSyncExternalStore(\n    (callback) => {\n      const query = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n      query.addEventListener(\"change\", callback);\n      return () => query.removeEventListener(\"change\", callback);\n    },\n    () => window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches,\n    () => false,\n  );\n}\n\nfunction useWidth<T extends HTMLElement>(fallback: number) {\n  const ref = React.useRef<T>(null);\n  const [width, setWidth] = React.useState(fallback);\n  React.useEffect(() => {\n    const el = ref.current;\n    if (!el) return;\n    const ro = new ResizeObserver(([entry]) => {\n      setWidth(Math.round(entry.contentRect.width));\n    });\n    ro.observe(el);\n    return () => ro.disconnect();\n  }, []);\n  return [ref, width] as const;\n}\n\n/** Drawn as SVG, because a tiny border-radius circle turns lumpy on 1x screens. */\nfunction Dot({ size, color }: { size: number; color: string }) {\n  const r = size / 2;\n  return (\n    <svg\n      aria-hidden=\"true\"\n      width={size}\n      height={size}\n      viewBox={`0 0 ${size} ${size}`}\n      className=\"block shrink-0\"\n    >\n      <circle\n        cx={r}\n        cy={r}\n        r={r}\n        className=\"motion-safe:transition-[fill] motion-safe:duration-200\"\n        style={{ fill: color }}\n      />\n    </svg>\n  );\n}\n\nexport function ModelFailoverPath({\n  theme = \"dark\",\n  title = \"Support replies\",\n  rule = \"Moves on after two failed tries or eight seconds.\",\n  models = DEFAULT_MODELS,\n  onChange,\n  className,\n  style,\n  ...rest\n}: ModelFailoverPathProps) {\n  const c = PALETTES[theme];\n  const reduce = useReducedMotion();\n  const [down, setDown] = React.useState(() =>\n    models.filter((m) => m.down).map((m) => m.id),\n  );\n  const [log, setLog] = React.useState<{ key: number; text: string }[]>([\n    { key: 0, text: \"All models healthy\" },\n    { key: -1, text: \"Medium recovered at 09:41\" },\n    { key: -2, text: \"Medium timed out at 09:36\" },\n  ]);\n  const [wrapRef, width] = useWidth<HTMLDivElement>(700);\n  const svgRef = React.useRef<SVGSVGElement>(null);\n  const logKey = React.useRef(1);\n\n  const active = models.find((m) => !down.includes(m.id)) ?? null;\n  const activeIndex = active ? models.indexOf(active) : -1;\n  const startX = 26;\n  const first = width < 520 ? 84 : 120;\n  const x = (i: number) =>\n    Math.round(\n      first +\n        (models.length > 1 ? i / (models.length - 1) : 0) *\n          (width - first - 40),\n    );\n  const reachX = active ? x(activeIndex) : startX;\n  const path = `M${startX} ${LINE_Y} L${reachX} ${LINE_Y}`;\n\n  React.useEffect(() => {\n    if (reduce || !active) return;\n    const el = svgRef.current?.querySelector(\"[data-packet]\");\n    const run = el?.animate(\n      [{ strokeDashoffset: 1.08 }, { strokeDashoffset: 0 }],\n      { duration: 1000, iterations: Infinity, easing: \"linear\" },\n    );\n    return () => run?.cancel();\n  }, [active, reduce]);\n\n  const toggle = (model: FailoverModel) => {\n    const isDown = down.includes(model.id);\n    const next = isDown\n      ? down.filter((id) => id !== model.id)\n      : [...down, model.id];\n    setDown(next);\n    const nowActive = models.find((m) => !next.includes(m.id)) ?? null;\n    logKey.current += 1;\n    const text = !nowActive\n      ? `${model.name} went down. Nothing left to try`\n      : nowActive.id !== active?.id\n        ? isDown\n          ? `${model.name} is back and taking traffic`\n          : `${model.name} went down. Moved to ${nowActive.name}`\n        : isDown\n          ? `${model.name} is back as a backup`\n          : `${model.name} went down. Still on ${nowActive.name}`;\n    const key = logKey.current;\n    setLog((prev) => [{ key, text }, ...prev].slice(0, LOG_ROWS));\n    onChange?.(nowActive, next);\n  };\n\n  return (\n    <section\n      aria-label={title}\n      className={`@container w-full rounded-2xl border ${className ?? \"\"}`}\n      style={{\n        background: c.card,\n        borderColor: c.border,\n        color: c.text,\n        fontFamily: FONT,\n        ...style,\n      }}\n      {...rest}\n    >\n      <div className=\"p-5 pb-3 @xl:p-6 @xl:pb-3\">\n        <div className=\"flex items-start justify-between gap-4\">\n          <header className=\"min-w-0\">\n            <h3 className=\"m-0 text-[20px] leading-[26px] font-[590] tracking-[-0.012em]\">\n              {title}\n            </h3>\n            <p\n              aria-live=\"polite\"\n              className=\"m-0 mt-1 flex items-center gap-2 text-[13px] leading-[20px] tracking-[-0.011em]\"\n              style={{ color: c.sub }}\n            >\n              <Dot\n                size={8}\n                color={!active ? RED : activeIndex === 0 ? GREEN : ORANGE}\n              />\n              <span className=\"truncate\">\n                {!active\n                  ? \"No model available\"\n                  : activeIndex === 0\n                    ? `Running on ${active.name}`\n                    : `Failed over to ${active.name}`}\n              </span>\n            </p>\n          </header>\n          <div className=\"shrink-0 text-right\">\n            <p className=\"m-0 text-[32px] leading-[32px] font-[510] tracking-[-0.022em] tabular-nums\">\n              {active ? `${active.latency} ms` : \"None\"}\n            </p>\n            <p\n              className=\"m-0 mt-1.5 text-[13px] leading-[20px] tabular-nums\"\n              style={{ color: c.sub }}\n            >\n              {active\n                ? `$${active.cost.toFixed(2)} per 1k requests`\n                : \"Requests are failing\"}\n            </p>\n          </div>\n        </div>\n\n        <div ref={wrapRef} className=\"mt-3 w-full\" style={{ height: H }}>\n          {/* Everything here is SVG with explicit coordinates, so it can't fall out of line. */}\n          <svg\n            ref={svgRef}\n            role=\"group\"\n            aria-label=\"Failover order\"\n            width={width}\n            height={H}\n            className=\"block\"\n          >\n            <line\n              x1={startX}\n              x2={x(models.length - 1)}\n              y1={LINE_Y}\n              y2={LINE_Y}\n              stroke={c.wire}\n              strokeWidth=\"1.5\"\n            />\n            <path\n              d={path}\n              fill=\"none\"\n              stroke={active ? ACCENT : c.wire}\n              strokeWidth=\"1.5\"\n            />\n            {active && (\n              <path\n                data-packet=\"\"\n                d={path}\n                fill=\"none\"\n                pathLength={1}\n                stroke={c.text}\n                strokeWidth=\"2.5\"\n                strokeLinecap=\"round\"\n                strokeDasharray=\"0.05 1.03\"\n                strokeDashoffset=\"1.08\"\n                opacity={reduce ? 0 : 1}\n              />\n            )}\n            <circle cx={startX} cy={LINE_Y} r=\"5\" fill={c.sub} />\n            <text\n              x={startX}\n              y={LINE_Y + 26}\n              textAnchor=\"middle\"\n              fontSize=\"12\"\n              fill={c.sub}\n            >\n              Request\n            </text>\n            {models.map((m, i) => {\n              const off = down.includes(m.id);\n              const on = active?.id === m.id;\n              const color = off ? RED : on ? ACCENT : c.faint;\n              return (\n                <g\n                  key={m.id}\n                  data-model={m.id}\n                  role=\"button\"\n                  tabIndex={0}\n                  aria-pressed={off}\n                  aria-label={`${m.name}, ${off ? \"down\" : \"up\"}. Press to change.`}\n                  onClick={() => toggle(m)}\n                  onKeyDown={(e) => {\n                    if (e.key === \"Enter\" || e.key === \" \") {\n                      e.preventDefault();\n                      toggle(m);\n                    }\n                  }}\n                  className=\"cursor-pointer outline-none [&:focus-visible>circle]:[stroke-width:2] [&:focus-visible>circle]:[stroke:#5E6AD2]\"\n                >\n                  <circle\n                    cx={x(i)}\n                    cy={LINE_Y}\n                    r={R}\n                    fill={c.node}\n                    stroke={off || on ? color : c.border}\n                    strokeWidth={off || on ? 1.5 : 1}\n                    className=\"motion-safe:transition-[stroke] motion-safe:duration-200\"\n                  />\n                  {off ? (\n                    <path\n                      d={`M${x(i) - 5} ${LINE_Y - 5} l10 10 m0 -10 l-10 10`}\n                      stroke={RED}\n                      strokeWidth=\"1.75\"\n                      strokeLinecap=\"round\"\n                      fill=\"none\"\n                    />\n                  ) : (\n                    <text\n                      x={x(i)}\n                      y={LINE_Y + 5}\n                      textAnchor=\"middle\"\n                      fontSize=\"13\"\n                      fontWeight=\"590\"\n                      fill={on ? c.text : c.sub}\n                    >\n                      {i + 1}\n                    </text>\n                  )}\n                  <text\n                    x={x(i)}\n                    y={LINE_Y + R + 18}\n                    textAnchor=\"middle\"\n                    fontSize=\"12.5\"\n                    fontWeight=\"510\"\n                    fill={on ? c.text : c.sub}\n                  >\n                    {m.name}\n                  </text>\n                  <text\n                    x={x(i)}\n                    y={LINE_Y + R + 33}\n                    textAnchor=\"middle\"\n                    fontSize=\"11\"\n                    fill={off ? RED : c.faint}\n                    style={{ fontVariantNumeric: \"tabular-nums\" }}\n                  >\n                    {off ? \"Down\" : `${m.latency} ms`}\n                  </text>\n                </g>\n              );\n            })}\n          </svg>\n        </div>\n      </div>\n\n      <div\n        className=\"border-t px-5 py-4 @xl:px-6\"\n        style={{ borderColor: c.border }}\n      >\n        <p\n          className=\"m-0 text-[12px] leading-[16px] tracking-[-0.005em]\"\n          style={{ color: c.faint }}\n        >\n          {rule} Click a model to take it down.\n        </p>\n        <ol\n          aria-live=\"polite\"\n          className=\"m-0 mt-2 list-none p-0\"\n          style={{ height: LOG_ROWS * 24 }}\n        >\n          {log.map((entry, i) => (\n            <li\n              key={entry.key}\n              className=\"truncate text-[13px] leading-[24px] tracking-[-0.011em]\"\n              style={{ color: i ? c.sub : c.soft }}\n            >\n              {entry.text}\n            </li>\n          ))}\n        </ol>\n      </div>\n    </section>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}