1818from typing import Any
1919from typing import Optional
2020
21- from ...agents .base_agent import BaseAgent
2221from ...agents .llm_agent import LlmAgent
22+ from ...workflow import BaseNode
2323
2424
25- def _create_empty_state (agent : BaseAgent , all_state : dict [str , Any ]):
26- for sub_agent in agent .sub_agents :
27- _create_empty_state (sub_agent , all_state )
25+ def _create_empty_state (
26+ agent : BaseNode , all_state : dict [str , Any ], visited : set [int ]
27+ ):
28+ agent_id = id (agent )
29+ if agent_id in visited :
30+ return
31+ visited .add (agent_id )
32+
33+ for sub_agent in getattr (agent , 'sub_agents' , []) or []:
34+ _create_empty_state (sub_agent , all_state , visited )
35+
36+ graph = getattr (agent , 'graph' , None )
37+ if graph is not None :
38+ for graph_node in getattr (graph , 'nodes' , []) or []:
39+ _create_empty_state (graph_node , all_state , visited )
2840
2941 if (
3042 isinstance (agent , LlmAgent )
@@ -36,11 +48,11 @@ def _create_empty_state(agent: BaseAgent, all_state: dict[str, Any]):
3648
3749
3850def create_empty_state (
39- agent : BaseAgent , initialized_states : Optional [dict [str , Any ]] = None
51+ agent : BaseNode , initialized_states : Optional [dict [str , Any ]] = None
4052) -> dict [str , Any ]:
4153 """Creates empty str for non-initialized states."""
4254 non_initialized_states = {}
43- _create_empty_state (agent , non_initialized_states )
55+ _create_empty_state (agent , non_initialized_states , set () )
4456 for key in initialized_states or {}:
4557 if key in non_initialized_states :
4658 del non_initialized_states [key ]
0 commit comments