Class NaruOpenApiStreamParser

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

public class NaruOpenApiStreamParser extends NaruSseResponseParser
Turns an OpenAI-compatible text/event-stream into chunks and, at the end, into the same NaruResponse the batch parser produces.

Three shapes arrive in the same choices[0].delta object and have to be told apart, because conflating them is how reasoning ends up printed to the user as the answer:

  • content -- the answer;
  • reasoning_content (DeepSeek, OpenAI o-series) or reasoning -- a dedicated reasoning field, which some providers interleave with the answer token by token;
  • tool_calls -- a structured call, whose arguments arrive as JSON string fragments that are only parseable once the call is complete.

Only content is scanned for thinking tags. A reasoning field is taken as reasoning on the provider's word, and putting it through a tag parser would let a model that quotes <think> inside its reasoning get its own reasoning truncated. Conversely the answer is scanned, because a model configured for tag-delimited reasoning puts the tags inside content -- skipping that would replay, as the answer, exactly the text the batch parser strips out. This is what keeps a streamed turn equal to a batched one.

Stateless with respect to the transport: feed it events in order, then call #finish(). It does not read the network itself, so the whole protocol is testable against a captured body.

  • Constructor Details

  • Method Details

    • onEvent

      public boolean onEvent(String name, String data)
      Consumes one SSE frame.
      Specified by:
      onEvent in class NaruSseResponseParser
      Parameters:
      data - the frame payload
      name - the event: field, or null for the default type
      Returns:
      false once the stream has signalled completion, so the caller can stop reading without waiting for the server to hang up
    • hasDeliveredContent

      public boolean hasDeliveredContent()
      Description copied from interface: NaruStreamResponseParser
      Whether any user-visible output has already been delivered.

      Consulted by the caller before retrying. A stream that has delivered nothing can be retried invisibly; one that has cannot, because the tokens cannot be recalled.

    • isCancelled

      public boolean isCancelled()
      Description copied from class: NaruSseResponseParser
      Polled between events, which is where a cancel lands.
      Overrides:
      isCancelled in class NaruSseResponseParser
    • finish

      public NaruResponse finish(boolean interrupted)
      Closes the stream and assembles the response.
      Specified by:
      finish in class NaruSseResponseParser
      Parameters:
      interrupted - true when the read stopped early -- a cancel, a dropped connection -- so a truncated reasoning segment is recorded as incomplete rather than silently treated as a finished thought