Class StreamEmitter<S>
java.lang.Object
com.google.genkit.ai.agent.internal.StreamEmitter<S>
- Type Parameters:
S- the type of the custom session state object
Bridges Session state-change events to AgentStreamChunk emission for one invocation.
Custom-state changes become customPatch chunks (first-of-turn = whole-doc replace, then incremental diffs); artifact add/update become artifact chunks. Suppressed entirely while detached.
-
Constructor Summary
ConstructorsConstructorDescriptionStreamEmitter(Consumer<AgentStreamChunk> sink, com.fasterxml.jackson.databind.ObjectMapper mapper) Constructs a new StreamEmitter. -
Method Summary
Modifier and TypeMethodDescriptionvoidAttaches this emitter to a Session.voidCall at the start of each turn.voidsetSuppressed(boolean suppressed) Controls whether chunks are emitted.
-
Constructor Details
-
StreamEmitter
public StreamEmitter(Consumer<AgentStreamChunk> sink, com.fasterxml.jackson.databind.ObjectMapper mapper) Constructs a new StreamEmitter.- Parameters:
sink- receives AgentStreamChunk instances; must not be nullmapper- ObjectMapper for converting custom state S to JsonNode; must not be null
-
-
Method Details
-
attach
Attaches this emitter to a Session. Registers onCustomChanged and onArtifactChanged listeners that emit customPatch and artifact chunks respectively.Since
Session.setOnCustomChanged(Runnable)takes aRunnable, the current custom state is retrieved viasession.getCustom()inside the callback.- Parameters:
session- the session to attach to
-
beginTurn
public void beginTurn()Call at the start of each turn. The next customPatch emitted will be a whole-document replace rather than an incremental diff. -
setSuppressed
public void setSuppressed(boolean suppressed) Controls whether chunks are emitted. Whentrue, all custom-state and artifact changes are silently dropped. Use for detached (non-streaming) runs.- Parameters:
suppressed-trueto suppress emission;falseto re-enable
-