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).
41 lines
1.4 KiB
Go
41 lines
1.4 KiB
Go
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)
|
|
}
|
|
}
|