Skip to content

Commit 4d326e5

Browse files
add chat runtime
1 parent b7ba27b commit 4d326e5

File tree

3 files changed

+99
-2
lines changed

3 files changed

+99
-2
lines changed

src/uipath/runtime/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
UiPathStreamOptions,
99
)
1010
from uipath.runtime.chat.bridge import UiPathChatBridgeProtocol
11+
from uipath.runtime.chat.runtime import UiPathChatRuntime
1112
from uipath.runtime.context import UiPathRuntimeContext
1213
from uipath.runtime.debug.breakpoint import UiPathBreakpointResult
1314
from uipath.runtime.debug.bridge import UiPathDebugBridgeProtocol
@@ -68,4 +69,5 @@
6869
"UiPathStreamNotSupportedError",
6970
"UiPathResumeTriggerName",
7071
"UiPathChatBridgeProtocol",
72+
"UiPathChatRuntime",
7173
]
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
"""Chat bridge protocol for conversational agents."""
1+
"""Chat bridge protocol and runtime for conversational agents."""
22

33
from uipath.runtime.chat.bridge import UiPathChatBridgeProtocol
4+
from uipath.runtime.chat.runtime import UiPathChatRuntime
45

5-
__all__ = ["UiPathChatBridgeProtocol"]
6+
__all__ = ["UiPathChatBridgeProtocol", "UiPathChatRuntime"]

src/uipath/runtime/chat/runtime.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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

Comments
 (0)