From 3c9c633e5de2bb048a76f0a3cd783b8826c90b2e Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 5 Oct 2026 23:47:05 +0800 Subject: [PATCH] provider, gui: a sub2api panel's /v1/usage is read with its field left empty, so its balance needs only the key (Singrun, #881) A sub2api panel answers GET /v1/usage, asked with the API key alone, with what is left on the key in dollars as "remaining": the wallet's balance, the key's own quota or its plan's (Wei-Shaw/sub2api, GatewayHandler.Usage, which its own CC Switch import reads the same way). Its profile query (/api/v1/user/profile) wants the login JWT, which expires; this one keeps working with the key the provider already has. balancePathOf now reads $remaining for a Balance URL ending /v1/usage when the field is left empty, and the Balance field's placeholder says so. TestKnownBalanceFieldLeftEmpty gains the case (sub2api's wallet reply, the key as Bearer) and fails without the change ("no balance path"). balance-field-default.test.cjs checks the placeholder ($remaining) and fails without app.js's line, on Chromium and WebKit. --- internal/gui/assets/app.js | 1 + internal/gui/tests/balance-field-default.test.cjs | 4 +++- internal/provider/balance.go | 8 ++++++++ internal/provider/balance_test.go | 12 +++++++++++- 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index 441a29b38..4ec26741b 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -10340,6 +10340,7 @@ function balanceFieldOf(raw) { if (path === "/api/user/self") return "$data.quota / 500000"; if (path === "/api/usage/token") return "$data.total_available / 500000"; if (path === "/api/v1/user/profile") return "$data.balance"; + if (path === "/v1/usage") return "$remaining"; if (path.endsWith("/dashboard/billing/credit_grants")) return "$total_available"; return "data.balance"; } diff --git a/internal/gui/tests/balance-field-default.test.cjs b/internal/gui/tests/balance-field-default.test.cjs index 5b7578f59..ef7fabd8e 100644 --- a/internal/gui/tests/balance-field-default.test.cjs +++ b/internal/gui/tests/balance-field-default.test.cjs @@ -2,7 +2,8 @@ // A custom provider's Balance field left empty shows, as its placeholder, // the field magpie reads from a Balance URL whose reply it knows (#881): // new-api's /api/usage/token and /api/user/self, a sub2api panel's -// /api/v1/user/profile, OpenAI's old credit_grants; any other URL keeps +// /api/v1/user/profile and its /v1/usage for a key, OpenAI's old +// credit_grants; any other URL keeps // the example. The placeholder follows the URL as it is typed. const assert = require("node:assert/strict"); const fs = require("node:fs/promises"); @@ -59,6 +60,7 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", for (const [typed, want] of [ ["https://relay.example.com/api/user/self/", "$data.quota / 500000"], ["https://panel.example.com/api/v1/user/profile", "$data.balance"], + ["https://panel.example.com/v1/usage", "$remaining"], ["https://relay.example.com/v1/dashboard/billing/credit_grants", "$total_available"], ["https://relay.example.com/whatever", "data.balance"], ["not a url", "data.balance"], diff --git a/internal/provider/balance.go b/internal/provider/balance.go index 7633d449d..30ce85c4b 100644 --- a/internal/provider/balance.go +++ b/internal/provider/balance.go @@ -93,6 +93,11 @@ func balancePathOf(p Provider) string { case path == sub2APIProfile: // a sub2api panel's profile, asked with its login JWT: dollars return "$data.balance" + case path == sub2APIKeyUsage: + // a sub2api panel's query for a key, asked with the key alone: + // what is left, in dollars, whether the wallet's, the key's own + // quota or its plan's + return "$remaining" case strings.HasSuffix(path, creditGrants): // OpenAI's old credit query, which relays still answer: dollars return "$total_available" @@ -114,6 +119,9 @@ const ( creditGrants = "/dashboard/billing/credit_grants" // sub2APIProfile is a sub2api panel's account, told to its login JWT sub2APIProfile = "/api/v1/user/profile" + // sub2APIKeyUsage is a sub2api panel's usage query for a key, which + // tells what is left on it (remaining, USD) without a login + sub2APIKeyUsage = "/v1/usage" ) // balanceURLPath is a balance URL's path, without a slash at its end. diff --git a/internal/provider/balance_test.go b/internal/provider/balance_test.go index 8265ae5eb..a1f80004d 100644 --- a/internal/provider/balance_test.go +++ b/internal/provider/balance_test.go @@ -370,7 +370,8 @@ func TestBalanceURLNamesTheKey(t *testing.T) { // A known balance query read with its field left empty (#881): new-api's // for a key and for the account, OpenAI's old credit grants, a sub2api -// panel's profile; a query magpie doesn't know still asks for the field. +// panel's profile and its query for a key; a query magpie doesn't know +// still asks for the field. func TestKnownBalanceFieldLeftEmpty(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { switch r.URL.Path { @@ -382,6 +383,14 @@ func TestKnownBalanceFieldLeftEmpty(t *testing.T) { w.Write([]byte(`{"object":"credit_summary","total_granted":20,"total_used":7.5,"total_available":12.5}`)) case "/api/v1/user/profile": w.Write([]byte(`{"code":0,"data":{"balance":4.2}}`)) + case "/v1/usage": + // sub2api's reply for a key on the wallet, its key alone asked + if r.Header.Get("Authorization") != "Bearer sk-one" { + w.WriteHeader(http.StatusUnauthorized) + w.Write([]byte(`{"error":{"type":"authentication_error","message":"Invalid API key"}}`)) + return + } + w.Write([]byte(`{"mode":"unrestricted","isValid":true,"planName":"钱包余额","remaining":7.25,"unit":"USD","balance":7.25}`)) default: w.Write([]byte(`{"left":1}`)) } @@ -394,6 +403,7 @@ func TestKnownBalanceFieldLeftEmpty(t *testing.T) { {"/v1/dashboard/billing/credit_grants", "", "$12.50"}, {"/dashboard/billing/credit_grants", "", "$12.50"}, {"/api/v1/user/profile", "eyJ.a.b", "$4.20"}, + {"/v1/usage", "", "$7.25"}, } { p := Provider{ID: "relay", Chat: srv.URL + "/v1", Key: "sk-one", BalanceURL: srv.URL + c.path, BalanceToken: c.token} if got, ok, err := Balance(context.Background(), p); err != nil || !ok || got != c.want {