Class NaruSseReader

java.lang.Object
net.thevpc.naru.ext.models.stream.NaruSseReader
All Implemented Interfaces:
Closeable, AutoCloseable

public class NaruSseReader extends Object implements Closeable
Pull-based reader for text/event-stream bodies.

Exists because the wire format is not line-delimited JSON and assuming it is gets it wrong in ways that only show up against a real server:

  • a frame's payload can be split across several data: lines, which must be rejoined with a newline rather than concatenated;
  • a comment line beginning with : is a keep-alive, not an event, and a long silent generation is mostly keep-alives;
  • a server that closes the connection right after the last frame has no trailing blank line, so a pending frame has to be dispatched at end-of-input or the final token is lost.

It reads on demand from the underlying Reader rather than pre-splitting, which is what makes it a real stream: a frame is handed back as soon as its terminating blank line arrives, and nothing waits for the response to end.

Not thread-safe, and not meant to be: a stream has one reader, on the thread that made the call.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final record 
    One dispatched server-sent event.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The end-of-stream sentinel every OpenAI-compatible server sends, as a bare data payload.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
     
    of(String sseBody)
    Convenience for tests and for replaying a captured body.
    The next event, blocking until one is complete.
    void
    Consumes the whole remaining stream, which is what a caller that only wants the assembled response does.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DONE

      public static final String DONE
      The end-of-stream sentinel every OpenAI-compatible server sends, as a bare data payload. Not a JSON document, so it can never arrive as one.
      See Also:
  • Constructor Details

    • NaruSseReader

      public NaruSseReader(Reader reader)
  • Method Details