mojentic/llm/tools/ephemeral_task_manager/
list_tasks.rs

1use super::task_list::TaskList;
2use crate::error::Result;
3use crate::llm::tools::{FunctionDescriptor, LlmTool, ToolDescriptor};
4use serde_json::{json, Value};
5use std::collections::HashMap;
6use std::sync::{Arc, Mutex};
7
8/// Tool for listing all tasks in the ephemeral task manager
9#[derive(Clone)]
10pub struct ListTasksTool {
11    task_list: Arc<Mutex<TaskList>>,
12}
13
14impl ListTasksTool {
15    /// Creates a new ListTasksTool with a shared task list
16    pub fn new(task_list: Arc<Mutex<TaskList>>) -> Self {
17        Self { task_list }
18    }
19
20    /// Formats a list of tasks as a string
21    fn format_tasks(&self, tasks: &[super::task::Task]) -> String {
22        if tasks.is_empty() {
23            return "No tasks found.".to_string();
24        }
25
26        tasks
27            .iter()
28            .map(|task| format!("{}. {} ({})", task.id, task.description, task.status.as_str()))
29            .collect::<Vec<_>>()
30            .join("\n")
31    }
32}
33
34impl LlmTool for ListTasksTool {
35    fn run(&self, _args: &HashMap<String, Value>) -> Result<Value> {
36        let task_list = self.task_list.lock().unwrap();
37        let tasks = task_list.list_tasks();
38        let task_list_str = self.format_tasks(&tasks);
39
40        Ok(json!({
41            "count": tasks.len(),
42            "tasks": task_list_str,
43            "summary": format!("Found {} tasks\n\n{}", tasks.len(), task_list_str)
44        }))
45    }
46
47    fn descriptor(&self) -> ToolDescriptor {
48        ToolDescriptor {
49            r#type: "function".to_string(),
50            function: FunctionDescriptor {
51                name: "list_tasks".to_string(),
52                description: "List all tasks in the task list.".to_string(),
53                parameters: json!({
54                    "type": "object",
55                    "properties": {}
56                }),
57            },
58        }
59    }
60    fn clone_box(&self) -> Box<dyn LlmTool> {
61        Box::new(self.clone())
62    }
63}