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 serde_json::{json, Value};
5use std::collections::HashMap;
6use std::sync::{Arc, Mutex};
7
8/// Tool for inserting a new task after a specific task ID in the ephemeral task manager list
9#[derive(Clone)]
10pub struct InsertTaskAfterTool {
11    task_list: Arc<Mutex<TaskList>>,
12}
13
14impl InsertTaskAfterTool {
15    /// Creates a new InsertTaskAfterTool 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 InsertTaskAfterTool {
22    fn run(&self, args: &HashMap<String, Value>) -> Result<Value> {
23        let existing_task_id =
24            args.get("existing_task_id").and_then(|v| v.as_u64()).unwrap_or(0) as usize;
25
26        let description =
27            args.get("description").and_then(|v| v.as_str()).unwrap_or("").to_string();
28
29        let mut task_list = self.task_list.lock().unwrap();
30        let task = task_list.insert_task_after(existing_task_id, description)?;
31
32        Ok(json!({
33            "id": task.id,
34            "description": task.description,
35            "status": task.status.as_str(),
36            "summary": format!("Task '{}' inserted after task '{}' successfully", task.id, existing_task_id)
37        }))
38    }
39
40    fn descriptor(&self) -> ToolDescriptor {
41        ToolDescriptor {
42            r#type: "function".to_string(),
43            function: FunctionDescriptor {
44                name: "insert_task_after".to_string(),
45                description: "Insert a new task after an existing task in the task list. The task will start with 'pending' status.".to_string(),
46                parameters: json!({
47                    "type": "object",
48                    "properties": {
49                        "existing_task_id": {
50                            "type": "integer",
51                            "description": "The ID of the existing task after which to insert the new task"
52                        },
53                        "description": {
54                            "type": "string",
55                            "description": "The description of the new task"
56                        }
57                    },
58                    "required": ["existing_task_id", "description"]
59                }),
60            },
61        }
62    }
63    fn clone_box(&self) -> Box<dyn LlmTool> {
64        Box::new(self.clone())
65    }
66}