convert To Model Messages
Convert a list of UI-shape UIMessages back into the model-shape ModelMessage list the LLM expects on the next turn. Mirrors v6's convertToModelMessages (per historical parity gap #5).
Required for any flow that PERSISTS the chat in its rendered form and later wants to RESUME the agent: history replay, crash recovery, subagent continuation, manual edit & re-run.
Conversion rules:
UIMessagePart.Text →
ContentPart.TextUIMessagePart.Reasoning →
ContentPart.ReasoningUIMessagePart.ToolUI (static) AND UIMessagePart.DynamicToolUI (runtime) share one rule, dispatched by
state:OutputAvailable+preliminary=false→ assistant carriesContentPart.ToolCall; a SEPARATE follow-upTool-role ModelMessage carriesContentPart.ToolResult. (Preliminary snapshots fromstreamingToolnever flow to the model — only the final emission. Preliminary parts are dropped here.)ApprovalRequired→ContentPart.ToolApprovalRequestInputStreaming/InputAvailableare INCOMPLETE — drop silently if ignoreIncompleteToolCalls, otherwise throw.Error→ drop. The tool failed; the model already saw aContentPart.ToolResultwithisError=trueat the time (encoded by the agent loop), no useful re-send shape.UIMessagePart.StepStart,
Source,File,Error→ drop. No useful model-side representation, or out-of-scope for v0.
Ordering is preserved: each UIMessage with tool calls becomes: Assistant(text, ToolCall...) → Tool(ToolResult) → Tool(ToolResult) → … — which is what the model expects on the next turn.
Parameters
The UI-shape history to convert.
If true, drop tool parts in InputStreaming / InputAvailable state silently. If false (default), throw IllegalStateException when an incomplete tool call is seen — these usually indicate corrupted history.