MCPcopy Create free account
hub / github.com/pollinations/pollinations / formatMessagesForAPI

Function formatMessagesForAPI

apps/chat/src/utils/api.js:214–276  ·  view source on GitHub ↗
(messages, _modelId)

Source from the content-addressed store, hash-verified

212};
213
214export const formatMessagesForAPI = (messages, _modelId) => {
215 return messages.map((msg) => {
216 const parts = [];
217 const textContent = typeof msg.content === "string" ? msg.content : "";
218 if (textContent) parts.push({ type: "text", text: textContent });
219
220 const attachments = Array.isArray(msg.attachments)
221 ? msg.attachments
222 : [];
223 const legacyImage =
224 !attachments.length && msg.image?.src
225 ? [
226 {
227 name: msg.image.name || "image",
228 data: msg.image.src,
229 mimeType: msg.image.mimeType || "image/png",
230 isImage: true,
231 },
232 ]
233 : [];
234
235 for (const attachment of [...attachments, ...legacyImage]) {
236 if (!attachment) continue;
237 let base64Data = attachment.data || attachment.base64 || "";
238 let mimeType =
239 attachment.mimeType ||
240 attachment.type ||
241 "application/octet-stream";
242
243 if (!base64Data && attachment.preview) {
244 const e = extractBase64FromDataUrl(attachment.preview);
245 base64Data = e.base64;
246 if (e.mimeType && !attachment.mimeType) mimeType = e.mimeType;
247 }
248 if (!base64Data && typeof attachment.src === "string") {
249 const e = extractBase64FromDataUrl(attachment.src);
250 base64Data = e.base64;
251 if (e.mimeType) mimeType = e.mimeType;
252 }
253 if (!base64Data) continue;
254
255 const isImage = attachment.isImage ?? mimeType.startsWith("image/");
256 if (isImage && attachment.preview?.startsWith("http")) {
257 parts.push({
258 type: "image_url",
259 image_url: { url: attachment.preview },
260 });
261 continue;
262 }
263 if (isImage && base64Data) {
264 parts.push({
265 type: "image_url",
266 image_url: { url: `data:${mimeType};base64,${base64Data}` },
267 });
268 }
269 }
270
271 if (parts.length === 1 && parts[0].type === "text")

Callers 1

sendMessageFunction · 0.85

Calls 2

extractBase64FromDataUrlFunction · 0.85
pushMethod · 0.80

Tested by

no test coverage detected