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 elementsBlockKey(usize)is a newtype - prevents mixing up different indices- Methods return
Result<(), StreamAssemblyError>for caller-decided policy ToolCallBufferdoes NOT storeid- it’s the map key, avoiding duplicationBox<RawValue>for args - no parsing in adapter
Implementations§
Source§impl BlockAssembler
impl BlockAssembler
Sourcepub fn new() -> BlockAssembler
pub fn new() -> BlockAssembler
Create a new empty assembler.
Sourcepub fn on_text_delta(&mut self, delta: &str, meta: Option<Box<ProviderMeta>>)
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.
Sourcepub fn on_reasoning_start(&mut self)
pub fn on_reasoning_start(&mut self)
Start a new reasoning block.
Sourcepub fn on_reasoning_delta(
&mut self,
delta: &str,
) -> Result<(), StreamAssemblyError>
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.
Sourcepub fn on_reasoning_complete(&mut self, meta: Option<Box<ProviderMeta>>)
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.
Sourcepub fn current_reasoning_text(&self) -> String
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.
Sourcepub fn on_tool_call_start(
&mut self,
id: String,
) -> Result<(), StreamAssemblyError>
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.
Sourcepub fn on_tool_call_delta(
&mut self,
id: &str,
name: Option<&str>,
args_delta: &str,
) -> Result<(), StreamAssemblyError>
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.
Sourcepub fn finalize_tool_args(
&self,
id: &str,
) -> Result<Box<RawValue>, StreamAssemblyError>
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
UnknownToolFinalizeif no buffer exists for this ID (protocol error)InvalidArgsJsonif the buffered JSON is malformed
Sourcepub fn on_tool_call_complete(
&mut self,
id: String,
name: String,
args: Box<RawValue>,
meta: Option<Box<ProviderMeta>>,
) -> Result<(), StreamAssemblyError>
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).
Sourcepub fn finalize(self) -> Vec<AssistantBlock>
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.