Files
magpie/internal/gateway/remote_magpie_tier_test.go
T
yetone fcae505ff7 gateway, codexcat: Codex is offered Fast on a remote magpie's model where that magpie offers its own Codex Fast, and the tier asked reaches it (#1234, PennyVibe)
remote-magpie/codex/gpt-6.1-sol had service_tiers [] in the local
/v1/codex/models while the remote's own codex/gpt-6.1-sol had Fast: the
local catalog only gave tiers to a provider added by its address
(OwnTier, Preset == ""), and the gateway sent service_tier only to one.

Now a remote magpie's /v1/models (asked with X-Magpie-Drawers) tells each
model's service_tiers as its own Codex catalog offers them
(codexcat.ServiceTiers over the same CodexCatalog/Entries as its
/v1/codex/models). The local keeps the ids (catalog.Model.Tiers ->
Entry.Tiers), codexListed hands them on for the remote's models, and
codexcat.Entries offers them with Codex's own names. The gateway sends the
tier Codex asked for to a remote magpie as it was asked (OwnTier, still
dropped and remembered if refused); the remote serves it as its own
Codex's turn. A remote from before this sends no tiers: unchanged.

Run:
- TestRemoteMagpieOffersItsTiers (gateway, the gateway as both computers
  over HTTP): fails without the codexListed change with
  `office/relay/gpt-6.1-sol offered "" ..., want "priority:Fast "`, and
  without the gateway change with `tier sent to the remote magpie [(none)],
  by it to its vendor [(none)]; want priority, priority`.
- TestServiceTiersGoOnToAnotherMagpie (codexcat): a ChatGPT account's
  model with Codex's own entry tells priority+ultrafast, a relay's GPT
  Fast, glm none; fails when either the own-entry branch of ServiceTiers
  or the namedTiers branch of Entries is taken out.
- gofmt -l clean; go vet -tags nogui for darwin, linux, windows on
  gateway, provider, codexcat, catalog, agent; go test -tags nogui of those
  five packages under a temp HOME: ok.
Not tried with a real ChatGPT account behind a real remote magpie.
2026-10-10 17:55:14 +08:00

124 lines
4.3 KiB
Go

package gateway
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// Codex is offered Fast on another magpie's model where that magpie offers
// its own Codex Fast on it, and a turn in Fast mode goes on to that magpie
// with service_tier priority, which it sends its vendor as its own Codex's
// turn would be (PennyVibe, #1234: remote-magpie/codex/gpt-6.1-sol had no
// tiers while sub2api/gpt-6.1-sol had Fast). A model it offers no tier on
// is offered none here either. Here the gateway is both computers', as in
// TestRemoteMagpieNativeAPI: "office" is it, reached over HTTP, and its
// vendor is a relay the user added by its address.
func TestRemoteMagpieOffersItsTiers(t *testing.T) {
var mu sync.Mutex
var vendor, hops []string // service_tier as the vendor, and the remote magpie, were sent it
tierOf := func(b []byte) string {
var v struct {
Tier *string `json:"service_tier"`
}
json.Unmarshal(b, &v)
if v.Tier == nil {
return "(none)"
}
return *v.Tier
}
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, _ := io.ReadAll(r.Body)
mu.Lock()
vendor = append(vendor, tierOf(b))
mu.Unlock()
w.Header().Set("Content-Type", "text/event-stream")
io.WriteString(w, sse(
`data: {"type":"response.created","response":{"id":"resp_1","model":"gpt-6.1-sol"}}`,
`data: {"type":"response.output_item.added","output_index":0,"item":{"type":"message","id":"m1","role":"assistant","content":[]}}`,
`data: {"type":"response.output_text.delta","output_index":0,"content_index":0,"delta":"hello"}`,
`data: {"type":"response.output_item.done","output_index":0,"item":{"type":"message","id":"m1","role":"assistant","content":[{"type":"output_text","text":"hello"}]}}`,
`data: {"type":"response.completed","response":{"id":"resp_1","model":"gpt-6.1-sol","usage":{"input_tokens":5,"output_tokens":2}}}`))
}))
t.Cleanup(up.Close)
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Key: "k", Models: []string{"gpt-6.1-sol", "glm-5"}, Responses: up.URL + "/v1"}); err != nil {
t.Fatal(err)
}
h := New().Handler()
remote := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
b, _ := io.ReadAll(r.Body)
r.Body = io.NopCloser(strings.NewReader(string(b)))
mu.Lock()
hops = append(hops, tierOf(b))
mu.Unlock()
}
h.ServeHTTP(w, r)
}))
t.Cleanup(remote.Close)
id, err := provider.Add(provider.Provider{ID: "office", Name: "Office", Preset: provider.RemoteMagpiePreset, Chat: strings.TrimPrefix(remote.URL, "http://")})
if err != nil {
t.Fatal(err)
}
office, err := provider.Find(id)
if err != nil {
t.Fatal(err)
}
if _, err := office.Fetch(context.Background()); err != nil {
t.Fatal(err)
}
// the catalog Codex here reads
rec := httptest.NewRecorder()
h.ServeHTTP(rec, httptest.NewRequest("GET", CodexCatalogPath, nil))
var cat struct {
Models []struct {
Slug string `json:"slug"`
Tiers []struct {
ID string `json:"id"`
Name string `json:"name"`
} `json:"service_tiers"`
} `json:"models"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &cat); err != nil {
t.Fatalf("%v: %s", err, rec.Body)
}
offered := map[string]string{}
for _, m := range cat.Models {
s := ""
for _, x := range m.Tiers {
s += x.ID + ":" + x.Name + " "
}
offered[m.Slug] = s
}
for slug, want := range map[string]string{
"relay/gpt-6.1-sol": "priority:Fast ", "office/relay/gpt-6.1-sol": "priority:Fast ",
"relay/glm-5": "", "office/relay/glm-5": "",
} {
if got, ok := offered[slug]; !ok || got != want {
t.Errorf("%s offered %q (listed %v), want %q", slug, got, ok, want)
}
}
// a turn in Fast mode, as Codex sends it
ask := strings.Replace(codexFastAsk("priority"), `"model":"m1"`, `"model":"office/relay/gpt-6.1-sol"`, 1)
code, body := post(t, "/v1/responses", ask)
if code != 200 {
t.Fatalf("%d: %s", code, body)
}
mu.Lock()
defer mu.Unlock()
if len(hops) != 1 || hops[0] != "priority" || len(vendor) != 1 || vendor[0] != "priority" {
t.Errorf("tier sent to the remote magpie %v, by it to its vendor %v; want priority, priority", hops, vendor)
}
}