UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 2.22 kB
import{a as isAvatarImageMimeType,o as isRenderableAvatarImageDataUrl,t as AVATAR_MAX_BYTES}from"./avatar-limits-CDL29nOi.mjs";import{c as resolveAvatarMime}from"./avatar-policy-ybXAwETm.mjs";import{d as normalizeMimeType}from"./mime-ZmYZsEjx.mjs";import{c as isAnimatedWebpBuffer,h as createImageProcessor}from"./image-ops-BYBI_WlA.mjs";import{i as createHttpImageRepresentation}from"./http-image-response-DDogkDyc.mjs"; //!src/gateway/assistant-avatar-thumbnail.runtime.ts const AVATAR_THUMBNAIL_SIDE=128;const thumbnails=/* @__PURE__ */ new WeakMap;const notificationIcons=/* @__PURE__ */ new WeakMap;async function createAvatarThumbnail(e,t=AVATAR_THUMBNAIL_SIDE,n){let r;let i;if(`file`in e){if(!e.file.body)throw new Error(`Avatar bytes were not prepared`);r=e.file.body;i=resolveAvatarMime(e.file.path)}else{if(!isRenderableAvatarImageDataUrl(e.dataUrl))throw new Error(`Unsupported avatar data URL`);const t=await fetch(e.dataUrl);i=t.headers.get(`content-type`)??``;r=Buffer.from(await t.arrayBuffer());if(r.length>2097152)throw new Error(`Avatar data URL exceeds size limit`)}const a=normalizeMimeType(i);if(!a||!isAvatarImageMimeType(a))throw new Error(`Unsupported avatar image type`);if([`image/png`,`image/jpeg`,`image/webp`].includes(a)){const{fileTypeFromBuffer:e}=await import(`file-type`);if((await e(r))?.mime===`image/apng`||isAnimatedWebpBuffer(r))return createHttpImageRepresentation(r,i);r=(await createImageProcessor().encode(r,{format:`png`,resize:{maxSide:t,enlarge:false},signal:n})).data;i=`image/png`}return createHttpImageRepresentation(r,i)}async function readGatewayAvatarThumbnail(e){let t=thumbnails.get(e);if(!t){t=createAvatarThumbnail(e).catch(t=>{thumbnails.delete(e);throw t});thumbnails.set(e,t)}return t}function readGatewayAvatarNotificationIcon(e){let t=notificationIcons.get(e);if(!t){t=(async()=>{const t=AbortSignal.timeout(5e3);for(const n of[32,24]){const r=await createAvatarThumbnail(e,n,t);if(r.contentType!==`image/png`)return;const i=`data:image/png;base64,${r.body.toString(`base64`)}`;if(i.length<=2400)return i}})().catch(()=>{notificationIcons.delete(e)});notificationIcons.set(e,t)}return t}export{readGatewayAvatarNotificationIcon,readGatewayAvatarThumbnail};