Class NaruStreamCollector

java.lang.Object
net.thevpc.naru.api.model.NaruStreamCollector
All Implemented Interfaces:
NaruStreamHandler

public class NaruStreamCollector extends Object implements NaruStreamHandler
A NaruStreamHandler that keeps what it is given.

Two jobs. It is how the model call reassembles the full answer while the renderer is still being fed fragment by fragment, and it is how a non-streaming provider is turned into a chunk sequence at all -- the whole response is classified and pushed through this, so downstream sees the same list it would have seen from a real stream.

  • Constructor Details

  • Method Details

    • addDelegate

      public NaruStreamCollector addDelegate(NaruStreamHandler delegate)
      Adds a handler to also feed, so rendering can happen while collection happens without either knowing about the other.
    • onChunk

      public void onChunk(NaruStreamChunk chunk)
      Description copied from interface: NaruStreamHandler
      Called once per chunk, in NaruStreamChunk.index() order.

      Called on the transport thread, so an implementation that touches the terminal or the session must not block and must be safe to call from another thread than the one that will read the final response.

      Specified by:
      onChunk in interface NaruStreamHandler
    • isCancelled

      public boolean isCancelled()
      Description copied from interface: NaruStreamHandler
      Whether the reader should stop emitting.

      Polled once per chunk, which is what makes interruption work during a long generation: a cancel lands between two chunks and the reader bails out instead of running to completion. This is the cooperative mechanism -- the transport cannot be aborted mid-socket, so this is the only place a stop can be observed.

      Specified by:
      isCancelled in interface NaruStreamHandler
      Returns:
      true to stop emitting; remaining chunks are not delivered
    • chunks

      public List<NaruStreamChunk> chunks()
    • size

      public int size()
    • text

      public String text(NaruChunkKind kind)
      All text of the given kind, concatenated. This is the reassembly step: a streamed answer is many chunks and only their concatenation is the answer.
    • has

      public boolean has(NaruChunkKind kind)
    • isTruncated

      public boolean isTruncated(NaruChunkKind kind)
      Whether the last chunk of the given kind was cut short, which is how a truncated thinking segment is recognised after the fact.
    • clear

      public void clear()