|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import React, { useMemo } from "react"; |
| 4 | +import { |
| 5 | + ReactFlow, |
| 6 | + Edge, |
| 7 | + Background, |
| 8 | + Controls, |
| 9 | + BackgroundVariant, |
| 10 | +} from "@xyflow/react"; |
| 11 | +import "@xyflow/react/dist/style.css"; |
| 12 | +import { |
| 13 | + StartEndNodeComponent, |
| 14 | + MainAgentNodeComponent, |
| 15 | + SubAgentNodeComponent, |
| 16 | + ToolNodeComponent, |
| 17 | +} from "./nodes"; |
| 18 | +import { ConfigurableFieldSubAgentsMetadata } from "@/types/configurable"; |
| 19 | + |
| 20 | +interface AgentGraphVisualizationProps { |
| 21 | + configurable: Record<string, any>; |
| 22 | + name: string; |
| 23 | +} |
| 24 | + |
| 25 | +interface SubAgentNode { |
| 26 | + id: string; |
| 27 | + type: "subagent"; |
| 28 | + position: { x: number; y: number }; |
| 29 | + data: { label: string; type: "subagent" }; |
| 30 | +} |
| 31 | + |
| 32 | +interface ToolNode { |
| 33 | + id: string; |
| 34 | + type: "tool"; |
| 35 | + position: { x: number; y: number }; |
| 36 | + data: { label: string; type: "tool" }; |
| 37 | +} |
| 38 | + |
| 39 | +interface StartEndNode { |
| 40 | + id: string; |
| 41 | + type: "startEnd"; |
| 42 | + position: { x: number; y: number }; |
| 43 | + data: { label: string; type: "start" | "end" }; |
| 44 | +} |
| 45 | + |
| 46 | +interface MainAgentNode { |
| 47 | + id: string; |
| 48 | + type: "mainAgent"; |
| 49 | + position: { x: number; y: number }; |
| 50 | + data: { label: string; type: "mainAgent" }; |
| 51 | +} |
| 52 | + |
| 53 | +type GraphNode = SubAgentNode | ToolNode | StartEndNode | MainAgentNode; |
| 54 | + |
| 55 | +const nodeTypes = { |
| 56 | + startEnd: StartEndNodeComponent, |
| 57 | + mainAgent: MainAgentNodeComponent, |
| 58 | + subagent: SubAgentNodeComponent, |
| 59 | + tool: ToolNodeComponent, |
| 60 | +}; |
| 61 | + |
| 62 | +export function AgentGraphVisualization({ |
| 63 | + configurable, |
| 64 | + name, |
| 65 | +}: AgentGraphVisualizationProps) { |
| 66 | + const { nodes, edges } = useMemo(() => { |
| 67 | + const nodes: GraphNode[] = []; |
| 68 | + const edges: Edge[] = []; |
| 69 | + |
| 70 | + let currentY = 50; |
| 71 | + const centerX = 400; |
| 72 | + const nodeSpacing = 150; |
| 73 | + |
| 74 | + nodes.push({ |
| 75 | + id: "start", |
| 76 | + type: "startEnd", |
| 77 | + position: { x: centerX - 50, y: currentY }, |
| 78 | + data: { label: "Start", type: "start" }, |
| 79 | + }); |
| 80 | + |
| 81 | + currentY += nodeSpacing; |
| 82 | + |
| 83 | + nodes.push({ |
| 84 | + id: "main-agent", |
| 85 | + type: "mainAgent", |
| 86 | + position: { x: centerX - 80, y: currentY }, |
| 87 | + data: { label: name, type: "mainAgent" }, |
| 88 | + }); |
| 89 | + |
| 90 | + edges.push({ |
| 91 | + id: "start-to-agent", |
| 92 | + source: "start", |
| 93 | + target: "main-agent", |
| 94 | + type: "smoothstep", |
| 95 | + sourceHandle: null, |
| 96 | + targetHandle: null, |
| 97 | + }); |
| 98 | + |
| 99 | + currentY += nodeSpacing; |
| 100 | + |
| 101 | + // Get subagents from configurable |
| 102 | + const subagents: NonNullable< |
| 103 | + ConfigurableFieldSubAgentsMetadata["default"] |
| 104 | + > = configurable?.subagents || []; |
| 105 | + |
| 106 | + // Layout subagents horizontally |
| 107 | + if (subagents.length > 0) { |
| 108 | + const subagentSpacing = 280; |
| 109 | + const subagentWidth = 140; |
| 110 | + const totalWidth = (subagents.length - 1) * subagentSpacing; |
| 111 | + const startX = centerX - totalWidth / 2 - subagentWidth / 2; |
| 112 | + |
| 113 | + subagents.forEach((subagent, index) => { |
| 114 | + const nodeId = `subagent-${index}`; |
| 115 | + const subagentLabel = |
| 116 | + typeof subagent === "string" |
| 117 | + ? subagent |
| 118 | + : subagent?.name || String(subagent); |
| 119 | + nodes.push({ |
| 120 | + id: nodeId, |
| 121 | + type: "subagent", |
| 122 | + position: { x: startX + index * subagentSpacing, y: currentY }, |
| 123 | + data: { label: subagentLabel, type: "subagent" }, |
| 124 | + }); |
| 125 | + |
| 126 | + edges.push({ |
| 127 | + id: `agent-to-${nodeId}`, |
| 128 | + source: "main-agent", |
| 129 | + target: nodeId, |
| 130 | + type: "smoothstep", |
| 131 | + }); |
| 132 | + }); |
| 133 | + |
| 134 | + currentY += nodeSpacing; |
| 135 | + } |
| 136 | + |
| 137 | + // Layout tools below subagents - each subagent gets its own tools |
| 138 | + if (subagents.length > 0) { |
| 139 | + let maxToolsCount = 0; |
| 140 | + |
| 141 | + subagents.forEach((subagent) => { |
| 142 | + const subagentTools = subagent?.tools || []; |
| 143 | + maxToolsCount = Math.max(maxToolsCount, subagentTools.length); |
| 144 | + }); |
| 145 | + |
| 146 | + subagents.forEach((subagent, subagentIndex) => { |
| 147 | + const subagentSpacing = 280; |
| 148 | + const subagentWidth = 140; |
| 149 | + const totalWidth = (subagents.length - 1) * subagentSpacing; |
| 150 | + const startX = centerX - totalWidth / 2 - subagentWidth / 2; |
| 151 | + const subagentX = startX + subagentIndex * subagentSpacing; |
| 152 | + |
| 153 | + // Get tools for this specific subagent |
| 154 | + const subagentTools = subagent?.tools || []; |
| 155 | + |
| 156 | + // Create tools for this specific subagent |
| 157 | + subagentTools.forEach((tool, toolIndex) => { |
| 158 | + const nodeId = `tool-${subagentIndex}-${toolIndex}`; |
| 159 | + const toolLabel = tool; |
| 160 | + |
| 161 | + const toolSpacing = 90; |
| 162 | + const totalToolsHeight = (maxToolsCount - 1) * toolSpacing; |
| 163 | + const thisSubagentHeight = (subagentTools.length - 1) * toolSpacing; |
| 164 | + const offset = (totalToolsHeight - thisSubagentHeight) / 2; |
| 165 | + |
| 166 | + const toolY = currentY + offset + toolIndex * toolSpacing; |
| 167 | + |
| 168 | + nodes.push({ |
| 169 | + id: nodeId, |
| 170 | + type: "tool", |
| 171 | + position: { x: subagentX, y: toolY }, |
| 172 | + data: { label: toolLabel, type: "tool" }, |
| 173 | + }); |
| 174 | + |
| 175 | + // Only connect first tool to subagent, other tools are just stacked |
| 176 | + if (toolIndex === 0) { |
| 177 | + edges.push({ |
| 178 | + id: `subagent-${subagentIndex}-to-${nodeId}`, |
| 179 | + source: `subagent-${subagentIndex}`, |
| 180 | + target: nodeId, |
| 181 | + type: "smoothstep", |
| 182 | + style: { |
| 183 | + strokeDasharray: "5,5", |
| 184 | + strokeWidth: 2, |
| 185 | + stroke: "#64748b", |
| 186 | + }, |
| 187 | + }); |
| 188 | + } |
| 189 | + }); |
| 190 | + }); |
| 191 | + |
| 192 | + // Update currentY to account for the tallest column of tools |
| 193 | + if (maxToolsCount > 0) { |
| 194 | + currentY += nodeSpacing + (maxToolsCount - 1) * 90; |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + // End node - center it below the middle subagent or main agent |
| 199 | + const endX = subagents.length > 0 ? centerX - 50 : centerX - 50; |
| 200 | + |
| 201 | + nodes.push({ |
| 202 | + id: "end", |
| 203 | + type: "startEnd", |
| 204 | + position: { x: endX, y: currentY }, |
| 205 | + data: { label: "End", type: "end" }, |
| 206 | + }); |
| 207 | + |
| 208 | + // Connect terminal nodes to end - always from subagents to end |
| 209 | + if (subagents.length > 0) { |
| 210 | + // Subagents always connect to end (tools are intermediate) |
| 211 | + subagents.forEach((_, index) => { |
| 212 | + edges.push({ |
| 213 | + id: `subagent-${index}-to-end`, |
| 214 | + source: `subagent-${index}`, |
| 215 | + target: "end", |
| 216 | + type: "smoothstep", |
| 217 | + }); |
| 218 | + }); |
| 219 | + } else { |
| 220 | + // If no subagents, main agent connects to end |
| 221 | + edges.push({ |
| 222 | + id: "main-agent-to-end", |
| 223 | + source: "main-agent", |
| 224 | + target: "end", |
| 225 | + type: "smoothstep", |
| 226 | + }); |
| 227 | + } |
| 228 | + |
| 229 | + return { nodes, edges }; |
| 230 | + }, [configurable, name]); |
| 231 | + |
| 232 | + return ( |
| 233 | + <div className="h-full min-h-[80vh] w-full rounded-lg border bg-gray-50"> |
| 234 | + <ReactFlow |
| 235 | + nodes={nodes} |
| 236 | + edges={edges} |
| 237 | + nodeTypes={nodeTypes} |
| 238 | + fitView |
| 239 | + attributionPosition="bottom-left" |
| 240 | + proOptions={{ hideAttribution: true }} |
| 241 | + defaultEdgeOptions={{ |
| 242 | + type: "smoothstep", |
| 243 | + style: { strokeWidth: 2, stroke: "#64748b" }, |
| 244 | + markerEnd: { |
| 245 | + type: "arrowclosed", |
| 246 | + color: "#64748b", |
| 247 | + }, |
| 248 | + }} |
| 249 | + > |
| 250 | + <Background |
| 251 | + variant={BackgroundVariant.Dots} |
| 252 | + gap={20} |
| 253 | + size={1} |
| 254 | + /> |
| 255 | + <Controls /> |
| 256 | + </ReactFlow> |
| 257 | + </div> |
| 258 | + ); |
| 259 | +} |
0 commit comments