Class NaruBufferedStreamWriter

java.lang.Object
net.thevpc.naru.impl.interaction.NaruBufferedStreamWriter

public class NaruBufferedStreamWriter extends Object
Holds streamed fragments until the stream ends, then reports them as one message.

For an interaction with nowhere to draw incrementally -- a headless session forwarding to an SSE listener, a logger, a test recorder -- a fragment is not a meaningful unit of output. Emitting each token as its own event would give a host hundreds of messages it has to reassemble, and a half-rendered markdown line is worse than none. So fragments are joined per mode and reported once, which is exactly what the batched path used to do.

Buffers are per mode because a single model turn interleaves reasoning and answer: joined into one buffer, a thinking model's output would arrive as a single message with the reasoning glued to the front of the reply.

  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    net.thevpc.nuts.text.NMsg
    append(NaruLogMode mode, net.thevpc.nuts.text.NMsg fragment, boolean end)
    Adds a fragment and returns the joined message when that mode has just ended, or null while it is still accumulating.
    List<Map.Entry<NaruLogMode,net.thevpc.nuts.text.NMsg>>
    Returns anything still open, for a session that ends mid-stream: without this a cancelled turn would throw away reasoning the model already paid to produce.
    void
    Reports anything still open through target, for an interaction that is still willing to write.
    void
    write(NaruInteraction target, NaruLogMode mode, net.thevpc.nuts.text.NMsg fragment, boolean end)
     

    Methods inherited from class java.lang.Object

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

    • NaruBufferedStreamWriter

      public NaruBufferedStreamWriter()
  • Method Details

    • write

      public void write(NaruInteraction target, NaruLogMode mode, net.thevpc.nuts.text.NMsg fragment, boolean end)
      Parameters:
      target - where the finished message goes
      mode - the mode this fragment belongs to
      end - whether this is the last fragment for that mode
    • append

      public net.thevpc.nuts.text.NMsg append(NaruLogMode mode, net.thevpc.nuts.text.NMsg fragment, boolean end)
      Adds a fragment and returns the joined message when that mode has just ended, or null while it is still accumulating. The caller decides how the finished message is emitted, which is what lets a closing interaction report it after its own closed flag has already shut the normal path.
    • drain

      public List<Map.Entry<NaruLogMode,net.thevpc.nuts.text.NMsg>> drain()
      Returns anything still open, for a session that ends mid-stream: without this a cancelled turn would throw away reasoning the model already paid to produce.
    • flushAll

      public void flushAll(NaruInteraction target)
      Reports anything still open through target, for an interaction that is still willing to write.