Spaces:
Sleeping
Sleeping
| """Task management endpoints.""" | |
| from typing import Optional | |
| from fastapi import APIRouter, HTTPException, BackgroundTasks | |
| from pydantic import BaseModel, Field | |
| from ...agents.orchestrator import run_workflow, stream_workflow | |
| from ...state.graph_state import create_initial_state | |
| router = APIRouter() | |
| class TaskRequest(BaseModel): | |
| """Task request model.""" | |
| task: str = Field(..., description="Task description") | |
| task_id: Optional[str] = Field(None, description="Optional custom task ID") | |
| class TaskResponse(BaseModel): | |
| """Task response model.""" | |
| task_id: str | |
| task: str | |
| status: str | |
| async def create_task(request: TaskRequest, background_tasks: BackgroundTasks): | |
| """ | |
| Create and execute a new task. | |
| Args: | |
| request: Task request payload | |
| background_tasks: Background task runner | |
| Returns: | |
| TaskResponse: Created task information | |
| """ | |
| try: | |
| state = create_initial_state(request.task, task_id=request.task_id) | |
| task_id = state["task_id"] | |
| # Run workflow in background | |
| background_tasks.add_task(run_workflow, state) | |
| return {"task_id": task_id, "task": request.task, "status": "processing"} | |
| except Exception as e: | |
| raise HTTPException(status_code=400, detail=str(e)) from e | |
| async def get_task(task_id: str): | |
| """ | |
| Get task status. | |
| Args: | |
| task_id: Task ID | |
| Returns: | |
| TaskResponse: Task information | |
| Raises: | |
| HTTPException: If task not found | |
| """ | |
| # This would fetch from persistent storage (Redis/DB) | |
| # Placeholder implementation | |
| return {"task_id": task_id, "task": "Example task", "status": "completed"} | |
| async def cancel_task(task_id: str): | |
| """ | |
| Cancel a running task. | |
| Args: | |
| task_id: Task ID to cancel | |
| Returns: | |
| dict: Cancellation confirmation | |
| """ | |
| return {"task_id": task_id, "status": "cancelled"} | |