Skip to main content

ToolLoopExecutor

Struct ToolLoopExecutor 

Source
pub struct ToolLoopExecutor { /* private fields */ }
Available on crate feature tool-loop only.
Expand description

An AgentExecutor that answers each message by running a ToolLoop.

The loop is wholly inside one A2A task: the caller sees Working, the answer artifact (ANSWER_ARTIFACT_ID), the trace artifact (TRACE_ARTIFACT_ID) when a tool ran, and Completed. The prompt is the message’s text parts, joined by newlines; a message with none is refused as an invalid request.

A run that ends without an answer fails the task with the failure-class extension (a2a_protocol_types::failure): transient when the model provider may come back (HTTP 429, 502, 503 or 504, a timeout, no connection), internal for any other provider failure or a tool that returned ToolError::Fatal, budget-exhausted when the model was still calling tools at the turn bound, the caller’s budget (RequestContext::budget, ADR 0023) ran out (its token usage charged to the tokens meter), or the model hit its output limit before writing any text, and policy-refusal when the provider’s content filter withheld the answer. A final reply with no text is such a run (LoopError::EmptyAnswer). The status message says which, in words. Cancellation ends the run at the next turn or call, with nothing written: the cancel path writes Canceled.

A reply that has text but stopped at the output limit completes the task: the answer artifact’s metadata and the Completed status message both carry FINISH_REASON_METADATA_KEY, so a caller can tell a cut-off answer from one that ended by itself.

Each run’s call keys are scoped to the task and the message it answers (ADR 0026): another message to the same task is new work, with new keys.

with_drafts also streams each turn’s text as it arrives, to an artifact of its own.

Implementations§

Source§

impl ToolLoopExecutor

Source

pub const fn new(tool_loop: ToolLoop) -> ToolLoopExecutor

An executor running tool_loop for each message.

Source

pub const fn with_steering(self) -> ToolLoopExecutor

Reads the turn’s steering inbox (RequestContext::inbox) before each model turn, and adds each message’s text to the conversation as a user turn, so a caller can correct course while the run works (ADR 0036). Off by default. Needs a handler built with with_steering; without one there is no inbox and this does nothing.

The text is the sender’s: it is user text like the prompt, with the sender’s authority over the task and no more. It cannot cancel the run (CancelTask does), approve an action, or lift a budget. A message that arrives during the turn that answers is not read: the run is over, and the message is counted as unread.

Source

pub const fn with_drafts(self) -> ToolLoopExecutor

Streams each turn’s text, as the model sends it, to a draft artifact of the turn’s own, draft-1, draft-2 and so on (draft_artifact_id), each closed with lastChunk when the next turn starts or the run ends; the answer artifact is sent at the end, as without (audit L5). A turn’s text is known to be the answer only when the turn ends, which is why it is not streamed into the answer itself. Needs a model that streams (ToolModel::complete_streaming; OpenAiChatModel does); with one that does not, no draft is sent. Off by default, since the drafts are artifacts a client reading every artifact would now see.

Source§

impl ToolLoopExecutor

Source

pub fn with_skills( self, catalog: &SkillCatalog, ) -> Result<ToolLoopExecutor, DuplicateTool>

Gives the loop the skills in catalog: the catalog goes at the end of the system prompt, and the model gets activate_skill (its name a JSON Schema enum of the skills’ names) and, when some skill bundles files, read_skill_file. A call to either is a tool call like any other: traced as an execute_tool span, counted among the run’s tool calls and bounded by the turn limit and the caller’s budget. No script is run, and no skill adds a tool or approves one.

A catalog with no skills changes nothing: an empty catalog and a tool with no valid argument would only confuse the model.

The prompt is appended to the loop’s own preamble, after a blank line, so this is called on an executor whose loop already has its preamble (the executor has no way to set one afterwards).

§Errors

DuplicateTool when the loop’s tools already include one named activate_skill or read_skill_file. The executor is consumed.

Trait Implementations§

Source§

impl AgentExecutor for ToolLoopExecutor

Source§

fn execute<'a>( &'a self, ctx: &'a RequestContext, queue: &'a (dyn EventQueueWriter + 'static), ) -> Pin<Box<dyn Future<Output = Result<(), A2aError>> + Send + 'a>>

Executes agent logic for the given request. Read more
Source§

fn cancel<'a>( &'a self, ctx: &'a RequestContext, queue: &'a (dyn EventQueueWriter + 'static), ) -> Pin<Box<dyn Future<Output = Result<(), A2aError>> + Send + 'a>>

Cancels an in-progress task. Read more
Source§

fn resumes_after_hand_off(&self) -> bool

Whether a turn this executor did not finish may be run again from its input, so that a graceful shutdown hands it back instead of failing it (ADR 0054, audit D-21). Read more
Source§

fn on_shutdown<'a>(&'a self) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>>

Called during handler shutdown to allow cleanup of external resources (database connections, file handles, etc.). Read more
Source§

impl Debug for ToolLoopExecutor

Source§

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

Formats the value using the given formatter. 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
§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

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
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FutureExt for T

§

fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
§

fn with_current_context(self) -> WithContext<Self> ⓘ

Attaches the current Context to this type, returning a WithContext wrapper. Read more
§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

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
§

impl<T> IntoRequest<T> for T

§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
§

impl<L> LayerExt<L> for L

§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in [Layered].
§

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more