"""Config loading: one YAML file, `roles.yaml`, validated into a model. A typo in a key is an error naming the key, because `Section` forbids extras. `sources` stays a dict: one block per plugin, whose keys only the plugin knows. """ from __future__ import annotations import os from pathlib import Path from typing import Any import yaml from pydantic import Field, ValidationError from desk.config import AgentConfig, ModelTaskConfig, Section, WorkersConfig DEFAULT_PATH = Path(os.environ.get("ROLEDESK_CONFIG", "roles.yaml")) class ExtractConfig(ModelTaskConfig): """Turning a posting's title and text into the gate columns. Off until the `extract` task exists; the section is here so the file's shape is settled.""" enabled: bool = False model: str = "claude-haiku-4-5" class Config(Section): """The whole file.""" database: str = "roledesk.db" plugin_paths: list[str] = Field(default_factory=lambda: ["plugins"]) workers: WorkersConfig = Field(default_factory=WorkersConfig) agent: AgentConfig = Field(default_factory=AgentConfig) extract: ExtractConfig = Field(default_factory=ExtractConfig) #: One block per plugin: `enabled`, and whatever that plugin reads. sources: dict[str, Any] = Field(default_factory=dict) #: Where this was loaded from. Never written back into the file. path: str = Field("", exclude=True) class ConfigError(FileNotFoundError): """A config that exists but does not validate. A FileNotFoundError so the CLI reports both kinds of unusable config the same way: as a message.""" def load(path: str | Path | None = None) -> Config: p = Path(path or DEFAULT_PATH).expanduser() if not p.exists(): raise FileNotFoundError( f"No config at {p}. Copy config.example.yaml to {p} and edit it.") with p.open(encoding="utf-8") as fh: raw = yaml.safe_load(fh) or {} try: config = Config(**raw, path=str(p)) except ValidationError as exc: raise ConfigError(f"{p}:\n{_explain(exc)}") from None config.agent.credentials_database = config.database return config def _explain(exc: ValidationError) -> str: lines = [] for err in exc.errors(): where = ".".join(str(p) for p in err["loc"]) or "(top level)" detail = err["msg"] if err["type"] == "extra_forbidden": detail = "unknown setting, check the spelling against config.example.yaml" lines.append(f" {where}: {detail}") return "\n".join(lines)