Skip to content

Commit 3e6e2d9

Browse files
committed
feat: add dev terminal
1 parent 41e4dea commit 3e6e2d9

6 files changed

Lines changed: 1796 additions & 1705 deletions

File tree

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "uipath-llamaindex"
3-
version = "0.0.32"
3+
version = "0.0.33"
44
description = "UiPath LlamaIndex SDK"
55
readme = { file = "README.md", content-type = "text/markdown" }
66
requires-python = ">=3.10"
@@ -9,7 +9,7 @@ dependencies = [
99
"llama-index-embeddings-azure-openai>=0.3.8",
1010
"llama-index-llms-azure-openai>=0.3.2",
1111
"openinference-instrumentation-llama-index>=4.3.0",
12-
"uipath>=2.1.10, <2.2.0",
12+
"uipath>=2.1.30, <2.2.0",
1313
]
1414
classifiers = [
1515
"Development Status :: 3 - Alpha",

src/uipath_llamaindex/_cli/_runtime/_runtime.py

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import logging
33
import os
44
import pickle
5-
from contextlib import suppress
65
from typing import Optional, cast
76

87
from llama_index.core.workflow import (
@@ -13,13 +12,6 @@
1312
WorkflowTimeoutError,
1413
)
1514
from llama_index.core.workflow.handler import WorkflowHandler # type: ignore
16-
from openinference.instrumentation.llama_index import (
17-
LlamaIndexInstrumentor,
18-
get_current_span,
19-
)
20-
from opentelemetry import trace
21-
from opentelemetry.sdk.trace import TracerProvider
22-
from opentelemetry.sdk.trace.export import BatchSpanProcessor
2315
from uipath._cli._runtime._contracts import (
2416
UiPathBaseRuntime,
2517
UiPathErrorCategory,
@@ -28,9 +20,8 @@
2820
UiPathRuntimeStatus,
2921
)
3022
from uipath._cli._runtime._hitl import HitlProcessor, HitlReader
31-
from uipath.tracing import TracingManager
3223

33-
from .._tracing._oteladapter import LlamaIndexExporter
24+
from .._utils._config import LlamaIndexConfig
3425
from ._context import UiPathLlamaIndexRuntimeContext
3526
from ._exception import UiPathLlamaIndexRuntimeError
3627

@@ -58,19 +49,6 @@ async def execute(self) -> Optional[UiPathRuntimeResult]:
5849
"""
5950
await self.validate()
6051

61-
self.trace_provider = TracerProvider()
62-
self.tracer = self.trace_provider.get_tracer("uipath.llamaindex.runtime")
63-
64-
with suppress(Exception):
65-
trace.set_tracer_provider(self.trace_provider)
66-
self.trace_provider.add_span_processor(
67-
BatchSpanProcessor(LlamaIndexExporter())
68-
)
69-
70-
LlamaIndexInstrumentor().instrument(tracer_provider=self.trace_provider)
71-
72-
TracingManager.register_current_span_provider(get_current_span)
73-
7452
try:
7553
if self.context.resume is False and self.context.job_id is None:
7654
# Delete the previous graph state file at debug time
@@ -176,8 +154,6 @@ async def execute(self) -> Optional[UiPathRuntimeResult]:
176154
detail,
177155
UiPathErrorCategory.USER,
178156
) from e
179-
finally:
180-
self.trace_provider.shutdown()
181157

182158
async def validate(self) -> None:
183159
"""Validate runtime inputs and load Llama agent configuration."""
@@ -193,12 +169,14 @@ async def validate(self) -> None:
193169
) from e
194170

195171
if self.context.config is None:
196-
raise UiPathLlamaIndexRuntimeError(
197-
"CONFIG_MISSING",
198-
"Invalid configuration",
199-
"Failed to load configuration",
200-
UiPathErrorCategory.DEPLOYMENT,
201-
)
172+
self.context.config = LlamaIndexConfig()
173+
if not self.context.config.exists:
174+
raise UiPathLlamaIndexRuntimeError(
175+
"CONFIG_MISSING",
176+
"Invalid configuration",
177+
"Failed to load configuration",
178+
UiPathErrorCategory.DEPLOYMENT,
179+
)
202180

203181
try:
204182
self.context.config.load_config()
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import asyncio
2+
from typing import Optional
3+
4+
from openinference.instrumentation.llama_index import (
5+
LlamaIndexInstrumentor,
6+
get_current_span,
7+
)
8+
from uipath._cli._dev._terminal import UiPathDevTerminal
9+
from uipath._cli._runtime._contracts import UiPathRuntimeFactory
10+
from uipath._cli._utils._console import ConsoleLogger
11+
from uipath._cli.middlewares import MiddlewareResult
12+
13+
from ._runtime._context import UiPathLlamaIndexRuntimeContext
14+
from ._runtime._runtime import UiPathLlamaIndexRuntime
15+
16+
console = ConsoleLogger()
17+
18+
19+
def llamaindex_dev_middleware(interface: Optional[str]) -> MiddlewareResult:
20+
"""Middleware to launch the developer terminal"""
21+
22+
try:
23+
if interface == "terminal":
24+
runtime_factory = UiPathRuntimeFactory(
25+
UiPathLlamaIndexRuntime, UiPathLlamaIndexRuntimeContext
26+
)
27+
runtime_factory.add_instrumentor(LlamaIndexInstrumentor, get_current_span)
28+
app = UiPathDevTerminal(runtime_factory)
29+
asyncio.run(app.run_async())
30+
else:
31+
console.error(f"Unknown interface: {interface}")
32+
except KeyboardInterrupt:
33+
console.info("Debug session interrupted by user")
34+
except Exception as e:
35+
console.error(f"Error occurred: {e}")
36+
return MiddlewareResult(
37+
should_continue=False,
38+
should_include_stacktrace=True,
39+
)
40+
41+
return MiddlewareResult(should_continue=False)

src/uipath_llamaindex/_cli/cli_run.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@
44
from typing import Optional
55

66
from dotenv import load_dotenv
7-
from uipath._cli._runtime._contracts import UiPathTraceContext
7+
from openinference.instrumentation.llama_index import (
8+
LlamaIndexInstrumentor,
9+
get_current_span,
10+
)
11+
from uipath._cli._runtime._contracts import UiPathRuntimeFactory, UiPathTraceContext
812
from uipath._cli.middlewares import MiddlewareResult
913

1014
from ._runtime._context import UiPathLlamaIndexRuntimeContext
1115
from ._runtime._exception import UiPathLlamaIndexRuntimeError
1216
from ._runtime._runtime import UiPathLlamaIndexRuntime
17+
from ._tracing._oteladapter import LlamaIndexExporter
1318
from ._utils._config import LlamaIndexConfig
1419

1520
logger = logging.getLogger(__name__)
@@ -60,8 +65,14 @@ async def execute():
6065
env["UIPATH_REQUESTING_PRODUCT"] = "uipath-python-sdk"
6166
env["UIPATH_REQUESTING_FEATURE"] = "llamaindex"
6267

63-
async with UiPathLlamaIndexRuntime.from_context(context) as runtime:
64-
await runtime.execute()
68+
runtime_factory = UiPathRuntimeFactory(UiPathLlamaIndexRuntime, UiPathLlamaIndexRuntimeContext)
69+
70+
if context.job_id:
71+
runtime_factory.add_span_exporter(LlamaIndexExporter())
72+
73+
runtime_factory.add_instrumentor(LlamaIndexInstrumentor, get_current_span)
74+
75+
await runtime_factory.execute(context)
6576

6677
asyncio.run(execute())
6778

src/uipath_llamaindex/middlewares.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from uipath._cli.middlewares import Middlewares
22

3+
from ._cli.cli_dev import llamaindex_dev_middleware
34
from ._cli.cli_init import llamaindex_init_middleware
45
from ._cli.cli_new import llamaindex_new_middleware
56
from ._cli.cli_run import llamaindex_run_middleware
@@ -10,3 +11,4 @@ def register_middleware():
1011
Middlewares.register("init", llamaindex_init_middleware)
1112
Middlewares.register("run", llamaindex_run_middleware)
1213
Middlewares.register("new", llamaindex_new_middleware)
14+
Middlewares.register("dev", llamaindex_dev_middleware)

0 commit comments

Comments
 (0)