@autobe/ui 0.29.2 → 0.30.0-dev.20260315
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +661 -661
- package/README.md +261 -0
- package/lib/components/AutoBeChatMain.js +5 -5
- package/lib/components/AutoBeChatMain.js.map +1 -1
- package/lib/components/AutoBeConfigModal.js +9 -9
- package/lib/components/AutoBeStatusModal.js +4 -4
- package/lib/components/AutoBeStatusModal.js.map +1 -1
- package/lib/components/AutoBeUserMessageMovie.d.ts +2 -2
- package/lib/components/common/ChatBubble.d.ts +2 -2
- package/lib/components/common/openai/OpenAIContent.d.ts +2 -2
- package/lib/components/common/openai/OpenAIContent.js.map +1 -1
- package/lib/components/common/openai/OpenAIUserAudioContent.js +1 -1
- package/lib/components/common/openai/OpenAIUserAudioContent.js.map +1 -1
- package/lib/components/common/openai/OpenAIUserFileContent.js +1 -1
- package/lib/components/common/openai/OpenAIUserFileContent.js.map +1 -1
- package/lib/components/common/openai/OpenAIUserImageContent.d.ts +2 -2
- package/lib/components/events/AutoBeCompleteEventMovie.d.ts +2 -2
- package/lib/components/events/AutoBeCompleteEventMovie.js +5 -5
- package/lib/components/events/AutoBeCompleteEventMovie.js.map +1 -1
- package/lib/components/events/AutoBeCorrectEventMovie.d.ts +2 -2
- package/lib/components/events/AutoBeCorrectEventMovie.js +4 -4
- package/lib/components/events/AutoBeCorrectEventMovie.js.map +1 -1
- package/lib/components/events/AutoBeEventMovie.js +38 -17
- package/lib/components/events/AutoBeEventMovie.js.map +1 -1
- package/lib/components/events/AutoBeProgressEventMovie.js +73 -13
- package/lib/components/events/AutoBeProgressEventMovie.js.map +1 -1
- package/lib/components/events/AutoBeScenarioEventMovie.d.ts +2 -2
- package/lib/components/events/AutoBeScenarioEventMovie.js +18 -5
- package/lib/components/events/AutoBeScenarioEventMovie.js.map +1 -1
- package/lib/components/events/AutoBeStartEventMovie.d.ts +2 -2
- package/lib/components/events/AutoBeStartEventMovie.js +2 -2
- package/lib/components/events/AutoBeStartEventMovie.js.map +1 -1
- package/lib/components/events/AutoBeValidateEventMovie.d.ts +2 -2
- package/lib/components/events/AutoBeValidateEventMovie.js +3 -11
- package/lib/components/events/AutoBeValidateEventMovie.js.map +1 -1
- package/lib/components/events/groups/CorrectEventGroup.d.ts +2 -2
- package/lib/components/events/groups/CorrectEventGroup.js +1 -1
- package/lib/components/events/groups/CorrectEventGroup.js.map +1 -1
- package/lib/components/events/groups/ValidateEventGroup.d.ts +2 -2
- package/lib/components/events/groups/ValidateEventGroup.js +1 -2
- package/lib/components/events/groups/ValidateEventGroup.js.map +1 -1
- package/lib/components/events/utils/eventGrouper.js +1 -2
- package/lib/components/events/utils/eventGrouper.js.map +1 -1
- package/lib/components/upload/AutoBeChatUploadBox.d.ts +3 -4
- package/lib/components/upload/AutoBeChatUploadBox.js +2 -1
- package/lib/components/upload/AutoBeChatUploadBox.js.map +1 -1
- package/lib/components/upload/AutoBeChatUploadSendButton.js +1 -1
- package/lib/components/upload/AutoBeChatUploadSendButton.js.map +1 -1
- package/lib/context/AutoBeAgentContext.d.ts +1 -3
- package/lib/context/AutoBeAgentContext.js +0 -4
- package/lib/context/AutoBeAgentContext.js.map +1 -1
- package/lib/hooks/useSessionStorage.d.ts +4 -0
- package/lib/hooks/useSessionStorage.js +16 -0
- package/lib/hooks/useSessionStorage.js.map +1 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/strategy/AutoBeAgentSessionStorageStrategy.d.ts +10 -0
- package/lib/strategy/AutoBeAgentSessionStorageStrategy.js +117 -0
- package/lib/strategy/AutoBeAgentSessionStorageStrategy.js.map +1 -0
- package/lib/structure/AutoBeListener.js +91 -23
- package/lib/structure/AutoBeListener.js.map +1 -1
- package/lib/structure/AutoBeListenerState.d.ts +3 -3
- package/lib/structure/AutoBeListenerState.js +4 -4
- package/lib/structure/AutoBeListenerState.js.map +1 -1
- package/lib/structure/IAutoBeAgentSessionStorageStrategy.js +1 -1
- package/lib/structure/IAutoBeAgentSessionStorageStrategy.js.map +1 -1
- package/lib/utils/AutoBeFileUploader.d.ts +2 -2
- package/lib/utils/AutoBeFileUploader.js.map +1 -1
- package/package.json +3 -4
- package/src/components/AutoBeAssistantMessageMovie.tsx +22 -22
- package/src/components/AutoBeChatMain.tsx +376 -376
- package/src/components/AutoBeChatSidebar.tsx +414 -414
- package/src/components/AutoBeConfigButton.tsx +83 -83
- package/src/components/AutoBeConfigModal.tsx +443 -443
- package/src/components/AutoBeStatusButton.tsx +75 -75
- package/src/components/AutoBeStatusModal.tsx +486 -484
- package/src/components/AutoBeUserMessageMovie.tsx +27 -27
- package/src/components/common/ActionButton.tsx +205 -205
- package/src/components/common/ActionButtonGroup.tsx +80 -80
- package/src/components/common/AutoBeConfigInput.tsx +185 -185
- package/src/components/common/ChatBubble.tsx +119 -119
- package/src/components/common/Collapsible.tsx +95 -95
- package/src/components/common/CompactSessionIndicator.tsx +73 -73
- package/src/components/common/CompactSessionList.tsx +82 -82
- package/src/components/common/index.ts +8 -8
- package/src/components/common/openai/OpenAIContent.tsx +53 -53
- package/src/components/common/openai/OpenAIUserAudioContent.tsx +70 -70
- package/src/components/common/openai/OpenAIUserFileContent.tsx +76 -76
- package/src/components/common/openai/OpenAIUserImageContent.tsx +34 -34
- package/src/components/common/openai/OpenAIUserTextContent.tsx +15 -15
- package/src/components/common/openai/index.ts +5 -5
- package/src/components/events/AutoBeCompleteEventMovie.tsx +402 -402
- package/src/components/events/AutoBeCorrectEventMovie.tsx +354 -368
- package/src/components/events/AutoBeEventGroupMovie.tsx +18 -18
- package/src/components/events/AutoBeEventMovie.tsx +158 -139
- package/src/components/events/AutoBeProgressEventMovie.tsx +217 -157
- package/src/components/events/AutoBeScenarioEventMovie.tsx +135 -95
- package/src/components/events/AutoBeStartEventMovie.tsx +82 -82
- package/src/components/events/AutoBeValidateEventMovie.tsx +249 -286
- package/src/components/events/README.md +300 -300
- package/src/components/events/common/CollapsibleEventGroup.tsx +211 -211
- package/src/components/events/common/EventCard.tsx +61 -61
- package/src/components/events/common/EventContent.tsx +31 -31
- package/src/components/events/common/EventHeader.tsx +85 -85
- package/src/components/events/common/EventIcon.tsx +82 -82
- package/src/components/events/common/ProgressBar.tsx +64 -64
- package/src/components/events/common/index.ts +13 -13
- package/src/components/events/groups/CorrectEventGroup.tsx +183 -183
- package/src/components/events/groups/ValidateEventGroup.tsx +143 -146
- package/src/components/events/groups/index.ts +8 -8
- package/src/components/events/index.ts +16 -16
- package/src/components/events/utils/eventGrouper.tsx +116 -117
- package/src/components/events/utils/index.ts +1 -1
- package/src/components/index.ts +13 -13
- package/src/components/upload/AutoBeChatUploadBox.tsx +425 -424
- package/src/components/upload/AutoBeChatUploadSendButton.tsx +66 -66
- package/src/components/upload/AutoBeFileUploadBox.tsx +123 -123
- package/src/components/upload/AutoBeUploadConfig.ts +5 -5
- package/src/components/upload/AutoBeVoiceRecoderButton.tsx +100 -100
- package/src/components/upload/index.ts +5 -5
- package/src/constant/color.ts +28 -28
- package/src/context/AutoBeAgentContext.tsx +245 -258
- package/src/context/AutoBeAgentSessionList.tsx +58 -58
- package/src/context/SearchParamsContext.tsx +49 -49
- package/src/hooks/index.ts +3 -3
- package/src/hooks/useEscapeKey.ts +24 -24
- package/src/hooks/useIsomorphicLayoutEffect.ts +8 -8
- package/src/hooks/useMediaQuery.ts +73 -73
- package/src/hooks/useSessionStorage.ts +10 -0
- package/src/icons/Receipt.tsx +74 -74
- package/src/index.ts +9 -8
- package/src/strategy/AutoBeAgentSessionStorageStrategy.ts +127 -0
- package/src/structure/AutoBeListener.ts +373 -304
- package/src/structure/AutoBeListenerState.ts +53 -53
- package/src/structure/IAutoBeAgentSessionStorageStrategy.ts +87 -87
- package/src/structure/IAutoBeEventGroup.ts +6 -6
- package/src/structure/index.ts +4 -4
- package/src/types/config.ts +44 -44
- package/src/types/index.ts +1 -1
- package/src/utils/AutoBeFileUploader.ts +279 -279
- package/src/utils/AutoBeVoiceRecorder.ts +95 -95
- package/src/utils/__tests__/crypto.test.ts +286 -286
- package/src/utils/__tests__/storage.test.ts +229 -229
- package/src/utils/crypto.ts +95 -95
- package/src/utils/index.ts +6 -6
- package/src/utils/number.ts +17 -17
- package/src/utils/storage.ts +96 -96
- package/src/utils/time.ts +14 -14
- package/tsconfig.json +9 -9
- package/vitest.config.ts +15 -15
|
@@ -1,424 +1,425 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
} from "@autobe/interface";
|
|
7
|
-
import { ReactNode, RefObject, useEffect, useRef, useState } from "react";
|
|
8
|
-
|
|
9
|
-
import {
|
|
10
|
-
AutoBeChatUploadSendButton,
|
|
11
|
-
AutoBeFileUploadBox,
|
|
12
|
-
AutoBeVoiceRecoderButton,
|
|
13
|
-
} from ".";
|
|
14
|
-
import { useAutoBeAgent } from "../../context/AutoBeAgentContext";
|
|
15
|
-
import { useMediaQuery } from "../../hooks/useMediaQuery";
|
|
16
|
-
import { AutoBeFileUploader } from "../../utils";
|
|
17
|
-
|
|
18
|
-
export interface IAutoBeBucket {
|
|
19
|
-
file: File;
|
|
20
|
-
content:
|
|
21
|
-
| AutoBeUserMessageAudioContent
|
|
22
|
-
| AutoBeUserMessageFileContent
|
|
23
|
-
|
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export interface IAutoBeChatUploadConfig {
|
|
27
|
-
supportAudio?: boolean;
|
|
28
|
-
file?: (file: File) => Promise<{ id: string }>;
|
|
29
|
-
image?: (file: File) => Promise<{ url: string }>;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export const AutoBeChatUploadBox = (props: AutoBeChatUploadBox.IProps) => {
|
|
33
|
-
const { listener } = useAutoBeAgent();
|
|
34
|
-
const inputRef = useRef<HTMLDivElement>(null);
|
|
35
|
-
const fileInputRef = useRef<HTMLInputElement>(null);
|
|
36
|
-
|
|
37
|
-
const [dragging, setDragging] = useState(false);
|
|
38
|
-
const [enabled, setEnabled] = useState(true);
|
|
39
|
-
const [text, setText] = useState("");
|
|
40
|
-
const [buckets, setBuckets] = useState<IAutoBeBucket[]>([]);
|
|
41
|
-
const [extensionError, setExtensionError] = useState<ReactNode | null>(null);
|
|
42
|
-
|
|
43
|
-
const [emptyText, setEmptyText] = useState(false);
|
|
44
|
-
const [isComposing, setIsComposing] = useState(false);
|
|
45
|
-
|
|
46
|
-
const removeFile = (index: number) => {
|
|
47
|
-
setBuckets(buckets.filter((_, i) => i !== index));
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
useEffect(() => {
|
|
51
|
-
async function trackEnable(value: boolean) {
|
|
52
|
-
setEnabled(value);
|
|
53
|
-
}
|
|
54
|
-
listener?.onEnable(trackEnable);
|
|
55
|
-
return () => {
|
|
56
|
-
listener?.offEnable(trackEnable);
|
|
57
|
-
};
|
|
58
|
-
}, [listener]);
|
|
59
|
-
|
|
60
|
-
// Sync text state when cleared programmatically (like after sending message)
|
|
61
|
-
useEffect(() => {
|
|
62
|
-
if (inputRef.current && text === "" && inputRef.current.innerText !== "") {
|
|
63
|
-
inputRef.current.innerText = "";
|
|
64
|
-
}
|
|
65
|
-
}, [text]);
|
|
66
|
-
|
|
67
|
-
const conversate = async () => {
|
|
68
|
-
if (enabled === false) return;
|
|
69
|
-
|
|
70
|
-
if (text.trim().length === 0 && buckets.length === 0) {
|
|
71
|
-
setEmptyText(true);
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const messages = [
|
|
76
|
-
{
|
|
77
|
-
type: "text",
|
|
78
|
-
text: text.trim(),
|
|
79
|
-
},
|
|
80
|
-
...buckets.map(({ content }) => content),
|
|
81
|
-
]
|
|
82
|
-
|
|
83
|
-
setEmptyText(false);
|
|
84
|
-
setText("");
|
|
85
|
-
setBuckets([]);
|
|
86
|
-
|
|
87
|
-
try {
|
|
88
|
-
await props.conversate(messages);
|
|
89
|
-
} catch (error) {
|
|
90
|
-
props.setError(
|
|
91
|
-
error instanceof Error ? error : new Error("Unknown error"),
|
|
92
|
-
);
|
|
93
|
-
}
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
const handleFileSelect = async (fileList: FileList | null) => {
|
|
97
|
-
if (!fileList) return;
|
|
98
|
-
|
|
99
|
-
setEnabled(false);
|
|
100
|
-
setExtensionError(null);
|
|
101
|
-
|
|
102
|
-
const newFiles: IAutoBeBucket[] = [];
|
|
103
|
-
const errorFileNames: string[] = [];
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
{extensions.
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
e.
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
e.
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
const
|
|
149
|
-
const
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
e.
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
e.
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
props.listener
|
|
172
|
-
props.listener.current.
|
|
173
|
-
props.listener.current.
|
|
174
|
-
props.listener.current.
|
|
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
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
const
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
AutoBeUserConversateContent,
|
|
3
|
+
AutoBeUserImageConversateContent,
|
|
4
|
+
AutoBeUserMessageAudioContent,
|
|
5
|
+
AutoBeUserMessageFileContent,
|
|
6
|
+
} from "@autobe/interface";
|
|
7
|
+
import { ReactNode, RefObject, useEffect, useRef, useState } from "react";
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
AutoBeChatUploadSendButton,
|
|
11
|
+
AutoBeFileUploadBox,
|
|
12
|
+
AutoBeVoiceRecoderButton,
|
|
13
|
+
} from ".";
|
|
14
|
+
import { useAutoBeAgent } from "../../context/AutoBeAgentContext";
|
|
15
|
+
import { useMediaQuery } from "../../hooks/useMediaQuery";
|
|
16
|
+
import { AutoBeFileUploader } from "../../utils";
|
|
17
|
+
|
|
18
|
+
export interface IAutoBeBucket {
|
|
19
|
+
file: File;
|
|
20
|
+
content:
|
|
21
|
+
| AutoBeUserMessageAudioContent
|
|
22
|
+
| AutoBeUserMessageFileContent
|
|
23
|
+
| AutoBeUserImageConversateContent;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface IAutoBeChatUploadConfig {
|
|
27
|
+
supportAudio?: boolean;
|
|
28
|
+
file?: (file: File) => Promise<{ id: string }>;
|
|
29
|
+
image?: (file: File) => Promise<{ url: string }>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export const AutoBeChatUploadBox = (props: AutoBeChatUploadBox.IProps) => {
|
|
33
|
+
const { listener } = useAutoBeAgent();
|
|
34
|
+
const inputRef = useRef<HTMLDivElement>(null);
|
|
35
|
+
const fileInputRef = useRef<HTMLInputElement>(null);
|
|
36
|
+
|
|
37
|
+
const [dragging, setDragging] = useState(false);
|
|
38
|
+
const [enabled, setEnabled] = useState(true);
|
|
39
|
+
const [text, setText] = useState("");
|
|
40
|
+
const [buckets, setBuckets] = useState<IAutoBeBucket[]>([]);
|
|
41
|
+
const [extensionError, setExtensionError] = useState<ReactNode | null>(null);
|
|
42
|
+
|
|
43
|
+
const [emptyText, setEmptyText] = useState(false);
|
|
44
|
+
const [isComposing, setIsComposing] = useState(false);
|
|
45
|
+
|
|
46
|
+
const removeFile = (index: number) => {
|
|
47
|
+
setBuckets(buckets.filter((_, i) => i !== index));
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
useEffect(() => {
|
|
51
|
+
async function trackEnable(value: boolean) {
|
|
52
|
+
setEnabled(value);
|
|
53
|
+
}
|
|
54
|
+
listener?.onEnable(trackEnable);
|
|
55
|
+
return () => {
|
|
56
|
+
listener?.offEnable(trackEnable);
|
|
57
|
+
};
|
|
58
|
+
}, [listener]);
|
|
59
|
+
|
|
60
|
+
// Sync text state when cleared programmatically (like after sending message)
|
|
61
|
+
useEffect(() => {
|
|
62
|
+
if (inputRef.current && text === "" && inputRef.current.innerText !== "") {
|
|
63
|
+
inputRef.current.innerText = "";
|
|
64
|
+
}
|
|
65
|
+
}, [text]);
|
|
66
|
+
|
|
67
|
+
const conversate = async () => {
|
|
68
|
+
if (enabled === false) return;
|
|
69
|
+
|
|
70
|
+
if (text.trim().length === 0 && buckets.length === 0) {
|
|
71
|
+
setEmptyText(true);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const messages: AutoBeUserConversateContent[] = [
|
|
76
|
+
{
|
|
77
|
+
type: "text",
|
|
78
|
+
text: text.trim(),
|
|
79
|
+
},
|
|
80
|
+
...buckets.map(({ content }) => content),
|
|
81
|
+
];
|
|
82
|
+
|
|
83
|
+
setEmptyText(false);
|
|
84
|
+
setText("");
|
|
85
|
+
setBuckets([]);
|
|
86
|
+
|
|
87
|
+
try {
|
|
88
|
+
await props.conversate(messages);
|
|
89
|
+
} catch (error) {
|
|
90
|
+
props.setError(
|
|
91
|
+
error instanceof Error ? error : new Error("Unknown error"),
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const handleFileSelect = async (fileList: FileList | null) => {
|
|
97
|
+
if (!fileList) return;
|
|
98
|
+
|
|
99
|
+
setEnabled(false);
|
|
100
|
+
setExtensionError(null);
|
|
101
|
+
|
|
102
|
+
const newFiles: IAutoBeBucket[] = [];
|
|
103
|
+
const errorFileNames: string[] = [];
|
|
104
|
+
|
|
105
|
+
const files = Array.from(fileList);
|
|
106
|
+
for (const file of files) {
|
|
107
|
+
try {
|
|
108
|
+
newFiles.push(
|
|
109
|
+
await AutoBeFileUploader.compose(props.uploadConfig ?? {}, file),
|
|
110
|
+
);
|
|
111
|
+
} catch (error) {
|
|
112
|
+
errorFileNames.push(file.name);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
if (errorFileNames.length > 0) {
|
|
116
|
+
const extensions: string[] = Array.from(
|
|
117
|
+
new Set(errorFileNames.map((n) => n.split(".").pop() ?? "unknown")),
|
|
118
|
+
).sort();
|
|
119
|
+
setExtensionError(
|
|
120
|
+
<>
|
|
121
|
+
<h2>
|
|
122
|
+
Unsupported extension{extensions.length > 1 ? "s" : ""}: (
|
|
123
|
+
{extensions.join(", ")})
|
|
124
|
+
</h2>
|
|
125
|
+
<ul>
|
|
126
|
+
{errorFileNames.map((name) => (
|
|
127
|
+
<li key={name}>{name}</li>
|
|
128
|
+
))}
|
|
129
|
+
</ul>
|
|
130
|
+
</>,
|
|
131
|
+
);
|
|
132
|
+
setTimeout(() => setExtensionError(null), 5_000);
|
|
133
|
+
}
|
|
134
|
+
setBuckets((o) => [...o, ...newFiles]);
|
|
135
|
+
setEnabled(true);
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
const handleDragEnter = (e: React.DragEvent) => {
|
|
139
|
+
e.preventDefault();
|
|
140
|
+
e.stopPropagation();
|
|
141
|
+
setDragging(true);
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
const handleDragLeave = (e: React.DragEvent) => {
|
|
145
|
+
e.preventDefault();
|
|
146
|
+
e.stopPropagation();
|
|
147
|
+
// Only set dragging to false if we're leaving the entire container
|
|
148
|
+
const rect = e.currentTarget.getBoundingClientRect();
|
|
149
|
+
const x = e.clientX;
|
|
150
|
+
const y = e.clientY;
|
|
151
|
+
if (x < rect.left || x >= rect.right || y < rect.top || y >= rect.bottom) {
|
|
152
|
+
setDragging(false);
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
const handleDragOver = (e: React.DragEvent) => {
|
|
157
|
+
e.preventDefault();
|
|
158
|
+
e.stopPropagation();
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
const handleDrop = async (e: React.DragEvent) => {
|
|
162
|
+
e.preventDefault();
|
|
163
|
+
e.stopPropagation();
|
|
164
|
+
setDragging(false);
|
|
165
|
+
|
|
166
|
+
const files = e.dataTransfer.files;
|
|
167
|
+
await handleFileSelect(files);
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
useEffect(() => {
|
|
171
|
+
if (!props.listener?.current) return;
|
|
172
|
+
props.listener.current.handleDragEnter = handleDragEnter;
|
|
173
|
+
props.listener.current.handleDragLeave = handleDragLeave;
|
|
174
|
+
props.listener.current.handleDrop = handleDrop;
|
|
175
|
+
props.listener.current.handleDragOver = handleDragOver;
|
|
176
|
+
}, [props.listener]);
|
|
177
|
+
|
|
178
|
+
return (
|
|
179
|
+
<div
|
|
180
|
+
style={{
|
|
181
|
+
maxWidth: useMediaQuery.WIDTH_MD,
|
|
182
|
+
margin: "0 auto",
|
|
183
|
+
padding: "12px",
|
|
184
|
+
borderRadius: "16px",
|
|
185
|
+
border: dragging ? "3px solid #1976d2" : "2px solid #e0e0e0",
|
|
186
|
+
backgroundColor: dragging
|
|
187
|
+
? "rgba(25, 118, 210, 0.04)"
|
|
188
|
+
: "rgba(255, 255, 255, 0.95)",
|
|
189
|
+
backdropFilter: "blur(10px)",
|
|
190
|
+
transition: "all 0.2s",
|
|
191
|
+
position: "relative",
|
|
192
|
+
boxShadow: "0px 8px 24px rgba(0, 0, 0, 0.12)",
|
|
193
|
+
}}
|
|
194
|
+
>
|
|
195
|
+
{dragging ? (
|
|
196
|
+
<div
|
|
197
|
+
style={{
|
|
198
|
+
display: "flex",
|
|
199
|
+
alignItems: "center",
|
|
200
|
+
justifyContent: "center",
|
|
201
|
+
minHeight: "120px",
|
|
202
|
+
paddingTop: "32px",
|
|
203
|
+
paddingBottom: "32px",
|
|
204
|
+
}}
|
|
205
|
+
>
|
|
206
|
+
<h6
|
|
207
|
+
style={{
|
|
208
|
+
color: "#1976d2",
|
|
209
|
+
fontWeight: "bold",
|
|
210
|
+
textAlign: "center",
|
|
211
|
+
fontSize: "1.25rem",
|
|
212
|
+
margin: 0,
|
|
213
|
+
}}
|
|
214
|
+
>
|
|
215
|
+
Drop files here to upload
|
|
216
|
+
</h6>
|
|
217
|
+
</div>
|
|
218
|
+
) : null}
|
|
219
|
+
|
|
220
|
+
<div
|
|
221
|
+
style={{
|
|
222
|
+
display: dragging ? "none" : "flex",
|
|
223
|
+
flexDirection: "column",
|
|
224
|
+
gap: "8px",
|
|
225
|
+
}}
|
|
226
|
+
>
|
|
227
|
+
{buckets.length > 0 && (
|
|
228
|
+
<div
|
|
229
|
+
style={{
|
|
230
|
+
display: "flex",
|
|
231
|
+
flexWrap: "wrap",
|
|
232
|
+
gap: "4px",
|
|
233
|
+
}}
|
|
234
|
+
>
|
|
235
|
+
{buckets.map(({ file }, index) => (
|
|
236
|
+
<div
|
|
237
|
+
key={index}
|
|
238
|
+
style={{
|
|
239
|
+
display: "inline-flex",
|
|
240
|
+
alignItems: "center",
|
|
241
|
+
backgroundColor: "#f5f5f5",
|
|
242
|
+
borderRadius: "16px",
|
|
243
|
+
padding: "4px 8px",
|
|
244
|
+
fontSize: "0.875rem",
|
|
245
|
+
maxWidth: "200px",
|
|
246
|
+
border: "1px solid #e0e0e0",
|
|
247
|
+
}}
|
|
248
|
+
>
|
|
249
|
+
<span
|
|
250
|
+
style={{
|
|
251
|
+
overflow: "hidden",
|
|
252
|
+
textOverflow: "ellipsis",
|
|
253
|
+
whiteSpace: "nowrap",
|
|
254
|
+
marginRight: "4px",
|
|
255
|
+
}}
|
|
256
|
+
>
|
|
257
|
+
{file.name}
|
|
258
|
+
</span>
|
|
259
|
+
<button
|
|
260
|
+
style={{
|
|
261
|
+
background: "none",
|
|
262
|
+
border: "none",
|
|
263
|
+
cursor: "pointer",
|
|
264
|
+
padding: "2px",
|
|
265
|
+
display: "flex",
|
|
266
|
+
alignItems: "center",
|
|
267
|
+
justifyContent: "center",
|
|
268
|
+
borderRadius: "50%",
|
|
269
|
+
width: "16px",
|
|
270
|
+
height: "16px",
|
|
271
|
+
}}
|
|
272
|
+
onClick={() => removeFile(index)}
|
|
273
|
+
onMouseEnter={(e) => {
|
|
274
|
+
e.currentTarget.style.backgroundColor = "#f0f0f0";
|
|
275
|
+
}}
|
|
276
|
+
onMouseLeave={(e) => {
|
|
277
|
+
e.currentTarget.style.backgroundColor = "transparent";
|
|
278
|
+
}}
|
|
279
|
+
>
|
|
280
|
+
<svg
|
|
281
|
+
width="12"
|
|
282
|
+
height="12"
|
|
283
|
+
viewBox="0 0 24 24"
|
|
284
|
+
fill="currentColor"
|
|
285
|
+
>
|
|
286
|
+
<path d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z" />
|
|
287
|
+
</svg>
|
|
288
|
+
</button>
|
|
289
|
+
</div>
|
|
290
|
+
))}
|
|
291
|
+
</div>
|
|
292
|
+
)}
|
|
293
|
+
|
|
294
|
+
<div style={{ position: "relative" }}>
|
|
295
|
+
<div
|
|
296
|
+
ref={inputRef}
|
|
297
|
+
contentEditable={true}
|
|
298
|
+
style={{
|
|
299
|
+
width: "97%",
|
|
300
|
+
minHeight: "40px",
|
|
301
|
+
maxHeight: "192px",
|
|
302
|
+
padding: "8px 12px",
|
|
303
|
+
border: "none",
|
|
304
|
+
borderRadius: "8px",
|
|
305
|
+
fontSize: "0.95rem",
|
|
306
|
+
fontFamily: "inherit",
|
|
307
|
+
outline: "none",
|
|
308
|
+
boxShadow: "none",
|
|
309
|
+
backgroundColor: "transparent",
|
|
310
|
+
transition: "color 0.2s, opacity 0.2s",
|
|
311
|
+
overflowY: "auto",
|
|
312
|
+
wordBreak: "break-word",
|
|
313
|
+
whiteSpace: "pre-wrap",
|
|
314
|
+
lineHeight: "1.4",
|
|
315
|
+
// Custom scrollbar styles
|
|
316
|
+
scrollbarWidth: "thin",
|
|
317
|
+
scrollbarColor: "rgba(0, 0, 0, 0.3) transparent",
|
|
318
|
+
}}
|
|
319
|
+
onKeyDown={(e) => {
|
|
320
|
+
if (e.key === "Enter" && !e.shiftKey && !isComposing) {
|
|
321
|
+
e.preventDefault();
|
|
322
|
+
if (enabled) {
|
|
323
|
+
void conversate();
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}}
|
|
327
|
+
onCompositionStart={() => {
|
|
328
|
+
setIsComposing(true);
|
|
329
|
+
}}
|
|
330
|
+
onCompositionEnd={() => {
|
|
331
|
+
setIsComposing(false);
|
|
332
|
+
}}
|
|
333
|
+
onInput={(e) => {
|
|
334
|
+
const target = e.target as HTMLDivElement;
|
|
335
|
+
const newText = target.innerText || "";
|
|
336
|
+
// Only update state if text actually changed to prevent cursor issues
|
|
337
|
+
if (newText !== text) {
|
|
338
|
+
setText(newText);
|
|
339
|
+
}
|
|
340
|
+
}}
|
|
341
|
+
suppressContentEditableWarning={true}
|
|
342
|
+
/>
|
|
343
|
+
{!text && (
|
|
344
|
+
<div
|
|
345
|
+
style={{
|
|
346
|
+
position: "absolute",
|
|
347
|
+
top: "8px",
|
|
348
|
+
left: "12px",
|
|
349
|
+
right: "12px",
|
|
350
|
+
bottom: "8px",
|
|
351
|
+
color: "rgba(153, 153, 153, 0.6)",
|
|
352
|
+
fontSize: "0.95rem",
|
|
353
|
+
fontFamily: "inherit",
|
|
354
|
+
pointerEvents: "none",
|
|
355
|
+
lineHeight: "1.4",
|
|
356
|
+
padding: "2px 0",
|
|
357
|
+
}}
|
|
358
|
+
>
|
|
359
|
+
{emptyText
|
|
360
|
+
? "Cannot send empty message"
|
|
361
|
+
: dragging
|
|
362
|
+
? "Drop files here..."
|
|
363
|
+
: "Conversate with AI Chatbot"}
|
|
364
|
+
</div>
|
|
365
|
+
)}
|
|
366
|
+
</div>
|
|
367
|
+
|
|
368
|
+
<input
|
|
369
|
+
ref={fileInputRef}
|
|
370
|
+
type="file"
|
|
371
|
+
multiple
|
|
372
|
+
accept={AutoBeFileUploader.getAcceptAttribute(
|
|
373
|
+
props.uploadConfig?.supportAudio ?? false,
|
|
374
|
+
!!props.uploadConfig?.file,
|
|
375
|
+
)}
|
|
376
|
+
style={{ display: "none" }}
|
|
377
|
+
onChange={(e) => {
|
|
378
|
+
void handleFileSelect(e.target.files);
|
|
379
|
+
// Reset input to allow selecting the same file again
|
|
380
|
+
if (fileInputRef.current) fileInputRef.current.value = "";
|
|
381
|
+
}}
|
|
382
|
+
/>
|
|
383
|
+
|
|
384
|
+
<div
|
|
385
|
+
style={{
|
|
386
|
+
display: "flex",
|
|
387
|
+
justifyContent: "space-between",
|
|
388
|
+
alignItems: "center",
|
|
389
|
+
}}
|
|
390
|
+
>
|
|
391
|
+
<AutoBeFileUploadBox
|
|
392
|
+
extensionError={extensionError}
|
|
393
|
+
onClick={() => fileInputRef.current?.click()}
|
|
394
|
+
enabled={enabled}
|
|
395
|
+
/>
|
|
396
|
+
{props.uploadConfig?.supportAudio === true ? (
|
|
397
|
+
<AutoBeVoiceRecoderButton
|
|
398
|
+
enabled={enabled}
|
|
399
|
+
onComplete={(content) => setBuckets((o) => [...o, content])}
|
|
400
|
+
/>
|
|
401
|
+
) : null}
|
|
402
|
+
<AutoBeChatUploadSendButton
|
|
403
|
+
onClick={() => conversate()}
|
|
404
|
+
enabled={enabled}
|
|
405
|
+
/>
|
|
406
|
+
</div>
|
|
407
|
+
</div>
|
|
408
|
+
</div>
|
|
409
|
+
);
|
|
410
|
+
};
|
|
411
|
+
|
|
412
|
+
export namespace AutoBeChatUploadBox {
|
|
413
|
+
export interface IProps {
|
|
414
|
+
listener: RefObject<IListener>;
|
|
415
|
+
uploadConfig?: IAutoBeChatUploadConfig;
|
|
416
|
+
conversate: (messages: AutoBeUserConversateContent[]) => Promise<void>;
|
|
417
|
+
setError: (error: Error) => void;
|
|
418
|
+
}
|
|
419
|
+
export interface IListener {
|
|
420
|
+
handleDragEnter: (event: React.DragEvent) => void;
|
|
421
|
+
handleDragLeave: (event: React.DragEvent) => void;
|
|
422
|
+
handleDragOver: (event: React.DragEvent) => void;
|
|
423
|
+
handleDrop: (event: React.DragEvent) => void;
|
|
424
|
+
}
|
|
425
|
+
}
|