diff --git a/internal/provider/host_speaking_test.go b/internal/provider/host_speaking_test.go new file mode 100644 index 000000000..fcf039844 --- /dev/null +++ b/internal/provider/host_speaking_test.go @@ -0,0 +1,35 @@ +package provider + +import "testing" + +// Host is the host of the first protocol the provider speaks, as it was +// when it read Speaks' list; it builds no list now: one for each look was +// 8 MB of what twenty polls of the Providers page allocated with real +// accounts (IsBedrock, IsKilo, IsCline and IsOpenCode each look). +func TestHostBuildsNoList(t *testing.T) { + for _, p := range []Provider{ + {ID: "a", Chat: "https://api.example.com/v1"}, + {ID: "b", Anthropic: "https://api.anthropic.example/v1"}, + {ID: "c", Responses: "https://r.example.com/v1", Anthropic: "https://a.example.com"}, + {ID: "d", Gemini: "https://generativelanguage.example.com/v1beta"}, + {ID: "e", Decide: "https://decide.example.com/v1"}, + {ID: "f"}, + } { + want := "" + for _, pr := range p.Speaks() { + if u := p.Base(pr); u != "" { + want = HostOf(u) + break + } + } + if want == "" && p.Decide != "" { + want = HostOf(p.Decide) + } + if got := p.Host(); got != want { + t.Errorf("%s: Host = %q, want %q", p.ID, got, want) + } + if n := testing.AllocsPerRun(20, func() { p.Host() }); n != 0 { + t.Errorf("%s: Host allocated %.0f times a look", p.ID, n) + } + } +} diff --git a/internal/provider/provider.go b/internal/provider/provider.go index ebb06da04..8af7739e0 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -1687,19 +1687,26 @@ var speaksLooked atomic.Int64 // Speaks lists the protocols the vendor serves natively, preferred first. func (p Provider) Speaks() []Protocol { speaksLooked.Add(1) + return slices.Collect(p.speaking) +} + +// speaking is Speaks one protocol at a time, for a look that wants only +// the first (Host): a list built for it was 8 MB of what twenty polls of +// the Providers page allocated with real accounts. +func (p Provider) speaking(yield func(Protocol) bool) { // a Google sign-in speaks Code Assist, and only that if p.Account != nil && p.Account.codeAssist != "" && !p.IsPlugin() { - return []Protocol{CodeAssist} + yield(CodeAssist) + return } - var out []Protocol for _, pr := range Protocols { - if p.Base(pr) != "" { - out = append(out, pr) + if p.Base(pr) != "" && !yield(pr) { + return } } // a plugin's Gemini models, beside what else it serves - if p.IsPlugin() && p.Account.codeAssist != "" { - out = append(out, CodeAssist) + if p.IsPlugin() && p.Account.codeAssist != "" && !yield(CodeAssist) { + return } // Factory's Gemini models, on generateContent. A model droid didn't // list stays on the other three (factoryAPIs); this is not one of them. @@ -1710,12 +1717,11 @@ func (p Provider) Speaks() []Protocol { // one). if p.IsVertex() { if p.vertexBase() != "" { - out = append(out, Gemini) + yield(Gemini) } } else if p.FactoryGemini() || p.Gemini != "" { - out = append(out, Gemini) + yield(Gemini) } - return out } // FactoryGemini is Factory's sign-in, whose Gemini models are asked on its @@ -1850,7 +1856,7 @@ func (p Provider) Host() string { if p.Account != nil && p.Account.moved { return p.Account.wasHost // not plugin:// } - for _, pr := range p.Speaks() { + for pr := range p.speaking { if u := p.Base(pr); u != "" { return HostOf(u) }