latent.wire.aisdk¶
AI SDK v5 wire protocol — the ai-sdk :class:~latent.wire.WireProtocol.
This is the codec selected by X-Stream-Protocol: ai-sdk (stdlib plus
nanoid for block/message ids — no other third-party deps). It is
the inverse pair promised by ADR-003 D2: :meth:AiSdkWireProtocol.encode
serialises an AgentEvent stream onto the AI SDK UI data-stream SSE, and
:meth:AiSdkWireProtocol.decode parses that SSE back into AgentEvent s so a
latent-native client (Studio's RemoteAgentProxy) reconstructs the canonical
stream from a stock AI SDK response.
The encoder side reuses the migrated lifecycle orchestrator
(:func:~latent.wire.stream.stream_ui_message) verbatim — this module only
adapts a pre-produced event iterator into the BoundProviderAdapter shape
the orchestrator expects, so the start→content→finish→[DONE] lifecycle stays in
one place. The decoder is net-new and intentionally lossy on lifecycle framing
(ADR-009 D5): pure-structural parts (start, text-start/text-end,
tool-input-start/tool-input-delta, …) are consumed and dropped; only
parts that carry content map back to an AgentEvent.
Classes¶
AiSdkWireProtocol¶
Bidirectional AI SDK v5 codec (X-Stream-Protocol: ai-sdk).
Methods¶
AiSdkWireProtocol.decode¶
Parse AI SDK UI data-stream SSE back into AgentEvent s.
Content-bearing parts map to canonical events; lifecycle/structural
parts (start, text-start/text-end, reasoning-start/
reasoning-end, tool-input-start/tool-input-delta,
finish-step, source-url, [DONE]) are consumed and dropped
(ADR-009 D5). start-step maps to a :class:StepBoundary — using the
native latentStep/latentLabel fidelity when present, else a
re-derived incrementing index (foreign streams); data-latent-<name>
parts reconstruct the
exact canonical event they carry (ADR-003 D2 fidelity). A foreign
finish with usage metadata synthesises a :class:Usage (latent
sources carry usage as data-latent-Usage instead).
AiSdkWireProtocol.decode_request¶
Parse an AI SDK v5 request body into (messages, config).
Accepts the v5 parts shape: text parts become the message
content, and each tool part (dynamic-tool or tool-<name>)
becomes a :class:ToolCall on that message plus, once it has an
output, a following role="tool" row with the same tool_call_id.
Falls back to the flat OpenAI shape {role, content, tool_calls?,
tool_call_id?}. config is the request dict with messages
removed. An empty or absent body yields ([], {}).
AiSdkWireProtocol.encode¶
Encode an AgentEvent stream onto the AI SDK UI data stream.
Reuses :func:stream_ui_message for the full start→finish→[DONE]
lifecycle; this method only feeds the pre-produced events into it.
AiSdkWireProtocol.encode_request¶
Serialize messages into an AI SDK v5 request body (inverse of
:meth:decode_request). Tool calls and their results travel as tool
parts on the assistant message; a tool result with no matching call,
or a non-text content block, raises ValueError.