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
impl AgentRun
Sourcepub fn new(prompt: impl Into<Message>) -> Self
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.
Sourcepub fn with_history(self, history: Vec<Message>) -> Self
pub fn with_history(self, history: Vec<Message>) -> Self
Set the input chat history preceding the prompt.
Sourcepub fn max_turns(self, max_turns: usize) -> Self
pub fn max_turns(self, max_turns: usize) -> Self
Set the maximum multi-turn depth. Exceeding it makes
AgentRun::next_step return PromptError::MaxTurnsError.
Sourcepub fn max_invalid_tool_call_retries(self, retries: usize) -> Self
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.
Sourcepub fn with_tool_choice(self, tool_choice: ToolChoice) -> Self
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.
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 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 multi-turn depth 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: InvalidToolCallHookAction,
) -> Result<ModelTurnOutcome, PromptError>
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:
InvalidToolCallHookAction::Failfails the run withPromptError::UnknownToolCall.InvalidToolCallHookAction::Retryrolls the turn back with corrective feedback while budget remains, consuming multi-turn depth.InvalidToolCallHookAction::Repairrenames the tool call; the repaired name is revalidated against the allowed tools.InvalidToolCallHookAction::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: InvalidToolCallHookAction,
) -> Result<StreamedResolution, PromptError>
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.
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<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
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§
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>,
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 more