-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathruntime.py
More file actions
94 lines (81 loc) · 3.08 KB
/
runtime.py
File metadata and controls
94 lines (81 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"""Chat runtime implementation."""
import logging
from typing import Any, AsyncGenerator, cast
from uipath.runtime.base import (
UiPathExecuteOptions,
UiPathRuntimeProtocol,
UiPathStreamOptions,
)
from uipath.runtime.chat.protocol import UiPathChatProtocol
from uipath.runtime.events import (
UiPathRuntimeEvent,
UiPathRuntimeMessageEvent,
)
from uipath.runtime.result import (
UiPathRuntimeResult,
UiPathRuntimeStatus,
)
from uipath.runtime.schema import UiPathRuntimeSchema
logger = logging.getLogger(__name__)
class UiPathChatRuntime:
"""Specialized runtime for chat mode that streams message events to a chat bridge."""
def __init__(
self,
delegate: UiPathRuntimeProtocol,
chat_bridge: UiPathChatProtocol,
):
"""Initialize the UiPathChatRuntime.
Args:
delegate: The underlying runtime to wrap
chat_bridge: Bridge for chat event communication
"""
super().__init__()
self.delegate = delegate
self.chat_bridge = chat_bridge
async def execute(
self,
input: dict[str, Any] | None = None,
options: UiPathExecuteOptions | None = None,
) -> UiPathRuntimeResult:
"""Execute the workflow with chat support."""
result: UiPathRuntimeResult | None = None
async for event in self.stream(input, cast(UiPathStreamOptions, options)):
if isinstance(event, UiPathRuntimeResult):
result = event
return (
result
if result
else UiPathRuntimeResult(status=UiPathRuntimeStatus.SUSPENDED)
)
async def stream(
self,
input: dict[str, Any] | None = None,
options: UiPathStreamOptions | None = None,
) -> AsyncGenerator[UiPathRuntimeEvent, None]:
"""Stream execution events with chat support."""
await self.chat_bridge.connect()
async for event in self.delegate.stream(input, options=options):
if isinstance(event, UiPathRuntimeResult):
# In chat mode, convert successful completion to SUSPENDED
# Breakpoints and resumable triggers are already suspended
# Faulted jobs remain faulted
if event.status == UiPathRuntimeStatus.SUCCESSFUL:
yield UiPathRuntimeResult(
status=UiPathRuntimeStatus.SUSPENDED,
output=event.output,
)
else:
yield event
elif isinstance(event, UiPathRuntimeMessageEvent):
if event.payload:
await self.chat_bridge.emit_message_event(event.payload)
yield event
async def get_schema(self) -> UiPathRuntimeSchema:
"""Get schema from the delegate runtime."""
return await self.delegate.get_schema()
async def dispose(self) -> None:
"""Cleanup runtime resources."""
try:
await self.chat_bridge.disconnect()
except Exception as e:
logger.warning(f"Error disconnecting chat bridge: {e}")