matrix0487 on Discord: since images go to DeepSeek by file id, the Routing card shows no pictures and the Sessions page loses the turns. Cause: the views keep the body as the agent sent it, with every picture inline, and they cap it. The Routing card keeps a body of up to 32 MiB (promptBodyMax), and its archive read refuses a larger one. A gateway-recorded turn keeps 8 MiB of input (conversationBodyMax), and cutting it there leaves JSON that doesn't parse. Before #5407, DeepSeek refused more than 64 MiB inline, so a conversation never got far past these caps. With file ids it does: the reporter's turns send about 30 MB of pictures each. The card then says the text is gone and draws nothing, and the Sessions turn keeps only the reply. Fix: leanImages swaps each inline picture's base64 (a data:image/... URL, or a bare Anthropic or Gemini source that starts as a PNG, JPEG, GIF or WebP does, 1 KiB or more, with no escapes) for a short ref before the caps apply. - Routing card: the kept body and the archive's copy (before its cap) keep their pictures in keptImages. It is memory only and bounded: 128 MiB, least recently used first, and the same picture is kept once. dataImage reads a ref back to the data: URL. A ref whose picture has gone draws nothing. - Sessions: the recorded input drops the base64 outright, since the page shows a picture as [image]. What goes upstream, what the archive stores, and the Recent calls' body are unchanged. Run (mote, through mote-run): - go test ./internal/gateway -run 'Prompt|Context|Conversation| DeepSeek|Archive|Image|Lean|FileID': ok. - go test ./internal/gui -run 'Context|Gateway|Sessions': ok. - go vet: darwin gateway+gui, GOOS=linux -tags nogui, GOOS=windows. - gofmt -l: clean. TestFileIDConversationStillShows covers chat, responses and anthropic. Each sends a 36 MiB turn of three 12 MiB pictures through the fake DeepSeek, which names them by file id. It checks: - the card draws all three, from memory and from the archive; - the chat text is still there; - the Sessions transcript holds the turn's text and three [image]. Broken by hand, each one red: - wiring removed: memory, 0 pictures drawn; - the archive's cap put back before the lean: archive not read; - the conversation lean removed: the transcript keeps only the reply, cut:true (the reporter's "会话内容也看不到了"); - the ref not resolved: memory, 0 pictures drawn. Not tried: - the reporter's real DeepSeek account and the GUI by eye. No GUI file changed, so the GUI suite was not run. - masking turned on explicitly with a lean body. The refs are plain text, so they are not expected to match a mask. - Sessions read from an agent's own transcript (Claude Code, Codex) never pass through the gateway, so #5407 cannot touch them. Reported-by: matrix0487
176 lines
4.7 KiB
Go
176 lines
4.7 KiB
Go
package gateway
|
|
|
|
// A request's inline images, kept apart from its text for the views that
|
|
// show the request again: the Routing card and the Sessions page
|
|
// (matrix0487 on Discord). Sending images to DeepSeek by file id (#5407)
|
|
// let a conversation go past the 64 MiB DeepSeek takes inline, and its
|
|
// bodies past what those views keep: a body over promptBodyMax lost the
|
|
// card's text and pictures, and one over conversationBodyMax its turn.
|
|
// Almost all of such a body is base64, so each view keeps it with every
|
|
// picture swapped for a short ref, and the card reads the picture back
|
|
// from keptImages, in memory only and bounded, as the bodies are.
|
|
|
|
import (
|
|
"bytes"
|
|
"container/list"
|
|
"crypto/sha256"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
)
|
|
|
|
const (
|
|
// imageRef stands in a lean body for a picture's base64, followed by
|
|
// its id in keptImages; never sent anywhere, and not base64, so a
|
|
// picture's checks never take it for one
|
|
imageRef = "magpie-image:"
|
|
// leanMin is the shortest string swapped: a smaller picture costs
|
|
// less than its ref's bookkeeping
|
|
leanMin = 1 << 10
|
|
// keptImagesMax is what the kept pictures take at most together, the
|
|
// least recently used going first
|
|
keptImagesMax = 128 << 20
|
|
)
|
|
|
|
type keptImage struct {
|
|
id int64
|
|
sum [32]byte
|
|
b64 string
|
|
}
|
|
|
|
var keptImages = struct {
|
|
sync.Mutex
|
|
bySum map[[32]byte]*list.Element
|
|
byID map[int64]*list.Element
|
|
lru *list.List // front: the newest
|
|
size int
|
|
next int64
|
|
}{bySum: map[[32]byte]*list.Element{}, byID: map[int64]*list.Element{}, lru: list.New()}
|
|
|
|
// keepImage keeps a picture's base64, giving its id; the same picture
|
|
// keeps one id.
|
|
func keepImage(b64 []byte) int64 {
|
|
sum := sha256.Sum256(b64)
|
|
k := &keptImages
|
|
k.Lock()
|
|
defer k.Unlock()
|
|
if e, ok := k.bySum[sum]; ok {
|
|
k.lru.MoveToFront(e)
|
|
return e.Value.(*keptImage).id
|
|
}
|
|
k.next++
|
|
im := &keptImage{id: k.next, sum: sum, b64: string(b64)}
|
|
e := k.lru.PushFront(im)
|
|
k.bySum[sum], k.byID[im.id] = e, e
|
|
k.size += len(im.b64)
|
|
for k.size > keptImagesMax && k.lru.Len() > 1 {
|
|
old := k.lru.Back().Value.(*keptImage)
|
|
k.lru.Remove(k.lru.Back())
|
|
delete(k.bySum, old.sum)
|
|
delete(k.byID, old.id)
|
|
k.size -= len(old.b64)
|
|
}
|
|
return im.id
|
|
}
|
|
|
|
// keptImageOf is the base64 of the picture a ref names; false when it
|
|
// has gone.
|
|
func keptImageOf(ref string) (string, bool) {
|
|
id, err := strconv.ParseInt(ref, 10, 64)
|
|
if err != nil {
|
|
return "", false
|
|
}
|
|
k := &keptImages
|
|
k.Lock()
|
|
defer k.Unlock()
|
|
e, ok := k.byID[id]
|
|
if !ok {
|
|
return "", false
|
|
}
|
|
k.lru.MoveToFront(e)
|
|
return e.Value.(*keptImage).b64, true
|
|
}
|
|
|
|
// imageMagic is how a PNG, JPEG, GIF or WebP's base64 starts: an
|
|
// Anthropic or Gemini source's data, which carries no data: head.
|
|
var imageMagic = []string{"iVBORw0KGg", "/9j/", "R0lGOD", "UklGR"}
|
|
|
|
// imagePayload is where a JSON string's picture base64 starts: after a
|
|
// data:image/…;base64, head, or at 0 for bare base64 that starts as a
|
|
// picture does. False for anything else.
|
|
func imagePayload(s []byte) (int, bool) {
|
|
at := 0
|
|
if bytes.HasPrefix(s, []byte("data:")) {
|
|
head, _, ok := bytes.Cut(s, []byte(","))
|
|
if !ok {
|
|
return 0, false
|
|
}
|
|
h := strings.ToLower(string(head))
|
|
if !strings.HasPrefix(h, "data:image/") || !strings.HasSuffix(h, ";base64") {
|
|
return 0, false
|
|
}
|
|
at = len(head) + 1
|
|
} else {
|
|
magic := false
|
|
for _, m := range imageMagic {
|
|
magic = magic || bytes.HasPrefix(s, []byte(m))
|
|
}
|
|
if !magic {
|
|
return 0, false
|
|
}
|
|
}
|
|
if len(s)-at < leanMin {
|
|
return 0, false
|
|
}
|
|
for _, c := range s[at:] {
|
|
if !('A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' || '0' <= c && c <= '9' || c == '+' || c == '/' || c == '=') {
|
|
return 0, false
|
|
}
|
|
}
|
|
return at, true
|
|
}
|
|
|
|
// leanImages is a JSON body with each inline picture's base64 swapped for
|
|
// a ref: kept in keptImages for the card to draw again when keep, a bare
|
|
// ref otherwise (the Sessions page shows a picture as [image]). The body
|
|
// itself when it has none. Only JSON strings without escapes are read, so
|
|
// the body's shape is never changed.
|
|
func leanImages(body []byte, keep bool) []byte {
|
|
var out []byte
|
|
last := 0
|
|
for i := 0; i < len(body); i++ {
|
|
if body[i] != '"' {
|
|
continue
|
|
}
|
|
j, escaped := i+1, false
|
|
for ; j < len(body) && body[j] != '"'; j++ {
|
|
if body[j] == '\\' {
|
|
escaped = true
|
|
j++
|
|
}
|
|
}
|
|
if j >= len(body) {
|
|
break
|
|
}
|
|
if s := body[i+1 : j]; !escaped && len(s) >= leanMin {
|
|
if at, ok := imagePayload(s); ok {
|
|
if out == nil {
|
|
out = make([]byte, 0, len(body)/8)
|
|
}
|
|
ref := imageRef
|
|
if keep {
|
|
ref += strconv.FormatInt(keepImage(s[at:]), 10)
|
|
}
|
|
out = append(out, body[last:i+1+at]...)
|
|
out = append(out, ref...)
|
|
last = j
|
|
}
|
|
}
|
|
i = j
|
|
}
|
|
if out == nil {
|
|
return body
|
|
}
|
|
return append(out, body[last:]...)
|
|
}
|