Files
magpie/internal/gateway/draw.go
T
yetone 841af36bfc gateway: models the user disabled are refused on every route, and leave every list (#1867)
oxsean asked for one place to turn a model (claude-fable-5) off for good:
not reached by a direct request, an old session, a routing group member,
a fallback, a sub-agent, count_tokens or a decision model.

What changed:
- settings.DisabledModels: patterns, an exact id or a glob (* any run, /
  included; ? one), matched ignoring case against provider/model, the
  bare id and its last segment (provider/disabled.go). Empty by default,
  so nothing changes for anyone who doesn't set it.
- Lists: buildEntries (entriesOff) leaves a disabled model out of the
  catalog, so /v1/models, agents' model files and pickers, Codex's list
  and every group go without it; membersIn skips it as a member, Deciders
  as a decision model; codexModels drops Codex's own disabled slugs and
  codexListTag changes so Codex refetches.
- Requests (gateway/disabled.go): 403 "model <id> is disabled in magpie"
  in each API's error shape, at #882's choke points: serve (idOff),
  perKeyBarred (no candidate: group next member, provider Fallback),
  count_tokens and Gemini :countTokens, serveSystemOne,
  serveDecisionGroup and postDecide, draw/video/embeddings, and
  codexBackend for Codex's own bare models. A group with a member left
  goes on without it; one with none left is refused naming them. A bare
  id the catalog no longer has is refused as disabled, not 404. A Codex
  sub-agent pick onto a disabled model stays on its lead's model.
- An unreadable settings.json is never "none disabled": the list read
  last stays; with none read, every request is refused; nothing is
  written over the file.
- CLI: magpie model disable <model|glob>..., enable ..., disabled
  (list-disabled).
- GUI: Settings > Models > Disabled models: each pattern with the models
  it matches now and Enable; Disable... opens the model picker, which
  also takes a typed glob. Says only requests through magpie are covered.
  POST /api/settings/disabled-models; POST /api/settings keeps the list.
  Strings in zh, zh-TW, ja, de.
- docs/subsystems/gateway-routing.md: Disabled models section.

What was run (worktree at origin/main 091593f7d, private GOCACHE, temp HOME):
- gofmt -l clean; go vet on darwin (touched pkgs), GOOS=linux -tags nogui
  ./..., GOOS=windows ./... clean; go build ./...
- go test -count=1 ./internal/provider ./internal/gateway ./internal/cli
  ./internal/gui ./internal/settings: all ok (gui after adding the field to
  TestSettingsSaveKeepsWhatItDoesNotSend's fixture, which it asked for).
- node --test internal/gui/tests/disabled-models.test.cjs: 16/16 (chromium
  + webkit, en/zh/ja/de, 1100/420); neighbouring Settings > Models tests
  and i18n-keys: 131/131.
- Broken by hand, each turned TestDisabled* red: serve's idOff (404
  instead of 403), perKeyBarred's filter (fallback reached spare), the
  catalog filter (/v1/models and Codex list), countOff (count_tokens 200),
  postDecide's check (decision call went out).
- Not tried with a real vendor account or a real agent.

Ref #1867
2026-10-13 05:34:13 +08:00

1498 lines
50 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package gateway
import (
"bytes"
"cmp"
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"math"
"mime"
"mime/multipart"
"net/http"
"net/textproto"
"slices"
"strconv"
"strings"
"sync"
"time"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/settings"
"github.com/yetone/magpie/internal/usage"
)
// The gateway draws: /v1/images/generations and /v1/images/edits take
// OpenAI's images request and answer in its shape, whichever vendor draws.
// A model on an images API (gpt-image, dall-e, imagen, flux, seedream,
// qwen-image…) is asked there; one that answers in chat with pictures
// (gemini-*-image, gpt-5-image, anything on OpenRouter) is asked on chat
// completions with modalities; Gemini at Google is asked on its own
// generateContent. A request that names no model draws with the Settings'
// image generation model, or one magpie picks (AutoDrawer).
// DrawAgent is the User-Agent magpie's generate_image tool adds to its own.
const DrawAgent = "magpie-image/1"
// drawTimeout is how long a vendor is given to draw: to answer, and to send
// all of its answer.
var drawTimeout = 5 * time.Minute
// maxDrawings is how many images one request may ask for.
const maxDrawings = 4
// drawer is the model a request that names none draws with: the Settings'
// ImageGen while it resolves, else AutoDrawer's. None when it is off or no
// provider draws.
func drawer() (string, bool) {
switch v := settings.Load().ImageGen; v {
case "off":
return "", false
case "":
default:
if pickedMissing(v) == "" {
return v, true
}
}
m := AutoDrawer()
return m, m != ""
}
// Drawer is the model a request that names none draws with (drawer), as
// provider/model: "" when image generation is off or no provider draws.
func Drawer() string {
m, _ := drawer()
return m
}
// DrawerMissing is the model Settings › Models › Image generation names
// when magpie can't find it any more, so that AutoDrawer's draws in its
// place, as VisionMissing is Image recognition's. "" when none is named,
// it is off, or it resolves.
func DrawerMissing() string { return pickedMissing(settings.Load().ImageGen) }
func resolveDrawing(id string) (provider.Provider, string, bool) {
if strings.Contains(id, "/") {
return provider.Resolve(id)
}
for _, p := range provider.All() {
if !p.On() || p.DecideOnly() {
continue
}
for _, m := range Drawers(p) {
if m.ID == id {
return p, m.ID, true
}
}
}
// a subscription's newer image model, named before magpie lists it
// (grok-imagine-image-2.0, #1857): its vendor draws it, or says why not
for _, p := range provider.All() {
if p.On() && !p.DecideOnly() && drawsFamily(p, id) {
return p, id, true
}
}
return provider.Resolve(id)
}
// drawsFamily is whether a subscription whose image models magpie lists
// itself (codexDrawers, grokDrawers) draws id, a model of the family those
// are of: any grok-imagine-image* on a Grok subscription, any gpt-image-*
// on a ChatGPT account. The vendor's newer ones work before magpie names
// them, as grok-imagine-video* do for videos; one it doesn't serve is
// refused by the vendor, in its own words.
func drawsFamily(p provider.Provider, id string) bool {
switch {
case drawsGrok(p):
return strings.HasPrefix(id, grokDrawerFamily)
case drawsCodex(p):
return strings.HasPrefix(id, codexDrawerFamily)
}
return false
}
// The families of the image models a ChatGPT account and a Grok
// subscription draw with (drawsFamily).
const (
codexDrawerFamily = "gpt-image-"
grokDrawerFamily = "grok-imagine-image"
)
// codexDrawers is the image model a ChatGPT account draws with, at its
// Codex backend's images API, as Codex CLI does (gpt-image-2 is the one it
// asks for). It is one: the backend picks the model itself, and answers
// gpt-image-2, gpt-image-2.5 and a made-up name alike with the same image
// (#1865), so listing 2.5 apart said there was a choice where there is
// none. A gpt-image-2.5 saved when it was listed still draws there
// (drawsFamily) and shows as this one (ListedDrawer).
var codexDrawers = []catalog.Model{
{ID: "gpt-image-2", Name: "GPT Image", Released: "2026-01-01"},
}
// VendorPicksDrawer is whether p's vendor picks the image model itself,
// whichever is named: a ChatGPT account's images backend (#1865).
func VendorPicksDrawer(p provider.Provider) bool { return drawsCodex(p) }
// ListedDrawer is the listed image model a picked one (provider/model)
// stands for: a ChatGPT account's gpt-image-2.5, or any other GPT Image
// name, is its one GPT Image, as its backend draws the same for any name
// (#1865). ref itself otherwise, and when its provider isn't found. The
// pick itself is kept as it was saved, and draws as before.
func ListedDrawer(ref string) string {
pid, id, ok := strings.Cut(ref, "/")
if !ok || !strings.HasPrefix(id, codexDrawerFamily) || id == codexDrawer {
return ref
}
p, err := provider.Find(pid)
if err != nil || !drawsCodex(*p) {
return ref
}
return pid + "/" + codexDrawer
}
// codexDrawer is the image model Codex CLI asks a ChatGPT account for, the
// one Automatic picks there.
const codexDrawer = "gpt-image-2"
// grokDrawers are the image models a Grok subscription (SuperGrok, X Premium+)
// draws with, at the Imagine API of the backend Grok Build's image_gen tool
// calls: cli-chat-proxy.grok.com/v1/images/generations.
var grokDrawers = []catalog.Model{
{ID: "grok-imagine-image", Name: "Grok Imagine"},
{ID: "grok-imagine-image-quality", Name: "Grok Imagine Quality"},
}
// drawsGrok is whether p is a Grok subscription, which draws at the Imagine
// API of the backend Grok Build talks to: the built-in's, or the Grok
// plugin's, which signs what it is sent there as the built-in does.
func drawsGrok(p provider.Provider) bool {
return p.Account != nil && (p.Account.Agent == "grok" || p.PluginProvider() == "grok") && p.Base(provider.Responses) != ""
}
// drawsCodex is whether p is a ChatGPT account, which draws at its Codex
// backend's images API.
func drawsCodex(p provider.Provider) bool {
return p.Account != nil && p.Account.Agent == "codex" && p.Base(provider.Responses) != ""
}
// Drawers are the models a provider can draw with: its catalogs' models
// that make images, those its own model list names that do, and those of
// its own picks named for images; a ChatGPT account's GPT Image, a Grok
// subscription's Grok Imagine, a WorkBuddy plan's those its config lists.
func Drawers(p provider.Provider) []catalog.Model {
if drawsWorkBuddy(p) {
return wbDrawers(p)
}
if drawsCodex(p) || drawsGrok(p) {
out := slices.Clone(codexDrawers)
if drawsGrok(p) {
out = slices.Clone(grokDrawers)
}
for i := range out {
out[i].Provider = p.ID
}
return out
}
if googleAccount(p) {
// the image models a Google sign-in lists (Antigravity's
// gemini-3.1-flash-image), which draw on Code Assist (#757)
var out []catalog.Model
for _, m := range p.Available() {
if catalog.DrawsID(m.ID) {
m.Provider = p.ID
out = append(out, m)
}
}
return out
}
// any other subscription is asked through its agent's own API, which
// draws nothing magpie can ask for yet. A Responses-only relay (a Codex
// backend such as sub2api) has no chat base but serves the images API
// under the same root as its Responses endpoint, so it draws all the same.
if p.Account != nil || (p.Base(provider.Chat) == "" && p.Base(provider.Responses) == "") {
return nil
}
out := p.PlanDrawers()
have := make(map[string]bool, len(out))
for _, m := range out {
have[m.ID] = true
}
for _, c := range p.Catalogs() {
for _, m := range catalog.Drawers(c) {
if !have[m.ID] {
have[m.ID] = true
out = append(out, m)
}
}
}
for _, m := range catalog.LiveDrawers(p.ID) {
if !have[m.ID] {
have[m.ID] = true
m.Provider = p.ID
out = append(out, m)
}
}
for _, id := range p.Models {
if !have[id] && catalog.DrawsID(id) {
have[id] = true
out = append(out, catalog.Model{ID: id, Provider: p.ID})
}
}
return out
}
// AutoDrawer is the model magpie draws with when the Settings name none:
// a ChatGPT account's, which its plan pays for, else the cheapest of the
// models the API keys set up can draw with, the newest of those whose price
// isn't known. "" when none can.
func AutoDrawer() string {
best, bestCost, bestDate := "", 0.0, ""
for _, p := range provider.All() {
if !p.On() || p.DecideOnly() || drawsWorkBuddy(p) {
// WorkBuddy's images cost the plan's credits: it draws only
// with its model picked or named
continue
}
for _, m := range Drawers(p) {
if drawsCodex(p) && m.ID != codexDrawer {
// Codex CLI draws with gpt-image-2 only; a plan may be
// refused the others (chatgpt.com: 403, #545)
continue
}
cost := 1e9
switch {
case drawsCodex(p), drawsGrok(p):
cost = 0 // a ChatGPT or Grok plan's images come with it
case m.Price != nil:
cost = m.Price.Input + m.Price.Output
}
if best == "" || cost < bestCost || cost == bestCost && m.Released > bestDate {
best, bestCost, bestDate = p.ID+"/"+m.ID, cost, m.Released
}
}
}
return best
}
// picture is an image sent along to draw from, or one drawn.
type picture struct {
Mime string
Data []byte
URL string // an image the vendor keeps, when it gave no bytes
}
func (p picture) dataURL() string {
if p.URL != "" {
return p.URL
}
return "data:" + p.Mime + ";base64," + base64.StdEncoding.EncodeToString(p.Data)
}
// drawing is one images request, whichever shape it came in.
type drawing struct {
Model, Prompt, Size, Quality, Background, Format string
// Want is the response_format the caller asked for: with "b64_json"
// an image a vendor gave only a URL for is fetched (inline)
Want string
N int
Images []picture // to edit, or draw from
Mask *picture
}
// drawn is what a vendor gave back.
type drawn struct {
Images []picture
Text string // what a chat model said besides
Revised string // the prompt as an images API rewrote it
Input int
Output int
}
func (s *Server) images(edit bool) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
inputBody, admitted := s.requestBody(w, r, provider.Chat)
if !admitted {
return
}
r.Body = io.NopCloser(bytes.NewReader(inputBody))
defer func() {
if r.MultipartForm != nil {
r.MultipartForm.RemoveAll()
}
}()
start := time.Now()
d, err := readDrawing(r)
if err != nil {
writeError(w, provider.Chat, 400, err.Error())
return
}
if edit && len(d.Images) == 0 {
writeError(w, provider.Chat, 400, "an edit needs the image to edit")
return
}
who := callerOf(r)
call := Call{Time: start, From: provider.Chat, Agent: who.agent, Via: who.via, Model: d.Model}
usage.Saw(agentOf(r))
fail := func(code int, msg string) {
call.Status, call.Error, call.Millis = code, msg, time.Since(start).Milliseconds()
writeError(w, provider.Chat, code, msg)
s.record(call)
}
named := d.Model != "" // a gateway key's models hold one the caller names (#882)
if d.Model == "" {
m, ok := drawer()
if !ok {
if v := DrawerMissing(); v != "" {
fail(400, fmt.Sprintf("no model to draw with: the Image generation model picked in magpie's Settings, %q, isn't set up any more: pick another in Settings → Models → Image generation, or name one", v))
return
}
fail(400, "no model to draw with: pick one in magpie's Settings → Images → Image generation, or name one")
return
}
d.Model, call.Model = m, m
}
p, model, ok := s.drawingModel(r, d.Model)
if !ok {
if agentOf(r) == "codex" && !strings.Contains(d.Model, "/") {
fail(400, "no model to draw with: Codex's provider has no matching image model and magpie's Settings → Images → Image generation has none enabled")
return
}
if off, isOff := provider.SwitchedOff(d.Model); isOff {
fail(404, switchedOff(off, d.Model))
return
}
fail(404, fmt.Sprintf("magpie knows no model %q to draw with", d.Model))
return
}
if keyWho, held := keyHolds(r); held && named && !modelAllowed(keyWho, p, model) {
fail(403, keyModelError(keyWho, d.Model))
return
}
if msg, off := modelOff(p, model, d.Model); off { // disabled by the user (#1867)
fail(403, msg)
return
}
var unmask func()
w, d.Prompt, unmask = redactedPrompt(w, d.Prompt)
defer unmask()
call.Provider, call.To = p.ID, provider.Chat
ctx, cancel := context.WithTimeout(r.Context(), drawTimeout)
defer cancel()
// the account that drew, or refused last, is the one recorded
drew, out, code, capBack, err := s.drawOnAccounts(ctx, r, p, model, d)
p = drew
call.Millis = time.Since(start).Milliseconds()
call.Status, call.Usage.Input, call.Usage.Output = code, out.Input, out.Output
if err == nil && len(out.Images) == 0 {
code, err = 502, errors.New(model+" drew nothing"+vendorSaid(out.Text))
call.Status = code
}
providerKeyID, providerKeyName := "", ""
if p.Account == nil && p.Key != "" {
providerKeyID, providerKeyName = provider.KeyID(p.Key), p.KeyName
}
appendUsage(r, usage.Record{Operation: "generate_content", Time: start, Agent: call.Agent, Via: call.Via, Provider: p.ID, Host: p.Where(), Model: model, Requested: call.Model, ProviderKeyID: providerKeyID, ProviderKeyName: providerKeyName, ProviderAccount: accountOf(p),
Input: out.Input, Output: out.Output, Millis: call.Millis, Status: call.Status, Session: sessionOf(r.Header)})
if err != nil {
call.Error = err.Error()
s.record(call)
if d := time.Until(capBack); d > 0 {
w.Header().Set("Retry-After", strconv.Itoa(int(d.Seconds())+1))
}
writeError(w, provider.Chat, code, err.Error())
return
}
s.record(call)
// asked for the image itself, one the vendor keeps at a URL is
// fetched the way the vendor was reached: through its proxy, by
// the client that just had its answer (#1576: the 4K image a relay
// drew in 40 s was lost to generate_image's own download). One
// that can't be fetched is answered by its URL all the same, never
// dropped: it is drawn, and paid for.
data := make([]map[string]any, 0, len(out.Images))
for _, im := range out.Images {
if im.URL != "" && inline(d.Want) {
if b, mt, err := FetchImage(p.Via(r.Context()), s.client, im.URL); err == nil {
im = picture{Mime: mt, Data: b}
} else if s.debug {
log.Printf("images: %s's image at %s is answered by its URL: %v", p.ID, im.URL, err)
}
}
e := map[string]any{}
if im.URL != "" {
e["url"] = im.URL
} else {
e["b64_json"] = base64.StdEncoding.EncodeToString(im.Data)
e["mime_type"] = im.Mime
}
if out.Revised != "" {
e["revised_prompt"] = out.Revised
}
data = append(data, e)
}
res := map[string]any{"created": start.Unix(), "model": p.ID + "/" + model, "data": data,
"usage": map[string]int{"input_tokens": out.Input, "output_tokens": out.Output, "total_tokens": out.Input + out.Output}}
if out.Text != "" {
res["text"] = out.Text
}
writeJSON(w, 200, res)
}
}
func vendorSaid(text string) string {
if text = strings.TrimSpace(text); text == "" {
return ""
}
if len(text) > 300 {
text = text[:300] + "…"
}
return ": " + text
}
// readDrawing reads an images request: JSON, or the multipart form OpenAI's
// edits take.
func readDrawing(r *http.Request) (drawing, error) {
var d drawing
ct, _, _ := mime.ParseMediaType(r.Header.Get("Content-Type"))
if ct == "multipart/form-data" {
if err := r.ParseMultipartForm(64 << 20); err != nil {
return d, err
}
f := r.MultipartForm
get := func(k string) string {
if v := f.Value[k]; len(v) > 0 {
return strings.TrimSpace(v[0])
}
return ""
}
d.Model, d.Prompt, d.Size, d.Quality, d.Background, d.Format, d.Want = get("model"), get("prompt"), get("size"), get("quality"), get("background"), get("output_format"), get("response_format")
d.N, _ = strconv.Atoi(get("n"))
for _, k := range []string{"image", "image[]"} {
for _, fh := range f.File[k] {
pic, err := readPart(fh)
if err != nil {
return d, err
}
d.Images = append(d.Images, pic)
}
}
if fhs := f.File["mask"]; len(fhs) > 0 {
pic, err := readPart(fhs[0])
if err != nil {
return d, err
}
d.Mask = &pic
}
} else {
body, err := io.ReadAll(r.Body)
if err != nil {
return d, err
}
var in struct {
Model string `json:"model"`
Prompt string `json:"prompt"`
Size string `json:"size"`
Quality string `json:"quality"`
Background string `json:"background"`
Format string `json:"output_format"`
N int `json:"n"`
Image json.RawMessage `json:"image"`
Images json.RawMessage `json:"images"`
Mask json.RawMessage `json:"mask"`
ResponseFormat string `json:"response_format"`
}
if err := json.Unmarshal(body, &in); err != nil {
return d, fmt.Errorf("the request isn't JSON: %v", err)
}
d = drawing{Model: in.Model, Prompt: in.Prompt, Size: in.Size, Quality: in.Quality, Background: in.Background, Format: in.Format, Want: strings.TrimSpace(in.ResponseFormat), N: in.N}
for _, raw := range []json.RawMessage{in.Image, in.Images} {
for _, src := range imageSources(raw) {
pic, err := pictureOf(src)
if err != nil {
return d, err
}
d.Images = append(d.Images, pic)
}
}
if srcs := imageSources(in.Mask); len(srcs) > 0 {
pic, err := pictureOf(srcs[0])
if err != nil {
return d, err
}
d.Mask = &pic
}
}
d.Model = strings.TrimSpace(d.Model)
if strings.TrimSpace(d.Prompt) == "" {
return d, errors.New("say what to draw in prompt")
}
if d.N <= 0 {
d.N = 1
}
if d.N > maxDrawings {
return d, fmt.Errorf("n is at most %d", maxDrawings)
}
return d, nil
}
// imageSources are the images a JSON field gives: a URL, {"image_url": …},
// or a list of either.
func imageSources(raw json.RawMessage) []string {
if len(raw) == 0 || string(raw) == "null" {
return nil
}
var one string
if json.Unmarshal(raw, &one) == nil {
return []string{one}
}
var obj struct {
ImageURL json.RawMessage `json:"image_url"`
URL string `json:"url"`
}
if json.Unmarshal(raw, &obj) == nil && (len(obj.ImageURL) > 0 || obj.URL != "") {
if obj.URL != "" {
return []string{obj.URL}
}
return imageSources(obj.ImageURL)
}
var list []json.RawMessage
if json.Unmarshal(raw, &list) == nil {
var out []string
for _, e := range list {
out = append(out, imageSources(e)...)
}
return out
}
return nil
}
// pictureOf is the image at a data: URL, or an http one left for the vendor
// to fetch.
func pictureOf(src string) (picture, error) {
src = strings.TrimSpace(src)
if rest, ok := strings.CutPrefix(src, "data:"); ok {
meta, data, ok := strings.Cut(rest, ",")
if !ok || !strings.HasSuffix(meta, ";base64") {
return picture{}, errors.New("an image's data: URL must be base64")
}
b, err := base64.StdEncoding.DecodeString(data)
if err != nil {
return picture{}, fmt.Errorf("an image's data: URL isn't base64: %v", err)
}
return picture{Mime: strings.TrimSuffix(meta, ";base64"), Data: b}, nil
}
if strings.HasPrefix(src, "http://") || strings.HasPrefix(src, "https://") {
return picture{URL: src}, nil
}
return picture{}, errors.New("an image is a data: or http(s) URL")
}
func readPart(fh *multipart.FileHeader) (picture, error) {
f, err := fh.Open()
if err != nil {
return picture{}, err
}
defer f.Close()
b, err := io.ReadAll(f)
if err != nil {
return picture{}, err
}
mt := fh.Header.Get("Content-Type")
if mt == "" || mt == "application/octet-stream" {
mt = http.DetectContentType(b)
}
return picture{Mime: mt, Data: b}, nil
}
// drawVia is the API a model draws on at a provider.
type drawVia int
const (
viaImages drawVia = iota // /images/generations, /images/edits
viaChat // chat completions with modalities
viaGemini // Google's own generateContent
)
func (v drawVia) String() string {
return [...]string{"images API", "chat completions", "generateContent"}[v]
}
func isGoogle(p provider.Provider) bool {
return provider.HostOf(p.Base(provider.Chat)) == "generativelanguage.googleapis.com"
}
// viaFor is where model is best asked to draw at p.
func viaFor(p provider.Provider, model string) drawVia {
switch {
case p.IsRemoteMagpie():
// another magpie's images API asks the model's vendor the way
// that model draws there
return viaImages
case isGoogle(p) && strings.Contains(strings.ToLower(model), "gemini"):
return viaGemini
case provider.HostOf(p.Base(provider.Chat)) == "openrouter.ai":
return viaChat
case catalog.ImagesAPI(model):
return viaImages
}
return viaChat
}
// drawOnAccounts is draw on a subscription's accounts in the order its
// text requests go over them (the provider's candidates, without its
// fallback models): one whose plan won't draw (chatgpt.com's 403
// {"detail":"Forbidden"} for a plan without images) or is out of them
// hands the request to the next, as a Plus account further down draws
// what the pinned one may not (#545). The refusal is of images alone, so
// no account rests for text over it. It answers with the account that
// drew, or the last one's error when every one refused. A provider's keys
// are gone over the same way (#1657): a key whose own list of models
// leaves the image model out never draws, nor is the first key used for
// one only another lists; with every key or account set not to serve the
// model, it is refused with why, as a chat is.
func (s *Server) drawOnAccounts(ctx context.Context, r *http.Request, p provider.Provider, model string, d drawing) (provider.Provider, drawn, int, time.Time, error) {
if p.Account == nil && p.Key == "" {
// no key and no account (a local ollama): nothing to go over
out, code, err := s.draw(ctx, p, model, d)
return p, out, code, time.Time{}, err
}
q := p
q.Fallback = nil
cs := s.candidates(q, model, provider.Chat)
// an account whose list lacks the image model is put aside for text
// when another's list is unknown; for drawing it is asked last
_, _, left, barred := perKeyBarred(q, model, provider.Chat)
for _, c := range left {
if !slices.ContainsFunc(cs, func(o candidate) bool { return o.p.AccountID() == c.p.AccountID() }) {
cs = append(cs, c)
}
}
// the accounts the calling key may not use are left out of the whole
// fan-out (#905): the next one a refusal hands the request to no less
// than the first, so none it may not use draws
if keyWho, held := accountHolds(r); held {
cs = slices.DeleteFunc(cs, func(c candidate) bool { return !accountAllowed(keyWho, c) })
if len(cs) == 0 {
return p, drawn{}, http.StatusForbidden, time.Time{}, errors.New(keyAccountsError(keyWho, d.Model))
}
}
if len(cs) == 0 && slices.ContainsFunc(barred, func(c candidate) bool { return c.capped != nil }) {
// every account is held at its usage cap
msg, back := cappedError(model, barredOf(barred, q, false, provider.Chat, nil), time.Now())
return p, drawn{}, http.StatusTooManyRequests, back, errors.New(msg)
}
if len(cs) == 0 && len(barred) > 0 {
// every key or account is set not to serve the model (#474)
return p, drawn{}, http.StatusForbidden, time.Time{}, errors.New(barredError(d.Model, barredOf(barred, q, false, provider.Chat, nil)))
}
if len(cs) == 0 {
cs = []candidate{{p: p, model: model, rest: p.ID}}
}
cs = probeOne(cs) // of those resting, only the one back soonest (#1799)
var out drawn
var code int
var err error
for i, c := range cs {
p = c.p
out, code, err = s.draw(ctx, p, model, d)
if err == nil || ctx.Err() != nil || i+1 == len(cs) || !accountRefusedDrawing(code) && !keyLacksModel(code, err) {
break
}
}
return p, out, code, time.Time{}, err
}
// keyLacksModel says a vendor answered 404 that the model isn't one this
// key or account serves: a relay's "Model "gpt-image-2" is not supported by
// any configured account in this group" for a key of its text models
// alone, while the provider's next key serves it (#1657). The next is
// asked; a bare 404, a wrong address, is the request's and asks none.
func keyLacksModel(code int, err error) bool {
return code == http.StatusNotFound && err != nil && modelRefused(code, []byte(err.Error()))
}
// accountRefusedDrawing says a failure to draw was the account's — its
// sign-in, its plan, its allowance — not the request's, so another
// account of the subscription may draw it.
func accountRefusedDrawing(code int) bool {
switch code {
case http.StatusUnauthorized, http.StatusPaymentRequired, http.StatusForbidden, http.StatusTooManyRequests:
return true
}
return false
}
// draw asks the provider for d's images, on the API model draws on there,
// and on the other when that one isn't served.
func (s *Server) draw(ctx context.Context, p provider.Provider, model string, d drawing) (drawn, int, error) {
if drawsWorkBuddy(p) {
return s.drawWorkBuddy(ctx, p, model, d)
}
if drawsCodex(p) || drawsGrok(p) {
return s.drawImages(ctx, p, model, d)
}
if googleAccount(p) {
return s.drawCodeAssist(ctx, p, model, d)
}
if p.Base(provider.Chat) == "" && p.Base(provider.Responses) == "" {
return drawn{}, 400, fmt.Errorf("%s can't draw: magpie draws only through an OpenAI-compatible API, and %s has none", p.Name, p.Name)
}
via := viaFor(p, model)
out, code, err := s.drawOn(ctx, p, model, d, via)
if err != nil && via != viaGemini && (code == 404 || code == 405 || pointsAway(via, code, err)) {
other := viaChat
if via == viaChat {
other = viaImages
}
// the vendor named the other API: its answer there is the one to
// tell, not the refusal that sent the request on
if o, c, e := s.drawOn(ctx, p, model, d, other); e == nil || pointsAway(via, code, err) {
return o, c, e
}
}
return out, code, err
}
// pointsAway is whether a vendor turned a drawing away on one API with
// words that send it to the other: a 400 on chat completions saying "Use
// /v1/images/generations" (Agnes, #1837), or one on the images API naming
// chat completions. The model is then asked there, as when the API it was
// asked on isn't served at all (404, 405).
func pointsAway(via drawVia, code int, err error) bool {
if code != http.StatusBadRequest && code != http.StatusUnprocessableEntity {
return false
}
if via == viaChat {
return catalog.PointsToImagesAPI(err.Error())
}
return catalog.PointsToChat(err.Error())
}
func (s *Server) drawOn(ctx context.Context, p provider.Provider, model string, d drawing, via drawVia) (drawn, int, error) {
switch via {
case viaGemini:
return s.drawGemini(ctx, p, model, d)
case viaChat:
return s.drawChat(ctx, p, model, d)
}
return s.drawImages(ctx, p, model, d)
}
// send posts to the vendor and reads its answer; a failure's code is the
// vendor's, with its own message.
func (s *Server) send(ctx context.Context, p provider.Provider, url, contentType string, body []byte, sign bool) ([]byte, int, error) {
return s.sendAs(ctx, p, http.MethodPost, url, contentType, body, sign)
}
// sendAs is send with the method: a video's progress is asked with a GET.
func (s *Server) sendAs(ctx context.Context, p provider.Provider, method, url, contentType string, body []byte, sign bool) ([]byte, int, error) {
return s.sendWith(ctx, p, method, url, contentType, body, sign, nil)
}
// sendWith is sendAs with headers of the vendor's own besides.
func (s *Server) sendWith(ctx context.Context, p provider.Provider, method, url, contentType string, body []byte, sign bool, extra http.Header) ([]byte, int, error) {
ctx = s.metered(p.Via(ctx), p, "") // counted against its MaxRPM (rpm.go)
req, err := http.NewRequestWithContext(ctx, method, url, bytes.NewReader(body))
if err != nil {
return nil, 500, err
}
if contentType != "" {
req.Header.Set("Content-Type", contentType)
}
// magpie's, or the agent's own where the provider passes it on (#1851)
stampUA(ctx, p, req.Header)
if p.IsRemoteMagpie() {
req.Header.Set("User-Agent", "magpie/"+Version)
passOnCaller(ctx, req)
}
if sign {
if err := p.Sign(ctx, req, provider.Chat, body); err != nil {
return nil, 502, err
}
if drawsCodex(p) {
// the images API answers in JSON, not the stream Codex's
// signing asks of its Responses; the id is Codex CLI's own
req.Header.Set("Accept", "application/json")
req.Header.Set("x-codex-imagegen-request-id", newUUID())
req.Header.Set("x-codex-image-turn-id", newUUID())
}
} else {
// Google's API keys go in their own header
req.Header.Set("x-goog-api-key", p.Key)
for k, v := range p.Headers {
catalog.PutUserHeader(req.Header, k, v)
}
}
for k, v := range extra {
req.Header[k] = v
}
asked := time.Now()
res, err := p.Do(s.client, req) // a plugin's through the plugin
if err != nil {
if ctx.Err() != nil {
return nil, 504, fmt.Errorf("%s didn't answer in %s", p.Name, drawTimeout)
}
return nil, 502, err
}
defer res.Body.Close()
answered := time.Since(asked)
rb := &tracked{r: res.Body, last: time.Now()}
b, err := io.ReadAll(io.LimitReader(rb, 256<<20))
if err != nil {
if ctx.Err() != nil {
// not a bare "context deadline exceeded" (#1576): the vendor
// began answering, and its answer is what ran out of time
return nil, 504, fmt.Errorf("%s began answering after %s (%d %s) but hadn't sent all of its answer in %s: %s", p.Name, answered.Round(time.Second), res.StatusCode, http.StatusText(res.StatusCode), drawTimeout, stalledAt(b, time.Since(rb.last)))
}
return nil, 502, err
}
if res.StatusCode >= 300 {
hint := ""
if drawsCodex(p) && res.StatusCode == http.StatusForbidden {
hint = fmt.Sprintf(" — ChatGPT turned %s's images away: its plan or workspace may not draw, or not with this model (Codex CLI draws with %s)", p.Name, codexDrawer)
}
return b, res.StatusCode, fmt.Errorf("%s: %d %s%s%s", provider.HostOf(url), res.StatusCode, http.StatusText(res.StatusCode), vendorSaid(vendorMessage(b)), hint)
}
return b, res.StatusCode, nil
}
// tracked is a body that notes when its last bytes came.
type tracked struct {
r io.Reader
last time.Time
}
func (t *tracked) Read(p []byte) (int, error) {
n, err := t.r.Read(p)
if n > 0 {
t.last = time.Now()
}
return n, err
}
// stalledAt says where an answer that didn't finish stopped (#1576): how
// much had come, how long nothing more had, and whether the image was
// coming as a link (so the answer itself stuck, after its start) or as
// b64_json (so it was the image's own bytes that came too slowly). A relay
// may answer one size with either, so the two point at different fixes.
func stalledAt(got []byte, quiet time.Duration) string {
where := fmt.Sprintf("%s had come, and nothing more for the last %s", sizeOf(len(got)), quiet.Round(time.Second))
switch {
case bytes.Contains(got, []byte(`"b64_json"`)):
return "it was sending the image itself as b64_json; " + where
case bytes.Contains(got, []byte(`"url"`)):
return "it was answering with the image's url, not the image; " + where
}
return "what came doesn't yet say whether the image was coming as a url or as b64_json; " + where
}
// sizeOf is n bytes for a reader: 386 B, 12.4 KB, 3.0 MB.
func sizeOf(n int) string {
switch {
case n < 1000:
return fmt.Sprintf("%d B", n)
case n < 1000*1000:
return fmt.Sprintf("%.1f KB", float64(n)/1000)
}
return fmt.Sprintf("%.1f MB", float64(n)/1000/1000)
}
// vendorMessage is the message of a vendor's error, or its body.
func vendorMessage(b []byte) string {
var e struct {
Error json.RawMessage `json:"error"`
Errors struct {
Message string `json:"message"`
} `json:"errors"` // ModelScope's
Code json.RawMessage `json:"code"` // WorkBuddy's {code, msg}
Msg string `json:"msg"`
}
if json.Unmarshal(b, &e) == nil && e.Errors.Message != "" {
return e.Errors.Message
}
if json.Unmarshal(b, &e) == nil && len(e.Error) == 0 && e.Msg != "" {
if code := strings.Trim(string(e.Code), `"`); code != "" && code != "0" && code != "null" {
return e.Msg + " (" + code + ")"
}
}
if json.Unmarshal(b, &e) == nil && len(e.Error) > 0 {
var m struct {
Message string `json:"message"`
}
if json.Unmarshal(e.Error, &m) == nil && m.Message != "" {
return m.Message
}
var s string
if json.Unmarshal(e.Error, &s) == nil {
return s
}
}
return string(b)
}
// drawImages asks an images API: generations, or edits with the images
// sent along.
func (s *Server) drawImages(ctx context.Context, p provider.Provider, model string, d drawing) (drawn, int, error) {
if isModelScope(p) {
return s.drawModelScope(ctx, p, model, d)
}
base := strings.TrimRight(p.Base(provider.Chat), "/")
if base == "" {
// a Responses-only relay serves the images API under the same root
// as its Responses endpoint
base = strings.TrimRight(p.Base(provider.Responses), "/")
}
if drawsCodex(p) || drawsGrok(p) {
base = strings.TrimRight(p.Base(provider.Responses), "/")
}
var body []byte
var ct, url string
m := strings.ToLower(model)
if drawsGrok(p) {
// Grok's Imagine API takes an aspect ratio, not a size or a quality
req := map[string]any{"model": model, "prompt": d.Prompt, "n": d.N, "response_format": "b64_json"}
if ar := aspectAmong(d.Size, grokAspects); ar != "" {
req["aspect_ratio"] = ar
}
if len(d.Images) > 0 {
var imgs []map[string]string
for _, pic := range d.Images {
imgs = append(imgs, map[string]string{"url": pic.dataURL(), "type": "image_url"})
}
req["images"] = imgs
ct, url = "application/json", base+"/images/edits"
} else {
ct, url = "application/json", base+"/images/generations"
}
body, _ = json.Marshal(req)
} else if len(d.Images) == 0 {
req := map[string]any{"model": model, "prompt": d.Prompt, "n": d.N}
for k, v := range map[string]string{"size": d.Size, "quality": d.Quality, "background": d.Background, "output_format": d.Format} {
if v != "" {
req[k] = v
}
}
// only dall-e gives a URL unless asked; gpt-image turns the field away
if strings.Contains(m, "dall-e") {
req["response_format"] = "b64_json"
}
body, _ = json.Marshal(req)
ct, url = "application/json", base+"/images/generations"
} else if drawsCodex(p) {
// ChatGPT's backend takes an edit only as JSON, each image a data URL
req := map[string]any{"model": model, "prompt": d.Prompt, "n": d.N}
for k, v := range map[string]string{"size": d.Size, "quality": d.Quality, "background": d.Background, "output_format": d.Format} {
if v != "" {
req[k] = v
}
}
var imgs []map[string]string
for _, pic := range d.Images {
imgs = append(imgs, map[string]string{"image_url": pic.dataURL()})
}
req["images"] = imgs
if d.Mask != nil {
req["mask"] = map[string]string{"image_url": d.Mask.dataURL()}
}
body, _ = json.Marshal(req)
ct, url = "application/json", base+"/images/edits"
} else {
var buf bytes.Buffer
mw := multipart.NewWriter(&buf)
for k, v := range map[string]string{"model": model, "prompt": d.Prompt, "n": strconv.Itoa(d.N), "size": d.Size, "quality": d.Quality, "background": d.Background, "output_format": d.Format} {
if v != "" {
mw.WriteField(k, v)
}
}
if strings.Contains(m, "dall-e") {
mw.WriteField("response_format", "b64_json")
}
field := "image"
if len(d.Images) > 1 {
field = "image[]"
}
pics := append([]picture(nil), d.Images...)
fields := make([]string, len(pics))
for i := range pics {
fields[i] = field
}
if d.Mask != nil {
pics, fields = append(pics, *d.Mask), append(fields, "mask")
}
for i, pic := range pics {
if pic.URL != "" {
b, mt, err := s.fetchPicture(ctx, pic.URL)
if err != nil {
return drawn{}, 400, err
}
pic = picture{Mime: mt, Data: b}
}
h := textproto.MIMEHeader{}
h.Set("Content-Disposition", fmt.Sprintf(`form-data; name=%q; filename="image%d%s"`, fields[i], i, extOf(pic.Mime)))
h.Set("Content-Type", pic.Mime)
part, _ := mw.CreatePart(h)
part.Write(pic.Data)
}
mw.Close()
body, ct, url = buf.Bytes(), mw.FormDataContentType(), base+"/images/edits"
}
b, code, err := s.send(ctx, p, url, ct, body, true)
if err != nil {
return drawn{}, code, err
}
return readImagesAnswer(p, url, b, code)
}
// readImagesAnswer reads an images API's answer to url: its images, as bytes
// or the URLs the vendor keeps them at, and what they cost.
func readImagesAnswer(p provider.Provider, url string, b []byte, code int) (drawn, int, error) {
var res struct {
Data []struct {
B64 string `json:"b64_json"`
URL string `json:"url"`
Revised string `json:"revised_prompt"`
} `json:"data"`
OutputFormat string `json:"output_format"`
Usage struct {
Input int `json:"input_tokens"`
Output int `json:"output_tokens"`
} `json:"usage"`
}
if err := json.Unmarshal(b, &res); err != nil {
return drawn{}, 502, fmt.Errorf("%s's answer isn't an images API's: %v", p.Name, err)
}
out := drawn{Input: res.Usage.Input, Output: res.Usage.Output}
for _, e := range res.Data {
if e.Revised != "" {
out.Revised = e.Revised
}
if e.B64 != "" {
data, err := base64.StdEncoding.DecodeString(e.B64)
if err != nil {
return drawn{}, 502, fmt.Errorf("%s's image isn't base64: %v", p.Name, err)
}
out.Images = append(out.Images, picture{Mime: http.DetectContentType(data), Data: data})
} else if e.URL != "" {
out.Images = append(out.Images, picture{URL: e.URL})
}
}
if len(out.Images) == 0 {
// an answer with no image says why, or is shown as it came
return out, 502, fmt.Errorf("%s answered with no image%s", provider.HostOf(url), vendorSaid(vendorMessage(b)))
}
return out, code, nil
}
// fetchPicture downloads an image an edit was given by URL, for an images
// API that takes only files.
func (s *Server) fetchPicture(ctx context.Context, url string) ([]byte, string, error) {
b, _, err := FetchImage(ctx, s.client, url)
if err != nil {
return nil, "", fmt.Errorf("couldn't fetch the image at %s: %v", url, err)
}
return b, http.DetectContentType(b), nil
}
// inline says a response_format asks for the image itself.
func inline(format string) bool { return strings.EqualFold(format, "b64_json") }
// ImageStall is how long an image being downloaded may send nothing before
// the download is given up, and ImageLongest how long it may take in all.
// A slow download goes on while it moves: a 4K PNG is 10 MB and more, and
// a cap on the whole of it (generate_image's was 2 minutes) lost one that
// was still coming (#1576).
var (
ImageStall = time.Minute
ImageLongest = 5 * time.Minute
)
// maxImageBytes is the most of an image a download reads.
const maxImageBytes = 64 << 20
var errImageStalled = errors.New("the download stalled")
// FetchImage downloads the image at url with c: its bytes, and its type —
// what the bytes are, over what the server says. It gives up when nothing
// comes for ImageStall or it has taken ImageLongest, and says which, and
// how much had come.
func FetchImage(ctx context.Context, c *http.Client, url string) ([]byte, string, error) {
if !strings.HasPrefix(url, "http://") && !strings.HasPrefix(url, "https://") {
return nil, "", fmt.Errorf("%q isn't an http(s) URL", url)
}
ctx, cancel := context.WithTimeout(ctx, ImageLongest)
defer cancel()
ctx, stop := context.WithCancelCause(ctx)
defer stop(nil)
stall := time.AfterFunc(ImageStall, func() { stop(errImageStalled) })
defer stall.Stop()
got := 0
why := func(err error) error {
if ctx.Err() == nil {
return err
}
switch context.Cause(ctx) {
case errImageStalled:
return fmt.Errorf("nothing came for %s, after %s", ImageStall, mb(got))
case context.DeadlineExceeded:
return fmt.Errorf("only %s came in %s", mb(got), ImageLongest)
}
return err
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, "", err
}
res, err := c.Do(req)
if err != nil {
return nil, "", why(err)
}
defer res.Body.Close()
if res.StatusCode >= 300 {
return nil, "", errors.New(res.Status)
}
var buf bytes.Buffer
chunk := make([]byte, 256<<10)
for {
stall.Reset(ImageStall)
n, err := res.Body.Read(chunk)
buf.Write(chunk[:n])
if got = buf.Len(); got > maxImageBytes {
return nil, "", fmt.Errorf("it is over %s", mb(maxImageBytes))
}
if err == io.EOF {
break
}
if err != nil {
return nil, "", why(err)
}
}
b := buf.Bytes()
mt, _, _ := mime.ParseMediaType(res.Header.Get("Content-Type"))
if sniffed := http.DetectContentType(b); strings.HasPrefix(sniffed, "image/") || !strings.HasPrefix(mt, "image/") {
mt = sniffed
}
return b, mt, nil
}
// mb is n bytes as people read it.
func mb(n int) string { return fmt.Sprintf("%.1f MB", float64(n)/(1<<20)) }
func extOf(mimeType string) string {
switch mimeType {
case "image/jpeg":
return ".jpg"
case "image/webp":
return ".webp"
case "image/gif":
return ".gif"
}
return ".png"
}
// drawPrompt is the prompt with what an images API takes as parameters
// said in words, for a model asked in chat.
func drawPrompt(d drawing) string {
var b strings.Builder
b.WriteString(d.Prompt)
if r := aspectOf(d.Size); r != "" {
fmt.Fprintf(&b, "\n\nAspect ratio: %s.", r)
}
if d.Background == "transparent" {
b.WriteString(" Transparent background.")
}
return b.String()
}
// drawChat asks a chat model to answer with an image, once for each image
// asked for.
func (s *Server) drawChat(ctx context.Context, p provider.Provider, model string, d drawing) (drawn, int, error) {
content := []map[string]any{{"type": "text", "text": drawPrompt(d)}}
for _, pic := range d.Images {
content = append(content, map[string]any{"type": "image_url", "image_url": map[string]string{"url": pic.dataURL()}})
}
req := map[string]any{"model": model, "stream": false, "modalities": []string{"image", "text"},
"messages": []map[string]any{{"role": "user", "content": content}}}
if r := aspectOf(d.Size); r != "" {
req["image_config"] = map[string]string{"aspect_ratio": r}
}
body, _ := json.Marshal(req)
url := strings.TrimRight(p.Base(provider.Chat), "/") + "/chat/completions"
return s.eachDrawing(d.N, func() (drawn, int, error) {
b, code, err := s.send(ctx, p, url, "application/json", p.Prepare(body), true)
if err != nil {
return drawn{}, code, err
}
var res struct {
Choices []struct {
Message struct {
Content json.RawMessage `json:"content"`
Images []struct {
ImageURL struct {
URL string `json:"url"`
} `json:"image_url"`
} `json:"images"`
// AIHubMix's Gemini: the parts as Gemini gives them
MultiMod []struct {
InlineData struct {
Data string `json:"data"`
MimeType string `json:"mime_type"`
} `json:"inline_data"`
} `json:"multi_mod_content"`
} `json:"message"`
} `json:"choices"`
Usage struct {
Prompt int `json:"prompt_tokens"`
Completion int `json:"completion_tokens"`
} `json:"usage"`
}
if err := json.Unmarshal(b, &res); err != nil {
return drawn{}, 502, fmt.Errorf("%s's answer isn't chat completions': %v", p.Name, err)
}
out := drawn{Input: res.Usage.Prompt, Output: res.Usage.Completion}
if len(res.Choices) == 0 {
return out, code, nil
}
msg := res.Choices[0].Message
var srcs []string
for _, im := range msg.Images {
srcs = append(srcs, im.ImageURL.URL)
}
for _, part := range msg.MultiMod {
if in := part.InlineData; in.Data != "" {
srcs = append(srcs, "data:"+cmp.Or(in.MimeType, "image/png")+";base64,"+in.Data)
}
}
var text string
if json.Unmarshal(msg.Content, &text) != nil {
var parts []struct {
Type string `json:"type"`
Text string `json:"text"`
ImageURL json.RawMessage `json:"image_url"`
}
json.Unmarshal(msg.Content, &parts)
for _, part := range parts {
text += part.Text
srcs = append(srcs, imageSources(part.ImageURL)...)
}
}
out.Text = strings.TrimSpace(text)
// a model that puts the image in its text, as markdown
if len(srcs) == 0 {
for _, f := range strings.Split(text, "(data:image/")[1:] {
if end := strings.IndexByte(f, ')'); end > 0 {
srcs = append(srcs, "data:image/"+f[:end])
}
}
}
for _, src := range srcs {
pic, err := pictureOf(src)
if err != nil {
return out, 502, fmt.Errorf("%s's image: %v", p.Name, err)
}
out.Images = append(out.Images, pic)
}
return out, code, nil
})
}
// drawGemini asks Gemini at Google on its own API, which answers with
// images where its OpenAI-compatible one doesn't.
func (s *Server) drawGemini(ctx context.Context, p provider.Provider, model string, d drawing) (drawn, int, error) {
parts := []map[string]any{{"text": drawPrompt(d)}}
for _, pic := range d.Images {
if pic.URL != "" {
b, mt, err := s.fetchPicture(ctx, pic.URL)
if err != nil {
return drawn{}, 400, err
}
pic = picture{Mime: mt, Data: b}
}
parts = append(parts, map[string]any{"inlineData": map[string]string{"mimeType": pic.Mime, "data": base64.StdEncoding.EncodeToString(pic.Data)}})
}
cfg := map[string]any{"responseModalities": []string{"TEXT", "IMAGE"}}
if r := aspectOf(d.Size); r != "" {
cfg["imageConfig"] = map[string]string{"aspectRatio": r}
}
body, _ := json.Marshal(map[string]any{"contents": []map[string]any{{"role": "user", "parts": parts}}, "generationConfig": cfg})
base := strings.TrimSuffix(strings.TrimRight(p.Base(provider.Chat), "/"), "/openai")
url := base + "/models/" + model + ":generateContent"
return s.eachDrawing(d.N, func() (drawn, int, error) {
b, code, err := s.send(ctx, p, url, "application/json", body, false)
if err != nil {
return drawn{}, code, err
}
var res struct {
Candidates []struct {
Content struct {
Parts []struct {
Text string `json:"text"`
Thought bool `json:"thought"`
InlineData *struct {
MimeType string `json:"mimeType"`
Data string `json:"data"`
} `json:"inlineData"`
} `json:"parts"`
} `json:"content"`
} `json:"candidates"`
Usage struct {
Prompt int `json:"promptTokenCount"`
Candidates int `json:"candidatesTokenCount"`
} `json:"usageMetadata"`
}
if err := json.Unmarshal(b, &res); err != nil {
return drawn{}, 502, fmt.Errorf("%s's answer isn't Gemini's: %v", p.Name, err)
}
out := drawn{Input: res.Usage.Prompt, Output: res.Usage.Candidates}
for _, c := range res.Candidates {
for _, part := range c.Content.Parts {
switch {
case part.InlineData != nil && !part.Thought:
data, err := base64.StdEncoding.DecodeString(part.InlineData.Data)
if err != nil {
return out, 502, fmt.Errorf("%s's image isn't base64: %v", p.Name, err)
}
out.Images = append(out.Images, picture{Mime: part.InlineData.MimeType, Data: data})
case part.Text != "" && !part.Thought:
out.Text += part.Text
}
}
}
out.Text = strings.TrimSpace(out.Text)
return out, code, nil
})
}
// drawCodeAssist asks an image model on a Google sign-in to draw, as it is
// asked to chat: on Code Assist, in the account's envelope, which asks for
// images beside text for an image model (buildCodeAssistSent); what it
// draws comes back as the pictures of its reply.
func (s *Server) drawCodeAssist(ctx context.Context, p provider.Provider, model string, d drawing) (drawn, int, error) {
parts := []Part{{Kind: Text, Text: drawPrompt(d)}}
for _, pic := range d.Images {
if pic.URL != "" {
b, mt, err := s.fetchPicture(ctx, pic.URL)
if err != nil {
return drawn{}, 400, err
}
pic = picture{Mime: mt, Data: b}
}
parts = append(parts, Part{Kind: Image, MediaType: pic.Mime, Data: base64.StdEncoding.EncodeToString(pic.Data)})
}
req := &Request{Messages: []Message{{Role: "user", Parts: parts}}}
return s.eachDrawing(d.N, func() (drawn, int, error) {
// one each: the drawings are asked for at once
ask := s.askTranslated(p, provider.CodeAssist, model, http.Header{}, http.Header{})
events, code, msg := ask(ctx, req)
if events == nil {
return drawn{}, code, errors.New(msg)
}
var out drawn
var failed string
for ev := range events {
switch ev.Kind {
case KImage:
data, err := base64.StdEncoding.DecodeString(ev.Text)
if err != nil {
failed = fmt.Sprintf("%s's image isn't base64: %v", p.Name, err)
continue
}
out.Images = append(out.Images, picture{Mime: cmp.Or(ev.Name, "image/png"), Data: data})
case KText:
out.Text += ev.Text
case KUsage:
out.Input += ev.Usage.Input + ev.Usage.CacheRead
out.Output += ev.Usage.Output
case KError:
failed = p.Name + ": " + ev.Text
}
}
out.Text = strings.TrimSpace(out.Text)
if failed != "" && len(out.Images) == 0 {
return out, 502, errors.New(failed)
}
return out, 200, nil
})
}
// eachDrawing asks once for each of n images, at once, for a vendor that
// draws one image an answer; what they drew and cost is summed.
func (s *Server) eachDrawing(n int, ask func() (drawn, int, error)) (drawn, int, error) {
type result struct {
out drawn
code int
err error
}
results := make([]result, n)
var wg sync.WaitGroup
for i := range n {
wg.Add(1)
go func() {
defer wg.Done()
o, c, e := ask()
results[i] = result{o, c, e}
}()
}
wg.Wait()
var out drawn
code := 200
var firstErr error
for _, r := range results {
out.Input += r.out.Input
out.Output += r.out.Output
out.Images = append(out.Images, r.out.Images...)
if r.out.Text != "" && out.Text == "" {
out.Text = r.out.Text
}
if r.err != nil && firstErr == nil {
firstErr, code = r.err, r.code
}
}
// some images drawn is an answer; none, the first failure
if len(out.Images) == 0 && firstErr != nil {
return out, code, firstErr
}
return out, 200, nil
}
// aspectOf is the ratio an images API size ("1536x1024") is, as the models
// that take a ratio name it: the nearest of those they take. "" for none
// or "auto".
func aspectOf(size string) string {
return aspectAmong(size, []string{"1:1", "2:3", "3:2", "3:4", "4:3", "4:5", "5:4", "9:16", "16:9", "21:9"})
}
// grokAspects are the aspect ratios Grok's Imagine API takes.
var grokAspects = []string{"1:1", "16:9", "9:16", "4:3", "3:4", "3:2", "2:3", "2:1", "1:2", "19.5:9", "9:19.5", "20:9", "9:20"}
// aspectAmong is size as the nearest of the aspect ratios in ratios: a
// ratio given as one passes through, WIDTHxHEIGHT is matched to the closest.
func aspectAmong(size string, ratios []string) string {
w, h, ok := strings.Cut(strings.ToLower(strings.TrimSpace(size)), "x")
if !ok {
if strings.Contains(size, ":") {
return strings.TrimSpace(size)
}
return ""
}
x, err1 := strconv.ParseFloat(w, 64)
y, err2 := strconv.ParseFloat(h, 64)
if err1 != nil || err2 != nil || x <= 0 || y <= 0 {
return ""
}
best, bestD := "", math.Inf(1)
for _, r := range ratios {
a, b, _ := strings.Cut(r, ":")
ra, _ := strconv.ParseFloat(a, 64)
rb, _ := strconv.ParseFloat(b, 64)
if d := math.Abs(math.Log(x/y) - math.Log(ra/rb)); d < bestD {
best, bestD = r, d
}
}
return best
}