Skip to main content

BlockAssembler

Struct BlockAssembler 

Source
pub struct BlockAssembler { /* private fields */ }
Expand description

Assembler for building ordered blocks from streaming events.

The assembler tracks global arrival order across all block types, not just tool calls. Blocks are ordered by when they started, not when they completed.

§Design

  • Vec<BlockSlot> provides stable indices because we never remove elements
  • BlockKey(usize) is a newtype - prevents mixing up different indices
  • Methods return Result<(), StreamAssemblyError> for caller-decided policy
  • ToolCallBuffer does NOT store id - it’s the map key, avoiding duplication
  • Box<RawValue> for args - no parsing in adapter

Implementations§

Source§

impl BlockAssembler

Source

pub fn new() -> BlockAssembler

Create a new empty assembler.

Source

pub fn on_text_delta(&mut self, delta: &str, meta: Option<Box<ProviderMeta>>)

Handle a text delta event.

Text deltas can always succeed - no Result needed. meta is used by Gemini for thoughtSignature on text parts.

Source

pub fn on_reasoning_start(&mut self)

Start a new reasoning block.

Source

pub fn on_reasoning_delta( &mut self, delta: &str, ) -> Result<(), StreamAssemblyError>

Handle a reasoning delta event.

§Errors

Returns OrphanedReasoningDelta if no reasoning block is currently being assembled.

Source

pub fn on_reasoning_complete(&mut self, meta: Option<Box<ProviderMeta>>)

Complete the current reasoning block.

Provider adapter converts raw JSON to typed ProviderMeta before calling.

Source

pub fn current_reasoning_text(&self) -> String

Return a snapshot of the current reasoning buffer text. Returns an empty string if no reasoning block is in progress.

Source

pub fn on_tool_call_start( &mut self, id: String, ) -> Result<(), StreamAssemblyError>

Start a new tool call block.

§Errors

Returns DuplicateToolStart if a tool call with the same ID is already being assembled.

Source

pub fn on_tool_call_delta( &mut self, id: &str, name: Option<&str>, args_delta: &str, ) -> Result<(), StreamAssemblyError>

Handle a tool call delta event.

§Errors

Returns OrphanedToolDelta if no tool call with the given ID is being assembled.

Source

pub fn finalize_tool_args( &self, id: &str, ) -> Result<Box<RawValue>, StreamAssemblyError>

Convert buffered args_json to RawValue. Called before on_tool_call_complete.

§Errors
  • UnknownToolFinalize if no buffer exists for this ID (protocol error)
  • InvalidArgsJson if the buffered JSON is malformed
Source

pub fn on_tool_call_complete( &mut self, id: String, name: String, args: Box<RawValue>, meta: Option<Box<ProviderMeta>>, ) -> Result<(), StreamAssemblyError>

Complete a tool call block.

Provider adapter converts raw JSON to typed ProviderMeta before calling.

§Errors

This method never returns an error. If no prior start exists for the ID, the tool call is inserted at the end (ordering may be off but we have the data).

Source

pub fn finalize(self) -> Vec<AssistantBlock>

Finalize the assembler and return the ordered blocks.

Slab iteration is in insertion order, so blocks are returned in the order they were started.

Trait Implementations§

Source§

impl Default for BlockAssembler

Source§

fn default() -> BlockAssembler

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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: 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: 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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