Package com.axelor.ai.chat.stream
Class StreamWriter
java.lang.Object
com.axelor.ai.chat.stream.StreamWriter
- All Implemented Interfaces:
AutoCloseable
- Direct Known Subclasses:
BufferStreamWriter,CollectingStreamWriter
Writes
StreamEvents as SSE to an OutputStream. One instance per response.-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedProtected no-arg constructor for decorator subclasses that override all write methods.StreamWriter(OutputStream outputStream, com.fasterxml.jackson.databind.ObjectMapper objectMapper) -
Method Summary
Modifier and TypeMethodDescriptionvoidclearNotify(String key) Dismisses a previously emitted notification by key.voidclose()voiddone()Writes thedata: [DONE]\n\nterminator and flushes.voidEmits a transient notification to the frontend.voidEmits a transient notification that auto-dismisses after the specified duration.voidwrite(StreamEvent event) Serializes the event asdata: {json}\n\nand flushes.
-
Field Details
-
HEADER_NAME
HTTP response header name that identifies the stream protocol.- See Also:
-
HEADER_VALUE
HTTP response header value — protocol version.- See Also:
-
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. LeaveswriterandobjectMappernull — subclasses MUST overridewrite(StreamEvent),done(), andclose(). -
StreamWriter
public StreamWriter(OutputStream outputStream, com.fasterxml.jackson.databind.ObjectMapper objectMapper) - Parameters:
outputStream- the raw HTTP response streamobjectMapper- mapper used to serializeStreamEvents to JSON
-
-
Method Details
-
write
Serializes the event asdata: {json}\n\nand flushes.- Throws:
IOException
-
done
Writes thedata: [DONE]\n\nterminator and flushes. Must be the last write.- Throws:
IOException
-
notify
Emits a transient notification to the frontend. The notification is identified bykeyand displayed with the givenmessage. 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
Emits a transient notification that auto-dismisses after the specified duration.- Parameters:
key- unique identifier for this notificationmessage- display textclearAfterMs- auto-dismiss after this many milliseconds
-
clearNotify
Dismisses a previously emitted notification by key.- Parameters:
key- the notification key to dismiss
-
close
- Specified by:
closein interfaceAutoCloseable- Throws:
IOException
-