|
9 | 9 | from urllib.parse import urlparse |
10 | 10 |
|
11 | 11 | from uipath.core.chat import ( |
| 12 | + UiPathConversationErrorEvent, |
| 13 | + UiPathConversationErrorStartEvent, |
12 | 14 | UiPathConversationEvent, |
13 | 15 | UiPathConversationExchangeEndEvent, |
14 | 16 | UiPathConversationExchangeEvent, |
@@ -246,6 +248,51 @@ async def emit_exchange_end_event(self) -> None: |
246 | 248 | logger.error(f"Error sending conversation event to WebSocket: {e}") |
247 | 249 | raise RuntimeError(f"Failed to send conversation event: {e}") from e |
248 | 250 |
|
| 251 | + async def emit_exchange_error_event( |
| 252 | + self, error_id: str, message: str, details: Any | None = None |
| 253 | + ) -> None: |
| 254 | + """Send an exchange error event to signal an error to the UI. |
| 255 | +
|
| 256 | + Args: |
| 257 | + error_id: Identifier for the error type |
| 258 | + message: Human-readable error message |
| 259 | + details: Optional additional error details |
| 260 | + """ |
| 261 | + if self._client is None: |
| 262 | + raise RuntimeError("WebSocket client not connected. Call connect() first.") |
| 263 | + |
| 264 | + if not self._connected_event.is_set() and not self._websocket_disabled: |
| 265 | + raise RuntimeError("WebSocket client not in connected state") |
| 266 | + |
| 267 | + try: |
| 268 | + exchange_error_event = UiPathConversationEvent( |
| 269 | + conversation_id=self.conversation_id, |
| 270 | + exchange=UiPathConversationExchangeEvent( |
| 271 | + exchange_id=self.exchange_id, |
| 272 | + error=UiPathConversationErrorEvent( |
| 273 | + error_id=error_id, |
| 274 | + start=UiPathConversationErrorStartEvent( |
| 275 | + message=message, details=details |
| 276 | + ), |
| 277 | + ), |
| 278 | + ), |
| 279 | + ) |
| 280 | + |
| 281 | + event_data = exchange_error_event.model_dump( |
| 282 | + mode="json", exclude_none=True, by_alias=True |
| 283 | + ) |
| 284 | + |
| 285 | + if self._websocket_disabled: |
| 286 | + logger.info( |
| 287 | + f"SocketIOChatBridge is in debug mode. Not sending event: {json.dumps(event_data)}" |
| 288 | + ) |
| 289 | + else: |
| 290 | + await self._client.emit("ConversationEvent", event_data) |
| 291 | + |
| 292 | + except Exception as e: |
| 293 | + logger.error(f"Error sending exchange error event to WebSocket: {e}") |
| 294 | + raise RuntimeError(f"Failed to send exchange error event: {e}") from e |
| 295 | + |
249 | 296 | async def emit_interrupt_event(self, resume_trigger: UiPathResumeTrigger): |
250 | 297 | if self._client and self._connected_event.is_set(): |
251 | 298 | try: |
|
0 commit comments