1use std::fmt;
7use std::path::PathBuf;
8use std::str::FromStr;
9use std::sync::Arc;
10
11use serde::{Deserialize, Serialize, Serializer};
12use strands::context_manager::ContextManagerStrategy;
13use strands::memory::MemoryStore;
14
15use crate::config::BuiltinToolName;
16pub(crate) use crate::tools::programmatic_tool_caller::ProgrammaticToolCallerConfig;
17pub(crate) use crate::tools::web_fetch::{WebFetchConfig, WebFetchTransport};
18pub(crate) use crate::tools::web_search::WebSearchSetting;
19
20#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
23#[serde(rename_all = "lowercase")]
24#[non_exhaustive]
25pub enum Effort {
26 #[default]
28 Auto,
29 Off,
31 Minimal,
33 Low,
35 Medium,
37 High,
39 Xhigh,
41 Max,
43}
44
45impl Effort {
46 pub(crate) const ALL: [Self; 8] = [
48 Self::Auto,
49 Self::Off,
50 Self::Minimal,
51 Self::Low,
52 Self::Medium,
53 Self::High,
54 Self::Xhigh,
55 Self::Max,
56 ];
57
58 #[must_use]
60 pub fn as_str(self) -> &'static str {
61 match self {
62 Self::Auto => "auto",
63 Self::Off => "off",
64 Self::Minimal => "minimal",
65 Self::Low => "low",
66 Self::Medium => "medium",
67 Self::High => "high",
68 Self::Xhigh => "xhigh",
69 Self::Max => "max",
70 }
71 }
72
73 pub(crate) fn names() -> String {
74 Self::ALL.map(Self::as_str).join(", ")
75 }
76}
77
78impl fmt::Display for Effort {
79 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
80 f.write_str(self.as_str())
81 }
82}
83
84impl FromStr for Effort {
85 type Err = strands::Error;
86
87 fn from_str(value: &str) -> strands::Result<Self> {
89 Self::ALL
90 .into_iter()
91 .find(|effort| effort.as_str() == value)
92 .ok_or_else(|| {
93 strands::Error::other(format!(
94 "Effort {} is not valid. Supported: {}.",
95 json_string(value),
96 Self::names()
97 ))
98 })
99 }
100}
101
102#[derive(Debug, Clone, PartialEq)]
105#[non_exhaustive]
106pub enum Setting<C> {
107 Enabled(bool),
109 Config(C),
111}
112
113impl<C> Setting<C> {
114 #[must_use]
116 pub fn is_enabled(&self) -> bool {
117 !matches!(self, Self::Enabled(false))
118 }
119}
120
121impl<C> From<bool> for Setting<C> {
122 fn from(enabled: bool) -> Self {
123 Self::Enabled(enabled)
124 }
125}
126
127impl<C: Serialize> Serialize for Setting<C> {
128 fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
129 match self {
130 Self::Enabled(enabled) => serializer.serialize_bool(*enabled),
131 Self::Config(config) => config.serialize(serializer),
132 }
133 }
134}
135
136macro_rules! setting_from {
137 ($($config:ty),* $(,)?) => {$(
138 impl From<$config> for Setting<$config> {
139 fn from(config: $config) -> Self {
140 Self::Config(config)
141 }
142 }
143 )*};
144}
145
146setting_from!(
147 ReadConfig,
148 ShellConfig,
149 WebFetchConfig,
150 ProgrammaticToolCallerConfig,
151 SubagentConfig,
152 SessionConfig,
153 MemoryConfig,
154 crate::config::HarnessConfigWebFetch,
155 crate::config::HarnessConfigMemory,
156);
157
158#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize, bon::Builder)]
160#[serde(rename_all = "camelCase", deny_unknown_fields)]
161pub struct SubagentConfig {
162 #[serde(default, skip_serializing_if = "Option::is_none")]
165 pub max_depth: Option<usize>,
166}
167
168#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, bon::Builder)]
170pub struct ReadConfig {
171 #[serde(skip_serializing_if = "Option::is_none")]
174 pub media: Option<bool>,
175}
176
177#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, bon::Builder)]
179pub struct ShellConfig {
180 #[builder(into)]
182 #[serde(skip_serializing_if = "Option::is_none")]
183 pub description: Option<String>,
184}
185
186#[derive(Debug, Clone, Default, bon::Builder)]
201pub struct BuiltinToolsConfig {
202 pub all: Option<bool>,
204 #[builder(into)]
206 pub shell: Option<Setting<ShellConfig>>,
207 #[builder(into)]
209 pub read: Option<Setting<ReadConfig>>,
210 pub write: Option<bool>,
212 pub edit: Option<bool>,
214 #[builder(into)]
216 pub web_fetch: Option<Setting<WebFetchConfig>>,
217 #[builder(into)]
219 pub web_search: Option<WebSearchSetting>,
220 #[builder(into)]
222 pub programmatic_tool_caller: Option<Setting<ProgrammaticToolCallerConfig>>,
223 #[builder(into)]
225 pub subagent: Option<Setting<SubagentConfig>>,
226}
227
228#[derive(Debug, Clone)]
231#[non_exhaustive]
232pub enum BuiltinToolsOption {
233 List(Vec<BuiltinToolName>),
235 Mapping(BuiltinToolsConfig),
237}
238
239impl From<Vec<BuiltinToolName>> for BuiltinToolsOption {
240 fn from(names: Vec<BuiltinToolName>) -> Self {
241 Self::List(names)
242 }
243}
244
245impl From<&[BuiltinToolName]> for BuiltinToolsOption {
246 fn from(names: &[BuiltinToolName]) -> Self {
247 Self::List(names.to_vec())
248 }
249}
250
251impl<const N: usize> From<[BuiltinToolName; N]> for BuiltinToolsOption {
252 fn from(names: [BuiltinToolName; N]) -> Self {
253 Self::List(names.to_vec())
254 }
255}
256
257impl From<BuiltinToolsConfig> for BuiltinToolsOption {
258 fn from(config: BuiltinToolsConfig) -> Self {
259 Self::Mapping(config)
260 }
261}
262
263#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, bon::Builder)]
265pub struct SessionConfig {
266 #[builder(into)]
270 #[serde(skip_serializing_if = "Option::is_none")]
271 pub id: Option<String>,
272 #[builder(into)]
274 #[serde(skip_serializing_if = "Option::is_none")]
275 pub dir: Option<PathBuf>,
276}
277
278#[derive(Clone, Default, bon::Builder)]
282pub struct MemoryConfig {
283 #[builder(into)]
285 pub dir: Option<PathBuf>,
286 #[builder(default)]
290 pub stores: Vec<Arc<dyn MemoryStore>>,
291}
292
293impl fmt::Debug for MemoryConfig {
294 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
295 f.debug_struct("MemoryConfig")
296 .field("dir", &self.dir)
297 .field(
298 "stores",
299 &self.stores.iter().map(|store| store.name()).collect::<Vec<_>>(),
300 )
301 .finish()
302 }
303}
304
305pub type ContextManagerOption = ContextManagerStrategy;
309
310pub(crate) fn json_string(value: &str) -> String {
312 serde_json::Value::String(value.to_owned()).to_string()
313}
314
315#[cfg(test)]
316mod tests {
317 use super::*;
318
319 #[test]
320 fn rejects_an_effort_outside_the_enum_by_name() {
321 let error = "bogus".parse::<Effort>().unwrap_err();
322 assert_eq!(
323 error.to_string(),
324 "Effort \"bogus\" is not valid. Supported: auto, off, minimal, low, medium, high, xhigh, max."
325 );
326 assert_eq!("xhigh".parse::<Effort>().unwrap(), Effort::Xhigh);
327 }
328}
329
330#[derive(Debug, Clone)]
339#[non_exhaustive]
340pub enum McpServersOption {
341 Path(PathBuf),
343 Json(strands::types::JsonObject),
345 #[cfg(feature = "mcp")]
347 Source(strands::mcp::McpServersSource),
348}
349
350impl From<&str> for McpServersOption {
351 fn from(path: &str) -> Self {
352 Self::Path(PathBuf::from(path))
353 }
354}
355
356impl From<String> for McpServersOption {
357 fn from(path: String) -> Self {
358 Self::Path(PathBuf::from(path))
359 }
360}
361
362impl From<PathBuf> for McpServersOption {
363 fn from(path: PathBuf) -> Self {
364 Self::Path(path)
365 }
366}
367
368impl From<strands::types::JsonObject> for McpServersOption {
369 fn from(servers: strands::types::JsonObject) -> Self {
370 Self::Json(servers)
371 }
372}
373
374#[cfg(feature = "mcp")]
375impl From<strands::mcp::McpServersSource> for McpServersOption {
376 fn from(source: strands::mcp::McpServersSource) -> Self {
377 Self::Source(source)
378 }
379}
380
381#[cfg(feature = "mcp")]
382impl McpServersOption {
383 pub(crate) fn into_source(self) -> strands::mcp::McpServersSource {
384 match self {
385 Self::Path(path) => strands::mcp::McpServersSource::Path(path),
386 Self::Json(servers) => strands::mcp::McpServersSource::Json(servers),
387 Self::Source(source) => source,
388 }
389 }
390}