opencode icon indicating copy to clipboard operation
opencode copied to clipboard

feat: auto-compress clipboard images to avoid API size limits

Open noah79 opened this issue 2 weeks ago • 1 comments

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.

noah79 avatar Dec 30 '25 17:12 noah79

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

R44VC0RP avatar Jan 12 '26 20:01 R44VC0RP