Skip to main content

AgentRun

Struct AgentRun 

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

The sans-IO agent loop state machine. See the module docs for the driving protocol.

Implementations§

Source§

impl AgentRun

Source

pub fn new(prompt: impl Into<Message>) -> Self

Create a run for one prompt with no input history, no multi-turn depth and no invalid tool-call retries.

Source

pub fn with_history(self, history: Vec<Message>) -> Self

Set the input chat history preceding the prompt.

Source

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

Set the maximum multi-turn depth. Exceeding it makes AgentRun::next_step return PromptError::MaxTurnsError.

Source

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

Set the retry budget for InvalidToolCallHookAction::Retry resolutions. Invalid tool-call retries also consume multi-turn depth.

Source

pub fn with_tool_choice(self, tool_choice: ToolChoice) -> Self

Set the tool choice active for this run. Used to reject InvalidToolCallHookAction::Skip resolutions under ToolChoice::None and reported in invalid tool-call contexts.

Source

pub fn usage(&self) -> Usage

Aggregated token usage across all completed model calls so far.

Source

pub fn turn(&self) -> usize

Number of model calls emitted so far (including retries).

Source

pub fn completion_calls(&self) -> &[CompletionCall]

Details for each completed model call so far.

Source

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

Messages accumulated by this run (the prompt plus all assistant turns and tool results), excluding the input history.

Source

pub fn full_history(&self) -> Vec<Message>

The full conversation: input history followed by Self::messages.

Source

pub fn is_done(&self) -> bool

Whether the run reached AgentRunStep::Done.

Source

pub fn response(&self) -> Option<&PromptResponse>

The final response once the run is done, without cloning it. AgentRun::next_step in the done state returns an owned clone (including the full accumulated message history); prefer this when only inspecting the result.

Source

pub fn cancel_error(&self, reason: impl Into<String>) -> PromptError

Build the cancellation error a driver should return when one of its hooks terminates the run, carrying the current full history.

Source

pub fn pending_invalid_tool_call(&self) -> Option<InvalidToolCallContext>

The invalid tool call currently awaiting AgentRun::resolve_invalid_tool_call, if any. Useful to re-derive the resolution context after deserializing a suspended run.

Source

pub fn next_step(&mut self) -> Result<AgentRunStep, PromptError>

Advance the machine and return the next action for the driver.

§Errors
Source

pub fn model_response( &mut self, turn: ModelTurn, ) -> Result<ModelTurnOutcome, PromptError>

Feed the model’s response for the pending AgentRunStep::CallModel.

Records the completion call and aggregates usage, then validates the turn’s tool calls against the advertised tool names. See ModelTurnOutcome for what the driver must do next.

Source

pub fn resolve_invalid_tool_call( &mut self, action: InvalidToolCallHookAction, ) -> Result<ModelTurnOutcome, PromptError>

Answer a pending ModelTurnOutcome::NeedsResolution.

Applies the agent loop’s recovery semantics:

Source

pub fn tool_results( &mut self, results: Vec<UserContent>, ) -> Result<(), PromptError>

Feed the tool results for the pending AgentRunStep::CallTools.

Results may be in any order; they are appended as a single user message, matching what providers expect for parallel tool calls. Each result must be a tool result answering one of the pending calls, and every pending call must be answered — exactly what providers require to accept the next request.

Source

pub fn record_streamed_completion_call( &mut self, usage: Usage, ) -> Result<CompletionCall, PromptError>

Record one provider completion call for a streamed turn.

Streamed turns learn usage from the provider’s final stream event — including for turns abandoned by invalid tool-call recovery, where the stream is drained for usage after the rollback — so recording is decoupled from turn ingestion. Valid while a model response is pending or between a turn rollback and the next AgentRunStep::CallModel; aggregates usage into the run total. Zero-valued usage means the provider reported no usage metrics.

Source

pub fn streamed_invalid_tool_call_context( &self, partial: &PartialStreamedTurn, invalid: &StreamedInvalidToolCall, ) -> InvalidToolCallContext

The recovery-hook context for an invalid tool call surfaced mid-stream by a streamed::StreamedTurnAssembler.

Source

pub fn resolve_streamed_invalid_tool_call( &mut self, partial: &PartialStreamedTurn, invalid: &StreamedInvalidToolCall, action: InvalidToolCallHookAction, ) -> Result<StreamedResolution, PromptError>

Resolve an invalid tool call surfaced mid-stream.

Applies the same recovery semantics as AgentRun::resolve_invalid_tool_call, but rollback messages are assembled from the partial streamed turn — exactly what the model has produced so far — and a successful retry or skip abandons the turn (see StreamedResolution) instead of finishing it.

Source

pub fn streamed_turn(&mut self, turn: StreamedTurn) -> Result<(), PromptError>

Feed the assembled streamed turn for the pending AgentRunStep::CallModel.

Remaining tool calls are validated fail-fast — mid-stream resolution already had recovery-hook access — and the turn then advances through AgentRun::next_step exactly like a non-streamed one.

Trait Implementations§

Source§

impl Clone for AgentRun

Source§

fn clone(&self) -> AgentRun

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for AgentRun

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for AgentRun

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for AgentRun

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

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

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> CloneableStorage for T
where T: Any + Send + Sync + Clone,

Source§

impl<T> DebuggableStorage for T
where T: Any + Send + Sync + Debug,

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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<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