Documentation
¶
Overview ¶
Image resize + re-encode policy for CLI/LLM image attachments, plus the EXIF orientation reader (upstream exif-orientation.ts). PNG conversion for Kitty (upstream image-convert.ts) lives in image_convert.go.
Mirrors upstream packages/coding-agent/src/utils/image-resize.ts using the Go standard library plus golang.org/x/image in place of Photon:
- Decode + apply EXIF orientation once.
- If needed, resize to fit within 2000x2000.
- Try PNG first, then JPEG quality steps until the base64 payload fits under 4.5 MB.
- If still too large, shrink dimensions by 25% and retry.
Index ¶
- Constants
- Variables
- func ConvertImageBytesToPng(data []byte) []byte
- func DecodeImage(data []byte) (image.Image, error)
- func DetectSupportedImageMimeType(buffer []byte) string
- func DetectSupportedImageMimeTypeFromFile(filePath string) (string, error)
- func NormalizeToolResultImages(result agent.AgentToolResult, autoResize bool) agent.AgentToolResult
- func NormalizeToolResultImagesWithOptions(result agent.AgentToolResult, autoResize bool, ...) agent.AgentToolResult
- func PrepareCLIImageAttachment(in []byte, inputMIME string) ([]byte, string, string, error)
- func ProcessImage(in []byte, inputMIME string, autoResize bool, ...) ([]byte, string, string, error)
- func ResizeImageForLLM(in []byte) ([]byte, string, error)
- type ProcessImageFunc
- type ResizedImage
Constants ¶
const ( MaxLongestSide = 2000 MaxEncodedBytes = int(4.5 * 1024 * 1024) // base64 payload bytes JPEGQuality = 80 )
const ImageTypeSniffBytes = 4100
ImageTypeSniffBytes mirrors upstream IMAGE_TYPE_SNIFF_BYTES: detection looks only at a file's first 4100 bytes.
Variables ¶
var ErrImageTooLarge = errors.New("image: could not be resized below inline image size limit")
Functions ¶
func ConvertImageBytesToPng ¶
ConvertImageBytesToPng applies EXIF orientation before encoding PNG, as upstream image-convert.ts does through Photon. Failure returns nil.
func DecodeImage ¶
DecodeImage decodes the bytes Pi hands to Photon's PhotonImage.new_from_byteslice. Photon 0.3.4 decodes with the image 0.24.9 crate, which also accepts 16-bit BMPs and BI_BITFIELDS BMPs under BITMAPINFOHEADER, BITMAPV2INFOHEADER and BITMAPV3INFOHEADER. golang.org/x/image/bmp rejects those layouts, so they fall back to decodeBitfieldBMP. Windows places such bitmaps on the clipboard as CF_DIB for Print Screen and GDI captures. upstream: packages/coding-agent/src/utils/image-convert.ts:convertImageBytesToPng
func DetectSupportedImageMimeType ¶
DetectSupportedImageMimeType mirrors upstream mime.ts and returns a supported still-image MIME type for the provided bytes, or "" when unsupported.
The formats named here are also the only decoders this package links, so a format outside the allowlist has no decoder to reach. Adding a MIME type here or to tools.SupportedImageMime without adding its decoder import decodes nothing; adding the decoder import widens the attack surface of the image pipeline to that format's parser.
func DetectSupportedImageMimeTypeFromFile ¶
DetectSupportedImageMimeTypeFromFile is upstream detectSupportedImageMimeTypeFromFile (utils/mime.ts:25): it reads the sniff window from the start of a file and returns the supported still-image MIME type, or "" when unsupported or empty. A file that cannot be opened or read is the error.
func NormalizeToolResultImages ¶
func NormalizeToolResultImages(result agent.AgentToolResult, autoResize bool) agent.AgentToolResult
NormalizeToolResultImages processes tool images before history. Each conversion or resize hint follows its image; failures and unchanged results retain the original blocks and content slice. Ports packages/coding-agent/src/utils/tool-result-images.ts.
func NormalizeToolResultImagesWithOptions ¶
func NormalizeToolResultImagesWithOptions(result agent.AgentToolResult, autoResize bool, options *ai.ModelImageResizeOptions) agent.AgentToolResult
NormalizeToolResultImagesWithOptions applies the active model profile after extension hooks. Failed image processing retains the original image block.
func PrepareCLIImageAttachment ¶
PrepareCLIImageAttachment processes one CLI image file into the attached image payload plus the upstream dimension note (empty when not resized).
func ProcessImage ¶
func ProcessImage(in []byte, inputMIME string, autoResize bool, options *ai.ModelImageResizeOptions) ([]byte, string, string, error)
ProcessImage normalizes unsupported formats before applying a model's resize profile. A false autoResize flag still converts unsupported formats. Errors carry upstream's omission hint; callers decide whether to keep a tool image.
Types ¶
type ProcessImageFunc ¶
type ProcessImageFunc func([]byte, string, bool, *ai.ModelImageResizeOptions) ([]byte, string, string, error)
ProcessImageFunc is the selected image processor at the model-input boundary.
type ResizedImage ¶
type ResizedImage struct {
Data string
MimeType string
OriginalWidth int
OriginalHeight int
Width int
Height int
WasResized bool
}
ResizedImage is upstream's ResizedImage (image-resize-core.ts:9). Data is the image's base64 text, as upstream's `data`; Bytes returns the decoded image.
func ResizeImage ¶
func ResizeImage(in []byte, mimeType string, options *ai.ModelImageResizeOptions) (ResizedImage, error)
ResizeImage fits an image within the option limits and the encoded size limit, as upstream resizeImage (image-resize.ts:85) does. It reports an error where upstream returns null. An image already within every limit returns the input bytes under the declared mimeType, or image/png when none is declared (image-resize-core.ts:80).
func (ResizedImage) Bytes ¶
func (r ResizedImage) Bytes() []byte
Bytes returns the image Data encodes. Data is always the base64 text this package produced, so it decodes.