1717
1818from sql_agents .agents .agent_config import AgentBaseConfig
1919from sql_agents .helpers .agents_manager import SqlAgents
20+ from sql_agents .agent_manager import set_sql_agents , clear_sql_agents
2021
2122import uvicorn
2223# from agent_services.agents_routes import router as agents_router
3233azure_client = None
3334
3435
35- def get_sql_agents () -> SqlAgents :
36- """Get the global SQL agents instance."""
37- return sql_agents
38-
39-
40- async def update_agent_config (convert_from : str , convert_to : str ):
41- """Update the global agent configuration for different SQL conversion types."""
42- global sql_agents
43- if sql_agents and sql_agents .agent_config :
44- sql_agents .agent_config .sql_from = convert_from
45- sql_agents .agent_config .sql_to = convert_to
46- logger .logger .info (f"Updated agent configuration: { convert_from } -> { convert_to } " )
47- else :
48- logger .logger .warning ("SQL agents not initialized, cannot update configuration" )
49-
50-
5136@asynccontextmanager
5237async def lifespan (app : FastAPI ):
5338 """Manage application lifespan - startup and shutdown."""
@@ -73,6 +58,9 @@ async def lifespan(app: FastAPI):
7358
7459 # Create SQL agents
7560 sql_agents = await SqlAgents .create (agent_config )
61+
62+ # Set the global agents instance
63+ set_sql_agents (sql_agents )
7664 logger .logger .info ("SQL agents initialized successfully." )
7765
7866 except Exception as exc :
@@ -84,9 +72,12 @@ async def lifespan(app: FastAPI):
8472 # Shutdown
8573 try :
8674 if sql_agents :
87- logger .logger .info ("Cleaning up SQL agents..." )
75+ logger .logger .info ("Application shutting down - cleaning up SQL agents..." )
8876 await sql_agents .delete_agents ()
8977 logger .logger .info ("SQL agents cleaned up successfully." )
78+
79+ # Clear the global agents instance
80+ await clear_sql_agents ()
9081
9182 if azure_client :
9283 await azure_client .close ()
0 commit comments