Files
magpie/internal/provider/gemini_api.go
T
yetone b1d5912def gateway: a Gemini image model listed as models/{id} is drawn at models/{id}, and a Lite one is sent no 2K
Dani Ariyanto on Discord: Nano Banana 2.1 on Google AI Studio always
answered 404 "models/nano-banana-2.1 is not found". AI Studio's
OpenAI-compatible /models (the Google Gemini preset's list) names every
model models/{id}, so the provider's id is models/gemini-nano-banana-2.1
(google/models/gemini-nano-banana-2.1 at the gateway, the model
Automatic image generation picked for them). Chat already dropped the
prefix (GeminiPath), but drawGemini put the id after its own /models/,
and asked /v1beta/models/models/gemini-nano-banana-2.1:generateContent.
GeminiModelPath now drops a leading models/ itself, so every path built
from it agrees: chat (GeminiPath), image generation (drawGemini), a
plugin's Code Assist path and Vertex's. The listed ids are left as they
are: renaming them would orphan every pick saved as google/models/... .

The reporter also saw gemini-3.1-flash-lite-image (Nano Banana 2 Lite)
refuse size 1024x1536 with 400 "Image size 2K is not supported for this
model". takesImageSize now says which models may be asked an imageSize
past 1K: not Gemini 2.x (as before) and not a Gemini / Nano Banana Lite,
read without models/ or a relay's google/. imageConfigOf (Gemini API
and Code Assist draws) and openRouterSize (OpenRouter's Image API
resolution) both ask it; such a model gets the aspect ratio alone and
draws at its 1K, as it did with no size asked.

Run at this change on f9492fd8e (rebased since onto 6abea5f8e, which touches internal/agent only; vet below at the pushed head):
- mica: go test -timeout 45m -count=1 ./internal/gateway/ ./internal/provider/: ok (gateway 1124.8s, provider 160.1s)
- wsl-run (Linux): the same: ok (gateway 1532s, provider 199s)
- mica at the pushed head: the three new tests ok; GOOS=linux and GOOS=windows go vet ./internal/gateway/ ./internal/provider/ ok; gofmt -l clean.
Broken by hand (old gemini_api.go and draw.go, openRouterSize given the
new signature only): TestGeminiImageModelListedWithModelsPrefix FAIL
"404 ... (asked /v1beta/models/models/gemini-nano-banana-2.1:generateContent)",
TestOpenRouterSizeOfALiteImageModel FAIL "resolution:2K",
TestGeminiModelPathKeepsSlashes FAIL for the three models/ ids. With
takesImageSize returning true for Lite, the lite case is red
(imageConfig {"aspectRatio":"2:3","imageSize":"2K"}).
Not tried: a real AI Studio key drawing Nano Banana 2.1 or 2 Lite; the
tests use the reporter's 404 and 400 texts with no network. Whether
Lite takes imageSize 512 is not known, so it is asked none.
2026-11-17 20:06:48 +08:00

143 lines
5.6 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
}
// GeminiAsked is the base a Gemini API is asked at: the saved base, and a
// bare host given /v1beta, as Google's SDKs add the version to a base_url
// with no path. GeminiBase keeps a relay's root as typed (JNDliuliuM on X),
// so the version is added here, where a request goes: WandAPI's
// https://api.wandapi.ai was asked at /models/… and refused (equinox0253
// on X). A base with a path of its own (/v1beta, a relay's /gemini) is
// asked as typed.
func GeminiAsked(u string) string {
if u == "" || geminiKept(u) {
return u
}
return geminiVersioned(u)
}
// geminiKept reports, without allocating, that geminiVersioned would give u
// back as it is: a scheme, a path of its own with no trailing slash, query,
// fragment or /models, and no space around it. Host asks Base(Gemini) on
// every look (TestHostBuildsNoList), and a saved base is already this.
func geminiKept(u string) bool {
if strings.TrimSpace(u) != u || strings.HasSuffix(u, "/") || strings.ContainsAny(u, "?#") || strings.Contains(u, "/models") {
return false
}
i := strings.Index(u, "://")
if i <= 0 {
return false
}
j := strings.IndexByte(u[i+3:], '/')
return j > 0
}
// GeminiModelPath is model as every Gemini and Vertex AI path names it
// after models/: each slash-separated segment escaped and the slashes
// kept, so a relay's namespaced id (example/model-demo) is asked at
// /models/example/model-demo, the path the relay routes by, not
// /models/example%2Fmodel-demo (#6695, LuoRuiyao on X). An id with an
// empty, . or .. segment, which a server or proxy could clean into a
// path outside models/, goes as one escaped segment instead. An id with
// the models/ Google's own lists name a model by (AI Studio's
// OpenAI-compatible /models lists "models/gemini-…") is asked without
// it, since the path already has one: drawn at models/models/… the
// Gemini API answered 404 (Dani Ariyanto on Discord, Nano Banana 2.1).
func GeminiModelPath(model string) string {
model = strings.TrimPrefix(model, "models/")
segs := strings.Split(model, "/")
for i, s := range segs {
if s == "" || s == "." || s == ".." {
return url.PathEscape(model)
}
segs[i] = url.PathEscape(s)
}
return strings.Join(segs, "/")
}
// 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 := GeminiModelPath(model)
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)
}