import { LanguageModelV3Prompt, UnsupportedFunctionalityError, } from '@ai-sdk/provider'; import { GroqChatPrompt } from './groq-api-types'; import { convertToBase64 } from '@ai-sdk/provider-utils'; export function convertToGroqChatMessages( prompt: LanguageModelV3Prompt, ): GroqChatPrompt { const messages: GroqChatPrompt = []; for (const { role, content } of prompt) { switch (role) { case 'system': { messages.push({ role: 'system', content }); break; } case 'user': { if (content.length === 1 && content[0].type === 'text') { messages.push({ role: 'user', content: content[0].text }); break; } messages.push({ role: 'user', content: content.map(part => { switch (part.type) { case 'text': { return { type: 'text', text: part.text }; } case 'file': { if (!part.mediaType.startsWith('image/')) { throw new UnsupportedFunctionalityError({ functionality: 'Non-image file content parts', }); } const mediaType = part.mediaType === 'image/*' ? 'image/jpeg' : part.mediaType; return { type: 'image_url', image_url: { url: part.data instanceof URL ? part.data.toString() : `data:${mediaType};base64,${convertToBase64(part.data)}`, }, }; } } }), }); break; } case 'assistant': { let text = ''; let reasoning = ''; const toolCalls: Array<{ id: string; type: 'function'; function: { name: string; arguments: string }; }> = []; for (const part of content) { switch (part.type) { // groq supports reasoning for tool-calls in multi-turn conversations // https://github.com/vercel/ai/issues/7860 case 'reasoning': { reasoning += part.text; break; } case 'text': { text += part.text; break; } case 'tool-call': { toolCalls.push({ id: part.toolCallId, type: 'function', function: { name: part.toolName, arguments: JSON.stringify(part.input), }, }); break; } } } messages.push({ role: 'assistant', content: text, ...(reasoning.length > 0 ? { reasoning } : null), ...(toolCalls.length > 0 ? { tool_calls: toolCalls } : null), }); break; } case 'tool': { for (const toolResponse of content) { if (toolResponse.type === 'tool-approval-response') { continue; } const output = toolResponse.output; let contentValue: string; switch (output.type) { case 'text': case 'error-text': contentValue = output.value; break; case 'execution-denied': contentValue = output.reason ?? 'Tool execution denied.'; break; case 'content': case 'json': case 'error-json': contentValue = JSON.stringify(output.value); break; } messages.push({ role: 'tool', tool_call_id: toolResponse.toolCallId, content: contentValue, }); } break; } default: { const _exhaustiveCheck: never = role; throw new Error(`Unsupported role: ${_exhaustiveCheck}`); } } } return messages; }