|
7 | 7 | from urllib.parse import urlparse |
8 | 8 |
|
9 | 9 | import socketio # type: ignore[import-untyped] |
| 10 | +from rich.console import Console |
| 11 | +from rich.live import Live |
| 12 | +from rich.markdown import Markdown |
| 13 | +from rich.panel import Panel |
10 | 14 | from socketio import AsyncClient |
11 | 15 | from uipath.core.chat import ( |
12 | 16 | UiPathConversationEvent, |
13 | 17 | UiPathConversationExchangeEndEvent, |
14 | 18 | UiPathConversationExchangeEvent, |
| 19 | + UiPathConversationMessageEvent, |
| 20 | + UiPathConversationMessageStartEvent, |
15 | 21 | ) |
16 | 22 | from uipath.runtime.chat import UiPathChatProtocol |
17 | 23 | from uipath.runtime.context import UiPathRuntimeContext |
18 | 24 |
|
19 | 25 | logger = logging.getLogger(__name__) |
20 | 26 |
|
21 | 27 |
|
| 28 | +class ConsoleChatBridge(UiPathChatProtocol): |
| 29 | + """Console implementation of UiPathChatProtocol that streams message content.""" |
| 30 | + |
| 31 | + def __init__(self): |
| 32 | + self.console = Console(force_terminal=True) |
| 33 | + self._connected = False |
| 34 | + |
| 35 | + self._live: Live | None = None |
| 36 | + self._buffer: list[str] = [] |
| 37 | + self._current_role: str | None = None |
| 38 | + |
| 39 | + async def connect(self) -> None: |
| 40 | + self._connected = True |
| 41 | + self.console.print( |
| 42 | + Panel("[bold green]Console Chat Started[/bold green]", expand=False) |
| 43 | + ) |
| 44 | + |
| 45 | + async def disconnect(self) -> None: |
| 46 | + if self._live: |
| 47 | + self._live.stop() |
| 48 | + self.console.print( |
| 49 | + Panel("[bold red]Console Chat Ended[/bold red]", expand=False) |
| 50 | + ) |
| 51 | + self._connected = False |
| 52 | + |
| 53 | + async def emit_message_event(self, event: UiPathConversationMessageEvent) -> None: |
| 54 | + """Handles *all* message-level events.""" |
| 55 | + if not self._connected: |
| 56 | + return |
| 57 | + |
| 58 | + if event.start: |
| 59 | + await self._on_message_start(event.start) |
| 60 | + |
| 61 | + if event.content_part: |
| 62 | + cp = event.content_part |
| 63 | + |
| 64 | + if cp.start: |
| 65 | + await self._on_content_part_start(cp.start.mime_type) |
| 66 | + |
| 67 | + if cp.chunk: |
| 68 | + await self._on_content_part_chunk(cp.chunk.data or "") |
| 69 | + |
| 70 | + if cp.end: |
| 71 | + await self._on_content_part_end() |
| 72 | + |
| 73 | + if event.end: |
| 74 | + await self._on_message_end() |
| 75 | + |
| 76 | + async def _on_message_start(self, start: UiPathConversationMessageStartEvent): |
| 77 | + self._current_role = start.role or "assistant" |
| 78 | + self.console.print( |
| 79 | + Panel( |
| 80 | + "[bold blue]User[/bold blue]" |
| 81 | + if self._current_role == "user" |
| 82 | + else "[bold green]Assistant[/bold green]", |
| 83 | + expand=False, |
| 84 | + ) |
| 85 | + ) |
| 86 | + |
| 87 | + async def _on_message_end(self): |
| 88 | + if self._live: |
| 89 | + self._live.stop() |
| 90 | + self._live = None |
| 91 | + self._buffer = [] |
| 92 | + |
| 93 | + async def _on_content_part_start(self, mime_type: str): |
| 94 | + """Prepare to stream a content part of a message.""" |
| 95 | + self._buffer = [] |
| 96 | + |
| 97 | + if not self._live: |
| 98 | + self._live = Live(auto_refresh=False) |
| 99 | + self._live.start() |
| 100 | + |
| 101 | + async def _on_content_part_chunk(self, text: str): |
| 102 | + """Stream one chunk of assistant text.""" |
| 103 | + if not self._live: |
| 104 | + # Should never happen, but safe guard |
| 105 | + self._live = Live(auto_refresh=False) |
| 106 | + self._live.start() |
| 107 | + |
| 108 | + self._buffer.append(text) |
| 109 | + |
| 110 | + rendered = Markdown("".join(self._buffer)) |
| 111 | + self._live.update( |
| 112 | + Panel(rendered, title="[green]Assistant (streaming)"), |
| 113 | + ) |
| 114 | + self._live.refresh() |
| 115 | + |
| 116 | + async def _on_content_part_end(self): |
| 117 | + """Finalize streaming for this part (but message may continue).""" |
| 118 | + if self._live: |
| 119 | + self._live.stop() |
| 120 | + self._live = None |
| 121 | + |
| 122 | + # Print the final assembled text once more |
| 123 | + final_text = "".join(self._buffer) |
| 124 | + self.console.print( |
| 125 | + Panel(Markdown(final_text), expand=False, border_style="green") |
| 126 | + ) |
| 127 | + self._buffer = [] |
| 128 | + |
| 129 | + |
22 | 130 | class WebSocketChatBridge: |
23 | 131 | """WebSocket-based chat bridge for streaming conversational events to CAS. |
24 | 132 |
|
|
0 commit comments