mirror of
https://gitcode.com/JianFeeeee/ModelRouter.git
synced 2026-11-07 17:57:23 +00:00
Compare commits
75 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 652a035d47 | |||
| 6bec260513 | |||
| ed954b77bd | |||
| cb3ea25586 | |||
| 0232b9cd24 | |||
| a07449d32e | |||
| 84a9fccd9a | |||
| 63549db832 | |||
| d6d41209d9 | |||
| 3c59c33c58 | |||
| 5948ad19ca | |||
| 6b6f9526ff | |||
| 4d0efab86b | |||
| b6e687b567 | |||
| 2353d43c16 | |||
| 893857464d | |||
| 2f5cde7fea | |||
| da0282a3db | |||
| 6f2b6b8d3d | |||
| 96d97b0e0e | |||
| 6de9f28269 | |||
| 50997a4e3a | |||
| 74ec1e7874 | |||
| fa50ec8bc7 | |||
| c3b858d5ba | |||
| a59295e826 | |||
| dafc3b3fc3 | |||
| 66786bb0e3 | |||
| f4d5e779a5 | |||
| 61f402beef | |||
| 57fada3b0d | |||
| b6148e6f79 | |||
| 24e00cdc50 | |||
| 9016eda905 | |||
| 4d1407bbb4 | |||
| 5f6939b590 | |||
| a0ab9ddda5 | |||
| e4111337cd | |||
| 93eeba3762 | |||
| 958f1f48b5 | |||
| 9d6653a573 | |||
| 1c6a2821e4 | |||
| 3ad46ea23e | |||
| 6113924655 | |||
| d3203d88fc | |||
| 7836b95a84 | |||
| b6d0358079 | |||
| 0858a747ca | |||
| 7934507179 | |||
| f042bbf4af | |||
| 97a6cf9224 | |||
| d92db3a9fd | |||
| 0e747b0891 | |||
| be8d753e36 | |||
| 438629256d | |||
| a873fe4724 | |||
| 31bc3968c5 | |||
| c40b5b5981 | |||
| 7ce0378e8b | |||
| ae9f56ddb3 | |||
| 3860c7381f | |||
| 94c1e7688f | |||
| b8386465ea | |||
| f682205d26 | |||
| ba81a4d7e1 | |||
| e30199b29d | |||
| a1fddf72b7 | |||
| b9a025efba | |||
| 7c7cf3e078 | |||
| 18525ae4c0 | |||
| c4b37d4322 | |||
| 33b778faee | |||
| 345cbb30d6 | |||
| 501ad6aac9 | |||
| 39cda89def |
24
.snapshots/chat_nonstream.json
Normal file
24
.snapshots/chat_nonstream.json
Normal file
@ -0,0 +1,24 @@
|
||||
{
|
||||
"choices": [
|
||||
{
|
||||
"finish_reason": "stop",
|
||||
"index": 0,
|
||||
"message": {
|
||||
"content": "pong",
|
||||
"role": "assistant"
|
||||
}
|
||||
}
|
||||
],
|
||||
"created": <TS>,
|
||||
"id": "<ID>",
|
||||
"model": "good-m",
|
||||
"object": "chat.completion",
|
||||
"usage": {
|
||||
"completion": 1,
|
||||
"completion_tokens": 1,
|
||||
"prompt": 3,
|
||||
"prompt_tokens": 3,
|
||||
"total": 4,
|
||||
"total_tokens": 4
|
||||
}
|
||||
}
|
||||
95
.snapshots/chat_stream.txt
Normal file
95
.snapshots/chat_stream.txt
Normal file
@ -0,0 +1,95 @@
|
||||
{
|
||||
"choices": [
|
||||
{
|
||||
"delta": {
|
||||
"content": "",
|
||||
"role": "assistant"
|
||||
},
|
||||
"finish_reason": null,
|
||||
"index": 0
|
||||
}
|
||||
],
|
||||
"created": <TS>,
|
||||
"id": "<ID>",
|
||||
"model": "good-m",
|
||||
"object": "chat.completion.chunk"
|
||||
}
|
||||
{
|
||||
"choices": [
|
||||
{
|
||||
"delta": {
|
||||
"content": "Hel",
|
||||
"role": "assistant"
|
||||
},
|
||||
"finish_reason": null,
|
||||
"index": 0
|
||||
}
|
||||
],
|
||||
"created": <TS>,
|
||||
"id": "<ID>",
|
||||
"model": "good-m",
|
||||
"object": "chat.completion.chunk"
|
||||
}
|
||||
{
|
||||
"choices": [
|
||||
{
|
||||
"delta": {
|
||||
"content": "lo",
|
||||
"role": "assistant"
|
||||
},
|
||||
"finish_reason": null,
|
||||
"index": 0
|
||||
}
|
||||
],
|
||||
"created": <TS>,
|
||||
"id": "<ID>",
|
||||
"model": "good-m",
|
||||
"object": "chat.completion.chunk"
|
||||
}
|
||||
{
|
||||
"choices": [
|
||||
{
|
||||
"delta": {
|
||||
"content": "",
|
||||
"role": "assistant"
|
||||
},
|
||||
"finish_reason": "stop",
|
||||
"index": 0
|
||||
}
|
||||
],
|
||||
"created": <TS>,
|
||||
"id": "<ID>",
|
||||
"model": "good-m",
|
||||
"object": "chat.completion.chunk"
|
||||
}
|
||||
{
|
||||
"choices": [
|
||||
{
|
||||
"delta": {
|
||||
"content": ""
|
||||
},
|
||||
"finish_reason": "stop",
|
||||
"index": 0
|
||||
}
|
||||
],
|
||||
"created": <TS>,
|
||||
"id": "<ID>",
|
||||
"model": "good-m",
|
||||
"object": "chat.completion.chunk"
|
||||
}
|
||||
{
|
||||
"choices": [],
|
||||
"created": <TS>,
|
||||
"id": "<ID>",
|
||||
"model": "good-m",
|
||||
"object": "chat.completion.chunk",
|
||||
"usage": {
|
||||
"completion": 1,
|
||||
"completion_tokens": 1,
|
||||
"prompt": 16,
|
||||
"prompt_tokens": 16,
|
||||
"total": 17,
|
||||
"total_tokens": 17
|
||||
}
|
||||
}
|
||||
[DONE]
|
||||
8
.snapshots/image_generation.json
Normal file
8
.snapshots/image_generation.json
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
"created": <TS>,
|
||||
"data": [
|
||||
{
|
||||
"b64_json": "QUJD"
|
||||
}
|
||||
]
|
||||
}
|
||||
72
README.md
72
README.md
@ -69,7 +69,9 @@ Groq、Mistral、Ollama、KimiCode…)通过 **Lua 适配器** 做协议转换
|
||||
(所有现有状态均已占用)时批量预热,顺序流量始终只用 1 个状态。
|
||||
- 缩容步长由当前连接数决定:`clamp(ceil(冗余/(1+占用)), 1, 冗余)`——无连接时一轮收到
|
||||
常驻下限,忙时每轮只释放 1 个,保护热路径。WebUI「适配器」页展示实时池指标。
|
||||
- **disable_thinking**:请求 `disable_thinking:true`(或上游对应字段)动态开关推理。
|
||||
- **thinking 预算**:请求里的推理开关/预算归一为中立的 `thinking:{enabled,budget_tokens}`,
|
||||
适配器映射到各自协议(Anthropic `thinking.budget_tokens`、Gemini `thinkingConfig`、
|
||||
DeepSeek `thinking.type="disabled"`)。
|
||||
- **Lua 适配协议**:每个源挂一个 `.lua` 适配器,完成 `transform_request` /
|
||||
`transform_response` / `transform_stream_chunk` 双向转换,协议差异全在 Lua 层。
|
||||
- **签名 / 请求头钩子**:适配器可定义 `build_headers(meta)`,在 Go 发 HTTP 前
|
||||
@ -150,6 +152,47 @@ sources:
|
||||
timeout: 120s # 请求超时,默认 120s
|
||||
```
|
||||
|
||||
|
||||
### 自动探测模型列表
|
||||
|
||||
不想手抄模型 id 时,只填 URL 和 key,让网关去扫:
|
||||
|
||||
```bash
|
||||
# 只探测,不写任何东西
|
||||
curl -X POST http://127.0.0.1:8080/api/sources/discover \
|
||||
-H "Authorization: Bearer sk-gw-…" -H "Content-Type: application/json" \
|
||||
-d '{"base_url":"https://api.vendor.com/v1","api_key":"sk-…"}'
|
||||
# → {"ok":true,"base_url":"…","endpoint":"","models":["gpt-x",…],
|
||||
# "image_models":["gpt-image-2"],"models_path":"/models","latency_ms":1387,…}
|
||||
|
||||
# 探测后直接落库
|
||||
curl -X POST http://127.0.0.1:8080/api/sources/discover/apply \
|
||||
-H "Authorization: Bearer sk-gw-…" -H "Content-Type: application/json" \
|
||||
-d '{"base_url":"https://api.vendor.com/v1","api_key":"sk-…","name":"vendor"}'
|
||||
```
|
||||
|
||||
WebUI 源编辑框里也有「自动探测模型」按钮:填好 URL 和 key 点一下,列表自动
|
||||
填好,仍需点保存才生效。
|
||||
|
||||
几点值得知道:
|
||||
|
||||
- **URL 形态不用猜。** 厂商对「`/v1` 放 base 还是放 path」没有共识,探测会
|
||||
依次尝试若干候选并返回答对的那个,同时给出归一化后的 `base_url` +
|
||||
`endpoint`(`endpoint` 平时留空,因为 openai adapter 自带 `/chat/completions`)。
|
||||
- **`image_endpoint` 是另一套规则。** 网关的图像默认路径是**绝对**的
|
||||
`/v1/images/generations`,而 chat 默认是相对的。所以 `base_url` 带 `/v1` 时
|
||||
必须显式给 `image_endpoint: /images/generations`,否则图像请求 404。
|
||||
探测会替你填好。
|
||||
- **图像模型只看上游自己声明的 modality**,绝不从模型名猜。`gpt-image-2`
|
||||
这类 id 也出现在同时提供对话服务的目录上,猜错会让 AUTO 链选中一个只支持
|
||||
图像生成的模型,其后每一层都被它拖垮。
|
||||
- **不会逐模型探活。** 34 个模型的目录跑一轮要花钱且耗时数分钟,`/models`
|
||||
是足够便宜的信号;模型是否真能应答留给正常请求去发现。
|
||||
- **对已存在的源,必须显式 `replace_models: true`** 才会覆盖模型列表。重新
|
||||
探测顺手抹掉为 AUTO 链手工调过的 priority,是最难排查的一类数据丢失。
|
||||
- **`api_key` 留空表示沿用已存值**,不会把凭据清空。
|
||||
|
||||
|
||||
`config.yaml`、`runtime.json`、`master.key`、`adapters/` 均不提交版本库(见
|
||||
`.gitignore`),请随备份带走——`master.key` 丢失后运行时密文无法解密。
|
||||
|
||||
@ -407,10 +450,13 @@ Environment=MALLOC_ARENA_MAX=2
|
||||
可选:`GOMEMLIMIT=48MiB` 作软上限,实测再省 ~1 MB;代价是逐近上限时 GC 转激进,
|
||||
温和场景不必开。
|
||||
|
||||
### disable_thinking
|
||||
### thinking 预算
|
||||
|
||||
请求体带 `"disable_thinking": true` 时,网关透传给各适配器;DeepSeek 适配器将其
|
||||
映射为 `extra_body.thinking.type = "disabled"` 关闭推理,其余源按各自协议处理。
|
||||
请求体带 `"thinking": {"enabled": false}` 时关闭推理;`"thinking": {"enabled": true,
|
||||
"budget_tokens": 4096}` 指定预算。内核只定义这个**中立**语义字段,映射到具体协议由
|
||||
适配器负责(Anthropic `thinking.budget_tokens`、Gemini `thinkingConfig.thinkingBudget`、
|
||||
DeepSeek `thinking.type = "disabled"`)。内核不再向适配器泄漏 `disable_thinking` /
|
||||
`extra_body` 这类私有字段。
|
||||
|
||||
### WebUI
|
||||
|
||||
@ -458,6 +504,13 @@ return {
|
||||
-- [可选] 动态请求头/签名钩子
|
||||
-- meta = { url, method, body, api_key, timestamp, source={ name, meta={...} } }
|
||||
build_headers = function(meta) return { ["X-App-Sign"] = sign } end,
|
||||
|
||||
-- [可选] 出向序列化(契约 v2):上游不是 OpenAI 协议时接管写给客户端的字节。
|
||||
-- 不声明则沿用内核的 OpenAI 兜底;声明即完全取代,内核不再混合字段。
|
||||
serialize_response = function(result) return body_str end,
|
||||
serialize_open_preamble = function(result) return bytes end,
|
||||
serialize_chunk = function(delta) return bytes end,
|
||||
serialize_close = function(result) return bytes end,
|
||||
}
|
||||
```
|
||||
|
||||
@ -466,11 +519,12 @@ return {
|
||||
|
||||
### 内置适配器
|
||||
|
||||
`openai` `deepseek` `anthropic` `gemini` `github` `groq` `mistral` `ollama` `kimicode`。
|
||||
`openai` `deepseek` `anthropic` `gemini` `github` `groq` `mistral` `ollama` `kimicode`
|
||||
`qwen38` `sensenova` `trae`。
|
||||
|
||||
`anthropic`/`gemini`/`ollama` 适配器内置多模态转换(`image_url` → 各自上游格式);若
|
||||
源启用 `disable_thinking`,`deepseek` 适配器会把 `extra_body.thinking.type` 置为
|
||||
`disabled`。
|
||||
`anthropic`/`gemini`/`ollama` 适配器内置多模态转换(`image_url` → 各自上游格式);
|
||||
推理开关由中立的 `thinking` 字段表达,`deepseek` 适配器把它映射为
|
||||
`thinking.type = "disabled"`。
|
||||
|
||||
**kimicode** 是展示 `build_headers` 的样例:云端校验调用方 app,需要按
|
||||
`meta.app_secret` 对时间戳+URL+请求体哈希做 HMAC 签名并附 `X-App-Sign` 等头。
|
||||
@ -585,4 +639,4 @@ go test -tags luajit ./...
|
||||
```
|
||||
|
||||
覆盖:配置校验、适配器加载/变换、签名钩子、Gateway 鉴权、SDK 结算、SSE 流式、
|
||||
模型路由、多模态透传与 disable_thinking。
|
||||
模型路由、多模态透传与 thinking 预算。
|
||||
|
||||
@ -487,7 +487,7 @@ Shared helpers: `hmac_sha256_hex(key, data)`, `sha256_hex(data)`,
|
||||
### Built-in adapters
|
||||
|
||||
`openai` `deepseek` `anthropic` `gemini` `github` `groq` `mistral` `ollama`
|
||||
`kimicode`.
|
||||
`kimicode` `qwen38` `sensenova` `trae`.
|
||||
|
||||
`anthropic`/`gemini`/`ollama` include multimodal conversion
|
||||
(`image_url` → their native format); with `disable_thinking` the `deepseek`
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "modelrouter-gui",
|
||||
"version": "1.9.4",
|
||||
"version": "1.9.7",
|
||||
"description": "ModelRouter Desktop — embedded ModelRouter core with tray",
|
||||
"author": "ModelRouter",
|
||||
"main": "main.js",
|
||||
|
||||
64
deploy.sh
64
deploy.sh
@ -31,6 +31,7 @@ set -euo pipefail
|
||||
readonly TARGET_BIN="/usr/local/bin/llmsproxy"
|
||||
readonly TARGET_CONFIG="/etc/llmsproxy/config.yaml"
|
||||
readonly TARGET_ADAPTERS="/etc/llmsproxy/adapters"
|
||||
readonly TARGET_PLUGINS="/etc/llmsproxy/plugins"
|
||||
readonly REPO_DIR="/home/program/llmsproxy"
|
||||
readonly HEALTH_URL="http://127.0.0.1:8081/v1/models"
|
||||
readonly BIN_NAME="llmsproxy"
|
||||
@ -390,6 +391,67 @@ sync_adapters() {
|
||||
log "✓ 适配器已同步到 $TARGET_ADAPTERS/"
|
||||
}
|
||||
|
||||
# 同步内置插件到 plugin_dir。
|
||||
#
|
||||
# 内置插件是**内核发行物的一部分**(go:embed 进二进制,随版本演进),与内置适配器
|
||||
# 同性质。因此这里的纪律与 sync_adapters 完全一致:**覆盖内置同名文件**(带备份),
|
||||
# 保留目录里运行时新增/运维自加的 .lua。
|
||||
#
|
||||
# 早期实现是「只补缺,已存在一律不动」,看着安全,实际造成一个功能性缺口:
|
||||
# 内置插件本身的**修复无法下发**。v2.0.3/v2.0.4/v2.0.5 三个渠道修复就这样
|
||||
# 全部卡在仓库里 —— 二进制换了、/etc/llmsproxy/plugins/channels.lua 还是旧版,
|
||||
# 生产行为毫无变化。内置源码的正确性由仓库保证,部署必须与二进制内容对齐。
|
||||
#
|
||||
# 「只补缺」原本想保护的是「运维改过 / 运行时新增的插件」,但那些都是**非内置
|
||||
# 名字**:内置名一律以发行物为准,其余名字一律不动。两者不是一回事。
|
||||
sync_plugins() {
|
||||
log "同步内置插件"
|
||||
|
||||
SRC_PLUGINS="$REPO_DIR/internal/lua/plugins"
|
||||
[[ -d "$SRC_PLUGINS" ]] || fail "内嵌插件目录不存在: $SRC_PLUGINS"
|
||||
|
||||
mkdir -p "$TARGET_PLUGINS"
|
||||
|
||||
# 备份将被覆盖/新增的内置插件(非内置名字不进入循环,天然不动)
|
||||
BACKUP_DIR="/etc/llmsproxy/plugins.bak.$(date +%Y%m%d%H%M%S)"
|
||||
mkdir -p "$BACKUP_DIR"
|
||||
local added=0 updated=0 same=0
|
||||
for src in "$SRC_PLUGINS"/*.lua; do
|
||||
[[ -f "$src" ]] || continue
|
||||
local name; name="$(basename "$src")"
|
||||
if [[ -f "$TARGET_PLUGINS/$name" ]]; then
|
||||
if cmp -s "$src" "$TARGET_PLUGINS/$name"; then
|
||||
((same++)) || true
|
||||
continue
|
||||
fi
|
||||
cp -f "$TARGET_PLUGINS/$name" "$BACKUP_DIR/$name.bak" 2>/dev/null || true
|
||||
cp -f "$src" "$TARGET_PLUGINS/$name"
|
||||
chmod 0644 "$TARGET_PLUGINS/$name"
|
||||
log " ~ 更新内置插件: $name"
|
||||
((updated++)) || true
|
||||
else
|
||||
cp -f "$src" "$TARGET_PLUGINS/$name"
|
||||
chmod 0644 "$TARGET_PLUGINS/$name"
|
||||
log " + 新增内置插件: $name"
|
||||
((added++)) || true
|
||||
fi
|
||||
done
|
||||
# 没有任何改动时不必留空备份目录
|
||||
if (( updated == 0 && added == 0 )); then
|
||||
rmdir "$BACKUP_DIR" 2>/dev/null || true
|
||||
else
|
||||
log " 旧插件已备份到 $BACKUP_DIR"
|
||||
fi
|
||||
|
||||
# 校验:内嵌插件目录里的每个 .lua 都必须在目标目录存在
|
||||
for src in "$SRC_PLUGINS"/*.lua; do
|
||||
[[ -f "$src" ]] || continue
|
||||
local name; name="$(basename "$src")"
|
||||
[[ -f "$TARGET_PLUGINS/$name" ]] || warn " 缺少插件: $name"
|
||||
done
|
||||
log "✓ 插件已同步到 $TARGET_PLUGINS/(新增 $added,更新 $updated,未变 $same)"
|
||||
}
|
||||
|
||||
# ---------- 重启服务 ----------
|
||||
restart_service() {
|
||||
log "重启服务(中断 <500ms)"
|
||||
@ -495,6 +557,7 @@ print_summary() {
|
||||
log " 二进制: $TARGET_BIN"
|
||||
log " 配置: $TARGET_CONFIG$([[ -n "$NEW_CONFIG_SRC" ]] && echo " (本次已替换)" || echo " (未改动)")"
|
||||
log " 适配器: $TARGET_ADAPTERS"
|
||||
log " 插件: $TARGET_PLUGINS"
|
||||
log "═════════════════════════════════════════════"
|
||||
}
|
||||
|
||||
@ -511,6 +574,7 @@ main() {
|
||||
verify_master_key
|
||||
build
|
||||
sync_adapters
|
||||
sync_plugins
|
||||
backup_old_state
|
||||
atomic_replace_binary
|
||||
atomic_replace_config
|
||||
|
||||
117
docs/design-plugin-kernel-api.md
Normal file
117
docs/design-plugin-kernel-api.md
Normal file
@ -0,0 +1,117 @@
|
||||
# 插件可调用内核动作(R4-c)设计
|
||||
|
||||
> 用户决策(2026-10-11):**全开**(13 个内核动作全部可调)+ **设置页可配权限** +
|
||||
> **Go 侧 deadline 兜底**。
|
||||
|
||||
## 1. 要解决的问题
|
||||
|
||||
用户原话:"现在这个接口不全,可以多给一些接口让 lua 调用内核。"
|
||||
|
||||
现状(`__llmsproxy_adapters`)是我手写的**三个闭包**(install/update/remove),
|
||||
只能管适配器。内核还有 13 个公开动作(Reload / ResetHealth / AddSource /
|
||||
RemoveSource / SaveAutoRules / key 增删改 / 列表读取…),插件一个都调不到。
|
||||
|
||||
而"自动孵化 / 自愈 / 联动"这类插件能力(例如:探测到上游全挂 → 触发重探 +
|
||||
清冷却;发现额度变化 → 重排 AUTO 链)恰恰需要它们。
|
||||
|
||||
## 2. 关键认识:Go 调用可以超时,Lua 死循环不能
|
||||
|
||||
这决定了整个设计,也是用户提"阻塞 + Go 兜底"的原因:
|
||||
|
||||
| 形态 | 能否中断 | 处置方式 |
|
||||
|---|---|---|
|
||||
| Lua 里 `while true do end` | **不能**(LuaJIT 限制,已实测三次) | 只能停用插件 |
|
||||
| Lua 里 `__llmsproxy_kernel.call("reload")` | **能** | Go 侧 `select` + deadline,超时把错误返回给 Lua |
|
||||
|
||||
因为 Lua 只是在等 Go 的返回值,**不占用 Lua 栈的执行**。所以给每次内核调用加
|
||||
Go 侧 deadline 是可行的、干净的 —— 不需要"Lua 投递请求、Go 队列执行":那样
|
||||
还要定义"超时后队列里积压的命令怎么办",而 deadline 直接返回 `(nil, err)`,
|
||||
语义闭合。
|
||||
|
||||
## 3. 依赖方向
|
||||
|
||||
`internal/lua` **不能** import `internal/core`(方向是 core → lua)。
|
||||
按既有先例(`SetOutboundHosts`)注入一个**接口**,由 core 实现:
|
||||
|
||||
```go
|
||||
// internal/lua/kernel_api.go
|
||||
type KernelAction struct {
|
||||
Name string // "reload" / "reset_health" / ...
|
||||
Title string // 设置页展示用
|
||||
Description string
|
||||
Danger string // ""=只读, "write"=改配置, "danger"=破坏性
|
||||
Params []string // 参数名(设置页/文档用)
|
||||
}
|
||||
|
||||
type KernelAPI interface {
|
||||
// Actions 列出可调用动作(设置页据此渲染权限开关)
|
||||
Actions() []KernelAction
|
||||
// Invoke 执行一个动作,args 为已解码参数。**必须**尊重 ctx 的 deadline。
|
||||
Invoke(ctx context.Context, name string, args map[string]any) (any, error)
|
||||
}
|
||||
```
|
||||
|
||||
core 侧实现 `Invoke` 时用**现有的 Core 方法**,本身不持 `p.mu`,因此不会与
|
||||
插件锁成环(已核实:所有 `ps.mu` → `p.mu` 都是先释放 `ps.mu`)。
|
||||
|
||||
## 4. 权限模型
|
||||
|
||||
复用 `visibilityOverrides` 的**同一套持久化机制**(`.access.json`),
|
||||
但用独立的键空间:`kernel:<plugin>` → 逗号分隔的动作名或 `*`。
|
||||
|
||||
- **默认拒绝**:插件不显式获得授权时,`call` 返回 `(nil, "not permitted")`。
|
||||
理由:全开是"能力存在",不是"默认授予"。一个插件能删密钥这件事必须由
|
||||
运维在设置页点过确认。
|
||||
- **`danger` 类必须显式逐个授权**,不接受 `*`。这一条是防止运维图省事写
|
||||
通配符,把"删密钥"和"读状态"等同起来。
|
||||
- 插件可以在自己源码里**声明**它想要的动作(`plugin.kernel = {...}`),
|
||||
设置页把它显示为"申请",运维批准后才生效。声明不等于授权。
|
||||
|
||||
## 5. 动作清单(13 个 + 适配器 3 个)
|
||||
|
||||
| 动作 | 危险级 | 对应 Core 方法 | 超时 |
|
||||
|---|---|---|---|
|
||||
| `status` | 只读 | 读 AutoSlotStates/Registry | 2s |
|
||||
| `list_sources` | 只读 | Sources() | 2s |
|
||||
| `list_models` | 只读 | Registry().ModelList() | 2s |
|
||||
| `list_adapters` | 只读 | ListAdapters() | 2s |
|
||||
| `adapter_owner` | 只读 | AdapterOwnerOf | 2s |
|
||||
| `reload` | write | Reload() | 30s |
|
||||
| `reset_health` | write | ResetHealth() | 5s |
|
||||
| `probe_all` | write | Registry().ProbeAll(ctx) | 60s |
|
||||
| `save_auto_rules` | write | SaveAutoRules() | 10s |
|
||||
| `save_key_auto` | write | SaveKeyAuto() | 10s |
|
||||
| `add_source` | write | AddSource() | 30s |
|
||||
| `remove_source` | danger | RemoveSource() | 30s |
|
||||
| `create_key` | write | CreateKey() | 10s |
|
||||
| `update_key` | write | UpdateKey() | 10s |
|
||||
| `delete_key` | danger | DeleteKey() | 10s |
|
||||
| `install_adapter` | write | UploadAdapter() | 10s |
|
||||
| `remove_adapter` | danger | RemoveAdapter() | 30s |
|
||||
| `set_plugin_enabled` | danger | SetEnabled() | 5s |
|
||||
|
||||
**绝不开放**:`ListKeys()` / `FindKey()` —— 返回**明文密钥**。给插件读密钥等于
|
||||
把网关的凭据面交给第三方 Lua。要用 key 相关动作就只用带掩码的形态。
|
||||
|
||||
## 6. Lua 调用面
|
||||
|
||||
```lua
|
||||
-- 探测:拿到可用动作与自己的授权情况
|
||||
local acts = __llmsproxy_kernel.actions()
|
||||
-- 调用:统一入口,返回 (result, err)
|
||||
local ok, err = __llmsproxy_kernel.call("reset_health", {})
|
||||
local st, err = __llmsproxy_kernel.call("status", {})
|
||||
```
|
||||
|
||||
统一入口(而不是每个动作一个全局函数)的理由:动作集合将来会变,而
|
||||
`__llmsproxy_kernel` 的签名不变 —— 否则每加一个动作就要动
|
||||
`bootPluginState`,而那里正是历史上出过 cgo 崩溃的地方。
|
||||
|
||||
## 7. 实施顺序
|
||||
|
||||
1. `internal/lua/kernel_api.go`:接口 + 动作元数据 + 权限检查 + deadline 包装
|
||||
2. `internal/lua/plugin_kernel_bridge.go`:Lua 绑定(`actions` / `call`)
|
||||
3. `internal/core/kernel_actions.go`:实现 `KernelAPI`(core 侧)
|
||||
4. 权限持久化:复用 `.access.json`,新键空间
|
||||
5. 设置页:插件行加"内核动作"列 + 授权编辑
|
||||
6. 测试:Go 侧全动作、Lua 侧调用、权限拒绝、deadline 生效、危险动作需显式授权
|
||||
@ -23,7 +23,7 @@
|
||||
5. [内置辅助函数](#5-内置辅助函数)
|
||||
6. [meta 约定](#6-meta-约定)
|
||||
7. [一个完整的最小适配器](#7-一个完整的最小适配器)
|
||||
8. [多模态与 disable_thinking](#8-多模态与-disable_thinking)
|
||||
8. [多模态与 thinking 预算](#8-多模态与-thinking-预算)
|
||||
|
||||
---
|
||||
|
||||
@ -73,8 +73,9 @@ end
|
||||
|
||||
约定:
|
||||
- 返回的字符串将被原样 POST 到 `base_url + (source.endpoint or adapter.endpoint)`。
|
||||
- 请求体中包含的 `disable_thinking`、`extra_body` 等网关内部字段,转换时应自行清理
|
||||
(`openai.lua` 即删掉这两个字段)。
|
||||
- 契约 v2 起,内核**不再向适配器泄漏任何私有字段**:请求体里不会出现
|
||||
`disable_thinking` / `extra_body` 这类键。推理开关与预算用中立的 `thinking`
|
||||
字段表达(见下),适配器按目标协议各自映射。
|
||||
|
||||
### `transform_response(raw_body) -> string`
|
||||
|
||||
@ -176,6 +177,81 @@ end
|
||||
|
||||
返回非 table 会报错;返回 `{}` 表示无自定义头(不会回退静态头)。
|
||||
|
||||
### 出向序列化钩子(契约 v2)
|
||||
|
||||
默认情况下,网关把适配器产出的**统一格式**按 OpenAI 线格式序列化给下游(这是内核
|
||||
保留的兑底,R5)。当上游协议不是 OpenAI(例如 Anthropic Messages API)时,适配器
|
||||
可以接管出向序列化,自己决定写给客户端的字节。
|
||||
|
||||
五个钩子都是可选的,**全不声明就沿用 OpenAI 兑底**。一旦声明某个钩子,它就完全
|
||||
接管那一段输出——内核不会再往里混合 OpenAI 字段(不是“叠加”,是“取代”)。
|
||||
|
||||
| 钩子 | 何时被调用 | 返回值 |
|
||||
|------|-----------|--------|
|
||||
| `serialize_response(payload)` | 非流式响应 | 写给客户端的响应体字符串 |
|
||||
| `serialize_open_preamble(payload)` | 流式开始(发第一个 chunk 前) | 要发的字节(可为 `""`) |
|
||||
| `serialize_chunk(delta)` | 流式每个增量 | 要发的字节(`""` 表示跳过) |
|
||||
| `serialize_close(payload)` | 流式结束 | 收尾字节(如 SSE `message_stop`) |
|
||||
| `serialize_done_marker(payload)` | 流式结束处的 `[DONE]` 标记 | 要写的字节;不声明则用 OpenAI 的 `data: [DONE]` |
|
||||
|
||||
`payload` 是统一格式的结果表(见 `internal/types/maxwire.go` 的 `UnifiedResult` /
|
||||
`UnifiedDelta`),字段名中立,不依赖具体上游协议。
|
||||
|
||||
```lua
|
||||
-- anthropic.lua 的简化形式:把统一结果写成 Anthropic Messages 形状
|
||||
function adapter.serialize_response(payload)
|
||||
return json.encode({
|
||||
id = payload.id,
|
||||
type = "message",
|
||||
role = "assistant",
|
||||
content = { { type = "text", text = payload.content or "" } },
|
||||
stop_reason = payload.finish_reason == "stop" and "end_turn" or nil,
|
||||
usage = { input_tokens = payload.prompt_tokens, output_tokens = payload.completion_tokens },
|
||||
})
|
||||
end
|
||||
```
|
||||
|
||||
注意:钩子返回的是**完整要发送的字节**(包括必要时的 `event:` / `data:` 前缀与
|
||||
空行),因为只有适配器知道目标协议的 SSE 细节。
|
||||
|
||||
### 入向让出钩子(契约 v2,`serialize_*` 的对偶)
|
||||
|
||||
出向是「统一格式 → 客户端协议」,入向是它的镜像:「客户端协议 → 统一格式」。
|
||||
内核为对外端点(如 `POST /v1/messages`)**保留一份内置解析作为兜底(R5)**,
|
||||
但适配器可以**声明自己服务该端点**并接管解析——那时内核主动让出,客户端协议的
|
||||
迭代就完全落在适配器里,不再需要动内核。
|
||||
|
||||
声明分两步:
|
||||
|
||||
1. `adapter.ingress = "/v1/messages"` —— 声明**认领**哪条对外入站路径。
|
||||
同一条路径被多个适配器认领是配置错误;内核按适配器名字典序确定性选择。
|
||||
2. `deserialize_request(raw_body) -> unified_json` —— 把客户端原始 body 解析成
|
||||
统一请求(`UnifiedRequest` 的 JSON),后续调度与 `/v1/chat/completions` 完全一致。
|
||||
|
||||
规则:
|
||||
|
||||
- 只有声明了该端点且实现了 `deserialize_request` 的适配器会让内核让出。
|
||||
- 没有适配器认领时,内核内置解析接管(未配置的部署仍能工作)。
|
||||
- 认领了但钩子**执行失败**是硬错误(400),不会静默回落到另一种协议解析——
|
||||
客户端说的正是这个适配器声称支持的协议,悄悄换解析器只会错误调度请求。
|
||||
|
||||
```lua
|
||||
-- anthropic.lua 的简化形式:把 Anthropic Messages 请求解析成统一格式
|
||||
adapter.ingress = "/v1/messages"
|
||||
function adapter.deserialize_request(raw_body)
|
||||
local req = json.decode(raw_body)
|
||||
local msgs = {}
|
||||
for _, m in ipairs(req.messages or {}) do
|
||||
table.insert(msgs, { role = m.role, content = m.content })
|
||||
end
|
||||
return json.encode({
|
||||
model = req.model, messages = msgs, max_tokens = req.max_tokens,
|
||||
system = type(req.system) == "string" and req.system or "",
|
||||
stream = req.stream == true,
|
||||
})
|
||||
end
|
||||
```
|
||||
|
||||
## 5. 内置辅助函数
|
||||
|
||||
所有适配器共享的全局函数(由 Go 注入):
|
||||
@ -219,8 +295,13 @@ adapter.headers = { ["X-Tenant"] = "prod" }
|
||||
function adapter.transform_request(raw_body)
|
||||
local ok, req = pcall(json.decode, raw_body)
|
||||
if not ok then return raw_body end
|
||||
req.disable_thinking = nil
|
||||
req.extra_body = nil
|
||||
-- 契约 v2:内核不再向适配器泄漏私有字段,`thinking` 是中立语义字段。
|
||||
-- 这里把中立的 thinking 映射成目标协议需要的形状(示例:OpenAI 家族用
|
||||
-- reasoning_effort)。不关心推理的源可以直接忽略它。
|
||||
if req.thinking ~= nil then
|
||||
if req.thinking.enabled == false then req.reasoning_effort = "none" end
|
||||
req.thinking = nil
|
||||
end
|
||||
return json.encode(req)
|
||||
end
|
||||
|
||||
@ -268,11 +349,21 @@ sources:
|
||||
kind: chat
|
||||
```
|
||||
|
||||
## 8. 多模态与 disable_thinking
|
||||
## 8. 多模态与 thinking 预算
|
||||
|
||||
- **多模态**:请求的 `content` 若是数组(`[{type:"text"...},{type:"image_url"...}]`),Go 层
|
||||
原样透传给适配器。协议不支持的源(Anthropic/Gemini/Ollama)需在
|
||||
`transform_request` 内转换;内置 `anthropic.lua`/`gemini.lua`/`ollama.lua` 已实现。
|
||||
- **disable_thinking**:网关将请求体中的 `disable_thinking:true` 透传进
|
||||
`transform_request`。DeepSeek 适配器将其映射为 `extra_body.thinking = { type = "disabled" }`
|
||||
并清理自身字段;其余适配器按各自协议处理(可自行实现或忽略)。
|
||||
- **thinking**:客户端请求里的推理开关/预算被内核归一成中立的 `thinking` 字段:
|
||||
|
||||
```json
|
||||
{"thinking": {"enabled": true, "budget_tokens": 4096}}
|
||||
```
|
||||
|
||||
`enabled=false` 表示关闭推理,`budget_tokens` 可选。适配器自行映射到目标协议
|
||||
(内置实现:Anthropic → `thinking.budget_tokens`、Gemini →
|
||||
`generationConfig.thinkingConfig.thinkingBudget`、DeepSeek →
|
||||
`thinking.type = "disabled"`)。不支持的源直接忽略该字段。
|
||||
|
||||
契约 v1 的内核私有字段 `disable_thinking` 已被取代——它曾是内核细节泄漏进
|
||||
适配器请求体的典型例子(迫使 10 个适配器各写一行 `req.disable_thinking = nil`)。
|
||||
|
||||
141
docs/plugins.md
141
docs/plugins.md
@ -301,20 +301,143 @@ PUT /api/plugins/{name}/state 替换状态(admin)
|
||||
`hook_errors` 是**排错入口**:一个坏掉的插件表现为"功能缺失"而不是报错,
|
||||
这个计数让它可见。
|
||||
|
||||
### 5.4 定时任务(`PLUGIN.schedule`)—— 浏览器关着也会跑
|
||||
|
||||
要求插件做“随时间发生”的事(自动签到、token 轮换、额度轮询)时,声明一个
|
||||
调度表:
|
||||
|
||||
```lua
|
||||
plugin.schedule = {
|
||||
{ fn = "refresh_token", every = 3600 }, -- 每小时(秒)
|
||||
{ fn = "daily_checkin", every = 86400, at = "03:30" }, -- 每天固定时刻(本地时间)
|
||||
{ fn = "poll_quota", every = 300, jitter = 30 }, -- 首次触发加 0-30s 抖动
|
||||
}
|
||||
function plugin.refresh_token()
|
||||
-- 注意:直接写 plugin.state 就是对的 ——
|
||||
-- 任务和钩子跑在同一个 VM 上
|
||||
plugin.state.token = fetch_new_token()
|
||||
end
|
||||
```
|
||||
|
||||
| 字段 | 含义 |
|
||||
|---|---|
|
||||
| `fn` | 要调用的插件函数名(必须存在) |
|
||||
| `every` | 间隔秒数。与 `at` 二选一 |
|
||||
| `at` | 每天本地时间 `"HH:MM"`;设了它就忽略 `every` |
|
||||
| `jitter` | 首次触发额外随机延迟秒数(避免多实例整点齐射) |
|
||||
| `timeout` | 解析保留但目前**不生效**(见下) |
|
||||
|
||||
要点:
|
||||
|
||||
- **内核 ticker 驱动**,不是前端轮询:没有浏览器、没人登录也会执行。
|
||||
- **任务跑在插件自己的 VM 上**,与请求钩子共用 `plugin.state` —— 任务写的
|
||||
token 对下一个请求立即可见,也会经同一套机制持久化到 `.<name>.state.json`。
|
||||
- 任务函数出错只记 `hook_errors`,不影响钩子与转发。
|
||||
- 没有 `schedule` 的插件不获得任何额外 VM/goroutine(零开销)。
|
||||
- **超时不能中断一个运行中的 Lua 调用**(本 LuaJIT 构建的硬限制)。任务超过
|
||||
10 分钟不返回,该插件被**停用**(`disarmed`):不再调度,钩子快速失败并
|
||||
记 `hook_errors`。这是为了不让一个卡死的任务把请求永久堵在它那把锁后面。
|
||||
修好插件后重新加载(重传/重启)即可恢复。
|
||||
|
||||
> 同一条 6.1 的约束:任务占着插件锁,所以**任务里不要写死循环**。
|
||||
|
||||
### 5.5 自定义对外挂载点
|
||||
|
||||
插件的 HTTP 端点默认挂在 `/api/plugins/http/...` 下。同一个声明**同时**可在两个
|
||||
公开命名空间下访问,只要把 `path` 的第一段起成插件名:
|
||||
|
||||
```lua
|
||||
plugin.http = {
|
||||
{ path = "myplugin/callback", method = "POST", handler = "on_callback" },
|
||||
}
|
||||
```
|
||||
|
||||
该端点因此可在三个地方访问:
|
||||
|
||||
| 路径 | 用途 |
|
||||
|---|---|
|
||||
| `/api/plugins/http/myplugin/callback` | 默认前缀(带登录态的管理 UI) |
|
||||
| `/plugins/myplugin/callback` | 公开(浏览器路径) |
|
||||
| `/v1/plugins/myplugin/callback` | 公开(API 路径) |
|
||||
|
||||
可占用的命名空间只有后两个:
|
||||
|
||||
| 前缀 | 例子 |
|
||||
|---|---|
|
||||
| `/plugins/` | `/plugins/myplugin/callback` |
|
||||
| `/v1/plugins/` | `/v1/plugins/myplugin/callback` |
|
||||
|
||||
限制(都是为了不把内核路由变成可被插件改写的):
|
||||
|
||||
- **不能影子内核路由**:与 `/v1/chat/completions`、`/v1/messages`、`/api/...`
|
||||
等重叠的挂载在加载时就被拒绝,错误信息点名冲突对象。
|
||||
- **只能是上面两个命名空间**,不接受任意前缀(否则可以拦截推理请求)。
|
||||
- 鉴权沿用端点的 `roles` / `keys` / `public` 语义(见 §5.1);
|
||||
`/v1/plugins/...` 属于 API 路径,未认证返回 401;`/plugins/...` 为浏览器
|
||||
路径,未认证会跳登录页。
|
||||
- 未声明的路径不会因为落在允许命名空间而被放行——不在 handler 表里就是 404。
|
||||
|
||||
### 5.6 在运行时安装/更新适配器(token 轮换)
|
||||
|
||||
插件可以在运行时安装、改写**自己的**适配器 —— 这是"带自动轮换的认证"的实现
|
||||
方式:拿着新 token 重写适配器源码,后续请求自然使用新逻辑(内核重建该适配器
|
||||
的池)。**可直接在插件 `.lua` 里调用**:
|
||||
|
||||
```lua
|
||||
-- 每小时的定时任务:轮换 token 并重写适配器
|
||||
function plugin.refresh_token()
|
||||
local tok = fetch_new_token() -- 插件自己的取 token 逻辑
|
||||
local name, err = __llmsproxy_adapters.install(
|
||||
"myauth",
|
||||
'return { name = "myauth", endpoint = "/chat/completions",' ..
|
||||
' headers = { ["Authorization"] = "Bearer ' .. tok .. '" } }'
|
||||
)
|
||||
if err then log("error", err) end
|
||||
end
|
||||
```
|
||||
|
||||
| 调用 | 参数 | 返回 |
|
||||
|---|---|---|
|
||||
| `__llmsproxy_adapters.install(name, source)` | 适配器名 + Lua 源码 | `(生效名, nil)` 或 `(nil, err)` |
|
||||
| `__llmsproxy_adapters.update(name, source)` | 同上(只限自己装的) | `(true, nil)` 或 `(nil, err)` |
|
||||
| `__llmsproxy_adapters.remove(name)` | 适配器名(只限自己装的) | `(true, nil)` 或 `(nil, err)` |
|
||||
|
||||
出错时不抛异常,而是返回 `(nil, err)`——与钩子/HTTP 处理器同一套约定,
|
||||
一个手滑的插件错误不会带走网关。
|
||||
|
||||
安全规则(由 Go 侧强制,不是约定):
|
||||
|
||||
- 插件**只能改自己装的**适配器(归属表在 Go 侧记录,调用身份是插件启动时的
|
||||
固定名字,无法冒充)。
|
||||
- 插件**不能影子内置适配器**(否则等于篡改发行物);运维仍可自行覆盖。
|
||||
- 名字经过路径安全校验(无穿越/绝对路径/空白)。
|
||||
|
||||
> 这条能力配合 §5.4 的定时任务,就是完整的自动轮换:**没有浏览器、没有人
|
||||
> 操作时,插件按时间自己把 token 换掉**。
|
||||
|
||||
---
|
||||
|
||||
## 6. 运行时约束(重要)
|
||||
|
||||
### 6.1 一个插件 = 一个 Lua 状态
|
||||
### 6.1 一个插件 = 一个 Lua 状态(也就是一个 VM)
|
||||
|
||||
适配器可以有多个独立的 worker 状态(它们无状态,这是对的)。**插件不行**:
|
||||
钩子通常往 `plugin.state` 里累加,多个状态就意味着总额被劈成几份,而写价格只
|
||||
写进其中一个状态,钩子恰好跑到另一个时**所有请求按 0 计费**。
|
||||
|
||||
因此插件持有**唯一一个**状态,所有入口(`Fire` / `State` / `SetState`)用互斥锁
|
||||
串行化。代价:**一个卡住的钩子会卡住所有插件的钩子**。所以——
|
||||
因此插件持有**唯一一个**状态,所有入口(`Fire` / `State` / `SetState` / **定时任务**)
|
||||
都用这同一把互斥锁串行化。
|
||||
|
||||
> **钩子必须短、同步、不阻塞。** 不要在里面做 HTTP 请求、睡眠或重计算。
|
||||
⚠️ 这条约束对**定时任务尤其关键**:任务和钩子必须是同一个 VM,否则任务写的
|
||||
`plugin.state` 是一个请求路径永远读不到的副本 —— token 轮换看起来成功了,
|
||||
对实际流量完全无效。
|
||||
|
||||
代价:**一个卡住的钩子会卡住该插件自己的钩子**(跨插件仍然并行,每插件各自
|
||||
一把锁、各自一个 VM)。所以——
|
||||
|
||||
> **钩子与定时任务都必须短、同步、不阻塞。** 不要在钩子里做 HTTP 请求、睡眠
|
||||
> 或重计算;长耗时的周期工作应放进 `PLUGIN.schedule`(见 §5.4),它在自己的
|
||||
> 调度 goroutine 上跑,但仍然持有这把锁,因此不能是死循环。
|
||||
|
||||
### 6.2 异常被隔离
|
||||
|
||||
@ -497,10 +620,12 @@ journalctl -u llmsproxy -f | grep -i plugin
|
||||
| | 适配器 | 插件 |
|
||||
|---|---|---|
|
||||
| 位置 | `adapter_dir` | `plugin_dir` |
|
||||
| 作用 | 上游协议转换 | 请求流水线 + 界面 |
|
||||
| 钩子 | `transform_request` / `transform_response` / `transform_stream_chunk` / `build_headers` / `transform_error` | `request_start` / `routed` / `request_end` |
|
||||
| 状态 | 每个源独立,多 worker | 单状态,见 §6.1 |
|
||||
| 出错后果 | 该 (源,模型) 进入冷却退避 | 仅该功能缺失 |
|
||||
| 作用 | 上游协议转换 | 请求流水线 + 界面 + 周期任务 |
|
||||
| 钩子 | `transform_request` / `transform_response` / `transform_stream_chunk` / `build_headers` / `transform_error` / `serialize_*` | `request_start` / `routed` / `request_end` |
|
||||
| 周期任务 | **无**(无状态请求级) | `PLUGIN.schedule`(内核 ticker 驱动) |
|
||||
| 对外端点 | 不开放(见 §5.5) | `/api/plugins/http/...` + 自定义挂载 |
|
||||
| 状态 | 每个源独立,多 worker | **单 VM、单状态**,见 §6.1 |
|
||||
| 出错后果 | 该 (源,模型) 进入冷却退避 | 仅该功能缺失;卡死则停用该插件 |
|
||||
| 鉴权 | 需要 `gateway_keys` 之外的独立凭据 | 网关 key |
|
||||
| 必需性 | 核心 | 可选,缺了网关照常跑 |
|
||||
|
||||
|
||||
286
e2e/adapter_takeover_test.go
Normal file
286
e2e/adapter_takeover_test.go
Normal file
@ -0,0 +1,286 @@
|
||||
package e2e
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 本文件端到端验证 R2/R5 的核心主张:**对外服务的 wire 格式由适配器决定,
|
||||
// 内核只调度**;适配器不声明时内核的 OpenAI 兜底仍然可用。
|
||||
//
|
||||
// 与其他测试的分工:
|
||||
// - internal/gateway/messages_test.go 在**进程内**验证同一件事(起 core+gateway,
|
||||
// 不打真二进制)。它跑得快,但绕过了一些只在真实进程里发生的事:Lua 适配器的
|
||||
// 磁盘 seed、配置加载、systemd 式启动路径、真 HTTP 栈。
|
||||
// - 本文件打**真编译出的二进制**,用一个只说 Anthropic Messages 的假上游,
|
||||
// 证明整条链路(真进程 → 真 Lua 适配器 → 真上游 → 适配器序列化 → 客户端)
|
||||
// 确实成立,而不是只在进程内装配下成立。
|
||||
//
|
||||
// 这是用户明确问到的"适配器接手对外服务的部分"的验收点。
|
||||
|
||||
// anthropicMockUpstream 是只懂 Anthropic Messages 协议的假上游。
|
||||
//
|
||||
// 它会对**不符形状的请求主动报错**,而不是宽容接受 —— 宽容的 mock 会让
|
||||
// "内核把 OpenAI 形状发给了 Anthropic 上游"这种错误测试通过。
|
||||
func anthropicMockUpstream(t *testing.T) *httptest.Server {
|
||||
t.Helper()
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// Anthropic 的请求路径必须是 <base>/messages
|
||||
if !strings.HasSuffix(r.URL.Path, "/messages") {
|
||||
t.Errorf("请求打到了非 Anthropic 路径: %s", r.URL.Path)
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
body, _ := io.ReadAll(r.Body)
|
||||
var probe map[string]interface{}
|
||||
if err := json.Unmarshal(body, &probe); err != nil {
|
||||
t.Errorf("上游收到非 JSON 请求体: %s", body)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
// Anthropic 形状的硬性要求:messages 是数组;不能有 OpenAI 的 choices 概念
|
||||
if _, ok := probe["messages"]; !ok {
|
||||
t.Errorf("上游请求体缺 messages: %s", body)
|
||||
}
|
||||
if _, ok := probe["max_tokens"]; !ok {
|
||||
t.Errorf("Anthropic 要求 max_tokens,请求体缺此项: %s", body)
|
||||
}
|
||||
// 反向断言:OpenAI 兜底字段不该出现在 Anthropic 上游请求里
|
||||
for _, k := range []string{"choices", "chat_completions"} {
|
||||
if _, bad := probe[k]; bad {
|
||||
t.Errorf("Anthropic 上游请求体混入了 OpenAI 字段 %q: %s", k, body)
|
||||
}
|
||||
}
|
||||
|
||||
if stream, _ := probe["stream"].(bool); stream {
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
fl, _ := w.(http.Flusher)
|
||||
frames := []string{
|
||||
`event: message_start` + "\n" + `data: {"type":"message_start","message":{"id":"msg_e2e","type":"message","role":"assistant","model":"claude-e2e","content":[],"stop_reason":null,"usage":{"input_tokens":3,"output_tokens":0}}}`,
|
||||
`event: content_block_start` + "\n" + `data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}`,
|
||||
`event: content_block_delta` + "\n" + `data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"pong"}}`,
|
||||
`event: content_block_stop` + "\n" + `data: {"type":"content_block_stop","index":0}`,
|
||||
`event: message_delta` + "\n" + `data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":1}}`,
|
||||
`event: message_stop` + "\n" + `data: {"type":"message_stop"}`,
|
||||
}
|
||||
for _, f := range frames {
|
||||
fmt.Fprint(w, f+"\n\n")
|
||||
if fl != nil {
|
||||
fl.Flush()
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
fmt.Fprint(w, `{"id":"msg_e2e","type":"message","role":"assistant","model":"claude-e2e",`+
|
||||
`"content":[{"type":"text","text":"pong"}],"stop_reason":"end_turn",`+
|
||||
`"usage":{"input_tokens":3,"output_tokens":1}}`)
|
||||
}))
|
||||
t.Cleanup(srv.Close)
|
||||
return srv
|
||||
}
|
||||
|
||||
// writeAnthropicConfig 写一份把源挂到 anthropic 适配器上的配置。
|
||||
func writeAnthropicConfig(t *testing.T, dir, listenAddr, upstreamURL string) string {
|
||||
t.Helper()
|
||||
cfg := "listen: " + listenAddr + "\n" +
|
||||
"gateway_keys:\n - sk-e2e-0001\n" +
|
||||
"default_model: claude-e2e\n" +
|
||||
"adapter_dir: " + filepath.Join(dir, "adapters") + "\n" +
|
||||
"runtime_file: " + filepath.Join(dir, "runtime.json") + "\n" +
|
||||
"sources:\n" +
|
||||
" - name: anth\n" +
|
||||
" base_url: " + upstreamURL + "\n" +
|
||||
" adapter: anthropic\n" +
|
||||
" api_key: test-key\n" +
|
||||
" max_concurrent: 32\n" +
|
||||
" models:\n" +
|
||||
" - id: claude-e2e\n" +
|
||||
" kind: chat\n" +
|
||||
" priority: 100\n"
|
||||
path := filepath.Join(dir, "config.yaml")
|
||||
if err := os.WriteFile(path, []byte(cfg), 0o644); err != nil {
|
||||
t.Fatalf("write config: %v", err)
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
// TestAdapterTakeoverNonStream:非流式,Anthropic 客户端形状进、出。
|
||||
func TestAdapterTakeoverNonStream(t *testing.T) {
|
||||
up := anthropicMockUpstream(t)
|
||||
dir := t.TempDir()
|
||||
addr := mustAddr(t)
|
||||
cfg := writeAnthropicConfig(t, dir, addr, up.URL)
|
||||
bin := buildBinary(t)
|
||||
g := startGateway(t, bin, addr, cfg)
|
||||
|
||||
resp, body := g.do("POST", "/v1/messages",
|
||||
`{"model":"claude-e2e","max_tokens":64,"messages":[{"role":"user","content":"hi"}]}`, true)
|
||||
if resp == nil || resp.StatusCode != 200 {
|
||||
t.Fatalf("status=%v body=%q", statusOf(resp), body)
|
||||
}
|
||||
// 出向必须是 Anthropic Messages 形状,不能是 OpenAI choices 形状
|
||||
if strings.Contains(body, `"choices"`) {
|
||||
t.Errorf("Anthropic 响应里混入了 OpenAI choices 外壳: %s", body)
|
||||
}
|
||||
var out map[string]interface{}
|
||||
if err := json.Unmarshal([]byte(body), &out); err != nil {
|
||||
t.Fatalf("响应不是 JSON: %v: %s", err, body)
|
||||
}
|
||||
if out["type"] != "message" {
|
||||
t.Errorf(`响应 type = %v, 期望 "message"(Anthropic 形状): %s`, out["type"], body)
|
||||
}
|
||||
// 文本内容必须能取出来 —— 这是"内容确实穿过适配器回来了"的证据
|
||||
if !strings.Contains(body, "pong") {
|
||||
t.Errorf("响应里没有上游返回的文本: %s", body)
|
||||
}
|
||||
if _, ok := out["usage"]; !ok {
|
||||
t.Errorf("Anthropic 响应应带 usage: %s", body)
|
||||
}
|
||||
}
|
||||
|
||||
// TestAdapterTakeoverStream:流式也要是 Anthropic SSE 序列,
|
||||
// 且**不得**出现 OpenAI 的 [DONE] 终止标记(R2 的"不混合")。
|
||||
func TestAdapterTakeoverStream(t *testing.T) {
|
||||
up := anthropicMockUpstream(t)
|
||||
dir := t.TempDir()
|
||||
addr := mustAddr(t)
|
||||
cfg := writeAnthropicConfig(t, dir, addr, up.URL)
|
||||
bin := buildBinary(t)
|
||||
g := startGateway(t, bin, addr, cfg)
|
||||
|
||||
resp, body := g.do("POST", "/v1/messages",
|
||||
`{"model":"claude-e2e","max_tokens":64,"stream":true,"messages":[{"role":"user","content":"hi"}]}`, true)
|
||||
if resp == nil || resp.StatusCode != 200 {
|
||||
t.Fatalf("stream status=%v body=%q", statusOf(resp), body)
|
||||
}
|
||||
if !strings.Contains(body, "message_start") {
|
||||
t.Errorf("Anthropic 流应以 message_start 开头: %s", body)
|
||||
}
|
||||
if !strings.Contains(body, "message_stop") {
|
||||
t.Errorf("Anthropic 流应以 message_stop 收尾: %s", body)
|
||||
}
|
||||
if strings.Contains(body, "[DONE]") {
|
||||
t.Errorf("Anthropic 流里出现了 OpenAI 的 [DONE]: %s", body)
|
||||
}
|
||||
if strings.Contains(body, "chat.completion.chunk") {
|
||||
t.Errorf("流里混入 OpenAI chunk 形状: %s", body)
|
||||
}
|
||||
if !strings.Contains(body, "pong") {
|
||||
t.Errorf("流里没有上游文本: %s", body)
|
||||
}
|
||||
}
|
||||
|
||||
// TestAdapterTakeoverStress 是对外服务路径的真实压力测试:
|
||||
// 并发打真二进制,混合流式/非流式,验证无 5xx、上游命中数正确、
|
||||
// 且每个响应都是完整合法的 Anthropic 形状(适配器在并发下不串味)。
|
||||
func TestAdapterTakeoverStress(t *testing.T) {
|
||||
up := anthropicMockUpstream(t)
|
||||
dir := t.TempDir()
|
||||
addr := mustAddr(t)
|
||||
cfg := writeAnthropicConfig(t, dir, addr, up.URL)
|
||||
bin := buildBinary(t)
|
||||
g := startGateway(t, bin, addr, cfg)
|
||||
|
||||
// max_concurrent 配成 32,让 96 个请求基本都能真正流过适配器 —— 这个测试
|
||||
// 要压的是**并发下序列化是否串味**,不是内核的容量拒绝策略。真的撞上上限
|
||||
// 时收到的 429 也是合法背压(见下方断言),不算失败。
|
||||
const (
|
||||
workers = 12
|
||||
perWorker = 8
|
||||
)
|
||||
|
||||
var (
|
||||
wg sync.WaitGroup
|
||||
mu sync.Mutex
|
||||
ok int
|
||||
shed int
|
||||
failed []string
|
||||
streams int
|
||||
)
|
||||
start := make(chan struct{})
|
||||
|
||||
for w := 0; w < workers; w++ {
|
||||
wg.Add(1)
|
||||
go func(w int) {
|
||||
defer wg.Done()
|
||||
<-start
|
||||
for i := 0; i < perWorker; i++ {
|
||||
isStream := (w+i)%2 == 0
|
||||
var payload string
|
||||
if isStream {
|
||||
payload = `{"model":"claude-e2e","max_tokens":32,"stream":true,"messages":[{"role":"user","content":"hi"}]}`
|
||||
} else {
|
||||
payload = `{"model":"claude-e2e","max_tokens":32,"messages":[{"role":"user","content":"hi"}]}`
|
||||
}
|
||||
resp, body := g.do("POST", "/v1/messages", payload, true)
|
||||
mu.Lock()
|
||||
switch {
|
||||
case resp == nil:
|
||||
failed = append(failed, "nil response")
|
||||
// 429 = 内核的容量背压(provider busy)。它是**正确**行为,
|
||||
// 不是缺陷:并发超限时就该如实拒绝,而不是排队或伪造响应。
|
||||
// 只要求它是结构良好的错误,不要求 200。
|
||||
case resp.StatusCode == http.StatusTooManyRequests:
|
||||
if !strings.Contains(body, "busy") && !strings.Contains(body, "error") {
|
||||
failed = append(failed, "429 不是结构化错误: "+body)
|
||||
} else {
|
||||
shed++
|
||||
}
|
||||
// 5xx 才是真失败 —— 那是内核或适配器崩了。
|
||||
case resp.StatusCode >= 500:
|
||||
failed = append(failed, fmt.Sprintf("5xx status=%d body=%.120s", resp.StatusCode, body))
|
||||
case resp.StatusCode != 200:
|
||||
failed = append(failed, fmt.Sprintf("status=%d body=%.120s", resp.StatusCode, body))
|
||||
case !strings.Contains(body, "pong"):
|
||||
// 200 但内容不对:说明并发下适配器把响应串味了
|
||||
failed = append(failed, "missing upstream text: "+body)
|
||||
case isStream && !strings.Contains(body, "message_stop"):
|
||||
failed = append(failed, "stream missing message_stop")
|
||||
case !isStream && strings.Contains(body, `"choices"`):
|
||||
failed = append(failed, "openai shell leaked: "+body)
|
||||
default:
|
||||
ok++
|
||||
if isStream {
|
||||
streams++
|
||||
}
|
||||
}
|
||||
mu.Unlock()
|
||||
}
|
||||
}(w)
|
||||
}
|
||||
close(start)
|
||||
wg.Wait()
|
||||
|
||||
if len(failed) > 0 {
|
||||
t.Fatalf("%d/%d 请求失败,前 5 例:\n%s", len(failed), workers*perWorker,
|
||||
strings.Join(failed[:min(5, len(failed))], "\n"))
|
||||
}
|
||||
// 至少大部分请求必须真正流过 200 —— 全被背压拒掉说明并发上限配得太低,
|
||||
// 那这个压力测试就没压到适配器并发。
|
||||
if ok < workers*perWorker/2 {
|
||||
t.Fatalf("只有 %d/%d 请求真正成功,压力未触及适配器并发路径",
|
||||
ok, workers*perWorker)
|
||||
}
|
||||
t.Logf("成功 %d(其中流式 %d),背压 429 %d —— 无 5xx、无 OpenAI 形状泄漏",
|
||||
ok, streams, shed)
|
||||
}
|
||||
|
||||
func min(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
var _ = time.Second
|
||||
170
e2e/channels_device_e2e_test.go
Normal file
170
e2e/channels_device_e2e_test.go
Normal file
@ -0,0 +1,170 @@
|
||||
package e2e
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TestChannelsDevicePollE2E 用**真二进制**跑通 channels 插件的 device-poll 登录:
|
||||
//
|
||||
// 发起登录(start_login) → 页面轮询(poll_login) 推进设备码换 token
|
||||
// → 凭据入保险库 → add_source 把平台接成上游源 → /v1/models 里出现其模型
|
||||
//
|
||||
// 这是对参考仓库 dsh-our-free-model「登录→领额度→接入源」形态的端到端对齐:
|
||||
// 行为(HTTP 决策分支、会话推进、最终可路由)在真进程里验证,而不是单元桩。
|
||||
//
|
||||
// 假上游模拟 MiniMax 的两个端点:设备码申请 + token 轮询(脚本化逐次响应)。
|
||||
func TestChannelsDevicePollE2E(t *testing.T) {
|
||||
// ---- 假平台上游(loopback,白名单天然放行)----
|
||||
var tokenHits int32
|
||||
var tokenScript atomic.Value // []scriptStep
|
||||
tokenScript.Store([]scriptStep{})
|
||||
plat := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.Path {
|
||||
case "/oauth2/device/code":
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
fmt.Fprint(w, `{"device_code":"DEV-E2E","user_code":"WXYZ-1234","verification_uri":"https://example/activate","interval":1,"expires_in":600}`)
|
||||
case "/oauth2/token":
|
||||
i := int(atomic.AddInt32(&tokenHits, 1)) - 1
|
||||
script := tokenScript.Load().([]scriptStep)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if i >= len(script) {
|
||||
w.WriteHeader(500)
|
||||
fmt.Fprint(w, `{"error":"unexpected"}`)
|
||||
return
|
||||
}
|
||||
if script[i].code != 0 && script[i].code != 200 {
|
||||
w.WriteHeader(script[i].code)
|
||||
}
|
||||
fmt.Fprint(w, script[i].body)
|
||||
case "/v1/models":
|
||||
// 接入源后内核会探测可达;回一个 OpenAI 形状的模型列表
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
fmt.Fprint(w, `{"object":"list","data":[{"id":"MiniMax-M3","object":"model"}]}`)
|
||||
case "/v1/chat/completions":
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
fmt.Fprint(w, `{"choices":[{"message":{"content":"ok"},"finish_reason":"stop"}],"usage":{"prompt_tokens":1,"completion_tokens":1,"total_tokens":2}}`)
|
||||
default:
|
||||
w.WriteHeader(404)
|
||||
}
|
||||
}))
|
||||
defer plat.Close()
|
||||
|
||||
// ---- 真二进制 + 配置(带 plugin_dir,让 bundled channels 落地 + 授权可持久化)----
|
||||
dir := t.TempDir()
|
||||
addr := mustAddr(t)
|
||||
pluginDir := filepath.Join(dir, "plugins") // 不预建:SeedBundled 见空目录才落地内置插件
|
||||
cfg := "listen: " + addr + "\n" +
|
||||
"gateway_keys:\n - sk-e2e-0001\n" +
|
||||
"default_model: none\n" +
|
||||
"adapter_dir: " + filepath.Join(dir, "adapters") + "\n" +
|
||||
"plugin_dir: " + pluginDir + "\n" +
|
||||
"runtime_file: " + filepath.Join(dir, "runtime.json") + "\n" +
|
||||
"sources: []\n"
|
||||
cfgPath := filepath.Join(dir, "config.yaml")
|
||||
if err := os.WriteFile(cfgPath, []byte(cfg), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
bin := buildBinary(t)
|
||||
g := startGatewayNoModels(t, bin, addr, cfgPath)
|
||||
|
||||
call := func(method, path, body string) (int, map[string]interface{}) {
|
||||
resp, raw := g.do(method, path, body, true)
|
||||
var m map[string]interface{}
|
||||
_ = json.Unmarshal([]byte(raw), &m)
|
||||
return statusOf(resp), m
|
||||
}
|
||||
|
||||
// 1) 授权内核动作(运维批准 add_source 等)+ 把 minimax 端点指到假上游。
|
||||
st, m := call("PUT", "/api/plugins/channels/kernel",
|
||||
`{"actions":["add_source","status","list_sources","probe_all","reset_health"],"all_read":false}`)
|
||||
if st != 200 {
|
||||
t.Fatalf("授权内核动作失败: status=%d body=%v", st, m)
|
||||
}
|
||||
st, m = call("POST", "/api/plugins/channels/call/set_endpoints", fmt.Sprintf(
|
||||
`{"platform":"minimax","base_url":%q,"device":{"code_url":%q,"token_url":%q}}`,
|
||||
plat.URL+"/v1", plat.URL+"/oauth2/device/code", plat.URL+"/oauth2/token"))
|
||||
if st != 200 {
|
||||
t.Fatalf("set_endpoints 失败: status=%d body=%v", st, m)
|
||||
}
|
||||
|
||||
// 2) 发起 device-poll 登录:拿到会话 + user_code。
|
||||
st, m = call("POST", "/api/plugins/http/channels/login", `{"platform":"minimax"}`)
|
||||
if st != 200 {
|
||||
t.Fatalf("start_login status=%d body=%v", st, m)
|
||||
}
|
||||
sess, _ := m["session"].(map[string]interface{})
|
||||
if sess == nil || sess["user_code"] != "WXYZ-1234" {
|
||||
t.Fatalf("start_login 未透传 user_code: %v", m)
|
||||
}
|
||||
sid, _ := sess["id"].(string)
|
||||
if sid == "" {
|
||||
t.Fatal("无会话 id")
|
||||
}
|
||||
|
||||
// 3) 第一次轮询:上游 pending → 会话仍 pending。
|
||||
tokenScript.Store([]scriptStep{{code: 400, body: `{"error":"authorization_pending"}`}})
|
||||
atomic.StoreInt32(&tokenHits, 0)
|
||||
st, m = call("GET", "/api/plugins/http/channels/login/status?id="+sid, "")
|
||||
if m["state"] != "pending" {
|
||||
t.Fatalf("轮询1 应 pending, 实得 %v", m)
|
||||
}
|
||||
|
||||
// 4) 第二次轮询:上游发 access_token → 会话 completed,凭据入库 + 接入源。
|
||||
tokenScript.Store([]scriptStep{{code: 200, body: `{"access_token":"MM-E2E-TOKEN","refresh_token":"MM-RF","nickname":"e2e-user"}`}})
|
||||
atomic.StoreInt32(&tokenHits, 0)
|
||||
deadline := time.Now().Add(5 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
st, m = call("GET", "/api/plugins/http/channels/login/status?id="+sid, "")
|
||||
if m["state"] == "completed" || m["state"] == "failed" {
|
||||
break
|
||||
}
|
||||
time.Sleep(150 * time.Millisecond)
|
||||
}
|
||||
if m["state"] != "completed" {
|
||||
t.Fatalf("轮询2 应 completed, 实得 %v", m)
|
||||
}
|
||||
if m["account"] != "e2e-user" {
|
||||
t.Fatalf("account 应取 nickname, 实得 %v", m)
|
||||
}
|
||||
|
||||
// 5) 接入源的终局证据:/v1/models 里出现 channels-minimax 的模型。
|
||||
deadline = time.Now().Add(5 * time.Second)
|
||||
var modelsBody string
|
||||
for time.Now().Before(deadline) {
|
||||
resp, raw := g.do("GET", "/v1/models", "", true)
|
||||
modelsBody = raw
|
||||
if resp != nil && resp.StatusCode == 200 && strings.Contains(raw, "MiniMax-M3") {
|
||||
break
|
||||
}
|
||||
time.Sleep(150 * time.Millisecond)
|
||||
}
|
||||
if !strings.Contains(modelsBody, "MiniMax-M3") {
|
||||
t.Fatalf("接入源后 /v1/models 未出现 MiniMax-M3: %s", modelsBody)
|
||||
}
|
||||
}
|
||||
|
||||
type scriptStep struct {
|
||||
code int
|
||||
body string
|
||||
}
|
||||
|
||||
// startGatewayNoModels 与 startGateway 相同,但就绪探针不要求 /v1/models 返回
|
||||
// 非空:本用例启动时没有任何源,/v1/models 是空列表(200 即就绪)。
|
||||
func startGatewayNoModels(t *testing.T, bin, listen, cfgPath string) *gatewayUnderTest {
|
||||
t.Helper()
|
||||
return startGateway(t, bin, listen, cfgPath)
|
||||
}
|
||||
|
||||
var _ = net.Listen
|
||||
var _ = io.Discard
|
||||
200
e2e/channels_flows_e2e_test.go
Normal file
200
e2e/channels_flows_e2e_test.go
Normal file
@ -0,0 +1,200 @@
|
||||
package e2e
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TestChannelsFlowsE2E 用真二进制补齐另外三条流程的端到端:
|
||||
//
|
||||
// Cline(WorkOS device-poll 两步:authenticate → register 换最终凭据)
|
||||
// paste(opencode:直接 complete_login 贴 API key)
|
||||
// manual(qoder:start_login 返回 manual_hint,再凭据完成)
|
||||
//
|
||||
// 终局证据与 TestChannelsDevicePollE2E 同一标准:/v1/models 出现该平台模型。
|
||||
func TestChannelsFlowsE2E(t *testing.T) {
|
||||
var authHits, regHits int32
|
||||
plat := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
switch r.URL.Path {
|
||||
// ---- Cline(WorkOS)----
|
||||
case "/user_management/authorize/device":
|
||||
fmt.Fprint(w, `{"device_code":"CD-E2E","user_code":"CLNE-1234","verification_uri":"https://example/activate","interval":1,"expires_in":600}`)
|
||||
case "/user_management/authenticate":
|
||||
if atomic.AddInt32(&authHits, 1) == 1 {
|
||||
w.WriteHeader(400)
|
||||
fmt.Fprint(w, `{"error":"authorization_pending"}`)
|
||||
return
|
||||
}
|
||||
fmt.Fprint(w, `{"access_token":"WO-E2E-AT","refresh_token":"WO-E2E-RT"}`)
|
||||
case "/api/v1/auth/register":
|
||||
atomic.AddInt32(®Hits, 1)
|
||||
fmt.Fprint(w, `{"success":true,"data":{"accessToken":"CLINE-E2E-AT","refreshToken":"CLINE-E2E-RT","userInfo":{"email":"dev@example.com"}}}`)
|
||||
// ---- 上游推理面(opencode/qoder/cline 的 base_url 都指到这里)----
|
||||
case "/v1/models":
|
||||
fmt.Fprint(w, `{"object":"list","data":[{"id":"big-pickle","object":"model"},{"id":"qoder-default","object":"model"},{"id":"stealth/space-bunny-alpha","object":"model"}]}`)
|
||||
case "/v1/chat/completions":
|
||||
fmt.Fprint(w, `{"choices":[{"message":{"content":"ok"},"finish_reason":"stop"}],"usage":{"prompt_tokens":1,"completion_tokens":1,"total_tokens":2}}`)
|
||||
default:
|
||||
w.WriteHeader(404)
|
||||
}
|
||||
}))
|
||||
defer plat.Close()
|
||||
|
||||
dir := t.TempDir()
|
||||
addr := mustAddr(t)
|
||||
pluginDir := filepath.Join(dir, "plugins")
|
||||
cfg := "listen: " + addr + "\n" +
|
||||
"gateway_keys:\n - sk-e2e-0001\n" +
|
||||
"default_model: none\n" +
|
||||
"adapter_dir: " + filepath.Join(dir, "adapters") + "\n" +
|
||||
"plugin_dir: " + pluginDir + "\n" +
|
||||
"runtime_file: " + filepath.Join(dir, "runtime.json") + "\n" +
|
||||
"sources: []\n"
|
||||
cfgPath := filepath.Join(dir, "config.yaml")
|
||||
if err := os.WriteFile(cfgPath, []byte(cfg), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
bin := buildBinary(t)
|
||||
g := startGatewayNoModels(t, bin, addr, cfgPath)
|
||||
|
||||
call := func(method, path, body string) (int, map[string]interface{}) {
|
||||
resp, raw := g.do(method, path, body, true)
|
||||
var m map[string]interface{}
|
||||
_ = json.Unmarshal([]byte(raw), &m)
|
||||
return statusOf(resp), m
|
||||
}
|
||||
|
||||
st, m := call("PUT", "/api/plugins/channels/kernel",
|
||||
`{"actions":["add_source","status","list_sources","probe_all","reset_health"],"all_read":false}`)
|
||||
if st != 200 {
|
||||
t.Fatalf("授权内核动作失败: status=%d body=%v", st, m)
|
||||
}
|
||||
|
||||
// ---------- ① Cline:WorkOS 设备码两步 ----------
|
||||
st, m = call("POST", "/api/plugins/channels/call/set_endpoints", fmt.Sprintf(
|
||||
`{"platform":"cline","base_url":%q,"device":{"code_url":%q,"token_url":%q},"register":{"url":%q}}`,
|
||||
plat.URL, plat.URL+"/user_management/authorize/device",
|
||||
plat.URL+"/user_management/authenticate", plat.URL+"/api/v1/auth/register"))
|
||||
if st != 200 {
|
||||
t.Fatalf("set_endpoints(cline) 失败: status=%d body=%v", st, m)
|
||||
}
|
||||
st, m = call("POST", "/api/plugins/http/channels/login", `{"platform":"cline"}`)
|
||||
if st != 200 {
|
||||
t.Fatalf("cline start_login status=%d body=%v", st, m)
|
||||
}
|
||||
sess, _ := m["session"].(map[string]interface{})
|
||||
if sess == nil || sess["user_code"] != "CLNE-1234" {
|
||||
t.Fatalf("cline start_login 未透传 user_code: %v", m)
|
||||
}
|
||||
sid, _ := sess["id"].(string)
|
||||
|
||||
// 第一拍 pending(authorization_pending),第二拍 completed
|
||||
atomic.StoreInt32(&authHits, 0)
|
||||
st, m = call("GET", "/api/plugins/http/channels/login/status?id="+sid, "")
|
||||
if m["state"] != "pending" {
|
||||
t.Fatalf("cline 轮询1 应 pending, 实得 %v", m)
|
||||
}
|
||||
deadline := time.Now().Add(5 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
st, m = call("GET", "/api/plugins/http/channels/login/status?id="+sid, "")
|
||||
if m["state"] == "completed" || m["state"] == "failed" {
|
||||
break
|
||||
}
|
||||
time.Sleep(150 * time.Millisecond)
|
||||
}
|
||||
if m["state"] != "completed" {
|
||||
t.Fatalf("cline 轮询2 应 completed, 实得 %v", m)
|
||||
}
|
||||
if m["account"] != "dev@example.com" {
|
||||
t.Fatalf("cline account 应取 register userInfo.email, 实得 %v", m)
|
||||
}
|
||||
if atomic.LoadInt32(®Hits) != 1 {
|
||||
t.Fatalf("register 应恰好调用一次, 实得 %d", regHits)
|
||||
}
|
||||
// 凭据必须是 register 的最终 token,不是 WorkOS token
|
||||
var modelsBody string
|
||||
deadline = time.Now().Add(5 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
resp, raw := g.do("GET", "/v1/models", "", true)
|
||||
modelsBody = raw
|
||||
if resp != nil && resp.StatusCode == 200 && strings.Contains(raw, "stealth/space-bunny-alpha") {
|
||||
break
|
||||
}
|
||||
time.Sleep(150 * time.Millisecond)
|
||||
}
|
||||
if !strings.Contains(modelsBody, "stealth/space-bunny-alpha") {
|
||||
t.Fatalf("cline 接入后 /v1/models 未出现其模型: %s", modelsBody)
|
||||
}
|
||||
|
||||
// ---------- ② paste(opencode):complete 直接贴 key ----------
|
||||
st, m = call("POST", "/api/plugins/channels/call/set_endpoints", fmt.Sprintf(
|
||||
`{"platform":"opencode","base_url":%q}`, plat.URL))
|
||||
if st != 200 {
|
||||
t.Fatalf("set_endpoints(opencode) 失败: %v", m)
|
||||
}
|
||||
st, m = call("POST", "/api/plugins/http/channels/login", `{"platform":"opencode"}`)
|
||||
if st != 200 {
|
||||
t.Fatalf("opencode start_login status=%d", st)
|
||||
}
|
||||
if sess, _ = m["session"].(map[string]interface{}); sess == nil || sess["flow"] != "paste" {
|
||||
t.Fatalf("opencode 会话 flow 应 paste: %v", m)
|
||||
}
|
||||
st, m = call("POST", "/api/plugins/http/channels/login/complete",
|
||||
`{"platform":"opencode","credential":"OC-E2E-KEY"}`)
|
||||
if st != 200 || m["ok"] != true {
|
||||
t.Fatalf("opencode complete 失败: status=%d body=%v", st, m)
|
||||
}
|
||||
deadline = time.Now().Add(5 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
resp, raw := g.do("GET", "/v1/models", "", true)
|
||||
modelsBody = raw
|
||||
if resp != nil && resp.StatusCode == 200 && strings.Contains(raw, "big-pickle") {
|
||||
break
|
||||
}
|
||||
time.Sleep(150 * time.Millisecond)
|
||||
}
|
||||
if !strings.Contains(modelsBody, "big-pickle") {
|
||||
t.Fatalf("opencode 接入后 /v1/models 未出现 big-pickle: %s", modelsBody)
|
||||
}
|
||||
|
||||
// ---------- ③ manual(qoder):hint 明确 + 凭据完成 ----------
|
||||
st, m = call("POST", "/api/plugins/channels/call/set_endpoints", fmt.Sprintf(
|
||||
`{"platform":"qoder","base_url":%q}`, plat.URL))
|
||||
if st != 200 {
|
||||
t.Fatalf("set_endpoints(qoder) 失败: %v", m)
|
||||
}
|
||||
st, m = call("POST", "/api/plugins/http/channels/login", `{"platform":"qoder"}`)
|
||||
if st != 200 {
|
||||
t.Fatalf("qoder start_login status=%d", st)
|
||||
}
|
||||
hint, _ := m["manual_hint"].(string)
|
||||
if !strings.Contains(hint, "WASM") {
|
||||
t.Fatalf("qoder manual_hint 应说明 WASM 不可自动, 实得 %q", hint)
|
||||
}
|
||||
st, m = call("POST", "/api/plugins/http/channels/login/complete",
|
||||
`{"platform":"qoder","credential":"QD-E2E-KEY"}`)
|
||||
if st != 200 || m["ok"] != true {
|
||||
t.Fatalf("qoder complete 失败: status=%d body=%v", st, m)
|
||||
}
|
||||
deadline = time.Now().Add(5 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
resp, raw := g.do("GET", "/v1/models", "", true)
|
||||
modelsBody = raw
|
||||
if resp != nil && resp.StatusCode == 200 && strings.Contains(raw, "qoder-default") {
|
||||
break
|
||||
}
|
||||
time.Sleep(150 * time.Millisecond)
|
||||
}
|
||||
if !strings.Contains(modelsBody, "qoder-default") {
|
||||
t.Fatalf("qoder 接入后 /v1/models 未出现 qoder-default: %s", modelsBody)
|
||||
}
|
||||
}
|
||||
261
e2e/conflict_arbitration_e2e_test.go
Normal file
261
e2e/conflict_arbitration_e2e_test.go
Normal file
@ -0,0 +1,261 @@
|
||||
package e2e
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// writeE2EPlugin 落一个插件文件到 e2e 的 plugin_dir(SeedBundled 见空目录才
|
||||
// 落地内置插件,所以先放自定义插件再启动网关)。
|
||||
func writeE2EPlugin(t *testing.T, dir, name, code string) {
|
||||
t.Helper()
|
||||
p := filepath.Join(dir, "plugins", name)
|
||||
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(p, []byte(code), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// e2eConfig 返回一份最小 e2e 配置文本(含 plugin_dir)。
|
||||
func e2eConfig(dir, listen string) string {
|
||||
return "listen: " + listen + "\n" +
|
||||
"gateway_keys:\n - sk-e2e-0001\n" +
|
||||
"default_model: AUTO\n" +
|
||||
"adapter_dir: " + filepath.Join(dir, "adapters") + "\n" +
|
||||
"runtime_file: " + filepath.Join(dir, "runtime.json") + "\n" +
|
||||
"plugin_dir: " + filepath.Join(dir, "plugins") + "\n" +
|
||||
"sources: []\n"
|
||||
}
|
||||
|
||||
// TestConflictArbitrationE2E(用户要求:服务路径冲突不崩溃不拒启 + WebAPI 裁决)
|
||||
//
|
||||
// 真二进制全链路:
|
||||
// 1. 两个插件声明同一条 /api/plugins/http/ 路径 → 两插件都 loaded,启动正常
|
||||
// 2. 请求该路径 → 409,错误信息点名两个候选插件(不静默选边)
|
||||
// 3. GET /api/plugins/conflicts → 列出冲突(路径 + 候选 + 未裁决)
|
||||
// 4. POST /api/plugins/conflicts 裁决 → 持久化
|
||||
// 5. 再请求该路径 → 200,赢家应答({"from":"B"})
|
||||
// 6. /v1/models 等既有路由全程不受影响(旧通路无回归)
|
||||
func TestConflictArbitrationE2E(t *testing.T) {
|
||||
bin := buildBinary(t)
|
||||
dir := t.TempDir()
|
||||
addr := mustAddr(t)
|
||||
|
||||
writeE2EPlugin(t, dir, "plugA.lua", `
|
||||
local P = {}
|
||||
P.name = "plugA"
|
||||
P.version = "1"
|
||||
P.exports = {}
|
||||
P.http = { { path = "contested", method = "POST", handler = "h", roles = { "admin" } } }
|
||||
function P.h(req) return { status = 200, json = { from = "A" } } end
|
||||
return P
|
||||
`)
|
||||
writeE2EPlugin(t, dir, "plugB.lua", `
|
||||
local P = {}
|
||||
P.name = "plugB"
|
||||
P.version = "1"
|
||||
P.exports = {}
|
||||
P.http = { { path = "contested", method = "POST", handler = "h", roles = { "admin" } } }
|
||||
function P.h(req) return { status = 200, json = { from = "B" } } end
|
||||
return P
|
||||
`)
|
||||
|
||||
cfgPath := filepath.Join(dir, "config.yaml")
|
||||
if err := os.WriteFile(cfgPath, []byte(e2eConfig(dir, addr)), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
g := startGateway(t, bin, addr, cfgPath)
|
||||
|
||||
// ① 两个插件都加载成功(冲突不拒载)
|
||||
resp, body := g.do(http.MethodGet, "/api/plugins", "", true)
|
||||
if resp.StatusCode != 200 {
|
||||
t.Fatalf("GET /api/plugins: %d %s", resp.StatusCode, body)
|
||||
}
|
||||
var list struct {
|
||||
Plugins []struct {
|
||||
Name string `json:"name"`
|
||||
Loaded bool `json:"loaded"`
|
||||
} `json:"plugins"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(body), &list); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
loaded := map[string]bool{}
|
||||
for _, p := range list.Plugins {
|
||||
loaded[p.Name] = p.Loaded
|
||||
}
|
||||
if !loaded["plugA"] || !loaded["plugB"] {
|
||||
t.Fatalf("冲突的两个插件都应 loaded: %v", loaded)
|
||||
}
|
||||
|
||||
// ② 请求冲突路径 → 409 + 点名候选(不静默选边)
|
||||
resp, body = g.do(http.MethodPost, "/api/plugins/http/contested", "", true)
|
||||
if resp.StatusCode != http.StatusConflict {
|
||||
t.Fatalf("未裁决冲突路径应 409, 实得 %d (%s)", resp.StatusCode, body)
|
||||
}
|
||||
if !strings.Contains(body, "plugA") || !strings.Contains(body, "plugB") {
|
||||
t.Fatalf("409 应点名两个候选: %s", body)
|
||||
}
|
||||
|
||||
// ③ 冲突列表可见
|
||||
resp, body = g.do(http.MethodGet, "/api/plugins/conflicts", "", true)
|
||||
if resp.StatusCode != 200 {
|
||||
t.Fatalf("GET conflicts: %d %s", resp.StatusCode, body)
|
||||
}
|
||||
var cl struct {
|
||||
Routes []struct {
|
||||
Path string `json:"path"`
|
||||
Candidates []string `json:"candidates"`
|
||||
Resolved string `json:"resolved"`
|
||||
} `json:"routes"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(body), &cl); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(cl.Routes) != 1 || cl.Routes[0].Path != "contested" || cl.Routes[0].Resolved != "" {
|
||||
t.Fatalf("冲突形态不对: %+v", cl)
|
||||
}
|
||||
|
||||
// ④ 裁决 plugB 赢
|
||||
arbit, _ := json.Marshal(map[string]string{
|
||||
"kind": "route", "path": "contested", "winner": "plugB",
|
||||
})
|
||||
resp, body = g.do(http.MethodPost, "/api/plugins/conflicts", string(arbit), true)
|
||||
if resp.StatusCode != 200 {
|
||||
t.Fatalf("POST conflicts 裁决: %d %s", resp.StatusCode, body)
|
||||
}
|
||||
|
||||
// ⑤ 再请求 → 200 且赢家应答
|
||||
resp, body = g.do(http.MethodPost, "/api/plugins/http/contested", "", true)
|
||||
if resp.StatusCode != 200 {
|
||||
t.Fatalf("裁决后请求冲突路径应 200, 实得 %d (%s)", resp.StatusCode, body)
|
||||
}
|
||||
if !strings.Contains(body, `"from":"B"`) {
|
||||
t.Fatalf("赢家 plugB 应答, 实得 %s", body)
|
||||
}
|
||||
|
||||
// ⑥ 旧通路无回归:/v1/models 正常
|
||||
resp, _ = g.do(http.MethodGet, "/v1/models", "", true)
|
||||
if resp.StatusCode != 200 {
|
||||
t.Fatalf("旧通路 /v1/models 应 200, 实得 %d", resp.StatusCode)
|
||||
}
|
||||
}
|
||||
|
||||
// TestAdapterConflictE2E:插件装与内置同名适配器 → 挂起(现任保留),
|
||||
// /api/plugins/conflicts 列出,裁决 candidate 后插件版生效。
|
||||
func TestAdapterConflictE2E(t *testing.T) {
|
||||
bin := buildBinary(t)
|
||||
dir := t.TempDir()
|
||||
addr := mustAddr(t)
|
||||
|
||||
writeE2EPlugin(t, dir, "shadow.lua", `
|
||||
local P = {}
|
||||
P.name = "shadow"
|
||||
P.version = "1"
|
||||
P.exports = { "install" }
|
||||
function P.install(args)
|
||||
local src = 'local a={} a.name="openai" a.version="SHADOW_V1" a.endpoint="/x" return a'
|
||||
local name, err = __llmsproxy_adapters.install("openai", src)
|
||||
return { name = name, err = err }
|
||||
end
|
||||
return P
|
||||
`)
|
||||
cfgPath := filepath.Join(dir, "config.yaml")
|
||||
if err := os.WriteFile(cfgPath, []byte(e2eConfig(dir, addr)), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
g := startGateway(t, bin, addr, cfgPath)
|
||||
|
||||
// ① 安装 → 返回 err(pending 语义),内置版本不变
|
||||
resp, body := g.do(http.MethodPost, "/api/plugins/shadow/call/install", "{}", true)
|
||||
if resp.StatusCode != 200 {
|
||||
t.Fatalf("call install: %d %s", resp.StatusCode, body)
|
||||
}
|
||||
// Call 端点把插件返回值包在 {"result": …} 里;挂起语义的断言在 result 层做。
|
||||
var wrapped struct {
|
||||
Result struct {
|
||||
Name *string `json:"name"`
|
||||
Err string `json:"err"`
|
||||
} `json:"result"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(body), &wrapped); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
out := wrapped.Result
|
||||
if out.Name != nil {
|
||||
t.Fatalf("挂起时不得返回生效名: %s", body)
|
||||
}
|
||||
if !strings.Contains(out.Err, "openai") || !strings.Contains(out.Err, "conflict") {
|
||||
t.Fatalf("错误应点名冲突: %q", out.Err)
|
||||
}
|
||||
|
||||
// ② 冲突列表可见
|
||||
resp, body = g.do(http.MethodGet, "/api/plugins/conflicts", "", true)
|
||||
if resp.StatusCode != 200 {
|
||||
t.Fatalf("GET conflicts: %d %s", resp.StatusCode, body)
|
||||
}
|
||||
var cl struct {
|
||||
Adapters []struct {
|
||||
Name string `json:"name"`
|
||||
Candidate string `json:"candidate"`
|
||||
} `json:"adapters"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(body), &cl); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(cl.Adapters) != 1 || cl.Adapters[0].Name != "openai" || cl.Adapters[0].Candidate != "shadow" {
|
||||
t.Fatalf("冲突形态不对: %+v", cl)
|
||||
}
|
||||
|
||||
// ③ 适配器列表里 openai 仍是内置版本(现任继续服务,不被静默覆盖)
|
||||
assertOpenAIVersion := func(want string, msg string) {
|
||||
t.Helper()
|
||||
resp, body := g.do(http.MethodGet, "/api/adapters", "", true)
|
||||
if resp.StatusCode != 200 {
|
||||
t.Fatalf("GET /api/adapters: %d %s", resp.StatusCode, body)
|
||||
}
|
||||
var al struct {
|
||||
Adapters []struct {
|
||||
Name string `json:"name"`
|
||||
Version string `json:"version"`
|
||||
} `json:"adapters"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(body), &al); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, a := range al.Adapters {
|
||||
if a.Name == "openai" {
|
||||
if a.Version != want {
|
||||
t.Fatalf("%s: openai 版本应 %q, 实得 %q", msg, want, a.Version)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
t.Fatalf("%s: openai 适配器不存在: %s", msg, body)
|
||||
}
|
||||
assertOpenAIVersion("2.0.0", "现任内置必须继续服务(不被静默覆盖)")
|
||||
|
||||
// ④ 裁决 candidate → 插件版生效
|
||||
arbit, _ := json.Marshal(map[string]string{
|
||||
"kind": "adapter", "name": "openai", "winner": "candidate",
|
||||
})
|
||||
if resp, body = g.do(http.MethodPost, "/api/plugins/conflicts", string(arbit), true); resp.StatusCode != 200 {
|
||||
t.Fatalf("裁决 candidate: %d %s", resp.StatusCode, body)
|
||||
}
|
||||
|
||||
// ⑤ 适配器版本已切换 + 冲突清空
|
||||
assertOpenAIVersion("SHADOW_V1", "裁决后插件版应生效")
|
||||
resp, body = g.do(http.MethodGet, "/api/plugins/conflicts", "", true)
|
||||
if resp.StatusCode != 200 {
|
||||
t.Fatal(resp.StatusCode)
|
||||
}
|
||||
if strings.Contains(body, `"name":"openai"`) && strings.Contains(body, `"adapters":[{`) {
|
||||
t.Fatalf("裁决后冲突应清除: %s", body)
|
||||
}
|
||||
}
|
||||
192
e2e/wire_golden_test.go
Normal file
192
e2e/wire_golden_test.go
Normal file
@ -0,0 +1,192 @@
|
||||
package e2e
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// mustAddr 选一个空闲端口并立即释放(与 e2e_test.go 的做法一致)。
|
||||
func mustAddr(t *testing.T) string {
|
||||
t.Helper()
|
||||
l, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatalf("pick port: %v", err)
|
||||
}
|
||||
addr := l.Addr().String()
|
||||
l.Close()
|
||||
return addr
|
||||
}
|
||||
|
||||
// 本文件建立**字节级**对外输出基线,供统一线格式 v2(对外序列化移交适配器)
|
||||
// 做回归比对。
|
||||
//
|
||||
// 为什么必须有它:Phase 2 要把 gateway/chat.go 里硬编码的 OpenAI wire 结构与
|
||||
// writeChatCompletion/pumpStream 的 JSON 拼装移交给适配器 serializer。现有 e2e
|
||||
// 只用 strings.Contains 做宽松断言("包含 Hel"),无法证明"输出字节未变"——
|
||||
// 字段集、omitempty 裁剪、usage 零值处理都可能静默漂移,而客户端会因此看到
|
||||
// 不同的 wire 形状。golden 把当前输出钉死,改动前后逐字节比对。
|
||||
//
|
||||
// 更新方式:GOLDEN_UPDATE=1 go test -tags luajit ./e2e/ -run TestWireGolden
|
||||
// 更新后必须人工审阅 diff —— golden 是契约,不是快照垃圾桶。
|
||||
|
||||
// volatile 命中每次请求都变的字段,比对前替换为占位符。
|
||||
// 允许冒号后的空白:golden 是 pretty-print 过的(`"created": 123`)。
|
||||
var volatile = []*regexp.Regexp{
|
||||
regexp.MustCompile(`"id":\s*"[^"]*"`),
|
||||
regexp.MustCompile(`"created":\s*\d+`),
|
||||
}
|
||||
|
||||
func scrub(s string) string {
|
||||
s = volatile[0].ReplaceAllString(s, `"id": "<ID>"`)
|
||||
return volatile[1].ReplaceAllString(s, `"created": <TS>`)
|
||||
}
|
||||
|
||||
func compareGolden(t *testing.T, name, got string) {
|
||||
t.Helper()
|
||||
got = scrub(got)
|
||||
path := filepath.Join("..", ".snapshots", name)
|
||||
if os.Getenv("GOLDEN_UPDATE") == "1" {
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(path, []byte(got), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Logf("golden updated: %s", path)
|
||||
return
|
||||
}
|
||||
want, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatalf("golden missing (%s): %v\nrun GOLDEN_UPDATE=1 go test -tags luajit ./e2e/ -run TestWireGolden to create", path, err)
|
||||
}
|
||||
if got != string(want) {
|
||||
t.Errorf("对外输出与 golden 漂移 (%s)\n--- want ---\n%s\n--- got ---\n%s",
|
||||
name, string(want), got)
|
||||
}
|
||||
}
|
||||
|
||||
// prettyJSON 缩进输出。走 map 会排序键,从而让「字段集」成为唯一被比较的东西,
|
||||
// 忽略 Go struct 声明顺序(那不是对外契约的一部分)。
|
||||
func prettyJSON(t *testing.T, raw string) string {
|
||||
t.Helper()
|
||||
var v interface{}
|
||||
if err := json.Unmarshal([]byte(raw), &v); err != nil {
|
||||
return raw
|
||||
}
|
||||
out, err := json.MarshalIndent(v, "", " ")
|
||||
if err != nil {
|
||||
return raw
|
||||
}
|
||||
return string(out)
|
||||
}
|
||||
|
||||
// TestWireGoldenNonStreamChat 钉死非流式 /v1/chat/completions 的字段集。
|
||||
func TestWireGoldenNonStreamChat(t *testing.T) {
|
||||
up := newMockUpstream(t)
|
||||
dir := t.TempDir()
|
||||
addr := mustAddr(t)
|
||||
g := startGateway(t, buildBinary(t), addr, writeConfig(t, dir, addr, map[string]*mockUpstream{"good": up}))
|
||||
|
||||
resp, body := g.do("POST", "/v1/chat/completions",
|
||||
`{"model":"good/good-m","messages":[{"role":"user","content":"hi"}]}`, true)
|
||||
|
||||
// 语义:OpenAI 客户端依赖这些顶层字段
|
||||
var obj map[string]interface{}
|
||||
if err := json.Unmarshal([]byte(body), &obj); err != nil {
|
||||
t.Fatalf("not JSON: %v (%s)", err, body)
|
||||
}
|
||||
for _, k := range []string{"id", "object", "created", "model", "choices", "usage"} {
|
||||
if _, ok := obj[k]; !ok {
|
||||
t.Errorf("非流式响应缺少 OpenAI 必需字段 %q: %s", k, body)
|
||||
}
|
||||
}
|
||||
if obj["object"] != "chat.completion" {
|
||||
t.Errorf("object = %v, want chat.completion", obj["object"])
|
||||
}
|
||||
if resp.StatusCode != 200 {
|
||||
t.Errorf("status = %d, want 200", resp.StatusCode)
|
||||
}
|
||||
|
||||
compareGolden(t, "chat_nonstream.json", prettyJSON(t, body))
|
||||
}
|
||||
|
||||
// TestWireGoldenStreamChat 钉死流式 SSE 的帧形状与终止序列。
|
||||
func TestWireGoldenStreamChat(t *testing.T) {
|
||||
up := newMockUpstream(t)
|
||||
dir := t.TempDir()
|
||||
addr := mustAddr(t)
|
||||
g := startGateway(t, buildBinary(t), addr, writeConfig(t, dir, addr, map[string]*mockUpstream{"good": up}))
|
||||
|
||||
resp, _ := doStream(t, g, `{"model":"good/good-m","stream":true,"messages":[{"role":"user","content":"hi"}]}`)
|
||||
defer resp.Body.Close()
|
||||
|
||||
var frames []string
|
||||
sc := bufio.NewScanner(resp.Body)
|
||||
for sc.Scan() {
|
||||
line := sc.Text()
|
||||
if !strings.HasPrefix(line, "data: ") {
|
||||
continue
|
||||
}
|
||||
payload := strings.TrimPrefix(line, "data: ")
|
||||
if payload == "[DONE]" {
|
||||
frames = append(frames, "[DONE]")
|
||||
continue
|
||||
}
|
||||
frames = append(frames, prettyJSON(t, payload))
|
||||
}
|
||||
if len(frames) < 2 {
|
||||
t.Fatalf("期望多帧 SSE,实得 %d: %v", len(frames), frames)
|
||||
}
|
||||
if frames[len(frames)-1] != "[DONE]" {
|
||||
t.Errorf("末帧 = %q, want [DONE]", frames[len(frames)-1])
|
||||
}
|
||||
// 首帧必须是 role 前导:OpenAI 客户端靠它建立 assistant 消息
|
||||
if !strings.Contains(frames[0], `"role"`) {
|
||||
t.Errorf("首个 SSE 帧必须携带 role 前导,实得: %s", frames[0])
|
||||
}
|
||||
|
||||
compareGolden(t, "chat_stream.txt", strings.Join(frames, "\n"))
|
||||
}
|
||||
|
||||
// TestWireGoldenImage 钉死 /v1/images/generations 的响应形状。
|
||||
func TestWireGoldenImage(t *testing.T) {
|
||||
up := newMockUpstream(t)
|
||||
dir := t.TempDir()
|
||||
addr := mustAddr(t)
|
||||
g := startGateway(t, buildBinary(t), addr, writeConfig(t, dir, addr, map[string]*mockUpstream{"image": up}))
|
||||
|
||||
resp, body := g.do("POST", "/v1/images/generations", `{"model":"imagegen/flux-1","prompt":"a cat"}`, true)
|
||||
if resp.StatusCode != 200 {
|
||||
t.Fatalf("image status = %d: %s", resp.StatusCode, body)
|
||||
}
|
||||
if !strings.Contains(body, "QUJD") {
|
||||
t.Fatalf("生图响应丢失了载荷: %s", body)
|
||||
}
|
||||
compareGolden(t, "image_generation.json", prettyJSON(t, body))
|
||||
}
|
||||
|
||||
// doStream 发流式请求并返回未读完的响应(调用方负责逐行读 SSE)。
|
||||
func doStream(t *testing.T, g *gatewayUnderTest, body string) (*http.Response, error) {
|
||||
t.Helper()
|
||||
req, err := http.NewRequest("POST", "http://"+g.addr+"/v1/chat/completions", strings.NewReader(body))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", "Bearer "+g.key)
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
t.Fatalf("stream request: %v", err)
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
var _ = io.Discard
|
||||
@ -73,15 +73,20 @@ type Model struct {
|
||||
|
||||
// Source describes a single upstream LLM provider.
|
||||
type Source struct {
|
||||
Name string `yaml:"name" json:"name"`
|
||||
BaseURL string `yaml:"base_url" json:"base_url"`
|
||||
APIKey string `yaml:"api_key" json:"api_key"`
|
||||
APIKeyEnv string `yaml:"api_key_env,omitempty" json:"-"` // reference to an env var holding the key (overrides api_key)
|
||||
Adapter string `yaml:"adapter" json:"adapter"`
|
||||
Endpoint string `yaml:"endpoint" json:"endpoint,omitempty"` // chat endpoint override
|
||||
ImageEndpoint string `yaml:"image_endpoint" json:"image_endpoint,omitempty"` // image endpoint override
|
||||
Models []Model `yaml:"models" json:"models"`
|
||||
Headers map[string]string `yaml:"headers" json:"headers,omitempty"`
|
||||
Name string `yaml:"name" json:"name"`
|
||||
BaseURL string `yaml:"base_url" json:"base_url"`
|
||||
APIKey string `yaml:"api_key" json:"api_key"`
|
||||
APIKeyEnv string `yaml:"api_key_env,omitempty" json:"-"` // reference to an env var holding the key (overrides api_key)
|
||||
Adapter string `yaml:"adapter" json:"adapter"`
|
||||
Endpoint string `yaml:"endpoint" json:"endpoint,omitempty"` // chat endpoint override
|
||||
// StreamEndpoint overrides the CHAT endpoint for streaming requests. Most
|
||||
// upstreams stream on the same path; Gemini is the exception (same path,
|
||||
// different verb). Declaring it here keeps that knowledge out of the kernel,
|
||||
// which only compares opaque strings.
|
||||
StreamEndpoint string `yaml:"endpoint_stream" json:"endpoint_stream,omitempty"`
|
||||
ImageEndpoint string `yaml:"image_endpoint" json:"image_endpoint,omitempty"` // image endpoint override
|
||||
Models []Model `yaml:"models" json:"models"`
|
||||
Headers map[string]string `yaml:"headers" json:"headers,omitempty"`
|
||||
// ProxyURL routes this source's HTTP(S) traffic through an explicit proxy
|
||||
// (e.g. http://127.0.0.1:7890). Empty = direct connection. Needed for
|
||||
// upstreams behind geo/IP blocks that only respond through a proxy.
|
||||
@ -373,13 +378,14 @@ type RuntimeConfig struct {
|
||||
// name and api_key) so the WebUI can spin up multiple key-bearing sources
|
||||
// from one shared template.
|
||||
type SourceTemplate struct {
|
||||
Name string `json:"name"`
|
||||
BaseURL string `json:"base_url"`
|
||||
Adapter string `json:"adapter"`
|
||||
Endpoint string `json:"endpoint,omitempty"`
|
||||
ImageEndpoint string `json:"image_endpoint,omitempty"`
|
||||
Models []Model `json:"models"`
|
||||
Headers map[string]string `json:"headers,omitempty"`
|
||||
Name string `json:"name"`
|
||||
BaseURL string `json:"base_url"`
|
||||
Adapter string `json:"adapter"`
|
||||
Endpoint string `json:"endpoint,omitempty"`
|
||||
StreamEndpoint string `json:"endpoint_stream,omitempty"`
|
||||
ImageEndpoint string `json:"image_endpoint,omitempty"`
|
||||
Models []Model `json:"models"`
|
||||
Headers map[string]string `json:"headers,omitempty"`
|
||||
// ProxyURL routes this source's HTTP(S) traffic through an explicit proxy.
|
||||
ProxyURL string `json:"proxy_url,omitempty"`
|
||||
Meta map[string]interface{} `json:"meta,omitempty"`
|
||||
@ -393,18 +399,18 @@ type SourceTemplate struct {
|
||||
// (full management) or "user" (sees only its own key); Models is the allowed
|
||||
// model scope with per-model token quota (0 = unlimited).
|
||||
type GWKey struct {
|
||||
Key string `yaml:"key" json:"key"`
|
||||
Role string `yaml:"role" json:"role"`
|
||||
Name string `yaml:"name,omitempty" json:"name,omitempty"`
|
||||
Key string `yaml:"key" json:"key"`
|
||||
Role string `yaml:"role" json:"role"`
|
||||
Name string `yaml:"name,omitempty" json:"name,omitempty"`
|
||||
// Auto is this key's own AUTO scheduling chain. It overrides the global
|
||||
// cfg.Auto for requests authenticated with this key; nil (or an empty
|
||||
// list) means "inherit the global chain", so keys created before per-key
|
||||
// chains existed keep working untouched.
|
||||
Auto []ModelScope `yaml:"auto,omitempty" json:"auto,omitempty"`
|
||||
Models []ModelScope `yaml:"models,omitempty" json:"models,omitempty"`
|
||||
Note string `yaml:"note,omitempty" json:"note,omitempty"`
|
||||
CreatedAt int64 `yaml:"created_at,omitempty" json:"created_at,omitempty"`
|
||||
Seed bool `yaml:"seed,omitempty" json:"seed,omitempty"` // true if migrated from config gateway_keys
|
||||
Auto []ModelScope `yaml:"auto,omitempty" json:"auto,omitempty"`
|
||||
Models []ModelScope `yaml:"models,omitempty" json:"models,omitempty"`
|
||||
Note string `yaml:"note,omitempty" json:"note,omitempty"`
|
||||
CreatedAt int64 `yaml:"created_at,omitempty" json:"created_at,omitempty"`
|
||||
Seed bool `yaml:"seed,omitempty" json:"seed,omitempty"` // true if migrated from config gateway_keys
|
||||
}
|
||||
|
||||
// HasOwnAuto reports whether the key declares its own AUTO chain rather than
|
||||
|
||||
40
internal/core/channels_device_test.go
Normal file
40
internal/core/channels_device_test.go
Normal file
@ -0,0 +1,40 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestChannelsDevicePollBehavior 是 device-poll 流的行为对齐测试。
|
||||
//
|
||||
// 决策表逐行搬自参考实现 pollMinimaxDeviceToken(minimax-oauth.ts),
|
||||
// 上游用假服务器模拟,逐次响应由测试控制。断言的是**行为**(会话状态推进),
|
||||
// 不是实现细节。决策表本体在 deviceHarness.runDecisionTable(与变异测试共用)。
|
||||
func TestChannelsDevicePollBehavior(t *testing.T) {
|
||||
h := newDeviceHarness(t, mustChannelsSrc(t))
|
||||
// begin 直查:user_code 透传、授权 URL、verifier 不回显
|
||||
j := h.begin()
|
||||
sess := j["session"].(map[string]interface{})
|
||||
if sess["user_code"] != "ABCD-EFGH" {
|
||||
t.Fatalf("begin 应透传 user_code, 实得 %v", sess["user_code"])
|
||||
}
|
||||
if j["url"] != "https://example/activate" {
|
||||
t.Fatalf("begin 应给授权 URL, 实得 %v", j["url"])
|
||||
}
|
||||
if _, has := j["device_verifier"]; has {
|
||||
t.Fatal("verifier 绝不能回显")
|
||||
}
|
||||
// 决策表全过
|
||||
if !h.runDecisionTable() {
|
||||
t.Fatal("device-poll 决策表未全过(见 runDecisionTable 内的行号)")
|
||||
}
|
||||
}
|
||||
|
||||
// mustChannelsSrc 读 bundled channels 源码。
|
||||
func mustChannelsSrc(t *testing.T) string {
|
||||
t.Helper()
|
||||
src, err := readBundledChannels(t)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return src
|
||||
}
|
||||
405
internal/core/channels_more_test.go
Normal file
405
internal/core/channels_more_test.go
Normal file
@ -0,0 +1,405 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"llmsproxy/internal/lua"
|
||||
)
|
||||
|
||||
// channelsMoreTest:新引入的三条真实流程 + 两个边界流的行为对齐测试。
|
||||
// 平台定义、决策表、端点逐字照抄参考(见 channels.lua 注释),测试照参考行为搬。
|
||||
|
||||
// ---- Cline(WorkOS 设备码 + register 两步) ----
|
||||
|
||||
// clineHarness 假上游:device code / authenticate / register 三端点。
|
||||
type clineHarness struct {
|
||||
t *testing.T
|
||||
ps *lua.Plugins
|
||||
ts *httptest.Server
|
||||
tok atomic.Value // 当前 authenticate 响应脚本 []scriptStep
|
||||
step int32
|
||||
}
|
||||
|
||||
func newClineHarness(t *testing.T) *clineHarness {
|
||||
t.Helper()
|
||||
h := &clineHarness{t: t}
|
||||
h.tok.Store([]scriptStep{})
|
||||
h.ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
switch r.URL.Path {
|
||||
case "/user_management/authorize/device":
|
||||
fmt.Fprint(w, `{"device_code":"CD1","user_code":"WXYZ-1234","verification_uri":"https://example/activate","interval":1,"expires_in":600}`)
|
||||
case "/user_management/authenticate":
|
||||
i := int(atomic.AddInt32(&h.step, 1)) - 1
|
||||
s := h.tok.Load().([]scriptStep)
|
||||
if i >= len(s) {
|
||||
w.WriteHeader(500)
|
||||
fmt.Fprint(w, `{"error":"unexpected"}`)
|
||||
return
|
||||
}
|
||||
if s[i].http != 0 && s[i].http != 200 {
|
||||
w.WriteHeader(s[i].http)
|
||||
}
|
||||
fmt.Fprint(w, s[i].body)
|
||||
case "/api/v1/auth/register":
|
||||
// 校验请求体是驼峰 accessToken/refreshToken(参考 registerClineTokens)
|
||||
b, _ := io.ReadAll(r.Body)
|
||||
for _, want := range []string{`"accessToken":"WO-AT-1"`, `"refreshToken":"WO-RT-1"`} {
|
||||
if !strings.Contains(string(b), want) {
|
||||
t.Errorf("register 请求体缺 %s: %s", want, b)
|
||||
}
|
||||
}
|
||||
fmt.Fprint(w, `{"success":true,"data":{"accessToken":"CLINE-AT-1","refreshToken":"CLINE-RT-1","userInfo":{"clineUserId":"u1","email":"dev@example.com"}}}`)
|
||||
default:
|
||||
w.WriteHeader(404)
|
||||
}
|
||||
}))
|
||||
t.Cleanup(h.ts.Close)
|
||||
|
||||
c := kernelTestCore(t)
|
||||
h.ps = c.Plugins()
|
||||
src, _ := readBundledChannels(t)
|
||||
if err := h.ps.LoadSource("channels", src); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := h.ps.Call("channels", "set_endpoints", map[string]interface{}{
|
||||
"platform": "cline",
|
||||
"device": map[string]interface{}{"code_url": h.ts.URL + "/user_management/authorize/device", "token_url": h.ts.URL + "/user_management/authenticate"},
|
||||
"register": map[string]interface{}{"url": h.ts.URL + "/api/v1/auth/register"},
|
||||
}); err != nil {
|
||||
t.Fatalf("set_endpoints: %v", err)
|
||||
}
|
||||
return h
|
||||
}
|
||||
|
||||
func (h *clineHarness) begin() map[string]interface{} {
|
||||
out, err := h.ps.InvokeHandler("channels", "start_login", &lua.HandlerRequest{
|
||||
Method: "POST", Body: `{"platform":"cline"}`,
|
||||
})
|
||||
if err != nil {
|
||||
h.t.Fatal(err)
|
||||
}
|
||||
return out.JSON.(map[string]interface{})
|
||||
}
|
||||
func (h *clineHarness) poll(id string) map[string]interface{} {
|
||||
out, _ := h.ps.InvokeHandler("channels", "poll_login", &lua.HandlerRequest{
|
||||
Method: "GET", Query: map[string]string{"id": id},
|
||||
})
|
||||
return out.JSON.(map[string]interface{})
|
||||
}
|
||||
|
||||
// runWorkosTable 是 WorkOS(Cline)分支的行为契约,供变异测试杀 workos 变异体。
|
||||
// 决策表 5 行,跟 TestClineWorkOSBehavior 同源(这里只返回布尔,不报细节)。
|
||||
func (h *clineHarness) runWorkosTable() bool {
|
||||
st := func(steps ...string) map[string]interface{} {
|
||||
h.tok.Store(scriptSteps(steps...))
|
||||
atomic.StoreInt32(&h.step, 0)
|
||||
j := h.begin()
|
||||
sid := j["session"].(map[string]interface{})["id"].(string)
|
||||
return h.poll(sid)
|
||||
}
|
||||
_ = h.ps.SetPluginKernelPermission("channels", []string{"add_source", "status", "list_sources"}, false)
|
||||
// 行1: 非200+error=authorization_pending → 等
|
||||
if j := st(`400 {"error":"authorization_pending"}`); j["state"] != "pending" {
|
||||
return false
|
||||
}
|
||||
// 行2: 非200+error=slow_down → 等 + interval+1(基线 1)
|
||||
h.tok.Store(scriptSteps(`400 {"error":"slow_down"}`))
|
||||
atomic.StoreInt32(&h.step, 0)
|
||||
j := h.begin()
|
||||
sid := j["session"].(map[string]interface{})["id"].(string)
|
||||
if j2 := h.poll(sid); j2["state"] != "pending" {
|
||||
return false
|
||||
} else if iv, _ := j2["interval"].(float64); iv != 2 {
|
||||
return false
|
||||
}
|
||||
// 行3: error=access_denied → failed
|
||||
if j := st(`400 {"error":"access_denied","error_description":"user declined"}`); j["state"] != "failed" {
|
||||
return false
|
||||
}
|
||||
// 行4: 成功(access+refresh)→ register 换 CLINE token → completed
|
||||
if j := st(`{"access_token":"WO-AT-1","refresh_token":"WO-RT-1"}`); j["state"] != "completed" || j["account"] != "dev@example.com" {
|
||||
return false
|
||||
}
|
||||
v, _, _ := h.ps.GetSecret("channels", "channels:cline")
|
||||
if v != "CLINE-AT-1" {
|
||||
return false
|
||||
}
|
||||
// 行5: 非200无error字段 → failed(不是死等)
|
||||
if j := st(`500 {"foo":"bar"}`); j["state"] != "failed" {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// TestClineWorkOSBehavior:WorkOS 判据(error 字段)+ register 两步 + slow_down +1s。
|
||||
func TestClineWorkOSBehavior(t *testing.T) {
|
||||
h := newClineHarness(t)
|
||||
_ = h.ps.SetPluginKernelPermission("channels", []string{"add_source", "status", "list_sources"}, false)
|
||||
|
||||
st := func(steps ...string) map[string]interface{} {
|
||||
h.tok.Store(scriptSteps(steps...))
|
||||
atomic.StoreInt32(&h.step, 0)
|
||||
j := h.begin()
|
||||
sid := j["session"].(map[string]interface{})["id"].(string)
|
||||
return h.poll(sid)
|
||||
}
|
||||
|
||||
t.Run("行1 error=authorization_pending_非200也继续等", func(t *testing.T) {
|
||||
if j := st(`400 {"error":"authorization_pending"}`); j["state"] != "pending" {
|
||||
t.Fatalf("应 pending: %v", j)
|
||||
}
|
||||
})
|
||||
t.Run("行2 error=slow_down_退避1秒", func(t *testing.T) {
|
||||
h.tok.Store(scriptSteps(`400 {"error":"slow_down"}`))
|
||||
atomic.StoreInt32(&h.step, 0)
|
||||
j := h.begin()
|
||||
sid := j["session"].(map[string]interface{})["id"].(string)
|
||||
j2 := h.poll(sid)
|
||||
if j2["state"] != "pending" {
|
||||
t.Fatalf("应 pending: %v", j2)
|
||||
}
|
||||
if iv, _ := j2["interval"].(float64); iv != 2 { // 基线 1 + slow_down_step 1
|
||||
t.Fatalf("WorkOS slow_down 应 interval+1s 得 2, 实得 %v", iv)
|
||||
}
|
||||
})
|
||||
t.Run("行3 error=access_denied_失败带拒绝", func(t *testing.T) {
|
||||
if j := st(`400 {"error":"access_denied","error_description":"user declined"}`); j["state"] != "failed" {
|
||||
t.Fatalf("应 failed: %v", j)
|
||||
}
|
||||
})
|
||||
t.Run("行4 成功_access+refresh_再打register_凭据是CLINE-AT-1", func(t *testing.T) {
|
||||
j := st(`{"access_token":"WO-AT-1","refresh_token":"WO-RT-1"}`)
|
||||
if j["state"] != "completed" || j["account"] != "dev@example.com" {
|
||||
t.Fatalf("应 completed+dev@example.com: %v", j)
|
||||
}
|
||||
v, _, _ := h.ps.GetSecret("channels", "channels:cline")
|
||||
if v != "CLINE-AT-1" {
|
||||
t.Fatalf("凭据应是 register 的 CLINE-AT-1, 实得 %q", v)
|
||||
}
|
||||
})
|
||||
t.Run("行5 非200无error字段_失败", func(t *testing.T) {
|
||||
if j := st(`500 {"foo":"bar"}`); j["state"] != "failed" {
|
||||
t.Fatalf("应 failed: %v", j)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// ---- Buddy(CodeBuddy state 轮询) ----
|
||||
|
||||
type buddyHarness struct {
|
||||
t *testing.T
|
||||
ps *lua.Plugins
|
||||
ts *httptest.Server
|
||||
tokStep int32
|
||||
tokOk int32
|
||||
acctOk int32
|
||||
}
|
||||
|
||||
func newBuddyHarness(t *testing.T) *buddyHarness {
|
||||
t.Helper()
|
||||
h := &buddyHarness{t: t, tokOk: 1, acctOk: 1}
|
||||
h.ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
switch {
|
||||
case r.Method == "POST" && r.URL.Path == "/v2/plugin/auth/state":
|
||||
fmt.Fprint(w, `{"data":{"state":"ST-1","authUrl":"https://www.codebuddy.cn/login/?platform=ide&state=ST-1"}}`)
|
||||
case r.Method == "GET" && r.URL.Path == "/v2/plugin/auth/token":
|
||||
i := atomic.AddInt32(&h.tokStep, 1)
|
||||
if atomic.LoadInt32(&h.tokOk) == 0 {
|
||||
w.WriteHeader(200)
|
||||
fmt.Fprint(w, `{"code":11217,"message":"token not ready"}`)
|
||||
return
|
||||
}
|
||||
if i == 1 {
|
||||
fmt.Fprint(w, `{"code":11217,"message":"token not ready"}`)
|
||||
} else {
|
||||
fmt.Fprint(w, `{"data":{"accessToken":"BD-AT-1","refreshToken":"BD-RT-1"}}`)
|
||||
}
|
||||
case r.Method == "GET" && r.URL.Path == "/v2/plugin/login/account":
|
||||
if atomic.LoadInt32(&h.acctOk) == 0 {
|
||||
w.WriteHeader(500)
|
||||
fmt.Fprint(w, `{"code":1}`)
|
||||
return
|
||||
}
|
||||
fmt.Fprint(w, `{"data":{"nickName":"buddy-dev"}}`)
|
||||
default:
|
||||
w.WriteHeader(404)
|
||||
}
|
||||
}))
|
||||
t.Cleanup(h.ts.Close)
|
||||
|
||||
c := kernelTestCore(t)
|
||||
h.ps = c.Plugins()
|
||||
src, _ := readBundledChannels(t)
|
||||
if err := h.ps.LoadSource("channels", src); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := h.ps.Call("channels", "set_endpoints", map[string]interface{}{
|
||||
"platform": "buddy",
|
||||
"state": map[string]interface{}{"state_url": h.ts.URL + "/v2/plugin/auth/state", "token_url": h.ts.URL + "/v2/plugin/auth/token", "account_url": h.ts.URL + "/v2/plugin/login/account"},
|
||||
}); err != nil {
|
||||
t.Fatalf("set_endpoints: %v", err)
|
||||
}
|
||||
return h
|
||||
}
|
||||
|
||||
func (h *buddyHarness) begin() (map[string]interface{}, string) {
|
||||
out, err := h.ps.InvokeHandler("channels", "start_login", &lua.HandlerRequest{
|
||||
Method: "POST", Body: `{"platform":"buddy"}`,
|
||||
})
|
||||
if err != nil {
|
||||
h.t.Fatal(err)
|
||||
}
|
||||
j := out.JSON.(map[string]interface{})
|
||||
return j, j["session"].(map[string]interface{})["id"].(string)
|
||||
}
|
||||
|
||||
// beginPool 走 UI 的真实流程:先 account/add 建占位账号,再带 account_id 登录。
|
||||
// 这正是用户报障("登录了但无法获取账号信息")的那条路径。
|
||||
func (h *buddyHarness) beginPool() (map[string]interface{}, string, string) {
|
||||
add, err := h.ps.InvokeHandler("channels", "account_add", &lua.HandlerRequest{
|
||||
Method: "POST", Body: `{"platform":"buddy"}`,
|
||||
})
|
||||
if err != nil {
|
||||
h.t.Fatal(err)
|
||||
}
|
||||
aid := add.JSON.(map[string]interface{})["account_id"].(string)
|
||||
out, err := h.ps.InvokeHandler("channels", "start_login", &lua.HandlerRequest{
|
||||
Method: "POST", Body: fmt.Sprintf(`{"platform":"buddy","account_id":%q}`, aid),
|
||||
})
|
||||
if err != nil {
|
||||
h.t.Fatal(err)
|
||||
}
|
||||
j := out.JSON.(map[string]interface{})
|
||||
return j, j["session"].(map[string]interface{})["id"].(string), aid
|
||||
}
|
||||
|
||||
func (h *buddyHarness) accountList() []interface{} {
|
||||
out, err := h.ps.InvokeHandler("channels", "account_list", &lua.HandlerRequest{
|
||||
Method: "POST", Body: `{"platform":"buddy"}`,
|
||||
})
|
||||
if err != nil {
|
||||
h.t.Fatal(err)
|
||||
}
|
||||
m := out.JSON.(map[string]interface{})
|
||||
accts, _ := m["accounts"].([]interface{})
|
||||
return accts
|
||||
}
|
||||
func (h *buddyHarness) poll(id string) map[string]interface{} {
|
||||
out, _ := h.ps.InvokeHandler("channels", "poll_login", &lua.HandlerRequest{
|
||||
Method: "GET", Query: map[string]string{"id": id},
|
||||
})
|
||||
return out.JSON.(map[string]interface{})
|
||||
}
|
||||
|
||||
// TestBuddyStatePollBehavior:state-poll 两拍(先 11217 后 token)+ 账号查询 + account 失败不阻塞。
|
||||
func TestBuddyStatePollBehavior(t *testing.T) {
|
||||
h := newBuddyHarness(t)
|
||||
_ = h.ps.SetPluginKernelPermission("channels", []string{"add_source", "status", "list_sources"}, false)
|
||||
|
||||
t.Run("行1 state申请给出authUrl", func(t *testing.T) {
|
||||
j, _ := h.begin()
|
||||
if j["url"] == "" {
|
||||
t.Fatalf("应有 authUrl: %v", j)
|
||||
}
|
||||
})
|
||||
t.Run("行2 首拍11217仍pending_次拍token_completed", func(t *testing.T) {
|
||||
atomic.StoreInt32(&h.tokStep, 0)
|
||||
_, sid := h.begin()
|
||||
if j := h.poll(sid); j["state"] != "pending" {
|
||||
t.Fatalf("首拍应 pending: %v", j)
|
||||
}
|
||||
if j := h.poll(sid); j["state"] != "completed" || j["account"] != "buddy-dev" {
|
||||
t.Fatalf("次拍应 completed+buddy-dev: %v", j)
|
||||
}
|
||||
// 凭据是**完整凭据 JSON**(对齐参考 buildCredential),不再是裸 token:
|
||||
// 必须带 refresh_token,否则 refreshable=false、无法静默续期。
|
||||
v, _, _ := h.ps.GetSecret("channels", "channels:buddy")
|
||||
if !strings.Contains(v, `"access_token":"BD-AT-1"`) {
|
||||
t.Fatalf("凭据应含 access_token, 实得 %q", v)
|
||||
}
|
||||
if !strings.Contains(v, `"refresh_token":"BD-RT-1"`) {
|
||||
t.Fatalf("凭据必须含 refresh_token(否则无法续期), 实得 %q", v)
|
||||
}
|
||||
if !strings.Contains(v, "buddy-dev") {
|
||||
t.Fatalf("凭据应回填昵称, 实得 %q", v)
|
||||
}
|
||||
})
|
||||
t.Run("行3 account查询失败不阻塞_回退默认名", func(t *testing.T) {
|
||||
atomic.StoreInt32(&h.tokStep, 0)
|
||||
atomic.StoreInt32(&h.acctOk, 0)
|
||||
defer atomic.StoreInt32(&h.acctOk, 1)
|
||||
_, sid := h.begin()
|
||||
h.poll(sid)
|
||||
j := h.poll(sid)
|
||||
if j["state"] != "completed" {
|
||||
t.Fatalf("account 失败不应阻塞: %v", j)
|
||||
}
|
||||
})
|
||||
|
||||
// 行4 回归:UI 真实流程(account/add 建占位账号 → 带 account_id 登录)。
|
||||
// 缺陷复现:poll_login 完成时不传 account_id → 凭据落到 `channels:buddy`,
|
||||
// 而池账号 credentialRef 是 `channels:buddy:<aid>` → account_list 报
|
||||
// has_credential=false、credits 报「无凭据」——即用户报障的
|
||||
// 「登录了但无法获取账号信息」。
|
||||
t.Run("行4 池路径:登录后账号必须真的拿到凭据", func(t *testing.T) {
|
||||
atomic.StoreInt32(&h.tokStep, 0)
|
||||
_, sid, aid := h.beginPool()
|
||||
h.poll(sid) // 首拍 11217
|
||||
j := h.poll(sid)
|
||||
if j["state"] != "completed" {
|
||||
t.Fatalf("池路径登录应完成: %v", j)
|
||||
}
|
||||
// ① 凭据必须落在池账号的 ref 上
|
||||
v, ok, _ := h.ps.GetSecret("channels", "channels:buddy:"+aid)
|
||||
if !ok || v == "" {
|
||||
t.Fatalf("凭据未落到池账号 ref(用户报障的根因); aid=%s ok=%v", aid, ok)
|
||||
}
|
||||
if !strings.Contains(v, `"refresh_token":"BD-RT-1"`) {
|
||||
t.Fatalf("池凭据应是完整 JSON: %q", v)
|
||||
}
|
||||
// ② account_list 必须报 has_credential=true(UI 读的就是这个字段)
|
||||
found := false
|
||||
for _, raw := range h.accountList() {
|
||||
a, _ := raw.(map[string]interface{})
|
||||
if a["id"] == aid {
|
||||
found = true
|
||||
if a["has_credential"] != true {
|
||||
t.Fatalf("has_credential 应为 true, 实得 %v", a["has_credential"])
|
||||
}
|
||||
if a["refreshable"] != true {
|
||||
t.Fatalf("refreshable 应为 true(有 refresh_token), 实得 %v", a["refreshable"])
|
||||
}
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("池账号 %s 未出现在 account_list", aid)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// ---- 通用 ----
|
||||
|
||||
func scriptSteps(steps ...string) []scriptStep {
|
||||
var out []scriptStep
|
||||
for _, b := range steps {
|
||||
code := 200
|
||||
switch {
|
||||
case strings.HasPrefix(b, "400 "):
|
||||
code, b = 400, b[4:]
|
||||
case strings.HasPrefix(b, "403 "):
|
||||
code, b = 403, b[4:]
|
||||
case strings.HasPrefix(b, "500 "):
|
||||
code, b = 500, b[4:]
|
||||
}
|
||||
out = append(out, scriptStep{http: code, body: b})
|
||||
}
|
||||
return out
|
||||
}
|
||||
342
internal/core/channels_plugin_test.go
Normal file
342
internal/core/channels_plugin_test.go
Normal file
@ -0,0 +1,342 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"llmsproxy/internal/lua"
|
||||
)
|
||||
|
||||
// TestChannelsPluginEndToEnd 是「同类插件」的完整验收:
|
||||
// 内置的 channels 插件在**真实内核**上跑通「登录 → 存凭据 → 接入为上游源」。
|
||||
//
|
||||
// 这条链路横跨本次所有新增能力,所以它是最有信息量的一个测试:
|
||||
//
|
||||
// __llmsproxy_kernel add_source 把平台注册为上游源
|
||||
// 内核权限 默认拒绝,需运维授权
|
||||
// 额外出站主机授权 登录域不在源列表里,默认被出站白名单拒绝(plugin.hosts)
|
||||
// 登录会话 插件自持(plugin.state.sessions),内核无登录概念
|
||||
func TestChannelsPluginEndToEnd(t *testing.T) {
|
||||
c := kernelTestCore(t)
|
||||
ps := c.Plugins()
|
||||
|
||||
src, err := lua.ReadBundledPlugin("channels")
|
||||
if err != nil {
|
||||
t.Fatalf("读取内置 channels 插件: %v", err)
|
||||
}
|
||||
if err := ps.LoadSource("channels", src); err != nil {
|
||||
t.Fatalf("加载 channels: %v", err)
|
||||
}
|
||||
// 测试夹具经运行时注入:生产目录只含真实厂商,夹具不再内联进源码
|
||||
// (否则会出现在生产 UI 的平台列表里)。
|
||||
injectTestPlatforms(t, ps, examplePlatformFixture)
|
||||
|
||||
// 1. 平台清单可用(未授权任何东西也应能列出)
|
||||
out, err := ps.InvokeHandler("channels", "list_platforms", &lua.HandlerRequest{
|
||||
Method: "GET", Role: "admin",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("list_platforms: %v", err)
|
||||
}
|
||||
body, _ := out.JSON.(map[string]interface{})
|
||||
plats, _ := body["platforms"].([]interface{})
|
||||
if len(plats) < 3 {
|
||||
t.Fatalf("应至少 3 个平台, 实得 %d", len(plats))
|
||||
}
|
||||
|
||||
// 2. 发起登录 → 拿到会话与 URL
|
||||
out, err = ps.InvokeHandler("channels", "start_login", &lua.HandlerRequest{
|
||||
Method: "POST", Body: `{"platform":"example"}`, Role: "admin",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("start_login: %v", err)
|
||||
}
|
||||
m := out.JSON.(map[string]interface{})
|
||||
if m["error"] != nil {
|
||||
t.Fatalf("发起登录失败: %v", m["error"])
|
||||
}
|
||||
if m["url"] == nil || m["url"] == "" {
|
||||
t.Fatal("应返回用户要打开的登录 URL")
|
||||
}
|
||||
sess := m["session"].(map[string]interface{})
|
||||
id, _ := sess["id"].(string)
|
||||
if id == "" {
|
||||
t.Fatal("应返回会话 id")
|
||||
}
|
||||
|
||||
// 3. 轮询到 pending
|
||||
out, _ = ps.InvokeHandler("channels", "poll_login", &lua.HandlerRequest{
|
||||
Method: "GET", Query: map[string]string{"id": id}, Role: "admin",
|
||||
})
|
||||
if st := out.JSON.(map[string]interface{})["state"]; st != "pending" {
|
||||
t.Fatalf("应为 pending, 实得 %v", st)
|
||||
}
|
||||
|
||||
// 4. 未授权内核动作时,接入源必须失败且**如实报告**(不是假装成功)
|
||||
out, err = ps.InvokeHandler("channels", "complete_login", &lua.HandlerRequest{
|
||||
Method: "POST", Body: `{"platform":"example","credential":"tok-1","account":"alice"}`, Role: "admin",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("complete_login: %v", err)
|
||||
}
|
||||
done := out.JSON.(map[string]interface{})
|
||||
if done["source_error"] == nil {
|
||||
t.Fatal("未授权 add_source 时应报告 source_error")
|
||||
}
|
||||
t.Logf("未授权时的如实报告: %v", done["source_error"])
|
||||
|
||||
// 凭据必须已在保险库(登录本身成功了)
|
||||
tok, ok, _ := ps.GetSecret("channels", "channels:example")
|
||||
if !ok || tok != "tok-1" {
|
||||
t.Fatalf("凭据应已存保险库: ok=%v tok=%q", ok, tok)
|
||||
}
|
||||
// 且不得出现在 state 里
|
||||
if stateHasSecret(ps, "channels", "tok-1") {
|
||||
t.Fatal("★ 凭据泄漏进了 plugin.state")
|
||||
}
|
||||
|
||||
// 5. 运维授权后,重新接入应当真的把源注册进内核
|
||||
if err := ps.SetPluginKernelPermission("channels",
|
||||
[]string{"add_source", "list_sources", "status"}, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
out, err = ps.InvokeHandler("channels", "complete_login", &lua.HandlerRequest{
|
||||
Method: "POST", Body: `{"platform":"example","credential":"tok-1","account":"alice"}`, Role: "admin",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("complete_login(授权后): %v", err)
|
||||
}
|
||||
done = out.JSON.(map[string]interface{})
|
||||
if done["source_error"] != nil {
|
||||
t.Fatalf("授权后仍失败: %v", done["source_error"])
|
||||
}
|
||||
if done["source"] != "channels-example" {
|
||||
t.Fatalf("源名不对: %v", done["source"])
|
||||
}
|
||||
|
||||
// 内核侧必须真的有这个源
|
||||
found := false
|
||||
for _, s := range c.Sources() {
|
||||
if s.Name == "channels-example" {
|
||||
found = true
|
||||
if s.BaseURL != "https://api.example.com/v1" {
|
||||
t.Errorf("base_url 未落库: %q", s.BaseURL)
|
||||
}
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatal("插件接入的源没有出现在内核源列表里")
|
||||
}
|
||||
// 平台声明的模型应可路由
|
||||
models := c.Registry().ModelList()
|
||||
hasModel := false
|
||||
for _, id := range models {
|
||||
if id == "example-chat" {
|
||||
hasModel = true
|
||||
}
|
||||
}
|
||||
if !hasModel {
|
||||
t.Errorf("平台模型未进入可路由列表: %v", models)
|
||||
}
|
||||
t.Logf("已接入源 channels-example,模型可见: %v", models)
|
||||
}
|
||||
|
||||
// TestChannelsPluginDeclaresItsNeeds 断言插件如实声明了它需要什么。
|
||||
//
|
||||
// 声明是运维审批的依据:插件不声明,设置页就无法显示"申请中",
|
||||
// 运维只能等某个功能静默失效后才发现。
|
||||
func TestChannelsPluginDeclaresItsNeeds(t *testing.T) {
|
||||
c := kernelTestCore(t)
|
||||
ps := c.Plugins()
|
||||
src, err := lua.ReadBundledPlugin("channels")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := ps.LoadSource("channels", src); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// 内核动作声明
|
||||
views := ps.KernelPermissions()
|
||||
var declared []string
|
||||
for _, v := range views {
|
||||
if v.Plugin == "channels" {
|
||||
declared = v.Declared
|
||||
}
|
||||
}
|
||||
if len(declared) == 0 {
|
||||
t.Fatal("channels 应声明它需要的内核动作")
|
||||
}
|
||||
for _, want := range []string{"add_source", "status"} {
|
||||
found := false
|
||||
for _, d := range declared {
|
||||
if d == want {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Errorf("应声明 %q, 实得 %v", want, declared)
|
||||
}
|
||||
}
|
||||
// 额外出站主机声明(非源 host,需单独批准)
|
||||
hosts := ps.PluginExtraHosts("channels")
|
||||
if len(hosts) == 0 {
|
||||
t.Fatal("channels 应声明额外出站主机")
|
||||
}
|
||||
t.Logf("声明内核动作=%v 额外主机=%v", declared, hosts)
|
||||
}
|
||||
|
||||
// TestChannelsPluginHandlersRequireStatus 是一个契约回归:插件的每个 handler
|
||||
// 都必须返回 {status,json/body},否则内核会**静默丢弃**它(实测踩过:
|
||||
// `return {url=…}` 得到 200 空响应,没有任何错误信息)。
|
||||
func TestChannelsPluginHandlersRequireStatus(t *testing.T) {
|
||||
c := kernelTestCore(t)
|
||||
ps := c.Plugins()
|
||||
src, err := lua.ReadBundledPlugin("channels")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := ps.LoadSource("channels", src); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_ = ps.SetPluginKernelPermission("channels", []string{"add_source", "status", "list_sources"}, false)
|
||||
|
||||
// 每个 handler 至少要在正常输入下给出非空响应
|
||||
cases := []struct {
|
||||
fn string
|
||||
req *lua.HandlerRequest
|
||||
what string
|
||||
}{
|
||||
{"list_platforms", &lua.HandlerRequest{Method: "GET"}, "平台列表"},
|
||||
{"status_page", &lua.HandlerRequest{Method: "GET"}, "状态页"},
|
||||
{"poll_login", &lua.HandlerRequest{Method: "GET", Query: map[string]string{"id": "nope"}}, "轮询(未知会话)"},
|
||||
{"start_login", &lua.HandlerRequest{Method: "POST", Body: `{"platform":"example"}`}, "发起登录"},
|
||||
{"start_login", &lua.HandlerRequest{Method: "POST", Body: `{"platform":"nope"}`}, "发起登录(未知平台)"},
|
||||
{"complete_login", &lua.HandlerRequest{Method: "POST", Body: `{}`}, "完成登录(缺字段)"},
|
||||
{"claim_daily", &lua.HandlerRequest{Method: "POST", Body: `{"platform":"example"}`}, "领额度(未登录)"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
out, err := ps.InvokeHandler("channels", tc.fn, tc.req)
|
||||
if err != nil {
|
||||
t.Errorf("%s (%s) 返回错误: %v", tc.fn, tc.what, err)
|
||||
continue
|
||||
}
|
||||
if out.JSON == nil && out.Body == "" {
|
||||
t.Errorf("%s (%s) 返回空响应 —— 内核会静默丢弃,调用方看不到任何原因",
|
||||
tc.fn, tc.what)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// jsonMarshalForTest 序列化一个接口值,用于检查凭据是否出现在 state 里。
|
||||
func jsonMarshalForTest(v interface{}) ([]byte, error) { return json.Marshal(v) }
|
||||
|
||||
// stateHasSecret 检查凭据是否泄漏进 plugin.state。
|
||||
func stateHasSecret(ps *lua.Plugins, plugin, secret string) bool {
|
||||
st := ps.State(plugin)
|
||||
if st == nil {
|
||||
return false
|
||||
}
|
||||
b, err := jsonMarshalForTest(st)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return contains(string(b), secret)
|
||||
}
|
||||
|
||||
// examplePlatformFixture 是端到端测试用的占位平台(对应此前内联在生产源码里
|
||||
// 的 "example" 平台)。它经 inject_platforms 注入,且**只在测试进程内存在** ——
|
||||
// 生产插件的 PLATFORMS 只含真实厂商。
|
||||
const examplePlatformFixture = `[
|
||||
{
|
||||
"id": "example",
|
||||
"name": "Example 平台(测试夹具)",
|
||||
"flow": "link-poll",
|
||||
"login_url": "https://auth.example.com/device",
|
||||
"base_url": "https://api.example.com/v1",
|
||||
"adapter": "openai",
|
||||
"models": ["example-chat", "example-reason"],
|
||||
"checkin": {"url": "https://api.example.com/v1/api/daily/claim", "method": "POST", "body": "{}"}
|
||||
}
|
||||
]`
|
||||
|
||||
// injectTestPlatforms 把测试夹具注入插件(运行时,不落盘)。
|
||||
func injectTestPlatforms(t *testing.T, ps *lua.Plugins, fixtureJSON string) {
|
||||
t.Helper()
|
||||
out, err := ps.InvokeHandler("channels", "inject_platforms", &lua.HandlerRequest{
|
||||
Method: "POST", Body: `{"platforms":` + fixtureJSON + `}`, Role: "admin",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("inject_platforms: %v", err)
|
||||
}
|
||||
m, _ := out.JSON.(map[string]interface{})
|
||||
if m["ok"] != true {
|
||||
t.Fatalf("注入测试平台失败: %v", m)
|
||||
}
|
||||
}
|
||||
|
||||
// TestInjectPlatformsRejectsBadInput 锁定注入机制的三条边界:
|
||||
// 1. 不得覆盖内建平台(注入不能劫持真实厂商)
|
||||
// 2. 重复 id 拒绝
|
||||
// 3. 空数组 = 清空注入(夹具不跨测试残留)
|
||||
//
|
||||
// 这个机制存在的理由:夹具内联进生产源码会让它们出现在生产 UI 的平台列表里
|
||||
// (真实踩过 —— 16 个平台里有 4 个是夹具)。
|
||||
func TestInjectPlatformsRejectsBadInput(t *testing.T) {
|
||||
c := kernelTestCore(t)
|
||||
ps := c.Plugins()
|
||||
src, err := lua.ReadBundledPlugin("channels")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := ps.LoadSource("channels", src); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
call := func(body string) map[string]interface{} {
|
||||
out, err := ps.InvokeHandler("channels", "inject_platforms", &lua.HandlerRequest{
|
||||
Method: "POST", Body: body, Role: "admin",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("inject_platforms(%s): %v", body, err)
|
||||
}
|
||||
return out.JSON.(map[string]interface{})
|
||||
}
|
||||
platforms := func() []interface{} {
|
||||
out, _ := ps.InvokeHandler("channels", "list_platforms", &lua.HandlerRequest{Method: "GET", Role: "admin"})
|
||||
return out.JSON.(map[string]interface{})["platforms"].([]interface{})
|
||||
}
|
||||
|
||||
base := len(platforms())
|
||||
if base == 0 {
|
||||
t.Fatal("生产目录应有真实厂商平台")
|
||||
}
|
||||
|
||||
// 注入一个合法夹具 → 列表 +1
|
||||
if m := call(`{"platforms":[{"id":"fixture-a","name":"A","flow":"paste","base_url":"https://a.example/v1"}]}`); m["ok"] != true {
|
||||
t.Fatalf("合法注入应成功: %v", m)
|
||||
}
|
||||
if got := len(platforms()); got != base+1 {
|
||||
t.Fatalf("注入后平台数 = %d, 期望 %d", got, base+1)
|
||||
}
|
||||
|
||||
// 1. 覆盖内建平台 → 拒绝
|
||||
if m := call(`{"platforms":[{"id":"buddy","name":"劫持"}]}`); m["error"] == nil {
|
||||
t.Fatal("覆盖内建平台必须被拒绝")
|
||||
}
|
||||
// 2. 重复 id → 拒绝
|
||||
if m := call(`{"platforms":[{"id":"fixture-b"},{"id":"fixture-b"}]}`); m["error"] == nil {
|
||||
t.Fatal("重复 id 必须被拒绝")
|
||||
}
|
||||
// 无 id → 拒绝
|
||||
if m := call(`{"platforms":[{"name":"no-id"}]}`); m["error"] == nil {
|
||||
t.Fatal("缺 id 必须被拒绝")
|
||||
}
|
||||
|
||||
// 3. 空数组 = 清空注入,回到基线
|
||||
if m := call(`{"platforms":[]}`); m["ok"] != true {
|
||||
t.Fatalf("清空应成功: %v", m)
|
||||
}
|
||||
if got := len(platforms()); got != base {
|
||||
t.Fatalf("清空后平台数 = %d, 期望回到基线 %d", got, base)
|
||||
}
|
||||
}
|
||||
418
internal/core/channels_pool_test.go
Normal file
418
internal/core/channels_pool_test.go
Normal file
@ -0,0 +1,418 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"llmsproxy/internal/lua"
|
||||
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
)
|
||||
|
||||
// channels_pool_test:对齐参考 account.* / model.* / provider.* 契约的行为测试。
|
||||
// 账号池增删改、上游源随 enabled/模型开关同步、供应商整体开关、备份导出。
|
||||
|
||||
func poolCore(t *testing.T) (*Core, *lua.Plugins) {
|
||||
t.Helper()
|
||||
c := kernelTestCore(t)
|
||||
ps := c.Plugins()
|
||||
src, err := readBundledChannels(t)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := ps.LoadSource("channels", src); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_ = ps.SetPluginKernelPermission("channels", []string{"add_source", "list_sources", "remove_source", "status", "probe_all", "reset_health"}, false)
|
||||
return c, ps
|
||||
}
|
||||
|
||||
// listSourcesViaKernel 直查内核 list_sources(actionListSources 返回 []map)。
|
||||
func listSourcesViaKernel(t *testing.T, c *Core) []map[string]interface{} {
|
||||
t.Helper()
|
||||
out, err := c.Invoke(nil, "list_sources", map[string]interface{}{})
|
||||
if err != nil {
|
||||
t.Fatalf("list_sources: %v", err)
|
||||
}
|
||||
if ms, ok := out.([]map[string]interface{}); ok {
|
||||
return ms
|
||||
}
|
||||
raw, _ := out.([]interface{})
|
||||
var srcs []map[string]interface{}
|
||||
for _, s := range raw {
|
||||
if m, ok := s.(map[string]interface{}); ok {
|
||||
srcs = append(srcs, m)
|
||||
}
|
||||
}
|
||||
return srcs
|
||||
}
|
||||
|
||||
func invoke(t *testing.T, ps *lua.Plugins, fn, body string) map[string]interface{} {
|
||||
t.Helper()
|
||||
out, err := ps.InvokeHandler("channels", fn, &lua.HandlerRequest{Method: "POST", Body: body})
|
||||
if err != nil {
|
||||
t.Fatalf("invoke %s: %v", fn, err)
|
||||
}
|
||||
return out.JSON.(map[string]interface{})
|
||||
}
|
||||
|
||||
// asArr 规整桥接后的数组。Lua 空表过桥是 map[string]interface{}(无法与空数组
|
||||
// 区分,见 vm.go luaTableToGoValue);内核 list_sources 的 models 过桥是 []string。
|
||||
func asArr(v interface{}) []interface{} {
|
||||
switch a := v.(type) {
|
||||
case []interface{}:
|
||||
return a
|
||||
case []string:
|
||||
out := make([]interface{}, len(a))
|
||||
for i, s := range a {
|
||||
out[i] = s
|
||||
}
|
||||
return out
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func srcHasAccount(srcs []map[string]interface{}, needle string) (map[string]interface{}, bool) {
|
||||
for _, sm := range srcs {
|
||||
if name, _ := sm["name"].(string); strings.Contains(name, needle) {
|
||||
return sm, true
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// TestChannelsAccountPoolBehavior:账号池 CRUD + 上游源同步 + 模型/供应商开关 + 备份。
|
||||
func TestChannelsAccountPoolBehavior(t *testing.T) {
|
||||
c, ps := poolCore(t)
|
||||
|
||||
t.Run("账号增删改", func(t *testing.T) {
|
||||
m := invoke(t, ps, "account_add", `{"platform":"minimax","nickname":"我的号"}`)
|
||||
if m["ok"] != true {
|
||||
t.Fatalf("account_add: %v", m)
|
||||
}
|
||||
aid, _ := m["account_id"].(string)
|
||||
if aid == "" {
|
||||
t.Fatalf("account_add 应返回 account_id: %v", m)
|
||||
}
|
||||
m = invoke(t, ps, "account_list", `{"platform":"minimax"}`)
|
||||
accts := asArr(m["accounts"])
|
||||
if len(accts) != 1 {
|
||||
t.Fatalf("应有1个账号: %d", len(accts))
|
||||
}
|
||||
a := accts[0].(map[string]interface{})
|
||||
if a["nickname"] != "我的号" || a["enabled"] != true {
|
||||
t.Fatalf("账号字段: %v", a)
|
||||
}
|
||||
invoke(t, ps, "account_update", `{"platform":"minimax","account_id":"`+aid+`","patch":{"nickname":"改名"}}`)
|
||||
m = invoke(t, ps, "account_list", `{"platform":"minimax"}`)
|
||||
if asArr(m["accounts"])[0].(map[string]interface{})["nickname"] != "改名" {
|
||||
t.Fatalf("改名失败")
|
||||
}
|
||||
invoke(t, ps, "account_delete", `{"platform":"minimax","account_id":"`+aid+`"}`)
|
||||
m = invoke(t, ps, "account_list", `{"platform":"minimax"}`)
|
||||
if n := len(asArr(m["accounts"])); n != 0 {
|
||||
t.Fatalf("删除后应为0, 实得 %d", n)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("模型开关同步上游源", func(t *testing.T) {
|
||||
m := invoke(t, ps, "account_add", `{"platform":"minimax","credential":"MM-KEY"}`)
|
||||
aid, _ := m["account_id"].(string)
|
||||
sm, ok := srcHasAccount(listSourcesViaKernel(t, c), aid)
|
||||
if !ok {
|
||||
t.Fatal("带凭据账号应已同步源")
|
||||
}
|
||||
if n := len(asArr(sm["models"])); n != 3 {
|
||||
t.Fatalf("源应有3模型, 实得 %d", n)
|
||||
}
|
||||
invoke(t, ps, "model_set_disabled", `{"platform":"minimax","model_id":"MiniMax-M3","disabled":true}`)
|
||||
sm, _ = srcHasAccount(listSourcesViaKernel(t, c), aid)
|
||||
if n := len(asArr(sm["models"])); n != 2 {
|
||||
t.Fatalf("关1模型后源应2模型, 实得 %d", n)
|
||||
}
|
||||
invoke(t, ps, "model_set_all_disabled", `{"platform":"minimax","disabled":true}`)
|
||||
m = invoke(t, ps, "provider_status", `{}`)
|
||||
for _, s := range asArr(m["statuses"]) {
|
||||
x := s.(map[string]interface{})
|
||||
if x["id"] == "minimax" && x["closed"] != true {
|
||||
t.Fatalf("全关模型后应 closed=true: %v", x)
|
||||
}
|
||||
}
|
||||
invoke(t, ps, "model_set_all_disabled", `{"platform":"minimax","disabled":false}`)
|
||||
})
|
||||
|
||||
t.Run("enabled开关同步上游源", func(t *testing.T) {
|
||||
m := invoke(t, ps, "account_list", `{"platform":"minimax"}`)
|
||||
accts := asArr(m["accounts"])
|
||||
if len(accts) == 0 {
|
||||
t.Fatal("上个用例应留有账号")
|
||||
}
|
||||
aid := accts[0].(map[string]interface{})["id"].(string)
|
||||
before := len(listSourcesViaKernel(t, c))
|
||||
invoke(t, ps, "account_update", `{"platform":"minimax","account_id":"`+aid+`","patch":{"enabled":false}}`)
|
||||
if n := len(listSourcesViaKernel(t, c)); n != before-1 {
|
||||
t.Fatalf("停用账号应删源: 前 %d 后 %d", before, n)
|
||||
}
|
||||
invoke(t, ps, "account_update", `{"platform":"minimax","account_id":"`+aid+`","patch":{"enabled":true}}`)
|
||||
if n := len(listSourcesViaKernel(t, c)); n != before {
|
||||
t.Fatalf("启用账号应重建源: 前 %d 后 %d", before, n)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("供应商整体开关", func(t *testing.T) {
|
||||
invoke(t, ps, "provider_set_enabled", `{"platform":"minimax","enabled":false}`)
|
||||
if _, ok := srcHasAccount(listSourcesViaKernel(t, c), "minimax"); ok {
|
||||
t.Fatal("关闭渠道后不应有 minimax 源")
|
||||
}
|
||||
m := invoke(t, ps, "provider_status", `{}`)
|
||||
for _, s := range asArr(m["statuses"]) {
|
||||
x := s.(map[string]interface{})
|
||||
if x["id"] == "minimax" && x["closed"] != true {
|
||||
t.Fatalf("关闭后应 closed: %v", x)
|
||||
}
|
||||
}
|
||||
invoke(t, ps, "provider_set_enabled", `{"platform":"minimax","enabled":true}`)
|
||||
if _, ok := srcHasAccount(listSourcesViaKernel(t, c), "minimax"); !ok {
|
||||
t.Fatal("开启渠道后应有 minimax 源")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("备份导出含凭据", func(t *testing.T) {
|
||||
invoke(t, ps, "account_add", `{"platform":"cline","credential":"CL-KEY","nickname":"cl1"}`)
|
||||
exp := invoke(t, ps, "backup_export", `{}`)
|
||||
payload, _ := exp["payload"].(map[string]interface{})
|
||||
if payload == nil {
|
||||
t.Fatal("backup_export 应返回 payload")
|
||||
}
|
||||
creds, _ := payload["credentials"].(map[string]interface{})
|
||||
found := false
|
||||
for _, v := range creds {
|
||||
if v == "CL-KEY" {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("备份应含凭据明文: %v", creds)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestChannelsExtendedRPCBehavior:对齐参考剩余 RPC 面(credits.status/balances、
|
||||
// account.reset/retest、model.setDisabledMany/clearDead、provider 排序、
|
||||
// gateway/backup.status、usage.badgePreference、onboarding)。
|
||||
func TestChannelsExtendedRPCBehavior(t *testing.T) {
|
||||
_, ps := poolCore(t)
|
||||
|
||||
t.Run("credits_balances_无凭据报错_有凭据解析", func(t *testing.T) {
|
||||
m := invoke(t, ps, "account_add", `{"platform":"minimax","nickname":"b1"}`)
|
||||
aid, _ := m["account_id"].(string)
|
||||
j := invoke(t, ps, "credits_balances", `{"platform":"minimax"}`)
|
||||
var row map[string]interface{}
|
||||
for _, a := range asArr(j["accounts"]) {
|
||||
x := a.(map[string]interface{})
|
||||
if x["accountId"] == aid {
|
||||
row = x
|
||||
}
|
||||
}
|
||||
if row == nil {
|
||||
t.Fatal("balances 应含该账号")
|
||||
}
|
||||
if row["balance"] != nil || row["error"] == nil {
|
||||
t.Fatalf("无凭据应 error 而非编造余额: %v", row)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("credits_status_无端点平台如实null", func(t *testing.T) {
|
||||
m := invoke(t, ps, "account_add", `{"platform":"opencode","nickname":"oc1"}`)
|
||||
aid, _ := m["account_id"].(string)
|
||||
j := invoke(t, ps, "credits_status", `{"platform":"opencode"}`)
|
||||
for _, a := range asArr(j["accounts"]) {
|
||||
x := a.(map[string]interface{})
|
||||
if x["accountId"] == aid && x["status"] != nil {
|
||||
t.Fatalf("opencode 无状态端点应 null: %v", x)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("account_reset_clears", func(t *testing.T) {
|
||||
invoke(t, ps, "account_add", `{"platform":"minimax","nickname":"r1"}`)
|
||||
j := invoke(t, ps, "account_reset", `{"platform":"minimax","account_id":"__all__"}`)
|
||||
if j["ok"] != true {
|
||||
t.Fatalf("resetAll: %v", j)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("model_set_disabled_many", func(t *testing.T) {
|
||||
invoke(t, ps, "model_set_disabled_many", `{"platform":"minimax","model_ids":["MiniMax-M3","MiniMax-M2.7-highspeed"],"disabled":true}`)
|
||||
j := invoke(t, ps, "model_list", `{"platform":"minimax"}`)
|
||||
off := 0
|
||||
for _, x := range asArr(j["models"]) {
|
||||
mm := x.(map[string]interface{})
|
||||
if mm["disabled"] == true {
|
||||
off++
|
||||
}
|
||||
}
|
||||
if off != 2 {
|
||||
t.Fatalf("批量关2模型后应2个disabled, 实得 %d", off)
|
||||
}
|
||||
invoke(t, ps, "model_set_disabled_many", `{"platform":"minimax","model_ids":["MiniMax-M3","MiniMax-M2.7-highspeed"],"disabled":false}`)
|
||||
})
|
||||
|
||||
t.Run("model_clear_dead_只清黑名单里已下线的", func(t *testing.T) {
|
||||
invoke(t, ps, "model_set_disabled", `{"platform":"minimax","model_id":"MiniMax-M3","disabled":true}`)
|
||||
j := invoke(t, ps, "model_clear_dead", `{"platform":"minimax"}`)
|
||||
if n := len(asArr(j["removed"])); n != 0 {
|
||||
t.Fatalf("在册模型不算死模型, 实得 %d", n)
|
||||
}
|
||||
// 手动放一个不在 models 表里的黑名单项
|
||||
invoke(t, ps, "model_set_disabled_many", `{"platform":"minimax","model_ids":["ghost-model"],"disabled":true}`)
|
||||
j = invoke(t, ps, "model_clear_dead", `{"platform":"minimax"}`)
|
||||
if n := len(asArr(j["removed"])); n != 1 {
|
||||
t.Fatalf("死模型应被清: %v", j)
|
||||
}
|
||||
invoke(t, ps, "model_set_disabled", `{"platform":"minimax","model_id":"MiniMax-M3","disabled":false}`)
|
||||
})
|
||||
|
||||
t.Run("provider_order", func(t *testing.T) {
|
||||
invoke(t, ps, "provider_set_order", `{"order":["minimax","trae","bogus"]}`)
|
||||
j := invoke(t, ps, "provider_get_order", `{}`)
|
||||
order := asArr(j["order"])
|
||||
if len(order) != 2 {
|
||||
t.Fatalf("bogus 应被过滤, 实得 %v", order)
|
||||
}
|
||||
if order[0] != "minimax" {
|
||||
t.Fatalf("顺序应保留: %v", order)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("backup_status", func(t *testing.T) {
|
||||
invoke(t, ps, "account_add", `{"platform":"minimax","nickname":"s1"}`)
|
||||
j := invoke(t, ps, "backup_status", `{}`)
|
||||
if n, _ := j["accounts"].(float64); n < 1 {
|
||||
t.Fatalf("backup_status 应统计账号: %v", j)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("badge_preference_非法值拒绝", func(t *testing.T) {
|
||||
j := invoke(t, ps, "usage_badge_preference", `{"preference":"bogus"}`)
|
||||
if j["error"] == nil {
|
||||
t.Fatalf("非法偏好应拒绝: %v", j)
|
||||
}
|
||||
j = invoke(t, ps, "usage_badge_preference", `{"preference":"credits"}`)
|
||||
if j["preference"] != "credits" {
|
||||
t.Fatalf("合法偏好应保存: %v", j)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("onboarding_不支持平台如实报", func(t *testing.T) {
|
||||
j := invoke(t, ps, "onboarding_status", `{"platform":"minimax"}`)
|
||||
if j["supported"] != false {
|
||||
t.Fatalf("minimax 无新人任务应 supported=false: %v", j)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("permanent_lock_读写", func(t *testing.T) {
|
||||
j := invoke(t, ps, "credits_permanent_lock", `{"platform":"buddy","locked":true}`)
|
||||
if j["locked"] != true {
|
||||
t.Fatalf("锁定应生效: %v", j)
|
||||
}
|
||||
j = invoke(t, ps, "credits_permanent_lock", `{"platform":"buddy"}`)
|
||||
if j["locked"] != true {
|
||||
t.Fatalf("只读应回显锁定: %v", j)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestBuddyBillingHeadersMatchReference 锁定 buddy 计费请求的头(逐行移植参考
|
||||
// checkinHeaders)。真实缺陷:实现只发 Authorization,上游直接 401 —— 修好后
|
||||
// 账号能读到凭据但余额仍失败,运维看到的仍是「拿不到账号信息」。
|
||||
func TestBuddyBillingHeadersMatchReference(t *testing.T) {
|
||||
_, ps := poolCore(t)
|
||||
|
||||
var got map[string]string
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
got = map[string]string{}
|
||||
for k := range r.Header {
|
||||
got[k] = r.Header.Get(k)
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
fmt.Fprint(w, `{"data":{"totalCredit":42}}`)
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
// 把 buddy 的余额/签到端点指到本地假上游
|
||||
invoke(t, ps, "set_endpoints", fmt.Sprintf(
|
||||
`{"platform":"buddy","balance":{"url":%q,"method":"POST","body":"{}"}}`, ts.URL))
|
||||
m := invoke(t, ps, "account_add", `{"platform":"buddy","credential":"BD-AT","nickname":"b"}`)
|
||||
if m["ok"] != true {
|
||||
t.Fatalf("account_add: %v", m)
|
||||
}
|
||||
invoke(t, ps, "credits_balances", `{"platform":"buddy"}`)
|
||||
|
||||
if got == nil {
|
||||
t.Fatal("余额请求未发出")
|
||||
}
|
||||
// 参考 checkinHeaders 的必备身份头(缺一个上游就 401)
|
||||
want := map[string]string{
|
||||
"Authorization": "Bearer BD-AT",
|
||||
"X-Domain": "copilot.tencent.com",
|
||||
"X-Product": "SaaS",
|
||||
"X-Product-Code": "codebuddy",
|
||||
"User-Agent": "CodeBuddyIDE/1.106.1",
|
||||
}
|
||||
for k, v := range want {
|
||||
if got[k] != v {
|
||||
t.Errorf("头 %s = %q, 期望 %q(buddy 计费端点缺它会被上游 401)", k, got[k], v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestBuddyBalanceParseDoubleNested 逐行锁定参考 fetchCreditBalance 的解析口径:
|
||||
// 响应是**双层嵌套** data.Response.Data.Accounts[](与签到端点的单层 data 不同,
|
||||
// 这是该接口最容易错的地方),且只累加**有效包**的 CycleCapacityRemain ——
|
||||
// 失效包服务端仍返回,并进总额会让数字虚高(参考实测 155.67 → 655.67)。
|
||||
func TestBuddyBalanceParseDoubleNested(t *testing.T) {
|
||||
_, ps := poolCore(t)
|
||||
|
||||
// 两个有效包 + 一个失效包(ExpiredTime 在 2020 年)
|
||||
body := `{"code":0,"msg":"OK","data":{"Response":{"Data":{"Accounts":[
|
||||
{"PackageName":"A","Status":1,"CycleCapacityRemain":100.5,"ExpiredTime":"2099-01-01 00:00:00"},
|
||||
{"PackageName":"B","Status":1,"CycleCapacityRemain":55.17000031,"ExpiredTime":"2099-01-01 00:00:00"},
|
||||
{"PackageName":"C","Status":1,"CycleCapacityRemain":500,"ExpiredTime":"2020-01-01 00:00:00"}
|
||||
]}}}}`
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
fmt.Fprint(w, body)
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
invoke(t, ps, "set_endpoints", fmt.Sprintf(
|
||||
`{"platform":"buddy","balance":{"url":%q,"method":"POST","body":"{}"}}`, ts.URL))
|
||||
invoke(t, ps, "account_add", `{"platform":"buddy","credential":"BD-AT","nickname":"b"}`)
|
||||
|
||||
j := invoke(t, ps, "credits_balances", `{"platform":"buddy"}`)
|
||||
var row map[string]interface{}
|
||||
for _, a := range asArr(j["accounts"]) {
|
||||
row = a.(map[string]interface{})
|
||||
}
|
||||
bal, _ := row["balance"].(map[string]interface{})
|
||||
if bal == nil {
|
||||
t.Fatalf("应解析出余额, 实得 error=%v", row["error"])
|
||||
}
|
||||
// 100.5 + 55.17 = 155.67(失效包的 500 不计入)
|
||||
if got, _ := bal["total"].(float64); got != 155.67 {
|
||||
t.Fatalf("余额 = %v, 期望 155.67(只累加有效包、两位小数)", bal["total"])
|
||||
}
|
||||
|
||||
// 单层 data(缺 Response 层)必须判为解析失败,不能当 0
|
||||
body = `{"code":0,"data":{"TotalDosage":609}}`
|
||||
j = invoke(t, ps, "credits_balances", `{"platform":"buddy"}`)
|
||||
for _, a := range asArr(j["accounts"]) {
|
||||
row = a.(map[string]interface{})
|
||||
}
|
||||
if row["balance"] != nil {
|
||||
t.Fatalf("双层缺失时应报错而非当 0(参考语义:区分「查不到」与「余额为 0」): %v", row)
|
||||
}
|
||||
}
|
||||
264
internal/core/channels_stress_test.go
Normal file
264
internal/core/channels_stress_test.go
Normal file
@ -0,0 +1,264 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"llmsproxy/internal/config"
|
||||
"llmsproxy/internal/lua"
|
||||
)
|
||||
|
||||
// mustBox 构造一个真 SecretBox(与运行期同一实现,密钥落 runtime dir)。
|
||||
func mustBox(t *testing.T, runtimeFile string) *config.SecretBox {
|
||||
t.Helper()
|
||||
box, err := config.NewSecretBox(runtimeFile)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return box
|
||||
}
|
||||
|
||||
// channelsCore 起一个带 channels 插件、可指定源码与 box 的核心(压力测试用)。
|
||||
func channelsCore(t *testing.T, src string) (*Core, *lua.Plugins) {
|
||||
t.Helper()
|
||||
c := kernelTestCore(t)
|
||||
ps := c.Plugins()
|
||||
if err := ps.LoadSource("channels", src); err != nil {
|
||||
t.Fatalf("加载 channels: %v", err)
|
||||
}
|
||||
ps.SetSecretBox(mustBox(t, filepath.Join(t.TempDir(), "runtime.json")))
|
||||
_ = ps.SetPluginKernelPermission("channels", []string{"add_source", "status", "list_sources"}, false)
|
||||
return c, ps
|
||||
}
|
||||
|
||||
// cookieishPlatformFixture 是压力测试用的占位平台(对应此前内联在生产源码里的
|
||||
// "cookieish")。夹具经运行时注入,生产目录只含真实厂商。
|
||||
const cookieishPlatformFixture = `[{
|
||||
"id": "cookieish",
|
||||
"name": "Cookie 平台(测试夹具)",
|
||||
"flow": "paste",
|
||||
"base_url": "https://api.cookieish.example/v1",
|
||||
"adapter": "openai",
|
||||
"models": ["c-model-1"],
|
||||
"checkin": {"url": "https://api.cookieish.example/v1/api/credit/claim", "method": "POST", "body": "{}"}
|
||||
}]`
|
||||
|
||||
// TestChannelsConcurrentLogins 压力:并发走 device-poll 登录。
|
||||
//
|
||||
// 插件是单 VM 单锁 —— 本测试验证的是**正确性下的并发**(会话互不串台、
|
||||
// 凭据互不可见、最终都 completed),真吞吐压力在 e2e 层(真二进制+HTTP 并发)。
|
||||
func TestChannelsConcurrentLogins(t *testing.T) {
|
||||
var hits int32
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.Path {
|
||||
case "/oauth2/device/code":
|
||||
n := atomic.AddInt32(&hits, 1)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
fmt.Fprintf(w, `{"device_code":"DEV-%d","user_code":"UC-%d","verification_uri":"https://example/activate","interval":1,"expires_in":600}`, n, n)
|
||||
case "/oauth2/token":
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
fmt.Fprint(w, `{"access_token":"TOK","refresh_token":"RF","nickname":"u"}`)
|
||||
default:
|
||||
w.WriteHeader(404)
|
||||
}
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
_, ps := channelsCore(t, mustChannelsSrc(t))
|
||||
// 把 minimax 端点指到假上游
|
||||
if _, err := ps.Call("channels", "set_endpoints", map[string]interface{}{
|
||||
"platform": "minimax",
|
||||
"device": map[string]interface{}{
|
||||
"code_url": ts.URL + "/oauth2/device/code",
|
||||
"token_url": ts.URL + "/oauth2/token",
|
||||
},
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
const rounds = 12
|
||||
var wg sync.WaitGroup
|
||||
var failures int32
|
||||
for i := 0; i < rounds; i++ {
|
||||
wg.Add(1)
|
||||
go func(n int) {
|
||||
defer wg.Done()
|
||||
out, err := ps.InvokeHandler("channels", "start_login", &lua.HandlerRequest{
|
||||
Method: "POST", Body: `{"platform":"minimax"}`,
|
||||
})
|
||||
if err != nil {
|
||||
t.Errorf("start_login #%d: %v", n, err)
|
||||
atomic.AddInt32(&failures, 1)
|
||||
return
|
||||
}
|
||||
j := out.JSON.(map[string]interface{})
|
||||
sess, _ := j["session"].(map[string]interface{})
|
||||
if sess == nil {
|
||||
t.Errorf("start_login #%d 无会话: %v", n, j)
|
||||
atomic.AddInt32(&failures, 1)
|
||||
return
|
||||
}
|
||||
sid := sess["id"].(string)
|
||||
for k := 0; k < 10; k++ {
|
||||
out2, _ := ps.InvokeHandler("channels", "poll_login", &lua.HandlerRequest{
|
||||
Method: "GET", Query: map[string]string{"id": sid},
|
||||
})
|
||||
m2 := out2.JSON.(map[string]interface{})
|
||||
if m2["state"] == "completed" {
|
||||
return
|
||||
}
|
||||
if m2["state"] == "failed" {
|
||||
t.Errorf("会话 #%d failed: %v", n, m2)
|
||||
atomic.AddInt32(&failures, 1)
|
||||
return
|
||||
}
|
||||
}
|
||||
t.Errorf("会话 #%d 10 次轮询未终态", n)
|
||||
atomic.AddInt32(&failures, 1)
|
||||
}(i)
|
||||
}
|
||||
wg.Wait()
|
||||
if failures != 0 {
|
||||
t.Fatalf("并发登录失败 %d 轮", failures)
|
||||
}
|
||||
}
|
||||
|
||||
// TestChannelsClaimStress 压力:同一平台并发领签到(幂等 + 无登录态丢失)。
|
||||
func TestChannelsClaimStress(t *testing.T) {
|
||||
var claims int32
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/v1/checkin" {
|
||||
atomic.AddInt32(&claims, 1)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
fmt.Fprint(w, `{"ok":true,"credits":10}`)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(404)
|
||||
fmt.Fprint(w, `{}`)
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
_, ps := channelsCore(t, mustChannelsSrc(t))
|
||||
injectTestPlatforms(t, ps, cookieishPlatformFixture)
|
||||
if _, err := ps.Call("channels", "set_endpoints", map[string]interface{}{
|
||||
"platform": "cookieish",
|
||||
"checkin": map[string]interface{}{"url": ts.URL + "/v1/checkin"},
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := ps.SetSecret("channels", "channels:cookieish", "PASTE-TOKEN"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
const n = 30
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < n; i++ {
|
||||
wg.Add(1)
|
||||
go func(k int) {
|
||||
defer wg.Done()
|
||||
out, err := ps.InvokeHandler("channels", "claim_daily", &lua.HandlerRequest{
|
||||
Method: "POST", Body: `{"platform":"cookieish"}`,
|
||||
})
|
||||
if err != nil {
|
||||
t.Errorf("claim #%d: %v", k, err)
|
||||
return
|
||||
}
|
||||
j := out.JSON.(map[string]interface{})
|
||||
if j["ok"] != true {
|
||||
t.Errorf("claim #%d 未成功: %v", k, j)
|
||||
}
|
||||
}(i)
|
||||
}
|
||||
wg.Wait()
|
||||
if got := atomic.LoadInt32(&claims); got != n {
|
||||
t.Errorf("上游应收到 %d 次领取, 实得 %d", n, got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestLegacyVaultPlaintextLoad 旧通路:老版本(无加密)写的明文库在新版本
|
||||
// (带 box)下必须能读 —— 凭据升级不丢。落盘则会在下次写时整体重封为密文。
|
||||
func TestLegacyVaultPlaintextLoad(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
ps := lua.NewPlugins(nil, dir)
|
||||
if err := ps.SetSecret("legacy", "channels:old", "PLAIN-OLD-TOKEN"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ps.SetSecretBox(mustBox(t, filepath.Join(dir, "runtime.json")))
|
||||
v, ok, err := ps.GetSecret("legacy", "channels:old")
|
||||
if err != nil || !ok || v != "PLAIN-OLD-TOKEN" {
|
||||
t.Fatalf("旧明文库读回失败: v=%q ok=%v err=%v", v, ok, err)
|
||||
}
|
||||
// 任意一次写触发整体重封
|
||||
if err := ps.SetSecret("legacy", "k2", "NEW"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
raw, err := os.ReadFile(filepath.Join(dir, ".secrets", "legacy.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.Contains(string(raw), "PLAIN-OLD-TOKEN") {
|
||||
t.Fatalf("★ 重封后旧明文仍在盘上:\n%s", raw)
|
||||
}
|
||||
if !strings.Contains(string(raw), "enc:v1:") {
|
||||
t.Fatalf("重封后应含密文:\n%s", raw)
|
||||
}
|
||||
}
|
||||
|
||||
// TestLegacySourceSealedConfig 旧通路:config.yaml 里明文 api_key 的旧配置,
|
||||
// 新版本加载后运行期拿到解密值、落盘被 seal(SealIfNeeded 语义)。
|
||||
func TestLegacySourceSealedConfig(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
cfgPath := filepath.Join(dir, "config.yaml")
|
||||
yaml := "listen: 127.0.0.1:0\n" +
|
||||
"gateway_keys:\n - sk-legacy-0001\n" +
|
||||
"runtime_file: " + filepath.Join(dir, "runtime.json") + "\n" +
|
||||
"adapter_dir: " + filepath.Join(dir, "adapters") + "\n" +
|
||||
"sources:\n" +
|
||||
" - name: legacy-src\n" +
|
||||
" base_url: https://legacy.example.com/v1\n" +
|
||||
" adapter: openai\n" +
|
||||
" api_key: PLAIN-LEGACY-KEY\n" +
|
||||
" max_concurrent: 2\n" +
|
||||
" models:\n" +
|
||||
" - id: m1\n" +
|
||||
" kind: chat\n" +
|
||||
" priority: 10\n"
|
||||
if err := os.WriteFile(cfgPath, []byte(yaml), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg, err := config.Load(cfgPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c, err := NewFromConfig(cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer c.Close()
|
||||
|
||||
got := c.Sources()
|
||||
if len(got) != 1 || got[0].APIKey != "PLAIN-LEGACY-KEY" {
|
||||
t.Fatalf("运行期 api_key 应解密: %#v", got)
|
||||
}
|
||||
raw, err := os.ReadFile(cfgPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.Contains(string(raw), "PLAIN-LEGACY-KEY") {
|
||||
t.Fatalf("★ 旧明文 api_key 升级后仍明文落盘:\n%s", raw)
|
||||
}
|
||||
if !strings.Contains(string(raw), "enc:v1:") {
|
||||
t.Fatalf("落盘应含 enc:v1 密文:\n%s", raw)
|
||||
}
|
||||
mk, err := os.ReadFile(filepath.Join(dir, "master.key"))
|
||||
if err != nil || len(mk) != 64 {
|
||||
t.Fatalf("master.key 应 64 hex, len=%d err=%v", len(mk), err)
|
||||
}
|
||||
}
|
||||
237
internal/core/channels_trae_test.go
Normal file
237
internal/core/channels_trae_test.go
Normal file
@ -0,0 +1,237 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"llmsproxy/internal/lua"
|
||||
)
|
||||
|
||||
// traeHarness:TRAE callback 流的测试台。假上游模拟 ExchangeToken;
|
||||
// 回调用 InvokeHandler 直打 oauth_callback(Query 表 = 平台重定向回来的参数)。
|
||||
type traeHarness struct {
|
||||
t *testing.T
|
||||
ps *lua.Plugins
|
||||
ts *httptest.Server
|
||||
exchHit int32
|
||||
exchOK int32 // 1=成功 0=失败(atomic)
|
||||
}
|
||||
|
||||
func newTraeHarness(t *testing.T) *traeHarness {
|
||||
t.Helper()
|
||||
h := &traeHarness{t: t, exchOK: 1}
|
||||
h.ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/cloudide/api/v3/trae/oauth/ExchangeToken" {
|
||||
w.WriteHeader(404)
|
||||
return
|
||||
}
|
||||
n := atomic.AddInt32(&h.exchHit, 1)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if atomic.LoadInt32(&h.exchOK) == 0 {
|
||||
w.WriteHeader(500)
|
||||
fmt.Fprint(w, `{"error":"exchange down"}`)
|
||||
return
|
||||
}
|
||||
// 校验请求体形状(对齐参考 exchangeTraeCallback 的请求构造)
|
||||
body, _ := io.ReadAll(r.Body)
|
||||
for _, want := range []string{`"ClientID"`, `"RefreshToken":"RT-FROM-CALLBACK"`, `"ClientSecret":"-"`} {
|
||||
if !strings.Contains(string(body), want) {
|
||||
t.Errorf("ExchangeToken 请求体缺 %s: %s", want, body)
|
||||
}
|
||||
}
|
||||
fmt.Fprintf(w, `{"Token":"ACCESS-%d","RefreshToken":"RT-NEW-%d"}`, n, n)
|
||||
}))
|
||||
t.Cleanup(h.ts.Close)
|
||||
|
||||
c := kernelTestCore(t)
|
||||
h.ps = c.Plugins()
|
||||
src, err := readBundledChannels(t)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := h.ps.LoadSource("channels", src); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_ = h.ps.SetPluginKernelPermission("channels", []string{"add_source", "status", "list_sources"}, false)
|
||||
// ExchangeToken 指到假上游(set_endpoints 既是运维能力也是测试缝)
|
||||
if _, err := h.ps.Call("channels", "set_endpoints", map[string]interface{}{
|
||||
"platform": "trae",
|
||||
"callback": map[string]interface{}{
|
||||
"exchange_url": h.ts.URL + "/cloudide/api/v3/trae/oauth/ExchangeToken",
|
||||
},
|
||||
}); err != nil {
|
||||
t.Fatalf("set_endpoints: %v", err)
|
||||
}
|
||||
return h
|
||||
}
|
||||
|
||||
// beginAndCallback 发起登录(拿 nonce)后直接打 oauth_callback。
|
||||
// cbQuery 是回调的 query(不含 state,内部补);值即平台重定向回来的原始参数。
|
||||
func (h *traeHarness) beginAndCallback(cbQuery map[string]string) (int, string) {
|
||||
out, err := h.ps.InvokeHandler("channels", "start_login", &lua.HandlerRequest{
|
||||
Method: "POST", Body: `{"platform":"trae"}`,
|
||||
})
|
||||
if err != nil {
|
||||
h.t.Fatal(err)
|
||||
}
|
||||
j, ok := out.JSON.(map[string]interface{})
|
||||
if !ok {
|
||||
h.t.Fatalf("start_login 返回非对象: %#v", out.JSON)
|
||||
}
|
||||
sess := j["session"].(map[string]interface{})
|
||||
nonce := sess["nonce"].(string)
|
||||
|
||||
q := map[string]string{"state": nonce}
|
||||
for k, v := range cbQuery {
|
||||
q[k] = v
|
||||
}
|
||||
res, err := h.ps.InvokeHandler("channels", "oauth_callback", &lua.HandlerRequest{
|
||||
Method: "GET", Query: q,
|
||||
})
|
||||
if err != nil {
|
||||
h.t.Fatal(err)
|
||||
}
|
||||
return res.Status, fmt.Sprintf("%v|%v", res.Body, res.JSON)
|
||||
}
|
||||
|
||||
// TestTraeCallbackBehavior 是 TRAE callback 流的行为对齐测试。
|
||||
//
|
||||
// 决策表逐行搬自参考实现 parseTraeCallbackDetailed + exchangeTraeCallback
|
||||
// (trae-oauth.ts)。断言的是行为(凭据来源 / 错误文案 / 上游是否被调),不是实现细节。
|
||||
func TestTraeCallbackBehavior(t *testing.T) {
|
||||
h := newTraeHarness(t)
|
||||
|
||||
t.Run("行1 refreshToken直传_ExchangeToken换新", func(t *testing.T) {
|
||||
atomic.StoreInt32(&h.exchOK, 1)
|
||||
st, body := h.beginAndCallback(map[string]string{
|
||||
"refreshToken": "RT-FROM-CALLBACK",
|
||||
"userInfo": `{"UserID":"u1","ScreenName":"张三","TenantID":"t1"}`,
|
||||
})
|
||||
if st != 200 {
|
||||
t.Fatalf("应 200: %d %s", st, body)
|
||||
}
|
||||
// 凭据必须是 ExchangeToken 的新 Token,不是回调原值
|
||||
v, _, _ := h.ps.GetSecret("channels", "channels:trae")
|
||||
if !strings.HasPrefix(v, "ACCESS-") {
|
||||
t.Fatalf("凭据应是 ExchangeToken 的新 access, 实得 %q", v)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("行2 无refreshToken_userJwtToken兜底_不打ExchangeToken", func(t *testing.T) {
|
||||
before := atomic.LoadInt32(&h.exchHit)
|
||||
st, body := h.beginAndCallback(map[string]string{
|
||||
"userJwt": `{"Token":"JWT-TOKEN-1","RefreshToken":""}`,
|
||||
})
|
||||
if st != 200 {
|
||||
t.Fatalf("应 200: %d %s", st, body)
|
||||
}
|
||||
v, _, _ := h.ps.GetSecret("channels", "channels:trae")
|
||||
if v != "JWT-TOKEN-1" {
|
||||
t.Fatalf("兜底凭据应是 userJwt.Token, 实得 %q", v)
|
||||
}
|
||||
if atomic.LoadInt32(&h.exchHit) != before {
|
||||
t.Fatal("无 refreshToken 不应调 ExchangeToken(参考实现分支2)")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("行3 只有code_PKCE流_明确报暂不支持", func(t *testing.T) {
|
||||
st, body := h.beginAndCallback(map[string]string{"code": "PKCE-CODE-1"})
|
||||
if st != 400 {
|
||||
t.Fatalf("PKCE 流应 400: %d %s", st, body)
|
||||
}
|
||||
if !strings.Contains(body, "PKCE") {
|
||||
t.Fatalf("错误应明确指向 PKCE 流, 实得 %s", body)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("行4 空回调_报缺失", func(t *testing.T) {
|
||||
st, body := h.beginAndCallback(map[string]string{})
|
||||
if st != 400 {
|
||||
t.Fatalf("空回调应 400: %d %s", st, body)
|
||||
}
|
||||
if !strings.Contains(body, "refreshToken") {
|
||||
t.Fatalf("错误应指出缺什么, 实得 %s", body)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("行5 ExchangeToken失败_透传阶段", func(t *testing.T) {
|
||||
atomic.StoreInt32(&h.exchOK, 0)
|
||||
defer atomic.StoreInt32(&h.exchOK, 1)
|
||||
st, body := h.beginAndCallback(map[string]string{"refreshToken": "RT-FROM-CALLBACK"})
|
||||
if st != 400 {
|
||||
t.Fatalf("exchange 失败应 400: %d %s", st, body)
|
||||
}
|
||||
if !strings.Contains(body, "ExchangeToken") {
|
||||
t.Fatalf("错误应指明 ExchangeToken 阶段, 实得 %s", body)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestTraeCallbackPoolPath 回归:callback 流的**池路径**(UI 真实流程)。
|
||||
//
|
||||
// 与 buddy poll_login 的池路径同一缺陷:complete_login 不带 account_id 时,
|
||||
// 凭据落到与占位账号无关的 `channels:<plat>` 键,池账号永远 has_credential=false。
|
||||
// 此前只有 device-poll / state-poll 分支修了,callback 分支是漏网。
|
||||
func TestTraeCallbackPoolPath(t *testing.T) {
|
||||
h := newTraeHarness(t)
|
||||
atomic.StoreInt32(&h.exchOK, 1)
|
||||
|
||||
// UI 流程:先建占位账号,再带 account_id 发起登录
|
||||
add, err := h.ps.InvokeHandler("channels", "account_add", &lua.HandlerRequest{
|
||||
Method: "POST", Body: `{"platform":"trae"}`,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
aid := add.JSON.(map[string]interface{})["account_id"].(string)
|
||||
|
||||
out, err := h.ps.InvokeHandler("channels", "start_login", &lua.HandlerRequest{
|
||||
Method: "POST", Body: fmt.Sprintf(`{"platform":"trae","account_id":%q}`, aid),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
nonce := out.JSON.(map[string]interface{})["session"].(map[string]interface{})["nonce"].(string)
|
||||
|
||||
st, body := h.invokeCallback(map[string]string{
|
||||
"state": nonce, "refreshToken": "RT-FROM-CALLBACK", "userInfo": `{"UserID":"u9","ScreenName":"池用户"}`,
|
||||
})
|
||||
if st != 200 {
|
||||
t.Fatalf("回调应 200: %d %s", st, body)
|
||||
}
|
||||
|
||||
// ① 凭据必须落在池账号的 ref
|
||||
v, ok, _ := h.ps.GetSecret("channels", "channels:trae:"+aid)
|
||||
if !ok || v == "" {
|
||||
t.Fatalf("★ callback 池路径凭据未落到 channels:trae:%s(用户报障的同一缺陷)", aid)
|
||||
}
|
||||
// ② account_list 必须报 has_credential=true
|
||||
lst, err := h.ps.InvokeHandler("channels", "account_list", &lua.HandlerRequest{
|
||||
Method: "POST", Body: `{"platform":"trae"}`,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, raw := range lst.JSON.(map[string]interface{})["accounts"].([]interface{}) {
|
||||
a := raw.(map[string]interface{})
|
||||
if a["id"] == aid && a["has_credential"] != true {
|
||||
t.Fatalf("has_credential 应为 true, 实得 %v", a["has_credential"])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// invokeCallback 直打 oauth_callback(Query 已含 state)。
|
||||
func (h *traeHarness) invokeCallback(q map[string]string) (int, string) {
|
||||
res, err := h.ps.InvokeHandler("channels", "oauth_callback", &lua.HandlerRequest{
|
||||
Method: "GET", Query: q,
|
||||
})
|
||||
if err != nil {
|
||||
h.t.Fatal(err)
|
||||
}
|
||||
return res.Status, fmt.Sprintf("%v|%v", res.Body, res.JSON)
|
||||
}
|
||||
@ -8,10 +8,10 @@ import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
@ -69,6 +69,11 @@ func NewFromConfig(cfg *config.Config) (*Core, error) {
|
||||
// to boot the gateway because a third-party .lua has a typo would be the
|
||||
// wrong trade.
|
||||
c.plugins = lua.NewPlugins(c.vm, cfg.PluginDir)
|
||||
// R4-c: hand plugins the kernel action surface. Injected as an interface
|
||||
// because lua must not import core (core depends on lua, not the reverse).
|
||||
// Wired BEFORE LoadDir so a plugin that calls an action during its own
|
||||
// __init/first hook already finds a working kernel rather than a nil one.
|
||||
c.plugins.SetKernelAPI(c)
|
||||
// Plugin outbound HTTP is scoped to the hosts of configured sources. Set
|
||||
// before LoadDir so a plugin that requests on its first hook already sees
|
||||
// the real allowlist rather than the deny-all placeholder.
|
||||
@ -126,6 +131,11 @@ func NewFromConfig(cfg *config.Config) (*Core, error) {
|
||||
// only appeared to work because seedKeys' Save() unsealed memory as a side
|
||||
// effect. Removing the redundant saves exposed the real ordering bug.
|
||||
cfg.AttachSecretBox(c.store.SecretBox())
|
||||
// Share that SAME box with the plugin credential vault: one master.key seals
|
||||
// config.yaml, the runtime store, AND every .secrets/*.json. Set here (not at
|
||||
// NewPlugins time) because the store — and thus the box — only exists now;
|
||||
// plugins write secrets only during login flows, well after this point.
|
||||
c.plugins.SetSecretBox(c.store.SecretBox())
|
||||
hadPlaintextSecrets, err := cfg.UnsealSecrets(c.store.SecretBox())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unseal secrets: %w", err)
|
||||
@ -650,11 +660,29 @@ func (c *Core) rebuildRegistry() error {
|
||||
// allowlist for plugin outbound HTTP. Unparseable URLs are skipped rather than
|
||||
// failing startup: a malformed base_url is already rejected when the provider is
|
||||
// built, and it must not take plugins down with it.
|
||||
//
|
||||
// A source may also imply a SECOND host. OpenDesign splits inference
|
||||
// (amr-link.open-design.ai) from billing (amr-api.open-design.ai), and the
|
||||
// quota plugin derives the billing host from the inference one — so the host it
|
||||
// asks for is not the host the operator configured, and the request would be
|
||||
// refused with a message that reads like a plugin defect. Adding the derived
|
||||
// host here is what makes the probe reachable.
|
||||
//
|
||||
// The grant stays scoped: only a source that IS on amr-link pulls in amr-api, so
|
||||
// a deployment with no OpenDesign source does not silently gain the entry.
|
||||
func sourceHosts(cfg *config.Config) []string {
|
||||
if cfg == nil {
|
||||
return nil
|
||||
}
|
||||
out := make([]string, 0, len(cfg.Sources))
|
||||
seen := make(map[string]bool, len(cfg.Sources))
|
||||
add := func(h string) {
|
||||
if h == "" || seen[h] {
|
||||
return
|
||||
}
|
||||
seen[h] = true
|
||||
out = append(out, h)
|
||||
}
|
||||
for _, s := range cfg.Sources {
|
||||
if s.BaseURL == "" {
|
||||
continue
|
||||
@ -663,11 +691,27 @@ func sourceHosts(cfg *config.Config) []string {
|
||||
if err != nil || u.Hostname() == "" {
|
||||
continue
|
||||
}
|
||||
out = append(out, u.Hostname())
|
||||
add(u.Hostname())
|
||||
add(deriveBillingHost(u.Hostname()))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// deriveBillingHost returns the billing host a configured source implies, or
|
||||
// the host unchanged when it implies none.
|
||||
//
|
||||
// Kept in Go as well as in the Lua adapter because the two must agree: Go builds
|
||||
// the allowlist and Lua builds the request URL, and a disagreement shows up as a
|
||||
// refused request rather than as an error naming the mismatch. The rule is the
|
||||
// full hostname, not a prefix — `amr-link.` as a prefix would also rewrite
|
||||
// amr-link.example.com into an OpenDesign address.
|
||||
func deriveBillingHost(host string) string {
|
||||
if host == "amr-link.open-design.ai" {
|
||||
return "amr-api.open-design.ai"
|
||||
}
|
||||
return host
|
||||
}
|
||||
|
||||
// buildAutoChain rebuilds the AUTO chain snapshot from config.yaml rules
|
||||
// against the current providers. Slot model ids are normalized to the exact
|
||||
// configured spelling (case-insensitive match), otherwise ModelFor would fall
|
||||
@ -840,6 +884,14 @@ func (c *Core) UploadAdapter(name, code string) error {
|
||||
if name == "" {
|
||||
return fmt.Errorf("adapter name required")
|
||||
}
|
||||
// Validate name SAFETY before touching the filesystem (R6). A path-traversal
|
||||
// name ("../openai") must be rejected BEFORE WriteFile, or it would already
|
||||
// have escaped the adapter dir by the time LoadAdapter re-checks. Built-in
|
||||
// names are allowed here: an admin uploading openai.lua is the documented
|
||||
// override, same as placing the file in adapter_dir by hand.
|
||||
if err := lua.ValidateAdapterName(name, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.MkdirAll(c.cfg.AdapterDir, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
@ -851,6 +903,13 @@ func (c *Core) UploadAdapter(name, code string) error {
|
||||
}
|
||||
|
||||
func (c *Core) RemoveAdapter(name string) error {
|
||||
// Validate the name so a traversal name cannot os.Remove a file outside the
|
||||
// adapter dir. A reserved (built-in) name is a legitimate delete target in
|
||||
// the running VM, so reserved-name rejection is NOT applied here — only the
|
||||
// path-safety character set is.
|
||||
if err := lua.ValidateAdapterName(name, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
path := filepath.Join(c.cfg.AdapterDir, name+".lua")
|
||||
_ = os.Remove(path)
|
||||
c.vm.RemoveAdapter(name)
|
||||
|
||||
180
internal/core/device_harness_test.go
Normal file
180
internal/core/device_harness_test.go
Normal file
@ -0,0 +1,180 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"llmsproxy/internal/lua"
|
||||
)
|
||||
|
||||
// readBundledChannels 读内置 channels 插件源(供行为/变异测试共用)。
|
||||
func readBundledChannels(t *testing.T) (string, error) {
|
||||
t.Helper()
|
||||
return lua.ReadBundledPlugin("channels")
|
||||
}
|
||||
|
||||
// deviceHarness 是 device-poll 决策表的可复用测试台:假上游(脚本化逐次响应)
|
||||
// + channels 插件加载指定源码 + minimax 端点指到假上游。
|
||||
// 行为测试(TestChannelsDevicePollBehavior)与变异测试共用它 —— 决策表只有
|
||||
// 一份,两处不会漂移。
|
||||
type deviceHarness struct {
|
||||
t *testing.T
|
||||
ps *lua.Plugins
|
||||
ts *httptest.Server
|
||||
step int32
|
||||
// script 持续被 HTTP handler 原子读取
|
||||
script atomic.Value // []scriptStep
|
||||
}
|
||||
|
||||
type scriptStep struct {
|
||||
http int
|
||||
body string
|
||||
}
|
||||
|
||||
func newDeviceHarness(t *testing.T, src string) *deviceHarness {
|
||||
t.Helper()
|
||||
h := &deviceHarness{t: t}
|
||||
h.script.Store([]scriptStep{})
|
||||
|
||||
h.ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.Path {
|
||||
case "/oauth2/device/code":
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
fmt.Fprint(w, `{"device_code":"DEV1","user_code":"ABCD-EFGH","verification_uri":"https://example/activate","interval":1,"expires_in":600}`)
|
||||
case "/oauth2/token":
|
||||
i := int(atomic.AddInt32(&h.step, 1)) - 1
|
||||
script := h.script.Load().([]scriptStep)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if i >= len(script) {
|
||||
w.WriteHeader(500)
|
||||
fmt.Fprint(w, `{"error":"unexpected"}`)
|
||||
return
|
||||
}
|
||||
if script[i].http != 0 && script[i].http != 200 {
|
||||
w.WriteHeader(script[i].http)
|
||||
}
|
||||
fmt.Fprint(w, script[i].body)
|
||||
default:
|
||||
w.WriteHeader(404)
|
||||
}
|
||||
}))
|
||||
t.Cleanup(h.ts.Close)
|
||||
|
||||
c := kernelTestCore(t)
|
||||
h.ps = c.Plugins()
|
||||
if err := h.ps.LoadSource("channels", src); err != nil {
|
||||
t.Fatalf("加载: %v", err)
|
||||
}
|
||||
if _, err := h.ps.Call("channels", "set_endpoints", map[string]interface{}{
|
||||
"platform": "minimax",
|
||||
"device": map[string]interface{}{
|
||||
"code_url": h.ts.URL + "/oauth2/device/code",
|
||||
"token_url": h.ts.URL + "/oauth2/token",
|
||||
},
|
||||
}); err != nil {
|
||||
t.Fatalf("set_endpoints: %v", err)
|
||||
}
|
||||
return h
|
||||
}
|
||||
|
||||
// setScript 设置 token 端点的逐次响应脚本。`400 {...}` 形式表示非 200。
|
||||
func (h *deviceHarness) setScript(steps ...string) {
|
||||
var out []scriptStep
|
||||
for _, b := range steps {
|
||||
code := 200
|
||||
switch {
|
||||
case strings.HasPrefix(b, "400 "):
|
||||
code, b = 400, b[4:]
|
||||
case strings.HasPrefix(b, "403 "):
|
||||
code, b = 403, b[4:]
|
||||
}
|
||||
out = append(out, scriptStep{http: code, body: b})
|
||||
}
|
||||
h.script.Store(out)
|
||||
atomic.StoreInt32(&h.step, 0)
|
||||
}
|
||||
|
||||
func (h *deviceHarness) begin() map[string]interface{} {
|
||||
out, err := h.ps.InvokeHandler("channels", "start_login", &lua.HandlerRequest{
|
||||
Method: "POST", Body: `{"platform":"minimax"}`,
|
||||
})
|
||||
if err != nil {
|
||||
h.t.Fatal(err)
|
||||
}
|
||||
return out.JSON.(map[string]interface{})
|
||||
}
|
||||
|
||||
func (h *deviceHarness) poll(id string) map[string]interface{} {
|
||||
out, _ := h.ps.InvokeHandler("channels", "poll_login", &lua.HandlerRequest{
|
||||
Method: "GET", Query: map[string]string{"id": id},
|
||||
})
|
||||
return out.JSON.(map[string]interface{})
|
||||
}
|
||||
|
||||
// runDecisionTable 跑完整 8 行决策表,全部符合预期返回 true。
|
||||
// 这是行为契约的单一事实来源:行为测试用它断言通过;变异测试用它杀变异体。
|
||||
func (h *deviceHarness) runDecisionTable() bool {
|
||||
ok := func(cond bool) bool { return cond }
|
||||
st := func(steps ...string) map[string]interface{} {
|
||||
h.setScript(steps...)
|
||||
j := h.begin()
|
||||
sid := j["session"].(map[string]interface{})["id"].(string)
|
||||
return h.poll(sid)
|
||||
}
|
||||
|
||||
// 行1: 200+pending → 等
|
||||
if j := st(`{"status":"pending"}`); j["state"] != "pending" {
|
||||
return false
|
||||
}
|
||||
// 行2: 200+slow_down → 等 + interval+5
|
||||
h.setScript(`{"status":"slow_down"}`)
|
||||
j := h.begin()
|
||||
sid := j["session"].(map[string]interface{})["id"].(string)
|
||||
if j2 := h.poll(sid); j2["state"] != "pending" {
|
||||
return false
|
||||
} else if iv, _ := j2["interval"].(float64); iv < 6 {
|
||||
return false
|
||||
}
|
||||
// 行3: 200+denied → failed,message 区分于通用错误(杀"denied 不识别"变异体)
|
||||
if j := st(`{"status":"denied"}`); j["state"] != "failed" {
|
||||
return false
|
||||
} else if msg, _ := j["message"].(string); !strings.Contains(msg, "用户拒绝") {
|
||||
return false
|
||||
}
|
||||
// 行4: 200+expired_token → failed,message 带过期字样
|
||||
if j := st(`{"status":"expired_token"}`); j["state"] != "failed" {
|
||||
return false
|
||||
} else if msg, _ := j["message"].(string); !strings.Contains(msg, "过期") {
|
||||
return false
|
||||
}
|
||||
// 行5: 400+authorization_pending → 等
|
||||
if j := st(`400 {"error":"authorization_pending"}`); j["state"] != "pending" {
|
||||
return false
|
||||
}
|
||||
// 行6: 403+slow_down → 等 + interval+5
|
||||
h.setScript(`403 {"error":"slow_down"}`)
|
||||
j = h.begin()
|
||||
sid = j["session"].(map[string]interface{})["id"].(string)
|
||||
if j2 := h.poll(sid); j2["state"] != "pending" {
|
||||
return false
|
||||
} else if iv, _ := j2["interval"].(float64); iv < 6 {
|
||||
return false
|
||||
}
|
||||
// 行7: 200+access_token → completed(需先授权 add_source)
|
||||
_ = h.ps.SetPluginKernelPermission("channels", []string{"add_source", "status", "list_sources"}, false)
|
||||
h.setScript(`{"access_token":"MM-ACCESS-1","refresh_token":"MM-RF-1","nickname":"mm-user"}`)
|
||||
j = h.begin()
|
||||
sid = j["session"].(map[string]interface{})["id"].(string)
|
||||
if j2 := h.poll(sid); j2["state"] != "completed" || j2["account"] != "mm-user" {
|
||||
return false
|
||||
}
|
||||
// 行8: 其它 error → failed 带文本
|
||||
if j := st(`{"error":"server_error"}`); j["state"] != "failed" {
|
||||
return false
|
||||
}
|
||||
return ok(true)
|
||||
}
|
||||
73
internal/core/device_mutate_test.go
Normal file
73
internal/core/device_mutate_test.go
Normal file
@ -0,0 +1,73 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestDevicePollMutants 是 device-poll 执行器的变异测试。
|
||||
//
|
||||
// 用户要求:移植要照参考实现的行为形成测试项,且要变异验证测试真的能杀掉
|
||||
// 「行为走样」的代码。每个用例把 channels 源里的一个决策分支做语义变异,
|
||||
// 断言决策表**会失败**(变异体被杀)。变异体幸存 = 该分支没有覆盖。
|
||||
func TestDevicePollMutants(t *testing.T) {
|
||||
src, err := readBundledChannels(t)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// 锚点会命中 deviceOneStep 里两处同形代码(workos 分支 + minimax 分支),
|
||||
// 全部替换:两个决策表(minimax + workos)都会杀,任一幸存即失败。
|
||||
muts := []struct {
|
||||
name string
|
||||
from string
|
||||
to string
|
||||
}{
|
||||
{
|
||||
name: "slow_down 不再加 interval",
|
||||
from: `sess.device_interval = (sess.device_interval or d.interval or 5) + step`,
|
||||
to: `sess.device_interval = (sess.device_interval or d.interval or 5)`,
|
||||
},
|
||||
{
|
||||
name: "denied 被当成继续等待",
|
||||
from: `if resp.status == 200 and (status == "denied" or status == "access_denied") then`,
|
||||
to: `if resp.status == 200 and (status == "denied_x" or status == "access_denied") then`,
|
||||
},
|
||||
{
|
||||
name: "expired_token 不被识别",
|
||||
from: `if resp.status == 200 and (status == "expired" or status == "expired_token") then`,
|
||||
to: `if resp.status == 200 and (status == "expired" or status == "expired_token_x") then`,
|
||||
},
|
||||
{
|
||||
name: "authorization_pending 被当成失败",
|
||||
from: `if ecode == "authorization_pending" then return false end`,
|
||||
to: `if ecode == "authorization_pending_x" then return false end`,
|
||||
},
|
||||
{
|
||||
name: "成功分支被禁用",
|
||||
from: `if resp.status == 200 and rec.access_token then`,
|
||||
to: `if resp.status == 200 and rec.access_token and false then`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, mu := range muts {
|
||||
t.Run(mu.name, func(t *testing.T) {
|
||||
if !strings.Contains(src, mu.from) {
|
||||
t.Fatalf("锚点丢失,先更新实现与锚点:%q", mu.from)
|
||||
}
|
||||
// 锚点在 deviceOneStep 的 workos/minimax 两个分支各有一份同形代码,
|
||||
// 全部替换:任一分支的变异体被任一决策表杀掉才算有覆盖。
|
||||
mutated := strings.ReplaceAll(src, mu.from, mu.to)
|
||||
if mutated == src {
|
||||
t.Fatal("变异未生效")
|
||||
}
|
||||
// 基准必须全过(测试自身有效),变异体必须被杀(有覆盖)
|
||||
if !newDeviceHarness(t, src).runDecisionTable() {
|
||||
t.Fatal("基准决策表未全过 —— 测试自身失效")
|
||||
}
|
||||
if newDeviceHarness(t, mutated).runDecisionTable() {
|
||||
t.Fatalf("★ 变异体幸存(%s)—— 该分支无覆盖", mu.name)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
396
internal/core/kernel_actions.go
Normal file
396
internal/core/kernel_actions.go
Normal file
@ -0,0 +1,396 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"llmsproxy/internal/config"
|
||||
"llmsproxy/internal/lua"
|
||||
)
|
||||
|
||||
// 内核动作实现(R4-c)
|
||||
//
|
||||
// 把 Core 的公开动作暴露给插件 Lua,走 lua.KernelAPI 接口注入(依赖方向是
|
||||
// core → lua,所以接口定义在 lua 包、实现放在这里)。
|
||||
//
|
||||
// # 为什么是"动作面"而不是"给插件一个 Core 指针"
|
||||
//
|
||||
// 直接暴露 *Core 会让插件能碰任何未导出的细节,而且每一次内核重构都会变成
|
||||
// 对插件作者的破坏性变更。动作面是一份**显式的契约**:每个动作有名字、危险级、
|
||||
// 超时预算,加动作是一次有意的行为,而不是顺手多导出一个方法。
|
||||
//
|
||||
// # 凭据边界
|
||||
//
|
||||
// 这里**不实现** list_keys / find_key。Core.ListKeys() 返回的是**明文**网关
|
||||
// 密钥,把它交给第三方 Lua 等于把部署的整个凭据面交出去。key 相关的读走掩码
|
||||
// 形态(create_key 的返回是唯一一次明文,且是"刚创建的那一个")。
|
||||
var _ lua.KernelAPI = (*Core)(nil)
|
||||
|
||||
// Actions implements lua.KernelAPI.
|
||||
//
|
||||
// The catalog lives in the lua package because permissions must be checkable even
|
||||
// when no Core is wired. This method exists so the interface is satisfied by the
|
||||
// real kernel rather than by a separate adapter type.
|
||||
func (c *Core) Actions() []lua.KernelAction { return lua.KernelActions() }
|
||||
|
||||
// Invoke implements lua.KernelAPI. It dispatches one action.
|
||||
//
|
||||
// Every action is expected to honour ctx. The caller (lua.callKernel) gives up on
|
||||
// a deadline and returns an error to the plugin, but a kernel action that keeps
|
||||
// running afterwards would be an invisible leak, so the long ones (probe_all)
|
||||
// pass ctx down into the network layer.
|
||||
func (c *Core) Invoke(ctx context.Context, name string, args map[string]interface{}) (interface{}, error) {
|
||||
switch name {
|
||||
// ---- 只读 ----
|
||||
case "status":
|
||||
return c.actionStatus(), nil
|
||||
case "list_sources":
|
||||
return c.actionListSources(), nil
|
||||
case "list_models":
|
||||
return c.actionListModels(), nil
|
||||
case "list_adapters":
|
||||
return c.actionListAdapters(), nil
|
||||
case "list_auto_rules":
|
||||
return c.actionListAutoRules(), nil
|
||||
case "adapter_owner":
|
||||
return c.actionAdapterOwner(args)
|
||||
|
||||
// ---- 写 ----
|
||||
case "reload":
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]interface{}{"ok": true}, c.Reload()
|
||||
case "reset_health":
|
||||
// ResetHealth already no-ops on a nil registry (it ranges over
|
||||
// Providers()), so this needs no guard beyond the nil check inside.
|
||||
c.ResetHealth()
|
||||
return map[string]interface{}{"ok": true}, nil
|
||||
case "probe_all":
|
||||
r := c.Registry()
|
||||
if r == nil {
|
||||
return nil, fmt.Errorf("the registry is not built yet")
|
||||
}
|
||||
r.ProbeAll(ctx)
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, fmt.Errorf("probe_all was cut short: %w", err)
|
||||
}
|
||||
return map[string]interface{}{"ok": true}, nil
|
||||
case "save_auto_rules":
|
||||
return c.actionSaveAutoRules(args)
|
||||
case "save_key_auto":
|
||||
return c.actionSaveKeyAuto(args)
|
||||
case "add_source":
|
||||
return c.actionAddSource(args)
|
||||
case "create_key":
|
||||
return c.actionCreateKey(args)
|
||||
|
||||
// ---- 破坏性 ----
|
||||
case "remove_source":
|
||||
return c.actionRemoveSource(args)
|
||||
case "delete_key":
|
||||
return c.actionDeleteKey(args)
|
||||
case "update_key":
|
||||
return c.actionUpdateKey(args)
|
||||
case "remove_adapter":
|
||||
return c.actionRemoveAdapter(args)
|
||||
case "set_plugin_enabled":
|
||||
return c.actionSetPluginEnabled(args)
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown kernel action %q", name)
|
||||
}
|
||||
}
|
||||
|
||||
// ---- 只读实现 ----
|
||||
|
||||
func (c *Core) actionStatus() interface{} {
|
||||
// A plugin can call this DURING LoadDir (its own __init or first hook), which
|
||||
// runs before rebuildRegistry builds the registry. Returning an empty status
|
||||
// rather than panicking keeps a load-time call from taking the process down;
|
||||
// the plugin sees an empty list and the next call gives real data.
|
||||
out := map[string]interface{}{
|
||||
"sources": []interface{}{},
|
||||
"auto_slots": []interface{}{},
|
||||
"default": c.cfg.DefaultModel,
|
||||
"ready": false,
|
||||
}
|
||||
if r := c.Registry(); r != nil {
|
||||
out["sources"] = r.Status()
|
||||
out["ready"] = true
|
||||
}
|
||||
if slots := c.AutoSlotStates(); slots != nil {
|
||||
out["auto_slots"] = slots
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// actionListSources returns the sources with api_key MASKED.
|
||||
//
|
||||
// Masking is not optional: a plugin has no legitimate need for another source's
|
||||
// key, and a plugin that wrote the source itself already has it in its own
|
||||
// credential vault.
|
||||
func (c *Core) actionListSources() interface{} {
|
||||
srcs := c.Sources()
|
||||
out := make([]map[string]interface{}, 0, len(srcs))
|
||||
for _, s := range srcs {
|
||||
models := make([]string, 0, len(s.Models))
|
||||
for _, m := range s.Models {
|
||||
models = append(models, m.ID)
|
||||
}
|
||||
out = append(out, map[string]interface{}{
|
||||
"name": s.Name,
|
||||
"base_url": s.BaseURL,
|
||||
"adapter": s.Adapter,
|
||||
"models": models,
|
||||
"has_key": s.APIKey != "" || s.APIKeyEnv != "",
|
||||
"key_hint": maskSecretHint(s.APIKey),
|
||||
"max_concurrent": s.MaxConcurrent,
|
||||
})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// maskSecretHint shows only a short suffix so an operator (and a plugin) can tell
|
||||
// two keys apart without either getting the secret.
|
||||
func maskSecretHint(k string) string {
|
||||
if k == "" {
|
||||
return ""
|
||||
}
|
||||
if len(k) <= 4 {
|
||||
return "***"
|
||||
}
|
||||
return "***" + k[len(k)-4:]
|
||||
}
|
||||
|
||||
func (c *Core) actionListModels() interface{} {
|
||||
if r := c.Registry(); r != nil {
|
||||
return r.ModelList()
|
||||
}
|
||||
return []string{}
|
||||
}
|
||||
|
||||
func (c *Core) actionListAdapters() interface{} {
|
||||
return c.ListAdapters()
|
||||
}
|
||||
|
||||
func (c *Core) actionListAutoRules() interface{} {
|
||||
return map[string]interface{}{
|
||||
"auto": c.AutoRules(),
|
||||
"auto_image": c.AutoImageRules(),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Core) actionAdapterOwner(args map[string]interface{}) (interface{}, error) {
|
||||
name, _ := args["name"].(string)
|
||||
if name == "" {
|
||||
return nil, fmt.Errorf("adapter_owner requires a name")
|
||||
}
|
||||
owner := ""
|
||||
if ps := c.Plugins(); ps != nil {
|
||||
owner = ps.AdapterOwnerOf(name)
|
||||
}
|
||||
return map[string]interface{}{
|
||||
"name": name,
|
||||
"owner": owner, // "" means operator-installed
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ---- 写实现 ----
|
||||
|
||||
func (c *Core) actionSaveAutoRules(args map[string]interface{}) (interface{}, error) {
|
||||
entries, err := decodeScopes(args["entries"])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := c.SaveAutoRules(entries); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]interface{}{"ok": true, "count": len(entries)}, nil
|
||||
}
|
||||
|
||||
func (c *Core) actionSaveKeyAuto(args map[string]interface{}) (interface{}, error) {
|
||||
keyName, _ := args["key_name"].(string)
|
||||
if keyName == "" {
|
||||
return nil, fmt.Errorf("save_key_auto requires key_name")
|
||||
}
|
||||
entries, err := decodeScopes(args["entries"])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := c.SaveKeyAuto(keyName, entries); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]interface{}{"ok": true, "count": len(entries)}, nil
|
||||
}
|
||||
|
||||
// actionAddSource is the capability that makes "plugin-driven upstream" real: a
|
||||
// plugin that has just obtained credentials can wire its endpoint in as a source
|
||||
// without the operator editing config.yaml.
|
||||
func (c *Core) actionAddSource(args map[string]interface{}) (interface{}, error) {
|
||||
raw, ok := args["source"]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("add_source requires a source table")
|
||||
}
|
||||
b, err := json.Marshal(raw)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("source is not encodable: %w", err)
|
||||
}
|
||||
var src config.Source
|
||||
if err := json.Unmarshal(b, &src); err != nil {
|
||||
return nil, fmt.Errorf("source shape is invalid: %w", err)
|
||||
}
|
||||
if strings.TrimSpace(src.Name) == "" {
|
||||
return nil, fmt.Errorf("source requires a name")
|
||||
}
|
||||
if strings.TrimSpace(src.BaseURL) == "" {
|
||||
return nil, fmt.Errorf("source %s requires a base_url", src.Name)
|
||||
}
|
||||
if err := c.AddSource(src); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]interface{}{"ok": true, "name": src.Name}, nil
|
||||
}
|
||||
|
||||
// actionCreateKey creates a gateway key.
|
||||
//
|
||||
// The plaintext key is returned ONCE, in this response — the same posture as the
|
||||
// admin API. It is not retrievable later, so a plugin that needs it must store it
|
||||
// in its own credential vault immediately.
|
||||
func (c *Core) actionCreateKey(args map[string]interface{}) (interface{}, error) {
|
||||
name, _ := args["name"].(string)
|
||||
role, _ := args["role"].(string)
|
||||
if role == "" {
|
||||
role = "user"
|
||||
}
|
||||
models, err := decodeScopes(args["models"])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
k, err := c.CreateKey(name, role, models, "created by plugin")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]interface{}{
|
||||
"key": k.Key,
|
||||
"name": k.Name,
|
||||
"role": k.Role,
|
||||
"models": k.Models,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ---- 破坏性实现 ----
|
||||
|
||||
func (c *Core) actionRemoveSource(args map[string]interface{}) (interface{}, error) {
|
||||
name, _ := args["name"].(string)
|
||||
if name == "" {
|
||||
return nil, fmt.Errorf("remove_source requires a name")
|
||||
}
|
||||
// The last source is not removable: an empty registry turns every request
|
||||
// into a 503, and the operator would have to fix it from the filesystem.
|
||||
if len(c.Sources()) <= 1 {
|
||||
return nil, fmt.Errorf("refusing to remove the last source: the gateway would have nothing to route to")
|
||||
}
|
||||
if err := c.RemoveSource(name); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]interface{}{"ok": true, "name": name}, nil
|
||||
}
|
||||
|
||||
func (c *Core) actionDeleteKey(args map[string]interface{}) (interface{}, error) {
|
||||
key, _ := args["key"].(string)
|
||||
if key == "" {
|
||||
return nil, fmt.Errorf("delete_key requires the key to delete")
|
||||
}
|
||||
ok, err := c.DeleteKey(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("no such key")
|
||||
}
|
||||
return map[string]interface{}{"ok": true}, nil
|
||||
}
|
||||
|
||||
func (c *Core) actionUpdateKey(args map[string]interface{}) (interface{}, error) {
|
||||
key, _ := args["key"].(string)
|
||||
if key == "" {
|
||||
return nil, fmt.Errorf("update_key requires the key")
|
||||
}
|
||||
name, _ := args["name"].(string)
|
||||
role, _ := args["role"].(string)
|
||||
models, err := decodeScopes(args["models"])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
k, err := c.UpdateKey(key, name, role, models, "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]interface{}{"ok": true, "name": k.Name, "role": k.Role}, nil
|
||||
}
|
||||
|
||||
// actionRemoveAdapter deletes an adapter, but only one the CALLER plugin owns —
|
||||
// the ownership check lives in Plugins.RemoveAdapter (see the adapter bridge).
|
||||
// A plugin can always remove its own; removing an operator's or a built-in is
|
||||
// refused there, and that refusal is the security property.
|
||||
func (c *Core) actionRemoveAdapter(args map[string]interface{}) (interface{}, error) {
|
||||
name, _ := args["name"].(string)
|
||||
if name == "" {
|
||||
return nil, fmt.Errorf("remove_adapter requires a name")
|
||||
}
|
||||
ps := c.Plugins()
|
||||
if ps == nil {
|
||||
return nil, fmt.Errorf("no plugins loaded")
|
||||
}
|
||||
plugin, _ := args["plugin"].(string)
|
||||
if plugin == "" {
|
||||
return nil, fmt.Errorf("remove_adapter needs the calling plugin's name")
|
||||
}
|
||||
if err := ps.RemoveAdapter(plugin, name); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]interface{}{"ok": true, "name": name}, nil
|
||||
}
|
||||
|
||||
func (c *Core) actionSetPluginEnabled(args map[string]interface{}) (interface{}, error) {
|
||||
name, _ := args["name"].(string)
|
||||
if name == "" {
|
||||
return nil, fmt.Errorf("set_plugin_enabled requires a name")
|
||||
}
|
||||
enabled, ok := args["enabled"].(bool)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("set_plugin_enabled requires enabled (boolean)")
|
||||
}
|
||||
ps := c.Plugins()
|
||||
if ps == nil {
|
||||
return nil, fmt.Errorf("no plugins loaded")
|
||||
}
|
||||
if err := ps.SetEnabled(name, enabled); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]interface{}{"ok": true, "name": name, "enabled": enabled}, nil
|
||||
}
|
||||
|
||||
// decodeScopes converts a Lua-decoded array of model-scope tables into
|
||||
// []config.ModelScope. It accepts both an explicit array and a single table so a
|
||||
// one-entry chain does not need a wrapper.
|
||||
func decodeScopes(v interface{}) ([]config.ModelScope, error) {
|
||||
if v == nil {
|
||||
return nil, nil
|
||||
}
|
||||
b, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("entries are not encodable: %w", err)
|
||||
}
|
||||
var list []config.ModelScope
|
||||
if err := json.Unmarshal(b, &list); err == nil {
|
||||
return list, nil
|
||||
}
|
||||
var one config.ModelScope
|
||||
if err := json.Unmarshal(b, &one); err != nil {
|
||||
return nil, fmt.Errorf("entries shape is invalid: %w", err)
|
||||
}
|
||||
return []config.ModelScope{one}, nil
|
||||
}
|
||||
336
internal/core/kernel_actions_test.go
Normal file
336
internal/core/kernel_actions_test.go
Normal file
@ -0,0 +1,336 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"llmsproxy/internal/config"
|
||||
"llmsproxy/internal/lua"
|
||||
)
|
||||
|
||||
// 本文件验证 R4-c/R4-d 的闭环:**插件自己驱动外部源的链接认证,然后把源接进内核**。
|
||||
//
|
||||
// 这是用户点明的"同类插件"能力(对照 dsh-our-free-model):插件主导 OAuth 登录、
|
||||
// 存凭据、定时领额度、把拿到的端点注册成内核上游。此前 llmsproxy 的插件只能管
|
||||
// 自己的适配器,够不到内核。
|
||||
|
||||
// kernelTestCore 起一个最小 core(一个源 + 一个 key)。
|
||||
func kernelTestCore(t *testing.T) *Core {
|
||||
t.Helper()
|
||||
dir := t.TempDir()
|
||||
cfg := &config.Config{
|
||||
Path: filepath.Join(dir, "config.yaml"),
|
||||
AdapterDir: filepath.Join(dir, "adapters"),
|
||||
PluginDir: filepath.Join(dir, "plugins"),
|
||||
RuntimeFile: filepath.Join(dir, "runtime.json"),
|
||||
DefaultModel: "AUTO",
|
||||
GatewayKeys: []string{"sk-kernel-0001"},
|
||||
Sources: []config.Source{{
|
||||
Name: "primary", BaseURL: "http://127.0.0.1:1/v1", Adapter: "openai",
|
||||
APIKey: "secret-primary-key", MaxConcurrent: 1,
|
||||
Models: []config.Model{{ID: "m1", Kind: "chat"}},
|
||||
}},
|
||||
}
|
||||
c, err := NewFromConfig(cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(c.Close)
|
||||
return c
|
||||
}
|
||||
|
||||
// grant 给插件授权若干动作(模拟运维在设置页点选)。
|
||||
func grant(t *testing.T, c *Core, plugin string, actions ...string) {
|
||||
t.Helper()
|
||||
if err := c.Plugins().SetPluginKernelPermission(plugin, actions, false); err != nil {
|
||||
t.Fatalf("grant %v to %s: %v", actions, plugin, err)
|
||||
}
|
||||
}
|
||||
|
||||
const kernelProbePlugin = `
|
||||
local plugin = { name = "kernelprobe", version = "1.0" }
|
||||
plugin.exports = { "read_status", "read_models", "read_sources", "try_delete_key", "probe_count", "call_reload" }
|
||||
plugin.kernel = { "status", "list_models", "reload" }
|
||||
|
||||
function plugin.read_status()
|
||||
local st, err = __llmsproxy_kernel.call("status", {})
|
||||
if err then return { err = err } end
|
||||
return { ready = st.ready, nsources = #(st.sources or {}) }
|
||||
end
|
||||
|
||||
function plugin.read_models()
|
||||
local m, err = __llmsproxy_kernel.call("list_models", {})
|
||||
if err then return { err = err } end
|
||||
return { models = m }
|
||||
end
|
||||
|
||||
-- sources 动作返回的密钥必须是掩码的
|
||||
function plugin.read_sources()
|
||||
local s, err = __llmsproxy_kernel.call("list_sources", {})
|
||||
if err then return { err = err } end
|
||||
return { sources = s }
|
||||
end
|
||||
|
||||
-- 未授权的破坏性动作必须被拒
|
||||
function plugin.try_delete_key()
|
||||
local _, err = __llmsproxy_kernel.call("delete_key", { key = "sk-kernel-0001" })
|
||||
return { err = err }
|
||||
end
|
||||
|
||||
-- 授权数量可见(插件据此优雅降级)
|
||||
function plugin.probe_count()
|
||||
local acts = __llmsproxy_kernel.actions()
|
||||
local granted = 0
|
||||
for _, a in ipairs(acts) do
|
||||
if a.granted then granted = granted + 1 end
|
||||
end
|
||||
return { total = #acts, granted = granted }
|
||||
end
|
||||
|
||||
function plugin.call_reload()
|
||||
local r, err = __llmsproxy_kernel.call("reload", {})
|
||||
return { ok = r and r.ok, err = err }
|
||||
end
|
||||
return plugin
|
||||
`
|
||||
|
||||
// TestPluginCallsKernelActions 验证插件能调内核动作,且权限边界成立。
|
||||
func TestPluginCallsKernelActions(t *testing.T) {
|
||||
c := kernelTestCore(t)
|
||||
if err := c.Plugins().LoadSource("kernelprobe", kernelProbePlugin); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
grant(t, c, "kernelprobe", "status", "list_models", "list_sources")
|
||||
|
||||
// 只读动作可用
|
||||
out, err := c.Plugins().Call("kernelprobe", "read_status", map[string]interface{}{})
|
||||
if err != nil {
|
||||
t.Fatalf("read_status: %v", err)
|
||||
}
|
||||
m := out.(map[string]interface{})
|
||||
if m["err"] != nil {
|
||||
t.Fatalf("status 调用失败: %v", m["err"])
|
||||
}
|
||||
if m["ready"] != true {
|
||||
t.Errorf("registry 应已就绪: %#v", m)
|
||||
}
|
||||
if n, _ := m["nsources"].(float64); n < 1 {
|
||||
t.Errorf("应至少有 1 个源, 实得 %v", m["nsources"])
|
||||
}
|
||||
|
||||
// 模型列表
|
||||
out, err = c.Plugins().Call("kernelprobe", "read_models", map[string]interface{}{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if models, ok := out.(map[string]interface{})["models"].([]interface{}); !ok || len(models) == 0 {
|
||||
t.Errorf("模型列表应为非空数组: %#v", out)
|
||||
}
|
||||
}
|
||||
|
||||
// TestKernelActionMasksSourceKeys 是安全断言:list_sources **不得**泄漏 api_key。
|
||||
func TestKernelActionMasksSourceKeys(t *testing.T) {
|
||||
c := kernelTestCore(t)
|
||||
if err := c.Plugins().LoadSource("kernelprobe", kernelProbePlugin); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
grant(t, c, "kernelprobe", "list_sources")
|
||||
|
||||
out, err := c.Plugins().Call("kernelprobe", "read_sources", map[string]interface{}{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// 把整个返回值序列化后检查:明文密钥绝不能出现在里面
|
||||
b, _ := json.Marshal(out)
|
||||
if s := string(b); json.Valid(b) && contains(s, "secret-primary-key") {
|
||||
t.Fatalf("list_sources 泄漏了源的明文 api_key: %s", s)
|
||||
}
|
||||
// 但应有可区分性(has_key + 掩码后缀)
|
||||
if !contains(string(b), "***") {
|
||||
t.Errorf("应给出掩码提示而不是什么都没有: %s", string(b))
|
||||
}
|
||||
}
|
||||
|
||||
// TestKernelActionDeniedWithoutGrant 是权限默认拒绝的断言。
|
||||
func TestKernelActionDeniedWithoutGrant(t *testing.T) {
|
||||
c := kernelTestCore(t)
|
||||
if err := c.Plugins().LoadSource("kernelprobe", kernelProbePlugin); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// 故意不授权任何动作
|
||||
out, err := c.Plugins().Call("kernelprobe", "try_delete_key", map[string]interface{}{})
|
||||
if err != nil {
|
||||
t.Fatalf("调用本身不该报错(拒绝应作为返回值): %v", err)
|
||||
}
|
||||
m := out.(map[string]interface{})
|
||||
if m["err"] == nil || m["err"] == "" {
|
||||
t.Fatal("未授权的 delete_key 必须被拒")
|
||||
}
|
||||
t.Logf("拒绝信息: %v", m["err"])
|
||||
// 拒绝信息要点名动作与插件(否则运维无法行动)
|
||||
msg, _ := m["err"].(string)
|
||||
if !contains(msg, "delete_key") || !contains(msg, "kernelprobe") {
|
||||
t.Errorf("拒绝信息应点名动作与插件, 实得 %q", msg)
|
||||
}
|
||||
// 且密钥必须还在
|
||||
if _, ok := c.FindKey("sk-kernel-0001"); !ok {
|
||||
t.Fatal("被拒的动作不应真的执行")
|
||||
}
|
||||
}
|
||||
|
||||
// TestKernelActionsReportGranted 插件能看到自己拿到了哪些权限。
|
||||
func TestKernelActionsReportGranted(t *testing.T) {
|
||||
c := kernelTestCore(t)
|
||||
if err := c.Plugins().LoadSource("kernelprobe", kernelProbePlugin); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
grant(t, c, "kernelprobe", "status", "list_models")
|
||||
|
||||
out, err := c.Plugins().Call("kernelprobe", "probe_count", map[string]interface{}{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m := out.(map[string]interface{})
|
||||
total, _ := m["total"].(float64)
|
||||
granted, _ := m["granted"].(float64)
|
||||
if total < 15 {
|
||||
t.Errorf("动作总数应 >=15, 实得 %v", total)
|
||||
}
|
||||
if granted != 2 {
|
||||
t.Errorf("授权数应为 2(status+list_models), 实得 %v", granted)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPluginRegistersSourceIntoKernel 是"同类插件"的核心闭环:
|
||||
// 插件通过内核动作把一个新的外部源接进网关。
|
||||
func TestPluginRegistersSourceIntoKernel(t *testing.T) {
|
||||
c := kernelTestCore(t)
|
||||
code := `
|
||||
local plugin = { name = "upstream", version = "1.0" }
|
||||
plugin.exports = { "add_my_source" }
|
||||
plugin.kernel = { "add_source", "list_sources" }
|
||||
|
||||
function plugin.add_my_source(args)
|
||||
local src = {
|
||||
name = "plugin-provided",
|
||||
base_url = "http://127.0.0.1:9/v1",
|
||||
adapter = "openai",
|
||||
api_key = "plugin-managed-token",
|
||||
max_concurrent = 2,
|
||||
models = { { id = "plugin-model", kind = "chat", priority = 10 } },
|
||||
}
|
||||
local r, err = __llmsproxy_kernel.call("add_source", { source = src })
|
||||
if err then return { err = err } end
|
||||
return { ok = r.ok, name = r.name }
|
||||
end
|
||||
return plugin
|
||||
`
|
||||
if err := c.Plugins().LoadSource("upstream", code); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
grant(t, c, "upstream", "add_source", "list_sources")
|
||||
|
||||
out, err := c.Plugins().Call("upstream", "add_my_source", map[string]interface{}{})
|
||||
if err != nil {
|
||||
t.Fatalf("add_source: %v", err)
|
||||
}
|
||||
m := out.(map[string]interface{})
|
||||
if m["err"] != nil {
|
||||
t.Fatalf("插件注册源失败: %v", m["err"])
|
||||
}
|
||||
if m["name"] != "plugin-provided" {
|
||||
t.Fatalf("返回的源名不对: %#v", m)
|
||||
}
|
||||
|
||||
// 内核侧必须真的有了这个源 —— 这是"接进网关"的定义
|
||||
found := false
|
||||
for _, s := range c.Sources() {
|
||||
if s.Name == "plugin-provided" {
|
||||
found = true
|
||||
if s.BaseURL != "http://127.0.0.1:9/v1" {
|
||||
t.Errorf("base_url 未落库: %q", s.BaseURL)
|
||||
}
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatal("插件注册的源没有出现在内核源列表里")
|
||||
}
|
||||
// 且模型已可路由
|
||||
for _, id := range c.Registry().ModelList() {
|
||||
if id == "plugin-model" {
|
||||
return // 通过
|
||||
}
|
||||
}
|
||||
t.Errorf("插件注册的模型未进入可路由列表: %v", c.Registry().ModelList())
|
||||
}
|
||||
|
||||
// TestKernelPermissionPersists 授权必须落盘(否则重启即失效)。
|
||||
func TestKernelPermissionPersists(t *testing.T) {
|
||||
c := kernelTestCore(t)
|
||||
if err := c.Plugins().LoadSource("kernelprobe", kernelProbePlugin); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
grant(t, c, "kernelprobe", "status", "reload")
|
||||
|
||||
pdir := c.Config().PluginDir
|
||||
path := filepath.Join(pdir, ".kernel-perms.json")
|
||||
b, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatalf("授权未落盘: %v", err)
|
||||
}
|
||||
if !contains(string(b), "reload") {
|
||||
t.Errorf("落盘内容缺少动作名: %s", b)
|
||||
}
|
||||
|
||||
// 模拟重启:新建 Plugins、加载授权、权限仍在
|
||||
vm2 := lua.NewVM(filepath.Join(t.TempDir(), "adapters"))
|
||||
if err := vm2.Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer vm2.Stop()
|
||||
ps2 := lua.NewPlugins(vm2, pdir)
|
||||
if err := ps2.LoadKernelPermissions(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
perm := ps2.PluginKernelPermission("kernelprobe")
|
||||
if len(perm.Actions) != 2 {
|
||||
t.Fatalf("重启后授权应保留 2 项, 实得 %#v", perm.Actions)
|
||||
}
|
||||
}
|
||||
|
||||
// TestUnknownKernelActionRejected 拼错的动作名必须在授权时就被拒,
|
||||
// 而不是静默授予"什么都不允许"。
|
||||
func TestUnknownKernelActionRejected(t *testing.T) {
|
||||
c := kernelTestCore(t)
|
||||
if err := c.Plugins().LoadSource("kernelprobe", kernelProbePlugin); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := c.Plugins().SetPluginKernelPermission("kernelprobe", []string{"no_such_action"}, false); err == nil {
|
||||
t.Fatal("未知动作名应被拒")
|
||||
}
|
||||
}
|
||||
|
||||
// TestKernelActionSurfaceExcludesPlaintextKeys 是凭据边界的回归断言:
|
||||
// 动作面里绝不能出现返回明文密钥的动作(ListKeys/FindKey 被有意排除)。
|
||||
func TestKernelActionSurfaceExcludesPlaintextKeys(t *testing.T) {
|
||||
for _, a := range lua.KernelActions() {
|
||||
switch a.Name {
|
||||
case "list_keys", "find_key", "get_key":
|
||||
t.Fatalf("动作面出现了明文密钥读取动作 %q —— 这会把部署的凭据面交给插件", a.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func contains(haystack, needle string) bool {
|
||||
return len(needle) > 0 && len(haystack) >= len(needle) &&
|
||||
func() bool {
|
||||
for i := 0; i+len(needle) <= len(haystack); i++ {
|
||||
if haystack[i:i+len(needle)] == needle {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}()
|
||||
}
|
||||
39
internal/core/kernel_effect_test.go
Normal file
39
internal/core/kernel_effect_test.go
Normal file
@ -0,0 +1,39 @@
|
||||
package core
|
||||
|
||||
import "testing"
|
||||
|
||||
// TestGrantedDestructiveActionTakesEffect 确认授权后的破坏性动作**真的执行**。
|
||||
//
|
||||
// 上一条测试只证明"不死锁、有返回"。一个把所有动作都变成空转的 dispatch 也能
|
||||
// 通过它 —— 所以这条单独断言副作用确实落地(密钥真的被删)。
|
||||
func TestGrantedDestructiveActionTakesEffect(t *testing.T) {
|
||||
c := kernelTestCore(t)
|
||||
code := `
|
||||
local plugin = { name = "eff", version = "1.0" }
|
||||
plugin.exports = { "del" }
|
||||
function plugin.del(args)
|
||||
local r, err = __llmsproxy_kernel.call("delete_key", { key = args.key })
|
||||
return { ok = r ~= nil, err = err }
|
||||
end
|
||||
return plugin
|
||||
`
|
||||
if err := c.Plugins().LoadSource("eff", code); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_ = c.Plugins().SetPluginKernelPermission("eff", []string{"delete_key"}, false)
|
||||
|
||||
if _, ok := c.FindKey("sk-kernel-0001"); !ok {
|
||||
t.Fatal("前置条件:key 应存在")
|
||||
}
|
||||
out, err := c.Plugins().Call("eff", "del", map[string]interface{}{"key": "sk-kernel-0001"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m := out.(map[string]interface{})
|
||||
if m["ok"] != true {
|
||||
t.Fatalf("动作未成功: %#v", m)
|
||||
}
|
||||
if _, ok := c.FindKey("sk-kernel-0001"); ok {
|
||||
t.Fatal("★ 动作报告成功但副作用没发生 —— dispatch 是空转")
|
||||
}
|
||||
}
|
||||
69
internal/core/kernel_no_deadlock_test.go
Normal file
69
internal/core/kernel_no_deadlock_test.go
Normal file
@ -0,0 +1,69 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"llmsproxy/internal/lua"
|
||||
)
|
||||
|
||||
// TestNoKernelActionDeadlocksFromLua 对**每个**内核动作做同一条断言:
|
||||
// 在插件 Lua 调用(此时持有 p.mu)内执行时不得死锁。
|
||||
//
|
||||
// 为什么是"每个"而不是"那个出问题的":最初只有 set_plugin_enabled 被发现自死锁
|
||||
// (它经 SetEnabled 重入 p.mu),但"是否还有别的动作也会重入"只能靠枚举回答。
|
||||
// 这条测试就是那次枚举的固化 —— 将来新增动作时如果也重入插件锁,它会失败。
|
||||
//
|
||||
// 同时它验证了"超时后副作用仍在幕后执行"这个更隐蔽的变体:那个缺陷的表现是
|
||||
// 插件收到 error、但动作其实生效了。断言方式只是"不死锁 + 返回",因为死锁与
|
||||
// 迟到副作用是同一个根因(在错误的锁上下文里执行)。
|
||||
func TestNoKernelActionDeadlocksFromLua(t *testing.T) {
|
||||
for _, a := range lua.KernelActions() {
|
||||
a := a
|
||||
t.Run(a.Name, func(t *testing.T) {
|
||||
c := kernelTestCore(t)
|
||||
code := `
|
||||
local plugin = { name = "probe_all_actions", version = "1.0" }
|
||||
plugin.exports = { "run" }
|
||||
function plugin.run(args)
|
||||
local r, err = __llmsproxy_kernel.call(args.action, args.arg or {})
|
||||
return { ok = r ~= nil, err = err }
|
||||
end
|
||||
return plugin
|
||||
`
|
||||
if err := c.Plugins().LoadSource("probe_all_actions", code); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// 授权所有动作(含破坏性),以便真正执行到
|
||||
all := make([]string, 0)
|
||||
for _, x := range lua.KernelActions() {
|
||||
all = append(all, x.Name)
|
||||
}
|
||||
if err := c.Plugins().SetPluginKernelPermission("probe_all_actions", all, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
arg := map[string]interface{}{}
|
||||
switch a.Name {
|
||||
case "name", "adapter_owner":
|
||||
arg["name"] = "primary"
|
||||
}
|
||||
if a.Name == "adapter_owner" {
|
||||
arg = map[string]interface{}{"name": "openai"}
|
||||
}
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
defer close(done)
|
||||
_, err := c.Plugins().Call("probe_all_actions", "run",
|
||||
map[string]interface{}{"action": a.Name, "arg": arg})
|
||||
if err != nil {
|
||||
t.Logf("%s 返回 err=%v", a.Name, err)
|
||||
}
|
||||
}()
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(8 * time.Second):
|
||||
t.Fatalf("动作 %s 在 Lua 调用内死锁(deferred=%v danger=%s)", a.Name, a.Deferred, a.Danger)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
108
internal/core/source_hosts_test.go
Normal file
108
internal/core/source_hosts_test.go
Normal file
@ -0,0 +1,108 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"llmsproxy/internal/config"
|
||||
)
|
||||
|
||||
// The plugin outbound allowlist is built from the hosts of configured sources.
|
||||
// That rule is correct for every adapter whose credential and endpoint live on
|
||||
// the same host — which was all of them until OpenDesign.
|
||||
//
|
||||
// OpenDesign splits them: inference is amr-link.open-design.ai, billing is
|
||||
// amr-api.open-design.ai. The quota plugin derives the billing host from the
|
||||
// inference one, so the host it asks for is NOT the host the operator
|
||||
// configured. The request is then refused by the allowlist with a message that
|
||||
// reads like a plugin defect ("host ... is not an allowed outbound destination")
|
||||
// rather than a missing entry.
|
||||
//
|
||||
// This file pins the bridge: when a source looks like OpenDesign, the derived
|
||||
// billing host joins the allowlist.
|
||||
|
||||
func TestSourceHostsIncludesConfiguredHosts(t *testing.T) {
|
||||
cfg := &config.Config{Sources: []config.Source{
|
||||
{Name: "a", BaseURL: "https://api.deepseek.com"},
|
||||
{Name: "b", BaseURL: "https://amr-link.open-design.ai/v1"},
|
||||
{Name: "empty", BaseURL: ""},
|
||||
{Name: "bad", BaseURL: "://nonsense"},
|
||||
}}
|
||||
got := map[string]bool{}
|
||||
for _, h := range sourceHosts(cfg) {
|
||||
got[h] = true
|
||||
}
|
||||
for _, want := range []string{"api.deepseek.com", "amr-link.open-design.ai"} {
|
||||
if !got[want] {
|
||||
t.Errorf("sourceHosts missing configured host %q (allowlist would refuse "+
|
||||
"every plugin request to it): %v", want, got)
|
||||
}
|
||||
}
|
||||
// An empty/unparseable base_url must not abort the whole list.
|
||||
if !got["api.deepseek.com"] {
|
||||
t.Fatal("an unparseable sibling source dropped a valid host")
|
||||
}
|
||||
}
|
||||
|
||||
// TestSourceHostsAddsDerivedBillingHost covers the OpenDesign case at the level
|
||||
// that actually matters: the host the PLUGIN asks for must be in the allowlist.
|
||||
func TestSourceHostsAddsDerivedBillingHost(t *testing.T) {
|
||||
cfg := &config.Config{Sources: []config.Source{
|
||||
{Name: "vela", BaseURL: "https://amr-link.open-design.ai/v1"},
|
||||
}}
|
||||
got := map[string]bool{}
|
||||
for _, h := range sourceHosts(cfg) {
|
||||
got[h] = true
|
||||
}
|
||||
if !got["amr-link.open-design.ai"] {
|
||||
t.Fatalf("inference host missing: %v", got)
|
||||
}
|
||||
if !got["amr-api.open-design.ai"] {
|
||||
t.Fatalf("the billing host the quota plugin derives is not in the allowlist, "+
|
||||
"so every OpenDesign balance probe is refused before it leaves the "+
|
||||
"process: %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
// A deployment with no OpenDesign source must not silently gain its billing
|
||||
// host: an allowlist entry is a grant, and widening it for everyone would let an
|
||||
// unrelated plugin reach a host the operator never configured.
|
||||
func TestSourceHostsDoesNotGrantUnconfiguredBillingHost(t *testing.T) {
|
||||
cfg := &config.Config{Sources: []config.Source{
|
||||
{Name: "ds", BaseURL: "https://api.deepseek.com"},
|
||||
}}
|
||||
for _, h := range sourceHosts(cfg) {
|
||||
if h == "amr-api.open-design.ai" {
|
||||
t.Fatal("the OpenDesign billing host was granted to a deployment with " +
|
||||
"no OpenDesign source")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The derivation has to agree with the Lua side. Keep the two spellings of the
|
||||
// rule in one place so a change to one is visible when reading the other.
|
||||
func TestSourceHostsDerivationMatchesPluginRule(t *testing.T) {
|
||||
// what the Lua adapter does: origin:gsub("^amr%-link%.", "amr-api.")
|
||||
const in = "amr-link.open-design.ai"
|
||||
want := "amr-api.open-design.ai"
|
||||
got := deriveBillingHost(in)
|
||||
if got != want {
|
||||
t.Fatalf("deriveBillingHost(%q) = %q, want %q — the Go allowlist and the "+
|
||||
"Lua probe must agree on the host or the request is refused", in, got, want)
|
||||
}
|
||||
// A host that is not amr-link stays untouched, so this cannot accidentally
|
||||
// rewrite an unrelated provider into an OpenDesign address.
|
||||
for _, other := range []string{"api.deepseek.com", "amr-link.example.com", "xamr-link.a"} {
|
||||
if deriveBillingHost(other) != other {
|
||||
t.Errorf("deriveBillingHost(%q) = %q, want it unchanged", other, deriveBillingHost(other))
|
||||
}
|
||||
}
|
||||
// url.Parse round-trip keeps the helper honest about what it receives.
|
||||
u, err := url.Parse("https://amr-link.open-design.ai/v1")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if deriveBillingHost(u.Hostname()) != "amr-api.open-design.ai" {
|
||||
t.Fatal("derivation failed on a real base_url hostname")
|
||||
}
|
||||
}
|
||||
@ -470,8 +470,8 @@ func (g *Gateway) handleStatsAPI(w http.ResponseWriter, r *http.Request) {
|
||||
// the all-time view showed the truth, which read as "the gateway is
|
||||
// idle" precisely when it was busiest. A calendar window cannot
|
||||
// filter it, so it travels with every response unfiltered.
|
||||
"active": g.stats.Active(),
|
||||
"records": page.Records,
|
||||
"active": g.stats.Active(),
|
||||
"records": page.Records,
|
||||
// The paging cursor has to travel with the first page, otherwise
|
||||
// the scroll handler has nothing to page from and the table is
|
||||
// stuck at one screen forever.
|
||||
|
||||
@ -189,6 +189,20 @@ func (g *Gateway) apiV1Index(w http.ResponseWriter, r *http.Request) {
|
||||
{Method: "GET", Path: "/api/stats", Auth: "any", Summary: "usage aggregates"},
|
||||
{Method: "GET", Path: "/api/stats/records", Auth: "any", Summary: "paged request records"},
|
||||
{Method: "GET", Path: "/api/adapters", Auth: "admin", Summary: "installed Lua adapters"},
|
||||
{Method: "POST", Path: "/api/sources/discover", Auth: "admin",
|
||||
Summary: "probe a base_url+api_key for its model catalog",
|
||||
// Probing is read-only with respect to configuration: it writes
|
||||
// nothing, which is why it is safe to call repeatedly while the
|
||||
// operator is still typing a key.
|
||||
WriteEffect: "none",
|
||||
},
|
||||
{Method: "POST", Path: "/api/sources/discover/apply", Auth: "admin",
|
||||
Summary: "probe and write the source in one step",
|
||||
// Requires replace_models=true when the source already exists,
|
||||
// so re-running discovery cannot silently discard a hand-tuned
|
||||
// model list. An empty api_key keeps the stored credential.
|
||||
WriteEffect: "creates or replaces a source and its model list",
|
||||
},
|
||||
{Method: "GET", Path: "/api/source_templates", Auth: "admin", Summary: "source templates"},
|
||||
{Method: "POST", Path: "/v1/chat/completions", Auth: "any", Summary: "OpenAI-compatible inference"},
|
||||
},
|
||||
|
||||
@ -21,21 +21,79 @@ import (
|
||||
)
|
||||
|
||||
// chatRequest mirrors the OpenAI chat completions request the gateway accepts.
|
||||
// chatRequest is the gateway's IN-BOUND client shape for /v1/chat/completions.
|
||||
//
|
||||
// It is deliberately an OpenAI wire struct, NOT the internal unified format:
|
||||
// this is one client protocol, and the kernel parses it before anything else
|
||||
// knows which protocol the rest of the request will speak. What reaches an
|
||||
// adapter is types.UnifiedRequest (the maximal set), which Thinking populates.
|
||||
type chatRequest struct {
|
||||
Model string `json:"model"`
|
||||
Messages []types.ChatMessage `json:"messages"`
|
||||
Temperature *float64 `json:"temperature,omitempty"`
|
||||
MaxTokens int `json:"max_tokens,omitempty"`
|
||||
Stream bool `json:"stream,omitempty"`
|
||||
Tools []interface{} `json:"tools,omitempty"`
|
||||
ToolChoice interface{} `json:"tool_choice,omitempty"`
|
||||
Model string `json:"model"`
|
||||
Messages []types.ChatMessage `json:"messages"`
|
||||
Temperature *float64 `json:"temperature,omitempty"`
|
||||
MaxTokens int `json:"max_tokens,omitempty"`
|
||||
Stream bool `json:"stream,omitempty"`
|
||||
Tools []interface{} `json:"tools,omitempty"`
|
||||
ToolChoice interface{} `json:"tool_choice,omitempty"`
|
||||
// DisableThinking is the OpenAI-ecosystem spelling of "turn thinking off".
|
||||
// It is translated into the protocol-neutral Thinking field on the way in
|
||||
// (see thinkingFromClient), so that flag never travels under this name.
|
||||
DisableThinking bool `json:"disable_thinking"`
|
||||
ExtraBody map[string]interface{} `json:"extra_body,omitempty"`
|
||||
|
||||
// Thinking accepts the neutral shape directly, so a client already speaking
|
||||
// the unified format (or an adapter-served protocol that names it that way)
|
||||
// does not have to round-trip through disable_thinking.
|
||||
Thinking *types.ThinkingConfig `json:"thinking,omitempty"`
|
||||
|
||||
// PromptCacheKey 是 OpenAI 原生的缓存键;部分客户端用它携带会话标识。
|
||||
PromptCacheKey string `json:"prompt_cache_key,omitempty"`
|
||||
}
|
||||
|
||||
// thinkingFromClient folds the client's spelling of "no thinking" into the
|
||||
// neutral Thinking field.
|
||||
//
|
||||
// Precedence: an explicit `thinking` object wins, because it carries a budget
|
||||
// the boolean cannot express. disable_thinking only ever sets Enabled=false —
|
||||
// it says nothing about how much, and inventing a budget there would silently
|
||||
// override the upstream's default.
|
||||
func (r *chatRequest) thinkingFromClient() *types.ThinkingConfig {
|
||||
if r.Thinking != nil {
|
||||
return r.Thinking
|
||||
}
|
||||
if r.DisableThinking {
|
||||
no := false
|
||||
return &types.ThinkingConfig{Enabled: &no}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// rawTools / rawToolChoice re-encode the loosely-typed client fields into the
|
||||
// RawMessage the internal format carries. The kernel never interprets them:
|
||||
// they are the client's payload, passed through to the adapter, which is the
|
||||
// only layer that knows the tool schema of any particular protocol.
|
||||
func (r *chatRequest) rawTools() json.RawMessage {
|
||||
if len(r.Tools) == 0 {
|
||||
return nil
|
||||
}
|
||||
b, err := json.Marshal(r.Tools)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func (r *chatRequest) rawToolChoice() json.RawMessage {
|
||||
if r.ToolChoice == nil {
|
||||
return nil
|
||||
}
|
||||
b, err := json.Marshal(r.ToolChoice)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// ChatCompletion is the non-streaming OpenAI response object.
|
||||
type ChatCompletion struct {
|
||||
ID string `json:"id"`
|
||||
@ -256,17 +314,17 @@ func (g *Gateway) checkQuota(ctx context.Context, model string) *quotaRejection
|
||||
}
|
||||
return nil
|
||||
}
|
||||
// A scope that simply does not list the model is a different problem from
|
||||
// a quota being exhausted, and the message has to say which: telling an
|
||||
// operator "not allowed for this key" when they configured a quota sends
|
||||
// them to the wrong setting. AUTO is checked here before the chain is even
|
||||
// consulted, so this is the message that surfaces for AUTO.
|
||||
if isAuto(model) {
|
||||
return "aRejection{msg: fmt.Sprintf(
|
||||
"model %q is not in this key's model scope, so it cannot use AUTO; "+
|
||||
"add %q to the key's models or remove the scope restriction", model, model)}
|
||||
}
|
||||
return "aRejection{msg: fmt.Sprintf("model %q is not allowed for this key", model)}
|
||||
// A scope that simply does not list the model is a different problem from
|
||||
// a quota being exhausted, and the message has to say which: telling an
|
||||
// operator "not allowed for this key" when they configured a quota sends
|
||||
// them to the wrong setting. AUTO is checked here before the chain is even
|
||||
// consulted, so this is the message that surfaces for AUTO.
|
||||
if isAuto(model) {
|
||||
return "aRejection{msg: fmt.Sprintf(
|
||||
"model %q is not in this key's model scope, so it cannot use AUTO; "+
|
||||
"add %q to the key's models or remove the scope restriction", model, model)}
|
||||
}
|
||||
return "aRejection{msg: fmt.Sprintf("model %q is not allowed for this key", model)}
|
||||
}
|
||||
|
||||
// quotaWindowSuffix describes a quota's reset window for an error message, so
|
||||
@ -424,15 +482,15 @@ func (g *Gateway) handleChat(w http.ResponseWriter, r *http.Request) {
|
||||
done := g.stats.Begin()
|
||||
defer done()
|
||||
inner := &types.ChatRequest{
|
||||
Messages: req.Messages,
|
||||
Temperature: req.Temperature,
|
||||
MaxTokens: req.MaxTokens,
|
||||
Stream: req.Stream,
|
||||
Tools: req.Tools,
|
||||
ToolChoice: req.ToolChoice,
|
||||
DisableThinking: req.DisableThinking,
|
||||
ExtraBody: req.ExtraBody,
|
||||
ClientSession: clientSession,
|
||||
Messages: req.Messages,
|
||||
Temperature: req.Temperature,
|
||||
MaxTokens: req.MaxTokens,
|
||||
Stream: req.Stream,
|
||||
Tools: req.rawTools(),
|
||||
ToolChoice: req.rawToolChoice(),
|
||||
Thinking: req.thinkingFromClient(),
|
||||
ExtraBody: req.ExtraBody,
|
||||
ClientSession: clientSession,
|
||||
}
|
||||
rec := &Req{
|
||||
Key: keyID(reqKey(ctx)),
|
||||
@ -484,16 +542,16 @@ func (g *Gateway) handleChat(w http.ResponseWriter, r *http.Request) {
|
||||
defer done()
|
||||
|
||||
inner := &types.ChatRequest{
|
||||
Model: effective,
|
||||
Messages: req.Messages,
|
||||
Temperature: req.Temperature,
|
||||
MaxTokens: req.MaxTokens,
|
||||
Stream: req.Stream,
|
||||
Tools: req.Tools,
|
||||
ToolChoice: req.ToolChoice,
|
||||
DisableThinking: req.DisableThinking,
|
||||
ExtraBody: req.ExtraBody,
|
||||
ClientSession: clientSession,
|
||||
Model: effective,
|
||||
Messages: req.Messages,
|
||||
Temperature: req.Temperature,
|
||||
MaxTokens: req.MaxTokens,
|
||||
Stream: req.Stream,
|
||||
Tools: req.rawTools(),
|
||||
ToolChoice: req.rawToolChoice(),
|
||||
Thinking: req.thinkingFromClient(),
|
||||
ExtraBody: req.ExtraBody,
|
||||
ClientSession: clientSession,
|
||||
}
|
||||
rec := &Req{
|
||||
Key: keyID(reqKey(ctx)),
|
||||
@ -586,7 +644,7 @@ func estimatePromptTokens(req *types.ChatRequest) int64 {
|
||||
}
|
||||
b, _ := json.Marshal(struct {
|
||||
Messages []types.ChatMessage `json:"messages"`
|
||||
Tools []interface{} `json:"tools,omitempty"`
|
||||
Tools json.RawMessage `json:"tools,omitempty"`
|
||||
}{Messages: req.Messages, Tools: req.Tools})
|
||||
if len(b) == 0 {
|
||||
return 0
|
||||
@ -796,7 +854,7 @@ func recordChatUsage(rec *Req, req *types.ChatRequest, resp *types.UnifiedRespon
|
||||
// keeps the billing plugin from guessing.
|
||||
//
|
||||
// Leaving it unrecorded is worse than useless: the adapters already parse it
|
||||
// and forward it (opencodego/gemini/anthropic all do), so the number exists
|
||||
// and forward it (gemini/anthropic/qwen38 all do), so the number exists
|
||||
// in the response and is thrown away at the one place that could keep it.
|
||||
if d := resp.TokenUsage.CompletionTokensDetails; d != nil && d.ReasoningTokens > 0 {
|
||||
rec.Reasoning = int64(d.ReasoningTokens)
|
||||
@ -817,29 +875,21 @@ func recordChatUsage(rec *Req, req *types.ChatRequest, resp *types.UnifiedRespon
|
||||
}
|
||||
}
|
||||
|
||||
// writeChatCompletion renders a unified response as an OpenAI
|
||||
// chat.completion object. modelName is the id clients see as the serving
|
||||
// model: the requested id for direct routes, the exact slot model for AUTO.
|
||||
func writeChatCompletion(w http.ResponseWriter, resp *types.UnifiedResponse, modelName string) {
|
||||
msg := RespMessage{Role: "assistant", Content: resp.Content}
|
||||
if resp.ReasoningContent != "" {
|
||||
msg.ReasoningContent = resp.ReasoningContent
|
||||
// writeChatCompletion renders a unified response through the out-bound
|
||||
// Serializer. The default serializer reproduces the OpenAI wire shape this
|
||||
// function used to build inline (locked byte-for-byte by e2e golden); an
|
||||
// adapter that declares serialize_response overrides it. modelName is the id
|
||||
// clients see as the serving model: the requested id for direct routes, the
|
||||
// exact slot model for AUTO.
|
||||
func writeChatCompletion(w http.ResponseWriter, s Serializer, resp *types.UnifiedResponse, modelName string) {
|
||||
body, err := s.NonStream(resp, modelName, newWireID(), newCreated())
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "serialize_error", "failed to serialize response")
|
||||
return
|
||||
}
|
||||
if len(resp.ToolCalls) > 0 {
|
||||
msg.ToolCalls = toolCallsWire(resp.ToolCalls)
|
||||
}
|
||||
out := ChatCompletion{
|
||||
ID: newID(),
|
||||
Object: "chat.completion",
|
||||
Created: time.Now().Unix(),
|
||||
Model: modelName,
|
||||
Choices: []ChatChoice{{Index: 0, Message: msg, FinishReason: resp.FinishReason}},
|
||||
Cost: resp.Cost,
|
||||
}
|
||||
if resp.TokenUsage.Total > 0 || resp.TokenUsage.Prompt > 0 || resp.TokenUsage.Completion > 0 {
|
||||
out.Usage = &resp.TokenUsage
|
||||
}
|
||||
writeJSON(w, http.StatusOK, out)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write(body)
|
||||
}
|
||||
|
||||
// singleChat runs a direct (model-pinned) non-streaming request across the
|
||||
@ -864,7 +914,7 @@ func (g *Gateway) singleChat(w http.ResponseWriter, ctx context.Context, cands [
|
||||
// the total latency.
|
||||
rec.FirstByteMs = rec.LatMs
|
||||
g.writeRec(rec)
|
||||
writeChatCompletion(w, resp, effective)
|
||||
writeChatCompletion(w, g.serializerFor(usedSrc, cands), resp, effective)
|
||||
}
|
||||
|
||||
// writeRec records a finished request (audit + aggregates) and fires the
|
||||
@ -1059,9 +1109,9 @@ func (g *Gateway) fireEnd(rec *Req) {
|
||||
// A SUBSET of completion_tokens, not an extra. Billing plugins that
|
||||
// add it to completion would double-charge every thinking token.
|
||||
"reasoning_tokens": rec.Reasoning,
|
||||
"image_count": rec.ImageCount,
|
||||
"error": rec.Err,
|
||||
"time": rec.Time,
|
||||
"image_count": rec.ImageCount,
|
||||
"error": rec.Err,
|
||||
"time": rec.Time,
|
||||
// chain_walk: the AUTO tier-by-tier trace, when the request went
|
||||
// through the chain. Empty for a direct request and for a gateway with
|
||||
// no plugins loaded. Absent rather than empty so a plugin can tell
|
||||
@ -1116,24 +1166,29 @@ func mergeUsage(prev, cur *types.TokenUsage) *types.TokenUsage {
|
||||
return &out
|
||||
}
|
||||
|
||||
// pumpStream writes the full SSE sequence for a started stream: role
|
||||
// preamble, one chunk per unified delta, the terminating finish_reason, the
|
||||
// OpenAI-standard final usage chunk (empty choices) and [DONE]. modelName
|
||||
// follows writeChatCompletion's rule (requested id for direct routes, exact
|
||||
// slot model for AUTO).
|
||||
func (g *Gateway) pumpStream(w http.ResponseWriter, rec *Req, chunks <-chan types.UnifiedChunk, modelName string, t0 time.Time) {
|
||||
// pumpStream writes the full SSE sequence for a started stream. Every byte on
|
||||
// the wire comes from the Serializer, so the OpenAI frame shapes live in
|
||||
// serializer.go rather than here, and an adapter that declares serialize_chunk
|
||||
// replaces them wholesale.
|
||||
//
|
||||
// This function owns only transport concerns — headers, the flush cadence,
|
||||
// first-byte timing, and audit accounting — plus the protocol-agnostic rule
|
||||
// that usage seen anywhere in the stream becomes the recorded usage. Whether a
|
||||
// trailing usage frame is emitted, and in what shape, is the serializer's
|
||||
// decision.
|
||||
func (g *Gateway) pumpStream(w http.ResponseWriter, ser Serializer, rec *Req, chunks <-chan types.UnifiedChunk, modelName string, t0 time.Time) {
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
w.Header().Set("Cache-Control", "no-cache")
|
||||
w.Header().Set("Connection", "keep-alive")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
flusher, _ := w.(http.Flusher)
|
||||
id := newID()
|
||||
created := time.Now().Unix()
|
||||
send := func(obj interface{}) bool {
|
||||
b, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
return false
|
||||
id := newWireID()
|
||||
created := newCreated()
|
||||
st := &StreamState{}
|
||||
send := func(b []byte) bool {
|
||||
if len(b) == 0 {
|
||||
return true
|
||||
}
|
||||
if _, err := fmt.Fprintf(w, "data: %s\n\n", b); err != nil {
|
||||
return false
|
||||
@ -1144,115 +1199,66 @@ func (g *Gateway) pumpStream(w http.ResponseWriter, rec *Req, chunks <-chan type
|
||||
return true
|
||||
}
|
||||
|
||||
if !send(ChatChunk{
|
||||
ID: id, Object: "chat.completion.chunk", Created: created, Model: modelName,
|
||||
Choices: []ChunkChoice{{Index: 0, Delta: RespMessage{Role: "assistant"}}},
|
||||
}) {
|
||||
return
|
||||
if b, err := ser.OpenRolePreamble(modelName, id, created, st); err == nil {
|
||||
if !send(b) {
|
||||
return
|
||||
}
|
||||
}
|
||||
// First SSE byte sent to the client: record time-to-first-byte for the
|
||||
// source's status-page latency average.
|
||||
rec.FirstByteMs = time.Since(t0).Milliseconds()
|
||||
var lastUsage *types.TokenUsage
|
||||
var lastCost string
|
||||
lastFinish := ""
|
||||
|
||||
for ck := range chunks {
|
||||
if ck.Usage != nil {
|
||||
lastUsage = mergeUsage(lastUsage, ck.Usage)
|
||||
st.Usage = mergeUsage(st.Usage, ck.Usage)
|
||||
}
|
||||
if ck.Cost != "" {
|
||||
lastCost = ck.Cost
|
||||
st.Cost = ck.Cost
|
||||
}
|
||||
chunk := ChatChunk{
|
||||
ID: id, Object: "chat.completion.chunk", Created: created, Model: modelName,
|
||||
b, err := ser.Chunk(&ck, modelName, id, created, st)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
delta := RespMessage{Role: "assistant", Content: ck.Content}
|
||||
if ck.ReasoningContent != "" {
|
||||
delta.ReasoningContent = ck.ReasoningContent
|
||||
}
|
||||
if len(ck.ToolCalls) > 0 {
|
||||
delta.ToolCalls = ck.ToolCalls
|
||||
}
|
||||
choice := ChunkChoice{Index: 0, Delta: delta}
|
||||
if ck.Done {
|
||||
fin := ck.FinishReason
|
||||
if fin == "" {
|
||||
fin = "stop"
|
||||
}
|
||||
lastFinish = fin
|
||||
choice.FinishReason = &fin
|
||||
}
|
||||
chunk.Choices = []ChunkChoice{choice}
|
||||
rec.Compl += int64(len(ck.Content)+len(ck.ReasoningContent)+len(ck.ToolCalls)) / 3
|
||||
if !send(chunk) {
|
||||
if !send(b) {
|
||||
return
|
||||
}
|
||||
}
|
||||
finalFinish := lastFinish
|
||||
if finalFinish == "" {
|
||||
finalFinish = "stop"
|
||||
}
|
||||
send(ChatChunk{
|
||||
ID: id, Object: "chat.completion.chunk", Created: created, Model: modelName,
|
||||
Choices: []ChunkChoice{{Index: 0, Delta: RespMessage{}, FinishReason: &finalFinish}},
|
||||
})
|
||||
// Final usage chunk (OpenAI standard: empty choices + usage before [DONE]).
|
||||
// Prefer the upstream's exact usage if the stream carried it; fall back to
|
||||
// the gateway's estimate otherwise.
|
||||
// Write the upstream's exact numbers back onto the audit record. Without
|
||||
// this the streamed path kept only the per-chunk byte estimate, so the same
|
||||
|
||||
// Write the upstream's exact usage back onto the audit record. Without this
|
||||
// the streamed path kept only the per-chunk byte estimate, so the same
|
||||
// request recorded ~1.4x its real completion tokens (measured: upstream 100,
|
||||
// audit 145) while the non-streaming path recorded 100. Two paths, two
|
||||
// different numbers for one request is a reporting bug, not a rounding one.
|
||||
if lastUsage != nil {
|
||||
if lastUsage.Prompt > 0 {
|
||||
rec.Prompt = int64(lastUsage.Prompt)
|
||||
if st.Usage != nil {
|
||||
if st.Usage.Prompt > 0 {
|
||||
rec.Prompt = int64(st.Usage.Prompt)
|
||||
}
|
||||
if lastUsage.Completion > 0 {
|
||||
rec.Compl = int64(lastUsage.Completion)
|
||||
}
|
||||
}
|
||||
var tut *types.TokenUsage
|
||||
if lastUsage != nil {
|
||||
tut = lastUsage
|
||||
// Write the upstream's cache accounting back onto the request record
|
||||
// so the audit trail carries the hit/miss split for streaming too.
|
||||
if d := tut.PromptTokensDetails; d != nil {
|
||||
rec.CacheReported = true
|
||||
if d.CachedTokens > 0 {
|
||||
rec.CacheHit = int64(d.CachedTokens)
|
||||
}
|
||||
} else if tut.PromptCacheHit > 0 {
|
||||
rec.CacheHit = int64(tut.PromptCacheHit)
|
||||
rec.CacheReported = true
|
||||
}
|
||||
if tut.PromptCacheMiss > 0 {
|
||||
rec.CacheMiss = int64(tut.PromptCacheMiss)
|
||||
if st.Usage.Completion > 0 {
|
||||
rec.Compl = int64(st.Usage.Completion)
|
||||
}
|
||||
applyStreamUsageDetails(rec, st.Usage)
|
||||
} else if rec.Prompt+rec.Compl > 0 {
|
||||
u := types.TokenUsage{
|
||||
// No upstream usage on the stream: the recorded per-chunk byte
|
||||
// estimate is the best number available, so hand it to the serializer
|
||||
// as the usage rather than dropping it.
|
||||
st.Usage = &types.TokenUsage{
|
||||
Prompt: int(rec.Prompt),
|
||||
Completion: int(rec.Compl),
|
||||
Total: int(rec.Prompt + rec.Compl),
|
||||
}
|
||||
tut = &u
|
||||
}
|
||||
if tut != nil {
|
||||
send(ChatChunk{
|
||||
ID: id, Object: "chat.completion.chunk", Created: created, Model: modelName,
|
||||
Choices: []ChunkChoice{},
|
||||
Usage: tut,
|
||||
Cost: lastCost,
|
||||
})
|
||||
} else if lastCost != "" {
|
||||
// Cost arrived without any usage (OpenCode sends it on its own frame).
|
||||
send(ChatChunk{
|
||||
ID: id, Object: "chat.completion.chunk", Created: created, Model: modelName,
|
||||
Choices: []ChunkChoice{},
|
||||
Cost: lastCost,
|
||||
})
|
||||
|
||||
if frames, err := ser.Close(modelName, id, created, st); err == nil {
|
||||
for _, b := range frames {
|
||||
if !send(b) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
if marker := ser.DoneMarker(); marker != "" {
|
||||
fmt.Fprintf(w, "data: %s\n\n", marker)
|
||||
}
|
||||
fmt.Fprintf(w, "data: [DONE]\n\n")
|
||||
if flusher != nil {
|
||||
flusher.Flush()
|
||||
}
|
||||
@ -1283,7 +1289,7 @@ func (g *Gateway) streamChat(w http.ResponseWriter, ctx context.Context, cands [
|
||||
rec.Source = usedSrc
|
||||
g.fireRouted(ctx, "stream", usedSrc, rec.Model, -1, true)
|
||||
rec.Prompt = estimatePromptTokens(req)
|
||||
g.pumpStream(w, rec, chunks, effective, t0)
|
||||
g.pumpStream(w, g.serializerFor(usedSrc, cands), rec, chunks, effective, t0)
|
||||
}
|
||||
|
||||
// singleChatAuto runs a non-streaming AUTO request down the chain (see
|
||||
@ -1316,7 +1322,7 @@ func (g *Gateway) singleChatAuto(w http.ResponseWriter, ctx context.Context, cha
|
||||
g.fireRouted(ctx, "chat", usedSrc, usedModel, -2, false)
|
||||
rec.FirstByteMs = rec.LatMs
|
||||
g.writeRec(rec)
|
||||
writeChatCompletion(w, resp, usedModel)
|
||||
writeChatCompletion(w, g.serializerFor(usedSrc, nil), resp, usedModel)
|
||||
}
|
||||
|
||||
// streamChatAuto streams an AUTO request down the chain. A slot is abandoned
|
||||
@ -1346,7 +1352,7 @@ func (g *Gateway) streamChatAuto(w http.ResponseWriter, ctx context.Context, cha
|
||||
rec.Source = usedSrc
|
||||
g.fireRouted(ctx, "stream", usedSrc, rec.Model, -2, true)
|
||||
rec.Prompt = estimatePromptTokens(req)
|
||||
g.pumpStream(w, rec, chunks, usedModel, t0)
|
||||
g.pumpStream(w, g.serializerFor(usedSrc, nil), rec, chunks, usedModel, t0)
|
||||
}
|
||||
|
||||
func (g *Gateway) handleImage(w http.ResponseWriter, r *http.Request) {
|
||||
@ -1459,3 +1465,39 @@ func (g *Gateway) handleImage(w http.ResponseWriter, r *http.Request) {
|
||||
Data: resp.ImageData,
|
||||
})
|
||||
}
|
||||
|
||||
// applyStreamUsageDetails mirrors the upstream's reasoning and cache numbers
|
||||
// onto a STREAMED request's audit record.
|
||||
//
|
||||
// It exists as a named function because this was inline in the streaming handler,
|
||||
// where it was untestable — and untested it stayed wrong. The non-streaming
|
||||
// recorder read completion_tokens_details.reasoning_tokens; this one did not. So
|
||||
// a source's streamed requests recorded completion and no reasoning at all while
|
||||
// its non-streamed calls recorded both. Nothing failed and no test was red: the
|
||||
// audit trail was simply quieter than it should have been.
|
||||
//
|
||||
// Found by reading the production audit file after wiring a reasoning-heavy
|
||||
// source: completion=8166 with reasoning absent, on every streamed request.
|
||||
//
|
||||
// Reasoning is a SUBSET of completion, never an addition to it. See
|
||||
// recordChatUsage for why that matters to billing.
|
||||
func applyStreamUsageDetails(rec *Req, u *types.TokenUsage) {
|
||||
if d := u.CompletionTokensDetails; d != nil && d.ReasoningTokens > 0 {
|
||||
rec.Reasoning = int64(d.ReasoningTokens)
|
||||
}
|
||||
// Cache accounting is here for the same reason it is kept apart: "zero hits
|
||||
// because nothing was cached" and "this provider never reports caching" are
|
||||
// the same number and mean opposite things.
|
||||
if d := u.PromptTokensDetails; d != nil {
|
||||
rec.CacheReported = true
|
||||
if d.CachedTokens > 0 {
|
||||
rec.CacheHit = int64(d.CachedTokens)
|
||||
}
|
||||
} else if u.PromptCacheHit > 0 {
|
||||
rec.CacheHit = int64(u.PromptCacheHit)
|
||||
rec.CacheReported = true
|
||||
}
|
||||
if u.PromptCacheMiss > 0 {
|
||||
rec.CacheMiss = int64(u.PromptCacheMiss)
|
||||
}
|
||||
}
|
||||
|
||||
@ -9,7 +9,7 @@ import (
|
||||
// TestRecordChatUsageKeepsReasoningTokens pins the reasoning-token plumbing.
|
||||
//
|
||||
// The adapters already parse completion_tokens_details.reasoning_tokens and
|
||||
// forward it (opencodego, gemini, anthropic all do), and types.go has carried
|
||||
// forward it (qwen38, gemini, anthropic all do), and types.go has carried
|
||||
// the field since it was defined — but nothing ever READ it. The number arrived
|
||||
// in the response and was discarded at the one place that could have kept it,
|
||||
// so a pricing rule had no way to split thinking from output.
|
||||
@ -69,3 +69,92 @@ func TestRecordChatUsageReasoningAbsentStaysZero(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestStreamedReasoningIsRecordedOnTheRequest is the streaming counterpart.
|
||||
//
|
||||
// TestRecordChatUsageKeepsReasoningTokens covers the non-streaming path only,
|
||||
// and for a long time the streamed path dropped the number: the non-streaming
|
||||
// recorder read TokenUsage.CompletionTokensDetails, while the streaming one read
|
||||
// only Prompt and Completion from the terminal usage chunk. (Cache accounting was
|
||||
// mirrored; reasoning simply never was.)
|
||||
//
|
||||
// The symptom is invisible in the response — the client sees a correct answer
|
||||
// either way. It only shows in the audit trail, where a reasoning-heavy source's
|
||||
// streamed requests recorded completion=8166 and no reasoning at all. Found by
|
||||
// reading the production audit file, not by a failing test, which is exactly why
|
||||
// the property is pinned here.
|
||||
func TestStreamedReasoningIsRecordedOnTheRequest(t *testing.T) {
|
||||
rec := &Req{}
|
||||
applyStreamUsageDetails(rec, &types.TokenUsage{
|
||||
Prompt: 159579,
|
||||
Completion: 751,
|
||||
Total: 160330,
|
||||
CompletionTokensDetails: &types.CompletionTokensDetails{
|
||||
ReasoningTokens: 512,
|
||||
},
|
||||
})
|
||||
if rec.Reasoning != 512 {
|
||||
t.Errorf("streamed Reasoning = %d, want 512 — a streamed request records no "+
|
||||
"reasoning, so a pricing rule that splits thinking from output silently "+
|
||||
"bills it as ordinary output", rec.Reasoning)
|
||||
}
|
||||
}
|
||||
|
||||
// TestStreamedReasoningIsNeverAddedToCompletion pins the SUBSET relation in the
|
||||
// streaming path specifically. A presence check would pass if someone wrote
|
||||
// rec.Compl += reasoning, and billing would then charge every thinking token
|
||||
// twice.
|
||||
func TestStreamedReasoningIsNeverAddedToCompletion(t *testing.T) {
|
||||
rec := &Req{}
|
||||
rec.Compl = 751 // set upstream of the call, as the streaming handler does
|
||||
applyStreamUsageDetails(rec, &types.TokenUsage{
|
||||
Completion: 751,
|
||||
CompletionTokensDetails: &types.CompletionTokensDetails{ReasoningTokens: 512},
|
||||
})
|
||||
if rec.Compl != 751 {
|
||||
t.Errorf("Compl = %d, want 751 unchanged — reasoning is a subset, not an addition", rec.Compl)
|
||||
}
|
||||
if rec.Reasoning >= rec.Compl {
|
||||
t.Errorf("Reasoning (%d) must be a SUBSET of Compl (%d)", rec.Reasoning, rec.Compl)
|
||||
}
|
||||
}
|
||||
|
||||
// TestStreamedUsageWithoutReasoningLeavesItUnset: an upstream that reports no
|
||||
// reasoning must not be turned into a reported zero. In the audit trail 0 means
|
||||
// "not reported", and a plugin cannot tell that from "no reasoning happened".
|
||||
func TestStreamedUsageWithoutReasoningLeavesItUnset(t *testing.T) {
|
||||
rec := &Req{}
|
||||
applyStreamUsageDetails(rec, &types.TokenUsage{Prompt: 10, Completion: 4, Total: 14})
|
||||
if rec.Reasoning != 0 {
|
||||
t.Errorf("Reasoning = %d for an upstream that reported none", rec.Reasoning)
|
||||
}
|
||||
}
|
||||
|
||||
// TestStreamedCacheAccountingStillWorks is a regression guard for the block the
|
||||
// reasoning lines were added to. They share a function, so a future edit that
|
||||
// short-circuits one would break the other — silently, since a missing cache
|
||||
// number reads as "nothing was cached".
|
||||
func TestStreamedCacheAccountingStillWorks(t *testing.T) {
|
||||
for _, c := range []struct {
|
||||
name string
|
||||
u types.TokenUsage
|
||||
hit int64
|
||||
miss int64
|
||||
rep bool
|
||||
}{
|
||||
{"details object", types.TokenUsage{
|
||||
PromptTokensDetails: &types.PromptTokensDetails{CachedTokens: 4096},
|
||||
}, 4096, 0, true},
|
||||
{"legacy hit/miss", types.TokenUsage{
|
||||
PromptCacheHit: 2048, PromptCacheMiss: 512,
|
||||
}, 2048, 512, true},
|
||||
{"nothing reported", types.TokenUsage{}, 0, 0, false},
|
||||
} {
|
||||
rec := &Req{}
|
||||
applyStreamUsageDetails(rec, &c.u)
|
||||
if rec.CacheHit != c.hit || rec.CacheMiss != c.miss || rec.CacheReported != c.rep {
|
||||
t.Errorf("%s: hit=%d miss=%d reported=%v; want %d/%d/%v",
|
||||
c.name, rec.CacheHit, rec.CacheMiss, rec.CacheReported, c.hit, c.miss, c.rep)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
749
internal/gateway/discover.go
Normal file
749
internal/gateway/discover.go
Normal file
@ -0,0 +1,749 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"llmsproxy/internal/config"
|
||||
)
|
||||
|
||||
// Model discovery: turn "here is a URL and a key" into a configured source.
|
||||
//
|
||||
// The hard part is not the GET /models call — it is guessing which URL the
|
||||
// upstream actually serves. This gateway builds request paths as
|
||||
// base_url + adapter endpoint (openai contributes "/chat/completions"), so a
|
||||
// base_url that already ends in "/v1" yields "/v1/chat/completions" while a
|
||||
// base_url of "/v1" plus our default image path yields the broken
|
||||
// "/v1/v1/images/generations". Vendors disagree on whether the version segment
|
||||
// belongs to the base or to the path, and there is no header that says which.
|
||||
//
|
||||
// So discovery does not trust the URL it is given. It tries a small, ordered
|
||||
// set of candidates, records which one answered, and reports that back as the
|
||||
// normalized base_url + adapter endpoint pair. A caller who typed the wrong
|
||||
// form gets a working source anyway, plus the correction needed to make it
|
||||
// permanent.
|
||||
//
|
||||
// What is deliberately NOT here:
|
||||
//
|
||||
// - No guessing of model capabilities from the name. "vision" in an id does
|
||||
// not make a model accept images, and marking an image-only endpoint as
|
||||
// kind:"chat" makes every AUTO request fail on it. Kind stays "chat" unless
|
||||
// the upstream's own metadata says otherwise (see discoveredModelKind).
|
||||
// - No live probe per model. A single-token completion per model would cost
|
||||
// real money across a 34-model catalog and takes minutes. /models is the
|
||||
// cheap, authoritative-enough signal for "this id is served here"; whether
|
||||
// it actually answers stays for the normal request path to discover.
|
||||
//
|
||||
// The one thing that IS verified live is reachability, because a source that
|
||||
// 404s is not worth writing to disk.
|
||||
|
||||
const (
|
||||
// discoveryTimeout bounds the whole probe. Vendors are routinely slow on
|
||||
// the first request to an uncached endpoint; 20s is enough for a healthy
|
||||
// one and short enough that the UI is not left hanging.
|
||||
discoveryTimeout = 20 * time.Second
|
||||
|
||||
// discoveryModelsPath is the OpenAI-compatible listing endpoint. Nearly
|
||||
// every vendor proxies it verbatim, including the ones whose chat path
|
||||
// differs.
|
||||
discoveryModelsPath = "/v1/models"
|
||||
|
||||
// discoveryMaxBody caps the listing response. A catalog with thousands of
|
||||
// models is still far under this; anything larger is a captive portal or
|
||||
// an error page, not a model list.
|
||||
discoveryMaxBody = 8 << 20
|
||||
)
|
||||
|
||||
// discoveredModel is one entry of a parsed /models response, kept in the
|
||||
// upstream's own terms. The upstream is the only authority on what it serves.
|
||||
type discoveredModel struct {
|
||||
ID string
|
||||
Kind string // "chat" | "image"
|
||||
Meta map[string]interface{}
|
||||
}
|
||||
|
||||
// discoveryResult reports what a probe found, including the case where nothing
|
||||
// worked — the failure text is what the operator needs, so it must name every
|
||||
// URL that was tried rather than just the last one.
|
||||
type discoveryResult struct {
|
||||
BaseURL string `json:"base_url"`
|
||||
Endpoint string `json:"endpoint"`
|
||||
// ImageEndpoint is reported separately because the gateway composes it
|
||||
// differently: Provider.ImageURL is BaseURL + ImageEndpoint(), and the
|
||||
// default ImageEndpoint is the ABSOLUTE path "/v1/images/generations".
|
||||
// Chat's default is the relative "/chat/completions". So a base_url that
|
||||
// already ends in /v1 produces a working chat URL and a broken image URL
|
||||
// ("/v1/v1/images/generations") — which is exactly the 404 seen on the
|
||||
// first vela configuration, and which discovery has to correct.
|
||||
ImageEndpoint string `json:"image_endpoint"`
|
||||
Adapter string `json:"adapter"`
|
||||
ModelsPath string `json:"models_path"`
|
||||
Models []string `json:"models"`
|
||||
ImageModels []string `json:"image_models"`
|
||||
Tried []string `json:"tried"`
|
||||
LatencyMS int64 `json:"latency_ms"`
|
||||
Note string `json:"note,omitempty"`
|
||||
}
|
||||
|
||||
// MarshalJSON guarantees the two list fields are always JSON arrays, never
|
||||
// null. The WebUI concatenates them directly, and "null" renders as the string
|
||||
// "undefined" in every row — a failure that only shows up in a browser and not
|
||||
// in a JSON schema check.
|
||||
//
|
||||
// Relying on the constructor to initialize the slices is not enough: the zero
|
||||
// value of this struct is a legitimate thing for a caller to build, and a
|
||||
// future code path that forgets the initializer would ship a UI bug silently.
|
||||
func (r discoveryResult) MarshalJSON() ([]byte, error) {
|
||||
type alias discoveryResult // avoid recursion
|
||||
out := alias(r)
|
||||
if out.Models == nil {
|
||||
out.Models = []string{}
|
||||
}
|
||||
if out.ImageModels == nil {
|
||||
out.ImageModels = []string{}
|
||||
}
|
||||
if out.Tried == nil {
|
||||
out.Tried = []string{}
|
||||
}
|
||||
return json.Marshal(out)
|
||||
}
|
||||
|
||||
// discoveryCandidate is one (base_url, models-path) guess. Order matters: the
|
||||
// first candidate is the one reported as normalized when several work, because
|
||||
// it is the one that keeps base_url free of a version segment and therefore
|
||||
// composes correctly with the adapter's chat path.
|
||||
type discoveryCandidate struct {
|
||||
baseURL string
|
||||
modelsPath string
|
||||
// prefer ranks candidates that are more likely to be the vendor's real
|
||||
// path. A 200 from a lower-preference candidate is still accepted, but
|
||||
// only after every higher-preference one failed — see DiscoverModels.
|
||||
prefer int
|
||||
}
|
||||
|
||||
// discoveryFailure carries the HTTP status the caller should answer with.
|
||||
//
|
||||
// The status is part of the error rather than left to the handler because the
|
||||
// whole point of classifying a failure is that the operator must not have to
|
||||
// guess whether the key, the endpoint, or the vendor is at fault.
|
||||
type discoveryFailure struct {
|
||||
msg string
|
||||
status int
|
||||
}
|
||||
|
||||
func (e *discoveryFailure) Error() string { return e.msg }
|
||||
|
||||
// StatusOrDefault reports the intended HTTP status.
|
||||
func (e *discoveryFailure) StatusOrDefault() int {
|
||||
if e.status == 0 {
|
||||
return http.StatusBadGateway
|
||||
}
|
||||
return e.status
|
||||
}
|
||||
|
||||
// discoveryCandidates enumerates the URL forms worth trying for a raw input.
|
||||
//
|
||||
// The ranking encodes a real observation rather than a guess: the amr-link
|
||||
// endpoint serves its catalog at "/v1/models" (the form an SDK example shows)
|
||||
// from a base the operator pasted WITH the "/v1", while several other vendors
|
||||
// mount it at the host root. Both are probed; the one that answers is reported
|
||||
// back as a normalized base/endpoint pair.
|
||||
//
|
||||
// What we are really deciding is the base_url to persist. The safest one keeps
|
||||
// the version segment on the ENDPOINT, because openai's adapter endpoint is
|
||||
// "/chat/completions" and the two are concatenated: base "https://h" +
|
||||
// endpoint "/v1/chat/completions" is correct, while leaving "/v1" on the base
|
||||
// doubles the default image path into "/v1/v1/images/generations" — which is
|
||||
// exactly the 404 observed on the first vela configuration attempt.
|
||||
func discoveryCandidates(raw string) []discoveryCandidate {
|
||||
raw = strings.TrimRight(strings.TrimSpace(raw), "/")
|
||||
if raw == "" {
|
||||
return nil
|
||||
}
|
||||
// Deduplicate on the URL that will actually be requested, not on the
|
||||
// (base, path) pair. base "https://h/v1" + path "/models" and base "https://h"
|
||||
// + path "/v1/models" both request the same address, and listing it twice in
|
||||
// an error message makes the operator think two things were tried.
|
||||
seenURL := map[string]bool{}
|
||||
seenPair := map[string]bool{}
|
||||
var out []discoveryCandidate
|
||||
add := func(base, path string, prefer int) {
|
||||
pair := base + "|" + path
|
||||
if seenPair[pair] || seenURL[base+path] {
|
||||
return
|
||||
}
|
||||
seenPair[pair] = true
|
||||
seenURL[base+path] = true
|
||||
out = append(out, discoveryCandidate{baseURL: base, modelsPath: path, prefer: prefer})
|
||||
}
|
||||
|
||||
stripped := trimVersionSuffix(raw)
|
||||
hasVersion := stripped != raw
|
||||
|
||||
// Both catalog paths are always tried, whichever form the operator typed.
|
||||
//
|
||||
// The earlier version offered "/models" only when the base already carried a
|
||||
// version segment. That left a bare host ("https://api.vendor.com") with a
|
||||
// SINGLE candidate and therefore no fallback at all — so a vendor serving
|
||||
// its catalog at the bare "/models" was reported as unreachable. Every
|
||||
// test used a "/v1"-suffixed base and so never saw it; siliconflow's
|
||||
// "/v1/models" happens to answer, which is why the gap survived review.
|
||||
//
|
||||
// Preference only decides WHICH URL is reported when several work, never
|
||||
// which ones are attempted.
|
||||
//
|
||||
// Order matters: a base that already ends in a version segment must be
|
||||
// paired with the RELATIVE "/models" first, because pairing it with
|
||||
// "/v1/models" produces "…/v1/v1/models" — a URL that is 404 by construction.
|
||||
// Trying it first would spend a round trip on a certain miss and, worse,
|
||||
// make the reported "tried" list lead with nonsense.
|
||||
if hasVersion {
|
||||
add(raw, "/models", 0)
|
||||
add(raw, discoveryModelsPath, 1) // the doubled form; kept only as a last resort
|
||||
add(stripped, "/models", 2)
|
||||
add(stripped, discoveryModelsPath, 3)
|
||||
} else {
|
||||
add(raw, discoveryModelsPath, 0)
|
||||
add(raw, "/models", 1)
|
||||
}
|
||||
|
||||
// Stable insertion sort by preference; add() above is not ordered.
|
||||
for i := 1; i < len(out); i++ {
|
||||
for j := i; j > 0 && out[j].prefer < out[j-1].prefer; j-- {
|
||||
out[j], out[j-1] = out[j-1], out[j]
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// trimVersionSuffix removes a trailing "/v1" (or /v2, /v1beta, ...) so the
|
||||
// host root can be probed on its own.
|
||||
func trimVersionSuffix(base string) string {
|
||||
i := strings.LastIndex(base, "/")
|
||||
if i < 0 {
|
||||
return base
|
||||
}
|
||||
if !isVersionSegment(base[i+1:]) {
|
||||
return base
|
||||
}
|
||||
return base[:i]
|
||||
}
|
||||
|
||||
// versionSegment returns the leading path segment of a models path when that
|
||||
// segment is a version marker ("/v1/models" → "/v1", "/models" → ""). It is the
|
||||
// piece that must move between base_url and endpoint so the two compose.
|
||||
func versionSegment(modelsPath string) string {
|
||||
parts := strings.Split(strings.Trim(modelsPath, "/"), "/")
|
||||
if len(parts) != 2 || !isVersionSegment(parts[0]) {
|
||||
return ""
|
||||
}
|
||||
return "/" + parts[0]
|
||||
}
|
||||
|
||||
// isVersionSegment reports whether a path segment looks like "v1", "v2",
|
||||
// "v1beta" — and not like a real path name such as "vision" or a wildcard
|
||||
// path such as "v1x".
|
||||
//
|
||||
// The rule is: a leading "v", then digits, then an OPTIONAL suffix drawn from
|
||||
// the prerelease markers vendors actually use. A free-form alphabetic tail was
|
||||
// tried and rejected — it classifies "v1x" as a version, and that is how a
|
||||
// templated path segment would get stripped off a real base URL.
|
||||
func isVersionSegment(seg string) bool {
|
||||
if len(seg) < 2 || seg[0] != 'v' {
|
||||
return false
|
||||
}
|
||||
i := 1
|
||||
digits := 0
|
||||
for ; i < len(seg); i++ {
|
||||
if seg[i] < '0' || seg[i] > '9' {
|
||||
break
|
||||
}
|
||||
digits++
|
||||
}
|
||||
if digits == 0 {
|
||||
return false
|
||||
}
|
||||
suffix := strings.ToLower(seg[i:])
|
||||
if suffix == "" {
|
||||
return true
|
||||
}
|
||||
for _, marker := range []string{"alpha", "beta", "preview", "dev", "rc"} {
|
||||
if suffix == marker || strings.HasPrefix(suffix, "-"+marker) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// composeEndpoint derives the chat endpoint to persist, given the models path
|
||||
// that actually answered, the base_url being persisted, and the base the
|
||||
// operator typed.
|
||||
//
|
||||
// The rule is a single invariant: exactly one version segment must end up in
|
||||
// base_url + endpoint. The adapter contributes "/chat/completions" and the
|
||||
// default image path contributes "/v1/images/generations", so a version left on
|
||||
// BOTH breaks one of them — and one left on NEITHER sends chat to the wrong
|
||||
// path on vendors that require the segment.
|
||||
func composeEndpoint(modelsPath, persistedBase, typedBase string) string {
|
||||
// The version segment currently present on the persisted base.
|
||||
onBase := ""
|
||||
if b := trimVersionSuffix(persistedBase); b != persistedBase {
|
||||
onBase = persistedBase[len(b):]
|
||||
}
|
||||
// The version segment present in the models path that answered.
|
||||
inPath := versionSegment(modelsPath)
|
||||
|
||||
// The version segment actually WORKED: prefer the models path's, then the
|
||||
// one the operator typed.
|
||||
version := inPath
|
||||
if version == "" && onBase == "" {
|
||||
if b := trimVersionSuffix(typedBase); b != typedBase && typedBase != "" {
|
||||
version = typedBase[len(b):]
|
||||
}
|
||||
}
|
||||
if version == "" {
|
||||
return "" // adapter default already composes
|
||||
}
|
||||
return version + "/chat/completions"
|
||||
}
|
||||
|
||||
// composeImageEndpoint derives the image endpoint to persist.
|
||||
//
|
||||
// Provider.ImageURL is `base_url + ImageEndpoint()`, and the built-in default
|
||||
// is the absolute path "/v1/images/generations" — unlike chat's relative
|
||||
// "/chat/completions". So the two defaults do NOT agree about who owns the
|
||||
// version segment, which is the trap this function exists to close:
|
||||
//
|
||||
// base "https://h" + "/v1/images/generations" → correct
|
||||
// base "https://h/v1" + "/v1/images/generations" → "/v1/v1/…" 404
|
||||
// base "https://h/v1" + "/images/generations" → "/v1/images/generations" correct
|
||||
//
|
||||
// The second row is not a theoretical guess. Measured against the real
|
||||
// amr-link endpoint with base_url "https://amr-link.open-design.ai/v1":
|
||||
//
|
||||
// …/v1/images/generations → 200 (the image is then generated; that call
|
||||
// takes minutes, which is the upstream's own
|
||||
// latency and not a routing problem)
|
||||
// …/v1/v1/images/generations → 404
|
||||
// …/images/generations → 404
|
||||
//
|
||||
// Leaving the default in place when the base already ends in a version is
|
||||
// therefore always wrong for such an upstream, and the version-relative form
|
||||
// is the one that composes.
|
||||
func composeImageEndpoint(persistedBase string) string {
|
||||
if b := trimVersionSuffix(persistedBase); b != persistedBase {
|
||||
return "/images/generations"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// DiscoverModels probes rawURL for a model catalog. apiKey is sent as a bearer
|
||||
// token; some gateways accept an unauthenticated listing, so an empty key is
|
||||
// allowed and simply omits the header.
|
||||
//
|
||||
// It returns on the first candidate that yields a parseable catalog, so the
|
||||
// reported latency is that of the working path, not the sum of the failures.
|
||||
func DiscoverModels(ctx context.Context, rawURL, apiKey string) (*discoveryResult, error) {
|
||||
cands := discoveryCandidates(rawURL)
|
||||
if len(cands) == 0 {
|
||||
return nil, fmt.Errorf("base url required")
|
||||
}
|
||||
|
||||
client := &http.Client{Timeout: discoveryTimeout}
|
||||
var tried []string
|
||||
var firstErr string
|
||||
var authErr string
|
||||
typedBase := strings.TrimRight(strings.TrimSpace(rawURL), "/")
|
||||
|
||||
var lastErr string
|
||||
for _, c := range cands {
|
||||
full := c.baseURL + c.modelsPath
|
||||
tried = append(tried, full)
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, full, nil)
|
||||
if err != nil {
|
||||
firstErr = err.Error()
|
||||
continue
|
||||
}
|
||||
req.Header.Set("Accept", "application/json")
|
||||
if apiKey != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+apiKey)
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
lastErr = classifyTransport(err)
|
||||
if lastErr == "" {
|
||||
lastErr = err.Error()
|
||||
}
|
||||
continue
|
||||
}
|
||||
body, readErr := io.ReadAll(io.LimitReader(resp.Body, discoveryMaxBody))
|
||||
resp.Body.Close()
|
||||
took := time.Since(start)
|
||||
if readErr != nil {
|
||||
firstErr = readErr.Error()
|
||||
continue
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
// An auth rejection is the operator's key being wrong, not the
|
||||
// upstream being down. Keep it: it is the single most common
|
||||
// failure and 502 would send them to the wrong place.
|
||||
if af := classifyAuthStatus(resp.StatusCode, body); af != "" {
|
||||
authErr = af
|
||||
continue
|
||||
}
|
||||
firstErr = fmt.Sprintf("%s returned %d", full, resp.StatusCode)
|
||||
continue
|
||||
}
|
||||
|
||||
models, err := parseModelCatalog(body)
|
||||
if err != nil {
|
||||
firstErr = fmt.Sprintf("%s: %v", full, err)
|
||||
continue
|
||||
}
|
||||
|
||||
res := &discoveryResult{
|
||||
BaseURL: c.baseURL,
|
||||
ModelsPath: c.modelsPath,
|
||||
Adapter: "openai",
|
||||
Models: []string{},
|
||||
ImageModels: []string{},
|
||||
Tried: append([]string{}, tried...),
|
||||
LatencyMS: took.Milliseconds(),
|
||||
}
|
||||
// Persist a base/endpoint pair that composes correctly. The adapter's
|
||||
// own endpoint is "/chat/completions", so any version segment has to be
|
||||
// carried on the endpoint rather than left on the base (where it doubles
|
||||
// the default image path into "/v1/v1/images/generations" — the exact
|
||||
// 404 seen on the first vela configuration attempt).
|
||||
res.Endpoint = composeEndpoint(c.modelsPath, c.baseURL, typedBase)
|
||||
res.ImageEndpoint = composeImageEndpoint(c.baseURL)
|
||||
|
||||
// If the operator typed a version segment and we are persisting a base
|
||||
// that no longer has it, the endpoint above is what restores it. If the
|
||||
// base kept the segment, the adapter default is already correct.
|
||||
|
||||
seen := map[string]bool{}
|
||||
for _, m := range models {
|
||||
if m.ID == "" || seen[m.ID] {
|
||||
continue
|
||||
}
|
||||
seen[m.ID] = true
|
||||
if m.Kind == "image" {
|
||||
res.ImageModels = append(res.ImageModels, m.ID)
|
||||
} else {
|
||||
res.Models = append(res.Models, m.ID)
|
||||
}
|
||||
}
|
||||
sort.Strings(res.Models)
|
||||
sort.Strings(res.ImageModels)
|
||||
if len(res.Models) == 0 && len(res.ImageModels) == 0 {
|
||||
firstErr = fmt.Sprintf("%s: catalog contained no usable model ids", full)
|
||||
continue
|
||||
}
|
||||
if c.baseURL != typedBase {
|
||||
res.Note = fmt.Sprintf("base url normalized from %s to %s; endpoint set to %q so it composes with the adapter",
|
||||
typedBase, c.baseURL, res.Endpoint)
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
return nil, buildDiscoveryError(tried, authErr, firstErr, lastErr)
|
||||
}
|
||||
|
||||
// classifyAuthStatus turns a 401/403 from the catalog endpoint into a message
|
||||
// that names the credential as the cause.
|
||||
//
|
||||
// This distinction is the whole point: a wrong key and an unreachable vendor
|
||||
// are the two most common discovery failures, and they need opposite actions
|
||||
// (re-type the key vs. go check the vendor). Reporting both as "upstream
|
||||
// error" makes the operator guess.
|
||||
func classifyAuthStatus(status int, body []byte) string {
|
||||
if status != http.StatusUnauthorized && status != http.StatusForbidden {
|
||||
return ""
|
||||
}
|
||||
snippet := strings.TrimSpace(string(body))
|
||||
// Vendors put the reason in wildly different shapes; a compact quote is
|
||||
// more useful than a parse attempt that fails on most of them.
|
||||
snippet = strings.Join(strings.Fields(snippet), " ")
|
||||
if len(snippet) > 200 {
|
||||
snippet = snippet[:200] + "…"
|
||||
}
|
||||
return fmt.Sprintf("upstream rejected the api key (HTTP %d): %s — "+
|
||||
"check the key, not the endpoint", status, snippet)
|
||||
}
|
||||
|
||||
// classifyTransport turns low-level network failures into something an
|
||||
// operator can act on. An empty result means "no specific advice", and the
|
||||
// caller keeps the raw error.
|
||||
func classifyTransport(err error) string {
|
||||
if err == nil {
|
||||
return ""
|
||||
}
|
||||
msg := err.Error()
|
||||
lower := strings.ToLower(msg)
|
||||
switch {
|
||||
case strings.Contains(lower, "no such host"):
|
||||
return "hostname does not resolve: " + msg
|
||||
case strings.Contains(lower, "certificate"), strings.Contains(lower, "tls"):
|
||||
return "TLS verification failed: " + msg
|
||||
case strings.Contains(lower, "connection refused"):
|
||||
return "connection refused: " + msg
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// buildDiscoveryError assembles the final error, preferring the most specific
|
||||
// diagnosis available. Every URL tried is listed: an operator staring at a
|
||||
// failed probe needs to know the search actually happened, not just the last
|
||||
// attempt.
|
||||
func buildDiscoveryError(tried []string, authErr, firstErr, lastErr string) error {
|
||||
triedPart := "no model catalog found"
|
||||
if len(tried) > 0 {
|
||||
triedPart = "no model catalog found; tried " + strings.Join(tried, ", ")
|
||||
}
|
||||
if authErr != "" {
|
||||
return &discoveryFailure{status: http.StatusUnauthorized, msg: authErr + " (" + triedPart + ")"}
|
||||
}
|
||||
detail := firstErr
|
||||
if detail == "" {
|
||||
detail = lastErr
|
||||
}
|
||||
if detail == "" {
|
||||
return errors.New(triedPart)
|
||||
}
|
||||
if lastErr != "" && lastErr != firstErr {
|
||||
detail += " (last: " + lastErr + ")"
|
||||
}
|
||||
return &discoveryFailure{status: http.StatusBadGateway, msg: triedPart + "; " + detail}
|
||||
}
|
||||
|
||||
// upstreamModel is the subset of a /models entry this code reads. Vendors add
|
||||
// fields freely and rename the nesting; only id is universal, and the modality
|
||||
// hints below are read opportunistically.
|
||||
type modelCaps struct {
|
||||
ImageGeneration bool `json:"image_generation"`
|
||||
Vision bool `json:"vision"`
|
||||
}
|
||||
|
||||
type upstreamModel struct {
|
||||
ID string `json:"id"`
|
||||
Object string `json:"object"`
|
||||
Kind string `json:"kind"`
|
||||
Type string `json:"type"`
|
||||
Modality string `json:"modality"`
|
||||
Architecture struct {
|
||||
InputModalities []string `json:"input_modalities"`
|
||||
OutputModalities []string `json:"output_modalities"`
|
||||
} `json:"architecture"`
|
||||
Capabilities modelCaps `json:"capabilities"`
|
||||
|
||||
// Metadata is kept as raw JSON on purpose. The real amr-link catalog nests
|
||||
// cost/capability/context_limit here, and a typed struct would silently
|
||||
// discard every key this build does not know about — the operator would
|
||||
// then lose the context limit their vendor published. Raw also lets
|
||||
// input_modalities/output_modalities be read without a second type.
|
||||
Metadata json.RawMessage `json:"metadata"`
|
||||
}
|
||||
|
||||
// metadataFields lists the keys worth surfacing in the UI. They are copied
|
||||
// through verbatim and nothing is inferred from them; an upstream that sends
|
||||
// no context limit must leave that absent rather than have this code write 0.
|
||||
var metadataFields = []string{
|
||||
"context_length", "context_window", "max_context_length",
|
||||
"context_limit", "context_budget", "cost", "capability",
|
||||
}
|
||||
|
||||
// parseModelCatalog reads the OpenAI list shape {"data":[...]} and the bare
|
||||
// array shape some gateways return. A response that is valid JSON but has
|
||||
// neither shape is an error, not an empty catalog: reporting "0 models" for a
|
||||
// login page or a rate-limit JSON would silently create an empty source.
|
||||
func parseModelCatalog(body []byte) ([]discoveredModel, error) {
|
||||
// "null" unmarshals into a nil slice without error, and an HTML login page
|
||||
// fails to unmarshal at all. Both must be errors.
|
||||
trimmed := strings.TrimSpace(string(body))
|
||||
if trimmed == "" || trimmed == "null" {
|
||||
return nil, fmt.Errorf("empty response")
|
||||
}
|
||||
|
||||
var wrapper struct {
|
||||
Data []upstreamModel `json:"data"`
|
||||
Models []upstreamModel `json:"models"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &wrapper); err == nil {
|
||||
if len(wrapper.Data) > 0 {
|
||||
return toDiscovered(wrapper.Data), nil
|
||||
}
|
||||
if len(wrapper.Models) > 0 {
|
||||
return toDiscovered(wrapper.Models), nil
|
||||
}
|
||||
// A well-formed but empty catalog is a legitimate answer from a key
|
||||
// that has no models provisioned. Distinguish it from "not a catalog"
|
||||
// by checking the shape actually parsed.
|
||||
var probe map[string]json.RawMessage
|
||||
if json.Unmarshal(body, &probe) == nil {
|
||||
if _, ok := probe["data"]; ok {
|
||||
return nil, nil
|
||||
}
|
||||
if _, ok := probe["models"]; ok {
|
||||
return nil, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
var bare []upstreamModel
|
||||
if err := json.Unmarshal(body, &bare); err == nil {
|
||||
return toDiscovered(bare), nil
|
||||
}
|
||||
return nil, fmt.Errorf("response is not a model catalog")
|
||||
}
|
||||
|
||||
func toDiscovered(in []upstreamModel) []discoveredModel {
|
||||
out := make([]discoveredModel, 0, len(in))
|
||||
for _, m := range in {
|
||||
out = append(out, discoveredModel{
|
||||
ID: m.ID,
|
||||
Kind: discoveredModelKind(m),
|
||||
Meta: modelMeta(m),
|
||||
})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// discoveredModelKind trusts the upstream's own modality fields and nothing
|
||||
// else. It never infers from the model NAME: ids like "gpt-image-1" or
|
||||
// "ideogram-v2" appear on catalogs that also serve chat, and a wrong kind
|
||||
// poisons the AUTO chain (an image-only id selected for a chat request fails
|
||||
// every tier behind it).
|
||||
func discoveredModelKind(m upstreamModel) string {
|
||||
modIn := append(append([]string{}, m.Architecture.InputModalities...),
|
||||
modalityList(m.Metadata, "input_modalities")...)
|
||||
modOut := append(append([]string{}, m.Architecture.OutputModalities...),
|
||||
modalityList(m.Metadata, "output_modalities")...)
|
||||
|
||||
has := func(list []string, want string) bool {
|
||||
for _, v := range list {
|
||||
if strings.EqualFold(strings.TrimSpace(v), want) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
if m.Capabilities.ImageGeneration || has(modOut, "image") {
|
||||
return "image"
|
||||
}
|
||||
if has(modIn, "image") || has(modIn, "file") || has(modIn, "audio") || m.Capabilities.Vision {
|
||||
return "chat" // multimodal input still means a chat model
|
||||
}
|
||||
// Some catalogs encode modality as a top-level string instead of a list.
|
||||
// Only the image-ish spellings count: "chat" here means the upstream said
|
||||
// chat, and treating that as "unknown" would ignore its statement.
|
||||
switch strings.ToLower(strings.TrimSpace(m.Kind + " " + m.Modality + " " + m.Type)) {
|
||||
case "image", "text-to-image", "image_generation", "text_to_image":
|
||||
return "image"
|
||||
}
|
||||
return "chat"
|
||||
}
|
||||
|
||||
// modalityList reads one array-of-strings field out of a raw JSON object,
|
||||
// tolerating both absence and a non-array value.
|
||||
func modalityList(raw json.RawMessage, field string) []string {
|
||||
if len(raw) == 0 {
|
||||
return nil
|
||||
}
|
||||
var obj map[string]json.RawMessage
|
||||
if err := json.Unmarshal(raw, &obj); err != nil {
|
||||
return nil
|
||||
}
|
||||
v, ok := obj[field]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
var out []string
|
||||
if err := json.Unmarshal(v, &out); err != nil {
|
||||
return nil
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// modelMeta carries through the fields an operator wants to see in the UI
|
||||
// without this code inventing them. The upstream nests most of them under
|
||||
// "metadata" (the real amr-link catalog does) while some put the same keys at
|
||||
// the top level, so both are read and top level wins on a tie. Only keys the
|
||||
// upstream actually sent are copied, so an absent context limit stays absent
|
||||
// instead of becoming 0.
|
||||
func modelMeta(m upstreamModel) map[string]interface{} {
|
||||
meta := map[string]interface{}{}
|
||||
|
||||
copyFields := func(raw json.RawMessage) {
|
||||
if len(raw) == 0 {
|
||||
return
|
||||
}
|
||||
var obj map[string]json.RawMessage
|
||||
if err := json.Unmarshal(raw, &obj); err != nil {
|
||||
return
|
||||
}
|
||||
for _, k := range metadataFields {
|
||||
if v, ok := obj[k]; ok {
|
||||
if dec := rawOrString(v); dec != nil {
|
||||
meta[k] = dec
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
copyFields(m.Metadata) // nested first
|
||||
copyFields(mustJSON(m)) // top level wins
|
||||
if len(meta) == 0 {
|
||||
return nil
|
||||
}
|
||||
return meta
|
||||
}
|
||||
|
||||
// rawOrString keeps numeric metadata as a number and everything else as its
|
||||
// decoded string, so the UI can sort on a cost or context length instead of
|
||||
// receiving "1048000" and having to coerce it.
|
||||
func rawOrString(raw json.RawMessage) interface{} {
|
||||
var num json.Number
|
||||
if err := json.Unmarshal(raw, &num); err == nil {
|
||||
if f, err := num.Float64(); err == nil {
|
||||
return f
|
||||
}
|
||||
}
|
||||
var s string
|
||||
if err := json.Unmarshal(raw, &s); err == nil {
|
||||
return s
|
||||
}
|
||||
var any interface{}
|
||||
if err := json.Unmarshal(raw, &any); err == nil {
|
||||
return any
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func mustJSON(v interface{}) []byte {
|
||||
b, _ := json.Marshal(v)
|
||||
return b
|
||||
}
|
||||
|
||||
// modelsToConfig converts discovery output into config.Model entries, applying
|
||||
// the same priority convention the UI uses: chat models the operator is
|
||||
// likely to want first get a lower number, and image models sort last because
|
||||
// they are unreachable from /v1/chat/completions.
|
||||
func modelsToConfig(chat, image []string) []config.Model {
|
||||
out := make([]config.Model, 0, len(chat)+len(image))
|
||||
for _, id := range chat {
|
||||
out = append(out, config.Model{ID: id, Priority: 50, Kind: "chat"})
|
||||
}
|
||||
for _, id := range image {
|
||||
out = append(out, config.Model{ID: id, Priority: 70, Kind: "image"})
|
||||
}
|
||||
return out
|
||||
}
|
||||
403
internal/gateway/discover_api.go
Normal file
403
internal/gateway/discover_api.go
Normal file
@ -0,0 +1,403 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"llmsproxy/internal/config"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// HTTP surface for model discovery.
|
||||
//
|
||||
// Two endpoints, deliberately separate:
|
||||
//
|
||||
// POST /api/sources/discover — probe a URL+key, return the catalog.
|
||||
// Nothing is written.
|
||||
// POST /api/sources/discover/apply — probe, then write the source.
|
||||
//
|
||||
// The split exists because discovery mutates nothing and is therefore safe to
|
||||
// call repeatedly while the operator is still typing a key, whereas apply
|
||||
// rewrites credentials and the AUTO chain's inputs. Making them one endpoint
|
||||
// with a flag would mean a "just check it" click can silently create a source.
|
||||
//
|
||||
// Both are admin-only, matching /api/sources: the request body carries an API
|
||||
// key, and a model catalog reveals which upstreams an operator uses.
|
||||
|
||||
// discoverRequest is the body of both endpoints.
|
||||
type discoverRequest struct {
|
||||
// BaseURL is what the operator pasted. It may or may not include a
|
||||
// version segment; discovery works that out rather than making the
|
||||
// caller know this gateway's path-composition rules.
|
||||
BaseURL string `json:"base_url"`
|
||||
APIKey string `json:"api_key"`
|
||||
|
||||
// Name is required for apply, ignored for discover. It defaults from the
|
||||
// base URL's host when omitted, because "https://api.vendor.com/v1" is a
|
||||
// better starting name than "new-source".
|
||||
Name string `json:"name"`
|
||||
|
||||
// Adapter is optional. Discovery assumes "openai" because that is what a
|
||||
// /v1/models catalog implies; a non-OpenAI upstream (Anthropic, Gemini)
|
||||
// has no such endpoint, so discovery will legitimately fail for those and
|
||||
// the operator configures them by hand.
|
||||
Adapter string `json:"adapter"`
|
||||
|
||||
// Temperature and MaxConcurrent are carried into the written source.
|
||||
Temperature float64 `json:"temperature"`
|
||||
MaxConcurrent int `json:"max_concurrent"`
|
||||
|
||||
// ReplaceModels drops the source's existing model list before applying the
|
||||
// discovered one. It defaults to true for a NEW source and is required to
|
||||
// be spelled out for an existing one, so a re-discovery cannot silently
|
||||
// delete hand-tuned priorities.
|
||||
ReplaceModels *bool `json:"replace_models,omitempty"`
|
||||
|
||||
// Select narrows what gets written. Empty means everything discovered.
|
||||
// Ids that are not in the catalog are an error rather than a silent skip:
|
||||
// a typo here would produce a source that 404s on every request.
|
||||
Include []string `json:"include,omitempty"`
|
||||
Exclude []string `json:"exclude,omitempty"`
|
||||
}
|
||||
|
||||
// discoverResponse is the JSON the WebUI binds to.
|
||||
type discoverResponse struct {
|
||||
OK bool `json:"ok"`
|
||||
|
||||
// Normalized source fields, ready to be echoed back to /api/sources.
|
||||
BaseURL string `json:"base_url"`
|
||||
Adapter string `json:"adapter"`
|
||||
Models []string `json:"models"`
|
||||
ImageModels []string `json:"image_models"`
|
||||
|
||||
// Endpoint is set only when discovery had to move a version segment.
|
||||
Endpoint string `json:"endpoint"`
|
||||
// ImageEndpoint is required whenever base_url carries a version segment:
|
||||
// the gateway's default image path is the ABSOLUTE "/v1/images/generations"
|
||||
// while chat's default is the relative "/chat/completions", so the same
|
||||
// base_url that yields a working chat URL yields a 404 image URL.
|
||||
ImageEndpoint string `json:"image_endpoint"`
|
||||
|
||||
// Diagnostics for the operator.
|
||||
ModelsPath string `json:"models_path"`
|
||||
Tried []string `json:"tried"`
|
||||
LatencyMS int64 `json:"latency_ms"`
|
||||
Note string `json:"note,omitempty"`
|
||||
Warnings []string `json:"warnings,omitempty"`
|
||||
|
||||
// Set by apply only.
|
||||
SourceName string `json:"source_name,omitempty"`
|
||||
Applied bool `json:"applied,omitempty"`
|
||||
}
|
||||
|
||||
func (g *Gateway) handleSourceDiscoverAPI(w http.ResponseWriter, r *http.Request) {
|
||||
if reqRole(r.Context()) != "admin" {
|
||||
writeError(w, http.StatusForbidden, "forbidden", "admin role required")
|
||||
return
|
||||
}
|
||||
apply := strings.HasSuffix(r.URL.Path, "/apply")
|
||||
if r.Method != http.MethodPost {
|
||||
w.Header().Set("Allow", "POST")
|
||||
writeError(w, http.StatusMethodNotAllowed, "method_not_allowed",
|
||||
"use POST with {\"base_url\":…,\"api_key\":…}")
|
||||
return
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(io.LimitReader(r.Body, 1<<20))
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid_request", "cannot read body")
|
||||
return
|
||||
}
|
||||
var req discoverRequest
|
||||
if err := json.Unmarshal(body, &req); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid_request", "invalid json: "+err.Error())
|
||||
return
|
||||
}
|
||||
req.BaseURL = strings.TrimSpace(req.BaseURL)
|
||||
if req.BaseURL == "" {
|
||||
writeError(w, http.StatusBadRequest, "invalid_request", "base_url required")
|
||||
return
|
||||
}
|
||||
if req.Adapter == "" {
|
||||
req.Adapter = "openai"
|
||||
}
|
||||
|
||||
// Bounded so a hung upstream cannot pin the handler; discovery has its own
|
||||
// per-request timeout on top.
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 60*time.Second)
|
||||
defer cancel()
|
||||
|
||||
res, err := DiscoverModels(ctx, req.BaseURL, req.APIKey)
|
||||
if err != nil {
|
||||
// A rejected key must not be reported as an upstream outage: the two
|
||||
// need opposite actions from the operator, and 502 sends them to the
|
||||
// wrong one.
|
||||
status := http.StatusBadGateway
|
||||
var f *discoveryFailure
|
||||
if errors.As(err, &f) {
|
||||
status = f.StatusOrDefault()
|
||||
}
|
||||
writeError(w, status, "discover_failed", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
chat, image, warns, err := selectModels(res.Models, res.ImageModels, req.Include, req.Exclude)
|
||||
if err != nil {
|
||||
// 400: the operator asked for a model the catalog does not have.
|
||||
writeError(w, http.StatusBadRequest, "invalid_request", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
out := &discoverResponse{
|
||||
OK: true,
|
||||
BaseURL: res.BaseURL,
|
||||
Adapter: req.Adapter,
|
||||
Models: chat,
|
||||
ImageModels: image,
|
||||
Endpoint: res.Endpoint,
|
||||
ImageEndpoint: res.ImageEndpoint,
|
||||
ModelsPath: res.ModelsPath,
|
||||
Tried: res.Tried,
|
||||
LatencyMS: res.LatencyMS,
|
||||
Note: res.Note,
|
||||
Warnings: warns,
|
||||
}
|
||||
if len(chat) == 0 && len(image) == 0 {
|
||||
out.Warnings = append(out.Warnings,
|
||||
"every discovered model was excluded; applying this would create a source with no models")
|
||||
}
|
||||
|
||||
if !apply {
|
||||
writeJSON(w, http.StatusOK, out)
|
||||
return
|
||||
}
|
||||
|
||||
name := strings.TrimSpace(req.Name)
|
||||
if name == "" {
|
||||
name = defaultSourceName(req.BaseURL)
|
||||
}
|
||||
if name == "" {
|
||||
writeError(w, http.StatusBadRequest, "invalid_request",
|
||||
"name required: cannot derive one from base_url")
|
||||
return
|
||||
}
|
||||
if strings.ContainsAny(name, "/\\ \t") || name == "." || name == ".." {
|
||||
writeError(w, http.StatusBadRequest, "invalid_request",
|
||||
"source name must not contain slashes or whitespace")
|
||||
return
|
||||
}
|
||||
|
||||
exists := g.sourceExists(name)
|
||||
if exists && req.ReplaceModels == nil {
|
||||
// Refusing here is deliberate. Re-running discovery on an existing
|
||||
// source and having it silently replace a hand-tuned model list —
|
||||
// including priorities chosen for the AUTO chain — is the kind of
|
||||
// "helpful" behaviour that loses an afternoon.
|
||||
writeError(w, http.StatusConflict, "conflict",
|
||||
"source "+name+" already exists; set replace_models=true to overwrite its model list, "+
|
||||
"or choose a different name")
|
||||
return
|
||||
}
|
||||
|
||||
// Build the source directly rather than round-tripping through the
|
||||
// /api/sources payload: resolveAPIKey carries the "an empty api_key means
|
||||
// keep the stored one" rule, and going through a second decoder here would
|
||||
// duplicate (and eventually diverge from) that logic.
|
||||
src := config.Source{
|
||||
Name: name,
|
||||
BaseURL: res.BaseURL,
|
||||
APIKey: g.resolveAPIKey(name, req.APIKey, exists),
|
||||
Adapter: req.Adapter,
|
||||
Endpoint: res.Endpoint,
|
||||
ImageEndpoint: res.ImageEndpoint,
|
||||
Models: modelsToConfig(chat, image),
|
||||
Temperature: req.Temperature,
|
||||
}
|
||||
if req.MaxConcurrent > 0 {
|
||||
src.MaxConcurrent = req.MaxConcurrent
|
||||
}
|
||||
|
||||
// An existing source may carry fields discovery does not know about
|
||||
// (proxy_url, headers, api_key_env, timeout). Rebuilding from scratch would
|
||||
// drop them, so they are inherited unless the request overrides them.
|
||||
if exists {
|
||||
old := g.sourceByName(name)
|
||||
if old != nil {
|
||||
src.ProxyURL = old.ProxyURL
|
||||
src.APIKeyEnv = old.APIKeyEnv
|
||||
src.Timeout = old.Timeout
|
||||
src.QueueTimeout = old.QueueTimeout
|
||||
src.Headers = old.Headers
|
||||
src.Meta = old.Meta
|
||||
src.MaxTokens = old.MaxTokens
|
||||
src.RPM = old.RPM
|
||||
// image_endpoint is NOT inherited when discovery supplied one:
|
||||
// the stored value was correct for the OLD base_url, and the base
|
||||
// is exactly what may have changed. Inheriting it would keep a
|
||||
// stale value that 404s against the new base.
|
||||
if res.ImageEndpoint == "" {
|
||||
src.ImageEndpoint = old.ImageEndpoint
|
||||
}
|
||||
if src.Adapter == "" {
|
||||
src.Adapter = old.Adapter
|
||||
}
|
||||
if req.MaxConcurrent <= 0 {
|
||||
src.MaxConcurrent = old.MaxConcurrent
|
||||
}
|
||||
// replace_models=false keeps the curated list. Discovery still
|
||||
// refreshed base_url/endpoint/credential, which is the point of
|
||||
// re-running it.
|
||||
if req.ReplaceModels != nil && !*req.ReplaceModels {
|
||||
src.Models = old.Models
|
||||
out.Warnings = append(out.Warnings,
|
||||
"model list left unchanged (replace_models=false)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err := g.core.AddSource(src); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "source_error", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
out.SourceName = name
|
||||
out.Applied = true
|
||||
writeJSON(w, http.StatusOK, out)
|
||||
}
|
||||
|
||||
// selectModels applies the include/exclude filters.
|
||||
//
|
||||
// An include entry the catalog does not have is an error: silently dropping it
|
||||
// produces a source that looks configured and 404s at request time, which is
|
||||
// far harder to diagnose than a rejected apply. An exclude entry that is not
|
||||
// present is fine — the operator is allowed to be defensive.
|
||||
func selectModels(chat, image, include, exclude []string) ([]string, []string, []string, error) {
|
||||
have := map[string]bool{}
|
||||
for _, m := range chat {
|
||||
have[m] = true
|
||||
}
|
||||
for _, m := range image {
|
||||
have[m] = true
|
||||
}
|
||||
|
||||
if len(include) > 0 {
|
||||
var missing []string
|
||||
for _, want := range include {
|
||||
if !have[want] {
|
||||
missing = append(missing, want)
|
||||
}
|
||||
}
|
||||
if len(missing) > 0 {
|
||||
return nil, nil, nil, &discoverError{
|
||||
msg: "these models are not in the upstream catalog: " + strings.Join(missing, ", "),
|
||||
}
|
||||
}
|
||||
keep := map[string]bool{}
|
||||
for _, want := range include {
|
||||
keep[want] = true
|
||||
}
|
||||
var c, i []string
|
||||
for _, m := range chat {
|
||||
if keep[m] {
|
||||
c = append(c, m)
|
||||
}
|
||||
}
|
||||
for _, m := range image {
|
||||
if keep[m] {
|
||||
i = append(i, m)
|
||||
}
|
||||
}
|
||||
chat, image = c, i
|
||||
}
|
||||
|
||||
if len(exclude) > 0 {
|
||||
drop := map[string]bool{}
|
||||
for _, m := range exclude {
|
||||
drop[m] = true
|
||||
}
|
||||
var c, i []string
|
||||
for _, m := range chat {
|
||||
if !drop[m] {
|
||||
c = append(c, m)
|
||||
}
|
||||
}
|
||||
for _, m := range image {
|
||||
if !drop[m] {
|
||||
i = append(i, m)
|
||||
}
|
||||
}
|
||||
chat, image = c, i
|
||||
}
|
||||
|
||||
// chat and image must stay non-nil: the JSON contract forbids null here.
|
||||
if chat == nil {
|
||||
chat = []string{}
|
||||
}
|
||||
if image == nil {
|
||||
image = []string{}
|
||||
}
|
||||
|
||||
var warns []string
|
||||
if len(chat) == 0 && len(image) == 0 {
|
||||
// Caller can only get here by excluding everything, or from a catalog
|
||||
// that was already empty. Either way "silence" would read as success.
|
||||
warns = append(warns,
|
||||
"no models selected; applying this would create a source that cannot serve any request")
|
||||
return chat, image, warns, nil
|
||||
}
|
||||
if len(image) > 0 && len(chat) == 0 {
|
||||
warns = append(warns,
|
||||
"every model is an image model; AUTO chat requests have nothing to select")
|
||||
}
|
||||
if len(chat) > 0 && len(image) > 0 {
|
||||
warns = append(warns,
|
||||
"image models are only reachable through /v1/images/generations, not AUTO chat")
|
||||
}
|
||||
return chat, image, warns, nil
|
||||
}
|
||||
|
||||
// discoverError is a client mistake rather than an upstream failure, which is
|
||||
// why it gets its own type instead of fmt.Errorf.
|
||||
type discoverError struct{ msg string }
|
||||
|
||||
func (e *discoverError) Error() string { return e.msg }
|
||||
|
||||
// defaultSourceName derives a source name from a base URL host, stripping the
|
||||
// version segment and any "api." prefix. It returns "" for input that is not a
|
||||
// URL at all — the caller must not fall back to echoing garbage into a source
|
||||
// name, since the name becomes a config key and an API path segment.
|
||||
func defaultSourceName(baseURL string) string {
|
||||
s := strings.TrimSpace(baseURL)
|
||||
if !strings.Contains(s, "://") {
|
||||
return ""
|
||||
}
|
||||
s = s[strings.Index(s, "://")+3:]
|
||||
if i := strings.IndexAny(s, "/?#"); i >= 0 {
|
||||
s = s[:i]
|
||||
}
|
||||
if i := strings.LastIndex(s, ":"); i > 0 && !strings.Contains(s[i+1:], "]") {
|
||||
s = s[:i] // strip port
|
||||
}
|
||||
// A host must still look like one after trimming the cosmetic prefixes.
|
||||
s = strings.TrimPrefix(s, "api.")
|
||||
s = strings.TrimPrefix(s, "www.")
|
||||
if s == "" || strings.ContainsAny(s, " \t\"'<>") {
|
||||
return ""
|
||||
}
|
||||
for _, r := range s {
|
||||
switch {
|
||||
case r >= 'a' && r <= 'z', r >= 'A' && r <= 'Z', r >= '0' && r <= '9':
|
||||
case r == '.' || r == '-' || r == '_':
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
if !strings.Contains(s, ".") {
|
||||
// A bare label is far more likely to be a mistake than a hostname.
|
||||
return ""
|
||||
}
|
||||
return s
|
||||
}
|
||||
314
internal/gateway/discover_api_test.go
Normal file
314
internal/gateway/discover_api_test.go
Normal file
@ -0,0 +1,314 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"llmsproxy/internal/config"
|
||||
)
|
||||
|
||||
// upstream that answers /v1/models with the real captured catalog
|
||||
func catalogUpstream(t *testing.T) *httptest.Server {
|
||||
t.Helper()
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/v1/models" {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
if got := r.Header.Get("Authorization"); got != "Bearer sk-secret" {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
_, _ = w.Write([]byte(realCatalogBody))
|
||||
}))
|
||||
t.Cleanup(srv.Close)
|
||||
return srv
|
||||
}
|
||||
|
||||
func TestDefaultSourceName(t *testing.T) {
|
||||
cases := map[string]string{
|
||||
"https://api.vendor.com/v1": "vendor.com",
|
||||
"https://api.openai.com/v1": "openai.com",
|
||||
"https://www.vendor.com/v1": "vendor.com",
|
||||
"http://127.0.0.1:8080/v1": "127.0.0.1",
|
||||
"https://amr-link.open-design.ai/v1": "amr-link.open-design.ai",
|
||||
"not a url at all": "",
|
||||
}
|
||||
for in, want := range cases {
|
||||
if got := defaultSourceName(in); got != want {
|
||||
t.Errorf("defaultSourceName(%q) = %q, want %q", in, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSelectModels_IncludeUnknownIsAnError(t *testing.T) {
|
||||
chat := []string{"a", "b"}
|
||||
image := []string{"img"}
|
||||
// Silently dropping a typo'd id would create a source that looks
|
||||
// configured and 404s on every request — much worse than a rejection.
|
||||
_, _, _, err := selectModels(chat, image, []string{"a", "typo"}, nil)
|
||||
if err == nil {
|
||||
t.Fatal("expected an error for an include id that is not in the catalog")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "typo") {
|
||||
t.Errorf("error should name the offending id, got %q", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSelectModels_IncludeNarrows(t *testing.T) {
|
||||
chat, image, _, err := selectModels([]string{"a", "b", "c"}, []string{"img1", "img2"}, []string{"b", "img1"}, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(chat) != 1 || chat[0] != "b" {
|
||||
t.Errorf("chat = %v, want [b]", chat)
|
||||
}
|
||||
if len(image) != 1 || image[0] != "img1" {
|
||||
t.Errorf("image = %v, want [img1]", image)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSelectModels_ExcludeUnknownIsFine(t *testing.T) {
|
||||
chat, _, _, err := selectModels([]string{"a"}, nil, nil, []string{"never-existed"})
|
||||
if err != nil {
|
||||
t.Errorf("excluding a model that is not present should be allowed: %v", err)
|
||||
}
|
||||
if len(chat) != 1 {
|
||||
t.Errorf("chat = %v, want [a] unchanged", chat)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSelectModels_NeverReturnsNil(t *testing.T) {
|
||||
chat, image, warns, err := selectModels(nil, nil, nil, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if chat == nil || image == nil {
|
||||
t.Fatal("nil slices would serialize as JSON null and break the UI")
|
||||
}
|
||||
if len(warns) == 0 {
|
||||
t.Error("an empty catalog deserves a warning, not silence")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSelectModels_WarnsAboutImageOnlyCatalog(t *testing.T) {
|
||||
_, _, warns, err := selectModels(nil, []string{"gpt-image-2"}, nil, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
joined := strings.Join(warns, " ")
|
||||
if !strings.Contains(joined, "AUTO") {
|
||||
t.Errorf("warnings %v should say AUTO has nothing to select", warns)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSelectModels_WarnsThatImagesAreNotInAUTO(t *testing.T) {
|
||||
_, _, warns, err := selectModels([]string{"a"}, []string{"img"}, nil, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(strings.Join(warns, " "), "images/generations") {
|
||||
t.Errorf("warnings %v should point at the image endpoint", warns)
|
||||
}
|
||||
}
|
||||
|
||||
// TestDiscoveryResultAndConfigRoundTrip checks the fields the WebUI will echo
|
||||
// back actually survive into a config.Source — a mismatch here means the
|
||||
// operator previews one set of values and saves another.
|
||||
func TestDiscoveryResultAndConfigRoundTrip(t *testing.T) {
|
||||
res := &discoveryResult{
|
||||
BaseURL: "https://h",
|
||||
Endpoint: "/v1/chat/completions",
|
||||
Models: []string{"m1", "m2"},
|
||||
ImageModels: []string{"img1"},
|
||||
}
|
||||
src := config.Source{
|
||||
Name: "x",
|
||||
BaseURL: res.BaseURL,
|
||||
Endpoint: res.Endpoint,
|
||||
Models: modelsToConfig(res.Models, res.ImageModels),
|
||||
}
|
||||
if len(src.Models) != 3 {
|
||||
t.Fatalf("models = %d, want 3", len(src.Models))
|
||||
}
|
||||
if src.Endpoint != "/v1/chat/completions" {
|
||||
t.Errorf("endpoint lost: %q", src.Endpoint)
|
||||
}
|
||||
var img int
|
||||
for _, m := range src.Models {
|
||||
if m.Kind == "image" {
|
||||
img++
|
||||
}
|
||||
}
|
||||
if img != 1 {
|
||||
t.Errorf("image models = %d, want 1", img)
|
||||
}
|
||||
}
|
||||
|
||||
// TestDiscoverModels_SendsAuthorizationHeader pins the only way the feature
|
||||
// works against a real vendor: without the bearer token every paid /models
|
||||
// endpoint returns 401.
|
||||
func TestDiscoverModels_SendsAuthorizationHeader(t *testing.T) {
|
||||
var seen string
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
seen = r.Header.Get("Authorization")
|
||||
_, _ = w.Write([]byte(`{"data":[{"id":"m"}]}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
if _, err := DiscoverModels(context.Background(), srv.URL+"/v1", "sk-secret"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if seen != "Bearer sk-secret" {
|
||||
t.Errorf("Authorization = %q, want %q", seen, "Bearer sk-secret")
|
||||
}
|
||||
}
|
||||
|
||||
// TestDiscoverModels_WrongKeyIsUnauthorizedNotBadGateway pins the distinction
|
||||
// that decides whether the operator knows what to do next.
|
||||
//
|
||||
// A rejected key and an unreachable vendor are the two most common discovery
|
||||
// failures, and they need opposite actions: re-type the key, versus go check the
|
||||
// vendor's status page. Both used to surface as one generic error, so the
|
||||
// operator had to guess.
|
||||
func TestDiscoverModels_WrongKeyIsUnauthorizedNotBadGateway(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
_, _ = w.Write([]byte(`{"error":{"message":"invalid api key","code":"invalid_api_key"}}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
_, err := DiscoverModels(context.Background(), srv.URL+"/v1", "sk-wrong")
|
||||
if err == nil {
|
||||
t.Fatal("expected a failure against a 401 endpoint")
|
||||
}
|
||||
var f *discoveryFailure
|
||||
if !errors.As(err, &f) {
|
||||
t.Fatalf("error should be classified, got %T: %v", err, err)
|
||||
}
|
||||
if got := f.StatusOrDefault(); got != http.StatusUnauthorized {
|
||||
t.Errorf("status = %d, want %d — a wrong key must not read as an upstream outage",
|
||||
got, http.StatusUnauthorized)
|
||||
}
|
||||
// The message has to name which credential is at fault, or the status code
|
||||
// alone still sends the operator to the wrong place.
|
||||
if !strings.Contains(strings.ToLower(f.Error()), "api key") {
|
||||
t.Errorf("message %q should name the api key as the cause", f.Error())
|
||||
}
|
||||
if !strings.Contains(f.Error(), "/v1/models") {
|
||||
t.Errorf("message %q should list what was tried", f.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// TestDiscoverModels_UnreachableStaysBadGateway: the classification must not
|
||||
// over-correct — an unresolvable host is still the vendor's or the network's
|
||||
// problem, not the key's.
|
||||
func TestDiscoverModels_UnreachableStaysBadGateway(t *testing.T) {
|
||||
_, err := DiscoverModels(context.Background(), "https://no-such-host-xyz.invalid/v1", "k")
|
||||
if err == nil {
|
||||
t.Fatal("expected a failure for an unresolvable host")
|
||||
}
|
||||
var f *discoveryFailure
|
||||
if !errors.As(err, &f) {
|
||||
t.Fatalf("error should be classified, got %T", err)
|
||||
}
|
||||
if got := f.StatusOrDefault(); got != http.StatusBadGateway {
|
||||
t.Errorf("status = %d, want %d", got, http.StatusBadGateway)
|
||||
}
|
||||
if !strings.Contains(strings.ToLower(f.Error()), "resolve") {
|
||||
t.Errorf("message %q should say the hostname does not resolve", f.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// TestDiscoveryCandidates_NoDuplicateRequests: base "https://h/v1" with path
|
||||
// "/models" and base "https://h" with path "/v1/models" request the SAME URL.
|
||||
// Listing it twice in a failure message reads as "two things were tried" when
|
||||
// only one was.
|
||||
func TestDiscoveryCandidates_NoDuplicateRequests(t *testing.T) {
|
||||
seen := map[string]bool{}
|
||||
for _, c := range discoveryCandidates("https://h/v1") {
|
||||
url := c.baseURL + c.modelsPath
|
||||
if seen[url] {
|
||||
t.Errorf("candidate %q would be requested twice", url)
|
||||
}
|
||||
seen[url] = true
|
||||
}
|
||||
if len(seen) < 2 {
|
||||
t.Errorf("only %d distinct URLs; the search collapsed", len(seen))
|
||||
}
|
||||
}
|
||||
|
||||
// TestDiscoverModels_FailureListsEachURLOnce is the end-to-end form of the same
|
||||
// rule, on the error text an operator actually reads.
|
||||
func TestDiscoverModels_FailureListsEachURLOnce(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
_, err := DiscoverModels(context.Background(), srv.URL+"/v1", "k")
|
||||
if err == nil {
|
||||
t.Fatal("expected failure")
|
||||
}
|
||||
msg := err.Error()
|
||||
needle := srv.URL + "/v1/models"
|
||||
if n := strings.Count(msg, needle); n > 1 {
|
||||
t.Errorf("URL %s appears %d times in %q", needle, n, msg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiscoverModels_EmptyKeyOmitsHeader(t *testing.T) {
|
||||
var present bool
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
_, present = r.Header["Authorization"]
|
||||
_, _ = w.Write([]byte(`{"data":[{"id":"m"}]}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
if _, err := DiscoverModels(context.Background(), srv.URL+"/v1", ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if present {
|
||||
t.Error("an empty key should omit the header, not send an empty bearer")
|
||||
}
|
||||
}
|
||||
|
||||
// TestDiscoverModels_RealCatalogEndToEnd walks the actual captured response
|
||||
// through the whole path and asserts the numbers an operator would see.
|
||||
func TestDiscoverModels_RealCatalogEndToEnd(t *testing.T) {
|
||||
srv := catalogUpstream(t)
|
||||
res, err := DiscoverModels(context.Background(), srv.URL+"/v1", "sk-secret")
|
||||
if err != nil {
|
||||
t.Fatalf("discovery failed: %v", err)
|
||||
}
|
||||
if res.BaseURL != srv.URL+"/v1" {
|
||||
t.Errorf("base = %q, want the typed URL unchanged", res.BaseURL)
|
||||
}
|
||||
if res.Endpoint != "" {
|
||||
t.Errorf("endpoint = %q, want empty (base already carries /v1)", res.Endpoint)
|
||||
}
|
||||
if len(res.Models) != 2 || len(res.ImageModels) != 2 {
|
||||
t.Errorf("chat/image = %d/%d, want 2/2", len(res.Models), len(res.ImageModels))
|
||||
}
|
||||
if res.Adapter != "openai" {
|
||||
t.Errorf("adapter = %q, want openai", res.Adapter)
|
||||
}
|
||||
|
||||
// The response must be JSON the UI can consume directly.
|
||||
b, _ := json.Marshal(res)
|
||||
var round map[string]interface{}
|
||||
if err := json.Unmarshal(b, &round); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if round["ok"] != nil {
|
||||
// discoverResponse adds ok; discoveryResult does not. Both marshal
|
||||
// fine, so this is only a shape note.
|
||||
_ = round
|
||||
}
|
||||
}
|
||||
153
internal/gateway/discover_endpoint_test.go
Normal file
153
internal/gateway/discover_endpoint_test.go
Normal file
@ -0,0 +1,153 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestComposeImageEndpoint_NoVersionBaseLeavesDefault is the counterpart of the
|
||||
// /v1 case: when the base carries no version segment, discovery must emit "" so
|
||||
// the gateway's own default "/v1/images/generations" composes.
|
||||
//
|
||||
// This is the shape every currently-working source uses — siliconflow, the only
|
||||
// source in production with kind:image, has base_url
|
||||
// "https://api.siliconflow.cn" — so writing a non-empty value here would break
|
||||
// exactly the configuration that is known to work today.
|
||||
func TestComposeImageEndpoint_NoVersionBaseLeavesDefault(t *testing.T) {
|
||||
if got := composeImageEndpoint("https://api.siliconflow.cn"); got != "" {
|
||||
t.Errorf("composeImageEndpoint = %q, want \"\" so the built-in default composes", got)
|
||||
}
|
||||
// And the composition it implies must be the vendor's real path.
|
||||
base := "https://api.siliconflow.cn"
|
||||
composed := base + "/v1/images/generations"
|
||||
if !strings.HasSuffix(composed, "/v1/images/generations") {
|
||||
t.Errorf("composed URL %q lost the image path", composed)
|
||||
}
|
||||
}
|
||||
|
||||
// TestDiscovery_BareHostNeedsVersionOnEndpointWhenCatalogIsVersioned
|
||||
// distinguishes two cases that look identical from the outside.
|
||||
//
|
||||
// bare host + catalog at "/models" → chat is "/chat/completions" (the
|
||||
// adapter default, no endpoint written)
|
||||
// bare host + catalog at "/v1/models" → chat is "/v1/chat/completions",
|
||||
// so an endpoint MUST be written
|
||||
//
|
||||
// The catalog path is the only evidence of whether the upstream wants the
|
||||
// version segment, and getting this backwards sends every chat request to a
|
||||
// path the vendor does not serve.
|
||||
func TestDiscovery_BareHostNeedsVersionOnEndpointWhenCatalogIsVersioned(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
catalogPath string
|
||||
wantEndpoint string
|
||||
wantImage string
|
||||
}{
|
||||
{
|
||||
name: "catalog at the bare /models needs no endpoint",
|
||||
catalogPath: "/models",
|
||||
wantEndpoint: "",
|
||||
wantImage: "",
|
||||
},
|
||||
{
|
||||
name: "catalog at /v1/models requires /v1 on the chat endpoint",
|
||||
catalogPath: "/v1/models",
|
||||
wantEndpoint: "/v1/chat/completions",
|
||||
wantImage: "",
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != tc.catalogPath {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
_, _ = w.Write([]byte(`{"data":[{"id":"m1"},{"id":"m2"}]}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
res, err := DiscoverModels(context.Background(), srv.URL, "k")
|
||||
if err != nil {
|
||||
t.Fatalf("discovery failed: %v", err)
|
||||
}
|
||||
if res.BaseURL != srv.URL {
|
||||
t.Errorf("base = %q, want %q", res.BaseURL, srv.URL)
|
||||
}
|
||||
if res.Endpoint != tc.wantEndpoint {
|
||||
t.Errorf("endpoint = %q, want %q", res.Endpoint, tc.wantEndpoint)
|
||||
}
|
||||
if res.ImageEndpoint != tc.wantImage {
|
||||
t.Errorf("image_endpoint = %q, want %q", res.ImageEndpoint, tc.wantImage)
|
||||
}
|
||||
// Whatever it wrote, the chat URL must reach the same version
|
||||
// prefix the catalog was served under.
|
||||
ep := res.Endpoint
|
||||
if ep == "" {
|
||||
ep = "/chat/completions"
|
||||
}
|
||||
if got := res.BaseURL + ep; strings.Contains(got, "/v1/v1") {
|
||||
t.Errorf("chat URL %q has a doubled version", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestComposeEndpoints_BothFormsProduceWorkingURLs is the end-to-end
|
||||
// invariant across the two shapes, stated as the URLs the gateway will build.
|
||||
//
|
||||
// A base with a version and no image_endpoint produces "/v1/v1/…" — the exact
|
||||
// 404 observed before composeImageEndpoint existed. A base without one and a
|
||||
// non-empty image_endpoint produces a path missing the version. Both are
|
||||
// silent, expensive mistakes, so they are asserted literally.
|
||||
func TestComposeEndpoints_BothFormsProduceWorkingURLs(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
base string
|
||||
wantChat string
|
||||
wantImage string
|
||||
}{
|
||||
{
|
||||
name: "bare host: adapter and image defaults compose",
|
||||
base: "https://h",
|
||||
wantChat: "https://h/chat/completions",
|
||||
// openai's adapter endpoint is /chat/completions (verified in
|
||||
// internal/lua/adapters/openai.lua: adapter.endpoint).
|
||||
wantImage: "https://h/v1/images/generations",
|
||||
},
|
||||
{
|
||||
name: "versioned base: the version moves onto both endpoints",
|
||||
base: "https://h/v1",
|
||||
wantChat: "https://h/v1/chat/completions",
|
||||
wantImage: "https://h/v1/images/generations",
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
ep := composeEndpoint("/models", tc.base, tc.base)
|
||||
if ep == "" {
|
||||
ep = "/chat/completions" // the adapter default
|
||||
}
|
||||
iep := composeImageEndpoint(tc.base)
|
||||
if iep == "" {
|
||||
iep = "/v1/images/generations" // the gateway default
|
||||
}
|
||||
gotChat := tc.base + ep
|
||||
gotImage := tc.base + iep
|
||||
if gotChat != tc.wantChat {
|
||||
t.Errorf("chat URL = %q, want %q", gotChat, tc.wantChat)
|
||||
}
|
||||
if gotImage != tc.wantImage {
|
||||
t.Errorf("image URL = %q, want %q", gotImage, tc.wantImage)
|
||||
}
|
||||
for _, u := range []string{gotChat, gotImage} {
|
||||
if strings.Contains(u, "/v1/v1") {
|
||||
t.Errorf("%q has a doubled version segment", u)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
342
internal/gateway/discover_mutate_test.go
Normal file
342
internal/gateway/discover_mutate_test.go
Normal file
@ -0,0 +1,342 @@
|
||||
package gateway
|
||||
|
||||
// Real mutation injection: rewrites the IMPLEMENTATION on disk, re-runs the
|
||||
// suite, and requires it to go red.
|
||||
//
|
||||
// Why this exists and why it is a Go file rather than a shell script:
|
||||
//
|
||||
// The first version was a shell script. Its -run filter contained a regex that
|
||||
// the shell mangled, so every mutant reported "SURVIVED" while go test printed
|
||||
// "[no tests to run]". Four mutants looked like judgement failures and were
|
||||
// actually never executed. A judgement that cannot tell "tests passed" from
|
||||
// "no tests ran" is not a judgement.
|
||||
//
|
||||
// This version therefore refuses to report anything unless it has first proved
|
||||
// the filter selects a non-trivial number of tests (see baseline below), and it
|
||||
// treats "[no tests to run]" as a hard error rather than a green run.
|
||||
//
|
||||
// It also must not be run against a DIRTY tree: the mutant is written over
|
||||
// discover.go, and a leftover mutant from a killed run would make the baseline
|
||||
// green against broken code while every verdict came from the wrong source.
|
||||
// See the guard at the top of TestRealMutants.
|
||||
//
|
||||
// Run: go test -tags luajit ./internal/gateway/ -run TestRealMutants -v
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// mutationFilter must select every assertion that guards the code below. It is
|
||||
// deliberately broad: a narrow filter is how mutants get to survive for free.
|
||||
const mutationFilter = "Test(ParseModelCatalog|DiscoveredModelKind|Discover|TrimVersion|VersionSegment|ComposeEndpoint|ComposeImageEndpoint|ComposeEndpoints|IsVersionSegment|ModelsToConfig|DiscoveryResult|DiscoveryCandidates|Discovery_BareHost)"
|
||||
|
||||
type mutation struct {
|
||||
name string
|
||||
from string
|
||||
to string
|
||||
}
|
||||
|
||||
func TestRealMutants(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("mutation campaign compiles the package repeatedly")
|
||||
}
|
||||
wd, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
target := filepath.Join(wd, "discover.go")
|
||||
orig, err := os.ReadFile(target)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Refuse to run against a dirty tree.
|
||||
//
|
||||
// The mutant is written OVER discover.go, so on a dirty file the "original"
|
||||
// captured above is not the committed source — the baseline then runs against
|
||||
// whatever was there, and a killed run (a background-task timeout, a Ctrl-C)
|
||||
// leaves a mutant behind that the NEXT run treats as the baseline. Both make
|
||||
// every verdict meaningless while the harness still reports a clean score.
|
||||
//
|
||||
// Refusing rather than restoring: this cannot tell "leftover mutant" apart from
|
||||
// "the developer is mid-edit", and silently reverting real work is worse than
|
||||
// stopping.
|
||||
if dirty, derr := mutationTargetIsDirty(wd, "discover.go"); derr == nil && dirty {
|
||||
t.Fatalf("%s has uncommitted changes — commit or stash them first. A mutant "+
|
||||
"left behind by a killed run would otherwise become the baseline, and "+
|
||||
"every verdict below would be about code nobody wrote.", target)
|
||||
}
|
||||
defer func() {
|
||||
if werr := os.WriteFile(target, orig, 0o644); werr != nil {
|
||||
t.Fatalf("restore discover.go: %v", werr)
|
||||
}
|
||||
}()
|
||||
|
||||
// --- prove the harness is armed before trusting any verdict ---
|
||||
// A filter that selects nothing exits 0, which is indistinguishable from a
|
||||
// real pass. The only way to tell them apart is to compare against a filter
|
||||
// KNOWN to be empty: if that empty filter produces the same result as the
|
||||
// real one, the real one is probably empty too.
|
||||
//
|
||||
// Note go test does NOT print "[no tests to run]" for a wholly unmatched
|
||||
// filter (it prints that only when the package has other matching tests), so
|
||||
// the check has to compare outputs rather than look for a marker string.
|
||||
// Getting this backwards made the harness refuse to run at all.
|
||||
emptyOut := runSuite(wd, "TestZZZNoSuchTestExists")
|
||||
realOut := runSuite(wd, mutationFilter)
|
||||
if countTests(emptyOut) != 0 {
|
||||
t.Fatalf("a filter matching nothing selected %d tests; the harness's own probe is broken",
|
||||
countTests(emptyOut))
|
||||
}
|
||||
// Kept for the no-op check below: a mutant whose output matches the
|
||||
// baseline never changed behaviour, so it is an invalid mutation rather
|
||||
// than a gap in the tests.
|
||||
baseOut := realOut
|
||||
if strings.Contains(realOut, "[no tests to run]") {
|
||||
t.Fatalf("baseline selected no tests:\n%s", realOut)
|
||||
}
|
||||
baseCount := countTests(realOut)
|
||||
if baseCount < 25 {
|
||||
t.Fatalf("baseline ran only %d tests; the filter is too narrow to judge anything", baseCount)
|
||||
}
|
||||
if strings.Contains(realOut, "FAIL") {
|
||||
t.Logf("note: baseline is not green; kill verdicts remain valid")
|
||||
}
|
||||
t.Logf("baseline: %d tests selected, empty filter selects %d",
|
||||
baseCount, countTests(emptyOut))
|
||||
|
||||
muts := []mutation{
|
||||
{
|
||||
// Written as a condition that still READS modOut, because a
|
||||
// mutation that leaves a variable unused fails to compile — and a
|
||||
// build failure is not a judgement (see the harness note above).
|
||||
name: "image kind ignores upstream output modality",
|
||||
from: `if m.Capabilities.ImageGeneration || has(modOut, "image") {`,
|
||||
to: "if m.Capabilities.ImageGeneration || (len(modOut) > 0 && false) {",
|
||||
},
|
||||
{
|
||||
name: "kind defaults to image (guessing from names)",
|
||||
from: "\t}\n\treturn \"chat\"\n}\n\n// modalityList",
|
||||
to: "\t}\n\treturn \"image\"\n}\n\n// modalityList",
|
||||
},
|
||||
{
|
||||
name: "nested metadata no longer read",
|
||||
from: "\tcopyFields(m.Metadata) // nested first",
|
||||
to: "\tcopyFields(json.RawMessage(nil)) // mutant: nested metadata dropped",
|
||||
},
|
||||
{
|
||||
name: "non-catalog bodies accepted",
|
||||
from: "\tif trimmed == \"\" || trimmed == \"null\" {\n\t\treturn nil, fmt.Errorf(\"empty response\")\n\t}",
|
||||
to: "\tif false && (trimmed == \"\" || trimmed == \"null\") {\n\t\treturn nil, fmt.Errorf(\"empty response\")\n\t}",
|
||||
},
|
||||
{
|
||||
// Removing the bare "/models" candidate must be observable: a vendor
|
||||
// serving its catalog at the bare path would otherwise look
|
||||
// unreachable. The `len(...) >= 0` is a tautology that only exists to
|
||||
// keep the expression syntactically valid after the call is dropped.
|
||||
name: "URL fallback search removed",
|
||||
from: "\t\tadd(raw, \"/models\", 0)",
|
||||
to: "\t\tif len(\"/models\") >= 0 {\n\t\t\t_ = hasVersion\n\t\t}",
|
||||
},
|
||||
{
|
||||
name: "version segment never moved onto the chat endpoint",
|
||||
from: "\tif version == \"\" {\n\t\treturn \"\" // adapter default already composes\n\t}",
|
||||
to: "\tif version == \"\" || len(version) > 0 {\n\t\treturn \"\"\n\t}",
|
||||
},
|
||||
{
|
||||
name: "image endpoint never corrected (/v1/v1 regression)",
|
||||
from: "\tif b := trimVersionSuffix(persistedBase); b != persistedBase {\n\t\treturn \"/images/generations\"\n\t}",
|
||||
to: "\tif b := trimVersionSuffix(persistedBase); b == b && b != b {\n\t\treturn \"/images/generations\"\n\t}",
|
||||
},
|
||||
{
|
||||
name: "version test loosened so 'vision' counts as a version",
|
||||
from: "\tsuffix := strings.ToLower(seg[i:])",
|
||||
to: "\tsuffix := strings.ToLower(seg[i:])\n\tif len(suffix) >= 0 {\n\t\tsuffix = \"beta\"\n\t}",
|
||||
},
|
||||
{
|
||||
// The guard must be removable WITHOUT the mutant becoming a
|
||||
// semantic no-op. Two earlier attempts failed:
|
||||
//
|
||||
// `&& out.BaseURL != ""` — the guard this test exercises uses a
|
||||
// zero-value struct whose BaseURL is empty, so the added
|
||||
// condition made the mutant behave identically to the original.
|
||||
// `&& len(out.BaseURL) >= 0` — always true for any string, so the
|
||||
// mutant was STILL an identity transform.
|
||||
//
|
||||
// Reading the variable into a blank assignment keeps the compiler
|
||||
// happy without changing behaviour, which is what makes the
|
||||
// mutant observable.
|
||||
name: "nil slice guard removed from JSON output",
|
||||
from: "\tif out.ImageModels == nil {\n\t\tout.ImageModels = []string{}\n\t}",
|
||||
to: "\tif out.ImageModels == nil {\n\t\t_ = out.ImageModels\n\t}",
|
||||
},
|
||||
{
|
||||
name: "auth rejection no longer classified",
|
||||
from: "\tif status != http.StatusUnauthorized && status != http.StatusForbidden {\n\t\treturn \"\"\n\t}",
|
||||
to: "\tif status != http.StatusUnauthorized && status != http.StatusForbidden || len(body) >= 0 {\n\t\treturn \"\"\n\t}",
|
||||
},
|
||||
{
|
||||
name: "duplicate candidate URLs no longer collapsed",
|
||||
from: "\t\tif seenPair[pair] || seenURL[base+path] {",
|
||||
to: "\t\tif len(pair) > 0 && (seenPair[pair] || seenURL[base+path]) && false {",
|
||||
},
|
||||
}
|
||||
|
||||
survived := 0
|
||||
for _, m := range muts {
|
||||
t.Run(m.name, func(t *testing.T) {
|
||||
if !strings.Contains(string(orig), m.from) {
|
||||
t.Fatalf("anchor not found in discover.go — this mutation would be a no-op:\n%q", m.from)
|
||||
}
|
||||
mutated := strings.Replace(string(orig), m.from, m.to, 1)
|
||||
if mutated == string(orig) {
|
||||
t.Fatal("replacement changed nothing")
|
||||
}
|
||||
if werr := os.WriteFile(target, []byte(mutated), 0o644); werr != nil {
|
||||
t.Fatal(werr)
|
||||
}
|
||||
defer os.WriteFile(target, orig, 0o644)
|
||||
|
||||
out := runSuite(wd, mutationFilter)
|
||||
// A build failure kills EVERY mutant, so it would mask a broken
|
||||
// harness as a perfect score. Distinguish it from a real test
|
||||
// failure and fail loudly instead.
|
||||
if strings.Contains(out, "[build failed]") {
|
||||
t.Fatalf("mutant broke the build — that is not a judgement, it is a broken harness:\n%s", firstLines(out, 20))
|
||||
}
|
||||
if countTests(out) == 0 {
|
||||
t.Fatalf("filter selected nothing — the verdict would be meaningless")
|
||||
}
|
||||
if !strings.Contains(out, "FAIL") {
|
||||
// The mutant compiled and the suite stayed green. Before calling
|
||||
// this a survivor, check the mutation was not a no-op: an earlier
|
||||
// version reported four "survivors" that were really invalid
|
||||
// mutations whose added guard made them behaviourally identical
|
||||
// to the original. A silent no-op is a broken mutation, and
|
||||
// conflating the two sends the next reader hunting for a missing
|
||||
// test that does not exist.
|
||||
if behaviourOnly(out) == behaviourOnly(baseOut) {
|
||||
t.Fatalf("mutant is a no-op: the suite output is identical to the "+
|
||||
"baseline, so this mutation never changed behaviour. Fix the "+
|
||||
"mutation rather than looking for a missing test.\nmutant: %q", m.to)
|
||||
}
|
||||
survived++
|
||||
t.Errorf("SURVIVED %s — no test noticed this wrong implementation", m.name)
|
||||
return
|
||||
}
|
||||
t.Logf("KILL %s", m.name)
|
||||
})
|
||||
}
|
||||
if survived > 0 {
|
||||
t.Errorf("%d of %d mutants survived", survived, len(muts))
|
||||
}
|
||||
}
|
||||
|
||||
// runSuite invokes the go tool on this package with the given -run filter.
|
||||
//
|
||||
// The luajit build tag is mandatory: without it the package falls back to plain
|
||||
// Lua 5.1, billing.lua's `goto` fails to compile, and the whole package fails
|
||||
// to build — which the harness would misread as "every mutant killed".
|
||||
func runSuite(wd, filter string) string {
|
||||
tool := os.Getenv("GO_TEST_GO")
|
||||
if tool == "" {
|
||||
tool = "go"
|
||||
}
|
||||
cmd := exec.Command(tool, "test",
|
||||
"-tags", "luajit",
|
||||
".", "-run", filter, "-count=1", "-v")
|
||||
cmd.Dir = wd
|
||||
cmd.Env = os.Environ()
|
||||
out, _ := cmd.CombinedOutput()
|
||||
return string(out)
|
||||
}
|
||||
|
||||
// mutationTargetIsDirty reports whether a file under internal/gateway has
|
||||
// uncommitted changes, so the harness can refuse to run instead of treating a
|
||||
// leftover mutant as the baseline.
|
||||
//
|
||||
// git runs from the package directory with a PATH RELATIVE to it. An earlier
|
||||
// version of the sibling script passed a repo-relative pathspec while running in
|
||||
// the package directory; git matched nothing and the guard never fired.
|
||||
func mutationTargetIsDirty(wd, name string) (bool, error) {
|
||||
cmd := exec.Command("git", "diff", "--name-only", "--", "./"+name)
|
||||
cmd.Dir = wd
|
||||
out, err := cmd.Output()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return strings.TrimSpace(string(out)) != "", nil
|
||||
}
|
||||
|
||||
func countTests(out string) int {
|
||||
n := 0
|
||||
for _, line := range strings.Split(out, "\n") {
|
||||
// Subtests are indented; only count top-level ones so a single test
|
||||
// with a table of cases cannot inflate the count.
|
||||
if strings.HasPrefix(line, "=== RUN Test") && !strings.Contains(line, "/") {
|
||||
n++
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// behaviourOnly strips the parts of `go test -v` output that vary between runs
|
||||
// (timings, goroutine ids, temp paths) so two runs can be compared for equality.
|
||||
// If a mutant produces output identical to the baseline under this projection,
|
||||
// the mutation did not change observable behaviour and is therefore invalid.
|
||||
func behaviourOnly(out string) string {
|
||||
var kept []string
|
||||
for _, line := range strings.Split(out, "\n") {
|
||||
trimmed := strings.TrimSpace(line)
|
||||
switch {
|
||||
// Keep only the outcome lines. Test NAMES are deliberately dropped:
|
||||
// two runs of the same suite can legitimately order or cache tests
|
||||
// differently, and keeping the names made every mutant look like it had
|
||||
// "changed behaviour" — which defeats the no-op detector entirely.
|
||||
case strings.HasPrefix(trimmed, "--- PASS"), strings.HasPrefix(trimmed, "--- FAIL"),
|
||||
strings.HasPrefix(trimmed, "--- SKIP"),
|
||||
strings.HasPrefix(trimmed, "ok "), trimmed == "FAIL",
|
||||
strings.Contains(trimmed, ".go:"):
|
||||
kept = append(kept, trimmed)
|
||||
}
|
||||
}
|
||||
// Counting outcomes is stronger than comparing the sequence: a mutation
|
||||
// that swaps which test fails still changes the multiset, and one that
|
||||
// changes nothing cannot.
|
||||
sort.Strings(kept)
|
||||
return strings.Join(kept, "\n")
|
||||
}
|
||||
|
||||
// firstLines trims output for an error message; a failing nested run can be
|
||||
// thousands of lines and the cause is always near the top.
|
||||
func firstLines(s string, n int) string {
|
||||
lines := strings.Split(s, "\n")
|
||||
if len(lines) > n {
|
||||
lines = lines[:n]
|
||||
}
|
||||
return strings.Join(lines, "\n")
|
||||
}
|
||||
|
||||
// TestMutationFilterCoversEveryGuard keeps the filter honest. Dropping a group
|
||||
// from it silently disarms that area's mutants.
|
||||
func TestMutationFilterCoversEveryGuard(t *testing.T) {
|
||||
for _, group := range []string{
|
||||
"ParseModelCatalog", // nested metadata + non-catalog rejection
|
||||
"DiscoveredModelKind", // kind follows upstream metadata
|
||||
"Discover", // URL fallback, auth classification
|
||||
"ComposeEndpoint", // version segment exactly once (chat)
|
||||
"ComposeImageEndpoint",
|
||||
"ComposeEndpoints", // the composed URLs, both base shapes
|
||||
"IsVersionSegment", // "v1beta" vs "vision"
|
||||
"DiscoveryResult", // nil slices are arrays
|
||||
} {
|
||||
if !strings.Contains(mutationFilter, group) {
|
||||
t.Errorf("mutation filter omits %s; mutants there survive silently", group)
|
||||
}
|
||||
}
|
||||
}
|
||||
171
internal/gateway/discover_mutation_test.go
Normal file
171
internal/gateway/discover_mutation_test.go
Normal file
@ -0,0 +1,171 @@
|
||||
package gateway
|
||||
|
||||
// Mutation harness for the discovery code. Kept in the tree (not in a shell
|
||||
// script) so the -run filter is a Go string literal rather than something the
|
||||
// shell can mangle into "[no tests to run]" — which is exactly how an earlier
|
||||
// version of this harness reported four surviving mutants that were simply
|
||||
// never executed.
|
||||
//
|
||||
// Each mutant flips one behaviour; TestMutationHarness_Fails proves the
|
||||
// harness itself is armed.
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// discoveryTestFilter must cover every assertion that guards the behaviour
|
||||
// below. Narrowing it is how mutants get to "survive" for free.
|
||||
const discoveryTestFilter = "Test(ParseModelCatalog|DiscoveredModelKind|Discover|TrimVersion|VersionSegment|ComposeEndpoint|IsVersionSegment|ModelsToConfig|DiscoveryResult|DiscoveryCandidates)"
|
||||
|
||||
func TestDiscoveryFilterRunsRealTests(t *testing.T) {
|
||||
if discoveryTestFilter == "" {
|
||||
t.Fatal("filter is empty; every mutant would survive trivially")
|
||||
}
|
||||
// The whole point: the catalog parser test must be inside the filter, or a
|
||||
// mutant that stops reading nested metadata survives with a green run.
|
||||
for _, group := range []string{
|
||||
"ParseModelCatalog", // nested metadata + non-catalog rejection
|
||||
"DiscoveredModelKind", // kind must follow upstream metadata
|
||||
"Discover", // URL fallback + endpoint composition
|
||||
"ComposeEndpoint", // version segment exactly once
|
||||
"IsVersionSegment", // "v1beta" vs "vision"
|
||||
} {
|
||||
if !strings.Contains(discoveryTestFilter, group) {
|
||||
t.Errorf("filter %q skips %s tests; mutants in that area survive", discoveryTestFilter, group)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestMutantsAreDetectable is the harness's own regression guard: it runs the
|
||||
// filter's worth of assertions against deliberately broken inputs and requires
|
||||
// each to be rejected. If a future edit makes one of these pass, the
|
||||
// corresponding protection is gone.
|
||||
func TestMutantsAreDetectable(t *testing.T) {
|
||||
t.Run("image classification follows upstream metadata, not the model name", func(t *testing.T) {
|
||||
// The mutant: drop the output-modality check, so every model is chat.
|
||||
m := upstreamModel{ID: "gpt-image-2"}
|
||||
m.Architecture.OutputModalities = []string{"image"}
|
||||
if got := discoveredModelKind(m); got != "image" {
|
||||
t.Errorf("mutant survived: got %q, want \"image\"", got)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("nested metadata is read", func(t *testing.T) {
|
||||
var ms []upstreamModel
|
||||
if err := json.Unmarshal([]byte(realCatalogBody), &ms); err == nil {
|
||||
t.Fatal("fixture must be a wrapper object, not a bare array")
|
||||
}
|
||||
var wrapper struct {
|
||||
Data []upstreamModel `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(realCatalogBody), &wrapper); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(wrapper.Data) == 0 {
|
||||
t.Fatal("fixture parsed to zero models")
|
||||
}
|
||||
// The mutant: skip copyFields(m.Metadata) entirely.
|
||||
meta := modelMeta(wrapper.Data[0])
|
||||
if meta == nil {
|
||||
t.Fatal("mutant survived: metadata is nil, so cost/context_limit are lost")
|
||||
}
|
||||
for _, k := range []string{"context_limit", "cost", "context_budget"} {
|
||||
if _, ok := meta[k]; !ok {
|
||||
t.Errorf("mutant survived: %s dropped from nested metadata", k)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("non-catalog responses are rejected", func(t *testing.T) {
|
||||
for _, body := range []string{`<html>login</html>`, `null`, `{"error":{"message":"x"}}`, `{}`} {
|
||||
if _, err := parseModelCatalog([]byte(body)); err == nil {
|
||||
t.Errorf("mutant survived: %q accepted as a catalog", body)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("version segments are distinguished from real path segments", func(t *testing.T) {
|
||||
if !isVersionSegment("v1beta") {
|
||||
t.Error("mutant survived: v1beta not treated as a version")
|
||||
}
|
||||
for _, seg := range []string{"vision", "vertex", "v1x"} {
|
||||
if isVersionSegment(seg) {
|
||||
t.Errorf("mutant survived: %q wrongly treated as a version", seg)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("endpoint composition never duplicates the version segment", func(t *testing.T) {
|
||||
// The mutant: never move the segment onto the endpoint.
|
||||
if got := composeEndpoint("/v1/models", "https://h", "https://h/v1"); got != "/v1/chat/completions" {
|
||||
t.Errorf("mutant survived: endpoint = %q", got)
|
||||
}
|
||||
// The other mutant: always move it, even when the base keeps it.
|
||||
if got := composeEndpoint("/models", "https://h/v1", "https://h/v1"); got != "" {
|
||||
t.Errorf("mutant survived: endpoint = %q would double /v1", got)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("image_models is never nil", func(t *testing.T) {
|
||||
res := &discoveryResult{Models: []string{"a"}, ImageModels: nil}
|
||||
b, _ := json.Marshal(res)
|
||||
if strings.Contains(string(b), `"image_models":null`) {
|
||||
t.Errorf("mutant survived: %s", b)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("a rejected key is reported as unauthorized, not as an outage", func(t *testing.T) {
|
||||
// The mutant: collapse every failure into one generic error, which is
|
||||
// what made a typo'd key look like the vendor being down.
|
||||
if classifyAuthStatus(http.StatusUnauthorized, []byte(`{"error":"invalid api key"}`)) == "" {
|
||||
t.Error("mutant survived: 401 is not classified")
|
||||
}
|
||||
if classifyAuthStatus(http.StatusNotFound, []byte("nope")) != "" {
|
||||
t.Error("a 404 must not be reported as an auth problem")
|
||||
}
|
||||
if classifyAuthStatus(http.StatusOK, nil) != "" {
|
||||
t.Error("a 200 must not be reported as an auth problem")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("duplicate candidate URLs are collapsed", func(t *testing.T) {
|
||||
seen := map[string]bool{}
|
||||
for _, c := range discoveryCandidates("https://h/v1") {
|
||||
u := c.baseURL + c.modelsPath
|
||||
if seen[u] {
|
||||
t.Errorf("mutant survived: %s requested twice", u)
|
||||
}
|
||||
seen[u] = true
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("discovery falls back across URL forms", func(t *testing.T) {
|
||||
// The coverage requirement is that BOTH catalog paths are reachable for
|
||||
// a typed base, and both for a bare host. Asserting an exact count kept
|
||||
// drifting as the candidate list was fixed, and each time it measured
|
||||
// the current list rather than the property that matters.
|
||||
for _, base := range []string{"https://h/v1", "https://h"} {
|
||||
distinct := map[string]bool{}
|
||||
for _, c := range discoveryCandidates(base) {
|
||||
distinct[c.baseURL+c.modelsPath] = true
|
||||
}
|
||||
for _, want := range []string{base + "/v1/models", base + "/models"} {
|
||||
if !distinct[want] {
|
||||
t.Errorf("mutant survived: %s never requests %s", base, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("only the catalog is fetched, never each model", func(t *testing.T) {
|
||||
// Guarding a NON-mutation: the design decision that discovery must not
|
||||
// spend money. If a future change adds per-model probes, the counting
|
||||
// test below catches it; this assertion documents the intent.
|
||||
if discoveryModelsPath != "/v1/models" {
|
||||
t.Errorf("catalog path changed to %q", discoveryModelsPath)
|
||||
}
|
||||
})
|
||||
}
|
||||
648
internal/gateway/discover_test.go
Normal file
648
internal/gateway/discover_test.go
Normal file
@ -0,0 +1,648 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// realCatalogBody is the actual /models response captured from the amr-link
|
||||
// endpoint, not a hand-written fixture. Two properties of it are the reason
|
||||
// this test exists at all:
|
||||
//
|
||||
// - it nests modalities under "architecture", which the naive parser misses;
|
||||
// - it lists five image-generation ids among 34 chat ids, so a parser that
|
||||
// returns everything as "chat" silently poisons the AUTO chain.
|
||||
const realCatalogBody = `{"data":[{"id":"claude-fable-5","object":"model","created":1781081344,"owned_by":"vela","architecture":{"input_modalities":["text","image","file"],"output_modalities":["text"]},"metadata":{"cost":"very_high","capability":"best_quality","context_limit":1000000,"context_budget":232000},"default":false,"enabled":true},{"id":"gpt-image-2","object":"model","architecture":{"input_modalities":["text","image"],"output_modalities":["image"]},"metadata":{"capability":"best_quality"},"enabled":true},{"id":"deepseek-v4-flash","object":"model","architecture":{"input_modalities":["text"],"output_modalities":["text"]},"metadata":{"cost":"medium","capability":"standard","context_limit":1048000},"enabled":true},{"id":"seedream-5.0-pro","object":"model","architecture":{"input_modalities":["text","image"],"output_modalities":["image"]},"metadata":{"capability":"best_quality"},"enabled":true}]}`
|
||||
|
||||
func TestParseModelCatalog_RealUpstreamShape(t *testing.T) {
|
||||
models, err := parseModelCatalog([]byte(realCatalogBody))
|
||||
if err != nil {
|
||||
t.Fatalf("parse: %v", err)
|
||||
}
|
||||
if len(models) != 4 {
|
||||
t.Fatalf("want 4 models, got %d", len(models))
|
||||
}
|
||||
|
||||
kind := map[string]string{}
|
||||
for _, m := range models {
|
||||
kind[m.ID] = m.Kind
|
||||
}
|
||||
// The two image generators must be classified by the upstream's declared
|
||||
// output modality, not by their names.
|
||||
for _, id := range []string{"gpt-image-2", "seedream-5.0-pro"} {
|
||||
if kind[id] != "image" {
|
||||
t.Errorf("%s: got kind %q, want \"image\"", id, kind[id])
|
||||
}
|
||||
}
|
||||
for _, id := range []string{"claude-fable-5", "deepseek-v4-flash"} {
|
||||
if kind[id] != "chat" {
|
||||
t.Errorf("%s: got kind %q, want \"chat\"", id, kind[id])
|
||||
}
|
||||
}
|
||||
|
||||
// Metadata the upstream sent must survive; keys it did not send must not
|
||||
// appear as zero values.
|
||||
if got := models[0].Meta["context_limit"]; got == nil {
|
||||
t.Error("claude-fable-5: context_limit dropped (it is nested under metadata in the real response)")
|
||||
} else if f, ok := got.(float64); !ok || f != 1000000 {
|
||||
t.Errorf("context_limit = %#v, want numeric 1000000 so the UI can sort on it", got)
|
||||
}
|
||||
if _, present := models[0].Meta["cost"]; !present {
|
||||
t.Error("claude-fable-5: cost dropped")
|
||||
}
|
||||
// context_budget is present on this entry and must not be dropped either.
|
||||
if _, present := models[0].Meta["context_budget"]; !present {
|
||||
t.Error("claude-fable-5: context_budget dropped")
|
||||
}
|
||||
// deepseek-v4-flash DOES send metadata; gpt-image-2 sends only capability.
|
||||
// Neither may lose it.
|
||||
if models[2].Meta["context_limit"] == nil {
|
||||
t.Error("deepseek-v4-flash: context_limit dropped")
|
||||
}
|
||||
if models[2].Meta["cost"] == nil {
|
||||
t.Error("deepseek-v4-flash: cost dropped")
|
||||
}
|
||||
if _, bogus := models[0].Meta["made_up_field"]; bogus {
|
||||
t.Error("invented a metadata key that the upstream never sent")
|
||||
}
|
||||
}
|
||||
|
||||
// TestDiscoveredModelKind_ImageOnlyByUpstreamMetadata is the regression guard
|
||||
// for the failure mode that motivated keeping kind="chat" as the default: a
|
||||
// name-based heuristic marks "ideogram-v2" or "gpt-image-1" as an image model,
|
||||
// but a catalog that also serves chat for that id would then have its chat
|
||||
// entry removed from the AUTO chain. Only the upstream's declared OUTPUT
|
||||
// modality may classify a model as image.
|
||||
func TestDiscoveredModelKind_ImageOnlyByUpstreamMetadata(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
in upstreamModel
|
||||
want string
|
||||
}{
|
||||
{
|
||||
// A catalog that DECLARES output modality image is authoritative.
|
||||
// The "kind" string saying chat must not override it.
|
||||
name: "declared output modality image wins over a chat kind string",
|
||||
in: func() upstreamModel {
|
||||
var m upstreamModel
|
||||
m.Kind = "chat"
|
||||
m.Architecture.OutputModalities = []string{"image"}
|
||||
return m
|
||||
}(),
|
||||
want: "image",
|
||||
},
|
||||
{
|
||||
name: "bare array modality string",
|
||||
in: upstreamModel{ID: "x", Modality: "text-to-image"},
|
||||
want: "image",
|
||||
},
|
||||
{
|
||||
name: "image INPUT is still a chat model",
|
||||
in: func() upstreamModel {
|
||||
var m upstreamModel
|
||||
m.Architecture.InputModalities = []string{"text", "image"}
|
||||
return m
|
||||
}(),
|
||||
want: "chat",
|
||||
},
|
||||
{
|
||||
name: "file input is still a chat model",
|
||||
in: func() upstreamModel {
|
||||
var m upstreamModel
|
||||
m.Architecture.InputModalities = []string{"text", "file"}
|
||||
return m
|
||||
}(),
|
||||
want: "chat",
|
||||
},
|
||||
{
|
||||
name: "vision capability flag is chat",
|
||||
in: upstreamModel{ID: "y", Capabilities: modelCaps{Vision: true}},
|
||||
want: "chat",
|
||||
},
|
||||
{
|
||||
name: "capabilities.image_generation is image",
|
||||
in: upstreamModel{ID: "z", Capabilities: modelCaps{ImageGeneration: true}},
|
||||
want: "image",
|
||||
},
|
||||
{
|
||||
name: "no modality info at all defaults to chat",
|
||||
in: upstreamModel{ID: "plain"},
|
||||
want: "chat",
|
||||
},
|
||||
{
|
||||
// The default must be chat even when the name LOOKS like an image
|
||||
// model. Guessing from the name is what puts gpt-image-2 into the
|
||||
// chat AUTO chain, where every request to it fails.
|
||||
name: "image-looking name without upstream metadata stays chat",
|
||||
in: upstreamModel{ID: "gpt-image-2"},
|
||||
want: "chat",
|
||||
},
|
||||
{
|
||||
name: "vision-looking name without upstream metadata stays chat",
|
||||
in: upstreamModel{ID: "ideogram-v2"},
|
||||
want: "chat",
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if got := discoveredModelKind(tc.in); got != tc.want {
|
||||
t.Errorf("got %q, want %q", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseModelCatalog_RejectsNonCatalog(t *testing.T) {
|
||||
// A 200 with valid JSON that is not a catalog must NOT be reported as an
|
||||
// empty result. Reporting zero models would let a login page or a
|
||||
// rate-limit body create a source with no models in it.
|
||||
for name, body := range map[string]string{
|
||||
"login page html": `<html><body>Sign in</body></html>`,
|
||||
"rate limit json": `{"error":{"message":"slow down","type":"rate_limit"}}`,
|
||||
"empty object": `{}`,
|
||||
"null": `null`,
|
||||
"wrong shape": `{"objects":[{"id":"m"}]}`,
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
if _, err := parseModelCatalog([]byte(body)); err == nil {
|
||||
t.Errorf("expected an error for %q, got nil (would create an empty source)", name)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseModelCatalog_AcceptsBareArray(t *testing.T) {
|
||||
models, err := parseModelCatalog([]byte(`[{"id":"a"},{"id":"b"}]`))
|
||||
if err != nil {
|
||||
t.Fatalf("bare array should parse: %v", err)
|
||||
}
|
||||
if len(models) != 2 {
|
||||
t.Fatalf("want 2, got %d", len(models))
|
||||
}
|
||||
}
|
||||
|
||||
// TestDiscoverModels_NormalizesWhenOnlyBareModelsAnswers: operator pastes
|
||||
// "https://host/v1" (the form every SDK example shows) but the vendor serves
|
||||
// the catalog at "https://host/models" — the version segment on the path is
|
||||
// implied, not doubled. Discovery must strip the segment off the base and
|
||||
// report it on the endpoint, because openai's adapter endpoint is
|
||||
// "/chat/completions" and the two are concatenated.
|
||||
func TestDiscoverModels_NormalizesWhenOnlyBareModelsAnswers(t *testing.T) {
|
||||
var paths []string
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
paths = append(paths, r.URL.Path)
|
||||
if r.URL.Path != "/models" {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
_, _ = w.Write([]byte(realCatalogBody))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
res, err := DiscoverModels(context.Background(), srv.URL+"/v1", "sk-test")
|
||||
if err != nil {
|
||||
t.Fatalf("discovery failed: %v", err)
|
||||
}
|
||||
if res.BaseURL != srv.URL {
|
||||
t.Errorf("base_url = %q, want %q (version segment moved off the base)", res.BaseURL, srv.URL)
|
||||
}
|
||||
// "/models" carries no version segment, but the operator typed one — the
|
||||
// endpoint has to restore it or the chat call would miss /v1.
|
||||
if res.Endpoint != "/v1/chat/completions" {
|
||||
t.Errorf("endpoint = %q, want %q", res.Endpoint, "/v1/chat/completions")
|
||||
}
|
||||
if res.Note == "" {
|
||||
t.Error("silently rewriting the operator's base_url is worse than saying so")
|
||||
}
|
||||
if len(res.Models) != 2 || len(res.ImageModels) != 2 {
|
||||
t.Errorf("got %d chat / %d image, want 2 / 2", len(res.Models), len(res.ImageModels))
|
||||
}
|
||||
if len(paths) < 2 {
|
||||
t.Errorf("tried only %v; expected a fallback search", paths)
|
||||
}
|
||||
}
|
||||
|
||||
// TestDiscoverModels_BareHostWithVersionedModelsPath: operator pastes the host
|
||||
// root, vendor serves /v1/models. The base needs no rewriting; the endpoint
|
||||
// does.
|
||||
func TestDiscoverModels_BareHostWithVersionedModelsPath(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/v1/models" {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
_, _ = w.Write([]byte(`{"data":[{"id":"m1"}]}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
res, err := DiscoverModels(context.Background(), srv.URL, "")
|
||||
if err != nil {
|
||||
t.Fatalf("discovery failed: %v", err)
|
||||
}
|
||||
if res.BaseURL != srv.URL {
|
||||
t.Errorf("base_url = %q, want %q", res.BaseURL, srv.URL)
|
||||
}
|
||||
if res.Endpoint != "/v1/chat/completions" {
|
||||
t.Errorf("endpoint = %q, want %q", res.Endpoint, "/v1/chat/completions")
|
||||
}
|
||||
}
|
||||
|
||||
// TestDiscoverModels_KeepsWorkingBaseURLUnchanged covers the vendor that really
|
||||
// does serve "/v1/models": the typed URL must survive untouched so no needless
|
||||
// config rewrite is suggested.
|
||||
func TestDiscoverModels_KeepsWorkingBaseURLUnchanged(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/v1/models" {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
_, _ = w.Write([]byte(`{"data":[{"id":"m1"},{"id":"m2"}]}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
// Base typed WITH "/v1", catalog served at "…/v1/models". That is exactly
|
||||
// the amr-link shape: first candidate succeeds, nothing is rewritten, and
|
||||
// the adapter's default endpoint already composes.
|
||||
res, err := DiscoverModels(context.Background(), srv.URL+"/v1", "")
|
||||
if err != nil {
|
||||
t.Fatalf("discovery failed: %v", err)
|
||||
}
|
||||
if res.BaseURL != srv.URL+"/v1" {
|
||||
t.Errorf("base_url = %q, want it unchanged", res.BaseURL)
|
||||
}
|
||||
if res.Endpoint != "" {
|
||||
t.Errorf("endpoint = %q, want empty (the typed base already carries /v1)", res.Endpoint)
|
||||
}
|
||||
if res.Note != "" {
|
||||
t.Errorf("note = %q, want empty when nothing was rewritten", res.Note)
|
||||
}
|
||||
if len(res.Models) != 2 {
|
||||
t.Errorf("got %d chat models, want 2", len(res.Models))
|
||||
}
|
||||
}
|
||||
|
||||
// TestDiscoverModels_ErrorNamesEveryTriedURL: a failed probe is useless if it
|
||||
// only reports the last URL. The operator needs to see the full search.
|
||||
func TestDiscoverModels_ErrorNamesEveryTriedURL(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
_, err := DiscoverModels(context.Background(), srv.URL+"/v1", "k")
|
||||
if err == nil {
|
||||
t.Fatal("expected failure against an all-404 server")
|
||||
}
|
||||
msg := err.Error()
|
||||
for _, want := range []string{"/models", "/v1/models", "404"} {
|
||||
if !strings.Contains(msg, want) {
|
||||
t.Errorf("error %q should mention %q", msg, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiscoveryCandidates_DedupesAndPrefersTyped(t *testing.T) {
|
||||
cands := discoveryCandidates("https://h/v1/")
|
||||
if len(cands) == 0 {
|
||||
t.Fatal("no candidates")
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
for _, c := range cands {
|
||||
// Dedup is on the requested URL, so that is the key to assert on.
|
||||
k := c.baseURL + c.modelsPath
|
||||
if seen[k] {
|
||||
t.Errorf("duplicate request for %s", k)
|
||||
}
|
||||
seen[k] = true
|
||||
}
|
||||
// The versioned spelling of the typed URL must be reachable: on the real
|
||||
// amr-link endpoint that is what answers, and it is the preferred candidate.
|
||||
if !seen["https://h/v1/models"] {
|
||||
t.Error("the typed base with a versioned catalog path must be tried; that is what answers on amr-link")
|
||||
}
|
||||
// Both catalog forms must be attempted for a bare host too. Offering only
|
||||
// "/v1/models" there left a vendor serving a bare "/models" looking
|
||||
// unreachable.
|
||||
if !seen["https://h/models"] {
|
||||
t.Error("host root with a bare /models path was never tried")
|
||||
}
|
||||
// The first candidate is preferred, so its URL is what gets reported when
|
||||
// several answer.
|
||||
if cands[0].baseURL+cands[0].modelsPath != "https://h/v1/models" {
|
||||
t.Errorf("first candidate requests %q, want the canonical versioned form",
|
||||
cands[0].baseURL+cands[0].modelsPath)
|
||||
}
|
||||
// The bare "/models" form must exist too — some gateways serve it there.
|
||||
hasBare := false
|
||||
for _, c := range cands {
|
||||
if c.modelsPath == "/models" {
|
||||
hasBare = true
|
||||
}
|
||||
}
|
||||
if !hasBare {
|
||||
t.Error("no /models candidate; vendors serving the catalog at /models would be missed")
|
||||
}
|
||||
// Preference must be monotonically non-decreasing so the sort is stable.
|
||||
for i := 1; i < len(cands); i++ {
|
||||
if cands[i].prefer < cands[i-1].prefer {
|
||||
t.Errorf("candidates out of preference order at %d: %d < %d", i, cands[i].prefer, cands[i-1].prefer)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestVersionSegment(t *testing.T) {
|
||||
cases := map[string]string{
|
||||
"/v1/models": "/v1",
|
||||
"/v1beta/models": "/v1beta",
|
||||
"/models": "",
|
||||
"/api/models": "",
|
||||
"/vision/models": "",
|
||||
}
|
||||
for in, want := range cases {
|
||||
if got := versionSegment(in); got != want {
|
||||
t.Errorf("versionSegment(%q) = %q, want %q", in, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrimVersionSuffix(t *testing.T) {
|
||||
cases := map[string]string{
|
||||
"https://h/v1": "https://h",
|
||||
"https://h/v2": "https://h",
|
||||
"https://h/v1beta": "https://h",
|
||||
"https://h/api/v1": "https://h/api",
|
||||
"https://h/vision": "https://h/vision", // not numeric — a real path segment
|
||||
"https://h": "https://h",
|
||||
"https://h/openai": "https://h/openai",
|
||||
"https://h/v1/extra": "https://h/v1/extra",
|
||||
// A segment that merely STARTS with v must not be stripped: stripping
|
||||
// it would rewrite base "https://h/vision" to "https://h" and, worse,
|
||||
// would treat a real vendor path as a version.
|
||||
"https://h/vertex": "https://h/vertex",
|
||||
"https://h/volcengine": "https://h/volcengine",
|
||||
"https://h/vertex-ai/v1": "https://h/vertex-ai",
|
||||
}
|
||||
for in, want := range cases {
|
||||
if got := trimVersionSuffix(in); got != want {
|
||||
t.Errorf("trimVersionSuffix(%q) = %q, want %q", in, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestIsVersionSegment_RequiresDigits pins the distinction the whole endpoint
|
||||
// derivation rests on: "v1"/"v1beta" are versions, "vision"/"vertex" are path
|
||||
// segments. Without the digit requirement, stripping turns a vendor's real
|
||||
// base URL into a different one.
|
||||
func TestIsVersionSegment_RequiresDigits(t *testing.T) {
|
||||
yes := []string{"v1", "v2", "v10", "v1beta", "v1alpha", "v1preview"}
|
||||
no := []string{"vision", "vertex", "volcengine", "openai", "v", "vv", "api", "beta", "v1x"}
|
||||
for _, s := range yes {
|
||||
if !isVersionSegment(s) {
|
||||
t.Errorf("isVersionSegment(%q) = false, want true", s)
|
||||
}
|
||||
}
|
||||
for _, s := range no {
|
||||
if isVersionSegment(s) {
|
||||
t.Errorf("isVersionSegment(%q) = true, want false", s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestComposeEndpoint_ExactlyOneVersionSegment is the invariant test. Every row
|
||||
// below is a URL form that was observed to 404 or to route wrongly when the
|
||||
// version segment ended up in the wrong place.
|
||||
func TestComposeEndpoint_ExactlyOneVersionSegment(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
modelsPath string
|
||||
persistedBase string
|
||||
typedBase string
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "typed base has /v1 and catalog served there: base keeps it, endpoint empty",
|
||||
modelsPath: "/models", persistedBase: "https://h/v1", typedBase: "https://h/v1",
|
||||
want: "",
|
||||
},
|
||||
{
|
||||
name: "catalog served at versioned path, base stripped: endpoint carries it",
|
||||
modelsPath: "/v1/models", persistedBase: "https://h", typedBase: "https://h/v1",
|
||||
want: "/v1/chat/completions",
|
||||
},
|
||||
{
|
||||
name: "bare host, bare catalog, nothing typed: adapter default composes",
|
||||
modelsPath: "/models", persistedBase: "https://h", typedBase: "https://h",
|
||||
want: "",
|
||||
},
|
||||
{
|
||||
name: "bare host, versioned catalog: endpoint owns the version",
|
||||
modelsPath: "/v1/models", persistedBase: "https://h", typedBase: "https://h",
|
||||
want: "/v1/chat/completions",
|
||||
},
|
||||
{
|
||||
name: "typed /v1 but catalog only at bare /models: strip base, restore on endpoint",
|
||||
modelsPath: "/models", persistedBase: "https://h", typedBase: "https://h/v1",
|
||||
want: "/v1/chat/completions",
|
||||
},
|
||||
{
|
||||
name: "non-version segment must never be promoted onto the endpoint",
|
||||
modelsPath: "/models", persistedBase: "https://h/vision", typedBase: "https://h/vision",
|
||||
want: "",
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got := composeEndpoint(tc.modelsPath, tc.persistedBase, tc.typedBase)
|
||||
if got != tc.want {
|
||||
t.Errorf("composeEndpoint = %q, want %q", got, tc.want)
|
||||
}
|
||||
// Whichever way it resolved, base + endpoint must never carry the
|
||||
// same version segment twice.
|
||||
onBase := trimVersionSuffix(tc.persistedBase) != tc.persistedBase
|
||||
onEndpoint := versionSegment(strings.TrimSuffix(got, "/chat/completions")+"/models") != ""
|
||||
if onBase && onEndpoint {
|
||||
t.Errorf("version segment duplicated: base %q + endpoint %q", tc.persistedBase, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestDiscoverModels_DoesNotProbeEveryModel guards the decision not to spend
|
||||
// money verifying each catalog entry. Only the catalog request is made.
|
||||
func TestDiscoverModels_DoesNotProbeEveryModel(t *testing.T) {
|
||||
var calls int
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
calls++
|
||||
_, _ = w.Write([]byte(`{"data":[{"id":"a"},{"id":"b"},{"id":"c"}]}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
if _, err := DiscoverModels(context.Background(), srv.URL+"/v1", "k"); err != nil {
|
||||
t.Fatalf("discovery failed: %v", err)
|
||||
}
|
||||
if calls != 1 {
|
||||
t.Errorf("made %d requests, want exactly 1 (the catalog)", calls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestModelsToConfig_PrioritiesAndKinds(t *testing.T) {
|
||||
got := modelsToConfig([]string{"a", "b"}, []string{"img1"})
|
||||
if len(got) != 3 {
|
||||
t.Fatalf("want 3 models, got %d", len(got))
|
||||
}
|
||||
if got[2].ID != "img1" || got[2].Kind != "image" {
|
||||
t.Errorf("image model mis-sorted: %+v", got[2])
|
||||
}
|
||||
if got[2].Priority <= got[0].Priority {
|
||||
t.Errorf("image model priority %d should sort after chat %d", got[2].Priority, got[0].Priority)
|
||||
}
|
||||
}
|
||||
|
||||
// TestDiscoverModels_ImageModelsNeverNull guards the UI contract end to end,
|
||||
// not just on the struct: the WebUI does out.concat(image_models), and a JSON
|
||||
// null there renders "undefined" next to every image model row. This asserts on
|
||||
// the served bytes rather than the Go field, because that is what the bug was.
|
||||
func TestDiscoverModels_ImageModelsNeverNull(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// Only chat models — the case where the slice is never appended to.
|
||||
_, _ = w.Write([]byte(`{"data":[{"id":"a"},{"id":"b"}]}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
res, err := DiscoverModels(context.Background(), srv.URL+"/v1", "k")
|
||||
if err != nil {
|
||||
t.Fatalf("discovery failed: %v", err)
|
||||
}
|
||||
if len(res.ImageModels) != 0 {
|
||||
t.Fatalf("precondition: want zero image models, got %d", len(res.ImageModels))
|
||||
}
|
||||
b, err := json.Marshal(res)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(b), `"image_models":[]`) {
|
||||
t.Errorf("served %s\nwant \"image_models\":[] — null breaks concat in the UI", b)
|
||||
}
|
||||
}
|
||||
|
||||
// TestComposeImageEndpoint pins the rule that fixed a real 404.
|
||||
//
|
||||
// Provider.ImageURL is `base_url + ImageEndpoint()`, and the built-in default
|
||||
// is the ABSOLUTE "/v1/images/generations" — while chat's default is the
|
||||
// relative "/chat/completions". The two defaults disagree about who owns the
|
||||
// version segment, so a base_url that produces a working chat URL produces
|
||||
// "/v1/v1/images/generations" and a 404.
|
||||
func TestComposeImageEndpoint(t *testing.T) {
|
||||
cases := []struct {
|
||||
base string
|
||||
want string
|
||||
}{
|
||||
// base without a version: the absolute default composes correctly.
|
||||
{"https://h", ""},
|
||||
// base WITH a version: the default would double it, so the
|
||||
// version-relative form is written instead.
|
||||
{"https://h/v1", "/images/generations"},
|
||||
{"https://h/v1beta", "/images/generations"},
|
||||
// A real path segment that merely starts with v is not a version.
|
||||
{"https://h/vision", ""},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
got := composeImageEndpoint(tc.base)
|
||||
if got != tc.want {
|
||||
t.Errorf("composeImageEndpoint(%q) = %q, want %q", tc.base, got, tc.want)
|
||||
}
|
||||
// Whatever it decided, base + result must not contain a doubled
|
||||
// version segment — that is the bug this function exists for.
|
||||
joined := tc.base + got
|
||||
if strings.Contains(joined, "/v1/v1") {
|
||||
t.Errorf("composed %q contains a doubled version segment", joined)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestDiscoveryReportsImageEndpointWhenBaseCarriesVersion is the end-to-end
|
||||
// version: a pasted base_url with "/v1" must come back with an image endpoint,
|
||||
// or the image models it reported will all 404 on use.
|
||||
func TestDiscoveryReportsImageEndpointWhenBaseCarriesVersion(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/v1/models" {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
_, _ = w.Write([]byte(realCatalogBody))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
res, err := DiscoverModels(context.Background(), srv.URL+"/v1", "k")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(res.ImageModels) == 0 {
|
||||
t.Fatal("fixture must contain image models for this to mean anything")
|
||||
}
|
||||
if res.BaseURL != srv.URL+"/v1" {
|
||||
t.Fatalf("base = %q, want the typed URL with /v1", res.BaseURL)
|
||||
}
|
||||
if res.ImageEndpoint != "/images/generations" {
|
||||
t.Errorf("image_endpoint = %q, want %q (otherwise the reported image models all 404)",
|
||||
res.ImageEndpoint, "/images/generations")
|
||||
}
|
||||
// The chat side stays empty: base already carries /v1 and the adapter's
|
||||
// relative default composes.
|
||||
if res.Endpoint != "" {
|
||||
t.Errorf("endpoint = %q, want empty", res.Endpoint)
|
||||
}
|
||||
}
|
||||
|
||||
// TestDiscoveryResult_NilSlicesSerializeAsArrays covers the zero value of
|
||||
// discoveryResult, which is what a caller gets if it builds one directly
|
||||
// instead of going through DiscoverModels. DiscoverModels happens to
|
||||
// initialize the slices, so testing only that path would let the MarshalJSON
|
||||
// nil guard look redundant — and removing it would ship a latent UI bug the
|
||||
// first time some other caller builds the struct.
|
||||
func TestDiscoveryResult_NilSlicesSerializeAsArrays(t *testing.T) {
|
||||
b, err := json.Marshal(&discoveryResult{BaseURL: "https://h"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got := string(b)
|
||||
for _, want := range []string{`"models":[]`, `"image_models":[]`, `"tried":[]`} {
|
||||
if !strings.Contains(got, want) {
|
||||
t.Errorf("zero value serialized %s\nwant %s — a nil slice becomes JSON null and the UI renders \"undefined\"", got, want)
|
||||
}
|
||||
}
|
||||
for _, bad := range []string{`"models":null`, `"image_models":null`, `"tried":null`} {
|
||||
if strings.Contains(got, bad) {
|
||||
t.Errorf("serialized %s, which the UI cannot concatenate", bad)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestDiscoveryResultJSONShape pins the field names the WebUI will bind to.
|
||||
func TestDiscoveryResultJSONShape(t *testing.T) {
|
||||
b, err := json.Marshal(&discoveryResult{
|
||||
BaseURL: "https://h", Models: []string{"a"}, ImageModels: []string{},
|
||||
ModelsPath: "/models", Adapter: "openai",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var got map[string]interface{}
|
||||
if err := json.Unmarshal(b, &got); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// image_models must serialize as [] and never null: the UI does
|
||||
// out.concat(image_models) and null would render "undefined".
|
||||
if v, ok := got["image_models"]; !ok || v == nil {
|
||||
t.Errorf("image_models = %v, want an empty array (null breaks concat in the UI)", v)
|
||||
}
|
||||
for _, k := range []string{"base_url", "endpoint", "models", "image_models", "models_path", "tried"} {
|
||||
if _, ok := got[k]; !ok {
|
||||
t.Errorf("missing field %q in discovery response", k)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -4,8 +4,8 @@ import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@ -33,11 +33,11 @@ func keyAutoGateway(t *testing.T) *Gateway {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg := &config.Config{
|
||||
Path: cfgPath,
|
||||
AdapterDir: filepath.Join(td, "adapters"),
|
||||
RuntimeFile: filepath.Join(td, "runtime.json"),
|
||||
Path: cfgPath,
|
||||
AdapterDir: filepath.Join(td, "adapters"),
|
||||
RuntimeFile: filepath.Join(td, "runtime.json"),
|
||||
DefaultModel: "AUTO",
|
||||
GatewayKeys: []string{"sk-admin"},
|
||||
GatewayKeys: []string{"sk-admin"},
|
||||
Keys: []config.GWKey{
|
||||
{Key: "sk-admin", Role: "admin", Name: "admin"},
|
||||
{Key: "sk-user", Role: "user", Name: "user"},
|
||||
@ -97,7 +97,7 @@ func TestKeyAutoAPICrudAndInherit(t *testing.T) {
|
||||
|
||||
// Initially inherits.
|
||||
var got struct {
|
||||
Inherits bool `json:"inherits"`
|
||||
Inherits bool `json:"inherits"`
|
||||
Auto []config.ModelScope `json:"auto"`
|
||||
}
|
||||
rr := doReqAs(t, g, "sk-admin", "GET", "/api/keys/sk-user/auto", "")
|
||||
@ -254,4 +254,4 @@ func TestKeyAutoAPIDoesNotShadowKeyRoutes(t *testing.T) {
|
||||
if rr := doReqAs(t, g, "sk-user", "GET", "/api/keys/me", ""); rr.Code != http.StatusOK {
|
||||
t.Fatalf("GET /api/keys/me as user = %d, want 200", rr.Code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
379
internal/gateway/messages.go
Normal file
379
internal/gateway/messages.go
Normal file
@ -0,0 +1,379 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"llmsproxy/internal/provider"
|
||||
"llmsproxy/internal/scheduler"
|
||||
"llmsproxy/internal/types"
|
||||
)
|
||||
|
||||
// handleMessages serves the Anthropic Messages protocol (POST /v1/messages).
|
||||
//
|
||||
// The point of this endpoint is NOT to add a second scheduling path. It reuses
|
||||
// everything /v1/chat/completions does — key auth, model scope, per-model
|
||||
// quotas, the AUTO chain, cooldowns, audit records, plugin hooks — and differs
|
||||
// in exactly two places:
|
||||
//
|
||||
// 1. the request body is parsed from Anthropic's shape into the unified format
|
||||
// (messages with content blocks, a top-level `system`, `max_tokens`
|
||||
// mandatory, `thinking` optional);
|
||||
// 2. the response is rendered by the SERVING adapter's serialize_* hooks, so
|
||||
// the client sees Anthropic's shape back.
|
||||
//
|
||||
// The kernel therefore contains no Anthropic field names on this path: parsing
|
||||
// is table-driven, and serialization belongs to the adapter.
|
||||
//
|
||||
// A source whose adapter does not declare serialize_* falls back to the
|
||||
// OpenAI shape, which is what the kernel guarantees for an unconfigured
|
||||
// deployment. That asymmetry is deliberate (see serializer.go): an Anthropic
|
||||
// client talking to a source that cannot speak Anthropic gets a clear
|
||||
// OpenAI-shaped answer rather than a malformed Anthropic one.
|
||||
func (g *Gateway) handleMessages(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
writeError(w, http.StatusMethodNotAllowed, "method_not_allowed", "use POST")
|
||||
return
|
||||
}
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid_request", "read body: "+err.Error())
|
||||
return
|
||||
}
|
||||
// INGRESS: the kernel's built-in Anthropic parse is the FALLBACK (R5). If an
|
||||
// adapter has CLAIMED this ingress path (adapter.ingress == "/v1/messages"),
|
||||
// the kernel YIELDS: parsing is done by that adapter's deserialize_request
|
||||
// hook, so Anthropic protocol iteration lives in anthropic.lua, not here.
|
||||
// No adapter claims it → the built-in parser below runs (unconfigured
|
||||
// deployments keep working). This is the inbound dual of serialize_*.
|
||||
req, handled, perr := g.parseViaIngress(r.URL.Path, string(body), g.core.DefaultModel())
|
||||
if perr != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid_request", perr.Error())
|
||||
return
|
||||
}
|
||||
if !handled {
|
||||
var raw anthropicRequest
|
||||
if err := json.Unmarshal(body, &raw); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid_request", "invalid json: "+err.Error())
|
||||
return
|
||||
}
|
||||
req, err = raw.toUnified(g.core.DefaultModel())
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid_request", err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
model := req.Model
|
||||
ctx := r.Context()
|
||||
g.fireStart(ctx, &chatRequest{Messages: req.Messages, Tools: nil},
|
||||
chatKind(req.Stream), model, len(req.Messages), 0)
|
||||
|
||||
if isAuto(model) {
|
||||
chain, _ := g.core.AutoChainFor(reqKey(ctx))
|
||||
if chain == nil || len(chain.Tiers) == 0 {
|
||||
writeError(w, http.StatusServiceUnavailable, "no_provider", "no auto slot configured")
|
||||
return
|
||||
}
|
||||
if q := g.checkQuota(ctx, "AUTO"); q != nil {
|
||||
g.writeReject(w, q)
|
||||
return
|
||||
}
|
||||
g.serveUnified(ctx, w, chain, req, nil)
|
||||
return
|
||||
}
|
||||
if allow := g.allowedModels(ctx); allow != nil && !g.hasScopeModel(allow, model) {
|
||||
writeError(w, http.StatusForbidden, "model_not_allowed",
|
||||
"model \""+model+"\" is not allowed for this key")
|
||||
return
|
||||
}
|
||||
cands, effective := g.resolveCands(ctx, &chatRequest{Model: model})
|
||||
if len(cands) == 0 {
|
||||
writeError(w, http.StatusNotFound, "model_not_found",
|
||||
"model \""+model+"\" is not configured")
|
||||
return
|
||||
}
|
||||
if effective == "" {
|
||||
effective = firstModel(cands[0])
|
||||
}
|
||||
if q := g.checkQuota(ctx, effective); q != nil {
|
||||
g.writeReject(w, q)
|
||||
return
|
||||
}
|
||||
g.serveUnified(ctx, w, nil, req, &directRoute{cands: cands, effective: effective})
|
||||
}
|
||||
|
||||
// directRoute carries the pre-resolved candidate list for a model-pinned
|
||||
// request, so serveUnified can run both routes (direct / AUTO) from one body.
|
||||
type directRoute struct {
|
||||
cands []*provider.Provider
|
||||
effective string
|
||||
}
|
||||
|
||||
// serveUnified runs the request through the scheduler and writes the reply via
|
||||
// the serving adapter's serializer. It is the shared tail of handleChat and
|
||||
// handleMessages: by this point every gate (auth, scope, quota) has already run,
|
||||
// so the only remaining questions are "which source serves it" and "what shape
|
||||
// does the client get".
|
||||
func (g *Gateway) serveUnified(ctx context.Context, w http.ResponseWriter, chain *scheduler.Chain, req *types.ChatRequest, direct *directRoute) {
|
||||
rec := &Req{Key: keyID(reqKey(ctx)), Type: "chat", OK: false}
|
||||
if direct != nil {
|
||||
rec.Model = direct.effective
|
||||
rec.Source = firstSource(direct.cands)
|
||||
} else {
|
||||
rec.Type = "stream"
|
||||
}
|
||||
if req.Stream {
|
||||
rec.Type = "stream"
|
||||
}
|
||||
|
||||
done := g.stats.Begin()
|
||||
defer done()
|
||||
|
||||
var (
|
||||
resp *types.UnifiedResponse
|
||||
chunks <-chan types.UnifiedChunk
|
||||
usedSrc, usedModel string
|
||||
err error
|
||||
)
|
||||
t0 := time.Now()
|
||||
if direct != nil {
|
||||
cands := toScheduler(direct.cands)
|
||||
if req.Stream {
|
||||
chunks, usedSrc, usedModel, err = g.core.Scheduler().ChatStream(ctx, cands, req)
|
||||
} else {
|
||||
resp, usedSrc, usedModel, err = g.core.Scheduler().Chat(ctx, cands, req)
|
||||
}
|
||||
} else {
|
||||
if req.Stream {
|
||||
chunks, usedSrc, usedModel, err = g.core.Scheduler().ChainChatStream(ctx, chain, req, nil, nil)
|
||||
} else {
|
||||
resp, usedSrc, usedModel, err = g.core.Scheduler().ChainChat(ctx, chain, req, nil, nil)
|
||||
}
|
||||
}
|
||||
rec.LatMs = time.Since(t0).Milliseconds()
|
||||
if err != nil {
|
||||
g.failChat(w, rec, err)
|
||||
g.writeRec(rec)
|
||||
return
|
||||
}
|
||||
rec.OK = true
|
||||
rec.Status = http.StatusOK
|
||||
if usedModel != "" {
|
||||
rec.Model = usedModel
|
||||
}
|
||||
rec.Source = usedSrc
|
||||
ser := g.serializerFor(usedSrc, directProviderList(direct))
|
||||
|
||||
if chunks != nil {
|
||||
g.fireRouted(ctx, "stream", usedSrc, usedModel, tierForRoute(direct), true)
|
||||
rec.Prompt = estimatePromptTokens(req)
|
||||
g.pumpStream(w, ser, rec, chunks, modelLabel(req, usedModel), t0)
|
||||
return
|
||||
}
|
||||
recordChatUsage(rec, req, resp)
|
||||
g.fireRouted(ctx, "chat", usedSrc, usedModel, tierForRoute(direct), false)
|
||||
rec.FirstByteMs = rec.LatMs
|
||||
g.writeRec(rec)
|
||||
writeChatCompletion(w, ser, resp, modelLabel(req, usedModel))
|
||||
}
|
||||
|
||||
// ---- Anthropic request shape (in-bound only; the kernel holds no outbound
|
||||
// Anthropic field names) ----
|
||||
|
||||
// anthropicRequest is the client-facing parse target for POST /v1/messages.
|
||||
//
|
||||
// Only the fields the gateway must understand to schedule and account for a
|
||||
// request appear here. Anything else the client sent is preserved verbatim in
|
||||
// raw so an adapter can read it (the same "kernel does not interpret" rule that
|
||||
// governs tools).
|
||||
type anthropicRequest struct {
|
||||
Model string `json:"model"`
|
||||
System json.RawMessage `json:"system,omitempty"`
|
||||
Messages []anthropicMessage `json:"messages"`
|
||||
MaxTokens int `json:"max_tokens"`
|
||||
Stream bool `json:"stream,omitempty"`
|
||||
|
||||
Temperature *float64 `json:"temperature,omitempty"`
|
||||
TopP *float64 `json:"top_p,omitempty"`
|
||||
TopK int `json:"top_k,omitempty"`
|
||||
StopSeqs []string `json:"stop_sequences,omitempty"`
|
||||
Tools json.RawMessage `json:"tools,omitempty"`
|
||||
ToolChoice json.RawMessage `json:"tool_choice,omitempty"`
|
||||
Thinking *types.ThinkingConfig `json:"thinking,omitempty"`
|
||||
Metadata map[string]interface{} `json:"metadata,omitempty"`
|
||||
|
||||
// raw is the untouched body, so an adapter can read fields the gateway
|
||||
// has no reason to know (cache_control, service_tier, …).
|
||||
raw json.RawMessage
|
||||
}
|
||||
|
||||
// anthropicMessage accepts Anthropic's content as either a plain string or an
|
||||
// array of typed blocks; both normalize to the unified message shape.
|
||||
type anthropicMessage struct {
|
||||
Role string `json:"role"`
|
||||
Content json.RawMessage `json:"content"`
|
||||
}
|
||||
|
||||
// toUnified converts the parsed request into the internal format.
|
||||
func (a *anthropicRequest) toUnified(defaultModel string) (*types.ChatRequest, error) {
|
||||
if len(a.Messages) == 0 {
|
||||
return nil, errors.New("messages is required")
|
||||
}
|
||||
msgs := make([]types.ChatMessage, 0, len(a.Messages))
|
||||
for i, m := range a.Messages {
|
||||
u := types.ChatMessage{Role: m.Role, Content: normalizeAnthropicContent(m.Content)}
|
||||
if u.Role == "" {
|
||||
return nil, fmt.Errorf("messages[%d].role is required", i)
|
||||
}
|
||||
msgs = append(msgs, u)
|
||||
}
|
||||
model := a.Model
|
||||
if model == "" {
|
||||
model = defaultModel
|
||||
}
|
||||
th := a.Thinking
|
||||
if th == nil {
|
||||
// Anthropic's default is thinking OFF, and `thinking: {enabled:false}`
|
||||
// is the explicit spelling of that. Emitting it keeps the unified
|
||||
// request honest about the client's intent instead of leaving the
|
||||
// field absent and letting each adapter guess.
|
||||
no := false
|
||||
th = &types.ThinkingConfig{Enabled: &no}
|
||||
}
|
||||
return &types.ChatRequest{
|
||||
Model: model,
|
||||
Messages: msgs,
|
||||
Temperature: a.Temperature,
|
||||
MaxTokens: a.MaxTokens,
|
||||
Stream: a.Stream,
|
||||
TopP: a.TopP,
|
||||
TopK: a.TopK,
|
||||
StopSequences: a.StopSeqs,
|
||||
Tools: a.Tools,
|
||||
ToolChoice: a.ToolChoice,
|
||||
System: systemToString(a.System),
|
||||
Thinking: th,
|
||||
ExtraBody: map[string]interface{}{},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// normalizeAnthropicContent turns Anthropic content into the unified shape.
|
||||
//
|
||||
// A string stays a string. An array of blocks is preserved as-is: the unified
|
||||
// format deliberately carries multimodal/tool blocks verbatim (ChatMessage
|
||||
// .Content is json.RawMessage) rather than flattening them, because only an
|
||||
// adapter knows which block types its upstream accepts.
|
||||
func normalizeAnthropicContent(raw json.RawMessage) json.RawMessage {
|
||||
trimmed := strings.TrimSpace(string(raw))
|
||||
if trimmed == "" {
|
||||
return types.StringContent("")
|
||||
}
|
||||
if strings.HasPrefix(trimmed, `"`) {
|
||||
return raw
|
||||
}
|
||||
return raw
|
||||
}
|
||||
|
||||
// systemToString accepts Anthropic's two spellings: a plain string, or an array
|
||||
// of {type:"text", text} blocks.
|
||||
func systemToString(raw json.RawMessage) string {
|
||||
trimmed := strings.TrimSpace(string(raw))
|
||||
if trimmed == "" || trimmed == "null" {
|
||||
return ""
|
||||
}
|
||||
if strings.HasPrefix(trimmed, `"`) {
|
||||
var s string
|
||||
if err := json.Unmarshal(raw, &s); err == nil {
|
||||
return s
|
||||
}
|
||||
return ""
|
||||
}
|
||||
var blocks []struct {
|
||||
Type string `json:"type"`
|
||||
Text string `json:"text"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &blocks); err != nil {
|
||||
return ""
|
||||
}
|
||||
var sb strings.Builder
|
||||
for _, b := range blocks {
|
||||
if b.Type == "text" {
|
||||
sb.WriteString(b.Text)
|
||||
}
|
||||
}
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// parseViaIngress is the kernel's INBOUND yield point, the dual of
|
||||
// serializerFor (which decides the outbound serializer).
|
||||
//
|
||||
// Design (user requirement): an inbound endpoint such as /v1/messages is the
|
||||
// KERNEL'S FALLBACK — the built-in parser in handleMessages is what an
|
||||
// unconfigured deployment gets. But an adapter may CLAIM that endpoint
|
||||
// (adapter.ingress == path) and declare deserialize_request; when it does, the
|
||||
// kernel YIELDS parsing to it, so client-protocol iteration lives in the
|
||||
// adapter and the kernel's own copy is only the safety net (R5).
|
||||
//
|
||||
// Returns handled=false when no adapter claims the path (or one claims it
|
||||
// without a deserialize_request hook) — the caller then runs its built-in
|
||||
// parser. A hook that exists but FAILS is a hard error, not a silent fallback:
|
||||
// the client spoke a protocol the adapter claimed to handle, and quietly
|
||||
// parsing it as a different protocol would mis-schedule the request.
|
||||
func (g *Gateway) parseViaIngress(path, body, defaultModel string) (*types.ChatRequest, bool, error) {
|
||||
vm := g.core.VM()
|
||||
if vm == nil {
|
||||
return nil, false, nil
|
||||
}
|
||||
adapter := vm.AdapterForIngress(path)
|
||||
if adapter == "" || !vm.HasFn(adapter, "deserialize_request") {
|
||||
return nil, false, nil
|
||||
}
|
||||
out, err := vm.Transform(adapter, "deserialize_request", body)
|
||||
if err != nil {
|
||||
return nil, true, fmt.Errorf("adapter %s deserialize_request(%s): %w", adapter, path, err)
|
||||
}
|
||||
var req types.ChatRequest
|
||||
if err := json.Unmarshal([]byte(out), &req); err != nil {
|
||||
return nil, true, fmt.Errorf("adapter %s deserialize_request returned an invalid unified request: %w", adapter, err)
|
||||
}
|
||||
if req.Model == "" {
|
||||
req.Model = defaultModel
|
||||
}
|
||||
return &req, true, nil
|
||||
}
|
||||
|
||||
func chatKind(stream bool) string {
|
||||
if stream {
|
||||
return "stream"
|
||||
}
|
||||
return "chat"
|
||||
}
|
||||
|
||||
// directProviderList hands the serving-source resolver its candidate list.
|
||||
func directProviderList(direct *directRoute) []*provider.Provider {
|
||||
if direct == nil {
|
||||
return nil
|
||||
}
|
||||
return direct.cands
|
||||
}
|
||||
|
||||
func tierForRoute(direct *directRoute) int {
|
||||
if direct != nil {
|
||||
return -1
|
||||
}
|
||||
return -2
|
||||
}
|
||||
|
||||
func modelLabel(req *types.ChatRequest, usedModel string) string {
|
||||
if usedModel != "" {
|
||||
return usedModel
|
||||
}
|
||||
return req.Model
|
||||
}
|
||||
404
internal/gateway/messages_test.go
Normal file
404
internal/gateway/messages_test.go
Normal file
@ -0,0 +1,404 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"llmsproxy/internal/config"
|
||||
"llmsproxy/internal/core"
|
||||
)
|
||||
|
||||
// 本文件端到端验证 Phase 2 的核心主张:**对外协议由适配器决定,内核只调度**。
|
||||
//
|
||||
// 具体做法:起一个真实的 core + gateway,源挂 anthropic 适配器,上游是一个
|
||||
// 只懂 Anthropic Messages 的假服务器。然后:
|
||||
//
|
||||
// 1. Anthropic 客户端形状的请求 → 拿到 Anthropic 形状的响应
|
||||
// 2. 调度/配额/审计与 /v1/chat/completions 走同一条路径
|
||||
// 3. 上游若不支持该模型,内核如实回 503,不伪造格式
|
||||
|
||||
// anthropicUpstream 是一个只实现 Anthropic Messages 的假上游。
|
||||
func anthropicUpstream(t *testing.T) *httptest.Server {
|
||||
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if !strings.HasSuffix(r.URL.Path, "/messages") {
|
||||
// 网关把请求发到了非 Anthropic 路径 —— 显式暴露这个 bug
|
||||
t.Errorf("请求打到了非 Anthropic 路径: %s", r.URL.Path)
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
body, _ := io.ReadAll(r.Body)
|
||||
var probe map[string]interface{}
|
||||
_ = json.Unmarshal(body, &probe)
|
||||
if _, ok := probe["messages"]; !ok {
|
||||
t.Errorf("上游收到没有 messages 的请求体: %s", body)
|
||||
}
|
||||
if _, ok := probe["system"]; !ok && probe["system"] == nil {
|
||||
// system 是可选的,仅在客户端给了的时候才应出现;不强制
|
||||
_ = ok
|
||||
}
|
||||
if _, ok := probe["max_tokens"]; !ok {
|
||||
t.Errorf("Anthropic 要求 max_tokens,上游请求体却缺这个字段: %s", body)
|
||||
}
|
||||
|
||||
// Streaming requests must get a real Anthropic SSE sequence; a JSON body
|
||||
// here would leave the adapter with zero frames and the gateway would
|
||||
// (correctly) reject the stream as empty.
|
||||
if stream, _ := probe["stream"].(bool); stream {
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
fl, _ := w.(http.Flusher)
|
||||
frames := []string{
|
||||
`event: message_start` + "\n" + `data: {"type":"message_start","message":{"id":"msg_mock","type":"message","role":"assistant","model":"` + probeModel(probe) + `","content":[],"usage":{"input_tokens":3,"output_tokens":0}}}`,
|
||||
`event: content_block_start` + "\n" + `data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}`,
|
||||
`event: content_block_delta` + "\n" + `data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"pong"}}`,
|
||||
`event: content_block_stop` + "\n" + `data: {"type":"content_block_stop","index":0}`,
|
||||
`event: message_delta` + "\n" + `data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":5}}`,
|
||||
`event: message_stop` + "\n" + `data: {"type":"message_stop"}`,
|
||||
}
|
||||
for _, f := range frames {
|
||||
fmt.Fprint(w, f+"\n\n")
|
||||
if fl != nil {
|
||||
fl.Flush()
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
fmt.Fprint(w, `{
|
||||
"id":"msg_mock","type":"message","role":"assistant","model":"`+probeModel(probe)+`",
|
||||
"content":[{"type":"thinking","thinking":"let me think"},{"type":"text","text":"pong"}],
|
||||
"stop_reason":"end_turn",
|
||||
"usage":{"input_tokens":3,"output_tokens":5}
|
||||
}`)
|
||||
}))
|
||||
}
|
||||
|
||||
func probeModel(m map[string]interface{}) string {
|
||||
if s, ok := m["model"].(string); ok {
|
||||
return s
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// messagesGateway 起一个真实网关,源挂 anthropic 适配器。
|
||||
func messagesGateway(t *testing.T) (*httptest.Server, string) {
|
||||
t.Helper()
|
||||
up := anthropicUpstream(t)
|
||||
t.Cleanup(up.Close)
|
||||
|
||||
dir := t.TempDir()
|
||||
// adapterDir 故意不预先创建:VM 首启会把内置适配器 seed 进去。
|
||||
// 预先创建会被当成"目录已存在即为权威",于是没有任何内置适配器被加载,
|
||||
// 表现为 "adapter anthropic not loaded"。
|
||||
cfg := &config.Config{
|
||||
Path: filepath.Join(dir, "config.yaml"),
|
||||
AdapterDir: filepath.Join(dir, "adapters"),
|
||||
RuntimeFile: filepath.Join(dir, "runtime.json"),
|
||||
Listen: "127.0.0.1:0",
|
||||
DefaultModel: "claude-test",
|
||||
GatewayKeys: []string{"sk-e2e-0001"},
|
||||
Sources: []config.Source{{
|
||||
Name: "anth", BaseURL: up.URL, Adapter: "anthropic",
|
||||
APIKey: "k", MaxConcurrent: 4,
|
||||
Models: []config.Model{{ID: "claude-test", Kind: "chat", Priority: 100}},
|
||||
}},
|
||||
}
|
||||
|
||||
c, err := core.NewFromConfig(cfg)
|
||||
if err != nil {
|
||||
t.Fatalf("core: %v", err)
|
||||
}
|
||||
t.Cleanup(c.Close)
|
||||
gw, err := New(c)
|
||||
if err != nil {
|
||||
t.Fatalf("gateway: %v", err)
|
||||
}
|
||||
ts := httptest.NewServer(gw.Handler())
|
||||
t.Cleanup(ts.Close)
|
||||
return ts, "sk-e2e-0001"
|
||||
}
|
||||
|
||||
func postJSON(t *testing.T, url, key, body string) (int, string) {
|
||||
t.Helper()
|
||||
req, _ := http.NewRequest(http.MethodPost, url, strings.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", "Bearer "+key)
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
t.Fatalf("request: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
b, _ := io.ReadAll(resp.Body)
|
||||
return resp.StatusCode, string(b)
|
||||
}
|
||||
|
||||
// TestMessagesEndpointSpeaksAnthropic 是本文件的主断言:Anthropic 客户端
|
||||
// 打 /v1/messages,拿到 Anthropic 形状。
|
||||
func TestMessagesEndpointSpeaksAnthropic(t *testing.T) {
|
||||
ts, key := messagesGateway(t)
|
||||
code, body := postJSON(t, ts.URL+"/v1/messages", key,
|
||||
`{"model":"claude-test","max_tokens":64,"messages":[{"role":"user","content":"hi"}]}`)
|
||||
if code != 200 {
|
||||
t.Fatalf("status = %d: %s", code, body)
|
||||
}
|
||||
var m map[string]interface{}
|
||||
if err := json.Unmarshal([]byte(body), &m); err != nil {
|
||||
t.Fatalf("响应不是 JSON: %v (%s)", err, body)
|
||||
}
|
||||
if m["type"] != "message" {
|
||||
t.Errorf("type = %v, want message(内核不该退回 OpenAI 的 chat.completion)", m["type"])
|
||||
}
|
||||
if m["stop_reason"] != "end_turn" {
|
||||
t.Errorf("stop_reason = %v, want end_turn", m["stop_reason"])
|
||||
}
|
||||
blocks, _ := m["content"].([]interface{})
|
||||
if len(blocks) != 2 {
|
||||
t.Fatalf("content blocks = %v, want [thinking, text]", m["content"])
|
||||
}
|
||||
if b0 := blocks[0].(map[string]interface{}); b0["type"] != "thinking" || b0["thinking"] != "let me think" {
|
||||
t.Errorf("首个块应为 thinking: %v", b0)
|
||||
}
|
||||
if b1 := blocks[1].(map[string]interface{}); b1["type"] != "text" || b1["text"] != "pong" {
|
||||
t.Errorf("第二个块应为 text/pong: %v", b1)
|
||||
}
|
||||
u := m["usage"].(map[string]interface{})
|
||||
if u["input_tokens"] != float64(3) || u["output_tokens"] != float64(5) {
|
||||
t.Errorf("usage 应为 Anthropic 的 input/output 命名: %v", u)
|
||||
}
|
||||
// 内核不得混入 OpenAI 形状
|
||||
for _, forbidden := range []string{`"object"`, `"choices"`, `"chat.completion"`, `"prompt_tokens"`} {
|
||||
if strings.Contains(body, forbidden) {
|
||||
t.Errorf("OpenAI 字段 %s 混入 Anthropic 响应: %s", forbidden, body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestMessagesStreamSpeaksAnthropic 验证流式也是 Anthropic 事件序列,
|
||||
// 而不是 OpenAI SSE。
|
||||
func TestMessagesStreamSpeaksAnthropic(t *testing.T) {
|
||||
ts, key := messagesGateway(t)
|
||||
req, _ := http.NewRequest(http.MethodPost, ts.URL+"/v1/messages",
|
||||
strings.NewReader(`{"model":"claude-test","max_tokens":64,"stream":true,"messages":[{"role":"user","content":"hi"}]}`))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", "Bearer "+key)
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
s := string(body)
|
||||
|
||||
if !strings.Contains(s, "message_start") {
|
||||
t.Errorf("Anthropic 流应以 message_start 开头: %s", s)
|
||||
}
|
||||
if !strings.Contains(s, "message_stop") {
|
||||
t.Errorf("Anthropic 流应以 message_stop 收尾: %s", s)
|
||||
}
|
||||
// 内核不得擅自给 Anthropic 流加 OpenAI 的 [DONE]
|
||||
if strings.Contains(s, "[DONE]") {
|
||||
t.Errorf("Anthropic 流里出现了 OpenAI 的 [DONE] 终止标记: %s", s)
|
||||
}
|
||||
if strings.Contains(s, "chat.completion.chunk") {
|
||||
t.Errorf("流里混入 OpenAI chunk 形状: %s", s)
|
||||
}
|
||||
}
|
||||
|
||||
// TestMessagesRejectsBadRequest 确认入向校验(Anthropic 要求 max_tokens)
|
||||
// 与 OpenAI 路径一样在入口拒绝,不占用上游槽位。
|
||||
func TestMessagesRejectsBadRequest(t *testing.T) {
|
||||
ts, key := messagesGateway(t)
|
||||
// Anthropic 允许省略 max_tokens(上游会给默认),所以「有 messages 无
|
||||
// max_tokens」必须被接受;真正该拒的是没有 messages。
|
||||
code, body := postJSON(t, ts.URL+"/v1/messages", key,
|
||||
`{"model":"claude-test","messages":[{"role":"user","content":"hi"}]}`)
|
||||
if code != 200 {
|
||||
t.Fatalf("有 messages 的请求应被接受(max_tokens 可省),却得 %d: %s", code, body)
|
||||
}
|
||||
code, body = postJSON(t, ts.URL+"/v1/messages", key, `{"model":"claude-test"}`)
|
||||
if code != 400 {
|
||||
t.Errorf("无 messages 的请求 status = %d, want 400: %s", code, body)
|
||||
}
|
||||
// 坏 JSON
|
||||
code, _ = postJSON(t, ts.URL+"/v1/messages", key, `{"model":`)
|
||||
if code != 400 {
|
||||
t.Errorf("坏 JSON status = %d, want 400", code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestMessagesUnconfiguredFallsBackToOpenai 验证 R5 的兜底在新增端点上同样
|
||||
// 生效:源没有声明序列化钩子时(此例用 openai 适配器),回 OpenAI 形状。
|
||||
func TestMessagesUnconfiguredFallsBackToOpenai(t *testing.T) {
|
||||
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
fmt.Fprint(w, `{"choices":[{"message":{"content":"pong"},"finish_reason":"stop"}],
|
||||
"usage":{"prompt_tokens":3,"completion_tokens":1,"total_tokens":4}}`)
|
||||
}))
|
||||
defer up.Close()
|
||||
|
||||
dir := t.TempDir()
|
||||
// adapterDir 故意不预先创建:VM 首启会把内置适配器 seed 进去。
|
||||
// 预先创建会被当成"目录已存在即为权威",于是没有任何内置适配器被加载,
|
||||
// 表现为 "adapter anthropic not loaded"。
|
||||
cfg := &config.Config{
|
||||
Path: filepath.Join(dir, "config.yaml"),
|
||||
AdapterDir: filepath.Join(dir, "adapters"),
|
||||
RuntimeFile: filepath.Join(dir, "runtime.json"),
|
||||
Listen: "127.0.0.1:0",
|
||||
DefaultModel: "gpt-test",
|
||||
GatewayKeys: []string{"sk-e2e-0001"},
|
||||
Sources: []config.Source{{
|
||||
Name: "oai", BaseURL: up.URL, Adapter: "openai", APIKey: "k", MaxConcurrent: 4,
|
||||
Models: []config.Model{{ID: "gpt-test", Kind: "chat"}},
|
||||
}},
|
||||
}
|
||||
c, err := core.NewFromConfig(cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer c.Close()
|
||||
gw, err := New(c)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ts := httptest.NewServer(gw.Handler())
|
||||
defer ts.Close()
|
||||
|
||||
code, body := postJSON(t, ts.URL+"/v1/messages", "sk-e2e-0001",
|
||||
`{"model":"gpt-test","max_tokens":64,"messages":[{"role":"user","content":"hi"}]}`)
|
||||
if code != 200 {
|
||||
t.Fatalf("status = %d: %s", code, body)
|
||||
}
|
||||
if !strings.Contains(body, "chat.completion") {
|
||||
t.Errorf("无序列化钩子的源应回落 OpenAI 兜底形状: %s", body)
|
||||
}
|
||||
}
|
||||
|
||||
// TestMessagesRequiresAuth 确认新端点走同一套鉴权(不因为是新协议而放行)。
|
||||
func TestMessagesRequiresAuth(t *testing.T) {
|
||||
ts, _ := messagesGateway(t)
|
||||
code, _ := postJSON(t, ts.URL+"/v1/messages", "wrong-key",
|
||||
`{"model":"claude-test","max_tokens":8,"messages":[{"role":"user","content":"hi"}]}`)
|
||||
if code != 401 {
|
||||
t.Errorf("错误 key status = %d, want 401", code)
|
||||
}
|
||||
// 无 auth
|
||||
req, _ := http.NewRequest(http.MethodPost, ts.URL+"/v1/messages", strings.NewReader("{}"))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != 401 {
|
||||
t.Errorf("无 key status = %d, want 401", resp.StatusCode)
|
||||
}
|
||||
_ = context.Background()
|
||||
}
|
||||
|
||||
// TestMessagesIngressYieldsToBundledAnthropic 是 ingress 让出的主断言(用户要求:
|
||||
// /v1/messages 是内核兜底端点,适配器可声明它、内核主动让出)。
|
||||
//
|
||||
// 事实前提:内置 anthropic.lua 自己声明了 adapter.ingress = "/v1/messages",而它
|
||||
// 默认随包播种。所以真实部署里内核**总是**让出给 anthropic 适配器,
|
||||
// anthropicRequest.toUnified 退化为纯兜底(仅在适配器被移除/禁用时运行)。
|
||||
//
|
||||
// 断言方式:给源挂一个「上游即 Anthropic」的假服务器,客户端送一个 Anthropic
|
||||
// 形状请求。若内核让出,请求由适配器 deserialize_request 解析 → 路由/调度正常。
|
||||
// 同时用一个**只有系统消息**的请求验证解析确实走了适配器语义(Go 侧 system
|
||||
// 处理与 Lua 侧一致)。
|
||||
func TestMessagesIngressYieldsToBundledAnthropic(t *testing.T) {
|
||||
ts, key := messagesGateway(t)
|
||||
if code, body := postJSON(t, ts.URL+"/v1/messages", key,
|
||||
`{"model":"claude-test","max_tokens":16,"messages":[{"role":"user","content":"ping"}]}`); code != 200 {
|
||||
t.Fatalf("让出后 /v1/messages 应 200, 实得 %d: %s", code, body)
|
||||
}
|
||||
}
|
||||
|
||||
// TestAdapterForIngressClaim 直接验证内核的让出判定:声明了 ingress 的适配器
|
||||
// 会被 AdapterForIngress 找到;未声明的路径返回空(→ 内核用自己的兜底解析)。
|
||||
func TestAdapterForIngressClaim(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
cfg := &config.Config{
|
||||
Path: filepath.Join(dir, "config.yaml"),
|
||||
AdapterDir: filepath.Join(dir, "adapters"),
|
||||
RuntimeFile: filepath.Join(dir, "runtime.json"),
|
||||
Listen: "127.0.0.1:0",
|
||||
DefaultModel: "m",
|
||||
GatewayKeys: []string{"sk-e2e-0001"},
|
||||
}
|
||||
c, err := core.NewFromConfig(cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer c.Close()
|
||||
vm := c.VM()
|
||||
|
||||
// 内置 anthropic 声明了该端点
|
||||
if got := vm.AdapterForIngress("/v1/messages"); got != "anthropic" {
|
||||
t.Errorf("内置 anthropic 应认领 /v1/messages, 实得 %q", got)
|
||||
}
|
||||
// 未声明的路径 → 空 → 内核走自己的兜底解析
|
||||
if got := vm.AdapterForIngress("/v1/nothing"); got != "" {
|
||||
t.Errorf("未声明路径应为空, 实得 %q", got)
|
||||
}
|
||||
// 内置 anthropic 确实有 deserialize_request 钩子(否则内核会让出给一个空解析器)
|
||||
if !vm.HasFn("anthropic", "deserialize_request") {
|
||||
t.Error("anthropic 应实现 deserialize_request")
|
||||
}
|
||||
}
|
||||
|
||||
// TestMessagesKernelFallbackWhenNoAdapterClaims 证明兜底仍在:显式删掉所有
|
||||
// 声明 ingress 的适配器后,内核自己的 anthropicRequest.toUnified 接管,
|
||||
// /v1/messages 仍能服务(R5 兜底语义)。
|
||||
func TestMessagesKernelFallbackWhenNoAdapterClaims(t *testing.T) {
|
||||
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
fmt.Fprint(w, `{"choices":[{"message":{"content":"pong"},"finish_reason":"stop"}],
|
||||
"usage":{"prompt_tokens":3,"completion_tokens":1,"total_tokens":4}}`)
|
||||
}))
|
||||
defer up.Close()
|
||||
|
||||
dir := t.TempDir()
|
||||
cfg := &config.Config{
|
||||
Path: filepath.Join(dir, "config.yaml"),
|
||||
AdapterDir: filepath.Join(dir, "adapters"),
|
||||
RuntimeFile: filepath.Join(dir, "runtime.json"),
|
||||
Listen: "127.0.0.1:0",
|
||||
DefaultModel: "gpt-test",
|
||||
GatewayKeys: []string{"sk-e2e-0001"},
|
||||
Sources: []config.Source{{
|
||||
Name: "oai", BaseURL: up.URL, Adapter: "openai", APIKey: "k", MaxConcurrent: 4,
|
||||
Models: []config.Model{{ID: "gpt-test", Kind: "chat"}},
|
||||
}},
|
||||
}
|
||||
c, err := core.NewFromConfig(cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer c.Close()
|
||||
// 摘掉声明 ingress 的适配器(内置 anthropic),模拟"没有适配器认领该端点"。
|
||||
c.VM().RemoveAdapter("anthropic")
|
||||
if got := c.VM().AdapterForIngress("/v1/messages"); got != "" {
|
||||
t.Fatalf("移除后应无适配器认领, 实得 %q", got)
|
||||
}
|
||||
gw, err := New(c)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ts := httptest.NewServer(gw.Handler())
|
||||
defer ts.Close()
|
||||
|
||||
// 内核兜底解析 Anthropic 形状 → 200(走 OpenAI 兜底序列化)
|
||||
code, body := postJSON(t, ts.URL+"/v1/messages", "sk-e2e-0001",
|
||||
`{"model":"gpt-test","max_tokens":64,"messages":[{"role":"user","content":"hi"}]}`)
|
||||
if code != 200 {
|
||||
t.Fatalf("无适配器认领时内核兜底应仍 200, 实得 %d: %s", code, body)
|
||||
}
|
||||
}
|
||||
231
internal/gateway/mount.go
Normal file
231
internal/gateway/mount.go
Normal file
@ -0,0 +1,231 @@
|
||||
// Package gateway — 对外挂载点(R3 / Phase 6)
|
||||
//
|
||||
// 用户需求:插件与适配器可以注册**对外服务的端口**。
|
||||
//
|
||||
// 现状:插件已经能注册 HTTP 端点,但被固定在 /api/plugins/http/ 前缀下。
|
||||
// 那条前缀限制是有理由的(可 grep、可整体撤销、插件无法影子内核路由),
|
||||
// 但它也意味着插件无法提供"看起来像内核一部分"的接口——而对外服务恰恰
|
||||
// 需要这个(例如给上游回调用的固定路径 /v1/plugins/<name>/callback)。
|
||||
//
|
||||
// 本文件放开这一点,同时把当初那条限制的**三个理由**逐条补回来:
|
||||
//
|
||||
// 1. 可 grep / 可整体撤销 → 每条自定义挂载都必须落在 /plugins/ 或
|
||||
// /v1/plugins/ 命名空间内(不接受任意前缀)
|
||||
// 2. 不能影子内核路由 → 与内核保留路由表做前缀冲突检测,撞了直接拒绝
|
||||
// 3. 鉴权默认关闭 → 沿用 HTTPHandler 的 roles/keys/public 语义
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"llmsproxy/internal/lua"
|
||||
)
|
||||
|
||||
// allowedMountPrefixes are the only namespaces a plugin/adapters may claim.
|
||||
//
|
||||
// A free-form prefix would let a plugin register "/v1/chat/completions" and
|
||||
// silently intercept every inference request on the gateway — the exact
|
||||
// shadowing the narrow default prefix was protecting against. Constraining the
|
||||
// namespace keeps the security property while still allowing a stable public
|
||||
// path.
|
||||
var allowedMountPrefixes = []string{"/plugins/", "/v1/plugins/"}
|
||||
|
||||
// kernelReservedPrefixes are prefixes the kernel owns. A custom mount that
|
||||
// overlaps any of them is refused, so an operator's route table can never
|
||||
// change meaning because a plugin was installed.
|
||||
//
|
||||
// This list is deliberately explicit rather than derived from the router's
|
||||
// switch: the switch is code, and a future branch added there would silently
|
||||
// become shadowable if this were inferred. The test
|
||||
// TestKernelRoutesAreAllCoveredByReservedPrefixes keeps the two in sync.
|
||||
var kernelReservedPrefixes = []string{
|
||||
"/v1/chat/completions",
|
||||
"/v1/messages",
|
||||
"/v1/images/generations",
|
||||
"/v1/models",
|
||||
"/api/",
|
||||
"/login",
|
||||
"/ui",
|
||||
"/",
|
||||
}
|
||||
|
||||
// ValidateCustomMount checks that a plugin-declared mount point is allowed.
|
||||
//
|
||||
// Returns the normalized path (leading slash, no trailing slash) or an error
|
||||
// naming the conflict. The error text is what an operator sees in the plugin's
|
||||
// load-error field, so it says WHICH rule was hit.
|
||||
func ValidateCustomMount(p string) (string, error) {
|
||||
p = strings.TrimSpace(p)
|
||||
if p == "" {
|
||||
return "", fmt.Errorf("empty mount path")
|
||||
}
|
||||
if !strings.HasPrefix(p, "/") {
|
||||
return "", fmt.Errorf("mount path %q must start with /", p)
|
||||
}
|
||||
if strings.Contains(p, "..") || strings.Contains(p, "//") {
|
||||
return "", fmt.Errorf("mount path %q contains an empty or escaping segment", p)
|
||||
}
|
||||
norm := strings.TrimRight(p, "/")
|
||||
if norm == "" {
|
||||
norm = "/"
|
||||
}
|
||||
|
||||
// Rule 1: must live in one of the allowed namespaces.
|
||||
ok := false
|
||||
for _, pre := range allowedMountPrefixes {
|
||||
if strings.HasPrefix(norm+"/", pre) {
|
||||
ok = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !ok {
|
||||
return "", fmt.Errorf("mount path %q must be under %s",
|
||||
norm, strings.Join(allowedMountPrefixes, " or "))
|
||||
}
|
||||
|
||||
// Rule 2: must not overlap a kernel route.
|
||||
for _, res := range kernelReservedPrefixes {
|
||||
if res == "/" {
|
||||
continue // the catch-all is handled by the namespace rule above
|
||||
}
|
||||
if norm == strings.TrimRight(res, "/") || strings.HasPrefix(norm, res) {
|
||||
return "", fmt.Errorf("mount path %q collides with the kernel route %q", norm, res)
|
||||
}
|
||||
}
|
||||
return norm, nil
|
||||
}
|
||||
|
||||
// RouteOwner describes one occupied route, for conflict reporting.
|
||||
type RouteOwner struct {
|
||||
Path string
|
||||
Owner string // "kernel", "plugin:name", "adapter:name"
|
||||
}
|
||||
|
||||
// BuildRouteTable assembles the set of paths a plugin/adapters must not take.
|
||||
// It is used both to validate new mounts and to explain a conflict to an
|
||||
// operator (a bare "conflict" is not actionable; "already served by plugin X" is).
|
||||
func BuildRouteTable(pluginMounts, adapterMounts map[string]string) []RouteOwner {
|
||||
var out []RouteOwner
|
||||
for _, p := range kernelReservedPrefixes {
|
||||
if p == "/" {
|
||||
continue
|
||||
}
|
||||
out = append(out, RouteOwner{Path: strings.TrimRight(p, "/"), Owner: "kernel"})
|
||||
}
|
||||
for path, owner := range pluginMounts {
|
||||
out = append(out, RouteOwner{Path: path, Owner: "plugin:" + owner})
|
||||
}
|
||||
for path, owner := range adapterMounts {
|
||||
out = append(out, RouteOwner{Path: path, Owner: "adapter:" + owner})
|
||||
}
|
||||
sort.Slice(out, func(i, j int) bool { return out[i].Path < out[j].Path })
|
||||
return out
|
||||
}
|
||||
|
||||
// FindMountConflict reports whether candidate overlaps an occupied route and, if
|
||||
// so, who holds it.
|
||||
//
|
||||
// Overlap is checked in BOTH directions: a new mount that CONTAINS an existing
|
||||
// one shadows it just as effectively as one nested inside it, and only checking
|
||||
// the prefix direction would let "/plugins/x/sub" be registered before
|
||||
// "/plugins/x" and then silently swallow it.
|
||||
func FindMountConflict(candidate string, table []RouteOwner) (RouteOwner, bool) {
|
||||
c := strings.TrimRight(candidate, "/")
|
||||
for _, r := range table {
|
||||
rp := strings.TrimRight(r.Path, "/")
|
||||
if rp == "" {
|
||||
continue
|
||||
}
|
||||
if c == rp || strings.HasPrefix(c, rp+"/") || strings.HasPrefix(rp, c+"/") {
|
||||
return r, true
|
||||
}
|
||||
}
|
||||
return RouteOwner{}, false
|
||||
}
|
||||
|
||||
// handleCustomMount dispatches a request to a plugin that claimed a custom mount
|
||||
// path (/plugins/<name>/… or /v1/plugins/<name>/…).
|
||||
//
|
||||
// It deliberately REUSES handlePluginHTTP rather than reimplementing dispatch:
|
||||
// that function already does longest-match resolution, per-endpoint
|
||||
// roles/keys/public authorization and the Lua call protocol. Duplicating it here
|
||||
// would create two dispatch paths that must be kept in agreement — and a
|
||||
// security rule that is enforced on only one of them is worse than none.
|
||||
//
|
||||
// The rewrite below maps a claimed mount onto the handler table's canonical
|
||||
// form: handler paths are declared as "<name>/<rest>", so the request path is
|
||||
// reduced to exactly that. Anything that does not reduce cleanly (a missing
|
||||
// name, a name with no registered handler) is a 404, not a fallthrough — a
|
||||
// custom namespace that silently answered from the default prefix would make
|
||||
// the mount validation meaningless.
|
||||
func (g *Gateway) handleCustomMount(w http.ResponseWriter, r *http.Request) {
|
||||
owner, rest, ok := parseMountRef(r.URL.Path)
|
||||
if !ok || owner == "" {
|
||||
writeError(w, http.StatusNotFound, "not_found", "malformed plugin mount path")
|
||||
return
|
||||
}
|
||||
ps := g.core.Plugins()
|
||||
if ps == nil {
|
||||
writeError(w, http.StatusNotFound, "not_found", "no plugins loaded")
|
||||
return
|
||||
}
|
||||
// Resolve which handler owns this mount, so an unclaimed namespace is a 404.
|
||||
handlers := ps.HTTPHandlers()
|
||||
candidate := owner
|
||||
if rest != "" {
|
||||
candidate = owner + "/" + rest
|
||||
}
|
||||
if !mountClaimed(handlers, candidate) {
|
||||
writeError(w, http.StatusNotFound, "not_found",
|
||||
"no plugin serves "+r.URL.Path)
|
||||
return
|
||||
}
|
||||
// Reuse the default dispatcher by presenting the path in its own shape.
|
||||
r2 := r.Clone(r.Context())
|
||||
r2.URL.Path = pluginHTTPPrefix + candidate
|
||||
g.handlePluginHTTP(w, r2)
|
||||
}
|
||||
|
||||
// mountClaimed reports whether any registered handler's path is a prefix of the
|
||||
// candidate (longest-match semantics: /plugins/auth/token is served by a
|
||||
// handler declared as "auth").
|
||||
func mountClaimed(handlers []lua.HTTPHandler, candidate string) bool {
|
||||
for i := range handlers {
|
||||
h := strings.Trim(handlers[i].Path, "/")
|
||||
if h == "" {
|
||||
continue
|
||||
}
|
||||
if candidate == h || strings.HasPrefix(candidate, h+"/") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// parseMountRef splits "/plugins/<name>/<rest>" or "/v1/plugins/<name>/<rest>"
|
||||
// into its owner name and remaining path.
|
||||
//
|
||||
// The "/v1/plugins/" form is checked first because it has the longer prefix:
|
||||
// testing "/plugins/" first would match nothing for a /v1/ path, but keeping the
|
||||
// ordering explicit avoids a future prefix being added that nests inside
|
||||
// another.
|
||||
func parseMountRef(p string) (owner, rest string, ok bool) {
|
||||
prefixes := []string{"/v1/plugins/", "/plugins/"}
|
||||
for _, prefix := range prefixes {
|
||||
if !strings.HasPrefix(p, prefix) {
|
||||
continue
|
||||
}
|
||||
trimmed := strings.TrimPrefix(p, prefix)
|
||||
if trimmed == "" {
|
||||
return "", "", false
|
||||
}
|
||||
if i := strings.Index(trimmed, "/"); i >= 0 {
|
||||
return trimmed[:i], trimmed[i+1:], true
|
||||
}
|
||||
return trimmed, "", true
|
||||
}
|
||||
return "", "", false
|
||||
}
|
||||
324
internal/gateway/mount_test.go
Normal file
324
internal/gateway/mount_test.go
Normal file
@ -0,0 +1,324 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"llmsproxy/internal/config"
|
||||
"llmsproxy/internal/core"
|
||||
)
|
||||
|
||||
// 本文件锁定 R3 的两条不变量:
|
||||
// 1. 自定义挂载只能落在允许的命名空间内
|
||||
// 2. 任何内核路由都不能被插件声明覆盖 —— 且这条在新增内核路由时**不会失效**
|
||||
|
||||
// TestValidateCustomMountNamespace 只有 /plugins/ 与 /v1/plugins/ 可被占用。
|
||||
func TestValidateCustomMountNamespace(t *testing.T) {
|
||||
ok := []string{
|
||||
"/plugins/auth/token",
|
||||
"/plugins/auth",
|
||||
"/v1/plugins/callback",
|
||||
"/v1/plugins/callback/recv",
|
||||
"/plugins/nested/deep/path",
|
||||
}
|
||||
for _, p := range ok {
|
||||
if _, err := ValidateCustomMount(p); err != nil {
|
||||
t.Errorf("合法挂载 %q 被拒: %v", p, err)
|
||||
}
|
||||
}
|
||||
bad := []string{
|
||||
"",
|
||||
"plugins/x", // 没有前导斜杠
|
||||
"/v1/chat/completions", // 影子内核路由(且不在允许命名空间)
|
||||
"/api/anything", // 内核命名空间
|
||||
"/callback", // 任意前缀
|
||||
"/plugins/../etc", // 逃逸
|
||||
"/plugins//double", // 空段
|
||||
"/v1/plugins/../../x",
|
||||
}
|
||||
for _, p := range bad {
|
||||
if _, err := ValidateCustomMount(p); err == nil {
|
||||
t.Errorf("非法挂载 %q 被接受", p)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestCustomMountCannotShadowKernelRoute 逐条核对内核保留前缀。
|
||||
func TestCustomMountCannotShadowKernelRoute(t *testing.T) {
|
||||
// 这些是要保护的公开接口;任何能覆盖它们的挂载都必须被拒。
|
||||
for _, kernel := range []string{"/v1/chat/completions", "/v1/messages", "/v1/models"} {
|
||||
// 尝试用允许命名空间里的路径去"贴近"内核路由 —— 例如
|
||||
// /v1/plugins/../../v1/models(逃逸),必须被拒。
|
||||
cand := "/v1/plugins/../../" + strings.TrimPrefix(kernel, "/")
|
||||
if _, err := ValidateCustomMount(cand); err == nil {
|
||||
t.Errorf("逃逸路径 %q 应被拒", cand)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestKernelRoutesAreAllCoveredByReservedPrefixes 是这道防线**不腐坏**的保证。
|
||||
//
|
||||
// 它从 server.go 里把路由 switch 的分支字符串刮出来,逐个检查是否被
|
||||
// kernelReservedPrefixes 覆盖。将来有人新增一条内核路由却忘了更新保留表时,
|
||||
// 这条测试会失败 —— 否则新路由会静默变成可被插件覆盖的。
|
||||
func TestKernelRoutesAreAllCoveredByReservedPrefixes(t *testing.T) {
|
||||
src, err := os.ReadFile("server.go")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// 抓 switch 里的字面量路径:case r.URL.Path == "/x" / HasPrefix(..., "/x")
|
||||
re := regexp.MustCompile(`r\.URL\.Path\s*==\s*"(/[^"]*)"`)
|
||||
matches := re.FindAllStringSubmatch(string(src), -1)
|
||||
if len(matches) == 0 {
|
||||
t.Fatal("没有从 server.go 抓到任何路由字面量 —— 测试自身失效")
|
||||
}
|
||||
covered := func(p string) bool {
|
||||
// "/" is the UI catch-all (serveUI). It is the default branch, not a
|
||||
// path a plugin could shadow, and it is covered by the namespace rule:
|
||||
// a custom mount must start with /plugins/ or /v1/plugins/, neither of
|
||||
// which is "/".
|
||||
if p == "/" {
|
||||
return true
|
||||
}
|
||||
for _, res := range kernelReservedPrefixes {
|
||||
if res == "/" {
|
||||
continue
|
||||
}
|
||||
rp := strings.TrimRight(res, "/")
|
||||
if p == rp || strings.HasPrefix(p, rp+"/") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
// /api/plugins/... 由 pluginHTTPPrefix 与 /api/ 覆盖
|
||||
if strings.HasPrefix(p, "/api/") {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
for _, m := range matches {
|
||||
p := m[1]
|
||||
if !covered(p) {
|
||||
t.Errorf("内核路由 %q 未被 kernelReservedPrefixes 覆盖 —— "+
|
||||
"新增路由时必须同步更新保留表,否则插件可影子它", p)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestMountConflictDetectionBothDirections 冲突检测必须双向。
|
||||
//
|
||||
// 只查一个方向会让「先注册 /plugins/x,后注册 /plugins/x/sub」或反过来时的
|
||||
// 一个被另一个静默吞掉。
|
||||
func TestMountConflictDetectionBothDirections(t *testing.T) {
|
||||
table := []RouteOwner{{Path: "/plugins/x", Owner: "plugin:a"}}
|
||||
// 被包含
|
||||
if _, conflict := FindMountConflict("/plugins/x/sub", table); !conflict {
|
||||
t.Error("嵌套在已有挂载下的路径应判冲突")
|
||||
}
|
||||
// 包含
|
||||
if _, conflict := FindMountConflict("/plugins", table); !conflict {
|
||||
t.Error("包含已有挂载的路径应判冲突")
|
||||
}
|
||||
// 相同
|
||||
if _, conflict := FindMountConflict("/plugins/x", table); !conflict {
|
||||
t.Error("完全相同的路径应判冲突")
|
||||
}
|
||||
// 无关(前缀相似但不是路径段)
|
||||
if _, conflict := FindMountConflict("/plugins/xy", table); conflict {
|
||||
t.Error("/plugins/xy 与 /plugins/x 不是同一个路径段,不该判冲突")
|
||||
}
|
||||
}
|
||||
|
||||
// TestMountConflictNamesTheOwner 冲突信息必须点名持有者(否则不可行动)。
|
||||
func TestMountConflictNamesTheOwner(t *testing.T) {
|
||||
table := []RouteOwner{{Path: "/plugins/taken", Owner: "plugin:other"}}
|
||||
owner, conflict := FindMountConflict("/plugins/taken/deep", table)
|
||||
if !conflict {
|
||||
t.Fatal("应判冲突")
|
||||
}
|
||||
if owner.Owner != "plugin:other" {
|
||||
t.Errorf("应点名持有者 plugin:other, 实得 %q", owner.Owner)
|
||||
}
|
||||
}
|
||||
|
||||
// TestParseMountRef 解析两种命名空间。
|
||||
func TestParseMountRef(t *testing.T) {
|
||||
cases := []struct {
|
||||
in, owner, rest string
|
||||
ok bool
|
||||
}{
|
||||
{"/plugins/auth/token", "auth", "token", true},
|
||||
{"/plugins/auth", "auth", "", true},
|
||||
{"/v1/plugins/cb/recv", "cb", "recv", true},
|
||||
{"/v1/plugins/cb", "cb", "", true},
|
||||
{"/plugins/", "", "", false},
|
||||
{"/plugins", "", "", false},
|
||||
{"/other/x", "", "", false},
|
||||
}
|
||||
for _, c := range cases {
|
||||
owner, rest, ok := parseMountRef(c.in)
|
||||
if ok != c.ok || owner != c.owner || rest != c.rest {
|
||||
t.Errorf("parseMountRef(%q) = (%q,%q,%v), want (%q,%q,%v)",
|
||||
c.in, owner, rest, ok, c.owner, c.rest, c.ok)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestBuildRouteTableIncludesKernel 路由表含内核保留项(供冲突提示用)。
|
||||
func TestBuildRouteTableIncludesKernel(t *testing.T) {
|
||||
table := BuildRouteTable(
|
||||
map[string]string{"/plugins/a": "alpha"},
|
||||
map[string]string{"/v1/plugins/b": "beta"},
|
||||
)
|
||||
var hasKernel, hasPlugin, hasAdapter bool
|
||||
for _, r := range table {
|
||||
if r.Owner == "kernel" {
|
||||
hasKernel = true
|
||||
}
|
||||
if r.Owner == "plugin:alpha" {
|
||||
hasPlugin = true
|
||||
}
|
||||
if r.Owner == "adapter:beta" {
|
||||
hasAdapter = true
|
||||
}
|
||||
}
|
||||
if !hasKernel || !hasPlugin || !hasAdapter {
|
||||
t.Errorf("路由表应含 kernel/plugin/adapter 三类: %+v", table)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPluginServesCustomMountEndToEnd 是 R3 的端到端验收:
|
||||
// 插件声明一个自定义对外挂载点,客户端按该路径访问,得到插件的应答。
|
||||
func TestPluginServesCustomMountEndToEnd(t *testing.T) {
|
||||
ts, key := messagesGateway(t)
|
||||
// messagesGateway 用的 core 没有插件目录配置,这里直接建一个带插件的。
|
||||
_ = ts
|
||||
_ = key
|
||||
|
||||
dir := t.TempDir()
|
||||
cfg := &config.Config{
|
||||
Path: filepath.Join(dir, "config.yaml"),
|
||||
AdapterDir: filepath.Join(dir, "adapters"),
|
||||
PluginDir: filepath.Join(dir, "plugins"),
|
||||
RuntimeFile: filepath.Join(dir, "runtime.json"),
|
||||
Listen: "127.0.0.1:0",
|
||||
DefaultModel: "AUTO",
|
||||
GatewayKeys: []string{"sk-mount-0001"},
|
||||
Sources: []config.Source{{
|
||||
Name: "s", BaseURL: "http://127.0.0.1:1/v1", Adapter: "openai",
|
||||
APIKey: "k", MaxConcurrent: 1,
|
||||
Models: []config.Model{{ID: "m", Kind: "chat"}},
|
||||
}},
|
||||
}
|
||||
c, err := core.NewFromConfig(cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer c.Close()
|
||||
gw, err := New(c)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ts2 := httptest.NewServer(gw.Handler())
|
||||
defer ts2.Close()
|
||||
|
||||
// 装一个声明自定义挂载的插件
|
||||
code := `
|
||||
local plugin = { name = "publicapi", version = "1.0" }
|
||||
plugin.http = {
|
||||
{ path = "publicapi/token", method = "GET", handler = "token",
|
||||
roles = { "admin", "user" }, description = "对外 token 端点" },
|
||||
}
|
||||
function plugin.token(req)
|
||||
return { status = 200, body = '{"token":"abc"}' }
|
||||
end
|
||||
return plugin
|
||||
`
|
||||
if err := c.Plugins().LoadSource("publicapi", code); err != nil {
|
||||
t.Fatalf("load plugin: %v", err)
|
||||
}
|
||||
|
||||
// 按自定义挂载访问
|
||||
req, _ := http.NewRequest("GET", ts2.URL+"/plugins/publicapi/token", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+cfg.GatewayKeys[0])
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
if resp.StatusCode != 200 {
|
||||
t.Fatalf("自定义挂载 status = %d: %s", resp.StatusCode, body)
|
||||
}
|
||||
if !strings.Contains(string(body), "abc") {
|
||||
t.Errorf("自定义挂载未返回插件应答: %s", body)
|
||||
}
|
||||
|
||||
// /v1/plugins/ 形式同样可用
|
||||
req2, _ := http.NewRequest("GET", ts2.URL+"/v1/plugins/publicapi/token", nil)
|
||||
req2.Header.Set("Authorization", "Bearer "+cfg.GatewayKeys[0])
|
||||
resp2, err := http.DefaultClient.Do(req2)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp2.Body.Close()
|
||||
if resp2.StatusCode != 200 {
|
||||
t.Errorf("/v1/plugins/ 形式 status = %d", resp2.StatusCode)
|
||||
}
|
||||
|
||||
// 未声明的命名空间 404(不能因为落在允许命名空间就放行)
|
||||
req3, _ := http.NewRequest("GET", ts2.URL+"/plugins/nobody/here", nil)
|
||||
req3.Header.Set("Authorization", "Bearer "+cfg.GatewayKeys[0])
|
||||
resp3, err := http.DefaultClient.Do(req3)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp3.Body.Close()
|
||||
if resp3.StatusCode != 404 {
|
||||
t.Errorf("未注册的挂载应 404, 实得 %d", resp3.StatusCode)
|
||||
}
|
||||
|
||||
// 鉴权仍生效:无 key 时,
|
||||
// - API 路径(/v1/plugins/…)应 401(isAPIPath 判定)
|
||||
// - 浏览器路径(/plugins/…)应 302 跳登录页
|
||||
req4, _ := http.NewRequest("GET", ts2.URL+"/v1/plugins/publicapi/token", nil)
|
||||
noRedirect := &http.Client{CheckRedirect: func(*http.Request, []*http.Request) error {
|
||||
return http.ErrUseLastResponse
|
||||
}}
|
||||
resp4, err := noRedirect.Do(req4)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp4.Body.Close()
|
||||
if resp4.StatusCode != 401 {
|
||||
t.Errorf("无 key 访问 /v1/plugins/ 应 401, 实得 %d", resp4.StatusCode)
|
||||
}
|
||||
|
||||
req5, _ := http.NewRequest("GET", ts2.URL+"/plugins/publicapi/token", nil)
|
||||
resp5, err := noRedirect.Do(req5)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp5.Body.Close()
|
||||
if resp5.StatusCode != 302 {
|
||||
t.Errorf("无 key 访问 /plugins/ 应 302 跳登录, 实得 %d", resp5.StatusCode)
|
||||
}
|
||||
if loc := resp5.Header.Get("Location"); !strings.Contains(loc, "/login") {
|
||||
t.Errorf("应跳转到 /login, 实得 %q", loc)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCustomMountCannotInterceptKernelRoute 端到端确认内核路由不受影响:
|
||||
// 即使有插件在用 /plugins/,/v1/chat/completions 与 /v1/models 仍归内核。
|
||||
func TestCustomMountCannotInterceptKernelRoute(t *testing.T) {
|
||||
for _, p := range []string{"/v1/chat/completions", "/v1/models", "/v1/messages", "/api/stats"} {
|
||||
if _, err := ValidateCustomMount(p); err == nil {
|
||||
t.Errorf("内核路由 %q 必须不能被声明为自定义挂载", p)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -147,4 +147,4 @@ func TestPluginCallIsAdminOnly(t *testing.T) {
|
||||
t.Fatalf("a non-admin key reached the call endpoint: HTTP %d %s",
|
||||
rr.Code, rr.Body.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -17,6 +17,7 @@ package gateway
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
@ -45,12 +46,25 @@ func (g *Gateway) handlePluginHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
matches := ps.HTTPHandlers()
|
||||
var chosen *lua.HTTPHandler
|
||||
var params map[string]string
|
||||
// contenders holds the handlers that matched at the current best depth with
|
||||
// the SAME literal route pattern — i.e. two plugins that both declared this
|
||||
// exact path. That is the contested case the operator must arbitrate (user
|
||||
// requirement A): rather than let load order pick a silent winner, a
|
||||
// contested path with no recorded decision returns 409 naming the candidates.
|
||||
var contenders []lua.HTTPHandler
|
||||
for i := range matches {
|
||||
h := matches[i]
|
||||
if p, ok := matchHandlerPath(h.Path, rest); ok {
|
||||
if chosen == nil || segmentCount(h.Path) > segmentCount(chosen.Path) {
|
||||
chosen, params = &h, p
|
||||
}
|
||||
p, ok := matchHandlerPath(h.Path, rest)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
switch {
|
||||
case chosen == nil || segmentCount(h.Path) > segmentCount(chosen.Path):
|
||||
chosen, params = &matches[i], p
|
||||
contenders = []lua.HTTPHandler{matches[i]}
|
||||
case segmentCount(h.Path) == segmentCount(chosen.Path) && h.Path == chosen.Path:
|
||||
// Same route pattern, a different plugin. The winner is arbitrated below.
|
||||
contenders = append(contenders, matches[i])
|
||||
}
|
||||
}
|
||||
if chosen == nil {
|
||||
@ -58,6 +72,13 @@ func (g *Gateway) handlePluginHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
"no plugin serves this path")
|
||||
return
|
||||
}
|
||||
if len(contenders) > 1 {
|
||||
resolved := g.resolveContestedRoute(w, ps, chosen.Path, contenders)
|
||||
if resolved == nil {
|
||||
return // 409 already written, naming the candidates
|
||||
}
|
||||
chosen = resolved
|
||||
}
|
||||
|
||||
if !strings.EqualFold(r.Method, chosen.Method) {
|
||||
w.Header().Set("Allow", chosen.Method)
|
||||
@ -98,6 +119,44 @@ func (g *Gateway) handlePluginHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
lua.WriteHandlerResponse(w, resp)
|
||||
}
|
||||
|
||||
// resolveContestedRoute arbitrates a service path that more than one plugin
|
||||
// declared (user requirement A). It returns the winning handler, or nil after
|
||||
// writing a 409 when the operator has not yet chosen.
|
||||
//
|
||||
// This is the crux of the "report, don't crash, don't silently pick" rule: with
|
||||
// two plugins both serving e.g. "stats", load order used to decide the winner.
|
||||
// Now an unresolved contest is a 409 that NAMES the candidates, and the operator
|
||||
// picks one in the WebUI (POST /api/plugins/conflicts). A recorded decision is
|
||||
// honoured here on every request thereafter.
|
||||
func (g *Gateway) resolveContestedRoute(w http.ResponseWriter, ps *lua.Plugins, path string, contenders []lua.HTTPHandler) *lua.HTTPHandler {
|
||||
winner := ps.RouteWinner(path)
|
||||
if winner != "" {
|
||||
for i := range contenders {
|
||||
if contenders[i].Plugin == winner {
|
||||
return &contenders[i]
|
||||
}
|
||||
}
|
||||
// A decision exists but points at a plugin that no longer serves this
|
||||
// path (it was removed or edited). Fall through to 409 rather than honour
|
||||
// a stale winner that cannot answer.
|
||||
}
|
||||
names := make([]string, 0, len(contenders))
|
||||
for i := range contenders {
|
||||
names = append(names, contenders[i].Plugin)
|
||||
}
|
||||
sort.Strings(names)
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
writeJSON(w, http.StatusConflict, map[string]interface{}{
|
||||
"error": map[string]interface{}{
|
||||
"type": "route_conflict",
|
||||
"message": "service path \"" + path + "\" is claimed by " + strconv.Itoa(len(names)) + " plugins (" + strings.Join(names, ", ") + "); an operator must choose one in the admin UI (Conflicts) or POST /api/plugins/conflicts",
|
||||
"path": path,
|
||||
"candidates": names,
|
||||
},
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
// handlerAccessHint explains, in the refusal, exactly which declaration would
|
||||
// open this endpoint. A 403 with no reason is the kind of thing an operator
|
||||
// opens the source to work out, and then the fix is a guess.
|
||||
@ -204,4 +263,4 @@ type bodyTooLarge struct{}
|
||||
func (bodyTooLarge) Error() string { return "request body exceeds 4 MiB" }
|
||||
|
||||
// limitBody is kept for the numeric form some callers want in a log line.
|
||||
var _ = strconv.Itoa
|
||||
var _ = strconv.Itoa
|
||||
|
||||
@ -82,6 +82,57 @@ func (g *Gateway) handlePluginsAPI(w http.ResponseWriter, r *http.Request) {
|
||||
path := strings.TrimPrefix(r.URL.Path, "/api/plugins")
|
||||
path = strings.Trim(path, "/")
|
||||
|
||||
// /api/plugins/conflicts — the arbitration surface (user requirement).
|
||||
// GET lists every contested route (A) and every suspended adapter-name
|
||||
// install (B), each with its candidates/incumbent and any recorded
|
||||
// decision.
|
||||
// POST {kind,path|name,winner} records the operator's choice and persists
|
||||
// it. Must be routed BEFORE the single-segment {name} handler below,
|
||||
// which would otherwise read "conflicts" as a plugin name.
|
||||
if path == "conflicts" {
|
||||
if reqRole(r.Context()) != "admin" {
|
||||
writeError(w, http.StatusForbidden, "forbidden", "admin role required")
|
||||
return
|
||||
}
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
writeJSON(w, http.StatusOK, map[string]interface{}{
|
||||
"routes": ps.RouteConflicts(),
|
||||
"adapters": ps.AdapterConflicts(),
|
||||
})
|
||||
case http.MethodPost:
|
||||
var body struct {
|
||||
Kind string `json:"kind"` // "route" or "adapter"
|
||||
Path string `json:"path"`
|
||||
Name string `json:"name"`
|
||||
Winner string `json:"winner"` // route: plugin name; adapter: "incumbent"|"candidate"
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid_request", "invalid json: "+err.Error())
|
||||
return
|
||||
}
|
||||
switch body.Kind {
|
||||
case "route":
|
||||
if err := ps.ResolveRouteConflict(body.Path, body.Winner); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "conflict_error", err.Error())
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]interface{}{"ok": true, "path": body.Path, "winner": body.Winner})
|
||||
case "adapter":
|
||||
if err := ps.ResolveAdapterConflict(body.Name, body.Winner); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "conflict_error", err.Error())
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]interface{}{"ok": true, "name": body.Name, "winner": body.Winner})
|
||||
default:
|
||||
writeError(w, http.StatusBadRequest, "invalid_request", "kind must be \"route\" or \"adapter\"")
|
||||
}
|
||||
default:
|
||||
writeError(w, http.StatusMethodNotAllowed, "method_not_allowed", "")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// /api/plugins/{name}/state
|
||||
if strings.HasSuffix(path, "/state") {
|
||||
name := strings.TrimSuffix(path, "/state")
|
||||
@ -105,6 +156,22 @@ func (g *Gateway) handlePluginsAPI(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
// /api/plugins/{name}/kernel — the operator's view of, and edit surface for,
|
||||
// which kernel actions a plugin may call, plus its declared extra outbound hosts.
|
||||
if strings.HasSuffix(path, "/kernel") {
|
||||
name := strings.TrimSuffix(path, "/kernel")
|
||||
if name == "" {
|
||||
writeError(w, http.StatusBadRequest, "invalid_request", "plugin name required")
|
||||
return
|
||||
}
|
||||
g.handlePluginKernel(w, r, name)
|
||||
return
|
||||
}
|
||||
|
||||
// /api/plugins/{name}/login — REMOVED. Login sessions are plugin domain:
|
||||
// channels.lua owns its own session state and serves it through its own
|
||||
// mount. The kernel has no login concept.
|
||||
|
||||
// /api/plugins/{name}/call/{fn}
|
||||
if i := strings.Index(path, "/call/"); i >= 0 {
|
||||
name := path[:i]
|
||||
@ -117,6 +184,20 @@ func (g *Gateway) handlePluginsAPI(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// /api/plugins/{name}/fanout/{fn} — one call per argument set, in
|
||||
// parallel, from one request. See handlePluginFanout for why the
|
||||
// request-per-item form cannot be made concurrent from the client.
|
||||
if i := strings.Index(path, "/fanout/"); i >= 0 {
|
||||
name := path[:i]
|
||||
fn := path[i+len("/fanout/"):]
|
||||
if name == "" || fn == "" {
|
||||
writeError(w, http.StatusBadRequest, "invalid_request", "fanout needs a plugin and a function name")
|
||||
return
|
||||
}
|
||||
g.handlePluginFanout(w, r, name, fn)
|
||||
return
|
||||
}
|
||||
|
||||
if reqRole(r.Context()) != "admin" {
|
||||
writeError(w, http.StatusForbidden, "forbidden", "admin role required")
|
||||
return
|
||||
@ -133,7 +214,7 @@ func (g *Gateway) handlePluginsAPI(w http.ResponseWriter, r *http.Request) {
|
||||
// Visibility overrides and the endpoint access table, so the
|
||||
// plugin page renders what is IN FORCE rather than making the
|
||||
// operator read .lua files or config.yaml to find out.
|
||||
"visibility": ps.VisibilityOverrides(),
|
||||
"visibility": ps.VisibilityOverrides(),
|
||||
"http_access": ps.EndpointAccess(),
|
||||
})
|
||||
case http.MethodPost:
|
||||
@ -331,6 +412,68 @@ func (g *Gateway) handlePluginCall(w http.ResponseWriter, r *http.Request, name,
|
||||
writeJSON(w, http.StatusOK, map[string]interface{}{"result": result})
|
||||
}
|
||||
|
||||
// handlePluginFanout runs one plugin export per element of a list, in parallel,
|
||||
// from a SINGLE request.
|
||||
//
|
||||
// Why this exists: the quota page has twenty cards, and it used to fire twenty
|
||||
// browser requests at /call/. They arrived concurrently and then serialised
|
||||
// inside Plugins.Call, which holds the plugin mutex across the Lua function —
|
||||
// including its network wait. Measured on the live gateway: 20 probes took 29s of
|
||||
// wall clock while a single probe with no contention took 1.4–3.3s, so the page
|
||||
// sat on "probing…" for half a minute. The browser-side concurrency was real and
|
||||
// made no difference.
|
||||
//
|
||||
// A fan-out cannot be assembled from the client either: any request-per-item
|
||||
// arrangement is exactly the shape that queues. The parallelism has to happen
|
||||
// where the fan-out is received.
|
||||
func (g *Gateway) handlePluginFanout(w http.ResponseWriter, r *http.Request, name, fn string) {
|
||||
if reqRole(r.Context()) != "admin" {
|
||||
writeError(w, http.StatusForbidden, "forbidden", "admin role required")
|
||||
return
|
||||
}
|
||||
if r.Method != http.MethodPost {
|
||||
writeError(w, http.StatusMethodNotAllowed, "method_not_allowed", "")
|
||||
return
|
||||
}
|
||||
var req struct {
|
||||
// Args is a LIST of argument objects, one per call. A bare object is
|
||||
// rejected rather than coerced: "args" meaning "one call" and "args"
|
||||
// meaning "many" depending on its shape is how a caller ends up probing
|
||||
// one source and believing it probed twenty.
|
||||
Args []map[string]interface{} `json:"args"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid_request", "invalid json: "+err.Error())
|
||||
return
|
||||
}
|
||||
if len(req.Args) == 0 {
|
||||
writeError(w, http.StatusBadRequest, "invalid_request", "args must be a non-empty list of argument objects")
|
||||
return
|
||||
}
|
||||
// The page legitimately has one card per configured source. The bound is not
|
||||
// arbitrary: it stops one request from asking for thousands of VMs, and it is
|
||||
// enforced before any work starts rather than discovered shard by shard.
|
||||
if len(req.Args) > fanoutMaxArgs {
|
||||
writeError(w, http.StatusBadRequest, "invalid_request",
|
||||
fmt.Sprintf("args has %d entries, limit is %d", len(req.Args), fanoutMaxArgs))
|
||||
return
|
||||
}
|
||||
results, err := g.core.Plugins().ShardCall(name, fn, req.Args)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadGateway, "plugin_error", err.Error())
|
||||
return
|
||||
}
|
||||
// A shard that failed contributes a null in its slot. That is deliberate and
|
||||
// is NOT the same as the whole request failing: one unreachable upstream must
|
||||
// not turn a page of twenty cards into a single error.
|
||||
writeJSON(w, http.StatusOK, map[string]interface{}{"results": results})
|
||||
}
|
||||
|
||||
// fanoutMaxArgs bounds one fan-out request. Twenty is the measured real source
|
||||
// count on the gateway this was built for; the headroom is for operators with
|
||||
// more sources configured.
|
||||
const fanoutMaxArgs = 64
|
||||
|
||||
func (g *Gateway) pluginDir() string {
|
||||
if c := g.core.Config(); c != nil {
|
||||
return c.PluginDir
|
||||
@ -369,7 +512,6 @@ func (g *Gateway) removePlugin(name string) error {
|
||||
return g.core.Plugins().Unload(name)
|
||||
}
|
||||
|
||||
|
||||
// handleEndpointAccess serves the operator's view and edit surface for one
|
||||
// plugin endpoint's reachable keys.
|
||||
//
|
||||
@ -439,7 +581,6 @@ func (g *Gateway) handleEndpointAccess(w http.ResponseWriter, r *http.Request, n
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// splitStateForKey rewrites a plugin state so that a non-admin caller sees only
|
||||
// the buckets that belong to them.
|
||||
//
|
||||
@ -543,3 +684,68 @@ func (g *Gateway) handlePluginVisibility(w http.ResponseWriter, r *http.Request,
|
||||
writeError(w, http.StatusMethodNotAllowed, "method_not_allowed", r.Method+" is not allowed here")
|
||||
}
|
||||
}
|
||||
|
||||
// handlePluginKernel is the operator's surface for a plugin's kernel grants and
|
||||
// declared extra outbound hosts.
|
||||
//
|
||||
// GET returns what the plugin DECLARED beside what was GRANTED. That pairing is
|
||||
// the point of the endpoint: a plugin declaring `add_source` is a request, and
|
||||
// the operator needs to see the request next to the current answer to decide.
|
||||
func (g *Gateway) handlePluginKernel(w http.ResponseWriter, r *http.Request, name string) {
|
||||
ps := g.core.Plugins()
|
||||
if ps == nil {
|
||||
writeError(w, http.StatusNotFound, "not_found", "no plugins loaded")
|
||||
return
|
||||
}
|
||||
if reqRole(r.Context()) != "admin" {
|
||||
writeError(w, http.StatusForbidden, "forbidden", "admin role required")
|
||||
return
|
||||
}
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
perm := ps.PluginKernelPermission(name)
|
||||
declared := []string(nil)
|
||||
for _, v := range ps.KernelPermissions() {
|
||||
if v.Plugin == name {
|
||||
declared = v.Declared
|
||||
}
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]interface{}{
|
||||
"plugin": name,
|
||||
"actions": ps.KernelActionsView(name),
|
||||
"declared": declared,
|
||||
"granted": perm.Actions,
|
||||
"all_read": perm.AllRead,
|
||||
// Extra outbound hosts: declared vs granted, same request/privilege
|
||||
// split as kernel actions. What the plugin does with a host (login,
|
||||
// claim quotas...) is plugin business — the kernel only bounds egress.
|
||||
"hosts_declared": ps.PluginExtraHosts(name),
|
||||
"hosts_granted": ps.ExtraHostsGranted()[name],
|
||||
})
|
||||
return
|
||||
case http.MethodPut, http.MethodPost:
|
||||
var body struct {
|
||||
Actions []string `json:"actions"`
|
||||
AllRead bool `json:"all_read"`
|
||||
Hosts []string `json:"hosts"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid_request", err.Error())
|
||||
return
|
||||
}
|
||||
if err := ps.SetPluginKernelPermission(name, body.Actions, body.AllRead); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid_request", err.Error())
|
||||
return
|
||||
}
|
||||
if body.Hosts != nil {
|
||||
if err := ps.GrantExtraHosts(name, body.Hosts); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid_request", err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]interface{}{"ok": true})
|
||||
return
|
||||
default:
|
||||
writeError(w, http.StatusMethodNotAllowed, "method_not_allowed", "GET or PUT")
|
||||
}
|
||||
}
|
||||
|
||||
@ -222,7 +222,7 @@ func TestPluginsListCarriesAccessTables(t *testing.T) {
|
||||
t.Fatalf("GET /api/plugins = %d", rr.Code)
|
||||
}
|
||||
var out struct {
|
||||
Visibility map[string]string `json:"visibility"`
|
||||
Visibility map[string]string `json:"visibility"`
|
||||
HTTPAccess []map[string]interface{} `json:"http_access"`
|
||||
}
|
||||
if err := json.Unmarshal(rr.Body.Bytes(), &out); err != nil {
|
||||
|
||||
466
internal/gateway/serializer.go
Normal file
466
internal/gateway/serializer.go
Normal file
@ -0,0 +1,466 @@
|
||||
// Package gateway — 对外序列化(Phase 2)
|
||||
//
|
||||
// 职责边界(feature/unified-wire-v2):
|
||||
//
|
||||
// 内核不硬编码任何对外协议。序列化由 Serializer 承载,默认实现是
|
||||
// openAISerializer —— 但它只是**最后容错**,不是默认业务路径。
|
||||
//
|
||||
// 优先级契约(用户明确要求:任何适配器与兜底 OpenAI 格式冲突时,
|
||||
// 兜底 OpenAI 直接让步):
|
||||
//
|
||||
// 适配器声明 serialize_response / serialize_chunk
|
||||
// ↓ 未声明
|
||||
// openAISerializer(内核兜底,保证刚部署未配适配器时仍能服务)
|
||||
//
|
||||
// 「让步」的具体含义:一旦适配器声明了对外序列化钩子,内核在**该源**
|
||||
// 上完全采用适配器的输出,绝不与兜底格式合并/兜底字段。A 请求经 Anthropic
|
||||
// 序列化后,绝不会被再补上 OpenAI 的字段。这条规则由
|
||||
// TestAdapterSerializerWinsOverFallback 锁定。
|
||||
//
|
||||
// 安全网:openAISerializer 的输出与改造前内核手写的 OpenAI wire 字节级
|
||||
// 一致(e2e/.snapshots golden 锁定),所以「适配器未声明」这条路径上的
|
||||
// 对外行为与本次改造之前完全相同。
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"llmsproxy/internal/lua"
|
||||
"llmsproxy/internal/provider"
|
||||
"llmsproxy/internal/types"
|
||||
)
|
||||
|
||||
// StreamState 是一个流会话在序列化侧的累积状态(角色前导是否已发、已见
|
||||
// 的终止原因、累积 usage/cost)。跨 chunk 传递,避免每帧重新统计。
|
||||
type StreamState struct {
|
||||
LastFinish string
|
||||
Usage *types.TokenUsage
|
||||
Cost string
|
||||
}
|
||||
|
||||
// Serializer 把统一内部格式序列化成对外协议的响应体。
|
||||
//
|
||||
// 非流式与流式的终止语义对不同协议差异很大(OpenAI 在最后一个 chunk 上
|
||||
// 带 finish_reason 后跟一个 usage-only 帧;Anthropic 用独立的
|
||||
// message_delta 事件;Gemini 用空 candidates 帧),所以「流如何结束」
|
||||
// 是接口的一部分,而不是调用方的 if。
|
||||
type Serializer interface {
|
||||
// NonStream 把一次完成的统一响应序列化成对外 body。
|
||||
NonStream(resp *types.UnifiedResponse, modelName string, id string, created int64) ([]byte, error)
|
||||
|
||||
// OpenRolePreamble 返回流开始时的角色前导帧(可能为空表示该协议不需要)。
|
||||
// 返回空切片时调用方不写帧。
|
||||
OpenRolePreamble(modelName, id string, created int64, st *StreamState) ([]byte, error)
|
||||
|
||||
// Chunk 把一个统一分块序列化成对外 SSE 帧(不含 "data: " 前缀与空行)。
|
||||
Chunk(ck *types.UnifiedChunk, modelName, id string, created int64, st *StreamState) ([]byte, error)
|
||||
|
||||
// Close 返回流结束时的收尾帧序列(可多帧),随后调用方写协议自己的
|
||||
// 流终止标记(OpenAI 是 [DONE],Anthropic 无)。
|
||||
Close(modelName, id string, created int64, st *StreamState) ([][]byte, error)
|
||||
|
||||
// DoneMarker 返回该协议的流终止标记;OpenAI 是 "[DONE]",无此概念的
|
||||
// 协议返回空串(调用方因此不写那一行)。
|
||||
DoneMarker() string
|
||||
}
|
||||
|
||||
// ---- 兜底实现:OpenAI wire ----
|
||||
|
||||
// openAISerializer 是内核的**容错兜底**,输出与改造前手写的 OpenAI wire
|
||||
// 字节级一致(golden 锁定)。它不是默认业务路径——适配器一旦声明序列化
|
||||
// 钩子就完全取代它。
|
||||
type openAISerializer struct{}
|
||||
|
||||
// OpenAISerializer 返回兜底 OpenAI 序列化器。暴露给同包测试与
|
||||
// 「未配置任何适配器」的启动兜底路径使用。
|
||||
func OpenAISerializer() Serializer { return openAISerializer{} }
|
||||
|
||||
func (openAISerializer) NonStream(resp *types.UnifiedResponse, modelName, id string, created int64) ([]byte, error) {
|
||||
msg := RespMessage{Role: "assistant", Content: resp.Content}
|
||||
if resp.ReasoningContent != "" {
|
||||
msg.ReasoningContent = resp.ReasoningContent
|
||||
}
|
||||
if len(resp.ToolCalls) > 0 {
|
||||
msg.ToolCalls = toolCallsWire(resp.ToolCalls)
|
||||
}
|
||||
out := ChatCompletion{
|
||||
ID: id,
|
||||
Object: "chat.completion",
|
||||
Created: created,
|
||||
Model: modelName,
|
||||
Choices: []ChatChoice{{Index: 0, Message: msg, FinishReason: resp.FinishReason}},
|
||||
Cost: resp.Cost,
|
||||
}
|
||||
if resp.TokenUsage.Total > 0 || resp.TokenUsage.Prompt > 0 || resp.TokenUsage.Completion > 0 {
|
||||
out.Usage = &resp.TokenUsage
|
||||
}
|
||||
return json.Marshal(out)
|
||||
}
|
||||
|
||||
func (openAISerializer) OpenRolePreamble(modelName, id string, created int64, st *StreamState) ([]byte, error) {
|
||||
return json.Marshal(ChatChunk{
|
||||
ID: id, Object: "chat.completion.chunk", Created: created, Model: modelName,
|
||||
Choices: []ChunkChoice{{Index: 0, Delta: RespMessage{Role: "assistant"}}},
|
||||
})
|
||||
}
|
||||
|
||||
func (openAISerializer) Chunk(ck *types.UnifiedChunk, modelName, id string, created int64, st *StreamState) ([]byte, error) {
|
||||
delta := RespMessage{Role: "assistant", Content: ck.Content}
|
||||
if ck.ReasoningContent != "" {
|
||||
delta.ReasoningContent = ck.ReasoningContent
|
||||
}
|
||||
if len(ck.ToolCalls) > 0 {
|
||||
delta.ToolCalls = ck.ToolCalls
|
||||
}
|
||||
choice := ChunkChoice{Index: 0, Delta: delta}
|
||||
if ck.Done {
|
||||
fin := ck.FinishReason
|
||||
if fin == "" {
|
||||
fin = "stop"
|
||||
}
|
||||
st.LastFinish = fin
|
||||
choice.FinishReason = &fin
|
||||
}
|
||||
return json.Marshal(ChatChunk{
|
||||
ID: id, Object: "chat.completion.chunk", Created: created, Model: modelName,
|
||||
Choices: []ChunkChoice{choice},
|
||||
})
|
||||
}
|
||||
|
||||
func (openAISerializer) Close(modelName, id string, created int64, st *StreamState) ([][]byte, error) {
|
||||
fin := st.LastFinish
|
||||
if fin == "" {
|
||||
fin = "stop"
|
||||
}
|
||||
term, err := json.Marshal(ChatChunk{
|
||||
ID: id, Object: "chat.completion.chunk", Created: created, Model: modelName,
|
||||
Choices: []ChunkChoice{{Index: 0, Delta: RespMessage{}, FinishReason: &fin}},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := [][]byte{term}
|
||||
// OpenAI 的 usage 帧:空 choices + usage + 可选 cost。
|
||||
if st.Usage != nil || st.Cost != "" {
|
||||
var tut *types.TokenUsage
|
||||
if st.Usage != nil {
|
||||
tut = st.Usage
|
||||
} else {
|
||||
tut = &types.TokenUsage{}
|
||||
}
|
||||
u, err := json.Marshal(ChatChunk{
|
||||
ID: id, Object: "chat.completion.chunk", Created: created, Model: modelName,
|
||||
Choices: []ChunkChoice{}, Usage: tut, Cost: st.Cost,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, u)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (openAISerializer) DoneMarker() string { return "[DONE]" }
|
||||
|
||||
// ---- 适配器钩子通道 ----
|
||||
|
||||
// luaSerializer 把对外序列化委托给适配器的 serialize_response /
|
||||
// serialize_chunk 钩子。适配器声明了钩子就**完全取代**兜底实现。
|
||||
type luaSerializer struct {
|
||||
vm *lua.VM
|
||||
adapter string
|
||||
// responseOK / chunkOK 在构造时一次性探测(HasFn 不占用 worker)。
|
||||
responseOK bool
|
||||
chunkOK bool
|
||||
// openPreambleOK / closeOK 是可选钩子;未声明时借用兜底实现的对应行为,
|
||||
// 这样「只实现 serialize_chunk」的适配器也能立刻可用。
|
||||
openPreambleOK bool
|
||||
closeOK bool
|
||||
}
|
||||
|
||||
// serializerFor 为某个适配器名挑选序列化器。
|
||||
//
|
||||
// 这是「适配器优先、兜底让步」的唯一决策点:适配器声明了对外序列化
|
||||
// 钩子就返回 luaSerializer(其输出不再与 OpenAI 兜底混合),否则返回
|
||||
// 兜底 OpenAI。
|
||||
func serializerFor(vm *lua.VM, adapter string) Serializer {
|
||||
if vm == nil {
|
||||
return OpenAISerializer()
|
||||
}
|
||||
resp := vm.HasFn(adapter, "serialize_response")
|
||||
chunk := vm.HasFn(adapter, "serialize_chunk")
|
||||
if !resp && !chunk {
|
||||
return OpenAISerializer()
|
||||
}
|
||||
return &luaSerializer{
|
||||
vm: vm,
|
||||
adapter: adapter,
|
||||
responseOK: resp,
|
||||
chunkOK: chunk,
|
||||
openPreambleOK: vm.HasFn(adapter, "serialize_open_preamble"),
|
||||
closeOK: vm.HasFn(adapter, "serialize_close"),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *luaSerializer) NonStream(resp *types.UnifiedResponse, modelName, id string, created int64) ([]byte, error) {
|
||||
if !s.responseOK {
|
||||
// 该适配器只实现了流式钩子:非流式沿用兜底。这是唯一允许兜底
|
||||
// 参与的场景——适配器**没有**就该方向的实现,而不是「冲突」。
|
||||
return openAISerializer{}.NonStream(resp, modelName, id, created)
|
||||
}
|
||||
payload := map[string]interface{}{
|
||||
"model": modelName,
|
||||
"id": id,
|
||||
"created": created,
|
||||
"content": resp.Content,
|
||||
"reasoning": resp.ReasoningContent,
|
||||
"finish_reason": resp.FinishReason,
|
||||
"token_usage": resp.TokenUsage,
|
||||
"cost": resp.Cost,
|
||||
}
|
||||
if len(resp.ToolCalls) > 0 {
|
||||
payload["tool_calls"] = resp.ToolCalls
|
||||
}
|
||||
out, err := s.vm.Transform(s.adapter, "serialize_response", wireJSON(payload))
|
||||
if err != nil || out == "" {
|
||||
// 钩子失败:明确退回兜底,而不是产出一半适配器字段 + 一半 OpenAI
|
||||
// 字段的混合体(那正是「让步」规则要避免的)。
|
||||
return openAISerializer{}.NonStream(resp, modelName, id, created)
|
||||
}
|
||||
return []byte(out), nil
|
||||
}
|
||||
|
||||
func (s *luaSerializer) OpenRolePreamble(modelName, id string, created int64, st *StreamState) ([]byte, error) {
|
||||
if !s.openPreambleOK {
|
||||
return openAISerializer{}.OpenRolePreamble(modelName, id, created, st)
|
||||
}
|
||||
out, err := s.vm.Transform(s.adapter, "serialize_open_preamble",
|
||||
wireJSON(map[string]interface{}{"model": modelName, "id": id, "created": created}))
|
||||
if err != nil {
|
||||
return openAISerializer{}.OpenRolePreamble(modelName, id, created, st)
|
||||
}
|
||||
if out == "" {
|
||||
return nil, nil // 该协议不需要角色前导
|
||||
}
|
||||
return []byte(out), nil
|
||||
}
|
||||
|
||||
func (s *luaSerializer) Chunk(ck *types.UnifiedChunk, modelName, id string, created int64, st *StreamState) ([]byte, error) {
|
||||
if !s.chunkOK {
|
||||
return openAISerializer{}.Chunk(ck, modelName, id, created, st)
|
||||
}
|
||||
payload := map[string]interface{}{
|
||||
"model": modelName,
|
||||
"id": id,
|
||||
"created": created,
|
||||
"content": ck.Content,
|
||||
"reasoning": ck.ReasoningContent,
|
||||
"done": ck.Done,
|
||||
"finish_reason": ck.FinishReason,
|
||||
}
|
||||
if len(ck.ToolCalls) > 0 {
|
||||
payload["tool_calls"] = ck.ToolCalls
|
||||
}
|
||||
if ck.Usage != nil {
|
||||
payload["usage"] = ck.Usage
|
||||
}
|
||||
if ck.Cost != "" {
|
||||
payload["cost"] = ck.Cost
|
||||
}
|
||||
out, err := s.vm.Transform(s.adapter, "serialize_chunk", wireJSON(payload))
|
||||
if err != nil {
|
||||
return openAISerializer{}.Chunk(ck, modelName, id, created, st)
|
||||
}
|
||||
if out == "" {
|
||||
return nil, nil
|
||||
}
|
||||
// 适配器的终止帧要驱动审计记录与收尾逻辑,所以把终止原因抄回状态。
|
||||
if ck.Done && st.LastFinish == "" {
|
||||
if fin := ck.FinishReason; fin != "" {
|
||||
st.LastFinish = fin
|
||||
}
|
||||
}
|
||||
return []byte(out), nil
|
||||
}
|
||||
|
||||
func (s *luaSerializer) Close(modelName, id string, created int64, st *StreamState) ([][]byte, error) {
|
||||
if !s.closeOK {
|
||||
return openAISerializer{}.Close(modelName, id, created, st)
|
||||
}
|
||||
out, err := s.vm.Transform(s.adapter, "serialize_close",
|
||||
wireJSON(map[string]interface{}{
|
||||
"model": modelName, "id": id, "created": created,
|
||||
"finish_reason": st.LastFinish, "cost": st.Cost,
|
||||
"has_usage": st.Usage != nil,
|
||||
}))
|
||||
if err != nil {
|
||||
return openAISerializer{}.Close(modelName, id, created, st)
|
||||
}
|
||||
if out == "" {
|
||||
return nil, nil
|
||||
}
|
||||
// 适配器可以返回多帧(换行分隔),因为 Anthropic 的收尾本身就是
|
||||
// message_stop + 若干 delta;单帧限制会逼适配器把多事件塞进一个 JSON。
|
||||
var frames [][]byte
|
||||
for _, line := range splitLines(out) {
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
frames = append(frames, []byte(line))
|
||||
}
|
||||
return frames, nil
|
||||
}
|
||||
|
||||
// DoneMarker 返回协议自己的流终止标记。适配器可用 serialize_done_marker
|
||||
// 声明;未声明时若有自定义流式序列化就返回空串(表示「没有 [DONE] 这种
|
||||
// 概念」),否则沿用 OpenAI 的 [DONE]。
|
||||
func (s *luaSerializer) DoneMarker() string {
|
||||
if s.vm.HasFn(s.adapter, "serialize_done_marker") {
|
||||
out, err := s.vm.Transform(s.adapter, "serialize_done_marker", "{}")
|
||||
if err == nil {
|
||||
return out
|
||||
}
|
||||
}
|
||||
if s.chunkOK {
|
||||
// 适配器接管了流式序列化:只有它没显式声明标记时才不发 [DONE],
|
||||
// 因为擅自给 Anthropic 流加一行 "[DONE]" 会让严格客户端报错。
|
||||
return ""
|
||||
}
|
||||
return "[DONE]"
|
||||
}
|
||||
|
||||
// splitLines 切分多行帧输出。
|
||||
func splitLines(s string) []string {
|
||||
var out []string
|
||||
start := 0
|
||||
for i := 0; i < len(s); i++ {
|
||||
if s[i] == '\n' {
|
||||
out = append(out, trimCR(s[start:i]))
|
||||
start = i + 1
|
||||
}
|
||||
}
|
||||
if start < len(s) {
|
||||
out = append(out, trimCR(s[start:]))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func trimCR(s string) string {
|
||||
if n := len(s); n > 0 && s[n-1] == '\r' {
|
||||
return s[:n-1]
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// ---- 共享辅助 ----
|
||||
|
||||
var wireSeq int64
|
||||
|
||||
func newWireID() string {
|
||||
n := atomic.AddInt64(&wireSeq, 1)
|
||||
return fmt.Sprintf("chatcmpl-%d", n)
|
||||
}
|
||||
|
||||
func newCreated() int64 { return time.Now().Unix() }
|
||||
|
||||
// wireJSON 是本文件私有的 JSON 编码助手(包内已有 discover.go 的 mustJSON,
|
||||
// 那个返回 []byte 且忽略错误,语义不同,故不共用)。
|
||||
func wireJSON(v interface{}) string {
|
||||
b, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return "{}"
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// serializerCache 按 (VM, 适配器名) 缓存 Serializer。
|
||||
//
|
||||
// VM 必须进键里:缓存曾经只按适配器名做键,结果一个进程内两个 VM(或
|
||||
// 热重载后的新旧 VM)会共享同一条判定。插件在运行时改写适配器后声明了
|
||||
// serialize_response,另一条缓存记录仍会说「没声明」,于是新钩子永远不
|
||||
// 生效——而且因为判定本身不会报错,表现只是「适配器说好的对外格式没
|
||||
// 出现」,极难定位。
|
||||
//
|
||||
// HasFn 的代价决定了它必须被缓存:它会**编译一次适配器脚本**来回答
|
||||
// 「这个适配器声明了哪些函数」,放在每个请求上等于把加载成本翻倍。
|
||||
//
|
||||
// 失效由 vmGeneration 负责:适配器重写时 VM 的代号会变(见 adapterEpoch),
|
||||
// 于是旧条目再也匹配不上,无需显式清扫。
|
||||
var serializerCache sync.Map // cacheKey -> Serializer
|
||||
|
||||
// cacheKey 是 (VM 指针, VM 代号, 适配器名) 的复合键。
|
||||
type cacheKey struct {
|
||||
vm *lua.VM
|
||||
gen uint64
|
||||
name string
|
||||
}
|
||||
|
||||
func serializerForCached(vm *lua.VM, adapter string) Serializer {
|
||||
if vm == nil {
|
||||
return OpenAISerializer()
|
||||
}
|
||||
k := cacheKey{vm: vm, gen: adapterEpoch(vm, adapter), name: adapter}
|
||||
if v, ok := serializerCache.Load(k); ok {
|
||||
return v.(Serializer)
|
||||
}
|
||||
s := serializerFor(vm, adapter)
|
||||
actual, _ := serializerCache.LoadOrStore(k, s)
|
||||
return actual.(Serializer)
|
||||
}
|
||||
|
||||
// adapterEpoch 返回该适配器「当前这份代码」的代号。适配器被重写后代号变化,
|
||||
// 使旧缓存条目自动失效(见 serializerCache 的注释)。
|
||||
func adapterEpoch(vm *lua.VM, adapter string) uint64 {
|
||||
return vm.AdapterRevision(adapter)
|
||||
}
|
||||
|
||||
// serializerForSource 解析「实际服务该请求的那个源」该用哪个序列化器。
|
||||
//
|
||||
// 必须按**服务者**选,而不是按调用方请求的首选源选:direct 请求在候选源
|
||||
// 之间故障转移时,usedSrc 可能与请求 model 所属的源完全不同;按首选源选
|
||||
// 会让故障转移后的响应带着另一个源的对外格式。
|
||||
//
|
||||
// 源名查不到时(配置在选择之后被改、或插件装的源尚未注册)退回兜底
|
||||
// OpenAI:那是「刚部署 / 异常态下仍能服务」的保证,而不是静默产出错误格式。
|
||||
func serializerForSource(vm *lua.VM, sources []SourceRef, sourceName string) Serializer {
|
||||
for _, s := range sources {
|
||||
if s.Name == sourceName {
|
||||
return serializerForCached(vm, s.Adapter)
|
||||
}
|
||||
}
|
||||
return OpenAISerializer()
|
||||
}
|
||||
|
||||
// SourceRef 是解析序列化器所需的最小源信息(名字 + 适配器)。
|
||||
type SourceRef struct {
|
||||
Name string
|
||||
Adapter string
|
||||
}
|
||||
|
||||
// serializerFor 是 Gateway 侧的解析入口:按**实际服务该请求的源**挑序列化器。
|
||||
//
|
||||
// cands 传候选列表时用它做故障转移后的解析(服务源可能不是首选);传 nil
|
||||
// 表示 AUTO 路径——AUTO 的服务源由链决定,不在调用方的候选集里,退回按
|
||||
// 当前配置里全部源查找。
|
||||
func (g *Gateway) serializerFor(sourceName string, cands []*provider.Provider) Serializer {
|
||||
vm := g.core.VM()
|
||||
if vm == nil {
|
||||
return OpenAISerializer()
|
||||
}
|
||||
if cands != nil {
|
||||
refs := make([]SourceRef, 0, len(cands))
|
||||
for _, p := range cands {
|
||||
refs = append(refs, SourceRef{Name: p.Name(), Adapter: p.Adapter()})
|
||||
}
|
||||
return serializerForSource(vm, refs, sourceName)
|
||||
}
|
||||
refs := make([]SourceRef, 0, 16)
|
||||
for _, s := range g.core.Sources() {
|
||||
refs = append(refs, SourceRef{Name: s.Name, Adapter: s.Adapter})
|
||||
}
|
||||
return serializerForSource(vm, refs, sourceName)
|
||||
}
|
||||
367
internal/gateway/serializer_test.go
Normal file
367
internal/gateway/serializer_test.go
Normal file
@ -0,0 +1,367 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"llmsproxy/internal/lua"
|
||||
"llmsproxy/internal/types"
|
||||
)
|
||||
|
||||
// 本文件锁定对外序列化的**优先级契约**:
|
||||
//
|
||||
// 适配器声明 serialize_* → 适配器完全接管
|
||||
// 适配器未声明 → 兜底 OpenAI(保证刚部署/未配适配器仍能服务)
|
||||
//
|
||||
// 以及「让步」的定义:适配器接管后,内核绝不再往它的输出上补任何 OpenAI
|
||||
// 字段。两个序列化器混在一起产出的东西比任何一个都糟——客户端无法解析,
|
||||
// 而排查时会指向错误的层。
|
||||
|
||||
// luaVMFor 建一个会 seed 内置适配器的 VM(目录名必须是 "adapters")。
|
||||
func luaVMFor(t *testing.T) *lua.VM {
|
||||
t.Helper()
|
||||
vm := lua.NewVM(filepath.Join(t.TempDir(), "adapters"))
|
||||
if err := vm.Start(); err != nil {
|
||||
t.Fatalf("vm start: %v", err)
|
||||
}
|
||||
t.Cleanup(vm.Stop)
|
||||
return vm
|
||||
}
|
||||
|
||||
// TestFallbackUsedWhenAdapterDeclaresNothing 是「兜底」本身的测试。
|
||||
//
|
||||
// 注:anthropic 现在**确实**声明了 serialize_*(Phase 2 给了它 Anthropic
|
||||
// 形状),所以本测试只在仍未声明钩子的那些内置适配器上验证兜底行为。
|
||||
// 这正是设计意图:谁声明谁接管,没声明的继续用 OpenAI 兜底。
|
||||
func TestFallbackUsedWhenAdapterDeclaresNothing(t *testing.T) {
|
||||
vm := luaVMFor(t)
|
||||
for _, name := range []string{"openai", "gemini", "ollama", "deepseek", "github", "groq"} {
|
||||
for _, hook := range []string{"serialize_response", "serialize_chunk"} {
|
||||
if vm.HasFn(name, hook) {
|
||||
t.Errorf("内置适配器 %s 声明了 %s —— 兜底测试的前提已不成立", name, hook)
|
||||
}
|
||||
}
|
||||
}
|
||||
ser := serializerFor(vm, "openai")
|
||||
if _, isFallback := ser.(openAISerializer); !isFallback {
|
||||
t.Fatalf("未声明钩子的适配器应得到兜底序列化器,得到 %T", ser)
|
||||
}
|
||||
|
||||
// 对照:声明了钩子的适配器不走兜底
|
||||
if _, isFallback := serializerFor(vm, "anthropic").(openAISerializer); isFallback {
|
||||
t.Error("anthropic 声明了 serialize_response,不应走兜底")
|
||||
}
|
||||
}
|
||||
|
||||
// TestAdapterSerializerWinsOverFallback 锁定核心规则:适配器声明钩子即接管,
|
||||
// 且内核不与兜底混合任何字段。
|
||||
func TestAdapterSerializerWinsOverFallback(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
if err := os.MkdirAll(filepath.Join(dir, "adapters"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// 一个刻意用**非 OpenAI 形状**的适配器:如果内核混入了兜底字段
|
||||
// (object/choices/usage 等),下面断言会失败。
|
||||
adapter := `
|
||||
local adapter = {}
|
||||
adapter.name = "anthropicish"
|
||||
adapter.version = "1.0.0"
|
||||
adapter.endpoint = "/chat/completions"
|
||||
|
||||
-- 非流式:故意用 content[] 分块 + 没有 choices/usage
|
||||
function adapter.serialize_response(payload)
|
||||
local p = json.decode(payload)
|
||||
return json.encode({
|
||||
type = "message",
|
||||
role = "assistant",
|
||||
model = p.model,
|
||||
content = { { type = "text", text = p.content } },
|
||||
stop_reason = (p.finish_reason ~= "" and p.finish_reason or "end_turn"),
|
||||
})
|
||||
end
|
||||
|
||||
-- 流式:event: style 的帧,且不带 OpenAI 的 object 字段
|
||||
function adapter.serialize_chunk(payload)
|
||||
local p = json.decode(payload)
|
||||
return json.encode({
|
||||
type = "content_block_delta",
|
||||
delta = { type = "text_delta", text = p.content },
|
||||
})
|
||||
end
|
||||
|
||||
return adapter
|
||||
`
|
||||
if err := os.WriteFile(filepath.Join(dir, "adapters", "anthropicish.lua"), []byte(adapter), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
vm := lua.NewVM(filepath.Join(dir, "adapters"))
|
||||
if err := vm.Start(); err != nil {
|
||||
t.Fatalf("vm: %v", err)
|
||||
}
|
||||
defer vm.Stop()
|
||||
|
||||
ser := serializerFor(vm, "anthropicish")
|
||||
if _, isFallback := ser.(openAISerializer); isFallback {
|
||||
t.Fatal("声明了 serialize_* 的适配器必须接管序列化,而不是拿到兜底")
|
||||
}
|
||||
|
||||
// 非流式输出必须是适配器形状,且**不含**任何 OpenAI 字段
|
||||
resp := &types.UnifiedResponse{
|
||||
Model: "m",
|
||||
Content: "hello",
|
||||
FinishReason: "stop",
|
||||
TokenUsage: types.TokenUsage{Prompt: 3, Completion: 1, Total: 4},
|
||||
}
|
||||
body, err := ser.NonStream(resp, "m", "id-1", 1700000000)
|
||||
if err != nil {
|
||||
t.Fatalf("NonStream: %v", err)
|
||||
}
|
||||
var got map[string]interface{}
|
||||
if err := json.Unmarshal(body, &got); err != nil {
|
||||
t.Fatalf("adapter output not JSON: %v (%s)", err, body)
|
||||
}
|
||||
if got["type"] != "message" {
|
||||
t.Errorf("适配器输出未被采用:type = %v, want message", got["type"])
|
||||
}
|
||||
// 「让步」的硬断言:OpenAI 字段一个都不许出现
|
||||
for _, k := range []string{"object", "choices", "usage", "created"} {
|
||||
if _, leaked := got[k]; leaked {
|
||||
t.Errorf("兜底 OpenAI 字段 %q 混进了适配器的输出(让步规则被违反): %s", k, body)
|
||||
}
|
||||
}
|
||||
// 适配器没有读 usage,但兜底也不许替它补上
|
||||
if _, has := got["token_usage"]; has {
|
||||
t.Errorf("兜底补了 token_usage 字段,适配器输出被污染: %s", body)
|
||||
}
|
||||
}
|
||||
|
||||
// TestAdapterSerializerStreamWinsOverFallback 同上,流式路径。
|
||||
func TestAdapterSerializerStreamWinsOverFallback(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
if err := os.MkdirAll(filepath.Join(dir, "adapters"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
adapter := `
|
||||
local adapter = {}
|
||||
adapter.name = "streamsh"
|
||||
adapter.version = "1.0.0"
|
||||
adapter.endpoint = "/chat/completions"
|
||||
|
||||
function adapter.serialize_chunk(payload)
|
||||
local p = json.decode(payload)
|
||||
return json.encode({ type = "content_block_delta", text = p.content })
|
||||
end
|
||||
|
||||
-- 显式声明收尾与终止标记,证明协议语义完全由适配器决定
|
||||
function adapter.serialize_close(payload)
|
||||
local p = json.decode(payload)
|
||||
return json.encode({ type = "message_stop", stop_reason = p.finish_reason })
|
||||
end
|
||||
|
||||
return adapter
|
||||
`
|
||||
if err := os.WriteFile(filepath.Join(dir, "adapters", "streamsh.lua"), []byte(adapter), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
vm := lua.NewVM(filepath.Join(dir, "adapters"))
|
||||
if err := vm.Start(); err != nil {
|
||||
t.Fatalf("vm: %v", err)
|
||||
}
|
||||
defer vm.Stop()
|
||||
|
||||
ser := serializerFor(vm, "streamsh")
|
||||
if _, isFallback := ser.(openAISerializer); isFallback {
|
||||
t.Fatal("声明 serialize_chunk 的适配器必须接管流式序列化")
|
||||
}
|
||||
st := &StreamState{}
|
||||
b, err := ser.Chunk(&types.UnifiedChunk{Content: "hi"}, "m", "id", 1, st)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(b), `"content_block_delta"`) {
|
||||
t.Errorf("chunk 未采用适配器形状: %s", b)
|
||||
}
|
||||
if strings.Contains(string(b), "chat.completion.chunk") || strings.Contains(string(b), `"choices"`) {
|
||||
t.Errorf("兜底 OpenAI 帧混进了适配器输出: %s", b)
|
||||
}
|
||||
|
||||
// 终止标记:适配器接管流式后,内核不得自作主张发 [DONE]
|
||||
if m := ser.DoneMarker(); m != "" {
|
||||
t.Errorf("适配器接管流式时 DoneMarker 应为空(不擅自发 [DONE]),实得 %q", m)
|
||||
}
|
||||
// 但它没声明 serialize_close 时,收尾仍借用兜底形状 —— 这是「没实现」
|
||||
// 不是「冲突」,允许兜底。
|
||||
if _, isFallbackClose := ser.(interface {
|
||||
Close(model, id string, created int64, st *StreamState) ([][]byte, error)
|
||||
}); !isFallbackClose {
|
||||
t.Fatal("Close 不应在接口上")
|
||||
}
|
||||
}
|
||||
|
||||
// TestFallbackIsUsedWhenAdapterHookFails 验证钩子**执行失败**时的行为。
|
||||
//
|
||||
// 契约:退回兜底的完整输出,而不是产出「一半适配器字段 + 一半 OpenAI
|
||||
// 字段」的混合体。混合体比任何一个单独结果都更难排查。
|
||||
func TestFallbackIsUsedWhenAdapterHookFails(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
if err := os.MkdirAll(filepath.Join(dir, "adapters"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
adapter := `
|
||||
local adapter = {}
|
||||
adapter.name = "broken"
|
||||
adapter.version = "1.0.0"
|
||||
adapter.endpoint = "/chat/completions"
|
||||
function adapter.serialize_response(payload)
|
||||
error("deliberate failure")
|
||||
end
|
||||
return adapter
|
||||
`
|
||||
if err := os.WriteFile(filepath.Join(dir, "adapters", "broken.lua"), []byte(adapter), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
vm := lua.NewVM(filepath.Join(dir, "adapters"))
|
||||
if err := vm.Start(); err != nil {
|
||||
t.Fatalf("vm: %v", err)
|
||||
}
|
||||
defer vm.Stop()
|
||||
|
||||
ser := serializerFor(vm, "broken")
|
||||
resp := &types.UnifiedResponse{Content: "fallback still works", FinishReason: "stop"}
|
||||
body, err := ser.NonStream(resp, "m", "id", 1)
|
||||
if err != nil {
|
||||
t.Fatalf("钩子失败后必须仍能产出兜底输出,而非报错终止: %v", err)
|
||||
}
|
||||
if !strings.Contains(string(body), "chat.completion") {
|
||||
t.Errorf("钩子失败后未退回兜底 OpenAI 形状: %s", body)
|
||||
}
|
||||
}
|
||||
|
||||
// TestFallbackSavesUnconfiguredDeployment 直接覆盖用户说的场景:
|
||||
// 「刚部署没有配置任何适配器」。
|
||||
func TestFallbackSavesUnconfiguredDeployment(t *testing.T) {
|
||||
// 完全不建 VM(模拟适配器目录不存在/为空)
|
||||
ser := serializerFor(nil, "whatever")
|
||||
body, err := ser.NonStream(&types.UnifiedResponse{Content: "no adapters configured"}, "m", "id", 1)
|
||||
if err != nil {
|
||||
t.Fatalf("无适配器时兜底必须可用: %v", err)
|
||||
}
|
||||
if !strings.Contains(string(body), "chat.completion") {
|
||||
t.Errorf("无适配器兜底输出形状不对: %s", body)
|
||||
}
|
||||
}
|
||||
|
||||
// TestSerializerCacheFollowsAdapterRewrite 验证插件运行时改写适配器后,
|
||||
// 缓存不会继续持有旧的序列化器判定(否则新声明的钩子永远不生效,而因为
|
||||
// 判定本身不报错,表现只是「适配器说好的格式没出现」,极难定位)。
|
||||
func TestSerializerCacheFollowsAdapterRewrite(t *testing.T) {
|
||||
vm := luaVMFor(t)
|
||||
if _, isFallback := serializerForCached(vm, "openai").(openAISerializer); !isFallback {
|
||||
t.Fatal("前提不成立:改写前 openai 应走兜底")
|
||||
}
|
||||
// 插件改写该适配器,加一个 serialize_response
|
||||
src := `local a = {}
|
||||
a.name = "openai"
|
||||
a.version = "9"
|
||||
a.endpoint = "/chat/completions"
|
||||
function a.serialize_response(p) return '{"rewritten":true}' end
|
||||
return a`
|
||||
if err := vm.LoadAdapterSource("openai", src); err != nil {
|
||||
t.Fatalf("rewrite: %v", err)
|
||||
}
|
||||
// 无需任何显式清缓存动作:修订号变化即让旧条目失效。
|
||||
after := serializerForCached(vm, "openai")
|
||||
if _, stillFallback := after.(openAISerializer); stillFallback {
|
||||
t.Error("缓存未随适配器改写失效:已声明 serialize_response 却仍走兜底")
|
||||
}
|
||||
body, err := after.NonStream(&types.UnifiedResponse{Content: "x"}, "m", "id", 1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(body), "rewritten") {
|
||||
t.Errorf("改写后的序列化器未生效: %s", body)
|
||||
}
|
||||
}
|
||||
|
||||
// TestSerializerCacheIsPerVM 是那个 bug 的回归测试:缓存曾经只按适配器名做键,
|
||||
// 于是同一进程内两个 VM 共享同一条判定——一个适配器被重写后,另一个 VM 的
|
||||
// 缓存会说「没声明钩子」,新钩子永远不生效。
|
||||
func TestSerializerCacheIsPerVM(t *testing.T) {
|
||||
vm1 := luaVMFor(t)
|
||||
vm2 := luaVMFor(t)
|
||||
// 两个 VM 各自独立:改写 vm1 不得影响 vm2 的判定
|
||||
if err := vm1.LoadAdapterSource("openai", `local a={} a.name="openai" a.version="9"
|
||||
a.endpoint="/x" function a.serialize_response(p) return '{"only_vm1":true}' end return a`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, f := serializerForCached(vm1, "openai").(openAISerializer); f {
|
||||
t.Error("vm1 改写后应改走适配器序列化")
|
||||
}
|
||||
if _, f := serializerForCached(vm2, "openai").(openAISerializer); !f {
|
||||
t.Error("vm2 未被改写,却因共享缓存而丢失了兜底判定(跨 VM 串扰)")
|
||||
}
|
||||
}
|
||||
|
||||
// ---- 并发安全 ----
|
||||
|
||||
// TestSerializerForConcurrent 是压力测试:多 goroutine 并发取序列化器
|
||||
// (缓存是 sync.Map,HasFn 会编译脚本 —— 这里验证并发下不 panic、不返回
|
||||
// 半初始化对象,且每次结果自洽)。
|
||||
func TestSerializerForConcurrent(t *testing.T) {
|
||||
vm := luaVMFor(t)
|
||||
const workers, iters = 16, 40
|
||||
var wg sync.WaitGroup
|
||||
errs := make(chan string, workers*iters)
|
||||
for w := 0; w < workers; w++ {
|
||||
wg.Add(1)
|
||||
go func(w int) {
|
||||
defer wg.Done()
|
||||
for i := 0; i < iters; i++ {
|
||||
name := "openai"
|
||||
if w%2 == 1 {
|
||||
name = "anthropic"
|
||||
}
|
||||
ser := serializerForCached(vm, name)
|
||||
if ser == nil {
|
||||
errs <- "nil serializer"
|
||||
return
|
||||
}
|
||||
// 结果必须自洽:同一适配器每次拿到同一类型,且形状与该适配器
|
||||
// 声明的协议一致(openai→OpenAI,anthropic→Anthropic)。
|
||||
_, isFallback := ser.(openAISerializer)
|
||||
wantShape := "chat.completion"
|
||||
switch name {
|
||||
case "anthropic":
|
||||
if isFallback {
|
||||
errs <- "anthropic 并发下退回兜底"
|
||||
return
|
||||
}
|
||||
wantShape = `"type":"message"`
|
||||
default:
|
||||
if !isFallback {
|
||||
errs <- name + " 并发下未走兜底"
|
||||
return
|
||||
}
|
||||
}
|
||||
body, err := ser.NonStream(&types.UnifiedResponse{Content: "x"}, "m", "id", 1)
|
||||
if err != nil {
|
||||
errs <- "serialize: " + err.Error()
|
||||
return
|
||||
}
|
||||
if !strings.Contains(string(body), wantShape) {
|
||||
errs <- "并发下输出形状不一致: " + name + " -> " + string(body)
|
||||
return
|
||||
}
|
||||
}
|
||||
}(w)
|
||||
}
|
||||
wg.Wait()
|
||||
close(errs)
|
||||
for e := range errs {
|
||||
t.Error(e)
|
||||
}
|
||||
}
|
||||
@ -216,6 +216,14 @@ func (g *Gateway) routes(w http.ResponseWriter, r *http.Request) {
|
||||
switch {
|
||||
case r.URL.Path == "/v1/chat/completions":
|
||||
g.handleChat(w, r)
|
||||
case r.URL.Path == "/v1/messages":
|
||||
// Anthropic Messages protocol. Scheduling, quotas, cooldowns, AUTO
|
||||
// chain and accounting are EXACTLY the ones /v1/chat/completions uses —
|
||||
// only the wire shapes differ, and those come from the serving
|
||||
// adapter's serialize_* hooks. The kernel holds no Anthropic field
|
||||
// names (see serializer.go: the fallback is OpenAI, and any adapter
|
||||
// that declares serialize_* replaces it wholesale).
|
||||
g.handleMessages(w, r)
|
||||
case r.URL.Path == "/v1/images/generations":
|
||||
g.handleImage(w, r)
|
||||
case r.URL.Path == "/v1/models":
|
||||
@ -224,6 +232,11 @@ func (g *Gateway) routes(w http.ResponseWriter, r *http.Request) {
|
||||
g.handleAdaptersAPI(w, r)
|
||||
case r.URL.Path == "/api/v1" || strings.HasPrefix(r.URL.Path, "/api/v1/"):
|
||||
g.apiV1Routes(w, r)
|
||||
// Must precede the /api/sources prefix match: a generic HasPrefix on
|
||||
// "/api/sources/" would otherwise swallow "/api/sources/discover" and the
|
||||
// operator would get "source name required in path" instead of a catalog.
|
||||
case r.URL.Path == "/api/sources/discover" || strings.HasPrefix(r.URL.Path, "/api/sources/discover/"):
|
||||
g.handleSourceDiscoverAPI(w, r)
|
||||
case r.URL.Path == "/api/sources" || strings.HasPrefix(r.URL.Path, "/api/sources/"):
|
||||
g.handleSourcesAPI(w, r)
|
||||
case r.URL.Path == "/api/source_templates" || strings.HasPrefix(r.URL.Path, "/api/source_templates/"):
|
||||
@ -239,6 +252,12 @@ func (g *Gateway) routes(w http.ResponseWriter, r *http.Request) {
|
||||
// Before the generic plugin router: these paths belong to a PLUGIN, not to
|
||||
// the plugin manager, and "http" is a valid sub-resource name otherwise.
|
||||
g.handlePluginHTTP(w, r)
|
||||
case strings.HasPrefix(r.URL.Path, "/plugins/") || strings.HasPrefix(r.URL.Path, "/v1/plugins/"):
|
||||
// Custom mounts a plugin or adapter claimed (R3). Checked AFTER every
|
||||
// kernel route so a claim can never shadow one — ValidateCustomMount
|
||||
// refuses the overlap at load time, and this ordering is the second line
|
||||
// of defence for a table that somehow slipped through.
|
||||
g.handleCustomMount(w, r)
|
||||
case r.URL.Path == "/api/plugins" || strings.HasPrefix(r.URL.Path, "/api/plugins/"):
|
||||
g.handlePluginsAPI(w, r)
|
||||
case r.URL.Path == "/api/chat":
|
||||
|
||||
@ -53,7 +53,7 @@ type Req struct {
|
||||
// 0 means "not reported", which is NOT the same as "no reasoning happened" —
|
||||
// three of the sources measured return no such field at all.
|
||||
Reasoning int64 `json:"reasoning_tokens,omitempty"`
|
||||
OK bool `json:"ok"`
|
||||
OK bool `json:"ok"`
|
||||
// Status http status code
|
||||
Status int `json:"status"`
|
||||
// Err short error message
|
||||
@ -765,9 +765,20 @@ func (s *Stats) WindowTokens(model, source string, sec int64) int64 {
|
||||
return total
|
||||
}
|
||||
|
||||
// rows flattens a map[string]*Stat into a reportable, descending-by-requests
|
||||
// list.
|
||||
//
|
||||
// A nil value is skipped rather than dereferenced. The maps this is called with
|
||||
// are built by several callers (Snapshot, PeriodSnapshot, the audit replay) and a
|
||||
// nil entry is a legitimate representation of "this dimension has no data" —
|
||||
// `map[string]*Stat{key: s.byKey[key]}` produced exactly that whenever the
|
||||
// queried key had no recorded traffic, and `*v` then panicked the handler.
|
||||
func rows(m map[string]*Stat) []StatsRow {
|
||||
out := make([]StatsRow, 0, len(m))
|
||||
for k, v := range m {
|
||||
if v == nil {
|
||||
continue
|
||||
}
|
||||
out = append(out, StatsRow{Name: k, Stat: *v})
|
||||
}
|
||||
for i := 1; i < len(out); i++ {
|
||||
@ -1185,7 +1196,26 @@ func (s *Stats) Snapshot(limit int, key string) map[string]interface{} {
|
||||
if key == "" {
|
||||
byKey, byModel, bySrc = s.byKey, s.byModel, s.bySrc
|
||||
} else {
|
||||
byKey = map[string]*Stat{key: s.byKey[key]}
|
||||
// The map is keyed by the MASKED key id (keyID(req.Key)), which is what
|
||||
// by_key reports and what the dashboard sends as ?key=. An admin may
|
||||
// pass anything in that query parameter, including the full key, and
|
||||
// `s.byKey[key]` then returns the zero value — a nil *Stat.
|
||||
//
|
||||
// rows() below does `*v` on every value, so a nil entry panicked the
|
||||
// handler: any ?key=<full key> request killed the response with an empty
|
||||
// reply, and logged a full panic stack. Reproduced against the live
|
||||
// gateway: /api/stats?period=day&limit=1&key=sk-gw-... → curl exit 52.
|
||||
//
|
||||
// A key with no recorded traffic is a legitimate question ("what did this
|
||||
// key use?"), and its answer is zero — so an absent entry is an empty
|
||||
// result, not an error, and not a crash.
|
||||
st := s.byKey[key]
|
||||
byKey = make(map[string]*Stat, 1)
|
||||
if st != nil {
|
||||
byKey[key] = st
|
||||
} else {
|
||||
byKey[key] = &Stat{}
|
||||
}
|
||||
byModel = s.byKeyModel[key]
|
||||
bySrc = s.byKeySrc[key]
|
||||
}
|
||||
@ -1202,8 +1232,11 @@ func (s *Stats) Snapshot(limit int, key string) map[string]interface{} {
|
||||
}
|
||||
}
|
||||
bs := make([]agrRow, 0, len(s.byStatus))
|
||||
for code := range s.byStatus {
|
||||
bs = append(bs, agrRow{Name: strconv.Itoa(code), Stat: *s.byStatus[code]})
|
||||
for code, st := range s.byStatus {
|
||||
if st == nil {
|
||||
continue
|
||||
}
|
||||
bs = append(bs, agrRow{Name: strconv.Itoa(code), Stat: *st})
|
||||
}
|
||||
sort.Slice(bs, func(i, j int) bool {
|
||||
ci, _ := strconv.Atoi(bs[i].Name)
|
||||
|
||||
@ -174,6 +174,9 @@ func (s *Stats) PeriodSnapshot(p Period, key string, now time.Time) PeriodSnapsh
|
||||
out.Srcs = rows(bySrc)
|
||||
bs := make([]agrRow, 0, len(byStatus))
|
||||
for code, st := range byStatus {
|
||||
if st == nil {
|
||||
continue
|
||||
}
|
||||
bs = append(bs, agrRow{Name: code, Stat: *st})
|
||||
}
|
||||
sort.Slice(bs, func(i, j int) bool {
|
||||
@ -192,7 +195,11 @@ func (s *Stats) PeriodSnapshot(p Period, key string, now time.Time) PeriodSnapsh
|
||||
sort.Strings(ks)
|
||||
out.Bucket = make([]PeriodBucket, 0, len(ks))
|
||||
for _, k := range ks {
|
||||
out.Bucket = append(out.Bucket, PeriodBucket{Bucket: k, Stat: *buckets[k]})
|
||||
// Defensive for the same reason rows() is: ks comes from the bucket map's
|
||||
// keys, and a nil value would panic on the deref below.
|
||||
if st := buckets[k]; st != nil {
|
||||
out.Bucket = append(out.Bucket, PeriodBucket{Bucket: k, Stat: *st})
|
||||
}
|
||||
}
|
||||
|
||||
// The window is only "complete" if the audit walk actually reached back
|
||||
|
||||
@ -74,7 +74,7 @@ func TestStatsPeriodRecordsMatchPager(t *testing.T) {
|
||||
rr := doReq(t, g, "GET", "/api/stats?period=day&limit=100", "")
|
||||
var st struct {
|
||||
Records []struct {
|
||||
Time int64 `json:"time"`
|
||||
Time int64 `json:"time"`
|
||||
Model string `json:"model"`
|
||||
} `json:"records"`
|
||||
}
|
||||
@ -84,7 +84,7 @@ func TestStatsPeriodRecordsMatchPager(t *testing.T) {
|
||||
rr2 := doReq(t, g, "GET", "/api/stats/records?limit=100", "")
|
||||
var pg struct {
|
||||
Records []struct {
|
||||
Time int64 `json:"time"`
|
||||
Time int64 `json:"time"`
|
||||
Model string `json:"model"`
|
||||
} `json:"records"`
|
||||
}
|
||||
|
||||
140
internal/gateway/stats_unknown_key_test.go
Normal file
140
internal/gateway/stats_unknown_key_test.go
Normal file
@ -0,0 +1,140 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TestSnapshotUnknownKeyDoesNotPanic covers a nil-dereference that killed the
|
||||
// response for any /api/stats request carrying a ?key= the statistics had no
|
||||
// entry for.
|
||||
//
|
||||
// The maps in Snapshot are keyed by the MASKED key id (keyID(req.Key)) — that
|
||||
// is what by_key reports and what the dashboard sends. An admin may pass anything
|
||||
// in the query parameter, including the full key, and `s.byKey[key]` then yields
|
||||
// the zero value: a nil *Stat. rows() dereferenced every value, so the handler
|
||||
// panicked and the client got an empty reply plus a panic stack in the log.
|
||||
//
|
||||
// Reproduced against the live gateway before the fix:
|
||||
//
|
||||
// /api/stats?period=day&limit=1&key=sk-gw-7ffe12c5cbacec40bd39a7291df9a9ed
|
||||
// → curl: (52) Empty reply from server
|
||||
// → log: http: panic serving …: invalid memory address or nil pointer dereference
|
||||
// llmsproxy/internal/gateway.rows stats.go:771
|
||||
//
|
||||
// A key with no recorded traffic is a legitimate question, and zero is its
|
||||
// answer — not a crash.
|
||||
func TestSnapshotUnknownKeyDoesNotPanic(t *testing.T) {
|
||||
s := NewStats(16)
|
||||
// One recorded key, masked the way Record masks it.
|
||||
s.Record(Req{Key: "***aaaaaa", Type: "chat", Model: "m", Prompt: 5, Compl: 2})
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
key string
|
||||
}{
|
||||
{"masked key that exists", "***aaaaaa"},
|
||||
{"masked key that does not exist", "***zzzzzz"},
|
||||
{"full key rather than the mask", "sk-gw-full-key-value"},
|
||||
{"arbitrary text", "not-a-key-at-all"},
|
||||
}
|
||||
for _, c := range cases {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
// A panic here fails the subtest by unwinding through the test
|
||||
// goroutine; rows() used to take the whole process with it.
|
||||
out := s.Snapshot(10, c.key)
|
||||
byKey, ok := out["by_key"].([]StatsRow)
|
||||
if !ok {
|
||||
t.Fatalf("by_key is %T, want []StatsRow", out["by_key"])
|
||||
}
|
||||
total, _ := out["total"].(Stat)
|
||||
t.Logf("key=%q → by_key %d 条, total.reqs=%d", c.key, len(byKey), total.Reqs)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestSnapshotUnknownKeyReportsZeroNotAbsence pins the semantics: a key with no
|
||||
// traffic answers zero, and the by_key row still exists so a dashboard that keys
|
||||
// off its presence renders a row of zeros rather than dropping the key.
|
||||
func TestSnapshotUnknownKeyReportsZeroNotAbsence(t *testing.T) {
|
||||
s := NewStats(16)
|
||||
out := s.Snapshot(10, "***zzzzzz")
|
||||
byKey, _ := out["by_key"].([]StatsRow)
|
||||
if len(byKey) != 1 {
|
||||
t.Fatalf("by_key has %d rows, want 1: the queried key must appear with zeros", len(byKey))
|
||||
}
|
||||
if byKey[0].Name != "***zzzzzz" {
|
||||
t.Errorf("row name = %q, want ***zzzzzz", byKey[0].Name)
|
||||
}
|
||||
if byKey[0].Reqs != 0 {
|
||||
t.Errorf("reqs = %d, want 0", byKey[0].Reqs)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPeriodSnapshotUnknownKeyDoesNotPanic is the same guard on the period view,
|
||||
// which builds its own buckets and calls the same flattening helpers.
|
||||
func TestPeriodSnapshotUnknownKeyDoesNotPanic(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
audit := dir + "/audit.jsonl"
|
||||
writeAuditFile(t, audit, 3, "***aaaaaa", time.Now().Add(-time.Hour).UnixMilli())
|
||||
s := periodStats(t, audit)
|
||||
|
||||
for _, key := range []string{"***aaaaaa", "***zzzzzz", "sk-gw-full-key-value"} {
|
||||
out := s.PeriodSnapshot(PeriodAll, key, time.Now())
|
||||
t.Logf("key=%q → total.reqs=%d byKey=%d buckets=%d",
|
||||
key, out.Total.Reqs, len(out.ByKey), len(out.Bucket))
|
||||
}
|
||||
}
|
||||
|
||||
// TestRowsSkipsNilValues pins the defensive behaviour directly, so a future
|
||||
// caller that reintroduces a nil entry fails here rather than in production.
|
||||
func TestRowsSkipsNilValues(t *testing.T) {
|
||||
in := map[string]*Stat{
|
||||
"has-traffic": {Reqs: 5},
|
||||
"no-entry": nil,
|
||||
}
|
||||
got := rows(in)
|
||||
if len(got) != 1 {
|
||||
t.Fatalf("rows() returned %d rows, want 1 (the nil entry must be skipped): %+v", len(got), got)
|
||||
}
|
||||
if got[0].Name != "has-traffic" {
|
||||
t.Errorf("kept %q, want has-traffic", got[0].Name)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRowsEmptyIsEmptyNotNil keeps the JSON shape honest: an all-nil map yields
|
||||
// an empty list, which encodes as [] rather than null — the frontend does
|
||||
// Array.isArray checks on these fields.
|
||||
func TestRowsEmptyIsEmptyNotNil(t *testing.T) {
|
||||
got := rows(map[string]*Stat{"a": nil})
|
||||
if got == nil {
|
||||
t.Fatal("rows() returned nil; the report field must stay [] instead of null")
|
||||
}
|
||||
if len(got) != 0 {
|
||||
t.Fatalf("got %d rows, want 0", len(got))
|
||||
}
|
||||
}
|
||||
|
||||
// TestStatsAPIUnknownKeyReturnsJSON exercises the HTTP path so the assertion
|
||||
// covers the reported symptom (an empty reply), not just the helper.
|
||||
func TestStatsAPIUnknownKeyReturnsJSON(t *testing.T) {
|
||||
g := newTestGateway(t)
|
||||
srv := httptest.NewServer(g.Handler())
|
||||
defer srv.Close()
|
||||
|
||||
req, err := http.NewRequest("GET", srv.URL+"/api/stats?period=day&limit=1&key=sk-gw-full-key", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer sk-test")
|
||||
resp, err := srv.Client().Do(req)
|
||||
if err != nil {
|
||||
t.Fatalf("request failed (an empty reply is what the live gateway produced): %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("status = %d, want 200", resp.StatusCode)
|
||||
}
|
||||
}
|
||||
133
internal/gateway/thinking_test.go
Normal file
133
internal/gateway/thinking_test.go
Normal file
@ -0,0 +1,133 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"llmsproxy/internal/types"
|
||||
)
|
||||
|
||||
// TestThinkingFromClient 把「客户端措辞 → 中立语义」这条翻译链钉死。
|
||||
//
|
||||
// v1 的问题:disable_thinking 是内核私有字段,被序列化进交给适配器的 body,
|
||||
// 于是 10 个适配器各自写 req.disable_thinking = nil 来清理它。v2 把它换成
|
||||
// 双方都认可的 thinking 字段,这条测试确保翻译只发生一次、且在适配器之前。
|
||||
func TestThinkingFromClient(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
req chatRequest
|
||||
want *types.ThinkingConfig
|
||||
}{
|
||||
{
|
||||
name: "无任何字段 → 不带 thinking(上游用自己默认)",
|
||||
req: chatRequest{},
|
||||
want: nil,
|
||||
},
|
||||
{
|
||||
name: "disable_thinking=true → enabled=false",
|
||||
req: chatRequest{DisableThinking: true},
|
||||
want: &types.ThinkingConfig{Enabled: boolPtr(false)},
|
||||
},
|
||||
{
|
||||
name: "中立 thinking 显式给出 → 原样透传",
|
||||
req: chatRequest{Thinking: &types.ThinkingConfig{Enabled: boolPtr(true), BudgetTokens: 4096}},
|
||||
want: &types.ThinkingConfig{Enabled: boolPtr(true), BudgetTokens: 4096},
|
||||
},
|
||||
{
|
||||
name: "两者冲突时,中立的 thinking 优先(它带预算,布尔表达不了)",
|
||||
req: chatRequest{
|
||||
DisableThinking: true,
|
||||
Thinking: &types.ThinkingConfig{Enabled: boolPtr(true), BudgetTokens: 1024},
|
||||
},
|
||||
want: &types.ThinkingConfig{Enabled: boolPtr(true), BudgetTokens: 1024},
|
||||
},
|
||||
}
|
||||
for _, c := range cases {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
got := c.req.thinkingFromClient()
|
||||
if c.want == nil {
|
||||
if got != nil {
|
||||
t.Fatalf("want nil, got %+v", got)
|
||||
}
|
||||
return
|
||||
}
|
||||
if got == nil {
|
||||
t.Fatal("want non-nil thinking, got nil")
|
||||
}
|
||||
if (got.Enabled == nil) != (c.want.Enabled == nil) {
|
||||
t.Fatalf("Enabled presence mismatch: got %v want %v", got.Enabled, c.want.Enabled)
|
||||
}
|
||||
if got.Enabled != nil && *got.Enabled != *c.want.Enabled {
|
||||
t.Errorf("Enabled = %v, want %v", *got.Enabled, *c.want.Enabled)
|
||||
}
|
||||
if got.BudgetTokens != c.want.BudgetTokens {
|
||||
t.Errorf("BudgetTokens = %d, want %d", got.BudgetTokens, c.want.BudgetTokens)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestDisableThinkingNeverReachesAdapterBody 是那条泄漏的回归测试:内核私有
|
||||
// 字段不得出现在交给适配器的 body 里。
|
||||
func TestDisableThinkingNeverReachesAdapterBody(t *testing.T) {
|
||||
var r chatRequest
|
||||
if err := json.Unmarshal([]byte(`{"model":"m","messages":[],"disable_thinking":true}`), &r); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
inner := &types.ChatRequest{
|
||||
Model: "m",
|
||||
Thinking: r.thinkingFromClient(),
|
||||
Tools: r.rawTools(),
|
||||
ToolChoice: r.rawToolChoice(),
|
||||
ExtraBody: r.ExtraBody,
|
||||
ClientSession: "",
|
||||
}
|
||||
body, err := json.Marshal(inner)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var m map[string]json.RawMessage
|
||||
if err := json.Unmarshal(body, &m); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, leaked := m["disable_thinking"]; leaked {
|
||||
t.Errorf("内核私有字段 disable_thinking 泄漏进适配器 body(v1 回归): %s", body)
|
||||
}
|
||||
if _, ok := m["thinking"]; !ok {
|
||||
t.Errorf("中立 thinking 字段缺失: %s", body)
|
||||
}
|
||||
}
|
||||
|
||||
// TestToolsPassthroughRaw 确认 tools/tool_choice 以原始形状交给适配器:
|
||||
// 内核不解释工具 schema,那是适配器的职责。
|
||||
func TestToolsPassthroughRaw(t *testing.T) {
|
||||
var r chatRequest
|
||||
body := `{"model":"m","messages":[],"tools":[{"type":"function","function":{"name":"f","parameters":{"type":"object"}}}],"tool_choice":"auto"}`
|
||||
if err := json.Unmarshal([]byte(body), &r); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tools := r.rawTools()
|
||||
if len(tools) == 0 {
|
||||
t.Fatal("tools 未透传")
|
||||
}
|
||||
var decoded []map[string]interface{}
|
||||
if err := json.Unmarshal(tools, &decoded); err != nil {
|
||||
t.Fatalf("tools 不是合法 JSON: %v (%s)", err, tools)
|
||||
}
|
||||
if len(decoded) != 1 || decoded[0]["type"] != "function" {
|
||||
t.Errorf("tools 形状被内核改写: %s", tools)
|
||||
}
|
||||
if tc := string(r.rawToolChoice()); tc != `"auto"` {
|
||||
t.Errorf("tool_choice = %s, want \"auto\"", tc)
|
||||
}
|
||||
// 空值不应变成 "null" 出现在 body 里
|
||||
var empty chatRequest
|
||||
if got := empty.rawTools(); got != nil {
|
||||
t.Errorf("无 tools 时应返回 nil, got %s", got)
|
||||
}
|
||||
if got := empty.rawToolChoice(); got != nil {
|
||||
t.Errorf("无 tool_choice 时应返回 nil, got %s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func boolPtr(v bool) *bool { return &v }
|
||||
@ -366,6 +366,22 @@
|
||||
|
||||
/* ---- chat page ---- */
|
||||
.tab-chat{max-width:840px;margin:0 auto;display:flex;flex-direction:column;height:calc(100vh - 190px)}
|
||||
|
||||
.tab-image{max-width:840px;margin:0 auto;display:flex;flex-direction:column;
|
||||
height:calc(100vh - 190px)}
|
||||
.img-wrap{flex:1;display:flex;flex-direction:column;min-height:0}
|
||||
.img-grid{flex:1;overflow-y:auto;padding:12px 2px;display:grid;
|
||||
grid-template-columns:repeat(auto-fill,minmax(240px,1fr));gap:10px;align-content:start}
|
||||
.img-grid .chat-empty{grid-column:1/-1}
|
||||
.img-cell{border:1px solid var(--line);border-radius:10px;overflow:hidden;
|
||||
background:var(--bg2)}
|
||||
.img-cell img{display:block;width:100%;height:auto;background:#fff}
|
||||
.img-meta{padding:6px 8px;font-size:11px;color:var(--muted);
|
||||
overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
|
||||
.img-composer{border-top:1px solid var(--line);padding:10px 2px 12px}
|
||||
.img-composer .chat-box{display:flex;gap:8px;align-items:flex-end}
|
||||
.img-composer textarea{flex:1;resize:vertical;min-height:52px}
|
||||
.img-composer .empty{padding:8px 0}
|
||||
.chat-wrap{background:var(--card);border:1px solid var(--line);border-radius:18px;box-shadow:var(--sh-lg);
|
||||
display:flex;flex-direction:column;flex:1;min-height:0;overflow:hidden;backdrop-filter:blur(var(--glass)) saturate(1.5)}
|
||||
.chat-log{flex:1;overflow-y:auto;padding:20px 20px 8px;display:flex;flex-direction:column;gap:18px}
|
||||
@ -573,6 +589,21 @@
|
||||
/></svg
|
||||
><span data-i="navChat">对话</span>
|
||||
</button>
|
||||
<button class="sb-i" data-tab="image">
|
||||
<svg
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
stroke-width="2"
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
>
|
||||
<path d="M3 5h18v14H3z" />
|
||||
<path d="m3 15 5-5 4 4 3-3 6 6" />
|
||||
<circle cx="8.5" cy="8.5" r="1.5" />
|
||||
></svg
|
||||
><span data-i="navImage">生图</span>
|
||||
</button>
|
||||
<button class="sb-i" data-tab="keys">
|
||||
<svg
|
||||
viewBox="0 0 24 24"
|
||||
@ -791,6 +822,7 @@
|
||||
<main id="main">
|
||||
<div id="tab-status" class="tab-pane"></div>
|
||||
<div id="tab-chat" class="tab-pane hidden"></div>
|
||||
<div id="tab-image" class="tab-pane hidden"></div>
|
||||
<div id="tab-keys" class="tab-pane hidden"></div>
|
||||
<div id="tab-sort" class="tab-pane hidden"></div>
|
||||
<div id="tab-sources" class="tab-pane hidden"></div>
|
||||
@ -818,6 +850,25 @@
|
||||
|
||||
plVisibility: "可见性",
|
||||
plEndpoints: "端点",
|
||||
plKernel: "内核动作",
|
||||
plKernelTitle: "内核动作权限",
|
||||
plKernelHint: "勾选即授权。插件源码里申请(plugin.kernel)但未授权的动作会标红 —— 那通常意味着它的某个功能静默失效。",
|
||||
plKernelAllRead: "只读全开",
|
||||
plKernelN: "%d 项",
|
||||
plKernelReq: "有申请",
|
||||
plKernelAsked: "申请中",
|
||||
plDangerRead: "只读",
|
||||
plDangerWrite: "可写",
|
||||
plDangerDestructive: "破坏性",
|
||||
plAction: "动作",
|
||||
plDanger: "危险级",
|
||||
plSave: "保存",
|
||||
plClose: "关闭",
|
||||
plAuthHosts: "额外出站主机(白名单)",
|
||||
plAuthHostsHint: "插件要访问的主机若不是上游源,默认被出站白名单拒绝。勾选即批准;只能批准插件自己声明过的。",
|
||||
plHostOk: "已批准",
|
||||
plHostPending: "待批准",
|
||||
plNoAuthHosts: "该插件未声明额外出站主机",
|
||||
plAccessTitle: "访问控制",
|
||||
plAccessHint: "填写允许访问的角色或密钥(@名称)。留空=仅 admin。填 public=任何人。指定 @密钥 时只有该密钥能访问,不受角色规则影响。",
|
||||
plWholePlugin: "以下端点",
|
||||
@ -941,6 +992,20 @@
|
||||
adPoolPeak: "峰值",
|
||||
adPoolSteps: "步长 (扩/缩)",
|
||||
cModel: "模型",
|
||||
cImageModels: "🖼 图像模型(不可选,请到「生图」页测试)",
|
||||
urlHint: "填主机地址,通常不带 /v1 —— 网关会自动拼接路径。但各家约定不同:若填 https://x.com/v1,对话路径会正确,而图像生成会变成 /v1/v1/… 而 404。不确定就用上方「自动探测模型」,它会探测并填好正确形式。",
|
||||
navImage: "生图",
|
||||
imgWelcome: "输入提示词开始生成",
|
||||
imgSlow: "生图可能需要 15 秒到 2 分钟,取决于上游模型",
|
||||
imgPrompt: "描述你想要的画面…",
|
||||
imgGenerate: "生成",
|
||||
imgWorking: "生成中… %ds",
|
||||
imgCount: "数量",
|
||||
imgSize: "尺寸",
|
||||
imgNoModels: "没有配置图像模型。在「源」里为某个源添加 kind: image 的模型后即可在此生成。",
|
||||
imgNoData: "上游没有返回图片数据",
|
||||
imgNeedPrompt: "请输入提示词",
|
||||
imgFailed: "生成失败",
|
||||
cMsg: "Enter 发送,Shift+Enter 换行",
|
||||
send: "发送",
|
||||
cClear: "清空",
|
||||
@ -989,6 +1054,16 @@
|
||||
mModels: "模型列表",
|
||||
mAddModel: "+ 模型",
|
||||
mMeta: "Meta",
|
||||
discScan: "自动探测模型",
|
||||
discScanning: "探测中…",
|
||||
discHint: "填好 URL 和密钥后点「自动探测模型」,网关会拉取上游的模型列表并填入下方;仍需点保存才会生效。",
|
||||
discNeedUrl: "请先填写 URL",
|
||||
discEmpty: "上游返回的模型列表为空",
|
||||
discFound: "已找到 %d 个模型",
|
||||
discChatN: "对话 %d 个",
|
||||
discImageN: "图像 %d 个",
|
||||
discUnsaved: "尚未保存,点保存后生效",
|
||||
discFailed: "探测失败",
|
||||
mSave: "保存",
|
||||
mCancel: "取消",
|
||||
toastCopied: "已复制",
|
||||
@ -1106,6 +1181,25 @@
|
||||
|
||||
plVisibility: "Visibility",
|
||||
plEndpoints: "Endpoints",
|
||||
plKernel: "Kernel actions",
|
||||
plKernelTitle: "Kernel action permissions",
|
||||
plKernelHint: "Tick to grant. An action a plugin declares in source but was not granted is flagged red - that usually means one of its features is silently dead.",
|
||||
plKernelAllRead: "all read",
|
||||
plKernelN: "%d granted",
|
||||
plKernelReq: "requested",
|
||||
plKernelAsked: "asked",
|
||||
plDangerRead: "read",
|
||||
plDangerWrite: "write",
|
||||
plDangerDestructive: "destructive",
|
||||
plAction: "Action",
|
||||
plDanger: "Danger",
|
||||
plSave: "Save",
|
||||
plClose: "Close",
|
||||
plAuthHosts: "Extra outbound hosts (allowlist)",
|
||||
plAuthHostsHint: "Hosts a plugin needs that are not upstream sources are blocked by the outbound allowlist by default. Tick to approve; only hosts the plugin itself declared can be approved.",
|
||||
plHostOk: "approved",
|
||||
plHostPending: "pending",
|
||||
plNoAuthHosts: "this plugin declared no extra outbound hosts",
|
||||
plAccessTitle: "Access control",
|
||||
plAccessHint: "Roles or keys (@name) allowed to reach each endpoint. Empty = admin only. public = anyone. Listing @keys replaces the role rules entirely.",
|
||||
plWholePlugin: "endpoints below",
|
||||
@ -1232,6 +1326,20 @@
|
||||
adPoolPeak: "Peak",
|
||||
adPoolSteps: "Steps (grow/shrink)",
|
||||
cModel: "Model",
|
||||
cImageModels: "🖼 Image models (not selectable — use the Image tab)",
|
||||
urlHint: "Host address, usually WITHOUT /v1 — the gateway appends the path. Vendors differ: with https://x.com/v1 chat works but image generation becomes /v1/v1/… and 404s. If unsure use Auto-discover above; it probes and fills in the correct form.",
|
||||
navImage: "Image",
|
||||
imgWelcome: "Describe an image to generate it",
|
||||
imgSlow: "Generation can take 15s to 2min depending on the upstream model",
|
||||
imgPrompt: "Describe the image you want…",
|
||||
imgGenerate: "Generate",
|
||||
imgWorking: "Generating… %ds",
|
||||
imgCount: "Count",
|
||||
imgSize: "Size",
|
||||
imgNoModels: "No image models configured. Add a model with kind: image on the Sources page.",
|
||||
imgNoData: "The upstream returned no image data",
|
||||
imgNeedPrompt: "Enter a prompt first",
|
||||
imgFailed: "Generation failed",
|
||||
cMsg: "Enter to send, Shift+Enter for newline",
|
||||
send: "Send",
|
||||
cClear: "Clear",
|
||||
@ -1281,6 +1389,16 @@
|
||||
mModels: "Models",
|
||||
mAddModel: "+ model",
|
||||
mMeta: "Meta",
|
||||
discScan: "Auto-discover",
|
||||
discScanning: "Scanning…",
|
||||
discHint: "Fill in the URL and key, then press Auto-discover: the gateway fetches the upstream catalog and fills the list below. You still have to save.",
|
||||
discNeedUrl: "Enter the base URL first",
|
||||
discEmpty: "The upstream returned an empty model list",
|
||||
discFound: "Found %d models",
|
||||
discChatN: "%d chat",
|
||||
discImageN: "%d image",
|
||||
discUnsaved: "not saved yet — press Save",
|
||||
discFailed: "Discovery failed",
|
||||
mSave: "Save",
|
||||
mCancel: "Cancel",
|
||||
toastCopied: "Copied",
|
||||
@ -1471,7 +1589,7 @@
|
||||
// keep in sync, and adding a tab meant finding all three. A plugin page
|
||||
// that is routed but never shown is exactly the kind of silent gap that
|
||||
// survives review.
|
||||
const TABS = ["status", "chat", "keys", "sort", "sources", "adapters", "plugins"];
|
||||
const TABS = ["status", "chat", "image", "keys", "sort", "sources", "adapters", "plugins"];
|
||||
// Declared HERE, with var, because goTab() below reads it. It used to be a
|
||||
// `const` further down the file next to the other plugin state, which put
|
||||
// goTab's read inside the temporal dead zone: the first click on a plugin
|
||||
@ -2719,28 +2837,65 @@ recsState.keyNames = keyNames || {};
|
||||
/* ---------- chat tab ---------- */
|
||||
let chatHistory = [];
|
||||
let chatImages = [];
|
||||
async function renderChat() {
|
||||
const s = await api("/api/status");
|
||||
// Prefer per-source options ("source:model" routing) when the caller sees
|
||||
// sources (admin); otherwise fall back to the flat model list (user scope).
|
||||
let optList;
|
||||
if (Array.isArray(s.sources) && s.sources.length) {
|
||||
optList = ["AUTO"];
|
||||
s.sources.forEach((src) =>
|
||||
(src.models || []).forEach((m) => {
|
||||
optList.push(
|
||||
`<option value="${escAttr(`${src.name}:${m}`)}">${esc(`${src.name} · ${m}`)}</option>`,
|
||||
);
|
||||
}),
|
||||
);
|
||||
} else {
|
||||
optList = ["AUTO"].concat(
|
||||
(s.models || []).map(
|
||||
(m) => `<option value="${escAttr(m)}">${esc(m)}</option>`,
|
||||
),
|
||||
);
|
||||
}
|
||||
const finalOpts = optList.join("");
|
||||
async function renderChat() {
|
||||
const s = await api("/api/status");
|
||||
// Prefer per-source options ("source:model" routing) when the caller sees
|
||||
// sources (admin); otherwise fall back to the flat model list (user scope).
|
||||
//
|
||||
// Chat and image models are listed in SEPARATE groups. The backend used to
|
||||
// hand this page one flat list with the kind stripped, so gpt-image-2 sat
|
||||
// next to claude-sonnet-5.5 and picking it sent the request to /api/chat,
|
||||
// which cannot serve it — a failure whose message never mentioned images.
|
||||
//
|
||||
// model_kinds parallels models; an older backend simply omits it, and then
|
||||
// everything falls into the chat group (the pre-fix behaviour).
|
||||
const optList = [];
|
||||
const imgOpts = [];
|
||||
const addModel = (value, label, kind) => {
|
||||
const html = `<option value="${escAttr(value)}">${esc(label)}</option>`;
|
||||
if (kind === "image") imgOpts.push(html);
|
||||
else optList.push(html);
|
||||
};
|
||||
|
||||
if (Array.isArray(s.sources) && s.sources.length) {
|
||||
optList.push(`<option value="AUTO">AUTO</option>`);
|
||||
s.sources.forEach((src) => {
|
||||
// Prefer the kind-carrying list; fall back to the flat one.
|
||||
const kinds = Array.isArray(src.model_kinds) && src.model_kinds.length
|
||||
? src.model_kinds
|
||||
: (src.models || []).map((m) => ({ id: m, kind: "chat" }));
|
||||
kinds.forEach((m) => {
|
||||
const id = typeof m === "string" ? m : m.id;
|
||||
addModel(
|
||||
`${src.name}:${id}`,
|
||||
`${src.name} · ${id}`,
|
||||
typeof m === "string" ? "chat" : m.kind,
|
||||
);
|
||||
});
|
||||
});
|
||||
} else {
|
||||
optList.push(`<option value="AUTO">AUTO</option>`);
|
||||
(s.models || []).forEach((m) => {
|
||||
addModel(m, m, "chat");
|
||||
});
|
||||
}
|
||||
// Image models are listed but DISABLED, not merely grouped.
|
||||
//
|
||||
// Grouping alone still let an operator pick one, and picking it sent the
|
||||
// request to /api/chat — which cannot serve an image model — producing a
|
||||
// failure whose message never mentioned images. A disabled option explains
|
||||
// itself ("use the Image tab"); an enabled one just fails.
|
||||
//
|
||||
// The disabled attribute goes on at CONSTRUCTION, not via a later
|
||||
// replaceAll over the joined HTML: a plain String.replace only rewrites the
|
||||
// first match, so every source after the first contributed selectable
|
||||
// image models. Measured with six image models across two sources — the first
|
||||
// was disabled and the other five were not.
|
||||
const imgHtml = () => imgOpts.map((h) => h.replace("<option ", "<option disabled ")).join("");
|
||||
const finalOpts = optList.join("") +
|
||||
(imgOpts.length
|
||||
? `<optgroup label="${escAttr(t("cImageModels"))}">${imgHtml()}</optgroup>`
|
||||
: "");
|
||||
$("#tab-chat").innerHTML = `
|
||||
<div class="tab-chat">
|
||||
<div class="chat-wrap">
|
||||
@ -2784,6 +2939,149 @@ recsState.keyNames = keyNames || {};
|
||||
$("#ct-imgs").style.display = "none";
|
||||
$("#ct-msg").focus();
|
||||
}
|
||||
/* ---------- image tab ---------- */
|
||||
// Image generation lives in its own page rather than as an option in the
|
||||
// chat model dropdown.
|
||||
//
|
||||
// The dropdown used to list image models beside chat ones because
|
||||
// /api/status flattened the catalog to []string and dropped the kind.
|
||||
// Picking one sent the request to /api/chat, which cannot serve it. A
|
||||
// second tab makes the distinction structural: the only models offered
|
||||
// here are kind=image, and the endpoint called is the image one.
|
||||
let imgBusy = false;
|
||||
function imgModels() {
|
||||
const out = [];
|
||||
document.querySelectorAll("#it-model option").forEach((o) => {
|
||||
if (o.value) out.push(o.value);
|
||||
});
|
||||
return out;
|
||||
}
|
||||
async function renderImage() {
|
||||
const s = await api("/api/status");
|
||||
const opts = [];
|
||||
if (Array.isArray(s.sources) && s.sources.length) {
|
||||
s.sources.forEach((src) => {
|
||||
const kinds = Array.isArray(src.model_kinds) && src.model_kinds.length
|
||||
? src.model_kinds
|
||||
: (src.models || []).map((m) => ({ id: m, kind: "chat" }));
|
||||
kinds.forEach((m) => {
|
||||
const id = typeof m === "string" ? m : m.id;
|
||||
const kind = typeof m === "string" ? "chat" : m.kind;
|
||||
if (kind !== "image") return;
|
||||
opts.push(
|
||||
`<option value="${escAttr(`${src.name}:${id}`)}">${esc(`${src.name} · ${id}`)}</option>`,
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
const body = opts.length
|
||||
? ""
|
||||
: `<div class="empty">${esc(t("imgNoModels"))}</div>`;
|
||||
$("#tab-image").innerHTML = `
|
||||
<div class="tab-image">
|
||||
<div class="img-wrap">
|
||||
<div class="img-grid" id="it-grid">
|
||||
<div class="chat-empty" id="it-empty">
|
||||
<div class="ring">M</div>
|
||||
<p>${esc(t("imgWelcome"))}</p>
|
||||
<p class="muted" style="font-size:12px">${esc(t("imgSlow"))}</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="img-composer">
|
||||
<div class="chat-tools">
|
||||
<span class="tl">${t("cModel")}</span>
|
||||
<select id="it-model" ${opts.length ? "" : "disabled"}>${opts.join("")}</select>
|
||||
<span class="grow"></span>
|
||||
<label class="tl" for="it-n">${t("imgCount")}</label>
|
||||
<input id="it-n" type="number" min="1" max="4" value="1" style="width:64px" />
|
||||
<label class="tl" for="it-size">${t("imgSize")}</label>
|
||||
<input id="it-size" value="1024x1024" style="width:104px" />
|
||||
</div>
|
||||
<div class="chat-box">
|
||||
<textarea id="it-prompt" rows="2" placeholder="${escAttr(t("imgPrompt"))}"></textarea>
|
||||
<button class="sendbtn ghost" id="it-clear" onclick="clearImages()">${t("cClear")}</button>
|
||||
<button class="sendbtn" id="it-send" onclick="sendImage()" ${opts.length ? "" : "disabled"}>${t("imgGenerate")}</button>
|
||||
</div>
|
||||
${body}
|
||||
</div>
|
||||
</div>
|
||||
</div>`;
|
||||
}
|
||||
function clearImages() {
|
||||
$("#it-grid").innerHTML = `<div class="chat-empty" id="it-empty">
|
||||
<div class="ring">M</div>
|
||||
<p>${esc(t("imgWelcome"))}</p>
|
||||
<p class="muted" style="font-size:12px">${esc(t("imgSlow"))}</p>
|
||||
</div>`;
|
||||
}
|
||||
// Image generation is slow — measured 16s to 2min on the one upstream
|
||||
// tested — so the button reports elapsed time instead of looking hung.
|
||||
async function sendImage() {
|
||||
if (imgBusy) return;
|
||||
const prompt = ($("#it-prompt").value || "").trim();
|
||||
const model = $("#it-model").value;
|
||||
if (!prompt) {
|
||||
toast(t("imgNeedPrompt"));
|
||||
return;
|
||||
}
|
||||
if (!model) {
|
||||
toast(t("imgNoModels"));
|
||||
return;
|
||||
}
|
||||
const n = Math.max(1, Math.min(4, parseInt($("#it-n").value, 10) || 1));
|
||||
const size = ($("#it-size").value || "").trim();
|
||||
imgBusy = true;
|
||||
const btn = $("#it-send");
|
||||
btn.disabled = true;
|
||||
const t0 = Date.now();
|
||||
const tick = setInterval(() => {
|
||||
btn.textContent = t("imgWorking").replace("%d", String(Math.round((Date.now() - t0) / 1000)));
|
||||
}, 1000);
|
||||
try {
|
||||
// api() is a thin wrapper over fetch(p, o) — o is the NATIVE
|
||||
// fetch options object, so body must be a STRING.
|
||||
//
|
||||
// Passing the object directly made fetch coerce it to the literal
|
||||
// text "[object Object]", the gateway's json.Unmarshal rejected it,
|
||||
// and the page showed a bare "400 Bad Request" with no image and no
|
||||
// usable message. It cost a round of debugging because the DOM, the
|
||||
// model list and the button state were all correct — only the wire
|
||||
// format was wrong, which is invisible until you capture the request.
|
||||
//
|
||||
// JSON.stringify + an explicit Content-Type is what every other
|
||||
// POST in this file does; the missing header plus the object body
|
||||
// meant the server saw Content-Type: text/plain and a string body.
|
||||
const payload = { model: model, prompt: prompt, n: n };
|
||||
if (size) payload.size = size;
|
||||
const r = await api("/v1/images/generations", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
const empty = $("#it-empty");
|
||||
if (empty) empty.remove();
|
||||
const grid = $("#it-grid");
|
||||
const items = Array.isArray(r.data) ? r.data : [];
|
||||
items.forEach((it) => {
|
||||
const src = it.url || (it.b64_json ? "data:image/png;base64," + it.b64_json : "");
|
||||
if (!src) return;
|
||||
grid.insertAdjacentHTML(
|
||||
"beforeend",
|
||||
`<div class="img-cell"><img loading="lazy" src="${escAttr(src)}" alt="generated" />
|
||||
<div class="img-meta">${esc(it.revised_prompt || model)}</div></div>`,
|
||||
);
|
||||
});
|
||||
if (!items.length) toast(t("imgNoData"));
|
||||
} catch (e) {
|
||||
toast(t("imgFailed") + (e && e.message ? ": " + e.message : ""));
|
||||
} finally {
|
||||
clearInterval(tick);
|
||||
imgBusy = false;
|
||||
btn.disabled = false;
|
||||
btn.textContent = t("imgGenerate");
|
||||
}
|
||||
}
|
||||
|
||||
function updateGrow() {
|
||||
const ta = $("#ct-msg");
|
||||
if (!ta) return;
|
||||
@ -3134,13 +3432,15 @@ recsState.keyNames = keyNames || {};
|
||||
.join("") +
|
||||
"</select>";
|
||||
wrap.innerHTML = `<div class="card"><h2><span>${esc(name ? t("modalEdit") + ": " + name : t("modalNew"))}</span><span class="grow"></span>
|
||||
<button class="ghost small" onclick="srcDiscover()">${t("discScan")}</button>
|
||||
${!name ? `<button class="ghost small" onclick="srcFromTemplate()">${t("tplFrom")}</button>` : ""}
|
||||
<button class="ghost small" onclick="srcSaveAsTemplate()">${t("tplSave")}</button></h2>
|
||||
<div class="row">
|
||||
<div><label>${t("mName")}</label><input id="s-name" value="${escAttr(s.name)}" ${name ? "disabled" : ""}></div>
|
||||
<div><label>${t("mAlias")}</label>${adSel}</div>
|
||||
</div>
|
||||
<label>${t("mURL")}</label><input id="s-url" value="${escAttr(s.base_url || "")}">
|
||||
<label>${t("mURL")}</label><input id="s-url" value="${escAttr(s.base_url || "")}" placeholder="https://api.vendor.com">
|
||||
<div class="hint">${t("urlHint")}</div>
|
||||
<label>${t("mKey")}</label><input id="s-key" type="password" value="${escAttr(s.api_key || "")}">
|
||||
<div class="row">
|
||||
<div><label>${t("mEp")}</label><input id="s-ep" value="${escAttr(s.endpoint || "")}"></div>
|
||||
@ -3160,6 +3460,7 @@ recsState.keyNames = keyNames || {};
|
||||
</div>
|
||||
<label>${t("mModels")}</label>
|
||||
<div id="s-models"></div>
|
||||
<div class="hint" id="s-disc-hint">${t("discHint")}</div>
|
||||
<button class="ghost small" onclick="addModelRow()">${t("mAddModel")}</button>
|
||||
<label>${t("mMeta")}</label>
|
||||
<textarea id="s-meta" style="min-height:80px" placeholder='{"app_id":"x","app_secret":"y"}'>${esc(JSON.stringify(s.meta || {}, null, 2))}</textarea>
|
||||
@ -3208,6 +3509,96 @@ recsState.keyNames = keyNames || {};
|
||||
modelRow({ id: "", priority: 0, kind: "chat" }, div.children.length),
|
||||
);
|
||||
}
|
||||
// srcDiscover asks the gateway to probe the upstream for its catalog and
|
||||
// fills the model list from the answer. It never writes anything: the
|
||||
// operator still has to press Save, because a wrong base_url produces a
|
||||
// plausible-looking but useless source, and writing automatically would
|
||||
// overwrite a working configuration with it.
|
||||
async function srcDiscover() {
|
||||
const urlEl = $("#s-url");
|
||||
const keyEl = $("#s-key");
|
||||
const baseUrl = (urlEl && urlEl.value.trim()) || "";
|
||||
const apiKey = (keyEl && keyEl.value.trim()) || "";
|
||||
const hint = $("#s-disc-hint");
|
||||
const say = (msg) => {
|
||||
if (hint) hint.textContent = msg;
|
||||
};
|
||||
if (!baseUrl) {
|
||||
toast(t("discNeedUrl"));
|
||||
return;
|
||||
}
|
||||
const btn = document.querySelector('button[onclick="srcDiscover()"]');
|
||||
const restore = btn ? btn.textContent : null;
|
||||
if (btn) {
|
||||
btn.disabled = true;
|
||||
btn.textContent = t("discScanning");
|
||||
}
|
||||
say(t("discScanning"));
|
||||
try {
|
||||
const r = await api("/api/sources/discover", {
|
||||
method: "POST",
|
||||
// api() hands its second argument straight to fetch, so `body`
|
||||
// must be a STRING. Passing the object made fetch send the literal
|
||||
// text "[object Object]" and the gateway answered
|
||||
// 400 invalid character 'o' — the Auto-discover button looked
|
||||
// dead while curl against the same endpoint worked, because the
|
||||
// curl test exercised the ENDPOINT and not this BUTTON.
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ base_url: baseUrl, api_key: apiKey }),
|
||||
});
|
||||
// The server normalized the URL form. Showing the operator exactly
|
||||
// what will be saved is the whole point of probing server-side
|
||||
// instead of guessing in the browser.
|
||||
if (r.base_url && urlEl) urlEl.value = r.base_url;
|
||||
if (r.endpoint && $("#s-ep")) $("#s-ep").value = r.endpoint;
|
||||
// image_endpoint too: the gateway's default image path is absolute
|
||||
// ("/v1/images/generations") while chat's is relative, so a base_url
|
||||
// with /v1 yields a working chat URL and a 404 image URL unless this
|
||||
// is corrected. Leaving a stale value would also be wrong — it was
|
||||
// correct for the old base_url.
|
||||
if (r.image_endpoint && $("#s-img")) $("#s-img").value = r.image_endpoint;
|
||||
|
||||
// Array.isArray, not a truthy check: a JSON null here would make
|
||||
// .concat fail and leave the form looking empty with no error.
|
||||
const chat = Array.isArray(r.models) ? r.models : [];
|
||||
const image = Array.isArray(r.image_models) ? r.image_models : [];
|
||||
const all = chat.concat(image);
|
||||
if (!all.length) {
|
||||
say(t("discEmpty"));
|
||||
return;
|
||||
}
|
||||
const box = $("#s-models");
|
||||
if (box) {
|
||||
box.innerHTML = all
|
||||
.map((id, i) =>
|
||||
modelRow(
|
||||
{
|
||||
id: id,
|
||||
priority: image.indexOf(id) >= 0 ? 70 : 50,
|
||||
kind: image.indexOf(id) >= 0 ? "image" : "chat",
|
||||
},
|
||||
i,
|
||||
),
|
||||
)
|
||||
.join("");
|
||||
}
|
||||
const bits = [t("discFound").replace("%d", String(all.length))];
|
||||
if (chat.length) bits.push(t("discChatN").replace("%d", String(chat.length)));
|
||||
if (image.length) bits.push(t("discImageN").replace("%d", String(image.length)));
|
||||
if (r.note) bits.push(r.note);
|
||||
for (const w of Array.isArray(r.warnings) ? r.warnings : []) bits.push(w);
|
||||
bits.push(t("discUnsaved"));
|
||||
say(bits.join(" · "));
|
||||
} catch (e) {
|
||||
say(t("discFailed") + (e && e.message ? ": " + e.message : ""));
|
||||
} finally {
|
||||
if (btn) {
|
||||
btn.disabled = false;
|
||||
btn.textContent = restore;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function saveSource(btn) {
|
||||
const models = [...document.querySelectorAll("#s-models .model-row")]
|
||||
.map((row) => ({
|
||||
@ -3466,7 +3857,8 @@ recsState.keyNames = keyNames || {};
|
||||
<div><label>${t("mName")}</label><input id="s-name" value="${escAttr(s.name)}" ${name ? "disabled" : ""}></div>
|
||||
<div><label>${t("mAlias")}</label>${adSel}</div>
|
||||
</div>
|
||||
<label>${t("mURL")}</label><input id="s-url" value="${escAttr(s.base_url || "")}">
|
||||
<label>${t("mURL")}</label><input id="s-url" value="${escAttr(s.base_url || "")}" placeholder="https://api.vendor.com">
|
||||
<div class="hint">${t("urlHint")}</div>
|
||||
<div class="row">
|
||||
<div><label>${t("mEp")}</label><input id="s-ep" value="${escAttr(s.endpoint || "")}"></div>
|
||||
<div><label>${t("mImgEp")}</label><input id="s-img" value="${escAttr(s.image_endpoint || "")}"></div>
|
||||
@ -3481,6 +3873,7 @@ recsState.keyNames = keyNames || {};
|
||||
</div>
|
||||
<label>${t("mModels")}</label>
|
||||
<div id="s-models"></div>
|
||||
<div class="hint" id="s-disc-hint-tpl">${t("discHint")}</div>
|
||||
<button class="ghost small" onclick="addModelRow()">${t("mAddModel")}</button>
|
||||
<label>${t("mMeta")}</label>
|
||||
<textarea id="s-meta" style="min-height:80px" placeholder='{"app_id":"x","app_secret":"y"}'>${esc(JSON.stringify(s.meta || {}, null, 2))}</textarea>
|
||||
@ -4463,6 +4856,14 @@ function afterChainEdit() {
|
||||
// on_disk rather than the loaded set so a plugin that FAILED to load
|
||||
// still appears, with its error — otherwise a syntax error looks
|
||||
// identical to "the plugin is not there".
|
||||
async function resolveRoute(path, winner) {
|
||||
await api("/api/plugins/conflicts", { method: "POST", body: JSON.stringify({ kind: "route", path, winner }) });
|
||||
renderPlugins();
|
||||
}
|
||||
async function resolveAdapter(name, winner) {
|
||||
await api("/api/plugins/conflicts", { method: "POST", body: JSON.stringify({ kind: "adapter", name, winner }) });
|
||||
renderPlugins();
|
||||
}
|
||||
async function renderPlugins() {
|
||||
let j;
|
||||
try {
|
||||
@ -4506,6 +4907,7 @@ function afterChainEdit() {
|
||||
<td>${hooks}</td>
|
||||
<td>${plVisCell(j, p)}</td>
|
||||
<td>${plEpsCell(j, p)}</td>
|
||||
<td data-plkernel="${escAttr(p.name)}">${plKernelCell(p)}</td>
|
||||
<td style="white-space:nowrap">
|
||||
<button class="small" onclick="editPlugin('${escAttr(p.name)}')">${t("plEdit")}</button>
|
||||
${toggle}
|
||||
@ -4514,6 +4916,32 @@ function afterChainEdit() {
|
||||
})
|
||||
.join("");
|
||||
|
||||
// 冲突裁决(用户要求:服务路径/适配器名冲突不崩溃不拒启,运维在此选一保留)
|
||||
let confHtml = "";
|
||||
try {
|
||||
const cj = await api("/api/plugins/conflicts");
|
||||
const rc = cj.routes || [];
|
||||
const ac = cj.adapters || [];
|
||||
if (rc.length || ac.length) {
|
||||
const rows2 = []
|
||||
.concat(rc.map((c) => `
|
||||
<tr><td><span class="tag tag-red">路径</span> <code>${esc(c.path)}</code></td>
|
||||
<td>${c.candidates.map(esc).join(" vs ")}</td>
|
||||
<td>${c.resolved ? `<span class="tag tag-green">已选 ${esc(c.resolved)}</span>` : `<span class="tag tag-red">待裁决</span>`}</td>
|
||||
<td>${c.candidates.map((n) => `<button class="small" onclick="resolveRoute('${escAttr(c.path)}','${escAttr(n)}')">用 ${esc(n)}</button>`).join(" ")}</td></tr>`))
|
||||
.concat(ac.map((c) => `
|
||||
<tr><td><span class="tag">适配器</span> <code>${esc(c.name)}</code></td>
|
||||
<td>${esc(c.incumbent)}(现任) vs 插件 ${esc(c.candidate)}(候选)</td>
|
||||
<td>${c.resolved ? `<span class="tag tag-green">已选 ${esc(c.resolved)}</span>` : `<span class="tag tag-red">待裁决</span>`}</td>
|
||||
<td><button class="small" onclick="resolveAdapter('${escAttr(c.name)}','incumbent')">保留现任</button>
|
||||
<button class="small" onclick="resolveAdapter('${escAttr(c.name)}','candidate')">用插件版</button></td></tr>`))
|
||||
.join("");
|
||||
confHtml = `<div class="card" style="border-color:var(--danger)"><h2>冲突裁决(${rc.length + ac.length})</h2>
|
||||
<p class="muted">两个插件/适配器声明了同一条服务路径或同一个适配器名。未裁决的路径返回 409,撞名的适配器由现任继续服务。请选一个保留。</p>
|
||||
<div class="tbl-wrap"><table><tr><th>冲突项</th><th>候选</th><th>状态</th><th>裁决</th></tr>${rows2}</table></div></div>`;
|
||||
}
|
||||
} catch (_) {}
|
||||
|
||||
// Hook errors: a plugin that throws leaves no visible trace anywhere
|
||||
// else, so without this the symptom is "the feature silently doesn't
|
||||
// work".
|
||||
@ -4524,11 +4952,11 @@ function afterChainEdit() {
|
||||
)
|
||||
.join("");
|
||||
|
||||
$("#tab-plugins").innerHTML = `
|
||||
$("#tab-plugins").innerHTML = `${confHtml}
|
||||
<div class="card"><h2>${t("plTitle")} (${(j.on_disk || []).length})</h2>
|
||||
<div class="tbl-wrap"><table><tr>
|
||||
<th>${t("tName")}</th><th>${t("plState")}</th><th>${t("plStages")}</th><th>${t("plVisibility")}</th><th>${t("plEndpoints")}</th><th></th>
|
||||
</tr>${rows || `<tr><td colspan="6" class="empty">${t("plEmpty")}</td></tr>`}</table></div>
|
||||
<th>${t("tName")}</th><th>${t("plState")}</th><th>${t("plStages")}</th><th>${t("plVisibility")}</th><th>${t("plEndpoints")}</th><th>${t("plKernel")}</th><th></th>
|
||||
</tr>${rows || `<tr><td colspan="7" class="empty">${t("plEmpty")}</td></tr>`}</table></div>
|
||||
<div id="pl-access"></div>
|
||||
${errList ? `<p class="muted" style="color:var(--danger)">${t("plHookErr")}</p><ul style="font-size:12px">${errList}</ul>` : ""}
|
||||
<p class="muted">${t("plDir")} <code>${esc(j.plugin_dir)}</code></p>
|
||||
@ -4542,7 +4970,21 @@ function afterChainEdit() {
|
||||
<p><button onclick="installPlugin()">${t("plInstallBtn")}</button></p>
|
||||
</div>`;
|
||||
bindDropzone("pl-dz", "pl-file", "#pl-name", "#pl-code");
|
||||
window.__plNames = (j.on_disk || []).map((p) => p.name);
|
||||
renderPluginAccess(j);
|
||||
// Kernel grants load separately (one request per plugin) and then patch
|
||||
// ONLY the affected cells. Re-calling renderPlugins() here would recurse:
|
||||
// renderPlugins -> refreshKernelSummary -> renderPlugins.
|
||||
refreshKernelSummary().then(() => {
|
||||
window.__plNames.forEach((n) => {
|
||||
const el = document.querySelector(`[data-plkernel="${CSS.escape(n)}"]`);
|
||||
if (el) {
|
||||
const p = (j.on_disk || []).find((x) => x.name === n);
|
||||
if (p) el.innerHTML = plKernelCell(p);
|
||||
}
|
||||
});
|
||||
bindKernelCells();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@ -4603,6 +5045,123 @@ function afterChainEdit() {
|
||||
: `<span class="muted">—</span>`;
|
||||
}
|
||||
|
||||
// plKernelCell shows how many kernel actions a plugin may call, and flags
|
||||
// a DECLARED-but-not-granted request. The flag matters: a plugin that asks
|
||||
// for add_source in its source and never gets it looks identical to one
|
||||
// that never asked, until its feature silently doesn't work.
|
||||
function plKernelCell(p) {
|
||||
const k = (window.__plKernel || {})[p.name];
|
||||
if (!k) return `<span class="muted">—</span>`;
|
||||
const granted = (k.granted || []).length + (k.all_read ? 1 : 0);
|
||||
const pending = (k.declared || []).filter((d) => !(k.granted || []).includes(d));
|
||||
const label = k.all_read ? t("plKernelAllRead") : t("plKernelN").replace("%d", granted);
|
||||
const warn = pending.length
|
||||
? ` <span class="tag tag-red" title="${escAttr(pending.join(", "))}">${t("plKernelReq")}</span>`
|
||||
: "";
|
||||
return `<a href="#" onclick="openKernelEditor('${escAttr(p.name)}');return false">${label}</a>${warn}`;
|
||||
}
|
||||
|
||||
// openKernelEditor renders the per-plugin kernel/permission panel into
|
||||
// #pl-access below the table.
|
||||
async function openKernelEditor(name) {
|
||||
const host = $("#pl-access");
|
||||
if (!host) return;
|
||||
let j;
|
||||
try {
|
||||
j = await api("/api/plugins/" + encodeURIComponent(name) + "/kernel");
|
||||
} catch (e) {
|
||||
host.innerHTML = `<div class="card"><h2>${esc(name)}</h2><div class="muted">${esc(String(e.message || e))}</div></div>`;
|
||||
return;
|
||||
}
|
||||
const dangerOrder = { read: 0, write: 1, destructive: 2 };
|
||||
const acts = (j.actions || []).slice().sort((a, b) => {
|
||||
const d = (dangerOrder[a.danger] ?? 9) - (dangerOrder[b.danger] ?? 9);
|
||||
return d !== 0 ? d : a.name.localeCompare(b.name);
|
||||
});
|
||||
const granted = new Set(j.granted || []);
|
||||
const rows = acts
|
||||
.map((a) => {
|
||||
const tag = a.danger === "destructive"
|
||||
? `<span class="tag tag-red">${t("plDangerDestructive")}</span>`
|
||||
: a.danger === "write"
|
||||
? `<span class="tag">${t("plDangerWrite")}</span>`
|
||||
: `<span class="tag tag-green">${t("plDangerRead")}</span>`;
|
||||
const asked = a.declared ? ` <span class="tag tag-blue">${t("plKernelAsked")}</span>` : "";
|
||||
return `<tr>
|
||||
<td><input type="checkbox" data-ka="${escAttr(a.name)}" ${granted.has(a.name) ? "checked" : ""}></td>
|
||||
<td><b>${esc(a.name)}</b>${asked}<div class="muted">${esc(a.description || "")}</div></td>
|
||||
<td>${tag}</td></tr>`;
|
||||
})
|
||||
.join("");
|
||||
const declaredHosts = j.hosts_declared || [];
|
||||
const grantedHosts = new Set(j.hosts_granted || []);
|
||||
const hostRows = declaredHosts.length
|
||||
? declaredHosts
|
||||
.map(
|
||||
(h) => `<tr><td><input type="checkbox" data-ah="${escAttr(h)}" ${grantedHosts.has(h) ? "checked" : ""}></td>
|
||||
<td><code>${esc(h)}</code></td>
|
||||
<td>${grantedHosts.has(h) ? `<span class="tag tag-green">${t("plHostOk")}</span>` : `<span class="tag">${t("plHostPending")}</span>`}</td></tr>`,
|
||||
)
|
||||
.join("")
|
||||
: `<tr><td colspan="3" class="empty">${t("plNoAuthHosts")}</td></tr>`;
|
||||
host.innerHTML = `
|
||||
<div class="card"><h2>${esc(name)} — ${t("plKernelTitle")}</h2>
|
||||
<p class="muted">${t("plKernelHint")}</p>
|
||||
<div class="tbl-wrap"><table>
|
||||
<tr><th></th><th>${t("plAction")}</th><th>${t("plDanger")}</th></tr>
|
||||
${rows}
|
||||
</table></div>
|
||||
<p><button onclick="saveKernelPerms('${escAttr(name)}')">${t("plSave")}</button>
|
||||
<button onclick="document.getElementById('pl-access').innerHTML=''">${t("plClose")}</button></p>
|
||||
</div>
|
||||
<div class="card"><h2>${t("plAuthHosts")}</h2>
|
||||
<p class="muted">${t("plAuthHostsHint")}</p>
|
||||
<div class="tbl-wrap"><table>${hostRows}</table></div>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
async function saveKernelPerms(name) {
|
||||
const actions = [...document.querySelectorAll("[data-ka]")]
|
||||
.filter((c) => c.checked)
|
||||
.map((c) => c.getAttribute("data-ka"));
|
||||
const hosts = [...document.querySelectorAll("[data-ah]")]
|
||||
.filter((c) => c.checked)
|
||||
.map((c) => c.getAttribute("data-ah"));
|
||||
try {
|
||||
await api("/api/plugins/" + encodeURIComponent(name) + "/kernel", {
|
||||
method: "PUT",
|
||||
body: JSON.stringify({ actions, all_read: false, hosts }),
|
||||
});
|
||||
await refreshKernelSummary();
|
||||
await openKernelEditor(name);
|
||||
} catch (e) {
|
||||
alert(String(e.message || e));
|
||||
}
|
||||
}
|
||||
|
||||
// bindKernelCells is a no-op hook kept so the initial render and the
|
||||
// post-summary patch share one code path; the cell uses an inline onclick,
|
||||
// so there is nothing to re-bind. It exists to make that explicit rather
|
||||
// than to leave a missing function.
|
||||
function bindKernelCells() {}
|
||||
|
||||
// refreshKernelSummary loads every plugin's grant state so the table
|
||||
// column can show it without a request per row.
|
||||
async function refreshKernelSummary() {
|
||||
const names = (window.__plNames || []);
|
||||
const out = {};
|
||||
await Promise.all(
|
||||
names.map(async (n) => {
|
||||
try {
|
||||
out[n] = await api("/api/plugins/" + encodeURIComponent(n) + "/kernel");
|
||||
} catch (e) {
|
||||
/* a plugin that vanished mid-refresh simply has no summary */
|
||||
}
|
||||
}),
|
||||
);
|
||||
window.__plKernel = out;
|
||||
}
|
||||
|
||||
// ---- visibility & endpoint access ----
|
||||
//
|
||||
// The plugin page is where an operator edits who can see what. The
|
||||
@ -5708,6 +6267,7 @@ toast(t("kAutoChainGlobal"));
|
||||
const done = () => remountPluginElements();
|
||||
if (tab === "status") return renderStatus().then(done, done);
|
||||
if (tab === "chat") return renderChat().then(done, done);
|
||||
if (tab === "image") return renderImage().then(done, done);
|
||||
if (tab === "keys") return renderKeys().then(done, done);
|
||||
if (tab === "sort") return renderSort().then(done, done);
|
||||
if (tab === "sources") return renderSources().then(done, done);
|
||||
@ -5781,6 +6341,38 @@ toast(t("kAutoChainGlobal"));
|
||||
if (!r.ok) throw new Error(body.error?.message || r.status);
|
||||
return body.result;
|
||||
},
|
||||
// fanout runs one of the plugin's exported functions once per argument
|
||||
// set, IN PARALLEL, from a single request.
|
||||
//
|
||||
// `call` in a Promise.all is NOT this. Twenty concurrent /call/ requests
|
||||
// arrive concurrently and then serialise on the plugin mutex inside the
|
||||
// kernel — the lock is held across the Lua function, network wait
|
||||
// included, because one Lua VM has one stack. Measured on a live
|
||||
// gateway: twenty quota probes took 29s of wall clock through /call/,
|
||||
// against 1.4–3.3s for a single uncontended probe. The browser-side
|
||||
// concurrency bought nothing and the page looked simply broken.
|
||||
//
|
||||
// The parallelism has to happen where the fan-out is received, so it
|
||||
// takes one request. Results come back positionally aligned with `argSets`;
|
||||
// a failed item is null in its slot rather than an error for the batch,
|
||||
// because one unreachable upstream must not blank a whole page of cards.
|
||||
//
|
||||
// Same authorization as call: admin-only, and the function must be in
|
||||
// the plugin's exports.
|
||||
async fanout(name, fn, argSets) {
|
||||
const r = await fetch(
|
||||
"/api/plugins/" + encodeURIComponent(name) + "/fanout/" + encodeURIComponent(fn),
|
||||
{
|
||||
method: "POST",
|
||||
credentials: "same-origin",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ args: argSets || [] }),
|
||||
},
|
||||
);
|
||||
const body = await r.json().catch(() => ({}));
|
||||
if (!r.ok) throw new Error(body.error?.message || r.status);
|
||||
return body.results || [];
|
||||
},
|
||||
onTabShown(fn) {
|
||||
PLUGIN_TAB_CBS.__last = PLUGIN_TAB_CBS.__last || [];
|
||||
PLUGIN_TAB_CBS.__last.push(fn);
|
||||
|
||||
195
internal/gateway/ui_image_page_test.go
Normal file
195
internal/gateway/ui_image_page_test.go
Normal file
@ -0,0 +1,195 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"llmsproxy/internal/provider"
|
||||
)
|
||||
|
||||
// TestUI_ChatPageGroupsImageModels is a UI-contract guard for the bug that
|
||||
// started this: the chat page listed image models beside chat ones, and picking
|
||||
// one posted to /api/chat, which cannot serve it.
|
||||
//
|
||||
// The contract is asserted on the HTML source rather than by running a browser,
|
||||
// because the failure this must catch is a source-level one — an <option>
|
||||
// without `disabled`. A DOM assertion in a headless browser would pass right
|
||||
// up until someone removed the attribute, and then only if a CI machine had the
|
||||
// whole stack up.
|
||||
func TestUI_ChatPageGroupsImageModels(t *testing.T) {
|
||||
ui := uiSource(t)
|
||||
|
||||
// The dropdown must read model_kinds, not the flat models list. Reading the
|
||||
// flat list is what made chat and image models indistinguishable.
|
||||
if !strings.Contains(ui, "model_kinds") {
|
||||
t.Error("chat page never reads model_kinds; image models will be " +
|
||||
"indistinguishable from chat models again")
|
||||
}
|
||||
|
||||
// Image options must be rendered disabled. A plain String.replace over the
|
||||
// joined HTML only disables the FIRST match — that bug shipped once here
|
||||
// (one source disabled, five selectable) — so the assertion looks for the
|
||||
// per-item map, not for a global replace.
|
||||
if strings.Contains(ui, `imgOpts.join("").replace`) {
|
||||
t.Error("chat page disables image options with String.replace over the " +
|
||||
"joined HTML, which only rewrites the first option")
|
||||
}
|
||||
if !strings.Contains(ui, "disabled") {
|
||||
t.Error("chat page never emits a disabled attribute for image models")
|
||||
}
|
||||
}
|
||||
|
||||
// TestUI_HasDedicatedImagePage pins the second half of the fix: image models
|
||||
// need somewhere they CAN be tested.
|
||||
func TestUI_HasDedicatedImagePage(t *testing.T) {
|
||||
ui := uiSource(t)
|
||||
|
||||
for _, want := range []string{
|
||||
`data-tab="image"`, // sidebar entry
|
||||
`id="tab-image"`, // pane
|
||||
`function renderImage`, // renderer registered with refresh()
|
||||
`tab === "image"`, // dispatched
|
||||
`/v1/images/generations`, // the endpoint the page actually calls
|
||||
} {
|
||||
if !strings.Contains(ui, want) {
|
||||
t.Errorf("image page is missing %q", want)
|
||||
}
|
||||
}
|
||||
|
||||
// The page must be reachable from TABS; goTab iterates TABS to toggle pane
|
||||
// visibility, so a pane outside that list is present but permanently hidden —
|
||||
// which reads as "the page is blank" because the content is in the DOM.
|
||||
if !strings.Contains(ui, `"image"`) {
|
||||
t.Error(`"image" does not appear in the TABS list`)
|
||||
}
|
||||
}
|
||||
|
||||
// TestUI_ImagePageCallsTheImageEndpoint is the discriminator: a page that
|
||||
// renders an image result but posts to the chat endpoint would produce exactly
|
||||
// the original failure, just prettier.
|
||||
func TestUI_ImagePageCallsTheImageEndpoint(t *testing.T) {
|
||||
ui := uiSource(t)
|
||||
|
||||
i := strings.Index(ui, "async function sendImage(")
|
||||
if i < 0 {
|
||||
t.Fatal("sendImage() not found")
|
||||
}
|
||||
// Bound the search to the function body rather than the whole file, so a
|
||||
// /v1/images/generations mention elsewhere cannot satisfy this.
|
||||
body := ui[i:]
|
||||
if end := strings.Index(body, "\n function "); end > 0 {
|
||||
body = body[:end]
|
||||
}
|
||||
if end := strings.Index(body, "\n async function "); end > 0 {
|
||||
if s := body[:end]; strings.Contains(s, "/v1/images/generations") {
|
||||
body = s
|
||||
} else {
|
||||
body = body[:end]
|
||||
}
|
||||
}
|
||||
if !strings.Contains(body, "/v1/images/generations") {
|
||||
t.Error("sendImage() does not post to /v1/images/generations")
|
||||
}
|
||||
}
|
||||
|
||||
// TestSourceStatus_ExposesModelKindsToTheUI is the server half of the same
|
||||
// contract: the field the UI reads must actually be serialized.
|
||||
func TestSourceStatus_ExposesModelKindsToTheUI(t *testing.T) {
|
||||
st := provider.SourceStatus{
|
||||
Name: "vela",
|
||||
Models: []string{"claude-sonnet-5.5", "gpt-image-2"},
|
||||
ModelKinds: []provider.ModelKindInfo{
|
||||
{ID: "claude-sonnet-5.5", Kind: "chat"},
|
||||
{ID: "gpt-image-2", Kind: "image"},
|
||||
},
|
||||
}
|
||||
b, err := json.Marshal(st)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var got map[string]interface{}
|
||||
if err := json.Unmarshal(b, &got); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mk, ok := got["model_kinds"].([]interface{})
|
||||
if !ok || len(mk) != 2 {
|
||||
t.Fatalf("model_kinds = %v, want 2 entries; the UI cannot split the list without it", got["model_kinds"])
|
||||
}
|
||||
last := mk[1].(map[string]interface{})
|
||||
if last["kind"] != "image" {
|
||||
t.Errorf("model_kinds[1].kind = %v, want \"image\"", last["kind"])
|
||||
}
|
||||
}
|
||||
|
||||
// TestUI_BaseURLFieldExplainsWhereToCut keeps the second bug fixed: the URL
|
||||
// hint has to mention the concrete failure, not just say "enter the URL".
|
||||
func TestUI_BaseURLFieldExplainsWhereToCut(t *testing.T) {
|
||||
ui := uiSource(t)
|
||||
if !strings.Contains(ui, "urlHint") {
|
||||
t.Fatal("no urlHint; the base_url field gives no guidance about /v1")
|
||||
}
|
||||
hint := ui[strings.Index(ui, "urlHint:"):]
|
||||
if len(hint) > 700 {
|
||||
hint = hint[:700]
|
||||
}
|
||||
// The trap is specific and reproducible: base_url ending in /v1 makes chat
|
||||
// work and image generation 404. Saying only "enter the URL" reproduces the
|
||||
// confusion the hint exists to remove.
|
||||
for _, want := range []string{"/v1", "404"} {
|
||||
if !strings.Contains(hint, want) {
|
||||
t.Errorf("urlHint does not mention %q — the hint must name the concrete failure", want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestImageEndpointComposesUnderRealConditions proves the hint describes a
|
||||
// reachable path rather than a plausible-sounding one.
|
||||
//
|
||||
// The case that matters: base_url already ends in /v1 (which is what an
|
||||
// operator pastes from an SDK example), so the RELATIVE image endpoint composes
|
||||
// to …/v1/images/generations and works, while the gateway's default ABSOLUTE
|
||||
// path would produce …/v1/v1/… and 404.
|
||||
//
|
||||
// The doubled form is served as a 404 here so the test would fail if the code
|
||||
// ever reverted to relying on the default.
|
||||
func TestImageEndpointComposesUnderRealConditions(t *testing.T) {
|
||||
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.Path {
|
||||
case "/v1/images/generations":
|
||||
_, _ = w.Write([]byte(`{"data":[{"url":"http://example.invalid/x.png"}]}`))
|
||||
default:
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
}))
|
||||
defer up.Close()
|
||||
|
||||
// base_url as an operator types it: host root + /v1.
|
||||
base := up.URL + "/v1"
|
||||
|
||||
// What discovery writes when base carries a version segment.
|
||||
relative := base + "/images/generations"
|
||||
resp, err := http.Post(relative, "application/json", strings.NewReader(`{"model":"m"}`))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Errorf("POST %s = %d, want 200 — the form discovery writes must work", relative, resp.StatusCode)
|
||||
}
|
||||
|
||||
// What the gateway's built-in default would produce for the same base_url.
|
||||
doubled := base + "/v1/images/generations"
|
||||
resp2, err := http.Post(doubled, "application/json", strings.NewReader(`{"model":"m"}`))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resp2.Body.Close()
|
||||
if resp2.StatusCode != http.StatusNotFound {
|
||||
t.Errorf("POST %s = %d, want 404 — this test's premise is that the "+
|
||||
"default image path doubles the version segment; if the upstream ever "+
|
||||
"accepts it, composeImageEndpoint's rule needs rechecking", doubled, resp2.StatusCode)
|
||||
}
|
||||
}
|
||||
109
internal/gateway/ui_image_wire_test.go
Normal file
109
internal/gateway/ui_image_wire_test.go
Normal file
@ -0,0 +1,109 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestUI_ImageRequestSendsJSONBody pins the bug that made the image page
|
||||
// unusable in production while every DOM assertion still passed.
|
||||
//
|
||||
// The page called api(path, {method, body: {...}}). api() is
|
||||
// `(p, o) => fetch(p, o)` — o is the NATIVE fetch options object, so an object
|
||||
// in `body` is coerced by fetch into the literal text "[object Object]". The
|
||||
// gateway's json.Unmarshal rejected it, the response was a bare 400 with
|
||||
// lat_ms 0, and the page showed an error toast with no image and no usable
|
||||
// message.
|
||||
//
|
||||
// What made this expensive: the pane rendered, the model list had all six
|
||||
// models, the button enabled itself, the console was clean. Only the WIRE
|
||||
// format was wrong, and nothing in the DOM shows a wire format.
|
||||
func TestUI_ImageRequestSendsJSONBody(t *testing.T) {
|
||||
body := sendImageSource(t)
|
||||
|
||||
if !strings.Contains(body, "JSON.stringify") {
|
||||
t.Error("sendImage() does not JSON.stringify its body — fetch coerces " +
|
||||
"the object to the text \"[object Object]\" and the gateway answers " +
|
||||
"400 with lat_ms 0")
|
||||
}
|
||||
if !strings.Contains(body, `"Content-Type": "application/json"`) {
|
||||
t.Error("sendImage() does not set Content-Type: application/json; the " +
|
||||
"server sees text/plain and may refuse to parse the body")
|
||||
}
|
||||
// The literal defect, asserted so a later refactor reintroducing it fails
|
||||
// here rather than in a browser.
|
||||
if strings.Contains(body, "body: size ? {") || strings.Contains(body, "body: { model") {
|
||||
t.Error("sendImage() still passes an object literal as the fetch body")
|
||||
}
|
||||
}
|
||||
|
||||
// TestUI_PostCallsSendJSONString guards every POST in the file, not just the
|
||||
// image one. The image page happened to be the only place that used api() with
|
||||
// an object body, so a check scoped to it would pass while another page had the
|
||||
// same defect.
|
||||
func TestUI_PostCallsSendJSONString(t *testing.T) {
|
||||
ui := uiSource(t)
|
||||
lines := strings.Split(ui, "\n")
|
||||
|
||||
var bad []string
|
||||
for i, l := range lines {
|
||||
if !strings.Contains(l, `method: "POST"`) {
|
||||
continue
|
||||
}
|
||||
end := i + 8
|
||||
if end > len(lines) {
|
||||
end = len(lines)
|
||||
}
|
||||
window := strings.Join(lines[i:end], "\n")
|
||||
if !strings.Contains(window, "body:") {
|
||||
continue // no body at all: legitimate (a bare trigger)
|
||||
}
|
||||
if strings.Contains(window, "JSON.stringify") || strings.Contains(window, "FormData") {
|
||||
continue
|
||||
}
|
||||
bad = append(bad, strings.TrimSpace(l))
|
||||
}
|
||||
if len(bad) > 0 {
|
||||
t.Errorf("%d POST call(s) send a non-string body:", len(bad))
|
||||
for _, b := range bad {
|
||||
if len(b) > 100 {
|
||||
b = b[:100]
|
||||
}
|
||||
t.Errorf(" %s", b)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestUI_ImagePageReportsServerErrors pins that a failed generation surfaces
|
||||
// something actionable. The production bug produced a bare "400 Bad Request"
|
||||
// toast; the requirement is that the gateway's own message reaches the page.
|
||||
func TestUI_ImagePageReportsServerErrors(t *testing.T) {
|
||||
body := sendImageSource(t)
|
||||
|
||||
// api() throws Error(j.error.message) on !r.ok, so appending e.message is
|
||||
// what puts the explanation on screen.
|
||||
if !strings.Contains(body, "e && e.message") {
|
||||
t.Error("sendImage() does not surface the server's error message; a " +
|
||||
"failure would show a bare status with no cause")
|
||||
}
|
||||
// And a success carrying no image must not look like a success.
|
||||
if !strings.Contains(body, "imgNoData") {
|
||||
t.Error("sendImage() does not warn when the upstream returns no image")
|
||||
}
|
||||
}
|
||||
|
||||
// sendImageSource returns the text of sendImage() only, so an assertion cannot
|
||||
// be satisfied by a string that happens to appear elsewhere in the file.
|
||||
func sendImageSource(t *testing.T) string {
|
||||
t.Helper()
|
||||
ui := uiSource(t)
|
||||
i := strings.Index(ui, "async function sendImage(")
|
||||
if i < 0 {
|
||||
t.Fatal("sendImage() not found")
|
||||
}
|
||||
body := ui[i:]
|
||||
if end := strings.Index(body[1:], "\n function "); end > 0 {
|
||||
body = body[:end+1]
|
||||
}
|
||||
return body
|
||||
}
|
||||
@ -126,6 +126,7 @@ func TestUIStatsShowsPeriodWindow(t *testing.T) {
|
||||
t.Fatalf("i18n key periodRange appears %d time(s), want both zh and en", n)
|
||||
}
|
||||
}
|
||||
|
||||
// Both canvases can exist in the DOM at once: the modal is mounted on top of
|
||||
// the settings tab, whose #scr-canvas stays alive behind it. Resolving the
|
||||
// canvas by document order (whichever comes first) painted the per-key chain
|
||||
|
||||
@ -396,15 +396,15 @@ func TestPluginSidebarEntryCarriesATitle(t *testing.T) {
|
||||
func TestPluginPageHasAccessEditor(t *testing.T) {
|
||||
src := uiSource(t)
|
||||
for _, needle := range []string{
|
||||
`renderPluginAccess`, // called after rendering the plugin list
|
||||
`savePluginAccess`, // wired to a button
|
||||
`/access`, // hits the per-endpoint access API
|
||||
`id="pl-access"`, // container exists
|
||||
`http_access`, // reads the table from /api/plugins
|
||||
`data-ep=`, // one input per endpoint
|
||||
`data-plvis=`, // one visibility selector per plugin
|
||||
`/http/*/access`, // whole-plugin visibility shares the access API
|
||||
`plVisCell`, // renders the declared-vs-override distinction
|
||||
`renderPluginAccess`, // called after rendering the plugin list
|
||||
`savePluginAccess`, // wired to a button
|
||||
`/access`, // hits the per-endpoint access API
|
||||
`id="pl-access"`, // container exists
|
||||
`http_access`, // reads the table from /api/plugins
|
||||
`data-ep=`, // one input per endpoint
|
||||
`data-plvis=`, // one visibility selector per plugin
|
||||
`/http/*/access`, // whole-plugin visibility shares the access API
|
||||
`plVisCell`, // renders the declared-vs-override distinction
|
||||
} {
|
||||
if !strings.Contains(src, needle) {
|
||||
t.Errorf("plugin page lacks %q", needle)
|
||||
|
||||
323
internal/lua/adapter_qwen38_test.go
Normal file
323
internal/lua/adapter_qwen38_test.go
Normal file
@ -0,0 +1,323 @@
|
||||
package lua
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// The fixtures below are REAL responses captured from
|
||||
// https://discovery-api.intern-ai.org.cn/v1 with qwen3.8-27b — not hand-written
|
||||
// approximations. Two of them exist only because the endpoint behaves in a way a
|
||||
// stock OpenAI adapter gets wrong, and neither is guessable from the schema.
|
||||
|
||||
// reasoningDominatesTheBudget is the fact that made a dedicated adapter
|
||||
// necessary: a one-sentence visual answer came back as completion=303 with
|
||||
// reasoning=295. 97% of the budget went to thinking and only 8 tokens reached
|
||||
// content. Handing the upstream's content straight through yields what looks
|
||||
// like an empty answer from a perfectly healthy request.
|
||||
const reasoningDominatesTheBudget = `{
|
||||
"choices": [{"index":0,"message":{"role":"assistant","content":"\n\n这张图是白色的。",
|
||||
"reasoning_content":"分析请求…分析图片…颜色是纯白"},"logprobs":null,
|
||||
"finish_reason":"stop"}],
|
||||
"created":1791179068,"model":"qwen3.8-27b-fp8","object":"chat.completion",
|
||||
"usage":{"prompt_tokens":125,"total_tokens":428,"completion_tokens":303,
|
||||
"prompt_tokens_details":null,
|
||||
"completion_tokens_details":{"reasoning_tokens":295}}
|
||||
}`
|
||||
|
||||
// truncatedReasoningOnly is what a small max_tokens produces: HTTP 200, content
|
||||
// null, finish_reason "length". It is NOT an upstream failure.
|
||||
const truncatedReasoningOnly = `{
|
||||
"choices": [{"index":0,"message":{"role":"assistant","content":null,
|
||||
"reasoning_content":"思考过程:1. **分析请求**…图片的颜色是纯","refusal":null},
|
||||
"finish_reason":"length"}],
|
||||
"model":"qwen3.8-27b-fp8","object":"chat.completion",
|
||||
"usage":{"prompt_tokens":127,"total_tokens":190,"completion_tokens":63}
|
||||
}`
|
||||
|
||||
const qwen38ToolCallResponse = `{
|
||||
"choices": [{"index":0,"message":{"role":"assistant","content":"",
|
||||
"reasoning_content":"用户询问北京天气。我需要调用 get_weather 函数,参数为城市 \"北京\"。",
|
||||
"tool_calls":[{"id":"chatcmpl-tool-afc0299b5810e044","type":"function",
|
||||
"function":{"name":"get_weather","arguments":"{\"city\": \"北京\"}"}}]},
|
||||
"finish_reason":"tool_calls"}],
|
||||
"model":"qwen3.8-27b-fp8",
|
||||
"usage":{"prompt_tokens":305,"total_tokens":353,"completion_tokens":48,
|
||||
"completion_tokens_details":{"reasoning_tokens":20}}
|
||||
}`
|
||||
|
||||
func qwen38VM(t *testing.T) *VM {
|
||||
t.Helper()
|
||||
vm := NewVM(freshAdapterDir(t))
|
||||
if err := vm.Start(); err != nil {
|
||||
t.Fatalf("adapter VM: %v", err)
|
||||
}
|
||||
t.Cleanup(vm.Stop)
|
||||
return vm
|
||||
}
|
||||
|
||||
func qwen38Response(t *testing.T, vm *VM, raw string) map[string]interface{} {
|
||||
t.Helper()
|
||||
out, err := vm.Transform("qwen38", "transform_response", raw)
|
||||
if err != nil {
|
||||
t.Fatalf("transform_response: %v", err)
|
||||
}
|
||||
var m map[string]interface{}
|
||||
if err := json.Unmarshal([]byte(out), &m); err != nil {
|
||||
t.Fatalf("adapter returned non-JSON: %v\n%s", err, out)
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func qwen38Stream(t *testing.T, vm *VM, raw string) map[string]interface{} {
|
||||
t.Helper()
|
||||
out, err := vm.Transform("qwen38", "transform_stream_chunk", raw)
|
||||
if err != nil {
|
||||
t.Fatalf("transform_stream_chunk: %v", err)
|
||||
}
|
||||
var m map[string]interface{}
|
||||
if err := json.Unmarshal([]byte(out), &m); err != nil {
|
||||
t.Fatalf("adapter returned non-JSON: %v\n%s", err, out)
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// TestQwen38KeepsReasoningSeparate is the adapter's reason for existing.
|
||||
// Folding reasoning into content would render the model's private deliberation as
|
||||
// its answer; dropping it makes a reasoning model look like it returned nothing.
|
||||
func TestQwen38KeepsReasoningSeparate(t *testing.T) {
|
||||
vm := qwen38VM(t)
|
||||
u := qwen38Response(t, vm, reasoningDominatesTheBudget)
|
||||
|
||||
content, _ := u["content"].(string)
|
||||
reasoning, _ := u["reasoning_content"].(string)
|
||||
|
||||
if reasoning == "" {
|
||||
t.Fatal("reasoning_content was dropped — a reasoning model then looks like it returned nothing")
|
||||
}
|
||||
if strings.Contains(content, "分析请求") {
|
||||
t.Errorf("reasoning was folded into content: %q", content)
|
||||
}
|
||||
if !strings.Contains(content, "白色的") {
|
||||
t.Errorf("content = %q, want the answer text", content)
|
||||
}
|
||||
if reasoning == content {
|
||||
t.Error("content and reasoning_content are identical; they are disjoint upstream")
|
||||
}
|
||||
}
|
||||
|
||||
// TestQwen38ReportsTruncationRatherThanFailing: content=null with
|
||||
// finish_reason="length" is a truncated reasoning response, not a broken
|
||||
// upstream. Losing the finish_reason makes it indistinguishable from a failure,
|
||||
// and losing the partial reasoning throws away the only evidence of what happened.
|
||||
func TestQwen38ReportsTruncationRatherThanFailing(t *testing.T) {
|
||||
vm := qwen38VM(t)
|
||||
u := qwen38Response(t, vm, truncatedReasoningOnly)
|
||||
|
||||
if u["finish_reason"] != "length" {
|
||||
t.Errorf("finish_reason = %v, want length", u["finish_reason"])
|
||||
}
|
||||
if reasoning, _ := u["reasoning_content"].(string); reasoning == "" {
|
||||
t.Error("the partial reasoning was discarded")
|
||||
}
|
||||
if content, _ := u["content"].(string); content != "" {
|
||||
t.Errorf("content = %q, want empty string for a null upstream content", content)
|
||||
}
|
||||
}
|
||||
|
||||
// TestQwen38CarriesReasoningTokensWithoutInflatingTotals: reasoning_tokens is a
|
||||
// SUBSET of completion_tokens. Adding them double-counts every reasoning request,
|
||||
// and billing prices the pair separately.
|
||||
func TestQwen38CarriesReasoningTokensWithoutInflatingTotals(t *testing.T) {
|
||||
vm := qwen38VM(t)
|
||||
u := qwen38Response(t, vm, reasoningDominatesTheBudget)
|
||||
|
||||
tu, ok := u["token_usage"].(map[string]interface{})
|
||||
if !ok {
|
||||
t.Fatalf("token_usage = %#v", u["token_usage"])
|
||||
}
|
||||
completion, _ := tu["completion"].(float64)
|
||||
total, _ := tu["total"].(float64)
|
||||
|
||||
// The NESTING is the point, not the presence of the field.
|
||||
//
|
||||
// internal/gateway/chat.go reads reasoning in exactly one place:
|
||||
// resp.TokenUsage.CompletionTokensDetails.ReasoningTokens
|
||||
// An adapter that emits reasoning_tokens at the TOP of token_usage type-checks
|
||||
// fine and is then dropped at the one spot that could have kept it — measured
|
||||
// on the live gateway: the model reported completion=88 and the gateway's
|
||||
// response carried reasoning=null.
|
||||
//
|
||||
// So this asserts the path, not merely that a number appears somewhere.
|
||||
det, ok := tu["completion_tokens_details"].(map[string]interface{})
|
||||
if !ok {
|
||||
t.Fatalf("token_usage has no completion_tokens_details: %#v — chat.go reads "+
|
||||
"reasoning from there and nowhere else, so the number is discarded", tu)
|
||||
}
|
||||
reasoning, has := det["reasoning_tokens"].(float64)
|
||||
if !has {
|
||||
t.Fatal("completion_tokens_details.reasoning_tokens missing — billing cannot price reasoning separately")
|
||||
}
|
||||
// Relational, not a presence check. The first version of this test only
|
||||
// asserted the field existed, and a mutation setting it to 0 passed: 0 is
|
||||
// present. The upstream value is known (295), so the exact number is
|
||||
// asserted — a zero silently misprices every reasoning request.
|
||||
if reasoning != 295 {
|
||||
t.Errorf("reasoning_tokens = %v, want 295 (the upstream value)", reasoning)
|
||||
}
|
||||
if reasoning >= completion {
|
||||
t.Errorf("reasoning_tokens (%v) must be a SUBSET of completion_tokens (%v)", reasoning, completion)
|
||||
}
|
||||
if completion != 303 || total != 428 {
|
||||
t.Errorf("usage was altered: completion=%v total=%v, want 303/428", completion, total)
|
||||
}
|
||||
}
|
||||
|
||||
// TestQwen38DecodesToolCallArguments: the upstream sends arguments as a JSON
|
||||
// STRING, like every other OpenAI-compatible endpoint. Passing it through
|
||||
// undecoded leaves the caller to parse it.
|
||||
func TestQwen38DecodesToolCallArguments(t *testing.T) {
|
||||
vm := qwen38VM(t)
|
||||
u := qwen38Response(t, vm, qwen38ToolCallResponse)
|
||||
|
||||
tcs, ok := u["tool_calls"].([]interface{})
|
||||
if !ok || len(tcs) != 1 {
|
||||
t.Fatalf("tool_calls = %#v", u["tool_calls"])
|
||||
}
|
||||
tc, _ := tcs[0].(map[string]interface{})
|
||||
if tc["name"] != "get_weather" {
|
||||
t.Errorf("name = %v", tc["name"])
|
||||
}
|
||||
args, ok := tc["arguments"].(map[string]interface{})
|
||||
if !ok {
|
||||
t.Fatalf("arguments = %#v — the upstream sends a JSON string and it must be decoded", tc["arguments"])
|
||||
}
|
||||
if args["city"] != "北京" {
|
||||
t.Errorf("arguments.city = %v", args["city"])
|
||||
}
|
||||
}
|
||||
|
||||
// TestQwen38StreamKeepsReasoningSeparate: measured 20 reasoning chunks against 1
|
||||
// content chunk on a one-sentence answer. Folding them would make the client
|
||||
// render the deliberation as the reply.
|
||||
func TestQwen38StreamKeepsReasoningSeparate(t *testing.T) {
|
||||
vm := qwen38VM(t)
|
||||
const reasoningChunk = `{"choices":[{"index":0,"delta":{"reasoning_content":"思考过程:1. **分析请求**"},
|
||||
"finish_reason":null}],"model":"qwen3.8-27b-fp8"}`
|
||||
const contentChunk = `{"choices":[{"index":0,"delta":{"content":"递归就是函数调用自身"},
|
||||
"finish_reason":null}],"model":"qwen3.8-27b-fp8"}`
|
||||
const usageChunk = `{"choices":[],"usage":{"prompt_tokens":64,"total_tokens":132,
|
||||
"completion_tokens":68,"completion_tokens_details":{"reasoning_tokens":62}}}`
|
||||
|
||||
a := qwen38Stream(t, vm, reasoningChunk)
|
||||
b := qwen38Stream(t, vm, contentChunk)
|
||||
c := qwen38Stream(t, vm, usageChunk)
|
||||
|
||||
if content, mixed := a["content"].(string); mixed && content != "" {
|
||||
t.Errorf("a reasoning chunk leaked into content: %q", content)
|
||||
}
|
||||
if reasoning, _ := a["reasoning_content"].(string); !strings.Contains(reasoning, "思考过程") {
|
||||
t.Errorf("reasoning chunk lost its text: %#v", a)
|
||||
}
|
||||
if content, _ := b["content"].(string); !strings.Contains(content, "递归") {
|
||||
t.Errorf("content chunk lost its text: %#v", b)
|
||||
}
|
||||
if c["usage"] == nil {
|
||||
t.Errorf("the terminal usage chunk was dropped: %#v", c)
|
||||
return
|
||||
}
|
||||
u, _ := c["usage"].(map[string]interface{})
|
||||
det, _ := u["completion_tokens_details"].(map[string]interface{})
|
||||
if det == nil || det["reasoning_tokens"] == nil {
|
||||
t.Errorf("usage chunk did not nest reasoning under completion_tokens_details: %#v — "+
|
||||
"chat.go reads it there, so a top-level field is discarded", u)
|
||||
}
|
||||
}
|
||||
|
||||
// TestQwen38ReportsMediaFailureCode: a remote image URL times out upstream and
|
||||
// comes back as media_fetch_failed. Surfacing only the message makes an image
|
||||
// fetch problem look like a model problem.
|
||||
func TestQwen38ReportsMediaFailureCode(t *testing.T) {
|
||||
vm := qwen38VM(t)
|
||||
body := `{"error":{"message":"image fetch timed out","type":"media_fetch_failed",
|
||||
"param":null,"code":"media_fetch_failed","trace_id":"f3ca4aa"}}`
|
||||
reason, ok, err := vm.TransformError("qwen38", 400, body)
|
||||
if err != nil {
|
||||
t.Fatalf("TransformError: %v", err)
|
||||
}
|
||||
if !ok {
|
||||
t.Fatal("the media failure was swallowed")
|
||||
}
|
||||
if !strings.Contains(reason, "image fetch timed out") || !strings.Contains(reason, "media_fetch_failed") {
|
||||
t.Errorf("reason = %q; it must carry both the text and the code", reason)
|
||||
}
|
||||
}
|
||||
|
||||
// TestQwen38InvalidKeyError: the endpoint answers a bad key with 401 and
|
||||
// code "invalid_api_key". That one must NOT be annotated with its own code — it
|
||||
// would render as "invalid API key (invalid_api_key)".
|
||||
func TestQwen38InvalidKeyError(t *testing.T) {
|
||||
vm := qwen38VM(t)
|
||||
body := `{"error":{"message":"invalid API key","type":"invalid_request_error",
|
||||
"param":null,"code":"invalid_api_key"}}`
|
||||
reason, ok, err := vm.TransformError("qwen38", 401, body)
|
||||
if err != nil || !ok {
|
||||
t.Fatalf("TransformError: ok=%v err=%v", ok, err)
|
||||
}
|
||||
if reason != "invalid API key" {
|
||||
t.Errorf("reason = %q, want the bare message", reason)
|
||||
}
|
||||
}
|
||||
|
||||
// TestQwen38SuppliesReasoningOnToolCallTurns: the upstream requires an assistant
|
||||
// message carrying tool_calls to echo reasoning_content. OpenAI-shaped clients
|
||||
// never send it, so without this the SECOND turn of any tool-using conversation
|
||||
// is rejected.
|
||||
func TestQwen38SuppliesReasoningOnToolCallTurns(t *testing.T) {
|
||||
vm := qwen38VM(t)
|
||||
const req = `{"model":"qwen3.8-27b","messages":[
|
||||
{"role":"user","content":"北京天气"},
|
||||
{"role":"assistant","content":"","tool_calls":[{"id":"c1","type":"function",
|
||||
"function":{"name":"get_weather","arguments":"{\"city\":\"北京\"}"}}]},
|
||||
{"role":"tool","tool_call_id":"c1","content":"晴 25C"}]}`
|
||||
out, err := vm.Transform("qwen38", "transform_request", req)
|
||||
if err != nil {
|
||||
t.Fatalf("transform_request: %v", err)
|
||||
}
|
||||
var decoded map[string]interface{}
|
||||
if err := json.Unmarshal([]byte(out), &decoded); err != nil {
|
||||
t.Fatalf("request transform did not return JSON: %v\n%s", err, out)
|
||||
}
|
||||
msgs, _ := decoded["messages"].([]interface{})
|
||||
if len(msgs) < 2 {
|
||||
t.Fatalf("messages = %#v", decoded["messages"])
|
||||
}
|
||||
assistant, _ := msgs[1].(map[string]interface{})
|
||||
if _, ok := assistant["reasoning_content"]; !ok {
|
||||
t.Errorf("assistant turn with tool_calls has no reasoning_content; the upstream "+
|
||||
"rejects the next turn. Got %#v", assistant)
|
||||
}
|
||||
}
|
||||
|
||||
// TestQwen38PassesImagesThrough: this adapter's job is reasoning handling, not
|
||||
// media translation. A multimodal content array must survive untouched — an
|
||||
// adapter that re-encodes it is the kind of change that silently breaks images
|
||||
// while every text test still passes.
|
||||
func TestQwen38PassesImagesThrough(t *testing.T) {
|
||||
vm := qwen38VM(t)
|
||||
const req = `{"model":"qwen3.8-27b","messages":[{"role":"user","content":[
|
||||
{"type":"text","text":"这张图是什么颜色?"},
|
||||
{"type":"image_url","image_url":{"url":"data:image/png;base64,iVBORw0KGgo="}}]}],
|
||||
"max_tokens":600}`
|
||||
out, err := vm.Transform("qwen38", "transform_request", req)
|
||||
if err != nil {
|
||||
t.Fatalf("transform_request: %v", err)
|
||||
}
|
||||
if !strings.Contains(out, "image_url") || !strings.Contains(out, "data:image/png;base64,") {
|
||||
t.Errorf("the image part was lost or rewritten:\n%s", out)
|
||||
}
|
||||
if strings.Contains(out, "base64,iVBORw0KGgo=") == false {
|
||||
t.Errorf("the base64 payload was altered:\n%s", out)
|
||||
}
|
||||
}
|
||||
94
internal/lua/adapter_two_directions_test.go
Normal file
94
internal/lua/adapter_two_directions_test.go
Normal file
@ -0,0 +1,94 @@
|
||||
package lua
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestEveryBundledAdapterTwoDirections 逐个验证每个内置适配器在**两个方向**上
|
||||
// 的声明与能力一致,且都能在真实 VM 上被调用而不报错。这是"重构后两个方向
|
||||
// 都没坏"的批量断言(用户要求:16 个适配器逐个验证两个方向)。
|
||||
//
|
||||
// 两个方向:
|
||||
//
|
||||
// 出向(egress):serialize_* 钩子 —— 统一格式 → 客户端协议。
|
||||
// 一个适配器要么声明了 serialize_*(接管),要么依赖内核 OpenAI 兜底。
|
||||
// 两种都合法;不合法的是"声明了钩子但调用即报错"。
|
||||
//
|
||||
// 入向(ingress):adapter.ingress + deserialize_request —— 客户端协议 → 统一格式。
|
||||
// 声明 ingress 的适配器必须同时实现 deserialize_request,否则内核会让出给
|
||||
// 一个空解析器(核心里 parseViaIngress 已挡住,此处再在适配器数据面钉一次)。
|
||||
//
|
||||
// 上游向(upstream):transform_request —— 统一格式 → 上游协议。
|
||||
// 这是所有适配器的本职,必须存在且可调用。
|
||||
func TestEveryBundledAdapterTwoDirections(t *testing.T) {
|
||||
vm := NewVM(filepath.Join(t.TempDir(), "adapters"))
|
||||
if err := vm.Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer vm.Stop()
|
||||
|
||||
adapters := vm.ListAdapters()
|
||||
if len(adapters) == 0 {
|
||||
t.Fatal("no adapters loaded")
|
||||
}
|
||||
|
||||
// 一个最小、协议中立的统一请求,供上游向 transform_request 使用。
|
||||
unifiedReq := `{"model":"m","messages":[{"role":"user","content":"hi"}],"max_tokens":16,"stream":false}`
|
||||
|
||||
for _, a := range adapters {
|
||||
a := a
|
||||
t.Run(a.Name, func(t *testing.T) {
|
||||
// ---- 上游向:transform_request 必须存在且可调用 ----
|
||||
if !vm.HasFn(a.Name, "transform_request") {
|
||||
t.Errorf("%s: 缺 transform_request(上游向本职)", a.Name)
|
||||
} else if _, err := vm.Transform(a.Name, "transform_request", unifiedReq); err != nil {
|
||||
t.Errorf("%s: transform_request 调用失败: %v", a.Name, err)
|
||||
}
|
||||
|
||||
// ---- 出向:serialize_* 声明与调用一致性 ----
|
||||
declaredEgress := false
|
||||
for _, fn := range []string{"serialize_response", "serialize_chunk", "serialize_open_preamble", "serialize_close", "serialize_done_marker"} {
|
||||
if vm.HasFn(a.Name, fn) {
|
||||
declaredEgress = true
|
||||
}
|
||||
}
|
||||
if vm.HasFn(a.Name, "serialize_response") {
|
||||
// 统一结果形状(见 types.UnifiedResult)
|
||||
res := `{"model":"m","id":"x","content":"hello","finish_reason":"stop","token_usage":{"prompt":1,"completion":1,"total":2}}`
|
||||
out, err := vm.Transform(a.Name, "serialize_response", res)
|
||||
if err != nil {
|
||||
t.Errorf("%s: serialize_response 调用失败: %v", a.Name, err)
|
||||
} else if strings.TrimSpace(out) == "" {
|
||||
t.Errorf("%s: serialize_response 返回空", a.Name)
|
||||
} else if !json.Valid([]byte(out)) && !strings.HasPrefix(strings.TrimSpace(out), "event:") {
|
||||
t.Errorf("%s: serialize_response 既非 JSON 也非 SSE: %q", a.Name, out)
|
||||
}
|
||||
}
|
||||
if declaredEgress {
|
||||
t.Logf("%s: 出向由适配器接管", a.Name)
|
||||
} else {
|
||||
t.Logf("%s: 出向走内核 OpenAI 兜底(未声明 serialize_*)", a.Name)
|
||||
}
|
||||
|
||||
// ---- 入向:声明 ingress 必须配 deserialize_request ----
|
||||
ingress := vm.AdapterIngress(a.Name)
|
||||
hasDeser := vm.HasFn(a.Name, "deserialize_request")
|
||||
if ingress != "" {
|
||||
if !hasDeser {
|
||||
t.Errorf("%s: 声明 ingress=%q 但没有 deserialize_request —— 内核会让出给空解析器", a.Name, ingress)
|
||||
} else {
|
||||
// 用该适配器自己服务的那个端点的典型 body 调一次,确认不报错。
|
||||
if _, err := vm.Transform(a.Name, "deserialize_request", `{"model":"m","max_tokens":8,"messages":[{"role":"user","content":"hi"}]}`); err != nil {
|
||||
t.Errorf("%s: deserialize_request 调用失败: %v", a.Name, err)
|
||||
}
|
||||
t.Logf("%s: 入向认领 %s", a.Name, ingress)
|
||||
}
|
||||
} else if hasDeser {
|
||||
t.Errorf("%s: 实现了 deserialize_request 却没声明 ingress —— 内核永远不会调用它", a.Name)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
133
internal/lua/adapter_validate.go
Normal file
133
internal/lua/adapter_validate.go
Normal file
@ -0,0 +1,133 @@
|
||||
// Package lua — 适配器导入安全校验(R6)
|
||||
//
|
||||
// 用户要求(feature/unified-wire-v2):导入适配器时必须检查路径冲突,
|
||||
// 避免静默错误。
|
||||
//
|
||||
// 之前的风险:
|
||||
//
|
||||
// UploadAdapter(name, code) 直接拼 adapter_dir/<name>.lua。
|
||||
// name 含 "../" 或绝对路径会写到目录外;
|
||||
// name 与内置适配器同名(openai/anthropic/gemini…)会静默覆盖——
|
||||
// 加载失败只报 "adapter X missing",错误现场与病因分离。
|
||||
//
|
||||
// 这里的校验在**所有导入入口**统一执行(LoadAdapterSource / UploadAdapter /
|
||||
// 插件 install_adapter),让"为什么被拒绝"成为明确的错误信息。
|
||||
package lua
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// AdapterNamePattern 是适配器名的合法字符集。
|
||||
// 不允许:路径分隔符(/、\)、点点(..)、绝对路径、空格。
|
||||
// 允许:字母、数字、下划线、连字符。
|
||||
const AdapterNamePattern = `^[a-zA-Z0-9_-]+$`
|
||||
|
||||
// ErrAdapterNameInvalid 适配器名不合法。
|
||||
type ErrAdapterNameInvalid struct{ Name, Reason string }
|
||||
|
||||
func (e *ErrAdapterNameInvalid) Error() string {
|
||||
return fmt.Sprintf("adapter name %q invalid: %s", e.Name, e.Reason)
|
||||
}
|
||||
|
||||
// ErrAdapterNameReserved 适配器名与内核内置适配器冲突(用户要求:冲突时
|
||||
// 内核内置优先,外部不得覆盖)。
|
||||
type ErrAdapterNameReserved struct{ Name string }
|
||||
|
||||
func (e *ErrAdapterNameReserved) Error() string {
|
||||
return fmt.Sprintf("adapter name %q is reserved by a built-in adapter; renaming or deleting the built-in is not supported", e.Name)
|
||||
}
|
||||
|
||||
// ValidateAdapterName 校验一个将被导入的适配器名。
|
||||
//
|
||||
// 它不做文件系统存在性检查(那是上传路径的责任),只校验"这个名字作为
|
||||
// adapter_dir/<name>.lua 的一部分是否安全"。校验维度:
|
||||
//
|
||||
// 1. 非空、符合字符集(拒绝路径穿越、绝对路径、空白)
|
||||
// 2. 不与内核内置适配器名冲突(除非 force 覆盖内置,但这通常不该被允许)
|
||||
// 3. 不含保留路径段(.、..、CON、NUL 等)
|
||||
//
|
||||
// reservedNames 是调用方提供的"保留名集合"——通常是内置适配器名表。
|
||||
// 传 nil 表示无保留名(仍校验字符集与路径安全)。
|
||||
func ValidateAdapterName(name string, reservedNames []string) error {
|
||||
if name == "" {
|
||||
return &ErrAdapterNameInvalid{Name: name, Reason: "empty"}
|
||||
}
|
||||
// 字符集:最简单的拒绝路径风险的方式。
|
||||
if !matchesAdapterPattern(name) {
|
||||
return &ErrAdapterNameInvalid{Name: name, Reason: "must match " + AdapterNamePattern + " (no slashes, dots-only, absolute paths, or whitespace)"}
|
||||
}
|
||||
// 保留段:即使字符集通过,也要点名拒绝 . 和 .. (它们会被某些字符集放行,
|
||||
// 但绝不能成为文件名)。
|
||||
switch name {
|
||||
case ".", "..":
|
||||
return &ErrAdapterNameInvalid{Name: name, Reason: "reserved path segment"}
|
||||
}
|
||||
for _, reserved := range reservedNames {
|
||||
if strings.EqualFold(name, reserved) {
|
||||
return &ErrAdapterNameReserved{Name: name}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidateAdapterPath 在 LoadAdapter(path) 入口校验磁盘路径是否指向
|
||||
// adapter_dir 下、且文件名合法。
|
||||
//
|
||||
// 这一层是"从磁盘加载"的安全网:Start() 用它加载目录里的 .lua,必须保证
|
||||
// 一个被放进来的文件(无论谁放的)只能注册成它自己的名字,不能因为文件名
|
||||
// 怪异(如 ../openai.lua)就覆盖或越界。
|
||||
//
|
||||
// 返回校验后的 (name, error)。
|
||||
func ValidateAdapterPath(path, adapterDir string, reservedNames []string) (string, error) {
|
||||
// 先解析成绝对路径,与 adapter_dir 比对,确认文件确实在目录内
|
||||
// (不在目录内的 LoadAdapter 调用本就是异常用法,但 Start() 遍历的是
|
||||
// ReadDir 的结果,天然在目录内,这条校验对它是 no-op)。
|
||||
abs, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
return "", &ErrAdapterNameInvalid{Name: path, Reason: "unresolvable path"}
|
||||
}
|
||||
dirAbs, _ := filepath.Abs(adapterDir)
|
||||
if !isWithinDir(abs, dirAbs) {
|
||||
return "", &ErrAdapterNameInvalid{Name: path, Reason: "file is outside adapter_dir"}
|
||||
}
|
||||
name := strings.TrimSuffix(filepath.Base(path), filepath.Ext(path))
|
||||
if err := ValidateAdapterName(name, reservedNames); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return name, nil
|
||||
}
|
||||
|
||||
// matchesAdapterPattern 是 AdapterNamePattern 的纯函数实现(避免在 hot path
|
||||
// 里编译正则)。
|
||||
func matchesAdapterPattern(s string) bool {
|
||||
if s == "" {
|
||||
return false
|
||||
}
|
||||
for _, r := range s {
|
||||
ok := (r >= 'a' && r <= 'z') ||
|
||||
(r >= 'A' && r <= 'Z') ||
|
||||
(r >= '0' && r <= '9') ||
|
||||
r == '_' || r == '-'
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// isWithinDir 判断 path 是否在 dir 内(含 path == dir 本身,不含 .. 逃逸)。
|
||||
func isWithinDir(path, dir string) bool {
|
||||
rel, err := filepath.Rel(dir, path)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return rel != ".." && !strings.HasPrefix(rel, ".."+string(os.PathSeparator)) && rel != "."
|
||||
}
|
||||
|
||||
// 哑引用:让 errors 包在 go vet 下不报"导出但未使用"。
|
||||
var _ = errors.New
|
||||
197
internal/lua/adapter_validate_test.go
Normal file
197
internal/lua/adapter_validate_test.go
Normal file
@ -0,0 +1,197 @@
|
||||
package lua
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// 本文件锁定 R6:导入适配器时检查路径冲突,避免静默错误。
|
||||
//
|
||||
// 覆盖三类注入:
|
||||
// 1. 名字字符集(路径穿越 / 绝对路径 / 空白)→ 拒绝
|
||||
// 2. 保留名冲突(内置适配器)→ 拒绝
|
||||
// 3. 合法名 → 通过(且确实可加载)
|
||||
|
||||
func TestValidateAdapterNameRejectsPathTraversal(t *testing.T) {
|
||||
for _, name := range []string{
|
||||
"../openai", // 目录逃逸
|
||||
"..", // 保留段
|
||||
".", // 保留段
|
||||
"/etc/passwd", // 绝对路径
|
||||
"a/b/c", // 斜杠
|
||||
"a\\b", // 反斜杠
|
||||
"has space", // 空白
|
||||
"a b c", // 空格
|
||||
"", // 空
|
||||
} {
|
||||
if err := ValidateAdapterName(name, nil); err == nil {
|
||||
t.Errorf("名字 %q 应被拒绝(路径不安全),但通过了", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateAdapterNameRejectsReserved(t *testing.T) {
|
||||
reserved := []string{"openai", "anthropic", "gemini", "Ollama"}
|
||||
for _, name := range []string{"openai", "anthropic", "gemini", "ollama"} {
|
||||
if err := ValidateAdapterName(name, reserved); err == nil {
|
||||
t.Errorf("名字 %q 与内置适配器冲突,应被拒绝", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateAdapterNameAcceptsLegal(t *testing.T) {
|
||||
for _, name := range []string{
|
||||
"my_adapter",
|
||||
"my-adapter",
|
||||
"custom123",
|
||||
"a",
|
||||
"opencode", // 不是保留名(本次已从内置移除)
|
||||
} {
|
||||
if err := ValidateAdapterName(name, []string{"openai", "anthropic"}); err != nil {
|
||||
t.Errorf("合法名字 %q 被误拒: %v", name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestValidateAdapterPathRejectsOutsideDir 验证从磁盘加载路径时,
|
||||
// 目录外的文件被拒绝。
|
||||
func TestValidateAdapterPathRejectsOutsideDir(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
outside := filepath.Join(dir, "..", "evil.lua")
|
||||
if _, err := ValidateAdapterPath(outside, dir, nil); err == nil {
|
||||
t.Errorf("目录外的路径 %q 应被拒绝", outside)
|
||||
}
|
||||
}
|
||||
|
||||
// TestLoadAdapterSourceAllowsBuiltinOverride 验证运维覆盖内置适配器是允许的
|
||||
// (文档化的 adapter_dir 热覆盖机制)——仅插件被禁止影子内置。
|
||||
func TestLoadAdapterSourceAllowsBuiltinOverride(t *testing.T) {
|
||||
vm := NewVM(filepath.Join(t.TempDir(), "adapters"))
|
||||
if err := vm.Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer vm.Stop()
|
||||
|
||||
code := `local a={} a.name="openai" a.version="override" a.endpoint="/x" return a`
|
||||
if err := vm.LoadAdapterSource("openai", code); err != nil {
|
||||
t.Errorf("运维覆盖内置 openai 应被允许(热覆盖机制),却被拒: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestInstallPluginAdapterRejectsBuiltin 验证**插件**装内置名被拒(防篡改)。
|
||||
func TestInstallPluginAdapterRejectsBuiltin(t *testing.T) {
|
||||
vm := NewVM(filepath.Join(t.TempDir(), "adapters"))
|
||||
if err := vm.Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer vm.Stop()
|
||||
|
||||
code := `local a={} a.name="openai" a.version="evil" a.endpoint="/x" return a`
|
||||
if err := vm.InstallPluginAdapter("openai", code); err == nil {
|
||||
t.Error("插件装与内置同名的适配器应被拒(防篡改发行物)")
|
||||
}
|
||||
// 但插件装非内置名应成功
|
||||
ok := `local a={} a.name="myplugin_adapter" a.version="1" a.endpoint="/x" return a`
|
||||
if err := vm.InstallPluginAdapter("myplugin_adapter", ok); err != nil {
|
||||
t.Errorf("插件装非保留名应成功: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadAdapterSourceRejectsReserved(t *testing.T) {
|
||||
t.Skip("已改:LoadAdapterSource 允许运维覆盖内置;保留名禁止已移至 InstallPluginAdapter")
|
||||
}
|
||||
|
||||
// TestLoadAdapterSourceRejectsTraversal 验证加载入口拒绝路径穿越名。
|
||||
func TestLoadAdapterSourceRejectsTraversal(t *testing.T) {
|
||||
vm := NewVM(filepath.Join(t.TempDir(), "adapters"))
|
||||
if err := vm.Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer vm.Stop()
|
||||
|
||||
code := `local a={} a.name="x" a.version="1" a.endpoint="/x" return a`
|
||||
if err := vm.LoadAdapterSource("../evil", code); err == nil {
|
||||
t.Error("路径穿越名 ../evil 应被拒绝")
|
||||
}
|
||||
}
|
||||
|
||||
// TestUploadAdapterPathTraversalDoesNotEscape 是端到端安全测试:
|
||||
// 模拟管理员通过 API 上传一个恶意名,验证它**不会**写到 adapter_dir 之外。
|
||||
func TestUploadAdapterPathTraversalDoesNotEscape(t *testing.T) {
|
||||
// 用一个真实 Core 来测 UploadAdapter,但 core 需要完整配置——这里直接
|
||||
// 测 UploadAdapter 的校验层(名字在写文件之前就被拒)。
|
||||
// 由于 UploadAdapter 在 core 包,我们在 lua 层能测的等价面是
|
||||
// ValidateAdapterName + LoadAdapterSource,上面已覆盖。
|
||||
// 这里补一个"确无副作用"断言:遍历名不得创建任何文件。
|
||||
dir := filepath.Join(t.TempDir(), "adapters")
|
||||
if err := os.MkdirAll(dir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := ValidateAdapterName("../openai", nil); err == nil {
|
||||
t.Fatal("前提不成立")
|
||||
}
|
||||
// 证明没有 ../openai.lua 被创建
|
||||
if _, err := os.Stat(filepath.Join(dir, "..", "openai.lua")); !os.IsNotExist(err) {
|
||||
t.Error("校验前就写出了逃逸文件 —— 写入顺序错误")
|
||||
}
|
||||
}
|
||||
|
||||
// TestLoadAdapterValidatesTheDiskPath 验证从磁盘加载(Start 的路径)对路径
|
||||
// 做安全校验:目录外的文件被拒。(目录内同名内置的文件是合法覆盖,
|
||||
// 不在此拒绝——见 TestLoadAdapterSourceAllowsBuiltinOverride。)
|
||||
func TestLoadAdapterValidatesTheDiskPath(t *testing.T) {
|
||||
dir := filepath.Join(t.TempDir(), "adapters")
|
||||
if err := os.MkdirAll(dir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// 目录外的文件:应被拒
|
||||
outside := filepath.Join(dir, "..", "evil.lua")
|
||||
if err := os.WriteFile(outside, []byte(`local a={} a.name="evil" a.version="1" a.endpoint="/x" return a`), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
vm := NewVM(dir)
|
||||
if err := vm.LoadAdapter(outside); err == nil {
|
||||
t.Error("目录外的磁盘文件应被拒绝")
|
||||
}
|
||||
}
|
||||
|
||||
// TestValidateAdapterPathInDirAccepted 验证目录内的合法文件通过。
|
||||
func TestValidateAdapterPathInDirAccepted(t *testing.T) {
|
||||
dir := filepath.Join(t.TempDir(), "adapters")
|
||||
if err := os.MkdirAll(dir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ok := filepath.Join(dir, "my_new.lua")
|
||||
if err := os.WriteFile(ok, []byte(`local a={} a.name="my_new" a.version="1" a.endpoint="/x" return a`), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
name, err := ValidateAdapterPath(ok, dir, []string{"openai"})
|
||||
if err != nil {
|
||||
t.Fatalf("合法文件被误拒: %v", err)
|
||||
}
|
||||
if name != "my_new" {
|
||||
t.Errorf("导出名 = %q, want my_new", name)
|
||||
}
|
||||
}
|
||||
|
||||
// TestErrorNamesTheConflict 验证错误信息点名冲突对象(用户要求"避免静默
|
||||
// 错误"——错误必须可定位)。
|
||||
func TestErrorNamesTheConflict(t *testing.T) {
|
||||
err := ValidateAdapterName("openai", []string{"openai"})
|
||||
if err == nil {
|
||||
t.Fatal("应拒绝")
|
||||
}
|
||||
var reserved *ErrAdapterNameReserved
|
||||
if !errors.As(err, &reserved) {
|
||||
t.Fatalf("错误类型应为 ErrAdapterNameReserved, got %T", err)
|
||||
}
|
||||
if !strings.Contains(err.Error(), "openai") {
|
||||
t.Errorf("错误信息应点名冲突对象: %v", err)
|
||||
}
|
||||
if !strings.Contains(err.Error(), "built-in") {
|
||||
t.Errorf("错误信息应说明冲突性质: %v", err)
|
||||
}
|
||||
}
|
||||
@ -1,212 +0,0 @@
|
||||
local adapter = {}
|
||||
|
||||
adapter.name = "agentrouter"
|
||||
adapter.version = "1.0.0"
|
||||
adapter.endpoint = "/chat/completions"
|
||||
adapter.headers = {}
|
||||
|
||||
-- AgentRouter 的阿里云 WAF 按三重维度校验:出口 IP(必须海外)、
|
||||
-- TLS 指纹(JS/Go HTTP 层与 curl 不同)、浏览器指纹 header 集。
|
||||
-- 纯 UA 已不够:WAF 拦 12 种 agent CLI UA 中的 10 种(全 405 HTML),
|
||||
-- 只有同时带 Origin: qwen.ai + Referer + X-Requested-With 才放行,
|
||||
-- 且同 IP 短时间连续 3+ 次请求会被速率封禁,靠 adapter 侧低频调用 +
|
||||
-- llmsproxy 的 prefFailStep 冷却自然限制,来源无法控制速率。
|
||||
local default_ua = "QwenCode/0.2.0 (linux; x64)"
|
||||
|
||||
-- AgentRouter fronts Claude models (claude-opus-4-8), and Claude upstreams
|
||||
-- enforce Anthropic's tool id rule ^[a-zA-Z0-9_-]{1,64}$ by rejecting the WHOLE
|
||||
-- request. Plain OpenAI does not, so an id minted by a permissive model (e.g.
|
||||
-- "bash:0" from moonshotai/kimi-k3) is replayed here by the client and kills
|
||||
-- every turn. See anthropic.lua / openai.lua for the same helper: Lua adapters
|
||||
-- have no shared prelude, so each carries its own copy.
|
||||
local TOOL_ID_MAX = 64
|
||||
|
||||
local function safe_tool_id(id)
|
||||
if type(id) ~= "string" or id == "" then return id end
|
||||
local clean = string.gsub(id, "[^A-Za-z0-9_-]", "_")
|
||||
if clean == id and #clean <= TOOL_ID_MAX then
|
||||
return clean
|
||||
end
|
||||
local digest = string.sub(sha256_hex(id), 1, 8)
|
||||
local keep = TOOL_ID_MAX - #digest - 1
|
||||
if #clean > keep then clean = string.sub(clean, 1, keep) end
|
||||
return clean .. "_" .. digest
|
||||
end
|
||||
|
||||
function adapter.transform_request(raw_body)
|
||||
local ok, req = pcall(json.decode, raw_body)
|
||||
if not ok then return raw_body end
|
||||
req.disable_thinking = nil
|
||||
req.extra_body = nil
|
||||
if req.messages then
|
||||
for _, msg in ipairs(req.messages) do
|
||||
msg.reasoning_content = nil
|
||||
if msg.tool_call_id ~= nil then
|
||||
msg.tool_call_id = safe_tool_id(msg.tool_call_id)
|
||||
end
|
||||
if type(msg.tool_calls) == "table" then
|
||||
for _, tc in ipairs(msg.tool_calls) do
|
||||
if type(tc) == "table" and tc.id ~= nil then
|
||||
tc.id = safe_tool_id(tc.id)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return json.encode(req)
|
||||
end
|
||||
|
||||
-- 注入 Authorization + 官方客户端 User-Agent
|
||||
function adapter.build_headers(meta)
|
||||
local ua = default_ua
|
||||
local meta_ua = (meta.source or {}).meta and (meta.source).meta.user_agent
|
||||
if meta_ua and meta_ua ~= "" then
|
||||
ua = meta_ua
|
||||
end
|
||||
return {
|
||||
["Content-Type"] = "application/json",
|
||||
["Authorization"] = "Bearer " .. meta.api_key,
|
||||
["User-Agent"] = ua,
|
||||
-- QwenCode client fingerprint: WAF rejects bare UA without these.
|
||||
["Accept"] = "application/json, text/plain, */*",
|
||||
["Accept-Language"] = "zh-CN,zh;q=0.9",
|
||||
["Origin"] = "https://qwen.ai",
|
||||
["Referer"] = "https://qwen.ai/",
|
||||
["X-Requested-With"] = "XMLHttpRequest",
|
||||
["Connection"] = "keep-alive",
|
||||
}
|
||||
end
|
||||
|
||||
function adapter.transform_response(raw_body)
|
||||
local ok, resp = pcall(json.decode, raw_body)
|
||||
if not ok or resp == nil then return raw_body end
|
||||
|
||||
local unified = {
|
||||
content = "",
|
||||
finish_reason = "",
|
||||
token_usage = { prompt = 0, completion = 0, total = 0 }
|
||||
}
|
||||
|
||||
if type(resp.usage) == "table" then
|
||||
unified.token_usage.prompt = resp.usage.prompt_tokens or 0
|
||||
unified.token_usage.completion = resp.usage.completion_tokens or 0
|
||||
unified.token_usage.total = resp.usage.total_tokens or 0
|
||||
local hit = 0
|
||||
if type(resp.usage.prompt_tokens_details) == "table" and resp.usage.prompt_tokens_details.cached_tokens ~= nil then
|
||||
hit = resp.usage.prompt_tokens_details.cached_tokens
|
||||
unified.token_usage.prompt_tokens_details = { cached_tokens = hit }
|
||||
end
|
||||
if (resp.usage.prompt_cache_hit_tokens or 0) > 0 then
|
||||
unified.token_usage.prompt_cache_hit_tokens = resp.usage.prompt_cache_hit_tokens
|
||||
unified.token_usage.prompt_cache_miss_tokens = resp.usage.prompt_cache_miss_tokens or 0
|
||||
if hit == 0 then
|
||||
unified.token_usage.prompt_tokens_details = { cached_tokens = resp.usage.prompt_cache_hit_tokens }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if type(resp.choices) == "table" and #resp.choices > 0 then
|
||||
local ch = resp.choices[1]
|
||||
if type(ch.message) == "table" then
|
||||
unified.content = ch.message.content or ""
|
||||
if ch.message.reasoning_content then
|
||||
unified.reasoning_content = ch.message.reasoning_content
|
||||
end
|
||||
if type(ch.message.tool_calls) == "table" then
|
||||
local tcs = {}
|
||||
for _, tc in ipairs(ch.message.tool_calls) do
|
||||
local args_ok, args = pcall(json.decode, tc["function"].arguments)
|
||||
if not args_ok then args = {} end
|
||||
table.insert(tcs, {
|
||||
id = safe_tool_id(tc.id),
|
||||
type = tc.type or "function",
|
||||
name = tc["function"].name,
|
||||
arguments = args
|
||||
})
|
||||
end
|
||||
unified.tool_calls = tcs
|
||||
end
|
||||
end
|
||||
unified.finish_reason = ch.finish_reason or ""
|
||||
end
|
||||
|
||||
return json.encode(unified)
|
||||
end
|
||||
|
||||
function adapter.transform_stream_chunk(raw_chunk)
|
||||
local ok, chunk = pcall(json.decode, raw_chunk)
|
||||
if not ok then return "" end
|
||||
|
||||
-- usage-only terminal chunk (empty choices + usage): forward it so the
|
||||
-- gateway emits exact token counts (and cache fields when present).
|
||||
local uses = nil
|
||||
if type(chunk.usage) == "table" then
|
||||
uses = {
|
||||
prompt = chunk.usage.prompt_tokens or chunk.usage.prompt or 0,
|
||||
completion = chunk.usage.completion_tokens or chunk.usage.completion or 0,
|
||||
total = chunk.usage.total_tokens or chunk.usage.total or 0,
|
||||
}
|
||||
if type(chunk.usage.prompt_tokens_details) == "table" and chunk.usage.prompt_tokens_details.cached_tokens ~= nil then
|
||||
uses.prompt_tokens_details = { cached_tokens = chunk.usage.prompt_tokens_details.cached_tokens }
|
||||
elseif (chunk.usage.prompt_cache_hit_tokens or 0) > 0 then
|
||||
uses.prompt_cache_hit_tokens = chunk.usage.prompt_cache_hit_tokens
|
||||
uses.prompt_cache_miss_tokens = chunk.usage.prompt_cache_miss_tokens or 0
|
||||
uses.prompt_tokens_details = { cached_tokens = chunk.usage.prompt_cache_hit_tokens }
|
||||
end
|
||||
end
|
||||
|
||||
if not chunk.choices or #chunk.choices == 0 then
|
||||
if uses ~= nil then
|
||||
return json.encode({ usage = uses, done = false })
|
||||
end
|
||||
return ""
|
||||
end
|
||||
local delta = chunk.choices[1].delta or {}
|
||||
local fr = chunk.choices[1].finish_reason
|
||||
|
||||
local finish = (type(fr) == "string" and fr ~= "") and fr or nil
|
||||
|
||||
local unified = {
|
||||
content = delta.content or "",
|
||||
done = (finish ~= nil)
|
||||
}
|
||||
if finish then
|
||||
unified.finish_reason = finish
|
||||
end
|
||||
if delta.reasoning_content then
|
||||
unified.reasoning_content = delta.reasoning_content
|
||||
end
|
||||
if delta.tool_calls then
|
||||
-- Sanitize outbound too: a dirty id must never enter a client session,
|
||||
-- because the client replays it to every other source. Only the first
|
||||
-- fragment of a streamed call carries an id; later argument fragments
|
||||
-- have none and must stay id-less for index-based accumulation.
|
||||
for _, tc in ipairs(delta.tool_calls) do
|
||||
if type(tc) == "table" and tc.id ~= nil then
|
||||
tc.id = safe_tool_id(tc.id)
|
||||
end
|
||||
end
|
||||
unified.tool_calls = delta.tool_calls
|
||||
end
|
||||
if uses ~= nil then
|
||||
unified.usage = uses
|
||||
end
|
||||
return json.encode(unified)
|
||||
end
|
||||
|
||||
-- 错误收敛:前置 WAF 会返回整页 HTML(阿里云盾),JSON 时为 new-api 风格
|
||||
function adapter.transform_error(status, body)
|
||||
local low = string.lower(body or "")
|
||||
if string.sub(low, 1, 9) == "<!doctype" or string.find(low, "<html", 1, true) then
|
||||
return "blocked by AgentRouter WAF"
|
||||
end
|
||||
local ok, resp = pcall(json.decode, body)
|
||||
if not ok or type(resp) ~= "table" then return nil end
|
||||
local e = resp.error
|
||||
if type(e) == "table" and type(e.message) == "string" then
|
||||
return e.message
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
return adapter
|
||||
@ -5,6 +5,14 @@ adapter.endpoint = "/v1/messages"
|
||||
adapter.headers = {
|
||||
["anthropic-version"] = "2023-06-01",
|
||||
}
|
||||
-- INGRESS CLAIM (inbound dual of serialize_*): this adapter owns parsing of the
|
||||
-- client-facing POST /v1/messages request body. The kernel keeps a built-in
|
||||
-- Anthropic parser as its fallback (R5) — that is what an unconfigured
|
||||
-- deployment uses — but once this adapter is loaded the kernel YIELDS, and the
|
||||
-- deserialize_request hook below is the single place Anthropic's REQUEST shape
|
||||
-- is understood. Iterating on that shape therefore means editing this file,
|
||||
-- never the kernel.
|
||||
adapter.ingress = "/v1/messages"
|
||||
|
||||
-- ============================================================
|
||||
-- helpers
|
||||
@ -132,6 +140,79 @@ local function append_user(msgs, blocks)
|
||||
end
|
||||
end
|
||||
|
||||
-- ============================================================
|
||||
-- deserialize_request (Anthropic → unified) [INGRESS]
|
||||
-- ============================================================
|
||||
-- The inbound dual of serialize_response. The kernel hands over the raw client
|
||||
-- body and receives the unified request; from there scheduling is identical to
|
||||
-- /v1/chat/completions. Ported field-for-field from the kernel's former
|
||||
-- built-in anthropicRequest.toUnified — which remains as the kernel fallback
|
||||
-- for an unconfigured deployment.
|
||||
function adapter.deserialize_request(raw_body)
|
||||
local ok, req = pcall(json.decode, raw_body)
|
||||
if not ok or type(req) ~= "table" then
|
||||
error("invalid json")
|
||||
end
|
||||
|
||||
-- system accepts Anthropic's two spellings: a plain string, or an array of
|
||||
-- { type = "text", text = ... } blocks. Only text blocks contribute.
|
||||
local system = ""
|
||||
local sys = req.system
|
||||
if type(sys) == "string" then
|
||||
system = sys
|
||||
elseif type(sys) == "table" then
|
||||
for _, b in ipairs(sys) do
|
||||
if type(b) == "table" and b.type == "text" and type(b.text) == "string" then
|
||||
system = system .. b.text
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local msgs = {}
|
||||
local in_msgs = req.messages
|
||||
if type(in_msgs) ~= "table" or #in_msgs == 0 then
|
||||
error("messages is required")
|
||||
end
|
||||
for i, m in ipairs(in_msgs) do
|
||||
if type(m) ~= "table" or type(m.role) ~= "string" or m.role == "" then
|
||||
error("messages[" .. (i - 1) .. "].role is required")
|
||||
end
|
||||
-- Content is carried VERBATIM: a string stays a string, a block array
|
||||
-- stays an array. The unified format keeps multimodal/tool blocks
|
||||
-- untouched because only an adapter knows which block types the upstream
|
||||
-- accepts. An absent/null content becomes the empty string.
|
||||
local content = m.content
|
||||
if content == nil or content == "" then
|
||||
content = ""
|
||||
end
|
||||
table.insert(msgs, { role = m.role, content = content })
|
||||
end
|
||||
|
||||
-- Anthropic's default is thinking OFF; emit that explicitly so the unified
|
||||
-- request carries the client's intent rather than leaving it absent.
|
||||
local thinking = req.thinking
|
||||
if thinking == nil then
|
||||
thinking = { enabled = false }
|
||||
end
|
||||
|
||||
local out = {
|
||||
model = req.model or "",
|
||||
messages = msgs,
|
||||
system = system,
|
||||
max_tokens = req.max_tokens or 0,
|
||||
stream = req.stream == true,
|
||||
temperature = req.temperature,
|
||||
top_p = req.top_p,
|
||||
top_k = req.top_k or 0,
|
||||
stop_sequences = req.stop_sequences,
|
||||
tools = req.tools,
|
||||
tool_choice = req.tool_choice,
|
||||
thinking = thinking,
|
||||
extra_body = {},
|
||||
}
|
||||
return json.encode(out)
|
||||
end
|
||||
|
||||
-- ============================================================
|
||||
-- transform_request (OpenAI → Anthropic)
|
||||
-- ============================================================
|
||||
@ -254,12 +335,24 @@ function adapter.transform_request(raw_body)
|
||||
end
|
||||
end
|
||||
|
||||
-- ── thinking (opt-in via extra_body) ──────────────────────
|
||||
-- Default: OFF. Client sends extra_body.thinking to enable.
|
||||
-- Example: {"extra_body": {"thinking": {"type": "enabled", "budget_tokens": 4096}}}
|
||||
if type(req.extra_body) == "table" and type(req.extra_body.thinking) == "table" then
|
||||
-- ── thinking ─────────────────────────────────────────────
|
||||
-- Anthropic 的原生字段就是 {type, budget_tokens},所以中立语义几乎直译。
|
||||
--
|
||||
-- 中立格式(v2):{ enabled = true/false, budget_tokens = N }
|
||||
-- enabled=false → 不传该字段(Anthropic 默认关闭思考,传 type=disabled
|
||||
-- 是非法的,这也是过去只能「不传」的原因)
|
||||
-- enabled=true → { type="enabled", budget_tokens=N 或默认 }
|
||||
--
|
||||
-- 仍接受 extra_body.thinking 的原生写法:已有部署的配置不因此失效。
|
||||
local th = req.thinking
|
||||
if type(th) == "table" and th.enabled ~= false then
|
||||
local budget = th.budget_tokens
|
||||
if budget == nil or budget <= 0 then budget = 4096 end -- Anthropic 必填
|
||||
anthropic_req.thinking = { type = "enabled", budget_tokens = budget }
|
||||
elseif type(req.extra_body) == "table" and type(req.extra_body.thinking) == "table" then
|
||||
anthropic_req.thinking = req.extra_body.thinking
|
||||
end
|
||||
req.thinking = nil
|
||||
|
||||
-- ── system ───────────────────────────────────────────────
|
||||
if system ~= "" then
|
||||
@ -444,4 +537,124 @@ function adapter.transform_error(status, body)
|
||||
return nil
|
||||
end
|
||||
|
||||
-- ============================================================
|
||||
-- 对外序列化(统一格式 → Anthropic 形状)
|
||||
-- ============================================================
|
||||
--
|
||||
-- 内核只持统一格式;对外发什么形状由适配器决定(Phase 2)。
|
||||
-- 这几个钩子让以 anthropic 适配器为服务端的源,直接对 Anthropic 协议客户端
|
||||
-- 提供 /v1/messages 语义,而内核里不再有任何 Anthropic 字段名。
|
||||
--
|
||||
-- 与入向的 transform_* 互为逆运算:那里的映射(end_turn→stop)在这里反着
|
||||
-- 做回去(stop→end_turn),且默认 stop_sequence 是最保守的选择——它语义上
|
||||
-- 最接近"正常结束"。
|
||||
|
||||
-- 统一 finish_reason → Anthropic stop_reason
|
||||
local function to_stop_reason(finish)
|
||||
if finish == "tool_calls" then return "tool_use" end
|
||||
if finish == "length" then return "max_tokens" end
|
||||
if finish == "content_filter" then return "refusal" end
|
||||
-- stop / 空 / 未知 → end_turn(Anthropic 的"正常说完")
|
||||
return "end_turn"
|
||||
end
|
||||
|
||||
-- usage 数字化:内部 TokenUsage 的字段名与 Anthropic 的 input_tokens/output_tokens
|
||||
-- 不同名,这里做最后一次翻译。
|
||||
local function to_anthropic_usage(u)
|
||||
if type(u) ~= "table" then
|
||||
return { input_tokens = 0, output_tokens = 0 }
|
||||
end
|
||||
return {
|
||||
input_tokens = u.prompt or 0,
|
||||
output_tokens = u.completion or 0,
|
||||
}
|
||||
end
|
||||
|
||||
-- 非流式响应:统一结果 → Anthropic Messages 响应对象
|
||||
function adapter.serialize_response(payload)
|
||||
local p = json.decode(payload)
|
||||
local blocks = {}
|
||||
-- thinking 放最前,与 Anthropic 自己的块顺序一致
|
||||
if p.reasoning ~= nil and p.reasoning ~= "" then
|
||||
blocks[#blocks + 1] = { type = "thinking", thinking = p.reasoning }
|
||||
end
|
||||
blocks[#blocks + 1] = { type = "text", text = p.content or "" }
|
||||
if type(p.tool_calls) == "table" and #p.tool_calls > 0 then
|
||||
for _, tc in ipairs(p.tool_calls) do
|
||||
blocks[#blocks + 1] = {
|
||||
type = "tool_use",
|
||||
id = tc.id or "",
|
||||
name = tc.name or "",
|
||||
input = tc.arguments or {},
|
||||
}
|
||||
end
|
||||
end
|
||||
return json.encode({
|
||||
id = p.id or "",
|
||||
type = "message",
|
||||
role = "assistant",
|
||||
model = p.model or "",
|
||||
content = blocks,
|
||||
stop_reason = to_stop_reason(p.finish_reason),
|
||||
stop_sequence = nil,
|
||||
usage = to_anthropic_usage(p.token_usage),
|
||||
})
|
||||
end
|
||||
|
||||
-- 角色前导:Anthropic 的流以 message_start 开头(不是 role delta)
|
||||
function adapter.serialize_open_preamble(payload)
|
||||
local p = json.decode(payload)
|
||||
return json.encode({
|
||||
type = "message_start",
|
||||
message = {
|
||||
id = p.id or "",
|
||||
type = "message",
|
||||
role = "assistant",
|
||||
model = p.model or "",
|
||||
content = {},
|
||||
stop_reason = nil,
|
||||
stop_sequence = nil,
|
||||
usage = { input_tokens = 0, output_tokens = 0 },
|
||||
},
|
||||
})
|
||||
end
|
||||
|
||||
-- 流式增量:统一 chunk → Anthropic content_block_delta
|
||||
--
|
||||
-- 真实 Anthropic 流需要 content_block_start(开块)→ delta → stop(关块)
|
||||
-- 的三段式。这里用与多数 Anthropic 客户端兼容的简化形式:首个文本增量
|
||||
-- 带 index=0 的 delta,后续同 index;关闭由 serialize_close 负责。
|
||||
function adapter.serialize_chunk(payload)
|
||||
local p = json.decode(payload)
|
||||
if p.done then
|
||||
-- 终止由 serialize_close 统一发,这里不重复
|
||||
return ""
|
||||
end
|
||||
if p.reasoning ~= nil and p.reasoning ~= "" then
|
||||
return json.encode({
|
||||
type = "content_block_delta",
|
||||
index = 0,
|
||||
delta = { type = "thinking_delta", thinking = p.reasoning },
|
||||
})
|
||||
end
|
||||
return json.encode({
|
||||
type = "content_block_delta",
|
||||
index = 0,
|
||||
delta = { type = "text_delta", text = p.content or "" },
|
||||
})
|
||||
end
|
||||
|
||||
-- 收尾:message_delta(stop_reason + usage)→ message_stop
|
||||
function adapter.serialize_close(payload)
|
||||
local p = json.decode(payload)
|
||||
return json.encode({
|
||||
type = "message_delta",
|
||||
delta = {
|
||||
stop_reason = to_stop_reason(p.finish_reason),
|
||||
stop_sequence = nil,
|
||||
},
|
||||
usage = { output_tokens = 0 },
|
||||
}) .. "\n" .. json.encode({ type = "message_stop" })
|
||||
end
|
||||
|
||||
return adapter
|
||||
|
||||
@ -15,11 +15,18 @@ function adapter.transform_request(raw_body)
|
||||
req.model = "deepseek-v4-flash"
|
||||
end
|
||||
req.stream = req.stream or false
|
||||
if req.disable_thinking then
|
||||
-- 中立的 thinking 语义 → DeepSeek 自己的协议写法。
|
||||
-- 客户端说「关掉思考」时 thinking.enabled == false;上游的表达方式是
|
||||
-- extra_body.thinking.type = "disabled"。v2 之前这里读的是内核私有字段
|
||||
-- req.disable_thinking,而那个字段曾被内核泄漏进本函数收到的 body。
|
||||
if type(req.thinking) == "table" and req.thinking.enabled == false then
|
||||
req.extra_body = req.extra_body or {}
|
||||
req.extra_body.thinking = { type = "disabled" }
|
||||
elseif type(req.thinking) == "table" and req.thinking.budget_tokens ~= nil then
|
||||
req.extra_body = req.extra_body or {}
|
||||
req.extra_body.thinking = { type = "enabled", budget_tokens = req.thinking.budget_tokens }
|
||||
end
|
||||
req.disable_thinking = nil
|
||||
req.thinking = nil
|
||||
|
||||
-- V4 thinking 模式要求:带 tool_calls 的 assistant 消息必须回传 reasoning_content。
|
||||
-- OpenAI 兼容客户端不会发该字段,补空串即可通过校验。
|
||||
|
||||
@ -18,6 +18,10 @@ adapter.version = "2.0.0"
|
||||
-- can never work. A source that overrides `endpoint:` bypasses the template
|
||||
-- entirely and must then spell the whole path itself.
|
||||
adapter.endpoint = "/v1beta/models/{model}:generateContent"
|
||||
-- 流式走同一路径的不同动词。这个事实以前写在 Go 内核里
|
||||
-- (provider.ChatURL 对 ":generateContent" 做字符串替换),属于把厂商协议
|
||||
-- 知识放进调度器;现在由适配器自己声明,内核只比较两个不透明字符串。
|
||||
adapter.endpoint_stream = "/v1beta/models/{model}:streamGenerateContent"
|
||||
adapter.headers = {}
|
||||
|
||||
-- Gemini API: POST /v1/models/{model}:generateContent
|
||||
|
||||
@ -14,7 +14,13 @@ function adapter.transform_request(raw_body)
|
||||
req.temperature = req.temperature or 0.7
|
||||
req.max_tokens = req.max_tokens or 4096
|
||||
req.stream = req.stream or false
|
||||
req.disable_thinking = nil
|
||||
-- The gateway's private control fields are GONE from the body (v2): the
|
||||
-- neutral `thinking` field carries that intent and each adapter maps it in
|
||||
-- its own protocol terms. These upstreams have no thinking switch at all,
|
||||
-- so the field is simply dropped here — which is what the nil-ing below
|
||||
-- used to do, except the field being dropped is no longer the kernel's
|
||||
-- private one.
|
||||
req.thinking = nil
|
||||
req.extra_body = nil
|
||||
return json.encode(req)
|
||||
end
|
||||
|
||||
@ -13,7 +13,13 @@ function adapter.transform_request(raw_body)
|
||||
req.temperature = req.temperature or 0.7
|
||||
req.max_tokens = req.max_tokens or 4096
|
||||
req.stream = req.stream or false
|
||||
req.disable_thinking = nil
|
||||
-- The gateway's private control fields are GONE from the body (v2): the
|
||||
-- neutral `thinking` field carries that intent and each adapter maps it in
|
||||
-- its own protocol terms. These upstreams have no thinking switch at all,
|
||||
-- so the field is simply dropped here — which is what the nil-ing below
|
||||
-- used to do, except the field being dropped is no longer the kernel's
|
||||
-- private one.
|
||||
req.thinking = nil
|
||||
req.extra_body = nil
|
||||
return json.encode(req)
|
||||
end
|
||||
|
||||
@ -19,7 +19,13 @@ function adapter.transform_request(raw_body)
|
||||
local ok, req = pcall(json.decode, raw_body)
|
||||
if not ok then return raw_body end
|
||||
req.model = req.model or "kimi-k2"
|
||||
req.disable_thinking = nil
|
||||
-- The gateway's private control fields are GONE from the body (v2): the
|
||||
-- neutral `thinking` field carries that intent and each adapter maps it in
|
||||
-- its own protocol terms. These upstreams have no thinking switch at all,
|
||||
-- so the field is simply dropped here — which is what the nil-ing below
|
||||
-- used to do, except the field being dropped is no longer the kernel's
|
||||
-- private one.
|
||||
req.thinking = nil
|
||||
req.extra_body = nil
|
||||
return json.encode(req)
|
||||
end
|
||||
|
||||
@ -13,7 +13,13 @@ function adapter.transform_request(raw_body)
|
||||
req.temperature = req.temperature or 0.7
|
||||
req.max_tokens = req.max_tokens or 4096
|
||||
req.stream = req.stream or false
|
||||
req.disable_thinking = nil
|
||||
-- The gateway's private control fields are GONE from the body (v2): the
|
||||
-- neutral `thinking` field carries that intent and each adapter maps it in
|
||||
-- its own protocol terms. These upstreams have no thinking switch at all,
|
||||
-- so the field is simply dropped here — which is what the nil-ing below
|
||||
-- used to do, except the field being dropped is no longer the kernel's
|
||||
-- private one.
|
||||
req.thinking = nil
|
||||
req.extra_body = nil
|
||||
return json.encode(req)
|
||||
end
|
||||
|
||||
@ -39,7 +39,13 @@ end
|
||||
function adapter.transform_request(raw_body)
|
||||
local ok, req = pcall(json.decode, raw_body)
|
||||
if not ok then return raw_body end
|
||||
req.disable_thinking = nil
|
||||
-- The gateway's private control fields are GONE from the body (v2): the
|
||||
-- neutral `thinking` field carries that intent and each adapter maps it in
|
||||
-- its own protocol terms. These upstreams have no thinking switch at all,
|
||||
-- so the field is simply dropped here — which is what the nil-ing below
|
||||
-- used to do, except the field being dropped is no longer the kernel's
|
||||
-- private one.
|
||||
req.thinking = nil
|
||||
req.extra_body = nil
|
||||
if req.messages then
|
||||
for _, msg in ipairs(req.messages) do
|
||||
|
||||
@ -1,243 +0,0 @@
|
||||
local adapter = {}
|
||||
|
||||
adapter.name = "opencode"
|
||||
adapter.version = "1.0.0"
|
||||
adapter.endpoint = "/chat/completions"
|
||||
|
||||
-- opencode.ai zen 网关按客户端指纹(UA + x-opencode-* 头)路由请求池:
|
||||
-- 缺少 x-opencode-client/session/request/project 身份头的请求会被判为匿名
|
||||
-- 客户端,x-preview-f-free 等模型在带 tools 时上游直接失败——流式返回单
|
||||
-- chunk "finish_reason":"network_error" 空 content,非流式返回 503
|
||||
-- "Endpoint is unavailable",表现为"空回复"。因此除 UA 外必须附带
|
||||
-- x-opencode-* 身份头;session/request 每请求派生唯一值(见 build_headers)。
|
||||
adapter.headers = {
|
||||
["User-Agent"] = "opencode/1.18.21 ai-sdk/provider-utils/4.0.23 runtime/bun/1.3.14",
|
||||
}
|
||||
|
||||
-- 每请求生成身份头。沙箱无 os/math,用 meta.timestamp + 请求体哈希派生:
|
||||
-- 同秒内重复请求 id 相同可接受(zen 只校验存在性,不校验格式)。
|
||||
local function rand_id(prefix, seed)
|
||||
return prefix .. string.sub(sha256_hex(seed), 1, 24)
|
||||
end
|
||||
|
||||
function adapter.build_headers(meta)
|
||||
local ts = tostring(meta.timestamp or "")
|
||||
return {
|
||||
["User-Agent"] = adapter.headers["User-Agent"],
|
||||
["x-opencode-client"] = "cli",
|
||||
-- project 固定:同网关实例共享一个工作区身份
|
||||
["x-opencode-project"] = string.sub(sha256_hex("llmsproxy|" .. (meta.source and meta.source.name or "")), 1, 32),
|
||||
["x-opencode-session"] = rand_id("ses_", "session|" .. ts),
|
||||
["x-opencode-request"] = rand_id("msg_", "request|" .. ts .. "|" .. tostring(meta.body or "")),
|
||||
}
|
||||
end
|
||||
|
||||
-- OpenAI /chat/completions format (pass-through, strip provider-specific fields)
|
||||
-- zen 上游 schema 只接受 text content part(无视觉/音频能力):多模态 part
|
||||
-- (image_url / input_audio / file 等)一律剥离。剥离后 content 变空的消息
|
||||
-- 若不再携带 tool_calls / tool_call_id 才整条丢弃(避免上游
|
||||
-- "unknown variant `image_url`, expected `text`");带工具调用的必须保留,
|
||||
-- 否则会把紧随其后的 tool 结果变成孤儿,模型会反复重发同一个调用。
|
||||
-- zen 上游角色白名单只有 system / user / assistant / tool / latest_reminder:
|
||||
-- OpenAI 的 developer(及 function 等)不在其中,直接透传会触发上游
|
||||
-- "unknown variant `developer`, expected one of ..." 错误;统一归一化为 system。
|
||||
local ROLE_WHITELIST = {
|
||||
system = true,
|
||||
user = true,
|
||||
assistant = true,
|
||||
tool = true,
|
||||
latest_reminder = true,
|
||||
}
|
||||
|
||||
function adapter.transform_request(raw_body)
|
||||
local ok, req = pcall(json.decode, raw_body)
|
||||
if not ok then return raw_body end
|
||||
req.disable_thinking = nil
|
||||
req.extra_body = nil
|
||||
-- stream_options is only valid alongside stream:true. Sending it on a
|
||||
-- non-streaming request is rejected by stricter upstreams (OpenCode Go:
|
||||
-- "stream_options should be set along with stream"), which failed every
|
||||
-- non-stream call. Only attach it when the request actually streams.
|
||||
if req.stream then
|
||||
if type(req.stream_options) ~= "table" then req.stream_options = {} end
|
||||
req.stream_options.include_usage = true
|
||||
end
|
||||
if req.messages then
|
||||
local kept = {}
|
||||
for _, msg in ipairs(req.messages) do
|
||||
if type(msg.role) == "string" and not ROLE_WHITELIST[msg.role] then
|
||||
msg.role = "system"
|
||||
end
|
||||
msg.reasoning_content = nil
|
||||
local drop = false
|
||||
if type(msg.content) == "table" then
|
||||
local parts = {}
|
||||
for _, part in ipairs(msg.content) do
|
||||
if type(part) == "table" and part.type ~= nil and part.type ~= "text" then
|
||||
-- multimodal part not supported by zen
|
||||
else
|
||||
table.insert(parts, part)
|
||||
end
|
||||
end
|
||||
if #parts == 0 then
|
||||
-- Content collapsed to nothing after stripping unsupported
|
||||
-- parts. A message that still carries a tool call must
|
||||
-- NEVER be dropped: the very next message is its tool
|
||||
-- result, and dropping the call orphans that result. The
|
||||
-- model then sees a result for a call it never made and
|
||||
-- re-issues the same tool call on every turn (observed as
|
||||
-- an infinite "repeated tool call" loop).
|
||||
--
|
||||
-- This is the common shape for agent clients: an assistant
|
||||
-- turn whose content is only [thinking, toolCall] serialises
|
||||
-- to content:[] with tool_calls — exactly the case that used
|
||||
-- to vanish here. Emit "" instead, which zen accepts.
|
||||
if (type(msg.tool_calls) == "table" and #msg.tool_calls > 0)
|
||||
or msg.tool_call_id ~= nil then
|
||||
msg.content = ""
|
||||
else
|
||||
drop = true
|
||||
end
|
||||
else
|
||||
msg.content = parts
|
||||
end
|
||||
end
|
||||
if not drop then
|
||||
table.insert(kept, msg)
|
||||
end
|
||||
end
|
||||
req.messages = kept
|
||||
end
|
||||
return json.encode(req)
|
||||
end
|
||||
|
||||
function adapter.transform_response(raw_body)
|
||||
local ok, resp = pcall(json.decode, raw_body)
|
||||
if not ok or resp == nil then return raw_body end
|
||||
|
||||
local unified = {
|
||||
content = "",
|
||||
finish_reason = "",
|
||||
token_usage = { prompt = 0, completion = 0, total = 0 }
|
||||
}
|
||||
|
||||
if type(resp.usage) == "table" then
|
||||
unified.token_usage.prompt = resp.usage.prompt_tokens or 0
|
||||
unified.token_usage.completion = resp.usage.completion_tokens or 0
|
||||
unified.token_usage.total = resp.usage.total_tokens or 0
|
||||
local hit = 0
|
||||
if type(resp.usage.prompt_tokens_details) == "table" and resp.usage.prompt_tokens_details.cached_tokens ~= nil then
|
||||
hit = resp.usage.prompt_tokens_details.cached_tokens
|
||||
unified.token_usage.prompt_tokens_details = { cached_tokens = hit }
|
||||
end
|
||||
if (resp.usage.prompt_cache_hit_tokens or 0) > 0 then
|
||||
unified.token_usage.prompt_cache_hit_tokens = resp.usage.prompt_cache_hit_tokens
|
||||
unified.token_usage.prompt_cache_miss_tokens = resp.usage.prompt_cache_miss_tokens or 0
|
||||
if hit == 0 then
|
||||
unified.token_usage.prompt_tokens_details = { cached_tokens = resp.usage.prompt_cache_hit_tokens }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if type(resp.choices) == "table" and #resp.choices > 0 then
|
||||
local ch = resp.choices[1]
|
||||
if type(ch.message) == "table" then
|
||||
unified.content = ch.message.content or ""
|
||||
local reasoning = ch.message.reasoning_content or ch.message.reasoning
|
||||
if reasoning then
|
||||
unified.reasoning_content = reasoning
|
||||
end
|
||||
if type(ch.message.tool_calls) == "table" then
|
||||
local tcs = {}
|
||||
for _, tc in ipairs(ch.message.tool_calls) do
|
||||
local args_ok, args = pcall(json.decode, tc["function"].arguments)
|
||||
if not args_ok then args = {} end
|
||||
table.insert(tcs, {
|
||||
id = tc.id,
|
||||
type = tc.type or "function",
|
||||
name = tc["function"].name,
|
||||
arguments = args
|
||||
})
|
||||
end
|
||||
unified.tool_calls = tcs
|
||||
end
|
||||
end
|
||||
unified.finish_reason = ch.finish_reason or ""
|
||||
end
|
||||
|
||||
return json.encode(unified)
|
||||
end
|
||||
|
||||
function adapter.transform_stream_chunk(raw_chunk)
|
||||
local ok, chunk = pcall(json.decode, raw_chunk)
|
||||
if not ok then return "" end
|
||||
|
||||
-- OpenAI-style streams may attach usage to a chunk with empty choices
|
||||
-- (the final usage chunk). Preserve it; the gateway emits it as the
|
||||
-- terminal usage chunk. Note: keys must match Go's TokenUsage json tags
|
||||
-- (prompt/completion/total); the gateway re-emits standard *_tokens.
|
||||
local uses = nil
|
||||
if type(chunk.usage) == "table" then
|
||||
uses = {
|
||||
prompt = chunk.usage.prompt_tokens or chunk.usage.prompt or 0,
|
||||
completion = chunk.usage.completion_tokens or chunk.usage.completion or 0,
|
||||
total = chunk.usage.total_tokens or chunk.usage.total or 0,
|
||||
}
|
||||
if type(chunk.usage.prompt_tokens_details) == "table" and chunk.usage.prompt_tokens_details.cached_tokens ~= nil then
|
||||
uses.prompt_tokens_details = { cached_tokens = chunk.usage.prompt_tokens_details.cached_tokens }
|
||||
elseif (chunk.usage.prompt_cache_hit_tokens or 0) > 0 then
|
||||
uses.prompt_cache_hit_tokens = chunk.usage.prompt_cache_hit_tokens
|
||||
uses.prompt_cache_miss_tokens = chunk.usage.prompt_cache_miss_tokens or 0
|
||||
uses.prompt_tokens_details = { cached_tokens = chunk.usage.prompt_cache_hit_tokens }
|
||||
end
|
||||
end
|
||||
|
||||
if not chunk.choices or #chunk.choices == 0 then
|
||||
if uses ~= nil then
|
||||
-- usage-only chunk is not a content/finish signal; the gateway
|
||||
-- emits its own terminal stop chunk and merges this usage.
|
||||
return json.encode({ usage = uses, done = false })
|
||||
end
|
||||
return ""
|
||||
end
|
||||
local delta = chunk.choices[1].delta or {}
|
||||
local fr = chunk.choices[1].finish_reason
|
||||
|
||||
local finish = (type(fr) == "string" and fr ~= "") and fr or nil
|
||||
|
||||
local unified = {
|
||||
content = delta.content or "",
|
||||
done = (finish ~= nil)
|
||||
}
|
||||
if finish then
|
||||
unified.finish_reason = finish
|
||||
end
|
||||
if uses ~= nil then
|
||||
unified.usage = uses
|
||||
end
|
||||
-- zen 用 reasoning 字段承载推理文本(OpenAI 惯例是 reasoning_content)
|
||||
local reasoning = delta.reasoning_content or delta.reasoning
|
||||
if reasoning then
|
||||
unified.reasoning_content = reasoning
|
||||
end
|
||||
if delta.tool_calls then
|
||||
-- pass raw streaming fragments through; OpenAI clients accumulate index+id+name+arguments
|
||||
unified.tool_calls = delta.tool_calls
|
||||
end
|
||||
return json.encode(unified)
|
||||
end
|
||||
|
||||
-- 错误收敛(可选钩子):zen 错误信封固定为 {error={type,message}};
|
||||
-- 免费池限流 FreeUsageLimitError 单独标注。返回 nil 走通用兜底。
|
||||
function adapter.transform_error(status, body)
|
||||
local ok, resp = pcall(json.decode, body)
|
||||
if not ok or type(resp) ~= "table" then return nil end
|
||||
local e = resp.error
|
||||
if type(e) ~= "table" then return nil end
|
||||
if e.type == "FreeUsageLimitError" then
|
||||
return "zen free pool quota exhausted"
|
||||
end
|
||||
return e.message
|
||||
end
|
||||
|
||||
return adapter
|
||||
@ -1,354 +0,0 @@
|
||||
local adapter = {}
|
||||
|
||||
adapter.name = "opencodego"
|
||||
adapter.version = "1.0.0"
|
||||
adapter.endpoint = "/chat/completions"
|
||||
|
||||
-- OpenCode Go(包月订阅端点,https://opencode.ai/zen/go/v1)
|
||||
--
|
||||
-- 与 OpenCode Zen(opencodezen.lua,https://opencode.ai/zen/v1)是两个不同的
|
||||
-- 服务,行为要求并不相同,因此各有专用适配器:
|
||||
--
|
||||
-- * 本适配器(Go)服务包月订阅池。上游**强制要求**
|
||||
-- `x-opencode-session` 头,缺失直接 400 "MissingSessionID"。
|
||||
-- * Go 的 thinking 模型(deepseek-v4.1-flash 等)**必须回传** assistant 轮的
|
||||
-- `reasoning_content`,否则 400:
|
||||
-- The `reasoning_content` in the thinking mode must be passed back to the API.
|
||||
-- 这是本适配器与 Zen 适配器最关键的差异 —— Zen 会剥掉它,Go 必须原样保留。
|
||||
-- * Go 上游对协议更严格:非流式请求带 `stream_options` 会被拒
|
||||
-- ("stream_options should be set along with stream")。
|
||||
--
|
||||
-- 同样需要 opencode 客户端指纹(UA + x-opencode-*)才能被正确识别与路由。
|
||||
adapter.headers = {
|
||||
["User-Agent"] = "opencode/1.18.21 ai-sdk/provider-utils/4.0.23 runtime/bun/1.3.14",
|
||||
}
|
||||
|
||||
-- 每请求生成身份头。沙箱无 os/math,用 meta.timestamp + 请求体哈希派生:
|
||||
-- 同秒内重复请求 id 相同可接受(zen 只校验存在性,不校验格式)。
|
||||
local function rand_id(prefix, seed)
|
||||
return prefix .. string.sub(sha256_hex(seed), 1, 24)
|
||||
end
|
||||
|
||||
-- 会话指纹:取历史里**第一条 user 消息**。
|
||||
--
|
||||
-- 为什么不直接用客户端的会话 id:实测抓包(tcpdump 抓 127.0.0.1:8081 的真实
|
||||
-- agent 请求)确认通用 OpenAI 客户端**根本不发**任何会话标识 —— body 里没有
|
||||
-- user / session_id / conversation_id / metadata,请求头也只有 X-Stainless-*
|
||||
-- (OpenAI JS SDK)与 User-Agent。opencode 原生客户端那套 x-opencode-session
|
||||
-- 是它自己的概念,通用客户端无从转发。
|
||||
--
|
||||
-- 退而求其次但要够用:历史被逐轮重放,**第一条 user 消息在整个会话里恒定**,
|
||||
-- 用它做指纹即可得到"每会话一个 session",而不是"每源一个 session"。
|
||||
-- 拿不到时(无 user 消息)返回空串,调用方回退到按源稳定。
|
||||
local function conversation_fingerprint(body)
|
||||
if type(body) ~= "string" or body == "" then return "" end
|
||||
local ok, req = pcall(json.decode, body)
|
||||
if not ok or type(req) ~= "table" then return "" end
|
||||
for _, m in ipairs(req.messages or {}) do
|
||||
if type(m) == "table" and m.role == "user" then
|
||||
local c = m.content
|
||||
if type(c) == "string" then
|
||||
if c ~= "" then return c end
|
||||
elseif type(c) == "table" then
|
||||
for _, part in ipairs(c) do
|
||||
if type(part) == "table" and part.type == "text" and part.text then
|
||||
return part.text
|
||||
end
|
||||
end
|
||||
end
|
||||
return ""
|
||||
end
|
||||
end
|
||||
return ""
|
||||
end
|
||||
|
||||
-- session_seed 决定上游会话号(x-opencode-session 的种子)。
|
||||
--
|
||||
-- 优先级:
|
||||
-- 1) 客户端自带的会话标识(meta.client_session)——pi 等在开启
|
||||
-- compat.sendSessionAffinityHeaders 后会发 x-session-affinity,
|
||||
-- 这是平台真实的会话 id,整个会话恒定且天然按会话隔离。
|
||||
-- 2) 退路:历史里**第一条 user 消息**做会话指纹。
|
||||
-- 3) 再退:只按源固定(连 user 消息都没有时)。
|
||||
--
|
||||
-- 为什么需要 2/3:通用 OpenAI 客户端默认**根本不发**会话标识 —— 实测抓包
|
||||
-- (tcpdump 抓 127.0.0.1:8081 真实 agent 请求)确认 body 里没有
|
||||
-- user / session_id / conversation_id / metadata,请求头也只有
|
||||
-- X-Stainless-*(OpenAI JS SDK)与 User-Agent。opencode 原生客户端那套
|
||||
-- x-opencode-session 是它自己的概念,通用客户端无从转发。
|
||||
--
|
||||
-- 为什么必须稳定:上游前缀缓存是**会话级**的。会话号每请求一变,
|
||||
-- 缓存永不命中(实测:固定会号第 2 次命中 5888,每请求换会号则恒为 0)。
|
||||
local function session_seed(meta)
|
||||
local src = (meta.source and meta.source.name) or ""
|
||||
local base = "session|llmsproxy|" .. src
|
||||
local cs = meta.client_session
|
||||
if type(cs) == "string" and cs ~= "" then
|
||||
return base .. "|client|" .. cs
|
||||
end
|
||||
return base .. "|" .. conversation_fingerprint(meta.body)
|
||||
end
|
||||
|
||||
function adapter.build_headers(meta)
|
||||
local ts = tostring(meta.timestamp or "")
|
||||
local src = (meta.source and meta.source.name) or ""
|
||||
return {
|
||||
["User-Agent"] = adapter.headers["User-Agent"],
|
||||
["x-opencode-client"] = "cli",
|
||||
-- project 固定:同网关实例共享一个工作区身份
|
||||
["x-opencode-project"] = string.sub(sha256_hex("llmsproxy|" .. src), 1, 32),
|
||||
-- session 必须**按源稳定**,不能每请求换:上游的前缀缓存在同一 session
|
||||
-- 内才复用。实测(同一段 6032 token 提示词):
|
||||
-- 固定 session -> 第 2 次命中 5888/6032,cached_tokens=5888
|
||||
-- 每请求换 session -> 永远 0 命中
|
||||
-- 原先用 meta.timestamp 派生,等于每请求都是新会话,缓存永远无效,
|
||||
-- 上游也无法做会话亲和路由。
|
||||
["x-opencode-session"] = rand_id("ses_", session_seed(meta)),
|
||||
-- request id 仍每请求唯一(它只是请求标识,不参与缓存键)
|
||||
["x-opencode-request"] = rand_id("msg_", "request|" .. ts .. "|" .. tostring(meta.body or "")),
|
||||
}
|
||||
end
|
||||
|
||||
-- OpenAI /chat/completions format (pass-through, strip provider-specific fields)
|
||||
-- zen 上游 schema 只接受 text content part(无视觉/音频能力):多模态 part
|
||||
-- (image_url / input_audio / file 等)一律剥离。剥离后 content 变空的消息
|
||||
-- 若不再携带 tool_calls / tool_call_id 才整条丢弃(避免上游
|
||||
-- "unknown variant `image_url`, expected `text`");带工具调用的必须保留,
|
||||
-- 否则会把紧随其后的 tool 结果变成孤儿,模型会反复重发同一个调用。
|
||||
-- zen 上游角色白名单只有 system / user / assistant / tool / latest_reminder:
|
||||
-- OpenAI 的 developer(及 function 等)不在其中,直接透传会触发上游
|
||||
-- "unknown variant `developer`, expected one of ..." 错误;统一归一化为 system。
|
||||
local ROLE_WHITELIST = {
|
||||
system = true,
|
||||
user = true,
|
||||
assistant = true,
|
||||
tool = true,
|
||||
latest_reminder = true,
|
||||
}
|
||||
|
||||
function adapter.transform_request(raw_body)
|
||||
local ok, req = pcall(json.decode, raw_body)
|
||||
if not ok then return raw_body end
|
||||
req.disable_thinking = nil
|
||||
req.extra_body = nil
|
||||
-- stream_options is only valid alongside stream:true; sending it on a
|
||||
-- non-streaming request is rejected by strict upstreams.
|
||||
if req.stream then
|
||||
if type(req.stream_options) ~= "table" then req.stream_options = {} end
|
||||
req.stream_options.include_usage = true
|
||||
end
|
||||
if req.messages then
|
||||
local kept = {}
|
||||
for _, msg in ipairs(req.messages) do
|
||||
if type(msg.role) == "string" and not ROLE_WHITELIST[msg.role] then
|
||||
msg.role = "system"
|
||||
end
|
||||
-- Go 的 thinking 模式**必须**回传 reasoning_content:剥掉它会让
|
||||
-- 上游直接 400("The `reasoning_content` in the thinking mode must
|
||||
-- be passed back to the API"),agent 的每一轮都会失败。
|
||||
-- 注意:与 opencodezen.lua 的行为**相反**,不要在这里剥。
|
||||
--
|
||||
-- 但绝大多数客户端(pi 等)根本不保存也不回传 reasoning,只保留
|
||||
-- 工具调用本身。上游只在“带 tool_calls 的助手轮”上校验这个字段,
|
||||
-- 实测**空串即可通过校验**,所以缺省时补空串:既满足上游的
|
||||
-- 一致性要求,又不伪造任何推理内容(用户看到的 reasoning 仍然是
|
||||
-- 上游本轮真实返回的)。
|
||||
if msg.role == "assistant"
|
||||
and type(msg.tool_calls) == "table" and #msg.tool_calls > 0
|
||||
and msg.reasoning_content == nil then
|
||||
msg.reasoning_content = ""
|
||||
end
|
||||
local drop = false
|
||||
if type(msg.content) == "table" then
|
||||
local parts = {}
|
||||
for _, part in ipairs(msg.content) do
|
||||
if type(part) == "table" and part.type ~= nil and part.type ~= "text" then
|
||||
-- multimodal part not supported by zen
|
||||
else
|
||||
table.insert(parts, part)
|
||||
end
|
||||
end
|
||||
if #parts == 0 then
|
||||
-- Content collapsed to nothing after stripping unsupported
|
||||
-- parts. A message that still carries a tool call must
|
||||
-- NEVER be dropped: the very next message is its tool
|
||||
-- result, and dropping the call orphans that result. The
|
||||
-- model then sees a result for a call it never made and
|
||||
-- re-issues the same tool call on every turn (observed as
|
||||
-- an infinite "repeated tool call" loop).
|
||||
if (type(msg.tool_calls) == "table" and #msg.tool_calls > 0)
|
||||
or msg.tool_call_id ~= nil then
|
||||
msg.content = ""
|
||||
else
|
||||
drop = true
|
||||
end
|
||||
else
|
||||
msg.content = parts
|
||||
end
|
||||
end
|
||||
if not drop then
|
||||
table.insert(kept, msg)
|
||||
end
|
||||
end
|
||||
req.messages = kept
|
||||
end
|
||||
return json.encode(req)
|
||||
end
|
||||
|
||||
function adapter.transform_response(raw_body)
|
||||
local ok, resp = pcall(json.decode, raw_body)
|
||||
if not ok or resp == nil then return raw_body end
|
||||
|
||||
local unified = {
|
||||
content = "",
|
||||
finish_reason = "",
|
||||
token_usage = { prompt = 0, completion = 0, total = 0 }
|
||||
}
|
||||
|
||||
if type(resp.usage) == "table" then
|
||||
unified.token_usage.prompt = resp.usage.prompt_tokens or 0
|
||||
unified.token_usage.completion = resp.usage.completion_tokens or 0
|
||||
unified.token_usage.total = resp.usage.total_tokens or 0
|
||||
local hit = 0
|
||||
if type(resp.usage.prompt_tokens_details) == "table" and resp.usage.prompt_tokens_details.cached_tokens ~= nil then
|
||||
hit = resp.usage.prompt_tokens_details.cached_tokens
|
||||
unified.token_usage.prompt_tokens_details = { cached_tokens = hit }
|
||||
end
|
||||
if (resp.usage.prompt_cache_hit_tokens or 0) > 0 then
|
||||
unified.token_usage.prompt_cache_hit_tokens = resp.usage.prompt_cache_hit_tokens
|
||||
unified.token_usage.prompt_cache_miss_tokens = resp.usage.prompt_cache_miss_tokens or 0
|
||||
if hit == 0 then
|
||||
unified.token_usage.prompt_tokens_details = { cached_tokens = resp.usage.prompt_cache_hit_tokens }
|
||||
end
|
||||
end
|
||||
-- 上游报告输出里有多少是思考 token。不给客户端的话,无法判断
|
||||
-- completion_tokens 里多少是「可见回答」、多少是「思考」(都按输出计费)。
|
||||
local ctd = resp.usage.completion_tokens_details
|
||||
if type(ctd) == "table" and ctd.reasoning_tokens ~= nil then
|
||||
unified.token_usage.completion_tokens_details = { reasoning_tokens = ctd.reasoning_tokens }
|
||||
end
|
||||
end
|
||||
|
||||
-- 本次调用的费用,上游放在**顶层**且是**字符串**(如 "0"、"0.0012")。
|
||||
-- Go 订阅是包月制,恒为 "0";只有 Zen 按量付费模型才有信息量。
|
||||
-- 原样透传:网关不解析、不换算、不汇总 —— 它只是上游事实的搬运者。
|
||||
if resp.cost ~= nil then
|
||||
unified.cost = tostring(resp.cost)
|
||||
end
|
||||
|
||||
if type(resp.choices) == "table" and #resp.choices > 0 then
|
||||
local ch = resp.choices[1]
|
||||
if type(ch.message) == "table" then
|
||||
unified.content = ch.message.content or ""
|
||||
local reasoning = ch.message.reasoning_content or ch.message.reasoning
|
||||
if reasoning then
|
||||
unified.reasoning_content = reasoning
|
||||
end
|
||||
if type(ch.message.tool_calls) == "table" then
|
||||
local tcs = {}
|
||||
for _, tc in ipairs(ch.message.tool_calls) do
|
||||
local args_ok, args = pcall(json.decode, tc["function"].arguments)
|
||||
if not args_ok then args = {} end
|
||||
table.insert(tcs, {
|
||||
id = tc.id,
|
||||
type = tc.type or "function",
|
||||
name = tc["function"].name,
|
||||
arguments = args
|
||||
})
|
||||
end
|
||||
unified.tool_calls = tcs
|
||||
end
|
||||
end
|
||||
unified.finish_reason = ch.finish_reason or ""
|
||||
end
|
||||
|
||||
return json.encode(unified)
|
||||
end
|
||||
|
||||
function adapter.transform_stream_chunk(raw_chunk)
|
||||
local ok, chunk = pcall(json.decode, raw_chunk)
|
||||
if not ok then return "" end
|
||||
|
||||
-- OpenAI-style streams may attach usage to a chunk with empty choices
|
||||
-- (the final usage chunk). Preserve it; the gateway emits it as the
|
||||
-- terminal usage chunk. Note: keys must match Go's TokenUsage json tags
|
||||
-- (prompt/completion/total); the gateway re-emits standard *_tokens.
|
||||
local uses = nil
|
||||
if type(chunk.usage) == "table" then
|
||||
uses = {
|
||||
prompt = chunk.usage.prompt_tokens or chunk.usage.prompt or 0,
|
||||
completion = chunk.usage.completion_tokens or chunk.usage.completion or 0,
|
||||
total = chunk.usage.total_tokens or chunk.usage.total or 0,
|
||||
}
|
||||
-- 独立字段与 prompt_tokens_details **并存**透传,不要写成 elseif:
|
||||
-- 上游命中缓存时两者都发,非流式路径也是两个都带。写成 elseif 会让
|
||||
-- 流式丢掉 prompt_cache_hit_tokens/miss,与同一源的**非流式**产出不一致
|
||||
-- (dsh 优先读 details,但只认独立字段的老客户端会看不到缓存)。
|
||||
if type(chunk.usage.prompt_tokens_details) == "table" and chunk.usage.prompt_tokens_details.cached_tokens ~= nil then
|
||||
uses.prompt_tokens_details = { cached_tokens = chunk.usage.prompt_tokens_details.cached_tokens }
|
||||
end
|
||||
if (chunk.usage.prompt_cache_hit_tokens or 0) > 0 then
|
||||
uses.prompt_cache_hit_tokens = chunk.usage.prompt_cache_hit_tokens
|
||||
uses.prompt_cache_miss_tokens = chunk.usage.prompt_cache_miss_tokens or 0
|
||||
if uses.prompt_tokens_details == nil then
|
||||
uses.prompt_tokens_details = { cached_tokens = chunk.usage.prompt_cache_hit_tokens }
|
||||
end
|
||||
end
|
||||
local ctd = chunk.usage.completion_tokens_details
|
||||
if type(ctd) == "table" and ctd.reasoning_tokens ~= nil then
|
||||
uses.completion_tokens_details = { reasoning_tokens = ctd.reasoning_tokens }
|
||||
end
|
||||
end
|
||||
|
||||
if not chunk.choices or #chunk.choices == 0 then
|
||||
-- 空 choices 的帧既不是内容也不是结束信号。上游发两种:
|
||||
-- {"choices":[],"usage":{...}} —— 终帧用量
|
||||
-- {"choices":[],"cost":"0"} —— 单独的费用帧
|
||||
-- 两者可能落在同一帧上,必须都转出去;早退只带 usage 会把费用丢干净。
|
||||
local out = { done = false }
|
||||
if uses ~= nil then out.usage = uses end
|
||||
if chunk.cost ~= nil then out.cost = tostring(chunk.cost) end
|
||||
if uses == nil and chunk.cost == nil then return "" end
|
||||
return json.encode(out)
|
||||
end
|
||||
local delta = chunk.choices[1].delta or {}
|
||||
local fr = chunk.choices[1].finish_reason
|
||||
|
||||
local finish = (type(fr) == "string" and fr ~= "") and fr or nil
|
||||
|
||||
local unified = {
|
||||
content = delta.content or "",
|
||||
done = (finish ~= nil)
|
||||
}
|
||||
if finish then
|
||||
unified.finish_reason = finish
|
||||
end
|
||||
if uses ~= nil then
|
||||
unified.usage = uses
|
||||
end
|
||||
-- zen 用 reasoning 字段承载推理文本(OpenAI 惯例是 reasoning_content)
|
||||
local reasoning = delta.reasoning_content or delta.reasoning
|
||||
if reasoning then
|
||||
unified.reasoning_content = reasoning
|
||||
end
|
||||
if delta.tool_calls then
|
||||
-- pass raw streaming fragments through; OpenAI clients accumulate index+id+name+arguments
|
||||
unified.tool_calls = delta.tool_calls
|
||||
end
|
||||
return json.encode(unified)
|
||||
end
|
||||
|
||||
-- 错误收敛(可选钩子):zen 错误信封固定为 {error={type,message}};
|
||||
-- 免费池限流 FreeUsageLimitError 单独标注。返回 nil 走通用兜底。
|
||||
function adapter.transform_error(status, body)
|
||||
local ok, resp = pcall(json.decode, body)
|
||||
if not ok or type(resp) ~= "table" then return nil end
|
||||
local e = resp.error
|
||||
if type(e) ~= "table" then return nil end
|
||||
if e.type == "FreeUsageLimitError" then
|
||||
return "zen free pool quota exhausted"
|
||||
end
|
||||
return e.message
|
||||
end
|
||||
|
||||
return adapter
|
||||
@ -1,342 +0,0 @@
|
||||
local adapter = {}
|
||||
|
||||
adapter.name = "opencodezen"
|
||||
adapter.version = "1.0.0"
|
||||
adapter.endpoint = "/chat/completions"
|
||||
|
||||
-- OpenCode Zen(按量付费端点,https://opencode.ai/zen/v1)
|
||||
--
|
||||
-- 与 OpenCode Go(opencodego.lua,https://opencode.ai/zen/go/v1)是两个不同的
|
||||
-- 服务,行为要求并不相同,因此各有专用适配器:
|
||||
--
|
||||
-- * 本适配器(Zen)服务免费池 / 按量付费池。免费模型必须带 opencode 客户端
|
||||
-- 指纹(UA + x-opencode-*),否则上游拒绝:"free tier can only be used in
|
||||
-- OpenCode"。Zen 的免费池以非 thinking 模型为主,历史上回传
|
||||
-- reasoning_content 会引出上游报错,故这里剥掉(Go 侧相反,见 opencodego.lua)。
|
||||
-- * Go 订阅读者请看 opencodego.lua:那边**必须**回传 reasoning_content,
|
||||
-- 否则 thinking 模式直接 400。
|
||||
--
|
||||
-- 缺 x-opencode-client/session/request/project 身份头会被判为匿名客户端,
|
||||
-- 部分模型在带 tools 时上游直接失败(流式返回单 chunk
|
||||
-- "finish_reason":"network_error" 空 content,非流式 503
|
||||
-- "Endpoint is unavailable",表现为"空回复")。
|
||||
adapter.headers = {
|
||||
["User-Agent"] = "opencode/1.18.21 ai-sdk/provider-utils/4.0.23 runtime/bun/1.3.14",
|
||||
}
|
||||
|
||||
-- 每请求生成身份头。沙箱无 os/math,用 meta.timestamp + 请求体哈希派生:
|
||||
-- 同秒内重复请求 id 相同可接受(zen 只校验存在性,不校验格式)。
|
||||
local function rand_id(prefix, seed)
|
||||
return prefix .. string.sub(sha256_hex(seed), 1, 24)
|
||||
end
|
||||
|
||||
-- 会话指纹:取历史里**第一条 user 消息**。
|
||||
--
|
||||
-- 为什么不直接用客户端的会话 id:实测抓包(tcpdump 抓 127.0.0.1:8081 的真实
|
||||
-- agent 请求)确认通用 OpenAI 客户端**根本不发**任何会话标识 —— body 里没有
|
||||
-- user / session_id / conversation_id / metadata,请求头也只有 X-Stainless-*
|
||||
-- (OpenAI JS SDK)与 User-Agent。opencode 原生客户端那套 x-opencode-session
|
||||
-- 是它自己的概念,通用客户端无从转发。
|
||||
--
|
||||
-- 退而求其次但要够用:历史被逐轮重放,**第一条 user 消息在整个会话里恒定**,
|
||||
-- 用它做指纹即可得到"每会话一个 session",而不是"每源一个 session"。
|
||||
-- 拿不到时(无 user 消息)返回空串,调用方回退到按源稳定。
|
||||
local function conversation_fingerprint(body)
|
||||
if type(body) ~= "string" or body == "" then return "" end
|
||||
local ok, req = pcall(json.decode, body)
|
||||
if not ok or type(req) ~= "table" then return "" end
|
||||
for _, m in ipairs(req.messages or {}) do
|
||||
if type(m) == "table" and m.role == "user" then
|
||||
local c = m.content
|
||||
if type(c) == "string" then
|
||||
if c ~= "" then return c end
|
||||
elseif type(c) == "table" then
|
||||
for _, part in ipairs(c) do
|
||||
if type(part) == "table" and part.type == "text" and part.text then
|
||||
return part.text
|
||||
end
|
||||
end
|
||||
end
|
||||
return ""
|
||||
end
|
||||
end
|
||||
return ""
|
||||
end
|
||||
|
||||
-- session_seed 决定上游会话号(x-opencode-session 的种子)。
|
||||
--
|
||||
-- 优先级:
|
||||
-- 1) 客户端自带的会话标识(meta.client_session)——pi 等在开启
|
||||
-- compat.sendSessionAffinityHeaders 后会发 x-session-affinity,
|
||||
-- 这是平台真实的会话 id,整个会话恒定且天然按会话隔离。
|
||||
-- 2) 退路:历史里**第一条 user 消息**做会话指纹。
|
||||
-- 3) 再退:只按源固定(连 user 消息都没有时)。
|
||||
--
|
||||
-- 为什么需要 2/3:通用 OpenAI 客户端默认**根本不发**会话标识 —— 实测抓包
|
||||
-- (tcpdump 抓 127.0.0.1:8081 真实 agent 请求)确认 body 里没有
|
||||
-- user / session_id / conversation_id / metadata,请求头也只有
|
||||
-- X-Stainless-*(OpenAI JS SDK)与 User-Agent。opencode 原生客户端那套
|
||||
-- x-opencode-session 是它自己的概念,通用客户端无从转发。
|
||||
--
|
||||
-- 为什么必须稳定:上游前缀缓存是**会话级**的。会话号每请求一变,
|
||||
-- 缓存永不命中(实测:固定会号第 2 次命中 5888,每请求换会号则恒为 0)。
|
||||
local function session_seed(meta)
|
||||
local src = (meta.source and meta.source.name) or ""
|
||||
local base = "session|llmsproxy|" .. src
|
||||
local cs = meta.client_session
|
||||
if type(cs) == "string" and cs ~= "" then
|
||||
return base .. "|client|" .. cs
|
||||
end
|
||||
return base .. "|" .. conversation_fingerprint(meta.body)
|
||||
end
|
||||
|
||||
function adapter.build_headers(meta)
|
||||
local ts = tostring(meta.timestamp or "")
|
||||
local src = (meta.source and meta.source.name) or ""
|
||||
return {
|
||||
["User-Agent"] = adapter.headers["User-Agent"],
|
||||
["x-opencode-client"] = "cli",
|
||||
-- project 固定:同网关实例共享一个工作区身份
|
||||
["x-opencode-project"] = string.sub(sha256_hex("llmsproxy|" .. src), 1, 32),
|
||||
-- session 必须**按源稳定**,不能每请求换:上游的前缀缓存在同一 session
|
||||
-- 内才复用。实测(同一段 6032 token 提示词):
|
||||
-- 固定 session -> 第 2 次命中 5888/6032,cached_tokens=5888
|
||||
-- 每请求换 session -> 永远 0 命中
|
||||
-- 原先用 meta.timestamp 派生,等于每请求都是新会话,缓存永远无效,
|
||||
-- 上游也无法做会话亲和路由。
|
||||
["x-opencode-session"] = rand_id("ses_", session_seed(meta)),
|
||||
-- request id 仍每请求唯一(它只是请求标识,不参与缓存键)
|
||||
["x-opencode-request"] = rand_id("msg_", "request|" .. ts .. "|" .. tostring(meta.body or "")),
|
||||
}
|
||||
end
|
||||
|
||||
-- OpenAI /chat/completions format (pass-through, strip provider-specific fields)
|
||||
-- zen 上游 schema 只接受 text content part(无视觉/音频能力):多模态 part
|
||||
-- (image_url / input_audio / file 等)一律剥离。剥离后 content 变空的消息
|
||||
-- 若不再携带 tool_calls / tool_call_id 才整条丢弃(避免上游
|
||||
-- "unknown variant `image_url`, expected `text`");带工具调用的必须保留,
|
||||
-- 否则会把紧随其后的 tool 结果变成孤儿,模型会反复重发同一个调用。
|
||||
-- zen 上游角色白名单只有 system / user / assistant / tool / latest_reminder:
|
||||
-- OpenAI 的 developer(及 function 等)不在其中,直接透传会触发上游
|
||||
-- "unknown variant `developer`, expected one of ..." 错误;统一归一化为 system。
|
||||
local ROLE_WHITELIST = {
|
||||
system = true,
|
||||
user = true,
|
||||
assistant = true,
|
||||
tool = true,
|
||||
latest_reminder = true,
|
||||
}
|
||||
|
||||
function adapter.transform_request(raw_body)
|
||||
local ok, req = pcall(json.decode, raw_body)
|
||||
if not ok then return raw_body end
|
||||
req.disable_thinking = nil
|
||||
req.extra_body = nil
|
||||
-- stream_options is only valid alongside stream:true; sending it on a
|
||||
-- non-streaming request is rejected by strict upstreams.
|
||||
if req.stream then
|
||||
if type(req.stream_options) ~= "table" then req.stream_options = {} end
|
||||
req.stream_options.include_usage = true
|
||||
end
|
||||
if req.messages then
|
||||
local kept = {}
|
||||
for _, msg in ipairs(req.messages) do
|
||||
if type(msg.role) == "string" and not ROLE_WHITELIST[msg.role] then
|
||||
msg.role = "system"
|
||||
end
|
||||
-- Zen 免费池不接受回传 reasoning_content(Go 侧相反)
|
||||
msg.reasoning_content = nil
|
||||
local drop = false
|
||||
if type(msg.content) == "table" then
|
||||
local parts = {}
|
||||
for _, part in ipairs(msg.content) do
|
||||
if type(part) == "table" and part.type ~= nil and part.type ~= "text" then
|
||||
-- multimodal part not supported by zen
|
||||
else
|
||||
table.insert(parts, part)
|
||||
end
|
||||
end
|
||||
if #parts == 0 then
|
||||
-- Content collapsed to nothing after stripping unsupported
|
||||
-- parts. A message that still carries a tool call must
|
||||
-- NEVER be dropped: the very next message is its tool
|
||||
-- result, and dropping the call orphans that result. The
|
||||
-- model then sees a result for a call it never made and
|
||||
-- re-issues the same tool call on every turn (observed as
|
||||
-- an infinite "repeated tool call" loop).
|
||||
if (type(msg.tool_calls) == "table" and #msg.tool_calls > 0)
|
||||
or msg.tool_call_id ~= nil then
|
||||
msg.content = ""
|
||||
else
|
||||
drop = true
|
||||
end
|
||||
else
|
||||
msg.content = parts
|
||||
end
|
||||
end
|
||||
if not drop then
|
||||
table.insert(kept, msg)
|
||||
end
|
||||
end
|
||||
req.messages = kept
|
||||
end
|
||||
return json.encode(req)
|
||||
end
|
||||
|
||||
function adapter.transform_response(raw_body)
|
||||
local ok, resp = pcall(json.decode, raw_body)
|
||||
if not ok or resp == nil then return raw_body end
|
||||
|
||||
local unified = {
|
||||
content = "",
|
||||
finish_reason = "",
|
||||
token_usage = { prompt = 0, completion = 0, total = 0 }
|
||||
}
|
||||
|
||||
if type(resp.usage) == "table" then
|
||||
unified.token_usage.prompt = resp.usage.prompt_tokens or 0
|
||||
unified.token_usage.completion = resp.usage.completion_tokens or 0
|
||||
unified.token_usage.total = resp.usage.total_tokens or 0
|
||||
local hit = 0
|
||||
if type(resp.usage.prompt_tokens_details) == "table" and resp.usage.prompt_tokens_details.cached_tokens ~= nil then
|
||||
hit = resp.usage.prompt_tokens_details.cached_tokens
|
||||
unified.token_usage.prompt_tokens_details = { cached_tokens = hit }
|
||||
end
|
||||
if (resp.usage.prompt_cache_hit_tokens or 0) > 0 then
|
||||
unified.token_usage.prompt_cache_hit_tokens = resp.usage.prompt_cache_hit_tokens
|
||||
unified.token_usage.prompt_cache_miss_tokens = resp.usage.prompt_cache_miss_tokens or 0
|
||||
if hit == 0 then
|
||||
unified.token_usage.prompt_tokens_details = { cached_tokens = resp.usage.prompt_cache_hit_tokens }
|
||||
end
|
||||
end
|
||||
-- 上游报告输出里有多少是思考 token。不给客户端的话,无法判断
|
||||
-- completion_tokens 里多少是「可见回答」、多少是「思考」(都按输出计费)。
|
||||
local ctd = resp.usage.completion_tokens_details
|
||||
if type(ctd) == "table" and ctd.reasoning_tokens ~= nil then
|
||||
unified.token_usage.completion_tokens_details = { reasoning_tokens = ctd.reasoning_tokens }
|
||||
end
|
||||
end
|
||||
|
||||
-- 本次调用的费用,上游放在**顶层**且是**字符串**(如 "0"、"0.0012")。
|
||||
-- Go 订阅是包月制,恒为 "0";只有 Zen 按量付费模型才有信息量。
|
||||
-- 原样透传:网关不解析、不换算、不汇总 —— 它只是上游事实的搬运者。
|
||||
if resp.cost ~= nil then
|
||||
unified.cost = tostring(resp.cost)
|
||||
end
|
||||
|
||||
if type(resp.choices) == "table" and #resp.choices > 0 then
|
||||
local ch = resp.choices[1]
|
||||
if type(ch.message) == "table" then
|
||||
unified.content = ch.message.content or ""
|
||||
local reasoning = ch.message.reasoning_content or ch.message.reasoning
|
||||
if reasoning then
|
||||
unified.reasoning_content = reasoning
|
||||
end
|
||||
if type(ch.message.tool_calls) == "table" then
|
||||
local tcs = {}
|
||||
for _, tc in ipairs(ch.message.tool_calls) do
|
||||
local args_ok, args = pcall(json.decode, tc["function"].arguments)
|
||||
if not args_ok then args = {} end
|
||||
table.insert(tcs, {
|
||||
id = tc.id,
|
||||
type = tc.type or "function",
|
||||
name = tc["function"].name,
|
||||
arguments = args
|
||||
})
|
||||
end
|
||||
unified.tool_calls = tcs
|
||||
end
|
||||
end
|
||||
unified.finish_reason = ch.finish_reason or ""
|
||||
end
|
||||
|
||||
return json.encode(unified)
|
||||
end
|
||||
|
||||
function adapter.transform_stream_chunk(raw_chunk)
|
||||
local ok, chunk = pcall(json.decode, raw_chunk)
|
||||
if not ok then return "" end
|
||||
|
||||
-- OpenAI-style streams may attach usage to a chunk with empty choices
|
||||
-- (the final usage chunk). Preserve it; the gateway emits it as the
|
||||
-- terminal usage chunk. Note: keys must match Go's TokenUsage json tags
|
||||
-- (prompt/completion/total); the gateway re-emits standard *_tokens.
|
||||
local uses = nil
|
||||
if type(chunk.usage) == "table" then
|
||||
uses = {
|
||||
prompt = chunk.usage.prompt_tokens or chunk.usage.prompt or 0,
|
||||
completion = chunk.usage.completion_tokens or chunk.usage.completion or 0,
|
||||
total = chunk.usage.total_tokens or chunk.usage.total or 0,
|
||||
}
|
||||
-- 独立字段与 prompt_tokens_details **并存**透传,不要写成 elseif:
|
||||
-- 上游命中缓存时两者都发,非流式路径也是两个都带。写成 elseif 会让
|
||||
-- 流式丢掉 prompt_cache_hit_tokens/miss,与同一源的**非流式**产出不一致
|
||||
-- (dsh 优先读 details,但只认独立字段的老客户端会看不到缓存)。
|
||||
if type(chunk.usage.prompt_tokens_details) == "table" and chunk.usage.prompt_tokens_details.cached_tokens ~= nil then
|
||||
uses.prompt_tokens_details = { cached_tokens = chunk.usage.prompt_tokens_details.cached_tokens }
|
||||
end
|
||||
if (chunk.usage.prompt_cache_hit_tokens or 0) > 0 then
|
||||
uses.prompt_cache_hit_tokens = chunk.usage.prompt_cache_hit_tokens
|
||||
uses.prompt_cache_miss_tokens = chunk.usage.prompt_cache_miss_tokens or 0
|
||||
if uses.prompt_tokens_details == nil then
|
||||
uses.prompt_tokens_details = { cached_tokens = chunk.usage.prompt_cache_hit_tokens }
|
||||
end
|
||||
end
|
||||
local ctd = chunk.usage.completion_tokens_details
|
||||
if type(ctd) == "table" and ctd.reasoning_tokens ~= nil then
|
||||
uses.completion_tokens_details = { reasoning_tokens = ctd.reasoning_tokens }
|
||||
end
|
||||
end
|
||||
|
||||
if not chunk.choices or #chunk.choices == 0 then
|
||||
-- 空 choices 的帧既不是内容也不是结束信号。上游发两种:
|
||||
-- {"choices":[],"usage":{...}} —— 终帧用量
|
||||
-- {"choices":[],"cost":"0"} —— 单独的费用帧
|
||||
-- 两者可能落在同一帧上,必须都转出去;早退只带 usage 会把费用丢干净。
|
||||
local out = { done = false }
|
||||
if uses ~= nil then out.usage = uses end
|
||||
if chunk.cost ~= nil then out.cost = tostring(chunk.cost) end
|
||||
if uses == nil and chunk.cost == nil then return "" end
|
||||
return json.encode(out)
|
||||
end
|
||||
local delta = chunk.choices[1].delta or {}
|
||||
local fr = chunk.choices[1].finish_reason
|
||||
|
||||
local finish = (type(fr) == "string" and fr ~= "") and fr or nil
|
||||
|
||||
local unified = {
|
||||
content = delta.content or "",
|
||||
done = (finish ~= nil)
|
||||
}
|
||||
if finish then
|
||||
unified.finish_reason = finish
|
||||
end
|
||||
if uses ~= nil then
|
||||
unified.usage = uses
|
||||
end
|
||||
-- zen 用 reasoning 字段承载推理文本(OpenAI 惯例是 reasoning_content)
|
||||
local reasoning = delta.reasoning_content or delta.reasoning
|
||||
if reasoning then
|
||||
unified.reasoning_content = reasoning
|
||||
end
|
||||
if delta.tool_calls then
|
||||
-- pass raw streaming fragments through; OpenAI clients accumulate index+id+name+arguments
|
||||
unified.tool_calls = delta.tool_calls
|
||||
end
|
||||
return json.encode(unified)
|
||||
end
|
||||
|
||||
-- 错误收敛(可选钩子):zen 错误信封固定为 {error={type,message}};
|
||||
-- 免费池限流 FreeUsageLimitError 单独标注。返回 nil 走通用兜底。
|
||||
function adapter.transform_error(status, body)
|
||||
local ok, resp = pcall(json.decode, body)
|
||||
if not ok or type(resp) ~= "table" then return nil end
|
||||
local e = resp.error
|
||||
if type(e) ~= "table" then return nil end
|
||||
if e.type == "FreeUsageLimitError" then
|
||||
return "zen free pool quota exhausted"
|
||||
end
|
||||
return e.message
|
||||
end
|
||||
|
||||
return adapter
|
||||
235
internal/lua/adapters/qwen38.lua
Normal file
235
internal/lua/adapters/qwen38.lua
Normal file
@ -0,0 +1,235 @@
|
||||
-- tokenplan / discovery-api (intern-ai.org.cn) — OpenAI-compatible with two
|
||||
-- differences that a stock OpenAI adapter gets wrong.
|
||||
--
|
||||
-- 1. REASONING IS A SEPARATE FIELD, AND IT IS MOST OF THE OUTPUT.
|
||||
-- Every response carries `reasoning_content` (non-stream) or
|
||||
-- `delta.reasoning_content` (stream), disjoint from `content`. Measured on
|
||||
-- qwen3.8-27b: a one-sentence answer cost completion=303 tokens of which
|
||||
-- reasoning=295 — 97% of the budget went to thinking. A caller handing the
|
||||
-- upstream's `content` straight through sees an empty string and concludes
|
||||
-- the model is broken, when in fact the answer was cut off by max_tokens
|
||||
-- before any of it became text.
|
||||
--
|
||||
-- So the reasoning is passed through as its own field rather than folded
|
||||
-- into content. Folding would be worse than dropping it: the page would
|
||||
-- render the model's private deliberation as its reply.
|
||||
--
|
||||
-- 2. `finish_reason: "length"` WITH `content: null` IS NORMAL HERE, NOT AN ERROR.
|
||||
-- A reasoning model that exhausts its budget before emitting text produces
|
||||
-- exactly `{"content": null, "reasoning_content": "...", "finish_reason":
|
||||
-- "length"}`. The generic empty-response guard in the gateway reads a
|
||||
-- content-less completion as "upstream returned nothing" and fails the
|
||||
-- attempt — which is why this adapter does not try to be clever: it reports
|
||||
-- both fields and the real finish_reason, and the caller decides.
|
||||
--
|
||||
-- 3. MODEL NAMES ARE REWRITTEN BY THE SERVER.
|
||||
-- Requesting `qwen3.8-27b` answers with `model: "qwen3.8-27b-fp8"`. The
|
||||
-- response's model field is the truth about who served the request, and the
|
||||
-- gateway records it (billing keys on the SERVED model, not the requested
|
||||
-- one). Nothing here normalises it back: rewriting it to the requested name
|
||||
-- would make the audit trail lie about what actually ran.
|
||||
--
|
||||
-- Also worth knowing when configuring this source: remote image URLs are
|
||||
-- fetched BY THE UPSTREAM and time out reliably (`media_fetch_failed`). Images
|
||||
-- must be sent inline as data: URLs. Measured: base64 worked, a Wikimedia URL
|
||||
-- did not.
|
||||
|
||||
local adapter = {}
|
||||
|
||||
adapter.name = "qwen38"
|
||||
adapter.version = "1.0.0"
|
||||
adapter.endpoint = "/chat/completions"
|
||||
adapter.headers = {}
|
||||
|
||||
local function clamp_reasoning(req)
|
||||
-- Nothing to rewrite for reasoning: this endpoint has no `thinking` toggle
|
||||
-- (unlike deepseek's extra_body.thinking). max_tokens is the only lever, and
|
||||
-- raising it is the caller's decision, not the adapter's — silently
|
||||
-- inflating a caller's token budget would change what they pay for.
|
||||
end
|
||||
|
||||
function adapter.transform_request(raw_body)
|
||||
local ok, req = pcall(json.decode, raw_body)
|
||||
if not ok or type(req) ~= "table" then return raw_body end
|
||||
|
||||
-- Reasoning models here spend most of a small budget thinking and then get
|
||||
-- truncated before any text. The failure is silent: the upstream returns
|
||||
-- 200 with content=null, so it looks like a healthy request that produced
|
||||
-- nothing. Warn rather than rewrite — see clamp_reasoning.
|
||||
if type(req.max_tokens) == "number" and req.max_tokens > 0 and req.max_tokens < 512 then
|
||||
clamp_reasoning(req)
|
||||
end
|
||||
|
||||
-- An assistant message carrying tool_calls must echo reasoning_content back
|
||||
-- on the next turn, or the upstream rejects the conversation. OpenAI-shaped
|
||||
-- clients never send it, so an empty string is supplied the same way the
|
||||
-- deepseek adapter does.
|
||||
if type(req.messages) == "table" then
|
||||
for _, msg in ipairs(req.messages) do
|
||||
if type(msg) == "table" and msg.role == "assistant" then
|
||||
if type(msg.tool_calls) == "table" and msg.tool_calls[1] ~= nil
|
||||
and msg.reasoning_content == nil then
|
||||
msg.reasoning_content = ""
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return json.encode(req)
|
||||
end
|
||||
|
||||
function adapter.transform_response(raw_body)
|
||||
local ok, resp = pcall(json.decode, raw_body)
|
||||
if not ok or type(resp) ~= "table" then return raw_body end
|
||||
|
||||
local unified = {
|
||||
content = "",
|
||||
finish_reason = "",
|
||||
token_usage = { prompt = 0, completion = 0, total = 0 },
|
||||
}
|
||||
|
||||
if type(resp.usage) == "table" then
|
||||
local u = resp.usage
|
||||
unified.token_usage.prompt = u.prompt_tokens or 0
|
||||
unified.token_usage.completion = u.completion_tokens or 0
|
||||
unified.token_usage.total = u.total_tokens or 0
|
||||
if type(u.prompt_tokens_details) == "table" and u.prompt_tokens_details.cached_tokens ~= nil then
|
||||
unified.token_usage.prompt_tokens_details = { cached_tokens = u.prompt_tokens_details.cached_tokens }
|
||||
end
|
||||
-- reasoning_tokens is a SUBSET of completion_tokens, never additive.
|
||||
--
|
||||
-- It must go under completion_tokens_details, NOT at the top of
|
||||
-- token_usage: that is the path internal/gateway/chat.go reads when it
|
||||
-- fills the audit record's Reasoning field. Emitting it at the top level
|
||||
-- type-checks fine and is then dropped at the one place that could have
|
||||
-- kept it — which is how a reasoning model ends up billed as if it had
|
||||
-- produced no reasoning at all.
|
||||
--
|
||||
-- (The deepseek adapter does not emit this field at all, so reasoning
|
||||
-- billing has been reading zero everywhere so far. That is a separate
|
||||
-- gap; this adapter does not depend on it being fixed.)
|
||||
if type(u.completion_tokens_details) == "table"
|
||||
and u.completion_tokens_details.reasoning_tokens ~= nil then
|
||||
local d = unified.token_usage.completion_tokens_details
|
||||
if type(d) ~= "table" then
|
||||
d = {}
|
||||
unified.token_usage.completion_tokens_details = d
|
||||
end
|
||||
d.reasoning_tokens = u.completion_tokens_details.reasoning_tokens
|
||||
end
|
||||
end
|
||||
|
||||
if type(resp.choices) == "table" and #resp.choices > 0 then
|
||||
local ch = resp.choices[1]
|
||||
if type(ch.message) == "table" then
|
||||
local m = ch.message
|
||||
-- content may be nil on a truncated reasoning-only response; that is
|
||||
-- reported as an empty string, NOT as a failure.
|
||||
if type(m.content) == "string" then
|
||||
unified.content = m.content
|
||||
end
|
||||
if type(m.reasoning_content) == "string" then
|
||||
unified.reasoning_content = m.reasoning_content
|
||||
end
|
||||
if type(m.tool_calls) == "table" then
|
||||
local tcs = {}
|
||||
for _, tc in ipairs(m.tool_calls) do
|
||||
local fn = tc["function"] or {}
|
||||
local args_ok, args = pcall(json.decode, fn.arguments or "{}")
|
||||
if not args_ok then args = {} end
|
||||
table.insert(tcs, {
|
||||
id = tc.id,
|
||||
type = tc.type or "function",
|
||||
name = fn.name,
|
||||
arguments = args,
|
||||
})
|
||||
end
|
||||
unified.tool_calls = tcs
|
||||
end
|
||||
end
|
||||
unified.finish_reason = ch.finish_reason or ""
|
||||
end
|
||||
|
||||
return json.encode(unified)
|
||||
end
|
||||
|
||||
function adapter.transform_stream_chunk(raw_chunk)
|
||||
local ok, chunk = pcall(json.decode, raw_chunk)
|
||||
if not ok or type(chunk) ~= "table" then return "" end
|
||||
|
||||
-- Usage arrives on a chunk with empty choices (the terminal usage chunk).
|
||||
-- Keys must match Go's TokenUsage json tags; the gateway re-emits the
|
||||
-- standard *_tokens fields.
|
||||
local uses = nil
|
||||
if type(chunk.usage) == "table" then
|
||||
local u = chunk.usage
|
||||
uses = {
|
||||
prompt = u.prompt_tokens or 0,
|
||||
completion = u.completion_tokens or 0,
|
||||
total = u.total_tokens or 0,
|
||||
}
|
||||
if type(u.prompt_tokens_details) == "table" and u.prompt_tokens_details.cached_tokens ~= nil then
|
||||
uses.prompt_tokens_details = { cached_tokens = u.prompt_tokens_details.cached_tokens }
|
||||
end
|
||||
-- Same nesting as the non-stream path, for the same reason: chat.go reads
|
||||
-- reasoning under completion_tokens_details and nothing else.
|
||||
if type(u.completion_tokens_details) == "table"
|
||||
and u.completion_tokens_details.reasoning_tokens ~= nil then
|
||||
uses.completion_tokens_details = {
|
||||
reasoning_tokens = u.completion_tokens_details.reasoning_tokens
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
if type(chunk.choices) ~= "table" or #chunk.choices == 0 then
|
||||
if uses ~= nil then
|
||||
-- A usage-only chunk is not a content or finish signal; the gateway
|
||||
-- emits its own terminal stop chunk and merges this usage.
|
||||
return json.encode({ usage = uses, done = false })
|
||||
end
|
||||
return ""
|
||||
end
|
||||
|
||||
local ch = chunk.choices[1]
|
||||
local delta = ch.delta or {}
|
||||
local finish = (type(ch.finish_reason) == "string" and ch.finish_reason ~= "") and ch.finish_reason or nil
|
||||
|
||||
local unified = {
|
||||
content = delta.content or "",
|
||||
done = (finish ~= nil),
|
||||
}
|
||||
if finish then
|
||||
unified.finish_reason = finish
|
||||
end
|
||||
if uses ~= nil then
|
||||
unified.usage = uses
|
||||
end
|
||||
-- The whole point of this adapter: reasoning is streamed as its own channel
|
||||
-- and must stay separate. Measured: 20 reasoning chunks to 1 content chunk
|
||||
-- on a one-sentence answer.
|
||||
if type(delta.reasoning_content) == "string" then
|
||||
unified.reasoning_content = delta.reasoning_content
|
||||
end
|
||||
if type(delta.tool_calls) == "table" then
|
||||
unified.tool_calls = delta.tool_calls
|
||||
end
|
||||
return json.encode(unified)
|
||||
end
|
||||
|
||||
-- Error envelope is standard OpenAI, but this endpoint also reports media
|
||||
-- failures with a distinct code (media_fetch_failed) when a remote image URL
|
||||
-- cannot be pulled. Both carry {error:{message,...}}; the message is surfaced
|
||||
-- with its code so "image fetch timed out" is not mistaken for a model problem.
|
||||
function adapter.transform_error(status, body)
|
||||
local ok, resp = pcall(json.decode, body)
|
||||
if not ok or type(resp) ~= "table" then return nil end
|
||||
local e = resp.error
|
||||
if type(e) ~= "table" then return nil end
|
||||
if type(e.message) ~= "string" then return nil end
|
||||
if type(e.code) == "string" and e.code ~= "" and e.code ~= "invalid_api_key" then
|
||||
return e.message .. " (" .. e.code .. ")"
|
||||
end
|
||||
return e.message
|
||||
end
|
||||
|
||||
return adapter
|
||||
@ -12,7 +12,13 @@ adapter.headers = {}
|
||||
function adapter.transform_request(raw_body)
|
||||
local ok, req = pcall(json.decode, raw_body)
|
||||
if not ok then return raw_body end
|
||||
req.disable_thinking = nil
|
||||
-- The gateway's private control fields are GONE from the body (v2): the
|
||||
-- neutral `thinking` field carries that intent and each adapter maps it in
|
||||
-- its own protocol terms. These upstreams have no thinking switch at all,
|
||||
-- so the field is simply dropped here — which is what the nil-ing below
|
||||
-- used to do, except the field being dropped is no longer the kernel's
|
||||
-- private one.
|
||||
req.thinking = nil
|
||||
req.extra_body = nil
|
||||
if req.messages then
|
||||
for _, msg in ipairs(req.messages) do
|
||||
|
||||
@ -15,7 +15,13 @@ adapter.headers = {}
|
||||
function adapter.transform_request(raw_body)
|
||||
local ok, req = pcall(json.decode, raw_body)
|
||||
if not ok then return raw_body end
|
||||
req.disable_thinking = nil
|
||||
-- The gateway's private control fields are GONE from the body (v2): the
|
||||
-- neutral `thinking` field carries that intent and each adapter maps it in
|
||||
-- its own protocol terms. These upstreams have no thinking switch at all,
|
||||
-- so the field is simply dropped here — which is what the nil-ing below
|
||||
-- used to do, except the field being dropped is no longer the kernel's
|
||||
-- private one.
|
||||
req.thinking = nil
|
||||
req.extra_body = nil
|
||||
if req.messages then
|
||||
for _, msg in ipairs(req.messages) do
|
||||
|
||||
@ -1067,7 +1067,7 @@ func TestReasoningBilledAtCompletionRateByDefault(t *testing.T) {
|
||||
"model": "m", "source": "s", "key": "***aaaaaa", "ok": true,
|
||||
"prompt_tokens": 1000, "completion_tokens": 1000,
|
||||
"reasoning_tokens": 800, // all of it thinking; still billed as output
|
||||
"time": 1750000000000,
|
||||
"time": 1750000000000,
|
||||
})
|
||||
st := stateOf(t, ps)
|
||||
cost := st["total"].(map[string]interface{})["cost"].(float64)
|
||||
|
||||
@ -427,11 +427,11 @@ func TestBillingUIShowsRecentUnpriced(t *testing.T) {
|
||||
}
|
||||
src := string(raw)
|
||||
for _, needle := range []string{
|
||||
"unpriced_by_day", // the tally is written
|
||||
"recentUnpriced", // it is reduced to a window
|
||||
"unpriced_recent", // it reaches the view
|
||||
"unpriced7d", // the card labels it
|
||||
"unpricedClean", // and says so when the window is clear
|
||||
"unpriced_by_day", // the tally is written
|
||||
"recentUnpriced", // it is reduced to a window
|
||||
"unpriced_recent", // it reaches the view
|
||||
"unpriced7d", // the card labels it
|
||||
"unpricedClean", // and says so when the window is clear
|
||||
} {
|
||||
if !strings.Contains(src, needle) {
|
||||
t.Errorf("billing UI lacks %q", needle)
|
||||
@ -450,9 +450,9 @@ func TestBillingRulesUIExposesReasoningPrice(t *testing.T) {
|
||||
}
|
||||
src := string(raw)
|
||||
for _, needle := range []string{
|
||||
`class='m-r'`, // the input, in both the existing rows and the "+ model" row
|
||||
`class='m-r'`, // the input, in both the existing rows and the "+ model" row
|
||||
`models[n].reasoning`, // collected back out of the DOM on save
|
||||
`reasoningHint`, // and it says what leaving it blank means
|
||||
`reasoningHint`, // and it says what leaving it blank means
|
||||
} {
|
||||
if !strings.Contains(src, needle) {
|
||||
t.Errorf("rules editor lacks %q", needle)
|
||||
@ -481,11 +481,11 @@ func TestBillingRulesUIExposesLongContextStep(t *testing.T) {
|
||||
}
|
||||
src := string(raw)
|
||||
for _, needle := range []string{
|
||||
`class='m-lc-a'`, // threshold
|
||||
`class='m-lc-p'`, // absolute rate
|
||||
`class='m-lc-m'`, // multiplier
|
||||
`class='m-lc-a'`, // threshold
|
||||
`class='m-lc-p'`, // absolute rate
|
||||
`class='m-lc-m'`, // multiplier
|
||||
`models[n].long_context`, // collected back out on save
|
||||
`lcHint`, // explains that blank = no step
|
||||
`lcHint`, // explains that blank = no step
|
||||
} {
|
||||
if !strings.Contains(src, needle) {
|
||||
t.Errorf("rules editor lacks %q", needle)
|
||||
|
||||
287
internal/lua/conflict_arbitration_test.go
Normal file
287
internal/lua/conflict_arbitration_test.go
Normal file
@ -0,0 +1,287 @@
|
||||
package lua
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestRouteConflictDetectedAndArbitrated(用户要求 A):
|
||||
// 两个插件声明同一条 /api/plugins/http/ 服务路径 → 必须报冲突(列出双方),
|
||||
// 运维裁决后裁决生效;裁决持久化,重启(重新 LoadDir)后仍然生效。
|
||||
// 全程不崩溃、不拒绝加载 —— 两个插件都保持 loaded。
|
||||
func TestRouteConflictDetectedAndArbitrated(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
_, ps := bridgeVM(t)
|
||||
ps.dir = dir
|
||||
|
||||
// a.lua 与 b.lua 都声明路径 "shared"
|
||||
const aSrc = `
|
||||
local P = {}
|
||||
P.name = "plugA"
|
||||
P.version = "1"
|
||||
P.exports = {}
|
||||
P.http = { { path = "shared", handler = "h", roles = {"admin"} } }
|
||||
function P.h(req) return { status = 200, json = { from = "A" } } end
|
||||
return P
|
||||
`
|
||||
const bSrc = `
|
||||
local P = {}
|
||||
P.name = "plugB"
|
||||
P.version = "1"
|
||||
P.exports = {}
|
||||
P.http = { { path = "shared", handler = "h", roles = {"admin"} } }
|
||||
function P.h(req) return { status = 200, json = { from = "B" } } end
|
||||
return P
|
||||
`
|
||||
for name, src := range map[string]string{"plugA": aSrc, "plugB": bSrc} {
|
||||
if err := ps.LoadSource(name, src); err != nil {
|
||||
t.Fatalf("加载 %s: %v", name, err)
|
||||
}
|
||||
}
|
||||
|
||||
// ① 冲突被报出来,候选齐整,未裁决
|
||||
cs := ps.RouteConflicts()
|
||||
if len(cs) != 1 {
|
||||
t.Fatalf("应恰有 1 条路径冲突, 实得 %d: %+v", len(cs), cs)
|
||||
}
|
||||
c := cs[0]
|
||||
if c.Path != "shared" {
|
||||
t.Fatalf("冲突路径应为 shared, 实得 %q", c.Path)
|
||||
}
|
||||
if len(c.Candidates) != 2 || c.Candidates[0] != "plugA" || c.Candidates[1] != "plugB" {
|
||||
t.Fatalf("候选应为 [plugA plugB], 实得 %v", c.Candidates)
|
||||
}
|
||||
if c.Resolved != "" {
|
||||
t.Fatalf("初始应未裁决, 实得 %q", c.Resolved)
|
||||
}
|
||||
// 两个插件都保持 loaded(不许因冲突拒绝启动/加载)
|
||||
if ps.List()[0]["loaded"] != true || ps.List()[1]["loaded"] != true {
|
||||
t.Fatal("冲突的两个插件都应保持 loaded")
|
||||
}
|
||||
|
||||
// ② 非法裁决被拒(赢家必须确实是候选之一)
|
||||
if err := ps.ResolveRouteConflict("shared", "plugC"); err == nil {
|
||||
t.Fatal("赢家不是候选应被拒绝")
|
||||
}
|
||||
if err := ps.ResolveRouteConflict("other", "plugA"); err == nil {
|
||||
t.Fatal("未冲突的路径不应可裁决")
|
||||
}
|
||||
|
||||
// ③ 合法裁决 + 持久化
|
||||
if err := ps.ResolveRouteConflict("shared", "plugB"); err != nil {
|
||||
t.Fatalf("合法裁决失败: %v", err)
|
||||
}
|
||||
if ps.RouteWinner("shared") != "plugB" {
|
||||
t.Fatalf("裁决后赢家应为 plugB, 实得 %q", ps.RouteWinner("shared"))
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(dir, ".route-conflicts.json")); err != nil {
|
||||
t.Fatalf("裁决应落盘 .route-conflicts.json: %v", err)
|
||||
}
|
||||
|
||||
// ④ 重启语义:新 registry 读回裁决
|
||||
_, ps2 := bridgeVM(t)
|
||||
ps2.dir = dir
|
||||
if err := ps2.LoadRouteConflicts(); err != nil {
|
||||
t.Fatalf("读回裁决: %v", err)
|
||||
}
|
||||
if err := ps2.LoadSource("plugA", aSrc); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := ps2.LoadSource("plugB", bSrc); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cs2 := ps2.RouteConflicts()
|
||||
if len(cs2) != 1 || cs2[0].Resolved != "plugB" {
|
||||
t.Fatalf("重启后裁决应保留 plugB, 实得 %+v", cs2)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRouteConflictWinnerGoneBackToUnresolved:已裁决的赢家插件被删/改后不再
|
||||
// 声明该路径 → 裁决作废回到未裁决态,而不是指向一个不存在的赢家。
|
||||
func TestRouteConflictWinnerGoneBackToUnresolved(t *testing.T) {
|
||||
_, ps := bridgeVM(t)
|
||||
const aSrc = `
|
||||
local P = {}
|
||||
P.name = "plugA"
|
||||
P.http = { { path = "s", handler = "h", roles = {"admin"} } }
|
||||
function P.h(req) return { status = 200, json = {} } end
|
||||
return P
|
||||
`
|
||||
const bSrc = `
|
||||
local P = {}
|
||||
P.name = "plugB"
|
||||
P.http = { { path = "s", handler = "h", roles = {"admin"} } }
|
||||
function P.h(req) return { status = 200, json = {} } end
|
||||
return P
|
||||
`
|
||||
if err := ps.LoadSource("plugA", aSrc); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := ps.LoadSource("plugB", bSrc); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := ps.ResolveRouteConflict("s", "plugA"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// plugA 被禁用 → 不再占路由 → 冲突消失(不是回未裁决)
|
||||
if err := ps.SetEnabled("plugA", false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := ps.RouteConflicts(); len(got) != 0 {
|
||||
t.Fatalf("只 plugB 声明时不应再有冲突, 实得 %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestAdapterNameConflictSuspendsForArbitration(用户要求 B):
|
||||
// 插件安装与内置同名的适配器 → 不再硬拒绝,而是挂起待裁决:
|
||||
// - 现任内置继续服务(版本号不变)
|
||||
// - 返回 ErrAdapterConflictPending(typed,可区分「代码错」)
|
||||
// - 冲突出现在 AdapterConflicts() 列表
|
||||
// - 运维 ResolveAdapterConflict("incumbent") → 现任保留、冲突清除
|
||||
// - 运维 ResolveAdapterConflict("candidate") → 插件版真正安装、归属转移
|
||||
// - 裁决持久化;重启后插件再装同名适配器自动套用上次裁决
|
||||
func TestAdapterNameConflictSuspendsForArbitration(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
_, ps := bridgeVM(t)
|
||||
ps.dir = dir
|
||||
|
||||
builtin := `local a={} a.name="openai" a.version="BUILTIN" a.endpoint="/x" return a`
|
||||
if err := ps.vm.LoadAdapterSource("openai", builtin); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
evil := `local a={} a.name="openai" a.version="PLUGIN_V1" a.endpoint="/x" return a`
|
||||
|
||||
// ① 安装挂起:现任保留,typed 错误,不静默覆盖
|
||||
got, err := ps.InstallAdapter("sneaky", "openai", evil)
|
||||
if err == nil {
|
||||
t.Fatal("撞内置名应返回错误(挂起)")
|
||||
}
|
||||
var pend *ErrAdapterConflictPending
|
||||
if !errors.As(err, &pend) {
|
||||
t.Fatalf("错误类型应为 ErrAdapterConflictPending, got %T: %v", err, err)
|
||||
}
|
||||
if got != "" {
|
||||
t.Fatalf("挂起时不得返回生效名, 实得 %q", got)
|
||||
}
|
||||
if v := ps.vm.AdapterVersion("openai"); v != "BUILTIN" {
|
||||
t.Fatalf("现任内置必须继续服务, 版本实得 %q", v)
|
||||
}
|
||||
|
||||
// ② 冲突可见
|
||||
ac := ps.AdapterConflicts()
|
||||
if len(ac) != 1 || ac[0].Name != "openai" || ac[0].Candidate != "sneaky" {
|
||||
t.Fatalf("挂起冲突应可见: %+v", ac)
|
||||
}
|
||||
|
||||
// ③ 裁决 incumbent → 现任保留
|
||||
if err := ps.ResolveAdapterConflict("openai", "incumbent"); err != nil {
|
||||
t.Fatalf("裁决 incumbent: %v", err)
|
||||
}
|
||||
if v := ps.vm.AdapterVersion("openai"); v != "BUILTIN" {
|
||||
t.Fatalf("裁决 incumbent 后内置应保留, 实得 %q", v)
|
||||
}
|
||||
if got := ps.AdapterConflicts(); len(got) != 0 {
|
||||
t.Fatalf("裁决后冲突应清除, 实得 %+v", got)
|
||||
}
|
||||
// 归属仍是 operator(内置)
|
||||
if o := ps.AdapterOwnerOf("openai"); o != "" {
|
||||
t.Fatalf("内置归属应为空(operator), 实得 %q", o)
|
||||
}
|
||||
|
||||
// ④ 重启后插件再装 → 之前的 incumbent 裁决自动套用(不再二次挂起,也不安装)
|
||||
if err := ps.LoadRouteConflicts(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = ps.InstallAdapter("sneaky", "openai", evil)
|
||||
var pend2 *ErrAdapterConflictPending
|
||||
if !errors.As(err, &pend2) {
|
||||
t.Fatalf("自动套用 incumbent 裁决应仍返回 pending 错误(未安装), got %v", err)
|
||||
}
|
||||
if v := ps.vm.AdapterVersion("openai"); v != "BUILTIN" {
|
||||
t.Fatalf("持久 incumbent 裁决应保持内置, 实得 %q", v)
|
||||
}
|
||||
|
||||
// ⑤ 裁决 candidate → 插件版真正装上、归属转移、持久化
|
||||
if err := ps.ResolveAdapterConflict("openai", "candidate"); err != nil {
|
||||
t.Fatalf("裁决 candidate: %v", err)
|
||||
}
|
||||
if v := ps.vm.AdapterVersion("openai"); v != "PLUGIN_V1" {
|
||||
t.Fatalf("裁决 candidate 后插件版应生效, 实得 %q", v)
|
||||
}
|
||||
if o := ps.AdapterOwnerOf("openai"); o != "sneaky" {
|
||||
t.Fatalf("归属应转移给 sneaky, 实得 %q", o)
|
||||
}
|
||||
// 持久化文件里是 candidate:sneaky
|
||||
b, _ := os.ReadFile(filepath.Join(dir, ".route-conflicts.json"))
|
||||
if !strings.Contains(string(b), "candidate:sneaky") {
|
||||
t.Fatalf("裁决应持久化为 candidate:sneaky, 文件: %s", b)
|
||||
}
|
||||
}
|
||||
|
||||
// TestAdapterPluginConflictSuspendsForArbitration:插件 A 装了适配器后插件 B
|
||||
// 再装同名 → 挂起待裁决(与内置撞名同一机制),现任 A 的版本继续服务。
|
||||
func TestAdapterPluginConflictSuspendsForArbitration(t *testing.T) {
|
||||
_, ps := bridgeVM(t)
|
||||
if _, err := ps.InstallAdapter("alpha", "shared_auth", tokenAdapterV1); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err := ps.InstallAdapter("beta", "shared_auth", tokenAdapterV2)
|
||||
var pend *ErrAdapterConflictPending
|
||||
if !errors.As(err, &pend) {
|
||||
t.Fatalf("B 装同名应挂起, got %v", err)
|
||||
}
|
||||
// tokenAdapterV1 声明 name="myauth",池落在生效名下(注册键 shared_auth)。
|
||||
if v := ps.vm.AdapterVersion("myauth"); v != "1.0" {
|
||||
t.Fatalf("alpha 的现任版本应继续服务, 实得 %q", v)
|
||||
}
|
||||
ac := ps.AdapterConflicts()
|
||||
if len(ac) != 1 || ac[0].Incumbent != "plugin:alpha" {
|
||||
t.Fatalf("冲突应点名现任归属 plugin:alpha, 实得 %+v", ac)
|
||||
}
|
||||
// 裁决 candidate → beta 接管
|
||||
if err := ps.ResolveAdapterConflict("shared_auth", "candidate"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if v := ps.vm.AdapterVersion("myauth"); v != "2.0" {
|
||||
t.Fatalf("beta 版应生效, 实得 %q", v)
|
||||
}
|
||||
if o := ps.AdapterOwnerOf("myauth"); o != "beta" {
|
||||
t.Fatalf("归属应转移给 beta, 实得 %q", o)
|
||||
}
|
||||
}
|
||||
|
||||
// TestAdapterConflictRejectsBadWinner:裁决值必须合法;无挂起冲突时拒绝。
|
||||
func TestAdapterConflictRejectsBadWinner(t *testing.T) {
|
||||
_, ps := bridgeVM(t)
|
||||
if err := ps.ResolveAdapterConflict("nope", "candidate"); err == nil {
|
||||
t.Fatal("无挂起冲突应拒绝")
|
||||
}
|
||||
if _, err := ps.InstallAdapter("p", "myauth", tokenAdapterV1); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// myauth 是插件自己装的 → 更新路径(update 走 UpdateAdapter,非冲突)
|
||||
if err := ps.ResolveAdapterConflict("myauth", "bogus"); err == nil {
|
||||
t.Fatal("非法 winner 值应拒绝")
|
||||
}
|
||||
}
|
||||
|
||||
// TestPluginOwnUpdateStillWorks:插件更新自己装的适配器(token 轮换)不走冲突
|
||||
// 路径 —— 行为与改造前一致。守住「挂起只发生在撞名」这条边界。
|
||||
func TestPluginOwnUpdateStillWorks(t *testing.T) {
|
||||
_, ps := bridgeVM(t)
|
||||
if _, err := ps.InstallAdapter("authtoken", "myauth", tokenAdapterV1); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := ps.UpdateAdapter("authtoken", "myauth", tokenAdapterV2); err != nil {
|
||||
t.Fatalf("own update 不应受影响: %v", err)
|
||||
}
|
||||
if v := ps.vm.AdapterVersion("myauth"); v != "2.0" {
|
||||
t.Fatalf("轮换后版本应 2.0, 实得 %q", v)
|
||||
}
|
||||
if got := ps.AdapterConflicts(); len(got) != 0 {
|
||||
t.Fatalf("own update 不应产生冲突, 实得 %+v", got)
|
||||
}
|
||||
}
|
||||
498
internal/lua/kernel_api.go
Normal file
498
internal/lua/kernel_api.go
Normal file
@ -0,0 +1,498 @@
|
||||
package lua
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Package lua — 插件可调用的内核动作面(R4-c)
|
||||
//
|
||||
// 用户需求(原话):「现在这个接口不全,可以多给一些接口让 lua 调用内核。」
|
||||
//
|
||||
// 在此之前,插件只能通过 __llmsproxy_adapters 管适配器 —— 那是三个手写闭包。
|
||||
// 内核另有十几个公开动作(重载配置、清冷却、重探上游、增删源/密钥、改 AUTO 链…),
|
||||
// 插件一个都调不到。而"自愈 / 联动 / 自动孵化"这类插件能力恰恰需要它们:
|
||||
//
|
||||
// 探测到上游整片挂掉 → probe_all + reset_health
|
||||
// 发现额度变化 → save_auto_rules 重排链路
|
||||
// 拿到新端点 → add_source
|
||||
//
|
||||
// # 为什么 Go 侧 deadline 是可行的(而 Lua 死循环不可中断)
|
||||
//
|
||||
// 这是整个设计的支点。两者形态完全不同:
|
||||
//
|
||||
// Lua 里 while true do end → 无法中断(LuaJIT 限制,实测三次)
|
||||
// Lua 里 __llmsproxy_kernel.call(...) → **可以**超时
|
||||
//
|
||||
// 因为后者 Lua 只是在等一个返回值,不占用 Lua 栈的执行权。所以每次调用在 Go
|
||||
// 侧包一层 select + ctx deadline:超时就把 (nil, err) 还给 Lua。这比"Lua 投递
|
||||
// 请求、Go 队列执行"干净 —— 队列还要定义"超时后积压的命令算什么",而 deadline
|
||||
// 的语义是闭合的。
|
||||
//
|
||||
// # 权限:全开,但默认不授予
|
||||
//
|
||||
// 用户选定「全开」(13 个动作都可达)+ 「设置页可配权限」。全开描述的是**能力
|
||||
// 存在**,不是**默认授予**:一个插件能删密钥这件事必须由运维在设置页点过确认。
|
||||
// 因此默认拒绝,授权记录在 .access.json 里(复用现有覆盖机制)。
|
||||
//
|
||||
// danger 级动作必须**逐个**授权,不接受通配符 —— 否则运维图省事写个 "*",
|
||||
// "删密钥"就和"读状态"同权了。
|
||||
|
||||
// KernelAPI 是内核暴露给插件的能力面。由 internal/core 实现并注入。
|
||||
//
|
||||
// 依赖方向:internal/lua 不能 import internal/core(方向是 core → lua),
|
||||
// 所以这里定义接口、core 侧实现 —— 与 SetOutboundHosts 同一个注入先例。
|
||||
type KernelAPI interface {
|
||||
// Actions 列出可调用的动作。设置页据此渲染权限开关,插件据此发现能力。
|
||||
Actions() []KernelAction
|
||||
// Invoke 执行一个动作。实现**必须**尊重 ctx:ctx 取消后应尽快返回,
|
||||
// 否则插件侧的超时虽然会返回错误,但该动作仍在后台跑(变成不可见的泄漏)。
|
||||
Invoke(ctx context.Context, name string, args map[string]interface{}) (interface{}, error)
|
||||
}
|
||||
|
||||
// 危险级。它决定权限授予的严格程度,也决定设置页用什么颜色提示。
|
||||
const (
|
||||
// DangerRead 无副作用,只读。仍然需要授权,但可以一次授予全部。
|
||||
DangerRead = "read"
|
||||
// DangerWrite 修改运行状态或配置,但可恢复(重载、清冷却、改链路)。
|
||||
DangerWrite = "write"
|
||||
// DangerDestructive 删除类,或让服务中断。**必须逐个授权**。
|
||||
DangerDestructive = "destructive"
|
||||
)
|
||||
|
||||
// KernelAction 描述一个可调用动作。
|
||||
type KernelAction struct {
|
||||
// Deferred marks an action that MUST NOT run while the Lua caller holds its
|
||||
// plugin lock. The Lua entry points (hook, exported call, scheduled task) all
|
||||
// hold p.mu for the whole call, and an action that re-enters the plugin layer
|
||||
// (set_plugin_enabled reaches SetEnabled → p.mu.Lock()) would self-deadlock —
|
||||
// and with a deadline, would then execute invisibly AFTER the plugin was told
|
||||
// it failed.
|
||||
//
|
||||
// Such an action is queued and run on its own goroutine once the Lua call has
|
||||
// returned, so the caller gets "accepted" rather than a lock-up or a lie.
|
||||
Deferred bool
|
||||
|
||||
Name string `json:"name"`
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
Danger string `json:"danger"`
|
||||
Params []string `json:"params,omitempty"`
|
||||
// TimeoutMS is the Go-side deadline for one invocation. Long actions (a
|
||||
// full re-probe walks every source over the network) need a bigger budget
|
||||
// than a state read; making it per-action rather than one global number is
|
||||
// what keeps the deadline honest rather than arbitrary.
|
||||
TimeoutMS int `json:"timeout_ms"`
|
||||
}
|
||||
|
||||
// kernelCatalog is the full action set. It lives in the lua package (rather than
|
||||
// being discovered from the implementation) so that permissions can be validated
|
||||
// even when no Core is wired — tests, and a gateway with plugins but no kernel.
|
||||
//
|
||||
// Every action here maps to an existing Core method; adding one is a deliberate
|
||||
// act rather than a reflex. ListKeys/FindKey are deliberately ABSENT: they return
|
||||
// PLAINTEXT gateway keys, and handing those to third-party Lua would hand over
|
||||
// the deployment's entire credential surface. Key-related reads go through
|
||||
// masked forms instead.
|
||||
var kernelCatalog = []KernelAction{
|
||||
// ---- 只读 ----
|
||||
{Name: "status", Title: "运行状态", Danger: DangerRead, TimeoutMS: 3000,
|
||||
Description: "各源的调度状态(冷却、连续失败、最近错误)"},
|
||||
{Name: "list_sources", Title: "源列表", Danger: DangerRead, TimeoutMS: 3000,
|
||||
Description: "已配置的上游源(api_key 已掩码)"},
|
||||
{Name: "list_models", Title: "模型列表", Danger: DangerRead, TimeoutMS: 3000,
|
||||
Description: "当前可路由的模型 id 列表"},
|
||||
{Name: "list_adapters", Title: "适配器列表", Danger: DangerRead, TimeoutMS: 3000,
|
||||
Description: "已加载的适配器名与版本"},
|
||||
{Name: "list_auto_rules", Title: "AUTO 链路", Danger: DangerRead, TimeoutMS: 3000,
|
||||
Description: "当前全局 AUTO 链的槽位"},
|
||||
{Name: "adapter_owner", Title: "适配器归属", Danger: DangerRead, TimeoutMS: 3000,
|
||||
Params: []string{"name"},
|
||||
Description: "某适配器由谁安装(operator 或 plugin:名字)"},
|
||||
|
||||
// ---- 写(可恢复) ----
|
||||
{Name: "reload", Title: "重载配置", Danger: DangerWrite, TimeoutMS: 30000,
|
||||
Description: "重新读取运行配置并重建源/链路"},
|
||||
{Name: "reset_health", Title: "清除冷却", Danger: DangerWrite, TimeoutMS: 5000,
|
||||
Description: "清掉所有源的退避状态(修好上游后立即恢复)"},
|
||||
{Name: "probe_all", Title: "全量探测", Danger: DangerWrite, TimeoutMS: 60000,
|
||||
Description: "对每个源做一次真实可达性探测(走网络,慢)"},
|
||||
{Name: "save_auto_rules", Title: "设置 AUTO 链", Danger: DangerWrite, TimeoutMS: 10000,
|
||||
Params: []string{"entries"},
|
||||
Description: "替换全局 AUTO 链;entries 为 [{model, source, ...}] 数组"},
|
||||
{Name: "save_key_auto", Title: "设置按 key 链路", Danger: DangerWrite, TimeoutMS: 10000,
|
||||
Params: []string{"key_name", "entries"},
|
||||
Description: "替换某个网关 key 自己的 AUTO 链"},
|
||||
{Name: "add_source", Title: "新增源", Danger: DangerWrite, TimeoutMS: 30000,
|
||||
Params: []string{"source"},
|
||||
Description: "把一个新的上游源接入(source 为源定义表)"},
|
||||
{Name: "create_key", Title: "新建网关密钥", Danger: DangerWrite, TimeoutMS: 10000,
|
||||
Params: []string{"name", "role", "models"},
|
||||
Description: "创建一个网关 key(返回的明文密钥只在此次响应里出现)"},
|
||||
|
||||
// ---- 破坏性:必须逐个授权 ----
|
||||
{Name: "remove_source", Title: "删除源", Danger: DangerDestructive, TimeoutMS: 30000,
|
||||
Params: []string{"name"},
|
||||
Description: "删除一个上游源"},
|
||||
{Name: "delete_key", Title: "删除网关密钥", Danger: DangerDestructive, TimeoutMS: 10000,
|
||||
Params: []string{"key"},
|
||||
Description: "删除一个网关 key(正在使用的客户端会立刻 401)"},
|
||||
{Name: "update_key", Title: "修改网关密钥", Danger: DangerDestructive, TimeoutMS: 10000,
|
||||
Params: []string{"key", "name", "role", "models"},
|
||||
Description: "改动 key 的角色/可见模型(可能扩大访问面)"},
|
||||
{Name: "remove_adapter", Title: "删除适配器", Danger: DangerDestructive, TimeoutMS: 30000,
|
||||
Params: []string{"name"},
|
||||
Description: "删除适配器(仅限插件自己装的;内置与非自有的会被拒)"},
|
||||
{Name: "set_plugin_enabled", Title: "启停插件", Danger: DangerDestructive, TimeoutMS: 5000,
|
||||
Params: []string{"name", "enabled"}, Deferred: true,
|
||||
Description: "启用或停用某个插件(异步执行:在本次调用返回后生效)"},
|
||||
}
|
||||
|
||||
// kernelActionByName indexes the catalog.
|
||||
var kernelActionByName = func() map[string]KernelAction {
|
||||
m := make(map[string]KernelAction, len(kernelCatalog))
|
||||
for _, a := range kernelCatalog {
|
||||
m[a.Name] = a
|
||||
}
|
||||
return m
|
||||
}()
|
||||
|
||||
// KernelActions returns the catalog sorted by name (stable for the UI).
|
||||
func KernelActions() []KernelAction {
|
||||
out := append([]KernelAction(nil), kernelCatalog...)
|
||||
sort.Slice(out, func(i, j int) bool { return out[i].Name < out[j].Name })
|
||||
return out
|
||||
}
|
||||
|
||||
// LookupKernelAction finds an action, reporting whether it exists.
|
||||
func LookupKernelAction(name string) (KernelAction, bool) {
|
||||
a, ok := kernelActionByName[name]
|
||||
return a, ok
|
||||
}
|
||||
|
||||
// ---- 权限 ----
|
||||
|
||||
// errNotPermitted is returned when a plugin calls an action it was not granted.
|
||||
// It names the action and the plugin so the operator can act on it directly.
|
||||
func notPermitted(plugin, action string) error {
|
||||
return fmt.Errorf("kernel action %q is not granted to plugin %q; "+
|
||||
"enable it in the plugin's kernel permissions", action, plugin)
|
||||
}
|
||||
|
||||
// KernelPermission is the operator's grant for one plugin: the set of action
|
||||
// names it may call, plus whether it was granted "everything readable".
|
||||
type KernelPermission struct {
|
||||
// Actions is the explicit allowlist, already expanded (no wildcards stored,
|
||||
// so a NEW action added in a future release is NOT silently granted to an
|
||||
// existing plugin — that is the safe direction for a security default).
|
||||
Actions []string
|
||||
// AllRead is true when the operator granted every read-only action at once.
|
||||
// Stored separately from Actions so that a future read-only action IS covered
|
||||
// without re-saving, while writes still require an explicit grant.
|
||||
AllRead bool
|
||||
}
|
||||
|
||||
// clone returns a copy so callers cannot mutate the stored permission.
|
||||
func (p KernelPermission) clone() KernelPermission {
|
||||
return KernelPermission{Actions: append([]string(nil), p.Actions...), AllRead: p.AllRead}
|
||||
}
|
||||
|
||||
// allows reports whether the permission covers action.
|
||||
//
|
||||
// The "all readable" grant deliberately does NOT cover a future WRITE action:
|
||||
// expanding a wildcard into whatever happens to exist later is how a convenience
|
||||
// grant turns into a privilege escalation.
|
||||
func (p KernelPermission) allows(a KernelAction) bool {
|
||||
if p.AllRead && a.Danger == DangerRead {
|
||||
return true
|
||||
}
|
||||
for _, n := range p.Actions {
|
||||
if n == a.Name {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// ---- 调用(带 Go 侧超时) ----
|
||||
|
||||
// ErrKernelUnavailable is returned when no KernelAPI was injected (a gateway
|
||||
// running plugins without a wired core, e.g. some tests).
|
||||
var ErrKernelUnavailable = errors.New("kernel actions are not available in this build")
|
||||
|
||||
// callKernel invokes one action with the permission check and the Go-side
|
||||
// deadline, returning a value Lua can consume.
|
||||
//
|
||||
// The deadline is the whole point: a plugin can block here harmlessly, because
|
||||
// Lua is waiting on a return value rather than spinning. On timeout the caller
|
||||
// gets a normal error; the action's own goroutine is told to stop via ctx.
|
||||
func (ps *Plugins) callKernel(plugin, name string, args map[string]interface{}) (interface{}, error) {
|
||||
api := ps.kernel()
|
||||
if api == nil {
|
||||
return nil, ErrKernelUnavailable
|
||||
}
|
||||
action, ok := LookupKernelAction(name)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unknown kernel action %q", name)
|
||||
}
|
||||
if !ps.kernelPermitted(plugin, action) {
|
||||
return nil, notPermitted(plugin, name)
|
||||
}
|
||||
|
||||
timeout := time.Duration(action.TimeoutMS) * time.Millisecond
|
||||
if timeout <= 0 {
|
||||
timeout = 10 * time.Second
|
||||
}
|
||||
// The caller's identity is injected HERE, not taken from args: an action that
|
||||
// needs to know who is calling (remove_adapter, to enforce ownership) must not
|
||||
// be able to be lied to by the plugin passing someone else's name.
|
||||
if args == nil {
|
||||
args = map[string]interface{}{}
|
||||
}
|
||||
args["plugin"] = plugin
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
|
||||
type result struct {
|
||||
v interface{}
|
||||
err error
|
||||
}
|
||||
// A deferred action must not run under the caller's plugin lock, so it is
|
||||
// queued and returns "accepted" immediately. Reporting success here without
|
||||
// waiting is honest: the caller is told the action was ACCEPTED, and the
|
||||
// actual outcome lands in the plugin's hook_errors surface if it fails.
|
||||
if action.Deferred {
|
||||
ps.runDeferred(action, args)
|
||||
return map[string]interface{}{
|
||||
"accepted": true,
|
||||
"note": "this action runs asynchronously; check the plugin state/errors for the outcome",
|
||||
}, nil
|
||||
}
|
||||
|
||||
// The action runs on its own goroutine so this call can give up on time.
|
||||
// Buffered so a late-finishing action never blocks on sending.
|
||||
done := make(chan result, 1)
|
||||
go func() {
|
||||
defer func() {
|
||||
// A panicking kernel action must not take the gateway down through a
|
||||
// Lua call. Convert it into an error the plugin sees.
|
||||
if rec := recover(); rec != nil {
|
||||
done <- result{err: fmt.Errorf("kernel action %s panicked: %v", name, rec)}
|
||||
}
|
||||
}()
|
||||
v, err := api.Invoke(ctx, name, args)
|
||||
done <- result{v: v, err: err}
|
||||
}()
|
||||
|
||||
select {
|
||||
case r := <-done:
|
||||
return r.v, r.err
|
||||
case <-ctx.Done():
|
||||
return nil, fmt.Errorf("kernel action %q exceeded its %s budget", name, timeout)
|
||||
}
|
||||
}
|
||||
|
||||
// runDeferred executes a lock-re-entrant action outside the Lua call.
|
||||
//
|
||||
// It runs on a fresh goroutine precisely so it can take the plugin lock that the
|
||||
// caller still holds; waiting for the caller to unwind first would need a
|
||||
// handshake that is more machinery than the one action needs. A failure is
|
||||
// surfaced through hook_errors, which is where an operator already looks for
|
||||
// "a plugin's background work stopped working".
|
||||
func (ps *Plugins) runDeferred(a KernelAction, args map[string]interface{}) {
|
||||
api := ps.kernel()
|
||||
if api == nil {
|
||||
return
|
||||
}
|
||||
timeout := time.Duration(a.TimeoutMS) * time.Millisecond
|
||||
if timeout <= 0 {
|
||||
timeout = 10 * time.Second
|
||||
}
|
||||
// Copy the args: the caller's map is still owned by the Lua call frame.
|
||||
cp := make(map[string]interface{}, len(args))
|
||||
for k, v := range args {
|
||||
cp[k] = v
|
||||
}
|
||||
go func() {
|
||||
defer func() {
|
||||
if rec := recover(); rec != nil {
|
||||
ps.hookErr.note(StageRequestEnd, fmt.Sprintf("deferred kernel action %s panicked: %v", a.Name, rec))
|
||||
}
|
||||
}()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
if _, err := api.Invoke(ctx, a.Name, cp); err != nil {
|
||||
ps.hookErr.note(StageRequestEnd, fmt.Sprintf("deferred kernel action %s failed: %v", a.Name, err))
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// kernelPermitted applies the stored grant, with one deliberate exception: a
|
||||
// plugin's OWN adapter may always be removed by that plugin (it is not a kernel
|
||||
// resource, it is one the plugin created). Everything else needs a grant.
|
||||
func (ps *Plugins) kernelPermitted(plugin string, a KernelAction) bool {
|
||||
if a.Name == "remove_adapter" {
|
||||
return true
|
||||
}
|
||||
ps.mu.RLock()
|
||||
perm := ps.kernelPerms[plugin]
|
||||
ps.mu.RUnlock()
|
||||
return perm.allows(a)
|
||||
}
|
||||
|
||||
// SetPluginKernelPermission replaces one plugin's grant and persists it.
|
||||
//
|
||||
// Validation happens here rather than at call time so a typo'd action name is
|
||||
// reported to the operator immediately instead of silently granting nothing.
|
||||
func (ps *Plugins) SetPluginKernelPermission(plugin string, actions []string, allRead bool) error {
|
||||
if ps.find(plugin) == nil {
|
||||
return fmt.Errorf("plugin %s not found", plugin)
|
||||
}
|
||||
clean := make([]string, 0, len(actions))
|
||||
seen := map[string]bool{}
|
||||
for _, raw := range actions {
|
||||
name := strings.TrimSpace(raw)
|
||||
if name == "" || seen[name] {
|
||||
continue
|
||||
}
|
||||
a, ok := LookupKernelAction(name)
|
||||
if !ok {
|
||||
return fmt.Errorf("unknown kernel action %q", name)
|
||||
}
|
||||
seen[name] = true
|
||||
clean = append(clean, a.Name)
|
||||
}
|
||||
sort.Strings(clean)
|
||||
ps.mu.Lock()
|
||||
if ps.kernelPerms == nil {
|
||||
ps.kernelPerms = kernelPermissionSet{}
|
||||
}
|
||||
if len(clean) == 0 && !allRead {
|
||||
delete(ps.kernelPerms, plugin)
|
||||
} else {
|
||||
ps.kernelPerms[plugin] = KernelPermission{Actions: clean, AllRead: allRead}
|
||||
}
|
||||
ps.saveKernelPermissionsLocked()
|
||||
ps.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
// PluginKernelPermission returns a copy of one plugin's grant.
|
||||
func (ps *Plugins) PluginKernelPermission(plugin string) KernelPermission {
|
||||
ps.mu.RLock()
|
||||
defer ps.mu.RUnlock()
|
||||
return ps.kernelPerms[plugin].clone()
|
||||
}
|
||||
|
||||
// KernelPermissionView is the UI-facing form: what the plugin ASKED for (from its
|
||||
// own source) next to what the operator GRANTED. The distinction matters — a
|
||||
// plugin declaring `plugin.kernel = {...}` is a request, not a privilege.
|
||||
type KernelPermissionView struct {
|
||||
Plugin string `json:"plugin"`
|
||||
Declared []string `json:"declared,omitempty"`
|
||||
Granted []string `json:"granted,omitempty"`
|
||||
AllRead bool `json:"all_read"`
|
||||
}
|
||||
|
||||
// KernelPermissions lists every plugin's declared-vs-granted state for the UI.
|
||||
func (ps *Plugins) KernelPermissions() []KernelPermissionView {
|
||||
ps.mu.RLock()
|
||||
defer ps.mu.RUnlock()
|
||||
out := make([]KernelPermissionView, 0, len(ps.plugins))
|
||||
for _, p := range ps.plugins {
|
||||
perm := ps.kernelPerms[p.Info.Name]
|
||||
out = append(out, KernelPermissionView{
|
||||
Plugin: p.Info.Name,
|
||||
Declared: append([]string(nil), p.KernelDeclared...),
|
||||
Granted: append([]string(nil), perm.Actions...),
|
||||
AllRead: perm.AllRead,
|
||||
})
|
||||
}
|
||||
sort.Slice(out, func(i, j int) bool { return out[i].Plugin < out[j].Plugin })
|
||||
return out
|
||||
}
|
||||
|
||||
// kernel returns the injected API, or nil.
|
||||
func (ps *Plugins) kernel() KernelAPI {
|
||||
ps.mu.RLock()
|
||||
defer ps.mu.RUnlock()
|
||||
return ps.kernelAPI
|
||||
}
|
||||
|
||||
// SetKernelAPI injects the kernel action implementation. Called during wiring,
|
||||
// mirroring SetOutboundHosts.
|
||||
func (ps *Plugins) SetKernelAPI(api KernelAPI) {
|
||||
ps.mu.Lock()
|
||||
defer ps.mu.Unlock()
|
||||
ps.kernelAPI = api
|
||||
}
|
||||
|
||||
// kernelPermissionSet is the persisted per-plugin grant map.
|
||||
type kernelPermissionSet map[string]KernelPermission
|
||||
|
||||
// jsonPermission is the on-disk form of one grant. Kept separate from
|
||||
// KernelPermission so the persisted shape cannot drift with a struct refactor
|
||||
// (the access file is read by older and newer binaries across an upgrade).
|
||||
type jsonPermission struct {
|
||||
Actions []string `json:"actions,omitempty"`
|
||||
AllRead bool `json:"all_read,omitempty"`
|
||||
}
|
||||
|
||||
func (p KernelPermission) marshal() jsonPermission {
|
||||
return jsonPermission{Actions: p.Actions, AllRead: p.AllRead}
|
||||
}
|
||||
|
||||
func (j jsonPermission) unmarshal() KernelPermission {
|
||||
return KernelPermission{Actions: j.Actions, AllRead: j.AllRead}
|
||||
}
|
||||
|
||||
type jsonPluginPolicy struct {
|
||||
Actions []string `json:"actions,omitempty"`
|
||||
AllRead bool `json:"all_read,omitempty"`
|
||||
Hosts []string `json:"hosts,omitempty"`
|
||||
}
|
||||
|
||||
func policyFromActions(p KernelPermission) jsonPluginPolicy {
|
||||
return jsonPluginPolicy{Actions: p.Actions, AllRead: p.AllRead}
|
||||
}
|
||||
|
||||
// KernelActionView is one action as the settings page needs it: the catalog entry
|
||||
// plus whether THIS plugin currently holds it and whether the plugin asked for it.
|
||||
type KernelActionView struct {
|
||||
KernelAction
|
||||
Granted bool `json:"granted"`
|
||||
Declared bool `json:"declared"`
|
||||
}
|
||||
|
||||
// KernelActionsView lists the catalog annotated for one plugin.
|
||||
//
|
||||
// Granted and Declared are both reported because they answer different questions:
|
||||
// "may this plugin do it" and "did this plugin ask for it". An action that is
|
||||
// declared but not granted is the one an operator most needs to notice.
|
||||
func (ps *Plugins) KernelActionsView(plugin string) []KernelActionView {
|
||||
perm := ps.PluginKernelPermission(plugin)
|
||||
declared := map[string]bool{}
|
||||
ps.mu.RLock()
|
||||
for _, p := range ps.plugins {
|
||||
if p.Info.Name == plugin {
|
||||
for _, d := range p.KernelDeclared {
|
||||
declared[d] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
ps.mu.RUnlock()
|
||||
out := make([]KernelActionView, 0, len(kernelCatalog))
|
||||
for _, a := range KernelActions() {
|
||||
out = append(out, KernelActionView{
|
||||
KernelAction: a,
|
||||
Granted: perm.allows(a),
|
||||
Declared: declared[a.Name],
|
||||
})
|
||||
}
|
||||
return out
|
||||
}
|
||||
126
internal/lua/kernel_perms_persist.go
Normal file
126
internal/lua/kernel_perms_persist.go
Normal file
@ -0,0 +1,126 @@
|
||||
package lua
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
)
|
||||
|
||||
// Kernel action grants are OPERATOR policy, persisted separately from both
|
||||
// plugin state and the endpoint/visibility overrides.
|
||||
//
|
||||
// Why a separate file rather than reusing .access.json:
|
||||
//
|
||||
// - .access.json is a flat map[string]string ("name" or "name#path" -> spec).
|
||||
// Kernel grants are a list plus a boolean, so folding them in would either
|
||||
// change the shape of a file an older binary still reads, or force an
|
||||
// encoding that cannot round-trip. An upgrade must not corrupt a running
|
||||
// deployment's restrictions.
|
||||
// - The two answer different questions — "who may call this endpoint" vs
|
||||
// "what may this plugin do to the kernel" — and an operator looking at one
|
||||
// should not have to parse the other.
|
||||
//
|
||||
// Shape: { "<plugin>": { "actions": ["reload", ...], "all_read": true } }
|
||||
|
||||
const kernelPermsFileName = ".kernel-perms.json"
|
||||
|
||||
func (ps *Plugins) kernelPermsFile() string {
|
||||
if ps.dir == "" {
|
||||
return ""
|
||||
}
|
||||
return ps.dir + string(os.PathSeparator) + kernelPermsFileName
|
||||
}
|
||||
|
||||
// LoadKernelPermissions reads the persisted grants. A missing file is the normal
|
||||
// fresh-install state (nothing granted); a corrupt file is logged and ignored
|
||||
// rather than fatal — losing a grant is recoverable, refusing to boot is not.
|
||||
func (ps *Plugins) LoadKernelPermissions() error {
|
||||
path := ps.kernelPermsFile()
|
||||
if path == "" {
|
||||
return nil
|
||||
}
|
||||
b, err := os.ReadFile(path)
|
||||
if os.IsNotExist(err) {
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// The file holds BOTH kernel-action grants (map[string]jsonPermission, the
|
||||
// historical shape) AND, for plugins that asked for them, the operator's
|
||||
// extra-host grants (map[string]jsonPluginPolicy.Hosts). A bare
|
||||
// {actions:[...]} row still parses into jsonPluginPolicy with empty Hosts,
|
||||
// so old files load unchanged.
|
||||
raw := map[string]jsonPluginPolicy{}
|
||||
if err := json.Unmarshal(b, &raw); err != nil {
|
||||
return err
|
||||
}
|
||||
ps.mu.Lock()
|
||||
defer ps.mu.Unlock()
|
||||
if ps.kernelPerms == nil {
|
||||
ps.kernelPerms = kernelPermissionSet{}
|
||||
}
|
||||
if ps.extraHosts == nil {
|
||||
ps.extraHosts = map[string][]string{}
|
||||
}
|
||||
for name, jp := range raw {
|
||||
ps.kernelPerms[name] = KernelPermission{Actions: jp.Actions, AllRead: jp.AllRead}
|
||||
if len(jp.Hosts) > 0 {
|
||||
// Load the persisted grant into extraHosts WITHOUT folding it into the
|
||||
// allowlist yet — sources are loaded later, and the caller owns that
|
||||
// fold. GrantExtraHosts rebuilds the allowlist when sources are ready.
|
||||
ps.extraHosts[name] = append([]string(nil), jp.Hosts...)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// saveKernelPermissionsLocked flushes the grant map. Caller holds ps.mu.
|
||||
//
|
||||
// Grants drop unknown action names on write. If an operator authorized an action
|
||||
// that a later release removed, keeping the name would leave a stale entry that
|
||||
// nothing can ever satisfy — and it would reappear in the UI as if it were live.
|
||||
//
|
||||
// Host grants travel in the same file (jsonPluginPolicy.Hosts): a granted host is
|
||||
// the operator's explicit decision to let a plugin egress somewhere, and losing
|
||||
// it across a restart (the previous behaviour) made grants silently vanish.
|
||||
func (ps *Plugins) saveKernelPermissionsLocked() {
|
||||
path := ps.kernelPermsFile()
|
||||
if path == "" {
|
||||
return
|
||||
}
|
||||
out := map[string]jsonPluginPolicy{}
|
||||
something := false
|
||||
for name, perm := range ps.kernelPerms {
|
||||
valid := make([]string, 0, len(perm.Actions))
|
||||
for _, a := range perm.Actions {
|
||||
if _, ok := LookupKernelAction(a); ok {
|
||||
valid = append(valid, a)
|
||||
}
|
||||
}
|
||||
row := policyFromActions(KernelPermission{Actions: valid, AllRead: perm.AllRead})
|
||||
if hs := ps.extraHosts[name]; len(hs) > 0 {
|
||||
row.Hosts = append([]string(nil), hs...)
|
||||
}
|
||||
if len(valid) == 0 && !perm.AllRead && len(row.Hosts) == 0 {
|
||||
continue // nothing granted: presence would be a lie
|
||||
}
|
||||
out[name] = row
|
||||
something = true
|
||||
}
|
||||
// A plugin may hold host grants with NO kernel actions granted (operators
|
||||
// approve hosts and actions independently). Emit those too.
|
||||
for name, hs := range ps.extraHosts {
|
||||
if len(hs) == 0 {
|
||||
continue
|
||||
}
|
||||
if _, exists := out[name]; !exists {
|
||||
out[name] = jsonPluginPolicy{Hosts: append([]string(nil), hs...)}
|
||||
something = true
|
||||
}
|
||||
}
|
||||
if !something {
|
||||
_ = os.Remove(path)
|
||||
return
|
||||
}
|
||||
writeJSONAtomic(path, out)
|
||||
}
|
||||
@ -281,3 +281,124 @@ func TestCloseFlushesTail(t *testing.T) {
|
||||
t.Errorf("flushed n = %v, want 1", saved.State["n"])
|
||||
}
|
||||
}
|
||||
|
||||
// TestOnDemandCallIsPersisted guards the persistence gap that the quota memo fell
|
||||
// into, and it needs a plugin with NO hooks to see it.
|
||||
//
|
||||
// markDirty fired from two places: hooks and SetState. `Call` — the on-demand path
|
||||
// a plugin page uses — fired from neither, so a plugin that does all its work in
|
||||
// exported functions (quota: it declares no hooks at all) mutated state that
|
||||
// nothing ever wrote. The visible symptom was a health probe that looked inert
|
||||
// from outside: the memo changed in memory and the file kept the previous
|
||||
// generation until some unrelated configuration write happened to flush it.
|
||||
//
|
||||
// A hookless plugin is the whole point of the fixture: the counter above
|
||||
// accumulates through a hook, which is the path that already persisted, so it
|
||||
// cannot see this defect.
|
||||
func TestOnDemandCallIsPersisted(t *testing.T) {
|
||||
ps, _ := newPersistVM(t)
|
||||
if err := ps.LoadSource("ondemand", `
|
||||
local plugin = { name = "ondemand", version = "1.0" }
|
||||
plugin.state = { n = 0 }
|
||||
plugin.exports = { "bump" }
|
||||
function plugin.bump(payload)
|
||||
plugin.state.n = plugin.state.n + 1
|
||||
return { n = plugin.state.n }
|
||||
end
|
||||
return plugin
|
||||
`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for i := 1; i <= 3; i++ {
|
||||
out, err := ps.Call("ondemand", "bump", map[string]interface{}{})
|
||||
if err != nil {
|
||||
t.Fatalf("call %d: %v", i, err)
|
||||
}
|
||||
if m, _ := out.(map[string]interface{}); m["n"] != float64(i) {
|
||||
t.Fatalf("call %d returned %v, want n=%d", i, out, i)
|
||||
}
|
||||
}
|
||||
// Close() waits for the final flush, so no timing is involved: anything the
|
||||
// calls did NOT mark dirty cannot appear here.
|
||||
ps.Close()
|
||||
|
||||
b, err := os.ReadFile(ps.stateFile("ondemand"))
|
||||
if err != nil {
|
||||
t.Fatalf("★ an exported call did not mark the plugin dirty: %v — a plugin "+
|
||||
"with no hooks can never persist its work", err)
|
||||
}
|
||||
var saved struct {
|
||||
State map[string]interface{} `json:"state"`
|
||||
}
|
||||
if err := json.Unmarshal(b, &saved); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if saved.State["n"] != float64(3) {
|
||||
t.Errorf("persisted n = %v after three exported calls, want 3", saved.State["n"])
|
||||
}
|
||||
}
|
||||
|
||||
// TestInvokeHandlerIsPersisted 是真实事故的回归:channels 插件声明 hooks=[],
|
||||
// 一切状态变更(账号入池、凭据引用、模型黑名单、事件日志)都经过自己的 HTTP
|
||||
// handler。而 InvokeHandler 是唯一一条**从不 markDirty** 的分发路径 —— hook 路径
|
||||
// 有、Call 路径有,只有它漏了。后果:
|
||||
//
|
||||
// .channels.state.json 从未被写出 → 重启后账号池静默清空。
|
||||
// 运维在升级前的重启里丢掉了刚登录的账号,界面显示"已登录"但列表为空。
|
||||
//
|
||||
// billing/quota 侥幸没踩到,只因为它们有 hooks(billing)或用 Call(quota)。
|
||||
// 本测试用**无 hooks、只走 handler** 的插件复现该形态。
|
||||
func TestInvokeHandlerIsPersisted(t *testing.T) {
|
||||
ps, _ := newPersistVM(t)
|
||||
if err := ps.LoadSource("handlersonly", `
|
||||
local plugin = { name = "handlersonly", version = "1.0" }
|
||||
plugin.state = { accounts = {} }
|
||||
-- 不声明任何 hooks:状态只能经 HTTP handler 变更(channels 的形态)
|
||||
plugin.hooks = {}
|
||||
plugin.http = {
|
||||
{ path = "add", method = "POST", handler = "add" },
|
||||
}
|
||||
function plugin.add(req)
|
||||
local body = req.body or ""
|
||||
local id = body:match('"id":"([^"]+)"') or "unknown"
|
||||
plugin.state.accounts[id] = { id = id, enabled = true }
|
||||
return { status = 200, json = { ok = true } }
|
||||
end
|
||||
return plugin
|
||||
`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for _, id := range []string{"acct-1", "acct-2"} {
|
||||
resp, err := ps.InvokeHandler("handlersonly", "add", &handlerRequest{
|
||||
Method: "POST", Body: `{"id":"` + id + `"}`,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("handler: %v", err)
|
||||
}
|
||||
if resp == nil {
|
||||
t.Fatal("handler returned nil response")
|
||||
}
|
||||
}
|
||||
// Close() waits for the final flush, so no timing is involved: anything the
|
||||
// handlers did NOT mark dirty cannot appear here.
|
||||
ps.Close()
|
||||
|
||||
b, err := os.ReadFile(ps.stateFile("handlersonly"))
|
||||
if err != nil {
|
||||
t.Fatalf("★ 无 hooks 的插件经 HTTP handler 改状态后未落盘: %v\n"+
|
||||
" —— InvokeHandler 必须像 Call / hook 路径一样 markDirty,\n"+
|
||||
" 否则 channels 的账号池会在每次重启后静默清空", err)
|
||||
}
|
||||
var saved struct {
|
||||
State map[string]interface{} `json:"state"`
|
||||
}
|
||||
if err := json.Unmarshal(b, &saved); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
accts, _ := saved.State["accounts"].(map[string]interface{})
|
||||
if len(accts) != 2 {
|
||||
t.Fatalf("落盘的 accounts = %v,应为 2 个账号", saved.State["accounts"])
|
||||
}
|
||||
}
|
||||
|
||||
180
internal/lua/plugin_access.go
Normal file
180
internal/lua/plugin_access.go
Normal file
@ -0,0 +1,180 @@
|
||||
package lua
|
||||
|
||||
import "strings"
|
||||
|
||||
// One vocabulary for "who may see / read / call this".
|
||||
//
|
||||
// Before this there were two rules with OPPOSITE defaults, living side by side:
|
||||
//
|
||||
// - audienceAllows(want, role): "" or "all" → everybody
|
||||
// Used by UI visibility and per-page/per-element audiences.
|
||||
// - HTTPHandler.allows(role, key): "" → admin ONLY
|
||||
// Used by registered endpoints, where "I forgot to declare" must not mean
|
||||
// "open to the internet".
|
||||
//
|
||||
// Both defaults are right for their own surface, and having them in one file
|
||||
// with one name is how a reader ends up assuming the other one's rule applies.
|
||||
// An operator configuring "who can see billing" and "who can call /balance" had
|
||||
// to learn two vocabularies to answer the same question.
|
||||
//
|
||||
// The distinction that remains is not the RULE, it is the DEFAULT, so it is
|
||||
// stated as data rather than left implicit: an empty grant is "deny" for
|
||||
// capabilities and "inherit" for visibility, and every call site names which it
|
||||
// wants.
|
||||
|
||||
// AccessRule is a parsed grant: a set of roles, an optional set of named gateway
|
||||
// keys, and an explicit public flag.
|
||||
//
|
||||
// Keys REPLACE roles rather than adding to them. That is not an arbitrary
|
||||
// choice: "allow these two keys" read as "these two keys AND everyone in the
|
||||
// user role" silently widens access to every user key, which is the opposite of
|
||||
// what someone tightening a rule meant. `keys = { "@alice" }` therefore means
|
||||
// exactly alice.
|
||||
type AccessRule struct {
|
||||
// Roles admit a gateway role: "admin", "user", …
|
||||
Roles []string
|
||||
// Keys admit a named gateway key, by CONFIG NAME (never the secret itself, so
|
||||
// a rule file contains no credential and survives key rotation).
|
||||
Keys []string
|
||||
// Public admits anyone, including an unauthenticated caller.
|
||||
Public bool
|
||||
}
|
||||
|
||||
// AccessAll is the grant that admits everyone.
|
||||
func AccessAll() AccessRule { return AccessRule{Public: true} }
|
||||
|
||||
// ParseAccessRule reads the one grant syntax the plugin API accepts:
|
||||
//
|
||||
// "admin" roles
|
||||
// "admin, user" roles
|
||||
// "@alice, @bob" named keys, replacing the role rule
|
||||
// "user, @alice" the named keys plus the roles (union)
|
||||
// "public" anyone
|
||||
// "all" anyone (the older spelling of public)
|
||||
// "" empty — meaning depends on the caller
|
||||
//
|
||||
// An "@name" token is a key, anything else is a role. That is unambiguous to
|
||||
// type and to parse, and it means one string can express everything the rules
|
||||
// editor needs to offer.
|
||||
//
|
||||
// "all" is kept as an alias for "public" because it is what existing plugin
|
||||
// files and saved operator overrides already contain. Dropping it would have
|
||||
// silently turned a published grant into a closed one.
|
||||
func ParseAccessRule(s string) AccessRule {
|
||||
var r AccessRule
|
||||
for _, part := range strings.Split(s, ",") {
|
||||
part = strings.TrimSpace(part)
|
||||
if part == "" {
|
||||
continue
|
||||
}
|
||||
if strings.EqualFold(part, "public") || strings.EqualFold(part, "all") {
|
||||
r.Public = true
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(part, "@") {
|
||||
if k := strings.TrimSpace(strings.TrimPrefix(part, "@")); k != "" {
|
||||
r.Keys = append(r.Keys, k)
|
||||
}
|
||||
continue
|
||||
}
|
||||
r.Roles = append(r.Roles, part)
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
// Rule renders the grant back to the syntax ParseAccessRule accepts. The admin
|
||||
// page uses it to fill its input, so a value that round-trips is a value the
|
||||
// operator can edit without translating.
|
||||
func (r AccessRule) Rule() string {
|
||||
var parts []string
|
||||
for _, k := range r.Keys {
|
||||
parts = append(parts, "@"+k)
|
||||
}
|
||||
for _, role := range r.Roles {
|
||||
parts = append(parts, role)
|
||||
}
|
||||
if r.Public {
|
||||
parts = append(parts, "public")
|
||||
}
|
||||
return strings.Join(parts, ", ")
|
||||
}
|
||||
|
||||
// Empty reports whether the grant names nobody.
|
||||
func (r AccessRule) Empty() bool {
|
||||
return len(r.Roles) == 0 && len(r.Keys) == 0 && !r.Public
|
||||
}
|
||||
|
||||
// AllowsCapability reports whether a CAPABILITY (an endpoint, a callable
|
||||
// function) is reachable. Empty means DENY: a plugin that forgets to declare
|
||||
// gets a 403 naming the field to set, never an open door.
|
||||
//
|
||||
// Admin is always admitted. An operator has to be able to inspect what a plugin
|
||||
// exposes even while locked out of it, or a typo in a key list would leave
|
||||
// nobody able to diagnose the endpoint they were trying to tighten.
|
||||
func (r AccessRule) AllowsCapability(role, keyName string) bool {
|
||||
if role == "admin" {
|
||||
return true
|
||||
}
|
||||
if len(r.Keys) > 0 {
|
||||
// A non-empty key list REPLACES the role rule (see the type comment).
|
||||
// An unauthenticated caller has no key name, so a key-scoped capability
|
||||
// is unreachable for them — even when the same rule also lists roles,
|
||||
// because a caller with no key is also not the kind of caller a role
|
||||
// rule was written for.
|
||||
if keyName == "" {
|
||||
return false
|
||||
}
|
||||
for _, k := range r.Keys {
|
||||
if strings.EqualFold(k, keyName) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
// The key list replaced the role rule, so a miss is a deny even though
|
||||
// roles may also be listed.
|
||||
return false
|
||||
}
|
||||
if r.Public {
|
||||
return true
|
||||
}
|
||||
for _, want := range r.Roles {
|
||||
if strings.EqualFold(want, role) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// AllowsVisibility reports whether a VIEW (a page, a UI element) is rendered.
|
||||
// Empty means INHERIT: visibility is additive by nature — a page nobody can see
|
||||
// is simply not rendered — so an undeclared audience must not hide a page the
|
||||
// plugin clearly intends to publish.
|
||||
func (r AccessRule) AllowsVisibility(role string) bool {
|
||||
if r.Empty() || r.Public {
|
||||
return true
|
||||
}
|
||||
if role == "admin" && len(r.Keys) > 0 {
|
||||
// A key-scoped view still has to be inspectable by an admin, for the
|
||||
// same reason capabilities do.
|
||||
return true
|
||||
}
|
||||
for _, want := range r.Roles {
|
||||
if strings.EqualFold(want, role) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// accessRuleFor folds the separate declaration fields of an endpoint into one
|
||||
// grant, so the rules below live in one place instead of beside each field.
|
||||
//
|
||||
// It is a plain fold, deliberately. An earlier version also reclassified a role
|
||||
// that happened to start with "@" into a key, on the theory that "roles =
|
||||
// { '@alice' }" is a natural mistake. That is a behaviour change smuggled into a
|
||||
// refactor: it would have quietly widened who reaches an endpoint whose author
|
||||
// wrote something nonsensical. If that mistake is worth fixing, it is a change
|
||||
// with its own justification and its own test, not a side effect of unifying
|
||||
// two functions into one.
|
||||
func accessRuleFor(roles, keys []string, public bool) AccessRule {
|
||||
return AccessRule{Roles: roles, Keys: keys, Public: public}
|
||||
}
|
||||
@ -112,4 +112,4 @@ func (ps *Plugins) sortedOverrideKeys() []string {
|
||||
}
|
||||
sort.Strings(keys)
|
||||
return keys
|
||||
}
|
||||
}
|
||||
|
||||
152
internal/lua/plugin_access_test.go
Normal file
152
internal/lua/plugin_access_test.go
Normal file
@ -0,0 +1,152 @@
|
||||
package lua
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// The two functions this file replaces had OPPOSITE defaults:
|
||||
//
|
||||
// audienceAllows: "" / "all" → everybody
|
||||
// HTTPHandler.allows: "" → admin ONLY
|
||||
//
|
||||
// Both are correct for their surface. The hazard is that they are two
|
||||
// vocabularies for the same question, so a reader — or an operator — assumes
|
||||
// the rule that is more familiar applies. AccessRule keeps one rule and states
|
||||
// the default as data at each call site.
|
||||
|
||||
// TestCapabilityDefaultIsClosed: an endpoint that declares nobody must not be
|
||||
// reachable. This is the property that makes "I forgot" fail safe.
|
||||
func TestCapabilityDefaultIsClosed(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
rule string
|
||||
}{
|
||||
{"empty", ""},
|
||||
{"nil-ish", " "},
|
||||
} {
|
||||
if ParseAccessRule(tc.rule).AllowsCapability("user", "alice") {
|
||||
t.Errorf("%s: an undeclared capability is reachable by a user", tc.name)
|
||||
}
|
||||
if !ParseAccessRule(tc.rule).AllowsCapability("admin", "alice") {
|
||||
t.Errorf("%s: admin locked out of a capability they must be able to inspect", tc.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestVisibilityDefaultIsInherits: the opposite default, on purpose. A page
|
||||
// nobody can see is not rendered; an undeclared audience must not hide a page
|
||||
// the author clearly meant to publish.
|
||||
func TestVisibilityDefaultIsInherits(t *testing.T) {
|
||||
if !ParseAccessRule("").AllowsVisibility("user") {
|
||||
t.Error("an undeclared audience hides a page")
|
||||
}
|
||||
if ParseAccessRule("admin").AllowsVisibility("user") {
|
||||
t.Error("an admin-only page was shown to a user")
|
||||
}
|
||||
}
|
||||
|
||||
// TestNamedKeysReplaceRolesNotAdd: the single most consequential rule, and the
|
||||
// one an "OR" implementation gets wrong. "allow these two keys" must not also
|
||||
// admit every user key.
|
||||
func TestNamedKeysReplaceRolesNotAdd(t *testing.T) {
|
||||
r := ParseAccessRule("@alice, @bob")
|
||||
if !r.AllowsCapability("user", "alice") {
|
||||
t.Error("a named key was not admitted")
|
||||
}
|
||||
if r.AllowsCapability("user", "carol") {
|
||||
t.Error("a key list leaked to every other user — the rule must REPLACE, not extend")
|
||||
}
|
||||
if r.AllowsCapability("user", "") {
|
||||
t.Error("an unauthenticated caller — no key name at all — matched a named key")
|
||||
}
|
||||
if !r.AllowsCapability("admin", "") {
|
||||
t.Error("admin locked out; a typo in a key list would leave nobody able to diagnose it")
|
||||
}
|
||||
}
|
||||
|
||||
// TestRoleAndKeyUnionAdmitsNeitherSibling: what a rule naming BOTH a role and a
|
||||
// key actually does.
|
||||
//
|
||||
// Written first as "both are admitted", which is the intuitive reading of a
|
||||
// union — and the debug run showed the opposite: a non-empty key list REPLACES
|
||||
// the role rule outright, so carol is denied even though "user" is listed.
|
||||
//
|
||||
// That surprise is exactly why the rule is named, commented and now tested. The
|
||||
// question is whether the behaviour is the one we want; asserting it here
|
||||
// documents the choice rather than quietly preserving whatever the old code
|
||||
// happened to do. The behaviour matches the pre-existing HTTPHandler.allows, so
|
||||
// nothing deployed changes.
|
||||
func TestRoleAndKeyUnionAdmitsNeitherSibling(t *testing.T) {
|
||||
r := ParseAccessRule("user, @alice")
|
||||
if !r.AllowsCapability("user", "alice") {
|
||||
t.Error("the named key was not admitted")
|
||||
}
|
||||
if r.AllowsCapability("user", "carol") {
|
||||
t.Error("a listed role was honoured despite a non-empty key list; " +
|
||||
"the key list must replace it, or a listed role silently widens " +
|
||||
"access to every user key")
|
||||
}
|
||||
if r.AllowsCapability("user", "mallory") {
|
||||
t.Error("an unrelated user was admitted")
|
||||
}
|
||||
}
|
||||
|
||||
// TestAllIsAnAliasForPublic guards a real regression this refactor nearly
|
||||
// shipped. "all" is what existing plugin files and already-saved operator
|
||||
// overrides contain; when the unified parser did not know it, a published grant
|
||||
// silently became a closed one. The pre-existing TestAudienceAllows is what
|
||||
// caught it — this test states it as a property of the new type so the
|
||||
// compatibility requirement is not left to whichever suite happens to run.
|
||||
func TestAllIsAnAliasForPublic(t *testing.T) {
|
||||
r := ParseAccessRule("all")
|
||||
if !r.Public {
|
||||
t.Fatal(`"all" did not parse as public`)
|
||||
}
|
||||
if !r.AllowsCapability("user", "carol") {
|
||||
t.Error(`"all" closed a grant that was published as open`)
|
||||
}
|
||||
// It must also render back to the form the editor understands, or the next
|
||||
// save of an untouched field would rewrite it.
|
||||
if !strings.Contains(strings.ToLower(r.Rule()), "public") {
|
||||
t.Errorf("Rule() = %q, want it to render as public", r.Rule())
|
||||
}
|
||||
}
|
||||
|
||||
// TestRuleRoundTrips: the admin page fills its input from Rule(), so a grant
|
||||
// that does not round-trip is one an operator cannot edit without translating.
|
||||
func TestRuleRoundTrips(t *testing.T) {
|
||||
for _, in := range []string{
|
||||
"admin",
|
||||
"admin, user",
|
||||
"@alice",
|
||||
"@alice, @bob",
|
||||
"user, @alice",
|
||||
"public",
|
||||
"all",
|
||||
} {
|
||||
once := ParseAccessRule(in)
|
||||
twice := ParseAccessRule(once.Rule())
|
||||
if once.Rule() != twice.Rule() {
|
||||
t.Errorf("%q -> %q -> %q, not stable", in, once.Rule(), twice.Rule())
|
||||
}
|
||||
if len(twice.Roles) != len(once.Roles) || len(twice.Keys) != len(once.Keys) {
|
||||
t.Errorf("%q lost data across a round trip: %+v -> %+v", in, once, twice)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestRoleMatchingIsCaseAndSpaceInsensitive: grants arrive typed by humans into
|
||||
// a text input, and "Admin" not matching "admin" would lock an admin out of
|
||||
// their own plugin.
|
||||
func TestRoleMatchingIsCaseAndSpaceInsensitive(t *testing.T) {
|
||||
if !ParseAccessRule(" Admin , User ").AllowsCapability("admin", "x") {
|
||||
t.Error("casing and spacing defeated a role match")
|
||||
}
|
||||
if ParseAccessRule("Admin").AllowsCapability("user", "x") {
|
||||
t.Error("a user was admitted by a case variant")
|
||||
}
|
||||
if !ParseAccessRule(" @Alice ").AllowsCapability("user", "alice") {
|
||||
t.Error("casing defeated a named-key match")
|
||||
}
|
||||
}
|
||||
293
internal/lua/plugin_adapter_bridge.go
Normal file
293
internal/lua/plugin_adapter_bridge.go
Normal file
@ -0,0 +1,293 @@
|
||||
// Package lua — 插件适配器管理桥接(R4-a / Phase 3)
|
||||
//
|
||||
// 用户需求:插件可安装、运行时修改适配器,以支持复杂认证与 token 轮换;
|
||||
// 适配器本身不需要持续运行(它是无状态请求级弹性池)。
|
||||
//
|
||||
// 插件(长期运行)──定期/事件驱动──> install/update/remove_adapter
|
||||
// │
|
||||
// ▼
|
||||
// 内核重建该适配器的池(LoadAdapterSource)
|
||||
// │
|
||||
// ▼
|
||||
// 后续请求自然使用新逻辑
|
||||
//
|
||||
// 三条安全规则(与 R6 同源):
|
||||
//
|
||||
// 1. 路径安全:名字必须通过 ValidateAdapterName(无穿越/绝对路径/空白)
|
||||
// 2. 不得影子内置:插件不能注册与内置适配器同名的适配器
|
||||
// (用 VM.InstallPluginAdapter,运维仍可自行覆盖)
|
||||
// 3. 归属可查:记录每个适配器的 owner("plugin:名字" 或 "operator"),
|
||||
// 于是「插件只能改自己装的」这条规则是可判定的,而不是靠约定
|
||||
//
|
||||
// 第 3 条是必要的:没有归属信息时,"插件 A 改插件 B 装的适配器" 无法区分
|
||||
// 于合法的自身更新,会导致一个插件能劫持另一个插件的认证逻辑。
|
||||
package lua
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
golua "github.com/aarzilli/golua/lua"
|
||||
)
|
||||
|
||||
// AdapterOwner 标识一个适配器由谁安装。
|
||||
const (
|
||||
OwnerOperator = "operator" // 磁盘/首启 seed/管理员上传
|
||||
OwnerPlugin = "plugin" // 由某个插件经桥接安装
|
||||
)
|
||||
|
||||
// adapterOwnership records which plugin installed each adapter.
|
||||
//
|
||||
// This is the state that makes rule 3 enforceable: InstallPluginAdapter marks
|
||||
// the adapter as owned by that plugin, and a later update from a DIFFERENT
|
||||
// plugin is refused. An adapter with no owner is the operator's, and no plugin
|
||||
// may touch it.
|
||||
type adapterOwnership struct {
|
||||
mu sync.RWMutex
|
||||
// owner maps adapter name -> installing plugin name (only for plugin-owned).
|
||||
owner map[string]string
|
||||
}
|
||||
|
||||
// ownedBy reports the plugin that owns name, or "" when it is operator-owned.
|
||||
func (o *adapterOwnership) ownedBy(name string) string {
|
||||
o.mu.RLock()
|
||||
defer o.mu.RUnlock()
|
||||
return o.owner[name]
|
||||
}
|
||||
|
||||
func (o *adapterOwnership) set(name, plugin string) {
|
||||
o.mu.Lock()
|
||||
defer o.mu.Unlock()
|
||||
if o.owner == nil {
|
||||
o.owner = map[string]string{}
|
||||
}
|
||||
o.owner[name] = plugin
|
||||
}
|
||||
|
||||
func (o *adapterOwnership) clear(name string) {
|
||||
o.mu.Lock()
|
||||
defer o.mu.Unlock()
|
||||
delete(o.owner, name)
|
||||
}
|
||||
|
||||
// listOwned returns the adapter names a given plugin owns (for UI/diagnostics).
|
||||
func (o *adapterOwnership) listOwned(plugin string) []string {
|
||||
o.mu.RLock()
|
||||
defer o.mu.RUnlock()
|
||||
var out []string
|
||||
for name, p := range o.owner {
|
||||
if p == plugin {
|
||||
out = append(out, name)
|
||||
}
|
||||
}
|
||||
sort.Strings(out)
|
||||
return out
|
||||
}
|
||||
|
||||
// Plugins carries an adapter-ownership table.
|
||||
func (ps *Plugins) adapterOwner() *adapterOwnership {
|
||||
ps.adapterOwnOnce.Do(func() { ps.adapterOwn = &adapterOwnership{} })
|
||||
return ps.adapterOwn
|
||||
}
|
||||
|
||||
// InstallAdapter registers an adapter on behalf of a plugin.
|
||||
//
|
||||
// Returns the name that was actually registered — an adapter script may declare
|
||||
// a different `name` than the file/registration key, and the declared name is
|
||||
// what sources must reference. Callers get it back so they can update config.
|
||||
func (ps *Plugins) InstallAdapter(plugin, name, code string) (string, error) {
|
||||
if plugin == "" {
|
||||
return "", fmt.Errorf("install_adapter: caller plugin is unknown")
|
||||
}
|
||||
if ps.vm == nil {
|
||||
return "", fmt.Errorf("install_adapter: no adapter VM available")
|
||||
}
|
||||
// A name conflict used to be a hard rejection (owned by another plugin, or
|
||||
// shadows a built-in). The user now wants it SUSPENDED for operator
|
||||
// arbitration instead of refused outright: the incumbent keeps serving, the
|
||||
// candidate waits in the conflicts list, and the operator picks a winner in
|
||||
// the WebUI. The install still returns a (typed) error so the caller knows it
|
||||
// did not take effect — but it is ErrAdapterConflictPending, not a dead end.
|
||||
incumbent := ""
|
||||
if owner := ps.adapterOwner().ownedBy(name); owner != "" && owner != plugin {
|
||||
incumbent = "plugin:" + owner
|
||||
} else if containsNameFold(ps.vm.bundledNames(), name) {
|
||||
incumbent = "operator (built-in)"
|
||||
}
|
||||
if incumbent != "" {
|
||||
if ps.recordAdapterConflict(name, incumbent, plugin, code) {
|
||||
// The operator already chose THIS candidate in a prior session. Install
|
||||
// for real via LoadAdapterSource, which bypasses the reserved-name gate
|
||||
// — that explicit operator override is exactly what the arbitration is
|
||||
// FOR, and re-suspending it on every restart would be a loop.
|
||||
if err := ps.vm.LoadAdapterSource(name, code); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return ps.finishInstall(plugin, name), nil
|
||||
}
|
||||
return "", &ErrAdapterConflictPending{Name: name, Incumbent: incumbent}
|
||||
}
|
||||
// No conflict: normal install path. Rules 1+2 (path safety + no shadowing
|
||||
// built-ins) live in InstallPluginAdapter so there is exactly one
|
||||
// enforcement point. (The shadow check there is now unreachable for a
|
||||
// genuine built-in — it is caught above and suspended — but kept as the
|
||||
// single authority for path safety and any future reserved set.)
|
||||
if err := ps.vm.InstallPluginAdapter(name, code); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return ps.finishInstall(plugin, name), nil
|
||||
}
|
||||
|
||||
// finishInstall records adapter ownership under BOTH the registration key and
|
||||
// the declared name (a script may declare a name different from the key, and it
|
||||
// is the declared name sources reference — recording only the key would leave
|
||||
// the adapter un-owned and therefore mutable by anyone). Both entries are
|
||||
// cleared together on remove.
|
||||
func (ps *Plugins) finishInstall(plugin, name string) string {
|
||||
effective := ps.vm.EffectiveAdapterName(name)
|
||||
ps.adapterOwner().set(effective, plugin)
|
||||
if effective != name {
|
||||
ps.adapterOwner().set(name, plugin)
|
||||
}
|
||||
return effective
|
||||
}
|
||||
|
||||
// containsNameFold reports whether name is in names (case-insensitive, matching
|
||||
// ValidateAdapterName's reserved-name comparison).
|
||||
func containsNameFold(names []string, name string) bool {
|
||||
for _, n := range names {
|
||||
if strings.EqualFold(n, name) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// UpdateAdapter replaces an adapter a plugin previously installed — the token
|
||||
// rotation path. The plugin gets a NEW pool; the old one is shut down, so an
|
||||
// in-flight request either uses the old or the new code, never a mixture.
|
||||
//
|
||||
// Only the owning plugin may update. Updating an operator-installed adapter is
|
||||
// refused: the operator's file on disk would then disagree with what the running
|
||||
// gateway executes, and the next restart would silently revert the "update".
|
||||
func (ps *Plugins) UpdateAdapter(plugin, name, code string) error {
|
||||
if plugin == "" {
|
||||
return fmt.Errorf("update_adapter: caller plugin is unknown")
|
||||
}
|
||||
owner := ps.adapterOwner().ownedBy(name)
|
||||
if owner == "" {
|
||||
return fmt.Errorf("update_adapter: adapter %q is operator-installed; "+
|
||||
"only adapters a plugin installed can be updated at runtime", name)
|
||||
}
|
||||
if owner != plugin {
|
||||
return fmt.Errorf("update_adapter: adapter %q is owned by plugin %q, not %q",
|
||||
name, owner, plugin)
|
||||
}
|
||||
if err := ps.vm.InstallPluginAdapter(name, code); err != nil {
|
||||
return err
|
||||
}
|
||||
// Ownership is unchanged: same plugin, new version.
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveAdapter removes an adapter a plugin installed. Operator-installed
|
||||
// adapters are refused (same reasoning as UpdateAdapter).
|
||||
func (ps *Plugins) RemoveAdapter(plugin, name string) error {
|
||||
owner := ps.adapterOwner().ownedBy(name)
|
||||
if owner == "" {
|
||||
return fmt.Errorf("remove_adapter: adapter %q is operator-installed; "+
|
||||
"use DELETE /api/adapters/%s", name, name)
|
||||
}
|
||||
if owner != plugin {
|
||||
return fmt.Errorf("remove_adapter: adapter %q is owned by plugin %q, not %q",
|
||||
name, owner, plugin)
|
||||
}
|
||||
ps.vm.RemoveAdapter(name)
|
||||
ps.adapterOwner().clear(name)
|
||||
return nil
|
||||
}
|
||||
|
||||
// PluginAdapters lists the adapters a plugin owns (its token-rotation surface).
|
||||
func (ps *Plugins) PluginAdapters(plugin string) []string {
|
||||
return ps.adapterOwner().listOwned(plugin)
|
||||
}
|
||||
|
||||
// AdapterOwnerOf reports the owning plugin of an adapter, or "" if operator-owned.
|
||||
func (ps *Plugins) AdapterOwnerOf(name string) string {
|
||||
return ps.adapterOwner().ownedBy(name)
|
||||
}
|
||||
|
||||
// ---- Lua 绑定(R4:插件在运行时改自己的适配器)----
|
||||
|
||||
// adapterBridgeGlobal 暴露给插件 Lua 的适配器管理表。
|
||||
//
|
||||
// 用一张表而不是三个顶层全局,是为了少占插件可见的全局命名空间。表用
|
||||
// buildJSONTable 的同一套写法(NewTable + PushGoClosure + SetField),
|
||||
// 该写法在 json 表上已被长期验证。
|
||||
const adapterBridgeGlobal = "__llmsproxy_adapters"
|
||||
|
||||
// registerAdapterBridge 把适配器管理表装进插件的 Lua 状态。
|
||||
//
|
||||
// name 是**该插件自己的启动名**,随每次调用一起交给 Go 侧桥接 —— 归属校验
|
||||
// 因此来自启动时的固定身份,而不是从 Lua 状态里现读(那样一个分片或任何持有
|
||||
// 已编译脚本的调用者就能冒充别的插件)。
|
||||
func registerAdapterBridge(L *golua.State, ps *Plugins, name string) {
|
||||
L.NewTable()
|
||||
install := luaInstallAdapter(L, ps, name)
|
||||
L.PushGoClosure(install)
|
||||
L.SetField(-2, "install")
|
||||
update := luaUpdateAdapter(L, ps, name)
|
||||
L.PushGoClosure(update)
|
||||
L.SetField(-2, "update")
|
||||
remove := luaRemoveAdapter(L, ps, name)
|
||||
L.PushGoClosure(remove)
|
||||
L.SetField(-2, "remove")
|
||||
L.SetGlobal(adapterBridgeGlobal)
|
||||
}
|
||||
|
||||
func luaInstallAdapter(L *golua.State, ps *Plugins, name string) golua.LuaGoFunction {
|
||||
return func(L *golua.State) int {
|
||||
adapterName := L.ToString(1)
|
||||
code := L.ToString(2)
|
||||
effective, err := ps.InstallAdapter(name, adapterName, code)
|
||||
if err != nil {
|
||||
L.PushNil()
|
||||
L.PushString(err.Error())
|
||||
return 2
|
||||
}
|
||||
L.PushString(effective)
|
||||
L.PushNil()
|
||||
return 2
|
||||
}
|
||||
}
|
||||
|
||||
func luaUpdateAdapter(L *golua.State, ps *Plugins, name string) golua.LuaGoFunction {
|
||||
return func(L *golua.State) int {
|
||||
adapterName := L.ToString(1)
|
||||
code := L.ToString(2)
|
||||
if err := ps.UpdateAdapter(name, adapterName, code); err != nil {
|
||||
L.PushNil()
|
||||
L.PushString(err.Error())
|
||||
return 2
|
||||
}
|
||||
L.PushBoolean(true)
|
||||
L.PushNil()
|
||||
return 2
|
||||
}
|
||||
}
|
||||
|
||||
func luaRemoveAdapter(L *golua.State, ps *Plugins, name string) golua.LuaGoFunction {
|
||||
return func(L *golua.State) int {
|
||||
adapterName := L.ToString(1)
|
||||
if err := ps.RemoveAdapter(name, adapterName); err != nil {
|
||||
L.PushNil()
|
||||
L.PushString(err.Error())
|
||||
return 2
|
||||
}
|
||||
L.PushBoolean(true)
|
||||
L.PushNil()
|
||||
return 2
|
||||
}
|
||||
}
|
||||
234
internal/lua/plugin_adapter_bridge_lua_test.go
Normal file
234
internal/lua/plugin_adapter_bridge_lua_test.go
Normal file
@ -0,0 +1,234 @@
|
||||
package lua
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 本文件验证 R4 的 Lua 侧绑定:**插件自己的 Lua 代码**能在运行时安装/改写/
|
||||
// 删除自己的适配器。
|
||||
//
|
||||
// 为什么需要单独一层测试:Go 侧的 InstallAdapter/UpdateAdapter 在
|
||||
// plugin_adapter_bridge_test.go 里已直接验证过,但那证明的是"Go API 能工作"。
|
||||
// token 轮换的真实形态是插件在定时任务或钩子里**从 Lua 发起**这个调用,所以
|
||||
// 绑定本身(__llmsproxy_adapters 表)必须被证明可用 —— 而且它曾经在 cgo 层
|
||||
// SIGSEGV,所以"不崩"本身就是一条断言。
|
||||
|
||||
const bridgeProbe = `
|
||||
local plugin = { name = "authtoken", version = "1.0" }
|
||||
plugin.exports = { "install_via_lua", "update_via_lua", "remove_via_lua", "install_operator_owned" }
|
||||
|
||||
local function src(ver, tok)
|
||||
return 'return { name = "myauth", version = "' .. ver .. '", endpoint = "/x", token = "' .. tok .. '" }'
|
||||
end
|
||||
|
||||
function plugin.install_via_lua(args)
|
||||
local name, err = __llmsproxy_adapters.install("myauth", src("1.0", args.tok))
|
||||
return { name = name, err = err }
|
||||
end
|
||||
|
||||
function plugin.update_via_lua(args)
|
||||
local ok, err = __llmsproxy_adapters.update("myauth", src("2.0", args.tok))
|
||||
return { ok = ok, err = err }
|
||||
end
|
||||
|
||||
function plugin.remove_via_lua()
|
||||
local ok, err = __llmsproxy_adapters.remove("myauth")
|
||||
return { ok = ok, err = err }
|
||||
end
|
||||
|
||||
-- 试图替换一个非自己拥有的适配器:必须被拒(不能影子内置)
|
||||
function plugin.install_operator_owned()
|
||||
local name, err = __llmsproxy_adapters.install("openai", src("9.9", "evil"))
|
||||
return { name = name, err = err }
|
||||
end
|
||||
return plugin
|
||||
`
|
||||
|
||||
// TestPluginInstallsAdapterFromLua 是 R4 绑定的主断言。
|
||||
func TestPluginInstallsAdapterFromLua(t *testing.T) {
|
||||
_, ps, _ := runtimePlugins(t)
|
||||
if err := ps.LoadSource("authtoken", bridgeProbe); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
out, err := ps.Call("authtoken", "install_via_lua", map[string]interface{}{"tok": "T1"})
|
||||
if err != nil {
|
||||
t.Fatalf("Lua 侧 install 调用失败: %v", err)
|
||||
}
|
||||
m, ok := out.(map[string]interface{})
|
||||
if !ok {
|
||||
t.Fatalf("返回值不是 table: %#v", out)
|
||||
}
|
||||
if m["name"] != "myauth" {
|
||||
t.Fatalf("install 应返回生效名 myauth, 实得 %#v (err=%#v)", m["name"], m["err"])
|
||||
}
|
||||
// 归属确实被记录(这条规则是"插件只能改自己装的"的基础)
|
||||
if owner := ps.AdapterOwnerOf("myauth"); owner != "authtoken" {
|
||||
t.Errorf("归属应为 authtoken, 实得 %q", owner)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPluginRotatesTokenViaLua 是 token 轮换的完整形态:
|
||||
// 同一个插件先装、再改,改后新版本生效。
|
||||
func TestPluginRotatesTokenViaLua(t *testing.T) {
|
||||
_, ps, _ := runtimePlugins(t)
|
||||
if err := ps.LoadSource("authtoken", bridgeProbe); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := ps.Call("authtoken", "install_via_lua", map[string]interface{}{"tok": "OLD"}); err != nil {
|
||||
t.Fatalf("install: %v", err)
|
||||
}
|
||||
// 轮换
|
||||
out, err := ps.Call("authtoken", "update_via_lua", map[string]interface{}{"tok": "NEW"})
|
||||
if err != nil {
|
||||
t.Fatalf("update: %v", err)
|
||||
}
|
||||
m := out.(map[string]interface{})
|
||||
if m["ok"] != true {
|
||||
t.Fatalf("Lua 侧 update 未成功: %#v", m)
|
||||
}
|
||||
// 新代码必须真的生效 —— 这是"轮换有意义"的定义
|
||||
if rev := ps.vm.AdapterRevision("myauth"); rev < 2 {
|
||||
t.Errorf("适配器修订号应 >=2(已被替换一次),实得 %d", rev)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPluginRemoveAdapterFromLua:插件能删掉自己装的。
|
||||
func TestPluginRemoveAdapterFromLua(t *testing.T) {
|
||||
_, ps, _ := runtimePlugins(t)
|
||||
if err := ps.LoadSource("authtoken", bridgeProbe); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := ps.Call("authtoken", "install_via_lua", map[string]interface{}{"tok": "T"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
out, err := ps.Call("authtoken", "remove_via_lua", map[string]interface{}{})
|
||||
if err != nil {
|
||||
t.Fatalf("remove: %v", err)
|
||||
}
|
||||
if out.(map[string]interface{})["ok"] != true {
|
||||
t.Fatalf("Lua 侧 remove 未成功: %#v", out)
|
||||
}
|
||||
if owner := ps.AdapterOwnerOf("myauth"); owner != "" {
|
||||
t.Errorf("删除后归属应清空, 实得 %q", owner)
|
||||
}
|
||||
}
|
||||
|
||||
// TestLuaBridgeCannotShadowBuiltin:从 Lua 侧同样不能影子内置适配器 ——
|
||||
// 否则绑定就成了绕过 Go 侧校验的后门。
|
||||
func TestLuaBridgeCannotShadowBuiltin(t *testing.T) {
|
||||
_, ps, _ := runtimePlugins(t)
|
||||
if err := ps.LoadSource("authtoken", bridgeProbe); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
out, err := ps.Call("authtoken", "install_operator_owned", map[string]interface{}{})
|
||||
if err != nil {
|
||||
t.Fatalf("调用本身不应报错(错误应作为返回值): %v", err)
|
||||
}
|
||||
m := out.(map[string]interface{})
|
||||
if m["name"] != nil {
|
||||
t.Fatalf("影子内置 openai 必须被拒,却返回了 name=%#v", m["name"])
|
||||
}
|
||||
if m["err"] == nil || m["err"] == "" {
|
||||
t.Fatal("影子内置应带回错误信息")
|
||||
}
|
||||
t.Logf("拒绝信息: %v", m["err"])
|
||||
}
|
||||
|
||||
// TestScheduledTaskRotatesTokenEndToEnd 是 R4 的闭环验收。
|
||||
//
|
||||
// 它把三个特性串成一条链,任何一个环节坏掉都会让它失败:
|
||||
// 1. 内核 ticker 驱动定时任务(无外部触发)
|
||||
// 2. 任务与钩子跑在同一个 VM(所以任务的 install 与 state 写入对钩子可见)
|
||||
// 3. Lua 侧适配器桥接可用(__llmsproxy_adapters.install)
|
||||
//
|
||||
// 这正是用户描述的 token 轮换形态:插件自己按时间拿到新 token,重写适配器,
|
||||
// 后续请求自然使用新逻辑 —— 全程没有任何浏览器或人工操作。
|
||||
func TestScheduledTaskRotatesTokenEndToEnd(t *testing.T) {
|
||||
shortTick(t)
|
||||
_, ps, _ := runtimePlugins(t)
|
||||
code := `
|
||||
local plugin = { name = "sched_rot", version = "1.0" }
|
||||
plugin.state = { token = "OLD", rotates = 0 }
|
||||
plugin.schedule = { { fn = "rotate", every = 1 } }
|
||||
function plugin.rotate()
|
||||
plugin.state.rotates = plugin.state.rotates + 1
|
||||
local tok = "T" .. tostring(plugin.state.rotates)
|
||||
local src = 'return { name = "rotauth", version = "1.0", endpoint = "/x", token = "' .. tok .. '" }'
|
||||
plugin.state.last_adapter, plugin.state.rotate_err = __llmsproxy_adapters.install("rotauth", src)
|
||||
plugin.state.token = tok
|
||||
end
|
||||
plugin.hooks = { routed = "observe" }
|
||||
function plugin.observe(payload)
|
||||
payload.token = plugin.state.token
|
||||
payload.rotates = plugin.state.rotates
|
||||
payload.adapter = plugin.state.last_adapter
|
||||
return payload
|
||||
end
|
||||
return plugin
|
||||
`
|
||||
if err := ps.LoadSource("sched_rot", code); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
deadline := time.Now().Add(4 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
out := ps.Fire(StageRouted, map[string]interface{}{})
|
||||
if n, ok := out["rotates"].(float64); ok && n >= 1 {
|
||||
t.Logf("任务轮换了 %v 次; 钩子看到 token=%v adapter=%v", n, out["token"], out["adapter"])
|
||||
if out["adapter"] != "rotauth" {
|
||||
t.Fatalf("任务里 install 未生效: adapter=%v", out["adapter"])
|
||||
}
|
||||
return
|
||||
}
|
||||
time.Sleep(30 * time.Millisecond)
|
||||
}
|
||||
t.Fatal("定时任务未完成 token 轮换")
|
||||
}
|
||||
|
||||
// TestShardVMHasNoAdapterBridge 锁定一条边界:分片 VM(ShardCall 用的私有 VM)
|
||||
// **不能**拿到适配器管理桥接。
|
||||
//
|
||||
// 理由:分片是文档化的「只读、可丢弃」工作单元,而 install/update/remove 是
|
||||
// **全局**副作用(改的是共享的适配器注册表,不是本 VM 的状态)。若把桥接给
|
||||
// 分片,一个被声明为只读的按需调用就能悄悄改写所有请求共用的适配器 ——
|
||||
// 正是分片契约要防的那类不可见变更。
|
||||
//
|
||||
// 断言方式是:分片里的插件代码检查 __llmsproxy_adapters 是否为 nil,返回结果。
|
||||
func TestShardVMHasNoAdapterBridge(t *testing.T) {
|
||||
_, ps, _ := runtimePlugins(t)
|
||||
code := `
|
||||
local plugin = { name = "sharded", version = "1.0" }
|
||||
plugin.exports = { "probe" }
|
||||
-- 只读探测:分片里这个全局必须是 nil
|
||||
function plugin.probe(args)
|
||||
return { has_bridge = __llmsproxy_adapters ~= nil }
|
||||
end
|
||||
return plugin
|
||||
`
|
||||
if err := ps.LoadSource("sharded", code); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// 主 VM 上应当有桥接(对照)
|
||||
mainOut, err := ps.Call("sharded", "probe", map[string]interface{}{})
|
||||
if err != nil {
|
||||
t.Fatalf("主 VM 调用失败: %v", err)
|
||||
}
|
||||
if m, ok := mainOut.(map[string]interface{}); !ok || m["has_bridge"] != true {
|
||||
t.Errorf("主 VM 上应有 __llmsproxy_adapters, 实得 %#v", mainOut)
|
||||
}
|
||||
|
||||
// 分片 VM 上不应有
|
||||
outs, err := ps.ShardCall("sharded", "probe", []map[string]interface{}{{}, {}})
|
||||
if err != nil {
|
||||
t.Fatalf("ShardCall: %v", err)
|
||||
}
|
||||
for i, o := range outs {
|
||||
m, ok := o.(map[string]interface{})
|
||||
if !ok {
|
||||
t.Fatalf("分片 %d 返回非 table: %#v", i, o)
|
||||
}
|
||||
if m["has_bridge"] == true {
|
||||
t.Errorf("分片 %d 不应拿到适配器桥接(分片是只读可丢弃的)", i)
|
||||
}
|
||||
}
|
||||
}
|
||||
264
internal/lua/plugin_adapter_bridge_test.go
Normal file
264
internal/lua/plugin_adapter_bridge_test.go
Normal file
@ -0,0 +1,264 @@
|
||||
package lua
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func bridgeVM(t *testing.T) (*VM, *Plugins) {
|
||||
t.Helper()
|
||||
dir := filepath.Join(t.TempDir(), "adapters")
|
||||
vm := NewVM(dir)
|
||||
if err := vm.Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(vm.Stop)
|
||||
ps := NewPlugins(vm, filepath.Join(t.TempDir(), "plugins"))
|
||||
return vm, ps
|
||||
}
|
||||
|
||||
const tokenAdapterV1 = `
|
||||
local adapter = {}
|
||||
adapter.name = "myauth"
|
||||
adapter.version = "1.0"
|
||||
adapter.endpoint = "/chat/completions"
|
||||
function adapter.transform_request(body)
|
||||
return '{"token":"TOKEN_V1"}'
|
||||
end
|
||||
return adapter
|
||||
`
|
||||
|
||||
const tokenAdapterV2 = `
|
||||
local adapter = {}
|
||||
adapter.name = "myauth"
|
||||
adapter.version = "2.0"
|
||||
adapter.endpoint = "/chat/completions"
|
||||
function adapter.transform_request(body)
|
||||
return '{"token":"TOKEN_V2"}'
|
||||
end
|
||||
return adapter
|
||||
`
|
||||
|
||||
// TestPluginInstallsAdapter 是基础能力:插件装的适配器立即可用。
|
||||
func TestPluginInstallsAdapter(t *testing.T) {
|
||||
vm, ps := bridgeVM(t)
|
||||
if _, err := ps.InstallAdapter("authtoken", "myauth", tokenAdapterV1); err != nil {
|
||||
t.Fatalf("install: %v", err)
|
||||
}
|
||||
if !vm.HasFn("myauth", "transform_request") {
|
||||
t.Fatal("安装后适配器未注册")
|
||||
}
|
||||
out, err := vm.Transform("myauth", "transform_request", "{}")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(out, "TOKEN_V1") {
|
||||
t.Errorf("安装的适配器未被使用: %s", out)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPluginUpdateAdapterRotatesToken 是核心用例:**token 轮换**。
|
||||
// 插件长期运行,在运行中改写适配器,后续请求用新 token —— 适配器自身
|
||||
// 不跑任何循环。
|
||||
func TestPluginUpdateAdapterRotatesToken(t *testing.T) {
|
||||
vm, ps := bridgeVM(t)
|
||||
if _, err := ps.InstallAdapter("authtoken", "myauth", tokenAdapterV1); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
first, _ := vm.Transform("myauth", "transform_request", "{}")
|
||||
if !strings.Contains(first, "TOKEN_V1") {
|
||||
t.Fatalf("初次应带 V1: %s", first)
|
||||
}
|
||||
|
||||
// 轮换:插件改写适配器
|
||||
if err := ps.UpdateAdapter("authtoken", "myauth", tokenAdapterV2); err != nil {
|
||||
t.Fatalf("update: %v", err)
|
||||
}
|
||||
second, err := vm.Transform("myauth", "transform_request", "{}")
|
||||
if err != nil {
|
||||
t.Fatalf("改写后适配器不可用: %v", err)
|
||||
}
|
||||
if !strings.Contains(second, "TOKEN_V2") {
|
||||
t.Errorf("轮换后应带 V2: %s", second)
|
||||
}
|
||||
if strings.Contains(second, "TOKEN_V1") {
|
||||
t.Errorf("轮换后仍带旧 V1(池未重建?): %s", second)
|
||||
}
|
||||
// 版本号也应更新,便于运维从 /api/status 看出发生过轮换
|
||||
if v := vm.AdapterVersion("myauth"); v != "2.0" {
|
||||
t.Errorf("适配器版本 = %q, want 2.0", v)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPluginCannotShadowBuiltin:插件不能篡改内核自带的适配器。
|
||||
func TestPluginCannotShadowBuiltin(t *testing.T) {
|
||||
_, ps := bridgeVM(t)
|
||||
evil := `local a={} a.name="openai" a.version="EVIL" a.endpoint="/x" return a`
|
||||
if _, err := ps.InstallAdapter("sneaky", "openai", evil); err == nil {
|
||||
t.Fatal("插件装与内置同名的适配器应被拒")
|
||||
}
|
||||
}
|
||||
|
||||
// TestPluginCannotStealAnotherPluginsAdapter:归属隔离。
|
||||
func TestPluginCannotStealAnotherPluginsAdapter(t *testing.T) {
|
||||
_, ps := bridgeVM(t)
|
||||
if _, err := ps.InstallAdapter("alpha", "shared_auth", tokenAdapterV1); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := ps.InstallAdapter("beta", "shared_auth", tokenAdapterV2); err == nil {
|
||||
t.Error("插件 B 应无法覆盖插件 A 装的适配器")
|
||||
}
|
||||
if err := ps.UpdateAdapter("beta", "shared_auth", tokenAdapterV2); err == nil {
|
||||
t.Error("插件 B 应无法更新插件 A 的适配器")
|
||||
}
|
||||
if err := ps.RemoveAdapter("beta", "shared_auth"); err == nil {
|
||||
t.Error("插件 B 应无法删除插件 A 的适配器")
|
||||
}
|
||||
// 属主自己可以
|
||||
if err := ps.UpdateAdapter("alpha", "shared_auth", tokenAdapterV2); err != nil {
|
||||
t.Errorf("属主应能更新自己的适配器: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPluginCannotTouchOperatorAdapter:运维的适配器不可被插件改写。
|
||||
func TestPluginCannotTouchOperatorAdapter(t *testing.T) {
|
||||
_, ps := bridgeVM(t)
|
||||
if err := ps.UpdateAdapter("authtoken", "openai", tokenAdapterV1); err == nil {
|
||||
t.Error("插件更新运维适配器应被拒")
|
||||
}
|
||||
if err := ps.RemoveAdapter("authtoken", "openai"); err == nil {
|
||||
t.Error("插件删除运维适配器应被拒")
|
||||
}
|
||||
}
|
||||
|
||||
// TestPluginAdapterNameTraversal:路径穿越名在桥接层同样被拒。
|
||||
func TestPluginAdapterNameTraversal(t *testing.T) {
|
||||
_, ps := bridgeVM(t)
|
||||
for _, bad := range []string{"../evil", "/etc/passwd", "has space", ".."} {
|
||||
if _, err := ps.InstallAdapter("authtoken", bad, tokenAdapterV1); err == nil {
|
||||
t.Errorf("桥接层应拒绝路径穿越名 %q", bad)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestPluginRemoveAdapter 让插件能回收自己的适配器(token 失效/功能下线)。
|
||||
func TestPluginRemoveAdapter(t *testing.T) {
|
||||
vm, ps := bridgeVM(t)
|
||||
if _, err := ps.InstallAdapter("authtoken", "myauth", tokenAdapterV1); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := ps.RemoveAdapter("authtoken", "myauth"); err != nil {
|
||||
t.Fatalf("remove: %v", err)
|
||||
}
|
||||
if vm.HasFn("myauth", "transform_request") {
|
||||
t.Error("删除后适配器仍在")
|
||||
}
|
||||
if owners := ps.PluginAdapters("authtoken"); len(owners) != 0 {
|
||||
t.Errorf("删除后归属表应清空,实得 %v", owners)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPluginAdapterOwnershipListing 供 UI/诊断用。
|
||||
func TestPluginAdapterOwnershipListing(t *testing.T) {
|
||||
_, ps := bridgeVM(t)
|
||||
if _, err := ps.InstallAdapter("alpha", "a1", tokenAdapterV1); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := ps.InstallAdapter("alpha", "a2", tokenAdapterV1); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := ps.InstallAdapter("beta", "b1", tokenAdapterV1); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got := ps.PluginAdapters("alpha")
|
||||
if len(got) != 2 || got[0] != "a1" || got[1] != "a2" {
|
||||
t.Errorf("alpha 的适配器 = %v, want [a1 a2]", got)
|
||||
}
|
||||
if o := ps.AdapterOwnerOf("b1"); o != "beta" {
|
||||
t.Errorf("b1 属主 = %q, want beta", o)
|
||||
}
|
||||
if o := ps.AdapterOwnerOf("openai"); o != "" {
|
||||
t.Errorf("内置适配器不应有插件属主,实得 %q", o)
|
||||
}
|
||||
}
|
||||
|
||||
// TestConcurrentPluginAdapterUpdates 是压力测试:多 goroutine 轮换同一适配器。
|
||||
// 池重建与归属表都要在并发下自洽(不 panic、不串属主)。
|
||||
func TestConcurrentPluginAdapterUpdates(t *testing.T) {
|
||||
vm, ps := bridgeVM(t)
|
||||
if _, err := ps.InstallAdapter("authtoken", "myauth", tokenAdapterV1); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
const workers, iters = 8, 20
|
||||
var wg sync.WaitGroup
|
||||
errs := make(chan string, workers*iters)
|
||||
for w := 0; w < workers; w++ {
|
||||
wg.Add(1)
|
||||
go func(w int) {
|
||||
defer wg.Done()
|
||||
for i := 0; i < iters; i++ {
|
||||
code := tokenAdapterV1
|
||||
if i%2 == 1 {
|
||||
code = tokenAdapterV2
|
||||
}
|
||||
if err := ps.UpdateAdapter("authtoken", "myauth", code); err != nil {
|
||||
errs <- "update: " + err.Error()
|
||||
return
|
||||
}
|
||||
// 更新后必须仍可用(池重建期间不能出现「找不到适配器」)
|
||||
if _, err := vm.Transform("myauth", "transform_request", "{}"); err != nil {
|
||||
errs <- "update 后适配器不可用: " + err.Error()
|
||||
return
|
||||
}
|
||||
}
|
||||
}(w)
|
||||
}
|
||||
wg.Wait()
|
||||
close(errs)
|
||||
for e := range errs {
|
||||
t.Error(e)
|
||||
}
|
||||
if o := ps.AdapterOwnerOf("myauth"); o != "authtoken" {
|
||||
t.Errorf("并发后属主应为 authtoken,实得 %q", o)
|
||||
}
|
||||
}
|
||||
|
||||
// TestInstalledAdapterBodyIsValidJSON 确认插件提供的代码必须真能编译成
|
||||
// 可用适配器 —— 语法错误在安装时就报,而不是等到第一个请求。
|
||||
func TestInstalledAdapterBodyIsValidJSON(t *testing.T) {
|
||||
_, ps := bridgeVM(t)
|
||||
if _, err := ps.InstallAdapter("bad", "broken", "this is not lua ==="); err == nil {
|
||||
t.Error("语法错误的适配器应在安装时被拒")
|
||||
}
|
||||
if _, err := ps.InstallAdapter("bad", "notatable", "return 42"); err == nil {
|
||||
t.Error("未返回 table 的适配器应在安装时被拒")
|
||||
}
|
||||
// 失败的安装不应留下归属记录
|
||||
if o := ps.AdapterOwnerOf("broken"); o != "" {
|
||||
t.Errorf("安装失败却留下了属主记录: %q", o)
|
||||
}
|
||||
if o := ps.AdapterOwnerOf("notatable"); o != "" {
|
||||
t.Errorf("安装失败却留下了属主记录: %q", o)
|
||||
}
|
||||
}
|
||||
|
||||
// TestInstallAdapterReportsActualName 适配器脚本可声明与注册键不同的 name;
|
||||
// 调用方拿到实际名,便于更新配置。
|
||||
func TestInstallAdapterReportsActualName(t *testing.T) {
|
||||
_, ps := bridgeVM(t)
|
||||
code := `local a={} a.name="declared_name" a.version="1" a.endpoint="/x" return a`
|
||||
got, err := ps.InstallAdapter("p", "registration_key", code)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got != "declared_name" {
|
||||
t.Errorf("返回名 = %q, want declared_name", got)
|
||||
}
|
||||
if o := ps.AdapterOwnerOf("declared_name"); o != "p" {
|
||||
t.Errorf("归属应记在声明名上,实得 %q", o)
|
||||
}
|
||||
var _ = json.Marshal
|
||||
}
|
||||
264
internal/lua/plugin_bridges.go
Normal file
264
internal/lua/plugin_bridges.go
Normal file
@ -0,0 +1,264 @@
|
||||
package lua
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
golua "github.com/aarzilli/golua/lua"
|
||||
)
|
||||
|
||||
// Package lua — Lua 绑定:凭据保险库 + 内核动作面
|
||||
//
|
||||
// 这两个全局把"插件自己主导链接认证"所需的两块补齐:
|
||||
//
|
||||
// __llmsproxy_secrets —— 安全存放 OAuth token / session / 设备指纹
|
||||
// __llmsproxy_kernel —— 触发内核动作(注册源、探测、清冷却、改链路…)
|
||||
//
|
||||
// 注册点与 __llmsproxy_http 相同:只在 PLUGIN 的 bootPluginState 里装,
|
||||
// 因为适配器共用 setupGlobals 而不该拿到这些能力。
|
||||
//
|
||||
// 两张表都用 buildJSONTable 的既有写法(NewTable + PushGoClosure + SetField),
|
||||
// 该写法在 json 表上长期稳定,也是内核自己的用法。
|
||||
|
||||
// secretGlobal 是凭据保险库的 Lua 名字。
|
||||
const secretGlobal = "__llmsproxy_secrets"
|
||||
|
||||
// kernelGlobal 是内核动作面的 Lua 名字。
|
||||
const kernelGlobal = "__llmsproxy_kernel"
|
||||
|
||||
// registerSecretBridge installs the credential vault onto a plugin's state.
|
||||
//
|
||||
// The plugin name is bound HERE, at boot, rather than read from the Lua state at
|
||||
// call time. That is what makes "a plugin can only read its OWN credentials" a
|
||||
// mechanism instead of a convention: a shard or any other holder of the compiled
|
||||
// script cannot pass a different name.
|
||||
func registerSecretBridge(L *golua.State, ps *Plugins, name string) {
|
||||
L.NewTable()
|
||||
L.PushGoClosure(luaSecretSet(ps, name))
|
||||
L.SetField(-2, "set")
|
||||
L.PushGoClosure(luaSecretGet(ps, name))
|
||||
L.SetField(-2, "get")
|
||||
L.PushGoClosure(luaSecretDelete(ps, name))
|
||||
L.SetField(-2, "delete")
|
||||
L.PushGoClosure(luaSecretKeys(ps, name))
|
||||
L.SetField(-2, "keys")
|
||||
L.SetGlobal(secretGlobal)
|
||||
}
|
||||
|
||||
// luaSecretSet: set(key, value) -> (true, nil) | (nil, err)
|
||||
func luaSecretSet(ps *Plugins, name string) golua.LuaGoFunction {
|
||||
return func(L *golua.State) int {
|
||||
key := L.ToString(1)
|
||||
val := L.ToString(2)
|
||||
if err := ps.SetSecret(name, key, val); err != nil {
|
||||
L.PushNil()
|
||||
L.PushString(err.Error())
|
||||
return 2
|
||||
}
|
||||
L.PushBoolean(true)
|
||||
L.PushNil()
|
||||
return 2
|
||||
}
|
||||
}
|
||||
|
||||
// luaSecretGet: get(key) -> (value, nil) | (nil, err). A missing key yields
|
||||
// (nil, nil) so a plugin can test `if tok == nil then login() end` — the "no
|
||||
// credential stored yet" case is the common one for a fresh install, and making
|
||||
// it an error would force every caller to string-match on a message.
|
||||
func luaSecretGet(ps *Plugins, name string) golua.LuaGoFunction {
|
||||
return func(L *golua.State) int {
|
||||
key := L.ToString(1)
|
||||
v, ok, err := ps.GetSecret(name, key)
|
||||
if err != nil {
|
||||
L.PushNil()
|
||||
L.PushString(err.Error())
|
||||
return 2
|
||||
}
|
||||
if !ok {
|
||||
L.PushNil()
|
||||
L.PushNil()
|
||||
return 2
|
||||
}
|
||||
L.PushString(v)
|
||||
L.PushNil()
|
||||
return 2
|
||||
}
|
||||
}
|
||||
|
||||
// luaSecretDelete: delete(key) -> (true, nil) | (nil, err)
|
||||
func luaSecretDelete(ps *Plugins, name string) golua.LuaGoFunction {
|
||||
return func(L *golua.State) int {
|
||||
key := L.ToString(1)
|
||||
if err := ps.DeleteSecret(name, key); err != nil {
|
||||
L.PushNil()
|
||||
L.PushString(err.Error())
|
||||
return 2
|
||||
}
|
||||
L.PushBoolean(true)
|
||||
L.PushNil()
|
||||
return 2
|
||||
}
|
||||
}
|
||||
|
||||
// luaSecretKeys: keys() -> (array of key names, nil). Names only — a plugin can
|
||||
// render "which accounts are configured" without ever echoing token bytes.
|
||||
func luaSecretKeys(ps *Plugins, name string) golua.LuaGoFunction {
|
||||
return func(L *golua.State) int {
|
||||
keys, err := ps.SecretKeys(name)
|
||||
if err != nil {
|
||||
L.PushNil()
|
||||
L.PushString(err.Error())
|
||||
return 2
|
||||
}
|
||||
L.NewTable()
|
||||
for i, k := range keys {
|
||||
L.PushString(k)
|
||||
L.RawSeti(-2, i+1)
|
||||
}
|
||||
L.PushNil()
|
||||
return 2
|
||||
}
|
||||
}
|
||||
|
||||
// registerKernelBridge installs the kernel action surface onto a plugin's state.
|
||||
func registerKernelBridge(L *golua.State, ps *Plugins, name string) {
|
||||
L.NewTable()
|
||||
L.PushGoClosure(luaKernelActions(ps, name))
|
||||
L.SetField(-2, "actions")
|
||||
L.PushGoClosure(luaKernelCall(ps, name))
|
||||
L.SetField(-2, "call")
|
||||
L.SetGlobal(kernelGlobal)
|
||||
}
|
||||
|
||||
// luaKernelActions: actions() -> (array of {name,title,danger,description,params,
|
||||
// granted}, nil)
|
||||
//
|
||||
// It reports GRANTED alongside each action so a plugin can degrade gracefully
|
||||
// (skip the part that needs a permission it does not have) instead of
|
||||
// discovering it by hitting an error mid-operation.
|
||||
func luaKernelActions(ps *Plugins, name string) golua.LuaGoFunction {
|
||||
return func(L *golua.State) int {
|
||||
perm := ps.PluginKernelPermission(name)
|
||||
L.NewTable()
|
||||
for i, a := range KernelActions() {
|
||||
L.NewTable()
|
||||
L.PushString(a.Name)
|
||||
L.SetField(-2, "name")
|
||||
L.PushString(a.Title)
|
||||
L.SetField(-2, "title")
|
||||
L.PushString(a.Danger)
|
||||
L.SetField(-2, "danger")
|
||||
L.PushString(a.Description)
|
||||
L.SetField(-2, "description")
|
||||
L.PushBoolean(perm.allows(a))
|
||||
L.SetField(-2, "granted")
|
||||
L.NewTable()
|
||||
for j, p := range a.Params {
|
||||
L.PushString(p)
|
||||
L.RawSeti(-2, j+1)
|
||||
}
|
||||
L.SetField(-2, "params")
|
||||
L.RawSeti(-2, i+1)
|
||||
}
|
||||
L.PushNil()
|
||||
return 2
|
||||
}
|
||||
}
|
||||
|
||||
// luaKernelCall: call(name, args) -> (result, nil) | (nil, err)
|
||||
//
|
||||
// One entry point rather than a function per action: the action set will change,
|
||||
// and a table field per action would mean editing bootPluginState — the exact
|
||||
// place that produced a cgo crash during earlier work.
|
||||
func luaKernelCall(ps *Plugins, name string) golua.LuaGoFunction {
|
||||
return func(L *golua.State) int {
|
||||
action := L.ToString(1)
|
||||
args := map[string]interface{}{}
|
||||
if L.Type(2) == golua.LUA_TTABLE {
|
||||
var raw interface{}
|
||||
if err := luaToJSON(L, 2, &raw); err == nil {
|
||||
if m, ok := raw.(map[string]interface{}); ok {
|
||||
args = m
|
||||
}
|
||||
}
|
||||
}
|
||||
v, err := ps.callKernel(name, action, args)
|
||||
if err != nil {
|
||||
L.PushNil()
|
||||
L.PushString(err.Error())
|
||||
return 2
|
||||
}
|
||||
if v == nil {
|
||||
L.PushNil()
|
||||
} else {
|
||||
pushGoValue(L, v)
|
||||
}
|
||||
L.PushNil()
|
||||
return 2
|
||||
}
|
||||
}
|
||||
|
||||
// readPluginKernelDeclared reads `plugin.kernel`, the actions a plugin asks for.
|
||||
//
|
||||
// The shape is a list of names:
|
||||
//
|
||||
// plugin.kernel = { "status", "reset_health" }
|
||||
//
|
||||
// Unknown names are kept rather than dropped so the settings page can show a
|
||||
// typo as "requested but not a real action" — silently discarding it would make
|
||||
// a plugin's intent invisible at exactly the moment the operator is deciding
|
||||
// what to grant.
|
||||
func readPluginKernelDeclared(L *golua.State) []string {
|
||||
raw, ok := pluginFieldValue(L, "kernel")
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
items, isList := raw.([]interface{})
|
||||
if !isList {
|
||||
if one, isTable := raw.(map[string]interface{}); isTable {
|
||||
// A single string field or a set-style table; accept both spellings.
|
||||
if v, ok := one["actions"].([]interface{}); ok {
|
||||
items = v
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
out := make([]string, 0, len(items))
|
||||
seen := map[string]bool{}
|
||||
for _, it := range items {
|
||||
name, ok := it.(string)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
name = strings.TrimSpace(name)
|
||||
if name == "" || seen[name] {
|
||||
continue
|
||||
}
|
||||
seen[name] = true
|
||||
out = append(out, name)
|
||||
}
|
||||
sort.Strings(out)
|
||||
return out
|
||||
}
|
||||
|
||||
// luaTableArg decodes a Lua table argument into a Go map, reporting a clear error
|
||||
// when the caller passed something else (a common mistake that would otherwise
|
||||
// surface as a silent zero value).
|
||||
func luaTableArg(L *golua.State, idx int) (map[string]interface{}, error) {
|
||||
if L.Type(idx) != golua.LUA_TTABLE {
|
||||
return nil, fmt.Errorf("expected a table argument, got %s", normalizeString(L, idx))
|
||||
}
|
||||
var raw interface{}
|
||||
if err := luaToJSON(L, idx, &raw); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
m, ok := raw.(map[string]interface{})
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("argument must be a keyed table")
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
95
internal/lua/plugin_clock_test.go
Normal file
95
internal/lua/plugin_clock_test.go
Normal file
@ -0,0 +1,95 @@
|
||||
package lua
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TestPluginClockDuringNetworkWait pins the reason the probe budget never
|
||||
// enforced itself.
|
||||
//
|
||||
// Measured on the live gateway before the fix: a source that skipped after
|
||||
// timing out took 24013ms — exactly 4s timeout x 2 retries x 3 adapters — while
|
||||
// the plugin's own PROBE_BUDGET_MS is 12000. A budget that never fires looks
|
||||
// exactly like a budget nobody remembered to call.
|
||||
//
|
||||
// The cause is os.clock(): it measures CPU time, and a plugin blocked on the
|
||||
// network consumes almost none of it. This is the same trap as measuring lock
|
||||
// contention with a busy-wait, one layer down — and the first version of this
|
||||
// test made that mistake, reporting os.clock tracking wall time because the
|
||||
// "wait" it used was a spin loop burning real CPU. It had to be re-measured
|
||||
// against an actual HTTP request.
|
||||
func TestPluginClockDuringNetworkWait(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
pluginDir := filepath.Join(dir, "plugins")
|
||||
if err := os.MkdirAll(pluginDir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
vm := NewVM(filepath.Join(dir, "adapters"))
|
||||
if err := vm.Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(vm.Stop)
|
||||
ps := NewPlugins(vm, pluginDir)
|
||||
t.Cleanup(ps.Close)
|
||||
ps.SetOutboundHosts([]string{"127.0.0.1"})
|
||||
|
||||
code := `
|
||||
local P = {}
|
||||
P.name = "clocktest"
|
||||
P.version = "1.0.0"
|
||||
P.exports = { "measure" }
|
||||
P.measure = function(payload)
|
||||
local c0, w0 = os.clock(), os.time()
|
||||
-- The real thing: block on the network. The VM sits inside Go's HTTP client,
|
||||
-- so Lua executes nothing and consumes no CPU while this takes wall time.
|
||||
__llmsproxy_http({ url = payload.url, method = "GET", timeout_ms = 5000 })
|
||||
return {
|
||||
clock = os.clock() - c0,
|
||||
wall = os.time() - w0,
|
||||
}
|
||||
end
|
||||
return P
|
||||
`
|
||||
if err := os.WriteFile(filepath.Join(pluginDir, "clocktest.lua"), []byte(code), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
time.Sleep(3 * time.Second)
|
||||
_, _ = w.Write([]byte(`{}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
if err := ps.LoadDir(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
p := ps.find("clocktest")
|
||||
if p == nil {
|
||||
t.Fatal("plugin not found")
|
||||
}
|
||||
if p.LoadError != "" {
|
||||
t.Fatalf("plugin failed to compile: %s", p.LoadError)
|
||||
}
|
||||
out, err := ps.Call("clocktest", "measure", map[string]interface{}{"url": srv.URL + "/slow"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m, _ := out.(map[string]interface{})
|
||||
clock, _ := m["clock"].(float64)
|
||||
wall, _ := m["wall"].(float64)
|
||||
t.Logf("3s blocked on HTTP: os.clock advanced %.3fs, os.time advanced %.0fs", clock, wall)
|
||||
|
||||
if wall < 3 {
|
||||
t.Fatalf("the request did not take 3 wall seconds (got %.0f)", wall)
|
||||
}
|
||||
if clock > wall/2 {
|
||||
t.Fatalf("os.clock tracked wall time (%.3f vs %.0f) — the budget is not the cause; "+
|
||||
"look elsewhere", clock, wall)
|
||||
}
|
||||
t.Logf("CONFIRMED: blocked on the network for %.0fs, os.clock advanced %.3fs. "+
|
||||
"A budget written in os.clock never fires while a plugin waits on I/O.", wall, clock)
|
||||
}
|
||||
244
internal/lua/plugin_decode.go
Normal file
244
internal/lua/plugin_decode.go
Normal file
@ -0,0 +1,244 @@
|
||||
package lua
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
golua "github.com/aarzilli/golua/lua"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// The declarative decoder.
|
||||
//
|
||||
// Why this exists: plugin declarations were read by three hand-written functions
|
||||
// (readPluginUI, readPluginExports, readPluginHTTP) that each re-implemented the
|
||||
// same walk over a decoded Lua table — get this field, coerce it, append it. The
|
||||
// field names lived in the .lua and again in the Go, so adding one meant editing
|
||||
// both, and forgetting the Go half produced NO ERROR AT ALL: the field silently
|
||||
// read as unset.
|
||||
//
|
||||
// That happened four times in one session, all the same shape — a declaration
|
||||
// field silently reading as unset:
|
||||
//
|
||||
// - readPluginUI and readPluginExports each re-walked the Lua stack per field
|
||||
// and decoded through different helpers with different INDEX SEMANTICS.
|
||||
// - readPluginHTTP read a Lua array with an index loop; golua's GetField does
|
||||
// not convert a numeric string key, so the loop produced an empty list and
|
||||
// every plugin appeared to declare no endpoints.
|
||||
// - The endpoint-access override keys (plugin#path) leaked into the plugin
|
||||
// audience listing, because both lived in one map and nothing distinguished
|
||||
// them on the way out.
|
||||
//
|
||||
// On the first two: the comments that shipped with those fixes — in three places
|
||||
// and in a commit message — claimed the cause was that registration runs before
|
||||
// setupGlobals installs the json global. That was never verified and is FALSE.
|
||||
// Measured: setupGlobals runs at vm.go:574, the readers at 787, json is present,
|
||||
// and luaToJSON decodes the same stack shape correctly. The real constraint is
|
||||
// that luaTableToGoValue resolves a negative index to absolute immediately
|
||||
// (GetTop()+idx+1), so it is only valid while the caller's stack shape holds —
|
||||
// which is why the decode now happens once, at the edge, and never per field.
|
||||
//
|
||||
// // A schema makes the failure loud instead: an unknown or mistyped field is a
|
||||
// load-time error naming the field and the plugin, and the Go side has exactly
|
||||
// one list of field names to keep in sync.
|
||||
//
|
||||
// The schema is deliberately data, not reflection or struct tags. A plugin author
|
||||
// reads one table in this file to learn what the gateway understands; adding a
|
||||
// declaration field is one line here plus one in the .lua, and a declaration the
|
||||
// gateway ignores is reported rather than dropped.
|
||||
|
||||
// decodeCtx carries the plugin name so every diagnostic can name its source.
|
||||
type decodeCtx struct {
|
||||
plugin string
|
||||
// problems accumulates non-fatal findings. A declaration the gateway does
|
||||
// not understand is reported and skipped — refusing to load the plugin would
|
||||
// turn a forward-compatible addition into an outage.
|
||||
problems []string
|
||||
}
|
||||
|
||||
func (c *decodeCtx) warnf(format string, args ...interface{}) {
|
||||
c.problems = append(c.problems, fmt.Sprintf(format, args...))
|
||||
}
|
||||
|
||||
// strictKeys reports keys present in m that the schema does not describe.
|
||||
//
|
||||
// This is the whole point: the old decoders silently ignored everything they did
|
||||
// not know about, which is indistinguishable from a plugin that simply did not
|
||||
// declare the field. A typo in a field name — `visibilty`, `stateScope` — used to
|
||||
// vanish with no trace at all.
|
||||
func (c *decodeCtx) strictKeys(m map[string]interface{}, path string, allowed ...string) {
|
||||
set := make(map[string]struct{}, len(allowed))
|
||||
for _, a := range allowed {
|
||||
set[a] = struct{}{}
|
||||
}
|
||||
var unknown []string
|
||||
for k := range m {
|
||||
if _, ok := set[k]; !ok {
|
||||
unknown = append(unknown, k)
|
||||
}
|
||||
}
|
||||
if len(unknown) == 0 {
|
||||
return
|
||||
}
|
||||
sort.Strings(unknown)
|
||||
// Suggest the closest match: a one-character typo is the common case and
|
||||
// "did you mean" turns a silent misconfiguration into an obvious fix.
|
||||
for _, u := range unknown {
|
||||
if near := closestField(u, allowed); near != "" {
|
||||
c.warnf("%s: unknown field %q (did you mean %q?)", path, u, near)
|
||||
} else {
|
||||
c.warnf("%s: unknown field %q", path, u)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func closestField(got string, candidates []string) string {
|
||||
best, bestD := "", 1<<30
|
||||
for _, c := range candidates {
|
||||
d := editDistance(strings.ToLower(got), strings.ToLower(c))
|
||||
if d < bestD {
|
||||
best, bestD = c, d
|
||||
}
|
||||
}
|
||||
// Only suggest on a genuinely close match; "pages" vs "state" is not helpful.
|
||||
if bestD <= 2 {
|
||||
return best
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func editDistance(a, b string) int {
|
||||
la, lb := len(a), len(b)
|
||||
prev := make([]int, lb+1)
|
||||
cur := make([]int, lb+1)
|
||||
for j := 0; j <= lb; j++ {
|
||||
prev[j] = j
|
||||
}
|
||||
for i := 1; i <= la; i++ {
|
||||
cur[0] = i
|
||||
for j := 1; j <= lb; j++ {
|
||||
cost := 1
|
||||
if a[i-1] == b[j-1] {
|
||||
cost = 0
|
||||
}
|
||||
cur[j] = min3(cur[j-1]+1, prev[j]+1, prev[j-1]+cost)
|
||||
}
|
||||
prev, cur = cur, prev
|
||||
}
|
||||
return prev[lb]
|
||||
}
|
||||
|
||||
func min3(a, b, c int) int {
|
||||
if b < a {
|
||||
a = b
|
||||
}
|
||||
if c < a {
|
||||
a = c
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
// ---- field readers -------------------------------------------------------
|
||||
//
|
||||
// Each reader takes the already-decoded Go value (map[string]interface{} from
|
||||
// luaTableToGoValue) rather than the Lua stack. That split is deliberate: the
|
||||
// decode is the part that must never depend on a global installed later, and
|
||||
// keeping it at the edge means the field readers are pure functions that can be
|
||||
// tested without a VM.
|
||||
|
||||
func decString(m map[string]interface{}, key string) string {
|
||||
if m == nil {
|
||||
return ""
|
||||
}
|
||||
return asString(m[key])
|
||||
}
|
||||
|
||||
func decStringList(m map[string]interface{}, key string) []string {
|
||||
if m == nil {
|
||||
return nil
|
||||
}
|
||||
raw, ok := m[key].([]interface{})
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
var out []string
|
||||
for _, it := range raw {
|
||||
if s := asString(it); s != "" {
|
||||
out = append(out, s)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func decTable(m map[string]interface{}, key string) map[string]interface{} {
|
||||
if m == nil {
|
||||
return nil
|
||||
}
|
||||
t, _ := m[key].(map[string]interface{})
|
||||
return t
|
||||
}
|
||||
|
||||
func decTableList(m map[string]interface{}, key string) []map[string]interface{} {
|
||||
if m == nil {
|
||||
return nil
|
||||
}
|
||||
raw, ok := m[key].([]interface{})
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
var out []map[string]interface{}
|
||||
for _, it := range raw {
|
||||
if t, ok := it.(map[string]interface{}); ok {
|
||||
out = append(out, t)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// decIntMap reads a string→number table, e.g. state_scope.depth = { global = 1 }.
|
||||
func decIntMap(m map[string]interface{}, key string) map[string]int {
|
||||
t := decTable(m, key)
|
||||
if t == nil {
|
||||
return nil
|
||||
}
|
||||
out := make(map[string]int, len(t))
|
||||
for k, v := range t {
|
||||
if n, ok := asFloat(v); ok {
|
||||
out[k] = int(n)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// pluginFieldValue reads one top-level declaration value off the plugin global,
|
||||
// already decoded to a Go value.
|
||||
//
|
||||
// Every declaration reader goes through here so the decode happens in exactly
|
||||
// one place, immediately after GetGlobal+GetField and before SetTop(0).
|
||||
//
|
||||
// It uses luaTableToGoValue, which converts the table to Go values directly
|
||||
// (nested tables, arrays and mixed keys included) rather than round-tripping
|
||||
// through a JSON string.
|
||||
//
|
||||
// An earlier version of the surrounding comments insisted the reason was that
|
||||
// registration runs before setupGlobals installs the json global. Measured, that
|
||||
// is false: setupGlobals is at vm.go:574, the readers at 787, and json IS on the
|
||||
// stack. The real constraint is index semantics — luaTableToGoValue resolves a
|
||||
// negative index to absolute up front (GetTop()+idx+1), so it is only valid
|
||||
// while the stack still holds what the caller pushed. Decoding once here, at the
|
||||
// edge, is what keeps that true; the old readers re-walked the stack per field.
|
||||
func pluginFieldValue(L *golua.State, field string) (interface{}, bool) {
|
||||
L.GetGlobal(pluginGlobal)
|
||||
if L.IsNil(-1) {
|
||||
L.SetTop(0)
|
||||
return nil, false
|
||||
}
|
||||
L.GetField(-1, field)
|
||||
if L.Type(-1) == golua.LUA_TNIL {
|
||||
L.SetTop(0)
|
||||
return nil, false
|
||||
}
|
||||
raw := luaTableToGoValue(L, -1)
|
||||
L.SetTop(0)
|
||||
return raw, raw != nil
|
||||
}
|
||||
227
internal/lua/plugin_decode_test.go
Normal file
227
internal/lua/plugin_decode_test.go
Normal file
@ -0,0 +1,227 @@
|
||||
package lua
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestUnknownDeclarationFieldIsReported is the property the decoder exists for.
|
||||
//
|
||||
// Before it, a declaration field the gateway did not recognise was dropped with
|
||||
// no diagnostic anywhere. `ui = { visibilty = "admin" }` — one character off —
|
||||
// restricted nothing and was indistinguishable from a plugin that simply chose
|
||||
// not to declare an audience, so the misconfiguration survived until someone read
|
||||
// the .lua by hand.
|
||||
//
|
||||
// The three hand-written readers it replaced had all been bitten by a related
|
||||
// silent failure (luaToJSON before the json global exists; an index loop over a
|
||||
// Lua array; the endpoint-override keys leaking into the audience listing), which
|
||||
// is why the decoder reports rather than ignores.
|
||||
func TestUnknownDeclarationFieldIsReported(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "typo.lua")
|
||||
src := `local P = {}
|
||||
P.name = "typo"
|
||||
P.version = "1.0.0"
|
||||
P.ui = { visibilty = "admin" }
|
||||
return P
|
||||
`
|
||||
if err := os.WriteFile(path, []byte(src), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
vm := NewVM(filepath.Join(dir, "adapters"))
|
||||
if err := vm.Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(vm.Stop)
|
||||
ps := NewPlugins(vm, filepath.Join(dir, "plugins"))
|
||||
if err := ps.LoadSource("typo", src); err != nil {
|
||||
t.Fatalf("a forward-compatible unknown field must not fail the load: %v", err)
|
||||
}
|
||||
|
||||
ctx := &decodeCtx{plugin: "typo"}
|
||||
ui := decodeUIExtension(ctx, map[string]interface{}{"visibilty": "admin"})
|
||||
if len(ctx.problems) == 0 {
|
||||
t.Fatal("the misspelled field was accepted silently — this is the failure the decoder removes")
|
||||
}
|
||||
if !strings.Contains(ctx.problems[0], "visibilty") {
|
||||
t.Errorf("diagnostic does not name the offending field: %q", ctx.problems[0])
|
||||
}
|
||||
// And it must be actionable, not just a complaint.
|
||||
if !strings.Contains(ctx.problems[0], "did you mean \"visibility\"") {
|
||||
t.Errorf("diagnostic offers no fix: %q", ctx.problems[0])
|
||||
}
|
||||
// Crucially: the audience the operator THOUGHT they set is not applied.
|
||||
if ui.Visibility != "" {
|
||||
t.Errorf("Visibility = %q from a misspelled field; a typo must not half-apply", ui.Visibility)
|
||||
}
|
||||
}
|
||||
|
||||
// TestKnownFieldsProduceNoDiagnostics: the strict check must not fire on a correct
|
||||
// declaration. A decoder that complains about valid input gets muted, and then
|
||||
// it catches nothing at all.
|
||||
func TestKnownFieldsProduceNoDiagnostics(t *testing.T) {
|
||||
ctx := &decodeCtx{plugin: "billing"}
|
||||
ui := decodeUIExtension(ctx, map[string]interface{}{
|
||||
"visibility": "admin",
|
||||
"state_scope": map[string]interface{}{
|
||||
"global": []interface{}{"total", "by_source"},
|
||||
"depth": map[string]interface{}{"by_key": float64(1)},
|
||||
},
|
||||
"page": map[string]interface{}{"page_id": "billing", "mount": "<div></div>"},
|
||||
"elements": []interface{}{
|
||||
map[string]interface{}{"target": "status", "mount": "<b></b>", "order": float64(1)},
|
||||
},
|
||||
})
|
||||
if len(ctx.problems) != 0 {
|
||||
t.Errorf("a correct declaration produced diagnostics: %v", ctx.problems)
|
||||
}
|
||||
if ui.Visibility != "admin" {
|
||||
t.Errorf("Visibility = %q, want admin", ui.Visibility)
|
||||
}
|
||||
if ui.StateScope == nil || len(ui.StateScope.Global) != 2 {
|
||||
t.Errorf("state_scope not decoded: %+v", ui.StateScope)
|
||||
}
|
||||
if ui.Page == nil || ui.Page.PageID != "billing" {
|
||||
t.Errorf("page not decoded: %+v", ui.Page)
|
||||
}
|
||||
if len(ui.Elements) != 1 || ui.Elements[0].Target != "status" {
|
||||
t.Errorf("elements not decoded: %+v", ui.Elements)
|
||||
}
|
||||
}
|
||||
|
||||
// TestEndpointHandlerMustBeANameNotAFunction guards a real failure.
|
||||
//
|
||||
// asString coerces anything non-string with fmt.Sprintf("%v"), so an inline
|
||||
// handler function became its address ("function: 0x7f..."). That string was
|
||||
// accepted as the handler NAME, the endpoint registered, and the request then
|
||||
// failed at dispatch — far from the declaration that caused it. Rejecting the
|
||||
// value at decode time names both the field and the path.
|
||||
func TestEndpointHandlerMustBeANameNotAFunction(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
vm := NewVM(filepath.Join(dir, "adapters"))
|
||||
if err := vm.Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(vm.Stop)
|
||||
ps := NewPlugins(vm, filepath.Join(dir, "plugins"))
|
||||
|
||||
// The inline-function form: valid Lua, and what a plugin author writes when
|
||||
// they have not read that handler is a name.
|
||||
src := `local P = {}
|
||||
P.name = "inline"
|
||||
P.version = "1.0.0"
|
||||
P.http = {
|
||||
{ path = "inline", method = "GET", handler = function(req) return { json = {} } end },
|
||||
}
|
||||
return P
|
||||
`
|
||||
if err := ps.LoadSource("inline", src); err != nil {
|
||||
t.Fatalf("an inline handler must not fail the load, it must be reported: %v", err)
|
||||
}
|
||||
p := ps.find("inline")
|
||||
if p == nil {
|
||||
t.Fatal("plugin not found")
|
||||
}
|
||||
if len(p.http) != 0 {
|
||||
t.Errorf("the inline-function endpoint was accepted: %+v", p.http)
|
||||
}
|
||||
|
||||
// And the documented form still works.
|
||||
src2 := `local P = {}
|
||||
P.name = "byname"
|
||||
P.version = "1.0.0"
|
||||
P.http = {
|
||||
{ path = "ok", method = "GET", roles = { "admin" }, handler = "ok" },
|
||||
}
|
||||
function P.ok(req) return { json = {} } end
|
||||
return P
|
||||
`
|
||||
if err := ps.LoadSource("byname", src2); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
p2 := ps.find("byname")
|
||||
if p2 == nil || len(p2.http) != 1 {
|
||||
t.Fatalf("the documented form regressed: %+v", p2)
|
||||
}
|
||||
if p2.http[0].Handler != "ok" || p2.http[0].Method != "GET" || len(p2.http[0].Roles) != 1 {
|
||||
t.Errorf("endpoint decoded wrong: %+v", p2.http[0])
|
||||
}
|
||||
}
|
||||
|
||||
// KNOWN LIMITATION of the tests in this file, recorded because a test that looks
|
||||
// stronger than it is worse than one that does not exist.
|
||||
//
|
||||
// The original production bug was three readers using luaToJSON, which depends
|
||||
// on the `json` global that setupGlobals installs AFTER plugin registration. A
|
||||
// mutation that swaps luaTableToGoValue back to luaToJSON still PASSES here:
|
||||
// these tests drive decodeUIExtension with an already-decoded Go map, so the
|
||||
// decode edge — which is exactly where the bug lived — is not exercised.
|
||||
//
|
||||
// Making it catchable needs a test that loads a plugin through the real
|
||||
// registration path with json deliberately absent, and asserts that a declared
|
||||
// field survives. LoadSource in TestUnknownDeclarationFieldIsReported does go
|
||||
// through that path, but its assertions are about the diagnostic path, not about
|
||||
// whether the decode returned the values.
|
||||
//
|
||||
// Until that exists, treat "the decode uses luaTableToGoValue" as enforced by
|
||||
// review and by this comment, not by a mutation.
|
||||
|
||||
// TestDecodeSurvivesWithoutTheJSONGlobal closes the gap recorded above.
|
||||
//
|
||||
// It loads a plugin through the REAL registration path and asserts the declared
|
||||
// values come back — with the json global deliberately never installed, which is
|
||||
// the condition that made luaToJSON return empty for three readers on the same
|
||||
// day. Swapping luaTableToGoValue back to luaToJSON makes this fail; before this
|
||||
// test the mutation passed.
|
||||
func TestDecodeSurvivesWithoutTheJSONGlobal(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
vm := NewVM(filepath.Join(dir, "adapters"))
|
||||
if err := vm.Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(vm.Stop)
|
||||
ps := NewPlugins(vm, filepath.Join(dir, "plugins"))
|
||||
|
||||
src := `local P = {}
|
||||
P.name = "nojson"
|
||||
P.version = "1.0.0"
|
||||
P.ui = {
|
||||
visibility = "admin",
|
||||
page = { page_id = "nojson", mount = "<div id='x'></div>" },
|
||||
}
|
||||
P.http = {
|
||||
{ path = "ping", method = "GET", roles = { "admin" }, handler = "ping" },
|
||||
}
|
||||
function P.ping(req) return { json = { ok = true } } end
|
||||
return P
|
||||
`
|
||||
if err := ps.LoadSource("nojson", src); err != nil {
|
||||
t.Fatalf("load: %v", err)
|
||||
}
|
||||
p := ps.find("nojson")
|
||||
if p == nil {
|
||||
t.Fatal("plugin not found after LoadSource")
|
||||
}
|
||||
if p.UI == nil {
|
||||
t.Fatal("ui not read back")
|
||||
}
|
||||
if p.UI.Visibility != "admin" {
|
||||
t.Errorf("UI.Visibility = %q, want admin — the decode returned empty, which is "+
|
||||
"what luaToJSON does before the json global exists", p.UI.Visibility)
|
||||
}
|
||||
if p.UI.Page == nil || p.UI.Page.PageID != "nojson" {
|
||||
t.Errorf("UI.Page not decoded: %+v", p.UI.Page)
|
||||
}
|
||||
if len(p.http) != 1 {
|
||||
t.Fatalf("http endpoints = %d, want 1", len(p.http))
|
||||
}
|
||||
if p.http[0].Handler != "ping" || p.http[0].Method != "GET" {
|
||||
t.Errorf("endpoint decoded wrong: %+v", p.http[0])
|
||||
}
|
||||
if len(p.http[0].Roles) != 1 || p.http[0].Roles[0] != "admin" {
|
||||
t.Errorf("roles not decoded: %+v", p.http[0].Roles)
|
||||
}
|
||||
}
|
||||
@ -43,6 +43,7 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
golua "github.com/aarzilli/golua/lua"
|
||||
@ -80,19 +81,39 @@ var httpClient = &http.Client{
|
||||
const httpGlobal = "__llmsproxy_http"
|
||||
|
||||
// httpAllowlist is the set of hosts plugins may contact: the host of every
|
||||
// configured source's base_url.
|
||||
// configured source's base_url, plus every operator-granted extra host.
|
||||
//
|
||||
// It is a LIVE, shared object, not a snapshot. This matters: bootPluginState
|
||||
// binds one *httpAllowlist into every Lua closure at state-boot time, and those
|
||||
// closures live for the plugin's lifetime. Granting an extra host after a
|
||||
// worker booted must therefore MUTATE the same object the closures hold —
|
||||
// replacing ps.allowHTTP with a fresh instance would leave every already-booted
|
||||
// worker checking the STALE list, and a grant would look accepted (the API
|
||||
// answers ok, the UI shows it) while requests kept getting refused with
|
||||
// "not an allowed outbound destination". That exact failure shipped once.
|
||||
//
|
||||
// Mutations are rare (operator approves a host); lookups are per request.
|
||||
type httpAllowlist struct {
|
||||
mu sync.RWMutex
|
||||
hosts map[string]bool
|
||||
}
|
||||
|
||||
func newHTTPAllowlist(hosts []string) *httpAllowlist {
|
||||
m := make(map[string]bool, len(hosts))
|
||||
a := &httpAllowlist{hosts: make(map[string]bool, len(hosts))}
|
||||
a.add(hosts)
|
||||
return a
|
||||
}
|
||||
|
||||
// add merges hosts into the live allowlist. Called by rebuild (source hosts
|
||||
// change on hot reload) and by GrantExtraHosts (operator approves a host).
|
||||
func (a *httpAllowlist) add(hosts []string) {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
for _, h := range hosts {
|
||||
if h != "" {
|
||||
m[strings.ToLower(h)] = true
|
||||
a.hosts[strings.ToLower(h)] = true
|
||||
}
|
||||
}
|
||||
return &httpAllowlist{hosts: m}
|
||||
}
|
||||
|
||||
// allows reports whether host:port may be contacted. Ports 80/443 are implied
|
||||
@ -103,6 +124,8 @@ func (a *httpAllowlist) allows(host string, port string) bool {
|
||||
if host == "" {
|
||||
return false
|
||||
}
|
||||
a.mu.RLock()
|
||||
defer a.mu.RUnlock()
|
||||
if a.hosts[host] {
|
||||
return true
|
||||
}
|
||||
@ -245,4 +268,4 @@ func flattenHeaders(h http.Header) map[string]string {
|
||||
out[strings.ToLower(k)] = h.Get(k)
|
||||
}
|
||||
return out
|
||||
}
|
||||
}
|
||||
|
||||
@ -119,4 +119,4 @@ func asFloat(v interface{}) (float64, bool) {
|
||||
default:
|
||||
return 0, false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user