Files
magpie/internal/gateway/effort_test.go
T
yetone 729f6bdbdf models: a model its maker lists with a thinking switch alone takes no borrowed levels, and the gateway sends it no more than high (#214 Moody-Max: opencode 的 mimo-v2.6 Flash 你这边设置的默认推理强度是 max 导致400报错)
A model not under its provider's own models.dev vendor (a relay or provider of the user's, a live id that doesn't match, or a preview/-exp or typed id Known looks up) took the levels most listed for it across all of models.dev. For mimo-v2.6-flash those are the resellers' (llmgateway, counted once per upstream host it lists, and requesty, up to max), while Xiaomi lists it with a thinking switch alone. OpenCode was offered variants up to max, Pi and Codex the same levels, and the gateway passed max on unchanged (fitEffort keeps an effort when no levels are known), which Xiaomi turned away with a 400. The same held for Zhipu's glm-5/5.1/5v-turbo, Kimi k2.6, MiniMax M2.x, gemma-4 and others whose makers give no levels while resellers give up to max.

Now catalog.ListedBy looks a model up in given catalogs (vendor prefix, case, a ':free' or '(…)' suffix aside) and gives the levels listed there, none for a switch or no options, and skips an entry with a token budget alone (Claude 4.5), which says nothing of efforts. Known looks in the provider's own catalogs first, and every borrow (Known, effortsOf) asks the makers' catalogs (vendor presets, less Groq and Ollama Cloud, marked Hosts as they serve others' models) before the crowd. The crowd vote counts a provider once per list, so an aggregator listing a model under several hosts no longer outvotes the rest. Provider.Levelless says a model is known to have no levels, and the gateway (fitFor, on the direct and fallback paths) sends such a model no more than high; a model whose levels are simply unknown is still sent what was asked.

Tests: TestEffortsOfOneVoteAProvider and TestListedBy (catalog); TestMakerLevelsBeforeResellers (provider: Xiaomi's preset, a preview left out of the list and a relay get no levels, an unlisted model and a budget-only Claude one still borrow, Levelless); TestOpenCodeNoResellerLevelsForSwitchModel (opencode.json has empty variants for Xiaomi's and a relay's mimo, three for a reseller-only model; Pi has no reasoning or thinkingLevelMap for mimo); TestSwitchModelSentNoMoreThanHigh (gateway: mimo asked max/xhigh sent high, low as asked, an unknown model max). The agent and gateway tests fail on main. An audit of every preset against the current models.dev snapshot found the cases above. Not tested with a real Xiaomi key or a running OpenCode. go build -tags nogui (and linux, windows), go test -tags nogui ./... pass.
2026-09-29 19:41:29 +08:00

183 lines
8.1 KiB
Go

package gateway
import (
"encoding/json"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/usage"
)
func TestFitEffort(t *testing.T) {
glm := []string{"low", "high", "max"}
for _, c := range []struct {
want string
levels []string
got string
}{
{"medium", glm, "high"}, // a tie goes up
{"low", glm, "low"},
{"xhigh", glm, "max"},
{"max", []string{"low", "medium", "high"}, "high"},
{"low", []string{"none", "minimal", "medium"}, "medium"},
{"medium", nil, "medium"}, // levels not known: as asked
{"medium", []string{"none"}, "medium"},
} {
if got := fitEffort(c.want, c.levels); got != c.got {
t.Errorf("fitEffort(%q, %v) = %q, want %q", c.want, c.levels, got, c.got)
}
}
}
// Codex's effort reaches a custom OpenAI-compatible provider as
// reasoning_effort, at a level the model takes: glm-5.3-flash takes low,
// high and max, as models.dev has it from those serving it, so Codex's
// "medium" goes as "high"; a model no one gives levels for gets what was
// asked.
func TestResponsesEffortReachesChatVendor(t *testing.T) {
f := &fake{t: t, reply: sse(
`data: {"id":"c1","model":"glm-5.3-flash","choices":[{"delta":{"role":"assistant","content":"hi"}}]}`,
`data: {"id":"c1","choices":[{"delta":{},"finish_reason":"stop"}]}`,
`data: [DONE]`)}
up := setup(t, provider.Chat, f)
os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o755)
os.WriteFile(catalog.CachePath(), []byte(`{"zai":{"models":{"glm-5.3-flash":{"id":"glm-5.3-flash","reasoning_options":[{"type":"effort","values":["low","high","max"]}]}}}}`), 0o644)
catalog.Reset()
t.Cleanup(catalog.Reset)
if err := provider.Save(provider.Provider{ID: "volc", Name: "Volc", Key: "k", Chat: up.URL + "/v1", Models: []string{"glm-5.3-flash", "other"}}); err != nil {
t.Fatal(err)
}
for _, c := range []struct{ model, effort, sent string }{
{"volc/glm-5.3-flash", "medium", "high"},
{"volc/glm-5.3-flash", "max", "max"},
{"volc/glm-5.3-flash", "low", "low"},
{"volc/other", "medium", "medium"},
} {
code, body := post(t, "/v1/responses", `{"model":"`+c.model+`","stream":true,"input":"hi","reasoning":{"effort":"`+c.effort+`"}}`)
if code != 200 {
t.Fatalf("status %d: %s", code, body)
}
var got map[string]any
json.Unmarshal(f.got, &got)
if got["reasoning_effort"] != c.sent || !strings.HasSuffix(f.path, "/chat/completions") {
t.Errorf("%s at %s: sent %s", c.model, c.effort, f.got)
}
}
}
// A model its maker lists with a thinking switch alone — Xiaomi's
// mimo-v2.6-flash, served here by a relay — is sent no more than high: an
// agent asking max or xhigh of it got Xiaomi's 400 (#214). What it may take
// below that goes as asked, as does max to a model no one lists.
func TestSwitchModelSentNoMoreThanHigh(t *testing.T) {
f := &fake{t: t, reply: sse(
`data: {"id":"c1","model":"mimo-v2.6-flash","choices":[{"delta":{"role":"assistant","content":"hi"}}]}`,
`data: {"id":"c1","choices":[{"delta":{},"finish_reason":"stop"}]}`,
`data: [DONE]`)}
up := setup(t, provider.Chat, f)
os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o755)
os.WriteFile(catalog.CachePath(), []byte(`{
"xiaomi":{"models":{"mimo-v2.6-flash":{"id":"mimo-v2.6-flash","reasoning":true,"reasoning_options":[{"type":"toggle"}]}}},
"llmgateway":{"models":{"mimo-v2.6-flash":{"id":"mimo-v2.6-flash","reasoning_options":[{"type":"effort","values":["none","low","medium","high","xhigh","max"]}]}}},
"zai":{"models":{"glm-5.3-flash":{"id":"glm-5.3-flash","reasoning_options":[{"type":"effort","values":["low","high","max"]}]}}}}`), 0o644)
catalog.Reset()
t.Cleanup(catalog.Reset)
if err := provider.Save(provider.Provider{ID: "volc", Name: "Volc", Key: "k", Chat: up.URL + "/v1", Models: []string{"mimo-v2.6-flash", "glm-5.3-flash", "other"}}); err != nil {
t.Fatal(err)
}
for _, c := range []struct{ model, effort, sent string }{
{"volc/mimo-v2.6-flash", "max", "high"},
{"volc/mimo-v2.6-flash", "xhigh", "high"},
{"volc/mimo-v2.6-flash", "low", "low"},
{"volc/glm-5.3-flash", "medium", "high"},
{"volc/other", "max", "max"},
} {
code, body := post(t, "/v1/responses", `{"model":"`+c.model+`","stream":true,"input":"hi","reasoning":{"effort":"`+c.effort+`"}}`)
if code != 200 {
t.Fatalf("status %d: %s", code, body)
}
var got map[string]any
json.Unmarshal(f.got, &got)
if got["reasoning_effort"] != c.sent {
t.Errorf("%s at %s: sent %s", c.model, c.effort, f.got)
}
}
}
// Each request's route keeps the reasoning the agent asked for, and each
// try the reasoning its model was sent at, fitted to the model's levels;
// the usage keeps what the model was sent at, by session.
func TestTraceRecordsTheEffortSent(t *testing.T) {
f := &fake{t: t, reply: sse(
`data: {"id":"c1","model":"glm-5.3-flash","choices":[{"delta":{"role":"assistant","content":"hi"}}]}`,
`data: {"id":"c1","choices":[{"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":1}}`,
`data: [DONE]`)}
up := setup(t, provider.Chat, f)
os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o755)
os.WriteFile(catalog.CachePath(), []byte(`{"zai":{"models":{"glm-5.3-flash":{"id":"glm-5.3-flash","reasoning_options":[{"type":"effort","values":["low","high","max"]}]}}}}`), 0o644)
catalog.Reset()
t.Cleanup(catalog.Reset)
if err := provider.Save(provider.Provider{ID: "volc", Name: "Volc", Key: "k", Chat: up.URL + "/v1", Models: []string{"glm-5.3-flash", "other"}}); err != nil {
t.Fatal(err)
}
s := New()
for _, c := range []struct{ path, body, asked, sent string }{
{"/v1/responses", `{"model":"volc/glm-5.3-flash","stream":true,"input":"hi","reasoning":{"effort":"medium"}}`, "medium", "high"},
{"/v1/chat/completions", `{"model":"volc/other","stream":true,"messages":[{"role":"user","content":"hi"}],"reasoning_effort":"xhigh"}`, "xhigh", "xhigh"},
{"/v1/messages", `{"model":"volc/glm-5.3-flash","stream":true,"max_tokens":32000,"thinking":{"type":"enabled","budget_tokens":20000},"messages":[{"role":"user","content":"hi"}]}`, "high", "high"},
{"/v1/messages", `{"model":"volc/other","stream":true,"max_tokens":32000,"thinking":{"type":"adaptive"},"output_config":{"effort":"max"},"messages":[{"role":"user","content":"hi"}]}`, "max", "max"},
{"/v1/chat/completions", `{"model":"volc/other","stream":true,"messages":[{"role":"user","content":"hi"}]}`, "", ""},
} {
rec := httptest.NewRecorder()
req := httptest.NewRequest("POST", c.path, strings.NewReader(c.body))
req.Header.Set(SessionHeader, "sess-1")
s.Handler().ServeHTTP(rec, req)
if rec.Code != 200 {
t.Fatalf("%s: %d %s", c.body, rec.Code, rec.Body)
}
r := lastRoute(s)
if r.Effort != c.asked || len(r.Tries) != 1 || r.Tries[0].Effort != c.sent || r.Tries[0].Picked {
t.Errorf("%s: route at %q, tries %+v; want asked %q, sent %q", c.body, r.Effort, r.Tries, c.asked, c.sent)
}
}
var got []string
for _, rec := range usage.Load(time.Time{}) {
got = append(got, rec.Effort)
}
if strings.Join(got, ",") != "high,xhigh,high,max," {
t.Errorf("usage efforts %q", got)
}
vs := usage.Vias(time.Time{})[usage.AgentOf("")+"|sess-1"]
if len(vs) != 4 || vs[0].Model != "glm-5.3-flash" || vs[0].Effort != "high" || vs[0].Calls != 2 {
t.Errorf("vias %+v", vs)
}
}
// requestEffort reads the reasoning a request asks for in each API's words.
func TestRequestEffort(t *testing.T) {
for _, c := range []struct {
proto provider.Protocol
body string
want string
}{
{provider.Chat, `{"reasoning_effort":"low"}`, "low"},
{provider.Chat, `{"model":"m"}`, ""},
{provider.Responses, `{"reasoning":{"effort":"xhigh","summary":"auto"}}`, "xhigh"},
{provider.Responses, `{"reasoning":{"summary":"auto"}}`, ""},
{provider.Anthropic, `{"thinking":{"type":"adaptive"},"output_config":{"effort":"medium"}}`, "medium"},
{provider.Anthropic, `{"thinking":{"type":"enabled","budget_tokens":4096}}`, "low"},
{provider.Anthropic, `{"thinking":{"type":"disabled"},"output_config":{"effort":"high"}}`, ""},
{provider.Anthropic, `{"output_config":{"effort":"high"}}`, ""},
} {
if got := requestEffort(c.proto, []byte(c.body)); got != c.want {
t.Errorf("%s %s: %q, want %q", c.proto, c.body, got, c.want)
}
}
}