()
| 36 | * ``` |
| 37 | */ |
| 38 | export function useAgentflow(): AgentFlowInstance { |
| 39 | const { state, setNodes, setEdges, setDirty, getFlowData, setNodeExecutionStatus, clearExecutionState } = useAgentflowContext() |
| 40 | |
| 41 | const { nodes, edges, reactFlowInstance } = state |
| 42 | |
| 43 | /** |
| 44 | * Get the current flow data as a serializable object |
| 45 | */ |
| 46 | const getFlow = useCallback((): FlowData => { |
| 47 | return getFlowData() |
| 48 | }, [getFlowData]) |
| 49 | |
| 50 | /** |
| 51 | * Convert the current flow to a JSON string |
| 52 | */ |
| 53 | const toJSON = useCallback((): string => { |
| 54 | return JSON.stringify(getFlowData(), null, 2) |
| 55 | }, [getFlowData]) |
| 56 | |
| 57 | /** |
| 58 | * Validate the current flow structure |
| 59 | */ |
| 60 | const validate = useCallback((): ValidationResult => { |
| 61 | return validateFlow(nodes, edges) |
| 62 | }, [nodes, edges]) |
| 63 | |
| 64 | /** |
| 65 | * Fit the canvas view to show all nodes |
| 66 | */ |
| 67 | const fitView = useCallback((): void => { |
| 68 | reactFlowInstance?.fitView({ padding: 0.2, duration: 200 }) |
| 69 | }, [reactFlowInstance]) |
| 70 | |
| 71 | /** |
| 72 | * Get the underlying ReactFlow instance |
| 73 | */ |
| 74 | const getReactFlowInstance = useCallback(() => { |
| 75 | return reactFlowInstance |
| 76 | }, [reactFlowInstance]) |
| 77 | |
| 78 | /** |
| 79 | * Programmatically add a new node to the canvas |
| 80 | */ |
| 81 | const addNode = useCallback( |
| 82 | (nodeData: Partial<FlowNode>): void => { |
| 83 | const newNode: FlowNode = { |
| 84 | id: nodeData.id || `node_${Date.now()}`, |
| 85 | type: nodeData.type || 'agentFlow', |
| 86 | position: nodeData.position || { x: 100, y: 100 }, |
| 87 | data: nodeData.data || { name: '', label: '' }, |
| 88 | ...nodeData |
| 89 | } as FlowNode |
| 90 | |
| 91 | setNodes([...nodes, newNode]) |
| 92 | setDirty(true) |
| 93 | }, |
| 94 | [nodes, setNodes, setDirty] |
| 95 | ) |
no test coverage detected