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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
//! Agent tool execution implementation.
use anyhow::Result;
use std::sync::atomic::Ordering;
use tokio::time::{Duration, sleep};
use crate::approval::{ApproveMode, needs_approval};
use crate::event::{AgentEvent, EventData, EventType};
use crate::providers::{ChatResponse, ContentBlock, Message, MessageContent, Role};
use crate::truncate::truncate_with_suffix;
use super::helpers::extract_tool_detail;
use super::types::Agent;
/// Maximum tool result size (50KB) - prevents oversized responses
const MAX_TOOL_RESULT_SIZE: usize = 50_000;
/// Wait for cancellation signal, checking periodically.
async fn wait_for_cancel(token: &crate::cancel::CancellationToken) {
while !token.is_cancelled() {
sleep(Duration::from_millis(50)).await;
}
}
impl Agent {
/// Process response and handle tool_use
pub(crate) async fn process_response(&mut self, response: &ChatResponse) -> Result<bool> {
let mut has_tool_use = false;
let mut assistant_content: Vec<ContentBlock> = Vec::new();
let mut tool_results: Vec<Message> = Vec::new();
for block in &response.content {
match block {
ContentBlock::Text { text } => {
assistant_content.push(ContentBlock::Text { text: text.clone() });
}
ContentBlock::Thinking {
thinking,
signature,
} => {
assistant_content.push(ContentBlock::Thinking {
thinking: thinking.clone(),
signature: signature.clone(),
});
}
ContentBlock::ToolUse { id, name, input } => {
if let Some(token) = &self.cancel_token
&& token.is_cancelled()
{
return Err(anyhow::anyhow!("Operation cancelled"));
}
has_tool_use = true;
log::info!("Agent: starting tool '{}' with id {}", name, id);
let result = self.execute_tool(name, input.clone()).await;
log::info!("Agent: tool '{}' completed", name);
let (content, is_error) = match result {
Ok(output) => (output, false),
Err(e) => (e.to_string(), true),
};
// Truncate oversized tool results to prevent API issues
let content = if content.len() > MAX_TOOL_RESULT_SIZE {
let truncated = truncate_with_suffix(&content, MAX_TOOL_RESULT_SIZE);
log::warn!(
"Tool '{}' result truncated: {} -> {} bytes",
name,
content.len(),
truncated.len()
);
format!("{}\n\n⚠️ Output truncated ({} bytes total)", truncated, content.len())
} else {
content
};
self.emit(AgentEvent::tool_result(
id.clone(),
name.clone(),
extract_tool_detail(name, input),
content.clone(),
is_error,
))?;
assistant_content.push(ContentBlock::ToolUse {
id: id.clone(),
name: name.clone(),
input: input.clone(),
});
tool_results.push(Message {
role: Role::User,
content: MessageContent::Blocks(vec![ContentBlock::ToolResult {
tool_use_id: id.clone(),
content: format!(
"{}: {}",
if is_error { "Error" } else { "Result" },
content
),
}]),
});
}
_ => {}
}
}
if !assistant_content.is_empty() {
self.messages.push(Message {
role: Role::Assistant,
content: MessageContent::Blocks(assistant_content),
});
}
for msg in tool_results {
self.messages.push(msg);
}
Ok(has_tool_use)
}
/// Execute a tool
pub(crate) async fn execute_tool(
&mut self,
name: &str,
input: serde_json::Value,
) -> Result<String> {
// 先检查是否是代理工具
if self.proxy_tool_defs.iter().any(|t| t.definition.name == name) {
log::info!("Executing proxy tool: {}", name);
return self.handle_proxy_tool(name, input).await;
}
let tool = self.tools.iter().find(|t| t.definition().name == name);
if let Some(tool) = tool {
let current_mode = ApproveMode::from_u8(self.approve_mode.load(Ordering::Relaxed));
log::debug!(
"Tool '{}' approval check: mode={}, risk={}, needs_approval={}",
name,
current_mode,
tool.risk_level(),
needs_approval(current_mode, tool.risk_level())
);
if needs_approval(current_mode, tool.risk_level()) {
if self.ask_rx.is_some() {
let detail = match name {
"bash" => format!("Command: {}", input["command"].as_str().unwrap_or("?")),
"write" => format!("File: {}", input["path"].as_str().unwrap_or("?")),
"edit" | "multi_edit" => {
format!("File: {}", input["path"].as_str().unwrap_or("?"))
}
_ => format!("Tool: {}", name),
};
let question = format!(
"⚠️ Tool '{}' requires approval (risk: {})\n{}\n\nAllow? (y/n)",
name,
tool.risk_level(),
detail
);
self.emit(AgentEvent::with_data(
EventType::AskQuestion,
EventData::AskQuestion {
question,
options: None,
},
))?;
if let Some(rx) = &mut self.ask_rx {
// Wait for approval with cancellation check
let answer = if let Some(token) = &self.cancel_token {
tokio::select! {
result = rx.recv() => result,
_ = wait_for_cancel(token) => {
return Err(anyhow::anyhow!("Operation cancelled"));
}
}
} else {
rx.recv().await
};
match answer {
Some(answer) => {
let answer_lower = answer.trim().to_lowercase();
if matches!(
answer_lower.as_str(),
"a" | "abort" | "q" | "quit" | "stop"
) {
self.emit(AgentEvent::with_data(
EventType::Error,
EventData::Error {
message: "Aborted by user".into(),
code: None,
source: None,
},
))?;
return Err(anyhow::anyhow!("Session aborted by user"));
}
let approved = matches!(
answer_lower.as_str(),
"y" | "yes" | "ok" | "approve" | ""
);
if !approved {
return Err(anyhow::anyhow!(
"Tool '{}' rejected by user (answer: '{}')",
name,
answer_lower
));
}
}
None => {
return Err(anyhow::anyhow!("Approval channel closed"));
}
}
}
} else {
return Err(anyhow::anyhow!(
"Tool '{}' requires manual approval (risk: {}). Use --approve-mode auto to auto-approve.",
name,
tool.risk_level()
));
}
}
// Special handling for "ask" tool in TUI mode
if name == "ask" && self.ask_rx.is_some() {
if input
.get("questions")
.and_then(|q| q.as_array())
.filter(|a| !a.is_empty())
.is_some()
{
let intro = input.get("intro").and_then(|s| s.as_str()).unwrap_or("");
let questions = input.get("questions").cloned();
let options = serde_json::json!({
"questions": questions
});
self.emit(AgentEvent::with_data(
EventType::AskQuestion,
EventData::AskQuestion {
question: intro.to_string(),
options: Some(options),
},
))?;
if let Some(rx) = &mut self.ask_rx {
// Wait for answer with cancellation check
let answer = if let Some(token) = &self.cancel_token {
tokio::select! {
result = rx.recv() => result,
_ = wait_for_cancel(token) => {
return Err(anyhow::anyhow!("Operation cancelled"));
}
}
} else {
rx.recv().await
};
match answer {
Some(answer) => return Ok(answer),
None => return Err(anyhow::anyhow!("Ask channel closed")),
}
}
} else {
let question = input["question"].as_str().unwrap_or("").to_string();
let options = input.get("options").cloned();
self.emit(AgentEvent::with_data(
EventType::AskQuestion,
EventData::AskQuestion { question, options },
))?;
if let Some(rx) = &mut self.ask_rx {
// Wait for answer with cancellation check
let answer = if let Some(token) = &self.cancel_token {
tokio::select! {
result = rx.recv() => result,
_ = wait_for_cancel(token) => {
return Err(anyhow::anyhow!("Operation cancelled"));
}
}
} else {
rx.recv().await
};
match answer {
Some(answer) => return Ok(answer),
None => return Err(anyhow::anyhow!("Ask channel closed")),
}
}
}
}
self.emit(AgentEvent::progress(format!("Executing: {}", name), None))?;
tool.execute(input).await
} else {
Err(anyhow::anyhow!("Tool '{}' not found", name))
}
}
/// 处理代理工具 - 直接调用 executor 执行
async fn handle_proxy_tool(&mut self, name: &str, input: serde_json::Value) -> Result<String> {
// 检查是否有执行器
if let Some(executor) = &self.proxy_executor {
log::info!("Proxy tool: calling executor for {}", name);
// 获取超时时间
let timeout_ms = self.proxy_tool_defs
.iter()
.find(|t| t.definition.name == name)
.map(|t| t.timeout_ms)
.unwrap_or(30000);
// 执行代理工具(带超时和取消)
let result = if let Some(token) = &self.cancel_token {
tokio::select! {
result = tokio::time::timeout(
tokio::time::Duration::from_millis(timeout_ms),
executor.exec(name, input.clone())
) => result,
_ = wait_for_cancel(token) => {
return Err(anyhow::anyhow!("Operation cancelled"));
}
}
} else {
tokio::time::timeout(
tokio::time::Duration::from_millis(timeout_ms),
executor.exec(name, input.clone())
).await
};
match result {
Ok(inner_result) => {
log::info!("Proxy tool {} completed", name);
inner_result
}
Err(_) => Err(anyhow::anyhow!(
"Proxy tool '{}' timed out after {}ms",
name, timeout_ms
)),
}
} else {
Err(anyhow::anyhow!(
"Proxy tool '{}' requested but no executor configured. \
Use agent.set_proxy_executor() to configure.",
name
))
}
}
}