|
1 | 1 | """Create and configure the FastAPI application.""" |
| 2 | +from contextlib import asynccontextmanager |
| 3 | + |
2 | 4 | from api.api_routes import router as backend_router |
3 | 5 |
|
| 6 | +from azure.identity.aio import DefaultAzureCredential |
| 7 | + |
| 8 | +from common.config.config import app_config |
4 | 9 | from common.logger.app_logger import AppLogger |
5 | 10 |
|
6 | 11 | from dotenv import load_dotenv |
7 | 12 |
|
8 | 13 | from fastapi import FastAPI |
9 | 14 | from fastapi.middleware.cors import CORSMiddleware |
10 | 15 |
|
| 16 | +from semantic_kernel.agents.azure_ai.azure_ai_agent import AzureAIAgent # pylint: disable=E0611 |
| 17 | + |
| 18 | +from sql_agents.agent_manager import clear_sql_agents, set_sql_agents |
| 19 | +from sql_agents.agents.agent_config import AgentBaseConfig |
| 20 | +from sql_agents.helpers.agents_manager import SqlAgents |
| 21 | + |
11 | 22 | import uvicorn |
12 | 23 | # from agent_services.agents_routes import router as agents_router |
13 | 24 |
|
|
17 | 28 | # Configure logging |
18 | 29 | logger = AppLogger("app") |
19 | 30 |
|
| 31 | +# Global variables for agents |
| 32 | +sql_agents: SqlAgents = None |
| 33 | +azure_client = None |
| 34 | + |
| 35 | + |
| 36 | +@asynccontextmanager |
| 37 | +async def lifespan(app: FastAPI): |
| 38 | + """Manage application lifespan - startup and shutdown.""" |
| 39 | + global sql_agents, azure_client |
| 40 | + |
| 41 | + # Startup |
| 42 | + try: |
| 43 | + logger.logger.info("Initializing SQL agents...") |
| 44 | + |
| 45 | + # Create Azure credentials and client |
| 46 | + creds = DefaultAzureCredential() |
| 47 | + azure_client = AzureAIAgent.create_client( |
| 48 | + credential=creds, |
| 49 | + endpoint=app_config.ai_project_endpoint |
| 50 | + ) |
| 51 | + |
| 52 | + # Setup agent configuration with default conversion settings |
| 53 | + agent_config = AgentBaseConfig( |
| 54 | + project_client=azure_client, |
| 55 | + sql_from="informix", # Default source dialect |
| 56 | + sql_to="tsql" # Default target dialect |
| 57 | + ) |
| 58 | + |
| 59 | + # Create SQL agents |
| 60 | + sql_agents = await SqlAgents.create(agent_config) |
| 61 | + |
| 62 | + # Set the global agents instance |
| 63 | + set_sql_agents(sql_agents) |
| 64 | + logger.logger.info("SQL agents initialized successfully.") |
| 65 | + |
| 66 | + except Exception as exc: |
| 67 | + logger.logger.error("Failed to initialize SQL agents: %s", exc) |
| 68 | + # Don't raise the exception to allow the app to start even if agents fail |
| 69 | + |
| 70 | + yield # Application runs here |
| 71 | + |
| 72 | + # Shutdown |
| 73 | + try: |
| 74 | + if sql_agents: |
| 75 | + logger.logger.info("Application shutting down - cleaning up SQL agents...") |
| 76 | + await sql_agents.delete_agents() |
| 77 | + logger.logger.info("SQL agents cleaned up successfully.") |
| 78 | + |
| 79 | + # Clear the global agents instance |
| 80 | + await clear_sql_agents() |
| 81 | + |
| 82 | + if azure_client: |
| 83 | + await azure_client.close() |
| 84 | + |
| 85 | + except Exception as exc: |
| 86 | + logger.logger.error("Error during agent cleanup: %s", exc) |
| 87 | + |
20 | 88 |
|
21 | 89 | def create_app() -> FastAPI: |
22 | 90 | """Create and return the FastAPI application instance.""" |
23 | | - app = FastAPI(title="Code Gen Accelerator", version="1.0.0") |
| 91 | + app = FastAPI(title="Code Gen Accelerator", version="1.0.0", lifespan=lifespan) |
24 | 92 |
|
25 | 93 | # Configure CORS |
26 | 94 | app.add_middleware( |
|
0 commit comments