fix: address review — no auto-hijack, sync model on activation, cleanup

1. Creating a provider no longer auto-switches bot_type. Only an
   explicit make_active=true changes the active model — prevents
   silently hijacking users on Claude/OpenAI/etc.

2. When a provider IS activated, its 'model' is now written into the
   global 'model' field. This ensures all three paths (regular chat,
   agent_bridge, vision) use the correct model without per-path patches.

3. Removed unused i18n key 'models_custom_name_exists' (no longer
   referenced after the id-based rework removed name-collision checks).

4. Updated tests: 39 passing (added model-sync tests, fixed tests that
   relied on the removed auto-activation behavior).
This commit is contained in:
kirs-hi
2026-06-12 10:05:05 +08:00
parent 1940d628a8
commit 0092376c07
3 changed files with 56 additions and 16 deletions

View File

@@ -46,7 +46,6 @@ const I18N = {
models_custom_delete_confirm_msg: '确定删除该自定义厂商吗?此操作无法撤销。',
models_custom_name_required: '请填写名称',
models_custom_base_required: '请填写 API Base',
models_custom_name_exists: '该名称已存在',
models_custom_empty: '尚未配置自定义厂商,点击添加',
models_custom_edit_title: '编辑自定义厂商',
models_custom_add_title: '添加自定义厂商',
@@ -268,7 +267,6 @@ const I18N = {
models_custom_delete_confirm_msg: 'Delete this custom provider? This cannot be undone.',
models_custom_name_required: 'Name is required',
models_custom_base_required: 'API Base is required',
models_custom_name_exists: 'A provider with this name already exists',
models_custom_empty: 'No custom providers yet, click to add',
models_custom_edit_title: 'Edit custom provider',
models_custom_add_title: 'Add custom provider',

View File

@@ -2798,7 +2798,12 @@ class ModelsHandler:
"""Write the providers list to both in-memory conf and the on-disk
config, then reset the bridge so bots rebuild.
If ``bot_type`` is given, also update ``bot_type``."""
If ``bot_type`` is given, also update ``bot_type``. When activating a
provider (bot_type is ``custom:<id>``), also write the provider's
``model`` into the global ``model`` field so that all paths (chat,
agent, vision) automatically use the correct model."""
from models.custom_provider import parse_custom_bot_type
local_config = conf()
file_cfg = self._read_file_config()
local_config["custom_providers"] = providers
@@ -2806,6 +2811,13 @@ class ModelsHandler:
if bot_type is not None:
local_config["bot_type"] = bot_type
file_cfg["bot_type"] = bot_type
# Sync the provider's model into the global model field.
_, pid = parse_custom_bot_type(bot_type)
if pid:
provider = next((p for p in providers if p.get("id") == pid), None)
if provider and provider.get("model"):
local_config["model"] = provider["model"]
file_cfg["model"] = provider["model"]
self._write_file_config(file_cfg)
self._reset_bridge()
@@ -2865,12 +2877,9 @@ class ModelsHandler:
existing.pop("model", None)
created = False
# Decide bot_type.
_, current_active_id = parse_custom_bot_type(local_config.get("bot_type") or "")
# Decide bot_type — only switch when explicitly requested.
new_bot_type = None
if make_active or (created and not current_active_id):
# Activate on explicit request, or auto-activate the very first
# provider so the resolver has a definite target.
if make_active:
new_bot_type = f"custom:{provider_id}"
self._persist_custom_providers(providers, new_bot_type)