ForcePilot/backend/server/utils/lifespan.py
Wenjie Zhang d655cbe640 feat: 添加subagents管理功能
- 实现了应用程序启动期间的理subagents初始化。
- 为subagents的 CRUD 操作和存储库方法创建了单元测试。
- 开发了用于列出、检索、创建、更新和删除子代理的子代理 API 端点。
- 添加了用于在前端管理子代理的 SubAgentsComponent,包括搜索和详细信息视图。
2026-03-24 11:09:44 +08:00

57 lines
1.7 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.

from contextlib import asynccontextmanager
from fastapi import FastAPI
from yuxi.services.task_service import tasker
from yuxi.services.mcp_service import init_mcp_servers
from yuxi.services.subagent_service import init_builtin_subagents
from yuxi.services.run_queue_service import close_queue_clients, get_redis_client
from yuxi.storage.postgres.manager import pg_manager
from yuxi.knowledge import knowledge_base
from yuxi.utils import logger
@asynccontextmanager
async def lifespan(app: FastAPI):
"""FastAPI lifespan事件管理器"""
# 初始化数据库连接
try:
pg_manager.initialize()
await pg_manager.create_business_tables()
await pg_manager.ensure_business_schema()
await pg_manager.ensure_knowledge_schema()
except Exception as e:
logger.error(f"Failed to initialize database during startup: {e}")
# 初始化 MCP 服务器配置
try:
await init_mcp_servers()
except Exception as e:
logger.error(f"Failed to initialize MCP servers during startup: {e}")
# 初始化内置 SubAgent
try:
await init_builtin_subagents()
except Exception as e:
logger.error(f"Failed to initialize builtin subagents during startup: {e}")
raise
# 初始化知识库管理器
try:
await knowledge_base.initialize()
except Exception as e:
logger.error(f"Failed to initialize knowledge base manager: {e}")
# 预热 Redisrun 队列)
try:
redis = await get_redis_client()
await redis.ping()
except Exception as e:
logger.warning(f"Run queue redis unavailable on startup: {e}")
await tasker.start()
yield
await tasker.shutdown()
await close_queue_clients()
await pg_manager.close()