feat:物料存入数据库

This commit is contained in:
2026-09-30 09:00:44 -07:00
parent 3e88b339a0
commit f6302e89b5
5 changed files with 131 additions and 26 deletions
+8 -3
View File
@@ -8,6 +8,7 @@ from loguru import logger
from app.api.agent import router
from app.api.conversations import router as conversation_router
from app.api.materials import router as material_router
from app.core.exceptions import register_exception_handlers
from app.core.logging import setup_logging
@@ -19,15 +20,18 @@ async def lifespan(application: FastAPI):
from app.observability.tracing import flush_tracing
from app.processor.graph import build_graph
from app.services.agent import AgentService
from app.services.material_mongo import warm_material_cache
from app.services.persistence import open_persistence
# 按配置选择 MongoDB 或进程内存,会话记录与图检查点使用同一种存储
persistence = await open_persistence()
application.state.conversation_store = persistence.store
application.state.agent_service = AgentService(
build_graph(persistence.checkpointer), persistence.store
)
try:
# 物料目录启动时载入内存,agent 规划与前端接口共用,避免重复查询数据库
await warm_material_cache(persistence.db)
application.state.agent_service = AgentService(
build_graph(persistence.checkpointer), persistence.store
)
yield
finally:
# 共享客户端只在应用退出时关闭,追踪刷新放到线程避免阻塞事件循环
@@ -50,6 +54,7 @@ def create_app() -> FastAPI:
# 统一 /api 前缀,方便前端代理和反向代理只配置一条规则
application.include_router(router, prefix="/api")
application.include_router(conversation_router, prefix="/api")
application.include_router(material_router, prefix="/api")
return application