AgentPekko

org.llm4s.pekko.AgentPekko
See theAgentPekko companion object
trait AgentPekko

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.

Attributes

Companion
object
Source
AgentPekko.scala
Graph
Supertypes
class Object
trait Matchable
class Any

Members list

Value members

Abstract methods

def abandon(threadId: ThreadId, reason: String, config: RunConfig)(using ec: ExecutionContext): Future[Unit]

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.

Attributes

Source
AgentPekko.scala
def continueConversation(previous: AgentResult, query: String, config: RunConfig)(using ec: ExecutionContext): Future[AgentResult]

The next turn on previous's thread; see Agent.continueConversation.

The next turn on previous's thread; see Agent.continueConversation.

Attributes

Source
AgentPekko.scala
def events[A](run: AgentRun, eventType: EventType[A]): Source[A, Future[Done]]

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.

Attributes

Source
AgentPekko.scala
def events[A](run: AgentRun, eventType: EventType[A], bufferSize: Int): Source[A, Future[Done]]

events of eventType with bufferSize live events buffered for a slow consumer.

events of eventType with bufferSize live events buffered for a slow consumer.

Attributes

Source
AgentPekko.scala
def events[R](run: AgentRun, handlers: EventHandlers[R], bufferSize: Int): Source[R, Future[Done]]

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.

Attributes

Source
AgentPekko.scala
def nestedEvents[R](run: AgentRun, handlers: EventHandlers[R], bufferSize: Int): Source[R, Future[Done]]

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.

Attributes

Source
AgentPekko.scala
def recover(threadId: ThreadId, config: RunConfig)(using ec: ExecutionContext): Future[AgentResult]

Continues threadId's failed or interrupted run; see Agent.recover.

Continues threadId's failed or interrupted run; see Agent.recover.

Attributes

Source
AgentPekko.scala
def resume(threadId: ThreadId, answers: Map[InterruptId, Value], config: RunConfig)(using ec: ExecutionContext): Future[AgentResult]

Answers pending approvals and questions and continues; see Agent.resume.

Answers pending approvals and questions and continues; see Agent.resume.

Attributes

Source
AgentPekko.scala
def run(query: String, config: RunConfig)(using ec: ExecutionContext): Future[AgentResult]

One turn on a new thread; see Agent.run.

One turn on a new thread; see Agent.run.

Attributes

Source
AgentPekko.scala
def stream(threadId: ThreadId, query: String, config: RunConfig, bufferSize: Int): Source[AgentStreamItem, Future[Done]]

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.

Attributes

Source
AgentPekko.scala
def streamRecover(threadId: ThreadId, config: RunConfig, bufferSize: Int): Source[AgentStreamItem, Future[Done]]

recover as a stream; see stream.

recover as a stream; see stream.

Attributes

Source
AgentPekko.scala
def streamResume(threadId: ThreadId, answers: Map[InterruptId, Value], config: RunConfig, bufferSize: Int): Source[AgentStreamItem, Future[Done]]

resume as a stream; see stream.

resume as a stream; see stream.

Attributes

Source
AgentPekko.scala