fix: 优化上游模型同步的写回逻辑,采用增量 JSONB merge,避免脱敏的 credentials 覆盖或丢失原有敏感字段

This commit is contained in:
liumangmang
2026-07-02 15:47:34 +08:00
parent 63cbdf4dca
commit 3c1fff2491
2 changed files with 4 additions and 9 deletions
+2 -7
View File
@@ -1393,13 +1393,8 @@ def sync_website_accounts_upstream_models(
# 构造 model_mapping # 构造 model_mapping
model_mapping = {m: m for m in valid_models} model_mapping = {m: m for m in valid_models}
# 优先读取账号现有的 credentials 并 merge,以防覆盖掉 api_key/base_url 等其他配置 # 直接发送增量字段进行更新,依赖远端的 JSONB merge 逻辑,避免覆盖/丢失敏感 credentials
current_creds = remote_acc.get("credentials") or {} c.update_account(aid, {"credentials": {"model_mapping": model_mapping}})
updated_creds = dict(current_creds)
updated_creds["model_mapping"] = model_mapping
# 调用更新接口
c.update_account(aid, {"credentials": updated_creds})
items.append(SyncUpstreamModelsItem( items.append(SyncUpstreamModelsItem(
account_id=aid, account_id=aid,
+2 -2
View File
@@ -172,10 +172,10 @@ def test_sync_upstream_models_workflow(db_session, monkeypatch):
assert item_1001.model_count == 2 assert item_1001.model_count == 2
assert item_1001.models == ["gpt-3.5-turbo", "gpt-4"] assert item_1001.models == ["gpt-3.5-turbo", "gpt-4"]
# 1001 的 update 应当 merge credentials # 1001 的 update 应当只传递增量的 model_mapping,防止敏感字段丢失
assert len(update_calls) == 1 assert len(update_calls) == 1
assert update_calls[0][0] == "1001" 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"] == { assert update_calls[0][1]["credentials"]["model_mapping"] == {
"gpt-3.5-turbo": "gpt-3.5-turbo", "gpt-3.5-turbo": "gpt-3.5-turbo",
"gpt-4": "gpt-4" "gpt-4": "gpt-4"