diff --git a/internal/catalog/catalog.go b/internal/catalog/catalog.go index 089bb2c09..f44638080 100644 --- a/internal/catalog/catalog.go +++ b/internal/catalog/catalog.go @@ -38,6 +38,11 @@ type Model struct { // Keys, for a vendor whose keys each see models of their own, are the // keys (by fingerprint) whose list has this one; empty is every key. Keys []string `json:",omitempty"` + // EffortsBorrowed is set when Efforts are not the source's word but + // borrowed for its id (a plugin's model that thinks with no variants): + // a model listed by another id (provider's localIDs) borrows them for + // that id again. + EffortsBorrowed bool `json:"-"` // APIs, when the vendor says, are the APIs the model is served on // ("chat", "responses", "anthropic"); empty is not known. APIs []string `json:",omitempty"` diff --git a/internal/provider/localid.go b/internal/provider/localid.go index 3188e8d77..d3fb4d48e 100644 --- a/internal/provider/localid.go +++ b/internal/provider/localid.go @@ -99,6 +99,15 @@ func (l localIDs) list(ms []catalog.Model) []catalog.Model { m.Name = local } m.ID = local + // levels borrowed for the vendor's id are borrowed for the + // one it is listed by too, and for the model the user said + // that one is the same as: a plugin's qfmodel listed as + // claude-opus-5.5 has its levels (axelburks on Discord) + if m.EffortsBorrowed { + if e := borrowedEfforts(local); len(e) > 0 { + m.Efforts = e + } + } } else if own, ok := l.own[m.ID]; ok && has[own] { continue // listed by the model given its id } diff --git a/internal/provider/plugin_listas_efforts_test.go b/internal/provider/plugin_listas_efforts_test.go new file mode 100644 index 000000000..ac6a5b7d1 --- /dev/null +++ b/internal/provider/plugin_listas_efforts_test.go @@ -0,0 +1,105 @@ +package provider + +import ( + "encoding/json" + "os" + "path/filepath" + "slices" + "testing" + + "github.com/yetone/magpie/internal/catalog" + "github.com/yetone/magpie/internal/plugin" + "github.com/yetone/magpie/internal/settings" +) + +// axelburks on Discord: a plugin's model that thinks with no variants, +// set the same as and listed as claude-opus-5.5, took the price of Claude +// Opus 5.5 but showed its levels unknown with none ticked. pluginCatalog +// borrowed levels for the plugin's own id, which no catalog knows, and +// the rename to the listed id kept the empty list. The listed id, and +// the model the user said that one is the same as, lend their levels now; +// a model its plugin gives variants keeps them, and the levels the user +// kept of them narrow what is offered. +func TestPluginModelListedAsBorrowsLevels(t *testing.T) { + claudeHome(t) + t.Setenv("MAGPIE_BUN", "") + os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o755) + 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"]}],"limit":{"context":1000000},"cost":{"input":5,"output":25}}}}}`), 0o644) + catalog.Reset() + t.Cleanup(catalog.Reset) + spec := "@magpie-community/opencode-qf-auth" + write := func(name string, v any) { + t.Helper() + b, _ := json.Marshal(v) + os.MkdirAll(settings.Dir(), 0o700) + if err := os.WriteFile(filepath.Join(settings.Dir(), name), b, 0o600); err != nil { + t.Fatal(err) + } + } + write("plugins.json", map[string]any{"plugins": []map[string]any{{"spec": spec}}}) + write("plugin-auth.json", map[string]map[string]any{"qf": {"type": "oauth", "access": "a", "refresh": "r", "expires": 9999999999999}}) + plugin.UseCached([]plugin.Provider{{ID: "qf", Spec: spec, Name: "QF", + Methods: []plugin.Method{{Type: "oauth", Label: "Sign in"}}, + Accounts: []plugin.Account{{Key: "qf", Type: "oauth"}}, + Models: []plugin.Model{ + {ID: "qfmodel", Name: "qfmodel", Reasoning: true}, + {ID: "qfother", Name: "qfother", Reasoning: true}, + {ID: "qfvar", Name: "qfvar", Reasoning: true, Variants: []string{"high"}}, + }}}) + t.Cleanup(func() { plugin.UseCached(nil) }) + + str := func(s string) *string { return &s } + // the reporter's save: the same as and listed as one model at once + // (the editor's one Save), another listed by a name of the user's own + // that is the same as it, and one with variants of its own + if err := SetModelPrefs("qf", map[string]ModelPref{ + "qfmodel": {Same: str("claude-opus-5.5"), ID: str("claude-opus-5.5")}, + "qfother": {Same: str("claude-opus-5.5"), ID: str("my-opus")}, + "qfvar": {Same: str("claude-opus-5.5"), ID: str("opus-var")}, + }); err != nil { + t.Fatal(err) + } + all := []string{"low", "medium", "high", "xhigh", "max"} + p, err := Find("qf") + if err != nil { + t.Fatal(err) + } + want := map[string][]string{"claude-opus-5.5": all, "my-opus": all, "opus-var": {"high"}} + got := map[string][]string{} + for _, m := range p.Available() { + got[m.ID] = EffortsOf(m) // what the provider's editor shows + } + for id, w := range want { + if g, ok := got[id]; !ok || !slices.Equal(g, w) { + t.Errorf("%s listed with levels %v (listed: %v), want %v", id, g, ok, w) + } + // the gateway fits an effort to the same levels + if e := p.Efforts(id); !slices.Equal(e, w) { + t.Errorf("gateway's %s levels %v, want %v", id, e, w) + } + } + // the price came from the same model all along + if _, ok := EffectivePrice("qf", "claude-opus-5.5"); !ok { + t.Error("claude-opus-5.5 at qf has no price") + } + + // the levels the user keeps narrow the borrowed ones + if err := SetModelEfforts("qf/claude-opus-5.5", []string{"high", "max"}); err != nil { + t.Fatal(err) + } + p, _ = Find("qf") + var entry *Entry + for _, e := range Catalog() { + if e.ID == "qf/claude-opus-5.5" { + entry = &e + } + } + if entry == nil { + t.Fatal("no qf/claude-opus-5.5 in the catalog") + } + if !slices.Equal(entry.Efforts, []string{"high", "max"}) { + t.Errorf("kept levels: catalog offers %v, want [high max]", entry.Efforts) + } +} diff --git a/internal/provider/plugins.go b/internal/provider/plugins.go index 45511e21f..2a347eada 100644 --- a/internal/provider/plugins.go +++ b/internal/provider/plugins.go @@ -386,7 +386,7 @@ func pluginCatalog(pp plugin.Provider) []catalog.Model { // pick (laomao04_65777 on Discord); a custom provider's same id // borrows them (effortsOf) if len(c.Efforts) == 0 && (m.Reasoning || PluginServes(pp.ID) || pp.ID == "cline") { - c.Efforts = borrowedEfforts(m.ID) + c.Efforts, c.EffortsBorrowed = borrowedEfforts(m.ID), true } // OpenCode's price of a model it has none for is 0, as the // plugins give a plan's models: a price is only one above it