ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

基于 React Flow 的 Agent 工作流可视化编排:节点连线与执行状态动态高亮

基于 React Flow 的 Agent 工作流可视化编排:节点连线与执行状态动态高亮 基于 React Flow 的 Agent 工作流可视化编排节点连线与执行状态动态高亮在企业级 AI Agent 平台或自动化低代码系统的研发过程中如果所有的工作流编排仅停留在后端的 JSON 配置文件或纯文字 Prompt 中无论是对于业务开发者还是普通用户系统都像是一个冰冷不可见的“黑盒”用户无法直观理解当前 Agent 正在执行哪一步、为什么会跳转到某个分支、各个工具之间的输入输出依赖关系是怎样的当工作流发生执行超时或异常中断时排障人员只能去翻几万行的日志文本排查效率极低。将 Agent 的 DAG 有向无环图与循环状态机进行**“前端可视化交互编排Visual Workflow Graph”**是现代 AI 应用体验升级的核心标配。基于React FlowPro 级前端节点连线与流程图渲染引擎结合自定义复合节点Custom Nodes、SVG 动态粒子发光边Animated Glow Edges与SSE 流式执行状态机我们能够让用户在浏览器中像搭积木一样自由拖拽连线编排 Agent并在执行时实时呈现如科幻电影般动态脉冲发光的状态跃迁大屏。Agent 可视化编排拓扑与数据模型┌─────────────────────────────────────────────────────────────┐ │ 【React Flow 画布工作台 (Canvas)】 │ │ │ │ [Prompt 输入节点] ────(动态数据流连线)────► [LLM 规划节点] │ │ │ │ │ ┌──────────────────────────┴───────┐ │ │ ▼ (条件分支: 状态 SUCCESS / ACTIVE) ▼ │ │ 【代码生成节点 (正在脉冲发光 )】 [API 查询节点]│ │ │ │ │ ▼ (实时数据回传) │ │ [沙箱单测执行节点 (挂起等待)] │ └─────────────────────────────────────────────────────────────┘核心实现一自定义 Agent 复合节点AgentTaskNode在 React Flow 中定义支持展示执行状态、耗时、Token 消耗以及动态插槽Handle的自定义节点// components/AgentTaskNode.tsx import React, { memo } from react; import { Handle, Position, NodeProps } from reactflow; import { clsx } from clsx; export type NodeExecutionStatus IDLE | RUNNING | SUCCESS | FAILED; export interface AgentNodeData { title: string; role: string; status: NodeExecutionStatus; tokensUsed?: number; durationMs?: number; outputPreview?: string; } export const AgentTaskNode memo(({ data, isConnectable }: NodePropsAgentNodeData) { const { title, role, status, tokensUsed, durationMs, outputPreview } data; const statusStyles { IDLE: border-slate-700 bg-slate-900 text-slate-400, RUNNING: border-cyan-400 bg-slate-900 text-cyan-300 shadow-[0_0_25px_rgba(6,182,212,0.4)] animate-pulse, SUCCESS: border-emerald-500 bg-slate-900 text-emerald-300 shadow-[0_0_15px_rgba(16,185,129,0.2)], FAILED: border-rose-500 bg-slate-900 text-rose-300 shadow-[0_0_15px_rgba(244,63,94,0.3)], }; return ( div className{clsx( w-64 rounded-2xl border-2 p-4 transition-all duration-300 backdrop-blur-md, statusStyles[status] )} {/* 顶部入站连接桩 (Target Handle) */} Handle typetarget position{Position.Top} isConnectable{isConnectable} classNamew-3 h-3 bg-cyan-400 border-2 border-slate-950 / div classNameflex items-center justify-between pb-2 border-b border-slate-800 div classNameflex items-center gap-2 span className{clsx(w-2 h-2 rounded-full, { bg-slate-500: status IDLE, bg-cyan-400 animate-ping: status RUNNING, bg-emerald-400: status SUCCESS, bg-rose-500: status FAILED, })} / span classNamefont-bold text-xs text-white{title}/span /div span classNametext-[10px] font-mono px-2 py-0.5 rounded bg-slate-800 text-slate-300 {role} /span /div div classNamemt-3 space-y-1 text-[11px] font-mono text-slate-300 {durationMs ! undefined p耗时: {durationMs.toFixed(0)}ms/p} {tokensUsed ! undefined pTokens: {tokensUsed.toLocaleString()}/p} {outputPreview ( p classNamemt-2 p-1.5 bg-slate-950/80 rounded border border-slate-800 text-[10px] text-slate-400 truncate 产物: {outputPreview} /p )} /div {/* 底部出站连接桩 (Source Handle) */} Handle typesource position{Position.Bottom} isConnectable{isConnectable} classNamew-3 h-3 bg-emerald-400 border-2 border-slate-950 / /div ); }); AgentTaskNode.displayName AgentTaskNode;核心实现二动态发光粒子连线AnimatedGlowEdge编写自定义 SVG 边组件当数据在节点之间流动时呈现沿路径高速移动的霓虹光效粒子// components/AnimatedGlowEdge.tsx import React from react; import { BaseEdge, EdgeProps, getBezierPath } from reactflow; export const AnimatedGlowEdge: React.FCEdgeProps ({ id, sourceX, sourceY, targetX, targetY, sourcePosition, targetPosition, style {}, markerEnd, data, }) { const [edgePath] getBezierPath({ sourceX, sourceY, sourcePosition, targetPosition, targetX, targetY, }); const isFlowing data?.isFlowing; return ( {/* 基础静态底色连线 */} BaseEdge path{edgePath} id{id} style{{ stroke: isFlowing ? #06B6D4 : #334155, strokeWidth: isFlowing ? 2.5 : 1.5, ...style, }} markerEnd{markerEnd} / {/* 动态发光流光粒子 */} {isFlowing ( circle r4 fill#38BDF8 animateMotion dur1.2s repeatCountindefinite path{edgePath} / /circle )} / ); };前端 React 完整工作流画布组件import React, { useState, useCallback } from react; import ReactFlow, { addEdge, Background, Controls, applyNodeChanges, applyEdgeChanges, Node, Edge, OnNodesChange, OnEdgesChange, OnConnect, } from reactflow; import reactflow/dist/style.css; import { AgentTaskNode } from ./AgentTaskNode; import { AnimatedGlowEdge } from ./AnimatedGlowEdge; const nodeTypes { agentTask: AgentTaskNode }; const edgeTypes { animatedGlow: AnimatedGlowEdge }; const initialNodes: Node[] [ { id: node-1, type: agentTask, position: { x: 250, y: 50 }, data: { title: 需求意图解析, role: Planner, status: SUCCESS, durationMs: 420, tokensUsed: 150 }, }, { id: node-2, type: agentTask, position: { x: 100, y: 220 }, data: { title: 代码生成与重构, role: Coder, status: RUNNING, durationMs: 1200, tokensUsed: 890 }, }, { id: node-3, type: agentTask, position: { x: 400, y: 220 }, data: { title: 环境依赖检查, role: DevOps, status: IDLE }, }, ]; const initialEdges: Edge[] [ { id: e1-2, source: node-1, target: node-2, type: animatedGlow, data: { isFlowing: true } }, { id: e1-3, source: node-1, target: node-3, type: animatedGlow, data: { isFlowing: false } }, ]; export const AgentFlowVisualizer: React.FC () { const [nodes, setNodes] useStateNode[](initialNodes); const [edges, setEdges] useStateEdge[](initialEdges); const onNodesChange: OnNodesChange useCallback( (changes) setNodes((nds) applyNodeChanges(changes, nds)), [] ); const onEdgesChange: OnEdgesChange useCallback( (changes) setEdges((eds) applyEdgeChanges(changes, eds)), [] ); const onConnect: OnConnect useCallback( (params) setEdges((eds) addEdge({ ...params, type: animatedGlow }, eds)), [] ); return ( div classNamew-full h-[650px] bg-slate-950 rounded-3xl border border-slate-800 shadow-2xl overflow-hidden relative ReactFlow nodes{nodes} edges{edges} nodeTypes{nodeTypes} edgeTypes{edgeTypes} onNodesChange{onNodesChange} onEdgesChange{onEdgesChange} onConnect{onConnect} fitView Background color#1E293B gap{20} size{1.5} / Controls classNamebg-slate-900 border border-slate-700 text-white fill-white / /ReactFlow div classNameabsolute top-4 left-6 text-xs font-mono text-cyan-400 pointer-events-none ⚡ REALTIME AGENT GRAPH EXECUTOR | 60 FPS /div /div ); };落地成效黑盒推理 100% 透明化复杂的 Agent 递归思考过程在画布上以动态连线和状态高亮清晰呈现极大增强了终端用户对 AI 系统的信任度。可视化低代码拖拽编排业务人员可以直接在画布上拖拽新增节点、连线调整分支路由无需编写任何底层 YAML 或 Python 脚本。毫秒级实时排障定位发生异常时发生故障的节点瞬间爆红并展示错误堆栈排障时间缩短 90% 以上。
返回列表