Skip to main content

mojentic/llm/tools/ephemeral_task_manager/
insert_task_after.rs

1use super::task_list::TaskList;
2use crate::error::Result;
3use crate::llm::tools::{FunctionDescriptor, LlmTool, ToolDescriptor};
4use async_trait::async_trait;
5use serde_json::{json, Value};
6use std::collections::HashMap;
7use std::sync::{Arc, Mutex};
8
9/// Tool for inserting a new task after a specific task ID in the ephemeral task manager list
10#[derive(Clone)]
11pub struct InsertTaskAfterTool {
12    task_list: Arc<Mutex<TaskList>>,
13}
14
15impl InsertTaskAfterTool {
16    /// Creates a new InsertTaskAfterTool with a shared task list
17    pub fn new(task_list: Arc<Mutex<TaskList>>) -> Self {
18        Self { task_list }
19    }
20}
21
22#[async_trait]
23impl LlmTool for InsertTaskAfterTool {
24    async fn run(
25        &self,
26        args: &HashMap<String, Value>,
27        _ctx: &crate::llm::tools::ToolRunCtx,
28    ) -> Result<Value> {
29        let existing_task_id =
30            args.get("existing_task_id").and_then(|v| v.as_u64()).unwrap_or(0) as usize;
31
32        let description =
33            args.get("description").and_then(|v| v.as_str()).unwrap_or("").to_string();
34
35        let mut task_list = self.task_list.lock().unwrap();
36        let task = task_list.insert_task_after(existing_task_id, description)?;
37
38        Ok(json!({
39            "id": task.id,
40            "description": task.description,
41            "status": task.status.as_str(),
42            "summary": format!("Task '{}' inserted after task '{}' successfully", task.id, existing_task_id)
43        }))
44    }
45
46    fn descriptor(&self) -> ToolDescriptor {
47        ToolDescriptor {
48            r#type: "function".to_string(),
49            function: FunctionDescriptor {
50                name: "insert_task_after".to_string(),
51                description: "Insert a new task after an existing task in the task list. The task will start with 'pending' status.".to_string(),
52                parameters: json!({
53                    "type": "object",
54                    "properties": {
55                        "existing_task_id": {
56                            "type": "integer",
57                            "description": "The ID of the existing task after which to insert the new task"
58                        },
59                        "description": {
60                            "type": "string",
61                            "description": "The description of the new task"
62                        }
63                    },
64                    "required": ["existing_task_id", "description"]
65                }),
66            },
67        }
68    }
69    fn clone_box(&self) -> Box<dyn LlmTool> {
70        Box::new(self.clone())
71    }
72}
73
74#[cfg(test)]
75mod tests {
76    use super::*;
77    use crate::llm::tools::ToolRunCtx;
78
79    #[tokio::test]
80    async fn test_insert_task_after_run() {
81        let task_list = Arc::new(Mutex::new(TaskList::new()));
82        {
83            let mut list = task_list.lock().unwrap();
84            list.append_task("first".to_string());
85        }
86        let tool = InsertTaskAfterTool::new(Arc::clone(&task_list));
87        let mut args = HashMap::new();
88        args.insert("existing_task_id".to_string(), serde_json::Value::from(1u64));
89        args.insert("description".to_string(), serde_json::Value::from("second"));
90        let result = tool.run(&args, &ToolRunCtx::default()).await.unwrap();
91        assert_eq!(result["description"], "second");
92    }
93
94    #[test]
95    fn test_descriptor() {
96        let task_list = Arc::new(Mutex::new(TaskList::new()));
97        let tool = InsertTaskAfterTool::new(task_list);
98        assert_eq!(tool.descriptor().function.name, "insert_task_after");
99    }
100}