-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the factory for the chat document and widget
- Loading branch information
Showing
15 changed files
with
993 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 0 additions & 27 deletions
27
packages/jupyterlab-collaborative-chat/jupyterlab_collaborative_chat/handlers.py
This file was deleted.
Oops, something went wrong.
62 changes: 62 additions & 0 deletions
62
packages/jupyterlab-collaborative-chat/jupyterlab_collaborative_chat/ychat.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# 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 | ||
|
||
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")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/* | ||
* Copyright (c) Jupyter Development Team. | ||
* Distributed under the terms of the Modified BSD License. | ||
*/ | ||
|
||
import { ChatWidget, IChatModel } from 'chat-jupyter'; | ||
import { IThemeManager } from '@jupyterlab/apputils'; | ||
import { ABCWidgetFactory, DocumentRegistry } from '@jupyterlab/docregistry'; | ||
import { IRenderMimeRegistry } from '@jupyterlab/rendermime'; | ||
import { Contents } from '@jupyterlab/services'; | ||
import { Awareness } from 'y-protocols/awareness'; | ||
|
||
import { CollaborativeChatModel } from './model'; | ||
import { CollaborativeChatWidget } from './widget'; | ||
import { YChat } from './ychat'; | ||
|
||
/** | ||
* A widget factory to create new instances of CollaborativeChatWidget. | ||
*/ | ||
export class ChatWidgetFactory extends ABCWidgetFactory< | ||
CollaborativeChatWidget, | ||
CollaborativeChatModel | ||
> { | ||
/** | ||
* Constructor of ChatWidgetFactory. | ||
* | ||
* @param options Constructor options | ||
*/ | ||
constructor(options: ChatWidgetFactory.IOptions) { | ||
super(options); | ||
this._themeManager = options.themeManager; | ||
this._rmRegistry = options.rmRegistry; | ||
} | ||
|
||
/** | ||
* Create a new widget given a context. | ||
* | ||
* @param context Contains the information of the file | ||
* @returns The widget | ||
*/ | ||
protected createNewWidget( | ||
context: ChatWidgetFactory.IContext | ||
): CollaborativeChatWidget { | ||
context.chatModel = context.model; | ||
context.rmRegistry = this._rmRegistry; | ||
context.themeManager = this._themeManager; | ||
return new CollaborativeChatWidget({ | ||
context, | ||
content: new ChatWidget(context) | ||
}); | ||
} | ||
|
||
private _themeManager: IThemeManager | null; | ||
private _rmRegistry: IRenderMimeRegistry; | ||
} | ||
|
||
export namespace ChatWidgetFactory { | ||
export interface IContext | ||
extends DocumentRegistry.IContext<CollaborativeChatModel> { | ||
chatModel: IChatModel; | ||
themeManager: IThemeManager | null; | ||
rmRegistry: IRenderMimeRegistry; | ||
} | ||
|
||
export interface IOptions extends DocumentRegistry.IWidgetFactoryOptions { | ||
themeManager: IThemeManager | null; | ||
rmRegistry: IRenderMimeRegistry; | ||
} | ||
} | ||
|
||
export class CollaborativeChatModelFactory | ||
implements DocumentRegistry.IModelFactory<CollaborativeChatModel> | ||
{ | ||
constructor(options: CollaborativeChatModel.IOptions) { | ||
this._awareness = options.awareness; | ||
} | ||
|
||
collaborative = true; | ||
/** | ||
* The name of the model. | ||
* | ||
* @returns The name | ||
*/ | ||
get name(): string { | ||
return 'chat'; | ||
} | ||
|
||
/** | ||
* The content type of the file. | ||
* | ||
* @returns The content type | ||
*/ | ||
get contentType(): Contents.ContentType { | ||
return 'chat'; | ||
} | ||
|
||
/** | ||
* The format of the file. | ||
* | ||
* @returns the file format | ||
*/ | ||
get fileFormat(): Contents.FileFormat { | ||
return 'text'; | ||
} | ||
|
||
/** | ||
* Get whether the model factory has been disposed. | ||
* | ||
* @returns disposed status | ||
*/ | ||
|
||
get isDisposed(): boolean { | ||
return this._disposed; | ||
} | ||
|
||
/** | ||
* Dispose the model factory. | ||
*/ | ||
dispose(): void { | ||
this._disposed = true; | ||
} | ||
|
||
/** | ||
* Get the preferred language given the path on the file. | ||
* | ||
* @param path path of the file represented by this document model | ||
* @returns The preferred language | ||
*/ | ||
preferredLanguage(path: string): string { | ||
return ''; | ||
} | ||
|
||
/** | ||
* Create a new instance of CollaborativeChatModel. | ||
* | ||
* @param languagePreference Language | ||
* @param modelDB Model database | ||
* @returns The model | ||
*/ | ||
|
||
createNew( | ||
options: DocumentRegistry.IModelOptions<YChat> | ||
): CollaborativeChatModel { | ||
return new CollaborativeChatModel({ | ||
...options, | ||
awareness: this._awareness | ||
}); | ||
} | ||
|
||
private _disposed = false; | ||
private _awareness: Awareness; | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.