diff --git a/internal/provider/balance.go b/internal/provider/balance.go index 96cd899a8..64889604e 100644 --- a/internal/provider/balance.go +++ b/internal/provider/balance.go @@ -1,7 +1,7 @@ package provider // What is left on an API key, as the vendor's own balance endpoint tells -// it: DeepSeek, Kimi, OpenRouter, SiliconFlow, Command Code and AiHubMix are known by their hosts +// it: DeepSeek, Kimi, OpenRouter, SiliconFlow, StepFun, Command Code and AiHubMix are known by their hosts // (AiHubMix tells the whole account's to its access token, BalanceToken); // any other provider can name an endpoint and where the amount sits in its // reply (BalanceURL, BalancePath), the way a relay's own usage query does. @@ -55,6 +55,10 @@ func balanceSourceOf(p Provider) (balanceSource, bool) { return balanceSource{"https://api.siliconflow.cn/v1/user/info", readSiliconFlow("¥"), ""}, true case "api.siliconflow.com": return balanceSource{"https://api.siliconflow.com/v1/user/info", readSiliconFlow("$"), ""}, true + case "api.stepfun.com": + return balanceSource{"https://api.stepfun.com/v1/accounts", readStepFun("¥"), ""}, true + case "api.stepfun.ai": + return balanceSource{"https://api.stepfun.ai/v1/accounts", readStepFun("$"), ""}, true case "api.commandcode.ai": return balanceSource{"https://api.commandcode.ai/alpha/billing/credits", readCommandCode, ""}, true case "aihubmix.com": @@ -193,6 +197,27 @@ func readSiliconFlow(sign string) func([]byte) (string, error) { } } +// readStepFun: {"object":"account","type":"prepaid","balance":26.00, +// "total_cash_balance":0.00,"total_voucher_balance":26.00}, balance being +// what is left to spend, vouchers included, and the totals what was ever +// paid in and given. A Step Plan key tells it too: the plan's key is the +// account's, and the account's balance is what it spends past the plan. +func readStepFun(sign string) func([]byte) (string, error) { + return func(b []byte) (string, error) { + var r struct { + Balance any `json:"balance"` + } + if err := json.Unmarshal(b, &r); err != nil { + return "", err + } + v, ok := number(r.Balance) + if !ok { + return "", errors.New("no balance in the reply") + } + return money(sign, v), nil + } +} + // readAiHubMix: {"object":"list","total_usage":12.5}, what is left on the // key in dollars, despite the name. A key without a limit answers -1 of // AiHubMix's units ($1 is 500000 of them): it has no balance of its own, and diff --git a/internal/provider/balance_test.go b/internal/provider/balance_test.go index cb5d1f0ef..b39932f90 100644 --- a/internal/provider/balance_test.go +++ b/internal/provider/balance_test.go @@ -23,6 +23,8 @@ func TestBalanceReaders(t *testing.T) { {"commandcode", readCommandCode, `{"credits":{"monthlyCredits":12.3,"purchasedCredits":2,"freeCredits":0},"windowLimits":{"limited":true,"fiveHour":{"used":4.2,"cap":10},"weekly":{"used":9,"cap":50}}}`, "$14.30"}, {"commandcode credits only", readCommandCode, `{"credits":{"monthlyCredits":"70"},"windowLimits":null}`, "$70.00"}, {"siliconflow", readSiliconFlow("¥"), `{"code":20000,"data":{"balance":"0.88","totalBalance":"88.88"}}`, "¥88.88"}, + {"stepfun", readStepFun("¥"), `{"object":"account","type":"prepaid","balance":26.00,"total_cash_balance":0.00,"total_voucher_balance":26.00}`, "¥26.00"}, + {"stepfun intl", readStepFun("$"), `{"object":"account","type":"prepaid","balance":0.00,"total_cash_balance":0.00,"total_voucher_balance":0.00}`, "$0.00"}, } { got, err := c.read([]byte(c.body)) if err != nil || got != c.want { @@ -81,6 +83,8 @@ func TestBalanceSourceByHost(t *testing.T) { "https://openrouter.ai/api/v1": "https://openrouter.ai/api/v1/credits", "https://api.siliconflow.cn/v1": "https://api.siliconflow.cn/v1/user/info", "https://api.commandcode.ai/provider/v1": "https://api.commandcode.ai/alpha/billing/credits", + "https://api.stepfun.com/step_plan/v1": "https://api.stepfun.com/v1/accounts", + "https://api.stepfun.ai/v1": "https://api.stepfun.ai/v1/accounts", "https://relay.example.com/v1": "", "https://api.deepseek.com.evil/": "", } {