Skip to main content

task_graph_mcp/resources/
tasks.rs

1//! Task resource handlers.
2
3use crate::config::{DependenciesConfig, StatesConfig};
4use crate::db::Database;
5use anyhow::Result;
6use serde_json::{Value, json};
7
8pub fn get_all_tasks(db: &Database) -> Result<Value> {
9    let tasks = db.get_all_tasks()?;
10    let deps = db.get_all_dependencies()?;
11
12    Ok(json!({
13        "tasks": tasks.iter().map(|t| json!({
14            "id": &t.id,
15            "title": t.title,
16            "description": t.description,
17            "status": &t.status,
18            "priority": t.priority,
19            "worker_id": &t.worker_id,
20            "claimed_at": t.claimed_at,
21            "points": t.points,
22            "time_estimate_ms": t.time_estimate_ms,
23            "time_actual_ms": t.time_actual_ms,
24            "current_thought": t.current_thought,
25            "cost_usd": t.cost_usd,
26            "metrics": t.metrics,
27            "created_at": t.created_at,
28            "updated_at": t.updated_at
29        })).collect::<Vec<_>>(),
30        "dependencies": deps.iter().map(|d| json!({
31            "from": &d.from_task_id,
32            "to": &d.to_task_id,
33            "type": &d.dep_type
34        })).collect::<Vec<_>>()
35    }))
36}
37
38pub fn get_ready_tasks(
39    db: &Database,
40    states_config: &StatesConfig,
41    deps_config: &DependenciesConfig,
42) -> Result<Value> {
43    let tasks = db.get_ready_tasks(None, states_config, deps_config, None, None)?;
44
45    Ok(json!({
46        "tasks": tasks.iter().map(|t| json!({
47            "id": &t.id,
48            "title": t.title,
49            "description": t.description,
50            "priority": t.priority,
51            "points": t.points,
52            "needed_tags": t.needed_tags,
53            "wanted_tags": t.wanted_tags
54        })).collect::<Vec<_>>()
55    }))
56}
57
58pub fn get_blocked_tasks(
59    db: &Database,
60    states_config: &StatesConfig,
61    deps_config: &DependenciesConfig,
62) -> Result<Value> {
63    let tasks = db.get_blocked_tasks(states_config, deps_config, None, None)?;
64
65    Ok(json!({
66        "tasks": tasks.iter().map(|t| {
67            let blockers = db.get_blockers(&t.id).unwrap_or_default();
68            json!({
69                "id": &t.id,
70                "title": t.title,
71                "priority": t.priority,
72                "blocked_by": &blockers
73            })
74        }).collect::<Vec<_>>()
75    }))
76}
77
78pub fn get_claimed_tasks(db: &Database, agent_id: Option<&str>) -> Result<Value> {
79    let tasks = db.get_claimed_tasks(agent_id)?;
80
81    Ok(json!({
82        "tasks": tasks.iter().map(|t| json!({
83            "id": &t.id,
84            "title": t.title,
85            "status": &t.status,
86            "priority": t.priority,
87            "worker_id": &t.worker_id,
88            "claimed_at": t.claimed_at,
89            "current_thought": t.current_thought
90        })).collect::<Vec<_>>()
91    }))
92}
93
94pub fn get_task_tree(db: &Database, task_id: &str) -> Result<Value> {
95    let tree = db
96        .get_task_tree(task_id)?
97        .ok_or_else(|| anyhow::anyhow!("Task not found"))?;
98
99    Ok(serde_json::to_value(tree)?)
100}