| 1 | """Configuration files and utilities for mini-SWE-agent."""
|
| 2 |
|
| 3 | import json
|
| 4 | import os
|
| 5 | from pathlib import Path
|
| 6 |
|
| 7 | import yaml
|
| 8 |
|
| 9 | builtin_config_dir = Path(__file__).parent
|
| 10 |
|
| 11 |
|
| 12 | def get_config_path(config_spec: str | Path) -> Path:
|
| 13 | """Get the path to a config file."""
|
| 14 | config_spec = Path(config_spec)
|
| 15 | if config_spec.suffix != ".yaml":
|
| 16 | config_spec = config_spec.with_suffix(".yaml")
|
| 17 | candidates = [
|
| 18 | Path(config_spec),
|
| 19 | Path(os.getenv("MSWEA_CONFIG_DIR", ".")) / config_spec,
|
| 20 | builtin_config_dir / config_spec,
|
| 21 | builtin_config_dir / "extra" / config_spec,
|
| 22 | builtin_config_dir / "benchmarks" / config_spec,
|
| 23 | ]
|
| 24 | for candidate in candidates:
|
| 25 | if candidate.exists():
|
| 26 | return candidate
|
| 27 |
|
| 28 | raise FileNotFoundError(f"Could not find config file for {config_spec} (tried: {candidates})")
|
| 29 |
|
| 30 |
|
| 31 | def _key_value_spec_to_nested_dict(config_spec: str) -> dict:
|
| 32 | """Interpret key-value specs from the command line.
|
| 33 |
|
| 34 | Example:
|
| 35 |
|
| 36 | "model.model_name=anthropic/claude-sonnet-4-5-20250929" ->
|
| 37 | {"model": {"model_name": "anthropic/claude-sonnet-4-5-20250929"}}
|
| 38 | """
|
| 39 | key, value = config_spec.split("=", 1)
|
| 40 | try:
|
| 41 | value = json.loads(value)
|
| 42 | except json.JSONDecodeError:
|
| 43 | pass
|
| 44 | keys = key.split(".")
|
| 45 | result = {}
|
| 46 | current = result
|
| 47 | for k in keys[:-1]:
|
| 48 | current[k] = {}
|
| 49 | current = current[k]
|
| 50 | current[keys[-1]] = value
|
| 51 | return result
|
| 52 |
|
| 53 |
|
| 54 | def get_config_from_spec(config_spec: str | Path) -> dict:
|
| 55 | """Get a config from a config spec."""
|
| 56 | if isinstance(config_spec, str) and "=" in config_spec:
|
| 57 | return _key_value_spec_to_nested_dict(config_spec)
|
| 58 | path = get_config_path(config_spec)
|
| 59 | return yaml.safe_load(path.read_text())
|
| 60 |
|
| 61 |
|
| 62 | __all__ = ["builtin_config_dir", "get_config_path", "get_config_from_spec", "_key_value_spec_to_nested_dict"]
|
| 63 |
|