{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "graph-node-wire",
  "title": "Graph Node Wire",
  "description": "A wire between two ports, as a React Flow edge type: a curve in its tone with a drifting signal dot, that unfurls on springs when it is connected and loops back under its nodes. Includes the line drawn while connecting.",
  "dependencies": [
    "@xyflow/react@^12.12.1",
    "lucide-react@^1.41.0",
    "motion@^13.2.0"
  ],
  "registryDependencies": [
    "button",
    "@pipecat/graph"
  ],
  "files": [
    {
      "path": "src/components/graph/node-wire.tsx",
      "content": "\"use client\";\n\nimport {\n  type ConnectionLineComponentProps,\n  type Edge,\n  EdgeLabelRenderer,\n  type EdgeProps,\n  useInternalNode,\n} from \"@xyflow/react\";\nimport {\n  type MotionValue,\n  motion,\n  useReducedMotion,\n  useSpring,\n  useTransform,\n} from \"motion/react\";\nimport { PlusIcon } from \"lucide-react\";\nimport { useEffect } from \"react\";\n\nimport {\n  type GraphTone,\n  graphToneStyle,\n} from \"@/components/pipecat/graph/tone\";\nimport { Button } from \"@/components/ui/button\";\nimport { cn } from \"@/lib/utils\";\n\ntype Point = { x: number; y: number };\ntype Span = { top: number; bottom: number };\n\n/**\n * A wire is two cubic segments through a midpoint. Forward and backward wires\n * share this shape, so one can morph into the other when a node is dragged\n * past its neighbour.\n */\ntype Shape = { c1: Point; c2: Point; mid: Point; c3: Point; c4: Point };\n\n/** How far a wire runs straight out of a port before it turns. */\nconst MIN_REACH = 36;\nconst LOOP_REACH = { min: 64, max: 120 };\n/** The hook into the target is tighter, so it clears what sits to the target's left. */\nconst LOOP_ARRIVAL = 46;\n/** A target at least this far right of the source gets a plain S-curve. */\nconst FORWARD_SLACK = 24;\n/** Wires going down and wires going up use different lanes, so loops don't overlap. */\nconst LANE = 14;\nconst ROW_GAP = 40;\nconst UNDERPASS = 64;\n\nfunction between(a: Point, b: Point): Point {\n  return { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 };\n}\n\n/**\n * The y a backward wire travels along: the gap between the two nodes' rows, or\n * underneath both when they sit side by side.\n */\nfunction gutterBetween(source: Span, target: Span) {\n  if (target.top >= source.bottom + ROW_GAP)\n    return (source.bottom + target.top) / 2 - LANE;\n  if (source.top >= target.bottom + ROW_GAP)\n    return (target.bottom + source.top) / 2 + LANE;\n  return Math.max(source.bottom, target.bottom) + UNDERPASS;\n}\n\n/**\n * The control points of a wire from `source` to `target`. It leaves heading\n * right and arrives from the left, whichever way the ports face.\n */\nexport function wireShape(\n  source: Point,\n  target: Point,\n  gutterY: number | null = null,\n): Shape {\n  if (target.x - source.x >= FORWARD_SLACK) {\n    // One S-curve, split at its middle so it has the same shape as a loop.\n    const reach = Math.max(MIN_REACH, (target.x - source.x) / 2);\n    const out = { x: source.x + reach, y: source.y };\n    const into = { x: target.x - reach, y: target.y };\n    const c1 = between(source, out);\n    const c4 = between(into, target);\n    const top = between(out, into);\n    const c2 = between(c1, top);\n    const c3 = between(top, c4);\n    return { c1, c2, mid: between(c2, c3), c3, c4 };\n  }\n  // The target is behind the source: hook out to the right, travel back along\n  // the gutter, and hook into the target from its left.\n  const reach = Math.min(\n    LOOP_REACH.max,\n    Math.max(LOOP_REACH.min, (source.x - target.x) / 4),\n  );\n  const y = gutterY ?? (source.y + target.y) / 2;\n  return {\n    c1: { x: source.x + reach, y: source.y },\n    c2: { x: source.x + reach, y },\n    mid: { x: (source.x + target.x) / 2, y },\n    c3: { x: target.x - LOOP_ARRIVAL, y },\n    c4: { x: target.x - LOOP_ARRIVAL, y: target.y },\n  };\n}\n\n/** A wire's control points trail their ports on this, so it whips and settles. */\nconst SPRING = { stiffness: 170, damping: 9, mass: 0.9 };\n/** How long a wire stays elastic after it is connected or re-routed. */\nconst BOUNCE_MS = 1200;\n\n/**\n * Which bounce each value last played, and until when. Bookkeeping for\n * animations that live outside React, keyed by their motion value.\n */\nconst bounces = new WeakMap<\n  MotionValue<number>,\n  { key: string; until: number }\n>();\n\n/**\n * A motion value that follows `target`. It springs only while it is bouncing:\n * always when `bounce` is true, and for a moment each time `bounce` changes to\n * a new key. Otherwise it jumps, so the wire stays rigid as nodes move.\n */\nfunction useSpringFollow(\n  target: number,\n  from: number,\n  bounce: string | true | undefined,\n) {\n  const reduced = useReducedMotion();\n  const value = useSpring(from, SPRING);\n  // Motion's springs live outside React: a port position React Flow has just\n  // worked out has to be pushed into them.\n  useEffect(() => {\n    let springing = bounce === true;\n    if (typeof bounce === \"string\") {\n      const now = performance.now();\n      const last = bounces.get(value);\n      if (last?.key !== bounce)\n        bounces.set(value, { key: bounce, until: now + BOUNCE_MS });\n      springing = last?.key !== bounce || now < last.until;\n    }\n    if (springing && !reduced) value.set(target);\n    else value.jump(target);\n  }, [value, target, reduced, bounce]);\n  return value;\n}\n\ntype WireOptions = {\n  gutterY?: number | null;\n  /** Extra droop in the middle, for a wire that isn't connected yet. */\n  sag?: number;\n  bounce?: string | true;\n};\n\n/**\n * The path of a wire whose ends are pinned to their ports. While it bounces,\n * its middle trails on springs; a wire that bounces from the start unfurls\n * from its source.\n */\nfunction useWire(source: Point, target: Point, options: WireOptions = {}) {\n  const { gutterY = null, sag = 0, bounce } = options;\n  const shape = wireShape(source, target, gutterY);\n  const unfurl = bounce !== undefined;\n  const x = (to: number) => (unfurl ? source.x : to);\n  const y = (to: number) => (unfurl ? source.y : to);\n\n  const c1x = useSpringFollow(shape.c1.x, x(shape.c1.x), bounce);\n  const c1y = useSpringFollow(shape.c1.y, y(shape.c1.y), bounce);\n  const c2x = useSpringFollow(shape.c2.x, x(shape.c2.x), bounce);\n  const c2y = useSpringFollow(shape.c2.y + sag, y(shape.c2.y + sag), bounce);\n  const midX = useSpringFollow(shape.mid.x, x(shape.mid.x), bounce);\n  const midY = useSpringFollow(shape.mid.y + sag, y(shape.mid.y + sag), bounce);\n  const c3x = useSpringFollow(shape.c3.x, x(shape.c3.x), bounce);\n  const c3y = useSpringFollow(shape.c3.y + sag, y(shape.c3.y + sag), bounce);\n  const c4x = useSpringFollow(shape.c4.x, x(shape.c4.x), bounce);\n  const c4y = useSpringFollow(shape.c4.y, y(shape.c4.y), bounce);\n\n  const path = useTransform(\n    () =>\n      `M${source.x} ${source.y} C${c1x.get()} ${c1y.get()} ${c2x.get()} ${c2y.get()} ${midX.get()} ${midY.get()} C${c3x.get()} ${c3y.get()} ${c4x.get()} ${c4y.get()} ${target.x} ${target.y}`,\n  );\n  return { path, midX, midY };\n}\n\nfunction useGutter(sourceId: string, targetId: string) {\n  const source = useInternalNode(sourceId);\n  const target = useInternalNode(targetId);\n  if (!source || !target) return null;\n  const span = (node: NonNullable<typeof source>) => {\n    const top = node.internals.positionAbsolute.y;\n    return { top, bottom: top + (node.measured.height ?? 0) };\n  };\n  return gutterBetween(span(source), span(target));\n}\n\nexport type NodeWireData = {\n  /** The wire's tone. Left out, it takes the graph's. */\n  tone?: GraphTone;\n  /** A dashed wire with no signal dot, for a path that loops back. */\n  dashed?: boolean;\n  /** The dot drifting along the wire, showing which way it flows. On unless `false`. */\n  signal?: boolean;\n  /** The wire in use: heavier, with a halo in its tone. */\n  active?: boolean;\n  /** Give this a new value to make the wire spring, as when it is re-routed. */\n  bounce?: string;\n};\n\nexport type NodeWireEdge = Edge<NodeWireData, \"wire\">;\n\n/**\n * A wire between two ports, as a React Flow edge type. It carries a drifting\n * signal dot, and unfurls on springs when it is connected. Its `label` sits on\n * its middle.\n */\nexport function NodeWire({\n  source,\n  target,\n  sourceX,\n  sourceY,\n  targetX,\n  targetY,\n  data,\n  selected,\n  label,\n}: EdgeProps<NodeWireEdge>) {\n  const gutterY = useGutter(source, target);\n  const { path, midX, midY } = useWire(\n    { x: sourceX, y: sourceY },\n    { x: targetX, y: targetY },\n    { gutterY, bounce: data?.bounce },\n  );\n  const active = data?.active === true;\n  const dashed = data?.dashed === true;\n  const signal = data?.signal !== false && !dashed;\n\n  return (\n    <g\n      data-slot=\"node-wire\"\n      data-tone={data?.tone}\n      data-active={active || undefined}\n      style={graphToneStyle(data?.tone)}\n    >\n      {active ? (\n        <motion.path\n          d={path}\n          className=\"stroke-graph-tone/30 fill-none\"\n          strokeWidth={8}\n          strokeLinecap=\"round\"\n        />\n      ) : null}\n      <motion.path\n        d={path}\n        className={cn(\n          \"stroke-graph-tone fill-none\",\n          selected && \"stroke-foreground\",\n        )}\n        strokeWidth={selected || active ? 3 : 2}\n        strokeLinecap=\"round\"\n        strokeDasharray={dashed ? \"7 7\" : undefined}\n      />\n      {signal ? (\n        // One dot per 56px of wire, drifting along it.\n        <motion.path\n          d={path}\n          className=\"animate-wire-pulse stroke-graph-tone fill-none motion-reduce:animate-none\"\n          strokeWidth={5}\n          strokeLinecap=\"round\"\n          strokeDasharray=\"0.1 56\"\n        />\n      ) : null}\n      {/* A wide invisible stroke, so the wire is easy to click. */}\n      <motion.path\n        d={path}\n        className=\"react-flow__edge-interaction fill-none stroke-transparent\"\n        strokeWidth={24}\n      />\n      {label ? (\n        <EdgeLabelRenderer>\n          {/* Labels are drawn outside the wire, so its tone is put in scope again. */}\n          <motion.div\n            data-slot=\"node-wire-label\"\n            data-tone={data?.tone}\n            data-selected={selected || undefined}\n            className=\"group/node-wire-label nodrag nopan pointer-events-auto absolute top-0 left-0 -translate-1/2\"\n            style={{ ...graphToneStyle(data?.tone), x: midX, y: midY }}\n          >\n            {label}\n          </motion.div>\n        </EdgeLabelRenderer>\n      ) : null}\n    </g>\n  );\n}\n\n/** An icon-only button needs a name: one of these has to be given. */\ntype Named =\n  | { \"aria-label\": string }\n  | { \"aria-labelledby\": string }\n  | { children: React.ReactNode };\n\nexport type NodeWireActionProps = React.ComponentProps<typeof Button> & Named;\n\n/**\n * A small button for the middle of a wire, in the wire's tone: pass it as the\n * edge's `label`. It swells under the pointer, and inverts while it has\n * something open or its wire is selected.\n */\nexport function NodeWireAction({\n  className,\n  children,\n  ...props\n}: NodeWireActionProps) {\n  return (\n    <Button\n      variant=\"outline\"\n      size=\"icon-xs\"\n      {...props}\n      data-slot=\"node-wire-action\"\n      className={cn(\n        \"border-graph-tone bg-card text-graph-tone-ink size-4 rounded-full border-2 [&_svg]:stroke-[3.5] [&_svg:not([class*='size-'])]:size-2.5\",\n        // An overshooting ease stands in for a spring.\n        \"transition-[scale,background-color,color] duration-200 ease-[cubic-bezier(0.34,1.56,0.64,1)] hover:scale-150 active:scale-125 motion-reduce:transition-none\",\n        \"hover:bg-card hover:text-graph-tone-ink dark:border-graph-tone dark:bg-card dark:hover:bg-card\",\n        \"aria-expanded:bg-foreground aria-expanded:text-background dark:aria-expanded:bg-foreground aria-expanded:scale-150\",\n        \"group-data-selected/node-wire-label:bg-foreground group-data-selected/node-wire-label:text-background dark:group-data-selected/node-wire-label:bg-foreground group-data-selected/node-wire-label:scale-150\",\n        className,\n      )}\n    >\n      {children ?? <PlusIcon aria-hidden />}\n    </Button>\n  );\n}\n\n/** How far a loose wire droops before it finds a port. */\nconst SLACK = 30;\n\n/**\n * The wire being drawn, as React Flow's connection line. It hangs like a loose\n * rope while it follows the pointer, pulls taut when it snaps to a port that\n * fits, and turns red and dashed over one that doesn't.\n */\nexport function NodeWireConnection({\n  fromX,\n  fromY,\n  toX,\n  toY,\n  connectionStatus,\n}: ConnectionLineComponentProps) {\n  const snapped = connectionStatus === \"valid\";\n  const refused = connectionStatus === \"invalid\";\n  const { path } = useWire(\n    { x: fromX, y: fromY },\n    { x: toX, y: toY },\n    { sag: snapped ? 0 : SLACK, bounce: true },\n  );\n\n  return (\n    <g\n      data-slot=\"node-wire-connection\"\n      style={graphToneStyle(refused ? \"red\" : undefined)}\n    >\n      <motion.path\n        d={path}\n        className=\"stroke-graph-tone fill-none\"\n        strokeWidth={snapped ? 3 : 2}\n        strokeLinecap=\"round\"\n        strokeDasharray={refused ? \"6 7\" : undefined}\n      />\n      {snapped ? null : (\n        <circle cx={toX} cy={toY} r={5} className=\"fill-graph-tone\" />\n      )}\n    </g>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/pipecat/graph/node-wire.tsx"
    }
  ],
  "cssVars": {
    "theme": {
      "animate-wire-pulse": "wire-pulse 1.8s linear infinite"
    }
  },
  "css": {
    "@keyframes wire-pulse": {
      "to": {
        "stroke-dashoffset": "-56"
      }
    }
  },
  "docs": "Register NodeWire as an edge type and NodeWireConnection as the connection line on Graph: edgeTypes={{ wire: NodeWire }} connectionLineComponent={NodeWireConnection}. Set an edge's type to \"wire\" and its tone, dashed, signal, active and bounce through data. NodeWireAction is a button in the wire's tone for the edge's label.",
  "categories": [
    "graph"
  ],
  "type": "registry:component"
}