1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//! Agent builder implementation.
use std::path::PathBuf;
use std::sync::Arc;
use crate::approval::ApproveMode;
use crate::compress::CompressionConfig;
use crate::constants::QUICK_ACTION_MAX_TOKENS;
use crate::event::AgentEvent;
use crate::prompt::PromptProfile;
use crate::providers::Provider;
use crate::skills::Skill;
use crate::tools::Tool;
use crate::tools::toolproxy::{ProxyToolDef, ProxyToolExecutor};
use super::types::{Agent, AgentBuilder};
impl AgentBuilder {
pub fn new(provider: Box<dyn Provider>) -> Self {
Self {
provider,
model_name: "unknown".to_string(),
tools: Vec::new(),
max_tokens: QUICK_ACTION_MAX_TOKENS,
context_size_override: None,
think: false,
compression_config: CompressionConfig::default(),
profile: PromptProfile::Default,
skills: Vec::new(),
project_overview: None,
memory_summary: None,
project_path: None,
event_tx: None,
pending_input_rx: None,
approve_mode: ApproveMode::Auto,
proxy_tool_defs: Vec::new(),
proxy_executor: None,
mcp_registry: None,
lsp_registry: None,
}
}
pub fn model_name(mut self, name: impl Into<String>) -> Self {
self.model_name = name.into();
self
}
pub fn max_tokens(mut self, tokens: u32) -> Self {
self.max_tokens = tokens;
self
}
/// Override provider-inferred context window size.
pub fn context_size(mut self, context_size: Option<u32>) -> Self {
self.context_size_override = context_size;
self
}
/// Set compression config
pub fn compression_config(mut self, config: CompressionConfig) -> Self {
self.compression_config = config;
self
}
pub fn think(mut self, enabled: bool) -> Self {
self.think = enabled;
self
}
pub fn approve_mode(mut self, mode: ApproveMode) -> Self {
self.approve_mode = mode;
self
}
pub fn tool(mut self, tool: Arc<dyn Tool>) -> Self {
self.tools.push(tool);
self
}
/// Add multiple tools
pub fn tools(mut self, tools: Vec<Box<dyn Tool>>) -> Self {
self.tools.extend(tools.into_iter().map(Arc::from));
self
}
/// Add multiple tools with provider support
pub fn tools_with_provider(mut self, tools: Vec<Box<dyn Tool>>) -> Self {
self.tools.extend(tools.into_iter().map(Arc::from));
self
}
/// Set external event sender for streaming events
pub fn event_tx(mut self, tx: tokio::sync::mpsc::Sender<AgentEvent>) -> Self {
self.event_tx = Some(tx);
self
}
/// Add skills
pub fn skills(mut self, skills: Vec<Skill>) -> Self {
self.skills = skills;
self
}
/// Set prompt profile
pub fn profile(mut self, profile: PromptProfile) -> Self {
self.profile = profile;
self
}
/// Set project overview
pub fn overview(mut self, overview: impl Into<String>) -> Self {
self.project_overview = Some(overview.into());
self
}
/// Set memory summary
pub fn memory(mut self, summary: impl Into<String>) -> Self {
self.memory_summary = Some(summary.into());
self
}
/// Set system prompt directly (overrides auto-generated prompt).
/// Use with caution - this bypasses the normal prompt building process.
pub fn system_prompt(mut self, prompt: String) -> Self {
// Store as project_overview to be used in prompt building
// This is a workaround since system_prompt is built from context
self.project_overview = Some(prompt);
self
}
/// Set project path (for dynamic tool injection like CodeGraph)
pub fn project_path(mut self, path: PathBuf) -> Self {
self.project_path = Some(path);
self
}
/// 设置代理工具执行器
///
/// # Example
/// ```ignore
/// use std::sync::Arc;
/// use serde_json::json;
/// use matrixcode_core::tools::toolproxy::{ProxyToolExecutor, ProxyToolDef};
///
/// let executor = Arc::new(MyProxyExecutor);
/// let tool_def = ProxyToolDef::new("image_search", "搜索图片", json!({...}))
/// .with_priority(true);
///
/// builder.proxy_executor(executor, vec![tool_def])
/// ```
pub fn proxy_executor(
mut self,
executor: Arc<dyn ProxyToolExecutor>,
tool_defs: Vec<ProxyToolDef>,
) -> Self {
self.proxy_executor = Some(executor);
self.proxy_tool_defs = tool_defs;
self
}
pub fn build(self) -> Agent {
Agent::new(self)
}
/// 设置 MCP 工具注册表
///
/// # Example
/// ```ignore
/// use std::sync::Arc;
/// use matrixcode_core::mcp::McpToolRegistry;
///
/// let registry = Arc::new(tokio::sync::RwLock::new(McpToolRegistry::new()));
/// builder.mcp_registry(registry)
/// ```
pub fn mcp_registry(
mut self,
registry: Arc<tokio::sync::RwLock<crate::mcp::McpToolRegistry>>,
) -> Self {
self.mcp_registry = Some(registry);
self
}
/// 设置 LSP 客户端注册表
///
/// # Example
/// ```ignore
/// use std::sync::Arc;
/// use matrixcode_core::lsp::LspClientRegistry;
///
/// let registry = Arc::new(LspClientRegistry::new());
/// // 启动 LSP 服务器
/// registry.register(&config, &project_root).await?;
/// builder.lsp_registry(registry)
/// ```
pub fn lsp_registry(mut self, registry: Arc<crate::lsp::LspClientRegistry>) -> Self {
self.lsp_registry = Some(registry);
self
}
/// 设置实时追加消息接收器
///
/// 允许在 Agent 处理过程中接收新消息,实现实时追加功能。
///
/// # Example
/// ```ignore
/// let (pending_tx, pending_rx) = tokio::sync::mpsc::channel::<String>(100);
/// builder.pending_input_rx(pending_rx)
/// ```
pub fn pending_input_rx(mut self, rx: tokio::sync::mpsc::Receiver<String>) -> Self {
self.pending_input_rx = Some(rx);
self
}
}