agents_runtime/agent/
config.rs

1//! Configuration structs and types for Deep Agents
2//!
3//! This module contains all the configuration structures used to build Deep Agents,
4//! including parameter structs that mirror the Python SDK API.
5
6use crate::middleware::{AgentMiddleware, HitlPolicy};
7use agents_core::agent::PlannerHandle;
8use agents_core::persistence::Checkpointer;
9use agents_core::tools::ToolBox;
10use std::collections::{HashMap, HashSet};
11use std::sync::Arc;
12
13/// Parameters for create_deep_agent() that mirror the Python API exactly
14///
15/// This struct matches the Python function signature:
16/// ```python
17/// def create_deep_agent(
18///     tools: Sequence[Union[BaseTool, Callable, dict[str, Any]]] = [],
19///     instructions: str = "",
20///     middleware: Optional[list[AgentMiddleware]] = None,
21///     model: Optional[Union[str, LanguageModelLike]] = None,
22///     subagents: Optional[list[SubAgent | CustomSubAgent]] = None,
23///     context_schema: Optional[Type[Any]] = None,
24///     checkpointer: Optional[Checkpointer] = None,
25///     tool_configs: Optional[dict[str, bool | ToolConfig]] = None,
26/// )
27/// ```
28#[derive(Default)]
29pub struct CreateDeepAgentParams {
30    pub tools: Vec<ToolBox>,
31    pub instructions: String,
32    pub middleware: Vec<Arc<dyn AgentMiddleware>>,
33    pub model: Option<Arc<dyn agents_core::llm::LanguageModel>>,
34    pub subagents: Vec<SubAgentConfig>,
35    pub context_schema: Option<String>,
36    pub checkpointer: Option<Arc<dyn Checkpointer>>,
37    pub tool_configs: HashMap<String, HitlPolicy>,
38}
39
40/// Configuration for building a deep agent instance.
41///
42/// This is the internal configuration used by the builder and runtime.
43pub struct DeepAgentConfig {
44    pub instructions: String,
45    pub planner: Arc<dyn PlannerHandle>,
46    pub tools: Vec<ToolBox>,
47    pub subagent_configs: Vec<SubAgentConfig>,
48    pub summarization: Option<SummarizationConfig>,
49    pub tool_interrupts: HashMap<String, HitlPolicy>,
50    pub builtin_tools: Option<HashSet<String>>,
51    pub auto_general_purpose: bool,
52    pub enable_prompt_caching: bool,
53    pub checkpointer: Option<Arc<dyn Checkpointer>>,
54}
55
56impl DeepAgentConfig {
57    pub fn new(instructions: impl Into<String>, planner: Arc<dyn PlannerHandle>) -> Self {
58        Self {
59            instructions: instructions.into(),
60            planner,
61            tools: Vec::new(),
62            subagent_configs: Vec::new(),
63            summarization: None,
64            tool_interrupts: HashMap::new(),
65            builtin_tools: None,
66            auto_general_purpose: true,
67            enable_prompt_caching: false,
68            checkpointer: None,
69        }
70    }
71
72    pub fn with_tool(mut self, tool: ToolBox) -> Self {
73        self.tools.push(tool);
74        self
75    }
76
77    /// Add a sub-agent configuration
78    pub fn with_subagent_config(mut self, config: SubAgentConfig) -> Self {
79        self.subagent_configs.push(config);
80        self
81    }
82
83    /// Add multiple sub-agent configurations
84    pub fn with_subagent_configs<I>(mut self, configs: I) -> Self
85    where
86        I: IntoIterator<Item = SubAgentConfig>,
87    {
88        self.subagent_configs.extend(configs);
89        self
90    }
91
92    pub fn with_summarization(mut self, config: SummarizationConfig) -> Self {
93        self.summarization = Some(config);
94        self
95    }
96
97    pub fn with_tool_interrupt(mut self, tool_name: impl Into<String>, policy: HitlPolicy) -> Self {
98        self.tool_interrupts.insert(tool_name.into(), policy);
99        self
100    }
101
102    /// Limit which built-in tools are exposed. When omitted, all built-ins are available.
103    /// Built-ins: write_todos, ls, read_file, write_file, edit_file.
104    /// The `task` tool (for subagents) is always available when subagents are registered.
105    pub fn with_builtin_tools<I, S>(mut self, names: I) -> Self
106    where
107        I: IntoIterator<Item = S>,
108        S: Into<String>,
109    {
110        let set: HashSet<String> = names.into_iter().map(|s| s.into()).collect();
111        self.builtin_tools = Some(set);
112        self
113    }
114
115    /// Enable or disable automatic registration of a "general-purpose" subagent.
116    /// Enabled by default; set to false to opt out.
117    pub fn with_auto_general_purpose(mut self, enabled: bool) -> Self {
118        self.auto_general_purpose = enabled;
119        self
120    }
121
122    /// Enable or disable Anthropic prompt caching middleware.
123    /// Disabled by default; set to true to enable caching for better performance.
124    pub fn with_prompt_caching(mut self, enabled: bool) -> Self {
125        self.enable_prompt_caching = enabled;
126        self
127    }
128
129    /// Set the checkpointer for persisting agent state between runs.
130    pub fn with_checkpointer(mut self, checkpointer: Arc<dyn Checkpointer>) -> Self {
131        self.checkpointer = Some(checkpointer);
132        self
133    }
134}
135
136/// Configuration for creating and registering a subagent using a simple, Python-like shape.
137///
138/// Configuration for a sub-agent - a full AI agent with its own LLM, tools, and memory.
139///
140/// A sub-agent is just like the main agent: it has its own system instructions,
141/// tools, LLM, and can maintain its own conversation history. The main agent
142/// delegates tasks to sub-agents via the `task` tool.
143///
144/// ## Required Fields:
145/// - `name`: Unique identifier for this sub-agent
146/// - `description`: What this sub-agent specializes in (shown to main agent)
147/// - `instructions`: System prompt for this sub-agent
148///
149/// ## Optional Fields:
150/// - `model`: LLM to use (defaults to parent agent's model if not specified)
151/// - `tools`: Tools available to this sub-agent (defaults to empty)
152/// - `builtin_tools`: Built-in tools to enable (filesystem, todos, etc.)
153/// - `enable_prompt_caching`: Whether to cache prompts for efficiency
154pub struct SubAgentConfig {
155    // Required fields
156    pub name: String,
157    pub description: String,
158    pub instructions: String,
159
160    // Optional fields - agent configuration
161    pub model: Option<Arc<dyn agents_core::llm::LanguageModel>>,
162    pub tools: Option<Vec<ToolBox>>,
163    pub builtin_tools: Option<HashSet<String>>,
164    pub enable_prompt_caching: bool,
165}
166
167impl SubAgentConfig {
168    /// Create a new sub-agent configuration with required fields only
169    pub fn new(
170        name: impl Into<String>,
171        description: impl Into<String>,
172        instructions: impl Into<String>,
173    ) -> Self {
174        Self {
175            name: name.into(),
176            description: description.into(),
177            instructions: instructions.into(),
178            model: None,
179            tools: None,
180            builtin_tools: None,
181            enable_prompt_caching: false,
182        }
183    }
184
185    /// Set the LLM model for this sub-agent
186    pub fn with_model(mut self, model: Arc<dyn agents_core::llm::LanguageModel>) -> Self {
187        self.model = Some(model);
188        self
189    }
190
191    /// Set the tools for this sub-agent
192    pub fn with_tools(mut self, tools: Vec<ToolBox>) -> Self {
193        self.tools = Some(tools);
194        self
195    }
196
197    /// Enable specific built-in tools (filesystem, todos, etc.)
198    pub fn with_builtin_tools(mut self, tools: HashSet<String>) -> Self {
199        self.builtin_tools = Some(tools);
200        self
201    }
202
203    /// Enable prompt caching for this sub-agent
204    pub fn with_prompt_caching(mut self, enabled: bool) -> Self {
205        self.enable_prompt_caching = enabled;
206        self
207    }
208}
209
210impl IntoIterator for SubAgentConfig {
211    type Item = SubAgentConfig;
212    type IntoIter = std::iter::Once<SubAgentConfig>;
213
214    fn into_iter(self) -> Self::IntoIter {
215        std::iter::once(self)
216    }
217}
218
219/// Configuration for summarization middleware
220#[derive(Clone)]
221pub struct SummarizationConfig {
222    pub messages_to_keep: usize,
223    pub summary_note: String,
224}