Skip to content

latent.wire

latent.wire — wire codecs as pluggable WireProtocol implementations.

A wire protocol encodes the canonical :class:~latent.events.AgentEvent stream onto a streaming wire (AI SDK SSE, AG-UI, …) and decodes it back. It is bidirectional (ADR-009 D3): X-Stream-Protocol selects the response encoding, while decode_request accepts the request body — latent-native for Studio's RemoteAgentProxy, protocol-native for a stock client.

This seam is dependency-free so importing it never pulls a web/codec dep — import latent.wire stays light (enforced by tests/unit/test_import_without_extras.py). Concrete codecs (ai-sdk, pure stdlib; ag-ui, via ag-ui-protocol) register themselves via :func:register_wire_protocol; adding one is a single registration and leaves serving/proxy code untouched (ADR-002 D2's additive-codec promise).

Classes

WireProtocol

WireProtocol()

Bidirectional codec between AgentEvents and a wire protocol's bytes.

Server side consumes :meth:decode_request + :meth:encode; the client/proxy side consumes :meth:encode_request + :meth:decode. The same instance serves both ends so encode/decode stay a single inverse pair (encode ∘ decode = identity for latent-native agents — ADR-003 D2).

Functions

get_wire_protocol

get_wire_protocol(name: str = 'ai-sdk') -> WireProtocol

Return the registered codec for name; raise ValueError if unknown.

A built-in codec is imported on first lookup so it self-registers — the codec (and its extra deps) load only when actually requested.

register_wire_protocol

register_wire_protocol(proto: WireProtocol) -> None

Register a codec under its name (the X-Stream-Protocol value).

Methods

WireProtocol.decode

decode(sse: AsyncIterator[bytes]) -> AsyncIterator[AgentEvent]

Client/proxy: parse the wire back into AgentEvents.

WireProtocol.decode_request

decode_request(body: bytes) -> tuple[list[Message], dict[str, Any]]

Server: parse a request body into (messages, config).

WireProtocol.encode

encode(events: AsyncIterator[AgentEvent]) -> AsyncIterator[bytes]

Server: encode the agent's event stream onto the wire.

WireProtocol.encode_request

encode_request(messages: list[Message], config: dict[str, Any]) -> bytes

Client/proxy: serialize a request body.

Attributes

WIRE_REVISION