provider: a Gemini provider saved as a bare host reads its Host without building a URL (#6479)
16b8c300fmade Base(Gemini) return GeminiAsked(p.Gemini), which runs GeminiBase (a regexp replace and url.Parse) and url.Parse again, and adds /v1beta to a bare host. Host goes through Base on every look, so f856d381a's guarantee (Host builds nothing; one list a look was 8 MB over twenty Providers polls with real accounts) was gone for Gemini providers.baffcb65dgave GeminiAsked a no-allocation path for a base that is already versioned (case d of TestHostBuildsNoList), but a bare host saved as typed, WandAPI's https://api.wandapi.ai (the very case16b8c300fwas for), still allocates 6 times a look, and an untrimmed or pasted endpoint 5. Host now asks baseHost, which is HostOf(Base(pr)) without building the base: for a Gemini base saved with its scheme and a host, the host is read from the saved URL, since what GeminiAsked trims (spaces, slashes, a pasted /models/… endpoint) or adds (/v1beta) is all after the host. Anything else (no scheme, no host, Factory's or Vertex's Gemini) still goes through Base. Requests are unchanged: Base(Gemini) still returns the asked URL. The test was right and is unchanged apart from two more Gemini cases (d2: a bare host; d3: a pasted endpoint with spaces and a trailing slash). The new TestHostOfGeminiIsAskedHost checks Host against HostOf(Base(Gemini)) over odd saved values (no scheme, "https:///models", "https://?x", "/x.example/v1", a port, a fragment). Run (mica, -tags nogui; mote was at load 184): - at456b5c6c7, beforebaffcb65d: TestHostBuildsNoList red ("d: Host allocated 5 times a look", 3 of 3, also on mote), green after. - rebased on origin/mainea3edd4b1: with Host's old loop put back by hand, d2 and d3 red (6 and 5 allocations); with this change TestHostBuildsNoList and TestHostOfGeminiIsAskedHost ok -count=3; go test ./internal/provider ok (144s); GOOS=darwin/linux/windows go vet ./internal/provider ok; gofmt -l clean. Not run: the full ./... suite; the change is inside Host alone.
This commit is contained in:
@@ -12,6 +12,8 @@ func TestHostBuildsNoList(t *testing.T) {
|
||||
{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: "d2", Gemini: "https://api.wandapi.ai"}, // asked at its /v1beta (#6479)
|
||||
{ID: "d3", Gemini: " https://relay.example.com/gemini/models/ "},
|
||||
{ID: "e", Decide: "https://decide.example.com/v1"},
|
||||
{ID: "f"},
|
||||
} {
|
||||
@@ -33,3 +35,23 @@ func TestHostBuildsNoList(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Host takes a Gemini base's host from the saved URL rather than the one
|
||||
// GeminiAsked builds (#6479); it is the same host for whatever was saved.
|
||||
func TestHostOfGeminiIsAskedHost(t *testing.T) {
|
||||
for _, u := range []string{
|
||||
"https://generativelanguage.googleapis.com", "https://api.wandapi.ai/",
|
||||
"https://x.example/v1beta", " https://X.example/models/gemini:generateContent?alt=sse ",
|
||||
"api.wandapi.ai", "api.wandapi.ai/v1beta", "/models", "https:///models", "https://?x",
|
||||
"https://", "//", " ", "/x.example/v1", "https://x.example#f", "http://x.example:8080/",
|
||||
} {
|
||||
p := Provider{ID: "g", Gemini: u}
|
||||
want := ""
|
||||
if b := p.Base(Gemini); b != "" {
|
||||
want = HostOf(b)
|
||||
}
|
||||
if got := p.Host(); got != want {
|
||||
t.Errorf("Gemini %q: Host = %q, want %q (Base %q)", u, got, want, p.Base(Gemini))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2368,8 +2368,8 @@ func (p Provider) Host() string {
|
||||
return p.Account.wasHost // not plugin://<id>
|
||||
}
|
||||
for pr := range p.speaking {
|
||||
if u := p.Base(pr); u != "" {
|
||||
return HostOf(u)
|
||||
if h, ok := p.baseHost(pr); ok {
|
||||
return h
|
||||
}
|
||||
}
|
||||
if p.Decide != "" {
|
||||
@@ -2378,6 +2378,26 @@ func (p Provider) Host() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// baseHost is HostOf(Base(pr)), and whether Base(pr) is set, without
|
||||
// building the base: a Gemini base is asked at GeminiAsked, which parses
|
||||
// the saved URL and may add /v1beta, five allocations a look (#6479).
|
||||
// Neither the version nor the trimming changes the host, so the saved
|
||||
// URL's host is the asked one's.
|
||||
func (p Provider) baseHost(pr Protocol) (string, bool) {
|
||||
if pr == Gemini && !p.FactoryGemini() && !p.IsVertex() {
|
||||
// a URL with its scheme and a host: what GeminiAsked trims or adds
|
||||
// is after the host; anything else is left to Base
|
||||
u := strings.TrimSpace(p.Gemini)
|
||||
if i := strings.Index(u, "://"); i > 0 && len(u) > i+3 && u[i+3] != '/' {
|
||||
return HostOf(u), true
|
||||
}
|
||||
}
|
||||
if u := p.Base(pr); u != "" {
|
||||
return HostOf(u), true
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
// Where is what the provider's calls go to, as usage keeps it: the API's
|
||||
// host, and for a subscription who is signed in there too. The id alone
|
||||
// can't tell: it can be given to another vendor or account later.
|
||||
|
||||
Reference in New Issue
Block a user