Skip to content

Conversation

@roomote
Copy link

@roomote roomote bot commented Oct 22, 2025

Description

This PR fixes the issue where images larger than 5MB fail to upload when using the Anthropic API. The solution implements automatic image compression using the sharp library.

Problem

  • Users were unable to upload images larger than 5MB
  • Anthropic API has a 5MB limit for image uploads
  • This resulted in API request errors

Solution

  • Added sharp library for efficient image compression
  • Created a compression utility that:
    • Automatically detects images over 5MB
    • Applies progressive quality reduction (up to 5 attempts)
    • Converts PNG to JPEG for better compression when needed
    • Falls back to resizing if compression alone isn't sufficient
    • Targets 90% of the 5MB limit to leave a safety margin
  • Integrated compression into the image selection workflow with:
    • Progress notification during compression
    • Success message showing size reduction
    • Graceful error handling

Testing

  • Added comprehensive test suite with 12 test cases covering:
    • Small images (no compression needed)
    • Large images requiring compression
    • Different image formats (JPEG, PNG, WebP)
    • Progressive quality reduction
    • Resize fallback
    • Error handling
  • All tests pass successfully

User Experience

  • Users see a progress notification while images are being compressed
  • Compression results are displayed (e.g., "Image compressed from 6.5 MB to 4.2 MB")
  • Process is transparent and non-blocking

Future Enhancements (not in this PR)

  • Consider making MAX_FILE_SIZE configurable for different API providers
  • Add telemetry to track compression effectiveness
  • Consider caching compressed images

Fixes #8771


Important

Adds image compression for images over 5MB using sharp, integrated into the image selection workflow with tests and dependency updates.

  • Behavior:
    • Implements image compression for images over 5MB using sharp in image-compression.ts.
    • Converts PNG to JPEG for better compression and resizes if compression fails.
    • Integrated into selectImages() in process-images.ts with progress notifications.
  • Testing:
    • Adds image-compression.spec.ts with 12 test cases for different image sizes and formats.
  • Dependencies:
    • Adds sharp to package.json dependencies.

This description was created by Ellipsis for 429ccd0. You can customize this summary. It will automatically update as commits are pushed.

- Add sharp library for image compression
- Create image compression utility with progressive quality reduction
- Automatically compress images larger than 5MB in selectImages()
- Show progress notification during compression
- Add comprehensive tests for compression logic
- Convert PNG to JPEG for better compression when needed
- Resize images as last resort if compression alone fails

Fixes #8771
@roomote roomote bot requested review from cte, jr and mrubens as code owners October 22, 2025 11:48
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. bug Something isn't working labels Oct 22, 2025
@roomote
Copy link
Author

roomote bot commented Oct 22, 2025

Review Summary

I've reviewed the PR and found 1 issue that should be addressed:

  • Misleading success message when compression fails - The code shows "Image compressed from X MB to X MB" when compression encounters an error and returns the original buffer. Should verify size actually decreased before showing success message.

Follow Along on Roo Code Cloud

Comment on lines +43 to +45
// Show info about compression
const newSize = formatFileSize(buffer.length)
vscode.window.showInformationMessage(`Image compressed from ${originalSize} to ${newSize}`)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When compression fails and the original buffer is returned (due to the try-catch in compressImageIfNeeded), this message will incorrectly display something like "Image compressed from 6.0 MB to 6.0 MB". The code should verify that the size actually decreased before showing a success message, or show a different message when compression fails.

@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. size:L This PR changes 100-499 lines, ignoring generated files.

Projects

Status: Triage

Development

Successfully merging this pull request may close these issues.

[BUG] Anthropic error when trying to upload images with size > 5MB

2 participants