ForcePilot/server/main.py
Wenjie Zhang 43ec5a4a71 feat: 实现异步任务管理系统和任务中心UI
## 后端变更

  ### 任务管理核心
  - 新增 tasker 服务,支持异步任务队列和工作线程池
  - 添加任务执行时间追踪(started_at, completed_at)
  - 实现任务状态持久化(JSON文件存储)
  - 支持任务取消和进度更新

  ### 知识库导入优化
  - 将知识库文档导入改为异步任务处理
  - 逐个处理文档并实时更新进度(5% → 95%)
  - 支持任务取消检测

  ### API接口
  - 新增任务列表、详情、取消等REST API
  - 任务API需要管理员权限

  ### 测试
  - 添加任务路由集成测试

  ## 前端变更

  ### 任务中心UI
  - 新增任务中心抽屉组件,采用简洁的卡片式设计
  - 支持任务状态筛选(全部/进行中/已完成/失败)
  - 进行中任务显示实时进度条
  - 已完成任务显示执行时长和完成状态徽章
  - 支持相对时间显示(如:5分钟前)

  ### 状态管理
  - 新增 tasker store,管理任务列表和轮询
  - 支持任务注册、刷新、取消操作

  ### 交互优化
  - 在状态栏和侧边栏添加任务中心入口
  - 显示活跃任务数量徽章
  - 知识库导入自动注册到任务中心
2025-10-11 15:02:24 +08:00

76 lines
2.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import uvicorn
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from starlette.middleware.base import BaseHTTPMiddleware
from server.routers import router
from server.services.tasker import tasker
from server.utils.auth_middleware import is_public_path
from server.utils.common_utils import setup_logging
# 设置日志配置
setup_logging()
app = FastAPI()
app.include_router(router, prefix="/api")
# CORS 设置
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# 鉴权中间件
class AuthMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next):
# 获取请求路径
path = request.url.path
# 检查是否为公开路径,公开路径无需身份验证
if is_public_path(path):
return await call_next(request)
if not path.startswith("/api"):
# 非API路径可能是前端路由或静态资源
return await call_next(request)
# # 提取Authorization头
# auth_header = request.headers.get("Authorization")
# if not auth_header or not auth_header.startswith("Bearer "):
# return JSONResponse(
# status_code=status.HTTP_401_UNAUTHORIZED,
# content={"detail": f"请先登录。Path: {path}"},
# headers={"WWW-Authenticate": "Bearer"}
# )
# # 获取token
# token = auth_header.split("Bearer ")[1]
# # 添加token到请求状态后续路由可以直接使用
# request.state.token = token
# 继续处理请求
return await call_next(request)
# 添加鉴权中间件
app.add_middleware(AuthMiddleware)
@app.on_event("startup")
async def start_tasker() -> None:
await tasker.start()
@app.on_event("shutdown")
async def stop_tasker() -> None:
await tasker.shutdown()
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=5050, threads=10, workers=10, reload=True)