strands/vended_tools/
mod.rs1pub mod bash;
16pub mod edit;
17pub mod handoff_to_user;
18pub mod http_request;
19mod input;
20#[cfg(feature = "mcp")]
21pub mod mcp_router;
22pub mod notebook;
23pub mod paths;
24pub mod python_repl;
25pub mod read;
26pub mod shell;
27pub mod sleep;
28pub mod stop;
29pub mod subagent;
30pub mod web_fetch;
31pub mod write;
32
33#[cfg(test)]
34mod conformance_tests;
35#[cfg(test)]
36pub(crate) mod test_support;
37
38use std::sync::Arc;
39use std::time::Duration;
40
41use crate::sandbox::Sandbox;
42use crate::tools::ToolContext;
43use crate::types::JsonValue;
44
45macro_rules! tool_error {
47 ($(#[$doc:meta])* $name:ident) => {
48 $(#[$doc])*
49 #[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
50 #[error("{message}")]
51 #[non_exhaustive]
52 pub struct $name {
53 message: String,
54 }
55
56 impl $name {
57 pub const NAME: &'static str = stringify!($name);
59
60 pub fn new(message: impl Into<String>) -> Self {
62 Self { message: message.into() }
63 }
64
65 #[must_use]
67 pub fn message(&self) -> &str {
68 &self.message
69 }
70 }
71 };
72}
73pub(crate) use tool_error;
74
75fn resolve_sandbox(bound: Option<&Arc<dyn Sandbox>>, context: &ToolContext) -> Arc<dyn Sandbox> {
77 bound.unwrap_or_else(|| context.agent().sandbox()).clone()
78}
79
80fn js_number(value: f64) -> JsonValue {
82 if value.fract() == 0.0 && value.abs() < 9_007_199_254_740_992.0 {
83 #[allow(clippy::cast_possible_truncation, reason = "whole and within the safe integer range")]
84 return JsonValue::from(value as i64);
85 }
86 serde_json::Number::from_f64(value).map_or(JsonValue::Null, JsonValue::Number)
87}
88
89fn js_number_text(value: f64) -> String {
91 match js_number(value) {
92 JsonValue::Number(number) => number.to_string(),
93 _ => value.to_string(),
94 }
95}
96
97fn seconds(value: f64) -> Option<Duration> {
99 Duration::try_from_secs_f64(value).ok()
100}