File size: 4,338 Bytes
35765b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
698b2c1
35765b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
698b2c1
35765b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
"""Task-related MCP tools."""

from sqlalchemy.orm import Session
from sqlalchemy import desc
from app.database import SessionLocal
from app.models import Task, TaskStatus, Project, LogEntry


def create_task(project_id: str, title: str, description: str = None, assigned_to: str = None) -> dict:
    """
    Create a new task in a project.
    
    Args:
        project_id: ID of the project
        title: Task title
        description: Optional task description
        assigned_to: Optional user ID to assign the task to
    
    Returns:
        Created task data
    """
    db: Session = SessionLocal()
    try:
        # Verify project exists
        project = db.query(Project).filter(Project.id == project_id).first()
        if not project:
            return {"error": f"Project {project_id} not found"}
        
        # Create task
        task = Task(
            project_id=project_id,
            title=title,
            description=description,
            assigned_to=assigned_to,
            status=TaskStatus.todo
        )
        db.add(task)
        db.commit()
        
        return {
            "id": task.id,
            "project_id": task.project_id,
            "title": task.title,
            "description": task.description,
            "status": task.status.value,
            "assigned_to": task.assigned_to,
            "working_by": task.working_by,
            "created_at": task.created_at.isoformat()
        }
    finally:
        db.close()


def list_tasks(project_id: str, status: str = None) -> dict:
    """
    List all tasks for a project, optionally filtered by status.
    
    Args:
        project_id: ID of the project
        status: Optional status filter ("todo", "in_progress", "done")
    
    Returns:
        List of tasks
    """
    db: Session = SessionLocal()
    try:
        # Build query
        query = db.query(Task).filter(Task.project_id == project_id)
        
        # Apply status filter if provided
        if status:
            try:
                status_enum = TaskStatus(status)
                query = query.filter(Task.status == status_enum)
            except ValueError:
                return {"error": f"Invalid status: {status}. Must be one of: todo, in_progress, done"}
        
        # Order by created_at descending
        tasks = query.order_by(desc(Task.created_at)).all()
        
        return {
            "tasks": [
                {
                    "id": task.id,
                    "project_id": task.project_id,
                    "title": task.title,
                    "description": task.description,
                    "status": task.status.value,
                    "assigned_to": task.assigned_to,
                    "working_by": task.working_by,
                    "created_at": task.created_at.isoformat(),
                    "completed_at": task.completed_at.isoformat() if task.completed_at else None
                }
                for task in tasks
            ]
        }
    finally:
        db.close()


def list_activity(project_id: str, limit: int = 20) -> dict:
    """
    Get recent activity (log entries) for a project.
    
    Args:
        project_id: ID of the project
        limit: Maximum number of entries to return (default 20)
    
    Returns:
        List of recent log entries
    """
    db: Session = SessionLocal()
    try:
        # Query log entries ordered by most recent
        entries = (
            db.query(LogEntry)
            .filter(LogEntry.project_id == project_id)
            .order_by(desc(LogEntry.created_at))
            .limit(limit)
            .all()
        )
        
        return {
            "activity": [
                {
                    "id": entry.id,
                    "project_id": entry.project_id,
                    "task_id": entry.task_id,
                    "user_id": entry.user_id,
                    "actor_type": entry.actor_type.value,
                    "action_type": entry.action_type.value,
                    "raw_input": entry.raw_input,
                    "generated_doc": entry.generated_doc,
                    "tags": entry.tags,
                    "created_at": entry.created_at.isoformat()
                }
                for entry in entries
            ]
        }
    finally:
        db.close()