Class StreamWriter

java.lang.Object
com.axelor.ai.chat.stream.StreamWriter
All Implemented Interfaces:
AutoCloseable
Direct Known Subclasses:
BufferStreamWriter, CollectingStreamWriter

public class StreamWriter extends Object implements AutoCloseable
Writes StreamEvents as SSE to an OutputStream. One instance per response.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    HTTP response header name that identifies the stream protocol.
    static final String
    HTTP response header value — protocol version.
    static final String
    Notification key for "the agentic loop ran out of steps".
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Protected no-arg constructor for decorator subclasses that override all write methods.
     
    StreamWriter(OutputStream outputStream, com.fasterxml.jackson.databind.ObjectMapper objectMapper)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Dismisses a previously emitted notification by key.
    void
     
    void
    Writes the data: [DONE]\n\n terminator and flushes.
    void
    notify(String key, String message)
    Emits a transient notification to the frontend.
    void
    notify(String key, String message, int clearAfterMs)
    Emits a transient notification that auto-dismisses after the specified duration.
    void
    Serializes the event as data: {json}\n\n and flushes.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • HEADER_NAME

      public static final String HEADER_NAME
      HTTP response header name that identifies the stream protocol.
      See Also:
    • HEADER_VALUE

      public static final String HEADER_VALUE
      HTTP response header value — protocol version.
      See Also:
    • NOTIFY_MAX_STEPS

      public static final String NOTIFY_MAX_STEPS
      Notification key for "the agentic loop ran out of steps". Declared here because emitter ( ChatStreamerImpl) and consumer (CollectingStreamWriter) must agree on the wire literal: a drifted rename would silently hand programmatic callers a truncated fragment dressed as an answer.
      See Also:
  • Constructor Details

    • StreamWriter

      protected StreamWriter()
      Protected no-arg constructor for decorator subclasses that override all write methods. Leaves writer and objectMapper null — subclasses MUST override write(StreamEvent), done(), and close().
    • StreamWriter

      public StreamWriter(OutputStream outputStream, com.fasterxml.jackson.databind.ObjectMapper objectMapper)
      Parameters:
      outputStream - the raw HTTP response stream
      objectMapper - mapper used to serialize StreamEvents to JSON
  • Method Details

    • write

      public void write(StreamEvent event) throws IOException
      Serializes the event as data: {json}\n\n and flushes.
      Throws:
      IOException
    • done

      public void done() throws IOException
      Writes the data: [DONE]\n\n terminator and flushes. Must be the last write.
      Throws:
      IOException
    • notify

      public void notify(String key, String message)
      Emits a transient notification to the frontend. The notification is identified by key and displayed with the given message. Sending a new notification with the same key replaces the previous one.
      Parameters:
      key - unique identifier for this notification (used to replace/dismiss)
      message - display text (should be i18n'd by caller)
    • notify

      public void notify(String key, String message, int clearAfterMs)
      Emits a transient notification that auto-dismisses after the specified duration.
      Parameters:
      key - unique identifier for this notification
      message - display text
      clearAfterMs - auto-dismiss after this many milliseconds
    • clearNotify

      public void clearNotify(String key)
      Dismisses a previously emitted notification by key.
      Parameters:
      key - the notification key to dismiss
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Throws:
      IOException