pub struct StreamAssembly { /* private fields */ }Expand description
Accumulates streamed chat events into a finished ChatResponse.
Why: see the module doc — this is the inverse of super::buffered_stream,
so a consumer can stream deltas for display AND still hand the rest of its
pipeline the exact same response type the non-streaming path produced. That
equivalence is what lets a caller adopt streaming without forking its
downstream transcript / usage / tool-dispatch code.
What: Self::push folds one event in; Self::text exposes the text
accumulated so far; Self::into_response finalises. Tool calls are keyed
by ToolCallDelta::index in a BTreeMap so the rebuilt order is the
provider’s slot order regardless of the order fragments arrived in.
Test: concatenates_text_deltas, accumulates_fragmented_tool_call,
carries_terminal_usage_and_finish_reason.
Implementations§
Source§impl StreamAssembly
impl StreamAssembly
Sourcepub fn new() -> StreamAssembly
pub fn new() -> StreamAssembly
Start an empty assembly.
Why: one obvious entry point; the Default impl is the same thing for
callers that build it in a struct literal.
What: all three accumulators empty.
Test: concatenates_text_deltas.
Sourcepub fn push(&mut self, event: ChatStreamEvent)
pub fn push(&mut self, event: ChatStreamEvent)
Fold one streamed event into the accumulator.
Why: the caller drives the poll loop (so it can await between events),
so the accumulator must accept events one at a time rather than owning
the stream.
What: ChatStreamEvent::Delta appends to the text;
ChatStreamEvent::ToolCall merges into the slot named by its index;
ChatStreamEvent::Done records the terminal summary. A second Done
(which a healthy stream never sends) overwrites the first rather than
panicking — a malformed stream must not be able to abort the caller.
Test: concatenates_text_deltas, accumulates_fragmented_tool_call.
Sourcepub fn text(&self) -> &str
pub fn text(&self) -> &str
The assistant text accumulated so far.
Why: a caller that already forwarded each delta to a UI still needs the
full turn text for its transcript, and reading it without consuming the
assembly lets it do so mid-stream (e.g. to render a running preview).
What: the concatenation of every ChatStreamEvent::Delta pushed.
Test: concatenates_text_deltas.
Sourcepub fn into_response(
self,
id: impl Into<String>,
model: impl Into<String>,
) -> ChatResponse
pub fn into_response( self, id: impl Into<String>, model: impl Into<String>, ) -> ChatResponse
Finalise into the ChatResponse the buffered path would have returned.
Why: downstream code (transcript recording, usage accrual, tool
dispatch) must not care whether the turn was streamed — handing it the
same type is what makes streaming a transport detail instead of a second
code path.
What: builds a single-choice response. content is None for a
text-free (tool-only) turn — matching the wire shape the non-streaming
parser produces — and Some(text) otherwise. Tool calls are emitted in
slot order; a fragment that never carried an id/name yields empty
strings rather than being dropped, so a malformed stream surfaces as a
visible bad call instead of a silently missing one. finish_reason and
usage come from the terminal ChatStreamEvent::Done; a stream that
ended without one yields None/zeroed usage.
Test: carries_terminal_usage_and_finish_reason, no_done_event_yields_defaults.
Trait Implementations§
Source§impl Debug for StreamAssembly
impl Debug for StreamAssembly
Source§impl Default for StreamAssembly
impl Default for StreamAssembly
Source§fn default() -> StreamAssembly
fn default() -> StreamAssembly
Auto Trait Implementations§
impl Freeze for StreamAssembly
impl RefUnwindSafe for StreamAssembly
impl Send for StreamAssembly
impl Sync for StreamAssembly
impl Unpin for StreamAssembly
impl UnsafeUnpin for StreamAssembly
impl UnwindSafe for StreamAssembly
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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more