Skip to main content

AgentRunner

Struct AgentRunner 

Source
#[non_exhaustive]
pub struct AgentRunner<M>
where M: CompletionModel,
{ /* private fields */ }
Expand description

A hook-aware driver over AgentRun.

Construct one from an Agent with Agent::runner, attach hooks with add_hook, then call run (blocking) or stream (incremental). Hooks are held in a HookStack, an ordered, runtime-composable list; run() and stream() share the same loop and fire the same events, so they behave identically apart from the streamed delta events the medium adds.

Implementations§

Source§

impl<M> AgentRunner<M>

Source

pub async fn stream(self) -> StreamingResult<M::StreamingResponse>

Drive the agent loop, streaming assistant content, tool activity, and a final response. Hooks fire at every observable point, including streamed text and tool-call deltas. Returns the stream after loading any configured conversation memory.

Shares the drive loop, run construction, tool execution and fail-closed hook handling with the blocking run via drive_agent, so the two behave identically apart from the streamed delta events.

Source§

impl<M> AgentRunner<M>
where M: CompletionModel,

Source

pub fn from_agent(agent: &Agent<M>, prompt: impl Into<Message>) -> Self

Build a runner from an agent, seeding it with the agent’s default hook stack. Prefer Agent::runner.

Source

pub fn add_hook<H>(self, hook: H) -> Self
where H: AgentHook<M> + 'static,

Append a hook to the stack (on top of any the agent already carries). Hooks run in registration order; how their results compose is event-dependent (CompletionCall request patches accumulate and merge, ToolCall/ToolResult rewrites chain, and only observe-only/recovery events use first-non-Flow::Continue-wins). See the hook module docs.

Source§

impl<M> AgentRunner<M>
where M: CompletionModel,

Source

pub fn max_turns(self, max_turns: usize) -> Self

Set the total model-call budget, including the initial call and every retry or continuation. Zero emits no model calls; one permits only the initial call. Exceeding the budget returns PromptError::MaxTurnsError.

Source

pub fn tool_extensions(self, extensions: ToolCallExtensions) -> Self

Set the per-call runtime ToolCallExtensions for this run.

The extensions are threaded to every tool the agent executes, so tools can read caller-provided values (auth tokens, session IDs, conversation state, …) via Tool::call_with_extensions without the model ever seeing them. Replaces any extensions already set.

Source

pub fn history<I, T>(self, history: I) -> Self
where I: IntoIterator<Item = T>, T: Into<Message>,

Set the chat history preceding the prompt. Passing explicit history bypasses conversation memory for this run.

Source

pub fn tool_concurrency(self, concurrency: usize) -> Self

Execute up to concurrency tools at once (1 by default). Applies to both the blocking run and the streaming stream paths.

The resulting message history is the same in both paths regardless of concurrency: final tool results are persisted in tool-call order. At the default concurrency of 1 the two paths are fully in lock-step; with concurrency > 1 the tools run in parallel, so a ToolCall/ToolResult hook may fire in completion order rather than call order — the per-tool side effects interleave even though the final history does not.

For the streaming path: the driver emits all of a turn’s ToolCall stream items eagerly (in call order) when the model turn commits, then — only after the whole tool batch settles successfully — surfaces the per-tool ToolExecutionStart and ToolResult stream items in call order (never completion order), for the tools whose body actually ran. The persisted message history is unchanged.

A concurrency of 0 is clamped to 1; 0 and 1 both run a turn’s tools sequentially (the buffer_unordered path is used only at concurrency > 1).

Source

pub fn conversation(self, id: impl Into<String>) -> Self

Set the conversation id used to load and persist memory for this run.

Source

pub fn without_memory(self) -> Self

Disable conversation memory for this run (no load, no save).

Source

pub fn max_invalid_tool_call_retries(self, retries: usize) -> Self

Set the retry budget for invalid tool-call recovery. Invalid tool-call retries also consume the total model-call budget.

Source§

impl<M> AgentRunner<M>
where M: CompletionModel,

Source

pub async fn run(self) -> Result<PromptResponse, PromptError>

Drive the agent loop to completion, returning the aggregated PromptResponse. Hooks fire at every observable point; the first hook to terminate cancels the run.

Auto Trait Implementations§

§

impl<M> !RefUnwindSafe for AgentRunner<M>

§

impl<M> !UnwindSafe for AgentRunner<M>

§

impl<M> Freeze for AgentRunner<M>

§

impl<M> Send for AgentRunner<M>

§

impl<M> Sync for AgentRunner<M>

§

impl<M> Unpin for AgentRunner<M>

§

impl<M> UnsafeUnpin for AgentRunner<M>

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

Source§

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

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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> WasmCompatSend for T
where T: Send,

Source§

impl<T> WasmCompatSync for T
where T: Sync,

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