|
48 | 48 | import { utcToLocal } from '$lib/helpers/datetime'; |
49 | 49 | import { replaceNewLine } from '$lib/helpers/http'; |
50 | 50 | import { isAudio, isExcel, isPdf } from '$lib/helpers/utils/file'; |
51 | | - import { EditorType, FileSourceType, SenderAction, UserRole } from '$lib/helpers/enums'; |
| 51 | + import { ChatAction, EditorType, FileSourceType, SenderAction, UserRole } from '$lib/helpers/enums'; |
52 | 52 | import RichContent from './rich-content/rich-content.svelte'; |
53 | 53 | import RcMessage from "./rich-content/rc-message.svelte"; |
54 | 54 | import RcDisclaimer from './rich-content/rc-disclaimer.svelte'; |
|
59 | 59 | import ChatBigMessage from './chat-util/chat-big-message.svelte'; |
60 | 60 | import PersistLog from './persist-log/persist-log.svelte'; |
61 | 61 | import InstantLog from './instant-log/instant-log.svelte'; |
| 62 | + import Loader from '$lib/common/Loader.svelte'; |
62 | 63 | |
63 | 64 | |
64 | 65 | const options = { |
|
155 | 156 | let disableAction = false; |
156 | 157 | let loadChatUtils = false; |
157 | 158 | let disableSpeech = false; |
| 159 | + let isLoading = false; |
158 | 160 |
|
159 | 161 | |
160 | 162 | $: { |
|
196 | 198 | refresh(); |
197 | 199 | autoScrollLog = false; |
198 | 200 |
|
199 | | - window.addEventListener('message', e => { |
200 | | - if (e.data.action === 'logout') { |
| 201 | + window.addEventListener('message', async e => { |
| 202 | + if (e.data.action === ChatAction.Logout) { |
201 | 203 | resetLocalStorage(true); |
| 204 | + return; |
202 | 205 | } |
203 | 206 |
|
204 | | - if (e.data.action === 'chat' && !isThinking && !isSendingMsg) { |
205 | | - sendChatMessage(e.data.text, e.data.data || null); |
| 207 | + if (e.data.action === ChatAction.NewChat) { |
| 208 | + const conv = await createNewConversation(); |
| 209 | + if (!!e.data.text && !isThinking && !isSendingMsg) { |
| 210 | + isLoading = true; |
| 211 | + sendChatMessage(e.data.text, e.data.data || null, conv.id).then(() => { |
| 212 | + redirectToNewConversation(conv); |
| 213 | + isLoading = false; |
| 214 | + openFrame(); |
| 215 | + }); |
| 216 | + } |
| 217 | + return; |
| 218 | + } |
| 219 | +
|
| 220 | + if (e.data.action === ChatAction.Chat && !!e.data.text && !isThinking && !isSendingMsg) { |
| 221 | + sendChatMessage(e.data.text, e.data.data || null).then(() => { |
| 222 | + openFrame(); |
| 223 | + }); |
| 224 | + return; |
206 | 225 | } |
207 | 226 | }); |
208 | | - |
209 | | - // window.parent.postMessage({ event: "chat-box-mounted" }, "*"); |
210 | 227 | }); |
211 | 228 |
|
| 229 | + function openFrame() { |
| 230 | + window.parent.postMessage({ action: "open" }, "*"); |
| 231 | + } |
| 232 | +
|
212 | 233 | function resizeChatWindow() { |
213 | 234 | isLite = Viewport.Width <= screenWidthThreshold; |
214 | 235 | if (!isLite) { |
|
223 | 244 | } |
224 | 245 |
|
225 | 246 | function initChatView() { |
226 | | - isFrame = $page.url.searchParams.get('isFrame') === 'true'; |
| 247 | + isFrame = window.location != window.parent.location; |
227 | 248 | mode = $page.url.searchParams.get('mode') || ''; |
228 | 249 | // initial condition |
229 | 250 | isPersistLogClosed = false; |
|
434 | 455 | } |
435 | 456 |
|
436 | 457 | async function handleNewConversation() { |
| 458 | + const conv = await createNewConversation(); |
| 459 | + redirectToNewConversation(conv); |
| 460 | + } |
| 461 | +
|
| 462 | + async function createNewConversation() { |
437 | 463 | const conversation = await newConversation(params.agentId); |
438 | 464 | conversationStore.set(conversation); |
439 | | - const url = `chat/${params.agentId}/${conversation.id}`; |
440 | | - window.location.href = url; |
| 465 | + return conversation; |
| 466 | + } |
| 467 | +
|
| 468 | + /** @param {import('$conversationTypes').ConversationModel} conversation */ |
| 469 | + function redirectToNewConversation(conversation) { |
| 470 | + const url = new URL(`chat/${params.agentId}/${conversation.id}`, window.location.origin); |
| 471 | + const searchParams = $page.url.searchParams; |
| 472 | + url.search = searchParams?.toString(); |
| 473 | + window.location.href = url.toString(); |
441 | 474 | } |
442 | 475 |
|
443 | 476 | function handleSaveKnowledge() { |
|
447 | 480 | /** |
448 | 481 | * @param {string} msgText |
449 | 482 | * @param {import('$conversationTypes').MessageData?} data |
| 483 | + * @param {string?} conversationId |
450 | 484 | */ |
451 | | - function sendChatMessage(msgText, data = null) { |
| 485 | + function sendChatMessage(msgText, data = null, conversationId = null) { |
452 | 486 | isSendingMsg = true; |
453 | 487 | autoScrollLog = true; |
454 | 488 | clearInstantLogs(); |
|
475 | 509 | if (files?.length > 0 && !!!messageData.inputMessageId) { |
476 | 510 | const filePayload = buildFilePayload(files); |
477 | 511 | return new Promise((resolve, reject) => { |
478 | | - uploadConversationFiles(params.agentId, params.conversationId, files).then(resMessageId => { |
| 512 | + uploadConversationFiles(params.agentId, conversationId || params.conversationId, files).then(resMessageId => { |
479 | 513 | messageData = { ...messageData, inputMessageId: resMessageId }; |
480 | 514 | if (!!filePayload) { |
481 | 515 | messageData = { |
|
488 | 522 | }; |
489 | 523 | } |
490 | 524 |
|
491 | | - sendMessageToHub(params.agentId, params.conversationId, msgText, messageData).then(res => { |
| 525 | + sendMessageToHub(params.agentId, conversationId || params.conversationId, msgText, messageData).then(res => { |
492 | 526 | resolve(res); |
493 | 527 | }).catch(err => { |
494 | 528 | reject(err); |
|
514 | 548 | }; |
515 | 549 | } |
516 | 550 |
|
517 | | - sendMessageToHub(params.agentId, params.conversationId, msgText, messageData).then(res => { |
| 551 | + sendMessageToHub(params.agentId, conversationId || params.conversationId, msgText, messageData).then(res => { |
518 | 552 | resolve(res); |
519 | 553 | }).catch(err => { |
520 | 554 | reject(err); |
|
524 | 558 | }); |
525 | 559 | }); |
526 | 560 | } else { |
527 | | - sendMessageToHub(params.agentId, params.conversationId, msgText, messageData).then(res => { |
| 561 | + sendMessageToHub(params.agentId, conversationId || params.conversationId, msgText, messageData).then(res => { |
528 | 562 | resolve(res); |
529 | 563 | }).catch(err => { |
530 | 564 | reject(err); |
|
1007 | 1041 | bigText = ''; |
1008 | 1042 | sendChatMessage(text); |
1009 | 1043 | } |
| 1044 | +
|
| 1045 | + /** |
| 1046 | + * @param {any} e |
| 1047 | + * @param {any} message |
| 1048 | + */ |
| 1049 | + function likeMessage(e, message) { |
| 1050 | + e.preventDefault(); |
| 1051 | +
|
| 1052 | + const text = 'I like this message.'; |
| 1053 | + const data = { |
| 1054 | + postback: { |
| 1055 | + functionName: 'like_response', |
| 1056 | + payload: 'I really like this message!', |
| 1057 | + parentId: message?.id |
| 1058 | + }, |
| 1059 | + states: [] |
| 1060 | + }; |
| 1061 | + sendChatMessage(text, data); |
| 1062 | + } |
1010 | 1063 | </script> |
1011 | 1064 |
|
1012 | 1065 |
|
1013 | 1066 | <svelte:window on:resize={() => resizeChatWindow()}/> |
1014 | 1067 |
|
| 1068 | +{#if isLoading} |
| 1069 | + <Loader size={35} /> |
| 1070 | +{/if} |
| 1071 | +
|
1015 | 1072 | <DialogModal |
1016 | 1073 | title={'Edit message'} |
1017 | 1074 | size={'md'} |
|
1221 | 1278 | <div class="msg-container"> |
1222 | 1279 | <RcMessage message={message} /> |
1223 | 1280 | {#if message?.message_id === lastBotMsg?.message_id} |
1224 | | - <AudioSpeaker |
1225 | | - id={message?.message_id} |
1226 | | - text={message?.rich_content?.message?.text || message?.text} |
1227 | | - /> |
| 1281 | + <div style="display: flex; gap: 10px;"> |
| 1282 | + <AudioSpeaker |
| 1283 | + id={message?.message_id} |
| 1284 | + text={message?.rich_content?.message?.text || message?.text} |
| 1285 | + /> |
| 1286 | + <div class="line-align-center" style="font-size: 17px;"> |
| 1287 | + <!-- svelte-ignore a11y-click-events-have-key-events --> |
| 1288 | + <!-- svelte-ignore a11y-no-static-element-interactions --> |
| 1289 | + <div |
| 1290 | + class="clickable" |
| 1291 | + style="height: 95%;" |
| 1292 | + on:click={e => likeMessage(e, message)} |
| 1293 | + > |
| 1294 | + <i |
| 1295 | + class="mdi mdi-thumb-up-outline text-primary" |
| 1296 | + /> |
| 1297 | + </div> |
| 1298 | + </div> |
| 1299 | + </div> |
1228 | 1300 | {/if} |
1229 | 1301 | {#if !!message.is_chat_message || !!message.has_message_files} |
1230 | 1302 | <MessageFileGallery |
|
0 commit comments