The gateway reads one request's body a dozen times or more on its way:
modelOf, streamOf, conversationID, requestEffort, bodyEffort,
walkImages and swapImageBlocks (more than once through a group), turnOf's
parse, promptOf, chatToolNames, thinkingOffUnlessAsked, decodeRequest,
and for Responses pairToolItems, orphanedToolOutputs,
hasSealedAgentMessage and askedTier. Each read took a field or two with
json.Unmarshal, which checks the whole input and then steps through every
byte of every string one call at a time, so each read cost as much as
decoding the whole conversation: 74-77% of a request's CPU in pprof, most
of it checkValid and stateInString.
decodeJSON (fastjson.go) is json.Unmarshal for these reads. It checks the
body once with its own validator (json.Valid's grammar and 10000 depth,
8 string bytes at a time), splits objects and arrays at their members
without decoding them, and decodes only what dst has a place for:
strings, booleans, json.RawMessage (copied, as encoding/json does),
structs, pointers, slices and string-keyed maps by their parts, and
everything else (numbers, interfaces, types that decode themselves) by
encoding/json itself. Whatever it doesn't take on exactly as
json.Unmarshal would goes to json.Unmarshal with dst zeroed: invalid
input, a dst already holding something, an embedded field, a ",string"
option, two fields of one name, a key given twice, a value of the wrong
type. A list or object where dst takes a string (a "string or parts"
read) is answered by json.Unmarshal of its opening and close, which gives
the same error at the same offset without reading the rest. The request
paths' json.Unmarshal calls in gateway.go, decide.go, affinity.go,
image_input.go, textcalls.go, anthropic.go, ir.go, prompt.go, chat.go,
chattidy.go and responses.go, and hasSealedAgentMessage and askedTier,
now call it. Nothing that writes a body changed.
Numbers, M4 Pro under load 10-20 from other sessions, BenchmarkLargeRequest
(new: a 2 MB agent conversation with 30 tools, tool calls and results,
through a fake vendor), 20 requests each:
CPU in the request's handler time per request
(pprof, per request) (ns/op)
anthropic 154ms -> 45ms 208-212ms -> 52ms
anthropic, group 178ms -> 51ms 249-262ms -> 65-67ms
chat 122ms -> 35ms 167-254ms -> 50-54ms
responses 215ms -> 56ms 294-452ms -> 70ms
TestLargeRequestEndpoints (64 MB padded bodies): 16.8-17.6s -> 2.9-4.8s
(skipping runs of spaces 8 bytes at a time took its last ~5s).
Tests:
- TestUpstreamBodiesUnchanged sends 18 requests (Anthropic, Chat and
Responses; direct, through a group, translated to each other protocol,
with images, thinking, escapes, odd spacing, a repeated key, and four
2 MB conversations) and compares what the vendor gets with
testdata/upstream_bodies, recorded on the code before this change
(all 18 identical; three runs agreed). Breaking decodeJSON's \n
unescape by hand turns the three translated cases holding one red.
- TestDecodeJSONIsUnmarshal / FuzzDecodeJSON compare decodeJSON with
json.Unmarshal (dst DeepEqual, error nil-ness) for 19 types shaped like
the gateway's, and jsonValid with json.Valid, over seeds, every cut
and a changed byte at every place of each seed, and 2.5 minutes of
fuzzing (~1.75M inputs). Breaking the validator's control-byte check
by hand turns it red.
- go test -tags nogui -timeout 40m ./... on this commit rebased onto
origin/main 498b77fbc, with #1667's fix below it: all 48 packages ok
(gateway 394s).
gofmt -l clean; GOOS=linux and GOOS=windows go vet -tags nogui
./internal/gateway clean.
Not tried: real vendor traffic; Windows and Linux runs (vet only).
260 lines
7.5 KiB
Go
260 lines
7.5 KiB
Go
package gateway
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"strings"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
const imageOmitted = "[Image omitted: this model accepts text only.]"
|
|
|
|
// errCurrentImage stops a walk at an image the latest turn gives the model.
|
|
var errCurrentImage = errors.New("an image in the latest turn")
|
|
|
|
// textOnlyBody rejects a user image in the latest turn and omits tool images
|
|
// and images in older turns. It edits the wire JSON so passthrough keeps fields
|
|
// the IR does not represent (such as provider-specific request options).
|
|
func textOnlyBody(proto provider.Protocol, body []byte) ([]byte, bool) {
|
|
out, err := walkImages(proto, body, func(im imageAt) (json.RawMessage, error) {
|
|
if im.Current() {
|
|
return nil, errCurrentImage
|
|
}
|
|
return textBlock(proto, imageOmitted), nil
|
|
})
|
|
if err != nil {
|
|
return body, true
|
|
}
|
|
return out, false
|
|
}
|
|
|
|
// hasImage is whether the body has an image in any turn.
|
|
func hasImage(proto provider.Protocol, body []byte) bool {
|
|
found := errors.New("found")
|
|
_, err := walkImages(proto, body, func(imageAt) (json.RawMessage, error) { return nil, found })
|
|
return err == found
|
|
}
|
|
|
|
// hasUnportableCurrentImage reports a current Responses image with no URL to
|
|
// carry to another protocol. A file_id belongs to the Responses provider that
|
|
// stored it; translating it as a URL or dropping it would mislead the model.
|
|
func hasUnportableCurrentImage(body []byte) bool {
|
|
missing := errors.New("current image has no portable source")
|
|
_, err := walkImages(provider.Responses, body, func(im imageAt) (json.RawMessage, error) {
|
|
if im.Current() && im.Src == "" {
|
|
return nil, missing
|
|
}
|
|
return nil, nil
|
|
})
|
|
return err == missing
|
|
}
|
|
|
|
// imageAt is an image in a request, where it is.
|
|
type imageAt struct {
|
|
// Src is the image as a URL, a data: URL for one sent inline; empty for
|
|
// one only its vendor can open (a Responses file_id, a Gemini file).
|
|
Src string
|
|
// Last is set on one in the latest turn, Tool on one a tool gave back.
|
|
Last, Tool bool
|
|
}
|
|
|
|
// Current is whether the image is one the user gives the model now, which
|
|
// the model is asked about.
|
|
func (im imageAt) Current() bool { return im.Last && !im.Tool }
|
|
|
|
// walkImages gives every image in the body's turns to swap, and puts the
|
|
// block it returns in the image's place (nil keeps it). An error from swap
|
|
// ends the walk. The body is returned as it was when nothing was swapped.
|
|
func walkImages(proto provider.Protocol, body []byte, swap func(imageAt) (json.RawMessage, error)) ([]byte, error) {
|
|
var request map[string]json.RawMessage
|
|
if decodeJSON(body, &request) != nil {
|
|
return body, nil
|
|
}
|
|
turnsKey, contentKey := "messages", "content"
|
|
switch proto {
|
|
case provider.Responses:
|
|
turnsKey = "input"
|
|
case provider.Gemini:
|
|
turnsKey, contentKey = "contents", "parts"
|
|
}
|
|
var turns []json.RawMessage
|
|
if decodeJSON(request[turnsKey], &turns) != nil {
|
|
return body, nil
|
|
}
|
|
changed := false
|
|
for i, raw := range turns {
|
|
var turn map[string]json.RawMessage
|
|
if decodeJSON(raw, &turn) != nil {
|
|
continue
|
|
}
|
|
key := contentKey
|
|
tool := false
|
|
var kind string
|
|
if proto == provider.Responses {
|
|
decodeJSON(turn["type"], &kind)
|
|
if kind == "function_call_output" {
|
|
key, tool = "output", true
|
|
}
|
|
}
|
|
if proto == provider.Chat {
|
|
decodeJSON(turn["role"], &kind)
|
|
tool = kind == "tool"
|
|
}
|
|
content, swapped, err := swapImageBlocks(proto, turn[key], i == len(turns)-1, tool, swap)
|
|
if err != nil {
|
|
return body, err
|
|
}
|
|
if !swapped {
|
|
continue
|
|
}
|
|
turn[key] = content
|
|
turns[i], _ = json.Marshal(turn)
|
|
changed = true
|
|
}
|
|
if !changed {
|
|
return body, nil
|
|
}
|
|
request[turnsKey], _ = json.Marshal(turns)
|
|
return json.Marshal(request)
|
|
}
|
|
|
|
// swapImageBlocks swaps the images among a turn's blocks, and those in an
|
|
// Anthropic tool_result's, which are a tool's.
|
|
func swapImageBlocks(proto provider.Protocol, raw json.RawMessage, last, tool bool, swap func(imageAt) (json.RawMessage, error)) (json.RawMessage, bool, error) {
|
|
var blocks []json.RawMessage
|
|
if decodeJSON(raw, &blocks) != nil {
|
|
return raw, false, nil
|
|
}
|
|
swapped := false
|
|
for i, rawBlock := range blocks {
|
|
var block map[string]json.RawMessage
|
|
if decodeJSON(rawBlock, &block) != nil {
|
|
continue
|
|
}
|
|
var kind string
|
|
decodeJSON(block["type"], &kind)
|
|
src, isImage := imageSrc(proto, kind, block)
|
|
if isImage {
|
|
out, err := swap(imageAt{Src: src, Last: last, Tool: tool})
|
|
if err != nil {
|
|
return raw, false, err
|
|
}
|
|
if out != nil {
|
|
blocks[i] = out
|
|
swapped = true
|
|
}
|
|
continue
|
|
}
|
|
// Gemini's function response holds a tool's images in its own parts
|
|
if proto == provider.Gemini && len(block["functionResponse"]) > 0 {
|
|
var fr map[string]json.RawMessage
|
|
if decodeJSON(block["functionResponse"], &fr) == nil && len(fr["parts"]) > 0 {
|
|
content, nested, err := swapImageBlocks(proto, fr["parts"], last, true, swap)
|
|
if err != nil {
|
|
return raw, false, err
|
|
}
|
|
if nested {
|
|
fr["parts"] = content
|
|
block["functionResponse"], _ = json.Marshal(fr)
|
|
blocks[i], _ = json.Marshal(block)
|
|
swapped = true
|
|
}
|
|
}
|
|
}
|
|
if proto == provider.Anthropic && kind == "tool_result" {
|
|
content, nested, err := swapImageBlocks(proto, block["content"], last, true, swap)
|
|
if err != nil {
|
|
return raw, false, err
|
|
}
|
|
if nested {
|
|
block["content"] = content
|
|
blocks[i], _ = json.Marshal(block)
|
|
swapped = true
|
|
}
|
|
}
|
|
}
|
|
if !swapped {
|
|
return raw, false, nil
|
|
}
|
|
out, _ := json.Marshal(blocks)
|
|
return out, true, nil
|
|
}
|
|
|
|
// imageSrc is whether a block is an image, and the image as a URL when it
|
|
// can be read from the block.
|
|
func imageSrc(proto provider.Protocol, kind string, block map[string]json.RawMessage) (string, bool) {
|
|
switch proto {
|
|
case provider.Chat:
|
|
if kind != "image_url" {
|
|
return "", false
|
|
}
|
|
var url string
|
|
if decodeJSON(block["image_url"], &url) != nil {
|
|
var obj struct {
|
|
URL string `json:"url"`
|
|
}
|
|
decodeJSON(block["image_url"], &obj)
|
|
url = obj.URL
|
|
}
|
|
return url, true
|
|
case provider.Responses:
|
|
if kind != "input_image" {
|
|
return "", false
|
|
}
|
|
var url string
|
|
decodeJSON(block["image_url"], &url)
|
|
return url, true
|
|
case provider.Anthropic:
|
|
if kind != "image" {
|
|
return "", false
|
|
}
|
|
var src struct {
|
|
Type string `json:"type"`
|
|
MediaType string `json:"media_type"`
|
|
Data string `json:"data"`
|
|
URL string `json:"url"`
|
|
}
|
|
decodeJSON(block["source"], &src)
|
|
switch {
|
|
case src.Type == "base64" && src.Data != "":
|
|
return "data:" + src.MediaType + ";base64," + src.Data, true
|
|
case src.Type == "url":
|
|
return src.URL, true
|
|
}
|
|
return "", true
|
|
case provider.Gemini:
|
|
var data struct {
|
|
MimeType string `json:"mimeType"`
|
|
Data string `json:"data"`
|
|
}
|
|
decodeJSON(block["inlineData"], &data)
|
|
if strings.HasPrefix(data.MimeType, "image/") {
|
|
return "data:" + data.MimeType + ";base64," + data.Data, true
|
|
}
|
|
if len(block["fileData"]) > 0 {
|
|
var file struct {
|
|
MimeType string `json:"mimeType"`
|
|
}
|
|
if decodeJSON(block["fileData"], &file) != nil || file.MimeType == "" || strings.HasPrefix(file.MimeType, "image/") {
|
|
return "", true // unknown file types may be images; fail closed
|
|
}
|
|
}
|
|
}
|
|
return "", false
|
|
}
|
|
|
|
// textBlock is a text block of the protocol's.
|
|
func textBlock(proto provider.Protocol, text string) json.RawMessage {
|
|
var b []byte
|
|
switch proto {
|
|
case provider.Responses:
|
|
b, _ = json.Marshal(map[string]string{"type": "input_text", "text": text})
|
|
case provider.Gemini:
|
|
b, _ = json.Marshal(map[string]string{"text": text})
|
|
default:
|
|
b, _ = json.Marshal(map[string]string{"type": "text", "text": text})
|
|
}
|
|
return b
|
|
}
|