diff --git a/internal/provider/host_speaking_test.go b/internal/provider/host_speaking_test.go index fcf039844..541643a57 100644 --- a/internal/provider/host_speaking_test.go +++ b/internal/provider/host_speaking_test.go @@ -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)) + } + } +} diff --git a/internal/provider/provider.go b/internal/provider/provider.go index a3de8ba63..7ed3df712 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -2368,8 +2368,8 @@ func (p Provider) Host() string { return p.Account.wasHost // not plugin:// } 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.