feat(api): /api/v1/me 聚合能力目录 (upstreams + apix) (1.0.16)

- plugin-apix: 新增 apix::enabled(),仅列 enabled=true 的代理
- ipass-core: CoreExt::decorate_me 签名加 &User 和 &port,让装饰零额外
  shm 查询就能做权限收敛(upstreams 按 allowed_models 过滤)
- gateway::GatewayExt: decorate_me 多塞 upstreams(白名单过滤)和 apix
  (仅 enabled)两个字段;port 从请求 Host 头取(适配 88 / 443 反代)

老 3 个端点 /api/v1/{upstreams,apix,domains} 保留不破坏。同事拿 key 一个
me 拿全能力目录,不再需要分别调三个端点。

回归 281 全绿(网关 102 + LLM 179)
This commit is contained in:
admin committed 2026-09-14 17:14:00 +08:00
1 parent f34ba3d198
commit 479024c61e
3 files changed
+57 -8

No files matched your search

+13 -4
View File
@@ -136,8 +136,14 @@ pub fn run_guarded(r: *mut ngx_http_request_t, guard: Guard, handler: Handler) -
pub trait CoreExt: Sync {
/// 管理台账号列表:插件侧装饰(域名数 / 用量摘要),一次批量给全部账号
fn decorate_admin_users(&self, _views: &mut [Value]) {}
/// `/api/v1/me`:插件侧装饰(我的域名 / 用量摘要 / 策略 TTL)
fn decorate_me(&self, _username: &str, _view: &mut Value) {}
/// `/api/v1/me`:插件侧装饰(我的域名 / 用量摘要 / 策略 TTL / 能力目录)。
///
/// `_user` 直接传 `User` 而不只是 username:能力目录(upstreams/apix)
/// 需要按 `allowed_models` 白名单过滤,让 decorate 零额外 shm 查询就能做权限收敛。
/// `_port` 从请求 `Host` 头取,**不要写死**——apix/upstreams 的 `view_all` 要
/// 用它拼 `https://lm.mayishangshu.cn{port}/...`,部署在 88 时必须带 `:88`,
/// 放 443 反代后面又不能带端口(见 `plugin_apix::call_url` / `plugin_llm::client_base`)。
fn decorate_me(&self, _user: &crate::models::User, _port: &str, _view: &mut Value) {}
/// 删除账号:连带回收该账号名下资源(域名),返回条数
fn reclaim_on_delete(&self, _username: &str) -> usize {
0
@@ -440,9 +446,12 @@ fn h_me(r: *mut ngx_http_request_t, actor: Actor) -> Status {
};
let mut v = users::view(&user);
// 我的域名 / 用量摘要 / 策略 TTL 都是插件侧数据,由装配根装饰
// 我的域名 / 用量摘要 / 策略 TTL / 能力目录(upstreams + apix)都是
// 插件侧数据,由装配根装饰。port 从 Host 头取,让装饰能拼出正确的
// `https://lm.mayishangshu.cn:88/...` base_url(部署在 88 时必须带)。
let port = http::host_port(r);
if let Some(e) = ext() {
e.decorate_me(&user.username, &mut v);
e.decorate_me(&user, &port, &mut v);
}
http::ok(r, v, "ok")
}
+9
View File
@@ -472,6 +472,15 @@ pub fn all() -> Vec<(String, ApiProxy)> {
.collect()
}
/// 仅启用(`enabled = true`)的代理条目。
///
/// `plugin-apix::api::h_list` 与 gateway 的 `decorate_me` 都走这个:
/// 给同事/自己看的清单只该含「现在能调」的代理,停用的不该暴露,
/// 否则列表里出现一行「调就 404」会让人误以为是网关坏掉。
pub fn enabled() -> Vec<(String, ApiProxy)> {
all().into_iter().filter(|(_, it)| it.enabled).collect()
}
/// 客户端应当使用的调用地址(base,不带 query)。端口由调用方从请求 Host 头取,
/// **不要写死**(部署在 88 时要带 `:88`,放在 443 反代后面时不能带)。
/// query 参数由前端按 `params` 拼一次(避免后端与前端各拼一次导致 `?q=?q=`)。
+35 -4
View File
@@ -132,17 +132,31 @@ impl ipass_core::api::CoreExt for GatewayExt {
}
}
/// `/api/v1/me`:我的域名 + 用量摘要 + 策略 TTL
fn decorate_me(&self, username: &str, view: &mut Value) {
/// `/api/v1/me`:我的域名 + 用量摘要 + 策略 TTL + **能力目录**(upstreams + apix)。
///
/// 能力目录让同事拿一把 key 一个 me 拿全,不再需要分别调
/// `/api/v1/upstreams` / `/api/v1/apix` / `/api/v1/domains`。
/// 老 3 个端点保留,客户端脚本不破坏。
///
/// LLM 上游按 `allowed_models` 白名单过滤(复用 `plugin_llm::upstreams::available`),
/// apix 只列 `enabled = true` 的代理(`plugin_apix::apix::enabled`),
/// 否则列表里出现「调就 404」让人误判网关坏掉。
/// `port` 从请求 Host 头取,**不要写死**——见 `plugin_apix::call_url` 注释。
fn decorate_me(&self, user: &ipass_core::models::User, port: &str, view: &mut Value) {
let Value::Object(ref mut m) = view else {
return;
};
m.insert(
"domains".to_owned(),
json!(plugin_domain::domains::view_all(&plugin_domain::domains::by_owner(username))),
json!(plugin_domain::domains::view_all(
&plugin_domain::domains::by_owner(&user.username),
)),
);
// 用量摘要(明细走 /api/v1/usage)——让客户端一个请求就能决定要不要自我节流
m.insert("llm_usage".to_owned(), plugin_llm::stats::user(username, util::now()).json());
m.insert(
"llm_usage".to_owned(),
plugin_llm::stats::user(&user.username, util::now()).json(),
);
m.insert(
"policy".to_owned(),
json!({
@@ -150,6 +164,23 @@ impl ipass_core::api::CoreExt for GatewayExt {
"temp_domain_ttl_hours": plugin_domain::config::TEMP_DOMAIN_TTL / 3600,
}),
);
// 能力目录:upstreams(按白名单过滤)+ apix(仅 enabled)。
// 这两个 view_all 都返回完整结构(slot/base_url/models 或 slug/url/params/...),
// 同事拿到后能直接配客户端,不再需要二次问「这个 slot 怎么调」。
m.insert(
"upstreams".to_owned(),
json!(plugin_llm::upstreams::view_all(
&plugin_llm::upstreams::available(&user.allowed_models),
port,
)),
);
m.insert(
"apix".to_owned(),
json!(plugin_apix::apix::view_all(
&plugin_apix::apix::enabled(),
port,
)),
);
}
/// 删除账号:连带回收该账号名下的域名,避免出现无主映射