|
157 | 157 | let loadChatUtils = false; |
158 | 158 | let disableSpeech = false; |
159 | 159 | let isLoading = false; |
160 | | -
|
| 160 | + let isCreatingNewConv = false; |
161 | 161 | |
162 | 162 | $: { |
163 | 163 | const editor = lastBotMsg?.rich_content?.editor || ''; |
|
198 | 198 | refresh(); |
199 | 199 | autoScrollLog = false; |
200 | 200 |
|
201 | | - window.addEventListener('message', async e => { |
| 201 | + window.addEventListener('message', e => { |
202 | 202 | if (e.data.action === ChatAction.Logout) { |
203 | | - resetLocalStorage(true); |
204 | | - return; |
| 203 | + handleLogoutAction(); |
| 204 | + } else if (e.data.action === ChatAction.NewChat) { |
| 205 | + handleNewChatAction(e); |
| 206 | + } else if (e.data.action === ChatAction.Chat) { |
| 207 | + handleChatAction(e); |
205 | 208 | } |
| 209 | + }); |
| 210 | + }); |
206 | 211 |
|
207 | | - if (e.data.action === ChatAction.NewChat) { |
208 | | - const conv = await createNewConversation(); |
209 | | - if (!!e.data.text && !isThinking && !isSendingMsg) { |
| 212 | + function handleLogoutAction() { |
| 213 | + resetLocalStorage(true); |
| 214 | + } |
| 215 | +
|
| 216 | + /** @param {any} e */ |
| 217 | + function handleNewChatAction(e) { |
| 218 | + if (!isCreatingNewConv && !isThinking && !isSendingMsg) { |
| 219 | + isCreatingNewConv = true; |
| 220 | + createNewConversation().then(conv => { |
| 221 | + isCreatingNewConv = false; |
| 222 | + if (conv && !!e.data.text) { |
210 | 223 | isLoading = true; |
211 | 224 | sendChatMessage(e.data.text, e.data.data || null, conv.id).then(() => { |
212 | 225 | redirectToNewConversation(conv); |
213 | 226 | isLoading = false; |
214 | 227 | openFrame(); |
| 228 | + }).catch(() => { |
| 229 | + isLoading = false; |
| 230 | + openFrame(); |
215 | 231 | }); |
216 | 232 | } |
217 | | - return; |
218 | | - } |
| 233 | + }).catch(() => { |
| 234 | + isCreatingNewConv = false; |
| 235 | + }); |
| 236 | + } |
| 237 | + } |
219 | 238 |
|
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; |
225 | | - } |
226 | | - }); |
227 | | - }); |
| 239 | + /** @param {any} e */ |
| 240 | + function handleChatAction(e) { |
| 241 | + if (!!e.data.text && !isThinking && !isSendingMsg) { |
| 242 | + sendChatMessage(e.data.text, e.data.data || null).then(() => { |
| 243 | + openFrame(); |
| 244 | + }).catch(() => { |
| 245 | + openFrame(); |
| 246 | + }); |
| 247 | + } |
| 248 | + } |
228 | 249 |
|
229 | 250 | function openFrame() { |
230 | 251 | if (window.location != window.parent.location) { |
231 | | - window.parent.postMessage({ action: "open" }, "*"); |
| 252 | + window.parent.postMessage({ action: ChatAction.Open }, "*"); |
232 | 253 | } |
233 | 254 | } |
234 | 255 |
|
|
489 | 510 | autoScrollLog = true; |
490 | 511 | clearInstantLogs(); |
491 | 512 | renewUserSentMessages(msgText); |
| 513 | + const agentId = params.agentId; |
| 514 | + const convId = conversationId || params.conversationId; |
492 | 515 |
|
493 | 516 | let postback = data?.postback; |
494 | 517 | if (!postback) { |
|
511 | 534 | if (files?.length > 0 && !!!messageData.inputMessageId) { |
512 | 535 | const filePayload = buildFilePayload(files); |
513 | 536 | return new Promise((resolve, reject) => { |
514 | | - uploadConversationFiles(params.agentId, conversationId || params.conversationId, files).then(resMessageId => { |
| 537 | + uploadConversationFiles(agentId, convId, files).then(resMessageId => { |
515 | 538 | messageData = { ...messageData, inputMessageId: resMessageId }; |
516 | 539 | if (!!filePayload) { |
517 | 540 | messageData = { |
|
524 | 547 | }; |
525 | 548 | } |
526 | 549 |
|
527 | | - sendMessageToHub(params.agentId, conversationId || params.conversationId, msgText, messageData).then(res => { |
| 550 | + sendMessageToHub(agentId, convId, msgText, messageData).then(res => { |
528 | 551 | resolve(res); |
529 | 552 | }).catch(err => { |
530 | 553 | reject(err); |
|
537 | 560 | } else { |
538 | 561 | return new Promise((resolve, reject) => { |
539 | 562 | if (!!messageData?.inputMessageId) { |
540 | | - getConversationFiles(params.conversationId, messageData.inputMessageId, FileSourceType.User).then(retFiles => { |
| 563 | + getConversationFiles(convId, messageData.inputMessageId, FileSourceType.User).then(retFiles => { |
541 | 564 | const filePayload = buildFilePayload(retFiles); |
542 | 565 | if (!!filePayload) { |
543 | 566 | messageData = { |
|
550 | 573 | }; |
551 | 574 | } |
552 | 575 |
|
553 | | - sendMessageToHub(params.agentId, conversationId || params.conversationId, msgText, messageData).then(res => { |
| 576 | + sendMessageToHub(agentId, convId, msgText, messageData).then(res => { |
554 | 577 | resolve(res); |
555 | 578 | }).catch(err => { |
556 | 579 | reject(err); |
|
560 | 583 | }); |
561 | 584 | }); |
562 | 585 | } else { |
563 | | - sendMessageToHub(params.agentId, conversationId || params.conversationId, msgText, messageData).then(res => { |
| 586 | + sendMessageToHub(agentId, convId, msgText, messageData).then(res => { |
564 | 587 | resolve(res); |
565 | 588 | }).catch(err => { |
566 | 589 | reject(err); |
|
749 | 772 | } |
750 | 773 | }); |
751 | 774 | } else { |
752 | | - window.parent.postMessage({ action: "close" }, "*"); |
| 775 | + window.parent.postMessage({ action: ChatAction.Close }, "*"); |
753 | 776 | } |
754 | 777 | } |
755 | 778 |
|
|
0 commit comments