Package io.agentscope.core.agui.encoder
Class AguiEventEncoder
java.lang.Object
io.agentscope.core.agui.encoder.AguiEventEncoder
Encoder for AG-UI events to Server-Sent Events (SSE) format.
This encoder serializes AG-UI events to the SSE wire format:
data: {"type":"EVENT_TYPE",...}\n\n
The encoder is thread-safe and can be shared across multiple requests.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionEncode an AG-UI event to SSE format.encodeComment(String comment) Encode a comment in SSE format.encodeToJson(AguiEvent event) Encode an AG-UI event to JSON string only (without SSE wrapper).Generate a keep-alive comment.
-
Constructor Details
-
AguiEventEncoder
public AguiEventEncoder()Creates a new AguiEventEncoder.
-
-
Method Details
-
encode
Encode an AG-UI event to SSE format.The output format is:
data: {"type":"EVENT_TYPE",...}\n\n- Parameters:
event- The event to encode- Returns:
- The SSE-formatted string
- Throws:
AguiException.EncodingException- if the event cannot be serialized
-
encodeToJson
Encode an AG-UI event to JSON string only (without SSE wrapper).Note: The returned string has a leading space to ensure compatibility with AG-UI client libraries that expect SSE format "data: {...}" (with space). Spring WebFlux generates "data:" prefix, so the leading space produces the expected "data: {...}" format.
- Parameters:
event- The event to encode- Returns:
- The JSON string with leading space for SSE compatibility
- Throws:
AguiException.EncodingException- if the event cannot be serialized
-
encodeComment
Encode a comment in SSE format.Comments can be used as keep-alive signals:
: comment text\n\n
- Parameters:
comment- The comment text- Returns:
- The SSE-formatted comment
-
keepAlive
Generate a keep-alive comment.- Returns:
- An SSE keep-alive comment
-