Files
magpie/internal/provider/gemini_api.go
T
yetone 844b9ca3ac provider, gateway, gui: a custom provider's Base URL can be a Gemini API (generateContent): …/v1beta, its models listed from it, the key sent in x-goog-api-key (#1346, NagaseMinato)
The custom provider editor's Base URL has a fifth API, "Gemini compatible"
(Gemini 兼容 / Gemini 互換 / Gemini-kompatibel), beside OpenAI, Anthropic and
Responses; More endpoints has a Gemini URL; the CLI takes gemini=. A
Gemini URL is kept as its base (…/v1beta, a relay's path prefix kept, a
pasted models/…:generateContent cut off). Every client's request, Chat,
Messages, Responses or Gemini, streamed or not, goes upstream to
models/{model}:streamGenerateContent?alt=sse under it, with no model in
the body and the key in x-goog-api-key; models are listed from its
/models, a page at a time, keeping those that serve generateContent.
Detect doesn't ask a Gemini API and is hidden while it is picked.
Factory's Gemini route and Bearer token are unchanged.

With five APIs the pick no longer fits a 900px editor in English and
German, so it wraps to a second row, and its thumb now follows the
picked option onto that row.

Verified against fake httptest upstreams only; no real Gemini key or
relay was tried:
- TestCustomGeminiUpstream (gateway): chat, chat stream, messages, a
  Gemini client's stream and non-stream, behind /proxy/v1beta. Fails with
  "chat: 404" without the gateway.go change.
- TestCustomGeminiProvider, TestGeminiBase, TestFactoryGeminiUnchanged
  (provider).
- go vet, HOME=$(mktemp -d) go test -tags nogui ./... (44 packages ok),
  GOOS=linux and GOOS=windows go vet on the touched packages.
- provider-gemini.test.cjs (new, en/zh/ja/de, Chromium + WebKit, 8 pass;
  fails with "900px: the thumb is 22px off" without the slide() fix),
  and in both engines: provider-base-api, provider-detect,
  provider-detect-models, provider-add-models, provider-output-picks,
  provider-search, provider-url-each-api, seg-contrast, settings-groups,
  menu-scroll, click-scroll, ledger-hscroll, strip-scrollbar,
  view-scrollbar, usage-ledger-chart, system-one, gui-ja, gui-de,
  gui-zh-tw: all pass.
2026-10-09 01:10:25 +08:00

45 lines
1.5 KiB
Go

package provider
import (
"net/url"
"regexp"
"strings"
)
// A custom provider can be given a Gemini API (#1346): Google's own at
// generativelanguage.googleapis.com/v1beta, or one that answers as it does.
// Its base is the URL models/{model}:generateContent goes under, which is
// where Google's docs and SDKs put the version.
// geminiMethod is a Gemini endpoint pasted whole, cut back to its base:
// …/models/gemini-2.5-pro:streamGenerateContent?alt=sse, or …/models.
var geminiMethod = regexp.MustCompile(`/models(?:/[^/?#]*)?(?:[?#].*)?$`)
// GeminiBase is the base a Gemini URL is kept as: trimmed, an endpoint or
// the model list pasted whole cut back to the base, and a bare host given
// Google's version (/v1beta), as the Gemini API's SDKs add it.
func GeminiBase(u string) string {
u = strings.TrimRight(strings.TrimSpace(u), "/")
if u == "" {
return ""
}
if !strings.Contains(u, "://") {
u = "https://" + u
}
u = strings.TrimRight(geminiMethod.ReplaceAllString(u, ""), "/")
if pu, err := url.Parse(u); err == nil && pu.Host != "" && strings.Trim(pu.Path, "/") == "" {
return u + "/v1beta"
}
return u
}
// GeminiPath is the path under a Gemini base model is asked at: streamed as
// server-sent events (alt=sse) when stream, else one JSON reply.
func GeminiPath(model string, stream bool) string {
m := url.PathEscape(strings.TrimPrefix(model, "models/"))
if stream {
return "/models/" + m + ":streamGenerateContent?alt=sse"
}
return "/models/" + m + ":generateContent"
}