diff --git a/internal/agent/agents.go b/internal/agent/agents.go index 41964241e..2732750f5 100644 --- a/internal/agent/agents.go +++ b/internal/agent/agents.go @@ -446,34 +446,49 @@ func ownOptionsFrom(native func(p string) []catalog.Model, authFile string, cur sort.Strings(providers) var out []Option for _, p := range providers { - name := catalog.ProviderName(p) - if name == "" { - name = nativeProviderNames[p] + if native == nil { + out = append(out, ownBuilt.shared(p, func() []Option { return ownProvider(nil, p) })...) + } else { + out = append(out, ownProvider(native, p)...) } - if name == "" { - name = p - } - var ms []catalog.Model - if native != nil { - ms = native(p) - } - if len(ms) == 0 { - ms = catalog.Provider(p) - } - if len(ms) == 0 { - ms = codexServed(p) - } - opts := group(name, options(ms, p+"/")) - if ic := providerIcon(p); ic != "" { - for i := range opts { - opts[i].GroupIcon = ic - } - } - out = append(out, opts...) } return out } +// ownBuilt is ownProvider's options built last per provider, for agents +// without a registry of their own: Goose's model field asked them each +// poll, for the picker and again for Drift, each provider's models made +// into options anew, 20 MB of forty polls with real accounts. +var ownBuilt stampedLists[Option] + +// ownProvider is the options of one provider an agent reaches on its own. +func ownProvider(native func(p string) []catalog.Model, p string) []Option { + name := catalog.ProviderName(p) + if name == "" { + name = nativeProviderNames[p] + } + if name == "" { + name = p + } + var ms []catalog.Model + if native != nil { + ms = native(p) + } + if len(ms) == 0 { + ms = catalog.Provider(p) + } + if len(ms) == 0 { + ms = codexServed(p) + } + opts := group(name, options(ms, p+"/")) + if ic := providerIcon(p); ic != "" { + for i := range opts { + opts[i].GroupIcon = ic + } + } + return opts +} + // ---- agents ---------------------------------------------------------------- // magpieProviderJSON is the provider block agents with JSON configs get. diff --git a/internal/agent/own_options_kept_test.go b/internal/agent/own_options_kept_test.go new file mode 100644 index 000000000..2bb8060b8 --- /dev/null +++ b/internal/agent/own_options_kept_test.go @@ -0,0 +1,59 @@ +package agent + +import ( + "fmt" + "os" + "strings" + "testing" + + "github.com/yetone/magpie/internal/catalog" +) + +// A provider's options an agent reaches on its own are kept while +// nothing changed: Goose's model field asked them each poll, for the +// picker and again for Drift, each provider's models made into options +// anew (20 MB of forty polls with real accounts). Each caller still has +// a list of its own, and a catalog read since is offered. +func TestOwnOptionsKept(t *testing.T) { + syncHome(t) + writeCatalog := func(extra string) { + var ms []string + for i := range 200 { + ms = append(ms, fmt.Sprintf(`"m%d": {"id":"m%d","name":"M%d"}`, i, i, i)) + } + if extra != "" { + ms = append(ms, fmt.Sprintf(`"%s": {"id":"%s","name":"%s"}`, extra, extra, extra)) + } + if err := os.WriteFile(catalog.CachePath(), []byte(`{"openai": {"name":"OpenAI","models": {`+strings.Join(ms, ",")+`}}}`), 0o644); err != nil { + t.Fatal(err) + } + catalog.Reset() + } + writeCatalog("") + values := func() map[string]bool { + out := map[string]bool{} + for _, o := range ownOptions("", "", "openai") { + out[o.Value] = true + } + return out + } + if got := values(); !got["openai/m7"] || len(got) < 200 { + t.Fatalf("openai's models not offered: %d", len(got)) + } + built := testing.AllocsPerRun(5, func() { ownProvider(nil, "openai") }) + kept := testing.AllocsPerRun(20, func() { ownOptions("", "", "openai") }) + if kept*5 > built { + t.Fatalf("a provider's options already built took %.0f allocations, building them %.0f", kept, built) + } + mine := ownOptions("", "", "openai") + for i := range mine { + mine[i].Value = "x" + } + if got := values(); !got["openai/m7"] { + t.Fatal("a caller's write reached the options kept") + } + writeCatalog("fresh") + if !values()["openai/fresh"] { + t.Fatal("a model the catalog lists since is not offered") + } +}