-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathchat-input.tsx
331 lines (303 loc) · 8.76 KB
/
chat-input.tsx
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
/*
* Copyright (c) Jupyter Development Team.
* Distributed under the terms of the Modified BSD License.
*/
import React, { useEffect, useRef, useState } from 'react';
import {
Autocomplete,
Box,
InputAdornment,
SxProps,
TextField,
Theme
} from '@mui/material';
import clsx from 'clsx';
import { CancelButton } from './input/cancel-button';
import { SendButton } from './input/send-button';
import { IChatModel } from '../model';
import { IAutocompletionRegistry } from '../registry';
import {
AutocompleteCommand,
IAutocompletionCommandsProps,
IConfig,
Selection
} from '../types';
const INPUT_BOX_CLASS = 'jp-chat-input-container';
export function ChatInput(props: ChatInput.IProps): JSX.Element {
const { autocompletionName, autocompletionRegistry, model } = props;
const autocompletion = useRef<IAutocompletionCommandsProps>();
const [input, setInput] = useState<string>(props.value || '');
const [sendWithShiftEnter, setSendWithShiftEnter] = useState<boolean>(
model.config.sendWithShiftEnter ?? false
);
const [typingNotification, setTypingNotification] = useState<boolean>(
model.config.sendTypingNotification ?? false
);
// Display the include selection menu if it is not explicitly hidden, and if at least
// one of the tool to check for text or cell selection is enabled.
let hideIncludeSelection = props.hideIncludeSelection ?? false;
if (model.activeCellManager === null && model.selectionWatcher === null) {
hideIncludeSelection = true;
}
// store reference to the input element to enable focusing it easily
const inputRef = useRef<HTMLInputElement>();
useEffect(() => {
const configChanged = (_: IChatModel, config: IConfig) => {
setSendWithShiftEnter(config.sendWithShiftEnter ?? false);
setTypingNotification(config.sendTypingNotification ?? false);
};
model.configChanged.connect(configChanged);
const focusInputElement = () => {
if (inputRef.current) {
inputRef.current.focus();
}
};
model.focusInputSignal?.connect(focusInputElement);
return () => {
model.configChanged?.disconnect(configChanged);
model.focusInputSignal?.disconnect(focusInputElement);
};
}, [model]);
// The autocomplete commands options.
const [commandOptions, setCommandOptions] = useState<AutocompleteCommand[]>(
[]
);
// whether any option is highlighted in the slash command autocomplete
const [highlighted, setHighlighted] = useState<boolean>(false);
// controls whether the slash command autocomplete is open
const [open, setOpen] = useState<boolean>(false);
const inputExists = !!input.trim();
/**
* Effect: fetch the list of available autocomplete commands.
*/
useEffect(() => {
if (autocompletionRegistry === undefined) {
return;
}
autocompletion.current = autocompletionName
? autocompletionRegistry.get(autocompletionName)
: autocompletionRegistry.getDefaultCompletion();
if (autocompletion.current === undefined) {
return;
}
if (Array.isArray(autocompletion.current.commands)) {
setCommandOptions(autocompletion.current.commands);
} else if (typeof autocompletion.current.commands === 'function') {
autocompletion.current
.commands()
.then((commands: AutocompleteCommand[]) => {
setCommandOptions(commands);
});
}
}, []);
/**
* Effect: Open the autocomplete when the user types the 'opener' string into an
* empty chat input. Close the autocomplete and reset the last selected value when
* the user clears the chat input.
*/
useEffect(() => {
if (!autocompletion.current?.opener) {
return;
}
if (input === autocompletion.current?.opener) {
setOpen(true);
return;
}
if (input === '') {
setOpen(false);
return;
}
}, [input]);
function handleKeyDown(event: React.KeyboardEvent<HTMLInputElement>) {
if (event.key !== 'Enter') {
return;
}
// Do not send the message if the user was selecting a suggested command from the
// Autocomplete component.
if (highlighted) {
return;
}
// Do not send empty messages, and avoid adding new line in empty message.
if (!inputExists) {
event.stopPropagation();
event.preventDefault();
return;
}
if (
(sendWithShiftEnter && event.shiftKey) ||
(!sendWithShiftEnter && !event.shiftKey)
) {
onSend();
event.stopPropagation();
event.preventDefault();
}
}
/**
* Triggered when sending the message.
*
* Add code block if cell or text is selected.
*/
function onSend(selection?: Selection) {
let content = input;
if (selection) {
content += `
\`\`\`
${selection.source}
\`\`\`
`;
}
props.onSend(content);
setInput('');
}
/**
* Triggered when cancelling edition.
*/
function onCancel() {
setInput(props.value || '');
props.onCancel!();
}
// Set the helper text based on whether Shift+Enter is used for sending.
const helperText = sendWithShiftEnter ? (
<span>
Press <b>Shift</b>+<b>Enter</b> to send message
</span>
) : (
<span>
Press <b>Shift</b>+<b>Enter</b> to add a new line
</span>
);
return (
<Box sx={props.sx} className={clsx(INPUT_BOX_CLASS)}>
<Autocomplete
options={commandOptions}
value={props.value}
open={open}
autoHighlight
freeSolo
// ensure the autocomplete popup always renders on top
componentsProps={{
popper: {
placement: 'top'
},
paper: {
sx: {
border: '1px solid lightgray'
}
}
}}
ListboxProps={{
sx: {
'& .MuiAutocomplete-option': {
padding: 2
}
}
}}
renderInput={params => (
<TextField
{...params}
fullWidth
variant="outlined"
multiline
onKeyDown={handleKeyDown}
placeholder="Start chatting"
inputRef={inputRef}
InputProps={{
...params.InputProps,
endAdornment: (
<InputAdornment position="end">
{props.onCancel && <CancelButton onCancel={onCancel} />}
<SendButton
model={model}
sendWithShiftEnter={sendWithShiftEnter}
inputExists={inputExists}
onSend={onSend}
hideIncludeSelection={hideIncludeSelection}
hasButtonOnLeft={!!props.onCancel}
/>
</InputAdornment>
)
}}
FormHelperTextProps={{
sx: { marginLeft: 'auto', marginRight: 0 }
}}
helperText={input.length > 2 ? helperText : ' '}
/>
)}
{...autocompletion.current?.props}
inputValue={input}
onInputChange={(_, newValue: string) => {
setInput(newValue);
if (typingNotification && model.inputChanged) {
model.inputChanged(newValue);
}
}}
onHighlightChange={
/**
* On highlight change: set `highlighted` to whether an option is
* highlighted by the user.
*
* This isn't called when an option is selected for some reason, so we
* need to call `setHighlighted(false)` in `onClose()`.
*/
(_, highlightedOption) => {
setHighlighted(!!highlightedOption);
}
}
onClose={
/**
* On close: set `highlighted` to `false` and close the popup by
* setting `open` to `false`.
*/
() => {
setHighlighted(false);
setOpen(false);
}
}
// hide default extra right padding in the text field
disableClearable
/>
</Box>
);
}
/**
* The chat input namespace.
*/
export namespace ChatInput {
/**
* The properties of the react element.
*/
export interface IProps {
/**
* The chat model.
*/
model: IChatModel;
/**
* The initial value of the input (default to '')
*/
value?: string;
/**
* The function to be called to send the message.
*/
onSend: (input: string) => unknown;
/**
* The function to be called to cancel editing.
*/
onCancel?: () => unknown;
/**
* Whether to allow or not including selection.
*/
hideIncludeSelection?: boolean;
/**
* Custom mui/material styles.
*/
sx?: SxProps<Theme>;
/**
* Autocompletion properties.
*/
autocompletionRegistry?: IAutocompletionRegistry;
/**
* Autocompletion name.
*/
autocompletionName?: string;
}
}