diff --git a/internal/catalog/live.go b/internal/catalog/live.go index 821d7a94f..5191b69eb 100644 --- a/internal/catalog/live.go +++ b/internal/catalog/live.go @@ -92,6 +92,12 @@ func LiveByID(provider, model string) []Model { return slices.Clone(f.byID[model]) } +// LiveListed is whether Live lists any model, without the list copied. +func LiveListed(provider string) bool { + f, err := readLive(provider) + return err == nil && len(f.byID) > 0 +} + // Live returns the model list last fetched from the provider, if any: the // models to talk to, without the ones that draw (LiveDrawers). func Live(provider string) (models []Model, fetched time.Time, ok bool) { diff --git a/internal/provider/decide.go b/internal/provider/decide.go index 7463c5c82..1eec37e93 100644 --- a/internal/provider/decide.go +++ b/internal/provider/decide.go @@ -95,9 +95,18 @@ func (p Provider) DecidesModel(model string) bool { if p.bailianApart() { return model == BailianDecision } - if p.IsRemoteMagpie() || p.listsDecisions() { + // decideListed's answer, without its list copied for each model + // asked about: a provider's every model was asked, a copy of the whole + // list each time (0.4 GB of a real config's 4 GB over 20 polls) + if p.IsRemoteMagpie() { + return slices.ContainsFunc(catalog.LiveByID(p.ID, model), func(m catalog.Model) bool { return m.Decides }) + } + if p.listsDecisions() { // OpenRouter's Jev Router (typesafe/jev-router) is a chat model - return slices.ContainsFunc(p.decideListed(), func(m catalog.Model) bool { return m.ID == model }) + if id := decisionsID(p.ID); catalog.LiveListed(id) { + return len(catalog.LiveByID(id, model)) > 0 + } + return !p.serverMarksDecisions() && model == OpenRouterJev } return jevID(model) || p.DecideVia() == ViaCloudflare && CloudflareClef(model) } diff --git a/internal/provider/decide_copy_test.go b/internal/provider/decide_copy_test.go new file mode 100644 index 000000000..0298ffca7 --- /dev/null +++ b/internal/provider/decide_copy_test.go @@ -0,0 +1,87 @@ +package provider + +import ( + "fmt" + "os" + "runtime" + "testing" + "time" + + "github.com/yetone/magpie/internal/catalog" +) + +// Each of a provider's models is asked whether it decides, and each ask +// copied the provider's whole decision list (0.4 GB of a real config's +// 4 GB over 20 polls of the Providers and Agents pages). An ask reads the +// list kept, and answers as the list does. +func TestDecidesModelCopiesNoList(t *testing.T) { + h := t.TempDir() + t.Setenv("HOME", h) + t.Setenv("USERPROFILE", h) + t.Setenv("XDG_CONFIG_HOME", t.TempDir()) + t.Setenv("XDG_CACHE_HOME", t.TempDir()) + p := Provider{ID: "or-many", Name: "OpenRouter", Key: "k", Chat: "https://openrouter.ai/api/v1", Decide: "https://openrouter.ai/api/v1"} + if !p.listsDecisions() { + t.Fatal("not a provider that lists its decision models") + } + // none listed yet: OpenRouter's Jev + if !p.DecidesModel(OpenRouterJev) || p.DecidesModel("liquid/d1") { + t.Fatal("with no list, only Jev decides") + } + var ms []catalog.Model + for i := range 2000 { + ms = append(ms, catalog.Model{ID: fmt.Sprintf("vendor/decider-%d", i)}) + } + if err := catalog.SaveLive(decisionsID(p.ID), p.Decide, ms); err != nil { + t.Fatal(err) + } + fetchedEarlier(t, catalog.LivePath(decisionsID(p.ID))) + if !p.DecidesModel("vendor/decider-1999") || p.DecidesModel(OpenRouterJev) || p.DecidesModel("vendor/chat") { + t.Fatal("decision models are not the list's") + } + listed := bytesPer(func() { p.decideListed() }) + if n := bytesPer(func() { p.DecidesModel("vendor/decider-1999") }); n*20 > listed { + t.Fatalf("an ask took %d bytes, the list %d", n, listed) + } + + // a remote magpie's: the models its list marks + r := normalize(Provider{ID: "office", Preset: RemoteMagpiePreset, Chat: "http://127.0.0.1:1/typesafe"}) + ms = ms[:0] + for i := range 2000 { + ms = append(ms, catalog.Model{ID: fmt.Sprintf("m-%d", i), Decides: i%2 == 0}) + } + if err := catalog.SaveLive(r.ID, r.Chat, ms); err != nil { + t.Fatal(err) + } + fetchedEarlier(t, catalog.LivePath(r.ID)) + if !r.DecidesModel("m-1998") || r.DecidesModel("m-1999") || r.DecidesModel("m-x") { + t.Fatal("a remote magpie's decision models are not the ones its list marks") + } + listed = bytesPer(func() { r.decideListed() }) + if n := bytesPer(func() { r.DecidesModel("m-1998") }); n*20 > listed { + t.Fatalf("a remote magpie's ask took %d bytes, the list %d", n, listed) + } +} + +// bytesPer is what one call of f allocates, in bytes, over 20 calls. +func bytesPer(f func()) uint64 { + f() + var a, b runtime.MemStats + runtime.ReadMemStats(&a) + for range 20 { + f() + } + runtime.ReadMemStats(&b) + return (b.TotalAlloc - a.TotalAlloc) / 20 +} + +// fetchedEarlier dates f an hour back, as a list fetched earlier is: a file +// written just now is read again on each look, its time too recent to +// tell a later write apart. +func fetchedEarlier(t *testing.T, f string) { + t.Helper() + old := time.Now().Add(-time.Hour) + if err := os.Chtimes(f, old, old); err != nil { + t.Fatal(err) + } +}