chen on Discord: the Usage page showed a Volcengine Ark Coding Plan's
5-hour and weekly windows, while the Routing page's row for the same key
said only "one key", with no bar. Routing reads a key's windows through
KeyAllowance, and readsKeyWindows knew only plans a key tells itself
(planQuotaSourceOf: Kimi Code, GLM Coding Plan, MiniMax, Command Code,
Ollama, OpenCode Go) and sub2api keys. The plans whose windows the
account tells, not the key, were left out of routing:
- Volcengine Ark Coding / Agent Plan (account AccessKey, volcengine_usage.go)
- Tencent Cloud Token Plan Enterprise (SecretId/Key, tencent_usage.go)
- StepFun Step Plan (magpie's platform sign-in, stepfun_plan.go)
accountPlanOf names them; readsKeyWindows takes them, and keyWindows
reads them as their Usage card does. The cards' own reads note the
windows for routing at once (noteKeyAllowance), and Ark's card kept on
disk seeds routing after a restart (keyCardTag). Their tags are not
"plan"/"balance", so withKeyReadings adds no second card for them. A
Tencent account with several team plans gives a key no windows: which
plan a key is of isn't told. A plan without its access key, and
pay-as-you-go, are never asked. The Routing row then says "{n} left ·
renews in {d}" with its bar, as an account's does; no GUI change.
Test: TestAccountPlanKeyAllowance (Ark read, from its card, from disk;
pay-as-you-go and no-secret not read; Tencent plan and StepFun read) and
TestStepPlanKeyAllowance. Both red on origin/main c46829e0b ("an Ark plan
key has no windows routing reads"; "the key's windows were never read").
Run (mote, at c46829e0b + this change): gofmt -l internal clean; go vet
./internal/provider ./internal/gateway for darwin, linux -tags nogui and
windows: ok. go test -count=1 -timeout 45m ./internal/provider: ok
(91s). go test -run 'Key|Allowance|Routing|Weigh|Trace' ./internal/gateway:
ok.
Not tried: a real Ark, Tencent or StepFun account (none here); the full
./... suite and the GUI suite (no GUI file changed).
153 lines
5.5 KiB
Go
153 lines
5.5 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strconv"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// volcAllowanceServer is open.volcengineapi.com answering an access key's
|
|
// Coding Plan as the Usage page's card reads it, counting the asks.
|
|
func volcAllowanceServer(t *testing.T) *atomic.Int32 {
|
|
t.Helper()
|
|
asked := &atomic.Int32{}
|
|
five := time.Now().Add(3 * time.Hour).Unix()
|
|
week := time.Now().Add(4 * 24 * time.Hour).Unix()
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Header.Get("X-Host") != "open.volcengineapi.com" || r.URL.Query().Get("Action") != "GetCodingPlanUsage" {
|
|
t.Errorf("asked %s%s", r.Header.Get("X-Host"), r.URL)
|
|
w.WriteHeader(http.StatusNotFound)
|
|
return
|
|
}
|
|
asked.Add(1)
|
|
io.WriteString(w, `{"ResponseMetadata":{"RequestId":"r1"},"Result":{"Status":"ok","QuotaUsage":[`+
|
|
`{"Level":"session","Percent":0.8,"ResetTimestamp":`+unixText(five)+`},`+
|
|
`{"Level":"weekly","Percent":87.14,"ResetTimestamp":`+unixText(week)+`}]}}`)
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
old := http.DefaultClient.Transport
|
|
http.DefaultClient.Transport = rewrite{srv}
|
|
t.Cleanup(func() { http.DefaultClient.Transport = old })
|
|
return asked
|
|
}
|
|
|
|
var arkPlan = Provider{ID: "volcengine", Name: "Volcengine Ark", Key: "ark-k",
|
|
Chat: "https://ark.cn-beijing.volces.com/api/coding/v3", AccessKeyID: "AK1", SecretAccessKey: "SK1"}
|
|
|
|
// chen on Discord: the Usage page had an Ark Coding Plan's windows while
|
|
// the Routing page said "one key" of it, as routing never read them. A
|
|
// key whose account tells its plan's windows (Ark's, Tencent's, StepFun's)
|
|
// is weighed by them as a Kimi Code or GLM Coding Plan key is.
|
|
func TestAccountPlanKeyAllowance(t *testing.T) {
|
|
keyLimitsHome(t)
|
|
forgetPlanQuotas()
|
|
t.Cleanup(forgetPlanQuotas)
|
|
asked := volcAllowanceServer(t)
|
|
if err := Save(arkPlan); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if KeyAllowanceID(arkPlan) == "" {
|
|
t.Fatal("an Ark plan key has no windows routing reads")
|
|
}
|
|
a := waitAllowance(t, arkPlan, func(Allowance) bool { return true })
|
|
used, renews := a.For("deepseek-v4.1-flash", time.Now())
|
|
if used < 87 || used > 87.2 || len(renews) != 2 {
|
|
t.Fatalf("used %v, renews %v of %+v", used, renews, a)
|
|
}
|
|
waitRead(t)
|
|
if n := asked.Load(); n != 1 {
|
|
t.Fatalf("asked %d times", n)
|
|
}
|
|
|
|
// what the Usage page's card reads is what routing goes by at once
|
|
keyAllowances.Lock()
|
|
keyAllowances.m = nil
|
|
keyAllowances.Unlock()
|
|
lastQuotas.Lock()
|
|
lastQuotas.m, lastQuotas.loaded = nil, false
|
|
lastQuotas.Unlock()
|
|
if qs := PlanQuotas(context.Background()); len(qs) != 1 || len(qs[0].Windows) != 2 {
|
|
t.Fatalf("cards: %+v", qs)
|
|
}
|
|
if a, ok := KeyAllowance(arkPlan); !ok || len(a) != 2 {
|
|
t.Fatalf("after the card's read: %v %+v", ok, a)
|
|
}
|
|
if n := asked.Load(); n != 2 {
|
|
t.Fatalf("asked %d times: routing asked again what the card had just read", n)
|
|
}
|
|
|
|
// restarted: the card kept on disk stands in until a read is back
|
|
keyAllowances.Lock()
|
|
keyAllowances.m = nil
|
|
keyAllowances.Unlock()
|
|
lastQuotas.Lock()
|
|
lastQuotas.m, lastQuotas.loaded = nil, false
|
|
lastQuotas.Unlock()
|
|
if a, ok := KeyAllowance(arkPlan); !ok || len(a) != 2 {
|
|
t.Fatalf("from the card on disk: %v %+v", ok, a)
|
|
}
|
|
waitRead(t)
|
|
|
|
// pay-as-you-go, or a plan without the account's access key, has none
|
|
// and is never asked
|
|
for _, p := range []Provider{
|
|
{ID: "volc-payg", Key: "ark-k", Chat: "https://ark.cn-beijing.volces.com/api/v3", AccessKeyID: "AK1", SecretAccessKey: "SK1"},
|
|
{ID: "volc-nosecret", Key: "ark-k", Chat: "https://ark.cn-beijing.volces.com/api/coding/v3", AccessKeyID: "AK1"},
|
|
{ID: "tc-payg", Key: "tc-k", Chat: "https://tokenhub.tencentmaas.com/v1", AccessKeyID: "AK1", SecretAccessKey: "SK1"},
|
|
} {
|
|
if _, ok := KeyAllowance(p); ok || KeyAllowanceID(p) != "" {
|
|
t.Errorf("%s reads key windows", p.ID)
|
|
}
|
|
}
|
|
// a Tencent Token Plan Enterprise key and a StepFun key are read too
|
|
for _, p := range []Provider{
|
|
{ID: "tc-plan", Key: "tc-k", Chat: "https://tokenhub.tencentmaas.com/plan/v3", AccessKeyID: "AK1", SecretAccessKey: "SK1"},
|
|
{ID: "stepfun", Key: "sf-k", Chat: "https://api.stepfun.ai/step_plan/v1"},
|
|
} {
|
|
if KeyAllowanceID(p) == "" {
|
|
t.Errorf("%s reads no key windows", p.ID)
|
|
}
|
|
}
|
|
waitRead(t)
|
|
}
|
|
|
|
// A StepFun key is weighed by its Step Plan's windows, read with
|
|
// magpie's sign-in; one not signed in is no windows, not an error.
|
|
func TestStepPlanKeyAllowance(t *testing.T) {
|
|
keyLimitsHome(t)
|
|
forgetPlanQuotas()
|
|
t.Cleanup(forgetPlanQuotas)
|
|
f := newFakeStepFun(t, "ai")
|
|
soon, later := unixText(time.Now().Add(2*time.Hour).Unix()), unixText(time.Now().Add(5*24*time.Hour).Unix())
|
|
f.rateBody = `{"five_hour_usage_left_rate":0.6,"five_hour_usage_reset_time":"` + soon + `","weekly_usage_left_rate":"0.9","weekly_usage_reset_time":"` + later + `"}`
|
|
p := Provider{ID: "stepfun", Name: "StepFun", Key: "sf-k", Chat: "https://api.stepfun.ai/step_plan/v1"}
|
|
if err := Save(p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
KeyAllowance(p)
|
|
waitRead(t)
|
|
if a, ok := KeyAllowance(p); ok {
|
|
t.Fatalf("known without a sign-in: %+v", a)
|
|
}
|
|
|
|
acc, ref := stepfunTokens("4161", time.Now().Add(time.Hour), true)
|
|
f.access.Store(acc)
|
|
if err := SaveStepFunSession(context.Background(), "stepfun", "ai", acc, ref, "web-1"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
forgetKeyAllowances()
|
|
a := waitAllowance(t, p, func(Allowance) bool { return true })
|
|
// 40% of the five hours used, the most of the three
|
|
if used, _ := a.For("step-3.5-flash", time.Now()); used < 39.9 || used > 40.1 {
|
|
t.Fatalf("used %v of %+v", used, a)
|
|
}
|
|
waitRead(t)
|
|
}
|
|
|
|
func unixText(n int64) string { return strconv.FormatInt(n, 10) }
|