Spaces:
Sleeping
Sleeping
File size: 2,054 Bytes
2eef9ea | 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 | """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
@router.post("/", response_model=TaskResponse)
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
@router.get("/{task_id}", response_model=TaskResponse)
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"}
@router.delete("/{task_id}")
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"}
|