refactor: remove legacy router support and update configuration handling
This commit is contained in:
@@ -24,6 +24,7 @@ from cleveragents.agents.tool import ToolAgent
|
||||
from cleveragents.core.config import ConfigurationManager
|
||||
from cleveragents.core.exceptions import AgentCreationError
|
||||
from cleveragents.core.exceptions import CleverAgentsException
|
||||
from cleveragents.core.exceptions import ConfigurationError
|
||||
from cleveragents.interactive.session import InteractiveSession
|
||||
from cleveragents.routing.graph_builder import GraphBuilder
|
||||
from cleveragents.routing.router import Router
|
||||
@@ -103,39 +104,22 @@ class CleverAgentsApp:
|
||||
self.logger.info("Validating configuration")
|
||||
self.config_manager.validate()
|
||||
|
||||
# Initialize template renderer
|
||||
# Prefer the nested cleveragents.template_engine path, but
|
||||
# gracefully fall back to the legacy top-level key if needed.
|
||||
template_engine_name = self.config_manager.get(
|
||||
"cleveragents.template_engine",
|
||||
self.config_manager.get("template_engine", "simple"),
|
||||
"JINJA2",
|
||||
)
|
||||
try:
|
||||
template_engine = TemplateEngine[template_engine_name.upper()]
|
||||
except KeyError:
|
||||
template_engine = TemplateEngine.SIMPLE
|
||||
self.logger.warning(
|
||||
f"Unknown template engine '{template_engine_name}', using SIMPLE"
|
||||
)
|
||||
template_engine = TemplateEngine[template_engine_name.upper()]
|
||||
|
||||
template_renderer = TemplateRenderer(template_engine)
|
||||
|
||||
# Detect silent fallback to SIMPLE engine (e.g., when `jinja2`
|
||||
# is requested but not installed). Abort early with a clear
|
||||
# message so users can fix their environment instead of getting
|
||||
# hard-to-debug template errors later in execution.
|
||||
# Double check the expected engine was loaded, if not throw an error.
|
||||
requested_engine = template_engine
|
||||
loaded_engine = getattr(
|
||||
template_renderer,
|
||||
"engine_type", # Preferred attribute
|
||||
getattr( # Fallback to legacy/internal attr
|
||||
template_renderer, "engine", TemplateEngine.SIMPLE
|
||||
),
|
||||
None,
|
||||
)
|
||||
if (
|
||||
requested_engine is not TemplateEngine.SIMPLE
|
||||
and loaded_engine is TemplateEngine.SIMPLE
|
||||
):
|
||||
if requested_engine is not loaded_engine or loaded_engine is None:
|
||||
raise CleverAgentsException(
|
||||
f"Requested template engine '{requested_engine.name}' but it "
|
||||
"failed to initialise, so the system fell back to SIMPLE. "
|
||||
@@ -189,7 +173,6 @@ class CleverAgentsApp:
|
||||
Raises:
|
||||
CleverAgentsException: If the agent network is not configured or execution fails.
|
||||
"""
|
||||
# Build everything lazily so we stay compatible with legacy single-router configs
|
||||
self._initialize_agents_and_routers()
|
||||
|
||||
try:
|
||||
@@ -274,8 +257,7 @@ class CleverAgentsApp:
|
||||
|
||||
def _initialize_agents_and_routers(self) -> None:
|
||||
"""
|
||||
Lazily create agents and routers so that we support both the legacy
|
||||
single-router configuration and the new named-routes layout.
|
||||
Lazily create agents and routers
|
||||
"""
|
||||
if self.routers:
|
||||
return # Already initialised
|
||||
@@ -314,15 +296,9 @@ class CleverAgentsApp:
|
||||
router.add_routes(rules)
|
||||
self.routers[route_name] = router
|
||||
else:
|
||||
# Legacy single router section
|
||||
router_name = self.config_manager.get("router.name", "main_router")
|
||||
rules = self.config_manager.get("router.routes", [])
|
||||
router = Router(
|
||||
router_name, self.agents, self.agent_factory.template_renderer
|
||||
raise ConfigurationError(
|
||||
"No routes section defined in configuration, aborting"
|
||||
)
|
||||
router.add_routes(rules)
|
||||
self.routers[router_name] = router
|
||||
self.default_router_name = router_name
|
||||
|
||||
# --------------------------------------------------------------------- #
|
||||
def visualize_network(self, output_file: Path, format: str = "svg") -> None:
|
||||
|
||||
@@ -305,23 +305,10 @@ class SchemaValidator:
|
||||
f"Agent '{agent_name}' 'config' must be a dictionary"
|
||||
)
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# Router / Routes validation – support both legacy & new structures #
|
||||
# ------------------------------------------------------------------ #
|
||||
has_routes = "routes" in config and isinstance(config["routes"], dict)
|
||||
has_router = "router" in config and isinstance(config["router"], dict)
|
||||
|
||||
if not has_routes and not has_router:
|
||||
raise ConfigurationError(
|
||||
"Configuration must include either 'routes' (new style) or "
|
||||
"'router' (legacy style) section"
|
||||
)
|
||||
|
||||
if has_routes:
|
||||
if "routes" in config and isinstance(config["routes"], dict):
|
||||
if not config["routes"]:
|
||||
raise ConfigurationError("'routes' section cannot be empty")
|
||||
|
||||
# If using the new style, 'cleveragents.default_router' is required.
|
||||
clever_opts = config.get("cleveragents", {})
|
||||
if "default_router" not in clever_opts:
|
||||
raise ConfigurationError(
|
||||
@@ -333,10 +320,5 @@ class SchemaValidator:
|
||||
raise ConfigurationError(
|
||||
f"Default router '{default_router}' not found within 'routes'"
|
||||
)
|
||||
|
||||
if has_router:
|
||||
# Basic structural validation for legacy configs
|
||||
if "routes" not in config["router"]:
|
||||
raise ConfigurationError("'router' section must contain 'routes' key")
|
||||
if not isinstance(config["router"]["routes"], list):
|
||||
raise ConfigurationError("'router.routes' must be a list")
|
||||
else:
|
||||
raise ConfigurationError("Configuration must have 'routes' section")
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
agents:
|
||||
dummy:
|
||||
config:
|
||||
api_key: mock-api-key
|
||||
model: test-model
|
||||
provider: openai
|
||||
type: llm
|
||||
router:
|
||||
name: default_router
|
||||
routes:
|
||||
- destination: dummy
|
||||
source: input
|
||||
- destination: output
|
||||
source: dummy
|
||||
version: '1.0'
|
||||
@@ -1,6 +1,6 @@
|
||||
Feature: Agent Network Processing
|
||||
|
||||
Scenario: Process a message through the agent network
|
||||
Given a configuration file "tests/config/network_config.yaml"
|
||||
Given a configuration file "build/tests/config/network_config.yaml"
|
||||
When I process the message "Hello, AI!"
|
||||
Then I should receive a non-empty response
|
||||
Then I should receive a non-empty network response
|
||||
|
||||
@@ -30,6 +30,7 @@ def step_impl(context):
|
||||
config_data = {
|
||||
"version": "1.0",
|
||||
"logging": {"level": "INFO"},
|
||||
"cleveragents": {"default_router": "main_router"},
|
||||
"agents": {
|
||||
"classifier": {
|
||||
"type": "llm",
|
||||
@@ -42,10 +43,7 @@ def step_impl(context):
|
||||
},
|
||||
}
|
||||
},
|
||||
"router": {
|
||||
"name": "main_router",
|
||||
"routes": [{"source": "input", "destination": "classifier"}],
|
||||
},
|
||||
"routes": {"main_router": [{"source": "input", "destination": "classifier"}]},
|
||||
}
|
||||
|
||||
# Create a temporary file for the configuration
|
||||
|
||||
@@ -17,6 +17,7 @@ def step_impl_config_file(context, config_file):
|
||||
# Create a minimal but valid CleverAgents configuration file
|
||||
config_data = {
|
||||
"version": "1.0",
|
||||
"cleveragents": {"default_router": "default_router"},
|
||||
"agents": {
|
||||
"dummy": {
|
||||
"type": "llm",
|
||||
@@ -27,12 +28,11 @@ def step_impl_config_file(context, config_file):
|
||||
},
|
||||
}
|
||||
},
|
||||
"router": {
|
||||
"name": "default_router",
|
||||
"routes": [
|
||||
"routes": {
|
||||
"default_router": [
|
||||
{"source": "input", "destination": "dummy"},
|
||||
{"source": "dummy", "destination": "output"},
|
||||
],
|
||||
]
|
||||
},
|
||||
}
|
||||
config_path = Path(config_file)
|
||||
|
||||
Reference in New Issue
Block a user