|
34 | 34 | # Mount templates |
35 | 35 | templates = Jinja2Templates(directory="app/templates") |
36 | 36 |
|
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 |
39 | 49 |
|
40 | 50 | # Fast JSON serialization |
41 | 51 | def fast_json_dumps(obj): |
@@ -164,10 +174,15 @@ async def websocket_endpoint(websocket: WebSocket): |
164 | 174 | # === MULTI-AGENT MODE === |
165 | 175 |
|
166 | 176 | # 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 | + ) |
171 | 186 |
|
172 | 187 | domain = classification["domain"] |
173 | 188 | logger.info(f"Classified as domain: {domain} (confidence: {classification['confidence']})") |
|
0 commit comments