Skip to content

Commit 98ff95d

Browse files
committed
Add the chat model to the IChatProvider method's arguments
1 parent 1c4a090 commit 98ff95d

File tree

7 files changed

+44
-15
lines changed

7 files changed

+44
-15
lines changed

packages/jupyter-chat/src/chat-commands/registry.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import { Token } from '@lumino/coreutils';
77
import { ChatCommand, IChatCommandProvider } from './types';
88
import { IInputModel } from '../input-model';
9+
import { IChatModel } from '../model';
910

1011
/**
1112
* Interface of a chat command registry, which tracks a list of chat command
@@ -20,7 +21,11 @@ export interface IChatCommandRegistry {
2021
* Handles a chat command by calling `handleChatCommand()` on the provider
2122
* corresponding to this chat command.
2223
*/
23-
handleChatCommand(command: ChatCommand, inputModel: IInputModel): void;
24+
handleChatCommand(
25+
command: ChatCommand,
26+
inputModel: IInputModel,
27+
chatModel: IChatModel
28+
): void;
2429
}
2530

2631
/**
@@ -39,7 +44,11 @@ export class ChatCommandRegistry implements IChatCommandRegistry {
3944
return Array.from(this._providers.values());
4045
}
4146

42-
handleChatCommand(command: ChatCommand, inputModel: IInputModel) {
47+
handleChatCommand(
48+
command: ChatCommand,
49+
inputModel: IInputModel,
50+
chatModel: IChatModel
51+
) {
4352
const provider = this._providers.get(command.providerId);
4453
if (!provider) {
4554
console.error(
@@ -49,7 +58,7 @@ export class ChatCommandRegistry implements IChatCommandRegistry {
4958
return;
5059
}
5160

52-
provider.handleChatCommand(command, inputModel);
61+
provider.handleChatCommand(command, inputModel, chatModel);
5362
}
5463

5564
private _providers: Map<string, IChatCommandProvider>;

packages/jupyter-chat/src/chat-commands/types.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import { LabIcon } from '@jupyterlab/ui-components';
77
import { IInputModel } from '../input-model';
8+
import { IChatModel } from '../model';
89

910
export type ChatCommand = {
1011
/**
@@ -54,14 +55,18 @@ export interface IChatCommandProvider {
5455
* valid chat commands that match the current word. The current word is
5556
* space-separated word at the user's cursor.
5657
*/
57-
getChatCommands(inputModel: IInputModel): Promise<ChatCommand[]>;
58+
getChatCommands(
59+
inputModel: IInputModel,
60+
chatModel: IChatModel
61+
): Promise<ChatCommand[]>;
5862

5963
/**
6064
* Function called when a chat command is run by the user through the chat
6165
* commands menu.
6266
*/
6367
handleChatCommand(
6468
command: ChatCommand,
65-
inputModel: IInputModel
69+
inputModel: IInputModel,
70+
chatModel: IChatModel
6671
): Promise<void>;
6772
}

packages/jupyter-chat/src/components/chat-input.tsx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,24 @@ import React, { useEffect, useRef, useState } from 'react';
1818

1919
import { AttachmentPreviewList } from './attachments';
2020
import { AttachButton, CancelButton, SendButton } from './input';
21-
import { IInputModel, InputModel } from '../input-model';
22-
import { IAttachment, Selection } from '../types';
2321
import { useChatCommands } from './input/use-chat-commands';
22+
import { IInputModel, InputModel } from '../input-model';
2423
import { IChatCommandRegistry } from '../chat-commands';
24+
import { IChatModel } from '../model';
25+
import { IAttachment, Selection } from '../types';
2526

2627
const INPUT_BOX_CLASS = 'jp-chat-input-container';
2728

