Files
magpie/internal/gateway/draw_gemini_plugin_test.go
T
yetone e2b7233499 draw: a plugin's Gemini image model draws on generateContent through its fetch
XYenon (magpie-google-ai-studio, Discord) asked how a plugin's image
models reach /v1/images/generations and /v1/images/edits. A plugin's
model on Gemini's API (@ai-sdk/google) that says it gives images
(capabilities.output.image / modalities.output), or is named for them,
is now a drawer: Drawers lists it, and draw sends it to drawCodeAssist
(pluginDrawsGemini), as a Google sign-in's image models are drawn. That
asks generateContent with responseModalities ["TEXT","IMAGE"] through
the plugin's fetch, an edit's pictures as inlineData, and reads the
pictures from the answer. Request.draws asks for the IMAGE modality of
a model not named for images. Imagen (catalog.ImagesAPI, :predict) is
left out. The OpenAI-compatible plugin path (SenseNova) is unchanged.

Test: TestGeminiPluginDraws (fake plugin $FAKE_GEMINI_DRAWS: a model
named for images, one that only says it gives them, an Imagen).
Broken by hand: with the old draw.go the drawers are empty (red); with
the old codeassist.go the declared-only model is sent no
responseModalities (red).

Run on mote: gofmt -l clean; go vet ./internal/gateway ./internal/provider
./internal/plugin for darwin, linux (-tags nogui) and windows; go test
-count=1 -timeout 45m ./internal/gateway ./internal/plugin
./internal/provider ok at this commit on its first base 172e65c80
(gateway 515s, plugin 104s, provider 104s), and ok again with the next
two commits on top, rebased on abd94e0b1 (tree 7b8456f88: gateway 504s,
plugin 265s, provider 95s; vet for the three systems there too).
Not tried: a real Google AI Studio account through XYenon's plugin.
2026-10-29 22:54:06 +08:00

86 lines
3.2 KiB
Go

package gateway
import (
"encoding/base64"
"fmt"
"io"
"net/http"
"slices"
"strings"
"sync"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// A plugin's model on Gemini's API (@ai-sdk/google) that gives images
// draws as a Google sign-in's image models do: on generateContent asking
// for images beside text, through the plugin's fetch, the picture read
// from its answer's inlineData (XYenon's Google AI Studio plugin). One that
// only says it gives images is asked for them too; an Imagen, which draws
// on :predict, and a model that gives only text are no drawers.
func TestGeminiPluginDraws(t *testing.T) {
t.Setenv("FAKE_GEMINI_DRAWS", "1")
var mu sync.Mutex
var paths, bodies []string
pid := besideFake(t, "google-ai-studio", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, _ := io.ReadAll(r.Body)
mu.Lock()
paths, bodies = append(paths, r.URL.RequestURI()), append(bodies, string(b))
mu.Unlock()
if !strings.Contains(r.URL.Path, ":streamGenerateContent") {
w.WriteHeader(404)
return
}
w.Header().Set("Content-Type", "text/event-stream")
fmt.Fprintf(w, "data: %s\n\n", `{"candidates":[{"content":{"role":"model","parts":[{"text":"here"},{"inlineData":{"mimeType":"image/png","data":"`+base64.StdEncoding.EncodeToString(pngBytes)+`"}}]},"finishReason":"STOP"}],"usageMetadata":{"promptTokenCount":5,"candidatesTokenCount":1290}}`)
}))
p, err := provider.Find(pid)
if err != nil {
t.Fatal(err)
}
var drawers []string
for _, m := range Drawers(*p) {
drawers = append(drawers, m.ID)
}
slices.Sort(drawers)
if !slices.Equal(drawers, []string{"gemini-3-flash-canvas", "gemini-3-pro-image-preview"}) {
t.Fatalf("drawers %v, want gemini-3-flash-canvas and gemini-3-pro-image-preview", drawers)
}
s := New()
for _, model := range drawers {
mu.Lock()
paths, bodies = nil, nil
mu.Unlock()
code, a, raw := postImages(t, s, "/v1/images/generations", "application/json", `{"model":"`+pid+`/`+model+`","prompt":"a magpie"}`)
if code != 200 || len(a.Data) != 1 {
t.Fatalf("%s: %d %s", model, code, raw)
}
if b, _ := base64.StdEncoding.DecodeString(a.Data[0].B64); string(b) != string(pngBytes) {
t.Fatalf("%s: image %q", model, b)
}
mu.Lock()
if len(paths) != 1 || paths[0] != "/v1/models/"+model+":streamGenerateContent?alt=sse" {
t.Fatalf("%s was asked at %v", model, paths)
}
if !strings.Contains(bodies[0], `"responseModalities":["TEXT","IMAGE"]`) || !strings.Contains(bodies[0], "a magpie") {
t.Fatalf("%s was sent %s", model, bodies[0])
}
mu.Unlock()
}
// an edit: the picture goes as the prompt's inlineData
mu.Lock()
paths, bodies = nil, nil
mu.Unlock()
img := "data:image/png;base64," + base64.StdEncoding.EncodeToString(pngBytes)
code, a, raw := postImages(t, s, "/v1/images/edits", "application/json", `{"model":"`+pid+`/gemini-3-flash-canvas","prompt":"bluer","images":[{"image_url":"`+img+`"}]}`)
if code != 200 || len(a.Data) != 1 {
t.Fatalf("edit: %d %s", code, raw)
}
mu.Lock()
defer mu.Unlock()
if len(bodies) != 1 || !strings.Contains(bodies[0], `"inlineData"`) || !strings.Contains(bodies[0], base64.StdEncoding.EncodeToString(pngBytes)) {
t.Fatalf("edit was sent %v", bodies)
}
}