Skip to main content

ToolLoopHandle

Struct ToolLoopHandle 

Source
pub struct ToolLoopHandle<'a, Ctx: LoopDepth + Send + Sync + 'static> { /* private fields */ }
Expand description

Caller-driven resumable tool loop.

Unlike tool_loop which runs autonomously, this struct gives the caller control between each tool execution round. Call next_turn() to advance the loop, inspect the result, then consume the Yielded handle to control what happens next.

§No spawning required

This is a direct state machine — no background tasks, no channels. The caller drives it by calling next_turn() which performs one iteration (LLM call + tool execution) and returns.

§Lifecycle

  1. Create with new()
  2. Call next_turn() to get the first result
  3. If Yielded, consume via resume() / continue_loop() / etc., then call next_turn() again
  4. Repeat until Completed or Error
  5. Optionally call into_result() for a ToolLoopResult

Implementations§

Source§

impl<'a, Ctx: LoopDepth + Send + Sync + 'static> ToolLoopHandle<'a, Ctx>

Source

pub fn new( provider: &'a dyn DynProvider, registry: &'a ToolRegistry<Ctx>, params: ChatParams, config: ToolLoopConfig, ctx: &Ctx, ) -> Self

Create a new resumable tool loop.

Does not start execution — call next_turn() to begin the first iteration.

§Depth Tracking

Same as tool_loop — if Ctx implements LoopDepth, nested calls are tracked and max_depth is enforced. If the depth limit is already exceeded, the first call to next_turn() returns Error.

Source

pub async fn next_turn(&mut self) -> TurnResult<'a, '_, Ctx>

Advance the loop and return the result of this turn.

Each call performs one iteration: LLM generation, tool execution (if applicable), and returns the result.

Returns a TurnResult that must be matched:

After Completed or Error, all subsequent calls return the same terminal result.

Source

pub fn resume(&mut self, command: LoopCommand)

Tell the loop how to proceed before the next next_turn() call.

When using TurnResult::Yielded, prefer the convenience methods on Yielded (continue_loop(), inject_and_continue(), stop()), which consume the yielded handle and call this internally.

This method is useful when you need to set a command on the handle directly — for example, when driving the handle from an external event loop that receives the command asynchronously after the Yielded has already been consumed.

Has no effect after Completed or Error.

Source

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

Get a snapshot of the current conversation messages.

Useful for context window management or debugging.

Source

pub fn messages_mut(&mut self) -> &mut Vec<ChatMessage>

Get a mutable reference to the conversation messages.

Allows direct manipulation of the message history between iterations (e.g., for context compaction/summarization).

Source

pub fn total_usage(&self) -> &Usage

Get the accumulated usage across all iterations so far.

Source

pub fn iterations(&self) -> u32

Get the current iteration count.

Source

pub fn is_finished(&self) -> bool

Whether the loop has finished (returned Completed or Error).

Source

pub fn into_result(self) -> ToolLoopResult

Consume the handle and return a ToolLoopResult.

If the loop hasn’t completed yet, returns a result with current iteration count and TerminationReason::Complete.

Trait Implementations§

Source§

impl<Ctx: LoopDepth + Send + Sync + 'static> Debug for ToolLoopHandle<'_, Ctx>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a, Ctx> Freeze for ToolLoopHandle<'a, Ctx>
where Ctx: Freeze,

§

impl<'a, Ctx> !RefUnwindSafe for ToolLoopHandle<'a, Ctx>

§

impl<'a, Ctx> Send for ToolLoopHandle<'a, Ctx>

§

impl<'a, Ctx> Sync for ToolLoopHandle<'a, Ctx>

§

impl<'a, Ctx> Unpin for ToolLoopHandle<'a, Ctx>
where Ctx: Unpin,

§

impl<'a, Ctx> !UnwindSafe for ToolLoopHandle<'a, Ctx>

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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: 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: 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, 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> 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