|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import logging |
| 4 | +from typing import Any |
| 5 | + |
| 6 | +from fastapi import APIRouter, HTTPException, Query |
| 7 | +from pydantic import BaseModel |
| 8 | + |
| 9 | +from ..automation.taskModel import TaskDefinition |
| 10 | +from ..automation.taskRegistry import getTaskRegistry |
| 11 | +from ..automation.taskRunner import TaskRunner |
| 12 | + |
| 13 | +logger = logging.getLogger(__name__) |
| 14 | + |
| 15 | + |
| 16 | +class CreateTaskRequest(BaseModel): |
| 17 | + name: str |
| 18 | + documentPath: str |
| 19 | + description: str = "" |
| 20 | + schedule: str | None = None |
| 21 | + inputs: dict[str, Any] | None = None |
| 22 | + |
| 23 | + |
| 24 | +class UpdateTaskRequest(BaseModel): |
| 25 | + name: str | None = None |
| 26 | + description: str | None = None |
| 27 | + schedule: str | None = None |
| 28 | + enabled: bool | None = None |
| 29 | + |
| 30 | + |
| 31 | +class ScheduleRequest(BaseModel): |
| 32 | + schedule: str |
| 33 | + |
| 34 | + |
| 35 | +def createAutomationRouter(state: Any) -> APIRouter: |
| 36 | + router = APIRouter() |
| 37 | + |
| 38 | + @router.get("/api/tasks") |
| 39 | + def apiListTasks(): |
| 40 | + registry = getTaskRegistry() |
| 41 | + tasks = registry.listTasks() |
| 42 | + return { |
| 43 | + "tasks": [t.serialize() for t in tasks], |
| 44 | + "total": len(tasks), |
| 45 | + } |
| 46 | + |
| 47 | + @router.post("/api/tasks") |
| 48 | + def apiCreateTask(req: CreateTaskRequest): |
| 49 | + registry = getTaskRegistry() |
| 50 | + task = registry.create( |
| 51 | + name=req.name, |
| 52 | + documentPath=req.documentPath, |
| 53 | + description=req.description, |
| 54 | + schedule=req.schedule, |
| 55 | + inputs=req.inputs, |
| 56 | + ) |
| 57 | + return task.serialize() |
| 58 | + |
| 59 | + @router.get("/api/tasks/{taskId}") |
| 60 | + def apiGetTask(taskId: str): |
| 61 | + registry = getTaskRegistry() |
| 62 | + task = registry.get(taskId) |
| 63 | + if task is None: |
| 64 | + raise HTTPException(status_code=404, detail="Task not found") |
| 65 | + lastRun = registry.getLastRun(taskId) |
| 66 | + result = task.serialize() |
| 67 | + if lastRun: |
| 68 | + result["lastRun"] = lastRun.serialize() |
| 69 | + return result |
| 70 | + |
| 71 | + @router.put("/api/tasks/{taskId}") |
| 72 | + def apiUpdateTask(taskId: str, req: UpdateTaskRequest): |
| 73 | + registry = getTaskRegistry() |
| 74 | + task = registry.update( |
| 75 | + taskId, |
| 76 | + name=req.name, |
| 77 | + description=req.description, |
| 78 | + schedule=req.schedule, |
| 79 | + enabled=req.enabled, |
| 80 | + ) |
| 81 | + if task is None: |
| 82 | + raise HTTPException(status_code=404, detail="Task not found") |
| 83 | + return task.serialize() |
| 84 | + |
| 85 | + @router.delete("/api/tasks/{taskId}") |
| 86 | + def apiDeleteTask(taskId: str): |
| 87 | + registry = getTaskRegistry() |
| 88 | + deleted = registry.delete(taskId) |
| 89 | + if not deleted: |
| 90 | + raise HTTPException(status_code=404, detail="Task not found") |
| 91 | + return {"ok": True} |
| 92 | + |
| 93 | + @router.post("/api/tasks/{taskId}/run") |
| 94 | + async def apiRunTask(taskId: str): |
| 95 | + registry = getTaskRegistry() |
| 96 | + task = registry.get(taskId) |
| 97 | + if task is None: |
| 98 | + raise HTTPException(status_code=404, detail="Task not found") |
| 99 | + |
| 100 | + workspaceRoot = str(getattr(state, "workspaceRoot", ".")) |
| 101 | + runner = TaskRunner(workspaceRoot=workspaceRoot) |
| 102 | + run = await runner.run(task) |
| 103 | + registry.addRun(run) |
| 104 | + return run.serialize() |
| 105 | + |
| 106 | + @router.get("/api/tasks/{taskId}/runs") |
| 107 | + def apiGetRuns(taskId: str, limit: int = Query(20)): |
| 108 | + registry = getTaskRegistry() |
| 109 | + task = registry.get(taskId) |
| 110 | + if task is None: |
| 111 | + raise HTTPException(status_code=404, detail="Task not found") |
| 112 | + runs = registry.getRuns(taskId, limit=limit) |
| 113 | + return {"runs": [r.serialize() for r in runs]} |
| 114 | + |
| 115 | + @router.put("/api/tasks/{taskId}/schedule") |
| 116 | + def apiSetSchedule(taskId: str, req: ScheduleRequest): |
| 117 | + registry = getTaskRegistry() |
| 118 | + task = registry.update(taskId, schedule=req.schedule) |
| 119 | + if task is None: |
| 120 | + raise HTTPException(status_code=404, detail="Task not found") |
| 121 | + |
| 122 | + from ..automation.scheduler import TaskScheduler, parseScheduleSeconds |
| 123 | + |
| 124 | + if parseScheduleSeconds(req.schedule) is None: |
| 125 | + raise HTTPException(status_code=400, detail=f"Invalid schedule: {req.schedule}") |
| 126 | + |
| 127 | + return {"ok": True, "schedule": req.schedule} |
| 128 | + |
| 129 | + return router |
0 commit comments