Skip to content

Commit 317fedd

Browse files
brichetjtpio
andauthored
Add a welcome message (#89)
* Upgrade jupyter-chat to >=0.12.0 * Add a welcome message * Update src/chat-handler.ts Co-authored-by: Jeremy Tuloup <jeremy.tuloup@gmail.com> --------- Co-authored-by: Jeremy Tuloup <jeremy.tuloup@gmail.com>
1 parent 3281507 commit 317fedd

File tree

4 files changed

+46
-16
lines changed

4 files changed

+46
-16
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"watch:labextension": "jupyter labextension watch ."
5757
},
5858
"dependencies": {
59-
"@jupyter/chat": "^0.9.0",
59+
"@jupyter/chat": "^0.12.0",
6060
"@jupyterlab/application": "^4.4.0",
6161
"@jupyterlab/apputils": "^4.5.0",
6262
"@jupyterlab/completer": "^4.4.0",

src/chat-handler.ts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55

66
import {
77
ChatCommand,
8-
ChatModel,
8+
AbstractChatContext,
9+
AbstractChatModel,
910
IChatCommandProvider,
11+
IChatContext,
1012
IChatHistory,
1113
IChatMessage,
14+
IChatModel,
1215
IInputModel,
1316
INewMessage
1417
} from '@jupyter/chat';
@@ -22,8 +25,8 @@ import { UUID } from '@lumino/coreutils';
2225

2326
import { jupyternautLiteIcon } from './icons';
2427
import { chatSystemPrompt } from './provider';
25-
import { AIChatModel } from './types/ai-model';
2628
import { IAIProviderRegistry } from './tokens';
29+
import { AIChatModel } from './types/ai-model';
2730

2831
/**
2932
* The base64 encoded SVG string of the jupyternaut lite icon.
@@ -32,12 +35,24 @@ import { IAIProviderRegistry } from './tokens';
3235
const AI_AVATAR_BASE64 = btoa(jupyternautLiteIcon.svgstr);
3336
const AI_AVATAR = `data:image/svg+xml;base64,${AI_AVATAR_BASE64}`;
3437

38+
export const welcomeMessage = (providers: string[]) => `
39+
#### Ask JupyterLite AI
40+
41+
42+
The provider to use can be set in the settings editor, by selecting it from
43+
the <img src="${AI_AVATAR}" width="16" height="16"> _AI provider_ settings.
44+
45+
The current providers that are available are _${providers.sort().join('_, _')}_.
46+
47+
To clear the chat, you can use the \`/clear\` command from the chat input.
48+
`;
49+
3550
export type ConnectionMessage = {
3651
type: 'connection';
3752
client_id: string;
3853
};
3954

40-
export class ChatHandler extends ChatModel {
55+
export class ChatHandler extends AbstractChatModel {
4156
constructor(options: ChatHandler.IOptions) {
4257
super(options);
4358
this._providerRegistry = options.providerRegistry;
@@ -127,7 +142,7 @@ export class ChatHandler extends ChatModel {
127142
);
128143

129144
const sender = { username: this._personaName, avatar_url: AI_AVATAR };
130-
this.updateWriters([sender]);
145+
this.updateWriters([{ user: sender }]);
131146

132147
// create an empty message to be filled by the AI provider
133148
const botMsg: IChatMessage = {
@@ -185,6 +200,10 @@ export class ChatHandler extends ChatModel {
185200
this._controller?.abort();
186201
}
187202

203+
createChatContext(): IChatContext {
204+
return new ChatHandler.ChatContext({ model: this });
205+
}
206+
188207
private _providerRegistry: IAIProviderRegistry;
189208
private _personaName = 'AI';
190209
private _prompt: string;
@@ -198,12 +217,19 @@ export namespace ChatHandler {
198217
/**
199218
* The options used to create a chat handler.
200219
*/
201-
export interface IOptions extends ChatModel.IOptions {
220+
export interface IOptions extends IChatModel.IOptions {
202221
providerRegistry: IAIProviderRegistry;
203222
}
204223

205224
/**
206-
* The chat command provider for the chat.
225+
* The minimal chat context.
226+
*/
227+
export class ChatContext extends AbstractChatContext {
228+
users = [];
229+
}
230+
231+
/**
232+
* The chat command provider for the chat.
207233
*/
208234
export class ClearCommandProvider implements IChatCommandProvider {
209235
public id: string = '@jupyterlite/ai:clear-commands';

src/index.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { IFormRendererRegistry } from '@jupyterlab/ui-components';
2020
import { ReadonlyPartialJSONObject } from '@lumino/coreutils';
2121
import { ISecretsManager, SecretsManager } from 'jupyter-secrets-manager';
2222

23-
import { ChatHandler } from './chat-handler';
23+
import { ChatHandler, welcomeMessage } from './chat-handler';
2424
import { CompletionProvider } from './completion-provider';
2525
import { defaultProviderPlugins } from './default-providers';
2626
import { AIProviderRegistry } from './provider';
@@ -106,9 +106,11 @@ const chatPlugin: JupyterFrontEndPlugin<void> = {
106106
const stopButton = stopItem(() => chatHandler.stopStreaming());
107107
inputToolbarRegistry.addItem('stop', stopButton);
108108

109-
chatHandler.writersChanged.connect((_, users) => {
109+
chatHandler.writersChanged.connect((_, writers) => {
110110
if (
111-
users.filter(user => user.username === chatHandler.personaName).length
111+
writers.filter(
112+
writer => writer.user.username === chatHandler.personaName
113+
).length
112114
) {
113115
inputToolbarRegistry.hide('send');
114116
inputToolbarRegistry.show('stop');
@@ -124,7 +126,8 @@ const chatPlugin: JupyterFrontEndPlugin<void> = {
124126
themeManager,
125127
rmRegistry,
126128
chatCommandRegistry,
127-
inputToolbarRegistry
129+
inputToolbarRegistry,
130+
welcomeMessage: welcomeMessage(providerRegistry.providers)
128131
});
129132
chatWidget.title.caption = 'Jupyterlite AI Chat';
130133
} catch (e) {

yarn.lock

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -841,9 +841,9 @@ __metadata:
841841
languageName: node
842842
linkType: hard
843843

844-
"@jupyter/chat@npm:^0.9.0":
845-
version: 0.9.0
846-
resolution: "@jupyter/chat@npm:0.9.0"
844+
"@jupyter/chat@npm:^0.12.0":
845+
version: 0.12.0
846+
resolution: "@jupyter/chat@npm:0.12.0"
847847
dependencies:
848848
"@emotion/react": ^11.10.5
849849
"@emotion/styled": ^11.10.5
@@ -856,6 +856,7 @@ __metadata:
856856
"@jupyterlab/notebook": ^4.2.0
857857
"@jupyterlab/rendermime": ^4.2.0
858858
"@jupyterlab/ui-components": ^4.2.0
859+
"@lumino/algorithm": ^2.0.0
859860
"@lumino/commands": ^2.0.0
860861
"@lumino/coreutils": ^2.0.0
861862
"@lumino/disposable": ^2.0.0
@@ -865,7 +866,7 @@ __metadata:
865866
clsx: ^2.1.0
866867
react: ^18.2.0
867868
react-dom: ^18.2.0
868-
checksum: dee9a9e02ea8a6d25e1caebf4e9bece42c82fa40baa84dc655540bb64a676b4b1890373390c291f35eb1fe8f821d15935ec21fdd0e12063a497e575f4ddbcd78
869+
checksum: 4c36487123a5a176f2a865b7686b1f7d56930c3991d338be3a47286d7ebce5aac216ecd25e4f548e09e2592506a20e5756e489ed1a63ef93dec425b04295f9a9
869870
languageName: node
870871
linkType: hard
871872

@@ -1558,7 +1559,7 @@ __metadata:
15581559
version: 0.0.0-use.local
15591560
resolution: "@jupyterlite/ai@workspace:."
15601561
dependencies:
1561-
"@jupyter/chat": ^0.9.0
1562+
"@jupyter/chat": ^0.12.0
15621563
"@jupyterlab/application": ^4.4.0
15631564
"@jupyterlab/apputils": ^4.5.0
15641565
"@jupyterlab/builder": ^4.4.0

0 commit comments

Comments
 (0)