Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collaborative chat using jupyterlab_chat package #237

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions jupyter_collaboration/ychat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

# TODO: remove this module in favor of the one in jupyter_ydoc when released.

import json
from functools import partial
from typing import Any, Callable, List

import jupyter_ydoc
from jupyter_ydoc.ybasedoc import YBaseDoc
from pycrdt import Array, Map


class YChat(YBaseDoc):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._ydoc["content"] = self._ycontent = Map()
self._ydoc["messages"] = self._ymessages = Array()

@property
def version(self) -> str:
"""
Returns the version of the document.
:return: Document's version.
:rtype: str
"""
return "1.0.0"

@property
def messages(self) -> List:
return self._ymessages.to_py()

def get(self) -> str:
"""
Returns the messages of the document.
:return: Document's messages.
:rtype: Any
"""

messages = self._ymessages.to_py()
data = dict(messages=messages)
return json.dumps(data, indent=2, sort_keys=True)

def set(self, value: str) -> None:
"""
Sets the content of the document.
:param value: The content of the document.
:type value: str
"""
contents = json.loads(value)
if "messages" in contents.keys():
with self._ydoc.transaction():
for v in contents["messages"]:
self._ymessages.append(v)

def observe(self, callback: Callable[[str, Any], None]) -> None:
self.unobserve()
self._subscriptions[self._ystate] = self._ystate.observe(partial(callback, "state"))
self._subscriptions[self._ymessages] = self._ymessages.observe(
partial(callback, "messages")
)
self._subscriptions[self._ycontent] = self._ycontent.observe(partial(callback, "content"))


# Register the ydoc
jupyter_ydoc.ydocs["jupyter_collaboration.chat_ydoc:YChat"] = YChat
6 changes: 4 additions & 2 deletions packages/collaboration-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"dependencies": {
"@jupyter/collaboration": "^2.0.11",
"@jupyter/docprovider": "^2.0.11",
"@jupyter/ydoc": "^1.1.0-a0",
"@jupyter/ydoc": "^1.1.1",
"@jupyterlab/application": "^4.0.5",
"@jupyterlab/apputils": "^4.0.5",
"@jupyterlab/codemirror": "^4.0.5",
Expand All @@ -65,20 +65,22 @@
"@jupyterlab/fileeditor": "^4.0.5",
"@jupyterlab/logconsole": "^4.0.5",
"@jupyterlab/notebook": "^4.0.5",
"@jupyterlab/rendermime": "^4.0.5",
"@jupyterlab/services": "^7.0.5",
"@jupyterlab/settingregistry": "^4.0.5",
"@jupyterlab/statedb": "^4.0.5",
"@jupyterlab/translation": "^4.0.5",
"@jupyterlab/ui-components": "^4.0.5",
"@lumino/commands": "^2.1.0",
"@lumino/widgets": "^2.1.0",
"chat-jupyter": "0.1.0",
"y-protocols": "^1.0.5",
"y-websocket": "^1.3.15",
"yjs": "^13.5.40"
},
"devDependencies": {
"@jupyterlab/builder": "^4.0.5",
"@types/react": "~18.0.26",
"@types/react": "^18.0.26",
"npm-run-all": "^4.1.5",
"rimraf": "^4.1.2",
"typescript": "~5.0.4"
Expand Down
14 changes: 14 additions & 0 deletions packages/collaboration-extension/schema/chat.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"title": "Jupyter chat configuration",
"description": "Configuration for the chat panel",
"type": "object",
"properties": {
"sendWithShiftEnter": {
"description": "Whether to send a message via Shift-Enter instead of Enter.",
"type": "boolean",
"default": false,
"readOnly": false
}
},
"additionalProperties": false
}
Loading
Loading