-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject_config.py
More file actions
81 lines (53 loc) · 2.32 KB
/
project_config.py
File metadata and controls
81 lines (53 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""Load project configuration from ``config.json``."""
from __future__ import annotations
import json
from functools import lru_cache
from pathlib import Path
from typing import Literal
from pydantic import BaseModel, ConfigDict, Field
class StartupConfig(BaseModel):
"""Startup command planning and approval configuration."""
model_config = ConfigDict(extra="forbid")
approved_command: str | None = None
approval_required: bool = False
auto_start: bool = True
readiness_path: str = "/openapi.json"
readiness_timeout_seconds: float = 20.0
class ServiceConfig(BaseModel):
"""Configuration for the scanned target service and harness."""
model_config = ConfigDict(extra="forbid")
repo_path: str = "."
service_url: str | None = None
auth_token: str | None = None
startup: StartupConfig = Field(default_factory=StartupConfig)
class LLMConfig(BaseModel):
"""Configuration for the external LLM provider."""
model_config = ConfigDict(extra="forbid")
provider: str | None = None
base_url: str | None = None
api_key: str | None = None
model: str | None = None
mode: Literal["auto", "responses", "chat-completions"] = "auto"
class ProjectConfig(BaseModel):
"""Top-level project configuration loaded from JSON."""
model_config = ConfigDict(extra="forbid")
service: ServiceConfig = Field(default_factory=ServiceConfig)
llm: LLMConfig = Field(default_factory=LLMConfig)
def default_config_path() -> Path:
"""Return the default config file location."""
return Path(__file__).resolve().parent / "config.json"
def load_config(path: str | Path | None = None) -> ProjectConfig:
"""Load and validate project configuration from disk."""
config_path = Path(path) if path is not None else default_config_path()
return _load_config_cached(str(config_path.resolve()))
@lru_cache(maxsize=8)
def _load_config_cached(path: str) -> ProjectConfig:
config_path = Path(path)
if not config_path.exists():
raise FileNotFoundError(
f"Config file not found: {config_path}. Create it from config.example.json."
)
payload = json.loads(config_path.read_text(encoding="utf-8"))
if not isinstance(payload, dict):
raise TypeError("config.json must contain a JSON object.")
return ProjectConfig.model_validate(payload)