diff --git a/internal/cli/model_cli.go b/internal/cli/model_cli.go index a1b76ccad..8f8a53bd2 100644 --- a/internal/cli/model_cli.go +++ b/internal/cli/model_cli.go @@ -1,6 +1,7 @@ package cli import ( + "cmp" "fmt" "maps" "slices" @@ -325,6 +326,9 @@ func modelEfforts(args []string) error { } else { fmt.Println(muted.Render("· all offered")) } + if p.KnownBorrowed(model) { + fmt.Println(faint.Render(borrowedLine(p, model))) + } return nil } var levels []string @@ -335,9 +339,28 @@ func modelEfforts(args []string) error { } } } - if err := provider.SetModelEfforts(id, levels); err != nil { + changed, err := provider.ChangeModelEfforts(id, levels) + if err != nil { return err } + if !changed { + // nothing saved: say so, and why, rather than a tick (@czhhcz on + // X took "✓ offers every level it has" for a set that took) + switch kept, narrowed := p.ModelEfforts()[model]; { + case given && len(all) > 0: + fmt.Println(muted.Render(id+" already offers "+strings.Join(all, ", ")), faint.Render("· nothing changed")) + case given: + fmt.Println(muted.Render(id+" has no reasoning levels given"), faint.Render("· nothing changed")) + case narrowed: + fmt.Println(muted.Render(id+" already offers only "+strings.Join(kept, ", ")), faint.Render("· nothing changed")) + default: + fmt.Println(muted.Render(id+" already offers every level it has, "+strings.Join(all, ", ")), faint.Render("· nothing changed")) + if p.KnownBorrowed(model) { + fmt.Println(faint.Render(borrowedLine(p, model))) + } + } + return nil + } if given { if all = p.Efforts(model); len(all) > 0 { fmt.Println(green.Render("✓"), id, muted.Render("offers"), bold.Render(strings.Join(all, ", "))) @@ -354,6 +377,15 @@ func modelEfforts(args []string) error { return nil } +// borrowedLine says where a model's levels come from when its provider's +// list names none for it (Provider.KnownBorrowed): a relay's /v1/models +// with reasoning_levels: [] listed five for a Claude model, and the user +// had no way to tell magpie sends them all the same (@czhhcz on X). +func borrowedLine(p *provider.Provider, model string) string { + return " · " + cmp.Or(p.Name, p.ID) + "'s list names no reasoning levels for " + model + + ": these are the model's own from models.dev, which magpie fits requests to · off sends it no reasoning field" +} + // modelPrice shows what a provider's model is counted at, or sets what the // user says it costs in USD per million tokens. All four parts are given: a // price is a cost report, and one missing a part would understate it. diff --git a/internal/cli/model_efforts_borrowed_cli_test.go b/internal/cli/model_efforts_borrowed_cli_test.go new file mode 100644 index 000000000..0f60d748f --- /dev/null +++ b/internal/cli/model_efforts_borrowed_cli_test.go @@ -0,0 +1,91 @@ +package cli + +import ( + "context" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "slices" + "strings" + "testing" + + "github.com/yetone/magpie/internal/catalog" + "github.com/yetone/magpie/internal/provider" + "github.com/yetone/magpie/internal/settings" +) + +// @czhhcz on X: a relay's /v1/models listed a Claude model with +// reasoning_levels: [], `magpie model efforts` showed five levels for it +// with nothing saying where they came from, and setting all five printed a +// tick while nothing was saved. The listing says the levels are the +// model's own from models.dev, the relay's list naming none, and a set +// that changes nothing says so. +func TestModelEffortsOfARelayThatListsNone(t *testing.T) { + groupsHome(t) + catalog.Changed = nil // no agent's files are rewritten here + os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o755) + if err := os.WriteFile(catalog.CachePath(), []byte(`{"anthropic":{"id":"anthropic","models":{ + "claude-opus-5-5":{"id":"claude-opus-5-5","reasoning":true,"reasoning_options":[{"type":"effort","values":["low","medium","high","xhigh","max"]}]}}}}`), 0o644); err != nil { + t.Fatal(err) + } + catalog.Reset() + t.Cleanup(catalog.Reset) + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + w.Write([]byte(`{"data":[{"id":"claude-opus-5-5","object":"model","reasoning_levels":[],"supported_reasoning_levels":[]},{"id":"m","object":"model"}]}`)) + })) + defer srv.Close() + if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Key: "k", Anthropic: srv.URL}); err != nil { + t.Fatal(err) + } + p := mustFind(t, "relay") + if _, err := p.Fetch(context.Background()); err != nil { + t.Fatal(err) + } + all := []string{"low", "medium", "high", "xhigh", "max"} + if p := mustFind(t, "relay"); !slices.Equal(p.Efforts("claude-opus-5-5"), all) || !p.KnownBorrowed("claude-opus-5-5") || p.KnownBorrowed("m") { + t.Fatal(p.Efforts("claude-opus-5-5"), p.KnownBorrowed("claude-opus-5-5"), p.KnownBorrowed("m")) + } + ref := "relay/claude-opus-5-5" + out := saidArgs(t, "efforts", ref) + if !strings.Contains(out, "low medium high xhigh max") || !strings.Contains(out, "Relay's list names no reasoning levels for claude-opus-5-5") { + t.Fatalf("listing:\n%s", out) + } + // every level it has: nothing to save, and said so, not a tick + out = saidArgs(t, "efforts", ref, "low,medium,high,xhigh,max") + if strings.Contains(out, "✓") || !strings.Contains(out, "nothing changed") || !strings.Contains(out, "Relay's list names no reasoning levels") { + t.Fatalf("all of them again:\n%s", out) + } + if es := settings.Load().ModelEfforts; len(es) != 0 { + t.Fatal(es) + } + // and through magpie model spec, which said "✓ saved" over it + out = saidArgs(t, "spec", ref, "--reasoning", "low-max") + if strings.Contains(out, "✓") || !strings.Contains(out, "nothing changed") { + t.Fatalf("spec, all of them again:\n%s", out) + } + if out = saidArgs(t, "spec", ref, "--reasoning", "high-max"); !strings.Contains(out, "✓ saved") { + t.Fatalf("spec, high-max:\n%s", out) + } + if out = saidArgs(t, "efforts", ref, "--reset"); !strings.Contains(out, "✓") { + t.Fatalf("reset:\n%s", out) + } + // one of them is saved, and saying it again changes nothing + if out = saidArgs(t, "efforts", ref, "max"); !strings.Contains(out, "✓") { + t.Fatalf("max:\n%s", out) + } + if es := settings.Load().ModelEfforts[ref]; !slices.Equal(es, []string{"max"}) { + t.Fatal(settings.Load().ModelEfforts) + } + if out = saidArgs(t, "efforts", ref, "max"); strings.Contains(out, "✓") || !strings.Contains(out, "already offers only max") { + t.Fatalf("max again:\n%s", out) + } + // a model whose list names none and models.dev doesn't know: given + if out = saidArgs(t, "efforts", "relay/m"); strings.Contains(out, "list names no reasoning levels") { + t.Fatalf("unknown model:\n%s", out) + } + if out = saidArgs(t, "efforts", "relay/m", "--reset"); strings.Contains(out, "✓") || !strings.Contains(out, "nothing changed") { + t.Fatalf("reset of nothing:\n%s", out) + } +} diff --git a/internal/cli/model_spec_cli.go b/internal/cli/model_spec_cli.go index 4df90ac8a..9592e649c 100644 --- a/internal/cli/model_spec_cli.go +++ b/internal/cli/model_spec_cli.go @@ -103,10 +103,17 @@ func modelSpec(args []string) error { if c.Empty() { return showSpec(ref) } - if err := provider.SetModelSpec(ref, c); err != nil { + changed, err := provider.ChangeModelSpec(ref, c) + if err != nil { return err } - fmt.Println(green.Render("✓"), muted.Render("saved")) + if changed { + fmt.Println(green.Render("✓"), muted.Render("saved")) + } else { + // it goes by these already: a tick over nothing saved read as a + // set that took (@czhhcz on X) + fmt.Println(muted.Render("nothing changed"), faint.Render("· it goes by these already")) + } return showSpec(ref) } diff --git a/internal/gateway/relay_listed_no_levels_test.go b/internal/gateway/relay_listed_no_levels_test.go new file mode 100644 index 000000000..44ba4d74e --- /dev/null +++ b/internal/gateway/relay_listed_no_levels_test.go @@ -0,0 +1,55 @@ +package gateway + +import ( + "net/http/httptest" + "os" + "path/filepath" + "slices" + "testing" + + "github.com/yetone/magpie/internal/catalog" + "github.com/yetone/magpie/internal/provider" +) + +// @czhhcz on X: a relay's /v1/models lists a Claude model with +// reasoning_levels: [], and `magpie model efforts` offers it the model's +// own five from models.dev. Those are what a request is fitted to: Claude +// Code's max and xhigh reach the relay as asked, with nothing for the user +// to set. +func TestRelayListingNoLevelsStillGetsTheAgentsEffort(t *testing.T) { + fresh(t) + os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o755) + if err := os.WriteFile(catalog.CachePath(), []byte(`{"anthropic":{"id":"anthropic","models":{ + "claude-opus-5-5":{"id":"claude-opus-5-5","reasoning":true,"reasoning_options":[{"type":"effort","values":["low","medium","high","xhigh","max"]}]}}}}`), 0o644); err != nil { + t.Fatal(err) + } + catalog.Reset() + t.Cleanup(catalog.Reset) + up := &adaptiveVendor{} + srv := httptest.NewServer(up) + t.Cleanup(srv.Close) + if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Key: "k", Anthropic: srv.URL}); err != nil { + t.Fatal(err) + } + // the relay's list as it is read: the model, levels none + if err := catalog.SaveLive("relay", srv.URL, []catalog.Model{{ID: "claude-opus-5-5"}}); err != nil { + t.Fatal(err) + } + p, err := provider.Find("relay") + if err != nil { + t.Fatal(err) + } + if es := p.Efforts("claude-opus-5-5"); !slices.Equal(es, []string{"low", "medium", "high", "xhigh", "max"}) || !p.KnownBorrowed("claude-opus-5-5") { + t.Fatal(es) + } + for _, e := range []string{"max", "xhigh", "low"} { + code, body := post(t, "/v1/messages", `{"model":"relay/claude-opus-5-5","max_tokens":32000,"thinking":{"type":"adaptive"},"output_config":{"effort":"`+e+`"},"messages":[{"role":"user","content":"hi"}]}`) + if code != 200 { + t.Fatalf("%s: status %d: %s", e, code, body) + } + oc, _ := up.last()["output_config"].(map[string]any) + if got, _ := oc["effort"].(string); got != e { + t.Errorf("asked %s, the relay got %v", e, up.last()["output_config"]) + } + } +} diff --git a/internal/provider/modelprefs.go b/internal/provider/modelprefs.go index b8441a213..3b7b10236 100644 --- a/internal/provider/modelprefs.go +++ b/internal/provider/modelprefs.go @@ -239,7 +239,17 @@ var Levels = []string{"none", "minimal", "low", "medium", "high", "xhigh", "max" // levels aren't known, any of Levels, which it is given; none takes them // away. func SetModelEfforts(ref string, efforts []string) error { - return touchedIf(setModelEfforts(ref, efforts)) + _, err := ChangeModelEfforts(ref, efforts) + return err +} + +// ChangeModelEfforts is SetModelEfforts, saying whether anything changed: +// levels already kept, or every level a model offers already, change +// nothing, which the caller says rather than a tick over nothing saved +// (@czhhcz on X). +func ChangeModelEfforts(ref string, efforts []string) (bool, error) { + changed, err := setModelEfforts(ref, efforts) + return changed, touchedIf(changed, err) } func setModelEfforts(ref string, efforts []string) (bool, error) { diff --git a/internal/provider/models.go b/internal/provider/models.go index 9af0587cf..5efe7df64 100644 --- a/internal/provider/models.go +++ b/internal/provider/models.go @@ -2208,6 +2208,31 @@ func (p Provider) Known(model string) []string { return p.knownElsewhere(model) } +// KnownBorrowed reports whether the model's levels (Known) are not its +// provider's word but borrowed for its id (borrowedEfforts): its list +// names none for it — a relay's /v1/models with reasoning_levels: [] or +// no levels at all — and models.dev has none for that vendor, so it is +// asked at the levels its maker, or the others serving it, give it +// (@czhhcz on X: `magpie model efforts` listed five for a relay's Claude +// model whose list said none, and didn't say where they came from). +func (p Provider) KnownBorrowed(model string) bool { + for _, m := range p.Available() { + if m.ID == model { + if m.EffortsBorrowed { + return len(m.Efforts) > 0 + } + return len(m.Efforts) == 0 && m.Provider == "" && len(borrowedEfforts(m.ID)) > 0 + } + } + if p.ID == "devin" && devinEffortOf(model) != "" { + return false + } + if _, ok := catalog.ListedBy(p.Catalogs(), model); ok { + return false + } + return len(borrowedEfforts(model)) > 0 +} + // knownElsewhere are the reasoning levels of a model the provider's list // doesn't have — one of the vendor's own its list leaves out (a preview), // or typed in: the vendor's word on it, before the others'. diff --git a/internal/provider/modelspec.go b/internal/provider/modelspec.go index 0fd165755..c063ed01b 100644 --- a/internal/provider/modelspec.go +++ b/internal/provider/modelspec.go @@ -117,6 +117,21 @@ func (p Provider) ParseLevels(model, v string) (levels []string, off bool, err e // take it). Every part is checked before anything is written, so a bad // one writes nothing; the agents are told once, after them all. func SetModelSpec(ref string, c SpecChange) error { + _, err := ChangeModelSpec(ref, c) + return err +} + +// ChangeModelSpec is SetModelSpec, saying whether anything changed: a +// spec the model goes by already (every reasoning level it has, a window +// it was given) saves nothing, which the caller says rather than "saved" +// (@czhhcz on X). +func ChangeModelSpec(ref string, c SpecChange) (bool, error) { + var did bool + err := setModelSpec(ref, c, &did) + return did, err +} + +func setModelSpec(ref string, c SpecChange, did *bool) error { if c.Empty() { return errors.New("say what to change: --context, --output, --vision or --reasoning") } @@ -210,6 +225,7 @@ func SetModelSpec(ref string, c SpecChange) error { return touchedIf(changed, err) } } + *did = changed || saved return touchedIf(changed && !saved, nil) }