feat:架构重构
This commit is contained in:
+7
-13
@@ -18,11 +18,7 @@ from app.core.logging import setup_logging
|
||||
async def lifespan(application: FastAPI):
|
||||
setup_logging()
|
||||
from app.clients.decision import close_decision_engine
|
||||
from app.clients.material_mcp import (
|
||||
MaterialFieldServiceError,
|
||||
start_material_server,
|
||||
stop_material_server,
|
||||
)
|
||||
from app.clients.mcp import start_mcp_servers, stop_mcp_servers
|
||||
from app.clients.user_center import close_user_center
|
||||
from app.observability.tracing import flush_tracing
|
||||
from app.processor.graph import build_graph
|
||||
@@ -33,15 +29,12 @@ async def lifespan(application: FastAPI):
|
||||
# 按配置选择 MongoDB 或进程内存,会话记录与图检查点使用同一种存储
|
||||
persistence = await open_persistence()
|
||||
application.state.conversation_store = persistence.store
|
||||
material_server = None
|
||||
mcp_processes = []
|
||||
try:
|
||||
# 物料目录启动时载入内存,agent 规划与前端接口共用,避免重复查询数据库
|
||||
# 物料目录启动时载入内存,动作校验与前端接口共用,避免重复查询数据库
|
||||
await warm_material_cache(persistence.db)
|
||||
# 物料字段服务给模型查询字段;启动失败只记录,需要查字段的修改执行时会报错
|
||||
try:
|
||||
material_server = await start_material_server()
|
||||
except MaterialFieldServiceError as error:
|
||||
logger.error("{},修改组件属性和新增组件将无法执行", error)
|
||||
# 拉起 mcp_servers.json 中由应用管理的服务;单个服务失败只记录,用到它的请求执行时报错
|
||||
mcp_processes = await start_mcp_servers()
|
||||
application.state.agent_service = AgentService(
|
||||
build_graph(persistence.checkpointer), persistence.store
|
||||
)
|
||||
@@ -49,7 +42,7 @@ async def lifespan(application: FastAPI):
|
||||
finally:
|
||||
# 共享客户端只在应用退出时关闭,追踪刷新放到线程避免阻塞事件循环
|
||||
try:
|
||||
await stop_material_server(material_server)
|
||||
await stop_mcp_servers(mcp_processes)
|
||||
finally:
|
||||
try:
|
||||
await persistence.close()
|
||||
@@ -65,6 +58,7 @@ async def lifespan(application: FastAPI):
|
||||
finally:
|
||||
await logger.complete()
|
||||
|
||||
|
||||
def create_app() -> FastAPI:
|
||||
application = FastAPI(title="Screen Agent API", lifespan=lifespan)
|
||||
# 统一处理请求校验、HTTP 错误和未捕获异常
|
||||
|
||||
Reference in New Issue
Block a user