pub struct ToolHandle<'a> { /* private fields */ }Expand description
A registered tool, called directly rather than by the model (ToolHandle).
The name resolves like the model’s tool names: an exact match, then _ for -, then
case-insensitively. A recorded call appends the tool use, its result and
agent.tool.<name> was called. to history and fires MessageAddedEvent for each. Direct
calls run the tool alone: no tool hooks fire, and an interrupt becomes an error result.
use serde_json::json;
use strands::Agent;
use strands::testing::MockModel;
use strands::tools::FunctionTool;
let add = FunctionTool::builder("add", "Adds two numbers").callback(|input, _ctx| async move {
Ok(json!(input["a"].as_i64().unwrap_or(0) + input["b"].as_i64().unwrap_or(0)))
});
let mut agent = Agent::builder().model(MockModel::new()).tools([add]).printer(false).build()?;
let result = agent.tool("add").invoke(json!({"a": 5, "b": 3})).await?;
assert_eq!(serde_json::to_value(&result.content)?, json!([{"text": "8"}]));
assert_eq!(agent.messages().len(), 3);Implementations§
Source§impl<'a> ToolHandle<'a>
impl<'a> ToolHandle<'a>
Sourcepub async fn invoke(self, input: JsonValue) -> Result<ToolResultBlock>
pub async fn invoke(self, input: JsonValue) -> Result<ToolResultBlock>
Calls the tool and returns its result (invoke).
§Errors
ToolNotFoundError when no tool matches the name.
Sourcepub async fn invoke_with(
self,
input: JsonValue,
options: DirectToolCallOptions,
) -> Result<ToolResultBlock>
pub async fn invoke_with( self, input: JsonValue, options: DirectToolCallOptions, ) -> Result<ToolResultBlock>
Sourcepub fn stream(self, input: JsonValue) -> BoxStream<'a, Result<ToolStreamItem>>
pub fn stream(self, input: JsonValue) -> BoxStream<'a, Result<ToolStreamItem>>
Streams the tool’s progress events, ending with its result (stream).
Sourcepub fn stream_with(
self,
input: JsonValue,
options: DirectToolCallOptions,
) -> BoxStream<'a, Result<ToolStreamItem>>
pub fn stream_with( self, input: JsonValue, options: DirectToolCallOptions, ) -> BoxStream<'a, Result<ToolStreamItem>>
Streams the tool with options.
Trait Implementations§
Auto Trait Implementations§
impl<'a> !RefUnwindSafe for ToolHandle<'a>
impl<'a> !UnwindSafe for ToolHandle<'a>
impl<'a> Freeze for ToolHandle<'a>
impl<'a> Send for ToolHandle<'a>
impl<'a> Sync for ToolHandle<'a>
impl<'a> Unpin for ToolHandle<'a>
impl<'a> UnsafeUnpin for ToolHandle<'a>
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
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
§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> ⓘ
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 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> ⓘ
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 moreCreates a shared type from an unshared type.