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/main1bb8143b3(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, from091153bf). - 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/main9120acaff: - 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.
228 lines
8.2 KiB
Go
228 lines
8.2 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
)
|
|
|
|
func keyPlanHome(t *testing.T) {
|
|
home := t.TempDir()
|
|
t.Setenv("HOME", home)
|
|
t.Setenv("USERPROFILE", home)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
|
|
}
|
|
|
|
// volcMixed is yckonnnn's provider (on X): Volcengine at its Coding Plan,
|
|
// with a pay-as-you-go key and an Agent Plan key beside the plan's own.
|
|
func volcMixed() Provider {
|
|
pr := Preset("volcengine")
|
|
return Provider{ID: "volc", Name: "Volc", Preset: "volcengine", Chat: pr.Chat, Responses: pr.Responses, Anthropic: pr.Anthropic,
|
|
Key: "k-coding", Keys: []KeyAccount{{Key: "k-api", Plan: "api"}, {Key: "k-agent", Plan: "agent"}}}
|
|
}
|
|
|
|
// Each key goes to its own plan's endpoints, on every protocol; a key that
|
|
// names no plan stays on the provider's.
|
|
func TestKeyPlanEndpoints(t *testing.T) {
|
|
p := volcMixed()
|
|
if !p.PlansMixed() {
|
|
t.Fatal("keys on three plans read as one")
|
|
}
|
|
for _, c := range []struct{ key, chat, responses, anthropic string }{
|
|
{"k-coding", "https://ark.cn-beijing.volces.com/api/coding/v3", "https://ark.cn-beijing.volces.com/api/coding/v3", "https://ark.cn-beijing.volces.com/api/coding"},
|
|
{"k-api", "https://ark.cn-beijing.volces.com/api/v3", "https://ark.cn-beijing.volces.com/api/v3", ""},
|
|
{"k-agent", "https://ark.cn-beijing.volces.com/api/plan/v3", "https://ark.cn-beijing.volces.com/api/plan/v3", "https://ark.cn-beijing.volces.com/api/plan"},
|
|
} {
|
|
var k KeyAccount
|
|
for _, x := range p.KeysOn() {
|
|
if x.Key == c.key {
|
|
k = x
|
|
}
|
|
}
|
|
q := p.WithKey(k)
|
|
if q.Chat != c.chat || q.Responses != c.responses || q.Anthropic != c.anthropic {
|
|
t.Errorf("%s at %q %q %q", c.key, q.Chat, q.Responses, q.Anthropic)
|
|
}
|
|
if q.PlansMixed() {
|
|
t.Errorf("%s: one key's copy judged mixed again", c.key)
|
|
}
|
|
}
|
|
// at a host of its own (a mirror, a fake vendor) each plan is asked there
|
|
p.Chat, p.Responses, p.Anthropic = "http://127.0.0.1:9/api/coding/v3", "http://127.0.0.1:9/api/coding/v3", "http://127.0.0.1:9/api/coding"
|
|
if q := p.WithKey(p.Keys[1]); q.Chat != "http://127.0.0.1:9/api/plan/v3" || q.Anthropic != "http://127.0.0.1:9/api/plan" {
|
|
t.Errorf("agent key at its own host: %q %q", q.Chat, q.Anthropic)
|
|
}
|
|
}
|
|
|
|
// A model only one plan has goes only to that plan's key; one every plan
|
|
// has goes to every key; a model the user set a key to serve it serves.
|
|
func TestKeyPlanServing(t *testing.T) {
|
|
p := volcMixed()
|
|
serves := func(model string) string {
|
|
f := p.PlanServing(model)
|
|
var on []string
|
|
for _, k := range p.KeysOn() {
|
|
if f(k) {
|
|
on = append(on, k.Key)
|
|
}
|
|
}
|
|
return strings.Join(on, ",")
|
|
}
|
|
for model, want := range map[string]string{
|
|
"deepseek-v3-250324": "k-api", // pay as you go's alone
|
|
"doubao-seedance-2.0": "k-api,k-agent", // Agent Plan's video, and the open list
|
|
"kimi-k3": "k-coding,k-api,k-agent", // every plan
|
|
"Kimi-K3": "k-coding,k-api,k-agent",
|
|
} {
|
|
if got := serves(model); got != want {
|
|
t.Errorf("%s: %s, want %s", model, got, want)
|
|
}
|
|
}
|
|
p.AccountModels = map[string][]string{keyID("k-coding"): {"deepseek-v3-250324"}}
|
|
if got := serves("deepseek-v3-250324"); got != "k-coding,k-api" {
|
|
t.Errorf("a key set to serve a model: %s", got)
|
|
}
|
|
}
|
|
|
|
// Old configs are as they were: keys that name no plan, a plan that is the
|
|
// provider's own, a plan the preset doesn't have, or a preset whose keys
|
|
// are all on one plan.
|
|
func TestKeyPlanOldConfigsUnchanged(t *testing.T) {
|
|
keyPlanHome(t)
|
|
for name, p := range map[string]Provider{
|
|
"no plans": func() Provider { p := volcMixed(); p.Keys[0].Plan, p.Keys[1].Plan = "", ""; return p }(),
|
|
"own plan": func() Provider {
|
|
p := volcMixed()
|
|
p.KeyPlan, p.Keys[0].Plan, p.Keys[1].Plan = "coding", "coding", "coding"
|
|
return p
|
|
}(),
|
|
"gone": func() Provider { p := volcMixed(); p.Keys[0].Plan, p.Keys[1].Plan = "lite", "pro"; return p }(),
|
|
"one plan": {ID: "q", Name: "Q", Preset: "qwen", Chat: "https://dashscope-intl.aliyuncs.com/compatible-mode/v1", Key: "a", Keys: []KeyAccount{{Key: "b", Plan: "api"}}},
|
|
} {
|
|
if p.PlansMixed() {
|
|
t.Errorf("%s: mixed", name)
|
|
}
|
|
for _, k := range p.KeysOn() {
|
|
if q := p.WithKey(k); q.Chat != p.Chat || q.Anthropic != p.Anthropic {
|
|
t.Errorf("%s: key %s moved to %q", name, k.Key, q.Chat)
|
|
}
|
|
if !p.PlanServing("deepseek-v3-250324")(k) {
|
|
t.Errorf("%s: key %s barred", name, k.Key)
|
|
}
|
|
}
|
|
if got := planViews([]Provider{p}); len(got) != 1 || got[0].Key != p.Key || len(got[0].Keys) != len(p.Keys) {
|
|
t.Errorf("%s: views %+v", name, got)
|
|
}
|
|
}
|
|
// a saved provider that names no plan is written as before
|
|
p := volcMixed()
|
|
p.Keys[0].Plan, p.Keys[1].Plan = "", ""
|
|
if err := Save(p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b, err := os.ReadFile(Path())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if strings.Contains(string(b), `"plan"`) || strings.Contains(string(b), `"keyPlan"`) {
|
|
t.Errorf("a plan written for keys that name none:\n%s", b)
|
|
}
|
|
}
|
|
|
|
// SetKeyPlan keeps the provider's own plan as none, refuses a plan the
|
|
// preset doesn't have and a preset whose keys are on one plan.
|
|
func TestSetKeyPlan(t *testing.T) {
|
|
keyPlanHome(t)
|
|
p := volcMixed()
|
|
p.Keys[0].Plan, p.Keys[1].Plan = "", ""
|
|
if err := Save(p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := SetKeyPlan("volc", keyID("k-api"), "api"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := SetKeyPlan("volc", keyID("k-coding"), "agent"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := SetKeyPlan("volc", keyID("k-agent"), "coding"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := Find("volc")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got.KeyPlan != "agent" || got.Keys[0].Plan != "api" || got.Keys[1].Plan != "" {
|
|
t.Fatalf("plans %q %q %q", got.KeyPlan, got.Keys[0].Plan, got.Keys[1].Plan)
|
|
}
|
|
if l := got.KeyList(); l[0].Plan != "agent" || l[1].Plan != "api" {
|
|
t.Errorf("KeyList %+v", l)
|
|
}
|
|
if err := SetKeyPlan("volc", keyID("k-api"), "nope"); err == nil {
|
|
t.Error("a plan the preset doesn't have")
|
|
}
|
|
if err := Save(Provider{ID: "q", Name: "Q", Preset: "qwen", Chat: "https://dashscope-intl.aliyuncs.com/compatible-mode/v1", Key: "a"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := SetKeyPlan("q", keyID("a"), "api"); err == nil {
|
|
t.Error("a plan for a preset whose keys are on one")
|
|
}
|
|
}
|
|
|
|
// A provider of keys on mixed plans lists each key's plan: the Coding
|
|
// Plan's models as the preset says, without asking (its /models is Ark's
|
|
// general catalog), pay as you go's from its own /models with its key.
|
|
func TestKeyPlanModelList(t *testing.T) {
|
|
keyPlanHome(t)
|
|
var mu sync.Mutex
|
|
var asked []string
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
mu.Lock()
|
|
asked = append(asked, r.Header.Get("Authorization")+" "+r.URL.Path)
|
|
mu.Unlock()
|
|
_, _ = w.Write([]byte(`{"data":[{"id":"deepseek-v3-250324"},{"id":"kimi-k3"}]}`))
|
|
}))
|
|
defer srv.Close()
|
|
p := Provider{ID: "volc", Name: "Volc", Preset: "volcengine", Chat: srv.URL + "/api/coding/v3", Responses: srv.URL + "/api/coding/v3", Anthropic: srv.URL + "/api/coding",
|
|
Key: "k-coding", Keys: []KeyAccount{{Key: "k-api", Plan: "api"}}}
|
|
ms, err := p.fetch(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if strings.Join(asked, ",") != "Bearer k-api /api/v3/models" {
|
|
t.Fatalf("asked %v", asked)
|
|
}
|
|
have := map[string]bool{}
|
|
for _, m := range ms {
|
|
have[m.ID] = true
|
|
}
|
|
if !have["deepseek-v3-250324"] || !have["ark-code-latest"] || !have["kimi-k3"] {
|
|
t.Fatalf("models %v", have)
|
|
}
|
|
if p.Serves(p.first(), "deepseek-v3-250324") || !p.Serves(p.Keys[0], "deepseek-v3-250324") {
|
|
t.Error("pay as you go's model served by the Coding Plan key")
|
|
}
|
|
if !p.Serves(p.first(), "kimi-k3") || !p.Serves(p.Keys[0], "kimi-k3") {
|
|
t.Error("a model both plans have")
|
|
}
|
|
}
|
|
|
|
// Each plan a provider has keys of is its own view, with those keys alone,
|
|
// at that plan's endpoints: windows read per plan.
|
|
func TestKeyPlanViews(t *testing.T) {
|
|
v := planViews([]Provider{volcMixed()})
|
|
if len(v) != 3 {
|
|
t.Fatalf("%d views", len(v))
|
|
}
|
|
for i, want := range []string{"k-coding /api/coding/v3", "k-api /api/v3", "k-agent /api/plan/v3"} {
|
|
if got := v[i].Key + " " + strings.TrimPrefix(v[i].Chat, "https://ark.cn-beijing.volces.com"); got != want || len(v[i].Keys) != 0 {
|
|
t.Errorf("view %d: %s (%d more keys), want %s", i, got, len(v[i].Keys), want)
|
|
}
|
|
}
|
|
}
|