feat(llm): 上游拆 OpenAI/Anthropic 双地址 (1.0.40)
每上游存 base(OpenAI) + base_anthropic(Anthropic) 两条完整真实 URL; 网关出口纯 /<slot> 与 /<slot>/anthropic,厂商协议路径段原样留在 base* 里, 由 join_target 拼到 SDK 余下路径前。解决智谱/阿里单 base 无法表达 不同 OpenAI 与 Anthropic 路径段导致 404 进冷却的问题。 - models.rs: Upstream 加 base_anthropic 字段 - upstreams.rs: derive_anthropic/effective_anthropic、shm payload 三字段、 read_up 三元组、pick_account 按协议选 base、base_url/anthropic_base_url 纯网关出口、upsert 支持 base_anthropic 保留语义、view 回显 base_anthropic - llm.rs: is_anthropic 按 /anthropic 段分流、upstream_uri 剥标记段防重复拼 - api.rs/upstreams.js/me.js: 管理台双地址输入与回显 - tests: GLM 双协议夹具 + §9.5 路由断言,回归 245/0
This commit is contained in:
1 parent
d56dd52c3e
commit
1e52eae468
8 files changed
+209
-84
No files matched your search
@@ -116,6 +116,7 @@ fn h_admin_upstream_save(r: *mut ngx_http_request_t, _actor: Actor) -> Status {
|
||||
match upstreams::upsert(
|
||||
slot_raw,
|
||||
p.get("base"),
|
||||
p.get("base_anthropic"),
|
||||
models,
|
||||
p.get("auth"),
|
||||
accounts,
|
||||
|
||||
@@ -146,10 +146,35 @@ pub fn slot_of(r: *mut ngx_http_request_t) -> Option<String> {
|
||||
Some(slot.to_ascii_lowercase())
|
||||
}
|
||||
|
||||
/// 这条请求是不是走 Anthropic 协议:`/<slot>/anthropic/...` 是,其余不是。
|
||||
///
|
||||
/// 判定只看 slot **之后**的第一段路径是否 `anthropic`——模型名不在路径里,
|
||||
/// 不会误判。内部测试 location `/_upstream_test/<slot>/anthropic/...` 同样识别。
|
||||
pub fn is_anthropic(r: *mut ngx_http_request_t) -> bool {
|
||||
let raw = http::uri(r);
|
||||
let uri = strip_internal_prefix(&raw);
|
||||
let rest = uri.strip_prefix('/').unwrap_or(uri);
|
||||
// 剥掉 slot 段(第一段),看剩下第一段是否 `anthropic`
|
||||
let after_slot = match rest.find('/') {
|
||||
Some(i) => &rest[i..],
|
||||
None => return false,
|
||||
};
|
||||
after_slot
|
||||
.trim_start_matches('/')
|
||||
.split('/')
|
||||
.next()
|
||||
.map(|s| s.eq_ignore_ascii_case("anthropic"))
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
/// 剥掉 slot 段、拼回 query:`/openai/v1/chat/completions?a=1` → `/v1/chat/completions?a=1`
|
||||
///
|
||||
/// Anthropic 协议时(`anthropic = true`),额外剥掉路径里紧跟 slot 的
|
||||
/// `/anthropic` 段——那段是网关替客户端表达的「协议标记」,不属于上游路径;
|
||||
/// 不剥会让 `base_anthropic`(已以 `/anthropic` 结尾)变成 `…/anthropic/anthropic/...`。
|
||||
///
|
||||
/// query 必须在这里拼——变量形式的 `proxy_pass` **不会**自动追加原始 args。
|
||||
pub fn upstream_uri(r: *mut ngx_http_request_t) -> String {
|
||||
pub fn upstream_uri(r: *mut ngx_http_request_t, anthropic: bool) -> String {
|
||||
let uri = http::uri(r);
|
||||
let uri = strip_internal_prefix(&uri);
|
||||
let rest = uri.strip_prefix('/').unwrap_or(uri);
|
||||
@@ -158,6 +183,16 @@ pub fn upstream_uri(r: *mut ngx_http_request_t) -> String {
|
||||
Some(i) => &rest[i..],
|
||||
None => "/",
|
||||
};
|
||||
// Anthropic:剥掉紧跟 slot 的 `/anthropic` 段(及其后接的 `/` 由 join_target 处理)
|
||||
let path = if anthropic {
|
||||
match path.strip_prefix("/anthropic") {
|
||||
Some(tail) if tail.is_empty() => "/",
|
||||
Some(tail) => tail,
|
||||
None => path,
|
||||
}
|
||||
} else {
|
||||
path
|
||||
};
|
||||
let args = http::args_raw(r);
|
||||
if args.is_empty() {
|
||||
path.to_owned()
|
||||
@@ -330,11 +365,14 @@ pub extern "C" fn var_get(
|
||||
unsafe {
|
||||
let resolved = (|| -> Option<(String, String, String, usize)> {
|
||||
let slot = slot_of(r)?;
|
||||
// 协议分流:路径里有没有 `/anthropic` 段决定走 base_anthropic 还是 base
|
||||
let anthropic = is_anthropic(r);
|
||||
let key = auth::downstream_key(r);
|
||||
let now = util::now();
|
||||
// 选账号:粘性绑定 + 跳过冷却中账号,返回 (下标, auth, base)
|
||||
let (idx, auth, base) = upstreams::pick_account(&slot, key.as_deref(), now)?;
|
||||
let target = join_target(&base, &upstream_uri(r));
|
||||
let (idx, auth, base) =
|
||||
upstreams::pick_account(&slot, anthropic, key.as_deref(), now)?;
|
||||
let target = join_target(&base, &upstream_uri(r, anthropic));
|
||||
let host = host_of(&base).to_owned();
|
||||
Some((target, host, auth, idx))
|
||||
})();
|
||||
|
||||
@@ -38,8 +38,22 @@ pub struct UpstreamAccount {
|
||||
/// 因此存量数据零改动即可继续工作。
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Upstream {
|
||||
/// 上游基址,含 scheme,如 `https://api.openai.com`(可带端口)
|
||||
/// OpenAI 兼容协议的真实上游基址(含完整路径段)。
|
||||
///
|
||||
/// 例:`https://api.deepseek.com/v1` / `https://open.bigmodel.cn/api/coding/paas/v4`
|
||||
/// / `https://token-plan.cn-beijing.maas.aliyuncs.com/compatible-mode/v1`。
|
||||
/// 各厂商路径段完全不同,**网关只原样把它拼到 SDK 转发的余下路径前**,
|
||||
/// 不做任何 `/v1` 猜测(见 `upstreams::base_url` 与 `llm::join_target`)。
|
||||
pub base: String,
|
||||
/// Anthropic 协议的真实上游基址(如 `https://api.deepseek.com/anthropic` /
|
||||
/// `https://open.bigmodel.cn/api/anthropic` / `https://token-plan.../apps/anthropic`)。
|
||||
///
|
||||
/// 各家与 OpenAI 路径**完全独立、无法推导**(阿里是 `/apps/anthropic`、
|
||||
/// 智谱是 `/api/anthropic`、deepseek/minimax 是 `/anthropic`),所以必须显式配。
|
||||
/// 旧数据只有 `base` 时由 `upstreams::derive_anthropic` 兜底派生(仅对
|
||||
/// deepseek/minimax 这类「`base` 末尾 `/v1` 或根路径」的厂商正确)。
|
||||
#[serde(default)]
|
||||
pub base_anthropic: String,
|
||||
/// 该上游**挂载的模型名清单**(如 `deepseek-chat` / `deepseek-reasoner`)。
|
||||
///
|
||||
/// 一个上游(一家厂商)通常挂多个模型,客户端要能先"列出有什么"再选。
|
||||
|
||||
@@ -116,6 +116,31 @@ pub fn validate_base(raw: &str) -> Result<String, String> {
|
||||
Ok(s)
|
||||
}
|
||||
|
||||
/// 从 OpenAI 基址兜底派生 Anthropic 基址。
|
||||
///
|
||||
/// 仅对「`base` 末尾是 `/v1`(deepseek/openai)或根路径(minimax)」的厂商正确;
|
||||
/// 阿里(`/compatible-mode/v1` → `/apps/anthropic`)、智谱
|
||||
/// (`/api/coding/paas/v4` → `/api/anthropic`)路径完全对不上,**必须显式配
|
||||
/// `base_anthropic`**。留这个兜底是为了让「只配了 OpenAI 地址」的旧上游不至于
|
||||
/// 在 `/<slot>/anthropic` 上 404。
|
||||
fn derive_anthropic(base: &str) -> String {
|
||||
let t = base.trim_end_matches('/');
|
||||
if let Some(stripped) = t.strip_suffix("/v1") {
|
||||
format!("{stripped}/anthropic")
|
||||
} else {
|
||||
format!("{t}/anthropic")
|
||||
}
|
||||
}
|
||||
|
||||
/// 取生效的 Anthropic 基址:显式配了用显式的,否则派生。
|
||||
fn effective_anthropic(up: &Upstream) -> String {
|
||||
if up.base_anthropic.trim().is_empty() {
|
||||
derive_anthropic(&up.base)
|
||||
} else {
|
||||
up.base_anthropic.trim().to_owned()
|
||||
}
|
||||
}
|
||||
|
||||
// ===========================================================================
|
||||
// 共享内存同步(写穿 / 装载)
|
||||
// ===========================================================================
|
||||
@@ -124,9 +149,9 @@ pub fn validate_base(raw: &str) -> Result<String, String> {
|
||||
/// 实际部署不会接近这个数;超出部分不被支持(管理台会拦)。
|
||||
const ACCOUNT_INDEX_MAX: usize = 64;
|
||||
|
||||
/// `up:<slot>` payload:`base \n <账号数>`。
|
||||
fn up_payload(base: &str, n: usize) -> String {
|
||||
format!("{base}\n{n}")
|
||||
/// `up:<slot>` payload:`base \n base_anthropic \n <账号数>`。
|
||||
fn up_payload(base: &str, base_anthropic: &str, n: usize) -> String {
|
||||
format!("{base}\n{base_anthropic}\n{n}")
|
||||
}
|
||||
|
||||
/// `upa:<slot>:<idx>` payload:`auth \n <resets csv>`(resets 空则第二段为空)。
|
||||
@@ -156,11 +181,12 @@ pub fn sync_shm(slot: &str, up: &Upstream) {
|
||||
return;
|
||||
}
|
||||
let accts = up.effective_accounts();
|
||||
let ba = effective_anthropic(up);
|
||||
|
||||
if !state::put(
|
||||
KEY_UP_PREFIX,
|
||||
slot.as_bytes(),
|
||||
up_payload(&up.base, accts.len()).as_bytes(),
|
||||
up_payload(&up.base, &ba, accts.len()).as_bytes(),
|
||||
0,
|
||||
) {
|
||||
util::log_err(&format!(
|
||||
@@ -231,10 +257,11 @@ pub fn reload_shm() -> usize {
|
||||
}
|
||||
expected += 1;
|
||||
let accts = up.effective_accounts();
|
||||
let ba = effective_anthropic(&up);
|
||||
if state::put(
|
||||
KEY_UP_PREFIX,
|
||||
slot.as_bytes(),
|
||||
up_payload(&up.base, accts.len()).as_bytes(),
|
||||
up_payload(&up.base, &ba, accts.len()).as_bytes(),
|
||||
0,
|
||||
) {
|
||||
loaded += 1;
|
||||
@@ -258,12 +285,14 @@ pub fn reload_shm() -> usize {
|
||||
loaded
|
||||
}
|
||||
|
||||
/// 读 `up:<slot>`:`(base, 账号数)`。
|
||||
pub fn read_up(slot: &str) -> Option<(String, usize)> {
|
||||
/// 读 `up:<slot>`:`(base_openai, base_anthropic, 账号数)`。
|
||||
pub fn read_up(slot: &str) -> Option<(String, String, usize)> {
|
||||
let (payload, _) = state::get(KEY_UP_PREFIX, slot.as_bytes())?;
|
||||
let (base, nstr) = payload.split_once('\n').unwrap_or((&payload, "0"));
|
||||
let n = nstr.trim().parse::<usize>().unwrap_or(0);
|
||||
Some((base.to_owned(), n))
|
||||
let mut it = payload.split('\n');
|
||||
let base = it.next().unwrap_or("").to_owned();
|
||||
let ba = it.next().unwrap_or("").to_owned();
|
||||
let n = it.next().unwrap_or("0").trim().parse::<usize>().unwrap_or(0);
|
||||
Some((base, ba, n))
|
||||
}
|
||||
|
||||
/// 规范化账号 Authorization 头值:缺 scheme 时自动补 `Bearer `。
|
||||
@@ -445,10 +474,11 @@ fn cold_index(key: Option<&str>, n: usize) -> usize {
|
||||
/// ⚠️ 只读 `up:` / `upa:` / `uac:` / `ukb:`,**不回源读文件**;未命中即视为不可用。
|
||||
pub fn pick_account(
|
||||
slot: &str,
|
||||
anthropic: bool,
|
||||
key: Option<&str>,
|
||||
now: i64,
|
||||
) -> Option<(usize, String, String)> {
|
||||
let (base, n) = read_up(slot)?;
|
||||
let (base, base_anthropic, n) = read_up(slot)?;
|
||||
if n == 0 {
|
||||
return None;
|
||||
}
|
||||
@@ -512,7 +542,7 @@ pub fn pick_account(
|
||||
}
|
||||
}
|
||||
|
||||
Some((chosen.0, chosen.1, base))
|
||||
Some((chosen.0, chosen.1, if anthropic { base_anthropic } else { base }))
|
||||
}
|
||||
|
||||
// ===========================================================================
|
||||
@@ -624,62 +654,44 @@ pub fn available(allowed: &[String]) -> Vec<(String, Upstream)> {
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// 客户端应当使用的 base_url。
|
||||
///
|
||||
/// 规则:**把上游 base 的 scheme+host 换成网关域名,路径原样保留**——
|
||||
/// 这样它才真的能和厂商文档里的 base_url 逐个字符替换:
|
||||
/// 客户端应当使用的 OpenAI 兼容 `base_url`(网关出口地址,纯 slot,不带厂商路径)。
|
||||
///
|
||||
/// ```text
|
||||
/// https://api.deepseek.com/v1 → https://lm.mayishangshu.cn:88/deepseek/v1
|
||||
/// https://api.openai.com → https://lm.mayishangshu.cn:88/openai
|
||||
/// https://lm.mayishangshu.cn:88/deepseek → 客户端 OpenAI SDK 填这个
|
||||
/// https://lm.mayishangshu.cn:88/glm
|
||||
/// ```
|
||||
///
|
||||
/// `port` 由调用方从请求 Host 头取(`http::host_port`),**不要写死**:
|
||||
/// 部署在 88 时客户端必须带 `:88` 才连得到,而放在 443 反代后面时又必须不带端口。
|
||||
/// 写死任何一种,都会在另一种部署里给出一个「看着对、实际打不通」的地址。
|
||||
/// **关键**:出口地址是**纯 `/<slot>`**,不拼 `/v1`、也不带上游 base 的路径段
|
||||
/// (`/v1` / `/compatible-mode/v1` / `/api/coding/paas/v4` 等)。因为:
|
||||
///
|
||||
/// 反过来(不管上游 base 的路径、一律拼 `/v1`)会让「base 自带 /v1」的上游
|
||||
/// 变成 `/v1/v1/chat/completions` → 上游 404,且客户端拿到的是空响应体,
|
||||
/// 排查起来完全看不出是网关拼错了。**实测踩过,别再改回去。**
|
||||
pub fn base_url(slot: &str, up: &Upstream, port: &str) -> String {
|
||||
let rest = up.base.split("://").nth(1).unwrap_or("");
|
||||
let path = match rest.find('/') {
|
||||
Some(i) => &rest[i..],
|
||||
None => "",
|
||||
};
|
||||
let mut out = format!("https://{LLM_PUBLIC_HOST}{port}/{slot}{path}");
|
||||
// **1.0.37**:客户端 OpenAI SDK 不会自动在 base_url 后拼 `/v1`,
|
||||
// 而我们的网关 location 只接 `/<slot>/v1/...`(slot 拆完就是 /v1/chat/completions)。
|
||||
// 上游 base 若已是 `…/v1`(厂商文档常见写法),path 已经带 /v1,
|
||||
// 不能再无脑追加 —— join_target 的去重逻辑负责上游拼装,这里只看出口 URL。
|
||||
// 出口 URL 必须保证末尾是 `/v1`(覆盖无 path 的 base 与已有 path 但不是 v1 的两种情况)。
|
||||
if !out.ends_with("/v1") {
|
||||
out.push_str("/v1");
|
||||
}
|
||||
out
|
||||
/// 1. 各厂商的版本/协议路径段千差万别(见上),网关**不能**在这里猜——它原样
|
||||
/// 存在 `base`(`base_openai`)里,转发时由 `join_target` 拼到 SDK 转发的
|
||||
/// 余下路径前。出口地址只表达「打到哪个上游」,路径交给上游基址。
|
||||
/// 2. OpenAI 兼容 SDK 不会自动在 base_url 后拼 `/v1`,它拼的是 `/chat/completions`;
|
||||
/// 网关收到 `/<slot>/chat/completions` 后,把 `/chat/completions` 接到 `base`
|
||||
/// 后面即得 `https://api.deepseek.com/v1/chat/completions`(对 `/compatible-mode/v1`
|
||||
/// 等同样适用)。历史客户端若把 base_url 写成 `…/deepseek/v1`,网关靠
|
||||
/// `join_target` 的版本段去重依然兼容,不会变 `/v1/v1/...`。
|
||||
///
|
||||
/// `port` 由调用方从请求 Host 头取(`http::host_port`),**不要写死**:
|
||||
/// 部署在 88 时客户端必须带 `:88` 才连得到,放在 443 反代后面时又必须不带端口。
|
||||
pub fn base_url(slot: &str, _up: &Upstream, port: &str) -> String {
|
||||
format!("https://{LLM_PUBLIC_HOST}{port}/{slot}")
|
||||
}
|
||||
|
||||
/// Anthropic 客户端(Claude Code / Claude SDK)应当使用的 `ANTHROPIC_BASE_URL`。
|
||||
///
|
||||
/// **1.0.37 修正**:Anthropic SDK **会自动在 base_url 后拼 `/v1/messages`**,
|
||||
/// 所以出口 URL **不带 /v1**——让 SDK 自己拼,避免拼出 `/v1/v1/messages` 404。
|
||||
/// 网关出口地址:`/<slot>/anthropic`。Anthropic SDK **会自动在 base_url 后拼
|
||||
/// `/v1/messages`**,所以出口地址**不带 /v1**——让 SDK 自己拼,避免
|
||||
/// `/v1/v1/messages` 404。
|
||||
///
|
||||
/// 网关 location 同时兼容 `…/anthropic/v1/messages` 与 `…/anthropic/messages`:
|
||||
/// 后者由 `join_target` 把 SDK 拼的 `/v1` 接进 upstream base(去重),
|
||||
/// 两种都 200。
|
||||
///
|
||||
/// 协议段由**客户端**负责表达(网关不猜协议),但这段路径是网关替客户端拼好的,
|
||||
/// 所以要处理重合:上游 `base` 若已钉在 `…/anthropic`(协议钉死型记录),其
|
||||
/// `base_url` 末尾已经带 `/anthropic`,再无脑追加会得到 `…/anthropic/anthropic`。
|
||||
pub fn anthropic_base_url(slot: &str, up: &Upstream, port: &str) -> String {
|
||||
let b = base_url(slot, up, port);
|
||||
// base_url 已保证末尾是 /v1,先切掉再拼 /anthropic
|
||||
let b = b.trim_end_matches("/v1");
|
||||
if b.ends_with("/anthropic") {
|
||||
b.to_string()
|
||||
} else {
|
||||
format!("{b}/anthropic")
|
||||
}
|
||||
/// 网关收到 `/<slot>/anthropic/v1/messages` 后,剥掉 `/anthropic` 段,把
|
||||
/// `/v1/messages` 接到 `base_anthropic`(`https://api.deepseek.com/anthropic`)后
|
||||
/// 即得 `https://api.deepseek.com/anthropic/v1/messages`。各厂商 Anthropic 路径
|
||||
/// 不同(阿里 `/apps/anthropic`、智谱 `/api/anthropic`),同样原样存在
|
||||
/// `base_anthropic` 里,网关不猜。
|
||||
pub fn anthropic_base_url(slot: &str, _up: &Upstream, port: &str) -> String {
|
||||
format!("https://{LLM_PUBLIC_HOST}{port}/{slot}/anthropic")
|
||||
}
|
||||
|
||||
/// 模型发现:把可用上游挂载的模型摊平成 `(模型名, slot)` 列表。
|
||||
@@ -715,6 +727,7 @@ pub fn models_flat(allowed: &[String]) -> Vec<(String, String)> {
|
||||
pub fn upsert(
|
||||
slot_raw: &str,
|
||||
base_raw: Option<&String>,
|
||||
base_anthropic_raw: Option<&String>,
|
||||
models: Option<Vec<String>>,
|
||||
auth_raw: Option<&String>,
|
||||
accounts: Option<Vec<UpstreamAccount>>,
|
||||
@@ -756,6 +769,27 @@ pub fn upsert(
|
||||
return Err("缺少 base 参数".to_owned());
|
||||
}
|
||||
|
||||
// base_anthropic:OpenAI 兼容地址之外的 Anthropic 协议地址。
|
||||
// 语义与 base 一致(None / `Some("")` = 保留原值;`Some(有效)` = 校验并覆盖);
|
||||
// 新建且未传时兜底派生(仅对 deepseek/minimax 这类厂商正确,阿里/智谱必须显式配)。
|
||||
let base_anthropic = match base_anthropic_raw {
|
||||
Some(b) => {
|
||||
let b = util::trim(b);
|
||||
if b.is_empty() {
|
||||
existing
|
||||
.as_ref()
|
||||
.map(|e| e.base_anthropic.clone())
|
||||
.unwrap_or_else(|| derive_anthropic(&base))
|
||||
} else {
|
||||
validate_base(&b)?
|
||||
}
|
||||
}
|
||||
None => existing
|
||||
.as_ref()
|
||||
.map(|e| e.base_anthropic.clone())
|
||||
.unwrap_or_else(|| derive_anthropic(&base)),
|
||||
};
|
||||
|
||||
// 多账号:整体替换(清掉顶层 auth,避免重复);否则走 legacy 单 auth
|
||||
let (accounts, legacy_auth) = match accounts {
|
||||
Some(list) => {
|
||||
@@ -828,6 +862,7 @@ pub fn upsert(
|
||||
|
||||
let item = Upstream {
|
||||
base,
|
||||
base_anthropic,
|
||||
models,
|
||||
enabled,
|
||||
note,
|
||||
@@ -908,6 +943,7 @@ pub fn view(slot: &str, up: &Upstream, port: &str) -> Value {
|
||||
json!({
|
||||
"slot": slot,
|
||||
"base": up.base,
|
||||
"base_anthropic": effective_anthropic(up),
|
||||
"base_url": base_url(slot, up, port),
|
||||
"anthropic_base_url": anthropic_base_url(slot, up, port),
|
||||
"models": up.models,
|
||||
|
||||
+34
-14
@@ -285,21 +285,18 @@ print('yes' if ok else 'no')
|
||||
ck "alice 查模型 → 200" 200 "$(code -H "X-API-Key: $ALICE_KEY" $API/api/v1/models)"
|
||||
ck " 模型清单里带 base_url(lm 域名 + 端口 + slot)" yes \
|
||||
"$(curl -s -H "X-API-Key: $ALICE_KEY" $API/api/v1/models | has "lm.mayishangshu.cn:$API_PORT/openai")"
|
||||
# ★ base_url 必须**保留上游 base 的路径**:openai 的 base 无路径 → 无;other 的 base 带 /llm → 带 /llm。
|
||||
# 搞错这一条会让"base 自带 /v1"的上游变成 /v1/v1/... → 上游 404 且响应体为空,极难排查。
|
||||
# (校验 base_url 字段的**完整值**而不是整份响应的子串——后者会被同段新增的
|
||||
# anthropic_base_url(…/openai/anthropic)误伤。)
|
||||
#
|
||||
# **1.0.37 修正**:base_url **必须保证末尾是 /v1**(OpenAI SDK 不会自动拼 /v1),
|
||||
# 否则 SDK 用 base_url 拼 `/chat/completions` 会得到 `/openai/chat/completions`,
|
||||
# 网关拆出 slot 后上游路径只剩 `/chat/completions` → 上游 404 且响应体为空。
|
||||
# 上游 base 自带 /v1 时(如 `https://api.openai.com/v1`)也不能凭空再加一次,
|
||||
# 走 join_target 去重;出口 URL 一定以 /v1 结尾。
|
||||
ck "★base_url 强制带 /v1(base 无路径时补上)" yes \
|
||||
# ★ base_url / anthropic_base_url 是**纯网关出口地址**(只含 slot,不带厂商路径、不带 /v1):
|
||||
# 厂商版本/协议路径段(/v1 / /compatible-mode/v1 / /api/coding/paas/v4 / /anthropic …)
|
||||
# 原样存在上游的 base / base_anthropic 里,转发时由 join_target 拼到 SDK 余下路径前。
|
||||
# 这样 OpenAI SDK 填 `…/openai`、Anthropic SDK 填 `…/openai/anthropic` 即可,网关不猜路径。
|
||||
# (校验完整字段值而非整份响应子串——anthropic_base_url 同段会出现,避免误伤。)
|
||||
ck "★base_url 是纯 slot(无 /v1、不带上游 base 路径)" yes \
|
||||
"$(curl -s -H "X-API-Key: $ALICE_KEY" $API/api/v1/models | has "\"base_url\":\"https://lm.mayishangshu.cn:$API_PORT/openai\"")"
|
||||
ck "★base_url 不含 /v1(版本段交给上游 base)" no \
|
||||
"$(curl -s -H "X-API-Key: $ALICE_KEY" $API/api/v1/models | has "\"base_url\":\"https://lm.mayishangshu.cn:$API_PORT/openai/v1\"")"
|
||||
ck "★base_url 保留上游 base 的路径 /llm(不重复加 /v1)" yes \
|
||||
"$(curl -s -H "X-API-Key: $BOB_KEY" $API/api/v1/models | has "lm.mayishangshu.cn:$API_PORT/other/llm/v1")"
|
||||
ck "★anthropic_base_url 不带 /v1(Anthropic SDK 内部拼)" yes \
|
||||
ck "★base_url 纯 slot:other 也不带 /llm(路径在上游 base 里)" yes \
|
||||
"$(curl -s -H "X-API-Key: $BOB_KEY" $API/api/v1/models | has "lm.mayishangshu.cn:$API_PORT/other\"")"
|
||||
ck "★anthropic_base_url 是 /<slot>/anthropic(Anthropic SDK 内部拼 /v1/messages)" yes \
|
||||
"$(curl -s -H "X-API-Key: $ALICE_KEY" $API/api/v1/models | has "anthropic_base_url\":\"https://lm.mayishangshu.cn:$API_PORT/openai/anthropic\"")"
|
||||
# ★ 端口自适应:Host 不带端口(走 443 反代)时 base_url 也不该带端口。
|
||||
# 写死 88 → 443 部署下客户端连到另一个端口的服务;裸域名 → 88 部署下被出口设备抢答
|
||||
@@ -309,6 +306,25 @@ ck "★Host 无端口 → base_url 不带端口" yes \
|
||||
ck "★Host 带端口 → base_url 带同一端口" yes \
|
||||
"$(curl -s -H "X-API-Key: $ALICE_KEY" -H "Host: lm.example:$API_PORT" $API/api/v1/models | has "lm.mayishangshu.cn:$API_PORT/openai")"
|
||||
|
||||
echo
|
||||
echo "【9.5】★ 双协议地址:OpenAI 与 Anthropic 路径各走各的(智谱/阿里形态)"
|
||||
# glm 上游 OpenAI 路径 /api/coding/paas/v4、Anthropic 路径 /api/anthropic,完全不同。
|
||||
# 网关按「路径里有没有 /anthropic 段」分流到 base / base_anthropic,其余路径原样拼。
|
||||
# - `/glm/chat/completions` → 上游 /api/coding/paas/v4/chat/completions
|
||||
# - `/glm/anthropic/v1/messages` → 上游 /api/anthropic/v1/messages
|
||||
# 用 admin key(绕过白名单)直打,up_field path 读靶机实际收到的路径。
|
||||
GLM_BODY='{"model":"glm-4","max_tokens":1,"messages":[{"role":"user","content":"hi"}]}'
|
||||
ck "★glm OpenAI 协议 → 200" 200 \
|
||||
"$(code -X POST -H "X-API-Key: $ADMIN_KEY" -H 'Content-Type: application/json' \
|
||||
-d "$GLM_BODY" $GW/glm/chat/completions)"
|
||||
ck " 靶机路径 = base + /chat/completions(无 /v1 猜测)" \
|
||||
"/api/coding/paas/v4/chat/completions" "$(up_field path)"
|
||||
ck "★glm Anthropic 协议 → 200" 200 \
|
||||
"$(code -X POST -H "X-API-Key: $ADMIN_KEY" -H 'Content-Type: application/json' \
|
||||
-d "$GLM_BODY" $GW/glm/anthropic/v1/messages)"
|
||||
ck " 靶机路径 = base_anthropic + /v1/messages(/anthropic 段已剥)" \
|
||||
"/api/anthropic/v1/messages" "$(up_field path)"
|
||||
|
||||
ALICE_USAGE=$(curl -s -H "X-API-Key: $ALICE_KEY" $API/api/v1/usage)
|
||||
ck "alice 查用量 → 200" 200 "$(code -H "X-API-Key: $ALICE_KEY" $API/api/v1/usage)"
|
||||
ALICE_TOTAL=$(echo "$ALICE_USAGE" | jqv data.total)
|
||||
@@ -340,6 +356,10 @@ ck "管理员登录 → 200" 200 "$(echo "$LOGIN" | jqv code)"
|
||||
|
||||
USAGE=$(curl -s -b "$JAR" $API/api/admin/usage)
|
||||
ck "用量总览含 alice" yes "$(echo "$USAGE" | has '"username": *"alice"')"
|
||||
# ★ 上游视图返回 base_anthropic:双协议地址各厂商路径不同,必须随视图给出
|
||||
# (智谱 /api/coding/paas/v4 ↔ /api/anthropic、阿里 /compatible-mode/v1 ↔ /apps/anthropic)。
|
||||
ck "★上游视图返回 base_anthropic 字段" yes \
|
||||
"$(curl -s -b "$JAR" $API/api/admin/upstreams | has '"base_anthropic"')"
|
||||
ck " 总览含按上游分组(openai)" yes "$(echo "$USAGE" | has '"slot": *"openai"')"
|
||||
ck " 全局合计 > 0" yes "$(echo "$USAGE" | jqv data.total.total | awk '{print ($1>0)?"yes":"no"}')"
|
||||
ck "★删除前用量榜里有 bob(他真调过 other)" yes "$(echo "$USAGE" | has '"username": *"bob"')"
|
||||
|
||||
@@ -65,5 +65,14 @@
|
||||
],
|
||||
"note": "验证 normalize_auth:账号 auth 写成裸 key,转发时网关自动补 `Bearer `",
|
||||
"created_at": 1757500000
|
||||
},
|
||||
"glm": {
|
||||
"base": "http://127.0.0.1:18999/api/coding/paas/v4",
|
||||
"base_anthropic": "http://127.0.0.1:18999/api/anthropic",
|
||||
"models": ["glm-4", "glm-4v"],
|
||||
"auth": "Bearer sk-GLM-REAL",
|
||||
"enabled": true,
|
||||
"note": "双协议地址验证:OpenAI 路径 /api/coding/paas/v4 与 Anthropic /api/anthropic 完全不同,各走各的(智谱真实形态)",
|
||||
"created_at": 1757500000
|
||||
}
|
||||
}
|
||||
+6
-5
@@ -104,15 +104,16 @@
|
||||
|
||||
/**
|
||||
* 同一个 upstream 通常**两个 base_url 都存在**:
|
||||
* - `base_url`:OpenAI 协议路径(如 `https://lm.mayishangshu.cn:88/deepseek/v1`)
|
||||
* - `anthropic_base_url`:Anthropic 协议路径(如 `https://lm.mayishangshu.cn:88/deepseek/anthropic`)
|
||||
* - `base_url`:OpenAI 协议出口(如 `https://lm.mayishangshu.cn:88/deepseek`)
|
||||
* - `anthropic_base_url`:Anthropic 协议出口(如 `https://lm.mayishangshu.cn:88/deepseek/anthropic`)
|
||||
*
|
||||
* 网关出口是**纯 slot**(不带厂商路径、不带 /v1)——厂商版本/协议路径段
|
||||
* 原样存在上游配置里,转发时拼到 SDK 余下路径前。客户端 SDK 自己拼
|
||||
* `/chat/completions` 或 `/v1/messages`,网关不猜路径。
|
||||
*
|
||||
* **1.0.34b**:只展示**纯 URL**(不加任何尾巴),点击按钮直接复制。
|
||||
* 不展示 curl —— WorkBuddy / Claude Code 客户端拿到 URL 后**自行拼路径**,
|
||||
* 中间再加 `/chat/completions` 这种尾巴会 404。
|
||||
*
|
||||
* 注意 base_url 末尾已经带 `/v1` 或 `/anthropic` —— 客户端 SDK 看到
|
||||
* `https://host/slot/v1` 就知道 base path。
|
||||
*/
|
||||
function renderUpstreams(upstreams) {
|
||||
var el = $("dUpstream");
|
||||
|
||||
@@ -74,8 +74,12 @@
|
||||
'<div><label>slot(URL 前缀)</label>' +
|
||||
'<input id="upSlot" type="text" placeholder="openai" ' + (edit ? 'value="' + IP.esc(up.slot) + '" readonly' : 'required') + '></div>' +
|
||||
'<div><label>备注</label><input id="upNote" type="text" placeholder="可选" value="' + IP.esc(up ? up.note : "") + '"></div>' +
|
||||
'<div class="span-all"><label>上游地址(含路径段,如 <code>/v1</code>)</label>' +
|
||||
'<input id="upBase" class="field-mono" type="text" placeholder="https://api.deepseek.com/v1" value="' + IP.esc(up ? up.base : "") + '" required></div>' +
|
||||
'<div class="span-all"><label>OpenAI 兼容地址(base,含完整路径段)</label>' +
|
||||
'<input id="upBase" class="field-mono" type="text" placeholder="https://api.deepseek.com/v1 或 https://open.bigmodel.cn/api/coding/paas/v4" value="' + IP.esc(up ? up.base : "") + '" required>' +
|
||||
'<div class="muted" style="font-size:12px;margin-top:4px">网关出口即 <code>https://lm…:88/<slot></code>,路径段原样存在这里,网关不拼 <code>/v1</code>。</div></div>' +
|
||||
'<div class="span-all"><label>Anthropic 地址(base_anthropic,含完整路径段)</label>' +
|
||||
'<input id="upBaseAnthropic" class="field-mono" type="text" placeholder="https://api.deepseek.com/anthropic 或 https://open.bigmodel.cn/api/anthropic" value="' + IP.esc(up ? up.base_anthropic : "") + '">' +
|
||||
'<div class="muted" style="font-size:12px;margin-top:4px">网关出口即 <code>https://lm…:88/<slot>/anthropic</code>。各家与 OpenAI 路径不同(阿里 <code>/apps/anthropic</code>、智谱 <code>/api/anthropic</code>),**必须显式配**;留空则按 base 派生(仅 deepseek/minimax 正确)。</div></div>' +
|
||||
'<div class="span-all"><label>多账号(同一厂商多把 Key,额度耗尽自动切换同厂商下一个)</label>' +
|
||||
'<div class="muted" style="font-size:12px;margin:2px 0 6px">每行填 <code>Authorization</code> 头值(<code>Bearer sk-…</code>)。只填裸 key 也会自动补 <code>Bearer </code>。</div>' +
|
||||
'<div id="upAccounts" class="acct-list">' + rows + '</div>' +
|
||||
@@ -139,6 +143,7 @@
|
||||
var payload = {
|
||||
slot: card.querySelector("#upSlot").value.trim(),
|
||||
base: card.querySelector("#upBase").value.trim(),
|
||||
base_anthropic: card.querySelector("#upBaseAnthropic").value.trim(),
|
||||
note: card.querySelector("#upNote").value.trim(),
|
||||
// 模型清单总是显式提交:编辑框里就是"当前值",清空即清空(可预期)
|
||||
models: card.querySelector("#upModels").value,
|
||||
@@ -226,7 +231,8 @@
|
||||
"<td><b>" + IP.esc(u.slot) + "</b><div style='margin-top:4px'>" + stBadge + "</div>" +
|
||||
"<div class='row-actions' style='margin-top:6px'>" + urlBtns + "</div>" +
|
||||
"<div class='muted' style='font-size:11.5px;margin-top:4px'>点击复制 API 地址</div></td>" +
|
||||
"<td class='mono base-cell'>" + IP.esc(u.base) + "</td>" +
|
||||
"<td class='mono base-cell'>" + IP.esc(u.base) +
|
||||
(u.base_anthropic ? "<br><span class='muted'>" + IP.esc(u.base_anthropic) + "</span>" : "") + "</td>" +
|
||||
"<td>" + keyCell + "</td>" +
|
||||
"<td>" + (models.length ? "<span class='badge b-blue'>" + models.length + " 个</span>" + modelCell : modelCell) + "</td>" +
|
||||
"<td>" + IP.esc(u.note || "-") + "</td>" +
|
||||
|
||||
Reference in new issue
Block a user