|
| 1 | +"""Agent configuration models for the evaluation framework.""" |
| 2 | + |
| 3 | +import os |
| 4 | +from typing import Any, Literal, Optional, Union |
| 5 | + |
| 6 | +from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator |
| 7 | + |
| 8 | +from lightspeed_evaluation.core.constants import ( |
| 9 | + DEFAULT_API_BASE, |
| 10 | + DEFAULT_API_CACHE_DIR, |
| 11 | + DEFAULT_API_NUM_RETRIES, |
| 12 | + DEFAULT_API_TIMEOUT, |
| 13 | + DEFAULT_API_VERSION, |
| 14 | + DEFAULT_ENDPOINT_TYPE, |
| 15 | + SUPPORTED_AGENT_TYPES, |
| 16 | + SUPPORTED_ENDPOINT_TYPES, |
| 17 | +) |
| 18 | +from lightspeed_evaluation.core.system.exceptions import ConfigurationError |
| 19 | + |
| 20 | + |
| 21 | +class MCPServerConfig(BaseModel): |
| 22 | + """Configuration for a single MCP server authentication.""" |
| 23 | + |
| 24 | + model_config = ConfigDict(extra="forbid") |
| 25 | + |
| 26 | + env_var: str = Field( |
| 27 | + ..., |
| 28 | + min_length=1, |
| 29 | + description="Environment variable containing the token/key", |
| 30 | + ) |
| 31 | + header_name: Optional[str] = Field( |
| 32 | + default=None, |
| 33 | + description="Custom header name (optional, defaults to 'Authorization')", |
| 34 | + ) |
| 35 | + |
| 36 | + |
| 37 | +class MCPHeadersConfig(BaseModel): |
| 38 | + """Configuration for MCP headers functionality.""" |
| 39 | + |
| 40 | + model_config = ConfigDict(extra="forbid") |
| 41 | + |
| 42 | + enabled: bool = Field( |
| 43 | + default=True, |
| 44 | + description="Enable MCP headers functionality", |
| 45 | + ) |
| 46 | + servers: dict[str, MCPServerConfig] = Field( |
| 47 | + default_factory=dict, |
| 48 | + description="MCP server configurations", |
| 49 | + ) |
| 50 | + |
| 51 | + @model_validator(mode="after") |
| 52 | + def _validate_env_vars_when_enabled(self) -> "MCPHeadersConfig": |
| 53 | + """Validate that environment variables are set when MCP headers are enabled.""" |
| 54 | + if self.enabled and self.servers: |
| 55 | + missing_vars = [] |
| 56 | + for server_name, server_config in self.servers.items(): |
| 57 | + if not os.getenv(server_config.env_var): |
| 58 | + missing_vars.append(f"{server_name}: {server_config.env_var}") |
| 59 | + |
| 60 | + if missing_vars: |
| 61 | + missing_list = ", ".join(missing_vars) |
| 62 | + msg = ( |
| 63 | + "MCP headers are enabled but required environment " |
| 64 | + "variables are not set: " |
| 65 | + f"{missing_list}" |
| 66 | + ) |
| 67 | + raise ValueError(msg) |
| 68 | + |
| 69 | + return self |
| 70 | + |
| 71 | + |
| 72 | +class HttpApiBaseFields(BaseModel): |
| 73 | + """Shared HTTP API connection fields. |
| 74 | +
|
| 75 | + Base class for both ``HttpApiAgentConfig`` (agents layer) and |
| 76 | + ``APIConfig`` (legacy api: block) to avoid duplicate field definitions. |
| 77 | + """ |
| 78 | + |
| 79 | + model_config = ConfigDict(extra="forbid") |
| 80 | + |
| 81 | + api_base: str = Field( |
| 82 | + default=DEFAULT_API_BASE, |
| 83 | + description="Base URL for API requests (without version)", |
| 84 | + ) |
| 85 | + version: str = Field( |
| 86 | + default=DEFAULT_API_VERSION, description="API version (e.g., v1, v2)" |
| 87 | + ) |
| 88 | + endpoint_type: str = Field( |
| 89 | + default=DEFAULT_ENDPOINT_TYPE, |
| 90 | + description="API endpoint type (streaming / query / infer)", |
| 91 | + ) |
| 92 | + timeout: int = Field( |
| 93 | + default=DEFAULT_API_TIMEOUT, ge=1, description="Request timeout in seconds" |
| 94 | + ) |
| 95 | + provider: Optional[str] = Field(default=None, description="LLM provider for API") |
| 96 | + model: Optional[str] = Field(default=None, description="LLM model for API") |
| 97 | + no_tools: Optional[bool] = Field( |
| 98 | + default=None, description="Disable tool usage in API calls" |
| 99 | + ) |
| 100 | + system_prompt: Optional[str] = Field( |
| 101 | + default=None, description="System prompt for API calls" |
| 102 | + ) |
| 103 | + extra_request_params: Optional[dict[str, Any]] = Field(default=None) |
| 104 | + cache_dir: str = Field( |
| 105 | + default=DEFAULT_API_CACHE_DIR, |
| 106 | + min_length=1, |
| 107 | + description="Location of cached API queries", |
| 108 | + ) |
| 109 | + cache_enabled: bool = Field( |
| 110 | + default=True, description="Is caching of API queries enabled?" |
| 111 | + ) |
| 112 | + num_retries: int = Field( |
| 113 | + default=DEFAULT_API_NUM_RETRIES, |
| 114 | + ge=0, |
| 115 | + description="Maximum number of retry attempts for 429 errors", |
| 116 | + ) |
| 117 | + |
| 118 | + @field_validator("endpoint_type") |
| 119 | + @classmethod |
| 120 | + def validate_endpoint_type(cls, v: str) -> str: |
| 121 | + """Validate endpoint type is supported.""" |
| 122 | + if v not in SUPPORTED_ENDPOINT_TYPES: |
| 123 | + raise ValueError(f"Endpoint type must be one of {SUPPORTED_ENDPOINT_TYPES}") |
| 124 | + return v |
| 125 | + |
| 126 | + |
| 127 | +class HttpApiAgentConfig(HttpApiBaseFields): |
| 128 | + """Configuration for an HTTP API agent.""" |
| 129 | + |
| 130 | + type: Literal["http_api"] = Field( |
| 131 | + default="http_api", description="Agent type identifier" |
| 132 | + ) |
| 133 | + mcp_headers: Optional[MCPHeadersConfig] = Field( |
| 134 | + default=None, |
| 135 | + description="MCP headers configuration for authentication", |
| 136 | + ) |
| 137 | + |
| 138 | + |
| 139 | +# Discriminated union of all agent config types; extend by adding new |
| 140 | +# config classes to support additional agent types. |
| 141 | +AgentDefinition = Union[HttpApiAgentConfig] |
| 142 | + |
| 143 | + |
| 144 | +class AgentDefaultConfig(BaseModel): |
| 145 | + """Default agent selection and shared configuration.""" |
| 146 | + |
| 147 | + model_config = ConfigDict(extra="forbid") |
| 148 | + |
| 149 | + agent: Optional[str] = Field( |
| 150 | + default=None, |
| 151 | + min_length=1, |
| 152 | + pattern=r"\S", |
| 153 | + description="Name of the default agent when eval_data doesn't specify one", |
| 154 | + ) |
| 155 | + agent_config: Optional[dict[str, Any]] = Field( |
| 156 | + default=None, |
| 157 | + description="Shared default agent config overrides applied to all agents", |
| 158 | + ) |
| 159 | + |
| 160 | + |
| 161 | +class AgentsConfig(BaseModel): |
| 162 | + """Top-level agents configuration container. |
| 163 | +
|
| 164 | + Parses a flat YAML namespace where ``default`` is a reserved key holding |
| 165 | + shared config and agent selection, and all other keys with a ``type`` field |
| 166 | + are agent definitions. |
| 167 | + """ |
| 168 | + |
| 169 | + model_config = ConfigDict(extra="forbid") |
| 170 | + |
| 171 | + enabled: bool = Field( |
| 172 | + default=True, |
| 173 | + description="Enable agent-based API calls instead of using pre-filled data", |
| 174 | + ) |
| 175 | + default: AgentDefaultConfig = Field(default_factory=AgentDefaultConfig) |
| 176 | + agents: dict[str, AgentDefinition] = Field(default_factory=dict) |
| 177 | + |
| 178 | + @model_validator(mode="before") |
| 179 | + @classmethod |
| 180 | + def extract_agent_definitions(cls, data: Any) -> Any: |
| 181 | + """Extract named agent definitions from the flat YAML namespace.""" |
| 182 | + if not isinstance(data, dict): |
| 183 | + return data |
| 184 | + |
| 185 | + data = dict(data) |
| 186 | + agents: dict[str, Any] = {} |
| 187 | + default_data = data.pop("default", {}) |
| 188 | + remaining: dict[str, Any] = {} |
| 189 | + |
| 190 | + for key, value in data.items(): |
| 191 | + if key == "agents": |
| 192 | + agents.update(value) |
| 193 | + elif isinstance(value, dict) and "type" in value: |
| 194 | + agents[key] = value |
| 195 | + else: |
| 196 | + remaining[key] = value |
| 197 | + |
| 198 | + result: dict[str, Any] = {"default": default_data, "agents": agents} |
| 199 | + result.update(remaining) |
| 200 | + return result |
| 201 | + |
| 202 | + @model_validator(mode="after") |
| 203 | + def validate_agent_types(self) -> "AgentsConfig": |
| 204 | + """Validate that all agent definitions have supported types.""" |
| 205 | + for name, agent_def in self.agents.items(): |
| 206 | + if agent_def.type not in SUPPORTED_AGENT_TYPES: |
| 207 | + raise ConfigurationError( |
| 208 | + f"Agent '{name}' has unsupported type '{agent_def.type}'. " |
| 209 | + f"Supported types: {SUPPORTED_AGENT_TYPES}" |
| 210 | + ) |
| 211 | + return self |
| 212 | + |
| 213 | + def resolve_agent_config( |
| 214 | + self, |
| 215 | + agent_name: Optional[str] = None, |
| 216 | + agent_config_override: Optional[dict[str, Any]] = None, |
| 217 | + ) -> tuple[str, dict[str, Any]]: |
| 218 | + """Resolve final agent configuration from the 2-level priority chain. |
| 219 | +
|
| 220 | + Per-key merge in ascending priority order — higher levels override |
| 221 | + matching keys while non-overlapping keys from lower levels survive: |
| 222 | +
|
| 223 | + 1. ``default.agent_config`` (lowest) |
| 224 | + 2. ``agent_config_override`` from eval data (highest) |
| 225 | +
|
| 226 | + The agent definition's typed fields form the base; override dicts |
| 227 | + are applied on top. |
| 228 | +
|
| 229 | + Args: |
| 230 | + agent_name: Explicit agent name. Falls back to default.agent. |
| 231 | + agent_config_override: Per-evaluation config overrides (highest priority). |
| 232 | +
|
| 233 | + Returns: |
| 234 | + Tuple of (agent_name, merged_config_dict). |
| 235 | +
|
| 236 | + Raises: |
| 237 | + ConfigurationError: If no agent can be resolved or agent not found. |
| 238 | + """ |
| 239 | + name = agent_name or self.default.agent |
| 240 | + if name is None: |
| 241 | + raise ConfigurationError( |
| 242 | + "No agent specified and no default agent configured" |
| 243 | + ) |
| 244 | + |
| 245 | + if name not in self.agents: |
| 246 | + raise ConfigurationError( |
| 247 | + f"Agent '{name}' not found in agents configuration. " |
| 248 | + f"Available agents: {list(self.agents.keys())}" |
| 249 | + ) |
| 250 | + |
| 251 | + agent_def = self.agents[name] |
| 252 | + |
| 253 | + effective: dict[str, Any] = {} |
| 254 | + if self.default.agent_config: |
| 255 | + effective.update(self.default.agent_config) |
| 256 | + if agent_config_override: |
| 257 | + effective.update(agent_config_override) |
| 258 | + |
| 259 | + base_config = agent_def.model_dump() |
| 260 | + base_config.update(effective) |
| 261 | + |
| 262 | + return name, base_config |
0 commit comments