Skip to content

Conversation

@noah79
Copy link

@noah79 noah79 commented Dec 30, 2025

Summary

Screenshots pasted from clipboard can exceed Claude's 5MB API limit, causing upload failures. This PR adds automatic image compression using sharp.

Changes

  • New utility: packages/opencode/src/util/image.ts

    • optimizeForUpload() - main function that compresses images to <4MB
    • Smart format selection: preserves PNG/WebP for transparency, converts opaque images to JPEG
    • Progressive quality reduction with optional resizing as fallback
  • Integration: Modified clipboard handling in clipboard.ts and prompt/index.tsx to automatically compress pasted images

  • Tests: 23 unit tests covering compression, resizing, format detection, and edge cases

Testing

cd packages/opencode
bun test test/util/image.test.ts

All 23 tests pass.

Screenshots pasted from clipboard can exceed Claude's 5MB limit.
This adds automatic image compression using sharp:

- New Image utility with compress/resize/optimize functions
- Clipboard reads now auto-compress images >4MB
- File pastes also get optimized before upload
- Transparent images stay PNG/WebP, opaque convert to JPEG
- Quality reduced iteratively until under 4MB threshold
- Includes comprehensive test suite (23 tests)
@R44VC0RP
Copy link
Contributor

R44VC0RP commented Jan 12, 2026

Hey @noah79! I tested this PR and it's looking solid. Here's what I found:

Test Results

  • All 23 unit tests pass
  • Compression works great: tested a 34MB image → compressed to 1.27MB (96% reduction) in ~500ms
  • Correctly preserves transparency (uses WebP instead of JPEG for alpha images)
  • Progressive quality reduction works as expected
  • Output stays safely under the 5MB API limit (targets 4MB)

Merge Status

The PR has a minor conflict with dev that's easy to resolve:

File Status
bun.lock Conflict (just needs bun install to regenerate)
prompt/index.tsx Auto-merges cleanly
clipboard.ts Auto-merges cleanly (picks up recent PowerShell fixes)

To get this merged

Could you rebase onto the latest dev branch?

git fetch origin dev
git rebase origin/dev
bun install  # regenerates bun.lock
bun test test/util/image.test.ts  # verify tests still pass
git push --force-with-lease

really like this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants