Skip to main content

LoopDriver

Struct LoopDriver 

Source
pub struct LoopDriver<S>
where S: ModelSession,
{ /* private fields */ }
Expand description

The runtime driver that advances the agent loop step by step.

Obtained from Agent::start with the builder’s preloaded transcript and pending-input queue baked in. The typical usage pattern is:

  1. Call next to advance the loop.
  2. Handle the returned LoopStep:

§Example

use agentkit_core::{Item, ItemKind};
use agentkit_loop::{LoopDriver, LoopStep};

let step = driver.next().await?;
match step {
    LoopStep::Finished(result) => println!("Done: {:?}", result.finish_reason),
    LoopStep::Interrupt(interrupt) => {
        // Resolve via the pending handle, then call next() again.
        println!("Interrupted: {interrupt:?}");
    }
}

Implementations§

Source§

impl<S> LoopDriver<S>
where S: ModelSession,

Source

pub fn submit_input(&mut self, input: Vec<Item>) -> Result<(), LoopError>

Internal entry point for buffering user input. Reachable only via InputRequest::submit (resolves an AwaitingInput interrupt, including the very first one after Agent::start) and ToolRoundInfo::submit (interjects between tool rounds). Prior transcript items — the passive starting state of a session — are preloaded via AgentBuilder::transcript; an opening user turn for one-shot calls is preloaded via AgentBuilder::input. New input after start-up always flows through one of the typed submit handles.

Source

pub fn set_next_turn_cache( &mut self, cache: PromptCacheRequest, ) -> Result<(), LoopError>

Override the prompt cache request for the next model turn.

The override is consumed the next time the driver starts a model turn. Session-level defaults still apply to later turns.

Source

pub fn resolve_approval_for( &mut self, call_id: ToolCallId, decision: ApprovalDecision, ) -> Result<(), LoopError>

Resolve a pending LoopInterrupt::ApprovalRequest.

After calling this, invoke next to continue the loop. If the decision is ApprovalDecision::Approve the tool call executes; if denied, an error result is fed back to the model.

§Errors

Returns LoopError::InvalidState if no approval is pending.

Source

pub fn resolve_approval_for_with_patched_input( &mut self, call_id: ToolCallId, input: Value, ) -> Result<(), LoopError>

Resolve a pending LoopInterrupt::ApprovalRequest with a patched input that replaces the model’s original tool arguments.

Equivalent to calling [resolve_approval_for] with ApprovalDecision::Approve except the tool sees input instead of what the model emitted. The transcript still records the model’s original call.

§Errors

Returns LoopError::InvalidState if no approval is pending for call_id.

Source

pub fn resolve_approval( &mut self, decision: ApprovalDecision, ) -> Result<(), LoopError>

Resolve a pending LoopInterrupt::ApprovalRequest when exactly one approval is outstanding.

Source

pub fn snapshot(&self) -> LoopSnapshot

Take a read-only snapshot of the driver’s current transcript and input queue.

Source

pub async fn next(&mut self) -> Result<LoopStep, LoopError>

Advance the loop by one step.

This is the main method for driving the agent. It processes pending interrupt resolutions, consumes queued input, starts a model turn, executes tool calls, and returns once the turn finishes or an interrupt occurs.

If no input is queued and no interrupt is pending, returns LoopStep::Interrupt(LoopInterrupt::AwaitingInput(..)). This is the steady state after Agent::start when no input was preloaded via AgentBuilder::input: the prior transcript loaded via AgentBuilder::transcript is passive, so the first call surfaces AwaitingInput and waits for the host to supply input via InputRequest::submit before any model turn is dispatched. If input was preloaded, the first call dispatches the model directly.

§Errors

Returns LoopError::InvalidState if called while an unresolved interrupt is pending, or propagates provider / tool / compaction errors.

Auto Trait Implementations§

§

impl<S> Freeze for LoopDriver<S>
where S: Freeze,

§

impl<S> !RefUnwindSafe for LoopDriver<S>

§

impl<S> Send for LoopDriver<S>

§

impl<S> Sync for LoopDriver<S>
where S: Sync,

§

impl<S> Unpin for LoopDriver<S>
where S: Unpin,

§

impl<S> UnsafeUnpin for LoopDriver<S>
where S: UnsafeUnpin,

§

impl<S> !UnwindSafe for LoopDriver<S>

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