Skip to main content

enforce_events

Function enforce_events 

Source
pub fn enforce_events<S, F>(
    raw: S,
    on_warning: F,
) -> impl Stream<Item = Result<Event>>
where S: Stream<Item = Result<Value>>, F: FnMut(String),
Expand description

Runs raw wire events through the compatibility and enforcement boundary every transport shares, handing each of its warnings to on_warning.

This is the boundary a Thread applies to TransportStream::json, for code that reads wire JSON without one.

A warning says what the boundary removed or translated on the way: an unrecognised field, enum member or event, a retired THINKING_* event, a deprecated optional null, a newer declared protocol version. It arrives before the event it concerns, and a text already handed on in the same run is not handed on again. decode_events and RemoteAgent::run_events apply the same boundary and discard them; a Thread yields them as Update::Warning. Nothing in this crate prints them.

use ag_ui::client::transport::{decode_raw_events, enforce_events};
use futures_util::{StreamExt, stream};

let body = "data: {\"type\":\"RUN_STARTED\",\"threadId\":\"t\",\"runId\":\"r\",\"extra\":1}\n\n";
let chunks = stream::iter([Ok::<_, std::io::Error>(body.as_bytes())]);
let mut warnings = Vec::new();
let events: Vec<_> = enforce_events(decode_raw_events(chunks), |warning| warnings.push(warning))
    .collect()
    .await;
assert!(events[0].is_ok());
assert_eq!(warnings, ["unrecognised property at /extra"]);