Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

provider: an Ark, Tencent or StepFun plan key is weighed by its plan's windows

yetonecd10dfbe6f13
diff --git a/internal/provider/account_plan_allowance_test.go b/internal/provider/account_plan_allowance_test.go
new file mode 100644
index 000000000..9ca849411
--- /dev/null
+++ b/internal/provider/account_plan_allowance_test.go
@@ -0,0 +1,152 @@
+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) }
diff --git a/internal/provider/key_allowance.go b/internal/provider/key_allowance.go
index b41d6553f..b04b4fb3a 100644
--- a/internal/provider/key_allowance.go
+++ b/internal/provider/key_allowance.go
@@ -14,6 +14,7 @@ package provider
import (
"context"
+ "errors"
"strings"
"sync"
"time"
@@ -62,8 +63,64 @@ func readsKeyWindows(p Provider) bool {
if sub2APIKeyLimits(p) {
return true
}
- _, ok := planQuotaSourceOf(p)
- return ok
+ if _, ok := planQuotaSourceOf(p); ok {
+ return true
+ }
+ return accountPlanOf(p) != ""
+}
+
+// accountPlanOf is which plan p's key is on whose windows the key's
+// account tells, not the key: Volcengine Ark's Coding and Agent Plans and
+// Tencent Cloud's Token Plan Enterprise, read with the account's access
+// key (volcengine_usage.go, tencent_usage.go), and StepFun's Step Plan,
+// read with magpie's sign-in to its platform (stepfun_plan.go). The
+// Usage page had their windows while routing had none, and the Routing
+// page said "one key" of a plan with them (chen on Discord). "" for any
+// other key, or one whose access key magpie isn't given; said without
+// asking anything or reading a file, as a request routes by it.
+func accountPlanOf(p Provider) string {
+ if p.Account != nil || p.Key == "" {
+ return ""
+ }
+ keyed := p.AccessKeyID != "" && p.SecretAccessKey != ""
+ if _, _, _, ok := volcPlanOf(p); ok && keyed {
+ return "volc"
+ }
+ if _, ok := tencentPlanRegion(p); ok && keyed {
+ return "tencent"
+ }
+ if StepFunSite(p) != "" {
+ return "stepfun"
+ }
+ return ""
+}
+
+// accountPlanWindows reads q's account's plan windows, as its card on the
+// Usage page reads them. A Tencent account with several team plans has
+// no windows here: which one a key is of isn't told.
+func accountPlanWindows(ctx context.Context, q Provider) (keyReading, error) {
+ var c SubscriptionQuota
+ switch accountPlanOf(q) {
+ case "volc":
+ action, plan, region, _ := volcPlanOf(q)
+ c = volcPlanQuota(q.Via(ctx), q, action, plan, region)
+ case "tencent":
+ region, _ := tencentPlanRegion(q)
+ cs := tencentPlanCards(ctx, q, region, time.Now)
+ if len(cs) != 1 {
+ return keyReading{}, nil
+ }
+ c = cs[0]
+ case "stepfun":
+ if !StepFunSignedIn(q.ID) {
+ return keyReading{}, nil
+ }
+ c = stepPlanQuota(q.Via(ctx), q)
+ }
+ if c.Error != "" {
+ return keyReading{}, errors.New(c.Error)
+ }
+ return keyReading{ws: c.Windows, plan: c.Plan}, nil
}
func keyAllowanceID(p Provider) string { return p.ID + "#" + keyID(p.Key) }
@@ -179,6 +236,11 @@ func keyWindows(ctx context.Context, p Provider) (keyReading, error) {
}
var r keyReading
var err error
+ if accountPlanOf(q) != "" && !sub2APIKeyLimits(q) {
+ if _, ok := planQuotaSourceOf(q); !ok {
+ return accountPlanWindows(ctx, q)
+ }
+ }
if src, ok := planQuotaSourceOf(q); ok && !sub2APIKeyLimits(q) {
r.plan, r.ws, _, err = planKeyWindows(q.Via(ctx), q, src, q.Key)
return r, err
@@ -192,6 +254,20 @@ func keyWindows(ctx context.Context, p Provider) (keyReading, error) {
// keyCardTag is what p's key's card is kept on disk by (keepLast): its
// plan's card, or its balance's.
func keyCardTag(p Provider) string {
+ if _, ok := planQuotaSourceOf(p); !ok && !sub2APIKeyLimits(p) {
+ // an account's plan is its own card, never a key's
+ // (withKeyReadings): Volcengine's the access key's plan's
+ // (volcPlanQuotas), named for the first provider on it
+ switch accountPlanOf(p) {
+ case "volc":
+ action, _, _, _ := volcPlanOf(p)
+ return keyTag("volc "+action, p.AccessKeyID)
+ case "tencent":
+ return keyTag("tencent", p.AccessKeyID)
+ case "stepfun":
+ return keyTag("stepfun", p.ID)
+ }
+ }
if _, ok := planQuotaSourceOf(p); ok && !sub2APIKeyLimits(p) {
return keyTag("plan", p.Key)
}
diff --git a/internal/provider/stepfun_plan.go b/internal/provider/stepfun_plan.go
index 11ebcd62d..531d1dfaa 100644
--- a/internal/provider/stepfun_plan.go
+++ b/internal/provider/stepfun_plan.go
@@ -607,6 +607,10 @@ func stepPlanQuotas(ctx context.Context) []SubscriptionQuota {
go func() {
defer wg.Done()
out[i] = stepPlanQuota(ctx, p)
+ if out[i].Error == "" {
+ // what routing goes by, read just now (KeyAllowance)
+ noteKeyAllowance(p, out[i].Windows, time.Now())
+ }
}()
}
wg.Wait()
diff --git a/internal/provider/tencent_usage.go b/internal/provider/tencent_usage.go
index 3c043628e..5998996ba 100644
--- a/internal/provider/tencent_usage.go
+++ b/internal/provider/tencent_usage.go
@@ -128,6 +128,12 @@ func tencentPlanQuotas(ctx context.Context) []SubscriptionQuota {
go func() {
defer wg.Done()
got[i] = tencentPlanCards(ctx, j.p, j.region, time.Now)
+ if qs := got[i]; len(qs) == 1 && qs[0].Error == "" {
+ // the account's one plan is what routing goes by, read
+ // just now (KeyAllowance); of several, which a key is of
+ // isn't told
+ noteKeyAllowance(j.p, qs[0].Windows, time.Now())
+ }
}()
}
wg.Wait()
diff --git a/internal/provider/volcengine_usage.go b/internal/provider/volcengine_usage.go
index 13dfd4c79..6737e8fbb 100644
--- a/internal/provider/volcengine_usage.go
+++ b/internal/provider/volcengine_usage.go
@@ -135,6 +135,10 @@ func volcPlanQuotas(ctx context.Context) []SubscriptionQuota {
go func() {
defer wg.Done()
out[i] = volcPlanQuota(j.p.Via(ctx), j.p, j.action, j.plan, j.region)
+ if out[i].Error == "" {
+ // what routing goes by, read just now (KeyAllowance)
+ noteKeyAllowance(j.p, out[i].Windows, time.Now())
+ }
// a failed read shows what was last read, as a key's plan does
out[i] = keepReading(ctx, out[i], keyTag("volc "+j.action, j.p.AccessKeyID))
}()