Skip to main content

AgentRunner

Struct AgentRunner 

Source
pub struct AgentRunner { /* private fields */ }
Expand description

Manages an Agent across multiple conversation turns.

Preserves transcript between turns, emits events via a caller-provided channel, and tracks cumulative turn count. Each call to turn() runs the agent once with the given goal, then restores the transcript so the next turn continues the conversation.

Optionally holds a reference to a shared BackgroundJobManager. When clear() is called, any pending background jobs are also cleared.

§Example

let mut runner = AgentRunner::new(agent);
let (tx, mut rx) = mpsc::unbounded_channel();

// First turn
let outcome = runner.turn("Hello", Some(tx.clone())).await?;

// Second turn — transcript from turn 1 is preserved
let outcome = runner.turn("Follow up", Some(tx.clone())).await?;

// Start fresh
runner.clear();

Implementations§

Source§

impl AgentRunner

Source

pub fn new(agent: Agent) -> Self

Create a runner from a pre-built Agent.

Source

pub fn with_bg_manager( agent: Agent, bg_manager: Arc<Mutex<BackgroundJobManager>>, ) -> Self

Create a runner that also manages background jobs.

When clear() is called, all tracked background jobs are removed from the shared manager in addition to clearing the transcript.

Source

pub async fn turn( &mut self, goal: impl Into<String>, events: Option<UnboundedSender<StepEvent>>, ) -> Result<AgentOutcome>

Run a single turn with the given goal.

If events is Some(sender), step events are streamed through the channel in real time. Pass None to suppress events.

The transcript is automatically preserved for the next turn. Returns the outcome of this turn.

Source

pub fn clear(&mut self)

Clear the conversation history and reset the turn counter.

If a BackgroundJobManager was provided, all tracked background jobs are also removed.

Source

pub fn turns(&self) -> usize

Number of turns completed so far.

Source

pub fn agent(&self) -> &Agent

Access the underlying agent (e.g., to call confirm_plan).

Source

pub fn agent_mut(&mut self) -> &mut Agent

Mutable access to the underlying agent.

Source

pub async fn run_loop( &mut self, initial_goal: impl Into<String>, wakeup_slot: &WakeupSlot, events: Option<UnboundedSender<StepEvent>>, ) -> Result<Vec<AgentOutcome>>

Run a loop: execute turns until the agent stops scheduling wakeups.

Between turns, sleeps for the requested delay. If the agent doesn’t call schedule_wakeup during a turn, the loop ends.

The wakeup_slot should be the same slot registered with the ScheduleWakeup tool in the agent’s tool registry.

Source

pub async fn run_event_loop( &mut self, initial_goal: impl Into<String>, wakeup_slot: &WakeupSlot, events: Option<UnboundedSender<StepEvent>>, ) -> Result<Vec<AgentOutcome>>

Run a loop with background job awareness.

After each turn, checks both:

  1. The WakeupSlot for an explicit wakeup request (from schedule_wakeup tool)
  2. The BackgroundJobManager for completed jobs

If a background job completed, its output is injected as the next turn’s goal. If a wakeup was scheduled, the runner sleeps for the requested delay then continues. If neither is present, the loop ends.

The wakeup_slot should be the same slot registered with the ScheduleWakeup tool in the agent’s tool registry.

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.

Source§

impl<T> Instrument for T

Source§

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

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

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.

Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: 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
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<T> WithSubscriber for T

Source§

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
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

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