Files
magpie/internal/provider/volcengine_usage.go
T
yetone 6e6aba51c8 providers: a key may be for another plan of its vendor, sent to that plan's endpoints
yckonnnn on X uses Volcengine's pay as you go and its Agent Plan, and
wants a key of each in one provider. Each plan takes its own keys at its
own endpoints (/api/v3, /api/plan/v3, /api/coding/v3), and every key of
a provider went to the provider's one plan, so only one kind worked.

A key now may say which of its preset's Regions it is for
(KeyAccount.Plan; Provider.KeyPlan for the first key, as KeyProtocol).
None, or the provider's own plan, is the provider's plan, stored as
none: old configs read and write as before. Presets whose plans take
keys of their own (PresetDef.KeyPlans): zhipu, zai, stepfun,
stepfun-cn, xiaomi, baidu-qianfan, tencent-cloud, huaweicloud,
volcengine, siliconflow. Not: yylx (one key everywhere),
bailian-decision (decide only), bedrock and bedrock-mantle (regions of
one account).

Every reader of the region or a base URL, and what it does now:
- WithKey (keys.go): moves the key's copy to its plan (atKeyPlan:
  Chat, Responses, Anthropic, Decide, KeysURL, Website, Catalog via
  atRegionOf; a provider at a host of its own keeps that host) and marks
  it keyView. Every gateway key path goes through it: chat, Responses,
  Messages, count_tokens, failover, embeddings (serveRetrieval via
  onKeys), drawing (drawOnAccounts via perKeyBarred), key tests
  (test.go), key_models.go, gui/groups.go, markedKey.
- perKeyBarred (gateway/fallback.go): a key whose plan's list lacks the
  model (PlanServing) is not asked; only when no key's plan has it.
- fetch / fetchPerKey / planOnly (models.go): a mixed provider lists
  each key's plan, a NoList plan from its preset list with no request,
  pay as you go from /api/v3/models with its own key.
- planMedia (PlanEmbeddings, PlanDrawers, PlanVideos): every plan's.
- PlanQuotas (planquota.go): each key read at its own plan's source.
- volcPlanQuotas, tencentPlanQuotas: one view per plan (planViews).
- KeyBalances (balance.go): each key at its plan's host (SiliconFlow).
- startVideo / videoMaker (gateway/video.go): a mixed provider starts on
  a key whose plan makes the model (onKeys) and names it in the id
  (keyMark), so the poll goes to that key's plan.
- GUI: POST /api/keys/plan (SetKeyPlan, then the lists are read again);
  the editor keeps KeyPlan with an unchanged first key, typed() clears
  it with a new one; KeepSecrets, AddCopy, backup's withoutKeys carry it.
- app.js: planPicker, a plan chip in each key row (the app's own menu,
  openProtoMenu, through pickPill, which protoPicker now shares), shown
  for a KeyPlans preset while the provider has several keys or the key
  has a plan; i18n zh, zh-TW, ja, de.

A key's plan is not guessed from the key: Volcengine's keys carry
nothing that says their plan. Xiaomi's tp-, Tencent's sk-tp-, MiniMax's
sk-cp- and Bailian's sk-sp- are noted in code but not used.

Not done: a gateway key held to some keys (videoAsker) on a mixed
provider's video may pick another key; a mixed provider whose own plan
has no balance source while a key's plan has one is not read; keys left
out for their plan don't show in the Routing trace; StepFun's plan
windows are a sign-in per site, unchanged.

Credit: yckonnnn on X (tweet 2114657701305487483).

Run at this commit on origin/main 1bb8143b3 (macOS, go1.26.8):
- gofmt -l internal: clean. go vet ./..., GOOS=linux go vet -tags nogui
  ./..., GOOS=windows go vet ./...: clean.
