UiMessageStreamBackend

class UiMessageStreamBackend(endpoint: String, model: String = "", apiKey: String = "", tools: List<AgentTool> = emptyList(), approver: ToolApprover = ToolApprover.AlwaysApprove, client: OkHttpClient = DefaultHttpClient, maxRounds: Int = 8) : ChatBackend(source)

Talks to an agent server speaking a Vercel AI SDK streaming protocol — e.g. the bundled Pydantic AI server (server/main.py), or any AI SDK route:

  • v5/v6 UI Message Stream: SSE data: {"type": "text-delta", ...} chunks (toUIMessageStreamResponse()), ending in [DONE];

  • v4 Data Stream: 0:"text", g:"reasoning", 9:{toolCall}… lines (toDataStreamResponse()). Detected automatically per line.

Request: POST endpoint with {trigger, id, messages: UIMessage[]} — the whole history, including tool parts, as useChat sends it.

Like useChat, the turn continues automatically when the server hands work back to the client:

  • tool approval (AI SDK 6): a tool-approval-request asks approver; the answer is sent back as an approval-responded tool part (approval.reason carries the user's reason; the protocol has no edited arguments);

  • client-side tools: a tool-input-available for one of tools with no server output runs the tool here and sends its output-available part (sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithToolCalls).

A tool output that is itself a UIMessage (a subagent streaming through preliminary tool results) is shown as that tool's nested run.

Parameters

model

optional model override, sent as ?model= (server-specific).

Constructors

Link copied to clipboard
constructor(endpoint: String, model: String = "", apiKey: String = "", tools: List<AgentTool> = emptyList(), approver: ToolApprover = ToolApprover.AlwaysApprove, client: OkHttpClient = DefaultHttpClient, maxRounds: Int = 8)

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
open override fun stream(history: List<Message>): Flow<ChatEvent>