Files
magpie/internal/provider/gemini_api.go
T
yetone cc0d9c8b04 provider: a Gemini base typed without a version is kept as typed (JNDliuliuM on X)
GeminiBase gave every bare host /v1beta on Save (normalize), so a relay's
root typed in the editor came back as …/v1beta when reopened. Only Google's
own bare host (generativelanguage.googleapis.com) is given /v1beta now; any
other URL stays as typed. The detection (DetectBase), which proposes a base
rather than keeping one, still adds it (geminiVersioned). Siblings checked:
OpenAI/Responses bases are only trimmed on save (no /v1 added; fixV1 is the
5ba92aaf8 probe); the editor's respellURL runs only when the protocol is
switched or a URL pasted. Not changed: the Anthropic base still loses a
typed /v1 or /v1/messages, because requests add /v1/messages themselves and
the version would be sent twice.

Run (mote): TestGeminiBase and the new TestGeminiBaseSavedAsTyped red on the
old gemini_api.go/detect.go (relay root saved as …/v1beta), green after;
go test ./internal/provider ok; new GUI test provider-base-as-typed (save,
reload, reopen for gemini/openai/responses/anthropic) ok in chromium and
webkit, with provider-add-gemini, provider-base-api and provider-detect-draft
(46 tests; one failure seen on the first run was this file's own Escape
dialog, fixed by reloading). go vet darwin, linux, windows; gofmt clean.
Not tried: a real relay; the full suite runs after the tag.
2026-11-12 12:46:42 +08:00

90 lines
3.5 KiB
Go

package provider
import (
"net/url"
"strings"
"github.com/yetone/magpie/internal/lazyre"
)
// 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 = lazyre.New(`/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. Only Google's own bare
// host is given its version (/v1beta); any other URL stays as typed, so a
// relay's root is not rewritten on the next open (JNDliuliuM on X).
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 && strings.EqualFold(pu.Hostname(), "generativelanguage.googleapis.com") && strings.Trim(pu.Path, "/") == "" {
return u + "/v1beta"
}
return u
}
// geminiVersioned is GeminiBase with the /v1beta a bare host of any vendor
// is asked at, for a detection that proposes a base rather than keeping one
// the user typed.
func geminiVersioned(u string) string {
u = GeminiBase(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"
}
// AIStudioChat is Google AI Studio's OpenAI-compatible base, the Google
// Gemini preset's Chat URL.
const AIStudioChat = "https://generativelanguage.googleapis.com/v1beta/openai"
// AIStudioGemini is Google AI Studio's own Gemini API (generateContent),
// beside its OpenAI-compatible one.
const AIStudioGemini = "https://generativelanguage.googleapis.com/v1beta"
// aiStudioNative gives a provider at AI Studio's OpenAI-compatible base,
// the Google Gemini preset's or one typed in, the Gemini API at the same
// place with the same key (cocoasoda on Discord): Gemini's own clients are
// relayed there as they ask, and googleSearch, which the OpenAI-compatible
// API has no tool for, is run there. Its other requests stay on chat
// completions, which Speaks names first. It is filled in once
// (GeminiFilled): a Gemini URL of the provider's own is kept, and one the
// user cleared stays empty.
func (p *Provider) aiStudioNative() {
if p.GeminiFilled || p.Account != nil || !strings.EqualFold(p.Chat, AIStudioChat) {
return
}
// one filled in before it was recorded is recorded as it is
if p.Gemini == "" || strings.EqualFold(p.Gemini, AIStudioGemini) {
p.Gemini, p.GeminiFilled = AIStudioGemini, true
}
}
// AIStudioPair is a provider at both of AI Studio's APIs: its models are
// the OpenAI-compatible list's, which is the Gemini API's list again.
func (p Provider) AIStudioPair() bool {
return strings.EqualFold(p.Chat, AIStudioChat) && strings.EqualFold(p.Gemini, AIStudioGemini)
}