Commit Graph
1 Commits
Author SHA1 Message Date
yetone 6679e963fd gateway: a screenshot sent to a ChatGPT account goes at the size Codex's own client sends it (ylorn on Discord)
ylorn's ChatGPT account dropped turns a PNG screenshot took past about
1 MiB (the commit before this tells that as too large). ylorn uses Pi's
openai-codex through magpie, not Codex, and Codex's own client never
sends a screenshot that size: it scales every inline image down first.
Read in openai/codex at cc7ba336 (codex-rs/core/src/image_preparation.rs
resize_image, codex-rs/utils/image/src/lib.rs):
- detail none, auto or high: at most 2048 px a side and 2500 patches of
  32 px (HIGH_DETAIL); "original": 6000 px and 10000 patches; "low" it
  refuses itself (DetailBased mode, the default; UnifiedBudget is a
  feature flag)
- prompt_image_output_dimensions_for_limits: as it is if it fits,
  scaled to the longest side, then by area with the patch grid rounded
  down
- resized with FilterType::Triangle; PNG written as PNG, JPEG as JPEG at
  quality 85, WebP lossless, GIF as PNG; an image that fits keeps its
  bytes; no byte cap other than a 1 GiB guard
- Codex zstd-compresses request bodies to ChatGPT by default
  (enable_request_compression); magpie already reads those and sends
  upstream plain, which this doesn't change.

codexImagesFit (codex_images.go) does the same in forwardOnce, for a
ChatGPT (codex) account's Responses requests only: every data-URL
input_image in a message's content or a tool output's output larger
than Codex's limits for its detail is scaled to Codex's size with a
separable triangle filter widened by the scale (as the image crate's)
and written in its own format (JPEG q85, PNG, GIF as PNG). Only the
image_url string's bytes are replaced, so the rest of the request is
the same bytes for the prompt cache, and the last 64 results are kept
so a conversation's screenshot isn't resized on every turn. An image
that fits, "low" detail, a remote URL, an image past 80 Mpx, and WebP
(which Go's standard library doesn't decode) go as they came. Other
vendors, API keys and Chat requests are untouched. Codex's own requests
come already sized, so this changes nothing for them.

Tests (codex_images_test.go):
- TestCodexImageSizeAsCodex: Codex's own test cases (2048x2048 →
  1600x1600, 2050x2 → 2048x2, original 6401x100 → 6000x94, 4096x2048
  kept, 64x32 kept) and a 3456x2234 screenshot → 1979x1280 (checked
  against Codex's formula in f64).
- TestCodexScreenshotSizedAsCodex: still PNG, 1979x1280, its black and
  white halves and red band where they were, the rest of the body
  byte-identical.
- TestCodexImagesSizedInEveryPlace: JPEG stays JPEG, a tool output's
  image is sized, "original" gets the larger budget.
- TestCodexImagesThatFitGoAsTheyCame, TestCodexImagesOnlyForChatGPT.
- TestCodexAccountGetsScreenshotAtCodexSize: an agent's /v1/responses
  turn of 2.2 MB reaches the fake ChatGPT backend as 657 KB at
  1979x1280. Red with the call taken out of forwardOnce; the legibility
  checks go red with the filter's centre moved.

Run: go test -tags nogui ./internal/gateway, gofmt -l, go vet -tags
nogui with GOOS=darwin, linux and windows on ./internal/gateway. Not
tried: ylorn's account and network, a real agent's screenshot through
a real ChatGPT account after this change, WebP.
2026-10-11 14:32:57 +08:00