diff --git a/internal/codexcat/codexcat.go b/internal/codexcat/codexcat.go index 52a53349a..b0e5e83d5 100644 --- a/internal/codexcat/codexcat.go +++ b/internal/codexcat/codexcat.go @@ -456,7 +456,10 @@ func entriesAt(ms []catalog.Model, after int, effort string) []any { if e.MultiAgent == "" { e.MultiAgent = "v2" } - e.DetailOriginal = m.Images + // The original detail only on DeepSeek's own API: a relay's + // may refuse it, as OpenCode Go does with 400 "invalid image + // detail" (qiu_eth) + e.DetailOriginal = m.Images && strings.HasPrefix(m.ID, "deepseek/") } for _, ef := range m.Efforts { e.Efforts = append(e.Efforts, level{Effort: ef}) diff --git a/internal/codexcat/deepseek_test.go b/internal/codexcat/deepseek_test.go index c2d0aac6c..edec234eb 100644 --- a/internal/codexcat/deepseek_test.go +++ b/internal/codexcat/deepseek_test.go @@ -27,11 +27,12 @@ func TestCodexCatalogDeepSeekAsDeepSeekSays(t *testing.T) { {ID: "deepseek/deepseek-v4-pro", Name: "DeepSeek V4 Pro", Context: 1048576, Efforts: []string{"low", "high", "max"}}, {ID: "deepseek/deepseek-flash", Name: "DeepSeek V4.1 Flash", Context: 1048576, Images: true, Efforts: []string{"low", "high", "max"}}, {ID: "relay/deepseek-ai/DeepSeek-V4-Pro-0813", Name: "DeepSeek V4 Pro 0813", Context: 1000000}, + {ID: "opencode-go/deepseek-v4.1-flash", Name: "DeepSeek V4.1 Flash", Context: 1048576, Images: true}, {ID: "moonshot/kimi-k3", Name: "Kimi K3", Context: 262144, Images: true, Efforts: []string{"low", "high"}}, {ID: "zai/glm-5.2", Name: "GLM-5.2", Context: 200000}, {ID: "group/deepseek", Name: "deepseek"}, {ID: "x/deepseek-r1-distill-qwen-32b", Name: "R1 Distill Qwen"}, - }), &got); err != nil || len(got.Models) != 7 { + }), &got); err != nil || len(got.Models) != 8 { t.Fatalf("%v %v", err, got) } by := map[string]map[string]any{} @@ -55,6 +56,15 @@ func TestCodexCatalogDeepSeekAsDeepSeekSays(t *testing.T) { if by["deepseek/deepseek-flash"]["supports_image_detail_original"] != true { t.Error("deepseek-flash takes images: supports_image_detail_original true, as DeepSeek's setup says") } + // A relay's DeepSeek takes images at no detail Codex names: OpenCode + // Go answers 400 "invalid image detail" to "original" (qiu_eth) + if e := by["opencode-go/deepseek-v4.1-flash"]; e["supports_image_detail_original"] != nil || e["shell_type"] != "shell_command" { + t.Errorf("opencode-go/deepseek-v4.1-flash: supports_image_detail_original %v, shell %v; want none, DeepSeek's shell_command", + e["supports_image_detail_original"], e["shell_type"]) + } + if in, _ := by["opencode-go/deepseek-v4.1-flash"]["input_modalities"].([]any); len(in) != 2 { + t.Errorf("opencode-go/deepseek-v4.1-flash input_modalities %v; it takes images", in) + } if v, ok := by["deepseek/deepseek-v4-pro"]["supports_image_detail_original"]; ok { t.Errorf("deepseek-v4-pro takes no images; supports_image_detail_original %v", v) } diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index e08c5041f..9aa1ddd47 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -3352,6 +3352,10 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro // again (#4732) attemptBody = webpAsJPEG(from, attemptBody) } + // Codex's detail "original" goes only where it is taken (qiu_eth) + if from == provider.Responses && !takesOriginalDetail(c.p) { + attemptBody = originalAsHigh(attemptBody) + } // a value masking swapped for a placeholder: the model is told to // use it as written (treeMan on Discord). A conversation's values // are masked again on every turn, so once one is, every later diff --git a/internal/gateway/image_detail.go b/internal/gateway/image_detail.go new file mode 100644 index 000000000..536032465 --- /dev/null +++ b/internal/gateway/image_detail.go @@ -0,0 +1,55 @@ +package gateway + +import ( + "bytes" + "encoding/json" + + "github.com/yetone/magpie/internal/provider" +) + +// takesOriginalDetail is whether p's Responses API takes an input_image's +// detail "original", Codex's for a screenshot: OpenAI's own API (a ChatGPT +// account's Codex backend, Azure OpenAI), DeepSeek's own, whose Codex +// setup asks for it, and a remote magpie, which decides for its own +// upstream. A relay's Responses API may not: OpenCode Go answers 400 +// "invalid image detail" for DeepSeek V4.1 Flash, which it serves through +// another vendor (qiu_eth). +func takesOriginalDetail(p provider.Provider) bool { + if p.IsAzure() || p.IsRemoteMagpie() { + return true + } + switch provider.HostOf(p.Base(provider.Responses)) { + case "api.openai.com", "chatgpt.com", "api.deepseek.com": + return true + } + return false +} + +// originalAsHigh is a Responses body with each input_image's detail +// "original" sent as "high", the closest an API without it has, as a Chat +// upstream is sent it (chatDetail); the body as it came when it has none. +func originalAsHigh(body []byte) []byte { + if !bytes.Contains(body, []byte(`"original"`)) { + return body + } + out, err := walkImages(provider.Responses, body, func(im imageAt) (json.RawMessage, error) { + var block map[string]json.RawMessage + if decodeJSON(im.Block, &block) != nil { + return nil, nil + } + var d string + if decodeJSON(block["detail"], &d) != nil || d != "original" { + return nil, nil + } + block["detail"] = json.RawMessage(`"high"`) + b, err := json.Marshal(block) + if err != nil { + return nil, nil + } + return b, nil + }) + if err != nil { + return body + } + return out +} diff --git a/internal/gateway/image_detail_chat_test.go b/internal/gateway/image_detail_chat_test.go index bea85395e..678d9cce8 100644 --- a/internal/gateway/image_detail_chat_test.go +++ b/internal/gateway/image_detail_chat_test.go @@ -2,7 +2,10 @@ package gateway import ( "encoding/json" + "slices" "testing" + + "github.com/yetone/magpie/internal/provider" ) // chatImageDetails is the detail of every image_url in a Chat body, in @@ -95,3 +98,47 @@ func TestChatImageDetailReachesResponses(t *testing.T) { } } } + +// Codex's screenshot, detail "original", goes to a Responses API other +// than OpenAI's or DeepSeek's own as high, the closest it has: OpenCode +// Go answered 400 "invalid image detail" for DeepSeek V4.1 Flash +// (qiu_eth). The rest of the body goes as it came, low and auto too. +func TestOriginalDetailHighOnARelay(t *testing.T) { + sent := toolImageUp(t, provider.Responses) + img := `"image_url":"data:image/png;base64,` + pngA + `"` + postToolImages(t, "/v1/responses", `{"model":"probe/eye","stream":true,"input":[ + {"role":"user","content":[{"type":"input_text","text":"look"}, + {"type":"input_image","detail":"original",`+img+`}, + {"type":"input_image","detail":"low",`+img+`}]}, + {"type":"function_call","call_id":"c1","name":"view_image","arguments":"{}"}, + {"type":"function_call_output","call_id":"c1","output":[{"type":"input_image","detail":"original",`+img+`}]}, + {"role":"user","content":[{"type":"input_text","text":"and now?"}]}]}`) + var got []string + for _, im := range inputImages(t, sent()) { + d, _ := im["detail"].(string) + got = append(got, d) + } + if want := []string{"high", "low", "high"}; !slices.Equal(got, want) { + t.Errorf("details %q, want %q\n%s", got, want, sent()) + } +} + +// OpenAI's own API, a ChatGPT account's and DeepSeek's take "original"; +// a relay's, OpenCode Go's among them, isn't known to. +func TestTakesOriginalDetail(t *testing.T) { + for base, want := range map[string]bool{ + "https://api.openai.com/v1": true, + "https://chatgpt.com/backend-api/codex": true, + "https://api.deepseek.com/v1": true, + "https://opencode.ai/zen/go/v1": false, + "https://openrouter.ai/api/v1": false, + "": false, + } { + if got := takesOriginalDetail(provider.Provider{ID: "x", Responses: base}); got != want { + t.Errorf("%q: %v, want %v", base, got, want) + } + } + if !takesOriginalDetail(provider.Provider{ID: "x", Preset: provider.RemoteMagpiePreset, Responses: "http://10.0.0.2:3425/v1"}) { + t.Error("a remote magpie decides for its own upstream") + } +}