Skip to main content

mojentic/llm/tools/ephemeral_task_manager/
start_task.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 starting a task in the ephemeral task manager
10///
11/// This tool changes a task's status from Pending to InProgress
12#[derive(Clone)]
13pub struct StartTaskTool {
14    task_list: Arc<Mutex<TaskList>>,
15}
16
17impl StartTaskTool {
18    /// Creates a new StartTaskTool with a shared task list
19    pub fn new(task_list: Arc<Mutex<TaskList>>) -> Self {
20        Self { task_list }
21    }
22}
23
24#[async_trait]
25impl LlmTool for StartTaskTool {
26    async fn run(
27        &self,
28        args: &HashMap<String, Value>,
29        _ctx: &crate::llm::tools::ToolRunCtx,
30    ) -> Result<Value> {
31        let task_id = args.get("id").and_then(|v| v.as_u64()).unwrap_or(0) as usize;
32
33        let mut task_list = self.task_list.lock().unwrap();
34        let task = task_list.start_task(task_id)?;
35
36        Ok(json!({
37            "id": task.id,
38            "description": task.description,
39            "status": task.status.as_str(),
40            "summary": format!("Task '{}' started successfully", task_id)
41        }))
42    }
43
44    fn descriptor(&self) -> ToolDescriptor {
45        ToolDescriptor {
46            r#type: "function".to_string(),
47            function: FunctionDescriptor {
48                name: "start_task".to_string(),
49                description: "Start a task by changing its status from PENDING to IN_PROGRESS."
50                    .to_string(),
51                parameters: json!({
52                    "type": "object",
53                    "properties": {
54                        "id": {
55                            "type": "integer",
56                            "description": "The ID of the task to start"
57                        }
58                    },
59                    "required": ["id"]
60                }),
61            },
62        }
63    }
64    fn clone_box(&self) -> Box<dyn LlmTool> {
65        Box::new(self.clone())
66    }
67}
68
69#[cfg(test)]
70mod tests {
71    use super::*;
72    use crate::llm::tools::ToolRunCtx;
73
74    #[tokio::test]
75    async fn test_start_task_run() {
76        let task_list = Arc::new(Mutex::new(TaskList::new()));
77        {
78            let mut list = task_list.lock().unwrap();
79            list.append_task("pending task".to_string());
80        }
81        let tool = StartTaskTool::new(Arc::clone(&task_list));
82        let mut args = HashMap::new();
83        args.insert("id".to_string(), serde_json::Value::from(1u64));
84        let result = tool.run(&args, &ToolRunCtx::default()).await.unwrap();
85        assert_eq!(result["status"], "in_progress");
86    }
87
88    #[test]
89    fn test_descriptor() {
90        let task_list = Arc::new(Mutex::new(TaskList::new()));
91        let tool = StartTaskTool::new(task_list);
92        assert_eq!(tool.descriptor().function.name, "start_task");
93    }
94}