Class NaruOpenApiStreamParser
- All Implemented Interfaces:
NaruStreamResponseParser
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) orreasoning-- 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 Summary
ConstructorsConstructorDescriptionNaruOpenApiStreamParser(String provider, NaruStreamHandler handler, NaruModelConfig model) -
Method Summary
Modifier and TypeMethodDescriptionfinish(boolean interrupted) Closes the stream and assembles the response.booleanWhether any user-visible output has already been delivered.booleanPolled between events, which is where a cancel lands.booleanConsumes one SSE frame.Methods inherited from class net.thevpc.naru.ext.models.stream.NaruSseResponseParser
read
-
Constructor Details
-
NaruOpenApiStreamParser
-
-
Method Details
-
onEvent
Consumes one SSE frame.- Specified by:
onEventin classNaruSseResponseParser- Parameters:
data- the frame payloadname- theevent:field, ornullfor the default type- Returns:
falseonce 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:NaruStreamResponseParserWhether 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:NaruSseResponseParserPolled between events, which is where a cancel lands.- Overrides:
isCancelledin classNaruSseResponseParser
-
finish
Closes the stream and assembles the response.- Specified by:
finishin classNaruSseResponseParser- Parameters:
interrupted-truewhen 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
-