Apache Pekko Streams wrapper for Agent, the Pekko counterpart of AgentIO (cats-effect, fs2) and AgentZ (ZIO).
stream, streamResume and streamRecover run a turn as a Source of its events (AgentStreamItem.Event), then its result (AgentStreamItem.Done). The turn is started when the stream is materialized, once per materialization. Cancelling the stream - or stopping it early with take - cancels the turn: its model call and tool calls are interrupted and the thread is left for recover. Each stream materializes a Future[Done] that completes once the turn has ended and its thread is free - after the stream completes, fails or is cancelled - so a recover sequenced after it is never refused as busy by the turn it cancelled. A consumer too slow for the stream's buffer never holds the run up: it loses live events (text deltas, tool progress) and receives one StreamEvent.LiveGap with their count where they were dropped; durable events are kept, and the run carries on. A consumer that stops draining altogether fails the stream (and so cancels the turn) once more than Agent.StreamCapacity durable events wait, as the runtime disconnects a listener that far behind.
A refused start (a blank query, a busy thread), a failed turn and a subscription that disconnects all fail the stream with an LLMException. A turn that commits no terminal event still ends the stream.
run, continueConversation, recover and resume return a Future, started and awaited on the ExecutionContext you pass (a blocking one: the wait blocks a thread). A Future cannot be cancelled; stream the turn when it has to be cancellable.
Intentionally a thin wrapper: build the Agent with its tools, middleware (guardrails), handoffs and options through Agent.builder, or LLMClientPekko.agent.
Closes threadId's failed or interrupted run without continuing it: each tool call left without a result gets reason, nothing runs again, and the thread is at rest for its next turn; see Agent.abandon. Fails as it refuses - GraphError.NothingToRecover, GraphError.PendingInterrupts, GraphError.ThreadBusy - with the thread unchanged. The Future fails with an LLMException carrying the refusal.
Closes threadId's failed or interrupted run without continuing it: each tool call left without a result gets reason, nothing runs again, and the thread is at rest for its next turn; see Agent.abandon. Fails as it refuses - GraphError.NothingToRecover, GraphError.PendingInterrupts, GraphError.ThreadBusy - with the thread unchanged. The Future fails with an LLMException carrying the refusal.
The events of run that eventType names - durable and live - decoded: events with EventHandlers.on(eventType)(identity); see the overload taking handlers.
The events of run that eventType names - durable and live - decoded: events with EventHandlers.on(eventType)(identity); see the overload taking handlers.
What handlers make of run's events: one element for each event a handler takes, in delivery order. It observes the run, through AgentRun.subscribe, once per materialization: it replays the run's durable events from its start and then follows it live, but misses the live events sent before the stream started - use stream to see every one. It ends after the run's terminal event, or once a run that ends without one has delivered its last event; the run's own outcome is not failed with - await the run for it. Cancelling the stream, or stopping it early, cancels its subscription only: the run carries on. The materialized Future[Done] completes once the subscription has been released, however the stream ended.
What handlers make of run's events: one element for each event a handler takes, in delivery order. It observes the run, through AgentRun.subscribe, once per materialization: it replays the run's durable events from its start and then follows it live, but misses the live events sent before the stream started - use stream to see every one. It ends after the run's terminal event, or once a run that ends without one has delivered its last event; the run's own outcome is not failed with - await the run for it. Cancelling the stream, or stopping it early, cancels its subscription only: the run carries on. The materialized Future[Done] completes once the subscription has been released, however the stream ended.
Handlers run in the stream, not on the subscription: a slow or throwing handler slows or fails the stream, never the run. bufferSize live events wait for a slow consumer before the rest are counted into a LiveGap, which a handler sees through otherwise; a value below 1 fails the stream at once. A subscription that disconnects, or a consumer that falls more than Agent.StreamCapacity durable events behind, fails the stream with an LLMException.
events, with the events of every child run delegates to (design 4.47): the subscription is made with nested = true, so handlers also see each StreamEvent.Nested - matched with EventHandlers.onChild, or otherwise - after the parent's SubagentEvents.TaskStarted for its child. Nested events are live: past bufferSize a slow consumer loses them as a LiveGap.
events, with the events of every child run delegates to (design 4.47): the subscription is made with nested = true, so handlers also see each StreamEvent.Nested - matched with EventHandlers.onChild, or otherwise - after the parent's SubagentEvents.TaskStarted for its child. Nested events are live: past bufferSize a slow consumer loses them as a LiveGap.
One turn on threadId, as a stream: every event of the turn (Agent.stream), then Done(result). bufferSize is how many live events wait for a slow consumer before the rest are counted into a LiveGap; a value below 1 fails the stream at once.
One turn on threadId, as a stream: every event of the turn (Agent.stream), then Done(result). bufferSize is how many live events wait for a slow consumer before the rest are counted into a LiveGap; a value below 1 fails the stream at once.
The materialized Future[Done] completes once the turn has ended and its thread is free for the next call - recover after a cancel, say - however the stream ended; it never fails. Cancelling the stream returns at once, while the turn is still being cancelled: wait for this Future before starting another turn on threadId. A provider that ignores the interrupt keeps it pending until its call returns.