__init__.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # !/usr/bin/ python
  2. # -*- coding: utf-8 -*-
  3. '''
  4. @Project : lq-agent-api
  5. @File :__init__.py.py
  6. @IDE :PyCharm
  7. @Author :
  8. @Date :2025/7/10 17:04
  9. '''
  10. import uuid
  11. from contextlib import asynccontextmanager
  12. from contextvars import ContextVar
  13. from fastapi import FastAPI, APIRouter
  14. from foundation.base.mysql.async_mysql_conn_pool import AsyncMySQLPool
  15. from foundation.logger.loggering import server_logger
  16. @asynccontextmanager
  17. async def lifespan(app: FastAPI):
  18. # 启动时加载工具
  19. #await mcp_server.get_mcp_tools()
  20. # 全局数据库连接池实例
  21. async_db_pool = AsyncMySQLPool()
  22. await async_db_pool.initialize()
  23. app.state.async_db_pool = async_db_pool
  24. server_logger.info(f"✅ MySQL数据库连接池:{app.state.async_db_pool}")
  25. yield
  26. # 关闭时清理
  27. if async_db_pool and async_db_pool.close:
  28. await async_db_pool.close()
  29. test_router = APIRouter(prefix="/test", tags=["agent"])
  30. current_operation_id: ContextVar[str] = ContextVar("operation_id", default=str(uuid.uuid4()))
  31. def get_operation_id() -> str:
  32. """依赖项:获取当前操作ID"""
  33. return current_operation_id.get()