[PR #12069] fix: resize images before sending to Anthropic API #14054

Open
opened 2026-02-16 18:18:52 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/12069

State: open
Merged: No


Summary

Fixes #12068

Images pasted from clipboard or read via the file read tool are sent to Anthropic's API without any size checks. Images exceeding 2000px in either dimension cause the API to reject the request.

Changes

  • New packages/opencode/src/util/image.tsprocessImage(buffer, mime) utility that progressively downsizes images using sharp:

    1. Try compression only (quality 80)
    2. Resize to fit within 2000px, preserving aspect ratio
    3. Reduce JPEG quality to 60
    4. Aggressive downscale to 1000px max
    • Gracefully degrades if sharp is not available (returns original image)
    • Target buffer size: ~3.75MB (under Anthropic's 5MB API limit)
  • Modified packages/opencode/src/tool/read.ts — calls processImage() before creating base64 data URL for image files

  • Modified packages/opencode/src/cli/cmd/tui/util/clipboard.ts — calls processImage() on all clipboard image paths (macOS, Windows, Linux wayland, Linux x11)

  • Added sharp@0.34.5 as dependency in packages/opencode/package.json

Testing

  • Typechecks pass across all 17 workspace packages
  • Tested with 2560x1440 screenshots — correctly resized before API submission
**Original Pull Request:** https://github.com/anomalyco/opencode/pull/12069 **State:** open **Merged:** No --- ## Summary Fixes #12068 Images pasted from clipboard or read via the file read tool are sent to Anthropic's API without any size checks. Images exceeding 2000px in either dimension cause the API to reject the request. ## Changes - **New `packages/opencode/src/util/image.ts`** — `processImage(buffer, mime)` utility that progressively downsizes images using `sharp`: 1. Try compression only (quality 80) 2. Resize to fit within 2000px, preserving aspect ratio 3. Reduce JPEG quality to 60 4. Aggressive downscale to 1000px max - Gracefully degrades if `sharp` is not available (returns original image) - Target buffer size: ~3.75MB (under Anthropic's 5MB API limit) - **Modified `packages/opencode/src/tool/read.ts`** — calls `processImage()` before creating base64 data URL for image files - **Modified `packages/opencode/src/cli/cmd/tui/util/clipboard.ts`** — calls `processImage()` on all clipboard image paths (macOS, Windows, Linux wayland, Linux x11) - **Added `sharp@0.34.5`** as dependency in `packages/opencode/package.json` ## Testing - Typechecks pass across all 17 workspace packages - Tested with 2560x1440 screenshots — correctly resized before API submission
yindo added the pull-request label 2026-02-16 18:18:52 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#14054