From 73b957069a6a566982f3e8bf08adf29f5f870b7f Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 1 Nov 2026 07:21:07 +0800 Subject: [PATCH] provider: ModelAPI looks up the API set first, and counts what the provider speaks The Providers page asks ModelAPI of each model of every provider's list, and it listed the protocols the provider speaks (Speaks, a new slice) each time, though it needs only whether there are two, and most models have no API set at all. With real accounts that was 12 MB of what forty polls allocated. ModelAPI now looks up the API set for the model first (apiSet) and, only with one set, counts the protocols up to two; apisAmong passes the count of the list it already has. The answer is the same. Run on mote at this tree (on 7087d92a0, the previous commit before it was picked): gofmt, go vet (darwin, windows, linux) of internal/provider and gui ok; TestModelAPIListsNoProtocols, TestNoAPIFindsTheProvidersAPIsOnce and TestClaudeSignedOutKeepsWhatItParsed alone 3/3 ok; broken by hand (Speaks listed again in ModelAPI) it fails "ModelAPI allocated 112 bytes, listing the protocols 112"; go test ./internal/provider ok (130.3s); go test ./internal/gui fails only TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and TestMainWindowCorners (#3949), as on the run before this change. Real accounts, sandbox HOME, 40 polls (abr.sh), against the v0.1.2496 build: Provider.ModelAPI and Speaks gone from the profile (12 MB before); /api/state and the drift reply byte-identical. Footprint peak 136 MB (was 134-141). --- internal/provider/account.go | 6 ++-- internal/provider/modelapi_count_test.go | 40 ++++++++++++++++++++++++ internal/provider/modelprefs.go | 38 +++++++++++++++++----- 3 files changed, 75 insertions(+), 9 deletions(-) create mode 100644 internal/provider/modelapi_count_test.go diff --git a/internal/provider/account.go b/internal/provider/account.go index b6a58482a..3677b28ea 100644 --- a/internal/provider/account.go +++ b/internal/provider/account.go @@ -133,8 +133,10 @@ func (p Provider) APIs(model string) []Protocol { return p.apisAmong(p.Speaks(), // apisAmong is APIs with the protocols p speaks found already. func (p Provider) apisAmong(speaks []Protocol, model string) []Protocol { // the one the user said it is asked on, whatever the list says - if proto, ok := p.modelAPIAmong(speaks, model); ok { - return []Protocol{proto} + if proto, ok := p.apiSet(model); ok { + if proto, ok := p.modelAPIAmong(len(speaks), proto); ok { + return []Protocol{proto} + } } return p.ListedAPIs(model) } diff --git a/internal/provider/modelapi_count_test.go b/internal/provider/modelapi_count_test.go new file mode 100644 index 000000000..df47feff8 --- /dev/null +++ b/internal/provider/modelapi_count_test.go @@ -0,0 +1,40 @@ +package provider + +import ( + "path/filepath" + "testing" +) + +// ModelAPI is asked of each model of every provider's list on the +// Providers page: one with no API set is answered without listing the +// protocols the provider speaks, and one set is answered as before. +func TestModelAPIListsNoProtocols(t *testing.T) { + isolate(t) + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("USERPROFILE", home) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config")) + t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache")) + p := Provider{ID: "relay", Name: "Relay", Key: "k", Chat: "http://127.0.0.1:1/v1", Anthropic: "http://127.0.0.1:1", Models: []string{"a", "b"}} + if err := Save(p); err != nil { + t.Fatal(err) + } + if err := SetModelAPI("relay/b", string(Anthropic)); err != nil { + t.Fatal(err) + } + defer Hold()() + if _, ok := p.ModelAPI("a"); ok { + t.Fatal("a model with no API set has one") + } + if api, ok := p.ModelAPI("b"); !ok || api != Anthropic { + t.Fatalf("the model set on Anthropic's API is on %q (%v)", api, ok) + } + if one := (Provider{ID: "relay", Chat: p.Chat}); func() bool { _, ok := one.ModelAPI("b"); return ok }() { + t.Fatal("a provider with one API keeps a model set on another") + } + listed := allocatedBy(func() { _ = p.Speaks() }) + asked := allocatedBy(func() { _, _ = p.ModelAPI("a") }) + if listed == 0 || asked*4 > listed { + t.Fatalf("ModelAPI allocated %d bytes, listing the protocols %d", asked, listed) + } +} diff --git a/internal/provider/modelprefs.go b/internal/provider/modelprefs.go index 20061ecce..49a4c7e96 100644 --- a/internal/provider/modelprefs.go +++ b/internal/provider/modelprefs.go @@ -469,16 +469,40 @@ func setModelAPI(ref, api string) (bool, error) { // ModelAPI is the API the user said p's model is asked on, when p has a // URL for it still. -func (p Provider) ModelAPI(model string) (Protocol, bool) { return p.modelAPIAmong(p.Speaks(), model) } - -// modelAPIAmong is ModelAPI with the protocols p speaks found already. -func (p Provider) modelAPIAmong(speaks []Protocol, model string) (Protocol, bool) { - // Vertex AI's model id says its one API (ListedAPIs) - if len(speaks) < 2 || p.IsVertex() { +// +// The Providers page asks it of each model of every provider's list, and +// most have no API set: that is looked up first, and the protocols p +// speaks are counted, not listed (a list made for each model was 12 MB of +// what forty polls allocated with real accounts). +func (p Provider) ModelAPI(model string) (Protocol, bool) { + proto, ok := p.apiSet(model) + if !ok { return "", false } + n := 0 + for range p.speaking { + if n++; n == 2 { + break + } + } + return p.modelAPIAmong(n, proto) +} + +// apiSet is the API the user set model to be asked on, false with +// none. +func (p Provider) apiSet(model string) (Protocol, bool) { proto := Protocol(heldSettings().ModelAPIs[p.ID+"/"+model]) - if proto == "" || !modelAPI(proto) || p.Base(proto) == "" { + return proto, proto != "" +} + +// modelAPIAmong is ModelAPI with the number of protocols p speaks (up to +// two) and the one set found already. +func (p Provider) modelAPIAmong(speaks int, proto Protocol) (Protocol, bool) { + // Vertex AI's model id says its one API (ListedAPIs) + if speaks < 2 || p.IsVertex() { + return "", false + } + if !modelAPI(proto) || p.Base(proto) == "" { return "", false } return proto, true