|
| 1 | +"""Debug runtime implementation.""" |
| 2 | + |
| 3 | +import logging |
| 4 | +from typing import Generic, Optional, TypeVar |
| 5 | + |
| 6 | +from uipath.runtime import ( |
| 7 | + UiPathBaseRuntime, |
| 8 | + UiPathBreakpointResult, |
| 9 | + UiPathRuntimeContext, |
| 10 | + UiPathRuntimeFactory, |
| 11 | + UiPathRuntimeResult, |
| 12 | + UiPathRuntimeStatus, |
| 13 | + UiPathStreamNotSupportedError, |
| 14 | +) |
| 15 | +from uipath.runtime.debug import UiPathDebugBridge, UiPathDebugQuitError |
| 16 | +from uipath.runtime.events import ( |
| 17 | + UiPathRuntimeStateEvent, |
| 18 | +) |
| 19 | + |
| 20 | +logger = logging.getLogger(__name__) |
| 21 | + |
| 22 | +T = TypeVar("T", bound=UiPathBaseRuntime) |
| 23 | +C = TypeVar("C", bound=UiPathRuntimeContext) |
| 24 | + |
| 25 | + |
| 26 | +class UiPathDebugRuntime(UiPathBaseRuntime, Generic[T]): |
| 27 | + """Specialized runtime for debug runs that streams events to a debug bridge.""" |
| 28 | + |
| 29 | + def __init__( |
| 30 | + self, |
| 31 | + context: UiPathRuntimeContext, |
| 32 | + factory: UiPathRuntimeFactory[T], |
| 33 | + debug_bridge: UiPathDebugBridge, |
| 34 | + ): |
| 35 | + """Initialize the UiPathDebugRuntime.""" |
| 36 | + super().__init__(context) |
| 37 | + self.context: UiPathRuntimeContext = context |
| 38 | + self.factory: UiPathRuntimeFactory[T] = factory |
| 39 | + self.debug_bridge: UiPathDebugBridge = debug_bridge |
| 40 | + self._inner_runtime: Optional[T] = None |
| 41 | + |
| 42 | + async def execute(self) -> UiPathRuntimeResult: |
| 43 | + """Execute the workflow with debug support.""" |
| 44 | + try: |
| 45 | + await self.debug_bridge.connect() |
| 46 | + |
| 47 | + self._inner_runtime = self.factory.new_runtime() |
| 48 | + |
| 49 | + if not self._inner_runtime: |
| 50 | + raise RuntimeError("Failed to create inner runtime") |
| 51 | + |
| 52 | + await self.debug_bridge.emit_execution_started() |
| 53 | + |
| 54 | + result: UiPathRuntimeResult |
| 55 | + # Try to stream events from inner runtime |
| 56 | + try: |
| 57 | + result = await self._stream_and_debug() |
| 58 | + except UiPathStreamNotSupportedError: |
| 59 | + # Fallback to regular execute if streaming not supported |
| 60 | + logger.debug( |
| 61 | + f"Runtime {self._inner_runtime.__class__.__name__} does not support " |
| 62 | + "streaming, falling back to execute()" |
| 63 | + ) |
| 64 | + result = await self._inner_runtime.execute() |
| 65 | + |
| 66 | + await self.debug_bridge.emit_execution_completed(result) |
| 67 | + |
| 68 | + self.context.result = result |
| 69 | + |
| 70 | + return result |
| 71 | + |
| 72 | + except Exception as e: |
| 73 | + # Emit execution error |
| 74 | + self.context.result = UiPathRuntimeResult( |
| 75 | + status=UiPathRuntimeStatus.FAULTED, |
| 76 | + ) |
| 77 | + await self.debug_bridge.emit_execution_error( |
| 78 | + error=str(e), |
| 79 | + ) |
| 80 | + raise |
| 81 | + |
| 82 | + async def _stream_and_debug(self) -> Optional[UiPathRuntimeResult]: |
| 83 | + """Stream events from inner runtime and handle debug interactions.""" |
| 84 | + if not self._inner_runtime: |
| 85 | + return None |
| 86 | + |
| 87 | + final_result: Optional[UiPathRuntimeResult] = None |
| 88 | + execution_completed = False |
| 89 | + |
| 90 | + # Starting in paused state - wait for breakpoints and resume |
| 91 | + await self.debug_bridge.wait_for_resume() |
| 92 | + |
| 93 | + # Keep streaming until execution completes (not just paused at breakpoint) |
| 94 | + while not execution_completed: |
| 95 | + # Update breakpoints from debug bridge |
| 96 | + self._inner_runtime.context.breakpoints = ( |
| 97 | + self.debug_bridge.get_breakpoints() |
| 98 | + ) |
| 99 | + # Stream events from inner runtime |
| 100 | + async for event in self._inner_runtime.stream(): |
| 101 | + # Handle final result |
| 102 | + if isinstance(event, UiPathRuntimeResult): |
| 103 | + final_result = event |
| 104 | + |
| 105 | + # Check if it's a breakpoint result |
| 106 | + if isinstance(event, UiPathBreakpointResult): |
| 107 | + try: |
| 108 | + # Hit a breakpoint - wait for resume and continue |
| 109 | + await self.debug_bridge.emit_breakpoint_hit(event) |
| 110 | + await self.debug_bridge.wait_for_resume() |
| 111 | + |
| 112 | + self._inner_runtime.context.resume = True |
| 113 | + |
| 114 | + except UiPathDebugQuitError: |
| 115 | + final_result = UiPathRuntimeResult( |
| 116 | + status=UiPathRuntimeStatus.SUCCESSFUL, |
| 117 | + ) |
| 118 | + execution_completed = True |
| 119 | + else: |
| 120 | + # Normal completion or suspension with dynamic interrupt |
| 121 | + execution_completed = True |
| 122 | + # Handle dynamic interrupts if present |
| 123 | + # In the future, poll for resume trigger completion here, using the debug bridge |
| 124 | + |
| 125 | + # Handle state update events - send to debug bridge |
| 126 | + elif isinstance(event, UiPathRuntimeStateEvent): |
| 127 | + await self.debug_bridge.emit_state_update(event) |
| 128 | + |
| 129 | + return final_result |
| 130 | + |
| 131 | + async def validate(self) -> None: |
| 132 | + """Validate runtime configuration.""" |
| 133 | + if self._inner_runtime: |
| 134 | + await self._inner_runtime.validate() |
| 135 | + |
| 136 | + async def cleanup(self) -> None: |
| 137 | + """Cleanup runtime resources.""" |
| 138 | + try: |
| 139 | + if self._inner_runtime: |
| 140 | + await self._inner_runtime.cleanup() |
| 141 | + finally: |
| 142 | + try: |
| 143 | + await self.debug_bridge.disconnect() |
| 144 | + except Exception as e: |
| 145 | + logger.warning(f"Error disconnecting debug bridge: {e}") |
0 commit comments