Class AguiEventEncoder

java.lang.Object
io.agentscope.core.agui.encoder.AguiEventEncoder

public class AguiEventEncoder extends Object
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 Details

    • AguiEventEncoder

      public AguiEventEncoder()
      Creates a new AguiEventEncoder.
  • Method Details

    • encode

      public String encode(AguiEvent event)
      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

      public String encodeToJson(AguiEvent event)
      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

      public String encodeComment(String comment)
      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

      public String keepAlive()
      Generate a keep-alive comment.
      Returns:
      An SSE keep-alive comment