pub struct TransportStream(/* private fields */);Expand description
One run’s events as a transport has them, before the client reads them.
A transport builds one of two forms and the client does the rest:
json— wire JSON, exactly as received. The client runs it through the compatibility boundary: schema enforcement, the optional-null registry and the translation of retired events. This is the form for anything that reads bytes.typed— events built asEventin this process. They are delivered as given, with no serialize, schema walk and deserialize per event.
Either way the boundary’s warnings reach a
Thread as
Update::Warning.
Implementations§
Source§impl TransportStream
impl TransportStream
Sourcepub fn json(raw: impl Stream<Item = Result<Value>> + Send + 'static) -> Self
pub fn json(raw: impl Stream<Item = Result<Value>> + Send + 'static) -> Self
Wire JSON, one value per event, with unknown fields left in.
An item that is an error ends the run. decode_raw_events produces
this stream from the chunks of a text/event-stream body.
use ag_ui::RunAgentInput;
use ag_ui::client::transport::{Transport, TransportFuture, TransportStream};
use futures_util::stream;
use serde_json::{Value, json};
/// A socket, a queue, a file: anything that yields JSON values.
struct Frames(Vec<Value>);
impl Transport for Frames {
fn run(&self, _input: RunAgentInput) -> TransportFuture {
let frames = stream::iter(self.0.clone().into_iter().map(Ok));
Box::pin(async move { Ok(TransportStream::json(frames)) })
}
}Sourcepub fn typed(events: impl Stream<Item = Result<Event>> + Send + 'static) -> Self
pub fn typed(events: impl Stream<Item = Result<Event>> + Send + 'static) -> Self
Events that were built as Event in this process and never
serialized, such as the stream of an agent hosted in the same program.
They are delivered as given: nothing is checked against the schema and
nothing is removed, because an Event holds nothing this version
does not know. What 1.0 retired is still translated as its wire form
would be, so a consumer sees one contract whichever form fed it: a
THINKING_* event, and a binary content part in a message snapshot,
an input echo or a tool result. A newer declared protocol version is
still reported. Call enforced to have every event
checked instead.
use std::sync::Arc;
use ag_ui::client::transport::{Transport, TransportFuture, TransportStream};
use ag_ui::client::{RunEnd, Thread};
use ag_ui::server::{Agent, RunContext};
use ag_ui::{RunAgentInput, RunOutcome};
use futures_util::StreamExt;
struct Greeter;
impl Agent for Greeter {
type State = ();
async fn run(&self, ctx: &mut RunContext<()>) -> ag_ui::server::Result<RunOutcome> {
ctx.say("hello")?;
Ok(RunOutcome::Success)
}
}
/// An agent in the same process: no wire, so no JSON.
struct InProcess(Arc<Greeter>);
impl Transport for InProcess {
fn run(&self, input: RunAgentInput) -> TransportFuture {
let events = ag_ui::server::run(Arc::clone(&self.0), input).map(Ok);
Box::pin(async move { Ok(TransportStream::typed(events)) })
}
}
let mut thread = Thread::new(InProcess(Arc::new(Greeter)), "thread-1");
let report = thread.send("hi").unwrap().collect_report().await;
assert_eq!(report.end, RunEnd::Success { result: None });Sourcepub fn enforced(self) -> Self
pub fn enforced(self) -> Self
Has typed events read the way a peer’s are: each is serialized and goes through the whole compatibility boundary. Wire JSON is left as it is.
For a typed source that stands in for a remote agent, where the test is
what a client makes of the bytes. ReplayTransport does this.
Sourcepub fn into_json(self) -> RawEventStream
pub fn into_json(self) -> RawEventStream
The run as wire JSON, before the boundary: what a proxy or a recorder forwards. Typed events are serialized.
Sourcepub fn into_events<F>(self, on_warning: F) -> impl Stream<Item = Result<Event>>
pub fn into_events<F>(self, on_warning: F) -> impl Stream<Item = Result<Event>>
The run as typed events, after the boundary, handing each of its
warnings to on_warning. See enforce_events for what a warning is.