Class NaruStreamPipeline

java.lang.Object
net.thevpc.naru.api.model.NaruStreamPipeline

public class NaruStreamPipeline extends Object
Turns a model's output into the one form everything downstream understands: an ordered list of NaruStreamChunks, and an assistant message whose reasoning is already separated from its answer.

Exists so that no consumer ever has to ask where reasoning came from. A provider that has a real reasoning channel, a model that inlines it in <think> tags, and a model that does neither all end up as the same chunk sequence, which is what makes the batch and streaming paths interchangeable.

Incrementally fed, so the same instance serves a streamed response and a batch one. batch(java.lang.String, java.lang.String, net.thevpc.naru.api.model.NaruStreamHandler, net.thevpc.naru.api.model.NaruThinkingTags) is the batch entry point and is exactly equivalent to feeding the whole body and finishing.

Not thread-safe; a stream belongs to one reader.

  • Constructor Details

  • Method Details

    • of

      public static NaruStreamPipeline of(String provider, NaruStreamHandler handler)
      Convenience using the protocol's default delimiters.
    • batch

      public static NaruStreamPipeline batch(String provider, String text, NaruStreamHandler handler, NaruThinkingTags tags)
      Consumes a whole batch body.
    • feed

      public NaruStreamPipeline feed(String text)
      Feeds the next piece of the answer body as it arrives.

      Reasoning already received as a native field should be passed here as well, via feedNativeThinking(java.lang.String); the two are kept apart internally so the order between them is preserved in the final message.

    • feedNativeThinking

      public NaruStreamPipeline feedNativeThinking(String text)
      Records reasoning that arrived in a provider-native field rather than inline in the answer.

      Delivered as its own chunks rather than appended to the answer, so the transcript can interleave reasoning and answer in the order the model produced them.

    • thinkingTokens

      public NaruStreamPipeline thinkingTokens(long tokens)
      Records the reasoning token count reported by the provider.

      Only stored, never estimated: a made-up count would quietly inflate budget numbers. May be called at any point, since some protocols report usage in a final chunk after the content.

    • finish

      public NaruMessage finish()
      Signals that no more content is coming, whether the stream ended normally or was interrupted.

      Safe to call on an interrupted stream, which is the point: whatever reasoning was in flight becomes a segment marked incomplete rather than being dropped.

      Returns:
      the assistant message, with reasoning already separated out
    • finish

      public NaruMessage finish(boolean interrupted)
      Parameters:
      interrupted - whether generation was cut short
    • message

      public NaruMessage message()
      The message built so far. Valid before finish() for rendering, but only complete afterwards.

      Side-effect free, so rendering code may hold on to it and ask again without the segment numbering drifting.

    • segments

      public List<NaruThinkingSegment> segments()
      Reasoning recovered from the answer text, one segment per contiguous run of thinking chunks.

      Chunks are merged back together because a stream splits a single thought into many chunks; persisting each fragment as its own segment would make one long thought read as dozens of unrelated ones.