latent.wire.agui¶
AG-UI wire protocol — the ag-ui :class:~latent.wire.WireProtocol.
This is the second codec promised by ADR-004 / FR-10: the additive-codec design
(ADR-002 D2) says a new protocol is only a new WireProtocol registration,
leaving serving/proxy code untouched. ag-ui proves that — it reuses the same
AgentEvent -> NormalizedEvent intermediate representation the ai-sdk
codec consumes (:func:~latent.wire.providers.native.convert.convert_native_event),
then serialises those normalized events onto the Agent User Interaction Protocol
<https://docs.ag-ui.com>_ SSE event stream instead of the AI SDK data stream.
The encoder side adopts ag_ui.encoder.EventEncoder (the spike's Q1 decision):
EventEncoder().encode(event) returns an SSE data: {json}\n\n record for
any ag_ui.core.events.BaseEvent. This module owns the run lifecycle loop
(RUN_STARTED … RUN_FINISHED) rather than reusing
:func:~latent.wire.stream.stream_ui_message, which is AI-SDK-specific.
The decoder is net-new and hand-rolled (the library ships no decoder): it parses
each data: JSON record back into the canonical AgentEvent. As with the
ai-sdk codec it is intentionally lossy on lifecycle framing (ADR-009 D5) —
RUN_STARTED/RUN_FINISHED, the *_START/*_END block brackets, and
STEP_FINISHED are consumed and dropped; only content-bearing events map back.
Fidelity (ADR-003 D2 / ADR-009 D5): every canonical AgentEvent with no
natural AG-UI representation rides a single CUSTOM event whose name is the
class name and whose value is the serialized payload — the exact analogue of
the ai-sdk codec's data-latent-<name> part. So decode(encode([e])) ==
[e] holds for the whole taxonomy, not just the conformance subset.
Classes¶
AgUiWireProtocol¶
Bidirectional AG-UI codec (X-Stream-Protocol: ag-ui).
Methods¶
AgUiWireProtocol.decode¶
Parse an AG-UI SSE event stream back into AgentEvent s.
Content-bearing events map back to canonical events; lifecycle/structural
events (RUN_STARTED/RUN_FINISHED, TEXT_MESSAGE_START/_END,
the reasoning brackets, TOOL_CALL_START, STEP_FINISHED) are
consumed and dropped (ADR-009 D5). CUSTOM events whose name is a
canonical class reconstruct the exact event they carry; tool-call args
accumulate across TOOL_CALL_START/TOOL_CALL_ARGS and emit a
:class:ToolCall on TOOL_CALL_END. Unknown events are dropped
without crashing.
AgUiWireProtocol.decode_request¶
Parse an AG-UI request body into (messages, config).
AG-UI clients post a :class:ag_ui.core.RunAgentInput. We validate the
body with it when it fits, lifting its messages into latent
:class:Messages and returning the remaining run metadata
(threadId/runId/tools/context/…) as config. A body
that is not a well-formed RunAgentInput (e.g. a stock AI-SDK-style
{messages:[{role, content|parts}]} payload) falls back to the same
permissive parse the ai-sdk codec uses, so either client works. An
empty body yields ([], {}).
AgUiWireProtocol.encode¶
Encode an AgentEvent stream onto the AG-UI SSE event stream.
Owns the AG-UI run lifecycle directly (rather than reusing the
AI-SDK-specific :func:stream_ui_message): emits RUN_STARTED first,
converts each AgentEvent to normalized events via
:func:convert_native_event (the shared IR), maps each normalized event
to its AG-UI event(s), closes open blocks via the __finalize__
sentinel, and terminates with exactly one of RUN_FINISHED (success)
or RUN_ERROR (failure) — the AG-UI spec ends a run with one or the
other, never both — so the consumer never waits indefinitely.
AgUiWireProtocol.encode_request¶
Serialize messages into an AG-UI RunAgentInput body.
Inverse of :meth:decode_request: builds a minimal RunAgentInput
(filling the required state/tools/context/forwarded_props
with empty values when config omits them) so a stock AG-UI server
accepts it. config keys override the defaults.