11# WorkFlow.py
2+
23import os
34import re
45import json
5- from typing import Dict , List , TypedDict , Any , Annotated , Callable , Literal
6+ from typing import Dict , List , TypedDict , Any , Annotated , Callable , Literal , Optional
67import operator
78import inspect
89
@@ -25,14 +26,20 @@ def tool(func: Callable) -> Callable:
2526 tool_info_registry [func .__name__ ] = tool_info
2627 return func
2728
28- def load_nodes_from_json (filename : str ) -> Dict [str , NodeData ]:
29- with open (filename , 'r' ) as file :
30- data = json .load (file )
31- node_map = {}
32- for node_data in data ["nodes" ]:
33- node = NodeData .from_dict (node_data )
34- node_map [node .uniq_id ] = node
35- return node_map
29+ def parse_nodes_from_json (graph_data : Dict [str , Any ]) -> Dict [str , NodeData ]:
30+ """
31+ Parses node data from a subgraph's JSON structure.
32+
33+ Args:
34+ graph_data: A dictionary representing a subgraph.
35+ Returns:
36+ A dictionary of NodeData objects keyed by their unique IDs.
37+ """
38+ node_map = {}
39+ for node_data in graph_data .get ("nodes" , []):
40+ node = NodeData .from_dict (node_data )
41+ node_map [node .uniq_id ] = node
42+ return node_map
3643
3744def find_nodes_by_type (node_map : Dict [str , NodeData ], node_type : str ) -> List [NodeData ]:
3845 return [node for node in node_map .values () if node .type == node_type ]
@@ -220,11 +227,22 @@ def RunWorkFlow(node_map: Dict[str, NodeData], llm):
220227 flush_print (state )
221228
222229def run_workflow_as_server (llm ):
223- node_map = load_nodes_from_json ("graph.json" )
230+ # Load subgraph data
231+ with open ("graph.json" , 'r' ) as file :
232+ graphs = json .load (file )
233+
234+ # Process each subgraph
235+ for graph in graphs :
236+ subgraph_name = graph .get ("name" )
224237
225- # Register the tool functions dynamically
226- for tool in find_nodes_by_type (node_map , "TOOL" ):
227- tool_code = f"{ tool .description } "
228- exec (tool_code , globals ())
238+ if subgraph_name == "root" :
239+ node_map = parse_nodes_from_json (graph )
229240
230- RunWorkFlow (node_map , llm )
241+ # Register the tool functions dynamically
242+ for tool in find_nodes_by_type (node_map , "TOOL" ):
243+ tool_code = f"{ tool .description } "
244+ exec (tool_code , globals ())
245+
246+ RunWorkFlow (node_map , llm )
247+ else :
248+ print (f"Skipping subgraph: { subgraph_name } " ) # Optional logging
0 commit comments