|
| 1 | +import logging |
1 | 2 | from pathlib import Path |
2 | 3 | from typing import ClassVar, Self |
3 | 4 |
|
|
6 | 7 |
|
7 | 8 | from sgr_deep_research.core.agent_definition import AgentConfig, Definitions |
8 | 9 |
|
| 10 | +logger = logging.getLogger(__name__) |
| 11 | + |
9 | 12 |
|
10 | 13 | class GlobalConfig(BaseSettings, AgentConfig, Definitions): |
11 | 14 | _instance: ClassVar[Self | None] = None |
@@ -44,8 +47,39 @@ def from_yaml(cls, yaml_path: str) -> Self: |
44 | 47 |
|
45 | 48 | @classmethod |
46 | 49 | def definitions_from_yaml(cls, agents_yaml_path: str) -> Self: |
| 50 | + """Load agent definitions from YAML file and merge with existing |
| 51 | + agents. |
| 52 | +
|
| 53 | + Args: |
| 54 | + agents_yaml_path: Path to YAML file with agent definitions |
| 55 | +
|
| 56 | + Returns: |
| 57 | + GlobalConfig instance with merged agents |
| 58 | +
|
| 59 | + Raises: |
| 60 | + FileNotFoundError: If YAML file not found |
| 61 | + ValueError: If YAML file doesn't contain 'agents' key |
| 62 | + """ |
47 | 63 | agents_yaml_path = Path(agents_yaml_path) |
48 | 64 | if not agents_yaml_path.exists(): |
49 | 65 | raise FileNotFoundError(f"Agents definitions file not found: {agents_yaml_path}") |
50 | | - cls._instance.agents = Definitions(**yaml.safe_load(agents_yaml_path.read_text(encoding="utf-8"))).agents |
| 66 | + |
| 67 | + yaml_data = yaml.safe_load(agents_yaml_path.read_text(encoding="utf-8")) |
| 68 | + if not yaml_data.get("agents"): |
| 69 | + raise ValueError(f"Agents definitions file must contain 'agents' key: {agents_yaml_path}") |
| 70 | + |
| 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) |
51 | 85 | return cls._instance |
0 commit comments