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>
impl<'a, Deps, Output> AgentRun<'a, Deps, Output>
Sourcepub async fn new(
agent: &'a Agent<Deps, Output>,
prompt: UserContent,
deps: Deps,
options: RunOptions,
) -> Result<AgentRun<'a, Deps, Output>, AgentRunError>
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.
Sourcepub 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>
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();Sourcepub async fn run_to_completion(
self,
) -> Result<AgentRunResult<Output>, AgentRunError>
pub async fn run_to_completion( self, ) -> Result<AgentRunResult<Output>, AgentRunError>
Run to completion.
Sourcepub async fn step(&mut self) -> Result<StepResult, AgentRunError>
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.
Sourcepub fn messages(&self) -> &[ModelRequest]
pub fn messages(&self) -> &[ModelRequest]
Get current messages.
Sourcepub fn is_finished(&self) -> bool
pub fn is_finished(&self) -> bool
Check if finished.
Sourcepub fn step_number(&self) -> u32
pub fn step_number(&self) -> u32
Get current step number.
Sourcepub fn cancel(&self)
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.
Sourcepub fn is_cancelled(&self) -> bool
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.
Sourcepub fn cancellation_token(&self) -> Option<&CancellationToken>
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.