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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
//! Agent tool execution implementation.
use anyhow::Result;
use std::sync::atomic::Ordering;
use tokio::time::{Duration, sleep};
use crate::approval::{ApproveMode, needs_approval, RiskLevel};
use crate::event::{AgentEvent, EventData, EventType};
use crate::providers::{ChatResponse, ContentBlock, Message, MessageContent, Role};
use crate::tools::MustReadFirstError;
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 self.session.is_cancelled() {
return Err(anyhow::anyhow!("Operation cancelled"));
}
has_tool_use = true;
// Emit ToolUseStart event with full input before execution when the
// streaming phase did not already preview the complete input.
// This allows UI to display command details before result arrives.
if !self.state.remove_previewed_tool_input(id) {
self.emit(AgentEvent::tool_use_start(
id.clone(),
name.clone(),
Some(input.clone()),
))?;
}
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.state.add_message(Message {
role: Role::Assistant,
content: MessageContent::Blocks(assistant_content),
});
}
for msg in tool_results {
self.state.add_message(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;
}
// === Read history precondition check ===
// For edit/write tools, check if the file has been read first
if matches!(name, "edit" | "multi_edit" | "write") {
let file_path = input["path"]
.as_str()
.ok_or_else(|| anyhow::anyhow!("missing 'path' parameter for {} tool", name))?;
// Check if file exists (only enforce read-first for existing files)
let file_exists = tokio::fs::try_exists(file_path).await.unwrap_or(false);
if file_exists && !self.state.read_history().has_read(file_path) {
log::warn!(
"Tool '{}' rejected: file '{}' not read in this session",
name,
file_path
);
let error = MustReadFirstError::new(file_path);
return Err(anyhow::anyhow!("{}", error.message()));
}
}
// Find tool and extract info needed for approval check
let tool = self.tools.iter().find(|t| t.definition().name == name);
if tool.is_none() {
return Err(anyhow::anyhow!("Tool '{}' not found", name));
}
let tool = tool.unwrap();
let current_mode = ApproveMode::from_u8(self.approve_mode.load(Ordering::Relaxed));
let tool_risk_level = tool.risk_level();
let needs_approval_flag = needs_approval(current_mode, tool_risk_level);
log::debug!(
"Tool '{}' approval check: mode={}, risk={}, needs_approval={}",
name,
current_mode,
tool_risk_level,
needs_approval_flag
);
// Handle approval if needed (this requires mutable borrow, so we do it before tool.execute)
if needs_approval_flag {
self.handle_tool_approval(name, &input, tool_risk_level).await?;
}
// Handle "ask" tool special case (also requires mutable borrow)
if name == "ask" && self.has_ask_channel() {
return self.handle_ask_tool(&input).await;
}
// Now find tool again and execute (after all mutable borrows are released)
let tool = self.tools.iter().find(|t| t.definition().name == name);
if let Some(tool) = tool {
self.emit(AgentEvent::progress(format!("Executing: {}", name), None))?;
let result = tool.execute(input.clone()).await;
// === Read history post-action ===
// For read tool, mark the file as read on success
if name == "read" && result.is_ok() {
if let Some(file_path) = input["path"].as_str() {
self.state.read_history_mut().mark_read(file_path);
log::info!("File '{}' marked as read in session history", file_path);
}
}
result
} else {
Err(anyhow::anyhow!("Tool '{}' not found", name))
}
}
/// Handle tool approval flow
async fn handle_tool_approval(
&mut self,
name: &str,
input: &serde_json::Value,
tool_risk_level: RiskLevel,
) -> Result<()> {
if !self.has_ask_channel() {
return Err(anyhow::anyhow!(
"Tool '{}' requires manual approval (risk: {}). Use --approve-mode auto to auto-approve.",
name,
tool_risk_level
));
}
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,
},
))?;
// Check cancellation status before getting ask_channel
if self.session.is_cancelled() {
return Err(anyhow::anyhow!("Operation cancelled"));
}
if let Some(rx) = self.ask_channel() {
// Simple receive with cancellation check before
let answer = rx.recv().await;
// Check cancellation after receive
if self.session.is_cancelled() {
return Err(anyhow::anyhow!("Operation cancelled"));
}
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"));
}
}
}
Ok(())
}
/// Handle "ask" tool special case
async fn handle_ask_tool(&mut self, input: &serde_json::Value) -> Result<String> {
// Check cancellation before getting ask_channel
if self.session.is_cancelled() {
return Err(anyhow::anyhow!("Operation cancelled"));
}
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) = self.ask_channel() {
// Simple receive with cancellation check before
let answer = rx.recv().await;
// Check cancellation after receive
if self.session.is_cancelled() {
return Err(anyhow::anyhow!("Operation cancelled"));
}
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) = self.ask_channel() {
// Simple receive with cancellation check before
let answer = rx.recv().await;
// Check cancellation after receive
if self.session.is_cancelled() {
return Err(anyhow::anyhow!("Operation cancelled"));
}
match answer {
Some(answer) => return Ok(answer),
None => return Err(anyhow::anyhow!("Ask channel closed")),
}
}
}
Err(anyhow::anyhow!("Ask channel not available"))
}
/// 处理代理工具 - 直接调用 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.session.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
))
}
}
}