yt-chat-components 0.1.0

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.
Files changed (89) hide show
  1. package/.idea/langflow-embedded-chat.iml +12 -0
  2. package/.idea/modules.xml +8 -0
  3. package/.idea/sonarlint/issuestore/0/f/0f8c0c92cf798431ebb931ff6e997b1af86ecee5 +0 -0
  4. package/.idea/sonarlint/issuestore/3/9/39129446b425a1d640160c068e4194e96639eedf +0 -0
  5. package/.idea/sonarlint/issuestore/4/a/4a2f33951ce07c1ff7184f91877aa13db05d3785 +0 -0
  6. package/.idea/sonarlint/issuestore/4/a/4a7b99bdbee5792679d347b6474463bf5e14b66d +0 -0
  7. package/.idea/sonarlint/issuestore/4/b/4b6989b8ccae808ebc45d02230d336ea53800365 +0 -0
  8. package/.idea/sonarlint/issuestore/6/c/6c024c1d0ad64656b9d4b0695ec3c49c0454addf +0 -0
  9. package/.idea/sonarlint/issuestore/8/d/8d6123af13a140f93e06299fff7ea23c547e9ec8 +0 -0
  10. package/.idea/sonarlint/issuestore/8/e/8ec9a00bfd09b3190ac6b22251dbb1aa95a0579d +0 -0
  11. package/.idea/sonarlint/issuestore/d/9/d938938695d447dadda115e28781c6541f53fc4f +0 -0
  12. package/.idea/sonarlint/issuestore/index.pb +19 -0
  13. package/.idea/vcs.xml +6 -0
  14. package/README.md +274 -0
  15. package/build/asset-manifest.json +16 -0
  16. package/build/index.html +1 -0
  17. package/build/static/css/main.6f7c593d.css +2 -0
  18. package/build/static/css/main.6f7c593d.css.map +1 -0
  19. package/build/static/js/bundle.min.js +2 -0
  20. package/build/static/js/bundle.min.js.LICENSE.txt +124 -0
  21. package/build/static/js/main.cb252095.js +3 -0
  22. package/build/static/js/main.cb252095.js.LICENSE.txt +134 -0
  23. package/build/static/js/main.cb252095.js.map +1 -0
  24. package/build/static/media/aiavatar.74bafa995cce4c01b804.png +0 -0
  25. package/build/static/media/history-list-empty.1eb65b1550aef4e8c8a4.png +0 -0
  26. package/build/static/media/moreBg.9fc998472925cecd89f2.png +0 -0
  27. package/package.json +75 -0
  28. package/public/index.html +47 -0
  29. package/src/YtChatView/chatWidget/chatWindow/chatMessage/index.module.css +86 -0
  30. package/src/YtChatView/chatWidget/chatWindow/chatMessage/index.tsx +211 -0
  31. package/src/YtChatView/chatWidget/chatWindow/chatPlaceholder/index.module.css +9 -0
  32. package/src/YtChatView/chatWidget/chatWindow/chatPlaceholder/index.tsx +23 -0
  33. package/src/YtChatView/chatWidget/chatWindow/controllers/index.ts +236 -0
  34. package/src/YtChatView/chatWidget/chatWindow/index.module.css +197 -0
  35. package/src/YtChatView/chatWidget/chatWindow/index.tsx +791 -0
  36. package/src/YtChatView/chatWidget/chatWindow/types/chatWidget/index.ts +37 -0
  37. package/src/YtChatView/chatWidget/chatWindow/utils.ts +75 -0
  38. package/src/YtChatView/chatWidget/index.tsx +2289 -0
  39. package/src/YtChatView/logoBtn/index.css +4 -0
  40. package/src/YtChatView/logoBtn/index.jsx +65 -0
  41. package/src/YtChatView/logoSplitBtn/index.css +4 -0
  42. package/src/YtChatView/logoSplitBtn/index.jsx +67 -0
  43. package/src/YtChatView/previewDialog/index.jsx +431 -0
  44. package/src/YtChatView/previewDialog/index.module.css +144 -0
  45. package/src/assets/aicenter/add.png +0 -0
  46. package/src/assets/aicenter/aiavatar.png +0 -0
  47. package/src/assets/aicenter/aicenterbg.png +0 -0
  48. package/src/assets/aicenter/aicenterbgdark.png +0 -0
  49. package/src/assets/aicenter/close.png +0 -0
  50. package/src/assets/aicenter/closex.png +0 -0
  51. package/src/assets/aicenter/copy.png +0 -0
  52. package/src/assets/aicenter/file.png +0 -0
  53. package/src/assets/aicenter/fileupload.png +0 -0
  54. package/src/assets/aicenter/history-list-empty.png +0 -0
  55. package/src/assets/aicenter/history.png +0 -0
  56. package/src/assets/aicenter/luyin.png +0 -0
  57. package/src/assets/aicenter/moreAi.png +0 -0
  58. package/src/assets/aicenter/moreBg.png +0 -0
  59. package/src/assets/aicenter/play-run.gif +0 -0
  60. package/src/assets/aicenter/play.png +0 -0
  61. package/src/assets/aicenter/send-img.png +0 -0
  62. package/src/assets/aicenter/send-question-black.png +0 -0
  63. package/src/assets/aicenter/send-question.png +0 -0
  64. package/src/assets/aicenter/sendmessage.png +0 -0
  65. package/src/assets/aicenter/sound-wave.gif +0 -0
  66. package/src/assets/aicenter/toLeft.png +0 -0
  67. package/src/assets/aicenter/toRight.png +0 -0
  68. package/src/assets/aicenter/type-excel.png +0 -0
  69. package/src/assets/aicenter/type-markdown.png +0 -0
  70. package/src/assets/aicenter/type-mobi.png +0 -0
  71. package/src/assets/aicenter/type-pdf.png +0 -0
  72. package/src/assets/aicenter/type-rpub.png +0 -0
  73. package/src/assets/aicenter/type-text.png +0 -0
  74. package/src/assets/aicenter/type-word.png +0 -0
  75. package/src/assets/aicenter/upfile.png +0 -0
  76. package/src/chatPlaceholder/index.tsx +18 -0
  77. package/src/chatWidget/chatTrigger/index.tsx +15 -0
  78. package/src/chatWidget/chatWindow/chatMessage/index.tsx +42 -0
  79. package/src/chatWidget/chatWindow/index.tsx +426 -0
  80. package/src/chatWidget/index.tsx +2195 -0
  81. package/src/chatWidget/utils.ts +76 -0
  82. package/src/controllers/index.ts +205 -0
  83. package/src/index.tsx +60 -0
  84. package/src/react-app-env.d.ts +1 -0
  85. package/src/reportWebVitals.ts +15 -0
  86. package/src/setupTests.ts +5 -0
  87. package/src/types/chatWidget/index.ts +13 -0
  88. package/tsconfig.json +26 -0
  89. package/webpack.config.js +51 -0
