Files
magpie/internal/provider/cpa_efforts_test.go
T
yetone ea33a78a05 provider: a proxy's level- or speed-named model takes its model's levels (nianshou5)
nianshou5 on Discord added their own CLIProxyAPI (CPA) as a custom
provider, and its models offered no reasoning effort in the ChatGPT app
while a plugin's did. CPA's /v1/models gives id, object, created and
owned_by alone (sdk/api/handlers/openai OpenAIModels strips its
registry's thinking levels), so every level comes from magpie borrowing
the model's. That works for an id models.dev knows (gpt-5.5, claude-*,
gemini-*, deepseek-*), but CPA's Antigravity and xAI channels name a
model for the level or speed they serve it at: gemini-3.8-flash-high,
claude-opus-5-5-high, claude-opus-4-6-thinking, gpt-oss-120b-medium,
grok-3-mini-fast. No catalog lists those names, so they had no levels,
and Codex, the ChatGPT app, /v1/models and the gateway's effort fitting
all read none. CPA's own registry gives those models levels or a
thinking budget it fits an effort to.

borrowedEfforts, which is reached only for a model its provider gave
no word on, now tries the id without a trailing
-minimal/-low/-medium/-high/-xhigh/-max/-thinking/-fast
(catalog.Unsuffixed) after every other match failed. A vendor whose own
list names a model so keeps its own word (effortsOf takes the
provider's), and a variant whose base magpie doesn't know
(grok-4.7-build-fast) still takes none. Every list reads Entry.Efforts
or Provider.Known, so Codex CLI's magpie-models.json, the Codex and
ChatGPT apps' /backend-api/codex/models, /v1/codex/models and /v1/models'
reasoning_levels all change together.

The reporter's one visible id, whatstoken/deepseek-v4.1-flash-1017,
already borrowed low,high,max on main against this machine's real
models.dev cache; their other two CPA ids are not known yet (asked on
Discord). So this fixes the class of CPA ids that had none; it may not
be their case.

TestCPAVariantModelsBorrowLevels serves CPA's /v1/models bytes from an
httptest server, fetches them with catalog.Fetch, and checks
Entry.Efforts, Provider.Efforts and the Codex list's
supported_reasoning_levels. It is red with the fallback turned off by
hand (three ids with "" in each list) and green with it.

Run at the pushed head (rebased on origin/main 0b72ccbef):
- go build ./...; GOOS=linux and GOOS=windows go vet ./internal/catalog
  ./internal/provider: ok
- go test -count=1 ./internal/catalog ./internal/codexcat, and
  -run 'CPA|Effort|Level|Borrow|Kin|Host' ./internal/provider: ok
Run at the same change on 7d67910f7, before the rebase:
- gofmt -l internal/: clean
- go test -count=1 ./internal/catalog ./internal/provider
  ./internal/codexcat: ok
- go test -run 'Effort|Level|Reason|Think|Codex.*Catalog|ModelsList'
  ./internal/agent ./internal/gateway: ok
- Against a copy of the real models.dev cache, every id in CPA's
  internal/registry/models/models.json as a custom provider: the five
  suffixed ids above now get their base's levels.
Not run: the full suite (machine at load ~190 with eight suites queued;
not holding the push). Not tried: a real CPA instance or the real
ChatGPT app.
2026-10-25 03:51:20 +08:00

134 lines
5.1 KiB
Go

package provider
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/codexcat"
)
// nianshou5 on Discord: a CLIProxyAPI (CPA) the user hosts, added as a
// custom provider, offered no reasoning effort in the ChatGPT app. CPA's
// /v1/models gives id, object, created and owned_by alone, and its
// Antigravity and xAI channels name a model for the level or speed they
// serve it at (gemini-3.8-flash-high, claude-opus-5-5-high,
// grok-3-mini-fast), which models.dev lists by no such name. The model's
// own levels are borrowed, as for any custom provider's model, and reach
// every list Codex and the ChatGPT app are handed; one whose base magpie
// doesn't know still takes none, and a vendor's own word stays its own.
func TestCPAVariantModelsBorrowLevels(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"))
if err := os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(catalog.CachePath(), []byte(`{
"google": {"models": {
"gemini-3.8-flash": {"id":"gemini-3.8-flash","reasoning":true,"reasoning_options":[{"type":"effort","values":["low","medium","high"]}]}}},
"anthropic": {"models": {
"claude-opus-5-5": {"id":"claude-opus-5-5","reasoning":true,"reasoning_options":[{"type":"effort","values":["low","medium","high","xhigh","max"]}]}}},
"xai": {"models": {
"grok-3-mini": {"id":"grok-3-mini","reasoning":true,"reasoning_options":[{"type":"effort","values":["low","medium","high"]}]}}},
"acme": {"models": {
"acme-1-high": {"id":"acme-1-high","reasoning":true},
"acme-1": {"id":"acme-1","reasoning":true,"reasoning_options":[{"type":"effort","values":["low","high"]}]}}}
}`), 0o644); err != nil {
t.Fatal(err)
}
catalog.Reset()
t.Cleanup(catalog.Reset)
// CPA's /v1/models as its OpenAIModels handler writes it (router-for-
// me/CLIProxyAPI sdk/api/handlers/openai): the four fields, nothing of
// the thinking levels its registry has
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Write([]byte(`{"data":[` +
`{"id":"gemini-3.8-flash-high","object":"model","owned_by":"antigravity"},` +
`{"id":"claude-opus-5-5-high","object":"model","owned_by":"antigravity"},` +
`{"id":"grok-3-mini-fast","object":"model","created":1740960000,"owned_by":"xai"},` +
`{"id":"gemini-3.8-flash","object":"model","owned_by":"google"},` +
`{"id":"grok-4.7-build-fast","object":"model","created":1776211200,"owned_by":"xai"}` +
`],"object":"list"}`))
}))
defer srv.Close()
live, err := catalog.Fetch(context.Background(), srv.URL+"/v1", "k", false, nil)
if err != nil || len(live) != 5 {
t.Fatalf("fetch: %v %v", live, err)
}
ids := []string{"gemini-3.8-flash-high", "claude-opus-5-5-high", "grok-3-mini-fast", "gemini-3.8-flash", "grok-4.7-build-fast"}
if err := Save(Provider{ID: "cpa", Name: "CPA", Chat: srv.URL + "/v1", Key: "k", Models: ids}); err != nil {
t.Fatal(err)
}
if err := catalog.SaveLive("cpa", srv.URL+"/v1", live); err != nil {
t.Fatal(err)
}
// acme lists acme-1-high itself, with a thinking switch alone
if err := Save(Provider{ID: "acme", Name: "Acme", Catalog: "acme", Chat: "https://acme.test/v1", Key: "k", Models: []string{"acme-1-high"}}); err != nil {
t.Fatal(err)
}
want := map[string]string{
"cpa/gemini-3.8-flash-high": "low,medium,high",
"cpa/claude-opus-5-5-high": "low,medium,high,xhigh,max",
"cpa/grok-3-mini-fast": "low,medium,high",
"cpa/gemini-3.8-flash": "low,medium,high",
"cpa/grok-4.7-build-fast": "",
"acme/acme-1-high": "",
}
got := map[string]string{}
for _, e := range Catalog() {
got[e.ID] = strings.Join(e.Efforts, ",")
}
for id, w := range want {
if g, ok := got[id]; !ok || g != w {
t.Errorf("%s: efforts %q (listed %v), want %q", id, g, ok, w)
}
}
p, err := Find("cpa")
if err != nil {
t.Fatal(err)
}
if g := strings.Join(p.Efforts("gemini-3.8-flash-high"), ","); g != "low,medium,high" {
t.Errorf("the gateway fits gemini-3.8-flash-high to %q", g)
}
// what the Codex app and the ChatGPT app beside it are handed
body, err := json.Marshal(codexcat.Entries(CodexListed(), 0))
if err != nil {
t.Fatal(err)
}
var entries []struct {
Slug string `json:"slug"`
Levels []struct {
Effort string `json:"effort"`
} `json:"supported_reasoning_levels"`
}
if err := json.Unmarshal(body, &entries); err != nil {
t.Fatal(err)
}
listed := map[string]string{}
for _, e := range entries {
var ls []string
for _, l := range e.Levels {
ls = append(ls, l.Effort)
}
listed[e.Slug] = strings.Join(ls, ",")
}
for _, id := range []string{"cpa/gemini-3.8-flash-high", "cpa/claude-opus-5-5-high", "cpa/grok-3-mini-fast"} {
if listed[id] != want[id] {
t.Errorf("Codex list: %s offers %q, want %q (list %v)", id, listed[id], want[id], listed)
}
}
}