Skip to main content

Module run

Module run 

Source
Expand description

The driver: an Agent plus a RunAgentInput in, a [Stream] of events out.

The stream owns the agent’s future and polls it itself, so this crate needs no executor of its own — no tokio::spawn, nothing to configure, and the same code runs on wasm. Draining the stream is running the agent.

struct Greeter;

impl Agent for Greeter {
    type State = ();
    async fn run(&self, ctx: &mut RunContext<()>) -> Result<RunOutcome> {
        ctx.say("hello")?;
        Ok(RunOutcome::Success)
    }
}

let events: Vec<Event> = run(Greeter, RunAgentInput::new("thread-1", "run-1"))
    .collect()
    .await;

assert_eq!(events.first().map(Event::event_type), Some(ag_ui::EventType::RunStarted));
assert_eq!(events.last().map(Event::event_type), Some(ag_ui::EventType::RunFinished));

§A run that outlives its response

Draining the stream is running the agent, so the agent stops where it is when the stream is dropped, and one stream has one reader. A request and its response need no more. A server whose runs go on after the client that started them has gone does: a second client on the same thread, a client that joins a run in progress, a run that another process carries on.

Such a server stores the events and fans them out itself. Admission, storage and recovery are its own. What it takes from here:

  • Runner::into_parts hands out the future that drives the agent and the receiver of what it emits. The driver goes on an executor the server chooses, and the events go to its store.
  • Runner::continue_run drives a run that another driver began, from the events already sent: no second RUN_STARTED, and ordering checks that start where the run is. A server that builds its events by hand does the same with EventVerifier::replay, in verify.
  • RunProgress folds the events of a run so far into what the run has open, and builds the prelude a client that joins it is sent first. A client’s stream has to begin with RUN_STARTED, so the prelude does, and it stands for the events already sent: a client is sent one or the other, never both.

None of these spawns anything. The crate still has no executor of its own.

Structs§

Runner
A configured run: the agent, its transformer chain and its cancellation token.

Functions§

run
Runs agent against input with no transformers and a fresh cancellation token.