Skip to main content

StreamAssembly

Struct StreamAssembly 

Source
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

Source

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.

Source

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.

Source

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.

Source

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

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for StreamAssembly

Source§

fn default() -> StreamAssembly

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<Unshared, Shared> IntoShared<Shared> for Unshared
where Shared: FromUnshared<Unshared>,

Source§

fn into_shared(self) -> Shared

Creates a shared type from an unshared type.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more