2829
export function ChatInput(props: ChatInput.IProps): JSX.Element {
29-
const { documentManager, model } = props;
30+
const { chatModel, documentManager, model } = props;
3031
const [input, setInput] = useState<string>(model.value);
3132
const inputRef = useRef<HTMLInputElement>();
3233

33-
const chatCommands = useChatCommands(model, props.chatCommandRegistry);
34+
const chatCommands = useChatCommands(
35+
model,
36+
chatModel,
37+
props.chatCommandRegistry
38+
);
3439

3540
const [sendWithShiftEnter, setSendWithShiftEnter] = useState<boolean>(
3641
model.config.sendWithShiftEnter ?? false
@@ -273,9 +278,13 @@ export namespace ChatInput {
273278
*/
274279
export interface IProps {
275280
/**
276-
* The chat model.
281+
* The input model.
277282
*/
278283
model: IInputModel;
284+
/**
285+
* The chat model.
286+
*/
287+
chatModel: IChatModel;
279288
/**
280289
* The function to be called to send the message.
281290
*/

packages/jupyter-chat/src/components/chat-messages.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ export const ChatMessage = forwardRef<HTMLDivElement, ChatMessageProps>(
417417
hideIncludeSelection={true}
418418
chatCommandRegistry={props.chatCommandRegistry}
419419
documentManager={props.documentManager}
420+
chatModel={model}
420421
/>
421422
) : (
422423
<MarkdownRenderer

packages/jupyter-chat/src/components/chat.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export function ChatBody(props: Chat.IChatBodyProps): JSX.Element {
4747
model={model.input}
4848
documentManager={props.documentManager}
4949
chatCommandRegistry={props.chatCommandRegistry}
50+
chatModel={model}
5051
/>
5152
</AttachmentOpenerContext.Provider>
5253
);

packages/jupyter-chat/src/components/input/use-chat-commands.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { Box } from '@mui/material';
1313

1414
import { ChatCommand, IChatCommandRegistry } from '../../chat-commands';
1515
import { IInputModel } from '../../input-model';
16+
import { IChatModel } from '../../model';
1617

1718
type AutocompleteProps = GenericAutocompleteProps<any, any, any, any>;
1819

@@ -32,6 +33,7 @@ type UseChatCommandsReturn = {
3233
*/
3334
export function useChatCommands(
3435
inputModel: IInputModel,
36+
chatModel: IChatModel,
3537
chatCommandRegistry?: IChatCommandRegistry
3638
): UseChatCommandsReturn {
3739
// whether an option is highlighted in the chat commands menu
@@ -63,7 +65,7 @@ export function useChatCommands(
6365
// TODO: optimize performance when this method is truly async
6466
try {
6567
newCommands = newCommands.concat(
66-
await provider.getChatCommands(inputModel)
68+
await provider.getChatCommands(inputModel, chatModel)
6769
);
6870
} catch (e) {
6971
console.error(
@@ -116,7 +118,7 @@ export function useChatCommands(
116118
}
117119

118120
// otherwise, defer handling to the command provider
119-
chatCommandRegistry.handleChatCommand(command, inputModel);
121+
chatCommandRegistry.handleChatCommand(command, inputModel, chatModel);
120122
};
121123

122124
return {

python/jupyterlab-chat/src/chat-commands/providers/emoji.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import {
88
IChatCommandProvider,
99
IChatCommandRegistry,
1010
ChatCommand,
11-
IInputModel
11+
IInputModel,
12+
IChatModel
1213
} from '@jupyter/chat';
1314

1415
export class EmojiCommandProvider implements IChatCommandProvider {
@@ -47,7 +48,7 @@ export class EmojiCommandProvider implements IChatCommandProvider {
4748
// regex used to test the current word
4849
private _regex: RegExp = /^:\w*:?/;
4950

50-
async getChatCommands(inputModel: IInputModel) {
51+
async getChatCommands(inputModel: IInputModel, chatModel: IChatModel) {
5152
const match = inputModel.currentWord?.match(this._regex)?.[0];
5253
if (!match) {
5354
return [];
@@ -61,7 +62,8 @@ export class EmojiCommandProvider implements IChatCommandProvider {
6162

6263
async handleChatCommand(
6364
command: ChatCommand,
64-
inputModel: IInputModel
65+
inputModel: IInputModel,
66+
chatModel: IChatModel
6567
): Promise<void> {
6668
// no handling needed because `replaceWith` is set in each command.
6769
return;

0 commit comments

Comments
 (0)