Skip to content

Commit 5400f64

Browse files
committed
feat: enhance configuration system with version validation and CLI overrides
- Add version validation to prevent config file mismatches - Allow CLI arguments to override base_url and api_key from config files - Import project version for validation in config module - Improve config loading with better parameter handling and documentation - Maintain backward compatibility while adding new validation features
1 parent 24df456 commit 5400f64

2 files changed

Lines changed: 38 additions & 7 deletions

File tree

src/balatrollm/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,16 @@ def _validate_config_file(config_path: str) -> None:
145145
async def run_bot(args) -> None:
146146
"""Run the Balatro bot with the given arguments."""
147147
if args.from_config:
148-
config = Config.from_config_file(args.from_config)
148+
# Load config from file, but allow CLI arguments to override base_url and api_key
149+
config = Config.from_config_file(
150+
args.from_config,
151+
base_url=args.base_url
152+
if args.base_url != os.getenv("LITELLM_BASE_URL", "http://localhost:4000")
153+
else None,
154+
api_key=args.api_key
155+
if args.api_key != os.getenv("LITELLM_API_KEY", "sk-balatrollm-proxy-key")
156+
else None,
157+
)
149158
else:
150159
config = Config(
151160
model=args.model,

src/balatrollm/config.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from dataclasses import dataclass
66
from pathlib import Path
77

8+
from . import __version__
9+
810

911
@dataclass
1012
class Config:
@@ -26,20 +28,40 @@ def from_environment(cls) -> "Config":
2628
)
2729

2830
@classmethod
29-
def from_config_file(cls, config_path: str) -> "Config":
30-
"""Create configuration from a previous run's config.json file."""
31+
def from_config_file(
32+
cls, config_path: str, base_url: str | None = None, api_key: str | None = None
33+
) -> "Config":
34+
"""Create configuration from a previous run's config.json file.
35+
36+
Loads model and strategy from config.json, uses CLI overrides if provided,
37+
otherwise falls back to environment defaults for base_url and api_key.
38+
39+
Args:
40+
config_path: Path to the config.json file
41+
base_url: Optional CLI override for base_url
42+
api_key: Optional CLI override for api_key
43+
"""
3144
config_file = Path(config_path)
3245
if not config_file.exists():
3346
raise FileNotFoundError(f"Config file not found: {config_file}")
3447

3548
with config_file.open() as f:
3649
config_data = json.load(f)
3750

38-
# Map the config.json fields to Config fields
39-
# config.json uses 'base_url' and 'strategy' (updated field names)
51+
# Version validation - check if config version matches current repo version
52+
if "version" in config_data:
53+
config_version = config_data["version"]
54+
if config_version != __version__:
55+
raise ValueError(
56+
f"Version mismatch: Config file version '{config_version}' "
57+
f"does not match current repository version '{__version__}'. "
58+
f"Please use a config file from the same version or update your repository."
59+
)
60+
61+
# Create config with values from file, CLI overrides, or environment defaults
4062
return cls(
4163
model=config_data["model"],
42-
base_url=config_data["base_url"],
43-
api_key=config_data["api_key"],
64+
base_url=base_url or os.getenv("LITELLM_BASE_URL", "http://localhost:4000"),
65+
api_key=api_key or os.getenv("LITELLM_API_KEY", "sk-balatrollm-proxy-key"),
4466
strategy=config_data["strategy"],
4567
)

0 commit comments

Comments
 (0)