Skip to content

Commit 190edc4

Browse files
committed
fix: preserve old model config when switching and configuring new models
- first_run_configure: migrate existing default to fallback before overwriting - configure_new_model: new model becomes default, old default moves to fallback - _configure_modelscope_with_test: fix fallback variable reference - switch_model: guard against duplicate fallback entries
1 parent 7965698 commit 190edc4

2 files changed

Lines changed: 24 additions & 14 deletions

File tree

chcode/config.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,15 @@ async def first_run_configure() -> dict | None:
165165
console.print(f"[red]连接失败: {e}[/red]")
166166
return None
167167

168-
data = {"default": config, "fallback": {}}
168+
data = load_model_json()
169+
old_default = data.get("default")
170+
fallback = data.get("fallback", {})
171+
if old_default:
172+
old_name = old_default.get("model", "")
173+
if old_name and old_name not in fallback:
174+
fallback[old_name] = old_default
175+
data["default"] = config
176+
data["fallback"] = fallback
169177
save_model_json(data)
170178
console.print(f"[green]配置完成: {model}[/green]")
171179

@@ -221,10 +229,12 @@ async def configure_new_model() -> dict | None:
221229
data["default"] = config
222230
data["fallback"] = {}
223231
else:
224-
# 已有默认 — 新配置加入 fallback
225-
if config["model"] not in fallback:
226-
fallback[config["model"]] = config
227-
data["fallback"] = fallback
232+
# 已有默认 — 新模型设为默认,旧默认移到 fallback
233+
old_name = old_default.get("model", "")
234+
if old_name and old_name not in fallback:
235+
fallback[old_name] = old_default
236+
data["default"] = config
237+
data["fallback"] = fallback
228238

229239
save_model_json(data)
230240
console.print(f"[green]模型配置已保存: {config['model']}[/green]")
@@ -269,7 +279,7 @@ async def _configure_modelscope_with_test() -> dict | None:
269279
save_model_json(ms_config)
270280
else:
271281
# 已有配置 — 旧的 default 移入 fallback,魔搭作为新 default,合并 fallback
272-
if old_default["model"] not in ms_config["fallback"]:
282+
if old_default["model"] not in existing_fallback:
273283
existing_fallback[old_default["model"]] = old_default
274284
existing_fallback.update(ms_config["fallback"])
275285
data["default"] = ms_config["default"]
@@ -355,7 +365,7 @@ async def switch_model() -> dict | None:
355365
return None
356366

357367
selected_config = fallback.pop(selected_name)
358-
if default:
368+
if default and current_name not in fallback:
359369
fallback[current_name] = default
360370

361371
data["default"] = selected_config

tests/test_config_extended.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ async def test_first_config_becomes_default(self, mock_config_dir):
188188
assert data["default"]["model"] == "test-model"
189189

190190
@pytest.mark.asyncio
191-
async def test_second_config_goes_to_fallback(self, mock_config_dir):
192-
"""Test second configuration goes to fallback"""
191+
async def test_second_config_becomes_default(self, mock_config_dir):
192+
"""Test second configuration becomes default, old default moves to fallback"""
193193
import chcode.config as mod
194194

195195
# Setup existing default
@@ -201,7 +201,7 @@ async def test_second_config_goes_to_fallback(self, mock_config_dir):
201201
)
202202

203203
new_config = {
204-
"model": "fallback-model",
204+
"model": "new-model",
205205
"base_url": "https://api.test.com/v1",
206206
"api_key": "sk-test",
207207
"stream_usage": True,
@@ -219,12 +219,12 @@ async def test_second_config_goes_to_fallback(self, mock_config_dir):
219219
result = await mod.configure_new_model()
220220

221221
assert result is not None
222-
assert result["model"] == "fallback-model"
222+
assert result["model"] == "new-model"
223223

224-
# Verify it went to fallback
224+
# New model becomes default, old default moves to fallback
225225
data = mod.load_model_json()
226-
assert data["default"]["model"] == "default-model"
227-
assert "fallback-model" in data["fallback"]
226+
assert data["default"]["model"] == "new-model"
227+
assert "default-model" in data["fallback"]
228228

229229
@pytest.mark.asyncio
230230
async def test_user_cancels_form(self, mock_config_dir):

0 commit comments

Comments
 (0)