( imageBlock: ImageBlockParam, maxBytes: number = IMAGE_TARGET_RAW_SIZE, )
| 613 | * Wrapper around compressImageBuffer for ImageBlockParam. |
| 614 | */ |
| 615 | export async function compressImageBlock( |
| 616 | imageBlock: ImageBlockParam, |
| 617 | maxBytes: number = IMAGE_TARGET_RAW_SIZE, |
| 618 | ): Promise<ImageBlockParam> { |
| 619 | // Only process base64 images |
| 620 | if (imageBlock.source.type !== 'base64') { |
| 621 | return imageBlock |
| 622 | } |
| 623 | |
| 624 | // Decode base64 to buffer |
| 625 | const imageBuffer = Buffer.from(imageBlock.source.data, 'base64') |
| 626 | |
| 627 | // Check if already within size limit |
| 628 | if (imageBuffer.length <= maxBytes) { |
| 629 | return imageBlock |
| 630 | } |
| 631 | |
| 632 | // Compress the image |
| 633 | const compressed = await compressImageBuffer(imageBuffer, maxBytes) |
| 634 | |
| 635 | return { |
| 636 | type: 'image', |
| 637 | source: { |
| 638 | type: 'base64', |
| 639 | media_type: compressed.mediaType, |
| 640 | data: compressed.base64, |
| 641 | }, |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | // Helper functions for compression pipeline |
| 646 |
no test coverage detected