Files
magpie/internal/gateway/video_input.go
T
yetone 63c91cae5f gateway: the body's key checks look for the needle's rarest byte
Each request ran ~45 bytes.Contains over its whole body to ask whether
a key is there at all ("developer", "compaction, "display", the
passthrough key...). Most needles begin with a quote, JSON's commonest
byte, so bytes.Index stopped at every quote, gave up on that and
compared at every byte: on a 2 MB body ~9 ms a request, 36% of the
gateway's CPU in BenchmarkLargeRequest's profile.

bodyHas/bodyIndex find the needle's rarest byte (a static rank of the
bytes JSON, English and code use most) with IndexByte and compare the
needle only there; a needle of common bytes only hands the rest to
strings.Index after 64 misses, so it is never slower than before, and
it allocates nothing (a []byte(sep) of a needle over 32 bytes did).
148 literal and constant needles in internal/gateway use it, and
mayHoldHEIC's "eXB" walk uses bodyIndex.

mayHoldOddBase64 walked every byte through a switch (~2 ms a 2 MB
body); a run it looks for ends a string, so it now looks back from each
quote with a class table and gives the same answer.

Run on mica (Apple M6) at this tree:
- BenchmarkBodyHas, 7 needles over a 2 MB body: 4.43 ms -> 0.38 ms,
  0 allocs (was 1).
- BenchmarkLargeRequest -benchtime 200x, before -> after:
  anthropic 31.2 -> 28.9 ms, anthropic-group 31.4 -> 27.8,
  chat 35.7 -> 32.8, responses 39.8 -> 29.8.
- go vet ./internal/gateway; gofmt -l clean.
- go test ./internal/gateway -count 1 -timeout 45m: ok (858s).
- Broken by hand: bodyIndex's start off by one -> TestBodyIndexIsIndex
  red (wrong index on 5 needles and the random bodies); the URL-safe
  class never counted -> TestOddBase64LooksAsItWalked red. Both green
  again restored.

TestBodyIndexIsIndex holds bodyIndex to bytes.Index on a large
conversation and 3000 random bodies of common and rare bytes;
TestOddBase64LooksAsItWalked holds the new check to the old byte walk
(kept in the test) on 20000 random bodies and the three protocols'
large conversations.

Real account: the built app (mote, sandbox HOME) sent a 6KB request to
DeepSeek on /v1/responses and /v1/messages, both 200 "pong". Not
tried: Codex (the sandbox's sign-in is stale, and renewing it there
would rotate the live refresh token).
2026-11-14 05:51:42 +08:00

169 lines
5.8 KiB
Go

