-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.ts
516 lines (470 loc) · 15.1 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/*
* Copyright (c) Jupyter Development Team.
* Distributed under the terms of the Modified BSD License.
*/
import { chatIcon } from '@jupyter/chat';
import { ICollaborativeDrive } from '@jupyter/docprovider';
import {
ILayoutRestorer,
JupyterFrontEnd,
JupyterFrontEndPlugin
} from '@jupyterlab/application';
import {
ICommandPalette,
IThemeManager,
IToolbarWidgetRegistry,
InputDialog,
WidgetTracker,
createToolbarFactory,
showErrorMessage
} from '@jupyterlab/apputils';
import { PathExt } from '@jupyterlab/coreutils';
import { DocumentRegistry } from '@jupyterlab/docregistry';
import { ILauncher } from '@jupyterlab/launcher';
import { IObservableList } from '@jupyterlab/observables';
import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
import { Contents } from '@jupyterlab/services';
import { ISettingRegistry } from '@jupyterlab/settingregistry';
import { ITranslator, nullTranslator } from '@jupyterlab/translation';
import { launchIcon } from '@jupyterlab/ui-components';
import {
WidgetConfig,
ChatWidgetFactory,
CollaborativeChatModelFactory
} from './factory';
import { CollaborativeChatModel } from './model';
import { chatFileType, CommandIDs, IChatPanel, IChatFactory } from './token';
import { ChatPanel, CollaborativeChatPanel } from './widget';
import { YChat } from './ychat';
const FACTORY = 'Chat';
const pluginIds = {
chatCommands: 'jupyterlab-collaborative-chat:commands',
docFactories: 'jupyterlab-collaborative-chat:factory',
chatPanel: 'jupyterlab-collaborative-chat:chat-panel'
};
/**
* Extension registering the chat file type.
*/
const docFactories: JupyterFrontEndPlugin<IChatFactory> = {
id: pluginIds.docFactories,
description: 'A document factories for collaborative chat',
autoStart: true,
requires: [IRenderMimeRegistry],
optional: [
ICollaborativeDrive,
ILayoutRestorer,
ISettingRegistry,
IThemeManager,
IToolbarWidgetRegistry,
ITranslator
],
provides: IChatFactory,
activate: (
app: JupyterFrontEnd,
rmRegistry: IRenderMimeRegistry,
drive: ICollaborativeDrive | null,
restorer: ILayoutRestorer | null,
settingRegistry: ISettingRegistry | null,
themeManager: IThemeManager | null,
toolbarRegistry: IToolbarWidgetRegistry | null,
translator_: ITranslator | null
): IChatFactory => {
const translator = translator_ ?? nullTranslator;
// Declare the toolbar factory.
let toolbarFactory:
| ((
widget: CollaborativeChatPanel
) =>
| DocumentRegistry.IToolbarItem[]
| IObservableList<DocumentRegistry.IToolbarItem>)
| undefined;
/**
* Load the settings for the chat widgets.
*/
let sendWithShiftEnter = false;
let stackMessages = true;
function loadSetting(setting: ISettingRegistry.ISettings): void {
// Read the settings and convert to the correct type
sendWithShiftEnter = setting.get('sendWithShiftEnter')
.composite as boolean;
stackMessages = setting.get('stackMessages').composite as boolean;
widgetConfig.configChanged.emit({ sendWithShiftEnter, stackMessages });
}
if (settingRegistry) {
// Create the main area widget toolbar factory.
if (toolbarRegistry) {
toolbarFactory = createToolbarFactory(
toolbarRegistry,
settingRegistry,
FACTORY,
pluginIds.docFactories,
translator
);
}
// Wait for the application to be restored and
// for the settings to be loaded
Promise.all([app.restored, settingRegistry.load(pluginIds.docFactories)])
.then(([, setting]) => {
// Read the settings
loadSetting(setting);
// Listen for the plugin setting changes
setting.changed.connect(loadSetting);
})
.catch(reason => {
console.error(
`Something went wrong when reading the settings.\n${reason}`
);
});
}
/**
* The chat config object.
*/
const widgetConfig = new WidgetConfig({ sendWithShiftEnter });
// Namespace for the tracker
const namespace = 'chat';
// Creating the tracker for the document
const tracker = new WidgetTracker<CollaborativeChatPanel>({ namespace });
app.docRegistry.addFileType(chatFileType);
if (drive) {
const chatFactory = () => {
return YChat.create();
};
drive.sharedModelFactory.registerDocumentFactory('chat', chatFactory);
}
app.serviceManager.ready
.then(() => {
const user = app.serviceManager.user.identity;
// Creating and registering the model factory for our custom DocumentModel
const modelFactory = new CollaborativeChatModelFactory({
user,
widgetConfig
});
app.docRegistry.addModelFactory(modelFactory);
})
.catch(e =>
console.error(
'The collaborative chat model factory is not initialized',
e
)
);
// Creating the widget factory to register it so the document manager knows about
// our new DocumentWidget
const widgetFactory = new ChatWidgetFactory({
name: FACTORY,
label: 'Chat',
modelName: 'chat',
fileTypes: ['chat'],
defaultFor: ['chat'],
themeManager,
rmRegistry,
toolbarFactory,
translator
});
// Add the widget to the tracker when it's created
widgetFactory.widgetCreated.connect((sender, widget) => {
// Notify the instance tracker if restore data needs to update.
widget.context.pathChanged.connect(() => {
tracker.save(widget);
});
tracker.add(widget);
});
// Registering the widget factory
app.docRegistry.addWidgetFactory(widgetFactory);
// Handle state restoration.
if (restorer) {
void restorer.restore(tracker, {
command: 'docmanager:open',
args: panel => ({ path: panel.context.path, factory: FACTORY }),
name: panel => panel.context.path,
when: app.serviceManager.ready
});
}
return { widgetConfig, tracker };
}
};
/**
* Extension providing the commands, menu and laucher.
*/
const chatCommands: JupyterFrontEndPlugin<void> = {
id: pluginIds.chatCommands,
description: 'The commands to create or open a chat',
autoStart: true,
requires: [ICollaborativeDrive, IChatFactory],
optional: [IChatPanel, ICommandPalette, ILauncher],
activate: (
app: JupyterFrontEnd,
drive: ICollaborativeDrive,
factory: IChatFactory,
chatPanel: ChatPanel | null,
commandPalette: ICommandPalette | null,
launcher: ILauncher | null
) => {
const { commands } = app;
const { widgetConfig } = factory;
/**
* Command to create a new chat.
*
* args:
* name - optional, the name of the chat to create.
* Open a dialog if not provided.
* inSidePanel - optional (default to false).
* Whether to open the chat in side panel or in main area.
* isPalette - optional (default to false).
* Whether the command is in commands palette or not.
*/
commands.addCommand(CommandIDs.createChat, {
label: args => (args.isPalette ? 'Create a new chat' : 'Chat'),
caption: 'Create a chat',
icon: args => (args.isPalette ? undefined : chatIcon),
execute: async args => {
const inSidePanel: boolean = (args.inSidePanel as boolean) ?? false;
let name: string | null = (args.name as string) ?? null;
let filepath = '';
if (!name) {
name = (
await InputDialog.getText({
label: 'Name',
placeholder: 'untitled',
title: 'Create a new chat'
})
).value;
}
// no-op if the dialog has been cancelled.
// Fill the filepath if the dialog has been validated with content,
// otherwise create a new untitled chat (empty filepath).
if (name === null) {
return;
} else if (name) {
if (name.endsWith(chatFileType.extensions[0])) {
filepath = name;
} else {
filepath = `${name}${chatFileType.extensions[0]}`;
}
}
let fileExist = true;
if (filepath) {
await drive.get(filepath, { content: false }).catch(() => {
fileExist = false;
});
} else {
fileExist = false;
}
// Create a new file if it does not exists
if (!fileExist) {
// Create a new untitled chat.
let model: Contents.IModel | null = await drive.newUntitled({
type: 'file',
ext: chatFileType.extensions[0]
});
// Rename it if a name has been provided.
if (filepath) {
model = await drive.rename(model.path, filepath);
}
if (!model) {
showErrorMessage(
'Error creating a chat',
'An error occured while creating the chat'
);
return '';
}
filepath = model.path;
}
if (commands.hasCommand(CommandIDs.openChat)) {
return commands.execute(CommandIDs.openChat, {
filepath,
inSidePanel
});
} else {
commands.execute('docmanager:open', {
path: `RTC:${filepath}`,
factory: FACTORY
});
}
}
});
// Add the command to the palette
if (commandPalette) {
commandPalette.addItem({
category: 'Chat',
command: CommandIDs.createChat,
args: { isPalette: true }
});
}
// Add the create command to the launcher
if (launcher) {
launcher.add({
command: CommandIDs.createChat,
category: 'Chat',
rank: 1
});
}
app.serviceManager.ready
.then(() => {
const user = app.serviceManager.user.identity;
/*
* Command to open a chat.
*
* args:
* filepath - the chat file to open.
*/
commands.addCommand(CommandIDs.openChat, {
label: 'Open a chat',
execute: async args => {
const inSidePanel: boolean = (args.inSidePanel as boolean) ?? false;
let filepath: string | null = (args.filepath as string) ?? null;
if (filepath === null) {
filepath = (
await InputDialog.getText({
label: 'File path',
placeholder: '/path/to/the/chat/file',
title: 'Path of the chat'
})
).value;
}
if (!filepath) {
return;
}
let fileExist = true;
await drive.get(filepath, { content: false }).catch(() => {
fileExist = false;
});
if (!fileExist) {
showErrorMessage(
'Error opening chat',
`'${filepath}' is not a valid path`
);
return;
}
if (inSidePanel && chatPanel) {
// The chat is opened in the chat panel.
app.shell.activateById(chatPanel.id);
const model = await drive.get(filepath);
/**
* Create a share model from the chat file
*/
const sharedModel = drive.sharedModelFactory.createNew({
path: model.path,
format: model.format,
contentType: chatFileType.contentType,
collaborative: true
}) as YChat;
/**
* Initialize the chat model with the share model
*/
const chat = new CollaborativeChatModel({
user,
sharedModel,
widgetConfig
});
/**
* Add a chat widget to the side panel.
*/
chatPanel.addChat(
chat,
PathExt.basename(model.name, chatFileType.extensions[0])
);
} else {
// The chat is opened in the main area
commands.execute('docmanager:open', {
path: `RTC:${filepath}`,
factory: FACTORY
});
}
}
});
// Add the command to the palette
if (commandPalette) {
commandPalette.addItem({
category: 'Chat',
command: CommandIDs.openChat
});
}
})
.catch(e =>
console.error('The command to open a chat is not initialized\n', e)
);
}
};
/*
* Extension providing a chat panel.
*/
const chatPanel: JupyterFrontEndPlugin<ChatPanel> = {
id: pluginIds.chatPanel,
description: 'A chat extension for Jupyter',
autoStart: true,
provides: IChatPanel,
requires: [ICollaborativeDrive, IRenderMimeRegistry],
optional: [ILayoutRestorer, IThemeManager],
activate: (
app: JupyterFrontEnd,
drive: ICollaborativeDrive,
rmRegistry: IRenderMimeRegistry,
restorer: ILayoutRestorer | null,
themeManager: IThemeManager | null
): ChatPanel => {
const { commands } = app;
/**
* Add Chat widget to left sidebar
*/
const chatPanel = new ChatPanel({
commands,
drive,
rmRegistry,
themeManager
});
chatPanel.id = 'JupyterCollaborationChat:sidepanel';
chatPanel.title.icon = chatIcon;
chatPanel.title.caption = 'Jupyter Chat'; // TODO: i18n/
app.shell.add(chatPanel, 'left', {
rank: 2000
});
if (restorer) {
restorer.add(chatPanel, 'jupyter-chat');
}
// Use events system to watch changes on files.
const schemaID =
'https://events.jupyter.org/jupyter_server/contents_service/v1';
const actions = ['create', 'delete', 'rename'];
app.serviceManager.events.stream.connect((_, emission) => {
if (emission.schema_id === schemaID) {
const action = emission.action as string;
if (actions.includes(action)) {
chatPanel.updateChatNames();
}
}
});
/*
* Command to move a chat from the main area to the side panel.
*
*/
commands.addCommand(CommandIDs.moveToSide, {
label: 'Move the chat to the side panel',
caption: 'Move the chat to the side panel',
icon: launchIcon,
isEnabled: () => commands.hasCommand(CommandIDs.openChat),
execute: async () => {
const widget = app.shell.currentWidget;
// Ensure widget is a CollaborativeChatPanel and is in main area
if (
!widget ||
!(widget instanceof CollaborativeChatPanel) ||
!Array.from(app.shell.widgets('main')).includes(widget)
) {
console.error(
`The command '${CommandIDs.moveToSide}' should be executed from the toolbar button only`
);
return;
}
// Remove potential drive prefix
const filepath = widget.context.path.split(':').pop();
commands.execute(CommandIDs.openChat, {
filepath,
inSidePanel: true
});
widget.dispose();
}
});
return chatPanel;
}
};
export default [chatCommands, docFactories, chatPanel];