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.
303 lines
12 KiB
Go
303 lines
12 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"path"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestGeminiBase(t *testing.T) {
|
|
for in, want := range map[string]string{
|
|
"": "",
|
|
"generativelanguage.googleapis.com": "https://generativelanguage.googleapis.com/v1beta",
|
|
"https://generativelanguage.googleapis.com/": "https://generativelanguage.googleapis.com/v1beta",
|
|
"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-pro:streamGenerateContent?alt=sse": "https://generativelanguage.googleapis.com/v1beta",
|
|
"https://relay.example/proxy/v1beta/models": "https://relay.example/proxy/v1beta",
|
|
"https://relay.example": "https://relay.example",
|
|
"https://relay.example/": "https://relay.example",
|
|
"relay.example:8080": "https://relay.example:8080",
|
|
"https://relay.example/proxy/v1": "https://relay.example/proxy/v1",
|
|
} {
|
|
if got := GeminiBase(in); got != want {
|
|
t.Errorf("GeminiBase(%q) = %q, want %q", in, got, want)
|
|
}
|
|
}
|
|
if got := GeminiPath("models/gemini-2.5-pro", true); got != "/models/gemini-2.5-pro:streamGenerateContent?alt=sse" {
|
|
t.Errorf("stream path %q", got)
|
|
}
|
|
if got := GeminiPath("gemini-2.5-flash", false); got != "/models/gemini-2.5-flash:generateContent" {
|
|
t.Errorf("path %q", got)
|
|
}
|
|
}
|
|
|
|
// A namespaced model id keeps its slashes in every Gemini and Vertex AI
|
|
// path, each segment escaped on its own (#6695); Google's own ids are
|
|
// unchanged, and an id whose segments a server could clean into another
|
|
// path (empty, . or ..) stays one escaped segment under models/.
|
|
func TestGeminiModelPathKeepsSlashes(t *testing.T) {
|
|
for in, want := range map[string]string{
|
|
"gemini-2.5-pro": "gemini-2.5-pro",
|
|
"gemini-3.8-flash-preview": "gemini-3.8-flash-preview",
|
|
"claude-sonnet-4-5@20250929": "claude-sonnet-4-5@20250929",
|
|
"qwen3:8b": "qwen3:8b",
|
|
"example/model-demo": "example/model-demo",
|
|
"a/b/c": "a/b/c",
|
|
"ex ample/model?x#y": "ex%20ample/model%3Fx%23y",
|
|
"ns/100%": "ns/100%25",
|
|
"../secret": "..%2Fsecret",
|
|
"a/../b": "a%2F..%2Fb",
|
|
"a/./b": "a%2F.%2Fb",
|
|
"a//b": "a%2F%2Fb",
|
|
"/abs": "%2Fabs",
|
|
"trail/": "trail%2F",
|
|
// the models/ Google's lists name a model by is the path's own
|
|
"models/gemini-nano-banana-2.1": "gemini-nano-banana-2.1",
|
|
"models/example/model-demo": "example/model-demo",
|
|
"models/../x": "..%2Fx",
|
|
} {
|
|
if got := GeminiModelPath(in); got != want {
|
|
t.Errorf("GeminiModelPath(%q) = %q, want %q", in, got, want)
|
|
}
|
|
}
|
|
for got, want := range map[string]string{
|
|
GeminiPath("example/model-demo", true): "/models/example/model-demo:streamGenerateContent?alt=sse",
|
|
GeminiPath("models/example/model-demo", false): "/models/example/model-demo:generateContent",
|
|
GeminiPath("gemini-2.5-pro", true): "/models/gemini-2.5-pro:streamGenerateContent?alt=sse",
|
|
VertexPath("example/model-demo"): "/publishers/google/models/example/model-demo:streamGenerateContent?alt=sse",
|
|
VertexWholePath("example/model-demo"): "/publishers/google/models/example/model-demo:generateContent",
|
|
VertexWholePath("gemini-2.5-pro"): "/publishers/google/models/gemini-2.5-pro:generateContent",
|
|
} {
|
|
if got != want {
|
|
t.Errorf("path %q, want %q", got, want)
|
|
}
|
|
}
|
|
// whatever the id, the path stays under models/: cleaning it as a
|
|
// server or proxy may never leaves the models/ folder
|
|
for _, id := range []string{"..", "../..", "a/../../b", "./x", "x/.", "..%2F..", "%2e%2e/x"} {
|
|
p := GeminiPath(id, false)
|
|
clean := path.Clean(p)
|
|
if !strings.HasPrefix(clean, "/models/") || !strings.HasSuffix(clean, ":generateContent") {
|
|
t.Errorf("GeminiPath(%q) = %q cleans to %q, outside /models/", id, p, clean)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A custom provider with a Gemini URL alone speaks Gemini upstream, is
|
|
// signed with x-goog-api-key, and lists the models its API serves for
|
|
// generateContent, a page at a time, under a base with a path prefix
|
|
// (#1346, NagaseMinato).
|
|
func TestCustomGeminiProvider(t *testing.T) {
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
t.Setenv("XDG_CACHE_HOME", t.TempDir())
|
|
var keys, paths []string
|
|
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
keys = append(keys, r.Header.Get("x-goog-api-key")+"|"+r.Header.Get("Authorization"))
|
|
paths = append(paths, r.URL.Path+"?"+r.URL.RawQuery)
|
|
if r.URL.Path != "/proxy/v1beta/models" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
page := map[string]any{"models": []map[string]any{
|
|
{"name": "models/gemini-2.5-pro", "displayName": "Gemini 2.5 Pro", "supportedGenerationMethods": []string{"generateContent", "countTokens"}, "inputTokenLimit": 1048576, "outputTokenLimit": 65536},
|
|
{"name": "models/text-embedding-004", "supportedGenerationMethods": []string{"embedContent"}},
|
|
}, "nextPageToken": "p2"}
|
|
if r.URL.Query().Get("pageToken") == "p2" {
|
|
page = map[string]any{"models": []map[string]any{
|
|
{"name": "models/gemini-2.5-flash", "supportedGenerationMethods": []string{"generateContent"}},
|
|
}}
|
|
}
|
|
json.NewEncoder(w).Encode(page)
|
|
}))
|
|
defer up.Close()
|
|
|
|
p := Provider{ID: "gem", Name: "Gem", Key: "AIza-test", Gemini: up.URL + "/proxy/v1beta/models/gemini-2.5-pro:generateContent"}
|
|
if err := Save(p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := Find("gem")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p = *got
|
|
if p.Gemini != up.URL+"/proxy/v1beta" {
|
|
t.Errorf("base kept as %q", p.Gemini)
|
|
}
|
|
if got := p.Speaks(); !slices.Equal(got, []Protocol{Gemini}) || p.Base(Gemini) != p.Gemini {
|
|
t.Errorf("speaks %v at %q", got, p.Base(Gemini))
|
|
}
|
|
if h := AuthHeaders(p, Gemini); len(h) != 1 || h["x-goog-api-key"] != "AIza-test" {
|
|
t.Errorf("auth %v", h)
|
|
}
|
|
if p.ModelTest() != "" {
|
|
t.Errorf("not testable: %q", p.ModelTest())
|
|
}
|
|
ms, err := p.List(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var ids []string
|
|
for _, m := range ms {
|
|
ids = append(ids, m.ID)
|
|
}
|
|
if !slices.Equal(ids, []string{"gemini-2.5-pro", "gemini-2.5-flash"}) || ms[0].Name != "Gemini 2.5 Pro" || ms[0].Context != 1048576 || ms[0].Output != 65536 {
|
|
t.Errorf("listed %v: %+v", ids, ms)
|
|
}
|
|
if len(paths) != 2 || paths[0] != "/proxy/v1beta/models?pageSize=1000" || paths[1] != "/proxy/v1beta/models?pageSize=1000&pageToken=p2" {
|
|
t.Errorf("asked %v", paths)
|
|
}
|
|
for _, k := range keys {
|
|
if k != "AIza-test|" {
|
|
t.Errorf("signed %q", k)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Factory's sign-in keeps its own generate route and its Bearer token.
|
|
func TestFactoryGeminiUnchanged(t *testing.T) {
|
|
p := Provider{ID: "factory", Key: "tok", Account: &Account{}}
|
|
if !p.FactoryGemini() || p.Base(Gemini) != factoryAPI+"/api/llm/g/v1" {
|
|
t.Errorf("factory base %q", p.Base(Gemini))
|
|
}
|
|
if h := AuthHeaders(p, Gemini); h["Authorization"] != "Bearer tok" || h["x-goog-api-key"] != "" {
|
|
t.Errorf("factory auth %v", h)
|
|
}
|
|
}
|
|
|
|
// An AI Studio key (the Google Gemini preset, or a provider at AI Studio's
|
|
// OpenAI-compatible URL) speaks AI Studio's Gemini API too, with the same
|
|
// key, so a Gemini client and Google Search reach it (cocoasoda on
|
|
// Discord); one's own Gemini URL is kept, and nothing else gets one.
|
|
func TestAIStudioSpeaksGemini(t *testing.T) {
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
t.Setenv("XDG_CACHE_HOME", t.TempDir())
|
|
for _, p := range []Provider{
|
|
{ID: "google", Name: "Google Gemini", Preset: "google", Chat: AIStudioChat, Key: "AIza-k"},
|
|
{ID: "studio", Name: "Studio", Chat: AIStudioChat + "/", Key: "AIza-k"},
|
|
{ID: "relay", Name: "Relay", Chat: AIStudioChat, Gemini: "https://relay.example/v1beta", Key: "AIza-k"},
|
|
{ID: "other", Name: "Other", Chat: "https://relay.example/v1beta/openai", Key: "k"},
|
|
} {
|
|
if err := Save(p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
want := map[string]string{"google": AIStudioGemini, "studio": AIStudioGemini, "relay": "https://relay.example/v1beta", "other": ""}
|
|
for id, base := range want {
|
|
p, err := Find(id)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if p.Gemini != base {
|
|
t.Errorf("%s: Gemini = %q, want %q", id, p.Gemini, base)
|
|
}
|
|
if speaks := p.Speaks(); slices.Contains(speaks, Gemini) != (base != "") || !slices.Contains(speaks, Chat) {
|
|
t.Errorf("%s speaks %v", id, speaks)
|
|
}
|
|
if p.AIStudioPair() != (id == "google" || id == "studio") {
|
|
t.Errorf("%s: AIStudioPair = %v", id, p.AIStudioPair())
|
|
}
|
|
}
|
|
}
|
|
|
|
// The Gemini URL magpie gives an AI Studio key is filled in once: one the
|
|
// user clears stays empty, on every load and save after.
|
|
func TestAIStudioGeminiClearedStays(t *testing.T) {
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
t.Setenv("XDG_CACHE_HOME", t.TempDir())
|
|
if err := Save(Provider{ID: "google", Name: "Google Gemini", Preset: "google", Chat: AIStudioChat, Key: "AIza-k"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p, err := Find("google")
|
|
if err != nil || p.Gemini != AIStudioGemini || !p.GeminiFilled {
|
|
t.Fatalf("filled: %+v %v", p, err)
|
|
}
|
|
// one 3e61d814e filled in, before GeminiFilled, is recorded too
|
|
p.GeminiFilled = false
|
|
if err := Save(*p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if p, err = Find("google"); err != nil || p.Gemini != AIStudioGemini || !p.GeminiFilled {
|
|
t.Fatalf("an earlier fill isn't recorded: %+v %v", p, err)
|
|
}
|
|
p.Gemini = ""
|
|
if err := Save(*p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for range 2 {
|
|
got, err := Find("google")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got.Gemini != "" || slices.Contains(got.Speaks(), Gemini) {
|
|
t.Fatalf("the cleared Gemini URL came back: %q", got.Gemini)
|
|
}
|
|
if err := Save(*got); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A relay's Gemini base typed without a version is saved and reopened as
|
|
// typed; only Google's own bare host is given /v1beta (JNDliuliuM on X:
|
|
// 点击保存之后再重新打开看,还是会被添加上/v1beta).
|
|
func TestGeminiBaseSavedAsTyped(t *testing.T) {
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
t.Setenv("XDG_CACHE_HOME", t.TempDir())
|
|
for id, c := range map[string][2]string{
|
|
"root": {"https://relay.example", "https://relay.example"},
|
|
"v1": {"https://relay.example/v1", "https://relay.example/v1"},
|
|
"prefix": {"https://relay.example/gemini", "https://relay.example/gemini"},
|
|
"google": {"https://generativelanguage.googleapis.com", "https://generativelanguage.googleapis.com/v1beta"},
|
|
} {
|
|
if err := Save(Provider{ID: id, Name: id, Key: "k", Gemini: c[0]}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := Find(id)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got.Gemini != c[1] {
|
|
t.Errorf("%s: Gemini %q saved as %q, want %q", id, c[0], got.Gemini, c[1])
|
|
}
|
|
}
|
|
// the other protocols' bases are kept as typed too
|
|
if err := Save(Provider{ID: "oa", Name: "oa", Key: "k", Chat: "https://relay.example", Responses: "https://relay.example/api"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, _ := Find("oa")
|
|
if got.Chat != "https://relay.example" || got.Responses != "https://relay.example/api" {
|
|
t.Errorf("openai bases changed: %q %q", got.Chat, got.Responses)
|
|
}
|
|
}
|
|
|
|
// A relay's bare host, kept as typed (TestGeminiBase), is asked at its
|
|
// /v1beta, as Google's SDKs ask one; a base with a path is asked as typed
|
|
// (equinox0253 on X). The model list, a model's test and the gateway all
|
|
// ask at Base(Gemini).
|
|
func TestGeminiAsked(t *testing.T) {
|
|
for in, want := range map[string]string{
|
|
"": "",
|
|
"https://api.wandapi.ai": "https://api.wandapi.ai/v1beta",
|
|
"https://api.wandapi.ai/": "https://api.wandapi.ai/v1beta",
|
|
"http://127.0.0.1:8080": "http://127.0.0.1:8080/v1beta",
|
|
"https://api.wandapi.ai/v1beta": "https://api.wandapi.ai/v1beta",
|
|
"https://relay.example/gemini": "https://relay.example/gemini",
|
|
"https://relay.example/proxy/v1": "https://relay.example/proxy/v1",
|
|
"https://generativelanguage.googleapis.com/v1beta": "https://generativelanguage.googleapis.com/v1beta",
|
|
} {
|
|
if got := GeminiAsked(in); got != want {
|
|
t.Errorf("GeminiAsked(%q) = %q, want %q", in, got, want)
|
|
}
|
|
if got := (Provider{Gemini: in}).Base(Gemini); got != want {
|
|
t.Errorf("Base(Gemini) of %q = %q, want %q", in, got, want)
|
|
}
|
|
}
|
|
if got, _ := tiny(Provider{Gemini: "https://api.wandapi.ai"}, Gemini, "gemini-3.8-flash"); got != "https://api.wandapi.ai/v1beta/models/gemini-3.8-flash:generateContent" {
|
|
t.Errorf("a model's test asks %s", got)
|
|
}
|
|
}
|