MCPcopy Create free account
hub / github.com/TeleBoxOrg/TeleBox_Plugins / collectImagePartsFromSingleMessage

Function collectImagePartsFromSingleMessage

ai/ai.ts:712–784  ·  view source on GitHub ↗
(
  msg: Api.Message,
  out: AIContentPart[],
)

Source from the content-addressed store, hash-verified

710
711const collectImagePartsFromSingleMessage = async (
712 msg: Api.Message,
713 out: AIContentPart[],
714): Promise<void> => {
715 if (!msg.media || !msg.client) return;
716
717 if (msg.media instanceof Api.MessageMediaPhoto) {
718 const downloaded = await msg.client.downloadMedia(msg);
719 const buffer = await normalizeDownloadedMedia(downloaded);
720 if (!buffer) return;
721 const dataUrl = `data:image/jpeg;base64,${buffer.toString("base64")}`;
722 out.push({ type: "image_url", image_url: { url: dataUrl } });
723 return;
724 }
725
726 if (
727 msg.media instanceof Api.MessageMediaDocument &&
728 msg.media.document instanceof Api.Document
729 ) {
730 const doc = msg.media.document;
731 const docMime = doc.mimeType || "";
732 const isAnimated =
733 docMime === "image/gif" ||
734 docMime === "video/webm" ||
735 docMime === "application/x-tgsticker" ||
736 docMime === "application/x-tg-sticker" ||
737 doc.attributes?.some(
738 (attr) => attr instanceof Api.DocumentAttributeAnimated,
739 );
740
741 const thumb = getDocumentThumb(doc);
742
743 if (!isAnimated && docMime.startsWith("image/")) {
744 const downloaded = await msg.client.downloadMedia(msg);
745 const buffer = await normalizeDownloadedMedia(downloaded);
746 if (!buffer) return;
747 const dataUrl = `data:${docMime};base64,${buffer.toString("base64")}`;
748 out.push({ type: "image_url", image_url: { url: dataUrl } });
749 return;
750 }
751
752 let frameBuffer: Buffer | null = null;
753
754 if (thumb) {
755 const downloaded = await msg.client.downloadMedia(msg, { thumb });
756 const buffer = await normalizeDownloadedMedia(downloaded);
757 if (buffer) {
758 try {
759 frameBuffer = await sharp(buffer).png().toBuffer();
760 } catch {
761 frameBuffer = buffer;
762 }
763 }
764 }
765
766 if (!frameBuffer) {
767 const downloaded = await msg.client.downloadMedia(msg);
768 const buffer = await normalizeDownloadedMedia(downloaded);
769 if (buffer) {

Callers 1

getMessageImagePartsFunction · 0.85

Calls 4

normalizeDownloadedMediaFunction · 0.85
getDocumentThumbFunction · 0.85
extractFirstFrameFunction · 0.85
downloadMediaMethod · 0.45

Tested by

no test coverage detected