use std::fmt;
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::Arc;
use serde::{Deserialize, Serialize, Serializer};
use strands::context_manager::ContextManagerStrategy;
use strands::memory::MemoryStore;
use crate::config::BuiltinToolName;
pub(crate) use crate::tools::programmatic_tool_caller::ProgrammaticToolCallerConfig;
pub(crate) use crate::tools::web_fetch::{WebFetchConfig, WebFetchTransport};
pub(crate) use crate::tools::web_search::WebSearchSetting;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum Effort {
#[default]
Auto,
Off,
Minimal,
Low,
Medium,
High,
Xhigh,
Max,
}
impl Effort {
pub(crate) const ALL: [Self; 8] = [
Self::Auto,
Self::Off,
Self::Minimal,
Self::Low,
Self::Medium,
Self::High,
Self::Xhigh,
Self::Max,
];
#[must_use]
pub fn as_str(self) -> &'static str {
match self {
Self::Auto => "auto",
Self::Off => "off",
Self::Minimal => "minimal",
Self::Low => "low",
Self::Medium => "medium",
Self::High => "high",
Self::Xhigh => "xhigh",
Self::Max => "max",
}
}
pub(crate) fn names() -> String {
Self::ALL.map(Self::as_str).join(", ")
}
}
impl fmt::Display for Effort {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl FromStr for Effort {
type Err = strands::Error;
fn from_str(value: &str) -> strands::Result<Self> {
Self::ALL
.into_iter()
.find(|effort| effort.as_str() == value)
.ok_or_else(|| {
strands::Error::other(format!(
"Effort {} is not valid. Supported: {}.",
json_string(value),
Self::names()
))
})
}
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum Setting<C> {
Enabled(bool),
Config(C),
}
impl<C> Setting<C> {
#[must_use]
pub fn is_enabled(&self) -> bool {
!matches!(self, Self::Enabled(false))
}
}
impl<C> From<bool> for Setting<C> {
fn from(enabled: bool) -> Self {
Self::Enabled(enabled)
}
}
impl<C: Serialize> Serialize for Setting<C> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self {
Self::Enabled(enabled) => serializer.serialize_bool(*enabled),
Self::Config(config) => config.serialize(serializer),
}
}
}
macro_rules! setting_from {
($($config:ty),* $(,)?) => {$(
impl From<$config> for Setting<$config> {
fn from(config: $config) -> Self {
Self::Config(config)
}
}
)*};
}
setting_from!(
ReadConfig,
ShellConfig,
WebFetchConfig,
ProgrammaticToolCallerConfig,
SubagentConfig,
SessionConfig,
MemoryConfig,
crate::config::HarnessConfigWebFetch,
crate::config::HarnessConfigMemory,
);
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize, bon::Builder)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct SubagentConfig {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub max_depth: Option<usize>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, bon::Builder)]
pub struct ReadConfig {
#[serde(skip_serializing_if = "Option::is_none")]
pub media: Option<bool>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, bon::Builder)]
pub struct ShellConfig {
#[builder(into)]
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
}
#[derive(Debug, Clone, Default, bon::Builder)]
pub struct BuiltinToolsConfig {
pub all: Option<bool>,
#[builder(into)]
pub shell: Option<Setting<ShellConfig>>,
#[builder(into)]
pub read: Option<Setting<ReadConfig>>,
pub write: Option<bool>,
pub edit: Option<bool>,
#[builder(into)]
pub web_fetch: Option<Setting<WebFetchConfig>>,
#[builder(into)]
pub web_search: Option<WebSearchSetting>,
#[builder(into)]
pub programmatic_tool_caller: Option<Setting<ProgrammaticToolCallerConfig>>,
#[builder(into)]
pub subagent: Option<Setting<SubagentConfig>>,
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum BuiltinToolsOption {
List(Vec<BuiltinToolName>),
Mapping(BuiltinToolsConfig),
}
impl From<Vec<BuiltinToolName>> for BuiltinToolsOption {
fn from(names: Vec<BuiltinToolName>) -> Self {
Self::List(names)
}
}
impl From<&[BuiltinToolName]> for BuiltinToolsOption {
fn from(names: &[BuiltinToolName]) -> Self {
Self::List(names.to_vec())
}
}
impl<const N: usize> From<[BuiltinToolName; N]> for BuiltinToolsOption {
fn from(names: [BuiltinToolName; N]) -> Self {
Self::List(names.to_vec())
}
}
impl From<BuiltinToolsConfig> for BuiltinToolsOption {
fn from(config: BuiltinToolsConfig) -> Self {
Self::Mapping(config)
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, bon::Builder)]
pub struct SessionConfig {
#[builder(into)]
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<String>,
#[builder(into)]
#[serde(skip_serializing_if = "Option::is_none")]
pub dir: Option<PathBuf>,
}
#[derive(Clone, Default, bon::Builder)]
pub struct MemoryConfig {
#[builder(into)]
pub dir: Option<PathBuf>,
#[builder(default)]
pub stores: Vec<Arc<dyn MemoryStore>>,
}
impl fmt::Debug for MemoryConfig {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("MemoryConfig")
.field("dir", &self.dir)
.field(
"stores",
&self.stores.iter().map(|store| store.name()).collect::<Vec<_>>(),
)
.finish()
}
}
pub type ContextManagerOption = ContextManagerStrategy;
pub(crate) fn json_string(value: &str) -> String {
serde_json::Value::String(value.to_owned()).to_string()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rejects_an_effort_outside_the_enum_by_name() {
let error = "bogus".parse::<Effort>().unwrap_err();
assert_eq!(
error.to_string(),
"Effort \"bogus\" is not valid. Supported: auto, off, minimal, low, medium, high, xhigh, max."
);
assert_eq!("xhigh".parse::<Effort>().unwrap(), Effort::Xhigh);
}
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum McpServersOption {
Path(PathBuf),
Json(strands::types::JsonObject),
#[cfg(feature = "mcp")]
Source(strands::mcp::McpServersSource),
}
impl From<&str> for McpServersOption {
fn from(path: &str) -> Self {
Self::Path(PathBuf::from(path))
}
}
impl From<String> for McpServersOption {
fn from(path: String) -> Self {
Self::Path(PathBuf::from(path))
}
}
impl From<PathBuf> for McpServersOption {
fn from(path: PathBuf) -> Self {
Self::Path(path)
}
}
impl From<strands::types::JsonObject> for McpServersOption {
fn from(servers: strands::types::JsonObject) -> Self {
Self::Json(servers)
}
}
#[cfg(feature = "mcp")]
impl From<strands::mcp::McpServersSource> for McpServersOption {
fn from(source: strands::mcp::McpServersSource) -> Self {
Self::Source(source)
}
}
#[cfg(feature = "mcp")]
impl McpServersOption {
pub(crate) fn into_source(self) -> strands::mcp::McpServersSource {
match self {
Self::Path(path) => strands::mcp::McpServersSource::Path(path),
Self::Json(servers) => strands::mcp::McpServersSource::Json(servers),
Self::Source(source) => source,
}
}
}