|
44 | 44 | import { utcToLocal } from '$lib/helpers/datetime'; |
45 | 45 | import { replaceNewLine } from '$lib/helpers/http'; |
46 | 46 | import { isAudio, isExcel, isPdf } from '$lib/helpers/utils/file'; |
47 | | - import { EditorType, FileSourceType, SenderAction, UserRole } from '$lib/helpers/enums'; |
| 47 | + import { ChatAction, EditorType, FileSourceType, SenderAction, UserRole } from '$lib/helpers/enums'; |
48 | 48 | import RichContent from './rich-content/rich-content.svelte'; |
49 | 49 | import RcMessage from "./rich-content/rc-message.svelte"; |
50 | 50 | import RcDisclaimer from './rich-content/rc-disclaimer.svelte'; |
|
55 | 55 | import ChatBigMessage from './chat-util/chat-big-message.svelte'; |
56 | 56 | import PersistLog from './persist-log/persist-log.svelte'; |
57 | 57 | import InstantLog from './instant-log/instant-log.svelte'; |
| 58 | + import Loader from '$lib/common/Loader.svelte'; |
58 | 59 | |
59 | 60 | |
60 | 61 | const options = { |
|
151 | 152 | let disableAction = false; |
152 | 153 | let loadChatUtils = false; |
153 | 154 | let disableSpeech = false; |
| 155 | + let isLoading = false; |
154 | 156 |
|
155 | 157 | |
156 | 158 | $: { |
|
192 | 194 | refresh(); |
193 | 195 | autoScrollLog = false; |
194 | 196 |
|
195 | | - window.addEventListener('message', e => { |
196 | | - if (e.data.action === 'logout') { |
| 197 | + window.addEventListener('message', async e => { |
| 198 | + if (e.data.action === ChatAction.Logout) { |
197 | 199 | resetLocalStorage(true); |
| 200 | + return; |
198 | 201 | } |
199 | 202 |
|
200 | | - if (e.data.action === 'chat' && !isThinking && !isSendingMsg) { |
201 | | - sendChatMessage(e.data.text, e.data.data || null); |
| 203 | + if (e.data.action === ChatAction.NewChat) { |
| 204 | + const conv = await createNewConversation(); |
| 205 | + if (!!e.data.text && !isThinking && !isSendingMsg) { |
| 206 | + isLoading = true; |
| 207 | + sendChatMessage(e.data.text, e.data.data || null, conv.id).then(() => { |
| 208 | + redirectToNewConversation(conv); |
| 209 | + isLoading = false; |
| 210 | + openFrame(); |
| 211 | + }); |
| 212 | + } |
| 213 | + return; |
| 214 | + } |
| 215 | +
|
| 216 | + if (e.data.action === ChatAction.Chat && !!e.data.text && !isThinking && !isSendingMsg) { |
| 217 | + sendChatMessage(e.data.text, e.data.data || null).then(() => { |
| 218 | + openFrame(); |
| 219 | + }); |
| 220 | + return; |
202 | 221 | } |
203 | 222 | }); |
204 | | - |
205 | | - // window.parent.postMessage({ event: "chat-box-mounted" }, "*"); |
206 | 223 | }); |
207 | 224 |
|
| 225 | + function openFrame() { |
| 226 | + window.parent.postMessage({ action: "open" }, "*"); |
| 227 | + } |
| 228 | +
|
208 | 229 | function resizeChatWindow() { |
209 | 230 | isLite = Viewport.Width <= screenWidthThreshold; |
210 | 231 | if (!isLite) { |
|
219 | 240 | } |
220 | 241 |
|
221 | 242 | function initChatView() { |
222 | | - isFrame = $page.url.searchParams.get('isFrame') === 'true'; |
| 243 | + isFrame = window.location != window.parent.location; |
223 | 244 | mode = $page.url.searchParams.get('mode') || ''; |
224 | 245 | // initial condition |
225 | 246 | isPersistLogClosed = false; |
|
430 | 451 | } |
431 | 452 |
|
432 | 453 | async function handleNewConversation() { |
| 454 | + const conv = await createNewConversation(); |
| 455 | + redirectToNewConversation(conv); |
| 456 | + } |
| 457 | +
|
| 458 | + async function createNewConversation() { |
433 | 459 | const conversation = await newConversation(params.agentId); |
434 | 460 | conversationStore.set(conversation); |
435 | | - const url = `chat/${params.agentId}/${conversation.id}`; |
436 | | - window.location.href = url; |
| 461 | + return conversation; |
| 462 | + } |
| 463 | +
|
| 464 | + /** @param {import('$conversationTypes').ConversationModel} conversation */ |
| 465 | + function redirectToNewConversation(conversation) { |
| 466 | + const url = new URL(`chat/${params.agentId}/${conversation.id}`, window.location.origin); |
| 467 | + const searchParams = $page.url.searchParams; |
| 468 | + url.search = searchParams?.toString(); |
| 469 | + window.location.href = url.toString(); |
437 | 470 | } |
438 | 471 |
|
439 | 472 | function handleSaveKnowledge() { |
|
443 | 476 | /** |
444 | 477 | * @param {string} msgText |
445 | 478 | * @param {import('$conversationTypes').MessageData?} data |
| 479 | + * @param {string?} conversationId |
446 | 480 | */ |
447 | | - function sendChatMessage(msgText, data = null) { |
| 481 | + function sendChatMessage(msgText, data = null, conversationId = null) { |
448 | 482 | isSendingMsg = true; |
449 | 483 | autoScrollLog = true; |
450 | 484 | clearInstantLogs(); |
|
471 | 505 | if (files?.length > 0 && !!!messageData.inputMessageId) { |
472 | 506 | const filePayload = buildFilePayload(files); |
473 | 507 | return new Promise((resolve, reject) => { |
474 | | - uploadConversationFiles(params.agentId, params.conversationId, files).then(resMessageId => { |
| 508 | + uploadConversationFiles(params.agentId, conversationId || params.conversationId, files).then(resMessageId => { |
475 | 509 | messageData = { ...messageData, inputMessageId: resMessageId }; |
476 | 510 | if (!!filePayload) { |
477 | 511 | messageData = { |
|
484 | 518 | }; |
485 | 519 | } |
486 | 520 |
|
487 | | - sendMessageToHub(params.agentId, params.conversationId, msgText, messageData).then(res => { |
| 521 | + sendMessageToHub(params.agentId, conversationId || params.conversationId, msgText, messageData).then(res => { |
488 | 522 | resolve(res); |
489 | 523 | }).catch(err => { |
490 | 524 | reject(err); |
|
510 | 544 | }; |
511 | 545 | } |
512 | 546 |
|
513 | | - sendMessageToHub(params.agentId, params.conversationId, msgText, messageData).then(res => { |
| 547 | + sendMessageToHub(params.agentId, conversationId || params.conversationId, msgText, messageData).then(res => { |
514 | 548 | resolve(res); |
515 | 549 | }).catch(err => { |
516 | 550 | reject(err); |
|
520 | 554 | }); |
521 | 555 | }); |
522 | 556 | } else { |
523 | | - sendMessageToHub(params.agentId, params.conversationId, msgText, messageData).then(res => { |
| 557 | + sendMessageToHub(params.agentId, conversationId || params.conversationId, msgText, messageData).then(res => { |
524 | 558 | resolve(res); |
525 | 559 | }).catch(err => { |
526 | 560 | reject(err); |
|
1003 | 1037 | bigText = ''; |
1004 | 1038 | sendChatMessage(text); |
1005 | 1039 | } |
| 1040 | +
|
| 1041 | + /** |
| 1042 | + * @param {any} e |
| 1043 | + * @param {any} message |
| 1044 | + */ |
| 1045 | + function likeMessage(e, message) { |
| 1046 | + e.preventDefault(); |
| 1047 | +
|
| 1048 | + const text = 'I like this message.'; |
| 1049 | + const data = { |
| 1050 | + postback: { |
| 1051 | + functionName: 'like_response', |
| 1052 | + payload: 'I really like this message!', |
| 1053 | + parentId: message?.id |
| 1054 | + }, |
| 1055 | + states: [] |
| 1056 | + }; |
| 1057 | + sendChatMessage(text, data); |
| 1058 | + } |
1006 | 1059 | </script> |
1007 | 1060 |
|
1008 | 1061 |
|
1009 | 1062 | <svelte:window on:resize={() => resizeChatWindow()}/> |
1010 | 1063 |
|
| 1064 | +{#if isLoading} |
| 1065 | + <Loader size={35} /> |
| 1066 | +{/if} |
| 1067 | +
|
1011 | 1068 | <DialogModal |
1012 | 1069 | title={'Edit message'} |
1013 | 1070 | size={'md'} |
|
1217 | 1274 | <div class="msg-container"> |
1218 | 1275 | <RcMessage message={message} /> |
1219 | 1276 | {#if message?.message_id === lastBotMsg?.message_id} |
1220 | | - <AudioSpeaker |
1221 | | - id={message?.message_id} |
1222 | | - text={message?.rich_content?.message?.text || message?.text} |
1223 | | - /> |
| 1277 | + <div style="display: flex; gap: 10px;"> |
| 1278 | + <AudioSpeaker |
| 1279 | + id={message?.message_id} |
| 1280 | + text={message?.rich_content?.message?.text || message?.text} |
| 1281 | + /> |
| 1282 | + <div class="line-align-center" style="font-size: 17px;"> |
| 1283 | + <!-- svelte-ignore a11y-click-events-have-key-events --> |
| 1284 | + <!-- svelte-ignore a11y-no-static-element-interactions --> |
| 1285 | + <div |
| 1286 | + class="clickable" |
| 1287 | + style="height: 95%;" |
| 1288 | + on:click={e => likeMessage(e, message)} |
| 1289 | + > |
| 1290 | + <i |
| 1291 | + class="mdi mdi-thumb-up-outline text-primary" |
| 1292 | + /> |
| 1293 | + </div> |
| 1294 | + </div> |
| 1295 | + </div> |
1224 | 1296 | {/if} |
1225 | 1297 | {#if !!message.is_chat_message || !!message.has_message_files} |
1226 | 1298 | <MessageFileGallery |
|
0 commit comments