Skip to content

fix(server): preprocess oversized images before sending to agent SDK#820

Open
caoer wants to merge 4 commits into
getpaseo:mainfrom
caoer:fix/preprocess-oversized-images
Open

fix(server): preprocess oversized images before sending to agent SDK#820
caoer wants to merge 4 commits into
getpaseo:mainfrom
caoer:fix/preprocess-oversized-images

Conversation

@caoer
Copy link
Copy Markdown
Contributor

@caoer caoer commented May 8, 2026

Summary

  • The Claude Agent SDK resizes images exceeding 2000×2000px using sharp internally, but sharp may fail to load in the spawned CLI process, producing a [System Error] Unable to resize image — dimensions exceed the 2000x2000px limit and image processing failed error
  • Adds a preprocessImages() step in the daemon's session layer that resizes oversized images before dispatching to any agent provider — sharp is available in the daemon process from the hoisted SDK dependency
  • Gracefully falls back to sending the original image if sharp is unavailable, so this is purely additive

Changes

  • packages/server/src/server/agent/preprocess-image.ts — New utility: dynamically imports sharp, checks image dimensions, resizes to fit within 2000×2000 (preserving aspect ratio) and re-encodes as JPEG quality 80
  • packages/server/src/server/agent/preprocess-image.test.ts — Tests: small images pass through unchanged, oversized images get resized with correct aspect ratio, corrupt data handled gracefully
  • packages/server/src/server/session.ts — Both handleSendAgentMessage() and the send_agent_message_request handler now call preprocessImages() before buildAgentPrompt()

Test plan

  • All 7 unit tests pass (small image passthrough, oversized resize, aspect ratio preservation, corrupt data fallback, undefined/empty array handling, multiple images)
  • Typecheck passes across all packages
  • Lint + format clean
  • Manual: attach a >2000px image from the app and verify it reaches the agent without [System Error]

The Claude Agent SDK resizes images >2000x2000 using sharp internally,
but sharp may fail to load in the spawned CLI process, causing a
"[System Error] Unable to resize image" error.

Resize images in the daemon's session layer (where sharp is available
from the hoisted SDK dependency) before dispatching to any provider.
Gracefully falls back to sending the original if sharp is unavailable.
@boudra boudra force-pushed the main branch 2 times, most recently from 7ec394c to 5c90449 Compare May 8, 2026 11:48
caoer added 3 commits May 8, 2026 21:56
…fort

- Make sharp import non-static (variable module name) so TypeScript
  doesn't try to resolve it as a dependency at typecheck time
- Map xhigh thinking effort to SDK-accepted "high" value via
  SDK_EFFORT_MAP since the Agent SDK only accepts low/medium/high/max
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.

1 participant