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

Add commands, menu entry and launcher #12

Merged
merged 3 commits into from
Apr 10, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ def set(self, value: str) -> None:
:param value: The content of the document.
:type value: str
"""
contents = json.loads(value)
try:
contents = json.loads(value)
except json.JSONDecodeError:
contents = dict()
if "messages" in contents.keys():
with self._ydoc.transaction():
for v in contents["messages"]:
Expand Down
1 change: 1 addition & 0 deletions packages/jupyterlab-collaborative-chat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"@jupyterlab/apputils": "^4.0.0",
"@jupyterlab/coreutils": "^6.0.0",
"@jupyterlab/docregistry": "^4.0.0",
"@jupyterlab/launcher": "^4.0.0",
"@jupyterlab/rendermime": "^4.0.0",
"@jupyterlab/services": "^7.0.0",
"@jupyterlab/settingregistry": "^4.0.0",
Expand Down
28 changes: 28 additions & 0 deletions packages/jupyterlab-collaborative-chat/schema/commands.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"title": "Jupyter collaborative chat",
"description": "Configuration for the chat commands",
"type": "object",
"jupyter.lab.menus": {
"main": [
{
"id": "jp-mainmenu-file",
"items": [
{
"type": "submenu",
"submenu": {
"id": "jp-mainmenu-file-new",
"items": [
{
"command": "collaborative-chat:create",
"rank": 10
}
]
}
}
]
}
]
},
"properties": {},
"additionalProperties": false
}
168 changes: 165 additions & 3 deletions packages/jupyterlab-collaborative-chat/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,26 @@ import {
JupyterFrontEnd,
JupyterFrontEndPlugin
} from '@jupyterlab/application';
import { IThemeManager, WidgetTracker } from '@jupyterlab/apputils';
import {
ICommandPalette,
IThemeManager,
InputDialog,
WidgetTracker,
showErrorMessage
} from '@jupyterlab/apputils';
import { ILauncher } from '@jupyterlab/launcher';
import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
import { Awareness } from 'y-protocols/awareness';

import { ChatWidgetFactory, CollaborativeChatModelFactory } from './factory';
import { IChatFileType } from './token';
import { CommandIDs, IChatFileType } from './token';
import { CollaborativeChatWidget } from './widget';
import { YChat } from './ychat';
import { Contents } from '@jupyterlab/services';
import { chatIcon } from 'chat-jupyter';

const pluginIds = {
chatCreation: 'jupyterlab-collaborative-chat:commands',
chatDocument: 'jupyterlab-collaborative-chat:chat-document'
};

Expand Down Expand Up @@ -106,4 +116,156 @@ export const chatDocument: JupyterFrontEndPlugin<IChatFileType> = {
}
};

export default chatDocument;
/**
* Extension registering the chat file type.
*/
export const chatCreation: JupyterFrontEndPlugin<void> = {
id: pluginIds.chatCreation,
description: 'The commands to create or open a chat',
autoStart: true,
requires: [IChatFileType, ICollaborativeDrive],
optional: [ICommandPalette, ILauncher],
activate: (
app: JupyterFrontEnd,
chatFileType: IChatFileType,
drive: ICollaborativeDrive,
commandPalette: ICommandPalette | null,
launcher: ILauncher | null
) => {
const { commands } = app;

/**
* Command to create a new chat.
*
* args:
* name: the name of the chat to create.
*/
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 => {
let name: string | null = (args.name as string) ?? null;
let filepath = '';
if (!name) {
name = (
await InputDialog.getText({
label: 'Name',
placeholder: 'untitled',
title: 'Name of the 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;
await drive.get(filepath, { content: false }).catch(() => {
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;
}
return commands.execute(CommandIDs.openChat, { filepath });
}
});

/**
* Command to open a chat.
*
* args:
* filepath - the chat file to open.
*/
commands.addCommand(CommandIDs.openChat, {
label: 'Open a chat',
execute: async args => {
let filepath: string | null = (args.filepath as string) ?? null;
if (!filepath) {
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;
}

commands.execute('docmanager:open', {
path: `RTC:${filepath}`,
factory: 'chat-factory'
});
}
});

// Add the commands to the palette
if (commandPalette) {
commandPalette.addItem({
category: 'Chat',
command: CommandIDs.createChat,
args: { isPalette: true }
});
commandPalette.addItem({
category: 'Chat',
command: CommandIDs.openChat
});
}

// Add the create command to the launcher
if (launcher) {
launcher.add({
command: CommandIDs.createChat,
category: 'Chat',
rank: 1
});
}
}
};

export default [chatDocument, chatCreation];
14 changes: 14 additions & 0 deletions packages/jupyterlab-collaborative-chat/src/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,17 @@ export const IChatFileType = new Token<IChatFileType>(
* Chat file type.
*/
export type IChatFileType = DocumentRegistry.IFileType;

/**
* Command ids.
*/
export const CommandIDs = {
/**
* Create a chat file.
*/
createChat: 'collaborative-chat:create',
/**
* Open a chat file.
*/
openChat: 'collaborative-chat:open'
};
19 changes: 19 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2673,6 +2673,24 @@ __metadata:
languageName: node
linkType: hard

"@jupyterlab/launcher@npm:^4.0.0":
version: 4.1.6
resolution: "@jupyterlab/launcher@npm:4.1.6"
dependencies:
"@jupyterlab/apputils": ^4.2.6
"@jupyterlab/translation": ^4.1.6
"@jupyterlab/ui-components": ^4.1.6
"@lumino/algorithm": ^2.0.1
"@lumino/commands": ^2.2.0
"@lumino/coreutils": ^2.1.2
"@lumino/disposable": ^2.1.2
"@lumino/properties": ^2.0.1
"@lumino/widgets": ^2.3.1
react: ^18.2.0
checksum: 50168b135751f3a20dab73f5fb62bac2d5a784fa818eac64dddc6fdc96f12da58dc0de3292c9ccd6ce717ff89c0ac99090dfeeafbf5dd5bbea9a71ecf0fa2c1a
languageName: node
linkType: hard

"@jupyterlab/lsp@npm:^4.1.6":
version: 4.1.6
resolution: "@jupyterlab/lsp@npm:4.1.6"
Expand Down Expand Up @@ -9818,6 +9836,7 @@ __metadata:
"@jupyterlab/builder": ^4.0.0
"@jupyterlab/coreutils": ^6.0.0
"@jupyterlab/docregistry": ^4.0.0
"@jupyterlab/launcher": ^4.0.0
"@jupyterlab/rendermime": ^4.0.0
"@jupyterlab/services": ^7.0.0
"@jupyterlab/settingregistry": ^4.0.0
Expand Down
Loading