From 046dde720a42f19351fde8a00e3e724005e56721 Mon Sep 17 00:00:00 2001 From: multipleof4 Date: Fri, 12 Sep 2025 11:44:14 -0700 Subject: [PATCH] feat: update index.js --- index.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 1684ed5..51d25f8 100644 --- a/index.js +++ b/index.js @@ -223,10 +223,20 @@ export class MyDurableObject { mapToGoogleContents(messages) { const contents = messages.reduce((acc, m) => { - const role = m.role === 'assistant' ? 'model' : 'user', text = this.extractTextFromMessage(m); - if (!text) return acc; - if (acc.length > 0 && acc.at(-1).role === role) acc.at(-1).parts.push({ text }); - else acc.push({ role, parts: [{ text }] }); + const role = m.role === 'assistant' ? 'model' : 'user'; + const msgContent = Array.isArray(m.content) ? m.content : [{ type: 'text', text: String(m.content ?? '') }]; + const parts = msgContent.map(p => { + if (p.type === 'text') return { text: p.text || '' }; + if (p.type === 'image_url' && p.image_url?.url) { + const match = p.image_url.url.match(/^data:(image\/\w+);base64,(.*)$/); + if (match) return { inline_data: { mime_type: match[1], data: match[2] } }; + } + return null; + }).filter(Boolean); + + if (!parts.length) return acc; + if (acc.length > 0 && acc.at(-1).role === role) acc.at(-1).parts.push(...parts); + else acc.push({ role, parts }); return acc; }, []); if (contents.at(-1)?.role !== 'user') contents.pop();