1use std::fmt::Write;
2use std::sync::Mutex;
3
4use async_trait::async_trait;
5use serde_json::{Value, json};
6
7use crate::engine::EventBus;
8use crate::tool::{FrameworkTool, Tool, ToolContext, ToolOutput};
9use crate::types::{AgentResult, RuntimeEvent, UpdatePlanArgs};
10
11pub struct UpdatePlanTool {
22 event_bus: Mutex<Option<EventBus>>,
23 last_objective: Mutex<Option<String>>,
24 custom_description: Option<String>,
25}
26
27fn normalize_step_text(raw: &str) -> String {
35 let text = raw.trim();
36
37 let text = if let Some(rest) = text
39 .strip_prefix("Step")
40 .or_else(|| text.strip_prefix("step"))
41 {
42 let rest = rest.trim_start_matches(|c: char| c.is_ascii_digit() || c == ' ');
43 rest.trim_start_matches(|c: char| c == ':' || c == '.' || c == ')' || c == ' ')
44 } else {
45 text
46 };
47
48 let text = if let Some(rest) = text.strip_prefix('第') {
50 let rest = rest.trim_start_matches(|c: char| {
52 c.is_ascii_digit()
53 || c == ' '
54 || matches!(
55 c,
56 '一' | '二' | '三' | '四' | '五' | '六' | '七' | '八' | '九' | '十'
57 )
58 });
59 rest.strip_prefix('步')
61 .map(|r| {
62 r.trim_start_matches(|c: char| matches!(c, ':' | ':' | '、' | '.' | ')' | ' '))
63 })
64 .unwrap_or(text)
65 } else {
66 text
67 };
68
69 let text = text.trim_start_matches(|c: char| {
71 c.is_ascii_digit() || matches!(c, '.' | ')' | '(' | '、' | ' ' | '-' | '/')
72 });
73
74 let text = if text.chars().count() > 60 {
76 let truncated: String = text.chars().take(57).collect();
77 format!("{truncated}...")
78 } else {
79 text.to_string()
80 };
81
82 let trimmed = text.trim();
84 if trimmed.is_empty() {
85 raw.trim().to_string()
86 } else if trimmed.len() < text.len() {
87 trimmed.to_string()
88 } else {
89 text
90 }
91}
92
93impl UpdatePlanTool {
94 pub fn new() -> Self {
95 Self {
96 event_bus: Mutex::new(None),
97 last_objective: Mutex::new(None),
98 custom_description: None,
99 }
100 }
101
102 pub fn with_description(mut self, desc: String) -> Self {
108 self.custom_description = Some(desc);
109 self
110 }
111}
112
113impl Default for UpdatePlanTool {
114 fn default() -> Self {
115 Self::new()
116 }
117}
118
119impl FrameworkTool for UpdatePlanTool {
120 fn set_event_bus(&self, event_bus: EventBus) {
121 *self.event_bus.lock().unwrap() = Some(event_bus);
122 }
123}
124
125#[async_trait]
126impl Tool for UpdatePlanTool {
127 fn name(&self) -> &'static str {
128 "update_plan"
129 }
130
131 fn definition(&self) -> Value {
132 let description = self.custom_description.as_deref().unwrap_or(
133 "Record and display a structured plan / checklist to track progress on a complex task.\n\n\
134 Use this to show the user what steps you plan to take and update step statuses as you go.\n\n\
135 Rules:\n\
136 - Always include the user's goal as `objective`\n\
137 - Plan must have at least one step\n\
138 - At most one step may be in_progress at a time\n\
139 - Step descriptions should be concise and human-readable\n\
140 - Call this again whenever step statuses change\n\
141 - Skip this for simple/trivial tasks\n\n\
142 When creating a plan, follow these principles:\n\
143 1. 探查先行 — 第一步先确认相关组件和依赖是否就绪\n\
144 2. 依赖排序 — 被依赖的先执行,独立步骤可并行但不强制\n\
145 3. 每步闭环 — 一步做完可独立验证结果,不等下步才知道成败\n\
146 4. 标注风险 — 涉及 rm、kill、restart、改配置文件时注明\n\
147 5. 粒度适中 — 不过细也不过大,每步是独立可验证的最小逻辑单元\n\
148 6. 收敛止步 — 步骤过多说明任务需要拆分或先讨论再定"
149 );
150 json!({
151 "type": "function",
152 "function": {
153 "name": "update_plan",
154 "description": description,
155 "parameters": {
156 "type": "object",
157 "properties": {
158 "objective": {
159 "type": "string",
160 "description": "One-sentence summary of the user's goal. Example: \"安装 Casdoor 身份认证系统\". Must reflect the user's original intent, not just the current sub-task. Optional on subsequent calls — the tool remembers the last objective."
161 },
162 "explanation": {
163 "type": "string",
164 "description": "Optional explanation of why the plan is being created or changed."
165 },
166 "plan": {
167 "type": "array",
168 "description": "The complete plan checklist. Each item has a step description and status.",
169 "items": {
170 "type": "object",
171 "properties": {
172 "step": {
173 "type": "string",
174 "description": "Short description of this step (5-7 words). Example: '安装 Docker 引擎'"
175 },
176 "status": {
177 "type": "string",
178 "enum": ["pending", "in_progress", "completed"],
179 "description": "Current status of this step."
180 }
181 },
182 "required": ["step", "status"],
183 "additionalProperties": false
184 }
185 }
186 },
187 "required": ["plan"],
188 "additionalProperties": false
189 }
190 }
191 })
192 }
193
194 fn as_framework_tool(&self) -> Option<&dyn FrameworkTool> {
195 Some(self)
196 }
197
198 async fn call(&self, args: &Value, _ctx: &ToolContext) -> AgentResult<ToolOutput> {
199 let plan_args: UpdatePlanArgs = serde_json::from_value(args.clone()).map_err(|e| {
200 crate::types::AgentError::ToolArgsInvalid {
201 name: "update_plan".to_string(),
202 raw: format!("deserialization error: {e}"),
203 }
204 })?;
205
206 if let Err(validation_err) = plan_args.validate() {
208 return Err(crate::types::AgentError::ToolArgsInvalid {
209 name: "update_plan".to_string(),
210 raw: validation_err,
211 });
212 }
213
214 let objective = match plan_args.objective {
216 Some(ref obj) => {
217 *self.last_objective.lock().unwrap() = Some(obj.clone());
218 obj.clone()
219 }
220 None => self
221 .last_objective
222 .lock()
223 .unwrap()
224 .clone()
225 .unwrap_or_else(|| "(no objective specified)".to_string()),
226 };
227
228 let normalized_plan: Vec<crate::types::PlanItem> = plan_args
230 .plan
231 .into_iter()
232 .map(|item| crate::types::PlanItem {
233 step: normalize_step_text(&item.step),
234 status: item.status,
235 })
236 .collect();
237
238 let total = normalized_plan.len();
240 let completed = normalized_plan
241 .iter()
242 .filter(|item| item.status == crate::types::PlanStepStatus::Completed)
243 .count();
244 let in_progress = normalized_plan
245 .iter()
246 .filter(|item| item.status == crate::types::PlanStepStatus::InProgress)
247 .count();
248
249 let raw = Some(serde_json::to_value(&normalized_plan).unwrap_or_default());
252
253 let mut summary = format!("📋 {}: {}/{} steps completed", objective, completed, total);
254 if in_progress > 0 {
255 let current = normalized_plan
256 .iter()
257 .find(|item| item.status == crate::types::PlanStepStatus::InProgress);
258 if let Some(item) = current {
259 write!(summary, ". Current: \"{}\"", item.step).unwrap();
260 }
261 }
262 if total == completed {
263 summary = format!("📋 {} — all steps completed!", objective);
264 }
265
266 {
268 let guard = self.event_bus.lock().unwrap();
269 if let Some(ref event_bus) = *guard {
270 event_bus.emit(RuntimeEvent::PlanUpdated {
271 session_id: _ctx.session_id.clone(),
272 objective: objective.clone(),
273 explanation: plan_args.explanation.clone(),
274 plan: normalized_plan,
275 });
276 }
277 }
278
279 Ok(ToolOutput {
280 summary,
281 raw,
282 control_flow: crate::tool::ToolControlFlow::Continue,
283 truncation: None,
284 })
285 }
286}
287
288#[cfg(test)]
289mod tests {
290 use super::*;
291 use crate::types::{PlanItem, PlanStepStatus};
292
293 #[test]
294 fn test_update_plan_args_validation() {
295 let args = UpdatePlanArgs {
297 objective: Some("安装 Docker".into()),
298 explanation: None,
299 plan: vec![
300 PlanItem {
301 step: "Step 1".into(),
302 status: PlanStepStatus::Completed,
303 },
304 PlanItem {
305 step: "Step 2".into(),
306 status: PlanStepStatus::InProgress,
307 },
308 PlanItem {
309 step: "Step 3".into(),
310 status: PlanStepStatus::Pending,
311 },
312 ],
313 };
314 assert!(args.validate().is_ok());
315
316 let args = UpdatePlanArgs {
318 objective: None,
319 explanation: None,
320 plan: vec![PlanItem {
321 step: "Step 1".into(),
322 status: PlanStepStatus::Pending,
323 }],
324 };
325 assert!(args.validate().is_ok());
326
327 let args = UpdatePlanArgs {
329 objective: Some("".into()),
330 explanation: None,
331 plan: vec![PlanItem {
332 step: "Step 1".into(),
333 status: PlanStepStatus::Pending,
334 }],
335 };
336 assert!(args.validate().is_err());
337
338 let args = UpdatePlanArgs {
340 objective: Some("安装 Docker".into()),
341 explanation: None,
342 plan: vec![],
343 };
344 assert!(args.validate().is_err());
345
346 let args = UpdatePlanArgs {
348 objective: Some("安装 Docker".into()),
349 explanation: None,
350 plan: vec![
351 PlanItem {
352 step: "Step 1".into(),
353 status: PlanStepStatus::InProgress,
354 },
355 PlanItem {
356 step: "Step 2".into(),
357 status: PlanStepStatus::InProgress,
358 },
359 ],
360 };
361 assert!(args.validate().is_err());
362
363 let args = UpdatePlanArgs {
365 objective: Some("安装 Docker".into()),
366 explanation: None,
367 plan: vec![PlanItem {
368 step: " ".into(),
369 status: PlanStepStatus::Pending,
370 }],
371 };
372 assert!(args.validate().is_err());
373 }
374
375 #[test]
376 fn test_normalize_step_text() {
377 assert_eq!(normalize_step_text("1. 安装 Docker"), "安装 Docker");
379 assert_eq!(normalize_step_text("2) 添加 GPG 密钥"), "添加 GPG 密钥");
380 assert_eq!(
381 normalize_step_text("(3) 更新 APT 包列表"),
382 "更新 APT 包列表"
383 );
384 assert_eq!(normalize_step_text("Step 1: 安装 Docker"), "安装 Docker");
385 assert_eq!(normalize_step_text("step 2: 更新包列表"), "更新包列表");
386 assert_eq!(normalize_step_text("1、配置仓库"), "配置仓库");
387
388 assert_eq!(normalize_step_text("1-2) Install Docker"), "Install Docker");
390 assert_eq!(normalize_step_text("3/5) Verify config"), "Verify config");
391
392 assert_eq!(normalize_step_text("第一步:安装 Docker"), "安装 Docker");
394 assert_eq!(normalize_step_text("第1步:添加 GPG 密钥"), "添加 GPG 密钥");
395 assert_eq!(normalize_step_text("第 3 步: 更新包列表"), "更新包列表");
396 assert_eq!(normalize_step_text("第二步、配置仓库"), "配置仓库");
397
398 assert_eq!(normalize_step_text("第一个任务:安装"), "第一个任务:安装");
400
401 let long = "使用 apt install -y docker-ce docker-ce-cli containerd.io 命令来安装 Docker 引擎以及相关组件";
403 let result = normalize_step_text(long);
404 assert!(result.chars().count() <= 60);
405 assert!(result.ends_with("..."));
406
407 assert_eq!(normalize_step_text("安装 Docker 引擎"), "安装 Docker 引擎");
409
410 assert_eq!(normalize_step_text(" 安装 Docker "), "安装 Docker");
412
413 let result = normalize_step_text("123");
415 assert!(!result.is_empty());
416 }
417}