# Copyright 2026 zhaoxi826 # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 """Task API:管控节点短任务的查询接口。 task 由 regulatory_node 在完成短任务时内部建立,因此本路由只暴露读取, 不开放对外创建。 """ from typing import Optional from fastapi import APIRouter, Depends, HTTPException from kilostar.utils.access import Accessor, TokenData from kilostar.utils.ray_hook import ray_actor_hook task_router = APIRouter(prefix="/api/v1/task", tags=["task"]) @task_router.get("/list") async def list_tasks( status: Optional[str] = None, limit: int = 20, offset: int = 0, token_data: TokenData = Depends(Accessor.get_current_user), ): """列出当前用户的所有短任务,按时间倒序。""" postgres_database = ray_actor_hook("postgres_database").postgres_database tasks = await postgres_database.list_tasks_by_user.remote( user_id=token_data.user_id, status=status, limit=limit, offset=offset, ) return {"tasks": tasks, "total": len(tasks)} @task_router.get("/{task_id}") async def get_task( task_id: str, token_data: TokenData = Depends(Accessor.get_current_user), ): """按 task_id 读取一条 task 详情。仅 owner 可访问。""" postgres_database = ray_actor_hook("postgres_database").postgres_database task = await postgres_database.get_task.remote(task_id) if not task: raise HTTPException(status_code=404, detail="task not found") if task.get("user_id") != token_data.user_id: raise HTTPException(status_code=403, detail="forbidden") return task