"""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 from app.models import Gate, Weights DEFAULT_PATH = Path(os.environ.get("ROLEDESK_CONFIG", "roles.yaml")) class ExtractConfig(ModelTaskConfig): """Turning a posting's title and text into the gate columns. See app/jobs/extract.py. Off by default, so nobody is billed by surprise: on the API backend every batch is a paid call.""" enabled: bool = False model: str = "claude-haiku-4-5-20251001" batch_size: int = Field(20, ge=1) class EmbedConfig(Section): """Turning a role into a vector for `fit`. Needs sentence-transformers; without it `embed` says so and does nothing, and `score` leaves fit out. See app/jobs/embed.py.""" #: Recorded on every vector. Changing it drops the old vectors, which are #: a different geometry and never comparable. model: str = "sentence-transformers/all-MiniLM-L6-v2" #: Roles embedded per run at most, newest first. per_run: int = Field(50_000, ge=1) class AgentSeed(Section): """A search agent as `roles.yaml` lists it. Imported into an empty `agents` table once; after that the table is the answer.""" name: str intent: str = "" gates: list[Gate] = Field(default_factory=list) weights: Weights = Field(default_factory=Weights) enabled: bool = True 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) embed: EmbedConfig = Field(default_factory=EmbedConfig) #: Starter agents, imported once into an empty table. agents: list[AgentSeed] = Field(default_factory=list) #: 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)