Files
magpie/internal/provider/decide_bailian_test.go
T
yetone 919ceb3a68 provider: Bailian's decision model as a routing preset, and System One for custom providers (#647, Aimer779: Alibaba Cloud Bailian's decision-model-preview speaks TypeSafe's System One at POST <root>/systemone, and magpie only took it from TypeSafe's Jev) — a new preset, Bailian Decision Model (qwen icon, decide-only like TypeSafe Jev), asks decision-model-preview at its plan's root: Workspace · Beijing / Workspace · Singapore (https://{WorkspaceId}.<region>.maas.aliyuncs.com/compatible-mode/v1, the editor asks for the workspace ID and fills it in) or Token Plan · Beijing (token-plan.cn-beijing…, sk-sp- keys). Saving with the workspace still unfilled is refused, both in the editor and in provider.Add. A Bailian host has no /models for it, so its list is the one model, checked by a System One ask. A custom provider can now be a System One API: the CLI's decide=<root> (with models=… named anything, not only jev-*), workspace= and region=/plan= for a preset's plan; the editor's API seg has a fourth option, System One, whose Detect APIs posts a test question to <root>/systemone with the model typed, and a decide-only custom provider passes the "base URL is needed" check. A decide-only provider's first model is what it is asked as (Jev() no longer assumes jev-*), and its models stay routing groups' classifiers, never agents'. TestBailianDecisionPreset, TestCustomSystemOne, TestProviderSystemOne and TestProviderDecidePairs fail on the old code (undefined BailianDecision / DetectDecide, unknown decide=); internal/gui/tests/system-one.test.cjs (webkit, en/zh) covers the workspace, the Singapore and Token Plan plans, TypeSafe's unchanged editor and a custom System One detected and added. go vet, builds (darwin, GOOS=linux, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass but TestProvidersAnswerWhileListsComeIn, a timing test that passes alone. Not tried against a real Bailian key.
2026-10-03 15:21:15 +08:00

138 lines
4.8 KiB
Go

package provider
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"path/filepath"
"slices"
"strings"
"testing"
)
// Bailian's decision model (#647): a preset at the host of the key's
// workspace or the Token Plan's, asked as decision-model-preview.
func TestBailianDecisionPreset(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"))
p, err := FromPreset("bailian-decision")
if err != nil {
t.Fatal(err)
}
if !p.DecideOnly() || !strings.Contains(p.Decide, WorkspaceID) {
t.Fatalf("preset: %+v", p)
}
p.Key = "sk-test"
if _, err := Add(p); err == nil || !strings.Contains(err.Error(), "workspace ID") {
t.Fatalf("added with no workspace: %v", err)
}
pr := Preset("bailian-decision")
var plans []string
for _, r := range pr.Regions {
plans = append(plans, r.ID+" "+r.Decide)
}
want := []string{
"cn-beijing https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1",
"ap-southeast-1 https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
"token-plan https://token-plan.cn-beijing.maas.aliyuncs.com/compatible-mode/v1",
}
if !slices.Equal(plans, want) {
t.Fatalf("plans: %q", plans)
}
p.Decide = strings.ReplaceAll(p.Decide, WorkspaceID, "ws-1")
id, err := Add(p)
if err != nil {
t.Fatal(err)
}
saved, err := Find(id)
if err != nil {
t.Fatal(err)
}
if saved.DecideVia() != ViaSystemOne || saved.Jev() != BailianDecision {
t.Fatalf("asked as %s via %s", saved.Jev(), saved.DecideVia())
}
if ds := Deciders(); len(ds) != 1 || ds[0].ID != id+"/"+BailianDecision {
t.Fatalf("classifiers: %+v", ds)
}
for _, e := range providerEntries() {
if e.Provider.ID == id {
t.Fatalf("an agent is offered %s", e.ID)
}
}
if got, m, err := RouteDecider(id); err != nil || got.ID != id || m != BailianDecision {
t.Fatalf("route %s: %s %s %v", id, got.ID, m, err)
}
if got, m, err := RouteDecider(BailianDecision); err != nil || got.ID != id || m != BailianDecision {
t.Fatalf("route %s: %s %s %v", BailianDecision, got.ID, m, err)
}
}
// a System One API of a vendor's own: its model is named anything, its
// list (none here) needn't name it, and it is asked at POST …/systemone
func TestCustomSystemOne(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"))
asked := 0
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost || r.URL.Path != "/compatible-mode/v1/systemone" {
http.NotFound(w, r)
return
}
var q struct {
Model string `json:"model"`
State map[string]any `json:"state"`
Questions map[string]map[string]any `json:"questions"`
}
json.NewDecoder(r.Body).Decode(&q)
if r.Header.Get("Authorization") != "Bearer sk-1" {
w.WriteHeader(401)
w.Write([]byte(`{"error":{"message":"Invalid API-key provided."}}`))
return
}
if q.Model != "my-decider" || len(q.State) == 0 || len(q.Questions) == 0 {
w.WriteHeader(400)
w.Write([]byte(`{"error":{"message":"bad request"}}`))
return
}
asked++
w.Write([]byte(`{"model":"my-decider","answers":{"ok":{"type":"noul","noul":0.99}},"usage":{"input_tokens":12}}`))
}))
defer up.Close()
p := Provider{ID: "dec", Name: "Decider", Key: "sk-1", Decide: up.URL + "/compatible-mode/v1", Models: []string{"my-decider"}}
if err := Save(p); err != nil {
t.Fatal(err)
}
if p.Jev() != "my-decider" {
t.Fatalf("asked as %s", p.Jev())
}
ms, err := p.Fetch(context.Background())
if err != nil || len(ms) != 1 || ms[0].ID != "my-decider" || asked != 1 {
t.Fatalf("fetch: %+v %v (asked %d)", ms, err, asked)
}
if ds := Deciders(); len(ds) != 1 || ds[0].ID != "dec/my-decider" {
t.Fatalf("classifiers: %+v", ds)
}
if rs := p.Test(context.Background()); len(rs) != 1 || !rs[0].OK || rs[0].Model != "my-decider" {
t.Fatalf("test: %+v", rs)
}
// the editor's probe, a new provider with the address pasted whole
q := Provider{Key: "sk-1", Decide: up.URL + "/compatible-mode/v1/systemone/"}
if d := q.DetectDecide(context.Background(), "my-decider"); !d.OK || d.Base != up.URL+"/compatible-mode/v1" || d.Protocol != "decide" {
t.Fatalf("probe: %+v", d)
}
q.Key = "sk-2"
if d := q.DetectDecide(context.Background(), "my-decider"); d.OK || !strings.Contains(d.Error, "Invalid API-key") {
t.Fatalf("probe with a wrong key: %+v", d)
}
if d := (Provider{Decide: "https://" + WorkspaceID + ".cn-beijing.maas.aliyuncs.com/compatible-mode/v1"}).DetectDecide(context.Background(), ""); d.OK || d.Model != BailianDecision || !strings.Contains(d.Error, "workspace") {
t.Fatalf("probe with no workspace: %+v", d)
}
}