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 {