{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "duplex-timeline",
  "title": "Duplex Timeline",
  "description": "Scrolling agent and user waveforms on one clock, colored by what the audio was: speech, backchannels, and interruptions where the user talks over the bot. Dashed rules mark each handoff, with the reply latency between them. Headless view for any two tracks and a marker list, plus a Pipecat-connected wrapper that builds the markers from RTVI turn and backchannel events.",
  "dependencies": [
    "@pipecat-ai/client-js@^1.14.0",
    "@pipecat-ai/client-react@^1.9.0"
  ],
  "files": [
    {
      "path": "src/components/duplex-timeline.tsx",
      "content": "\"use client\";\n\nimport {\n  RTVIEvent,\n  type BotOutputData,\n  type UserInputData,\n} from \"@pipecat-ai/client-js\";\nimport {\n  usePipecatClientMediaTrack,\n  usePipecatClientTransportState,\n  useRTVIClientEvent,\n} from \"@pipecat-ai/client-react\";\nimport {\n  useCallback,\n  useEffect,\n  useReducer,\n  useRef,\n  type Dispatch,\n} from \"react\";\n\nimport {\n  DUPLEX_TIMELINE_INITIAL_STATE,\n  reduceDuplexTimeline,\n  type DuplexTimelineEvent,\n  type DuplexTimelineMarker,\n} from \"@/components/pipecat/duplex-timeline-markers\";\nimport {\n  createDuplexTimelineRenderer,\n  DUPLEX_TIMELINE_HEIGHT,\n  DUPLEX_TIMELINE_LANES,\n  type DuplexTimelineRenderer,\n} from \"@/components/pipecat/duplex-timeline-renderer\";\nimport { cn } from \"@/lib/utils\";\nimport { resolveVisualizerColor } from \"@/lib/visualizer\";\n\nexport type { DuplexTimelineMarker };\n\nconst DEFAULT_COLORS = {\n  agent: \"--agent-foreground\",\n  user: \"--client-foreground\",\n  backchannel: \"--muted-foreground\",\n  interruption: \"--inactive-background\",\n  grid: \"--border\",\n};\n\n/**\n * Label defaults. Segment colors come through the root's custom properties\n * so a label class name can override them.\n */\nconst LABEL_CLASSES = {\n  backchannel:\n    \"text-[10px] leading-none text-(color:--duplex-timeline-backchannel)\",\n  interruption:\n    \"text-[10px] leading-none text-(color:--duplex-timeline-interruption)\",\n  latency:\n    \"text-[10px] leading-none data-slow:text-(color:--duplex-timeline-slow-reply)\",\n  time: \"text-[10px] leading-none opacity-50\",\n};\n\n/** A color prop as CSS: \"--token\" names become var(--token). */\nfunction cssColor(color: string): string {\n  return color.startsWith(\"--\") ? `var(${color})` : color;\n}\n\n/**\n * A color prop for the canvas. Tokens resolve on the canvas itself, so an\n * override scoped to the timeline reaches the bars as well as the DOM parts.\n */\nfunction canvasColor(color: string, canvas: HTMLCanvasElement): string {\n  if (!color.startsWith(\"--\")) return resolveVisualizerColor(color, canvas);\n  return getComputedStyle(canvas).getPropertyValue(color).trim() || \"black\";\n}\n\nexport interface DuplexTimelineViewProps {\n  /** The bot's audio track; the agent lane stays flat when null. */\n  agentTrack?: MediaStreamTrack | null;\n  /** The user's audio track; the user lane stays flat when null. */\n  userTrack?: MediaStreamTrack | null;\n  /** Turn markers in performance.now() time, e.g. from useDuplexTimelineMarkers. */\n  markers?: readonly DuplexTimelineMarker[];\n  /** Scrolls while true and holds the last frame when false. Starting clears both lanes and restarts the time axis. */\n  running?: boolean;\n  /** Lights the agent lane label. */\n  agentSpeaking?: boolean;\n  /** Lights the user lane label. */\n  userSpeaking?: boolean;\n  agentLabel?: string;\n  userLabel?: string;\n  /** Agent speech bars. Supports \"--css-var\" names, \"currentColor\" and any CSS color. */\n  agentColor?: string;\n  /** User speech bars; same formats as agentColor. */\n  userColor?: string;\n  /** Backchannel bars and labels. */\n  backchannelColor?: string;\n  /** Interruption bars and labels. */\n  interruptionColor?: string;\n  /** Latency of replies at or above slowReplyMs; defaults to interruptionColor. */\n  slowReplyColor?: string;\n  /** Centre lines, second ticks and the now edge. */\n  gridColor?: string;\n  /** Replies at least this slow, in ms, draw their latency in slowReplyColor. */\n  slowReplyMs?: number;\n  /** Draw backchannels as speech, without labels. Markers still keep them off the handoffs. */\n  noBackchannels?: boolean;\n  /** Draw interruptions as speech, without labels. */\n  noInterruptions?: boolean;\n  /** Hide the dashed rules at turn ends and replies. */\n  noHandoffs?: boolean;\n  /** Hide reply latency brackets. */\n  noLatency?: boolean;\n  /** Hide the lane labels; the canvas takes their width. */\n  noLaneLabels?: boolean;\n  /** Hide backchannel and interruption labels; their bars keep their colors. */\n  noSegmentLabels?: boolean;\n  /** Hide the elapsed-time labels under the lanes. */\n  noTimeLabels?: boolean;\n  /** Hide the color key above the lanes; DuplexTimelineLegend can place it elsewhere. */\n  noLegend?: boolean;\n  className?: string;\n  /** Classes for the color key. */\n  legendClassName?: string;\n  /** Classes for each lane label, which carries data-lane and data-state=\"speaking\" | \"silent\". */\n  laneLabelClassName?: string;\n  /** Classes for backchannel labels. */\n  backchannelLabelClassName?: string;\n  /** Classes for interruption labels. */\n  interruptionLabelClassName?: string;\n  /** Classes for latency labels; slow replies carry data-slow. */\n  latencyLabelClassName?: string;\n  /** Classes for the elapsed-time labels. */\n  timeLabelClassName?: string;\n}\n\n/**\n * Scrolling agent and user waveforms. Handoff rules, latency and time labels\n * use the element's text color. Labels over the canvas are DOM spans.\n */\nexport function DuplexTimelineView({\n  agentTrack = null,\n  userTrack = null,\n  markers,\n  running = false,\n  agentSpeaking = false,\n  userSpeaking = false,\n  agentLabel = \"agent\",\n  userLabel = \"you\",\n  agentColor = DEFAULT_COLORS.agent,\n  userColor = DEFAULT_COLORS.user,\n  backchannelColor = DEFAULT_COLORS.backchannel,\n  interruptionColor = DEFAULT_COLORS.interruption,\n  slowReplyColor = interruptionColor,\n  gridColor = DEFAULT_COLORS.grid,\n  slowReplyMs = 1000,\n  noBackchannels = false,\n  noInterruptions = false,\n  noHandoffs = false,\n  noLatency = false,\n  noLaneLabels = false,\n  noSegmentLabels = false,\n  noTimeLabels = false,\n  noLegend = false,\n  className,\n  legendClassName,\n  laneLabelClassName,\n  backchannelLabelClassName,\n  interruptionLabelClassName,\n  latencyLabelClassName,\n  timeLabelClassName,\n}: DuplexTimelineViewProps) {\n  const canvasRef = useRef<HTMLCanvasElement>(null);\n  const labelsRef = useRef<HTMLDivElement>(null);\n  const rendererRef = useRef<DuplexTimelineRenderer | null>(null);\n\n  // The renderer owns the frame loop, audio and label spans; props are\n  // pushed into it.\n  useEffect(() => {\n    const canvas = canvasRef.current;\n    const labels = labelsRef.current;\n    if (!canvas || !labels) return;\n    const renderer = createDuplexTimelineRenderer(canvas, labels);\n    rendererRef.current = renderer;\n    return () => {\n      renderer.dispose();\n      rendererRef.current = null;\n    };\n  }, []);\n\n  // Resolve colors now and whenever the theme changes, on <html> or with the\n  // OS. className is a dependency because it can change what they resolve to.\n  useEffect(() => {\n    const canvas = canvasRef.current;\n    if (!canvas) return;\n    const apply = () => {\n      rendererRef.current?.setPalette({\n        agent: canvasColor(agentColor, canvas),\n        user: canvasColor(userColor, canvas),\n        backchannel: canvasColor(backchannelColor, canvas),\n        interruption: canvasColor(interruptionColor, canvas),\n        slowReply: canvasColor(slowReplyColor, canvas),\n        grid: canvasColor(gridColor, canvas),\n        text: canvasColor(\"currentColor\", canvas),\n      });\n    };\n    apply();\n    const observer = new MutationObserver(apply);\n    observer.observe(document.documentElement, {\n      attributes: true,\n      attributeFilter: [\"class\", \"style\", \"data-theme\"],\n    });\n    const scheme = matchMedia(\"(prefers-color-scheme: dark)\");\n    scheme.addEventListener(\"change\", apply);\n    return () => {\n      observer.disconnect();\n      scheme.removeEventListener(\"change\", apply);\n    };\n  }, [\n    agentColor,\n    userColor,\n    backchannelColor,\n    interruptionColor,\n    slowReplyColor,\n    gridColor,\n    className,\n  ]);\n\n  // Before markers, so the first frame creates label spans with their classes.\n  useEffect(() => {\n    rendererRef.current?.setOptions({\n      slowReplyMs,\n      backchannels: !noBackchannels,\n      interruptions: !noInterruptions,\n      handoffs: !noHandoffs,\n      latency: !noLatency,\n      segmentLabels: !noSegmentLabels,\n      timeLabels: !noTimeLabels,\n      labelClassNames: {\n        backchannel: cn(LABEL_CLASSES.backchannel, backchannelLabelClassName),\n        interruption: cn(\n          LABEL_CLASSES.interruption,\n          interruptionLabelClassName,\n        ),\n        latency: cn(LABEL_CLASSES.latency, latencyLabelClassName),\n        time: cn(LABEL_CLASSES.time, timeLabelClassName),\n      },\n    });\n  }, [\n    slowReplyMs,\n    noBackchannels,\n    noInterruptions,\n    noHandoffs,\n    noLatency,\n    noSegmentLabels,\n    noTimeLabels,\n    backchannelLabelClassName,\n    interruptionLabelClassName,\n    latencyLabelClassName,\n    timeLabelClassName,\n  ]);\n\n  useEffect(() => {\n    rendererRef.current?.setTrack(\"agent\", agentTrack);\n  }, [agentTrack]);\n\n  useEffect(() => {\n    rendererRef.current?.setTrack(\"user\", userTrack);\n  }, [userTrack]);\n\n  useEffect(() => {\n    rendererRef.current?.setMarkers(markers ?? []);\n  }, [markers]);\n\n  useEffect(() => {\n    const renderer = rendererRef.current;\n    if (!renderer) return;\n    if (running) renderer.reset();\n    renderer.setRunning(running);\n  }, [running]);\n\n  const { agent, user } = DUPLEX_TIMELINE_LANES;\n\n  return (\n    <div\n      data-slot=\"duplex-timeline\"\n      data-state={running ? \"live\" : \"idle\"}\n      className={cn(\n        \"text-muted-foreground flex flex-col gap-4 font-mono text-[11px]\",\n        className,\n      )}\n      style={\n        {\n          \"--duplex-timeline-backchannel\": cssColor(backchannelColor),\n          \"--duplex-timeline-interruption\": cssColor(interruptionColor),\n          \"--duplex-timeline-slow-reply\": cssColor(slowReplyColor),\n        } as React.CSSProperties\n      }\n    >\n      {!noLegend && (\n        <DuplexTimelineLegend\n          backchannelColor={backchannelColor}\n          interruptionColor={interruptionColor}\n          noBackchannels={noBackchannels}\n          noInterruptions={noInterruptions}\n          noHandoffs={noHandoffs}\n          className={legendClassName}\n        />\n      )}\n      <div className=\"flex gap-3\">\n        {!noLaneLabels && (\n          <div\n            className=\"flex shrink-0 flex-col\"\n            style={{\n              paddingTop: agent.top,\n              rowGap: user.top - agent.top - agent.height,\n            }}\n          >\n            <LaneLabel\n              lane=\"agent\"\n              label={agentLabel}\n              color={agentColor}\n              speaking={agentSpeaking}\n              height={agent.height}\n              className={laneLabelClassName}\n            />\n            <LaneLabel\n              lane=\"user\"\n              label={userLabel}\n              color={userColor}\n              speaking={userSpeaking}\n              height={user.height}\n              className={laneLabelClassName}\n            />\n          </div>\n        )}\n        <div className=\"relative min-w-0 flex-1 [mask-image:linear-gradient(to_right,transparent,black_3rem)]\">\n          <canvas\n            ref={canvasRef}\n            role=\"img\"\n            aria-label={`${agentLabel} and ${userLabel} speech over time`}\n            className=\"block w-full\"\n            style={{ height: DUPLEX_TIMELINE_HEIGHT }}\n          />\n          <div\n            ref={labelsRef}\n            aria-hidden\n            className=\"pointer-events-none absolute inset-0 overflow-hidden\"\n          />\n        </div>\n      </div>\n    </div>\n  );\n}\n\n/** Lane name, lit while that side speaks. */\nfunction LaneLabel({\n  lane,\n  label,\n  color,\n  speaking,\n  height,\n  className,\n}: {\n  lane: \"agent\" | \"user\";\n  label: string;\n  color: string;\n  speaking: boolean;\n  height: number;\n  className?: string;\n}) {\n  return (\n    <span\n      data-slot=\"duplex-timeline-lane-label\"\n      data-lane={lane}\n      data-state={speaking ? \"speaking\" : \"silent\"}\n      className={cn(\n        \"group/lane data-[state=speaking]:text-foreground flex items-center gap-1.5\",\n        className,\n      )}\n      style={{ height }}\n    >\n      <span\n        aria-hidden\n        className=\"size-1.5 shrink-0 rounded-full opacity-35 transition-opacity duration-150 group-data-[state=speaking]/lane:opacity-100 motion-reduce:transition-none\"\n        style={{ backgroundColor: cssColor(color) }}\n      />\n      {label}\n    </span>\n  );\n}\n\n/** The timeline's colors and no* props, so one options object can feed both. */\nexport type DuplexTimelineLegendProps = Pick<\n  DuplexTimelineViewProps,\n  | \"backchannelColor\"\n  | \"interruptionColor\"\n  | \"noBackchannels\"\n  | \"noInterruptions\"\n  | \"noHandoffs\"\n> & { className?: string };\n\n/** Key for the colors and rules the lane labels don't explain. */\nexport function DuplexTimelineLegend({\n  backchannelColor = DEFAULT_COLORS.backchannel,\n  interruptionColor = DEFAULT_COLORS.interruption,\n  noBackchannels = false,\n  noInterruptions = false,\n  noHandoffs = false,\n  className,\n}: DuplexTimelineLegendProps) {\n  const swatches = [\n    { label: \"backchannel\", color: backchannelColor, hidden: noBackchannels },\n    {\n      label: \"interruption\",\n      color: interruptionColor,\n      hidden: noInterruptions,\n    },\n  ].filter((swatch) => !swatch.hidden);\n  return (\n    <span\n      data-slot=\"duplex-timeline-legend\"\n      className={cn(\n        \"text-muted-foreground flex items-center gap-3 font-mono text-[11px]\",\n        className,\n      )}\n    >\n      {swatches.map(({ label, color }) => (\n        <span key={label} className=\"flex items-center gap-1.5\">\n          <span\n            aria-hidden\n            className=\"h-2 w-1\"\n            style={{ backgroundColor: cssColor(color) }}\n          />\n          {label}\n        </span>\n      ))}\n      {!noHandoffs && (\n        <span className=\"flex items-center gap-1.5\">\n          <span\n            aria-hidden\n            className=\"h-2 w-px border-l border-dashed border-current\"\n          />\n          handoff\n        </span>\n      )}\n    </span>\n  );\n}\n\ntype TurnEventType = Exclude<\n  DuplexTimelineEvent[\"type\"],\n  \"bot-backchannel\" | \"user-backchannel\"\n>;\n\n/** Forwards an RTVI turn event, stamped on arrival, with a stable handler. */\nfunction useTurnEvent(\n  event: RTVIEvent,\n  type: TurnEventType,\n  dispatch: Dispatch<DuplexTimelineEvent>,\n) {\n  const handler = useCallback(\n    () => dispatch({ type, now: performance.now() }),\n    [dispatch, type],\n  );\n  useRTVIClientEvent(event, handler);\n}\n\n/**\n * Turn markers and speaking state from RTVI turn and backchannel events.\n * Requires PipecatClientProvider and an RTVI 2.2 bot for backchannels.\n */\nexport function useDuplexTimelineMarkers() {\n  const [state, dispatch] = useReducer(\n    reduceDuplexTimeline,\n    DUPLEX_TIMELINE_INITIAL_STATE,\n  );\n\n  useTurnEvent(RTVIEvent.Connected, \"connected\", dispatch);\n  useTurnEvent(RTVIEvent.Disconnected, \"disconnected\", dispatch);\n  useTurnEvent(\n    RTVIEvent.UserStartedSpeaking,\n    \"user-started-speaking\",\n    dispatch,\n  );\n  useTurnEvent(\n    RTVIEvent.UserStoppedSpeaking,\n    \"user-stopped-speaking\",\n    dispatch,\n  );\n  useTurnEvent(RTVIEvent.BotStartedSpeaking, \"bot-started-speaking\", dispatch);\n  useTurnEvent(RTVIEvent.BotStoppedSpeaking, \"bot-stopped-speaking\", dispatch);\n\n  const onBotOutput = useCallback((data: BotOutputData) => {\n    // Only a backchannel's first event carries its text; the rest report\n    // how much has been spoken.\n    if (data.text_type !== \"backchannel\") return;\n    if (data.spoken_status && data.spoken_status !== \"new\") return;\n    dispatch({\n      type: \"bot-backchannel\",\n      text: data.text.trim(),\n      now: performance.now(),\n    });\n  }, []);\n  useRTVIClientEvent(RTVIEvent.BotOutput, onBotOutput);\n\n  const onUserInput = useCallback((data: UserInputData) => {\n    if (data.input_type !== \"backchannel\") return;\n    dispatch({\n      type: \"user-backchannel\",\n      text: data.text.trim(),\n      now: performance.now(),\n    });\n  }, []);\n  useRTVIClientEvent(RTVIEvent.UserInput, onUserInput);\n\n  return {\n    markers: state.markers,\n    agentSpeaking: state.botSpeaking,\n    userSpeaking: state.userSpeaking,\n  };\n}\n\nexport type DuplexTimelineProps = Omit<\n  DuplexTimelineViewProps,\n  | \"agentTrack\"\n  | \"userTrack\"\n  | \"markers\"\n  | \"running\"\n  | \"agentSpeaking\"\n  | \"userSpeaking\"\n>;\n\n/** Requires PipecatClientProvider; scrolls while the transport is ready. */\nexport function DuplexTimeline(props: DuplexTimelineProps) {\n  const agentTrack = usePipecatClientMediaTrack(\"audio\", \"bot\");\n  const userTrack = usePipecatClientMediaTrack(\"audio\", \"local\");\n  const running = usePipecatClientTransportState() === \"ready\";\n  const { markers, agentSpeaking, userSpeaking } = useDuplexTimelineMarkers();\n\n  return (\n    <DuplexTimelineView\n      {...props}\n      agentTrack={agentTrack}\n      userTrack={userTrack}\n      markers={markers}\n      running={running}\n      agentSpeaking={agentSpeaking}\n      userSpeaking={userSpeaking}\n    />\n  );\n}\n",
      "type": "registry:component",
      "target": "components/pipecat/duplex-timeline.tsx"
    },
    {
      "path": "src/components/duplex-timeline-renderer.ts",
      "content": "import type { DuplexTimelineMarker } from \"@/components/pipecat/duplex-timeline-markers\";\nimport { createVisualizerAnalyser } from \"@/lib/visualizer\";\n\nexport type DuplexTimelineLane = \"agent\" | \"user\";\nexport type DuplexTimelineLabelKind =\n  \"backchannel\" | \"interruption\" | \"latency\" | \"time\";\n\n/** Resolved canvas colors; see DuplexTimelineView for meanings. */\nexport interface DuplexTimelinePalette {\n  agent: string;\n  user: string;\n  backchannel: string;\n  interruption: string;\n  slowReply: string;\n  grid: string;\n  /** Handoff rules and latency brackets. */\n  text: string;\n}\n\n/** What to draw; see DuplexTimelineView's no* props. */\nexport interface DuplexTimelineOptions {\n  slowReplyMs: number;\n  backchannels: boolean;\n  interruptions: boolean;\n  handoffs: boolean;\n  latency: boolean;\n  segmentLabels: boolean;\n  timeLabels: boolean;\n  labelClassNames: Record<DuplexTimelineLabelKind, string>;\n}\n\n/** Scroll speed and bar geometry; one bar per slot of time. */\nconst PX_PER_SECOND = 56;\nconst BAR_WIDTH = 3;\nconst BAR_GAP = 2;\nconst SLOT_PX = BAR_WIDTH + BAR_GAP;\nconst SLOT_MS = (SLOT_PX / PX_PER_SECOND) * 1000;\n/** Elapsed whole seconds as m:ss. */\nfunction formatElapsed(seconds: number) {\n  const minutes = Math.floor(seconds / 60);\n  return `${String(minutes)}:${String(seconds % 60).padStart(2, \"0\")}`;\n}\n\n/** A time's distance along the timeline in whole pixels. */\nconst pixelsAt = (time: number) => Math.round((time / 1000) * PX_PER_SECOND);\n/** Slots kept per lane: more than the widest screen shows. */\nconst SLOT_CAPACITY = 1024;\n/** How far back level history reaches; older markers can't be drawn. */\nexport const DUPLEX_TIMELINE_HISTORY_MS = SLOT_CAPACITY * SLOT_MS;\n\n/** Vertical layout, CSS px. */\nconst LABEL_ROW = 13;\nconst LABEL_ROWS = 2;\nconst LANE_HEIGHT = 40;\nconst LANE_GAP = 16;\nconst AXIS_HEIGHT = 14;\nconst TOP = LABEL_ROW * LABEL_ROWS + 4;\nexport const DUPLEX_TIMELINE_LANES: Record<\n  DuplexTimelineLane,\n  { top: number; height: number }\n> = {\n  agent: { top: TOP, height: LANE_HEIGHT },\n  user: { top: TOP + LANE_HEIGHT + LANE_GAP, height: LANE_HEIGHT },\n};\nconst LANES_BOTTOM = DUPLEX_TIMELINE_LANES.user.top + LANE_HEIGHT;\nexport const DUPLEX_TIMELINE_HEIGHT = LANES_BOTTOM + AXIS_HEIGHT;\n\ninterface Segment {\n  start: number;\n  end: number;\n  kind: \"backchannel\" | \"interruption\";\n}\n\n/** Names a segment, from its start. */\ninterface SegmentLabel {\n  id: number;\n  kind: \"backchannel\" | \"interruption\";\n  text: string;\n  start: number;\n}\n\n/** Minimum level that draws a bar. */\nconst AUDIBLE_LEVEL = 0.02;\n/** Minimum level that counts as speech when locating a burst. */\nconst SPEECH_LEVEL = 0.18;\n/** Search window for an acknowledgment's audio before its message. */\nconst ACKNOWLEDGMENT_SEARCH_MS = 3000;\n/** Search window for the user's onset before a turn is announced. */\nconst ONSET_SEARCH_MS = 2500;\n/** Silent slots allowed inside one burst. */\nconst BURST_GAP_SLOTS = 3;\n/** Redraw interval under prefers-reduced-motion; levels still sample per frame. */\nconst REDUCED_MOTION_FRAME_MS = 500;\n\n/** A track's level, 0–1. */\nclass LevelMeter {\n  private readonly analyser: AnalyserNode;\n  private readonly samples: Float32Array<ArrayBuffer>;\n  readonly dispose: () => void;\n\n  constructor(track: MediaStreamTrack) {\n    const { analyser, dispose } = createVisualizerAnalyser(track);\n    this.analyser = analyser;\n    this.samples = new Float32Array(analyser.fftSize);\n    this.dispose = dispose;\n    // Starts suspended outside a user gesture; the connect click allows resume.\n    if (analyser.context instanceof AudioContext) {\n      void analyser.context.resume();\n    }\n  }\n\n  /** RMS mapped from -55 dBFS (0) to -10 dBFS (1). */\n  read(): number {\n    this.analyser.getFloatTimeDomainData(this.samples);\n    let squares = 0;\n    for (const sample of this.samples) squares += sample * sample;\n    const rms = Math.sqrt(squares / this.samples.length);\n    const db = 20 * Math.log10(rms + 1e-8);\n    return Math.min(1, Math.max(0, (db + 55) / 45));\n  }\n}\n\n/** Per-slot peak levels for a lane, in a ring buffer. */\nclass LevelHistory {\n  private readonly slots = new Float32Array(SLOT_CAPACITY);\n  private last = -1;\n\n  record(slot: number, level: number) {\n    if (this.last < 0 || slot - this.last > SLOT_CAPACITY) {\n      this.slots.fill(0);\n    } else {\n      for (let skipped = this.last + 1; skipped < slot; skipped++) {\n        this.slots[skipped % SLOT_CAPACITY] = 0;\n      }\n    }\n    if (slot !== this.last) this.slots[slot % SLOT_CAPACITY] = 0;\n    const index = slot % SLOT_CAPACITY;\n    this.slots[index] = Math.max(this.slots[index] ?? 0, level);\n    this.last = Math.max(this.last, slot);\n  }\n\n  level(slot: number): number {\n    if (slot > this.last || this.last - slot >= SLOT_CAPACITY) return 0;\n    return this.slots[slot % SLOT_CAPACITY] ?? 0;\n  }\n\n  clear() {\n    this.slots.fill(0);\n    this.last = -1;\n  }\n}\n\ninterface LabelSpec {\n  key: string;\n  kind: DuplexTimelineLabelKind;\n  text: string;\n  /** Sets data-slow, for latency at or above slowReplyMs. */\n  slow?: boolean;\n}\n\n/**\n * Labels as DOM spans over the canvas, so they take class names. A span\n * lives while its key is shown each frame; its text and classes are fixed.\n */\nclass LabelLayer {\n  private readonly root: HTMLElement;\n  private readonly labels = new Map<\n    string,\n    { span: HTMLSpanElement; width: number }\n  >();\n  private readonly shown = new Set<string>();\n  classNames: Record<DuplexTimelineLabelKind, string> = {\n    backchannel: \"\",\n    interruption: \"\",\n    latency: \"\",\n    time: \"\",\n  };\n\n  constructor(root: HTMLElement) {\n    this.root = root;\n  }\n\n  private get({ key, kind, text, slow = false }: LabelSpec) {\n    let label = this.labels.get(key);\n    if (!label) {\n      const span = document.createElement(\"span\");\n      span.dataset.slot = `duplex-timeline-${kind}-label`;\n      span.toggleAttribute(\"data-slow\", slow);\n      span.className = this.classNames[kind];\n      span.style.cssText = \"position:absolute;top:0;left:0;white-space:nowrap\";\n      span.textContent = text;\n      this.root.append(span);\n      label = { span, width: span.getBoundingClientRect().width };\n      this.labels.set(key, label);\n    }\n    return label;\n  }\n\n  /** A label's width, measured when its span is created. */\n  measure(spec: LabelSpec): number {\n    return this.get(spec).width;\n  }\n\n  /** Places a label's top-left at x, y, raised by `lift` of its height. */\n  show(spec: LabelSpec, x: number, y: number, lift = 0) {\n    const { span } = this.get(spec);\n    span.style.transform = `translate(${String(x)}px, ${String(y)}px) translateY(${String(-lift * 100)}%)`;\n    this.shown.add(spec.key);\n  }\n\n  /** Starts a frame. */\n  begin() {\n    this.shown.clear();\n  }\n\n  /** Ends a frame, removing labels it didn't show. */\n  end() {\n    for (const [key, { span }] of this.labels) {\n      if (this.shown.has(key)) continue;\n      span.remove();\n      this.labels.delete(key);\n    }\n  }\n\n  clear() {\n    for (const { span } of this.labels.values()) span.remove();\n    this.labels.clear();\n  }\n}\n\nexport interface DuplexTimelineRenderer {\n  setTrack(lane: DuplexTimelineLane, track: MediaStreamTrack | null): void;\n  setMarkers(markers: readonly DuplexTimelineMarker[]): void;\n  setPalette(palette: DuplexTimelinePalette): void;\n  setOptions(options: DuplexTimelineOptions): void;\n  /** Scrolls while running; holds the last frame when stopped. */\n  setRunning(running: boolean): void;\n  /** Clears both lanes' level history. */\n  reset(): void;\n  dispose(): void;\n}\n\n/**\n * Two scrolling waveforms, agent over user, colored by what the audio was\n * (speech, backchannel, interruption), with handoff rules and reply latency.\n * Owns its frame loop and the label spans in `labelRoot`; the caller feeds\n * tracks, markers, colors and options.\n */\nexport function createDuplexTimelineRenderer(\n  canvas: HTMLCanvasElement,\n  labelRoot: HTMLElement,\n): DuplexTimelineRenderer {\n  const context2d = canvas.getContext(\"2d\");\n  if (!context2d) throw new Error(\"Canvas 2D is unavailable\");\n  const ctx = context2d;\n\n  let palette: DuplexTimelinePalette = {\n    agent: \"black\",\n    user: \"black\",\n    backchannel: \"gray\",\n    interruption: \"black\",\n    slowReply: \"black\",\n    grid: \"gray\",\n    text: \"gray\",\n  };\n  let markers: readonly DuplexTimelineMarker[] = [];\n  let options: Omit<DuplexTimelineOptions, \"labelClassNames\"> = {\n    slowReplyMs: 1000,\n    backchannels: true,\n    interruptions: true,\n    handoffs: true,\n    latency: true,\n    segmentLabels: true,\n    timeLabels: true,\n  };\n  const labels = new LabelLayer(labelRoot);\n  const meters: Record<DuplexTimelineLane, LevelMeter | null> = {\n    agent: null,\n    user: null,\n  };\n  const history: Record<DuplexTimelineLane, LevelHistory> = {\n    agent: new LevelHistory(),\n    user: new LevelHistory(),\n  };\n  /** Per-lane alpha masks, opaque at the centre line. Color independent. */\n  const masks = new Map<DuplexTimelineLane, CanvasGradient>();\n\n  let running = false;\n  let frozenAt = performance.now();\n  /** Where the time axis counts from; reset() restarts it. */\n  let startedAt = frozenAt;\n  let frame = 0;\n  let lastDraw = -Infinity;\n  let width = 0;\n\n  const reducedMotion =\n    typeof matchMedia === \"function\"\n      ? matchMedia(\"(prefers-reduced-motion: reduce)\")\n      : null;\n\n  const resize = new ResizeObserver(([entry]) => {\n    if (!entry) return;\n    const dpr = window.devicePixelRatio || 1;\n    width = entry.contentRect.width;\n    canvas.width = Math.round(width * dpr);\n    canvas.height = Math.round(DUPLEX_TIMELINE_HEIGHT * dpr);\n    ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n    masks.clear();\n    redraw();\n  });\n  resize.observe(canvas);\n\n  /** Repaints a held frame; a running loop picks changes up itself. */\n  function redraw() {\n    if (!running) draw(frozenAt);\n  }\n\n  function tick(now: number) {\n    const slot = Math.floor(now / SLOT_MS);\n    for (const lane of [\"agent\", \"user\"] as const) {\n      history[lane].record(slot, meters[lane]?.read() ?? 0);\n    }\n    if (!reducedMotion?.matches || now - lastDraw >= REDUCED_MOTION_FRAME_MS) {\n      draw(now);\n      lastDraw = now;\n    }\n    frame = requestAnimationFrame(tick);\n  }\n\n  /**\n   * x of a time, with now at the right edge. Times and the scroll snap to\n   * pixels separately, so bars, rules and labels all step on the same frame.\n   */\n  function xAt(time: number, now: number) {\n    return width - pixelsAt(now) + pixelsAt(time);\n  }\n\n  function draw(now: number) {\n    ctx.clearRect(0, 0, width, DUPLEX_TIMELINE_HEIGHT);\n    labels.begin();\n    if (width > 0) {\n      const visible = markers.filter(\n        (marker) => xAt(markerEnd(marker, now), now) > -200,\n      );\n      const spans = segments(visible, now);\n      drawLane(\"agent\", palette.agent, spans.lanes.agent, now);\n      drawLane(\"user\", palette.user, spans.lanes.user, now);\n      drawGrid(now);\n      for (const marker of visible) drawHandoff(marker, now);\n      if (options.segmentLabels) drawSegmentLabels(spans.labels, now);\n    }\n    labels.end();\n  }\n\n  function markerEnd(marker: DuplexTimelineMarker, now: number) {\n    if (marker.kind === \"bot-backchannel\") return marker.end ?? now;\n    if (marker.kind === \"interruption\") return marker.cutAt ?? now;\n    if (marker.kind === \"possible-interruption\") return marker.cutAt;\n    return marker.at;\n  }\n\n  const slotOf = (time: number) => Math.floor(time / SLOT_MS);\n  const audible = (lane: DuplexTimelineLane, slot: number) =>\n    history[lane].level(slot) > AUDIBLE_LEVEL;\n\n  /**\n   * The speech burst at or before `from` (back to `limit`), widened to its\n   * quiet edges so a segment covers every bar it drew.\n   */\n  function burstAt(lane: DuplexTimelineLane, from: number, limit: number) {\n    let start: number | null = null;\n    let end: number | null = null;\n    let quiet = 0;\n    for (let slot = slotOf(from); slot >= slotOf(limit); slot--) {\n      const speaking = history[lane].level(slot) >= SPEECH_LEVEL;\n      if (end === null) {\n        if (speaking) end = start = slot;\n      } else if (speaking) {\n        start = slot;\n        quiet = 0;\n      } else if (++quiet > BURST_GAP_SLOTS) {\n        break;\n      }\n    }\n    if (start === null || end === null) return null;\n    return widen(lane, start, end);\n  }\n\n  /** Extends a slot range while its edges still draw bars. */\n  function widen(\n    lane: DuplexTimelineLane,\n    start: number,\n    end: number,\n  ): [number, number] {\n    while (audible(lane, start - 1)) start--;\n    while (audible(lane, end + 1)) end++;\n    return [start * SLOT_MS, (end + 1) * SLOT_MS];\n  }\n\n  /** A user acknowledgment: the last burst before its message. */\n  function acknowledgmentSpan(\n    marker: DuplexTimelineMarker & { kind: \"user-backchannel\" },\n  ): [number, number] {\n    return (\n      burstAt(\"user\", marker.at, marker.at - ACKNOWLEDGMENT_SEARCH_MS) ?? [\n        marker.at - 600,\n        marker.at,\n      ]\n    );\n  }\n\n  /** A bot backchannel: the agent audio after its message. */\n  function botBackchannelSpan(\n    marker: DuplexTimelineMarker & { kind: \"bot-backchannel\" },\n    now: number,\n  ): [number, number] {\n    const end = marker.end ?? now;\n    for (let slot = slotOf(marker.at); slot <= slotOf(end + 400); slot++) {\n      if (audible(\"agent\", slot)) {\n        const [start] = widen(\"agent\", slot, slot);\n        const [, last] = widen(\"agent\", slotOf(end), slotOf(end));\n        return [start, Math.max(last, end)];\n      }\n    }\n    return [marker.at, end];\n  }\n\n  /**\n   * An interruption's overlap: from the user's onset (before the turn was\n   * announced) to the bot's cut-off. Null if the audio didn't overlap.\n   */\n  function overlapSpans(\n    marker: DuplexTimelineMarker & {\n      kind: \"interruption\" | \"possible-interruption\";\n    },\n    now: number,\n  ): Record<DuplexTimelineLane, [number, number]> | null {\n    const cutAt = marker.cutAt ?? now;\n    const burst = burstAt(\"user\", marker.at, marker.at - ONSET_SEARCH_MS);\n    if (!burst || burst[0] >= cutAt) return null;\n    const [, agentEnd] = widen(\"agent\", slotOf(cutAt), slotOf(cutAt));\n    return {\n      user: [burst[0], cutAt],\n      agent: [burst[0], audible(\"agent\", slotOf(cutAt)) ? agentEnd : cutAt],\n    };\n  }\n\n  /** Non-speech stretches per lane (backchannels and interruptions) and their labels. */\n  function segments(visible: readonly DuplexTimelineMarker[], now: number) {\n    const lanes: Record<DuplexTimelineLane, Segment[]> = {\n      agent: [],\n      user: [],\n    };\n    const named: SegmentLabel[] = [];\n    for (const marker of visible) {\n      if (\n        marker.kind === \"bot-backchannel\" ||\n        marker.kind === \"user-backchannel\"\n      ) {\n        if (!options.backchannels) continue;\n        const [start, end] =\n          marker.kind === \"bot-backchannel\"\n            ? botBackchannelSpan(marker, now)\n            : acknowledgmentSpan(marker);\n        lanes[marker.kind === \"bot-backchannel\" ? \"agent\" : \"user\"].push({\n          start,\n          end,\n          kind: \"backchannel\",\n        });\n        named.push({\n          id: marker.id,\n          kind: \"backchannel\",\n          // \"Mm-hmm.\" → \"mm-hmm\"\n          text: marker.text.replace(/[.!?,]+$/, \"\").toLowerCase(),\n          start,\n        });\n      } else if (\n        marker.kind === \"interruption\" ||\n        marker.kind === \"possible-interruption\"\n      ) {\n        if (!options.interruptions) continue;\n        const overlap = overlapSpans(marker, now);\n        if (!overlap) continue;\n        for (const lane of [\"agent\", \"user\"] as const) {\n          const [start, end] = overlap[lane];\n          lanes[lane].push({ start, end, kind: \"interruption\" });\n        }\n        named.push({\n          id: marker.id,\n          kind: \"interruption\",\n          text: \"interruption\",\n          start: overlap.user[0],\n        });\n      }\n    }\n    return { lanes, labels: named };\n  }\n\n  /**\n   * Ticks every second since the start, labelled with the elapsed time every\n   * five, so each label scrolls with its tick. Then the now edge.\n   */\n  function drawGrid(now: number) {\n    ctx.fillStyle = palette.grid;\n    const visibleMs = (width / PX_PER_SECOND) * 1000;\n    const first = Math.max(0, Math.ceil((now - startedAt - visibleMs) / 1000));\n    for (let second = first; startedAt + second * 1000 <= now; second++) {\n      const x = Math.round(xAt(startedAt + second * 1000, now));\n      const major = second % 5 === 0;\n      ctx.fillRect(x, LANES_BOTTOM + 3, 1, major ? 4 : 2);\n      if (options.timeLabels && major) {\n        const label = formatElapsed(second);\n        labels.show(\n          { key: `time:${label}`, kind: \"time\", text: label },\n          x + 3,\n          DUPLEX_TIMELINE_HEIGHT,\n          1,\n        );\n      }\n    }\n    ctx.fillRect(width - 1, TOP - 2, 1, LANES_BOTTOM - TOP + 4);\n  }\n\n  /** Fades a lane's bars toward their tips. */\n  function laneMask(lane: DuplexTimelineLane) {\n    let mask = masks.get(lane);\n    if (!mask) {\n      const { top, height } = DUPLEX_TIMELINE_LANES[lane];\n      mask = ctx.createLinearGradient(0, top, 0, top + height);\n      mask.addColorStop(0, \"rgba(0, 0, 0, 0.5)\");\n      mask.addColorStop(0.5, \"rgba(0, 0, 0, 1)\");\n      mask.addColorStop(1, \"rgba(0, 0, 0, 0.5)\");\n      masks.set(lane, mask);\n    }\n    return mask;\n  }\n\n  /** Mirrored bars: lane color for speech, then backchannel or interruption. */\n  function drawLane(\n    lane: DuplexTimelineLane,\n    color: string,\n    spans: readonly Segment[],\n    now: number,\n  ) {\n    const box = DUPLEX_TIMELINE_LANES[lane];\n    const centre = Math.round(box.top + box.height / 2);\n    ctx.fillStyle = palette.grid;\n    ctx.fillRect(0, centre, width, 1);\n\n    const colors: Record<Segment[\"kind\"] | \"speech\", string> = {\n      speech: color,\n      backchannel: palette.backchannel,\n      interruption: palette.interruption,\n    };\n    const newest = Math.floor(now / SLOT_MS);\n    const count = Math.ceil(width / SLOT_PX) + 1;\n    for (let slot = newest; slot > newest - count; slot--) {\n      const level = history[lane].level(slot);\n      if (level <= AUDIBLE_LEVEL) continue;\n      const middle = (slot + 0.5) * SLOT_MS;\n      const segment = spans.find(\n        (span) => middle >= span.start && middle <= span.end,\n      );\n      ctx.fillStyle = colors[segment?.kind ?? \"speech\"];\n      const x = Math.round(xAt((slot + 1) * SLOT_MS, now)) - BAR_WIDTH;\n      const half = Math.max(1, Math.round(level * (box.height / 2 - 1)));\n      ctx.fillRect(x, centre - half, BAR_WIDTH, half * 2 + 1);\n    }\n\n    // Masking keeps any token color (oklch included) without parsing it.\n    // destination-in clears everything outside its shape, so clip to the lane.\n    ctx.save();\n    ctx.beginPath();\n    ctx.rect(0, box.top, width, box.height);\n    ctx.clip();\n    ctx.globalCompositeOperation = \"destination-in\";\n    ctx.fillStyle = laneMask(lane);\n    ctx.fillRect(0, box.top, width, box.height);\n    ctx.restore();\n  }\n\n  function dashedRule(x: number) {\n    ctx.strokeStyle = palette.text;\n    ctx.globalAlpha = 0.45;\n    ctx.lineWidth = 1;\n    ctx.setLineDash([3, 3]);\n    ctx.beginPath();\n    ctx.moveTo(Math.round(x) + 0.5, TOP - 2);\n    ctx.lineTo(Math.round(x) + 0.5, LANES_BOTTOM + 2);\n    ctx.stroke();\n    ctx.setLineDash([]);\n    ctx.globalAlpha = 1;\n  }\n\n  /** Handoff rules, with the reply latency between them. */\n  function drawHandoff(marker: DuplexTimelineMarker, now: number) {\n    if (marker.kind !== \"turn-end\" && marker.kind !== \"reply\") return;\n    const x = xAt(marker.at, now);\n    if (options.handoffs) dashedRule(x);\n    if (marker.kind === \"reply\" && options.latency) {\n      drawLatency(marker.id, xAt(marker.from, now), x, marker.at - marker.from);\n    }\n  }\n\n  /** A bracket between the lanes, labelled with the gap. */\n  function drawLatency(id: number, from: number, to: number, ms: number) {\n    const middle = Math.round(\n      DUPLEX_TIMELINE_LANES.agent.top + LANE_HEIGHT + LANE_GAP / 2,\n    );\n    const y = middle + 0.5;\n    const slow = ms >= options.slowReplyMs;\n    const label: LabelSpec = {\n      key: `latency:${String(id)}`,\n      kind: \"latency\",\n      text:\n        ms < 1000\n          ? `${String(Math.round(ms))}ms`\n          : `${(ms / 1000).toFixed(1)}s`,\n      slow,\n    };\n    const textWidth = labels.measure(label);\n    // Too narrow for the text: print it after the reply rule.\n    const inside = to - from > textWidth + 8;\n    const x = inside ? (from + to) / 2 - textWidth / 2 : to + 4;\n\n    // The line breaks around the text, so no background fill is needed.\n    ctx.strokeStyle = slow ? palette.slowReply : palette.text;\n    ctx.globalAlpha = 0.8;\n    ctx.beginPath();\n    ctx.moveTo(Math.round(from) + 0.5, y);\n    ctx.lineTo(Math.round(inside ? x - 3 : to) + 0.5, y);\n    if (inside) {\n      ctx.moveTo(Math.round(x + textWidth + 3) + 0.5, y);\n      ctx.lineTo(Math.round(to) + 0.5, y);\n    }\n    ctx.stroke();\n    ctx.globalAlpha = 1;\n    labels.show(label, Math.round(x), middle, 0.5);\n  }\n\n  /** Labels above the lanes; a colliding label drops to the second row. */\n  function drawSegmentLabels(named: SegmentLabel[], now: number) {\n    const rowEnds = Array<number>(LABEL_ROWS).fill(-Infinity);\n    for (const { id, kind, text, start } of named.sort(\n      (a, b) => a.start - b.start,\n    )) {\n      const x = Math.round(xAt(start, now));\n      const row = rowEnds.findIndex((end) => x > end + 8);\n      if (row < 0) continue;\n      const label: LabelSpec = { key: `${kind}:${String(id)}`, kind, text };\n      rowEnds[row] = x + labels.measure(label);\n      labels.show(label, x, row * LABEL_ROW + 2);\n    }\n  }\n\n  return {\n    setTrack(lane, track) {\n      meters[lane]?.dispose();\n      meters[lane] = track ? new LevelMeter(track) : null;\n    },\n    setMarkers(next) {\n      markers = next;\n      redraw();\n    },\n    setPalette(next) {\n      palette = next;\n      redraw();\n    },\n    setOptions({ labelClassNames, ...next }) {\n      options = next;\n      // Spans keep the classes and data-slow they were created with.\n      labels.clear();\n      labels.classNames = labelClassNames;\n      redraw();\n    },\n    setRunning(next) {\n      if (next === running) return;\n      running = next;\n      if (running) {\n        lastDraw = -Infinity;\n        frame = requestAnimationFrame(tick);\n      } else {\n        cancelAnimationFrame(frame);\n        frozenAt = performance.now();\n        draw(frozenAt);\n      }\n    },\n    reset() {\n      history.agent.clear();\n      history.user.clear();\n      frozenAt = startedAt = performance.now();\n      redraw();\n    },\n    dispose() {\n      cancelAnimationFrame(frame);\n      resize.disconnect();\n      labels.clear();\n      meters.agent?.dispose();\n      meters.user?.dispose();\n      meters.agent = null;\n      meters.user = null;\n    },\n  };\n}\n",
      "type": "registry:component",
      "target": "components/pipecat/duplex-timeline-renderer.ts"
    },
    {
      "path": "src/components/duplex-timeline-markers.ts",
      "content": "import { DUPLEX_TIMELINE_HISTORY_MS } from \"@/components/pipecat/duplex-timeline-renderer\";\n\n/** A moment on the duplex timeline. Times are performance.now() ms. */\nexport type DuplexTimelineMarker =\n  | { id: number; kind: \"turn-end\"; at: number }\n  | { id: number; kind: \"reply\"; at: number; from: number }\n  | {\n      id: number;\n      /** The user's turn started while the bot was replying. */\n      kind: \"interruption\";\n      /** The user's turn was announced. */\n      at: number;\n      /** The bot stopped, or null while it's still talking. */\n      cutAt: number | null;\n    }\n  | {\n      id: number;\n      /**\n       * The user's turn was announced just after the bot stopped. The server\n       * may have held the turn while classifying it, so only the audio shows\n       * whether the two overlapped; the timeline draws it as an interruption\n       * when they did.\n       */\n      kind: \"possible-interruption\";\n      at: number;\n      cutAt: number;\n    }\n  | {\n      id: number;\n      kind: \"bot-backchannel\";\n      at: number;\n      end: number | null;\n      text: string;\n    }\n  | { id: number; kind: \"user-backchannel\"; at: number; text: string };\n\n/** RTVI turn and backchannel events, stamped with performance.now(). */\nexport type DuplexTimelineEvent =\n  | {\n      type:\n        | \"connected\"\n        | \"disconnected\"\n        | \"user-started-speaking\"\n        | \"user-stopped-speaking\"\n        | \"bot-started-speaking\"\n        | \"bot-stopped-speaking\";\n      now: number;\n    }\n  | { type: \"bot-backchannel\" | \"user-backchannel\"; now: number; text: string };\n\nexport interface DuplexTimelineState {\n  markers: DuplexTimelineMarker[];\n  nextId: number;\n  userSpeaking: boolean;\n  botSpeaking: boolean;\n  /** The bot holds the floor (not backchannelling). */\n  botReplying: boolean;\n  /** The reply marker opened by the bot's current speech. */\n  replyId: number | null;\n  /** The bot's last reply ended uninterrupted. */\n  replyEndedAt: number | null;\n  /** The user's last turn ended, pending a reply. */\n  userStoppedAt: number | null;\n  /** The bot was talked over and is still speaking. */\n  interrupted: boolean;\n}\n\nexport const DUPLEX_TIMELINE_INITIAL_STATE: DuplexTimelineState = {\n  markers: [],\n  nextId: 0,\n  userSpeaking: false,\n  botSpeaking: false,\n  botReplying: false,\n  replyId: null,\n  replyEndedAt: null,\n  userStoppedAt: null,\n  interrupted: false,\n};\n\n/** How long a user turn may trail the end of a reply and still have overlapped it. */\nconst LATE_INTERRUPTION_MS = 1500;\n\ntype WithoutId<T> = T extends unknown ? Omit<T, \"id\"> : never;\n\n/** Adds a marker, dropping those older than the renderer's level history. */\nfunction withMarker(\n  state: DuplexTimelineState,\n  markers: DuplexTimelineMarker[],\n  marker: WithoutId<DuplexTimelineMarker>,\n): Pick<DuplexTimelineState, \"markers\" | \"nextId\"> {\n  const cutoff = marker.at - DUPLEX_TIMELINE_HISTORY_MS;\n  return {\n    markers: [\n      ...markers.filter((existing) => existing.at >= cutoff),\n      { ...marker, id: state.nextId },\n    ],\n    nextId: state.nextId + 1,\n  };\n}\n\n/** Ends open bot backchannels and interruptions; the same array if none were open. */\nfunction closeOpen(markers: DuplexTimelineMarker[], now: number) {\n  let changed = false;\n  const next = markers.map((marker) => {\n    if (marker.kind === \"bot-backchannel\" && marker.end === null) {\n      changed = true;\n      return { ...marker, end: now };\n    }\n    if (marker.kind === \"interruption\" && marker.cutAt === null) {\n      changed = true;\n      return { ...marker, cutAt: now };\n    }\n    return marker;\n  });\n  return changed ? next : markers;\n}\n\n/**\n * Turns RTVI turn and backchannel events into timeline markers: handoffs\n * with reply latency, interruptions, and backchannels on either side.\n * User turn events mark turns only; a user backchannel arrives on its own.\n */\nexport function reduceDuplexTimeline(\n  state: DuplexTimelineState,\n  event: DuplexTimelineEvent,\n): DuplexTimelineState {\n  const { now } = event;\n  switch (event.type) {\n    case \"connected\":\n      return { ...DUPLEX_TIMELINE_INITIAL_STATE, nextId: state.nextId };\n\n    case \"disconnected\":\n      return {\n        ...state,\n        markers: closeOpen(state.markers, now),\n        userSpeaking: false,\n        botSpeaking: false,\n        botReplying: false,\n        replyId: null,\n        interrupted: false,\n      };\n\n    case \"user-started-speaking\":\n      if (state.botReplying) {\n        return {\n          ...state,\n          ...withMarker(state, state.markers, {\n            kind: \"interruption\",\n            at: now,\n            cutAt: null,\n          }),\n          interrupted: true,\n          userSpeaking: true,\n        };\n      }\n      if (\n        state.replyEndedAt !== null &&\n        now - state.replyEndedAt < LATE_INTERRUPTION_MS\n      ) {\n        return {\n          ...state,\n          ...withMarker(state, state.markers, {\n            kind: \"possible-interruption\",\n            at: now,\n            cutAt: state.replyEndedAt,\n          }),\n          replyEndedAt: null,\n          userSpeaking: true,\n        };\n      }\n      return { ...state, userSpeaking: true };\n\n    case \"user-stopped-speaking\":\n      return {\n        ...state,\n        ...withMarker(state, state.markers, { kind: \"turn-end\", at: now }),\n        userSpeaking: false,\n        userStoppedAt: now,\n      };\n\n    case \"bot-started-speaking\": {\n      // Inside the user's turn it's a backchannel playing.\n      if (state.userSpeaking) return { ...state, botSpeaking: true };\n      const reply =\n        state.userStoppedAt === null\n          ? null\n          : withMarker(state, state.markers, {\n              kind: \"reply\",\n              at: now,\n              from: state.userStoppedAt,\n            });\n      return {\n        ...state,\n        ...reply,\n        botSpeaking: true,\n        botReplying: true,\n        replyId: reply ? state.nextId : null,\n        userStoppedAt: null,\n      };\n    }\n\n    case \"bot-stopped-speaking\":\n      return {\n        ...state,\n        markers: closeOpen(state.markers, now),\n        botSpeaking: false,\n        botReplying: false,\n        replyId: null,\n        replyEndedAt: state.botReplying && !state.interrupted ? now : null,\n        interrupted: false,\n      };\n\n    case \"bot-backchannel\": {\n      // Bot output follows bot-started-speaking, so the current speech may\n      // have read as a reply: undo that reply and keep the turn pending.\n      const reply = state.markers.find(\n        (marker) => marker.kind === \"reply\" && marker.id === state.replyId,\n      );\n      return {\n        ...state,\n        ...withMarker(\n          state,\n          state.markers.filter((marker) => marker !== reply),\n          { kind: \"bot-backchannel\", at: now, end: null, text: event.text },\n        ),\n        userStoppedAt:\n          reply?.kind === \"reply\" ? reply.from : state.userStoppedAt,\n        botReplying: false,\n        replyId: null,\n      };\n    }\n\n    case \"user-backchannel\":\n      return {\n        ...state,\n        ...withMarker(state, state.markers, {\n          kind: \"user-backchannel\",\n          at: now,\n          text: event.text,\n        }),\n      };\n  }\n}\n",
      "type": "registry:component",
      "target": "components/pipecat/duplex-timeline-markers.ts"
    },
    {
      "path": "src/lib/visualizer.ts",
      "content": "/** Track-derived silent/speaking states; connecting/thinking are explicit overrides. */\nexport type VisualizerState = \"connecting\" | \"silent\" | \"speaking\" | \"thinking\";\n\n/** One mel-spaced analyser band: its FFT bin range plus tilt boost. */\nexport interface VisualizerBand {\n  startBin: number;\n  endBin: number;\n  /** dB-space boost countering speech's high-frequency rolloff. */\n  tiltBoost: number;\n}\n\n/** Call dispose to release the AudioContext; smoothing here only reduces FFT jitter. */\nexport function createVisualizerAnalyser(track: MediaStreamTrack): {\n  analyser: AnalyserNode;\n  dispose: () => void;\n} {\n  const audioContext = new AudioContext();\n  const source = audioContext.createMediaStreamSource(new MediaStream([track]));\n  const analyser = audioContext.createAnalyser();\n  analyser.fftSize = 1024;\n  analyser.smoothingTimeConstant = 0.6;\n  source.connect(analyser);\n  return { analyser, dispose: () => void audioContext.close() };\n}\n\n/** Mel bands cover 200 Hz–8 kHz. A per-octave boost compensates speech rolloff so low bands do not dominate. */\nexport function createVoiceBands(\n  bandCount: number,\n  sampleRate: number,\n  frequencyBinCount: number,\n): VisualizerBand[] {\n  const nyquist = sampleRate / 2;\n  const MIN_FREQ = 200;\n  const MAX_FREQ = 8000;\n  const toMel = (hz: number) => 2595 * Math.log10(1 + hz / 700);\n  const toHz = (mel: number) => 700 * (Math.pow(10, mel / 2595) - 1);\n  const melMin = toMel(MIN_FREQ);\n  const melStep = (toMel(MAX_FREQ) - melMin) / bandCount;\n\n  const TILT_DB_PER_OCTAVE = 4;\n  const BYTES_PER_DB = 255 / 70;\n  const refCenter = Math.sqrt(toHz(melMin) * toHz(melMin + melStep));\n\n  return Array.from({ length: bandCount }, (_, i) => {\n    const startFreq = toHz(melMin + i * melStep);\n    const endFreq = toHz(melMin + (i + 1) * melStep);\n    const startBin = Math.round(\n      (startFreq / nyquist) * (frequencyBinCount - 1),\n    );\n    // Every band covers at least one bin, so high band counts can't\n    // round a band into permanent silence.\n    const endBin = Math.max(\n      Math.round((endFreq / nyquist) * (frequencyBinCount - 1)),\n      startBin + 1,\n    );\n    const center = Math.sqrt(startFreq * endFreq);\n    const tiltBoost =\n      TILT_DB_PER_OCTAVE * BYTES_PER_DB * Math.log2(center / refCenter);\n    return { startBin, endBin, tiltBoost };\n  });\n}\n\n/**\n * Averages a band's FFT bins and applies its tilt boost, returning 0–255.\n * The boost fades in with signal strength so the noise floor near\n * minDecibels isn't amplified into standing bars.\n */\nexport function readBand(spectrum: Uint8Array, band: VisualizerBand): number {\n  let sum = 0;\n  for (let j = band.startBin; j < band.endBin; j++) sum += spectrum[j] ?? 0;\n  const value = sum / (band.endBin - band.startBin);\n  return Math.min(255, value + band.tiltBoost * Math.min(1, value / 32));\n}\n\n/**\n * Resolves a visualizer color prop to a concrete canvas color:\n * \"currentColor\" resolves from the given element, \"--css-var\" names from\n * the document root (where the kit's theme tokens live).\n */\nexport function resolveVisualizerColor(\n  color: string,\n  element: Element | null,\n): string {\n  if (!color) return \"black\";\n  if (color === \"currentColor\") {\n    return element ? getComputedStyle(element).color || \"black\" : \"black\";\n  }\n  if (color.startsWith(\"--\")) {\n    return (\n      getComputedStyle(document.documentElement)\n        .getPropertyValue(color)\n        .trim() || \"black\"\n    );\n  }\n  return color;\n}\n",
      "type": "registry:lib"
    }
  ],
  "cssVars": {
    "light": {
      "agent-foreground": "oklch(0.585 0.233 277.117)",
      "client-foreground": "oklch(0.623 0.214 259.815)",
      "inactive-background": "oklch(0.702 0.189 22.23)"
    },
    "dark": {
      "agent-foreground": "oklch(0.585 0.233 277.117)",
      "client-foreground": "oklch(0.623 0.214 259.815)",
      "inactive-background": "oklch(0.702 0.189 22.23)"
    }
  },
  "docs": "Use DuplexTimeline inside a PipecatClientProvider; it reads both audio tracks, scrolls while the transport is ready, and marks backchannels from RTVI 2.2 bots. Use DuplexTimelineView with your own tracks and markers (useDuplexTimelineMarkers, or reduceDuplexTimeline from duplex-timeline-markers), and DuplexTimelineLegend to place the color key outside the timeline (with noLegend). Hide parts with the no* props (noBackchannels, noInterruptions, noLatency, noTimeLabels, noLegend…) and style labels with laneLabelClassName and the other *LabelClassName props.",
  "categories": [
    "voice"
  ],
  "type": "registry:component"
}