chatTurnsFromMessages function
Builds the engine-neutral structured view of messages passed to
LlamaSession.generate alongside the rendered prompt for image turns.
Content parts keep the author's ordering (text and media interleaved as
written). Media is extracted with the same rule the ChatFormat
templates use — DataContent with non-null bytes — typed by kind so the
message-level multimodal path can label each part (image vs audio).
Other content kinds (tool calls, tool results) have no part
representation and are dropped; their text, if any, is what reaches the
model through the rendered prompt.
Implementation
List<LlamaChatTurn> chatTurnsFromMessages(Iterable<ChatMessage> messages) =>
<LlamaChatTurn>[
for (final message in messages)
LlamaChatTurn(
role: message.role == ChatRole.system
? LlamaChatRole.system
: message.role == ChatRole.assistant
? LlamaChatRole.assistant
: message.role == ChatRole.tool
? LlamaChatRole.tool
: LlamaChatRole.user,
parts: <LlamaContentPart>[
for (final content in message.contents)
if (content is TextContent)
LlamaTextPart(content.text)
else if (content is DataContent && content.data != null)
if (content.hasTopLevelMediaType('image'))
LlamaImagePart(content.data!, mimeType: content.mediaType)
else if (content.hasTopLevelMediaType('audio'))
LlamaAudioPart(content.data!, mimeType: content.mediaType),
],
),
];