Skip to main content

TransportStream

Struct TransportStream 

Source
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 as Event in 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

Source

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)) })
    }
}
Source

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 });
Source

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.

Source

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.

Source

pub fn into_events<F>(self, on_warning: F) -> impl Stream<Item = Result<Event>>
where F: FnMut(String),

The run as typed events, after the boundary, handing each of its warnings to on_warning. See enforce_events for what a warning is.

Trait Implementations§

Source§

impl Debug for TransportStream

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> PolicyExt for T
where T: ?Sized,

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more