anderson-ufrj
commited on
Commit
·
58bf949
1
Parent(s):
2caad82
fix: use lazy initialization for agents to avoid import-time errors
Browse files- Change _initialize_agents to _ensure_agents_initialized with lazy loading
- Only create agent instances when first needed, not at module import
- Add error handling to continue even if some agents fail to load
- This prevents the CommunicationAgent abstract method error at startup
The real issue was that chat_service was being instantiated at module level,
causing all agents to be created during import, before everything was ready.
- src/services/chat_service.py +39 -24
src/services/chat_service.py
CHANGED
|
@@ -366,8 +366,9 @@ class ChatService:
|
|
| 366 |
self.sessions: Dict[str, ChatSession] = {}
|
| 367 |
self.messages: Dict[str, List[Dict]] = defaultdict(list)
|
| 368 |
|
| 369 |
-
# Initialize agents
|
| 370 |
-
self.
|
|
|
|
| 371 |
|
| 372 |
async def get_or_create_session(
|
| 373 |
self,
|
|
@@ -433,8 +434,11 @@ class ChatService:
|
|
| 433 |
|
| 434 |
async def get_agent_for_intent(self, intent: Intent) -> BaseAgent:
|
| 435 |
"""Get the appropriate agent for an intent"""
|
|
|
|
| 436 |
agent_id = intent.suggested_agent
|
| 437 |
-
|
|
|
|
|
|
|
| 438 |
|
| 439 |
async def update_session_investigation(
|
| 440 |
self,
|
|
@@ -446,25 +450,36 @@ class ChatService:
|
|
| 446 |
self.sessions[session_id].current_investigation_id = investigation_id
|
| 447 |
self.sessions[session_id].last_activity = datetime.utcnow()
|
| 448 |
|
| 449 |
-
def
|
| 450 |
-
"""Initialize
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 469 |
|
| 470 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 366 |
self.sessions: Dict[str, ChatSession] = {}
|
| 367 |
self.messages: Dict[str, List[Dict]] = defaultdict(list)
|
| 368 |
|
| 369 |
+
# Initialize agents lazily to avoid import-time errors
|
| 370 |
+
self.agents = None
|
| 371 |
+
self._agents_initialized = False
|
| 372 |
|
| 373 |
async def get_or_create_session(
|
| 374 |
self,
|
|
|
|
| 434 |
|
| 435 |
async def get_agent_for_intent(self, intent: Intent) -> BaseAgent:
|
| 436 |
"""Get the appropriate agent for an intent"""
|
| 437 |
+
self._ensure_agents_initialized()
|
| 438 |
agent_id = intent.suggested_agent
|
| 439 |
+
if self.agents:
|
| 440 |
+
return self.agents.get(agent_id, self.agents.get("abaporu"))
|
| 441 |
+
return None
|
| 442 |
|
| 443 |
async def update_session_investigation(
|
| 444 |
self,
|
|
|
|
| 450 |
self.sessions[session_id].current_investigation_id = investigation_id
|
| 451 |
self.sessions[session_id].last_activity = datetime.utcnow()
|
| 452 |
|
| 453 |
+
def _ensure_agents_initialized(self):
|
| 454 |
+
"""Initialize agents on first use (lazy loading)"""
|
| 455 |
+
if self._agents_initialized:
|
| 456 |
+
return
|
| 457 |
+
|
| 458 |
+
try:
|
| 459 |
+
# Import here to avoid circular imports
|
| 460 |
+
from src.agents import (
|
| 461 |
+
MasterAgent, InvestigatorAgent, AnalystAgent,
|
| 462 |
+
ReporterAgent, CommunicationAgent
|
| 463 |
+
)
|
| 464 |
+
|
| 465 |
+
# Create agent instances
|
| 466 |
+
agents = {
|
| 467 |
+
"abaporu": MasterAgent(),
|
| 468 |
+
"zumbi": InvestigatorAgent(),
|
| 469 |
+
"anita": AnalystAgent(),
|
| 470 |
+
"tiradentes": ReporterAgent(),
|
| 471 |
+
"drummond": CommunicationAgent()
|
| 472 |
+
}
|
| 473 |
+
|
| 474 |
+
# Add agent_id attribute to each agent
|
| 475 |
+
for agent_id, agent in agents.items():
|
| 476 |
+
agent.agent_id = agent_id
|
| 477 |
+
|
| 478 |
+
self.agents = agents
|
| 479 |
+
self._agents_initialized = True
|
| 480 |
|
| 481 |
+
except Exception as e:
|
| 482 |
+
logger.error(f"Failed to initialize agents: {type(e).__name__}: {e}")
|
| 483 |
+
# Create empty agents dict to prevent errors
|
| 484 |
+
self.agents = {}
|
| 485 |
+
self._agents_initialized = True
|