Spaces:
Running
Running
| export function prepareConversation(messages) { | |
| const imageUrls = []; | |
| const preparedMessages = messages.map(message => { | |
| if (!Array.isArray(message.content)) { | |
| const prepared = { role: message.role, content: message.content }; | |
| if (message.tool_calls) prepared.tool_calls = message.tool_calls; | |
| return prepared; | |
| } | |
| const imageCount = message.content.filter(item => item.type === 'image').length; | |
| let imageIndex = 0; | |
| const content = message.content.flatMap(item => { | |
| if (item.type !== 'image') return [{ type: 'text', text: item.value || '' }]; | |
| imageUrls.push(item.value); | |
| imageIndex += 1; | |
| if (imageCount < 2) return [{ type: 'image' }]; | |
| return [ | |
| { type: 'text', text: `Media-${imageIndex}` }, | |
| { type: 'image' }, | |
| { type: 'text', text: '\n' }, | |
| ]; | |
| }); | |
| const prepared = { role: message.role, content }; | |
| if (message.tool_calls) prepared.tool_calls = message.tool_calls; | |
| return prepared; | |
| }); | |
| return { messages: preparedMessages, imageUrls }; | |
| } | |