Files
magpie/internal/gateway/image_cap.go
T
yetone afd2df09b9 gateway: a Copilot Gemini conversation past the model's 10 images goes on with its oldest left out (yuushabu_buyinn on X)
Copilot refuses a Gemini request whose conversation holds more images than
the model takes ("too many images: maximum allowed for model
gemini-4-carbon is 10, got 12"), and it counts every image in the
conversation, not one message's. A long chat with screenshots stopped
working for good.

Now, on that 400 from a Copilot account, the gateway learns the cap the
error states (per provider and model), replaces the oldest images outside
the latest user message with a one-line placeholder until the count fits,
and sends again once. Later turns are fitted before they are sent. A
request under the cap is never changed. If the latest user message alone
is over the cap, the refusal is passed on as it came. Other vendors are
untouched (imageCapped: Copilot accounts only).

The cap is learned from the 400, not from /models' max_prompt_images:
Copilot lists 1 for gpt-* and 5 for Claude models but took 3 images on
gpt-4.1 and 7 on claude-sonnet-5 live, so the list is enforced only for
Gemini and would strip images that are fine.

Run (suite at the pre-rebase tree 4649e2269; after rebasing on 58cf76453, internal/gateway ran again: ok, 923s; on b9c385286 the targeted gateway tests above plus Advisor: ok; and go vet ./internal/... plus linux and windows vet of gateway and provider: ok):
- Red on the old code: with the two gateway.go hooks taken out,
  TestCopilotGeminiImagesOverCap and the follow-up turn fail with 400
  "too many images"; green with them.
- go test ./internal/gateway -run 'ImageCap|CopilotGeminiImages|ByteCap|
  OverByteCap|CopilotGemini|BodyCap|Compaction': ok.
- Full suite on mote (go test ./... -timeout 45m, one suite on the box):
  101 packages ok, no FAIL (gateway 1049s, agent 447s, provider 277s).
- go vet for darwin, linux and windows: ok. gofmt -l: clean.
- Real Copilot (gh token, sandbox HOME, magpie serve -tags nogui on mote):
  gemini-4-carbon with 12 images: 200 after one refusal and a resend with
  10; the next turn with 12: 200, fitted before sending; 10 images: 200
  unchanged; claude-sonnet-5 with 12 images: 200 unchanged.

Not tried: Copilot Gemini over Responses or Anthropic routes (Copilot
serves Gemini on /chat/completions only); a GUI path (none changed).
2026-11-16 14:51:54 +08:00

120 lines
4.5 KiB
Go

package gateway
import (
"bytes"
"context"
"log"
"net/http"
"strconv"
"github.com/yetone/magpie/internal/lazyre"
"github.com/yetone/magpie/internal/provider"
)
// A model's cap on the images of one request.
//
// Copilot's Gemini models take at most 10 images in a request, the whole
// conversation's counted: past them every turn is refused 400 "too many
// images: maximum allowed for model gemini-4-carbon is 10, got 12", and a
// conversation that has shown the model ten screenshots can't go on
// (yuushabu_buyinn on X). Copilot's /models lists a max_prompt_images
// for every model, but only Gemini's is held to (probed live: gpt-4.1,
// listed at 1, took 3; claude-sonnet-5, listed at 5, took 7), so the cap
// is learned from the refusal, which names it, by account and model.
//
// The refused request is sent again with its oldest images each replaced
// by a short text, as many as bring it to the cap, and the account's
// later requests to that model are fitted the same way before they are
// sent (forwardOnce). The latest user message is kept whole; when its own
// images are over the cap, nothing is left out and the refusal goes on.
// A request under the cap, and every other vendor's, goes as it came.
// imageCapWords are Copilot's words for a request over a model's image
// cap, with the cap.
var imageCapWords = lazyre.New(`(?i)too many images: maximum allowed for model \S+ is (\d+)`)
// imageOverCap stands where an image over the cap was left out. It goes
// to the vendor, so it names neither magpie nor the vendor.
const imageOverCap = "[An earlier image was omitted here to keep this request within the model's image limit.]"
// imageCapped is whether p's refusals of too many images are learned:
// Copilot's alone say so.
func imageCapped(p provider.Provider) bool {
return p.Account != nil && p.Account.Agent == "copilot"
}
// imageCapKey is where the cap of p's model is kept.
func imageCapKey(p provider.Provider, body []byte) string {
return p.ID + "\x00" + modelOf(body)
}
// withinImageCap is body as it goes to p: its oldest images left out
// down to the cap p's model refused more images over, when one is known
// and body is over it.
func (s *Server) withinImageCap(p provider.Provider, to provider.Protocol, path string, body []byte) []byte {
if !imageCapped(p) || countsTokens(path) {
return body
}
n := s.imageCaps.of(imageCapKey(p, body))
if n <= 0 {
return body
}
out, left, ok := withoutImagesOver(to, body, n)
if ok && left > 0 {
log.Printf("%s: the request has more images than the %d %s takes; the %d oldest left out", p.Name, n, modelOf(body), left)
return out
}
return body
}
// withoutImagesOver is body with its oldest history images replaced by
// imageOverCap, as many as bring it to most images; the rest of it byte
// for byte as it came. ok is false, and body as it came, when leaving
// every earlier one out doesn't bring it there.
func withoutImagesOver(to provider.Protocol, body []byte, most int) (out []byte, left int, ok bool) {
over := len(bodyImages(to, body, 0, true, imageOverCap)) - most
if over <= 0 {
return body, 0, true
}
imgs := bodyImages(to, body, 0, false, imageOverCap)
if len(imgs) < over {
return body, 0, false
}
var b bytes.Buffer
b.Grow(len(body))
last := 0
for _, im := range imgs[:over] {
b.Write(body[last:im.at])
b.WriteString(im.with)
last = im.end
}
b.Write(body[last:])
return b.Bytes(), over, true
}
// refitImageCap is forward's answer to res, Copilot's refusal of body for
// too many images: the cap learned, and the request sent again with its
// oldest images left out, when that brings it to the cap.
func (s *Server) refitImageCap(ctx context.Context, p provider.Provider, to provider.Protocol, path string, body []byte, in http.Header, res *http.Response) (*http.Response, error) {
if res.StatusCode != http.StatusBadRequest || !imageCapped(p) || countsTokens(path) {
return res, nil
}
b := peekError(res)
m := imageCapWords.FindStringSubmatch(string(wordsOf(b)))
if m == nil {
return res, nil
}
n, err := strconv.Atoi(m[1])
if err != nil || n <= 0 {
return res, nil
}
n = s.imageCaps.learn(imageCapKey(p, body), n)
if _, left, ok := withoutImagesOver(to, body, n); !ok || left == 0 {
return res, nil
}
log.Printf("%s refused a request to %s for its images (HTTP %d: %s); asking again with no more than %d", p.Name, modelOf(body), res.StatusCode, provider.APIError(b, ""), n)
res.Body.Close()
// forwardOnce fits it to the cap just learned
return s.forwardOnce(ctx, p, to, path, body, in)
}