diff --git a/backend/app/routers/websites.py b/backend/app/routers/websites.py index f7f48c7..f38a213 100644 --- a/backend/app/routers/websites.py +++ b/backend/app/routers/websites.py @@ -1393,13 +1393,8 @@ def sync_website_accounts_upstream_models( # 构造 model_mapping model_mapping = {m: m for m in valid_models} - # 优先读取账号现有的 credentials 并 merge,以防覆盖掉 api_key/base_url 等其他配置 - current_creds = remote_acc.get("credentials") or {} - updated_creds = dict(current_creds) - updated_creds["model_mapping"] = model_mapping - - # 调用更新接口 - c.update_account(aid, {"credentials": updated_creds}) + # 直接发送增量字段进行更新,依赖远端的 JSONB merge 逻辑,避免覆盖/丢失敏感 credentials + c.update_account(aid, {"credentials": {"model_mapping": model_mapping}}) items.append(SyncUpstreamModelsItem( account_id=aid, diff --git a/backend/test_sync_upstream_models.py b/backend/test_sync_upstream_models.py index bea411b..37f3a4c 100644 --- a/backend/test_sync_upstream_models.py +++ b/backend/test_sync_upstream_models.py @@ -172,10 +172,10 @@ def test_sync_upstream_models_workflow(db_session, monkeypatch): assert item_1001.model_count == 2 assert item_1001.models == ["gpt-3.5-turbo", "gpt-4"] - # 1001 的 update 应当 merge credentials + # 1001 的 update 应当只传递增量的 model_mapping,防止敏感字段丢失 assert len(update_calls) == 1 assert update_calls[0][0] == "1001" - assert update_calls[0][1]["credentials"]["api_key"] == "k1" + assert "api_key" not in update_calls[0][1]["credentials"] assert update_calls[0][1]["credentials"]["model_mapping"] == { "gpt-3.5-turbo": "gpt-3.5-turbo", "gpt-4": "gpt-4"