- go test -count=1 -skip TestUnreadLoginsWriteErrorsKeepCredentials
  ./internal/provider/ ./internal/gateway/: ok (103s, 421s). The skipped
  test crashes the whole provider package on main (#3632, from 091153bf).
- TestCoreScriptBudget: ok, budgets not raised (app.js 1,307,430 bytes).
- GUI, chromium and webkit: key-*, provider*-*, add-sheet, i18n-keys,
  gui-de, gui-ja, gui-zh-tw (58 files): 956 of 956 pass. key-plan.test.cjs
  covers en, zh, zh-TW, ja and de at 420 and 1000px.
Earlier, at the same change on origin/main 9120acaff:
- go test -timeout 45m ./...: 71 packages ok, internal/gateway and
  internal/gui among them. internal/provider crashed on #3632 only; run
  again with that one test skipped, it passed.
- The whole GUI suite (node --test tests/*.test.cjs) did not finish: the
  disk ran out at about 319 tests and the run was stopped. Up to then the
  only reds were account-discovery in both engines, #3497, red on main.
Broken by hand: without atKeyPlan in WithKey, TestKeyPlanEndpoints,
TestKeyPlanModelList and TestKeyPlansEachToItsOwnEndpoint go red; without
the planless filter in perKeyBarred the gateway test goes red; without
the plan chip key-plan.test.cjs goes red.
Not tried: real Volcengine (or any other vendor's) keys of two plans in
one provider; the video path (startVideo with mixed plans) has no test of
its own; Windows and Linux were vetted, not run.
2026-10-26 20:37:07 +08:00

423 lines
13 KiB
Go

package provider
// Volcengine Ark's Coding Plan and Agent Plan (#1427, yayoinoyume) have
// 5-hour, weekly and monthly windows, which Ark tells only to the
// account's control plane: GetCodingPlanUsage and GetAFPUsage on
// open.volcengineapi.com, signed with the account's AccessKey ID and
// Secret (AccessKeyID, SecretAccessKey), never to the plan's inference
// key. The plan a provider is on is its endpoint's: /api/coding is the
// Coding Plan's, /api/plan the Agent Plan's; pay-as-you-go (/api/v3) has
// no windows and no card.
//
// The request is signed as Volcengine's own SDK signs it
// (volc-sdk-golang base/sign.go GetSignRequest, the signer of
// volcengine-go-sdk's volcenginequery): HMAC-SHA256 over the canonical
// request with the signed headers sorted, the scope
// date/region/ark/request, and a key derived from the bare Secret.
// TestVolcSignMatchesSDK pins a signature that SDK made.
import (
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"time"
)
const (
volcOpenAPIHost = "open.volcengineapi.com"
volcAPIVersion = "2024-01-01"
volcDefaultRegion = "cn-beijing"
volcService = "ark"
volcContentType = "application/json"
volcAKSKHint = "check the AccessKey ID and Secret in the provider's editor, and that the access key may query Ark (IAM)"
)
// TakesVolcAccessKey says p is on Volcengine Ark (ark.<region>.volces.com),
// whose editor then asks for the account's AccessKey ID and Secret.
func TakesVolcAccessKey(p Provider) bool {
if p.Account != nil {
return false
}
for _, base := range []string{p.Chat, p.Responses, p.Anthropic} {
if h := hostOf(base); strings.HasPrefix(h, "ark.") && strings.HasSuffix(h, ".volces.com") {
return true
}
}
return false
}
// volcPlanOf is the plan p's endpoint is on: the action that tells its
// windows, the plan's name and the region it is in; ok false for
// pay-as-you-go or another vendor.
func volcPlanOf(p Provider) (action, plan, region string, ok bool) {
if !TakesVolcAccessKey(p) {
return "", "", "", false
}
for _, base := range []string{p.Chat, p.Anthropic, p.Responses} {
u, err := url.Parse(base)
if err != nil || !strings.HasSuffix(strings.ToLower(u.Host), ".volces.com") {
continue
}
switch path := strings.TrimSuffix(u.Path, "/") + "/"; {
case strings.HasPrefix(path, "/api/coding/"):
return "GetCodingPlanUsage", "Coding Plan", volcRegion(u.Host), true
case strings.HasPrefix(path, "/api/plan/"):
return "GetAFPUsage", "Agent Plan", volcRegion(u.Host), true
}
}
return "", "", "", false
}
// volcRegion is the region of an Ark host: ark.cn-beijing.volces.com is
// cn-beijing; cn-beijing when it names none.
func volcRegion(host string) string {
labels := strings.Split(strings.ToLower(host), ".")
if len(labels) == 4 && labels[0] == "ark" && labels[1] != "" {
return labels[1]
}
return volcDefaultRegion
}
// volcPlanQuotas is a card for each Volcengine Ark plan a provider in use
// is on and has the account's AccessKey ID and Secret for: one for each
// access key and plan, named for the first such provider. A plan no
// provider on it has the access key for is a card that says so
// (accessKeyCard, #1821): it was left out with nothing said.
func volcPlanQuotas(ctx context.Context) []SubscriptionQuota {
type job struct {
p Provider
action, plan, region string
}
seen := map[string]bool{}
var jobs []job
keyed := map[string]bool{} // the plans a provider has the access key for
var keyless []job
for _, p := range planViews(All()) {
if p.Hidden || p.Off {
continue
}
action, plan, region, ok := volcPlanOf(p)
if !ok {
continue
}
if p.AccessKeyID == "" || p.SecretAccessKey == "" {
keyless = append(keyless, job{p, action, plan, region})
continue
}
keyed[action] = true
if seen[p.AccessKeyID+" "+action] || !wantsCard(ctx, p.ID, "") {
continue
}
seen[p.AccessKeyID+" "+action] = true
jobs = append(jobs, job{p, action, plan, region})
}
var missing []SubscriptionQuota
for _, j := range keyless {
if keyed[j.action] || seen["none "+j.action] || !wantsCard(ctx, j.p.ID, "") {
continue
}
seen["none "+j.action] = true
missing = append(missing, accessKeyCard(j.p, j.plan))
}
out := make([]SubscriptionQuota, len(jobs))
var wg sync.WaitGroup
for i, j := range jobs {
wg.Add(1)
go func() {
defer wg.Done()
out[i] = volcPlanQuota(j.p.Via(ctx), j.p, j.action, j.plan, j.region)
// 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))
}()
}
wg.Wait()
return append(out, missing...)
}
// volcPlanQuota is p's plan as a card, read with its access key.
func volcPlanQuota(ctx context.Context, p Provider, action, plan, region string) SubscriptionQuota {
q := SubscriptionQuota{Provider: p.ID, Name: p.Name, Icon: p.Icon, Plan: plan, Windows: []QuotaWindow{}}
raw, err := volcOpenAPICall(ctx, p.AccessKeyID, p.SecretAccessKey, region, action, time.Now())
if err != nil {
q.Error = err.Error()
return q
}
var env struct {
Result json.RawMessage `json:"Result"`
}
_ = json.Unmarshal(raw, &env)
var ws []QuotaWindow
if action == "GetAFPUsage" {
var tier string
ws, tier = volcReadAFP(env.Result)
if tier != "" {
q.Plan = plan + " " + tier
}
} else {
ws = volcReadCodingPlan(env.Result)
}
if len(ws) == 0 {
q.Error = fmt.Sprintf("this Volcengine account has no %s, or Ark answered %s in a form magpie doesn't know: %s", plan, action, volcHead(raw))
return q
}
q.Windows = ws
return q
}
// volcHead is the head of a reply, for a card to say what was answered.
func volcHead(raw []byte) string {
s := strings.TrimSpace(string(raw))
if len(s) > 300 {
s = s[:300] + "…"
}
return s
}
// volcSign is the Authorization header of a POST to the control plane's
// "/" with query and body, at now, as volc-sdk-golang's GetSignRequest
// makes it for the headers volcOpenAPICall sends: Content-Type, Host,
// X-Content-Sha256 and X-Date, signed in that (sorted) order.
func volcSign(ak, sk, region string, query url.Values, body []byte, now time.Time) (auth, xDate, contentSHA string) {
xDate = now.UTC().Format("20060102T150405Z")
date := xDate[:8]
contentSHA = volcHex(body)
const signed = "content-type;host;x-content-sha256;x-date"
canonical := strings.Join([]string{
http.MethodPost,
"/",
volcQuery(query),
"content-type:" + volcContentType + "\n" +
"host:" + volcOpenAPIHost + "\n" +
"x-content-sha256:" + contentSHA + "\n" +
"x-date:" + xDate + "\n",
signed,
contentSHA,
}, "\n")
scope := date + "/" + region + "/" + volcService + "/request"
toSign := strings.Join([]string{"HMAC-SHA256", xDate, scope, volcHex([]byte(canonical))}, "\n")
key := volcMAC([]byte(sk), date)
for _, s := range []string{region, volcService, "request"} {
key = volcMAC(key, s)
}
sig := hex.EncodeToString(volcMAC(key, toSign))
return "HMAC-SHA256 Credential=" + ak + "/" + scope + ", SignedHeaders=" + signed + ", Signature=" + sig, xDate, contentSHA
}
// volcQuery is a query as the SDK signs and sends it: sorted by key,
// percent-encoded, a space as %20 (its normquery).
func volcQuery(v url.Values) string {
return strings.ReplaceAll(v.Encode(), "+", "%20")
}
func volcMAC(key []byte, s string) []byte {
m := hmac.New(sha256.New, key)
m.Write([]byte(s))
return m.Sum(nil)
}
func volcHex(b []byte) string {
sum := sha256.Sum256(b)
return hex.EncodeToString(sum[:])
}
// volcOpenAPICall asks the control plane's action, signed with the access
// key ak and its Secret sk, and gives the reply's body. The access key is
// sent nowhere but open.volcengineapi.com.
func volcOpenAPICall(ctx context.Context, ak, sk, region, action string, now time.Time) ([]byte, error) {
query := url.Values{"Action": {action}, "Version": {volcAPIVersion}}
body := []byte("{}")
auth, xDate, contentSHA := volcSign(ak, sk, region, query, body, now)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "https://"+volcOpenAPIHost+"/?"+volcQuery(query), strings.NewReader(string(body)))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", volcContentType)
req.Header.Set("X-Date", xDate)
req.Header.Set("X-Content-Sha256", contentSHA)
req.Header.Set("Authorization", auth)
req.Header.Set("Accept", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
raw, err := io.ReadAll(io.LimitReader(res.Body, 1<<20))
if err != nil {
return nil, err
}
var env struct {
ResponseMetadata struct {
Error *struct{ Code, Message string } `json:"Error"`
} `json:"ResponseMetadata"`
}
_ = json.Unmarshal(raw, &env)
if e := env.ResponseMetadata.Error; e != nil && (e.Code != "" || e.Message != "") {
msg := fmt.Sprintf("Volcengine %s: %s %s", action, e.Code, e.Message)
if volcRefusesKey(e.Code) || res.StatusCode == http.StatusUnauthorized || res.StatusCode == http.StatusForbidden {
msg += "; " + volcAKSKHint
}
return nil, fmt.Errorf("%s", msg)
}
switch {
case res.StatusCode == http.StatusUnauthorized || res.StatusCode == http.StatusForbidden:
return nil, fmt.Errorf("Volcengine %s: %s; %s", action, res.Status, volcAKSKHint)
case res.StatusCode >= 300:
return nil, fmt.Errorf("Volcengine %s: %s: %s", action, res.Status, volcHead(raw))
}
return raw, nil
}
// volcRefusesKey says a control-plane error code is about the access key
// or its rights (InvalidAccessKey, SignatureDoesNotMatch, AccessDenied…),
// not the plan.
func volcRefusesKey(code string) bool {
c := strings.ToLower(code)
for _, w := range []string{"accesskey", "signature", "accessdenied", "unauthorized", "forbidden", "credential", "authfailure"} {
if strings.Contains(c, w) {
return true
}
}
return false
}
// volcReadCodingPlan reads GetCodingPlanUsage's Result:
//
// {"QuotaUsage":[{"Level":"session","Percent":10.5,"ResetTimestamp":1758100000},
// {"Level":"weekly",…},{"Level":"monthly",…}]}
//
// Percent is the share used; ResetTimestamp is in seconds.
func volcReadCodingPlan(result json.RawMessage) []QuotaWindow {
var r struct {
QuotaUsage []struct {
Level string `json:"Level"`
Percent any `json:"Percent"`
ResetTimestamp any `json:"ResetTimestamp"`
} `json:"QuotaUsage"`
}
if json.Unmarshal(result, &r) != nil {
return nil
}
var out []QuotaWindow
for _, u := range r.QuotaUsage {
w, ok := volcWindow(u.Level)
if !ok {
continue
}
used, ok := volcNum(u.Percent)
if !ok {
continue
}
w.Used, w.ResetsAt = used, volcTime(u.ResetTimestamp)
out = append(out, w)
}
return out
}
// volcReadAFP reads GetAFPUsage's Result: AFPFiveHour, AFPWeekly and
// AFPMonthly (AFPDaily isn't shown by Ark's own console either), each
// with what was used of its quota and when it resets, and the plan's
// Tier. What each bucket carries is from tools built on it, not from a
// reply seen here: Used of Quota (lordqyxz/dsh-ark-quota), Used of Total
// or Percent (arkcli's struct tags, HaydenSmith1121/dsh-ark-plans), and a
// reset in ResetTime or ResetTimestamp, seconds or milliseconds.
func volcReadAFP(result json.RawMessage) ([]QuotaWindow, string) {
type bucket struct {
Used, Quota, Total, Percent, ResetTime, ResetTimestamp any
}
var r struct {
Tier string `json:"Tier"`
AFPFiveHour *bucket `json:"AFPFiveHour"`
AFPWeekly *bucket `json:"AFPWeekly"`
AFPMonthly *bucket `json:"AFPMonthly"`
}
if json.Unmarshal(result, &r) != nil {
return nil, ""
}
var out []QuotaWindow
for _, b := range []struct {
level string
b *bucket
}{{"session", r.AFPFiveHour}, {"weekly", r.AFPWeekly}, {"monthly", r.AFPMonthly}} {
if b.b == nil {
continue
}
w, _ := volcWindow(b.level)
used, ok := volcNum(b.b.Percent)
if !ok {
n, nok := volcNum(b.b.Used)
of, ook := volcNum(b.b.Quota)
if !ook {
of, ook = volcNum(b.b.Total)
}
if !nok || !ook || of <= 0 {
continue // no quota: not on this plan
}
used = n / of * 100
w.Amount, w.Limit = n, of
}
w.Used = used
if w.ResetsAt = volcTime(b.b.ResetTime); w.ResetsAt == nil {
w.ResetsAt = volcTime(b.b.ResetTimestamp)
}
out = append(out, w)
}
return out, strings.TrimSpace(r.Tier)
}
// volcWindow is the window a level names, as other plans' are named.
func volcWindow(level string) (QuotaWindow, bool) {
switch strings.ToLower(strings.TrimSpace(level)) {
case "session":
return QuotaWindow{Name: "5 hours", Span: 5 * time.Hour}, true
case "weekly":
return QuotaWindow{Name: "Weekly", Span: 7 * 24 * time.Hour}, true
case "monthly":
return QuotaWindow{Name: "Month", Span: 30 * 24 * time.Hour}, true
}
return QuotaWindow{}, false
}
// volcNum is a JSON number, or a string of one.
func volcNum(v any) (float64, bool) {
switch x := v.(type) {
case float64:
return x, true
case string:
f, err := strconv.ParseFloat(strings.TrimSpace(x), 64)
return f, err == nil
}
return 0, false
}
// volcTime is a reset as Ark gives it: epoch seconds (Coding Plan),
// milliseconds (Agent Plan) or RFC 3339; nil for none.
func volcTime(v any) *time.Time {
if s, ok := v.(string); ok {
if t, err := time.Parse(time.RFC3339, strings.TrimSpace(s)); err == nil {
return &t
}
}
n, ok := volcNum(v)
if !ok || n <= 0 {
return nil
}
// 1e11 seconds is the year 5138, 1e11 ms 1973: no reset is on the
// wrong side of it
var t time.Time
if n > 1e11 {
t = time.UnixMilli(int64(n))
} else {
t = time.Unix(int64(n), 0)
}
return &t
}