|
| 1 | +"""Debug bridge implementation for the web server.""" |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import logging |
| 5 | +from typing import Any, Callable, Literal |
| 6 | + |
| 7 | +from uipath.runtime.debug import UiPathBreakpointResult, UiPathDebugQuitError |
| 8 | +from uipath.runtime.events import UiPathRuntimeStateEvent |
| 9 | +from uipath.runtime.result import UiPathRuntimeResult |
| 10 | +from uipath.runtime.resumable import UiPathResumeTriggerType |
| 11 | + |
| 12 | +from uipath.dev.models.execution import ExecutionMode |
| 13 | + |
| 14 | +logger = logging.getLogger(__name__) |
| 15 | + |
| 16 | + |
| 17 | +class WebDebugBridge: |
| 18 | + """Bridge between the web server and UiPathDebugRuntime. |
| 19 | +
|
| 20 | + In RUN mode: no breakpoints, auto-resumes the initial debug pause. |
| 21 | + In DEBUG mode: same step-mode behavior as TextualDebugBridge. |
| 22 | + """ |
| 23 | + |
| 24 | + def __init__(self, mode: ExecutionMode = ExecutionMode.RUN): |
| 25 | + self._mode = mode |
| 26 | + self._auto_resume = mode == ExecutionMode.RUN |
| 27 | + self._resume_event = asyncio.Event() |
| 28 | + self._resume_data: dict[str, Any] | None = None |
| 29 | + self._terminate_event = asyncio.Event() |
| 30 | + self._breakpoints: list[str] | Literal["*"] = ( |
| 31 | + [] if mode == ExecutionMode.RUN else "*" |
| 32 | + ) |
| 33 | + |
| 34 | + # Callbacks (wired by RunService) |
| 35 | + self.on_execution_started: Callable[[], None] | None = None |
| 36 | + self.on_state_update: Callable[[UiPathRuntimeStateEvent], None] | None = None |
| 37 | + self.on_breakpoint_hit: Callable[[UiPathBreakpointResult], None] | None = None |
| 38 | + self.on_execution_completed: Callable[[UiPathRuntimeResult], None] | None = None |
| 39 | + self.on_execution_error: Callable[[str], None] | None = None |
| 40 | + |
| 41 | + # ------------------------------------------------------------------ |
| 42 | + # UiPathDebugProtocol implementation |
| 43 | + # ------------------------------------------------------------------ |
| 44 | + |
| 45 | + async def connect(self) -> None: |
| 46 | + logger.debug("WebDebugBridge connected (mode=%s)", self._mode) |
| 47 | + |
| 48 | + async def disconnect(self) -> None: |
| 49 | + self._resume_event.set() |
| 50 | + self._terminate_event.set() |
| 51 | + logger.debug("WebDebugBridge disconnected") |
| 52 | + |
| 53 | + async def emit_execution_started(self, **kwargs: Any) -> None: |
| 54 | + logger.debug("Execution started") |
| 55 | + if self.on_execution_started: |
| 56 | + self.on_execution_started() |
| 57 | + |
| 58 | + async def emit_state_update(self, state_event: UiPathRuntimeStateEvent) -> None: |
| 59 | + logger.debug("State update: %s", state_event.node_name) |
| 60 | + if self.on_state_update: |
| 61 | + self.on_state_update(state_event) |
| 62 | + |
| 63 | + async def emit_breakpoint_hit( |
| 64 | + self, breakpoint_result: UiPathBreakpointResult |
| 65 | + ) -> None: |
| 66 | + logger.debug("Breakpoint hit: %s", breakpoint_result) |
| 67 | + if self.on_breakpoint_hit: |
| 68 | + self.on_breakpoint_hit(breakpoint_result) |
| 69 | + |
| 70 | + async def emit_execution_suspended( |
| 71 | + self, runtime_result: UiPathRuntimeResult |
| 72 | + ) -> None: |
| 73 | + logger.debug("Execution suspended") |
| 74 | + if runtime_result.trigger is None: |
| 75 | + return |
| 76 | + |
| 77 | + if runtime_result.trigger.trigger_type == UiPathResumeTriggerType.API: |
| 78 | + if self.on_breakpoint_hit: |
| 79 | + self.on_breakpoint_hit( |
| 80 | + UiPathBreakpointResult( |
| 81 | + breakpoint_node="<suspended>", |
| 82 | + breakpoint_type="before", |
| 83 | + current_state=runtime_result.output, |
| 84 | + next_nodes=[], |
| 85 | + ) |
| 86 | + ) |
| 87 | + |
| 88 | + async def emit_execution_resumed(self, resume_data: Any) -> None: |
| 89 | + logger.debug("Execution resumed") |
| 90 | + |
| 91 | + async def emit_execution_completed( |
| 92 | + self, runtime_result: UiPathRuntimeResult |
| 93 | + ) -> None: |
| 94 | + logger.debug("Execution completed") |
| 95 | + if self.on_execution_completed: |
| 96 | + self.on_execution_completed(runtime_result) |
| 97 | + |
| 98 | + async def emit_execution_error(self, error: str) -> None: |
| 99 | + logger.error("Execution error: %s", error) |
| 100 | + if self.on_execution_error: |
| 101 | + self.on_execution_error(error) |
| 102 | + |
| 103 | + async def wait_for_resume(self) -> Any: |
| 104 | + if self._auto_resume: |
| 105 | + self._auto_resume = False # Only auto-resume the first (initial) pause |
| 106 | + return {} |
| 107 | + |
| 108 | + self._resume_event.clear() |
| 109 | + await self._resume_event.wait() |
| 110 | + |
| 111 | + if self._terminate_event.is_set(): |
| 112 | + raise UiPathDebugQuitError("Debug session quit requested") |
| 113 | + |
| 114 | + return self._resume_data |
| 115 | + |
| 116 | + async def wait_for_terminate(self) -> None: |
| 117 | + await self._terminate_event.wait() |
| 118 | + |
| 119 | + def get_breakpoints(self) -> list[str] | Literal["*"]: |
| 120 | + return self._breakpoints |
| 121 | + |
| 122 | + # ------------------------------------------------------------------ |
| 123 | + # Control methods (called from RunService / WS handler) |
| 124 | + # ------------------------------------------------------------------ |
| 125 | + |
| 126 | + def resume(self, resume_data: Any) -> None: |
| 127 | + self._resume_data = resume_data or {} |
| 128 | + self._resume_event.set() |
| 129 | + |
| 130 | + def quit(self) -> None: |
| 131 | + self._terminate_event.set() |
| 132 | + self._resume_event.set() |
| 133 | + |
| 134 | + def set_breakpoints(self, breakpoints: list[str] | Literal["*"]) -> None: |
| 135 | + self._breakpoints = breakpoints |
0 commit comments