mirror of
https://gitcode.com/JianFeeeee/ModelRouter.git
synced 2026-11-07 09:47:23 +00:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ed954b77bd |
@ -5,6 +5,10 @@ import (
|
||||
"testing"
|
||||
|
||||
"llmsproxy/internal/lua"
|
||||
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
)
|
||||
|
||||
// channels_pool_test:对齐参考 account.* / model.* / provider.* 契约的行为测试。
|
||||
@ -320,3 +324,47 @@ func TestChannelsExtendedRPCBehavior(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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