Skip to content

Commit 5f3d6c4

Browse files
author
Shreyas-Microsoft
committed
Centralized agents Management
1 parent 8486b50 commit 5f3d6c4

4 files changed

Lines changed: 266 additions & 217 deletions

File tree

src/backend/app.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
from sql_agents.agents.agent_config import AgentBaseConfig
1919
from sql_agents.helpers.agents_manager import SqlAgents
20+
from sql_agents.agent_manager import set_sql_agents, clear_sql_agents
2021

2122
import uvicorn
2223
# from agent_services.agents_routes import router as agents_router
@@ -32,22 +33,6 @@
3233
azure_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
5237
async 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()
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
Global agent manager for SQL agents.
3+
This module manages the global SQL agents instance to avoid circular imports.
4+
"""
5+
6+
import logging
7+
from typing import Optional
8+
9+
from sql_agents.helpers.agents_manager import SqlAgents
10+
11+
logger = logging.getLogger(__name__)
12+
13+
# Global variable to store the SQL agents instance
14+
_sql_agents: Optional[SqlAgents] = None
15+
16+
17+
def set_sql_agents(agents: SqlAgents) -> None:
18+
"""Set the global SQL agents instance."""
19+
global _sql_agents
20+
_sql_agents = agents
21+
logger.info("Global SQL agents instance has been set")
22+
23+
24+
def get_sql_agents() -> Optional[SqlAgents]:
25+
"""Get the global SQL agents instance."""
26+
return _sql_agents
27+
28+
29+
async def update_agent_config(convert_from: str, convert_to: str) -> None:
30+
"""Update the global agent configuration for different SQL conversion types."""
31+
global _sql_agents
32+
if _sql_agents and _sql_agents.agent_config:
33+
_sql_agents.agent_config.sql_from = convert_from
34+
_sql_agents.agent_config.sql_to = convert_to
35+
logger.info(f"Updated agent configuration: {convert_from} -> {convert_to}")
36+
else:
37+
logger.warning("SQL agents not initialized, cannot update configuration")
38+
39+
40+
async def clear_sql_agents() -> None:
41+
"""Clear the global SQL agents instance."""
42+
global _sql_agents
43+
await _sql_agents.delete_agents()
44+
_sql_agents = None
45+
logger.info("Global SQL agents instance has been cleared")

0 commit comments

Comments
 (0)