-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontext.py
More file actions
352 lines (294 loc) · 12.8 KB
/
context.py
File metadata and controls
352 lines (294 loc) · 12.8 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
"""Context information passed throughout the runtime execution."""
import json
import logging
import os
from functools import cached_property
from pathlib import Path
from typing import Any
from pydantic import BaseModel, ConfigDict
from uipath.core.errors import UiPathFaultedTriggerError
from uipath.core.tracing import UiPathTraceManager
from uipath.runtime.errors import (
UiPathErrorCategory,
UiPathErrorCode,
UiPathErrorContract,
UiPathRuntimeError,
)
from uipath.runtime.logging._interceptor import UiPathRuntimeLogsInterceptor
from uipath.runtime.result import UiPathRuntimeResult, UiPathRuntimeStatus
logger = logging.getLogger(__name__)
class UiPathRuntimeContext(BaseModel):
"""Context information passed throughout the runtime execution."""
entrypoint: str | None = None
input: str | None = None
resume: bool = False
command: str | None = None
job_id: str | None = None
conversation_id: str | None = None
exchange_id: str | None = None
message_id: str | None = None
mcp_server_id: str | None = None
mcp_server_slug: str | None = None
tenant_id: str | None = None
org_id: str | None = None
folder_key: str | None = None
process_key: str | None = None
config_path: str = "uipath.json"
runtime_dir: str | None = "__uipath"
result_file: str = "output.json"
state_file: str = "state.db"
input_file: str | None = None
output_file: str | None = None
trace_file: str | None = None
logs_file: str | None = "execution.log"
logs_min_level: str | None = "INFO"
result: UiPathRuntimeResult | None = None
trace_manager: UiPathTraceManager | None = None
model_config = ConfigDict(arbitrary_types_allowed=True, extra="allow")
def get_input(self) -> dict[str, Any] | None:
"""Get parsed input data.
Priority:
1. If input_file is specified, read and parse from file
2. Otherwise, parse the input string
Returns:
Parsed input dictionary
Raises:
UiPathRuntimeError: If JSON is invalid or file not found
"""
if self.input_file:
return self._read_input_from_file(self.input_file)
return self._parse_input_string(self.input)
def _read_input_from_file(self, file_path: str) -> dict[str, Any]:
"""Read and parse input from JSON file."""
path = Path(file_path)
# Validate file extension
if path.suffix != ".json":
raise UiPathRuntimeError(
code=UiPathErrorCode.INVALID_INPUT_FILE_EXTENSION,
title="Invalid Input File Extension",
detail=f"The provided input file must be in JSON format. Got: {path.suffix}",
category=UiPathErrorCategory.USER,
)
# Check if file exists
if not path.exists():
raise UiPathRuntimeError(
code=UiPathErrorCode.MISSING_INPUT_FILE,
title="Input File Not Found",
detail=f"The input file does not exist: {file_path}",
category=UiPathErrorCategory.USER,
)
try:
with open(path) as f:
return json.load(f)
except json.JSONDecodeError as e:
raise UiPathRuntimeError(
code=UiPathErrorCode.INPUT_INVALID_JSON,
title="Invalid JSON in Input File",
detail=f"The input file contains invalid JSON: {e}",
category=UiPathErrorCategory.USER,
) from e
except Exception as e:
raise UiPathRuntimeError(
code=UiPathErrorCode.INPUT_INVALID_JSON,
title="Failed to Read Input File",
detail=f"Error reading input file: {e}",
category=UiPathErrorCategory.SYSTEM,
) from e
def _parse_input_string(self, input_str: str | None) -> dict[str, Any] | None:
"""Parse input from JSON string."""
if not input_str or input_str.strip() == "":
return None
try:
parsed = json.loads(input_str)
# Ensure we return a dict
if not isinstance(parsed, dict):
raise UiPathRuntimeError(
code=UiPathErrorCode.INPUT_INVALID_JSON,
title="Invalid Input Type",
detail=f"Input must be a JSON object, got: {type(parsed).__name__}",
category=UiPathErrorCategory.USER,
)
return parsed
except json.JSONDecodeError as e:
raise UiPathRuntimeError(
code=UiPathErrorCode.INPUT_INVALID_JSON,
title="Invalid JSON Input",
detail=f"The input data is not valid JSON: {e}",
category=UiPathErrorCategory.USER,
) from e
def __enter__(self):
"""Enter method called when entering the 'async with' block.
Initializes and prepares the runtime contextual environment.
Returns:
The runtime context instance
"""
# Intercept all stdout/stderr/logs
# Write to file (runtime), stdout (debug) or log handler (if provided)
self.logs_interceptor = UiPathRuntimeLogsInterceptor(
min_level=self.logs_min_level,
dir=self.runtime_dir,
file=self.logs_file,
job_id=self.job_id,
)
self.logs_interceptor.setup()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
"""Async exit method called when exiting the 'async with' block.
Cleans up resources and handles any exceptions.
Always writes output file regardless of whether execution was successful,
suspended, or encountered an error.
"""
try:
if self.result is None:
self.result = UiPathRuntimeResult()
if exc_type:
# Create error info from exception
match exc_type:
case UiPathFaultedTriggerError():
error_info = UiPathRuntimeError.from_resume_trigger_error(
exc_type
).error_info
case UiPathRuntimeError():
error_info = exc_val.error_info
case _:
# Generic error
error_info = UiPathErrorContract(
code=f"ERROR_{exc_type.__name__}",
title=f"Runtime error: {exc_type.__name__}",
detail=str(exc_val),
category=UiPathErrorCategory.UNKNOWN,
)
self.result.status = UiPathRuntimeStatus.FAULTED
self.result.error = error_info
content = self.result.to_dict()
# Always write output file at runtime, except for inner runtimes
# Inner runtimes have execution_id
if self.job_id:
with open(self.result_file_path, "w") as f:
json.dump(content, f, indent=2, default=str)
# Write the execution output to file if requested
if self.output_file:
output_payload = content.get("output", {})
with open(self.output_file, "w") as f:
json.dump(output_payload, f, default=str)
# Don't suppress exceptions
return False
except Exception as e:
logger.error(f"Error during runtime shutdown: {str(e)}")
# Create a fallback error result if we fail during cleanup
if not isinstance(e, UiPathRuntimeError):
error_info = UiPathErrorContract(
code="RUNTIME_SHUTDOWN_ERROR",
title="Runtime shutdown failed",
detail=f"Error: {str(e)}",
category=UiPathErrorCategory.SYSTEM,
)
else:
error_info = e.error_info
# Last-ditch effort to write error output
try:
error_result = UiPathRuntimeResult(
status=UiPathRuntimeStatus.FAULTED, error=error_info
)
error_result_content = error_result.to_dict()
if self.job_id:
with open(self.result_file_path, "w") as f:
json.dump(error_result_content, f, indent=2, default=str)
except Exception as write_error:
logger.error(f"Failed to write error output file: {str(write_error)}")
raise
# Re-raise as RuntimeError if it's not already a UiPathRuntimeError
if not isinstance(e, UiPathRuntimeError):
raise RuntimeError(
error_info.code,
error_info.title,
error_info.detail,
error_info.category,
) from e
raise
finally:
# Restore original logging
if hasattr(self, "logs_interceptor"):
self.logs_interceptor.teardown()
@cached_property
def result_file_path(self) -> str:
"""Get the full path to the result file."""
if self.runtime_dir and self.result_file:
os.makedirs(self.runtime_dir, exist_ok=True)
return os.path.join(self.runtime_dir, self.result_file)
return os.path.join("__uipath", "output.json")
@cached_property
def state_file_path(self) -> str:
"""Get the full path to the state file."""
if self.runtime_dir and self.state_file:
os.makedirs(self.runtime_dir, exist_ok=True)
return os.path.join(self.runtime_dir, self.state_file)
return os.path.join("__uipath", "state.db")
@classmethod
def with_defaults(
cls, config_path: str | None = None, **kwargs
) -> "UiPathRuntimeContext":
"""Construct a context with defaults, reading env vars and config file."""
resolved_config_path = config_path or os.environ.get(
"UIPATH_CONFIG_PATH", "uipath.json"
)
base = cls.from_config(resolved_config_path)
base.config_path = resolved_config_path
bool_map = {"true": True, "false": False}
tracing_enabled = os.environ.get("UIPATH_TRACING_ENABLED", True)
if isinstance(tracing_enabled, str) and tracing_enabled.lower() in bool_map:
tracing_enabled = bool_map[tracing_enabled.lower()]
# Apply defaults from env
base.job_id = os.environ.get("UIPATH_JOB_KEY")
base.logs_min_level = os.environ.get("LOG_LEVEL", "INFO")
base.org_id = os.environ.get("UIPATH_ORGANIZATION_ID")
base.tenant_id = os.environ.get("UIPATH_TENANT_ID")
base.process_key = os.environ.get("UIPATH_PROCESS_UUID")
base.folder_key = os.environ.get("UIPATH_FOLDER_KEY")
# Override with kwargs
for k, v in kwargs.items():
setattr(base, k, v)
return base
@classmethod
def from_config(
cls, config_path: str | None = None, **kwargs
) -> "UiPathRuntimeContext":
"""Load configuration from uipath.json file."""
path = config_path or "uipath.json"
config: dict[str, Any] = {}
if os.path.exists(path):
with open(path, "r") as f:
config = json.load(f)
instance = cls()
mapping = {
"dir": "runtime_dir",
"outputFile": "result_file", # we need this to maintain back-compat with serverless runtime
"stateFile": "state_file",
"logsFile": "logs_file",
}
fps_mappings = {
"conversationalService.conversationId": "conversation_id",
"conversationalService.exchangeId": "exchange_id",
"conversationalService.messageId": "message_id",
"mcpServer.id": "mcp_server_id",
"mcpServer.slug": "mcp_server_slug",
}
attributes_set = set()
runtime_config = config.get("runtime", {})
fps_config = config.get("fpsProperties", {})
if runtime_config or fps_config:
# Handle runtime mapping
for config_key, attr_name in mapping.items():
if config_key in runtime_config and hasattr(instance, attr_name):
attributes_set.add(attr_name)
setattr(instance, attr_name, runtime_config[config_key])
# Handle fpsProperties mapping
for config_key, attr_name in fps_mappings.items():
if config_key in fps_config and hasattr(instance, attr_name):
attributes_set.add(attr_name)
setattr(instance, attr_name, fps_config[config_key])
for _, attr_name in mapping.items():
if attr_name in kwargs and hasattr(instance, attr_name):
if attr_name not in attributes_set:
setattr(instance, attr_name, kwargs[attr_name])
return instance