Class NaruSseResponseParser

java.lang.Object
net.thevpc.naru.ext.models.stream.NaruSseResponseParser
All Implemented Interfaces:
NaruStreamResponseParser
Direct Known Subclasses:
NaruOpenApiStreamParser

public abstract class NaruSseResponseParser extends Object implements NaruStreamResponseParser
Base for protocols whose stream arrives as text/event-stream.

Handles the transport-shaped work that is identical for every SSE protocol -- opening the body, splitting frames, stopping on a cancel -- and leaves the protocol-specific part, onEvent(java.lang.String, java.lang.String), to the subclass. Anthropic's stream and OpenAI's differ in payload shape but not in framing, and framing is where the subtle bugs live.

A read that stops early is reported as interrupted rather than as a successful end of stream, so a cancel or a dropped connection cannot be mistaken for a model that finished its thought.

  • Constructor Details

    • NaruSseResponseParser

      public NaruSseResponseParser()
  • Method Details

    • read

      public NaruResponse read(net.thevpc.nuts.net.NHttpResponse response) throws IOException
      Description copied from interface: NaruStreamResponseParser
      Consumes the body incrementally and returns the assembled response.

      Must deliver events as they arrive rather than reading the whole body first: the entire point is that a renderer sees text while the model is still generating it.

      Specified by:
      read in interface NaruStreamResponseParser
      Throws:
      IOException
    • onEvent

      public abstract boolean onEvent(String name, String data)
      Consumes one event payload.
      Parameters:
      name - the event: field, or null for the default type
      data - the data: lines, already rejoined
      Returns:
      false to stop reading, which is how a protocol reports a clean end of stream
    • finish

      public abstract NaruResponse finish(boolean interrupted)
      Assembles the response once reading has stopped.
      Parameters:
      interrupted - true when reading stopped before the protocol's own end-of-stream marker
    • isCancelled

      public boolean isCancelled()
      Polled between events, which is where a cancel lands.