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

Update @jupyter/chat to v0.8.1 #57

Merged
merged 3 commits into from
Mar 11, 2025
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"watch:labextension": "jupyter labextension watch ."
},
"dependencies": {
"@jupyter/chat": "^0.7.1",
"@jupyter/chat": "^0.8.1",
"@jupyterlab/application": "^4.4.0-alpha.0",
"@jupyterlab/apputils": "^4.5.0-alpha.0",
"@jupyterlab/completer": "^4.4.0-alpha.0",
Expand Down
34 changes: 34 additions & 0 deletions src/chat-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
*/

import {
ChatCommand,
ChatModel,
IChatCommandProvider,
IChatHistory,
IChatMessage,
IInputModel,
INewMessage
} from '@jupyter/chat';
import type { BaseChatModel } from '@langchain/core/language_models/chat_models';
Expand Down Expand Up @@ -186,4 +189,35 @@ export namespace ChatHandler {
export interface IOptions extends ChatModel.IOptions {
providerRegistry: IAIProviderRegistry;
}

export class ClearCommandProvider implements IChatCommandProvider {
public id: string = '@jupyterlite/ai:clear-commands';
private _slash_commands: ChatCommand[] = [
{
name: '/clear',
providerId: this.id,
replaceWith: '/clear',
description: 'Clear the chat'
}
];
async getChatCommands(inputModel: IInputModel) {
const match = inputModel.currentWord?.match(/^\/\w*/)?.[0];
if (!match) {
return [];
}

const commands = this._slash_commands.filter(cmd =>
cmd.name.startsWith(match)
);
return commands;
}

async handleChatCommand(
command: ChatCommand,
inputModel: IInputModel
): Promise<void> {
// no handling needed because `replaceWith` is set in each command.
return;
}
}
}
51 changes: 17 additions & 34 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import {
ActiveCellManager,
AutocompletionRegistry,
buildChatSidebar,
buildErrorWidget,
ChatCommandRegistry,
IActiveCellManager,
IAutocompletionCommandsProps,
IAutocompletionRegistry
IChatCommandRegistry
} from '@jupyter/chat';
import {
JupyterFrontEnd,
Expand All @@ -24,47 +23,31 @@ import { CompletionProvider } from './completion-provider';
import { AIProviders } from './llm-models';
import { AIProviderRegistry } from './provider';
import { aiSettingsRenderer } from './settings/panel';
import { renderSlashCommandOption } from './slash-commands';
import { IAIProviderRegistry } from './tokens';

const autocompletionRegistryPlugin: JupyterFrontEndPlugin<IAutocompletionRegistry> =
{
id: '@jupyterlite/ai:autocompletion-registry',
description: 'Autocompletion registry',
autoStart: true,
provides: IAutocompletionRegistry,
activate: () => {
const autocompletionRegistry = new AutocompletionRegistry();
const options = ['/clear'];
const autocompletionCommands: IAutocompletionCommandsProps = {
opener: '/',
commands: options.map(option => {
return {
id: option.slice(1),
label: option,
description: 'Clear the chat window'
};
}),
props: {
renderOption: renderSlashCommandOption
}
};
autocompletionRegistry.add('jupyterlite-ai', autocompletionCommands);
return autocompletionRegistry;
}
};
const chatCommandRegistryPlugin: JupyterFrontEndPlugin<IChatCommandRegistry> = {
id: '@jupyterlite/ai:autocompletion-registry',
description: 'Autocompletion registry',
autoStart: true,
provides: IChatCommandRegistry,
activate: () => {
const registry = new ChatCommandRegistry();
registry.addProvider(new ChatHandler.ClearCommandProvider());
return registry;
}
};

const chatPlugin: JupyterFrontEndPlugin<void> = {
id: '@jupyterlite/ai:chat',
description: 'LLM chat extension',
autoStart: true,
requires: [IAIProviderRegistry, IRenderMimeRegistry, IAutocompletionRegistry],
requires: [IAIProviderRegistry, IRenderMimeRegistry, IChatCommandRegistry],
optional: [INotebookTracker, ISettingRegistry, IThemeManager],
activate: async (
app: JupyterFrontEnd,
providerRegistry: IAIProviderRegistry,
rmRegistry: IRenderMimeRegistry,
autocompletionRegistry: IAutocompletionRegistry,
chatCommandRegistry: IChatCommandRegistry,
notebookTracker: INotebookTracker | null,
settingsRegistry: ISettingRegistry | null,
themeManager: IThemeManager | null
Expand Down Expand Up @@ -120,7 +103,7 @@ const chatPlugin: JupyterFrontEndPlugin<void> = {
model: chatHandler,
themeManager,
rmRegistry,
autocompletionRegistry
chatCommandRegistry
});
chatWidget.title.caption = 'Jupyterlite AI Chat';
} catch (e) {
Expand Down Expand Up @@ -201,7 +184,7 @@ const providerRegistryPlugin: JupyterFrontEndPlugin<IAIProviderRegistry> = {

export default [
providerRegistryPlugin,
autocompletionRegistryPlugin,
chatCommandRegistryPlugin,
chatPlugin,
completerPlugin
];
55 changes: 0 additions & 55 deletions src/slash-commands.tsx

This file was deleted.

Loading