|
1 | 1 | import os |
2 | 2 | import pathlib |
3 | 3 | import yaml |
| 4 | +from typing import Optional |
4 | 5 |
|
5 | 6 | DEFAULT_CONFIG = { |
6 | 7 | "ollama_url": "http://localhost:11434", |
|
14 | 15 | "skeptic": True, |
15 | 16 | "gemini_api_key": "", |
16 | 17 | "use_gemini_fallback": False, |
| 18 | + "gpu_tier": "auto", |
| 19 | + "model_classifier": "", |
| 20 | + "model_coder": "", |
| 21 | + "model_reasoner": "", |
| 22 | + "model_skeptic": "", |
17 | 23 | "agent_mappings": { |
18 | 24 | "errors": [ |
19 | 25 | {"pattern": "FAILED tests/", "agents": ["agency-reality-checker", "agency-senior-developer"]}, |
|
47 | 53 |
|
48 | 54 | class Config: |
49 | 55 | def __init__(self, config_path=None): |
50 | | - self.config_path = config_path or os.path.expanduser("~/.ghostcoder/config.yaml") |
51 | 56 | self.data = DEFAULT_CONFIG.copy() |
| 57 | + self.global_config_path = os.path.expanduser("~/.ghostcoder/config.yaml") |
| 58 | + self.local_config_path = self._find_local_config() |
| 59 | + |
| 60 | + if config_path: |
| 61 | + self.config_path = config_path |
| 62 | + else: |
| 63 | + self.config_path = self.local_config_path or self.global_config_path |
| 64 | + |
52 | 65 | self.load() |
53 | 66 |
|
| 67 | + def _find_local_config(self) -> Optional[str]: |
| 68 | + # Walk up to find .ghostcoder/config.yml or config.yaml |
| 69 | + try: |
| 70 | + curr = pathlib.Path(os.getcwd()).resolve() |
| 71 | + for parent in [curr] + list(curr.parents): |
| 72 | + for filename in ["config.yml", "config.yaml"]: |
| 73 | + p = parent / ".ghostcoder" / filename |
| 74 | + if p.exists() and p.is_file(): |
| 75 | + return str(p) |
| 76 | + except Exception: |
| 77 | + pass |
| 78 | + return None |
| 79 | + |
54 | 80 | def load(self): |
55 | | - path = pathlib.Path(self.config_path) |
56 | | - if path.exists(): |
| 81 | + # 1. Load global |
| 82 | + global_path = pathlib.Path(self.global_config_path) |
| 83 | + if global_path.exists(): |
57 | 84 | try: |
58 | | - with open(path, "r", encoding="utf-8") as f: |
| 85 | + with open(global_path, "r", encoding="utf-8") as f: |
59 | 86 | user_data = yaml.safe_load(f) |
60 | 87 | if user_data: |
61 | 88 | self._merge(self.data, user_data) |
62 | 89 | except Exception as e: |
63 | | - print(f"Error loading config file: {e}") |
| 90 | + print(f"Error loading global config: {e}") |
64 | 91 | else: |
65 | | - # Ensure folder exists |
66 | | - path.parent.mkdir(parents=True, exist_ok=True) |
67 | | - self.save() |
| 92 | + # Create global directory and file on first run |
| 93 | + try: |
| 94 | + global_path.parent.mkdir(parents=True, exist_ok=True) |
| 95 | + with open(global_path, "w", encoding="utf-8") as f: |
| 96 | + yaml.safe_dump(DEFAULT_CONFIG, f) |
| 97 | + except Exception: |
| 98 | + pass |
| 99 | + |
| 100 | + # 2. Load local |
| 101 | + if self.local_config_path: |
| 102 | + local_path = pathlib.Path(self.local_config_path) |
| 103 | + if local_path.exists(): |
| 104 | + try: |
| 105 | + with open(local_path, "r", encoding="utf-8") as f: |
| 106 | + user_data = yaml.safe_load(f) |
| 107 | + if user_data: |
| 108 | + self._merge(self.data, user_data) |
| 109 | + except Exception as e: |
| 110 | + print(f"Error loading local config: {e}") |
68 | 111 |
|
69 | 112 | def _merge(self, base, update): |
70 | 113 | for k, v in update.items(): |
@@ -107,13 +150,66 @@ def vram_headroom_mb(self): |
107 | 150 | def coder_idle_timeout(self): |
108 | 151 | return self.data.get("coder_idle_timeout", 30.0) |
109 | 152 |
|
| 153 | + def resolve_model(self, role: str) -> str: |
| 154 | + # 1. Check for manual override in config |
| 155 | + override_key = f"model_{role}" |
| 156 | + if self.data.get(override_key): |
| 157 | + return self.data[override_key] |
| 158 | + |
| 159 | + # 2. Check for backward compatibility with old config keys |
| 160 | + if role == "classifier" and self.data.get("classifier_model"): |
| 161 | + if self.data["classifier_model"] != DEFAULT_CONFIG["classifier_model"]: |
| 162 | + return self.data["classifier_model"] |
| 163 | + if role == "coder" and self.data.get("coder_model"): |
| 164 | + if self.data["coder_model"] != DEFAULT_CONFIG["coder_model"]: |
| 165 | + return self.data["coder_model"] |
| 166 | + |
| 167 | + # 3. Detect GPU and use preset |
| 168 | + from .backends.gpu_tier import GPUTierDetector, MODEL_PRESETS |
| 169 | + |
| 170 | + configured_tier = self.gpu_tier |
| 171 | + if configured_tier == "auto": |
| 172 | + gpu_info = GPUTierDetector.detect() |
| 173 | + configured_tier = gpu_info.tier |
| 174 | + |
| 175 | + presets = MODEL_PRESETS.get(configured_tier, MODEL_PRESETS["entry"]) |
| 176 | + return presets.get(role, "") |
| 177 | + |
| 178 | + @property |
| 179 | + def gpu_tier(self): |
| 180 | + return self.data.get("gpu_tier", "auto") |
| 181 | + |
| 182 | + @property |
| 183 | + def model_classifier(self): |
| 184 | + return self.data.get("model_classifier", "") |
| 185 | + |
| 186 | + @property |
| 187 | + def model_coder(self): |
| 188 | + return self.data.get("model_coder", "") |
| 189 | + |
| 190 | + @property |
| 191 | + def model_reasoner(self): |
| 192 | + return self.data.get("model_reasoner", "") |
| 193 | + |
| 194 | + @property |
| 195 | + def model_skeptic(self): |
| 196 | + return self.data.get("model_skeptic", "") |
| 197 | + |
110 | 198 | @property |
111 | 199 | def classifier_model(self): |
112 | | - return self.data.get("classifier_model", "qwen2.5:0.5b") |
| 200 | + return self.resolve_model("classifier") |
113 | 201 |
|
114 | 202 | @property |
115 | 203 | def coder_model(self): |
116 | | - return self.data.get("coder_model", "qwen2.5-coder:1.5b") |
| 204 | + return self.resolve_model("coder") |
| 205 | + |
| 206 | + @property |
| 207 | + def reasoner_model(self): |
| 208 | + return self.resolve_model("reasoner") |
| 209 | + |
| 210 | + @property |
| 211 | + def skeptic_model(self): |
| 212 | + return self.resolve_model("skeptic") |
117 | 213 |
|
118 | 214 | @property |
119 | 215 | def skeptic(self): |
|
0 commit comments