pub struct SseStreamDecoder {
buf: String,
done_sent: bool,
}Expand description
Stateful decoder that turns a sequence of raw SSE byte chunks into a
sequence of LlmStreamEvent values.
§Usage
ⓘ
let mut decoder = SseStreamDecoder::default();
while let Some(chunk) = byte_stream.next().await { … }
let (events, stop) = decoder.feed_bytes(&chunk);
for event in events { … }
if stop { break; }
}
if let Some(fallback) = decoder.finish() { … }Fields§
§buf: String§done_sent: boolSet to true once a LlmStreamEvent::Done has been yielded, so the
[DONE] sentinel doesn’t generate a duplicate.
Implementations§
Source§impl SseStreamDecoder
impl SseStreamDecoder
Sourcepub fn feed_bytes(
&mut self,
bytes: &[u8],
) -> (Vec<Result<LlmStreamEvent>>, bool)
pub fn feed_bytes( &mut self, bytes: &[u8], ) -> (Vec<Result<LlmStreamEvent>>, bool)
Feed one raw byte chunk into the decoder.
Returns (events, should_stop).
events— zero or more parsedLlmStreamEventvalues (or stream errors) extracted from the bytes fed so far.should_stop—truewhen the SSE stream has reached its natural end (a[DONE]sentinel or an unrecoverable parse error). The caller must not feed any further chunks once this flag istrue.
Sourcepub fn finish(self) -> Option<LlmStreamEvent>
pub fn finish(self) -> Option<LlmStreamEvent>
Emit a fallback Done event if the byte stream ended without one.
Call this once after the upstream byte stream is fully exhausted.
Returns None if a Done was already yielded by [feed_bytes].
Trait Implementations§
Source§impl Default for SseStreamDecoder
impl Default for SseStreamDecoder
Source§fn default() -> SseStreamDecoder
fn default() -> SseStreamDecoder
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for SseStreamDecoder
impl RefUnwindSafe for SseStreamDecoder
impl Send for SseStreamDecoder
impl Sync for SseStreamDecoder
impl Unpin for SseStreamDecoder
impl UnwindSafe for SseStreamDecoder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more