use std::collections::{BTreeMap, BTreeSet};
use serde::{Deserialize, Serialize};
use rig_core::completion::{FinishReason, Message};
use rig_core::error::ProviderError;
use rig_core::json_utils;
use rig_core::message::{
AssistantContent, CallId, Reasoning, Sealed, ToolCall, ToolFunction, ToolName, ToolResult,
};
use rig_core::streaming::{Item, PartKind, StreamEvent};
use super::policy::InvalidToolCallReason;
use super::transcript::{TOOL_NOT_EXECUTED_DUE_TO_INVALID_PEER, tool_result_message};
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("additional_params").is_some()
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StreamedInvalidToolCall {
pub tool_call: ToolCall,
pub args: Option<String>,
pub executable_tool_names: BTreeSet<String>,
pub allowed_tool_names: BTreeSet<String>,
pub reason: InvalidToolCallReason,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PartialStreamedTurn {
pub message_id: Option<String>,
pub text: Option<String>,
pub reasoning: Vec<Sealed<Reasoning>>,
pub pending_tool_calls: Vec<ToolCall>,
}
impl PartialStreamedTurn {
pub fn assistant_message(&self, current_tool_call: Option<ToolCall>) -> Option<Message> {
let content = self
.reasoning
.iter()
.cloned()
.map(AssistantContent::Reasoning)
.chain(
self.text
.as_ref()
.filter(|text| !text.is_empty())
.map(|text| AssistantContent::text(text.clone())),
)
.chain(
self.pending_tool_calls
.iter()
.cloned()
.chain(current_tool_call)
.map(AssistantContent::ToolCall),
)
.collect::<Vec<_>>();
if content.is_empty() {
return None;
}
Some(Message::Assistant {
id: self.message_id.clone(),
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 mut retry_results = self
.pending_tool_calls
.iter()
.map(|tool_call| {
tool_result_message(
tool_call.id.clone(),
tool_call.function.name.clone(),
TOOL_NOT_EXECUTED_DUE_TO_INVALID_PEER.to_string(),
)
})
.collect::<Vec<_>>();
retry_results.push(tool_result_message(
invalid_tool_call.id,
invalid_tool_call.function.name,
feedback,
));
let user_message = Message::User {
content: retry_results,
};
Some((assistant_message, user_message))
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StreamedTurn {
pub message_id: Option<String>,
pub choice: Vec<AssistantContent>,
pub executable_tool_names: BTreeSet<String>,
pub allowed_tool_names: BTreeSet<String>,
pub finish_reason: Option<FinishReason>,
}
#[derive(Debug)]
pub enum StreamedResolution {
Repaired {
tool_name: String,
},
TurnAbandoned {
skipped_tool_result: Option<ToolResult>,
},
Ignored,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum StreamedTurnEvent {
EmitIngested,
HoldToolCall,
EmitToolCall {
call: ToolCall,
},
InvalidToolCall(StreamedInvalidToolCall),
}
#[derive(Clone, Serialize, Deserialize)]
enum PendingInvalid {
Call { tool_call: ToolCall },
MalformedArgs { tool_call: ToolCall },
}
#[derive(Clone, Serialize, Deserialize)]
pub struct StreamedTurnAssembler {
executable_tool_names: BTreeSet<String>,
allowed_tool_names: BTreeSet<String>,
text: String,
reasoning: BTreeMap<usize, String>,
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(
executable_tool_names: BTreeSet<String>,
allowed_tool_names: BTreeSet<String>,
) -> Self {
Self {
executable_tool_names,
allowed_tool_names,
text: String::new(),
reasoning: 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 aggregated_text(&self) -> &str {
&self.text
}
pub fn aggregated_reasoning(&self, index: usize) -> Option<&str> {
self.reasoning.get(&index).map(String::as_str)
}
pub fn ingest(
&mut self,
item: &Item<StreamEvent>,
) -> Result<Vec<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(vec![StreamedTurnEvent::EmitIngested]);
}
};
match event {
StreamEvent::Text { text, .. } => {
self.text.push_str(text);
Ok(vec![StreamedTurnEvent::EmitIngested])
}
StreamEvent::Reasoning { part, text } => {
self.reasoning
.entry(part.index())
.or_default()
.push_str(text);
Ok(vec![StreamedTurnEvent::EmitIngested])
}
StreamEvent::Start {
kind: PartKind::ToolCall,
..
}
| StreamEvent::Arguments { .. } => Ok(vec![StreamedTurnEvent::HoldToolCall]),
StreamEvent::End {
content: AssistantContent::ToolCall(tool_call),
..
} => {
if !self
.allowed_tool_names
.contains(tool_call.function.name.as_str())
{
return Ok(self.surface_invalid_call(
tool_call.clone(),
Some(json_utils::serialize_json_value(
&tool_call.function.arguments,
)),
PendingInvalid::Call {
tool_call: tool_call.clone(),
},
InvalidToolCallReason::UnknownTool,
));
}
self.pending_tool_calls.push(tool_call.clone());
Ok(vec![StreamedTurnEvent::EmitToolCall {
call: tool_call.clone(),
}])
}
StreamEvent::Start { .. } | StreamEvent::End { .. } => {
Ok(vec![StreamedTurnEvent::EmitIngested])
}
}
}
pub fn resolve_pending_invalid(
&mut self,
resolution: &StreamedResolution,
) -> Vec<StreamedTurnEvent> {
let Some(pending) = self.pending_invalid.take() else {
return Vec::new();
};
match (resolution, pending) {
(
StreamedResolution::Repaired { tool_name },
PendingInvalid::Call { mut tool_call },
) => {
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());
vec![StreamedTurnEvent::EmitToolCall { call: tool_call }]
}
(StreamedResolution::Repaired { .. }, PendingInvalid::MalformedArgs { .. })
| (StreamedResolution::TurnAbandoned { .. }, _) => Vec::new(),
(
StreamedResolution::Ignored,
PendingInvalid::Call { tool_call } | PendingInvalid::MalformedArgs { tool_call },
) => {
self.ignored_calls.push(tool_call.id);
Vec::new()
}
}
}
pub fn partial_turn(
&self,
message_id: Option<String>,
ended: &[AssistantContent],
) -> PartialStreamedTurn {
PartialStreamedTurn {
message_id,
text: (!self.text.is_empty()).then(|| self.text.clone()),
reasoning: ended
.iter()
.filter_map(|content| match content {
AssistantContent::Reasoning(reasoning) => Some(reasoning.clone()),
_ => None,
})
.collect(),
pending_tool_calls: self.pending_tool_calls.clone(),
}
}
pub fn finish(
self,
message_id: Option<String>,
response: &rig_core::completion::CompletionResponse,
) -> StreamedTurn {
let choice = response
.choice
.iter()
.filter(|content| match content {
AssistantContent::ToolCall(call) => !self.ignored_calls.contains(&call.id),
AssistantContent::Text(_)
| AssistantContent::Reasoning(_)
| AssistantContent::Image(_) => 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 {
message_id,
choice,
executable_tool_names: self.executable_tool_names.clone(),
allowed_tool_names: self.allowed_tool_names.clone(),
finish_reason: response.finish_reason(),
}
}
fn surface_invalid_call(
&mut self,
tool_call: ToolCall,
args: Option<String>,
pending: PendingInvalid,
reason: InvalidToolCallReason,
) -> Vec<StreamedTurnEvent> {
let invalid = StreamedInvalidToolCall {
tool_call,
args,
executable_tool_names: self.executable_tool_names.clone(),
allowed_tool_names: self.allowed_tool_names.clone(),
reason,
};
self.pending_invalid = Some(pending);
vec![StreamedTurnEvent::InvalidToolCall(invalid)]
}
pub fn surface_malformed_input(
&mut self,
detail: &rig_core::error::MalformedToolInput,
) -> Vec<StreamedTurnEvent> {
let Ok(name) = ToolName::new(detail.name.clone()) else {
return Vec::new();
};
let tool_call = ToolCall::new(
detail.id.clone(),
ToolFunction::new(name, serde_json::Value::Null),
);
self.surface_invalid_call(
tool_call.clone(),
Some(detail.raw.clone()),
PendingInvalid::MalformedArgs { tool_call },
InvalidToolCallReason::MalformedArguments {
error: detail.error.clone(),
},
)
}
}
#[cfg(test)]
mod tests;