diff --git a/internal/agent/agents.go b/internal/agent/agents.go index 8297e166e..f955f847b 100644 --- a/internal/agent/agents.go +++ b/internal/agent/agents.go @@ -453,8 +453,12 @@ func piBlockKept(path string, value func() any) func() any { // api and baseUrl); without it the model is left on the provider's // openai-completions (Pencil, pencil.go). func piModelJSON(m catalog.Model, gw string, native bool) map[string]any { - // reasoning lets Pi offer its thinking levels for the model - e := map[string]any{"id": m.ID, "name": m.Name, "reasoning": len(m.Efforts) > 0} + // reasoning lets Pi offer its thinking levels for the model: as + // /v1/models says it (provider.Entry.Thinks), so one that thinks with + // no levels magpie knows (Trae CN's kimi-k3-max, #2380) is offered + // Pi's own off to high, which the gateway fits to the model, where + // it had off alone + e := map[string]any{"id": m.ID, "name": m.Name, "reasoning": m.Reasoning || len(m.Efforts) > 0} // each model is asked on the API its provider speaks natively, // so the gateway relays what Pi sent as it is instead of // translating Chat. One served on OpenAI's Responses API alone, diff --git a/internal/agent/commandcode.go b/internal/agent/commandcode.go index f258f85d5..e521ac8b5 100644 --- a/internal/agent/commandcode.go +++ b/internal/agent/commandcode.go @@ -157,9 +157,16 @@ func ccProviderJSONAt(v1 string) any { efforts = append(efforts, x) } } - if len(efforts) > 0 { + // a model that thinks with no levels magpie knows (Trae CN's + // kimi-k3-max, #2380) is marked reasoning alone, which Command + // Code takes for low, medium and high (its BYOK reference); the + // gateway fits them to the model + switch { + case len(efforts) > 0: e["reasoning"] = true e["reasoningEfforts"] = efforts + case len(m.Efforts) == 0 && m.Reasoning: + e["reasoning"] = true } ms[m.ID] = e } diff --git a/internal/agent/opencode_variants_test.go b/internal/agent/opencode_variants_test.go index 8ffaf4ffb..590a4b6a0 100644 --- a/internal/agent/opencode_variants_test.go +++ b/internal/agent/opencode_variants_test.go @@ -215,7 +215,9 @@ func TestOpenCodeWritesVariants(t *testing.T) { // thinking switch alone, gets no variants in opencode.json — under Xiaomi's // preset or a relay of the user's alike — and no thinking levels in Pi: // OpenCode offered it the resellers' levels up to max, which Xiaomi turns -// away with a 400 (#214). A model no maker lists keeps the resellers'. +// away with a 400 (#214). A model no maker lists keeps the resellers'. Pi is +// told it reasons, as /v1/models says (#2380), with no map, so Pi offers +// its own off to high and never xhigh or max. func TestOpenCodeNoResellerLevelsForSwitchModel(t *testing.T) { home := t.TempDir() t.Setenv("HOME", home) @@ -284,7 +286,7 @@ func TestOpenCodeNoResellerLevelsForSwitchModel(t *testing.T) { switch m.ID { case "xiaomi/mimo-v2.6-flash", "relay/mimo-v2.6-flash": seen++ - if m.Reasoning || m.Levels != nil { + if !m.Reasoning || m.Levels != nil { t.Errorf("pi %s: reasoning %v, levels %v", m.ID, m.Reasoning, m.Levels) } case "relay/open-model": diff --git a/internal/agent/route.go b/internal/agent/route.go index fa91b9d67..d62d9b3b5 100644 --- a/internal/agent/route.go +++ b/internal/agent/route.go @@ -150,7 +150,7 @@ func magpieModels(agent string) []catalog.Model { return ms } for i, e := range shown { - m := catalog.Model{ID: e.ID, Name: labels[i], Provider: firstOf(e.Provider.Catalogs()), Efforts: e.Efforts, Images: e.Images || seen, ImageInput: e.ImageInput, Context: e.Context, Output: e.Output, OutputSet: e.OutputSet, AgentsV2: e.AgentsV2, Reasoning: e.Reasoning} + m := catalog.Model{ID: e.ID, Name: labels[i], Provider: firstOf(e.Provider.Catalogs()), Efforts: e.Efforts, Images: e.Images || seen, ImageInput: e.ImageInput, Context: e.Context, Output: e.Output, OutputSet: e.OutputSet, AgentsV2: e.AgentsV2, Reasoning: e.Thinks()} // where Codex and Claude Code compact it (#876): a threshold the // user set on the model or on its provider, else a window they // typed whole (#1809), else the one for every model, which codexcat diff --git a/internal/agent/thinks_export_test.go b/internal/agent/thinks_export_test.go new file mode 100644 index 000000000..afac4059f --- /dev/null +++ b/internal/agent/thinks_export_test.go @@ -0,0 +1,57 @@ +package agent + +import ( + "os" + "reflect" + "testing" + + "github.com/yetone/magpie/internal/catalog" + "github.com/yetone/magpie/internal/provider" +) + +// #2380: Trae CN's kimi-k3-max thinks with no levels magpie knows, and +// /v1/models said reasoning: true for it, but Pi's models.json was written +// reasoning: false (the export read the levels alone), so Pi offered off +// alone and `magpie pi effort high` was clamped to off. Every export that +// writes a reasoning flag says what /v1/models says. +func TestThinkingWithoutLevelsReachesTheExports(t *testing.T) { + syncHome(t) + if err := os.WriteFile(catalog.CachePath(), []byte(`{ + "moonshotai": {"models": {"kimi-k3-max": {"id":"kimi-k3-max","name":"Kimi-K3-Max","reasoning":true,"limit":{"context":262144}}}} + }`), 0o644); err != nil { + t.Fatal(err) + } + catalog.Reset() + if err := provider.Save(provider.Provider{ID: "trae-cn", Name: "Trae CN", Key: "k", Chat: "http://127.0.0.1:1/v1", + Catalog: "moonshotai", Models: []string{"kimi-k3-max"}}); err != nil { + t.Fatal(err) + } + const id = "trae-cn/kimi-k3-max" + + shown, _ := provider.CatalogFor("pi") + var entry *provider.Entry + for i := range shown { + if shown[i].ID == id { + entry = &shown[i] + } + } + if entry == nil || !entry.Thinks() || len(entry.Efforts) != 0 { + t.Fatalf("the fixture is a model that thinks with no levels, as /v1/models says it: %+v", entry) + } + + if got := piOffered("pi", "magpie/"+id); !reflect.DeepEqual(got, []string{"off", "minimal", "low", "medium", "high"}) { + t.Fatalf("Pi offers %v for %s, want its own off to high", got, id) + } + if got := piClamp("high", piOffered("pi", "magpie/"+id)); got != "high" { + t.Fatalf("magpie pi effort high runs at %q", got) + } + + ms := ccProviderJSONAt("http://127.0.0.1:1/v1").(map[string]any)["models"].(map[string]any) + cc, _ := ms[id].(map[string]any) + if cc["reasoning"] != true { + t.Fatalf("Command Code's entry: %v", cc) + } + if _, set := cc["reasoningEfforts"]; set { + t.Fatalf("Command Code was told levels magpie doesn't know: %v", cc) + } +} diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index ee458f23e..a5b2f78bf 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -818,7 +818,7 @@ func modelObject(e provider.Entry) map[string]any { levels = append(levels, reasoningLevel{Effort: effort}) } m := map[string]any{"id": e.ID, "object": "model", "type": "model", "created": 0, "created_at": "2025-01-01T00:00:00Z", - "owned_by": e.Provider.ID, "display_name": e.PlainName(), "reasoning": e.Reasoning || len(levels) > 0, "supported_reasoning_levels": levels} + "owned_by": e.Provider.ID, "display_name": e.PlainName(), "reasoning": e.Thinks(), "supported_reasoning_levels": levels} // the window, as the names clients read it by: a group's context // (magpie group set … context=) included if e.Context > 0 { diff --git a/internal/provider/modelprefs.go b/internal/provider/modelprefs.go index f77097500..7145ff945 100644 --- a/internal/provider/modelprefs.go +++ b/internal/provider/modelprefs.go @@ -733,6 +733,14 @@ func Labels(es []Entry) []string { // Discord: "· Relay A" and "· routing group" stayed under Off). func (e Entry) PlainName() string { return cmp.Or(e.Plain, e.Name) } +// Thinks is whether the model thinks, as /v1/models says it and every +// agent's list is told it: a model with levels, or one marked Reasoning +// with a thinking switch alone. An export that read the levels alone +// marked Trae CN's kimi-k3-max, which always thinks and has no levels +// magpie knows, not reasoning for Pi, and `magpie pi effort high` was +// clamped to off (#2380). +func (e Entry) Thinks() bool { return e.Reasoning || len(e.Efforts) > 0 } + // The ways the agents' lists name models (SuffixMode): every name with its // provider's after it, as by default; all but the names the user gave // models (#92); or none (#335).