Skip to content

Commit 7d0ec47

Browse files
committed
WIP: notebook observers
1 parent 9a4e4ee commit 7d0ec47

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

jupyter_ai_router/router.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from jupyterlab_chat.models import Message
1515
from pycrdt import ArrayEvent
1616
from traitlets.config import LoggingConfigurable
17+
from jupyter_ydoc.ybasedoc import YBaseDoc
1718

1819
if TYPE_CHECKING:
1920
from jupyterlab_chat.ychat import YChat
@@ -56,6 +57,13 @@ def __init__(self, *args, **kwargs):
5657
self.slash_cmd_observers: Dict[str, Dict[str, List[Callable[[str, str, Message], Any]]]] = {}
5758
self.chat_msg_observers: Dict[str, List[Callable[[str, Message], Any]]] = {}
5859
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] = {}
5967

6068
# Active chat rooms
6169
self.active_chats: Dict[str, "YChat"] = {}
@@ -123,6 +131,33 @@ def observe_chat_msg(
123131
self.chat_msg_observers[room_id].append(callback)
124132
self.log.info("Registered message callback")
125133

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+
126161
def connect_chat(self, room_id: str, ychat: "YChat") -> None:
127162
"""
128163
Connect a new chat session to the router.

0 commit comments

Comments
 (0)