mirror of
https://github.com/openclaw/openclaw.git
synced 2026-06-06 05:51:15 +08:00
* 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
49 lines
1.4 KiB
TypeScript
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") } : {}),
|
|
};
|
|
}
|