AgentStreamListener

org.llm4s.javaapi.AgentStreamListener

Receives an agent turn's events from JAgent.stream, JAgent.streamResume or JAgent.streamRecover: each event in order through onEvent, then exactly one of onComplete (the turn's result) or onError (the error it failed with). Every call is made on the stream's own thread - never the caller's, and never the runtime's - one at a time.

Only onEvent is abstract, so a Java lambda is a listener:

LlmResult<AgentStream> started = agent.stream(threadId, "Explain monads", event ->
   StreamEvents.decode(AgentEvents.TextDelta(), event).ifPresent(d -> System.out.print(d.text())));
LlmResult<JAgentResult> result = started.get().await();

A listener slower than the stream never holds the turn up: live events (text deltas, tool progress) that do not fit in the stream's buffer are dropped, and the listener receives one StreamEvent.LiveGap with their count where they were dropped. Durable events are never dropped. A listener that throws from onEvent cancels the turn, and onError receives what it threw; one that sets its own thread's interrupt flag cancels it too, at once, and receives a CancelledError.

Attributes

Graph
Supertypes
class Object
trait Matchable
class Any

Members list

Value members

Abstract methods

def onEvent(event: StreamEvent): Unit

One event of the turn: a StreamEvent.Durable, a StreamEvent.Live, or a StreamEvent.LiveGap.

One event of the turn: a StreamEvent.Durable, a StreamEvent.Live, or a StreamEvent.LiveGap.

Attributes

Concrete methods

def onComplete(result: JAgentResult): Unit

The turn's result, after its last event. Called at most once, and never together with onError.

The turn's result, after its last event. Called at most once, and never together with onError.

Attributes

def onError(error: LlmException): Unit

The error the turn failed with - or, after AgentStream.cancel, the cancellation - after its last delivered event. Called at most once, and never together with onComplete.

The error the turn failed with - or, after AgentStream.cancel, the cancellation - after its last delivered event. Called at most once, and never together with onComplete.

Attributes