File size: 1,784 Bytes
f8b5d42 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | /**
* Extracts text content from a multimodal message
* If the content has multiple text items, it will join them together with a newline.
* @param {string|Array} content - Message content that could be string or array of content objects
* @returns {string} - The text content
*/
function extractTextContent(content) {
if (!Array.isArray(content)) return content;
return content
.filter((item) => item.type === "text")
.map((item) => item.text)
.join("\n");
}
/**
* Detects mime type from a base64 data URL string, defaults to PNG if not detected
* @param {string} dataUrl - The data URL string (e.g. data:image/jpeg;base64,...)
* @returns {string} - The mime type or 'image/png' if not detected
*/
function getMimeTypeFromDataUrl(dataUrl) {
try {
const matches = dataUrl.match(/^data:([^;]+);base64,/);
return matches ? matches[1].toLowerCase() : "image/png";
} catch (e) {
return "image/png";
}
}
/**
* Extracts attachments from a multimodal message
* The attachments provided are in OpenAI format since this util is used in the OpenAI compatible chat.
* However, our backend internal chat uses the Attachment type we use elsewhere in the app so we have to convert it.
* @param {Array} content - Message content that could be string or array of content objects
* @returns {import("../../../utils/helpers").Attachment[]} - The attachments
*/
function extractAttachments(content) {
if (!Array.isArray(content)) return [];
return content
.filter((item) => item.type === "image_url")
.map((item, index) => ({
name: `uploaded_image_${index}`,
mime: getMimeTypeFromDataUrl(item.image_url.url),
contentString: item.image_url.url,
}));
}
module.exports = {
extractTextContent,
extractAttachments,
};
|