Skip to content

Commit 521a5e3

Browse files
authored
Implement lazy initialization for HandoffService
Refactor handoff service initialization to lazy loading.
1 parent e9cb592 commit 521a5e3

1 file changed

Lines changed: 21 additions & 6 deletions

File tree

src/chat_app_multi_agent.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,18 @@
3434
# Mount templates
3535
templates = Jinja2Templates(directory="app/templates")
3636

37-
# Initialize handoff service for multi-agent routing
38-
handoff_service = HandoffService()
37+
# Lazy initialization for handoff service
38+
_handoff_service = None
39+
40+
def get_handoff_service():
41+
global _handoff_service
42+
if _handoff_service is None:
43+
try:
44+
_handoff_service = HandoffService()
45+
except Exception as e:
46+
logger.error(f"Failed to initialize HandoffService: {e}")
47+
return None
48+
return _handoff_service
3949

4050
# Fast JSON serialization
4151
def fast_json_dumps(obj):
@@ -164,10 +174,15 @@ async def websocket_endpoint(websocket: WebSocket):
164174
# === MULTI-AGENT MODE ===
165175

166176
# Step 1: Classify intent
167-
classification = handoff_service.classify_intent(
168-
user_message=user_message,
169-
conversation_history=conversation_history
170-
)
177+
svc = get_handoff_service()
178+
if not svc:
179+
logger.warning("HandoffService unavailable; defaulting to 'cora'")
180+
classification = {"domain": "cora", "confidence": 1.0, "reasoning": "HandoffService unavailable"}
181+
else:
182+
classification = svc.classify_intent(
183+
user_message=user_message,
184+
conversation_history=conversation_history
185+
)
171186

172187
domain = classification["domain"]
173188
logger.info(f"Classified as domain: {domain} (confidence: {classification['confidence']})")

0 commit comments

Comments
 (0)