#[non_exhaustive]pub struct ToolAgent { /* private fields */ }Expand description
The view of the agent that tools and per-tool hook events get.
Tools run on their own tasks, so this view is 'static and cheap to clone. It shares the
agent’s app state, model state, models and cancel handle, and holds a snapshot of the messages
and tool registry taken when the tool batch started. History cannot be rewritten mid-batch.
Implementations§
Source§impl ToolAgent
impl ToolAgent
Sourcepub fn session_id(&self) -> &str
pub fn session_id(&self) -> &str
The id of the current conversation session.
Sourcepub fn app_state(&self) -> &StateStore
pub fn app_state(&self) -> &StateStore
The agent’s app state, shared with the agent.
Sourcepub fn model_state(&self) -> &StateStore
pub fn model_state(&self) -> &StateStore
The model provider’s runtime state, shared with the agent.
Sourcepub fn sandbox(&self) -> &Arc<dyn Sandbox> ⓘ
pub fn sandbox(&self) -> &Arc<dyn Sandbox> ⓘ
Where the agent’s tools run commands and touch files (sandbox), shared with the agent.
Sourcepub fn aux_model(&self) -> &Arc<dyn Model> ⓘ
pub fn aux_model(&self) -> &Arc<dyn Model> ⓘ
The model for auxiliary side calls; the agent’s model unless one was configured.
Sourcepub fn cancel_signal(&self) -> CancelSignal
pub fn cancel_signal(&self) -> CancelSignal
The agent’s cancellation signal for the current invocation.
Sourcepub fn cancel(&self, options: CancelOptions)
pub fn cancel(&self, options: CancelOptions)
Cancels the agent’s current invocation (cancel). With
CancelOptions::after_current_tools the agent stops once the running tool batch ends.
Sourcepub fn tool_registry(&self) -> &ToolRegistry
pub fn tool_registry(&self) -> &ToolRegistry
The agent’s tools when the tool batch started.
Sourcepub fn invocation_context(&self) -> Option<&InvocationContext>
pub fn invocation_context(&self) -> Option<&InvocationContext>
The request the tool batch belongs to.
Sourcepub async fn call_tool(
&self,
name: &str,
input: JsonValue,
) -> Result<ToolResultBlock, InterruptError>
pub async fn call_tool( &self, name: &str, input: JsonValue, ) -> Result<ToolResultBlock, InterruptError>
Calls a registered tool through the agent’s full per-tool pipeline (hooks, interventions, middleware) and returns its result.
Inside a tool, the call’s id is <toolUseId>:call:<n>, numbered in call order from 0;
elsewhere it is a fresh tooluse_<uuid>. Failures, an unknown tool name included, are
error results.
§Errors
The InterruptError when a hook or intervention on the inner call (an approval, say)
or the inner tool itself raised an interrupt. Return it with ?: the calling tool then
stops with the interrupt, and it stops with it even if it carries on. On resume the
calling tool runs again from the start, its inner calls get the same ids, and the
answered call proceeds.
Sourcepub async fn call_tool_without_interrupts(
&self,
name: &str,
input: JsonValue,
refusal: impl FnOnce(&InterruptError) -> String + Send + 'static,
) -> Result<ToolResultBlock, RefusedCall>
pub async fn call_tool_without_interrupts( &self, name: &str, input: JsonValue, refusal: impl FnOnce(&InterruptError) -> String + Send + 'static, ) -> Result<ToolResultBlock, RefusedCall>
Calls a registered tool like call_tool, for a caller that cannot stop
for human input, such as code orchestrating tools.
An interrupt the inner call raises (from a hook, an intervention, middleware or the tool
itself) is returned in the error, but it is neither recorded on the calling tool nor
registered with the agent: the calling tool completes, and a resume never runs it again.
When a before-call hook or intervention raised it, the call is refused with an error
result whose text refusal gives, and AfterToolCallEvent fires with that result, as
for a call a hook cancels. Responses saved on the agent do not answer such an interrupt;
a preemptive response (InterruptParams::with_response) still does. Call ids are
numbered as for call_tool.
use serde_json::json;
use strands::Agent;
use strands::agent::DirectToolCallOptions;
use strands::tools::FunctionTool;
use strands::types::{InterruptParams, ToolResultContent};
let ask = FunctionTool::builder("ask", "needs a human").callback(|_, context| async move {
context.interrupt(InterruptParams::new("approve"))?;
Ok(json!("approved"))
});
let outer = FunctionTool::builder("outer", "calls ask").callback(|_, context| async move {
let refused = context
.agent()
.call_tool_without_interrupts("ask", json!({}), |_| "needs a human".to_owned())
.await
.is_err();
Ok(json!(format!("refused: {refused}")))
});
let mut agent = Agent::builder().tools([ask, outer]).printer(false).build()?;
let result = agent
.tool("outer")
.invoke_with(json!({}), DirectToolCallOptions { record_direct_tool_call: Some(false) })
.await?;
assert!(matches!(&result.content[..], [ToolResultContent::Text(text)] if text.text == "refused: true"));§Errors
A RefusedCall when the inner call raised an unanswered interrupt.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for ToolAgent
impl !UnwindSafe for ToolAgent
impl Freeze for ToolAgent
impl Send for ToolAgent
impl Sync for ToolAgent
impl Unpin for ToolAgent
impl UnsafeUnpin for ToolAgent
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> 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