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.
132 lines
5.5 KiB
Go
132 lines
5.5 KiB
Go
package gateway
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/base64"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/tidwall/gjson"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// webpShot is a 2x1 lossless webp, as Claude Desktop's browser tool
|
|
// answers a screenshot (#4732).
|
|
var webpShot = "UklGRh4AAABXRUJQVlA4TBEAAAAvAQAAAA+w//Mf8x8VMqL/AQA="
|
|
|
|
// zhipuFileRefusal is GLM's 400 [1214], its word for a content block it
|
|
// took for a file of its own and found no file_id, file_url or file_data
|
|
// in — what its Anthropic-style endpoint says of a webp picture (#4732).
|
|
const zhipuFileRefusal = `{"type":"error","error":{"type":"invalid_request_error","message":"[1214][messages[0].content[0].file必须传入file_id、file_url、file_data至少之一]"}}`
|
|
|
|
// zhipuFormatRefusal is GLM's 400 [1210] for an image it doesn't parse,
|
|
// the other of the reporter's two, the pair alternating (#4732).
|
|
const zhipuFormatRefusal = `{"error":{"code":"1210","message":"[1210][图片输入格式/解析错误]"}}`
|
|
|
|
// #4732: Claude Desktop's browser screenshot is a webp, GLM can't read
|
|
// one, and its 400 left the session stuck: the picture rides in the
|
|
// conversation's history, so every later turn was refused the same way.
|
|
// Asked again with the picture written as a JPEG, which it takes, and
|
|
// from then on written before it leaves, the session goes on.
|
|
func TestWebPRefusalHealed(t *testing.T) {
|
|
var mts []string
|
|
f := &fake{t: t, reply: anthropicHi}
|
|
f.refuse = func(b []byte) (int, string) {
|
|
mt := gjson.GetBytes(b, "messages.0.content.0.source.media_type").String()
|
|
mts = append(mts, mt)
|
|
if mt == "image/webp" {
|
|
return 400, zhipuFileRefusal
|
|
}
|
|
return 0, ""
|
|
}
|
|
setup(t, provider.Anthropic, f)
|
|
srv := New()
|
|
ask := `{"model":"m1","max_tokens":32,"messages":[{"role":"user","content":[` +
|
|
`{"type":"image","source":{"type":"base64","media_type":"image/webp","data":"` + webpShot + `"}},` +
|
|
`{"type":"text","text":"这是什么"}]}]}`
|
|
for turn, want := range [][]string{{"image/webp", "image/jpeg"}, {"image/jpeg"}} {
|
|
mts = nil
|
|
code, body := postTo(t, srv, "/v1/messages", ask)
|
|
if code != 200 || len(mts) != len(want) {
|
|
t.Fatalf("turn %d: %d %s, media types sent %q, want %q", turn+1, code, body, mts, want)
|
|
}
|
|
for i, mt := range want {
|
|
if mts[i] != mt {
|
|
t.Fatalf("turn %d: media type sent %d is %q, want %q (all %q)", turn+1, i, mts[i], mt, mts)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// The same refusal with no webp of the request's to write again: the next
|
|
// provider is asked, as for a shape it can't read, and this one doesn't
|
|
// rest — nothing is wrong with it.
|
|
func TestZhipuImageRefusalFailsOver(t *testing.T) {
|
|
plan := &fake{t: t, ctype: "application/json", code: 400, reply: zhipuFileRefusal}
|
|
spare := &fake{t: t, ctype: "application/json", reply: `{"id":"from-spare","choices":[]}`}
|
|
twoProviders(t, plan, spare)
|
|
code, body := post(t, "/v1/chat/completions", chatReq)
|
|
if code != 200 || !strings.Contains(body, "from-spare") || strings.Contains(body, "file_data") {
|
|
t.Fatalf("%d %s", code, body)
|
|
}
|
|
if plan.calls != 1 {
|
|
t.Fatalf("plan asked %d times, want 1", plan.calls)
|
|
}
|
|
}
|
|
|
|
func TestZhipuImageRefusalWords(t *testing.T) {
|
|
// the message \u-escaped, as a relay may pass it on (#1669)
|
|
escaped := `{"error":{"code":"1214","message":"[1214][messages[0].content[0].file\u5fc5\u987b\u4f20\u5165file_id\u3001file_url\u3001file_data\u81f3\u5c11\u4e4b\u4e00]"}}`
|
|
for b, want := range map[string]bool{
|
|
zhipuFileRefusal: true,
|
|
zhipuFormatRefusal: true,
|
|
escaped: true,
|
|
`{"error":{"message":"messages: field required"}}`: false,
|
|
`{"error":{"message":"messages[1210].content: must have a role"}}`: false,
|
|
`{"error":{"message":"Invalid API key"}}`: false,
|
|
// Z.ai's other 1210, its word for a model that always thinks
|
|
// (TestAnthropicPassthroughModelThatAlwaysThinks): the bare code,
|
|
// without the double brackets or the picture's words, is none of
|
|
// this family
|
|
`{"type":"error","error":{"type":"1210","message":"GLM-5.3 always engages in thinking; use low, high, or max"}}`: false,
|
|
} {
|
|
if got := zhipuImageRefusal.Match(wordsOf([]byte(b))); got != want {
|
|
t.Errorf("zhipuImageRefusal(%s) = %v, want %v", b, got, want)
|
|
}
|
|
if got := shapeRefused(400, []byte(b)); got != want {
|
|
t.Errorf("shapeRefused(400, %s) = %v, want %v", b, got, want)
|
|
}
|
|
if got := retryable(400, []byte(b)); got != want {
|
|
t.Errorf("retryable(400, %s) = %v, want %v", b, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestWebPAsJPEG(t *testing.T) {
|
|
ask := []byte(`{"model":"m1","max_tokens":32,"messages":[{"role":"user","content":[` +
|
|
`{"type":"image","source":{"type":"base64","media_type":"image/webp","data":"` + webpShot + `"}},` +
|
|
`{"type":"text","text":"这是什么"}]}]}`)
|
|
got := webpAsJPEG(provider.Anthropic, ask)
|
|
mt := gjson.GetBytes(got, "messages.0.content.0.source.media_type").String()
|
|
data, err := base64.StdEncoding.DecodeString(gjson.GetBytes(got, "messages.0.content.0.source.data").String())
|
|
if err != nil {
|
|
t.Fatalf("data: %v", err)
|
|
}
|
|
if mt != "image/jpeg" && mt != "image/png" {
|
|
t.Fatalf("media type %q, want a written-again picture", mt)
|
|
}
|
|
if len(data) > 4 && !bytes.HasPrefix(data, []byte{0xff, 0xd8}) && !bytes.HasPrefix(data, []byte("\x89PNG")) {
|
|
t.Fatalf("data is neither a JPEG nor a PNG: %x", data[:8])
|
|
}
|
|
if bytes.Contains(got, []byte(webpShot)) {
|
|
t.Fatalf("the webp is still there: %s", got)
|
|
}
|
|
if same := webpAsJPEG(provider.Anthropic, []byte(chatReq)); !bytes.Equal(same, []byte(chatReq)) {
|
|
t.Fatalf("a request with no webp was changed: %s", same)
|
|
}
|
|
if mayHoldWebP([]byte(chatReq)) {
|
|
t.Fatal("mayHoldWebP saw a webp in a plain text request")
|
|
}
|
|
}
|