feat(llm): 上游多账号路由 + 账号级负载统计 (1.0.14)

一个 slot 挂多把上游账号 Key,解决厂商「单账号额度/并发受限」与
「同一用户缓存命中」两个矛盾诉求。

多账号路由 (plugin-llm)
- models: Upstream.accounts[] (auth/note/resets) + 上游级
  exhaust_status/exhaust_markers;顶层 auth 保留兼容(accounts 为空时合成单账号)
- upstreams::pick_account:粘性绑定 ukb:<key> 优先 → 无绑定时
  FNV-1a(下游 Key) % 可用数 冷分散 → 绑定账号冷却则重新分散 → 全冷却取最早恢复
  * 分散避开厂商单账号 agent 并发上限;落点固定保住 prompt/token 缓存
- cool_until_until 天锚定冷却:boundary = day0 + ceil(已过/窗口)*窗口,
  封顶次日 0 点(MiniMax 5h 窗口、24=5*4+4 → 末段 20:00-24:00 只有 4h 随天重置)
- 耗尽双检测 (log 阶段):$upstream_status 命中 exhaust_status,或
  usage_filter 流式扫到 marker → $llm_acct 定位本次账号 → 写冷却 uac:
- shm: up:/upa:/uac:/ukb:/upe: 五类前缀;$llm_acct 变量 (NOCACHEABLE)

账号级负载统计(问题发现与跟踪)
- 新增 uda:<slot>:<idx>:<day>(请求/token)与 uex:(耗尽次数) 日桶,
  复用内核 usage::bump_day —— 无数据库、无新进程(项目红线)
- 计数点只在 log 阶段一次;pick_account 会被 $llm_target/$llm_auth/$llm_acct
  三个变量各触发一次,放进那里会三倍计数
- view() 投影带每账号 load 与 account_trend + trend_labels(日期由后端算)
- 管理台「上游」页密钥列:各 Key 今日请求/token/耗尽 + 负载比例条 +
  14 天迷你趋势(自绘 SVG,零第三方依赖)+ 冷却/耗尽徽标 + 上游合计

测试与验证
- 回归 279 条全绿(网关 102 + LLM 177),新增 6 条多账号/负载断言
- 假上游新增 UPSTREAM_FAIL_AUTH 模拟账号额度耗尽;靶机验证 Key 真被替换
- 前端以真实 admin.css + upstreams.js 渲染路径做无头浏览器断言:
  比例条与负载成比例(412 vs 96 → 100%/23%)、每账号 14 条块、
  悬停峰值日期与数据逐位吻合、单账号/无账号兜底正常、无横向溢出、零 JS 错误

文档
- 新增 docs/llm/MULTI_ACCOUNT_ROUTING.md(数据模型/路由/冷却数学/shm 布局/负载统计)
- readme §11 数据模型 + §15 上游配置 + §15 用量统计 + §12.6 tag 表 + §16 变更记录
- 修正 docs/llm/*.md 中插件化前的过时路径(rust/src/* → crates/plugin-llm/src/*)
This commit is contained in:
admin committed 2026-09-14 11:57:08 +08:00
1 parent db99ce1521
commit bfa3cf8ff8
21 files changed
+1506 -85

No files matched your search

+9
View File
@@ -8,6 +8,15 @@
> **关联文档**:`docs/llm/UPSTREAM_PROTOCOL_AND_AUTH.md`(出站头 / 多协议)、
> `rust/src/stats.rs`(当前请求级计数实现)、`rust/src/config.rs` §LLM 限流。
> **最后更新**:2026-09-12(基于 ipass-1.0.6)。
>
> ⚠️ **路径已过时**:本文写于插件化重构(`ipass-1.0.12`)之前,正文里的
> `rust/src/*.rs` 与其中的行号均为**当时的单 crate 布局**。现对应关系:
> `rust/src/stats.rs` → `rust/crates/plugin-llm/src/stats.rs`、
> `rust/src/config.rs` → 策略常量分家(内核 `rust/crates/ipass-core/src/config.rs` + 插件 `rust/crates/plugin-llm/src/config.rs`)、
> `rust/src/lib.rs` → 拆为 `rust/gateway/src/lib.rs`(装配根 + 管理 API 的 `ApiHandler`)
> 与各插件的 `handlers.rs`;LLM 热路径的三个处理器(`LlmHandler` / `LlmHealthHandler` /
> `AuditHandler`)在 `rust/crates/plugin-llm/src/handlers.rs`,`stats::record` 的调用点也在此。
> 阅读时以**模块名**为准,不要按行号定位。
---