Files
DocsGPT/docsgpt/core/settings/__init__.py
T
arc53-machine c17b23378e refactor(settings): split Settings into per-domain modules
docsgpt/core/settings.py had grown to 258 fields in one 600-line class,
touched by about two commits a week, with related settings scattered
(GitHub ingest caps inside the embeddings block, API keys in four places,
the OpenAI Responses knobs 100 lines from the other OpenAI fields).

It is now a package: one module per domain (auth, llm, embeddings,
retrieval, vectorstores, database, workers, ingestion, ocr, storage,
connectors, server, events, agents, guardrails, scheduler, sandbox,
speech), each a SettingsGroup owning its fields and validators, composed
by multiple inheritance into the same flat Settings class. Every
attribute name, type, default, alias and constraint is unchanged, so
settings.NAME reads, .env files and test monkeypatches all keep working;
the import path docsgpt.core.settings is the package. Settings.normalize_api_key
is kept as a classmethod for callers that reuse it.

The comment above or beside each field became its Field(description=...),
so the definitions are visible to tooling; the next commit generates the
docs reference from them.

Pitfall recorded for future groups: pydantic collects validators by
method name across the MRO, so two groups naming a validator the same
would silently keep only one. Each group's validator has a unique name.
2026-09-17 11:04:01 +01:00

78 lines
3.2 KiB
Python

"""Application settings.
``settings`` is the process-wide instance, loaded from the environment and the
``.env`` file in the data home (see ``docsgpt.core.paths``). Every setting is a
flat attribute, ``settings.NAME``, matching the environment variable of the
same name.
The definitions are split by domain into the modules of this package; each
module owns one ``SettingsGroup`` and ``Settings`` composes them all. Add a new
setting to the group it belongs to (or add a group and list it in
``SETTINGS_GROUPS``), with a ``description`` -- the settings reference in the
docs is generated from these definitions.
"""
from __future__ import annotations
from typing import Optional
from docsgpt.core.paths import env_file, home_dir
from docsgpt.core.settings._shared import SettingsGroup, normalize_secret
from docsgpt.core.settings.agents import AgentSettings
from docsgpt.core.settings.auth import AuthSettings
from docsgpt.core.settings.connectors import ConnectorSettings
from docsgpt.core.settings.database import DatabaseSettings
from docsgpt.core.settings.embeddings import EmbeddingsSettings
from docsgpt.core.settings.events import EventsSettings
from docsgpt.core.settings.guardrails import GuardrailSettings
from docsgpt.core.settings.ingestion import IngestionSettings
from docsgpt.core.settings.llm import LLMSettings
from docsgpt.core.settings.ocr import OCRSettings
from docsgpt.core.settings.retrieval import RetrievalSettings
from docsgpt.core.settings.sandbox import SandboxSettings
from docsgpt.core.settings.scheduler import SchedulerSettings
from docsgpt.core.settings.server import ServerSettings
from docsgpt.core.settings.speech import SpeechSettings
from docsgpt.core.settings.storage import StorageSettings
from docsgpt.core.settings.vectorstores import VectorStoreSettings
from docsgpt.core.settings.workers import WorkerSettings
#: Every settings group, in the order the generated reference lists them.
SETTINGS_GROUPS: tuple[tuple[str, type[SettingsGroup]], ...] = (
("Authentication", AuthSettings),
("LLM providers", LLMSettings),
("Embeddings", EmbeddingsSettings),
("Retrieval", RetrievalSettings),
("Vector stores", VectorStoreSettings),
("User-data database", DatabaseSettings),
("Workers", WorkerSettings),
("Ingestion and parsing", IngestionSettings),
("OCR", OCRSettings),
("File storage", StorageSettings),
("Connectors", ConnectorSettings),
("Server", ServerSettings),
("Events and devices", EventsSettings),
("Agents", AgentSettings),
("Guardrails", GuardrailSettings),
("Scheduler", SchedulerSettings),
("Sandbox", SandboxSettings),
("Speech", SpeechSettings),
)
# Runtime data home (DOCSGPT_HOME, the checkout, or cwd); see docsgpt.core.paths.
current_dir = str(home_dir())
class Settings(*(group for _, group in SETTINGS_GROUPS)):
"""All settings, composed from the per-domain groups in this package."""
@classmethod
def normalize_api_key(cls, v: Optional[str]) -> Optional[str]:
"""Normalize a secret the way the per-field validators do; kept for callers that reuse it."""
return normalize_secret(v)
settings = Settings(_env_file=env_file(), _env_file_encoding="utf-8")
__all__ = ["SETTINGS_GROUPS", "Settings", "SettingsGroup", "current_dir", "settings"]