Files
anything-llm/server/endpoints/api/openai/helpers.js
Marcello Fitton 4a4378ed99 chore: add ESLint to /server (#5126)
* add eslint config to server

* add break statements to switch case

* add support for browser globals and turn off empty catch blocks

* disable lines with useless try/catch wrappers

* format

* fix no-undef errors

* disbale lines violating no-unsafe-finally

* ignore syncStaticLists.mjs

* use proper null check for creatorId instead of unreachable nullish coalescing

* remove unneeded typescript eslint comment

* make no-unused-private-class-members a warning

* disable line for no-empty-objects

* add new lint script

* fix no-unused-vars violations

* make no-unsued-vars an error

---------

Co-authored-by: shatfield4 <seanhatfield5@gmail.com>
Co-authored-by: Timothy Carambat <rambat1010@gmail.com>
2026-03-05 16:32:45 -08:00

51 lines
1.7 KiB
JavaScript

/**
* 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 {
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,
};