use std::sync::Arc;
use futures::future::BoxFuture;
use strands::models::Model;
use strands::multiagent::{AgentSpec, Axis, ChildAgentBuilder, Choice, Fixed, SpecValue};
use strands::plugins::Plugin;
use strands::tools::Tool;
use strands::vended_tools::subagent::{MakeSubagentOptions, generalist, make_subagent};
use strands::{Agent, ModelSource};
use crate::builtin_tools::WebSearchMode;
use crate::defaults::DEFAULT_SUBAGENT_MAX_DEPTH;
use crate::env::Env;
use crate::plugins::EnvironmentContext;
use crate::tools::file_tools;
use crate::tools::programmatic_tool_caller::{ProgrammaticToolCallerConfig, programmatic_tool_caller};
use crate::tools::web_fetch::{WebFetchConfig, WebFetchTransport, web_fetch_tool};
use crate::types::ShellConfig;
#[derive(Debug, Clone, Default)]
pub(crate) struct ChildSpec {
pub(crate) instructions: Option<String>,
pub(crate) tools: Option<Vec<String>>,
pub(crate) model: Option<ModelSource>,
}
pub(crate) type ChildBuilder = Arc<dyn Fn(ChildSpec) -> BoxFuture<'static, strands::Result<Agent>> + Send + Sync>;
#[derive(Debug, Clone, Copy)]
pub(crate) struct ReadToolOptions {
pub(crate) media: bool,
}
#[derive(Clone)]
pub(crate) struct WebFetchToolOptions {
pub(crate) model: Arc<dyn Model>,
pub(crate) transport: Option<WebFetchTransport>,
}
#[derive(Clone)]
pub(crate) struct SubagentToolOptions {
pub(crate) builder: ChildBuilder,
pub(crate) inherited_tools: Vec<String>,
pub(crate) max_depth: Option<usize>,
}
pub(crate) struct BuiltinToolFactory {
pub(crate) shell: fn(&ShellConfig) -> strands::Result<Arc<dyn Tool>>,
pub(crate) read: fn(ReadToolOptions) -> strands::Result<Arc<dyn Tool>>,
pub(crate) write: fn() -> strands::Result<Arc<dyn Tool>>,
pub(crate) edit: fn() -> strands::Result<Arc<dyn Tool>>,
pub(crate) web_fetch: fn(WebFetchToolOptions) -> strands::Result<Arc<dyn Tool>>,
pub(crate) web_search: fn(WebSearchMode, &Env) -> strands::Result<Arc<dyn Tool>>,
pub(crate) programmatic_tool_caller: fn(&ProgrammaticToolCallerConfig) -> strands::Result<Arc<dyn Tool>>,
pub(crate) subagent: fn(SubagentToolOptions) -> strands::Result<Arc<dyn Tool>>,
pub(crate) environment: fn() -> Arc<dyn Plugin>,
}
pub(crate) const BUILTIN_TOOL_FACTORY: BuiltinToolFactory = BuiltinToolFactory {
shell: |config| Ok(Arc::new(file_tools::shell_tool(config.description.clone())?)),
read: |options| Ok(Arc::new(file_tools::read_tool(options.media))),
write: || Ok(Arc::new(file_tools::write_tool())),
edit: || Ok(Arc::new(file_tools::edit_tool())),
web_fetch: |options| {
let config = WebFetchConfig {
model: None,
transport: options.transport,
};
Ok(Arc::new(web_fetch_tool(config, Some(options.model))?))
},
web_search,
programmatic_tool_caller: |config| Ok(Arc::new(programmatic_tool_caller(config.clone()))),
subagent,
environment: || Arc::new(EnvironmentContext::default()),
};
fn web_search(mode: WebSearchMode, env: &Env) -> strands::Result<Arc<dyn Tool>> {
match mode {
WebSearchMode::AgentCore => agent_core_web_search(env),
_ => exa_web_search(),
}
}
#[cfg(any(feature = "bedrock", feature = "mantle"))]
fn agent_core_web_search(env: &Env) -> strands::Result<Arc<dyn Tool>> {
Ok(Arc::new(crate::tools::web_search::agent_core_web_search(env)))
}
#[cfg(not(any(feature = "bedrock", feature = "mantle")))]
fn agent_core_web_search(_env: &Env) -> strands::Result<Arc<dyn Tool>> {
Err(strands::Error::other(
"web_search through AgentCore needs the strands-harness feature \"bedrock\" or \"mantle\"; enable one, or drop web_search from builtinTools.",
))
}
#[cfg(feature = "mcp")]
fn exa_web_search() -> strands::Result<Arc<dyn Tool>> {
Ok(Arc::new(crate::tools::web_search::web_search_tool()))
}
#[cfg(not(feature = "mcp"))]
fn exa_web_search() -> strands::Result<Arc<dyn Tool>> {
Err(strands::Error::other(
"web_search through Exa needs the strands-harness feature \"mcp\"; enable it, or drop web_search from builtinTools.",
))
}
pub(crate) const SUBAGENT_DESCRIPTION: &str = concat!(
"Delegate a self-contained task to a subagent that runs in its own context and returns a final ",
"report. Reach for this when a subtask would otherwise flood your context with intermediate ",
"work and you only need its conclusion. Do not poll for progress or redo its work."
);
const CONTEXT_MODES: [&str; 3] = ["none", "all", "no_tools"];
fn child_spec(spec: AgentSpec) -> ChildSpec {
ChildSpec {
instructions: spec.instructions,
tools: spec.tools,
model: spec.model.and_then(|model| match model {
SpecValue::Model(model) => Some(ModelSource::Model(model)),
SpecValue::Text(model) => Some(ModelSource::Id(model)),
_ => None,
}),
}
}
fn subagent(options: SubagentToolOptions) -> strands::Result<Arc<dyn Tool>> {
let SubagentToolOptions {
builder,
inherited_tools,
max_depth,
} = options;
let tools: Axis = if inherited_tools.is_empty() {
Fixed::new(SpecValue::List(Vec::new())).into()
} else {
Choice::new(inherited_tools).multiple(true).into()
};
let tool = make_subagent(MakeSubagentOptions {
builder: Some(ChildAgentBuilder::new(move |spec| builder(child_spec(spec)))),
presets: Some(vec![("generalist".to_owned(), generalist())]),
tools: Some(tools),
context: Some(Choice::new(CONTEXT_MODES).into()),
max_depth: Some(max_depth.unwrap_or(DEFAULT_SUBAGENT_MAX_DEPTH)),
description: Some(SUBAGENT_DESCRIPTION.to_owned()),
..MakeSubagentOptions::default()
})?;
Ok(Arc::new(tool))
}
pub(crate) const ENVIRONMENT_PLUGIN_NAME: &str = "strands:environment";