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