feat(settings): rename custom gateway models

This commit is contained in:
chengma
2026-07-21 09:23:08 +08:00
parent 0da2d8a3c0
commit d76a1de864
6 changed files with 238 additions and 3 deletions
+92
View File
@@ -140,6 +140,7 @@ class SettingsTab(QWidget):
self.model_combo = QComboBox()
self.model_combo.setObjectName("aiModelCombo")
self.add_model_button = QPushButton("新增")
self.rename_model_button = QPushButton("重命名")
self.delete_model_button = QPushButton("删除")
self.enabled_checkbox = QCheckBox("启用")
@@ -250,6 +251,7 @@ class SettingsTab(QWidget):
model_picker_layout = QHBoxLayout()
model_picker_layout.addWidget(self.model_combo, 1)
model_picker_layout.addWidget(self.add_model_button)
model_picker_layout.addWidget(self.rename_model_button)
model_picker_layout.addWidget(self.delete_model_button)
action_layout = QHBoxLayout()
@@ -427,6 +429,7 @@ class SettingsTab(QWidget):
self.model_combo.currentIndexChanged.connect(self.load_selected_model)
self.add_model_button.clicked.connect(self.add_model)
self.rename_model_button.clicked.connect(self.rename_model)
self.delete_model_button.clicked.connect(self.delete_model)
self.save_model_button.clicked.connect(self.save_model)
self.test_connection_button.clicked.connect(self.test_connection)
@@ -716,6 +719,12 @@ class SettingsTab(QWidget):
try:
if self.current_model_name is None:
appconfig.add_ai_model(model, path=self.ai_models_path)
elif model["name"] != self.current_model_name:
self._rename_model_and_migrate_defaults(
self.current_model_name,
model["name"],
replacement=model,
)
else:
appconfig.update_ai_model(
self.current_model_name,
@@ -728,6 +737,32 @@ class SettingsTab(QWidget):
self.refresh_models(selected=model["name"])
self._set_status(f"AI 模型已保存:{model['name']}")
def rename_model(self, checked=False):
model = self._current_model()
if model is None:
return
old_name = model["name"]
new_name, accepted = QInputDialog.getText(
self,
"重命名 AI 模型",
"模型名称:",
QLineEdit.Normal,
old_name,
)
if not accepted:
return
new_name = new_name.strip()
if new_name == old_name:
self._set_status("AI 模型名称未改变")
return
try:
self._rename_model_and_migrate_defaults(old_name, new_name)
except Exception as exc:
self._show_error(exc)
return
self.refresh_models(selected=new_name)
self._set_status(f"AI 模型已重命名:{old_name} → {new_name}")
def delete_model(self, checked=False):
model = self._current_model()
if model is None:
@@ -1153,6 +1188,7 @@ class SettingsTab(QWidget):
):
widget.setEnabled(has_model and not testing)
self.add_model_button.setEnabled(not testing)
self.rename_model_button.setEnabled(has_model and not testing)
self.delete_model_button.setEnabled(
has_model and not testing and self._can_delete_model(self._current_model())
)
@@ -1474,6 +1510,62 @@ class SettingsTab(QWidget):
def _current_model(self):
return self._model_by_name(self.current_model_name)
def _rename_model_and_migrate_defaults(self, old_name, new_name, replacement=None):
"""Persist a model rename and keep stored/default UI references valid."""
original_model = appconfig.get_model(old_name, path=self.ai_models_path)
persisted_config = appconfig.load_config(self.config_path)
persisted_changed = self._migrate_default_model_references(
persisted_config,
old_name,
new_name,
)
renamed = False
try:
if replacement is None:
saved_model = appconfig.rename_ai_model(
old_name,
new_name,
path=self.ai_models_path,
)
else:
appconfig.update_ai_model(
old_name,
path=self.ai_models_path,
**replacement,
)
saved_model = appconfig.get_model(new_name, path=self.ai_models_path)
renamed = True
if persisted_changed:
appconfig.save_config(persisted_config, path=self.config_path)
except Exception:
if renamed:
try:
appconfig.update_ai_model(
new_name,
path=self.ai_models_path,
**original_model,
)
except Exception as rollback_exc:
raise appconfig.ConfigError(
"模型重命名失败,且无法恢复模型清单。请先备份数据目录后再重试。"
) from rollback_exc
raise
self._migrate_default_model_references(self.config, old_name, new_name)
return saved_model
@staticmethod
def _migrate_default_model_references(config, old_name, new_name):
ai_cfg = appconfig.ai_config(config)
changed = False
for key in ("default_text_model", "default_image_model"):
if ai_cfg.get(key) == old_name:
ai_cfg[key] = new_name
changed = True
if changed:
config["ai"] = ai_cfg
return changed
def _model_by_name(self, name):
for model in self.models:
if model.get("name") == name: