{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "xray-lens-trace-demo",
  "type": "registry:block",
  "title": "Xray Lens: Request Trace",
  "description": "Hover Reveal Magnifier example, Request Trace: An AI search answer over the live trace that produced it: retrieval, generation and citation spans on the request's timeline, and the answer recolored by when each word streamed. `autoPlay` tours the spans on desktop too until the pointer takes over, and `rest={false}` keeps the lens closed before the tour starts. A take on the magnifier (magnifying glass). Fits features and docs sections.",
  "author": "KeyframeUI <https://keyframeui.com>",
  "categories": [
    "features",
    "docs",
    "magnifier",
    "image-reveal",
    "before-and-after",
    "hover",
    "interaction",
    "developer"
  ],
  "docs": "Live preview, examples and props: https://keyframeui.com/components/xray-lens#xray-lens-trace-demo",
  "registryDependencies": [
    "https://keyframeui.com/r/xray-lens.json"
  ],
  "files": [
    {
      "path": "components/xray-lens-trace-demo.tsx",
      "type": "registry:component",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nimport { XrayLens, XrayLensSpot } from \"@/components/ui/xray-lens\";\n\n// One trace, in the order it ran. Each span is drawn over the part of the answer it produced.\nconst TRACE = [\n  { id: \"embed\", ms: 18 },\n  { id: \"retrieve\", ms: 42 },\n  { id: \"rerank\", ms: 31 },\n  { id: \"generate\", ms: 812 },\n  { id: \"cite\", ms: 12 },\n  { id: \"suggest\", ms: 64 },\n] as const;\n\ntype SpanId = (typeof TRACE)[number][\"id\"];\ntype At = { start: number; ms: number };\ntype Timing = Record<SpanId, At>;\n\nconst BASE = Object.fromEntries(TRACE.map((s) => [s.id, s.ms])) as Record<SpanId, number>;\n\nfunction lay(durations: Record<SpanId, number>) {\n  let t = 0;\n  const timing = {} as Timing;\n  for (const { id } of TRACE) {\n    timing[id] = { start: t, ms: durations[id] };\n    t += durations[id];\n  }\n  return { timing, total: t };\n}\n\n/** The trace re-measured every couple of seconds, as a live dashboard would show it. */\nfunction useLiveTrace() {\n  const [durations, setDurations] = React.useState(BASE);\n  React.useEffect(() => {\n    const id = window.setInterval(() => {\n      setDurations(\n        Object.fromEntries(\n          TRACE.map((s) => [s.id, Math.round(s.ms * (0.88 + Math.random() * 0.24))]),\n        ) as Record<SpanId, number>,\n      );\n    }, 1600);\n    return () => window.clearInterval(id);\n  }, []);\n  return lay(durations);\n}\n\nconst spanBox = \"outline-1 -outline-offset-1 outline-emerald-400/40 bg-emerald-400/[0.06]\";\n\n/** Where a span sits on the whole request's timeline. */\nfunction Bar({ at, total }: { at: At; total: number }) {\n  return (\n    <span className=\"relative h-1 min-w-8 flex-1 rounded-full bg-white/10\">\n      <span\n        className=\"absolute inset-y-0 min-w-[3px] rounded-full bg-emerald-400 transition-[left,width] duration-500\"\n        style={{ left: `${(at.start / total) * 100}%`, width: `${(at.ms / total) * 100}%` }}\n      />\n    </span>\n  );\n}\n\n/** A span in a box: its name and time, its place on the timeline, and a note. */\nfunction Span({ name, at, total, note }: { name: string; at: At; total: number; note?: string }) {\n  return (\n    <span className=\"flex h-full min-w-0 flex-col justify-center gap-1.5 px-2.5 font-mono text-[10.5px] leading-none\">\n      <span className=\"flex items-center justify-between gap-3 whitespace-nowrap\">\n        <span className=\"text-emerald-300\">{name}</span>\n        <span className=\"text-zinc-100 tabular-nums\">{at.ms}ms</span>\n      </span>\n      <span className=\"flex\">\n        <Bar at={at} total={total} />\n      </span>\n      {note && <span className=\"truncate text-zinc-500\">{note}</span>}\n    </span>\n  );\n}\n\n/** A span on one line, for the section labels. */\nfunction SpanRow({ name, at, total }: { name: string; at: At; total: number }) {\n  return (\n    <span className=\"flex h-full items-center gap-2.5 font-mono text-[10px] leading-none whitespace-nowrap\">\n      <span className=\"text-emerald-300\">{name}</span>\n      <Bar at={at} total={total} />\n      <span className=\"text-zinc-100 tabular-nums\">{at.ms}ms</span>\n    </span>\n  );\n}\n\n// The answer as text and citations. The surface and the xray both render it from\n// here, so they wrap the same way.\nconst ANSWER: (string | number)[] = [\n  \"Create the new key and deploy it alongside the old one. Once traffic has moved over, revoke the old key\",\n  1,\n  \". Both keys stay valid during the overlap, so no request fails\",\n  2,\n  \".\",\n];\nconst WORDS = ANSWER.filter((part) => typeof part === \"string\").join(\" \").split(/\\s+/).length;\n\nconst answer = \"text-[15px] leading-relaxed text-pretty\";\nconst cite =\n  \"mx-0.5 inline-grid size-[18px] place-items-center rounded-full align-[2px] text-[10px] font-semibold\";\n\n/** The answer again, each word colored by when it streamed in: green first, violet last. */\nfunction Streamed() {\n  let n = 0;\n  return (\n    <p className={answer}>\n      {ANSWER.map((part, i) =>\n        typeof part === \"number\" ? (\n          <span key={i} className={`${cite} text-transparent`}>\n            {part}\n          </span>\n        ) : (\n          part.split(/(\\s+)/).map((word, j) =>\n            /\\s/.test(word) || !word ? (\n              word\n            ) : (\n              <span key={`${i}-${j}`} style={{ color: `oklch(0.82 0.14 ${160 + (n++ / WORDS) * 140})` }}>\n                {word}\n              </span>\n            ),\n          )\n        ),\n      )}\n    </p>\n  );\n}\n\nconst SOURCES = [\n  { host: \"docs.acme.dev\", title: \"Rotating API keys\", score: \"0.91\" },\n  { host: \"acme.dev/blog\", title: \"Zero-downtime deploys\", score: \"0.88\" },\n  { host: \"status.acme.dev\", title: \"Incident 4127\", score: \"0.64\" },\n];\n\nfunction Answer({ timing, total }: { timing: Timing; total: number }) {\n  return (\n    <div className=\"flex flex-col gap-5 p-5 sm:p-6\">\n      <XrayLensSpot\n        label=\"embed · query\"\n        xrayClassName={spanBox}\n        xray={<Span name=\"embed\" at={timing.embed} total={total} note=\"trace 7f3a2c · 1536 dims\" />}\n        className=\"flex h-12 items-center gap-2.5 rounded-xl bg-zinc-100 px-3.5 text-sm text-zinc-900 dark:bg-zinc-900 dark:text-zinc-100\"\n      >\n        <svg aria-hidden viewBox=\"0 0 20 20\" className=\"size-4 shrink-0 fill-none stroke-zinc-400 stroke-2\">\n          <circle cx=\"9\" cy=\"9\" r=\"6\" />\n          <path d=\"M13.5 13.5 17 17\" strokeLinecap=\"round\" />\n        </svg>\n        <span className=\"truncate\">How do I rotate API keys without downtime?</span>\n      </XrayLensSpot>\n\n      <div>\n        <XrayLensSpot\n          label=\"generate\"\n          xray={<SpanRow name=\"generate\" at={timing.generate} total={total} />}\n          className=\"text-xs leading-4 font-medium tracking-wider text-zinc-500 uppercase\"\n        >\n          Answer\n        </XrayLensSpot>\n        <XrayLensSpot\n          label={`stream · 214 tokens · first at ${Math.round(timing.generate.ms * 0.22)}ms`}\n          xray={<Streamed />}\n          className=\"mt-2\"\n        >\n          <p className={`${answer} text-zinc-700 dark:text-zinc-300`}>\n            {ANSWER.map((part, i) =>\n              typeof part === \"number\" ? (\n                <XrayLensSpot\n                  key={i}\n                  as=\"span\"\n                  label={`cite · doc_${part} · ${timing.cite.ms}ms`}\n                  xrayClassName=\"outline-1 -outline-offset-1 outline-amber-300/80 bg-amber-300/15\"\n                  xray={\n                    <span className=\"grid size-full place-items-center font-mono text-[9px] text-amber-200\">\n                      d{part}\n                    </span>\n                  }\n                  className={`${cite} bg-zinc-100 text-zinc-600 dark:bg-zinc-800 dark:text-zinc-300`}\n                >\n                  {part}\n                </XrayLensSpot>\n              ) : (\n                part\n              ),\n            )}\n          </p>\n        </XrayLensSpot>\n      </div>\n\n      <div>\n        <XrayLensSpot\n          label=\"retrieve\"\n          xray={<SpanRow name=\"retrieve\" at={timing.retrieve} total={total} />}\n          className=\"text-xs leading-4 font-medium tracking-wider text-zinc-500 uppercase\"\n        >\n          Sources\n        </XrayLensSpot>\n        <div className=\"mt-2 grid gap-2 sm:grid-cols-3\">\n          {SOURCES.map((source, i) => (\n            <XrayLensSpot\n              key={source.host}\n              label={`${i === 2 ? \"rerank\" : \"retrieve\"} · doc_${i + 1}`}\n              xrayClassName={spanBox}\n              xray={\n                <Span\n                  name={i === 2 ? \"rerank\" : `doc_${i + 1}`}\n                  at={i === 2 ? timing.rerank : timing.retrieve}\n                  total={total}\n                  note={`score ${source.score}${i === 2 ? \" · dropped\" : \"\"}`}\n                />\n              }\n              className=\"rounded-lg p-2.5 ring-1 ring-zinc-950/10 dark:ring-white/10\"\n            >\n              <p className=\"truncate text-[11px] text-zinc-500\">{source.host}</p>\n              <p className=\"mt-0.5 truncate text-sm font-medium text-zinc-900 dark:text-zinc-100\">\n                {source.title}\n              </p>\n            </XrayLensSpot>\n          ))}\n        </div>\n      </div>\n\n      <XrayLensSpot\n        label=\"suggest · 2 follow-ups\"\n        xrayClassName={spanBox}\n        xray={<SpanRow name=\"suggest\" at={timing.suggest} total={total} />}\n        className=\"flex flex-wrap gap-2 self-start\"\n      >\n        {[\"Revoke a leaked key\", \"Scope a key to one project\"].map((q) => (\n          <button\n            key={q}\n            type=\"button\"\n            className=\"h-8 rounded-full px-3 text-xs text-zinc-600 ring-1 ring-zinc-950/10 transition-colors hover:bg-zinc-50 dark:text-zinc-400 dark:ring-white/10 dark:hover:bg-zinc-900\"\n          >\n            {q}\n          </button>\n        ))}\n      </XrayLensSpot>\n    </div>\n  );\n}\n\nexport function XrayLensTraceDemo() {\n  const { timing, total } = useLiveTrace();\n  return (\n    <section className=\"w-full max-w-2xl\">\n      <div className=\"mb-8 text-center\">\n        <p className=\"font-mono text-xs tracking-widest text-zinc-500 uppercase\">Observability</p>\n        <h3 className=\"mt-3 text-3xl font-semibold tracking-tight text-balance text-zinc-900 dark:text-zinc-50\">\n          Every answer, traced to the millisecond.\n        </h3>\n      </div>\n      <XrayLens\n        rest={false}\n        size=\"28%\"\n        autoPlay\n        zoom={1.08}\n        lensLabel=\"Trace lens\"\n        labels={[\"Answer\", \"Trace\"]}\n        toggleClassName=\"top-full right-auto bottom-auto left-1/2 mt-4 -translate-x-1/2\"\n        surface={<Answer timing={timing} total={total} />}\n        xray={\n          <div className=\"absolute inset-0 bg-[linear-gradient(90deg,rgb(52_211_153/0.07)_1px,transparent_1px)] bg-size-[24px_100%]\" />\n        }\n        xrayClassName=\"bg-[#050807] text-zinc-100\"\n        className=\"rounded-2xl bg-white shadow-[0_1px_2px_rgb(0_0_0/0.05),0_16px_40px_-20px_rgb(0_0_0/0.3)] ring-1 ring-zinc-950/5 dark:bg-zinc-950 dark:ring-white/10\"\n      />\n      <p className=\"mt-20 text-center text-sm text-zinc-500\">\n        The lens tours each span. Point at any part of the answer to take over.\n      </p>\n    </section>\n  );\n}\n"
    }
  ]
}