@@ -38,11 +38,31 @@ def from_yaml(cls, yaml_path: str) -> Self:
3838 if not yaml_path .exists ():
3939 raise FileNotFoundError (f"Configuration file not found: { yaml_path } " )
4040 config_data = yaml .safe_load (yaml_path .read_text (encoding = "utf-8" ))
41+ main_config_agents = config_data .pop ("agents" , {})
4142 if cls ._instance is None :
42- cls ._instance = cls (** config_data )
43+ cls ._instance = cls (
44+ ** config_data ,
45+ )
4346 else :
4447 cls ._initialized = False
4548 cls ._instance = cls (** config_data , agents = cls ._instance .agents )
49+ # agents should be initialized last to allow merging
50+ cls ._definitions_from_dict ({"agents" : main_config_agents })
51+ return cls ._instance
52+
53+ @classmethod
54+ def _definitions_from_dict (cls , agents_data : dict ) -> Self :
55+ for agent_name , agent_config in agents_data .get ("agents" ).items ():
56+ agent_config ["name" ] = agent_name
57+
58+ custom_agents = Definitions (** agents_data ).agents
59+
60+ # Check for agents that will be overridden
61+ overridden = set (cls ._instance .agents .keys ()) & set (custom_agents .keys ())
62+ if overridden :
63+ logger .warning (f"Loaded agents will override existing agents: " f"{ ', ' .join (sorted (overridden ))} " )
64+
65+ cls ._instance .agents .update (custom_agents )
4666 return cls ._instance
4767
4868 @classmethod
@@ -68,18 +88,4 @@ def definitions_from_yaml(cls, agents_yaml_path: str) -> Self:
6888 if not yaml_data .get ("agents" ):
6989 raise ValueError (f"Agents definitions file must contain 'agents' key: { agents_yaml_path } " )
7090
71- for agent_name , agent_config in yaml_data .get ("agents" ).items ():
72- agent_config ["name" ] = agent_name
73-
74- custom_agents = Definitions (** yaml_data ).agents
75-
76- # Check for agents that will be overridden
77- overridden = set (cls ._instance .agents .keys ()) & set (custom_agents .keys ())
78- if overridden :
79- logger .warning (
80- f"Custom agents from { agents_yaml_path .name } will override existing agents: "
81- f"{ ', ' .join (sorted (overridden ))} "
82- )
83-
84- cls ._instance .agents .update (custom_agents )
85- return cls ._instance
91+ return cls ._definitions_from_dict (yaml_data )
0 commit comments