-
-
Notifications
You must be signed in to change notification settings - Fork 362
/
Copy pathslash-autocompletion.tsx
93 lines (88 loc) · 2.37 KB
/
slash-autocompletion.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
import {
AutocompleteCommand,
IAutocompletionCommandsProps
} from '@jupyter/chat';
import {
Download,
FindInPage,
Help,
MoreHoriz,
MenuBook,
School,
HideSource,
AutoFixNormal
} from '@mui/icons-material';
import { Box, Typography } from '@mui/material';
import React from 'react';
import { AiService } from './handler';
type SlashCommandOption = AutocompleteCommand & {
id: string;
description: string;
};
/**
* List of icons per slash command, shown in the autocomplete popup.
*
* This list of icons should eventually be made configurable. However, it is
* unclear whether custom icons should be defined within a Lumino plugin (in the
* frontend) or served from a static server route (in the backend).
*/
const DEFAULT_SLASH_COMMAND_ICONS: Record<string, JSX.Element> = {
ask: <FindInPage />,
clear: <HideSource />,
export: <Download />,
fix: <AutoFixNormal />,
generate: <MenuBook />,
help: <Help />,
learn: <School />,
unknown: <MoreHoriz />
};
/**
* Renders an option shown in the slash command autocomplete.
*/
function renderSlashCommandOption(
optionProps: React.HTMLAttributes<HTMLLIElement>,
option: SlashCommandOption
): JSX.Element {
const icon =
option.id in DEFAULT_SLASH_COMMAND_ICONS
? DEFAULT_SLASH_COMMAND_ICONS[option.id]
: DEFAULT_SLASH_COMMAND_ICONS.unknown;
return (
<li {...optionProps}>
<Box sx={{ lineHeight: 0, marginRight: 4, opacity: 0.618 }}>{icon}</Box>
<Box sx={{ flexGrow: 1 }}>
<Typography
component="span"
sx={{
fontSize: 'var(--jp-ui-font-size1)'
}}
>
{option.label}
</Typography>
<Typography
component="span"
sx={{ opacity: 0.618, fontSize: 'var(--jp-ui-font-size0)' }}
>
{' — ' + option.description}
</Typography>
</Box>
</li>
);
}
/**
* The autocompletion command properties to add to the registry.
*/
export const autocompletion: IAutocompletionCommandsProps = {
opener: '/',
commands: async () => {
const slashCommands = (await AiService.listSlashCommands()).slash_commands;
return slashCommands.map<SlashCommandOption>(slashCommand => ({
id: slashCommand.slash_id,
label: '/' + slashCommand.slash_id + ' ',
description: slashCommand.description
}));
},
props: {
renderOption: renderSlashCommandOption
}
};