Class NaruOllamaNativeStreamParser

java.lang.Object
net.thevpc.naru.ext.models.ollama.NaruOllamaNativeStreamParser
All Implemented Interfaces:
NaruStreamResponseParser

public class NaruOllamaNativeStreamParser extends Object implements NaruStreamResponseParser
Reads Ollama's /api/chat stream.

Not SSE, and the difference matters: Ollama writes one bare JSON document per line with no data: prefix, no frame separator and no [DONE] sentinel. Termination is a done:true field on the last document. Reusing the SSE reader here would silently produce zero events rather than an error, which is the worst possible failure mode -- it looks like a model that has nothing to say.

Reasoning arrives in message.thinking for models that emit it, in its own field and interleaved with the answer, so it is fed as native thinking and kept out of the answer. Content is still scanned for thinking tags, because a model configured for inline reasoning puts the tags there instead -- the same rule the OpenAI-compatible path uses, and the reason a streamed Ollama turn equals a batched one.

Tool calls arrive whole rather than in fragments, so unlike the OpenAI-compatible protocol there is nothing to reassemble here.

  • Constructor Details

  • 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
    • onLine

      public boolean onLine(String line)
      Consumes one NDJSON document.
      Returns:
      false once the stream has ended
    • finish

      public NaruResponse finish(boolean interrupted)
    • 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.

      Specified by:
      hasDeliveredContent in interface NaruStreamResponseParser
    • isCancelled

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