Commit Graph
2 Commits
Author SHA1 Message Date
ggbdpq e8b6c3d357 gateway: a webp a vendor can't read is written again as a JPEG, and GLM's word for one fails over (#4732)
Claude Desktop's browser tool answers a screenshot as image/webp, and
GLM can't read one: its 400 [1210] "图片输入格式/解析错误" turns the
picture away, and its Anthropic-style endpoint says of the same picture
400 [1214] "messages[0].content[0].file必须传入file_id、file_url、
file_data至少之一", having taken it for a file of its own — the two
alternating (zhangqingwen204's report). The picture rides in the
conversation's history, so every later turn was refused the same way,
and the session was stuck; another model or provider of the same
conversation fared no better.

Now the request is asked again with the picture written again as a JPEG
(as PNG when it has transparency), the way a HEIC is (heic.go), and the
provider is remembered (unfit, webpRefused) to have every webp of its
requests written again before they leave — the session's later turns
pay the refusal no more. The words themselves join the shape refusals
(shapeRefused, retryable): a request no webp of whose can be written
again (one gone as an URL, say) moves to the next member, which may
take the picture as it is, and this one doesn't rest. Z.ai's bare
"1210", its word for a model that always thinks, is none of this: the
family is the double-bracket "[1210][" and "[1214][" forms and the two
wordings, read decoded (#1669).

Most vendors take webp, so nothing is written again for them until
their first refusal says otherwise.

Run: go test -tags nogui ./internal/gateway/

Generated-by: GLM-5.3-Flash (ZCode)

Landed from ggbdpq's branch (patch posted on the issue; sha256/patch-id
checked against the issue) by yetone, rebased on origin/main 2026-11-03.
Run at the pushed tree, on mote (macOS): go test -tags nogui -count=1 of
internal/gateway with -run over this batch's tests and their families
(PlainSchema, Antigravity, AutoMode, WebP, ClaudeTUI, NativeRelay,
Namespace, Grok, Zed, Codex, ClinePass, rewrite) ok; -race -count=3 of
the new tests ok; GOOS=linux and GOOS=windows go vet of internal/gateway
and gofmt -l clean. The full gateway-and-importers run, started at an
earlier base, was still running at the push; its reds get issues.
Changed in review: the ask-again records the try (failShape) in the
route trace, as the serve loop's other ask-agains do.
Broken by hand: the serve-loop branch and the pre-send rewrite turned
off, TestWebPRefusalHealed red; restored, green.
Not tried against GLM. The issue doesn't show the screenshot's media
type, so 'Claude Desktop's screenshot is webp' is the reporter's
reading, not checked here.
2026-11-03 02:27:57 +08:00
yetone d614f21721 gateway: a HEIC photo goes to the vendor as JPEG, or PNG with alpha (qikamatata on X)
Pi and Pi Desktop send an iPhone photo dragged in as image/heic, and
DeepSeek flash on WorkBuddy (a plugin's fetch) turned it away; most
vendors take JPEG, PNG, GIF and WebP only.

What: heicAsJPEG (heic.go) runs at the top of Server.serve, after
offAsNone, so every route below sees the converted image: a provider's
own, a plugin's fetch, the describer, every translation. count_tokens
does the same. It walks images through walkImages, the one image walker,
so it covers Chat image_url (string or object), Responses input_image,
Anthropic image blocks and Gemini inlineData, tool results included. It
goes by the bytes (ftyp + an HEVC still's brand), so a HEIC labelled
image/jpeg or image/png is converted too, and a JPEG labelled heic is
left alone. Other fields of the block (detail, cache_control) are kept.
Transparent images go as PNG, opaque ones as JPEG q90.

Cost: a body with no "image/hei" and no base64 ftyp mark ("eXB" + 5
chars, at the same place in any base64 that starts at the file) is not
walked. Caps: 48 MB of base64, 50 MP, the long side written at 4096 px,
one decode at a time, 30 s wait (a late result is cached for the next
turn), a 64 MB / 32 entry cache by sha256, since agents resend a
conversation's images every turn. Any failure, panic included, leaves
the image as it came. Measured on mote (M5 Pro): 12 MP 0.44 s, 24 MP
0.86 s, 48 MP 1.6 s / ~1 GB held, freed at once with FreeOSMemory.

Why gen2brain/h265 v0.2.4: pure Go (release builds are CGO_ENABLED=0),
MIT, no dependencies, SIMD on amd64 and arm64, maintained (latest
2026-10-29). Not chosen: gen2brain/heic (wazero WASM or a libheif dylib
through purego), jdeng/goheif (cgo libde265), OS converters (Windows
needs the paid HEVC extension, Linux tools are often missing).

Tests (heic_test.go, fixtures made on mote with sips, 1.6 KB each):
12 shapes converted with their kept fields, pixels kept, 7 non-HEIC
cases byte-identical, a cut file / a panicking decoder / a timeout go
as they came, and a plugin's fetch (fake plugin, new FAKE_SEES image
model) gets JPEG on /v1/chat/completions and /v1/messages.

Run at this tree (733fac48f + this change), on mote:
- gofmt -l internal: clean
- GOOS=darwin/linux/windows go vet -tags nogui ./internal/gateway
  ./internal/plugin: ok
- CGO_ENABLED=0 go build ./internal/gateway for darwin, linux, windows
  x amd64, arm64: ok
- go test ./internal/gateway ./internal/plugin -count=1 -timeout 45m:
  ok (774s, 134s)
- win-run (Windows amd64) and wsl-run (Linux amd64, AVX2 path):
  go test ./internal/gateway -run HEIC: ok

Broken by hand, each red, then restored:
- no heicAsJPEG call in serve: TestHEICReachesPluginAsJPEG red
- media_type not set for Anthropic: the messages subtests red
- no base64 mark scan: the "labelled jpeg/png" subtests red

Not tried: a real Pi -> WorkBuddy request with a real account; a real
iPhone gridded HEIC (only sips files and Apple's 26 MB wallpaper); a HEIC
behind an http URL (not fetched, goes as it came); Anthropic document
blocks, Responses input_file and images/edits.
2026-11-01 15:04:28 +08:00