Uh oh!
There was an error while loading. Please reload this page.
feat: auto-compress oversized images before API submission - #21371
Conversation
Images pasted from clipboard or read via the file read tool can exceed provider API limits (Anthropic's 5MB base64 / dimension caps). This adds automatic image compression using sharp at the provider transform layer, covering all image paths — clipboard, file read, and any future sources. Changes: - New Image utility (packages/opencode/src/util/image.ts): - Three-phase compression: quality reduction → dimension reduction → fallback - Smart format selection: preserves PNG/WebP for transparency, JPEG for opaque - Correct 3.75MB raw-byte threshold (base64 inflates ~4/3x → 5MB limit) - Provider transform integration (transform.ts): - compressImages() runs before message normalization - Handles both image and file parts with graceful error fallback - Surfacing corrupt/unsupported images as error text rather than silently failing - Read tool validation (read.ts): - Images validated and optimized via sharp before base64 encoding - Clear error message for corrupt or unsupported image formats - 23 unit tests covering compression, resizing, format detection, and edge cases - All 121 existing transform tests updated and passing
This PR doesn't fully meet our contributing guidelines and PR template. What needs to be fixed:
Please edit this PR description to address the above within 2 hours, or it will be automatically closed. If you believe this was flagged incorrectly, please let a maintainer know. |
The following comment was made by an LLM, it may be inaccurate: Related PRs FoundRelated PR:
Note: The original PR #6455 (which this PR supersedes) was auto-closed after 60 days and is not returned in search results, but it's a predecessor rather than a duplicate. |
This pull request has been automatically closed because it was not updated to meet our contributing guidelines within the 2-hour window. Feel free to open a new pull request that follows our guidelines. |
Summary
Images pasted from clipboard or read via the file read tool can exceed provider API limits (Anthropic's 5MB base64 limit, dimension caps). This adds automatic image compression using
sharpat the provider transform layer, covering all image paths uniformly.This supersedes #6455 (auto-closed after 60 days) with significant improvements, and addresses the
read.tscoverage gap noted in #12069.Changes
New
packages/opencode/src/util/image.ts— Image compression utility:Provider transform integration (
transform.ts):compressImages()step runs before message normalizationimageandfileparts with graceful error fallbackmessage()is now async to support compressionRead tool validation (
read.ts):23 unit tests covering compression, resizing, format detection, transparency handling, and error cases
Key differences from #6455
Testing