import sharp from "sharp";
const MAX_BASE64_BYTES = 750_000; // ~750KB target (well under 1MB)
const FIRST_PASS_WIDTH = 1920;
const SECOND_PASS_WIDTH = 1280;
export async function compressScreenshot(buffer: Buffer): Promise<string> {
let img = sharp(buffer).resize({ width: FIRST_PASS_WIDTH, withoutEnlargement: true }).png({ compressionLevel: 6 });
let output = await img.toBuffer();
if (output.length > MAX_BASE64_BYTES) {
img = sharp(buffer).resize({ width: SECOND_PASS_WIDTH, withoutEnlargement: true }).png({ compressionLevel: 9 });
output = await img.toBuffer();
}
return output.toString("base64");
}
export function imageContent(base64: string) {
return {
content: [
{
type: "image" as const,
data: base64,
mimeType: "image/png" as const,
},
],
};
}