Files
magpie/internal/provider/decide_ark_test.go
T
yetone f2f75ee4ae provider: Volcengine Ark's System One API, in beta, as a decider (axelburks on Discord)
Ark answers System One at https://ark.cn-beijing.volces.com/api/v1/systemone
(and /api/v3/systemone) only with the beta header ark-beta-systemone-api:
true; without it the reply is a bare 404. The new volcengine-decision preset
asks it with an Ark API key, and magpie sets the header (DecideHeaders) on
Ark's own host only, ark.<region>.volces.com, in both the gateway's
/v1/systemone and the provider's Test. Its decision model,
doubao-seed-2-1-lite-260915 (ArkDecision), is kept apart from Ark's chat
list as Bailian's is (vendorDecision), since no /models names it.

Learnt from the real API on 11-01 with the owner's Ark key (read from
magpie's config, never printed): only doubao-seed-2-1-lite-260915 answers;
doubao-seed-2-1-pro-260628, doubao-seed-2-0-lite-260428 and
deepseek-v4-1-flash are refused 400 "does not support the SystemOne API".
The Coding and Agent Plans' paths (/api/coding/v3, /api/plan/v3, their v1s)
answer an empty 404 even with the header, so it bills as pay as you go. An
image in the request was left unread (a red square came back 0.48, input
tokens as for no image), so images are off. The tests' replies are the
real response bytes.

Run:
- Real: a magpie built from this tree on mote, in a sandbox HOME:
  `magpie provider add volcengine-decision <key>` and `provider test`
  (decide ok, 413 ms), then `magpie serve` on a free port and POST
  /v1/systemone through it: a real 200 with Ark's choice.
- gofmt -l internal/: clean. go vet darwin, linux, windows of
  internal/provider and internal/gateway: ok.
- mote-run go test -count=1 -timeout 45m ./internal/provider/
  ./internal/gateway/ ./internal/gui/ ./internal/cli/ at base 21882a68d:
  provider ok (140.6s), gateway ok (652.6s), cli ok (58.7s); gui FAIL on
  two tests this change doesn't reach, both filed:
  TestSettingsSaveKeepsWhatItDoesNotSend (BusyLoad, red on main, #4739)
  and TestMainWindowCorners/macOS_26_SDK (mote's macOS 27, #3949).
- Rebased onto origin/main 788057340, this tree: vet darwin, linux,
  windows ok; go test -run 'Ark|Decid|SystemOne|Bailian' of provider
  and gateway ok.
- Broken by hand: without the DecideHeaders calls both Ark tests go red
  (provider: Ark's empty 404; gateway: FAIL).

Not tried: a routing group classified by Ark end to end in an agent;
regions other than cn-beijing; Ark's window on System One (no docs page
for it was found, so none is set).
2026-11-01 14:17:21 +08:00

161 lines
5.9 KiB
Go

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)
}
}