Allow consumers to observe document resets on YChat instances
#10
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Closes #6.
This PR introduces a new
observe_chat_reset()method ontoMessageRouter, which follows the same interface asobserve_chat_init(). Observers added by this method are called whenever theYChatobject undergoes a document reset.*.chatfile through the filesystem.Document resets currently only occur when
jupyter_server_documentsis installed. Observers added toobserve_chat_reset()are never fired ifjupyter_server_documentsis not installed.Technical details
jupyter_server_documents(JSD) is not added as a required dependency by this PR. To provide this optional integration with JSD, this branch introduces a new constantJSD_PRESENT = False, which is set toTrueifjupyter_server_documentsis importable.If
JSD_PRESENT == False, the extension calls_get_chat_jcollab(), which contains the previous logic for getting aYChatinstance from a room ID.If
JSD_PRESENT == True, the extension calls_get_chat_jsd(), which callsYRoom.get_jupyter_ydoc(on_reset=...), an API exclusive to JSD. This method automatically attaches anon_reset()observer. This callsMessageRouter._on_chat_reset(), which fires the observers added byMessageRouter.observe_chat_reset()when aYChatreset occurs.Due to a bug in JSD, the UI reset & notification only occur after upgrading to
jupyterlab_chat==0.18.2.Testing instructions
Make sure
jupyter_server_documentsis not installed by runningpip show jupyter_server_documents.Start the server and verify that everything works as it had before.
Run
pip install jupyter_server_documents && pip install -U jupyterlab_chat, then restart the server & browser session.Open a
*.chatfile and runtouch *.chatin another terminal within JupyterLab. This should trigger a document reset on the chat file, and you should see a notification in the bottom right.Open the server logs and verify the following warning was logged:
This confirms that the
YChatreset was detected byjupyter_ai_routerand the appropriate observers would have been called.