-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathcli_run.py
More file actions
208 lines (182 loc) · 7.19 KB
/
cli_run.py
File metadata and controls
208 lines (182 loc) · 7.19 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
import asyncio
import click
from uipath.core.tracing import UiPathTraceManager
from uipath.runtime import (
UiPathExecuteOptions,
UiPathRuntimeFactoryProtocol,
UiPathRuntimeFactoryRegistry,
UiPathRuntimeProtocol,
UiPathRuntimeResult,
UiPathStreamOptions,
)
from uipath.runtime.chat import UiPathChatProtocol, UiPathChatRuntime
from uipath.runtime.context import UiPathRuntimeContext
from uipath.runtime.debug import UiPathDebugProtocol
from uipath.runtime.errors import UiPathRuntimeError
from uipath.runtime.events import UiPathRuntimeStateEvent
from uipath._cli._chat._bridge import get_chat_bridge
from uipath._cli._debug._bridge import ConsoleDebugBridge
from uipath._cli._utils._common import read_resource_overwrites_from_file
from uipath._cli._utils._debug import setup_debugging
from uipath._utils._bindings import ResourceOverwritesContext
from uipath.tracing import JsonLinesFileExporter, LlmOpsHttpExporter
from ._utils._console import ConsoleLogger
from .middlewares import Middlewares
console = ConsoleLogger()
@click.command()
@click.argument("entrypoint", required=False)
@click.argument("input", required=False, default=None)
@click.option("--resume", is_flag=True, help="Resume execution from a previous state")
@click.option(
"-f",
"--file",
required=False,
type=click.Path(exists=True),
help="File path for the .json input",
)
@click.option(
"--input-file",
required=False,
type=click.Path(exists=True),
help="Alias for '-f/--file' arguments",
)
@click.option(
"--output-file",
required=False,
type=click.Path(exists=False),
help="File path where the output will be written",
)
@click.option(
"--trace-file",
required=False,
type=click.Path(exists=False),
help="File path where the trace spans will be written (JSON Lines format)",
)
@click.option(
"--debug",
is_flag=True,
help="Enable debugging with debugpy. The process will wait for a debugger to attach.",
)
@click.option(
"--debug-port",
type=int,
default=5678,
help="Port for the debug server (default: 5678)",
)
def run(
entrypoint: str | None,
input: str | None,
resume: bool,
file: str | None,
input_file: str | None,
output_file: str | None,
trace_file: str | None,
debug: bool,
debug_port: int,
) -> None:
"""Execute the project."""
input_file = file or input_file
# Setup debugging if requested
if not setup_debugging(debug, debug_port):
console.error(f"Failed to start debug server on port {debug_port}")
result = Middlewares.next(
"run",
entrypoint,
input,
resume,
input_file=input_file,
output_file=output_file,
trace_file=trace_file,
debug=debug,
debug_port=debug_port,
)
if result.error_message:
console.error(result.error_message)
return
if result.should_continue:
if not entrypoint:
console.error("""No entrypoint specified. Please provide the path to the Python function.
Usage: `uipath run <entrypoint> <input_arguments> [-f <input_json_file_path>]`""")
return
try:
async def execute_runtime(
ctx: UiPathRuntimeContext, runtime: UiPathRuntimeProtocol
) -> UiPathRuntimeResult:
options = UiPathExecuteOptions(resume=resume)
ctx.result = await runtime.execute(
input=ctx.get_input(), options=options
)
return ctx.result
async def debug_runtime(
ctx: UiPathRuntimeContext, runtime: UiPathRuntimeProtocol
) -> UiPathRuntimeResult | None:
debug_bridge: UiPathDebugProtocol = ConsoleDebugBridge()
await debug_bridge.emit_execution_started()
options = UiPathStreamOptions(resume=resume)
async for event in runtime.stream(ctx.get_input(), options=options):
if isinstance(event, UiPathRuntimeResult):
await debug_bridge.emit_execution_completed(event)
ctx.result = event
elif isinstance(event, UiPathRuntimeStateEvent):
await debug_bridge.emit_state_update(event)
return ctx.result
async def execute() -> None:
trace_manager = UiPathTraceManager()
ctx = UiPathRuntimeContext.with_defaults(
entrypoint=entrypoint,
input=input,
input_file=file or input_file,
output_file=output_file,
trace_file=trace_file,
resume=resume,
command="run",
trace_manager=trace_manager,
)
if ctx.trace_file:
trace_manager.add_span_exporter(
JsonLinesFileExporter(ctx.trace_file)
)
async with ResourceOverwritesContext(
lambda: read_resource_overwrites_from_file(ctx.runtime_dir)
):
with ctx:
runtime: UiPathRuntimeProtocol | None = None
chat_runtime: UiPathRuntimeProtocol | None = None
factory: UiPathRuntimeFactoryProtocol | None = None
try:
factory = UiPathRuntimeFactoryRegistry.get(context=ctx)
runtime = await factory.new_runtime(
entrypoint,
ctx.conversation_id or ctx.job_id or "default",
)
if ctx.job_id:
trace_manager.add_span_exporter(LlmOpsHttpExporter())
if ctx.conversation_id and ctx.exchange_id:
chat_bridge: UiPathChatProtocol = get_chat_bridge(
context=ctx
)
chat_runtime = UiPathChatRuntime(
delegate=runtime, chat_bridge=chat_bridge
)
ctx.result = await execute_runtime(
ctx, chat_runtime or runtime
)
else:
ctx.result = await debug_runtime(ctx, runtime)
finally:
if chat_runtime:
await chat_runtime.dispose()
if runtime:
await runtime.dispose()
if factory:
await factory.dispose()
asyncio.run(execute())
except UiPathRuntimeError as e:
console.error(f"{e.error_info.title} - {e.error_info.detail}")
except Exception as e:
console.error(
f"Error: Unexpected error occurred - {str(e)}", include_traceback=True
)
console.success("Successful execution.")
if __name__ == "__main__":
run()