add history endpoint
This commit is contained in:
parent
50589232d6
commit
fb7ab50de6
7 changed files with 206 additions and 6 deletions
|
|
@ -3,9 +3,11 @@ from fastapi.responses import JSONResponse
|
|||
|
||||
from api.contracts.task_schemas import (
|
||||
BrowserTaskAcceptedResponse,
|
||||
BrowserTaskHistoryResponse,
|
||||
BrowserTaskRequest,
|
||||
BrowserTaskResultResponse,
|
||||
BrowserTaskStatusResponse,
|
||||
TaskHistoryEvent,
|
||||
)
|
||||
from api.domain.task_status import TaskStatus
|
||||
from api.repositories.task_store import TaskRecord
|
||||
|
|
@ -69,6 +71,32 @@ async def get_task_result(
|
|||
)
|
||||
|
||||
|
||||
@router.get("/tasks/{task_id}/history", response_model=BrowserTaskHistoryResponse)
|
||||
async def get_task_history(
|
||||
task_id: str,
|
||||
service: TaskService = Depends(get_task_service),
|
||||
) -> JSONResponse | BrowserTaskHistoryResponse:
|
||||
rec = await service.get_task(task_id)
|
||||
if rec is None:
|
||||
raise HTTPException(status_code=404, detail="Task not found")
|
||||
|
||||
if rec.status in (TaskStatus.queued, TaskStatus.running):
|
||||
return JSONResponse(
|
||||
status_code=202,
|
||||
content={
|
||||
"task_id": rec.task_id,
|
||||
"status": rec.status.value,
|
||||
"history": rec.history,
|
||||
},
|
||||
)
|
||||
|
||||
return BrowserTaskHistoryResponse(
|
||||
task_id=rec.task_id,
|
||||
status=rec.status,
|
||||
history=_to_history_events(rec),
|
||||
)
|
||||
|
||||
|
||||
def _to_status_response(rec: TaskRecord) -> BrowserTaskStatusResponse:
|
||||
return BrowserTaskStatusResponse(
|
||||
task_id=rec.task_id,
|
||||
|
|
@ -78,3 +106,24 @@ def _to_status_response(rec: TaskRecord) -> BrowserTaskStatusResponse:
|
|||
finished_at=rec.finished_at,
|
||||
error=rec.error,
|
||||
)
|
||||
|
||||
|
||||
def _to_history_events(rec: TaskRecord) -> list[TaskHistoryEvent]:
|
||||
events: list[TaskHistoryEvent] = []
|
||||
for index, item in enumerate(rec.history, start=1):
|
||||
kind = str(item.get("kind") or item.get("type") or "system")
|
||||
content = item.get("content")
|
||||
if content is not None:
|
||||
content = str(content)
|
||||
data = item.get("data")
|
||||
if not isinstance(data, dict):
|
||||
data = {}
|
||||
|
||||
step = item.get("step")
|
||||
if not isinstance(step, int):
|
||||
step = index
|
||||
|
||||
events.append(TaskHistoryEvent(step=step, kind=kind, content=content, data=data))
|
||||
|
||||
return events
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue