|
| 1 | +"""Chat runtime implementation.""" |
| 2 | + |
| 3 | +import logging |
| 4 | +from typing import Any, AsyncGenerator, cast |
| 5 | + |
| 6 | +from uipath.runtime.base import ( |
| 7 | + UiPathExecuteOptions, |
| 8 | + UiPathRuntimeProtocol, |
| 9 | + UiPathStreamNotSupportedError, |
| 10 | + UiPathStreamOptions, |
| 11 | +) |
| 12 | +from uipath.runtime.chat.bridge import UiPathChatBridgeProtocol |
| 13 | +from uipath.runtime.events import ( |
| 14 | + UiPathRuntimeEvent, |
| 15 | + UiPathRuntimeMessageEvent, |
| 16 | +) |
| 17 | +from uipath.runtime.result import ( |
| 18 | + UiPathRuntimeResult, |
| 19 | + UiPathRuntimeStatus, |
| 20 | +) |
| 21 | +from uipath.runtime.schema import UiPathRuntimeSchema |
| 22 | + |
| 23 | +logger = logging.getLogger(__name__) |
| 24 | + |
| 25 | + |
| 26 | +class UiPathChatRuntime: |
| 27 | + """Specialized runtime for chat mode that streams message events to a chat bridge.""" |
| 28 | + |
| 29 | + def __init__( |
| 30 | + self, |
| 31 | + delegate: UiPathRuntimeProtocol, |
| 32 | + chat_bridge: UiPathChatBridgeProtocol, |
| 33 | + ): |
| 34 | + """Initialize the UiPathChatRuntime. |
| 35 | +
|
| 36 | + Args: |
| 37 | + delegate: The underlying runtime to wrap |
| 38 | + chat_bridge: Bridge for chat event communication |
| 39 | + """ |
| 40 | + super().__init__() |
| 41 | + self.delegate = delegate |
| 42 | + self.chat_bridge = chat_bridge |
| 43 | + |
| 44 | + async def execute( |
| 45 | + self, |
| 46 | + input: dict[str, Any] | None = None, |
| 47 | + options: UiPathExecuteOptions | None = None, |
| 48 | + ) -> UiPathRuntimeResult: |
| 49 | + """Execute the workflow with chat support.""" |
| 50 | + result: UiPathRuntimeResult | None = None |
| 51 | + async for event in self.stream(input, cast(UiPathStreamOptions, options)): |
| 52 | + if isinstance(event, UiPathRuntimeResult): |
| 53 | + result = event |
| 54 | + |
| 55 | + return ( |
| 56 | + result |
| 57 | + if result |
| 58 | + else UiPathRuntimeResult(status=UiPathRuntimeStatus.SUCCESSFUL) |
| 59 | + ) |
| 60 | + |
| 61 | + async def stream( |
| 62 | + self, |
| 63 | + input: dict[str, Any] | None = None, |
| 64 | + options: UiPathStreamOptions | None = None, |
| 65 | + ) -> AsyncGenerator[UiPathRuntimeEvent, None]: |
| 66 | + """Stream execution events with chat support.""" |
| 67 | + await self.chat_bridge.connect() |
| 68 | + |
| 69 | + result: UiPathRuntimeResult | None = None |
| 70 | + |
| 71 | + async for event in self.delegate.stream(input, options=options): |
| 72 | + # Send message events to chat bridge |
| 73 | + if isinstance(event, UiPathRuntimeMessageEvent): |
| 74 | + if event.payload: |
| 75 | + await self.chat_bridge.emit_message_event(event.payload) |
| 76 | + |
| 77 | + yield event |
| 78 | + |
| 79 | + if isinstance(event, UiPathRuntimeResult): |
| 80 | + result = event |
| 81 | + |
| 82 | + if result: |
| 83 | + logger.info(f"Chat execution completed with status: {result.status}") |
| 84 | + |
| 85 | + # Always disconnect from chat bridge |
| 86 | + await self.chat_bridge.disconnect("", "") |
| 87 | + |
| 88 | + async def get_schema(self) -> UiPathRuntimeSchema: |
| 89 | + """Get schema from the delegate runtime.""" |
| 90 | + return await self.delegate.get_schema() |
| 91 | + |
| 92 | + async def dispose(self) -> None: |
| 93 | + """Dispose the delegate runtime.""" |
| 94 | + await self.delegate.dispose() |
0 commit comments