openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
77 lines (76 loc) • 2.68 kB
JavaScript
import { a as isAvatarImageMimeType, o as isRenderableAvatarImageDataUrl, t as AVATAR_MAX_BYTES } from "./avatar-limits-DJrDD3v6.mjs";
import { c as resolveAvatarMime } from "./avatar-policy-C1iXc49e.mjs";
import { d as normalizeMimeType } from "./mime-D2wVBzpa.mjs";
import { c as isAnimatedWebpBuffer, h as createImageProcessor } from "./image-ops-zeupBxnN.mjs";
import { i as createHttpImageRepresentation } from "./http-image-response-BMmTktPR.mjs";
//#region 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;
}
/** Inline push icons work while the browser is offline from Gateway image authentication. */
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;
}
//#endregion
export { readGatewayAvatarNotificationIcon, readGatewayAvatarThumbnail };