forked from UiPath/uipath-dev-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmock_template_runtime.py
More file actions
196 lines (165 loc) · 6.55 KB
/
Copy pathmock_template_runtime.py
File metadata and controls
196 lines (165 loc) · 6.55 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
"""Mock runtime that replays events from a JSON file."""
import asyncio
import json
import logging
from pathlib import Path
from typing import Any, AsyncGenerator, cast
from opentelemetry import trace
from uipath.core.chat import UiPathConversationMessageEvent
from uipath.runtime import (
UiPathExecuteOptions,
UiPathRuntimeEvent,
UiPathRuntimeResult,
UiPathRuntimeStatus,
UiPathStreamOptions,
)
from uipath.runtime.events import (
UiPathRuntimeMessageEvent,
UiPathRuntimeStateEvent,
)
from uipath.runtime.schema import (
UiPathRuntimeEdge,
UiPathRuntimeGraph,
UiPathRuntimeNode,
UiPathRuntimeSchema,
)
logger = logging.getLogger(__name__)
class MockTemplateRuntime:
"""Mock runtime that replays events from a JSON file."""
def __init__(
self,
events_file: str | Path,
schema_file: str | Path,
) -> None:
"""Initialize the MockTemplateRuntime."""
self.events_file = Path(events_file)
self.schema_file = Path(schema_file)
self.tracer = trace.get_tracer("uipath.dev.mock.template")
self._events: list[dict[str, Any]] = []
self._schema_data: dict[str, Any] | None = None
self._load_events()
self._load_schema()
def _load_events(self) -> None:
"""Load events from JSON file."""
with open(self.events_file, "r", encoding="utf-8") as f:
data = json.load(f)
if isinstance(data, list):
self._events = data
else:
raise ValueError("Expected JSON array of events")
def _load_schema(self) -> None:
"""Load schema from separate JSON file if provided."""
with open(self.schema_file, "r", encoding="utf-8") as f:
self._schema_data = json.load(f)
async def get_schema(self) -> UiPathRuntimeSchema:
"""Get runtime schema from the loaded data or build default."""
assert self._schema_data is not None
entry_points = self._schema_data.get("entryPoints", [])
assert entry_points is not None and len(entry_points) > 0
ep = entry_points[0]
# Build graph if present
graph = None
if "graph" in ep and ep["graph"]:
graph_data = ep["graph"]
nodes = [UiPathRuntimeNode(**node) for node in graph_data.get("nodes", [])]
edges = [UiPathRuntimeEdge(**edge) for edge in graph_data.get("edges", [])]
graph = UiPathRuntimeGraph(nodes=nodes, edges=edges)
return UiPathRuntimeSchema(
filePath=ep["filePath"],
uniqueId=ep["uniqueId"],
type=ep["type"],
input=ep["input"],
output=ep["output"],
graph=graph,
)
async def execute(
self,
input: dict[str, Any] | None = None,
options: UiPathExecuteOptions | None = None,
) -> UiPathRuntimeResult:
"""Execute by replaying all events and returning final result."""
logger.info("MockTemplateRuntime: starting execution")
# Stream all events and capture the final result
final_result = None
async for event in self.stream(
input=input, options=cast(UiPathStreamOptions, options)
):
if isinstance(event, UiPathRuntimeResult):
final_result = event
return final_result or UiPathRuntimeResult(
output={},
status=UiPathRuntimeStatus.SUCCESSFUL,
)
async def stream(
self,
input: dict[str, Any] | None = None,
options: UiPathStreamOptions | None = None,
) -> AsyncGenerator[UiPathRuntimeEvent, None]:
"""Stream events from the JSON file."""
logger.info(f"MockTemplateRuntime: streaming {len(self._events)} events")
root_span = self.tracer.start_span(
"template.stream",
attributes={
"uipath.runtime.name": "MockTemplateRuntime",
"uipath.event.count": len(self._events),
},
)
try:
for i, event_data in enumerate(self._events):
event_type = event_data.get("event_type")
# Add small delay between events for realistic streaming
if i > 0:
await asyncio.sleep(0.01)
try:
if event_type == "runtime_message":
payload_data = event_data.get("payload", {})
conversation_event = (
UiPathConversationMessageEvent.model_validate(payload_data)
)
yield UiPathRuntimeMessageEvent(
payload=conversation_event,
execution_id=event_data.get("execution_id"),
metadata=event_data.get("metadata"),
)
elif event_type == "runtime_state":
yield UiPathRuntimeStateEvent(
payload=event_data.get("payload", {}),
node_name=event_data.get("node_name"),
execution_id=event_data.get("execution_id"),
metadata=event_data.get("metadata"),
)
elif event_type == "runtime_result":
yield UiPathRuntimeResult(
output=event_data.get("output"),
status=UiPathRuntimeStatus(
event_data.get("status", "successful")
),
execution_id=event_data.get("execution_id"),
metadata=event_data.get("metadata"),
)
else:
logger.warning(f"Unknown event type: {event_type}")
except Exception as e:
logger.error(f"Error processing event {i}: {e}", exc_info=True)
continue
finally:
root_span.end()
logger.info("MockTemplateRuntime: streaming completed")
async def dispose(self) -> None:
"""Cleanup resources."""
pass
def create_template_runtime(
events_json: str | Path,
schema_json: str | Path,
) -> MockTemplateRuntime:
"""Create a template runtime from JSON files.
Args:
events_json: Path to JSON file containing events array
schema_json: Path to entry-points.json schema file
Returns:
MockTemplateRuntime instance
"""
return MockTemplateRuntime(
events_file=events_json,
schema_file=schema_json,
)