# All files / src/components/streams CoordinationGraph.tsx

0% Statements 0/108
0% Branches 0/1
0% Functions 0/1
0% Lines 0/108

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174

     import { useCallback, useMemo, type JSX, type MouseEvent } from "react";
    import {
      Background,
      BackgroundVariant,
      Controls,
      MiniMap,
      ReactFlow,
      type EdgeTypes,
      type Node,
      type NodeTypes,
    } from "@xyflow/react";
    import "@xyflow/react/dist/style.css";
    import type { Session, StreamData, StreamMessageData } from "../../hooks/types.js";
    import { SESSION_STATUS_VAR_NAMES, sessionStatusStyle } from "../../utils/sessionStatus.js";
    import {
      SESSION_NODE_TYPE,
      STREAM_NODE_TYPE,
      useCoordinationLayout,
      type CoordNodeData,
    } from "./useCoordinationLayout.js";
    import { SessionNode } from "./SessionNode.js";
    import { StreamNode } from "./StreamNode.js";
    import { MessageDotEdge } from "./MessageDotEdge.js";
    import styles from "./CoordinationGraph.module.scss";

    /** Fallback node color for the MiniMap when a CSS variable is unavailable. */
    const MINIMAP_FALLBACK_COLOR: string = "#6b7a8d";

    /** Props for {@link CoordinationGraph}. */
    export interface CoordinationGraphProps {
      /** Streams to visualize (already filtered for internals by the caller). */
      streams: StreamData[];
      /** All known sessions, used to resolve subscribers into nodes and colors. */
      sessions: Session[];
      /** Currently selected stream id (its hub node is highlighted). */
      selectedStreamId?: string;
      /** Called with a stream id when its hub node is clicked. */
      onSelectStream: (streamId: string) => void;
      /**
       * Live per-stream message buffers (ascending by seq). When a stream's newest
       * seq advances, its edges animate a message dot. Omit for a static graph.
       */
      recentMessages?: Record<string, StreamMessageData[]>;
      /** Resolved theme id; recomputes MiniMap colors when the theme changes. */
      resolvedThemeId: string;
    }

    /** Custom node type registry for the coordination graph. */
    const nodeTypes: NodeTypes = {
      [SESSION_NODE_TYPE]: SessionNode,
      [STREAM_NODE_TYPE]: StreamNode,
    };

    /** Custom edge type registry — the animated message-dot edge. */
    const edgeTypes: EdgeTypes = {
      messageDot: MessageDotEdge,
    };

    /**
     * Live bipartite network graph of agent sessions and IPC streams. Sessions and
     * stream "hubs" are dagre-laid-out (deterministic); two-party pipes collapse to
     * direct edges. Read-only ("watch") - clicking a hub selects its stream.
     */
    export function CoordinationGraph({
      streams,
      sessions,
      selectedStreamId,
      onSelectStream,
      recentMessages,
      resolvedThemeId,
    }: CoordinationGraphProps): JSX.Element {
      const layout = useCoordinationLayout(streams, sessions);

      // Stamp each edge with its stream's latest message seq. When that seq advances,
      // MessageDotEdge fires a one-shot dot; stamping by streamId fans the pulse out
      // across all of a stream's edges (writer -> hub and hub -> readers).
      const edges = useMemo(
        () =>
          layout.edges.map((edge) => {
            if (!edge.data) {
              return edge;
            }
            const seq = recentMessages?.[edge.data.streamId]?.at(-1)?.seq;
            return seq === undefined ? edge : { ...edge, data: { ...edge.data, pulseSeq: seq } };
          }),
        [layout.edges, recentMessages],
      );

      // Mark the selected stream hub so React Flow applies node selection styling.
      const nodes = useMemo(
        () =>
          layout.nodes.map((node) => {
            const data = node.data as CoordNodeData;
            const selected = data.kind === "stream" && data.stream.id === selectedStreamId;
            return selected ? { ...node, selected: true } : node;
          }),
        [layout.nodes, selectedStreamId],
      );

      /** Theme-resolved colors for the session-status CSS vars (MiniMap needs concrete colors). */
      const resolvedStatusColors = useMemo(() => {
        const style = getComputedStyle(document.documentElement);
        const colors: Record<string, string> = {};
        for (const varName of SESSION_STATUS_VAR_NAMES) {
          colors[varName] = style.getPropertyValue(varName).trim() || MINIMAP_FALLBACK_COLOR;
        }
        return colors;
      }, [resolvedThemeId]);

      const onNodeClick = useCallback(
        (_event: MouseEvent, node: Node) => {
          const data = node.data as CoordNodeData;
          if (data.kind === "stream") {
            onSelectStream(data.stream.id);
          }
        },
        [onSelectStream],
      );

      const minimapNodeColor = useCallback(
        (node: Node): string => {
          const data = node.data as CoordNodeData;
          if (data.kind === "session") {
            return (
              resolvedStatusColors[sessionStatusStyle(data.session.status, data.external).varName] ??
              MINIMAP_FALLBACK_COLOR
            );
          }
          return MINIMAP_FALLBACK_COLOR;
        },
        [resolvedStatusColors],
      );

      if (layout.nodes.length === 0) {
        return (
          <div className={styles.empty} data-testid="coordination-graph-empty">
            No active streams to visualize
          </div>
        );
      }

      return (
        <div className={styles.graphContainer} data-testid="coordination-graph">
          <ReactFlow
            nodes={nodes}
            edges={edges}
            nodeTypes={nodeTypes}
            edgeTypes={edgeTypes}
            onNodeClick={onNodeClick}
            // Read-only "watch" graph: there is no edit mode and no onConnect, so
            // disable the drag-to-connect affordance on node handles (#1303).
            nodesConnectable={false}
            fitView
            fitViewOptions={{ padding: 0.2 }}
            minZoom={0.3}
            maxZoom={2}
          >
            <Background
              variant={BackgroundVariant.Dots}
              gap={24}
              size={1}
              color="var(--text-disabled)"
            />
            <Controls showInteractive={false} />
            <MiniMap
              nodeColor={minimapNodeColor}
              maskColor="var(--bg-overlay)"
              style={{ background: "var(--bg-inset)" }}
            />
          </ReactFlow>
        </div>
      );
    }