|
14 | 14 | from jupyterlab_chat.models import Message |
15 | 15 | from pycrdt import ArrayEvent |
16 | 16 | from traitlets.config import LoggingConfigurable |
| 17 | +from jupyter_ydoc.ybasedoc import YBaseDoc |
17 | 18 |
|
18 | 19 | if TYPE_CHECKING: |
19 | 20 | from jupyterlab_chat.ychat import YChat |
@@ -56,6 +57,13 @@ def __init__(self, *args, **kwargs): |
56 | 57 | self.slash_cmd_observers: Dict[str, Dict[str, List[Callable[[str, str, Message], Any]]]] = {} |
57 | 58 | self.chat_msg_observers: Dict[str, List[Callable[[str, Message], Any]]] = {} |
58 | 59 | self.chat_reset_observers: List[Callable[[str, "YChat"], Any]] = [] |
| 60 | + |
| 61 | + # Notebook observers |
| 62 | + self.notebook_activity_observers: Dict[str, List[Callable[[Any], Any]]] = {} |
| 63 | + self.notebook_cell_states: Dict[str, Dict] = {} |
| 64 | + |
| 65 | + # Active notebook rooms |
| 66 | + self.active_notebooks = Dict[str, YBaseDoc] = {} |
59 | 67 |
|
60 | 68 | # Active chat rooms |
61 | 69 | self.active_chats: Dict[str, "YChat"] = {} |
@@ -123,6 +131,33 @@ def observe_chat_msg( |
123 | 131 | self.chat_msg_observers[room_id].append(callback) |
124 | 132 | self.log.info("Registered message callback") |
125 | 133 |
|
| 134 | + |
| 135 | + def observe_notebook_activity( |
| 136 | + self, room_id: str, callback: Callable[[Any], Any] |
| 137 | + ) -> None: |
| 138 | + """ |
| 139 | + Register a callback for when notebook becomes active |
| 140 | + """ |
| 141 | + |
| 142 | + if room_id not in self.notebook_activity_observers: |
| 143 | + self.notebook_activity_observers[room_id] = [] |
| 144 | + |
| 145 | + self.notebook_activity_observers[room_id].append(callback) |
| 146 | + self.log.info("Registered notbook activity callback") |
| 147 | + |
| 148 | + |
| 149 | + def connect_notebook(self, room_id: str, ydoc: YBaseDoc) -> None: |
| 150 | + """ |
| 151 | + Connects a new notebook session to the router |
| 152 | + """ |
| 153 | + |
| 154 | + if room_id in self.active_notebooks: |
| 155 | + self.log.warning(f"Notebook {room_id} already connected to router") |
| 156 | + return |
| 157 | + |
| 158 | + self.active_notebooks[room_id] = ydoc |
| 159 | + |
| 160 | + |
126 | 161 | def connect_chat(self, room_id: str, ychat: "YChat") -> None: |
127 | 162 | """ |
128 | 163 | Connect a new chat session to the router. |
|
0 commit comments