Skip to main content

AgentRun

Struct AgentRun 

Source
pub struct AgentRun<'a, Deps, Output> { /* private fields */ }
Expand description

Active agent run that can be iterated.

§Cancellation

Use AgentRun::new_with_cancel to create a run with cancellation support. The run will check for cancellation at the start of each step and before each tool execution.

Implementations§

Source§

impl<'a, Deps, Output> AgentRun<'a, Deps, Output>
where Deps: Send + Sync + 'static, Output: Send + Sync + 'static,

Source

pub async fn new( agent: &'a Agent<Deps, Output>, prompt: UserContent, deps: Deps, options: RunOptions, ) -> Result<AgentRun<'a, Deps, Output>, AgentRunError>

Create a new agent run.

Source

pub async fn new_with_cancel( agent: &'a Agent<Deps, Output>, prompt: UserContent, deps: Deps, options: RunOptions, cancel_token: CancellationToken, ) -> Result<AgentRun<'a, Deps, Output>, AgentRunError>

Create a new agent run with cancellation support.

The provided CancellationToken can be used to cancel the agent run mid-execution. When cancelled:

  • The current step will complete (model request or tool execution)
  • The next step will return AgentRunError::Cancelled
§Example
use tokio_util::sync::CancellationToken;

let cancel_token = CancellationToken::new();
let mut run = AgentRun::new_with_cancel(
    &agent,
    "Hello!".into(),
    deps,
    RunOptions::default(),
    cancel_token.clone(),
).await?;

// Cancel from another task
cancel_token.cancel();
Source

pub async fn run_to_completion( self, ) -> Result<AgentRunResult<Output>, AgentRunError>

Run to completion.

Source

pub async fn step(&mut self) -> Result<StepResult, AgentRunError>

Execute one step.

If cancellation is enabled and the token has been triggered, this will return AgentRunError::Cancelled.

Source

pub fn messages(&self) -> &[ModelRequest]

Get current messages.

Source

pub fn usage(&self) -> &RunUsage

Get current usage.

Source

pub fn run_id(&self) -> &str

Get run ID.

Source

pub fn is_finished(&self) -> bool

Check if finished.

Source

pub fn step_number(&self) -> u32

Get current step number.

Source

pub fn cancel(&self)

Cancel the running agent.

If this run was created with cancellation support via AgentRun::new_with_cancel, this will trigger cancellation. The next call to step() will return AgentRunError::Cancelled.

If this run was created without cancellation support (via new), this method does nothing.

Source

pub fn is_cancelled(&self) -> bool

Check if this run was cancelled.

Returns true if a cancellation token was provided and it has been triggered, false otherwise.

Source

pub fn cancellation_token(&self) -> Option<&CancellationToken>

Get the cancellation token if one was provided.

This can be used to share the token with other tasks that need to coordinate cancellation.

Auto Trait Implementations§

§

impl<'a, Deps, Output> Freeze for AgentRun<'a, Deps, Output>
where Output: Freeze,

§

impl<'a, Deps, Output> !RefUnwindSafe for AgentRun<'a, Deps, Output>

§

impl<'a, Deps, Output> Send for AgentRun<'a, Deps, Output>
where Deps: Sync + Send, Output: Send + Sync,

§

impl<'a, Deps, Output> Sync for AgentRun<'a, Deps, Output>
where Deps: Sync + Send, Output: Sync,

§

impl<'a, Deps, Output> Unpin for AgentRun<'a, Deps, Output>
where Output: Unpin,

§

impl<'a, Deps, Output> !UnwindSafe for AgentRun<'a, Deps, Output>

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> Same for T

Source§

type Output = T

Should always be Self
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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