mojentic/llm/tools/ephemeral_task_manager/
append_task.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 appending a new task to the end of the ephemeral task manager list
9#[derive(Clone)]
10pub struct AppendTaskTool {
11    task_list: Arc<Mutex<TaskList>>,
12}
13
14impl AppendTaskTool {
15    /// Creates a new AppendTaskTool with a shared task list
16    pub fn new(task_list: Arc<Mutex<TaskList>>) -> Self {
17        Self { task_list }
18    }
19}
20
21impl LlmTool for AppendTaskTool {
22    fn run(&self, args: &HashMap<String, Value>) -> Result<Value> {
23        let description =
24            args.get("description").and_then(|v| v.as_str()).unwrap_or("").to_string();
25
26        let mut task_list = self.task_list.lock().unwrap();
27        let task = task_list.append_task(description);
28
29        Ok(json!({
30            "id": task.id,
31            "description": task.description,
32            "status": task.status.as_str(),
33            "summary": format!("Task '{}' appended successfully", task.id)
34        }))
35    }
36
37    fn descriptor(&self) -> ToolDescriptor {
38        ToolDescriptor {
39            r#type: "function".to_string(),
40            function: FunctionDescriptor {
41                name: "append_task".to_string(),
42                description: "Append a new task to the end of the task list with a description. The task will start with 'pending' status.".to_string(),
43                parameters: json!({
44                    "type": "object",
45                    "properties": {
46                        "description": {
47                            "type": "string",
48                            "description": "The description of the task"
49                        }
50                    },
51                    "required": ["description"]
52                }),
53            },
54        }
55    }
56    fn clone_box(&self) -> Box<dyn LlmTool> {
57        Box::new(self.clone())
58    }
59}