( imageBlock: ImageBlockParam, maxBytes: number = IMAGE_TARGET_RAW_SIZE, )
| 600 | * Wrapper around compressImageBuffer for ImageBlockParam. |
| 601 | */ |
| 602 | export async function compressImageBlock( |
| 603 | imageBlock: ImageBlockParam, |
| 604 | maxBytes: number = IMAGE_TARGET_RAW_SIZE, |
| 605 | ): Promise<ImageBlockParam> { |
| 606 | // Only process base64 images |
| 607 | if (imageBlock.source.type !== 'base64') { |
| 608 | return imageBlock |
| 609 | } |
| 610 | |
| 611 | // Decode base64 to buffer |
| 612 | const imageBuffer = Buffer.from(imageBlock.source.data, 'base64') |
| 613 | |
| 614 | // Check if already within size limit |
| 615 | if (imageBuffer.length <= maxBytes) { |
| 616 | return imageBlock |
| 617 | } |
| 618 | |
| 619 | // Compress the image |
| 620 | const compressed = await compressImageBuffer(imageBuffer, maxBytes) |
| 621 | |
| 622 | return { |
| 623 | type: 'image', |
| 624 | source: { |
| 625 | type: 'base64', |
| 626 | media_type: compressed.mediaType, |
| 627 | data: compressed.base64, |
| 628 | }, |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | // Helper functions for compression pipeline |
| 633 |
no test coverage detected