jupyterlab-chat 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/token.js ADDED
@@ -0,0 +1,59 @@
1
+ /*
2
+ * Copyright (c) Jupyter Development Team.
3
+ * Distributed under the terms of the Modified BSD License.
4
+ */
5
+ import { chatIcon } from '@jupyter/chat';
6
+ import { Token } from '@lumino/coreutils';
7
+ /**
8
+ * The file type for a chat document.
9
+ */
10
+ export const chatFileType = {
11
+ name: 'chat',
12
+ displayName: 'Chat',
13
+ mimeTypes: ['text/json', 'application/json'],
14
+ extensions: ['.chat'],
15
+ fileFormat: 'text',
16
+ contentType: 'chat',
17
+ icon: chatIcon
18
+ };
19
+ /**
20
+ * The token for the chat widget factory.
21
+ */
22
+ export const IChatFactory = new Token('jupyterlab-chat:IChatFactory');
23
+ /**
24
+ * Command ids.
25
+ */
26
+ export const CommandIDs = {
27
+ /**
28
+ * Create a chat file.
29
+ */
30
+ createChat: 'jupyterlab-chat:create',
31
+ /**
32
+ * Open a chat file.
33
+ */
34
+ openChat: 'jupyterlab-chat:open',
35
+ /**
36
+ * Move a main widget to the side panel.
37
+ */
38
+ moveToSide: 'jupyterlab-chat:moveToSide',
39
+ /**
40
+ * Mark as read.
41
+ */
42
+ markAsRead: 'jupyterlab-chat:markAsRead',
43
+ /**
44
+ * Focus the input of the current chat.
45
+ */
46
+ focusInput: 'jupyterlab-chat:focusInput'
47
+ };
48
+ /**
49
+ * The chat panel token.
50
+ */
51
+ export const IChatPanel = new Token('jupyterlab-chat:IChatPanel');
52
+ /**
53
+ * The active cell manager plugin.
54
+ */
55
+ export const IActiveCellManagerToken = new Token('jupyterlab-chat:IActiveCellManager');
56
+ /**
57
+ * The selection watcher plugin.
58
+ */
59
+ export const ISelectionWatcherToken = new Token('jupyterlab-chat:ISelectionWatcher');
@@ -0,0 +1,121 @@
1
+ import { ChatWidget, IAutocompletionRegistry, IChatModel } from '@jupyter/chat';
2
+ import { ICollaborativeDrive } from '@jupyter/docprovider';
3
+ import { IThemeManager } from '@jupyterlab/apputils';
4
+ import { DocumentWidget } from '@jupyterlab/docregistry';
5
+ import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
6
+ import { SidePanel } from '@jupyterlab/ui-components';
7
+ import { CommandRegistry } from '@lumino/commands';
8
+ import { Message } from '@lumino/messaging';
9
+ import { Panel } from '@lumino/widgets';
10
+ import { LabChatModel } from './model';
11
+ /**
12
+ * DocumentWidget: widget that represents the view or editor for a file type.
13
+ */
14
+ export declare class LabChatPanel extends DocumentWidget<ChatWidget, LabChatModel> {
15
+ constructor(options: DocumentWidget.IOptions<ChatWidget, LabChatModel>);
16
+ /**
17
+ * Dispose of the resources held by the widget.
18
+ */
19
+ dispose(): void;
20
+ /**
21
+ * The model for the widget.
22
+ */
23
+ get model(): LabChatModel;
24
+ /**
25
+ * Add class to tab when messages are unread.
26
+ */
27
+ private _unreadChanged;
28
+ }
29
+ /**
30
+ * Sidepanel widget including the chats and the add chat button.
31
+ */
32
+ export declare class ChatPanel extends SidePanel {
33
+ /**
34
+ * The constructor of the chat panel.
35
+ */
36
+ constructor(options: ChatPanel.IOptions);
37
+ /**
38
+ * Getter and setter of the defaultDirectory.
39
+ */
40
+ get defaultDirectory(): string;
41
+ set defaultDirectory(value: string);
42
+ /**
43
+ * Add a new widget to the chat panel.
44
+ *
45
+ * @param model - the model of the chat widget
46
+ * @param name - the name of the chat.
47
+ */
48
+ addChat(model: IChatModel, path: string): void;
49
+ /**
50
+ * Update the list of available chats in the default directory.
51
+ */
52
+ updateChatList: () => Promise<void>;
53
+ /**
54
+ * Open a chat if it exists in the side panel.
55
+ *
56
+ * @param path - the path of the chat.
57
+ * @returns a boolean, whether the chat existed in the side panel or not.
58
+ */
59
+ openIfExists(path: string): boolean;
60
+ /**
61
+ * A message handler invoked on an `'after-show'` message.
62
+ */
63
+ protected onAfterShow(msg: Message): void;
64
+ /**
65
+ * Return the index of the chat in the list (-1 if not opened).
66
+ *
67
+ * @param name - the chat name.
68
+ */
69
+ private _getChatIndex;
70
+ /**
71
+ * Expand the chat from its index.
72
+ */
73
+ private _expandChat;
74
+ /**
75
+ * Handle `change` events for the HTMLSelect component.
76
+ */
77
+ private _chatSelected;
78
+ /**
79
+ * Triggered when a section is toogled. If the section is opened, all others
80
+ * sections are closed.
81
+ */
82
+ private _onExpansionToggled;
83
+ private _chatNamesChanged;
84
+ private _commands;
85
+ private _defaultDirectory;
86
+ private _drive;
87
+ private _openChat;
88
+ private _rmRegistry;
89
+ private _themeManager;
90
+ private _autocompletionRegistry?;
91
+ }
92
+ /**
93
+ * The chat panel namespace.
94
+ */
95
+ export declare namespace ChatPanel {
96
+ /**
97
+ * Options of the constructor of the chat panel.
98
+ */
99
+ interface IOptions extends SidePanel.IOptions {
100
+ commands: CommandRegistry;
101
+ drive: ICollaborativeDrive;
102
+ rmRegistry: IRenderMimeRegistry;
103
+ themeManager: IThemeManager | null;
104
+ defaultDirectory: string;
105
+ autocompletionRegistry?: IAutocompletionRegistry;
106
+ }
107
+ }
108
+ /**
109
+ * The chat section namespace.
110
+ */
111
+ export declare namespace ChatSection {
112
+ /**
113
+ * Options to build a chat section.
114
+ */
115
+ interface IOptions extends Panel.IOptions {
116
+ commands: CommandRegistry;
117
+ defaultDirectory: string;
118
+ widget: ChatWidget;
119
+ path: string;
120
+ }
121
+ }
package/lib/widget.js ADDED
@@ -0,0 +1,342 @@
1
+ /*
2
+ * Copyright (c) Jupyter Development Team.
3
+ * Distributed under the terms of the Modified BSD License.
4
+ */
5
+ import { ChatWidget, readIcon } from '@jupyter/chat';
6
+ import { PathExt } from '@jupyterlab/coreutils';
7
+ import { DocumentWidget } from '@jupyterlab/docregistry';
8
+ import { addIcon, closeIcon, CommandToolbarButton, HTMLSelect, launchIcon, PanelWithToolbar, ReactWidget, SidePanel, ToolbarButton } from '@jupyterlab/ui-components';
9
+ import { Signal } from '@lumino/signaling';
10
+ import React, { useState } from 'react';
11
+ import { CommandIDs, chatFileType } from './token';
12
+ const MAIN_PANEL_CLASS = 'jp-lab-chat-main-panel';
13
+ const TITLE_UNREAD_CLASS = 'jp-lab-chat-title-unread';
14
+ const SIDEPANEL_CLASS = 'jp-lab-chat-sidepanel';
15
+ const ADD_BUTTON_CLASS = 'jp-lab-chat-add';
16
+ const OPEN_SELECT_CLASS = 'jp-lab-chat-open';
17
+ const SECTION_CLASS = 'jp-lab-chat-section';
18
+ const TOOLBAR_CLASS = 'jp-lab-chat-toolbar';
19
+ /**
20
+ * DocumentWidget: widget that represents the view or editor for a file type.
21
+ */
22
+ export class LabChatPanel extends DocumentWidget {
23
+ constructor(options) {
24
+ super(options);
25
+ /**
26
+ * Add class to tab when messages are unread.
27
+ */
28
+ this._unreadChanged = (_, unread) => {
29
+ if (unread.length) {
30
+ if (!this.title.className.includes(TITLE_UNREAD_CLASS)) {
31
+ this.title.className += ` ${TITLE_UNREAD_CLASS}`;
32
+ }
33
+ }
34
+ else {
35
+ this.title.className = this.title.className.replace(TITLE_UNREAD_CLASS, '');
36
+ }
37
+ };
38
+ this.addClass(MAIN_PANEL_CLASS);
39
+ this.model.name = this.context.localPath;
40
+ this.model.unreadChanged.connect(this._unreadChanged);
41
+ }
42
+ /**
43
+ * Dispose of the resources held by the widget.
44
+ */
45
+ dispose() {
46
+ this.model.unreadChanged.disconnect(this._unreadChanged);
47
+ this.context.dispose();
48
+ this.content.dispose();
49
+ super.dispose();
50
+ }
51
+ /**
52
+ * The model for the widget.
53
+ */
54
+ get model() {
55
+ return this.context.model;
56
+ }
57
+ }
58
+ /**
59
+ * Sidepanel widget including the chats and the add chat button.
60
+ */
61
+ export class ChatPanel extends SidePanel {
62
+ /**
63
+ * The constructor of the chat panel.
64
+ */
65
+ constructor(options) {
66
+ super(options);
67
+ /**
68
+ * Update the list of available chats in the default directory.
69
+ */
70
+ this.updateChatList = async () => {
71
+ const extension = chatFileType.extensions[0];
72
+ this._drive
73
+ .get(this._defaultDirectory)
74
+ .then(contentModel => {
75
+ const chatsNames = {};
76
+ contentModel.content
77
+ .filter(f => f.type === 'file' && f.name.endsWith(extension))
78
+ .forEach(f => {
79
+ chatsNames[PathExt.basename(f.name, extension)] = f.path;
80
+ });
81
+ this._chatNamesChanged.emit(chatsNames);
82
+ })
83
+ .catch(e => console.error('Error getting the chat files from drive', e));
84
+ };
85
+ /**
86
+ * Handle `change` events for the HTMLSelect component.
87
+ */
88
+ this._chatSelected = (event) => {
89
+ const select = event.target;
90
+ const path = select.value;
91
+ const name = select.options[select.selectedIndex].textContent;
92
+ if (name === '-') {
93
+ return;
94
+ }
95
+ this._commands.execute(CommandIDs.openChat, {
96
+ filepath: path,
97
+ inSidePanel: true
98
+ });
99
+ event.target.selectedIndex = 0;
100
+ };
101
+ this._chatNamesChanged = new Signal(this);
102
+ this.addClass(SIDEPANEL_CLASS);
103
+ this._commands = options.commands;
104
+ this._drive = options.drive;
105
+ this._rmRegistry = options.rmRegistry;
106
+ this._themeManager = options.themeManager;
107
+ this._defaultDirectory = options.defaultDirectory;
108
+ this._autocompletionRegistry = options.autocompletionRegistry;
109
+ const addChat = new CommandToolbarButton({
110
+ commands: this._commands,
111
+ id: CommandIDs.createChat,
112
+ args: { inSidePanel: true },
113
+ icon: addIcon
114
+ });
115
+ addChat.addClass(ADD_BUTTON_CLASS);
116
+ this.toolbar.addItem('createChat', addChat);
117
+ this._openChat = ReactWidget.create(React.createElement(ChatSelect, { chatNamesChanged: this._chatNamesChanged, handleChange: this._chatSelected.bind(this) }));
118
+ this._openChat.addClass(OPEN_SELECT_CLASS);
119
+ this.toolbar.addItem('openChat', this._openChat);
120
+ const content = this.content;
121
+ content.expansionToggled.connect(this._onExpansionToggled, this);
122
+ }
123
+ /**
124
+ * Getter and setter of the defaultDirectory.
125
+ */
126
+ get defaultDirectory() {
127
+ return this._defaultDirectory;
128
+ }
129
+ set defaultDirectory(value) {
130
+ if (value === this._defaultDirectory) {
131
+ return;
132
+ }
133
+ this._defaultDirectory = value;
134
+ // Update the list of discoverable chat (in default directory)
135
+ this.updateChatList();
136
+ // Update the sections names.
137
+ this.widgets.forEach(w => {
138
+ w.defaultDirectory = value;
139
+ });
140
+ }
141
+ /**
142
+ * Add a new widget to the chat panel.
143
+ *
144
+ * @param model - the model of the chat widget
145
+ * @param name - the name of the chat.
146
+ */
147
+ addChat(model, path) {
148
+ // Collapse all chats
149
+ const content = this.content;
150
+ for (let i = 0; i < this.widgets.length; i++) {
151
+ content.collapse(i);
152
+ }
153
+ // Set the name of the model.
154
+ model.name = path;
155
+ // Create a new widget.
156
+ const widget = new ChatWidget({
157
+ model: model,
158
+ rmRegistry: this._rmRegistry,
159
+ themeManager: this._themeManager,
160
+ autocompletionRegistry: this._autocompletionRegistry
161
+ });
162
+ this.addWidget(new ChatSection({
163
+ widget,
164
+ commands: this._commands,
165
+ path,
166
+ defaultDirectory: this._defaultDirectory
167
+ }));
168
+ }
169
+ /**
170
+ * Open a chat if it exists in the side panel.
171
+ *
172
+ * @param path - the path of the chat.
173
+ * @returns a boolean, whether the chat existed in the side panel or not.
174
+ */
175
+ openIfExists(path) {
176
+ const index = this._getChatIndex(path);
177
+ if (index > -1) {
178
+ this._expandChat(index);
179
+ }
180
+ return index > -1;
181
+ }
182
+ /**
183
+ * A message handler invoked on an `'after-show'` message.
184
+ */
185
+ onAfterShow(msg) {
186
+ var _a;
187
+ // Wait for the component to be rendered.
188
+ (_a = this._openChat.renderPromise) === null || _a === void 0 ? void 0 : _a.then(() => this.updateChatList());
189
+ }
190
+ /**
191
+ * Return the index of the chat in the list (-1 if not opened).
192
+ *
193
+ * @param name - the chat name.
194
+ */
195
+ _getChatIndex(path) {
196
+ return this.widgets.findIndex(w => w.path === path);
197
+ }
198
+ /**
199
+ * Expand the chat from its index.
200
+ */
201
+ _expandChat(index) {
202
+ if (!this.widgets[index].isVisible) {
203
+ this.content.expand(index);
204
+ }
205
+ }
206
+ /**
207
+ * Triggered when a section is toogled. If the section is opened, all others
208
+ * sections are closed.
209
+ */
210
+ _onExpansionToggled(panel, index) {
211
+ if (!this.widgets[index].isVisible) {
212
+ return;
213
+ }
214
+ for (let i = 0; i < this.widgets.length; i++) {
215
+ if (i !== index) {
216
+ panel.collapse(i);
217
+ }
218
+ }
219
+ }
220
+ }
221
+ /**
222
+ * The chat section containing a chat widget.
223
+ */
224
+ class ChatSection extends PanelWithToolbar {
225
+ /**
226
+ * Constructor of the chat section.
227
+ */
228
+ constructor(options) {
229
+ var _a;
230
+ super(options);
231
+ /**
232
+ * Change the title when messages are unread.
233
+ *
234
+ * TODO: fix it upstream in @jupyterlab/ui-components.
235
+ * Updating the title create a new Title widget, but does not attach again the
236
+ * toolbar. The toolbar is attached only when the title widget is attached the first
237
+ * time.
238
+ */
239
+ this._unreadChanged = (_, unread) => {
240
+ this._markAsRead.enabled = unread.length > 0;
241
+ // this.title.label = `${unread.length ? '* ' : ''}${this._name}`;
242
+ };
243
+ this.addWidget(options.widget);
244
+ this.addClass(SECTION_CLASS);
245
+ this._defaultDirectory = options.defaultDirectory;
246
+ this._path = options.path;
247
+ this._updateTitle();
248
+ this.toolbar.addClass(TOOLBAR_CLASS);
249
+ this._markAsRead = new ToolbarButton({
250
+ icon: readIcon,
251
+ iconLabel: 'Mark chat as read',
252
+ className: 'jp-mod-styled',
253
+ onClick: () => (this.model.unreadMessages = [])
254
+ });
255
+ const moveToMain = new ToolbarButton({
256
+ icon: launchIcon,
257
+ iconLabel: 'Move the chat to the main area',
258
+ className: 'jp-mod-styled',
259
+ onClick: () => {
260
+ this.model.dispose();
261
+ options.commands.execute(CommandIDs.openChat, {
262
+ filepath: this._path
263
+ });
264
+ this.dispose();
265
+ }
266
+ });
267
+ const closeButton = new ToolbarButton({
268
+ icon: closeIcon,
269
+ iconLabel: 'Close the chat',
270
+ className: 'jp-mod-styled',
271
+ onClick: () => {
272
+ this.model.dispose();
273
+ this.dispose();
274
+ }
275
+ });
276
+ this.toolbar.addItem('jupyterlabChat-markRead', this._markAsRead);
277
+ this.toolbar.addItem('jupyterlabChat-moveMain', moveToMain);
278
+ this.toolbar.addItem('jupyterlabChat-close', closeButton);
279
+ (_a = this.model.unreadChanged) === null || _a === void 0 ? void 0 : _a.connect(this._unreadChanged);
280
+ this._markAsRead.enabled = this.model.unreadMessages.length > 0;
281
+ options.widget.node.style.height = '100%';
282
+ }
283
+ /**
284
+ * The path of the chat.
285
+ */
286
+ get path() {
287
+ return this._path;
288
+ }
289
+ /**
290
+ * Set the default directory property.
291
+ */
292
+ set defaultDirectory(value) {
293
+ this._defaultDirectory = value;
294
+ this._updateTitle();
295
+ }
296
+ /**
297
+ * The model of the widget.
298
+ */
299
+ get model() {
300
+ return this.widgets[0].model;
301
+ }
302
+ /**
303
+ * Dispose of the resources held by the widget.
304
+ */
305
+ dispose() {
306
+ var _a;
307
+ (_a = this.model.unreadChanged) === null || _a === void 0 ? void 0 : _a.disconnect(this._unreadChanged);
308
+ super.dispose();
309
+ }
310
+ /**
311
+ * Update the section's title, depending on the default directory and chat file name.
312
+ * If the chat file is in the default directory, the section's name is its relative
313
+ * path to that default directory. Otherwise, it is it absolute path.
314
+ */
315
+ _updateTitle() {
316
+ const inDefault = this._defaultDirectory
317
+ ? !PathExt.relative(this._defaultDirectory, this._path).startsWith('..')
318
+ : true;
319
+ const pattern = new RegExp(`${chatFileType.extensions[0]}$`, 'g');
320
+ this.title.label = (inDefault
321
+ ? this._defaultDirectory
322
+ ? PathExt.relative(this._defaultDirectory, this._path)
323
+ : this._path
324
+ : '/' + this._path).replace(pattern, '');
325
+ this.title.caption = this._path;
326
+ }
327
+ }
328
+ /**
329
+ * A component to select a chat from the drive.
330
+ */
331
+ function ChatSelect({ chatNamesChanged, handleChange }) {
332
+ // An object associating a chat name to its path. Both are purely indicative, the name
333
+ // is the section title and the path is used as caption.
334
+ const [chatNames, setChatNames] = useState({});
335
+ // Update the chat list.
336
+ chatNamesChanged.connect((_, chatNames) => {
337
+ setChatNames(chatNames);
338
+ });
339
+ return (React.createElement(HTMLSelect, { onChange: handleChange },
340
+ React.createElement("option", { value: "-" }, "Open a chat"),
341
+ Object.keys(chatNames).map(name => (React.createElement("option", { value: chatNames[name] }, name)))));
342
+ }
package/lib/ychat.d.ts ADDED
@@ -0,0 +1,75 @@
1
+ import { IChatMessage, IUser } from '@jupyter/chat';
2
+ import { Delta, DocumentChange, IMapChange, YDocument } from '@jupyter/ydoc';
3
+ import { JSONObject, PartialJSONValue } from '@lumino/coreutils';
4
+ /**
5
+ * The type for a YMessage.
6
+ */
7
+ export type IYmessage = IChatMessage<string>;
8
+ /**
9
+ * The type for a YMessage.
10
+ */
11
+ export type IMetadata = PartialJSONValue;
12
+ /**
13
+ * Definition of the shared Chat changes.
14
+ */
15
+ export interface IChatChanges extends DocumentChange {
16
+ /**
17
+ * Changes in messages.
18
+ */
19
+ messageChanges?: MessageChange;
20
+ /**
21
+ * Changes in users.
22
+ */
23
+ userChanges?: UserChange[];
24
+ /**
25
+ * Changes in metadata.
26
+ */
27
+ metadataChanges?: MetadataChange[];
28
+ }
29
+ /**
30
+ * The message change type.
31
+ */
32
+ export type MessageChange = Delta<IYmessage[]>;
33
+ /**
34
+ * The user change type.
35
+ */
36
+ export type UserChange = IMapChange<IUser>;
37
+ /**
38
+ * The metadata change type.
39
+ */
40
+ export type MetadataChange = IMapChange<IMetadata>;
41
+ /**
42
+ * The jupyterlab chat shared document.
43
+ */
44
+ export declare class YChat extends YDocument<IChatChanges> {
45
+ /**
46
+ * Create a new jupyterlab chat model.
47
+ */
48
+ constructor(options?: YDocument.IOptions);
49
+ /**
50
+ * Document version
51
+ */
52
+ readonly version: string;
53
+ /**
54
+ * Static method to create instances on the sharedModel
55
+ *
56
+ * @returns The sharedModel instance
57
+ */
58
+ static create(options?: YDocument.IOptions): YChat;
59
+ get id(): string;
60
+ get users(): JSONObject;
61
+ get messages(): string[];
62
+ getUser(username: string | undefined): IUser | undefined;
63
+ setUser(value: IUser): void;
64
+ getMessage(index: number): IYmessage | undefined;
65
+ addMessage(value: IYmessage): void;
66
+ updateMessage(index: number, value: IYmessage): void;
67
+ getMessageIndex(id: string): number;
68
+ deleteMessage(index: number): void;
69
+ private _usersObserver;
70
+ private _messagesObserver;
71
+ private _metadataObserver;
72
+ private _users;
73
+ private _messages;
74
+ private _metadata;
75
+ }