-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathindex.tsx
200 lines (183 loc) · 5.5 KB
/
index.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
import { MutableRefObject, useCallback, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useRecoilState, useSetRecoilState } from 'recoil';
import { v4 as uuidv4 } from 'uuid';
import {
FileSpec,
ICommand,
IStep,
useAuth,
useChatData,
useChatInteract
} from '@chainlit/react-client';
import { Settings } from '@/components/icons/Settings';
import { Button } from '@/components/ui/button';
import { chatSettingsOpenState } from '@/state/project';
import { IAttachment, attachmentsState } from 'state/chat';
import { Attachments } from './Attachments';
import CommandButtons from './CommandButtons';
import CommandButton from './CommandPopoverButton';
import Input, { InputMethods } from './Input';
import SubmitButton from './SubmitButton';
import UploadButton from './UploadButton';
import VoiceButton from './VoiceButton';
interface Props {
fileSpec: FileSpec;
onFileUpload: (payload: File[]) => void;
onFileUploadError: (error: string) => void;
autoScrollRef: MutableRefObject<boolean>;
}
export default function MessageComposer({
fileSpec,
onFileUpload,
onFileUploadError,
autoScrollRef
}: Props) {
const inputRef = useRef<InputMethods>(null);
const [value, setValue] = useState('');
const [selectedCommand, setSelectedCommand] = useState<ICommand>();
const setChatSettingsOpen = useSetRecoilState(chatSettingsOpenState);
const [attachments, setAttachments] = useRecoilState(attachmentsState);
const { t } = useTranslation();
const { user } = useAuth();
const { sendMessage, replyMessage } = useChatInteract();
const { askUser, chatSettingsInputs, disabled: _disabled } = useChatData();
const disabled = _disabled || !!attachments.find((a) => !a.uploaded);
const onPaste = useCallback((event: ClipboardEvent) => {
if (event.clipboardData && event.clipboardData.items) {
const items = Array.from(event.clipboardData.items);
// If no text data, check for files (e.g., images)
items.forEach((item) => {
if (item.kind === 'file') {
const file = item.getAsFile();
if (file) {
onFileUpload([file]);
}
}
});
}
}, []);
const onSubmit = useCallback(
async (
msg: string,
attachments?: IAttachment[],
selectedCommand?: string
) => {
const message: IStep = {
threadId: '',
command: selectedCommand,
id: uuidv4(),
name: user?.identifier || 'User',
type: 'user_message',
output: msg,
createdAt: new Date().toISOString(),
metadata: { location: window.location.href }
};
const fileReferences = attachments
?.filter((a) => !!a.serverId)
.map((a) => ({ id: a.serverId! }));
if (autoScrollRef) {
autoScrollRef.current = true;
}
sendMessage(message, fileReferences);
},
[user, sendMessage]
);
const onReply = useCallback(
async (msg: string) => {
const message: IStep = {
threadId: '',
id: uuidv4(),
name: user?.identifier || 'User',
type: 'user_message',
output: msg,
createdAt: new Date().toISOString(),
metadata: { location: window.location.href }
};
replyMessage(message);
if (autoScrollRef) {
autoScrollRef.current = true;
}
},
[user, replyMessage]
);
const submit = useCallback(() => {
if (disabled || (value === '' && attachments.length === 0)) {
return;
}
if (askUser) {
onReply(value);
} else {
onSubmit(value, attachments, selectedCommand?.id);
}
setAttachments([]);
inputRef.current?.reset();
}, [
value,
disabled,
setValue,
askUser,
attachments,
selectedCommand,
setAttachments,
onSubmit
]);
return (
<div className="bg-accent dark:bg-card rounded-3xl p-3 px-4 w-full min-h-24 flex flex-col">
{attachments.length > 0 ? (
<div className="mb-1">
<Attachments />
</div>
) : null}
<Input
ref={inputRef}
id="chat-input"
autoFocus
selectedCommand={selectedCommand}
setSelectedCommand={setSelectedCommand}
onChange={setValue}
onPaste={onPaste}
onEnter={submit}
placeholder={t('chat.input.placeholder')}
/>
<div className="flex items-center justify-between">
<div className="flex items-center -ml-1.5">
<UploadButton
disabled={disabled}
fileSpec={fileSpec}
onFileUploadError={onFileUploadError}
onFileUpload={onFileUpload}
/>
<CommandButton
disabled={disabled}
onCommandSelect={setSelectedCommand}
/>
{chatSettingsInputs.length > 0 && (
<Button
id="chat-settings-open-modal"
disabled={disabled}
onClick={() => setChatSettingsOpen(true)}
className="hover:bg-muted"
variant="ghost"
size="icon"
>
<Settings className="!size-6" />
</Button>
)}
<VoiceButton disabled={disabled} />
<CommandButtons
disabled={disabled}
selectedCommandId={selectedCommand?.id}
onCommandSelect={setSelectedCommand}
/>
</div>
<div className="flex items-center gap-1">
<SubmitButton
onSubmit={submit}
disabled={disabled || !value.trim()}
/>
</div>
</div>
</div>
);
}