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 1 commit
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
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
# 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
import jupyter_ydoc
from functools import partial
from typing import Any, Callable, List

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


class YChat(YBaseDoc):
class YChat(jupyter_ydoc.YBaseDoc):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand how that can work, since jupyter_ydoc doesn't export YBaseDoc:

import jupyter_ydoc

jupyter_ydoc.YBaseDoc
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
# AttributeError: module 'jupyter_ydoc' has no attribute 'YBaseDoc'. Did you mean: 'ybasedoc'?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Me neither, but i fixed it anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the YBaseDoc should be prefixed with an underscore, to specified that it should not be imported.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it's fine to import it, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so, I misunderstood your comment and thought you meant not to import it.

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._ydoc["content"] = self._ycontent = Map()
Expand Down Expand Up @@ -58,3 +59,7 @@ def observe(self, callback: Callable[[str, Any], None]) -> None:
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
8 changes: 4 additions & 4 deletions packages/collaboration-extension/src/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import {
ChatPanel,
ChatWidgetFactory,
CollaborativeChatModelFactory,
CollaborativeChat,
CollaborativeChatModel,
CollaborativeChatWidget,
ICollaborativeDrive
ICollaborativeDrive,
YChat
} from '@jupyter/docprovider';
import {
ILayoutRestorer,
Expand Down Expand Up @@ -78,7 +78,7 @@ export const chatDocument: JupyterFrontEndPlugin<IChatFileType> = {

if (drive) {
const chatFactory = () => {
return CollaborativeChat.create();
return YChat.create();
};
drive.sharedModelFactory.registerDocumentFactory('chat', chatFactory);
}
Expand Down Expand Up @@ -280,7 +280,7 @@ export const chat: JupyterFrontEndPlugin<ChatPanel> = {
format: model.format,
contentType: chatFileType.contentType,
collaborative: true
}) as CollaborativeChat;
}) as YChat;

/**
* Initialize the chat model with the share model
Expand Down
5 changes: 3 additions & 2 deletions packages/docprovider/src/chat/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
import { Contents } from '@jupyterlab/services';
import { Awareness } from 'y-protocols/awareness';

import { CollaborativeChat, CollaborativeChatModel } from './model';
import { CollaborativeChatModel } from './model';
import { CollaborativeChatWidget } from './widget';
import { YChat } from './ychat';

/**
* A widget factory to create new instances of CollaborativeChatWidget.
Expand Down Expand Up @@ -138,7 +139,7 @@ export class CollaborativeChatModelFactory
*/

