mojentic/llm/tools/ephemeral_task_manager/
complete_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 completing a task in the ephemeral task manager
9///
10/// This tool changes a task's status from InProgress to Completed
11#[derive(Clone)]
12pub struct CompleteTaskTool {
13    task_list: Arc<Mutex<TaskList>>,
14}
15
16impl CompleteTaskTool {
17    /// Creates a new CompleteTaskTool with a shared task list
18    pub fn new(task_list: Arc<Mutex<TaskList>>) -> Self {
19        Self { task_list }
20    }
21}
22
23impl LlmTool for CompleteTaskTool {
24    fn run(&self, args: &HashMap<String, Value>) -> Result<Value> {
25        let task_id = args.get("id").and_then(|v| v.as_u64()).unwrap_or(0) as usize;
26
27        let mut task_list = self.task_list.lock().unwrap();
28        let task = task_list.complete_task(task_id)?;
29
30        Ok(json!({
31            "id": task.id,
32            "description": task.description,
33            "status": task.status.as_str(),
34            "summary": format!("Task '{}' completed successfully", task_id)
35        }))
36    }
37
38    fn descriptor(&self) -> ToolDescriptor {
39        ToolDescriptor {
40            r#type: "function".to_string(),
41            function: FunctionDescriptor {
42                name: "complete_task".to_string(),
43                description:
44                    "Complete a task by changing its status from IN_PROGRESS to COMPLETED."
45                        .to_string(),
46                parameters: json!({
47                    "type": "object",
48                    "properties": {
49                        "id": {
50                            "type": "integer",
51                            "description": "The ID of the task to complete"
52                        }
53                    },
54                    "required": ["id"]
55                }),
56            },
57        }
58    }
59    fn clone_box(&self) -> Box<dyn LlmTool> {
60        Box::new(self.clone())
61    }
62}