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