@@ -0,0 +1,426 @@
1
+ // @ts-nocheck
2
+ import { Send } from "lucide-react";
3
+ import { extractMessageFromOutput, getAnimationOrigin, getChatPosition } from "../utils";
4
+ import React, { useEffect, useRef, useState } from "react";
5
+ import { ChatMessageType } from "../../types/chatWidget";
6
+ import ChatMessage from "./chatMessage";
7
+ import {sendMessage, sendMessageStream} from "../../controllers";
8
+ import ChatMessagePlaceholder from "../../chatPlaceholder";
9
+
10
+ export default function ChatWindow({
11
+ api_key,
12
+ flowId,
13
+ hostUrl,
14
+ updateLastMessage,
15
+ messages,
16
+ output_type,
17
+ input_type,
18
+ output_component,
19
+ bot_message_style,
20
+ send_icon_style,
21
+ user_message_style,
22
+ chat_window_style,
23
+ error_message_style,
24
+ placeholder_sending,
25
+ send_button_style,
26
+ online = true,
27
+ open,
28
+ online_message = "We'll reply as soon as we can",
29
+ offline_message = "We're offline now",
30
+ window_title = "Chat",
31
+ placeholder,
32
+ input_style,
33
+ input_container_style,
34
+ addMessage,
35
+ position,
36
+ triggerRef,
37
+ width = 450,
38
+ height = 650,
39
+ tweaks,
40
+ sessionId,
41
+ additional_headers
42
+ }: {
43
+ api_key?: string;
44
+ output_type: string,
45
+ input_type: string,
46
+ output_component?: string,
47
+ bot_message_style?: React.CSSProperties;
48
+ send_icon_style?: React.CSSProperties;
49
+ user_message_style?: React.CSSProperties;
50
+ chat_window_style?: React.CSSProperties;
51
+ error_message_style?: React.CSSProperties;
52
+ send_button_style?: React.CSSProperties;
53
+ online?: boolean;
54
+ open: boolean;
55
+ online_message?: string;
56
+ placeholder_sending?: string;
57
+ offline_message?: string;
58
+ window_title?: string;
59
+ placeholder?: string;
60
+ input_style?: React.CSSProperties;
61
+ input_container_style?: React.CSSProperties;
62
+ tweaks?: { [key: string]: any };
63
+ flowId: string;
64
+ hostUrl: string;
65
+ updateLastMessage: Function;
66
+ messages: ChatMessageType[];
67
+ addMessage: Function;
68
+ position?: string;
69
+ triggerRef: React.RefObject<HTMLButtonElement>;
70
+ width?: number;
71
+ height?: number;
72
+ sessionId: React.MutableRefObject<string>;
73
+ additional_headers?: { [key: string]: string };
74
+
75
+ }) {
76
+ const [value, setValue] = useState<string>("");
77
+ const ref = useRef<HTMLDivElement>(null);
78
+ const lastMessage = useRef<HTMLDivElement>(null);
79
+ const [windowPosition, setWindowPosition] = useState({ left: "0", top: "0" });
80
+ const abortControllerRef = useRef(new AbortController());
81
+ const inputRef = useRef<HTMLInputElement>(null); /* User input Ref */
82
+ useEffect(() => {
83
+ if (triggerRef)
84
+ setWindowPosition(
85
+ getChatPosition(
86
+ triggerRef.current!.getBoundingClientRect(),
87
+ width,
88
+ height,
89
+ position
90
+ )
91
+ );
92
+ }, [triggerRef, width, height, position]);
93
+
94
+ const isStreamOutput = true;
95
+
96
+ /* Initial listener for loss of focus that refocuses User input after a small delay */
97
+
98
+ const [sendingMessage, setSendingMessage] = useState(false);
99
+
100
+ // 流式输出消息,实时显示(token为流式输出内容,end为结束输出,整体输出一次)
101
+ const handleMessageContent = (event, data) => {
102
+ if (event == 'add_message' && data['sender'] == 'Machine') {
103
+ getHistoryList();
104
+ } else if (event == 'token') {
105
+ setNowAIContent((prevState) => prevState + data['chunk']);
106
+ if (lastMessage.current) lastMessage.current.scrollIntoView({ behavior: 'smooth' });
107
+ } else if (event == 'end') {
108
+ const res = {
109
+ data: data['result'],
110
+ };
111
+ if (
112
+ res.data &&
113
+ res.data.outputs &&
114
+ Object.keys(res.data.outputs).length > 0 &&
115
+ res.data.outputs[0].outputs &&
116
+ res.data.outputs[0].outputs.length > 0
117
+ ) {
118
+ const flowOutputs: Array<any> = res.data.outputs[0].outputs;
119
+ if (output_component && flowOutputs.map((e) => e.component_id).includes(output_component)) {
120
+ Object.values(
121
+ flowOutputs.find((e) => e.component_id === output_component).outputs,
122
+ ).forEach((output: any) => {
123
+ addMessage({
124
+ message: extractMessageFromOutput(output),
125
+ isSend: false,
126
+ rawInfo: output.message,
127
+ });
128
+ });
129
+ } else if (flowOutputs.length === 1) {
130
+ Object.values(flowOutputs[0].outputs).forEach((output: any) => {
131
+ addMessage({
132
+ message: extractMessageFromOutput(output),
133
+ isSend: false,
134
+ rawInfo: output.message,
135
+ });
136
+ });
137
+ } else {
138
+ flowOutputs
139
+ .sort((a, b) => {
140
+ // Get the earliest timestamp from each flowOutput's outputs
141
+ const aTimestamp = Math.min(
142
+ ...Object.values(a.outputs).map((output: any) =>
143
+ Date.parse(output.message?.timestamp),
144
+ ),
145
+ );
146
+ const bTimestamp = Math.min(
147
+ ...Object.values(b.outputs).map((output: any) =>
148
+ Date.parse(output.message?.timestamp),
149
+ ),
150
+ );
151
+ return aTimestamp - bTimestamp; // Sort descending (newest first)
152
+ })
153
+ .forEach((flowOutput) => {
154
+ Object.values(flowOutput.outputs).forEach((output: any) => {
155
+ addMessage({
156
+ message: extractMessageFromOutput(output),
157
+ isSend: false,
158
+ rawInfo: output.message,
159
+ });
160
+ });
161
+ });
162
+ }
163
+ }
164
+ if (res.data && res.data.session_id) {
165
+ // sessionId = res.data.session_id;
166
+ }
167
+ setSendingMessage(false);
168
+ }
169
+ };
170
+
171
+ const sendMessageWrapper =()=>{
172
+ sendMessage(hostUrl, flowId, value, input_type, output_type, sessionId, output_component, tweaks, api_key, additional_headers)
173
+ .then((res) => {
174
+ if (
175
+ res.data &&
176
+ res.data.outputs &&
177
+ Object.keys(res.data.outputs).length > 0 &&
178
+ res.data.outputs[0].outputs && res.data.outputs[0].outputs.length > 0
179
+ ) {
180
+ const flowOutputs: Array<any> = res.data.outputs[0].outputs;
181
+ if (output_component &&
182
+ flowOutputs.map(e => e.component_id).includes(output_component)) {
183
+ Object.values(flowOutputs.find(e => e.component_id === output_component).outputs).forEach((output: any) => {
184
+ addMessage({
185
+ message: extractMessageFromOutput(output),
186
+ isSend: false,
187
+ });
188
+ })
189
+ } else if (
190
+ flowOutputs.length === 1
191
+ ) {
192
+ Object.values(flowOutputs[0].outputs).forEach((output: any) => {
193
+ addMessage({
194
+ message: extractMessageFromOutput(output),
195
+ isSend: false,
196
+ });
197
+ })
198
+ } else {
199
+ flowOutputs
200
+ .sort((a, b) => {
201
+ // Get the earliest timestamp from each flowOutput's outputs
202
+ const aTimestamp = Math.min(...Object.values(a.outputs).map((output: any) => Date.parse(output.message?.timestamp)));
203
+ const bTimestamp = Math.min(...Object.values(b.outputs).map((output: any) => Date.parse(output.message?.timestamp)));
204
+ return aTimestamp - bTimestamp; // Sort descending (newest first)
205
+ })
206
+ .forEach((flowOutput) => {
207
+ Object.values(flowOutput.outputs).forEach((output: any) => {
208
+ addMessage({
209
+ message: extractMessageFromOutput(output),
210
+ isSend: false,
211
+ });
212
+ });
213
+ });
214
+ }
215
+ }
216
+ if (res.data && res.data.session_id) {
217
+ sessionId.current = res.data.session_id;
218
+ }
219
+ setSendingMessage(false);
220
+ })
221
+ .catch((err) => {
222
+ const response = err.response;
223
+ if (err.code === "ERR_NETWORK") {
224
+ updateLastMessage({
225
+ message: "Network error",
226
+ isSend: false,
227
+ error: true,
228
+ });
229
+ } else if (
230
+ response &&
231
+ response.status === 500 &&
232
+ response.data &&
233
+ response.data.detail
234
+ ) {
235
+ updateLastMessage({
236
+ message: response.data.detail,
237
+ isSend: false,
238
+ error: true,
239
+ });
240
+ }
241
+ console.error(err);
242
+ setSendingMessage(false);
243
+ });
244
+ }
245
+
246
+ const sendMessageStreamWrapper = ()=>{
247
+ let embedAppExtend: embedAppExtend = {
248
+ operator_id: "2204411202",
249
+ upload_file_path_list: [],
250
+ http_extend: {
251
+ bd:{
252
+ code: "2204411202"
253
+ },
254
+ body: {
255
+ code: "2204411202"
256
+ }
257
+ }
258
+ };
259
+ sendMessageStream(
260
+ embedAppExtend,
261
+ false,
262
+ handleMessageContent,
263
+ abortControllerRef.current.signal,
264
+ hostUrl,
265
+ flowId,
266
+ value,
267
+ input_type,
268
+ output_type,
269
+ sessionId.current,
270
+ output_component,
271
+ tweaks,
272
+ api_key,
273
+ additional_headers,
274
+ )
275
+ .then((res) => {
276
+ setSendingMessage(false);
277
+ })
278
+ .catch((e) => {
279
+ console.log('e :>> ', e);
280
+ if (e.name !== 'AbortError') {
281
+ addMessage({
282
+ message: '网络错误',
283
+ isSend: false,
284
+ });
285
+ }
286
+ setSendingMessage(false);
287
+ })
288
+ }
289
+ function handleClick() {
290
+ if (value && value.trim() !== "") {
291
+ addMessage({ message: value, isSend: true });
292
+ setSendingMessage(true);
293
+ setValue("");
294
+ isStreamOutput ? sendMessageStreamWrapper() : sendMessageWrapper();
295
+ }
296
+ }
297
+
298
+ useEffect(() => {
299
+ if (lastMessage.current)
300
+ lastMessage.current.scrollIntoView({ behavior: "smooth" });
301
+ }, [messages]);
302
+
303
+ /* Refocus the User input whenever a new response is returned from the LLM */
304
+
305
+ useEffect(() => {
306
+ // after a slight delay
307
+ setTimeout(() => {
308
+ inputRef.current?.focus();
309
+ }, 100);
310
+ }, [messages, open]);
311
+
312
+ // console.log("---->",{
313
+ // api_key,
314
+ // flowId,
315
+ // hostUrl,
316
+ // updateLastMessage,
317
+ // messages,
318
+ // output_type,
319
+ // input_type,
320
+ // output_component,
321
+ // bot_message_style,
322
+ // send_icon_style,
323
+ // user_message_style,
324
+ // chat_window_style,
325
+ // error_message_style,
326
+ // placeholder_sending,
327
+ // send_button_style,
328
+ // online,
329
+ // open,
330
+ // online_message,
331
+ // offline_message,
332
+ // window_title,
333
+ // placeholder,
334
+ // input_style,
335
+ // input_container_style,
336
+ // addMessage,
337
+ // position,
338
+ // triggerRef,
339
+ // width,
340
+ // height,
341
+ // tweaks,
342
+ // sessionId,
343
+ // additional_headers
344
+ // })
345
+
346
+ return (
347
+ <div
348
+ className={
349
+ "cl-chat-window " +
350
+ getAnimationOrigin(position) +
351
+ (open ? " cl-scale-100" : " cl-scale-0")
352
+ }
353
+ style={{ ...windowPosition, zIndex: 9999 }}
354
+ >
355
+ <div
356
+ style={{ ...chat_window_style, width: width, height: height }}
357
+ ref={ref}
358
+ className="cl-window"
359
+ >
360
+ <div className="cl-header">
361
+ {window_title}
362
+ <div className="cl-header-subtitle">
363
+ {online ? (
364
+ <>
365
+ <div className="cl-online-message"></div>
366
+ {online_message}
367
+ </>
368
+ ) : (
369
+ <>
370
+ <div className="cl-offline-message"></div>
371
+ {offline_message}
372
+ </>
373
+ )}
374
+ </div>
375
+ </div>
376
+ <div className="cl-messages_container">
377
+ {messages.map((message, index) => (
378
+ <ChatMessage
379
+ bot_message_style={bot_message_style}
380
+ user_message_style={user_message_style}
381
+ error_message_style={error_message_style}
382
+ key={index}
383
+ message={message.message}
384
+ isSend={message.isSend}
385
+ error={message.error}
386
+ />
387
+ ))}
388
+ {sendingMessage && (
389
+ <ChatMessagePlaceholder bot_message_style={bot_message_style} />
390
+ )}
391
+ <div ref={lastMessage}></div>
392
+ </div>
393
+ <div style={input_container_style} className="cl-input_container">
394
+ <input
395
+ value={value}
396
+ onChange={(e) => setValue(e.target.value)}
397
+ onKeyDown={(e) => {
398
+ if (e.key === "Enter") handleClick();
399
+ }}
400
+ type="text"
401
+ disabled={sendingMessage}
402
+ placeholder={sendingMessage ? (placeholder_sending || "Thinking...") : (placeholder || "Type your message...")}
403
+ style={input_style}
404
+ ref={inputRef}
405
+ className="cl-input-element"
406
+ />
407
+ <button
408
+ style={send_button_style}
409
+ disabled={sendingMessage}
410
+ onClick={handleClick}
411
+ >
412
+ <Send
413
+ style={send_icon_style}
414
+ className={
415
+ "cl-send-icon " +
416
+ (!sendingMessage
417
+ ? "cl-notsending-message"
418
+ : "cl-sending-message")
419
+ }
420
+ />
421
+ </button>
422
+ </div>
423
+ </div>
424
+ </div>
425
+ );
426
+ }