fix(vision): downscale oversized images and follow image URL redirects - #1
Open
xiaomaozjj666 wants to merge 1 commit into
Open
fix(vision): downscale oversized images and follow image URL redirects#1xiaomaozjj666 wants to merge 1 commit into
xiaomaozjj666 wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two practical fixes for the vision tool, both hit real deployments (verified against the ModelScope API-Inference endpoint):
Downscale images that exceed the vision endpoint's input limit. ModelScope API-Inference rejects images above 2048×2048 with HTTP 400 (
input size exceed limit 2048x2048). This applies to any 2K/4K screenshot or large photo. The fix resizes (aspect-preserving, fit-inside) via sharp when it is resolvable, and falls back to the original bytes when sharp is not installed — so deployments without sharp keep exactly the previous behavior. Animated GIF inputs reduce to their first frame, which is acceptable for the description use case.Follow redirects when fetching image URLs. Many public image hosts (picsum.photos, imgur, …) answer with a 302 to their CDN. The tool previously used
redirect: 'error', so those URLs failed withfetch failed.redirect: 'follow'keeps the fetch to http(s) targets only (undici rejects non-http(s) redirect targets), preserving the SSRF posture for non-http schemes.Changes
src/index.ts:toImage()becomes async and passes bytes throughfitImageWithinVisionLimit()(new helper,VISION_MAX_SIDE = 2048);loadImage()URL fetch follows redirects. The vision endpoint POST keepsredirect: 'error'.Verification
https://picsum.photos/...URL (302 → fastly CDN) was fetched and described successfully. Asharpmodule is resolvable in the DSH host profile.Notes
sharpis intentionally an optional, dynamic import — no new hard dependency.VISION_MAX_SIDEas a config option if other endpoints have different limits.