package gateway
import (
"cmp"
"fmt"
"mime"
"path"
"slices"
"strings"
"github.com/yetone/magpie/internal/provider"
)
// A video a client gives the model (#6135, #4101) is a File part whose
// media type is video/*: Messages' video block (MiniMax's Anthropic
// endpoint takes one), Chat's video_url, a Chat file or Responses
// input_file of a video, and Gemini's inlineData or fileData of one. It
// goes to the upstream in that API's own shape: a video block on
// Messages, video_url on Chat, inlineData on Gemini's API. Responses has
// none, so a video in the latest turn translated there is turned away
// (unportableVideo), as is one for a model whose source says it takes no
// video (videoInput) or a group with no member that takes one. A video of
// an earlier turn to such a model is told of in words (attachmentText), as
// an image of an earlier turn is to a text-only model.
//
// Before, Messages' video block and Chat's video_url had no case in the
// parsers and were left out: the vendor answered 200 for the words alone
// and the model said it saw no video, or made one up.
// isVideo is whether p is a video the client gave.
func isVideo(p Part) bool {
return p.Kind == File && strings.HasPrefix(strings.ToLower(p.MediaType), "video/")
}
// videoPart is a video by its data: URL, bare base64 or address, with the
// media type the client named, else the one its data: URL or its name's
// extension says, else video/mp4; false for one with nothing in it.
func videoPart(src, mediaType, name string) (Part, bool) {
if src == "" {
return Part{}, false
}
if strings.HasPrefix(src, "data:") {
p := imagePart(src)
if p.Data == "" {
return Part{}, false
}
return Part{Kind: File, MediaType: cmp.Or(mediaType, p.MediaType, videoByName(name), "video/mp4"), Data: p.Data}, true
}
if strings.Contains(src, "://") {
at, _, _ := strings.Cut(src, "?")
return Part{Kind: File, MediaType: cmp.Or(mediaType, videoByName(cmp.Or(name, at)), "video/mp4"), URL: src}, true
}
return Part{Kind: File, MediaType: cmp.Or(mediaType, videoByName(name), "video/mp4"), Data: src}, true
}
// videoByName is the video media type a file's name says, "" for none.
func videoByName(name string) string {
mt, _, _ := strings.Cut(mime.TypeByExtension(strings.ToLower(path.Ext(name))), ";")
if strings.HasPrefix(mt, "video/") {
return mt
}
return ""
}
// videoInput is whether the model p serves as model takes video, from the
// kinds its source listed (Entry.Inputs: a plugin's capabilities, a model
// list's input_modalities, models.dev); nil when it listed none.
func videoInput(id, model string) *bool {
for _, e := range provider.Served() {
if e.Group == "" && e.Provider.ID == id && e.Model == model {
if e.Inputs == nil {
return nil
}
takes := slices.Contains(e.Inputs, "video")
return &takes
}
}
return nil
}
// noVideo is whether the model is known to take no video.
func noVideo(id, model string) bool {
v := videoInput(id, model)
return v != nil && !*v
}
// currentVideo is whether the request's latest user message gives the
// model a video, the one it is asked about now.
func currentVideo(r *Request) bool {
for i := len(r.Messages) - 1; i >= 0; i-- {
if m := r.Messages[i]; m.Role == "user" {
return slices.ContainsFunc(m.Parts, isVideo)
}
}
return false
}
// hasCurrentVideo is currentVideo of a body as the client sent it. Most
// bodies name no video at all and are not parsed.
func hasCurrentVideo(from provider.Protocol, body []byte) bool {
if !bodyHas(body, "video") {
return false
}
req, err := parse(from, body)
return err == nil && currentVideo(req)
}
// videoRefused is what the agent is told of a video for a model, or a
// group, that takes none.
func videoRefused(model, group string) string {
if group != "" {
return fmt.Sprintf("model %q does not support video input: no model of the routing group %s takes video. Add a member that takes video, or send frames as images", model, group)
}
return fmt.Sprintf("model %q does not support video input: its model list doesn't say it takes video. Send frames as images, or pick a model that takes video", model)
}
// unportableVideo is the 400 for a video in the latest turn translated to
// an API that has no way to carry one; "" when it can be carried.
func unportableVideo(to provider.Protocol, r *Request) string {
if to != provider.Responses || !currentVideo(r) {
return ""
}
return "this request gives the model a video, which the Responses API has no way to carry; send it on Chat, Messages or Gemini's API, or send frames as images"
}
// chatVideo is a video as Chat's video_url part, the shape the Chat APIs
// that take video read (DashScope's Qwen, Kimi, Zhipu, OpenRouter, vLLM).
func chatVideo(p Part) map[string]any {
return map[string]any{"type": "video_url", "video_url": map[string]any{"url": dataURL(p)}}
}
// videoBlock is a video as a Messages video block, the image block's shape
// under another type: MiniMax's Anthropic endpoint reads it.
func videoBlock(p Part) aBlock {
b := imageBlock(p)
b.Type = "video"
return b
}
// videoFileType is the video media type of a Responses input_file, by its
// data: URL's type or its name; "" for a file that is not a video.
func videoFileType(src, name string) string {
if rest, ok := strings.CutPrefix(src, "data:"); ok {
mt, _, _ := strings.Cut(rest, ";")
mt, _, _ = strings.Cut(mt, ",")
if strings.HasPrefix(strings.ToLower(mt), "video/") {
return strings.ToLower(mt)
}
if mt != "" {
return ""
}
}
at, _, _ := strings.Cut(src, "?")
if strings.Contains(src, "://") {
return cmp.Or(videoByName(name), videoByName(at))
}
return videoByName(name)
}
// anyVideo is whether any message of the request gives a video.
func anyVideo(r *Request) bool {
for _, m := range r.Messages {
if slices.ContainsFunc(m.Parts, isVideo) {
return true
}
}
return false
}