( modelMessage: ModelMessage, id?: string, )
| 400 | * @returns A UIMessage with parts |
| 401 | */ |
| 402 | export function modelMessageToUIMessage( |
| 403 | modelMessage: ModelMessage, |
| 404 | id?: string, |
| 405 | ): UIMessage { |
| 406 | const parts: Array<MessagePart> = [] |
| 407 | |
| 408 | if (modelMessage.role === 'assistant' && modelMessage.thinking?.length) { |
| 409 | for (const thinking of modelMessage.thinking) { |
| 410 | if (!thinking.content) continue |
| 411 | parts.push({ |
| 412 | type: 'thinking', |
| 413 | content: thinking.content, |
| 414 | ...(thinking.signature && { signature: thinking.signature }), |
| 415 | }) |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | // Handle tool results (when role is "tool") - only produce tool-result part, |
| 420 | // not a text part (the content IS the tool result, not display text) |
| 421 | if (modelMessage.role === 'tool' && modelMessage.toolCallId) { |
| 422 | parts.push({ |
| 423 | type: 'tool-result', |
| 424 | toolCallId: modelMessage.toolCallId, |
| 425 | content: getTextContent(modelMessage.content), |
| 426 | state: 'complete', |
| 427 | }) |
| 428 | } else if (Array.isArray(modelMessage.content)) { |
| 429 | // Multimodal content - preserve all content parts as MessageParts |
| 430 | for (const part of modelMessage.content) { |
| 431 | parts.push(part) |
| 432 | } |
| 433 | } else { |
| 434 | // String or null content |
| 435 | const textContent = getTextContent(modelMessage.content) |
| 436 | if (textContent) { |
| 437 | parts.push({ |
| 438 | type: 'text', |
| 439 | content: textContent, |
| 440 | }) |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | // Handle tool calls |
| 445 | if (modelMessage.toolCalls && modelMessage.toolCalls.length > 0) { |
| 446 | for (const toolCall of modelMessage.toolCalls) { |
| 447 | parts.push({ |
| 448 | type: 'tool-call', |
| 449 | id: toolCall.id, |
| 450 | name: toolCall.function.name, |
| 451 | arguments: toolCall.function.arguments, |
| 452 | state: 'input-complete', // Model messages have complete arguments |
| 453 | ...(toolCall.metadata !== undefined && { metadata: toolCall.metadata }), |
| 454 | }) |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | return { |
| 459 | id: id || generateMessageId(), |
no test coverage detected