use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
use rig_core::completion::Message;
use rig_core::error::ProviderError;
use rig_core::message::{AssistantContent, AssistantMessage, ToolCall, ToolName};
use rig_core::streaming::{Item, PartKind, StreamEvent};
use rig_core::tool::feedback::invalid_call_feedback;
use super::policy::TurnPolicy;
fn unknown_payload_loses_assistant_content(payload: &serde_json::Value) -> bool {
if rig_core::json_utils::from_value::<AssistantContent>(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 (content, _) = self.content_with(current_tool_call);
if content.is_empty() {
return None;
}
Some(Message::Assistant(self.head.clone().with_content(content)))
}
fn content_with(
&self,
current_tool_call: Option<ToolCall>,
) -> (Vec<AssistantContent>, Option<usize>) {
let current_slot = self.pending_tool_calls.len();
let mut calls: Vec<(usize, ToolCall)> = self
.pending_tool_calls
.iter()
.cloned()
.chain(current_tool_call)
.enumerate()
.collect();
let mut content = Vec::new();
let mut current = None;
let mut push_call = |content: &mut Vec<AssistantContent>,
(slot, call): (usize, ToolCall)| {
if slot == current_slot {
current = Some(content.len());
}
content.push(AssistantContent::ToolCall(call));
};
for part in &self.content {
match part {
AssistantContent::ToolCall(call) => {
let Some(at) = calls.iter().position(|(_, kept)| kept.id == call.id) else {
continue;
};
let (slot, mut kept) = calls.remove(at);
kept.native.clone_from(&call.native);
push_call(&mut content, (slot, kept));
}
part if !part.is_blank() => content.push(part.clone()),
_ => {}
}
}
for (slot, mut call) in calls {
call.native = None;
push_call(&mut content, (slot, call));
}
(content, current)
}
pub fn rollback_messages(&self, invalid_tool_call: ToolCall, feedback: String) -> [Message; 2] {
let (content, invalid_at) = self.content_with(Some(invalid_tool_call));
let results = invalid_call_feedback(&content, invalid_at.unwrap_or(usize::MAX), &feedback);
let assistant_message = Message::Assistant(self.head.clone().with_content(content));
[assistant_message, Message::User { content: results }]
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StreamedTurn {
pub head: AssistantMessage,
pub choice: Vec<AssistantContent>,
pub policy: TurnPolicy,
pub cut_off: Vec<usize>,
}
#[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 EndedCall {
call: ToolCall,
ignored: bool,
}
#[derive(Clone, Serialize, Deserialize)]
pub struct StreamedTurnAssembler {
policy: TurnPolicy,
#[serde(default)]
open_calls: BTreeMap<usize, OpenCall>,
ended_calls: Vec<EndedCall>,
pending_invalid: Option<ToolCall>,
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(),
ended_calls: Vec::new(),
pending_invalid: None,
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()) {
let invalid = StreamedInvalidToolCall {
tool_call: tool_call.clone(),
args: Some(tool_call.function.arguments_json()),
policy: self.policy.clone(),
};
self.pending_invalid = Some(tool_call.clone());
return Ok(StreamedTurnEvent::InvalidToolCall(invalid));
}
self.ended_calls.push(EndedCall {
call: tool_call.clone(),
ignored: false,
});
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 mut tool_call = self.pending_invalid.take()?;
match resolution {
StreamedResolution::Repaired { tool_name } => {
if let Ok(tool_name) = ToolName::new(tool_name.clone()) {
tool_call.function.name = tool_name;
}
self.ended_calls.push(EndedCall {
call: tool_call.clone(),
ignored: false,
});
Some(StreamedTurnEvent::EmitToolCall { call: tool_call })
}
StreamedResolution::TurnAbandoned => None,
StreamedResolution::Ignored => {
self.ended_calls.push(EndedCall {
call: tool_call,
ignored: true,
});
None
}
}
}
pub fn keep_pending_cut_off(&mut self) -> Option<StreamedTurnEvent> {
let tool_call = self.pending_invalid.take()?;
self.ended_calls.push(EndedCall {
call: tool_call.clone(),
ignored: false,
});
Some(StreamedTurnEvent::EmitToolCall { call: tool_call })
}
pub fn partial_turn(
&self,
partial: &rig_core::completion::CompletionResponse,
) -> PartialStreamedTurn {
PartialStreamedTurn {
head: partial.continued(Vec::new()),
content: self.resolved(&partial.choice),
pending_tool_calls: self
.ended_calls
.iter()
.filter(|ended| !ended.ignored)
.map(|ended| ended.call.clone())
.collect(),
}
}
pub fn finish(self, response: &rig_core::completion::CompletionResponse) -> StreamedTurn {
let choice = self.resolved(&response.choice);
let cut_off = choice
.iter()
.enumerate()
.filter_map(|(at, content)| match content {
AssistantContent::ToolCall(call)
if response.cut_off_calls().any(|cut| cut.id == call.id) =>
{
Some(at)
}
_ => None,
})
.collect();
StreamedTurn {
head: response.head(),
choice,
policy: self.policy,
cut_off,
}
}
fn resolved(&self, choice: &[AssistantContent]) -> Vec<AssistantContent> {
let mut ended: Vec<&EndedCall> = self.ended_calls.iter().collect();
choice
.iter()
.filter_map(|content| {
let AssistantContent::ToolCall(call) = content else {
return Some(content.clone());
};
let Some(at) = ended.iter().position(|ended| ended.call.id == call.id) else {
return Some(content.clone());
};
let ended = ended.remove(at);
(!ended.ignored).then(|| {
let mut call = call.clone();
call.function.name.clone_from(&ended.call.function.name);
AssistantContent::ToolCall(call)
})
})
.collect()
}
}
#[cfg(test)]
mod tests;