From bbe54f109bf8b1783970f019d14ceaf1cb4d9133 Mon Sep 17 00:00:00 2001 From: admin Date: Tue, 15 Sep 2026 15:19:03 +0800 Subject: [PATCH] =?UTF-8?q?fix(upstreams):=20upsert=20base=5Fraw=20?= =?UTF-8?q?=E6=94=B9=20Option=EF=BC=8C=E7=A9=BA=E4=B8=B2=E4=BF=9D=E7=95=99?= =?UTF-8?q?=E5=8E=9F=E5=80=BC=20(1.0.25)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `upsert` 其他字段(models/auth/note)语义都是 None / Some("") = 保留原值, Some(有效) = 校验覆盖。`base_raw` 是 `&str`,"" 被 `validate_base` 直接拦截报「缺少 base 参数」。 现场踩坑:用户改 minimax 槽位 resets 时点了「停用」按钮,前端只 发 `{slot: minimax, enabled: 0}`(其他字段没填),被 base 校验拦下。 修:`base_raw` 改 `Option<&String>`,与现有约定对齐。新建必填校验 移进闭包(existing.base 兜底),编辑「停用」/「改备注」/「改账号」 等不需要回填 base 的场景不再被拦。 回归 228/0 全绿。 --- rust/crates/plugin-llm/src/api.rs | 2 +- rust/crates/plugin-llm/src/upstreams.rs | 27 ++++++++++++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/rust/crates/plugin-llm/src/api.rs b/rust/crates/plugin-llm/src/api.rs index f48b2fc..fb5c994 100644 --- a/rust/crates/plugin-llm/src/api.rs +++ b/rust/crates/plugin-llm/src/api.rs @@ -112,7 +112,7 @@ fn h_admin_upstream_save(r: *mut ngx_http_request_t, _actor: Actor) -> Status { match upstreams::upsert( slot_raw, - p.get("base").map(String::as_str).unwrap_or(""), + p.get("base"), models, p.get("auth"), accounts, diff --git a/rust/crates/plugin-llm/src/upstreams.rs b/rust/crates/plugin-llm/src/upstreams.rs index d08584b..0081441 100644 --- a/rust/crates/plugin-llm/src/upstreams.rs +++ b/rust/crates/plugin-llm/src/upstreams.rs @@ -686,7 +686,7 @@ pub fn models_flat(allowed: &[String]) -> Vec<(String, String)> { /// 新增或覆盖一个上游。 /// /// 参数语义(兼容旧调用方,同时支持多账号): -/// * `models = None` / `auth_raw = None` / `note = None` 都表示**保持原值**; +/// * `models = None` / `auth_raw = None` / `note = None` / `base_raw = None` 都表示**保持原值**; /// `Some("")` 表示显式清空。 /// * `accounts = Some(..)` 表示用多账号列表**整体替换**(此时顶层 `auth` 不再生效, /// 写入时被清为 `""`,避免 `auth` 与 `accounts[0]` 重复);`None` 表示沿用旧逻辑 @@ -696,7 +696,7 @@ pub fn models_flat(allowed: &[String]) -> Vec<(String, String)> { /// `enabled = false` 会立刻从共享内存摘除。 pub fn upsert( slot_raw: &str, - base_raw: &str, + base_raw: Option<&String>, models: Option>, auth_raw: Option<&String>, accounts: Option>, @@ -706,7 +706,6 @@ pub fn upsert( note: Option<&String>, ) -> Result { let slot = validate_slot(slot_raw)?; - let base = validate_base(base_raw)?; let models = match models { Some(list) => Some(check_models(&list)?), None => None, @@ -717,6 +716,28 @@ pub fn upsert( // 先克隆出来:下面 insert 需要可变借用,不能与这里的不可变借用并存 let existing = table.get(&key).cloned(); + // base:None / `Some("")` = 保留原值;`Some(有效)` = 校验并覆盖。 + // 1.0.24 修:之前 base_raw 是 `&str`,管理台「停用」按钮(前端不传 base) + // 会被 `validate_base("")` 拦截。改成 Option + 保留原值后, + // 1) 新建时 base_raw 必传 → 否则报「缺少 base」; + // 2) 编辑时只传 enabled=false 也能停用(base 沿用原值)。 + let base = match base_raw { + Some(b) => { + let b = util::trim(b); + if b.is_empty() { + existing.as_ref().map(|e| e.base.clone()).unwrap_or_default() + } else { + validate_base(&b)? + } + } + None => existing.as_ref().map(|e| e.base.clone()).unwrap_or_default(), + }; + if base.is_empty() { + // 新建时(existing 为 None)必须 base 非空——上面已经保留了 existing.base, + // 这里只在「None + 无 existing」时报错(`base_raw = None` 是异常调用) + return Err("缺少 base 参数".to_owned()); + } + // 多账号:整体替换(清掉顶层 auth,避免重复);否则走 legacy 单 auth let (accounts, legacy_auth) = match accounts { Some(list) => {