createNew(
options: DocumentRegistry.IModelOptions<CollaborativeChat>
options: DocumentRegistry.IModelOptions<YChat>
): CollaborativeChatModel {
return new CollaborativeChatModel({
...options,
Expand Down
1 change: 1 addition & 0 deletions packages/docprovider/src/chat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
export * from './factory';
export * from './model';
export * from './widget';
export * from './ychat';
93 changes: 9 additions & 84 deletions packages/docprovider/src/chat/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,20 @@
*/

import { ChatModel, IChatMessage, INewMessage, IUser } from 'chat-jupyter';
import { Delta, MapChange, StateChange, YDocument } from '@jupyter/ydoc';
import { DocumentRegistry } from '@jupyterlab/docregistry';
import { IChangedArgs } from '@jupyterlab/coreutils';
import {
JSONExt,
JSONObject,
PartialJSONObject,
UUID
} from '@lumino/coreutils';
import { PartialJSONObject, UUID } from '@lumino/coreutils';
import { ISignal, Signal } from '@lumino/signaling';
import { Awareness } from 'y-protocols/awareness';
import * as Y from 'yjs';
import { ChatChange, YChat } from './ychat';

/**
* Collaborative chat namespace.
*/
export namespace CollaborativeChatModel {
export interface IOptions extends ChatModel.IOptions {
awareness: Awareness;
sharedModel?: CollaborativeChat;
sharedModel?: YChat;
languagePreference?: string;
}
}
Expand All @@ -44,7 +38,7 @@ export class CollaborativeChatModel
if (sharedModel) {
this._sharedModel = sharedModel;
} else {
this._sharedModel = CollaborativeChat.create();
this._sharedModel = YChat.create();
}

this.sharedModel.changed.connect(this._onchange, this);
Expand All @@ -54,7 +48,7 @@ export class CollaborativeChatModel

readonly collaborative = true;

get sharedModel(): CollaborativeChat {
get sharedModel(): YChat {
return this._sharedModel;
}

Expand Down Expand Up @@ -144,12 +138,9 @@ export class CollaborativeChatModel
this.sharedModel.transact(() => void this.sharedModel.setMessage(msg));
}

private _onchange = (
_: CollaborativeChat,
change: ICollaborativeChatModelChange
) => {
if (change.messageChange) {
const msgDelta = change.messageChange;
private _onchange = (_: YChat, change: ChatChange) => {
if (change.messagesChange) {
const msgDelta = change.messagesChange;
// let retain: number;
const messages: IChatMessage[] = [];
msgDelta.forEach(data => {
Expand All @@ -171,7 +162,7 @@ export class CollaborativeChatModel
readonly defaultKernelName: string = '';
readonly defaultKernelLanguage: string = '';

private _sharedModel: CollaborativeChat;
private _sharedModel: YChat;

private _dirty = false;
private _readOnly = false;
Expand All @@ -182,69 +173,3 @@ export class CollaborativeChatModel
private _awareness: Awareness;
private _user: IUser;
}

interface ICollaborativeChatModelChange {
messageChange?: Delta<any>;
contentChange?: MapChange;
stateChange?: StateChange<any>[];
}

interface IDict<T = any> {
[key: string]: T;
}

/**
* The collaborative chat shared document.
*/
export class CollaborativeChat extends YDocument<ICollaborativeChatModelChange> {
/**
* Create a new collaborative chat model.
*/
constructor(options?: YDocument.IOptions) {
super(options);
this._content = this.ydoc.getMap<IDict>('content');
this._content.observe(this._contentObserver);

this._messages = this.ydoc.getArray<IDict>('messages');
this._messages.observe(this._messagesObserver);
}

/**
* Document version
*/
readonly version: string = '1.0.0';

/**
* Static method to create instances on the sharedModel
*
* @returns The sharedModel instance
*/
static create(options?: YDocument.IOptions): CollaborativeChat {
return new CollaborativeChat(options);
}

get content(): JSONObject {
return JSONExt.deepCopy(this._content.toJSON());
}

get messages(): string[] {
return JSONExt.deepCopy(this._messages.toJSON());
}

setMessage(value: IDict): void {
this._messages.push([value]);
}

private _contentObserver = (event: Y.YMapEvent<IDict>): void => {
this._changed.emit(this.content);
};

private _messagesObserver = (event: Y.YArrayEvent<IDict>): void => {
const changes: ICollaborativeChatModelChange = {};
changes.messageChange = event.delta;
this._changed.emit(changes);
};

private _content: Y.Map<IDict>;
private _messages: Y.Array<IDict>;
}
84 changes: 84 additions & 0 deletions packages/docprovider/src/chat/ychat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* 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 { Delta, DocumentChange, MapChange, YDocument } from '@jupyter/ydoc';
import { JSONExt, JSONObject } from '@lumino/coreutils';
import * as Y from 'yjs';

/**
* Definition of the shared Chat changes.
*/
export type ChatChange = DocumentChange & {
/**
* Messages list change
*/
messagesChange?: Delta<any>;
/**
* Content change
*/
contentChange?: MapChange;
};

interface IDict<T = any> {
[key: string]: T;
}

/**
* The collaborative chat shared document.
*/
export class YChat extends YDocument<ChatChange> {
/**
* Create a new collaborative chat model.
*/
constructor(options?: YDocument.IOptions) {
super(options);
this._content = this.ydoc.getMap<IDict>('content');
this._content.observe(this._contentObserver);

this._messages = this.ydoc.getArray<IDict>('messages');
this._messages.observe(this._messagesObserver);
}

/**
* Document version
*/
readonly version: string = '1.0.0';

/**
* Static method to create instances on the sharedModel
*
* @returns The sharedModel instance
*/
static create(options?: YDocument.IOptions): YChat {
return new YChat(options);
}

get content(): JSONObject {
return JSONExt.deepCopy(this._content.toJSON());
}

get messages(): string[] {
return JSONExt.deepCopy(this._messages.toJSON());
}

setMessage(value: IDict): void {
this._messages.push([value]);
}

private _contentObserver = (event: Y.YMapEvent<IDict>): void => {
this._changed.emit(this.content);
};

private _messagesObserver = (event: Y.YArrayEvent<IDict>): void => {
const changes: ChatChange = {};
changes.messagesChange = event.delta;
this._changed.emit(changes);
};

private _content: Y.Map<IDict>;
private _messages: Y.Array<IDict>;
}
3 changes: 0 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,3 @@ module = [
"terminado"
]
ignore_missing_imports = true

[project.entry-points.jupyter_ydoc]
chat = "jupyter_collaboration.chat_ydoc:YChat"
Loading