Class SseFormatter

java.lang.Object
org.a2aproject.sdk.server.util.sse.SseFormatter

public class SseFormatter extends Object
Framework-agnostic utility for formatting A2A responses as Server-Sent Events (SSE).

Provides static methods to serialize A2A responses to JSON and format them as SSE events. This allows HTTP server frameworks (Vert.x, Jakarta/WildFly, etc.) to use their own reactive libraries for publisher mapping while sharing the serialization logic.

Example usage (Quarkus/Vert.x with Mutiny):

Flow.Publisher<A2AResponse<?>> responses = handler.onMessageSendStream(request, context);
AtomicLong eventId = new AtomicLong(0);

Multi<String> sseEvents = Multi.createFrom().publisher(responses)
    .map(response -> SseFormatter.formatResponseAsSSE(response, eventId.getAndIncrement()));

sseEvents.subscribe().with(sseEvent -> httpResponse.write(Buffer.buffer(sseEvent)));

Example usage (Jakarta/WildFly with custom reactive library):

Flow.Publisher<String> jsonStrings = restHandler.getJsonPublisher();
AtomicLong eventId = new AtomicLong(0);

Flow.Publisher<String> sseEvents = mapPublisher(jsonStrings,
    json -> SseFormatter.formatJsonAsSSE(json, eventId.getAndIncrement()));
  • Method Details

    • formatResponseAsSSE

      public static String formatResponseAsSSE(A2AResponse<?> response, long eventId)
      Format an A2A response as an SSE event.

      Serializes the response to JSON and formats as:

      data: {"jsonrpc":"2.0","result":{...},"id":123}
      id: 0
      
      
      Parameters:
      response - the A2A response to format
      eventId - the SSE event ID
      Returns:
      SSE-formatted string (ready to write to HTTP response)
    • formatJsonAsSSE

      public static String formatJsonAsSSE(String jsonString, long eventId)
      Format a pre-serialized JSON string as an SSE event.

      Wraps the JSON in SSE format as:

      data: {"jsonrpc":"2.0","result":{...},"id":123}
      id: 0
      
      

      Use this when you already have JSON strings (e.g., from REST transport) and just need to add SSE formatting.

      Parameters:
      jsonString - the JSON string to wrap
      eventId - the SSE event ID
      Returns:
      SSE-formatted string (ready to write to HTTP response)