feat(channels): 模型能力元数据(照参考 opencode-capability)—— isFree 改由权威源决定

`/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 个包全绿。
This commit is contained in:
JianFeeeee
2026-10-14 16:18:27 +08:00
parent 420379784b
commit 7a6e03692d
3 changed files with 339 additions and 3 deletions

View File

@ -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 {}