"""YAML configuration loader and validator.""" import yaml from pathlib import Path from .models import ChannelConfig, CollectionConfig def load_config(config_path: str) -> dict: """Load and validate configuration from YAML file.""" path = Path(config_path) if not path.exists(): raise FileNotFoundError(f"Config file not found: {config_path}") with open(path, 'r', encoding='utf-8') as f: raw = yaml.safe_load(f) # Parse channel configs genuine = _parse_channel(raw.get('genuine', {}), 'genuine') suspect = _parse_channel(raw.get('suspect', {}), 'suspect') # Parse collection config coll = raw.get('collection', {}) collection = CollectionConfig( repeat_count=coll.get('repeat_count', 3), timeout=coll.get('timeout', 60), max_tokens=coll.get('max_tokens', 1024), anthropic_version=coll.get('anthropic_version', '2023-06-01'), ) # Parse output config output = raw.get('output', {}) return { 'genuine': genuine, 'suspect': suspect, 'collection': collection, 'output': output, } def _parse_channel(data: dict, name: str) -> ChannelConfig: """Parse and validate a channel configuration.""" required = ['base_url', 'api_key', 'model'] for key in required: if key not in data or not data[key]: raise ValueError(f"Channel '{name}' missing required field: {key}") return ChannelConfig( base_url=data['base_url'].rstrip('/'), api_key=data['api_key'], model=data['model'], )