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

public final class StreamEmitter<S> extends 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 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 null
      mapper - ObjectMapper for converting custom state S to JsonNode; must not be null
  • Method Details

    • attach

      public void attach(Session<S> session)
      Attaches this emitter to a Session. Registers onCustomChanged and onArtifactChanged listeners that emit customPatch and artifact chunks respectively.

      Since Session.setOnCustomChanged(Runnable) takes a Runnable, the current custom state is retrieved via session.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. When true, all custom-state and artifact changes are silently dropped. Use for detached (non-streaming) runs.
      Parameters:
      suppressed - true to suppress emission; false to re-enable