feat:接入mongodb
This commit is contained in:
+16
-11
@@ -19,32 +19,37 @@ 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.persistence import open_persistence
|
||||
|
||||
from app.services.conversation import InMemoryConversationStore
|
||||
|
||||
# TODO 接入 MongoDB 后替换为持久化实现,并同步替换图的 checkpointer
|
||||
store = InMemoryConversationStore()
|
||||
application.state.conversation_store = store
|
||||
application.state.agent_service = AgentService(build_graph(), store)
|
||||
# 按配置选择 MongoDB 或进程内存,会话记录与图检查点使用同一种存储
|
||||
persistence = await open_persistence()
|
||||
application.state.conversation_store = persistence.store
|
||||
application.state.agent_service = AgentService(
|
||||
build_graph(persistence.checkpointer), persistence.store
|
||||
)
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
# 共享客户端只在应用退出时关闭,追踪刷新放到线程避免阻塞事件循环
|
||||
try:
|
||||
await close_decision_engine()
|
||||
await persistence.close()
|
||||
finally:
|
||||
try:
|
||||
await asyncio.to_thread(flush_tracing)
|
||||
await close_decision_engine()
|
||||
finally:
|
||||
await logger.complete()
|
||||
try:
|
||||
await asyncio.to_thread(flush_tracing)
|
||||
finally:
|
||||
await logger.complete()
|
||||
|
||||
|
||||
def create_app() -> FastAPI:
|
||||
application = FastAPI(title="Screen Agent API", lifespan=lifespan)
|
||||
# 统一处理请求校验、HTTP 错误和未捕获异常
|
||||
register_exception_handlers(application)
|
||||
application.include_router(router)
|
||||
application.include_router(conversation_router)
|
||||
# 统一 /api 前缀,方便前端代理和反向代理只配置一条规则
|
||||
application.include_router(router, prefix="/api")
|
||||
application.include_router(conversation_router, prefix="/api")
|
||||
return application
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user