mirror of
https://gitcode.com/JianFeeeee/ModelRouter.git
synced 2026-11-07 09:47:23 +00:00
fix(channels): buddy 计费请求缺身份头 → 上游 401(逐行移植参考 checkinHeaders)
修好凭据读取后,余额端点仍报 HTTP 401。逐行核对参考 checkinHeaders (credits.ts) 后发现我们只发了 Authorization,而 buddy 要求身份标识头齐全: X-Domain: copilot.tencent.com (以产品配置为准,不用凭据里的 domain 快照) X-Product: SaaS (部署类型) X-Product-Code: codebuddy (产品码) User-Agent: CodeBuddyIDE/1.106.1 X-User-Id / X-Enterprise-Id / X-Tenant-Id(凭据携带时) 参考的注释特别警告 X-Domain 的方向:凭据里的 domain 是「登录时那个域名」的 快照,跨产品迁移后会指错区域,所以必须以产品端点为准。 ## 实现 - 平台级声明 headers_fn(accessToken, cred) → 返回请求头表;buddy 据此逐行移植。 - fetchAccountJSON 增加头钩子位:spec 自己的 headers_fn 优先,否则回退平台级。 未声明的平台仍走通用默认头(Bearer + JSON),不改变既有行为。 - set_endpoints 补齐 checkin_status / balance 端点覆盖(此前只覆盖旧的 plat.checkin,buddy 这类平台无从改指,自动化测试也指不到假上游)。 端点覆盖是参考明确的产品配置能力,也是测试缝。 ## 判据 TestBuddyBillingHeadersMatchReference:把 buddy 余额端点指到本地假上游, 断言四个必备身份头逐字段正确。变异:改名 headers_fn → 立即 FAIL(已实测)。 go test ./internal/core/ 与 ./internal/lua/ 全绿。
This commit is contained in:
@ -287,6 +287,30 @@ local PLATFORMS = {
|
||||
hosts = { "copilot.tencent.com", "www.codebuddy.cn" },
|
||||
base_url = "https://copilot.tencent.com",
|
||||
adapter = "openai",
|
||||
-- 计费/状态请求头:逐行移植参考 checkinHeaders(credits.ts)。
|
||||
-- 实测只发 Authorization 会被上游 401:buddy 要求身份标识头齐全。
|
||||
-- X-Domain 以产品配置为准(不用凭据里的 domain 快照,那是登录时的区域,
|
||||
-- 跨产品迁移后会指错区);X-Product 是部署类型(SaaS);
|
||||
-- X-Product-Code 是产品码(codebuddy);X-User-Id 取凭据的 user_id。
|
||||
headers_fn = function(accessToken, cred)
|
||||
local h = {
|
||||
["Authorization"] = "Bearer " .. accessToken,
|
||||
["Accept"] = "application/json",
|
||||
["Content-Type"] = "application/json",
|
||||
["X-Domain"] = "copilot.tencent.com",
|
||||
["X-Product"] = "SaaS",
|
||||
["X-Product-Code"] = "codebuddy",
|
||||
["User-Agent"] = "CodeBuddyIDE/1.106.1",
|
||||
}
|
||||
local uid = cred and (cred.user_id or cred.uid)
|
||||
if type(uid) == "string" and uid ~= "" then h["X-User-Id"] = uid end
|
||||
local eid = cred and cred.enterprise_id
|
||||
if type(eid) == "string" and eid ~= "" then
|
||||
h["X-Enterprise-Id"] = eid
|
||||
h["X-Tenant-Id"] = eid
|
||||
end
|
||||
return h
|
||||
end,
|
||||
models = {
|
||||
"deepseek-v4-flash", "deepseek-v4-pro", "hy4-preview", "hy4-preview-x",
|
||||
"glm-5.3", "glm-5.3-flash", "glm-5.2",
|
||||
@ -2027,8 +2051,18 @@ end
|
||||
-- 签到状态 + 逐账号余额(对齐参考 credits.status / credits.balances)
|
||||
-- ============================================================================
|
||||
-- fetchAccountJSON 用某账号凭据发一次请求,返回 (rec_table, nil) 或 (nil, err)。
|
||||
local function fetchAccountJSON(spec, tok)
|
||||
local headers = spec.headers or { ["Authorization"] = "Bearer " .. tok, ["Content-Type"] = "application/json" }
|
||||
local function fetchAccountJSON(spec, tok, credTable, plat)
|
||||
local headers
|
||||
-- 头钩子位:spec(checkin_status / balance)自己的优先,否则用平台级的。
|
||||
-- buddy 在平台层声明一次,签到与余额复用同一套身份头。
|
||||
local hfn = spec.headers_fn or (plat and plat.headers_fn)
|
||||
if hfn then
|
||||
local okf, h = pcall(hfn, tok, credTable or {})
|
||||
if okf and type(h) == "table" then headers = h end
|
||||
end
|
||||
if not headers then
|
||||
headers = spec.headers or { ["Authorization"] = "Bearer " .. tok, ["Content-Type"] = "application/json" }
|
||||
end
|
||||
local resp, err = __llmsproxy_http({
|
||||
url = spec.url, method = spec.method or "GET",
|
||||
headers = headers, body = spec.body,
|
||||
@ -2054,7 +2088,7 @@ function plugin.credits_status(req)
|
||||
if cs then
|
||||
local tok = credentialOf(a.credentialRef)
|
||||
if tok then
|
||||
local rec, err = fetchAccountJSON(cs, tok)
|
||||
local rec, err = fetchAccountJSON(cs, tok, credentialFull(a.credentialRef), plat)
|
||||
if rec and plat.checkin_status.status_fn then
|
||||
local okf, st = pcall(plat.checkin_status.status_fn, rec)
|
||||
if okf then status = st end
|
||||
@ -2088,7 +2122,7 @@ function plugin.credits_balances(req)
|
||||
if not tok then
|
||||
entry.error = "无凭据"
|
||||
else
|
||||
local rec, err = fetchAccountJSON(b, tok)
|
||||
local rec, err = fetchAccountJSON(b, tok, credentialFull(a.credentialRef), plat)
|
||||
if err then
|
||||
entry.error = err
|
||||
elseif b.total_fn then
|
||||
@ -2399,6 +2433,26 @@ function plugin.set_endpoints(req)
|
||||
if args.checkin[k] then plat.checkin[k] = tostring(args.checkin[k]) changed[#changed+1] = "checkin." .. k end
|
||||
end
|
||||
end
|
||||
-- checkin_status / balance:声明式端点覆盖。参考把各平台的计费/状态端点当
|
||||
-- 产品配置,运维要能整体改指(自建代理、镜像、测试假上游)。此前只覆盖了
|
||||
-- 旧的 plat.checkin,buddy 这类"声明 checkin_status/balance"的平台无从覆盖 ——
|
||||
-- 自动化测试也就无法把它们的端点指到假上游。
|
||||
if type(args.checkin_status) == "table" and plat.checkin_status then
|
||||
for _, k in ipairs({ "url", "method", "body" }) do
|
||||
if args.checkin_status[k] then
|
||||
plat.checkin_status[k] = tostring(args.checkin_status[k])
|
||||
changed[#changed+1] = "checkin_status." .. k
|
||||
end
|
||||
end
|
||||
end
|
||||
if type(args.balance) == "table" and plat.balance then
|
||||
for _, k in ipairs({ "url", "method", "body" }) do
|
||||
if args.balance[k] then
|
||||
plat.balance[k] = tostring(args.balance[k])
|
||||
changed[#changed+1] = "balance." .. k
|
||||
end
|
||||
end
|
||||
end
|
||||
if type(args.callback) == "table" and plat.callback then
|
||||
for _, k in ipairs({ "exchange_url" }) do
|
||||
if args.callback[k] then plat.callback[k] = tostring(args.callback[k]) changed[#changed+1] = "callback." .. k end
|
||||
|
||||
Reference in New Issue
Block a user