pub struct StreamAccumulator { /* private fields */ }Expand description
Rebuilds one Response from a stream of WireStreamEvents.
This is the receiving half of the proxy pattern: the client-side equivalent
of RequestBuilder::stream_accumulated,
operating on events that arrived over a network rather than on a live
provider connection.
Unlike stream_accumulated, this does reassemble tool calls: fragments
from ToolCallStart /
ToolCallDelta /
ToolCallEnd are concatenated and attached
to the assistant message.
§Truncation is an error
finish fails unless the stream was
well-formed: it must have opened with
MessageStart and ended with a terminal
event. A stream that just stops — the client’s socket died mid-generation —
yields WireErrorKind::Stream, which is what distinguishes “the network
died” from “the provider refused” (the latter arrives as
WireStreamEvent::Error and is returned as its own WireError).
§Examples
use rai_sdk::ProviderKind;
use rai_sdk::wire::{StreamAccumulator, WireStreamEvent};
let mut accumulator = StreamAccumulator::new();
accumulator.push(WireStreamEvent::message_start("gpt-4o-mini", ProviderKind::OpenAI))?;
accumulator.push(WireStreamEvent::TextDelta { text: "done".into() })?;
accumulator.push(WireStreamEvent::MessageStop { finish_reason: Some("stop".into()) })?;
let response = accumulator.finish()?;
assert_eq!(response.text(), "done");
assert_eq!(response.finish_reason.as_deref(), Some("stop"));Implementations§
Source§impl StreamAccumulator
impl StreamAccumulator
Sourcepub fn protocol_version(&self) -> Option<u32>
pub fn protocol_version(&self) -> Option<u32>
The protocol version the sender advertised, once
MessageStart has been seen.
Sourcepub fn is_complete(&self) -> bool
pub fn is_complete(&self) -> bool
Whether a terminal event has been seen.
A stream whose events are exhausted while this is false was truncated.
Sourcepub fn push(&mut self, event: WireStreamEvent) -> Result<(), WireError>
pub fn push(&mut self, event: WireStreamEvent) -> Result<(), WireError>
Absorb one event.
§Errors
Returns the carried WireError when event is
WireStreamEvent::Error, so a caller driving this in a loop can stop
with ?. The error is remembered, so a later
finish reports it too.
Sourcepub fn finish(self) -> Result<Response, WireError>
pub fn finish(self) -> Result<Response, WireError>
Assemble everything pushed so far into one Response.
§Errors
- The
WireErrorfrom aWireStreamEvent::Error, if one arrived. WireErrorKind::Streamif noMessageStartwas seen, since the model and provider aResponserequires are unknown.WireErrorKind::Streamif no terminal event was seen, meaning the stream was cut short rather than finished.
Sourcepub async fn accumulate<S>(stream: S) -> Result<Response, WireError>where
S: Stream<Item = WireStreamEvent>,
pub async fn accumulate<S>(stream: S) -> Result<Response, WireError>where
S: Stream<Item = WireStreamEvent>,
Drain a stream of wire events into one Response.
The convenience form of new() + push() in a loop + finish(), for
the common client-side case where the whole stream is consumed at once.
§Errors
§Examples
use rai_sdk::ProviderKind;
use rai_sdk::wire::{StreamAccumulator, WireStreamEvent};
let events = futures::stream::iter(vec![
WireStreamEvent::message_start("claude-sonnet-4-6", ProviderKind::Anthropic),
WireStreamEvent::TextDelta { text: "hi".into() },
WireStreamEvent::MessageStop { finish_reason: Some("stop".into()) },
]);
let response = StreamAccumulator::accumulate(events).await?;
assert_eq!(response.text(), "hi");Trait Implementations§
Source§impl Clone for StreamAccumulator
impl Clone for StreamAccumulator
Source§fn clone(&self) -> StreamAccumulator
fn clone(&self) -> StreamAccumulator
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more