Files
hive/backend/app/api/tasks.py
anthonyrawlins 4de45bf450 Merge redundant coordinators into unified coordinator architecture
Major refactoring:
- Created UnifiedCoordinator that combines HiveCoordinator and DistributedCoordinator
- Eliminated code duplication and architectural redundancy
- Unified agent management, task orchestration, and workflow execution
- Single coordinator instance replaces two global coordinators
- Backward compatibility maintained through state aliases

Key features of UnifiedCoordinator:
 Combined agent types: Ollama + CLI agents with unified management
 Dual task modes: Simple tasks + complex distributed workflows
 Performance monitoring: Prometheus metrics + adaptive load balancing
 Background processes: Health monitoring + performance optimization
 Redis integration: Distributed caching and coordination (optional)
 Database integration: Agent loading + task persistence preparation

API updates:
- Updated all API endpoints to use unified coordinator
- Maintained interface compatibility for existing endpoints
- Fixed attribute references for unified agent model
- Simplified dependency injection pattern

Architecture benefits:
- Single point of coordination eliminates race conditions
- Reduced memory footprint (one coordinator vs two)
- Simplified initialization and lifecycle management
- Consistent feature set across all orchestration modes
- Better separation of concerns within single coordinator class

This resolves the critical architectural issue of redundant coordinators
while maintaining full backward compatibility and adding enhanced features.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-11 08:44:21 +10:00

109 lines
3.6 KiB
Python

from fastapi import APIRouter, Depends, HTTPException, Query
from typing import List, Dict, Any, Optional
from ..core.auth import get_current_user
from ..core.unified_coordinator import UnifiedCoordinator, AgentType, TaskStatus
router = APIRouter()
# This will be injected by main.py
coordinator: UnifiedCoordinator = None
def set_coordinator(coord: UnifiedCoordinator):
global coordinator
coordinator = coord
@router.post("/tasks")
async def create_task(task_data: Dict[str, Any]):
"""Create a new development task"""
try:
# Map string type to AgentType enum
task_type_str = task_data.get("type")
if task_type_str not in [t.value for t in AgentType]:
raise HTTPException(status_code=400, detail=f"Invalid task type: {task_type_str}")
task_type = AgentType(task_type_str)
priority = task_data.get("priority", 3)
context = task_data.get("context", {})
# Create task using coordinator
task = coordinator.create_task(task_type, context, priority)
return {
"id": task.id,
"type": task.type.value,
"priority": task.priority,
"status": task.status.value,
"context": task.context,
"created_at": task.created_at,
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.get("/tasks/{task_id}")
async def get_task(task_id: str, current_user: dict = Depends(get_current_user)):
"""Get details of a specific task"""
task = coordinator.get_task_status(task_id)
if not task:
raise HTTPException(status_code=404, detail="Task not found")
return {
"id": task.id,
"type": task.type.value,
"priority": task.priority,
"status": task.status.value,
"context": task.context,
"assigned_agent": task.assigned_agent,
"result": task.result,
"created_at": task.created_at,
"completed_at": task.completed_at,
}
@router.get("/tasks")
async def get_tasks(
status: Optional[str] = Query(None, description="Filter by task status"),
agent: Optional[str] = Query(None, description="Filter by assigned agent"),
limit: int = Query(20, description="Maximum number of tasks to return"),
current_user: dict = Depends(get_current_user)
):
"""Get list of tasks with optional filtering"""
# Get all tasks from coordinator
all_tasks = list(coordinator.tasks.values())
# Apply filters
filtered_tasks = all_tasks
if status:
try:
status_enum = TaskStatus(status)
filtered_tasks = [t for t in filtered_tasks if t.status == status_enum]
except ValueError:
raise HTTPException(status_code=400, detail=f"Invalid status: {status}")
if agent:
filtered_tasks = [t for t in filtered_tasks if t.assigned_agent == agent]
# Sort by creation time (newest first) and limit
filtered_tasks.sort(key=lambda t: t.created_at or 0, reverse=True)
filtered_tasks = filtered_tasks[:limit]
# Format response
tasks = []
for task in filtered_tasks:
tasks.append({
"id": task.id,
"type": task.type.value,
"priority": task.priority,
"status": task.status.value,
"context": task.context,
"assigned_agent": task.assigned_agent,
"result": task.result,
"created_at": task.created_at,
"completed_at": task.completed_at,
})
return {
"tasks": tasks,
"total": len(tasks),
"filtered": len(all_tasks) != len(tasks),
}