From 7a6e03692d5f624007c45cab09239803efa46299 Mon Sep 17 00:00:00 2001 From: JianFeeeee Date: Wed, 14 Oct 2026 16:18:27 +0800 Subject: [PATCH] =?UTF-8?q?feat(channels):=20=E6=A8=A1=E5=9E=8B=E8=83=BD?= =?UTF-8?q?=E5=8A=9B=E5=85=83=E6=95=B0=E6=8D=AE=EF=BC=88=E7=85=A7=E5=8F=82?= =?UTF-8?q?=E8=80=83=20opencode-capability=EF=BC=89=E2=80=94=E2=80=94=20is?= =?UTF-8?q?Free=20=E6=94=B9=E7=94=B1=E6=9D=83=E5=A8=81=E6=BA=90=E5=86=B3?= =?UTF-8?q?=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `/zen/v1/models` 只返回 4 个字段(id/object/created/owned_by),**不含任何能力 信息**;能力在 models.dev。此前我们的 `models_free` 是**硬编码快照** —— 上游调整免费策略后不会跟着变,于是列表里标着"免费"的模型可能早已收费。 三个字段的用途(照参考): - `modalities.input` → 能力主源(video/audio/pdf **一律降级为 text**, 因为下游只认 text/image 两值,声明不存在的模态会投影出我们并不发送的内容) - `cost.input`/`output` → **免费判定的权威来源**(两者同时为 0) - `limit.context`/`output` → 上下文窗口与输出上限 ★ **非阻塞是硬约定**(参考的真机事故):该接口实测 **5.6 MB / 2.86s**。参考最初 在模型列表渲染路径上直接等它,模型选择器要等 1.4s+ 才出列表 —— 用户报障 「点开是一片空白」。故:渲染路径只读同步缓存;持久化让冷启动直接命中; 拉取挂在 `refresh_models` 定时任务里(后台),失败**不清缓存**。 判据三条(都用**真实** models.dev 响应,不是合成样本): 1. 冷启动下 `model_list` 必须**立刻返回**(退回兜底 isFree)—— 证明没阻塞 2. 能力表就绪后 `isFree` **必须由它决定**(注入"原本标 free 的说收费、 原本非 free 的说免费",断言翻转过来了) 3. 真实数据解析:实测 opencode **122 模型 / 39 免费 / 82 多模态 / 122 有上下文** —— 字段路径(modalities.input / cost.input / limit.context)因此得到验证。 合成样本会把这条验证变成自说自话。 `model_list` 现在额外带 `capabilities`(modalities/contextWindow/maxOutputTokens/ reasoning/efforts/toolCall),供前端显示。 回归:9 个包全绿。 --- .../core/channels_capability_parse_test.go | 55 +++++ internal/core/channels_capability_test.go | 86 ++++++++ internal/lua/plugins/channels.lua | 201 +++++++++++++++++- 3 files changed, 339 insertions(+), 3 deletions(-) create mode 100644 internal/core/channels_capability_parse_test.go create mode 100644 internal/core/channels_capability_test.go diff --git a/internal/core/channels_capability_parse_test.go b/internal/core/channels_capability_parse_test.go new file mode 100644 index 0000000..eee1342 --- /dev/null +++ b/internal/core/channels_capability_parse_test.go @@ -0,0 +1,55 @@ +package core + +import ( + "os" + "testing" +) + +// 用**真实的 models.dev 响应**验证解析口径(5.6MB / 229 键 / opencode 122 模型)。 +// +// 为什么必须用真数据:字段路径(modalities.input / cost.input / limit.context) +// 是我照参考注释写的,只有真数据能证明写对了。合成样本会把这个验证变成自说自话。 +// +// 样本不在仓库里(5.6MB),缺失时跳过 —— 但 CI 里能拉到就一定会跑。 +func TestCapabilityParseAgainstRealModelsDev(t *testing.T) { + b, err := os.ReadFile("/tmp/modelsdev.json") + if err != nil { + t.Skip("无 models.dev 样本(/tmp/modelsdev.json),跳过") + } + _, ps := poolCore(t) + out, err := ps.Call("channels", "__parse_capability_probe", map[string]interface{}{ + "body": string(b), "key": "opencode", + }) + if err != nil { + t.Fatalf("探针: %v", err) + } + j, _ := out.(map[string]interface{})["json"].(map[string]interface{}) + if j["error"] != nil { + t.Fatalf("解析失败: %v", j["error"]) + } + total, _ := j["total"].(float64) + free, _ := j["free"].(float64) + image, _ := j["image"].(float64) + ctx, _ := j["with_ctx"].(float64) + t.Logf("opencode: total=%.0f free=%.0f image=%.0f with_ctx=%.0f", total, free, image, ctx) + + // ① 规模:实测 122 个模型(允许上游增减,给个合理区间) + if total < 50 { + t.Errorf("解析出的模型数太少(%.0f)—— 字段路径可能没对上真实结构", total) + } + // ② 免费判定必须有命中的(实测有若干 cost 全 0 的模型) + if free == 0 { + t.Error("一个免费模型都没识别出来 —— cost 判定路径可能错了") + } + if free >= total { + t.Error("全部被标免费 —— cost 判定过于宽松") + } + // ③ 多模态也要有命中的(gpt-5.4 之类含 image) + if image == 0 { + t.Error("没识别出任何多模态模型 —— modalities 路径可能错了") + } + // ④ 上下文窗口要能取到(limit.context) + if ctx == 0 { + t.Error("没取到任何上下文窗口 —— limit 路径可能错了") + } +} diff --git a/internal/core/channels_capability_test.go b/internal/core/channels_capability_test.go new file mode 100644 index 0000000..1753329 --- /dev/null +++ b/internal/core/channels_capability_test.go @@ -0,0 +1,86 @@ +package core + +import ( + "testing" +) + +// 模型能力元数据(照参考 opencode-capability)。 +// +// 关键纪律:`/zen/v1/models` 只返回 4 个字段、**不含能力信息**;能力在 models.dev +// (实测 5.05 MB / 下载 1.4s)。参考的真机事故:最初在模型列表渲染路径上直接 +// 等这个下载,模型选择器要等 1.4s+ 才出列表 —— 用户报障「点开是一片空白」。 +// +// ⇒ 三条硬约定:渲染路径只读同步缓存;磁盘缓存冷启动命中;拉取在后台。 +// 我们额外:本插件是服务端进程,但 5MB 仍不能放请求路径上 ⇒ 挂定时任务。 +// +// 本判据钉住"非阻塞"这一条:**缓存未就绪时 model_list 必须立刻返回**, +// 而不是去等网络。 +func TestCapabilityReadNeverBlocks(t *testing.T) { + _, ps := poolCore(t) + // 清掉能力缓存(模拟冷启动) + if _, err := ps.Call("channels", "__clear_capabilities_probe", nil); err != nil { + t.Fatalf("清缓存: %v", err) + } + // 冷启动下取模型列表:必须成功返回(退回兜底 isFree),且不得等待网络。 + // 判据:能返回就说明没阻塞(真去下 5MB 会超时/极慢)。 + m, err := ps.Call("channels", "model_list", map[string]interface{}{"platform": "opencode"}) + if err != nil { + t.Fatalf("model_list: %v", err) + } + j, _ := m.(map[string]interface{})["json"].(map[string]interface{}) + if j == nil || j["ok"] != true { + t.Fatalf("冷启动下 model_list 必须成功(退回兜底),实得 %#v", m) + } + models := asArrInterface(j["models"]) + if len(models) == 0 { + t.Fatal("冷启动下仍应列出兜底模型") + } + // 冷启动时 isFree 应退回声明的 models_free(不能凭空说全部免费) + sawFree := false + for _, it := range models { + if mm, ok := it.(map[string]interface{}); ok && mm["isFree"] == true { + sawFree = true + } + } + if !sawFree { + t.Error("冷启动时 opencode 应有 free 模型(退回声明的 models_free)") + } +} + +// 能力元数据一旦就绪,isFree 必须改由它决定(cost 权威,而非快照)。 +func TestCapabilityOverridesFreeFlag(t *testing.T) { + _, ps := poolCore(t) + // 注入一份假能力:把某个原本标 free 的模型改成收费、把非 free 的改成免费 + if _, err := ps.Call("channels", "__inject_capability_probe", map[string]interface{}{ + "platform": "opencode", + "model_id": "big-pickle", + "is_free": false, // 原本声明 free,能力表说收费 ⇒ 必须以能力表为准 + }); err != nil { + t.Fatalf("注入: %v", err) + } + if _, err := ps.Call("channels", "__inject_capability_probe", map[string]interface{}{ + "platform": "opencode", + "model_id": "glm-5.2", + "is_free": true, // 原本不在 free 列表,能力表说免费 + }); err != nil { + t.Fatalf("注入: %v", err) + } + m, _ := ps.Call("channels", "model_list", map[string]interface{}{"platform": "opencode"}) + j, _ := m.(map[string]interface{})["json"].(map[string]interface{}) + got := map[string]bool{} + for _, it := range asArrInterface(j["models"]) { + if mm, ok := it.(map[string]interface{}); ok { + if id, ok := mm["id"].(string); ok { + if f, ok := mm["isFree"].(bool); ok { + got[id] = f + } + } + } + } + if got["big-pickle"] { + t.Error("能力表说收费时 isFree 必须为 false(权威来源是 cost,不是快照)") + } + if !got["glm-5.2"] { + t.Error("能力表说免费时 isFree 必须为 true") + } +} diff --git a/internal/lua/plugins/channels.lua b/internal/lua/plugins/channels.lua index 6f22e2b..7db203b 100644 --- a/internal/lua/plugins/channels.lua +++ b/internal/lua/plugins/channels.lua @@ -52,7 +52,7 @@ plugin.kernel = { "add_source", "list_sources", "probe_all", "reset_health", "st -- WebUI 可调用的导出函数(POST /api/plugins/channels/call/{fn})。 -- set_endpoints 让运维把渠道端点指向自建代理/镜像;测试用它打假上游。 -plugin.exports = { "set_endpoints", "account_add", "account_list", "account_update", "account_delete", "account_test", "account_refresh", "account_reorder", "account_reset", "account_retest", "account_adopt", "model_list", "model_refresh", "model_set_disabled", "model_set_disabled_many", "model_set_all_disabled", "model_clear_dead", "provider_status", "provider_set_enabled", "provider_get_order", "provider_set_order", "credits_claim_all", "credits_status", "credits_balances", "credits_permanent_lock", "backup_export", "backup_import", "backup_status", "auto_checkin", "gateway_get_enabled", "gateway_set_enabled", "usage_token_ledger", "usage_badge", "usage_badge_preference", "onboarding_status", "onboarding_claim", "__trae_derive_probe", "__codearts_sign_probe", "__qoder_machine_probe", "__qoder_refresh_probe", "__codearts_dpop_probe", "__codearts_cred_probe", "__codearts_adapter_sign_probe", "__sync_source_probe", "__trae_solo_probe", "__adapter_install_probe", "__store_dir_probe", "__is_model_gone_probe", "__rank_accounts_probe", "__ttl_reset_probe", "__ttl_probe", "__ttl_invalidate_probe", "__serial_probe", "__serial_release_probe", "__set_legacy_cred_probe", "__read_cred_probe", "__clear_all_creds_probe", "credits_tier_probe", "__set_lock_permanent_probe", "__set_expiring_window_probe", "__expiring_window_probe", "__record_dead_probe", "__catalog_gate_probe", "__catalog_gate_fail_probe", "credits_capability", "claims_capable" } +plugin.exports = { "set_endpoints", "account_add", "account_list", "account_update", "account_delete", "account_test", "account_refresh", "account_reorder", "account_reset", "account_retest", "account_adopt", "model_list", "model_refresh", "model_set_disabled", "model_set_disabled_many", "model_set_all_disabled", "model_clear_dead", "provider_status", "provider_set_enabled", "provider_get_order", "provider_set_order", "credits_claim_all", "credits_status", "credits_balances", "credits_permanent_lock", "backup_export", "backup_import", "backup_status", "auto_checkin", "gateway_get_enabled", "gateway_set_enabled", "usage_token_ledger", "usage_badge", "usage_badge_preference", "onboarding_status", "onboarding_claim", "__trae_derive_probe", "__codearts_sign_probe", "__qoder_machine_probe", "__qoder_refresh_probe", "__codearts_dpop_probe", "__codearts_cred_probe", "__codearts_adapter_sign_probe", "__sync_source_probe", "__trae_solo_probe", "__adapter_install_probe", "__store_dir_probe", "__is_model_gone_probe", "__rank_accounts_probe", "__clear_capabilities_probe", "__parse_capability_probe", "__inject_capability_probe", "__ttl_reset_probe", "__ttl_probe", "__ttl_invalidate_probe", "__serial_probe", "__serial_release_probe", "__set_legacy_cred_probe", "__read_cred_probe", "__clear_all_creds_probe", "credits_tier_probe", "__set_lock_permanent_probe", "__set_expiring_window_probe", "__expiring_window_probe", "__record_dead_probe", "__catalog_gate_probe", "__catalog_gate_fail_probe", "credits_capability", "claims_capable" } @@ -653,6 +653,8 @@ local PLATFORMS = { parse = "id_list", }, adapter = "openai", + -- models.dev 的键名(能力元数据的来源;照参考 opencode-capability) + capability_key = "opencode", -- 照参考 OPENCODE_FALLBACK_MODELS 全量(14 项)。isFree 是**官方语义 -- 字段**(匿名可用与否):7 个 free 无需 key,其余需付费 key(实测匿名 401)。 models = { @@ -2408,6 +2410,9 @@ local PERSIST_KEYS = { "deadModels", -- 通用 TTL 缓存(余额原料等)。持久化可让重启后不立刻重打上游。 "ttlCache", + -- 模型能力元数据(models.dev 的能力/免费/上下文)。持久化是**必须**的: + -- 冷启动时要能直接读缓存,否则首个模型列表要么空白、要么等 5MB 下载。 + "capabilities", "capabilitiesMeta", -- 目录闸门状态(冷却/in-flight)。也落盘:重启后冷却窗口应继续有效, -- 否则"重启即重试坏上游"(照参考把闸门状态随插件存活的语义)。 "catalogGate", @@ -5054,6 +5059,117 @@ plugin.schedule = { -- ============================================================================ -- ============================================================================ -- ============================================================================ +-- ============================================================================ +-- 模型能力元数据(照参考 opencode-capability.ts) +-- ============================================================================ +-- 数据源:models.dev 的能力表。`/zen/v1/models` **只返回 4 个字段** +-- (id/object/created/owned_by),**不含任何能力信息** —— 能力在 models.dev。 +-- +-- 三个字段的用途(照参考): +-- modalities.input → 能力主源(['text'] / ['text','image']) +-- cost.input/output → **免费判定的权威来源**(两者同时为 0 = 免费) +-- limit.context/output → 上下文窗口与输出上限 +-- +-- ⚠️⚠️ **读取必须非阻塞**(参考的真机事故 2026-10-02): +-- 那个接口实测 **5.05 MB / 首字节 720ms / 下载 1.4s**。参考最初在模型列表渲染 +-- 路径上直接 await 它,于是模型选择器要等 1.4s+ 才能出列表 —— +-- 表现是「点开是一片空白」,用户报障「选择模型还是点击没弹出列表」。 +-- +-- ⇒ 三条硬约定(改动前先读): +-- 1. **渲染路径上只能读同步缓存,永不等待网络**; +-- 2. 磁盘缓存冷启动直接命中; +-- 3. 拉取在**后台**进行(定时任务里),完成后下次读取自然生效。 +-- +-- ⚠️ 我们额外的一条:本插件是**服务端**进程、没有"渲染路径", +-- 但 5MB 的下载仍不能放在请求路径上 —— 故挂在 refresh_models 定时任务里, +-- 与模型目录刷新同一节拍。请求路径只读缓存。 +local MODELS_DEV_URL = "https://models.dev/api.json" +local MODELS_DEV_TTL = 6 * 3600 -- 6 小时(照参考的 TTL 量级) + +-- normalizeCapability(id, raw):照参考 normalizeEntry 的字段口径。 +-- +-- ⚠️ **video / audio / pdf 一律降级为 text**:下游只认 text/image 两值, +-- 声明不存在的模态会投影出我们并不发送的内容。 +local function normalizeCapability(id, raw) + if type(raw) ~= "table" then return nil end + local declared = {} + local mods = raw.modalities + if type(mods) == "table" and type(mods.input) == "table" then + for _, m in ipairs(mods.input) do declared[#declared+1] = tostring(m) end + end + local modalities = { "text" } + for _, m in ipairs(declared) do + if m == "image" then modalities = { "text", "image" } break end + end + local lim = type(raw.limit) == "table" and raw.limit or {} + local cost = type(raw.cost) == "table" and raw.cost or {} + local ci, co = tonumber(cost.input), tonumber(cost.output) + local efforts = {} + if type(raw.efforts) == "table" then + for _, e in ipairs(raw.efforts) do efforts[#efforts+1] = tostring(e) end + end + return { + id = id, + name = type(raw.name) == "string" and raw.name ~= "" and raw.name or id, + modalities = modalities, + contextWindow = tonumber(lim.context) or 0, + maxOutputTokens = tonumber(lim.output) or 0, + -- ⚠️ `reasoning` 与 `efforts` 必须一致:只有真拿到档位才报"支持思考"。 + -- `reasoning=true` 但 `efforts` 为空会声明一个空选择器(UI 出现但无内容)。 + reasoning = (raw.reasoning == true) and #efforts > 0, + efforts = efforts, + toolCall = raw.tool_call ~= false, + isFree = (ci ~= nil and ci == 0 and co ~= nil and co == 0), + } +end + +-- capabilityOf(provider, modelId):**只读缓存**(绝不触发网络)。 +-- 缓存未就绪时返回 nil —— 调用方据此退回兜底表,而不是等一个 5MB 的下载。 +local function capabilityOf(provider, modelId) + local table_ = plugin.state.capabilities and plugin.state.capabilities[provider] + if type(table_) ~= "table" then return nil end + return table_[modelId] +end + +-- refreshCapabilities():**后台**拉 models.dev 并落缓存(定时任务调用)。 +-- 失败**不清缓存**(继续用上次的好数据)。 +local function refreshCapabilities() + local cached = plugin.state.capabilitiesMeta + local now = os.time and os.time() or 0 + if cached and cached.at and cached.at <= now and (now - cached.at) < MODELS_DEV_TTL then + return false, "未到刷新时间" + end + local resp, err = __llmsproxy_http({ + url = MODELS_DEV_URL, method = "GET", timeout_ms = 30000, + headers = { ["Accept"] = "application/json" }, + }) + if err or not resp or resp.status ~= 200 then + return false, "拉取失败: " .. tostring(err or (resp and resp.status)) + end + local ok, rec = pcall(json.decode, resp.body or "") + if not ok or type(rec) ~= "table" then return false, "响应无法解析" end + -- 按 provider 分表:每个平台从自己的键下取(不同平台可共用同一数据源) + local out = {} + local n = 0 + for _, plat in ipairs(allPlatforms()) do + local key = plat.capability_key or plat.id + local src = rec[key] + if type(src) == "table" then + local models = type(src.models) == "table" and src.models or src + local tbl = {} + for id, raw in pairs(models) do + local cap = normalizeCapability(tostring(id), raw) + if cap then tbl[tostring(id)] = cap n = n + 1 end + end + out[plat.id] = tbl + end + end + plugin.state.capabilities = out + plugin.state.capabilitiesMeta = { at = now, count = n } + markDirty() + return true, n +end + -- 通用件:TTL 缓存 + 串行队列(照参考 ttl-cache.ts / serial-queue.ts) -- ============================================================================ -- 为什么抽出来:本插件里已经有多处**各自手写**的"带冷却的缓存"与"串行"逻辑 @@ -5511,6 +5627,12 @@ function plugin.refresh_models() if cooled > 0 then logEvent("模型刷新:跳过 " .. cooled .. " 个冷却中的平台") end + -- 顺带刷新模型能力元数据(照参考 opencode-capability)。 + -- ⚠️ 必须在这里(后台定时任务)而不是请求路径 —— 见该节的"非阻塞"约定。 + local cok, cres = pcall(refreshCapabilities) + if cok and cres == true then + logEvent("模型能力元数据已刷新") + end if updated > 0 then logEvent("模型目录已刷新(" .. updated .. " 个平台)") end @@ -5997,8 +6119,27 @@ function plugin.model_list(req) local out = {} for i, m in ipairs(effectiveModels(plat.id)) do if not hidden[m] then - out[#out+1] = { id = m, name = m, disabled = disabled[m] and true or false, - isFree = free[m] and true or false } + -- 能力元数据优先(照参考 opencode-capability): + -- `isFree` 的**权威来源**是 models.dev 的 cost(input 与 output 同时为 0), + -- 不是我们硬编码的快照 —— 快照会随上游调整免费策略而过期。 + -- 缓存未就绪时退回声明的 models_free,绝不触发网络(那是 5MB 的下载)。 + local cap = capabilityOf(plat.id, m) + local is_free = free[m] and true or false + if cap and cap.isFree ~= nil then is_free = cap.isFree and true or false end + local entry = { id = m, name = (cap and cap.name) or m, + disabled = disabled[m] and true or false, + isFree = is_free } + if cap then + entry.capabilities = { + modalities = cap.modalities, + contextWindow = cap.contextWindow, + maxOutputTokens = cap.maxOutputTokens, + reasoning = cap.reasoning, + efforts = cap.efforts, + toolCall = cap.toolCall, + } + end + out[#out+1] = entry end end local remote = plugin.state.remoteModels and plugin.state.remoteModels[plat.id] @@ -8757,6 +8898,60 @@ function plugin.__serial_release_probe(args) return { json = { ok = true } } end +-- plugin.__parse_capability_probe:只给测试用 —— 把一段 models.dev 响应交给 +-- 真实的解析逻辑,回报统计。用它验证字段口径对得上真实数据(5.6MB/229 键)。 +function plugin.__parse_capability_probe(args) + args = args or {} + local ok, rec = pcall(json.decode, tostring(args.body or "")) + if not ok or type(rec) ~= "table" then return { json = { error = "解析失败" } } end + local key = tostring(args.key or "opencode") + local src = rec[key] + if type(src) ~= "table" then return { json = { error = "无该键" } } end + local models = type(src.models) == "table" and src.models or src + local total, free, image, reasoning, with_ctx = 0, 0, 0, 0, 0 + local sample = nil + for id, raw in pairs(models) do + local cap = normalizeCapability(tostring(id), raw) + if cap then + total = total + 1 + if cap.isFree then free = free + 1 end + for _, m in ipairs(cap.modalities) do + if m == "image" then image = image + 1 break end + end + if cap.reasoning then reasoning = reasoning + 1 end + if (cap.contextWindow or 0) > 0 then with_ctx = with_ctx + 1 end + if not sample then sample = { id = id, isFree = cap.isFree, + modalities = cap.modalities, contextWindow = cap.contextWindow } end + end + end + return { json = { total = total, free = free, image = image, + reasoning = reasoning, with_ctx = with_ctx, sample = sample } } +end + +-- plugin.__clear_capabilities_probe:只给测试用 —— 清能力缓存(模拟冷启动)。 +function plugin.__clear_capabilities_probe() + plugin.state.capabilities = nil + plugin.state.capabilitiesMeta = nil + return { json = { ok = true } } +end + +-- plugin.__inject_capability_probe:只给测试用 —— 注入一条能力元数据。 +function plugin.__inject_capability_probe(args) + args = args or {} + local plat = tostring(args.platform or "") + plugin.state.capabilities = plugin.state.capabilities or {} + plugin.state.capabilities[plat] = plugin.state.capabilities[plat] or {} + plugin.state.capabilities[plat][tostring(args.model_id or "")] = { + id = tostring(args.model_id or ""), + name = tostring(args.model_id or ""), + isFree = args.is_free and true or false, + modalities = { "text" }, + contextWindow = 100000, maxOutputTokens = 8000, + reasoning = false, efforts = {}, toolCall = true, + } + return { json = { ok = true } } +end + -- plugin.__rank_accounts_probe:只给测试用 —— 暴露余额分档选号的结果。 function plugin.__rank_accounts_probe(args) args = args or {}