diff --git a/internal/gui/providers.go b/internal/gui/providers.go index f69b8ad31..cd81549b7 100644 --- a/internal/gui/providers.go +++ b/internal/gui/providers.go @@ -982,6 +982,9 @@ func providerInfoWith(p provider.Provider, ls provider.Listing, agents []agentUs // Discord, Windows) set := held noAPI := p.NoAPI() + // the provider's catalogs worked out once, and each model priced once + // for its list price and whether it is free + pricer := p.Pricer() // each model's provider/model ref, made once: the settings' maps and // every agent's hidden list are looked up by it var refs []string @@ -999,7 +1002,8 @@ func providerInfoWith(p provider.Provider, ls provider.Listing, agents []agentUs } window := p.WindowOf(m) j := modelJSON{ID: m.ID, Name: m.Name, Efforts: provider.EffortsOf(m), On: on, Context: window, Output: provider.OutputWithin(window, p.ReplyLimitIn(m, set)), Listed: provider.ListedWindow(m), ListedOutput: provider.OutputWithin(provider.ListedWindow(m), p.ReplyLimitIn(m, settings.Settings{})), Max: m.MaxContext, Free: m.Free, Rate: m.Rate, RateWas: m.RateWas, Images: images, ImageSet: imageSet, Own: own, Best: newest[m.ID]} - j.Zero = !m.Free && p.PricedFree(set, m.ID) + list, listed, free := pricer.Prices(set, m.ID) + j.Zero = !m.Free && free // a plugin reports a model's load by its own id, not one the user // lists it by (axelburks on Discord) ownID := p.OwnModelID(m.ID) @@ -1040,8 +1044,8 @@ func providerInfoWith(p provider.Provider, ls provider.Listing, agents []agentUs j.Price = &pr } } - if pr, ok := p.ListPrice(m.ID); ok { - j.List, j.RemoteList = &pr, p.RemotePriced(m.ID) + if listed { + j.List, j.RemoteList = &list, p.RemotePriced(m.ID) } else if pr, ok := provider.MakerPrice(m.ID); ok { j.List = &pr } diff --git a/internal/provider/models.go b/internal/provider/models.go index f050dfb86..fac78fc45 100644 --- a/internal/provider/models.go +++ b/internal/provider/models.go @@ -2012,7 +2012,14 @@ func (p Provider) PricedFree(s settings.Settings, model string) bool { if pr, ok := userPrice(s, p, model); ok { return zeroPrice(pr) } - pr, ok, from := p.listPrice(model) + c := p.priceCatsOf() + pr, ok, from := p.listPriceIn(c, model) + return p.listedFree(c, model, pr, ok, from) +} + +// listedFree is PricedFree for a model the user set no price for, given +// what listPriceIn found. +func (p Provider) listedFree(c priceCats, model string, pr catalog.Price, ok bool, from int) bool { switch { case !ok || !zeroPrice(pr): return false @@ -2024,8 +2031,15 @@ func (p Provider) PricedFree(s settings.Settings, model string) bool { // a plan's catalog (zai-coding-plan) lists what its members pay // per token, nothing, not what the model costs: only a catalog // that is no plan's says it is free - plain := slices.DeleteFunc(priceCatalogs(p.priceCatalogsOf()), func(c string) bool { return strings.Contains(c, "-plan") }) - for _, m := range p.priceNames(model) { + // copied only when a plan's is among them: every $0 model of a + // catalog was a copy on each Providers ask + isPlan := func(c string) bool { return strings.Contains(c, "-plan") } + plain := c.priced + if slices.ContainsFunc(plain, isPlan) { + plain = slices.DeleteFunc(slices.Clone(plain), isPlan) + } + var buf [4]string + for _, m := range c.priceNames(buf[:0], model) { if pr, ok := catalog.PricedBy(plain, m); ok { return zeroPrice(pr) } @@ -2040,8 +2054,24 @@ func (p Provider) PricedFree(s settings.Settings, model string) bool { // from any provider (*/model), still the user's word, so before any list // price. func userPrice(s settings.Settings, p Provider, model string) (catalog.Price, bool) { - for _, key := range [...]string{p.ID + "/" + model, p.ID + "/*", AnyPriceKey(model)} { - if m, ok := s.ModelPrices[key]; ok { + if len(s.ModelPrices) == 0 { + return catalog.Price{}, false + } + // the keys made on the stack and looked up without a copy: made as + // strings, for every model of every provider on each Providers ask, + // they were most of what pricing a model allocated + var buf [256]byte + for i := range 3 { + k := buf[:0] + switch i { + case 0: + k = append(append(append(k, p.ID...), '/'), model...) + case 1: + k = append(append(k, p.ID...), "/*"...) + default: + k = append(append(k, settings.AnyProvider...), strings.ToLower(strings.TrimSpace(model))...) + } + if m, ok := s.ModelPrices[string(k)]; ok { if pr, bad := m.Price(); bad == "" { // a Claude model's 1-hour cache write left out: 2× input, // but where Copilot bills it at the 5-minute price @@ -2206,12 +2236,55 @@ const ( pricedMaker ) +// Pricer prices a provider's models as ListPrice and PricedFree do, with +// what is the provider's alone (the catalogs it is priced from) worked +// out once: the Providers page prices every model of every provider on +// each ask, and worked out for each model, twice, it was 4 MB of what +// forty asks allocated with real accounts. +type Pricer struct { + p Provider + c priceCats +} + +// Pricer is p's Pricer. +func (p Provider) Pricer() Pricer { return Pricer{p, p.priceCatsOf()} } + +// Prices is model's ListPrice on the Pricer's provider, and its +// PricedFree under s. +func (r Pricer) Prices(s settings.Settings, model string) (list catalog.Price, listed, free bool) { + pr, ok, from := r.p.listPriceIn(r.c, model) + if up, set := userPrice(s, r.p, model); set { + free = zeroPrice(up) + } else { + free = r.p.listedFree(r.c, model, pr, ok, from) + } + return pr, ok, free +} + +// priceCats are the catalogs listPrice prices p's models from +// (priceCatalogsOf), and the same without the membership catalogs. +// kimi is p.kimiCodeMember(). +type priceCats struct { + raw, priced []string + kimi bool +} + +func (p Provider) priceCatsOf() priceCats { + raw := p.priceCatalogsOf() + return priceCats{raw, priceCatalogs(raw), p.kimiCodeMember()} +} + // listPrice is ListPrice, and where it found the price. func (p Provider) listPrice(model string) (catalog.Price, bool, int) { + return p.listPriceIn(p.priceCatsOf(), model) +} + +// listPriceIn is listPrice with p's catalogs given. +func (p Provider) listPriceIn(c priceCats, model string) (catalog.Price, bool, int) { if own := p.OwnModelID(model); own != model { // a model listed by an id the user gave it is priced as the // provider's own id of it first, then as the id it is listed by - if pr, ok, from := p.listPrice(own); ok { + if pr, ok, from := p.listPriceIn(c, own); ok { return pr, true, from } } @@ -2231,17 +2304,18 @@ func (p Provider) listPrice(model string) (catalog.Price, bool, int) { if pr, ok := p.grokFastPrice(model); ok { return pr, true, pricedOwn } - names := p.priceNames(model) - cats := p.priceCatalogsOf() - for _, m := range names { - if pr, ok := catalog.PricedBy(priceCatalogs(cats), m); ok { + // the names on the stack: made for each model priced, they were + // 1.5 MB of forty Providers asks + var buf [4]string + for _, m := range c.priceNames(buf[:0], model) { + if pr, ok := catalog.PricedBy(c.priced, m); ok { return pr, true, pricedCatalog } if pr, ok := makerListPrice(m); ok { return pr, true, pricedMaker } } - if slices.Contains(cats, "anthropic") { + if slices.Contains(c.raw, "anthropic") { // a gated Claude model models.dev prices nowhere (Claude Mythos // 5.1): Anthropic's price, as it told Claude Code if pr, ok := claudeCodePrice(model); ok { @@ -2260,10 +2334,11 @@ func (p Provider) listPrice(model string) (catalog.Price, bool, int) { return catalog.Price{}, false, pricedOwn } -// priceNames are the ids ListPrice looks model up by in a catalog. -func (p Provider) priceNames(model string) []string { - names := pricedNames(model) - if p.kimiCodeMember() { +// priceNames are the ids ListPrice looks model up by in a catalog, +// appended to dst. +func (c priceCats) priceNames(dst []string, model string) []string { + names := pricedNamesTo(dst, model) + if c.kimi { if id, ok := kimiCodeAPI[strings.ToLower(strings.TrimSpace(model))]; ok { names = append(names, id) } @@ -2952,8 +3027,11 @@ var modelWindow = regexp.MustCompile(`^([^@]+)@[0-9]+[km]$`) // grok-4.7-mini, grok-4.7-fast); a Cursor account's fast ids are priced at // Cursor's own fast rate (Provider.cursorFastPrice), and a Grok account's // grok-4.7-build-fast at twice grok-4.7's (Provider.grokFastPrice). -func pricedNames(model string) []string { - out := []string{model} +func pricedNames(model string) []string { return pricedNamesTo(nil, model) } + +// pricedNamesTo is pricedNames appended to dst. +func pricedNamesTo(dst []string, model string) []string { + out := append(dst, model) if model == "codex-auto-review" { out = append(out, "gpt-5.6-luna") } diff --git a/internal/provider/pricedfree_test.go b/internal/provider/pricedfree_test.go index 24a9f92e6..1e0ad5f87 100644 --- a/internal/provider/pricedfree_test.go +++ b/internal/provider/pricedfree_test.go @@ -2,9 +2,11 @@ package provider import ( "os" + "reflect" "testing" "github.com/yetone/magpie/internal/catalog" + "github.com/yetone/magpie/internal/filememo" "github.com/yetone/magpie/internal/settings" ) @@ -60,6 +62,7 @@ func TestPricedFreeIsTheProvidersOwnZero(t *testing.T) { if got := c.p.PricedFree(none, c.model); got != c.want { t.Errorf("%s's %s: PricedFree %v, want %v", c.p.ID, c.model, got, c.want) } + samePricer(t, c.p, none, c.model) } zero, one := 0.0, 1.0 @@ -84,5 +87,49 @@ func TestPricedFreeIsTheProvidersOwnZero(t *testing.T) { if got := c.p.PricedFree(s, c.model); got != c.want { t.Errorf("with the user's prices, %s's %s: PricedFree %v, want %v", c.p.ID, c.model, got, c.want) } + samePricer(t, c.p, s, c.model) + } +} + +// samePricer checks that the Providers page's Pricer gives model the +// price and the free mark ListPrice and PricedFree do. +func samePricer(t *testing.T, p Provider, s settings.Settings, model string) { + t.Helper() + list, listed, free := p.Pricer().Prices(s, model) + want, wantListed := p.ListPrice(model) + if listed != wantListed || !reflect.DeepEqual(list, want) { + t.Errorf("%s's %s: Pricer's list price %+v %v, ListPrice's %+v %v", p.ID, model, list, listed, want, wantListed) + } + if wantFree := p.PricedFree(s, model); free != wantFree { + t.Errorf("%s's %s: Pricer's free %v, PricedFree's %v", p.ID, model, free, wantFree) + } +} + +// The Providers page prices every model of every provider on each ask. +// A Pricer works the provider's catalogs out once and prices each model +// once for both its list price and its free mark, so a model priced on +// it allocates nothing, where ListPrice and PricedFree asked apart +// worked the catalogs out, and made the names to look up, twice. +func TestPricerAllocatesNothingPerModel(t *testing.T) { + prefsHome(t) + data := `{"zfree":{"models":{ + "foo:free":{"id":"foo:free","cost":{"input":0,"output":0}}, + "foo":{"id":"foo","cost":{"input":1,"output":2}} + }}}` + if err := os.WriteFile(catalog.CachePath(), []byte(data), 0o644); err != nil { + t.Fatal(err) + } + catalog.Reset() + p := Provider{ID: "zf", Name: "ZF", Catalog: "zfree", Key: "k", Chat: "http://127.0.0.1:9/v1"} + none := settings.Settings{} + // as the page asks: under the request's holds (gui's held) + defer Hold()() + defer filememo.Hold()() + r := p.Pricer() + for _, model := range []string{"foo:free", "foo", "nobody-lists-this"} { + r.Prices(none, model) // the catalog's memo filled + if n := testing.AllocsPerRun(50, func() { r.Prices(none, model) }); n != 0 { + t.Errorf("%s: a Pricer allocated %v times for one model, want 0", model, n) + } } }