|
9 | 9 | from common.models.messages_kernel import TeamConfiguration |
10 | 10 | from semantic_kernel.agents.orchestration.magentic import MagenticOrchestration |
11 | 11 | from semantic_kernel.agents.runtime import InProcessRuntime |
| 12 | +from azure.core.exceptions import ResourceNotFoundError |
12 | 13 |
|
13 | 14 | # Create custom execution settings to fix schema issues |
14 | 15 | from semantic_kernel.connectors.ai.open_ai import ( |
@@ -76,20 +77,28 @@ def _user_aware_agent_callback(user_id: str): |
76 | 77 | """Factory method that creates a callback with captured user_id""" |
77 | 78 |
|
78 | 79 | def callback(message: ChatMessageContent): |
79 | | - return agent_response_callback(message, user_id) |
80 | | - |
| 80 | + try: |
| 81 | + return agent_response_callback(message, user_id) |
| 82 | + except Exception as e: |
| 83 | + logger = logging.getLogger(f"{__name__}.OrchestrationManager") |
| 84 | + logger.error(f"Error in agent response callback: {e}") |
81 | 85 | return callback |
82 | 86 |
|
83 | 87 | @staticmethod |
84 | 88 | def _user_aware_streaming_callback(user_id: str): |
85 | 89 | """Factory method that creates a streaming callback with captured user_id""" |
86 | 90 |
|
87 | 91 | async def callback( |
88 | | - streaming_message: StreamingChatMessageContent, is_final: bool |
| 92 | + streaming_message: StreamingChatMessageContent, |
| 93 | + is_final: bool |
89 | 94 | ): |
90 | | - return await streaming_agent_response_callback( |
91 | | - streaming_message, is_final, user_id |
92 | | - ) |
| 95 | + try: |
| 96 | + return await streaming_agent_response_callback( |
| 97 | + streaming_message, is_final, user_id |
| 98 | + ) |
| 99 | + except Exception as e: |
| 100 | + logger = logging.getLogger(f"{__name__}.OrchestrationManager") |
| 101 | + logger.error(f"Error in streaming agent response callback: {e}") |
93 | 102 |
|
94 | 103 | return callback |
95 | 104 |
|
@@ -172,14 +181,110 @@ async def run_orchestration(self, user_id, input_task) -> None: |
172 | 181 | message_type=WebsocketMessageType.FINAL_RESULT_MESSAGE, |
173 | 182 | ) |
174 | 183 | self.logger.info(f"Final result sent via WebSocket to user {user_id}") |
| 184 | + |
| 185 | + except ResourceNotFoundError as e: |
| 186 | + self.logger.error(f"Agent not found: {e}") |
| 187 | + self.logger.info(f"Error: {e}") |
| 188 | + self.logger.info(f"Error type: {type(e).__name__}") |
| 189 | + if hasattr(e, "__dict__"): |
| 190 | + self.logger.info(f"Error attributes: {e.__dict__}") |
| 191 | + self.logger.info("=" * 50) |
| 192 | + error_content = "**Attention:** The agent is currently unavailable. Please check if it was deleted or recreated.\n\nIf yes, please create a new plan from the home page." |
| 193 | + self.logger.info(f"🔴 Sending error message to user {user_id}: {error_content}") |
| 194 | + |
| 195 | + await connection_config.send_status_update_async( |
| 196 | + { |
| 197 | + "type": WebsocketMessageType.ERROR_MESSAGE, |
| 198 | + "data": { |
| 199 | + "content": error_content, |
| 200 | + "status": "error", |
| 201 | + "timestamp": asyncio.get_event_loop().time(), |
| 202 | + }, |
| 203 | + }, |
| 204 | + user_id, |
| 205 | + message_type=WebsocketMessageType.ERROR_MESSAGE, |
| 206 | + ) |
| 207 | + self.logger.info(f"✅ Error message sent via WebSocket to user {user_id}") |
| 208 | + |
175 | 209 | except Exception as e: |
| 210 | + self.logger.error(f"Error processing final result: {e}") |
| 211 | + # Send error message to user |
| 212 | + await connection_config.send_status_update_async( |
| 213 | + { |
| 214 | + "type": WebsocketMessageType.ERROR_MESSAGE, |
| 215 | + "data": { |
| 216 | + "content": "**Attention:** An error occurred while processing the final response.\n\nPlease try creating a new plan from the home page.", |
| 217 | + "status": "error", |
| 218 | + "timestamp": asyncio.get_event_loop().time(), |
| 219 | + }, |
| 220 | + }, |
| 221 | + user_id, |
| 222 | + message_type=WebsocketMessageType.ERROR_MESSAGE, |
| 223 | + ) |
| 224 | + |
| 225 | + except RuntimeError as e: |
| 226 | + if "did not return any response" in str(e): |
| 227 | + self.logger.error(f"Agent failed: {e}") |
| 228 | + # Send user-friendly error via WebSocket |
| 229 | + await connection_config.send_status_update_async( |
| 230 | + { |
| 231 | + "type": WebsocketMessageType.ERROR_MESSAGE, |
| 232 | + "data": { |
| 233 | + "content": "**Attention:** I'm having trouble connecting to the agent right now.\n\nPlease try creating a new plan from the home page or try again later.", |
| 234 | + "status": "error", |
| 235 | + "timestamp": asyncio.get_event_loop().time(), |
| 236 | + }, |
| 237 | + }, |
| 238 | + user_id, |
| 239 | + message_type=WebsocketMessageType.ERROR_MESSAGE, |
| 240 | + ) |
| 241 | + else: |
| 242 | + self.logger.exception("Unexpected RuntimeError") |
176 | 243 | self.logger.info(f"Error: {e}") |
177 | 244 | self.logger.info(f"Error type: {type(e).__name__}") |
178 | 245 | if hasattr(e, "__dict__"): |
179 | 246 | self.logger.info(f"Error attributes: {e.__dict__}") |
180 | 247 | self.logger.info("=" * 50) |
| 248 | + # Fallback error message |
| 249 | + await connection_config.send_status_update_async( |
| 250 | + { |
| 251 | + "type": WebsocketMessageType.ERROR_MESSAGE, |
| 252 | + "data": { |
| 253 | + "content": "**Attention:** Something went wrong.\n\nPlease try creating a new plan from the home page or try again later.", |
| 254 | + "status": "error", |
| 255 | + "timestamp": asyncio.get_event_loop().time(), |
| 256 | + }, |
| 257 | + }, |
| 258 | + user_id, |
| 259 | + message_type=WebsocketMessageType.ERROR_MESSAGE, |
| 260 | + ) |
181 | 261 |
|
182 | 262 | except Exception as e: |
183 | | - self.logger.error(f"Unexpected error: {e}") |
| 263 | + self.logger.error(f"🚨 Unexpected error during orchestration: {e}") |
| 264 | + self.logger.info(f"Error: {e}") |
| 265 | + self.logger.info(f"Error type: {type(e).__name__}") |
| 266 | + if hasattr(e, "__dict__"): |
| 267 | + self.logger.info(f"Error attributes: {e.__dict__}") |
| 268 | + self.logger.info("=" * 50) |
| 269 | + |
| 270 | + error_content = "**Attention:** Something went wrong.\n\nPlease try creating a new plan from the home page or try again later." |
| 271 | + |
| 272 | + self.logger.info(f"🔴 Sending error message to user {user_id}: {error_content}") |
| 273 | + |
| 274 | + await connection_config.send_status_update_async( |
| 275 | + { |
| 276 | + "type": WebsocketMessageType.ERROR_MESSAGE, |
| 277 | + "data": { |
| 278 | + "content": error_content, |
| 279 | + "status": "error", |
| 280 | + "timestamp": asyncio.get_event_loop().time(), |
| 281 | + }, |
| 282 | + }, |
| 283 | + user_id, |
| 284 | + message_type=WebsocketMessageType.ERROR_MESSAGE, |
| 285 | + ) |
| 286 | + |
| 287 | + self.logger.info(f"✅ Error message sent via WebSocket to user {user_id}") |
| 288 | + |
184 | 289 | finally: |
185 | 290 | await runtime.stop_when_idle() |
0 commit comments