Files
openclaw/extensions/whatsapp/src/image-preview.ts
Peter Steinberger b9f975b64e Replace Sharp image backend with Photon (#86437)
* refactor: replace sharp image backend with photon

* refactor: remove whatsapp jimp dependency

* chore: remove stale sharp install workarounds

* test: keep image fixtures off photon

* test: use valid prompt image fixtures

* test: account for optimized PNG fixtures

* test: use valid minimax image fixtures
2026-05-25 15:04:44 +01:00

49 lines
1.4 KiB
TypeScript

import type { AnyMessageContent } from "baileys";
import { getImageMetadata, resizeToJpeg } from "openclaw/plugin-sdk/media-runtime";
const WHATSAPP_IMAGE_THUMBNAIL_SIDE = 32;
const WHATSAPP_IMAGE_THUMBNAIL_QUALITY = 50;
type ImagePreviewContent = AnyMessageContent & {
image?: unknown;
jpegThumbnail?: unknown;
width?: unknown;
height?: unknown;
};
export async function addWhatsAppImagePreviewFields<T extends AnyMessageContent>(
content: T,
): Promise<T> {
const image = (content as ImagePreviewContent).image;
if (!Buffer.isBuffer(image)) {
return content;
}
const current = content as ImagePreviewContent;
const hasDimensions = typeof current.width === "number" && typeof current.height === "number";
const hasThumbnail = typeof current.jpegThumbnail === "string";
if (hasDimensions && hasThumbnail) {
return content;
}
const metadata = hasDimensions ? null : await getImageMetadata(image).catch(() => null);
if (!hasDimensions && !metadata) {
return content;
}
const thumbnail = hasThumbnail
? null
: await resizeToJpeg({
buffer: image,
maxSide: WHATSAPP_IMAGE_THUMBNAIL_SIDE,
quality: WHATSAPP_IMAGE_THUMBNAIL_QUALITY,
withoutEnlargement: true,
}).catch(() => null);
return {
...content,
...(metadata ? { width: metadata.width, height: metadata.height } : {}),
...(thumbnail ? { jpegThumbnail: thumbnail.toString("base64") } : {}),
};
}