|
| 1 | +"""Debug bridge implementation for Textual UI.""" |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import logging |
| 5 | +from typing import Any, Literal, Optional |
| 6 | + |
| 7 | +from uipath.runtime.debug import UiPathBreakpointResult, UiPathDebugQuitError |
| 8 | +from uipath.runtime.events import UiPathRuntimeStateEvent |
| 9 | +from uipath.runtime.result import UiPathRuntimeResult |
| 10 | + |
| 11 | +logger = logging.getLogger(__name__) |
| 12 | + |
| 13 | + |
| 14 | +class TextualDebugBridge: |
| 15 | + """Bridge between Textual UI and UiPathDebugRuntime.""" |
| 16 | + |
| 17 | + def __init__(self): |
| 18 | + """Initialize the debug bridge.""" |
| 19 | + self._connected = False |
| 20 | + self._resume_event = asyncio.Event() |
| 21 | + self._quit_requested = False |
| 22 | + self._breakpoints: list[str] | Literal["*"] = "*" # Default: step mode |
| 23 | + |
| 24 | + # Callbacks to UI |
| 25 | + self.on_execution_started: Optional[callable] = None |
| 26 | + self.on_state_update: Optional[callable] = None |
| 27 | + self.on_breakpoint_hit: Optional[callable] = None |
| 28 | + self.on_execution_completed: Optional[callable] = None |
| 29 | + self.on_execution_error: Optional[callable] = None |
| 30 | + |
| 31 | + async def connect(self) -> None: |
| 32 | + """Establish connection to debugger.""" |
| 33 | + self._connected = True |
| 34 | + self._quit_requested = False |
| 35 | + logger.debug("Debug bridge connected") |
| 36 | + |
| 37 | + async def disconnect(self) -> None: |
| 38 | + """Close connection to debugger.""" |
| 39 | + self._connected = False |
| 40 | + self._resume_event.set() # Unblock any waiting tasks |
| 41 | + logger.debug("Debug bridge disconnected") |
| 42 | + |
| 43 | + async def emit_execution_started(self, **kwargs) -> None: |
| 44 | + """Notify debugger that execution started.""" |
| 45 | + logger.debug("Execution started") |
| 46 | + if self.on_execution_started: |
| 47 | + self.on_execution_started() |
| 48 | + |
| 49 | + async def emit_state_update(self, state_event: UiPathRuntimeStateEvent) -> None: |
| 50 | + """Notify debugger of runtime state update.""" |
| 51 | + logger.debug(f"State update: {state_event.node_name}") |
| 52 | + if self.on_state_update: |
| 53 | + self.on_state_update(state_event) |
| 54 | + |
| 55 | + async def emit_breakpoint_hit( |
| 56 | + self, breakpoint_result: UiPathBreakpointResult |
| 57 | + ) -> None: |
| 58 | + """Notify debugger that a breakpoint was hit.""" |
| 59 | + logger.debug(f"Breakpoint hit: {breakpoint_result}") |
| 60 | + if self.on_breakpoint_hit: |
| 61 | + self.on_breakpoint_hit(breakpoint_result) |
| 62 | + |
| 63 | + async def emit_execution_completed( |
| 64 | + self, runtime_result: UiPathRuntimeResult |
| 65 | + ) -> None: |
| 66 | + """Notify debugger that execution completed.""" |
| 67 | + logger.debug("Execution completed") |
| 68 | + if self.on_execution_completed: |
| 69 | + self.on_execution_completed(runtime_result) |
| 70 | + |
| 71 | + async def emit_execution_error(self, error: str) -> None: |
| 72 | + """Notify debugger that an error occurred.""" |
| 73 | + logger.error(f"Execution error: {error}") |
| 74 | + if self.on_execution_error: |
| 75 | + self.on_execution_error(error) |
| 76 | + |
| 77 | + async def wait_for_resume(self) -> Any: |
| 78 | + """Wait for resume command from debugger. |
| 79 | +
|
| 80 | + Raises: |
| 81 | + UiPathDebugQuitError: If quit was requested |
| 82 | + """ |
| 83 | + self._resume_event.clear() |
| 84 | + await self._resume_event.wait() |
| 85 | + |
| 86 | + if self._quit_requested: |
| 87 | + raise UiPathDebugQuitError("Debug session quit requested") |
| 88 | + |
| 89 | + def resume(self) -> None: |
| 90 | + """Signal that execution should resume (called from UI buttons).""" |
| 91 | + self._resume_event.set() |
| 92 | + |
| 93 | + def quit(self) -> None: |
| 94 | + """Signal that execution should quit (called from UI stop button).""" |
| 95 | + self._quit_requested = True |
| 96 | + self._resume_event.set() |
| 97 | + |
| 98 | + def get_breakpoints(self) -> list[str] | Literal["*"]: |
| 99 | + """Get nodes to suspend execution at.""" |
| 100 | + return self._breakpoints |
| 101 | + |
| 102 | + def set_breakpoints(self, breakpoints: list[str] | Literal["*"]) -> None: |
| 103 | + """Set breakpoints (called from UI).""" |
| 104 | + self._breakpoints = breakpoints |
0 commit comments