Skip to main content

matrixcode_core/agent/
builder.rs

1//! Agent builder implementation.
2
3use std::sync::Arc;
4
5use crate::approval::ApproveMode;
6use crate::event::AgentEvent;
7use crate::prompt::PromptProfile;
8use crate::providers::Provider;
9use crate::skills::Skill;
10use crate::tools::Tool;
11use crate::tools::toolproxy::{ProxyToolExecutor, ProxyToolDef};
12
13use super::types::{Agent, AgentBuilder};
14
15impl AgentBuilder {
16    pub fn new(provider: Box<dyn Provider>) -> Self {
17        Self {
18            provider,
19            model_name: "unknown".to_string(),
20            tools: Vec::new(),
21            system_prompt: "You are a helpful AI coding assistant.".to_string(),
22            max_tokens: 4096,
23            think: false,
24            approve_mode: ApproveMode::Ask,
25            event_tx: None,
26            skills: Vec::new(),
27            profile: PromptProfile::Default,
28            project_overview: None,
29            memory_summary: None,
30            proxy_tool_defs: Vec::new(),
31            proxy_executor: None,
32        }
33    }
34
35    pub fn system_prompt(mut self, prompt: impl Into<String>) -> Self {
36        self.system_prompt = prompt.into();
37        self
38    }
39
40    pub fn model_name(mut self, name: impl Into<String>) -> Self {
41        self.model_name = name.into();
42        self
43    }
44
45    pub fn max_tokens(mut self, tokens: u32) -> Self {
46        self.max_tokens = tokens;
47        self
48    }
49
50    pub fn think(mut self, enabled: bool) -> Self {
51        self.think = enabled;
52        self
53    }
54
55    pub fn approve_mode(mut self, mode: ApproveMode) -> Self {
56        self.approve_mode = mode;
57        self
58    }
59
60    pub fn tool(mut self, tool: Arc<dyn Tool>) -> Self {
61        self.tools.push(tool);
62        self
63    }
64
65    /// Add multiple tools
66    pub fn tools(mut self, tools: Vec<Box<dyn Tool>>) -> Self {
67        self.tools.extend(tools.into_iter().map(Arc::from));
68        self
69    }
70
71    /// Add multiple tools with provider support
72    pub fn tools_with_provider(mut self, tools: Vec<Box<dyn Tool>>) -> Self {
73        self.tools.extend(tools.into_iter().map(Arc::from));
74        self
75    }
76
77    /// Set external event sender for streaming events
78    pub fn event_tx(mut self, tx: tokio::sync::mpsc::Sender<AgentEvent>) -> Self {
79        self.event_tx = Some(tx);
80        self
81    }
82
83    /// Add skills
84    pub fn skills(mut self, skills: Vec<Skill>) -> Self {
85        self.skills = skills;
86        self
87    }
88
89    /// Set prompt profile
90    pub fn profile(mut self, profile: PromptProfile) -> Self {
91        self.profile = profile;
92        self
93    }
94
95    /// Set project overview
96    pub fn overview(mut self, overview: impl Into<String>) -> Self {
97        self.project_overview = Some(overview.into());
98        self
99    }
100
101    /// Set memory summary
102    pub fn memory(mut self, summary: impl Into<String>) -> Self {
103        self.memory_summary = Some(summary.into());
104        self
105    }
106
107    /// 设置代理工具执行器
108    ///
109    /// # Example
110    /// ```rust
111    /// let executor = Arc::new(MyProxyExecutor);
112    /// let tool_def = ProxyToolDef::new("image_search", "搜索图片", json!({...}))
113    ///     .with_priority(true);
114    ///
115    /// builder.proxy_executor(executor, vec![tool_def])
116    /// ```
117    pub fn proxy_executor(mut self, executor: Arc<dyn ProxyToolExecutor>, tool_defs: Vec<ProxyToolDef>) -> Self {
118        self.proxy_executor = Some(executor);
119        self.proxy_tool_defs = tool_defs;
120        self
121    }
122
123    pub fn build(self) -> Agent {
124        Agent::new(self)
125    }
126}