"""Builds the LLM port from configuration.""" from collections.abc import Mapping from pathlib import Path from chemenu.adapters.llm.anthropic_api import AnthropicProvider from chemenu.adapters.llm.config import AnthropicConfig, LlmConfig from chemenu.adapters.llm.provider import Provider from chemenu.adapters.llm.recorded import RecordedProvider from chemenu.adapters.llm.router import ProfileRouter from chemenu.ports.llm import LlmPort def build_llm(config: LlmConfig, env: Mapping[str, str]) -> LlmPort: """Build the router with one provider per configured entry. Never reads `os.environ`; the caller passes `env`. A missing API key does not fail here, only the call (`MissingApiKeyError`). Recording directories are relative to the working directory. """ providers: dict[str, Provider] = {} for name, cfg in config.providers.items(): if isinstance(cfg, AnthropicConfig): providers[name] = AnthropicProvider(env.get(cfg.api_key_env)) else: providers[name] = RecordedProvider(Path(cfg.directory)) return ProfileRouter(config, providers)