Skip to main content

j_agent/tools/todo/
todo_read_tool.rs

1use super::todo_manager::TodoManager;
2use crate::tools::{PlanDecision, Tool, ToolResult, schema_to_tool_params};
3use schemars::JsonSchema;
4use serde::Deserialize;
5use serde_json::Value;
6use std::borrow::Cow;
7use std::sync::{Arc, atomic::AtomicBool};
8
9/// TodoReadTool 参数(无参数)
10#[derive(Deserialize, JsonSchema)]
11struct TodoReadParams {}
12
13/// 待办事项读取工具,用于查看当前所有待办项的状态
14#[derive(Debug)]
15pub struct TodoReadTool {
16    /// 待办事项管理器实例
17    pub manager: Arc<TodoManager>,
18}
19
20impl TodoReadTool {
21    pub const NAME: &'static str = "TodoRead";
22}
23
24impl Tool for TodoReadTool {
25    fn name(&self) -> &str {
26        Self::NAME
27    }
28
29    fn description(&self) -> Cow<'_, str> {
30        "Read and list all current todo items. Returns the full todo list with id, content, and status for each item. Use this to check progress or review the current state of your task list.".into()
31    }
32
33    fn parameters_schema(&self) -> Value {
34        schema_to_tool_params::<TodoReadParams>()
35    }
36
37    fn execute(&self, _arguments: &str, _cancelled: &Arc<AtomicBool>) -> ToolResult {
38        let items = self.manager.list_todos();
39        if items.is_empty() {
40            return ToolResult {
41                output: "No todo items found. Use TodoWrite to create new items.".to_string(),
42                is_error: false,
43                images: vec![],
44                plan_decision: PlanDecision::None,
45            };
46        }
47        ToolResult {
48            output: serde_json::to_string_pretty(&items).unwrap_or_default(),
49            is_error: false,
50            images: vec![],
51            plan_decision: PlanDecision::None,
52        }
53    }
54}