pub struct AgentRun { /* private fields */ }agent only.Expand description
The sans-IO agent loop state machine. See the module docs for the driving protocol.
Implementations§
Source§impl AgentRun
impl AgentRun
Sourcepub fn new(prompt: impl Into<Message>) -> AgentRun
pub fn new(prompt: impl Into<Message>) -> AgentRun
Create a run for one prompt with no input history, a one-model-call budget, and no invalid tool-call retries.
Sourcepub fn with_history(self, history: Vec<Message>) -> AgentRun
pub fn with_history(self, history: Vec<Message>) -> AgentRun
Set the input chat history preceding the prompt.
Sourcepub fn max_turns(self, max_turns: usize) -> AgentRun
pub fn max_turns(self, max_turns: usize) -> AgentRun
Set the total model-call budget, including the initial call and every
retry or continuation. A budget of zero emits no model calls. Exceeding
the budget makes AgentRun::next_step return
PromptError::MaxTurnsError.
Sourcepub fn with_output_validation(
self,
output_schema: Option<Value>,
max_output_retries: usize,
) -> AgentRun
pub fn with_output_validation( self, output_schema: Option<Value>, max_output_retries: usize, ) -> AgentRun
Configure Tool output-mode validation (#1928): the JSON schema the output-tool arguments should satisfy, and how many times to re-prompt the model — when it finalizes without calling the output tool, or calls it with arguments missing required fields — before finalizing best-effort.
Sourcepub fn max_invalid_tool_call_retries(self, retries: usize) -> AgentRun
pub fn max_invalid_tool_call_retries(self, retries: usize) -> AgentRun
Set the retry budget for InvalidToolCallAction::Retry
resolutions. Invalid tool-call retries also consume the total model-call
budget.
Sourcepub fn with_tool_choice(self, tool_choice: ToolChoice) -> AgentRun
pub fn with_tool_choice(self, tool_choice: ToolChoice) -> AgentRun
Set the tool choice active for this run. Used to reject
InvalidToolCallAction::Skip resolutions under
ToolChoice::None and reported in invalid tool-call contexts.
Sourcepub fn with_output_tool_name(self, name: impl Into<String>) -> AgentRun
pub fn with_output_tool_name(self, name: impl Into<String>) -> AgentRun
Set the synthetic output-tool name for Tool output mode (see #1928). When a model turn calls this tool, the run finalizes with the call’s arguments (serialized JSON) as the response.
Sourcepub fn completion_calls(&self) -> &[CompletionCall]
pub fn completion_calls(&self) -> &[CompletionCall]
Details for each completed model call so far.
Sourcepub fn messages(&self) -> &[Message]
pub fn messages(&self) -> &[Message]
Messages accumulated by this run (the prompt plus all assistant turns and tool results), excluding the input history.
Sourcepub fn retry_model_turn(
&mut self,
request: RetryRequest,
) -> Result<(), PromptError>
pub fn retry_model_turn( &mut self, request: RetryRequest, ) -> Result<(), PromptError>
Reject the accepted, tool-free model turn and prepare another model call.
RetryRequest::Repeat discards the rejected assistant response and
reuses the same prompt and preceding history with fresh request
preparation. RetryRequest::Feedback records the rejected response
followed by corrective user feedback. Canonical empty assistant turns
are omitted from history, matching normal turn advancement. Both modes
preserve completion-call and usage accounting, and the next call consumes
the existing total model-call budget.
Tool-bearing turns cannot be retried through this operation because preserving them without matching tool results would create invalid provider-visible history. Use tool-call hooks to steer those turns.
Sourcepub fn full_history(&self) -> Vec<Message>
pub fn full_history(&self) -> Vec<Message>
The full conversation: input history followed by Self::messages.
Sourcepub fn is_done(&self) -> bool
pub fn is_done(&self) -> bool
Whether the run reached AgentRunStep::Done.
Sourcepub fn response(&self) -> Option<&PromptResponse>
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.
Sourcepub fn cancel_error(&self, reason: impl Into<String>) -> PromptError
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.
Sourcepub fn pending_invalid_tool_call(&self) -> Option<InvalidToolCallContext>
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.
Sourcepub fn next_step(&mut self) -> Result<AgentRunStep, PromptError>
pub fn next_step(&mut self) -> Result<AgentRunStep, PromptError>
Advance the machine and return the next action for the driver.
§Errors
PromptError::MaxTurnsErrorwhen the total model-call budget is exhausted.PromptError::PromptCancelledwhen the machine is driven out of protocol (for example, calling this while a model response is pending).
Sourcepub fn model_response(
&mut self,
turn: ModelTurn,
) -> Result<ModelTurnOutcome, PromptError>
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.
Sourcepub fn resolve_invalid_tool_call(
&mut self,
action: InvalidToolCallAction,
) -> Result<ModelTurnOutcome, PromptError>
pub fn resolve_invalid_tool_call( &mut self, action: InvalidToolCallAction, ) -> Result<ModelTurnOutcome, PromptError>
Answer a pending ModelTurnOutcome::NeedsResolution.
Applies the agent loop’s recovery semantics:
InvalidToolCallAction::Failfails the run withPromptError::UnknownToolCall.InvalidToolCallAction::Retryrolls the turn back with corrective feedback while budget remains, consuming the total model-call budget.InvalidToolCallAction::Repairrenames the tool call; the repaired name is revalidated against the allowed tools.InvalidToolCallAction::Stopcancels the run withPromptError::prompt_cancelledand the supplied reason.InvalidToolCallAction::Skiprecords a synthetic tool result and suppresses execution of every tool call in the turn. Rejected underToolChoice::None.
Sourcepub fn tool_results(
&mut self,
results: Vec<UserContent>,
) -> Result<(), PromptError>
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.
Sourcepub fn record_streamed_completion_call(
&mut self,
usage: Usage,
) -> Result<CompletionCall, PromptError>
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.
Sourcepub fn streamed_invalid_tool_call_context(
&self,
partial: &PartialStreamedTurn,
invalid: &StreamedInvalidToolCall,
) -> InvalidToolCallContext
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.
Sourcepub fn resolve_streamed_invalid_tool_call(
&mut self,
partial: &PartialStreamedTurn,
invalid: &StreamedInvalidToolCall,
action: InvalidToolCallAction,
) -> Result<StreamedResolution, PromptError>
pub fn resolve_streamed_invalid_tool_call( &mut self, partial: &PartialStreamedTurn, invalid: &StreamedInvalidToolCall, action: InvalidToolCallAction, ) -> 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.
Sourcepub fn streamed_turn(&mut self, turn: StreamedTurn) -> Result<(), PromptError>
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<'de> Deserialize<'de> for AgentRun
impl<'de> Deserialize<'de> for AgentRun
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<AgentRun, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<AgentRun, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
Source§impl Serialize for AgentRun
impl Serialize for AgentRun
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
Auto Trait Implementations§
impl Freeze for AgentRun
impl RefUnwindSafe for AgentRun
impl Send for AgentRun
impl Sync for AgentRun
impl Unpin for AgentRun
impl UnsafeUnpin for AgentRun
impl UnwindSafe for AgentRun
Blanket Implementations§
impl<T> Allocation for T
Source§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
Source§type ArchivedMetadata = ()
type ArchivedMetadata = ()
Source§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneDebuggableStorage for Twhere
T: DebuggableStorage + Clone,
impl<T> CloneDebuggableStorage for Twhere
T: DebuggableStorage + Clone,
fn clone_storage(&self) -> Box<dyn CloneDebuggableStorage>
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> CloneableStorage for T
impl<T> CloneableStorage for T
fn clone_storage(&self) -> Box<dyn CloneableStorage>
impl<T> DebuggableStorage for T
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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 moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<T> IntoToolOutput for Twhere
T: Serialize + 'static,
impl<T> IntoToolOutput for Twhere
T: Serialize + 'static,
Source§fn into_tool_output(self) -> Result<ToolOutput, ToolExecutionError>
fn into_tool_output(self) -> Result<ToolOutput, ToolExecutionError>
Source§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
Source§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
Source§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
Source§unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
Source§fn resolve_niched(out: Place<NichedOption<T, N1>>)
fn resolve_niched(out: Place<NichedOption<T, N1>>)
out indicating that a T is niched.Source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.