use std::collections::{HashSet, VecDeque};
use std::pin::Pin;
use futures_core::Stream;
use futures_util::StreamExt;
use serde_json::Value;
use crate::client::error::{Error, Result};
use crate::{Event, InputContent, Message, ToolContent, UserContent};
pub(crate) enum Checked {
Event(Event),
Warning(String),
}
#[cfg(not(target_family = "wasm"))]
pub(crate) type CheckedStream = Pin<Box<dyn Stream<Item = Result<Checked>> + Send>>;
#[cfg(target_family = "wasm")]
pub(crate) type CheckedStream = Pin<Box<dyn Stream<Item = Result<Checked>>>>;
pub(crate) fn decode(value: Value, warnings: &mut Vec<String>) -> Result<Option<Event>> {
let Some(value) = crate::protocol::event(value, warnings).map_err(Error::protocol)? else {
return Ok(None);
};
let event: Event = serde_json::from_value(value)?;
note_version(&event, warnings);
Ok(Some(event))
}
fn note_version(event: &Event, warnings: &mut Vec<String>) {
let Event::RunStarted(started) = event else {
return;
};
let Some(version) = &started.protocol_version else {
return;
};
let current = (1, 0);
let declared = version
.split_once('.')
.and_then(|(major, minor)| Some((major.parse::<u64>().ok()?, minor.parse::<u64>().ok()?)));
if declared.is_none_or(|peer| peer > current) {
warnings.push(format!(
"producer declared newer or uninterpretable protocol version \"{}\"",
crate::protocol::excerpt(version, false)
));
}
}
pub(crate) fn stream<S>(raw: S) -> impl Stream<Item = Result<Event>>
where
S: Stream<Item = Result<Value>>,
{
events(raw, |_| {})
}
pub(crate) fn events<S, F>(raw: S, on_warning: F) -> impl Stream<Item = Result<Event>>
where
S: Stream<Item = Result<Value>>,
F: FnMut(String),
{
unchecked(checked(raw), on_warning)
}
pub(crate) fn unchecked<S, F>(checked: S, mut on_warning: F) -> impl Stream<Item = Result<Event>>
where
S: Stream<Item = Result<Checked>>,
F: FnMut(String),
{
checked.filter_map(move |item| {
std::future::ready(match item {
Ok(Checked::Event(event)) => Some(Ok(event)),
Ok(Checked::Warning(warning)) => {
on_warning(warning);
None
}
Err(error) => Some(Err(error)),
})
})
}
pub(crate) const REMEMBERED: usize = 256;
pub(crate) fn checked<S>(raw: S) -> impl Stream<Item = Result<Checked>>
where
S: Stream<Item = Result<Value>>,
{
futures_util::stream::unfold(Boundary::new(raw), |mut boundary| async move {
loop {
if let Some(item) = boundary.out.pop_front() {
return Some((item, boundary));
}
let mut warnings = Vec::new();
if let Some(value) = boundary.ready.pop_front() {
let decoded = decode(value, &mut warnings);
boundary.warn(warnings);
match decoded {
Ok(Some(event)) => boundary.out.push_back(Ok(Checked::Event(event))),
Ok(None) => {}
Err(error) => boundary.fail(error),
}
continue;
}
match boundary.raw.as_mut()?.next().await? {
Ok(value) => {
if value.get("type").and_then(Value::as_str) == Some("RUN_STARTED") {
boundary.delivered.clear();
}
let translated = boundary.legacy.translate(value, &mut warnings);
boundary.warn(warnings);
match translated {
Ok(values) => boundary.ready.extend(values),
Err(error) => boundary.fail(error),
}
}
Err(error) => boundary.fail(error),
}
}
})
}
pub(crate) fn typed<S>(events: S) -> impl Stream<Item = Result<Checked>>
where
S: Stream<Item = Result<Event>>,
{
futures_util::stream::unfold(Boundary::new(events), |mut boundary| async move {
loop {
if let Some(item) = boundary.out.pop_front() {
return Some((item, boundary));
}
match boundary.raw.as_mut()?.next().await? {
Ok(event) => boundary.typed(event),
Err(error) => boundary.fail(error),
}
}
})
}
struct Boundary<S> {
raw: Option<Pin<Box<S>>>,
legacy: Legacy,
ready: VecDeque<Value>,
out: VecDeque<Result<Checked>>,
delivered: HashSet<String>,
}
impl<S> Boundary<S> {
fn new(raw: S) -> Self {
Self {
raw: Some(Box::pin(raw)),
legacy: Legacy::default(),
ready: VecDeque::new(),
out: VecDeque::new(),
delivered: HashSet::new(),
}
}
fn typed(&mut self, event: Event) {
let mut warnings = Vec::new();
if matches!(event, Event::RunStarted(_)) {
self.delivered.clear();
self.legacy = Legacy::default();
}
if !event.is_deprecated() && !carries_legacy_content(&event) {
note_version(&event, &mut warnings);
self.warn(warnings);
self.out.push_back(Ok(Checked::Event(event)));
return;
}
let translated = serde_json::to_value(&event)
.map_err(Error::from)
.and_then(|value| self.legacy.translate(value, &mut warnings))
.and_then(|values| {
let mut events = Vec::with_capacity(values.len());
for value in values {
events.extend(decode(value, &mut warnings)?);
}
Ok(events)
});
self.warn(warnings);
match translated {
Ok(events) => self
.out
.extend(events.into_iter().map(|event| Ok(Checked::Event(event)))),
Err(error) => self.fail(error),
}
}
fn warn(&mut self, warnings: Vec<String>) {
for warning in warnings {
if self.delivered.contains(&warning) {
continue;
}
if self.delivered.len() < REMEMBERED {
self.delivered.insert(warning.clone());
}
self.out.push_back(Ok(Checked::Warning(warning)));
}
}
fn fail(&mut self, error: Error) {
self.out.push_back(Err(error));
self.raw = None;
self.ready.clear();
}
}
fn carries_legacy_content(event: &Event) -> bool {
let binary = |parts: &[InputContent]| {
parts
.iter()
.any(|part| matches!(part, InputContent::Binary(_)))
};
let in_messages = |messages: &[Message]| {
messages.iter().any(|message| match message {
Message::User(user) => {
matches!(&user.content, UserContent::Parts(parts) if binary(parts))
}
Message::Tool(tool) => {
matches!(&tool.content, ToolContent::Parts(parts) if binary(parts))
}
_ => false,
})
};
match event {
Event::MessagesSnapshot(snapshot) => in_messages(&snapshot.messages),
Event::RunStarted(started) => started
.input
.as_ref()
.is_some_and(|input| in_messages(&input.messages)),
Event::ToolCallResult(result) => {
matches!(&result.content, ToolContent::Parts(parts) if binary(parts))
}
_ => false,
}
}
#[derive(Default)]
struct Legacy {
reasoning_id: Option<String>,
message_id: Option<String>,
noticed: crate::protocol::Noticed,
}
impl Legacy {
fn translate(&mut self, mut value: Value, warnings: &mut Vec<String>) -> Result<Vec<Value>> {
let Some(tag) = value.get("type").and_then(Value::as_str).map(str::to_owned) else {
return Ok(vec![value]);
};
if tag == "RUN_STARTED" {
self.reasoning_id = None;
self.message_id = None;
self.noticed = Default::default();
}
crate::protocol::omit_event_nulls(&mut value, &tag, &mut self.noticed, warnings);
crate::message::upgrade_legacy_event_content(&mut value, &tag, warnings);
if !is_legacy_reasoning_tag(&tag) {
return Ok(vec![value]);
}
let _: Event = serde_json::from_value(value.clone())?;
let mut before = Vec::new();
let (replacement, generated) = match tag.as_str() {
"THINKING_START" => {
let id = crate::client::thread::random_id()?;
self.reasoning_id = Some(id.clone());
if value
.as_object_mut()
.and_then(|v| v.remove("title"))
.is_some()
{
warnings.push("retired THINKING_START.title was dropped".to_owned());
}
("REASONING_START", id)
}
"THINKING_TEXT_MESSAGE_START" => {
let id = match &self.reasoning_id {
Some(id) => id.clone(),
None => crate::client::thread::random_id()?,
};
self.message_id = Some(id.clone());
value["role"] = Value::String("reasoning".to_owned());
("REASONING_MESSAGE_START", id)
}
"THINKING_TEXT_MESSAGE_CONTENT" => {
let id = match &self.message_id {
Some(id) => id.clone(),
None => {
let id = match &self.reasoning_id {
Some(id) => id.clone(),
None => crate::client::thread::random_id()?,
};
self.message_id = Some(id.clone());
before.push(serde_json::json!({
"type": "REASONING_MESSAGE_START",
"messageId": id,
"role": "reasoning"
}));
id
}
};
("REASONING_MESSAGE_CONTENT", id)
}
"THINKING_TEXT_MESSAGE_END" => {
let id = match self.message_id.take() {
Some(id) => id,
None => {
let id = match &self.reasoning_id {
Some(id) => id.clone(),
None => crate::client::thread::random_id()?,
};
before.push(serde_json::json!({
"type": "REASONING_MESSAGE_START",
"messageId": id,
"role": "reasoning"
}));
id
}
};
("REASONING_MESSAGE_END", id)
}
"THINKING_END" => {
let id = match self.reasoning_id.take() {
Some(id) => id,
None => crate::client::thread::random_id()?,
};
if let Some(message_id) = self.message_id.take() {
before.push(serde_json::json!({
"type": "REASONING_MESSAGE_END",
"messageId": message_id
}));
}
("REASONING_END", id)
}
_ => unreachable!(),
};
value["type"] = Value::String(replacement.to_owned());
value["messageId"] = Value::String(generated);
warnings.push(format!("translated retired {tag} to {replacement}"));
before.push(value);
Ok(before)
}
}
fn is_legacy_reasoning_tag(tag: &str) -> bool {
matches!(
tag,
"THINKING_START"
| "THINKING_END"
| "THINKING_TEXT_MESSAGE_START"
| "THINKING_TEXT_MESSAGE_CONTENT"
| "THINKING_TEXT_MESSAGE_END"
)
}