use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
use rig_core::completion::{FinishReason, Message};
use rig_core::error::ProviderError;
use rig_core::message::{AssistantContent, AssistantMessage, CallId, ToolCall, ToolName};
use rig_core::streaming::{Item, PartKind, StreamEvent};
use super::policy::TurnPolicy;
fn unknown_payload_loses_assistant_content(payload: &serde_json::Value) -> bool {
if AssistantContent::deserialize(payload).is_ok() {
return true;
}
payload
.get("text")
.is_some_and(serde_json::Value::is_string)
&& payload.get("native").is_some()
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StreamedInvalidToolCall {
pub tool_call: ToolCall,
pub args: Option<String>,
pub policy: TurnPolicy,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PartialStreamedTurn {
#[serde(default)]
pub head: AssistantMessage,
pub content: Vec<AssistantContent>,
pub pending_tool_calls: Vec<ToolCall>,
}
impl PartialStreamedTurn {
pub fn assistant_message(&self, current_tool_call: Option<ToolCall>) -> Option<Message> {
let mut calls: Vec<ToolCall> = self
.pending_tool_calls
.iter()
.cloned()
.chain(current_tool_call)
.collect();
let mut content: Vec<AssistantContent> = self
.content
.iter()
.filter_map(|part| match part {
AssistantContent::ToolCall(call) => {
let at = calls.iter().position(|kept| kept.id == call.id)?;
let mut kept = calls.remove(at);
kept.native.clone_from(&call.native);
Some(AssistantContent::ToolCall(kept))
}
part => (!part.is_blank()).then(|| part.clone()),
})
.collect();
content.extend(calls.into_iter().map(|mut call| {
call.native = None;
AssistantContent::ToolCall(call)
}));
if content.is_empty() {
return None;
}
Some(Message::Assistant(self.head.clone().with_content(content)))
}
pub fn rollback_messages(
&self,
invalid_tool_call: ToolCall,
feedback: String,
) -> Option<(Message, Message)> {
let assistant_message = self.assistant_message(Some(invalid_tool_call.clone()))?;
let Message::Assistant(turn) = &assistant_message else {
return None;
};
let user_message = Message::User {
content: rig_core::tool::feedback::invalid_call_feedback(
&turn.content,
&invalid_tool_call.id,
&feedback,
),
};
Some((assistant_message, user_message))
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StreamedTurn {
pub head: AssistantMessage,
pub choice: Vec<AssistantContent>,
pub policy: TurnPolicy,
pub finish_reason: Option<FinishReason>,
}
#[derive(Debug)]
pub enum StreamedResolution {
Repaired {
tool_name: String,
},
TurnAbandoned,
Ignored,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub enum StreamedTurnEvent {
EmitIngested,
EmitToolCallDelta,
HoldToolCall,
EmitToolCall {
call: ToolCall,
},
InvalidToolCall(StreamedInvalidToolCall),
}
#[derive(Clone, Serialize, Deserialize)]
struct OpenCall {
name: Option<ToolName>,
live: bool,
}
#[derive(Clone, Serialize, Deserialize)]
struct PendingInvalid {
tool_call: ToolCall,
}
#[derive(Clone, Serialize, Deserialize)]
pub struct StreamedTurnAssembler {
policy: TurnPolicy,
#[serde(default)]
open_calls: BTreeMap<usize, OpenCall>,
pending_tool_calls: Vec<ToolCall>,
pending_invalid: Option<PendingInvalid>,
ignored_calls: Vec<CallId>,
repaired_calls: Vec<(CallId, ToolName)>,
excluded_assistant_content: ExclusionCount,
}
#[derive(Default, Clone, Serialize, Deserialize)]
#[serde(transparent)]
struct ExclusionCount(usize);
impl Drop for ExclusionCount {
fn drop(&mut self) {
if self.0 > 0 {
tracing::warn!(
excluded = self.0,
"stream items matching rig's tagged assistant-content \
serialization were excluded from the assembled assistant \
message — replayed assistant blocks are not stream-item \
shapes, and their content is lost from assembled history"
);
}
}
}
impl StreamedTurnAssembler {
pub fn new(policy: TurnPolicy) -> Self {
Self {
policy,
open_calls: BTreeMap::new(),
pending_tool_calls: Vec::new(),
pending_invalid: None,
ignored_calls: Vec::new(),
repaired_calls: Vec::new(),
excluded_assistant_content: ExclusionCount::default(),
}
}
pub fn excluded_assistant_content(&self) -> usize {
self.excluded_assistant_content.0
}
pub fn streaming_tool_name(&self, index: usize) -> Option<&ToolName> {
self.open_calls
.get(&index)
.and_then(|call| call.name.as_ref())
}
pub fn ingest(&mut self, item: &Item<StreamEvent>) -> Result<StreamedTurnEvent, ProviderError> {
if self.pending_invalid.is_some() {
return Err(ProviderError::Response(
"streamed turn ingested while an invalid tool call awaits resolution".to_string(),
));
}
let event = match item {
Item::Event(event) => event,
Item::Unknown(payload) => {
if unknown_payload_loses_assistant_content(payload.value()) {
self.excluded_assistant_content.0 += 1;
tracing::debug!(
excluded = self.excluded_assistant_content.0,
"stream item is a replayed assistant block, not a \
stream-item shape; excluded from assembly"
);
}
return Ok(StreamedTurnEvent::EmitIngested);
}
};
match event {
StreamEvent::Text { .. } | StreamEvent::Reasoning { .. } => {
Ok(StreamedTurnEvent::EmitIngested)
}
StreamEvent::Start {
part,
kind: PartKind::ToolCall,
name,
} => {
let live = name
.as_ref()
.is_some_and(|name| self.policy.allows(name.as_str()));
let name = name.clone();
self.open_calls
.insert(part.index(), OpenCall { name, live });
Ok(if live {
StreamedTurnEvent::EmitIngested
} else {
StreamedTurnEvent::HoldToolCall
})
}
StreamEvent::Arguments { part, .. } => {
let live = self
.open_calls
.get(&part.index())
.is_some_and(|call| call.live);
Ok(if live {
StreamedTurnEvent::EmitToolCallDelta
} else {
StreamedTurnEvent::HoldToolCall
})
}
StreamEvent::End {
part,
content: AssistantContent::ToolCall(tool_call),
} => {
self.open_calls.remove(&part.index());
if !self.policy.allows(tool_call.function.name.as_str()) {
return Ok(self.surface_invalid_call(
tool_call.clone(),
Some(tool_call.function.arguments_json()),
));
}
self.pending_tool_calls.push(tool_call.clone());
Ok(StreamedTurnEvent::EmitToolCall {
call: tool_call.clone(),
})
}
StreamEvent::Start { .. } | StreamEvent::End { .. } => {
Ok(StreamedTurnEvent::EmitIngested)
}
}
}
pub fn resolve_pending_invalid(
&mut self,
resolution: &StreamedResolution,
) -> Option<StreamedTurnEvent> {
let pending = self.pending_invalid.take()?;
let PendingInvalid { mut tool_call } = pending;
match resolution {
StreamedResolution::Repaired { tool_name } => {
if let Ok(tool_name) = ToolName::new(tool_name.clone()) {
self.repaired_calls
.push((tool_call.id.clone(), tool_name.clone()));
tool_call.function.name = tool_name;
}
self.pending_tool_calls.push(tool_call.clone());
Some(StreamedTurnEvent::EmitToolCall { call: tool_call })
}
StreamedResolution::TurnAbandoned => None,
StreamedResolution::Ignored => {
self.ignored_calls.push(tool_call.id);
None
}
}
}
pub fn partial_turn(
&self,
partial: &rig_core::completion::CompletionResponse,
) -> PartialStreamedTurn {
PartialStreamedTurn {
head: partial.continued(Vec::new()),
content: partial.choice.clone(),
pending_tool_calls: self.pending_tool_calls.clone(),
}
}
pub fn finish(self, response: &rig_core::completion::CompletionResponse) -> StreamedTurn {
let choice = response
.choice
.iter()
.filter(|content| match content {
AssistantContent::ToolCall(call) => !self.ignored_calls.contains(&call.id),
_ => true,
})
.cloned()
.map(|content| match content {
AssistantContent::ToolCall(mut call) => {
if let Some((_, name)) =
self.repaired_calls.iter().find(|(id, _)| *id == call.id)
{
call.function.name = name.clone();
}
AssistantContent::ToolCall(call)
}
other => other,
})
.collect();
StreamedTurn {
head: response.head(),
choice,
policy: self.policy,
finish_reason: response.finish_reason(),
}
}
fn surface_invalid_call(
&mut self,
tool_call: ToolCall,
args: Option<String>,
) -> StreamedTurnEvent {
let invalid = StreamedInvalidToolCall {
tool_call: tool_call.clone(),
args,
policy: self.policy.clone(),
};
self.pending_invalid = Some(PendingInvalid { tool_call });
StreamedTurnEvent::InvalidToolCall(invalid)
}
}
#[cfg(test)]
mod tests;