Skip to content

latent.adapters

Framework adapters — make a foreign agent framework satisfy AgentProtocol.

An adapter author implements only the per-turn event mapping (:meth:FrameworkAdapter.iter_events, framework events → :class:AgentEvents); the base owns the StepBoundary discipline (ADR-008 D2): one boundary per iteration, monotonic indices, and invoke() derived from stream() via the shared :func:~latent.agents.invoke.segment_stream.

Adapters target the AgentEvent / AgentProtocol layer, never a wire codec — once an adapter satisfies AgentProtocol it serves over any registered WireProtocol (ai-sdk, ag-ui, …) for free, and terminality is derived consumer-side (ADR-009 D5), so adapters never stamp a terminal flag.

Classes

FrameworkAdapter

FrameworkAdapter()

Base class adapting a foreign framework's stream to AgentProtocol.

Subclass and implement :meth:iter_events. The base emits a leading StepBoundary and renumbers any boundary the subclass yields to delimit further iterations, so the canonical "exactly one StepBoundary per iteration, before that iteration's other events" contract holds without the author tracking indices.

Methods

FrameworkAdapter.invoke

invoke(messages: list[Message], stream_wrapper: StreamWrapper | None = None, config: dict[str, Any] | None = None) -> InvokeResult

FrameworkAdapter.iter_events

iter_events(messages: list[Message], config: dict[str, Any] | None = None) -> AsyncIterator[AgentEvent]

Yield AgentEvents for one turn — the only method a subclass writes.

Declared async (an async generator) so the contract is explicit: a subclass overrides it as an async generator, and stream consumes it with async for. Yield content events (TextDelta, ToolCall, …) in emission order; yield a bare StepBoundary to start a new agent-loop iteration (its step is renumbered by :meth:stream, label kept).

FrameworkAdapter.ready

ready() -> bool

Readiness probe for serving — see latent.serve.ServingProtocol.

Default: ready once constructed, exactly like :class:~latent.agents.base.BaseAgent. Override to gate registration on the wrapped framework's credentials/backends being reachable.

FrameworkAdapter.stream

stream(messages: list[Message], config: dict[str, Any] | None = None) -> AsyncIterator[AgentEvent]