feat: auto-compress oversized images before API submission#21371
Closed
noah79 wants to merge 1 commit intoanomalyco:devfrom
Closed
feat: auto-compress oversized images before API submission#21371noah79 wants to merge 1 commit intoanomalyco:devfrom
noah79 wants to merge 1 commit intoanomalyco:devfrom
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
Contributor
|
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. |
Contributor
|
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. |
Contributor
|
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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