diff --git a/docs/reference.md b/docs/reference.md index a1f4cf8f9..fe3128c05 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -893,6 +893,12 @@ same model on the Qwen AI platform's pay as you go is `qwen-decision` Neither plan has an API for its credits: the console's subscription page shows them, so magpie shows no plan usage for these keys. +Volcengine Ark's System One API, in beta, is `volcengine-decision`: an Ark +API key, asked at `https://ark.cn-beijing.volces.com/api/v1/systemone` with +the `ark-beta-systemone-api: true` header magpie adds on Ark's host, and its +one decision model `doubao-seed-2-1-lite-260915`. The Coding and Agent +Plans' paths don't serve it, so it bills as pay as you go. + Qwen Cloud (qwencloud.com), Alibaba's international Qwen platform, has keys of its own, made at home.qwencloud.com. Its pay as you go is `qwencloud`, at `{region}.maas.qwencloudapi.com` in six regions (`ap-southeast-1`, the diff --git a/internal/gateway/decide.go b/internal/gateway/decide.go index 30c6d49db..26217dfde 100644 --- a/internal/gateway/decide.go +++ b/internal/gateway/decide.go @@ -473,6 +473,7 @@ func (s *Server) postDecide(ctx context.Context, p provider.Provider, model stri req.Header.Set("User-Agent", "magpie/"+Version) passOnCaller(ctx, req) } + p.DecideHeaders(req.Header) if via == provider.ViaVercelEval { // what the AI SDK's gateway provider sends with an evaluation req.Header.Set("ai-gateway-protocol-version", "0.0.1") diff --git a/internal/gateway/decide_ark_test.go b/internal/gateway/decide_ark_test.go new file mode 100644 index 000000000..b2ae7ee6a --- /dev/null +++ b/internal/gateway/decide_ark_test.go @@ -0,0 +1,69 @@ +package gateway + +import ( + "io" + "net/http" + "slices" + "strings" + "sync" + "testing" + + "github.com/yetone/magpie/internal/provider" +) + +// arkUp stands in for ark.cn-beijing.volces.com, in process: its System +// One answers only with the beta header, as it did on 2026-11-01 (the +// reply is the one it gave for a choice and a score; without the header, +// a bare 404). Other hosts go to the test's own servers. +type arkUp struct { + mu sync.Mutex + asked []string + next http.RoundTripper +} + +const arkChoiceReply = `{"model":"doubao-seed-2-1-lite-260915","answers":{"effort":{"type":"score","score":2.049442630883676,"probabilities":{"0":0.01045358664265636,"1":0.09317180355155118,"2":0.7328530020852525,"3":0.16352160772053995},"confidence":0.42819709728784927,"legend":{"3":"high","0":"none","1":"low","2":"medium"}},"intent":{"type":"choice","choice":"coding","probabilities":{"coding":0.9739693776599165,"none":0.0000752175921947392,"chat":0.025955404747888693},"confidence":0.8897008747807184},"level":{"type":"choice","choice":"coding","probabilities":{"chat":0.019124036750888904,"coding":0.9808759632491112},"confidence":0.8635063896029489}},"usage":{"input_tokens":452,"output_tokens":0}}` + +func (u *arkUp) RoundTrip(r *http.Request) (*http.Response, error) { + if r.URL.Hostname() != "ark.cn-beijing.volces.com" { + return u.next.RoundTrip(r) + } + io.Copy(io.Discard, r.Body) + beta := r.Header.Get("ark-beta-systemone-api") + u.mu.Lock() + u.asked = append(u.asked, r.URL.Path+" "+beta) + u.mu.Unlock() + if beta != "true" || r.URL.Path != "/api/v1/systemone" { + return &http.Response{StatusCode: 404, Header: http.Header{}, Body: io.NopCloser(strings.NewReader("")), Request: r}, nil + } + return &http.Response{StatusCode: 200, Header: http.Header{"Content-Type": {"application/json; charset=utf-8"}}, + Body: io.NopCloser(strings.NewReader(arkChoiceReply)), Request: r}, nil +} + +// axelburks on Discord: magpie's System One API, asked for Ark's decision +// model, reaches Ark with its beta header and gives its answer back. +func TestArkSystemOneSendsBetaHeader(t *testing.T) { + fresh(t) + if err := provider.Save(provider.Provider{ID: "volcengine-decision", Name: "Volcengine Ark Decision", Preset: "volcengine-decision", + Key: "ark-1", Decide: "https://ark.cn-beijing.volces.com/api/v1"}); err != nil { + t.Fatal(err) + } + up := &arkUp{next: http.DefaultTransport} + old := http.DefaultClient.Transport + http.DefaultClient.Transport = up + t.Cleanup(func() { http.DefaultClient.Transport = old }) + s := New() + s.client = &http.Client{Transport: up} + code, out := sendTo(s, "/v1/systemone", `{"model":"volcengine-decision/`+provider.ArkDecision+`","state":{"message":"Refactor the auth module to use JWT and add tests"},"questions":{"intent":{"type":"choice","criteria":{"coding":null,"chat":null}}}}`) + if code != 200 || !strings.Contains(out, `"choice":"coding"`) { + t.Fatalf("System One: %d %s", code, out) + } + if got := up.got(); !slices.Equal(got, []string{"/api/v1/systemone true"}) { + t.Fatalf("Ark asked: %v", got) + } +} + +func (u *arkUp) got() []string { + u.mu.Lock() + defer u.mu.Unlock() + return slices.Clone(u.asked) +} diff --git a/internal/provider/decide.go b/internal/provider/decide.go index 5869903bc..1f90eb7d8 100644 --- a/internal/provider/decide.go +++ b/internal/provider/decide.go @@ -46,16 +46,61 @@ func bailianDecides(base string) bool { return strings.HasSuffix(h, ".maas.aliyuncs.com") || h == "maas.qianwenaiapi.com" || strings.HasSuffix(h, ".maas.qianwenaiapi.com") } -// bailianApart reports whether p serves conversations and Bailian's -// decision model both (Bailian's Token Plan, #1506): its chat models' -// list never names the decision model, which is kept apart from it. +// ArkDecision is the model Volcengine Ark answers System One with, in its +// beta (axelburks on Discord): doubao-seed-2-1-lite-260915. Asked on +// 11-01, Ark's other Seed models it serves the account +// (doubao-seed-2-1-pro-260628, doubao-seed-2-0-lite-260428) and +// deepseek-v4-1-flash were each refused 400 "does not support the +// SystemOne API". +const ArkDecision = "doubao-seed-2-1-lite-260915" + +// ArkSystemOneHeader is the header Ark's System One API is turned on by +// while it is in beta: without it, POST …/systemone is a bare 404. +const ArkSystemOneHeader = "ark-beta-systemone-api" + +// arkDecides reports whether base is Volcengine Ark's own API host, +// ark..volces.com, whose pay-as-you-go /api/v1 and /api/v3 answer +// System One at …/systemone. Its Coding and Agent Plans' paths +// (/api/coding/v3, /api/plan/v3) answer it nothing (404), and neither +// /models lists the decision model. +func arkDecides(base string) bool { + h := HostOf(base) + return strings.HasPrefix(h, "ark.") && strings.HasSuffix(h, ".volces.com") +} + +// vendorDecision is the decision model of the vendor whose System One +// API base is, Bailian's or Ark's, which its model list never names. +func vendorDecision(base string) (catalog.Model, bool) { + switch { + case bailianDecides(base): + return catalog.Model{ID: BailianDecision, Name: "Decision model (preview)"}, true + case arkDecides(base): + return catalog.Model{ID: ArkDecision, Name: "Doubao Seed 2.1 Lite"}, true + } + return catalog.Model{}, false +} + +// DecideHeaders sets on h what p's decision API needs besides its key: +// Ark's beta switch (ArkSystemOneHeader) on Ark's host. +func (p Provider) DecideHeaders(h http.Header) { + if arkDecides(p.Decide) { + h.Set(ArkSystemOneHeader, "true") + } +} + +// bailianApart reports whether p serves conversations and a vendor's own +// decision model both (Bailian's Token Plan, #1506, or Ark's pay as you +// go with its decision URL given): its chat models' list never names the +// decision model, which is kept apart from it. func (p Provider) bailianApart() bool { - return !p.DecideOnly() && bailianDecides(p.Decide) + _, ok := vendorDecision(p.Decide) + return !p.DecideOnly() && ok } // ownDecideModels are a System One provider's models that its vendor's // list may not name: the ones picked for a decision-only provider (a -// vendor's own decision model needn't be called jev-…), else Bailian's. +// vendor's own decision model needn't be called jev-…), else Bailian's +// or Ark's. func (p Provider) ownDecideModels() []catalog.Model { var out []catalog.Model if p.DecideOnly() { @@ -65,8 +110,8 @@ func (p Provider) ownDecideModels() []catalog.Model { } } } - if len(out) == 0 && bailianDecides(p.Decide) { - out = []catalog.Model{{ID: BailianDecision, Name: "Decision model (preview)"}} + if m, ok := vendorDecision(p.Decide); len(out) == 0 && ok { + out = []catalog.Model{m} } return out } @@ -93,7 +138,8 @@ func (p Provider) DecidesModel(model string) bool { return true } if p.bailianApart() { - return model == BailianDecision + m, _ := vendorDecision(p.Decide) + return model == m.ID } // decideListed's answer, without its list copied for each model // asked about: a provider's every model was asked, a copy of the whole @@ -384,6 +430,11 @@ func decideFactOf(id string) (decideFact, bool) { return decideFact{context: 65_536, images: true}, true case last == BailianDecision: return decideFact{context: 65_536}, true + case last == ArkDecision: + // its window on System One is not documented; an image in the + // request was left unread on 11-01 (a red square asked "is it + // red?" came back 0.48, its input tokens as for no image) + return decideFact{}, true } return decideFact{}, false } @@ -856,8 +907,9 @@ func (p Provider) fetchDecide(ctx context.Context) ([]catalog.Model, error) { list = mlxServeDecisions(p.Decide) } else if p.ollamaDecides() { list = ollamaDecisions(p.Decide) - } else if bailianDecides(p.Decide) { - // Bailian lists its chat models only: its decision model is asked + } else if _, ok := vendorDecision(p.Decide); ok { + // Bailian and Ark list their chat models only: the decision model + // is asked return p.decideAsked(ctx) } req, err := http.NewRequestWithContext(ctx, http.MethodGet, list, nil) @@ -962,6 +1014,7 @@ func (p Provider) AskSystemOne(ctx context.Context, model string) error { return err } req.Header.Set("Content-Type", "application/json") + p.DecideHeaders(req.Header) if err := p.Sign(ctx, req, Chat, nil); err != nil { return err } diff --git a/internal/provider/decide_ark_test.go b/internal/provider/decide_ark_test.go new file mode 100644 index 000000000..5e5c03dec --- /dev/null +++ b/internal/provider/decide_ark_test.go @@ -0,0 +1,160 @@ +package provider + +import ( + "context" + "crypto/tls" + "encoding/json" + "io" + "net" + "net/http" + "net/http/httptest" + "path/filepath" + "strings" + "sync" + "testing" + "time" +) + +// Ark's System One answers, as ark.cn-beijing.volces.com gave them on +// 2026-11-01 to an Ark API key: magpie's smallest question (AskSystemOne's) +// answered, and the replies for a model that doesn't serve System One and +// for a request without the beta header (a bare 404). +const ( + arkPing = `{"model":"doubao-seed-2-1-lite-260915","answers":{"ok":{"type":"noul","noul":0.998451238905712}},"usage":{"input_tokens":297,"output_tokens":0}}` + arkUnsupported = `{"error":{"code":"InvalidParameter","message":"The parameter ` + "`model`" + ` specified in the request is not valid: does not support the SystemOne API. Request id: 021793512605442ffbfb0a147800028083cadce647c2","param":"model","type":"BadRequest"}}` +) + +// fakeArk answers for ark.cn-beijing.volces.com: POST /api/v1/systemone +// and /api/v3/systemone with the beta header, as Ark does; everything else +// 404 with no body. It returns the path, header and model of each request. +func fakeArk(t *testing.T) func() []string { + t.Helper() + var mu sync.Mutex + var asked []string + srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + b, _ := io.ReadAll(r.Body) + var q struct { + Model string `json:"model"` + } + json.Unmarshal(b, &q) + mu.Lock() + asked = append(asked, r.Method+" "+r.URL.Path+" "+r.Header.Get("ark-beta-systemone-api")+" "+q.Model) + mu.Unlock() + if r.Method != http.MethodPost || r.Header.Get("ark-beta-systemone-api") != "true" || + r.URL.Path != "/api/v1/systemone" && r.URL.Path != "/api/v3/systemone" || r.Header.Get("Authorization") != "Bearer ark-1" { + w.WriteHeader(http.StatusNotFound) + return + } + w.Header().Set("Content-Type", "application/json; charset=utf-8") + if q.Model != ArkDecision { + w.WriteHeader(http.StatusBadRequest) + io.WriteString(w, arkUnsupported) + return + } + io.WriteString(w, arkPing) + })) + t.Cleanup(srv.Close) + addr := srv.Listener.Addr().String() + tr := &http.Transport{ + DialContext: func(ctx context.Context, network, _ string) (net.Conn, error) { + return (&net.Dialer{Timeout: 5 * time.Second}).DialContext(ctx, network, addr) + }, + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + } + old := http.DefaultClient.Transport + http.DefaultClient.Transport = tr + t.Cleanup(func() { http.DefaultClient.Transport = old }) + return func() []string { + mu.Lock() + defer mu.Unlock() + return append([]string(nil), asked...) + } +} + +func arkHome(t *testing.T) { + h := t.TempDir() + t.Setenv("HOME", h) + t.Setenv("USERPROFILE", h) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config")) + t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache")) +} + +// axelburks on Discord: Volcengine Ark serves System One in beta, at +// /api/v1/systemone behind the ark-beta-systemone-api header. Its preset +// asks doubao-seed-2-1-lite-260915 there with the header, and is offered +// to routing groups as their classifier. +func TestArkDecisionPreset(t *testing.T) { + arkHome(t) + asked := fakeArk(t) + p, err := FromPreset("volcengine-decision") + if err != nil { + t.Fatal(err) + } + if !p.DecideOnly() || p.Decide != "https://ark.cn-beijing.volces.com/api/v1" || p.DecideVia() != ViaSystemOne || p.Jev() != ArkDecision { + t.Fatalf("preset: %+v, asked as %s", p, p.Jev()) + } + p.Key = "ark-1" + id, err := Add(p) + if err != nil { + t.Fatal(err) + } + saved, err := Find(id) + if err != nil { + t.Fatal(err) + } + if rs := saved.Test(context.Background()); len(rs) != 1 || !rs[0].OK || rs[0].Model != ArkDecision { + t.Fatalf("test: %+v", rs) + } + if got := asked(); len(got) != 1 || got[0] != "POST /api/v1/systemone true "+ArkDecision { + t.Fatalf("Ark asked: %q", got) + } + if ds := Deciders(); len(ds) != 1 || ds[0].ID != id+"/"+ArkDecision { + t.Fatalf("classifiers: %+v", ds) + } + ms := saved.decideModels() + if len(ms) != 1 || ms[0].ImageInput == nil || *ms[0].ImageInput { + t.Fatalf("decision models: %+v", ms) + } + for _, e := range providerEntries() { + if e.Provider.ID == id { + t.Fatalf("an agent is offered %s", e.ID) + } + } + if got, m, err := RouteDecider(ArkDecision); err != nil || got.ID != id || m != ArkDecision { + t.Fatalf("route: %s %s %v", got.ID, m, err) + } + // the editor's probe, its base pasted whole, and a model Ark doesn't + // serve System One with, its refusal said as Ark said it + q := Provider{Key: "ark-1", Decide: "https://ark.cn-beijing.volces.com/api/v3/systemone"} + if d := q.DetectDecide(context.Background(), ""); !d.OK || d.Model != ArkDecision || d.Base != "https://ark.cn-beijing.volces.com/api/v3" { + t.Fatalf("probe: %+v", d) + } + if d := q.DetectDecide(context.Background(), "doubao-seed-2-1-pro-260628"); d.OK || !strings.Contains(d.Error, "does not support the SystemOne API") { + t.Fatalf("probe of another model: %+v", d) + } +} + +// The beta header goes to Ark's host alone, and an Ark provider that +// serves conversations too keeps its decision model apart from them. +func TestArkHeaderScopedToArk(t *testing.T) { + for base, want := range map[string]string{ + "https://ark.cn-beijing.volces.com/api/v1": "true", + "https://ark.ap-southeast.volces.com/api/v3": "true", + "https://api.typesafe.ai/v1": "", + "https://openrouter.ai/api/v1": "", + "https://token-plan.cn-beijing.maas.aliyuncs.com/x": "", + } { + h := http.Header{} + Provider{Decide: base}.DecideHeaders(h) + if got := h.Get(ArkSystemOneHeader); got != want { + t.Errorf("%s: header %q", base, got) + } + } + mixed := Provider{ID: "ark", Chat: "https://ark.cn-beijing.volces.com/api/v3", Decide: "https://ark.cn-beijing.volces.com/api/v3", Models: []string{"glm-4-7-251222"}} + if !mixed.DecidesModel(ArkDecision) || mixed.DecidesModel("glm-4-7-251222") { + t.Fatal("an Ark chat model taken for its decision model, or the other way") + } + if ms := mixed.DecisionModels(); len(ms) != 1 || ms[0].ID != ArkDecision { + t.Fatalf("decision models: %+v", ms) + } +} diff --git a/internal/provider/presets.go b/internal/provider/presets.go index 7c43801b5..1d7f261fc 100644 --- a/internal/provider/presets.go +++ b/internal/provider/presets.go @@ -645,6 +645,14 @@ var presets = []PresetDef{ Decide: "https://maas.qianwenaiapi.com/compatible-mode/v1", Note: "routes groups · picks model and effort", Website: "https://www.qianwenai.com/models/decision-model-preview", KeysURL: "https://platform.qianwenai.com/home/api-keys"}, + // Volcengine Ark's System One API, in beta (axelburks on Discord): its + // pay-as-you-go /api/v1/systemone, behind the ark-beta-systemone-api + // header, answers with doubao-seed-2-1-lite-260915 (ArkDecision) on an + // Ark API key. The Coding and Agent Plans' paths don't serve it. + {ID: "volcengine-decision", Name: "Volcengine Ark Decision", Short: "Ark Decision", Icon: "volcengine-color", Kind: KindVendor, + Decide: "https://ark.cn-beijing.volces.com/api/v1", + Note: "routes groups · picks model and effort", + Website: "https://www.volcengine.com/product/ark", KeysURL: "https://ark.volcengine.com/region:cn-beijing/apikey"}, // its decision models (Nimble, Clef, Tev …), served at /v1/systemone // since 0.35.0, are the ones its /api/tags gives the "decision" // capability (ollama_decide.go)