Skip to content

Commit 5ee7678

Browse files
committed
prevent duplicate chat
1 parent cf04f53 commit 5ee7678

File tree

5 files changed

+47
-37
lines changed

5 files changed

+47
-37
lines changed

src/lib/common/LiveChatEntry.svelte

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import { getSettingDetail } from '$lib/services/setting-service';
66
import { chatBotStore } from '$lib/helpers/store';
77
import { CHAT_FRAME_ID } from '$lib/helpers/constants';
8+
import { ChatAction } from '$lib/helpers/enums';
89
910
let chatUrl = PUBLIC_LIVECHAT_HOST;
1011
@@ -15,9 +16,9 @@
1516
1617
// Handle event from iframe
1718
window.onmessage = async function(e) {
18-
if (e.data.action == 'close') {
19+
if (e.data.action == ChatAction.Close) {
1920
chatBotStore.set({ showChatBox: false });
20-
} else if (e.data.action == 'open') {
21+
} else if (e.data.action == ChatAction.Open) {
2122
chatBotStore.set({ showChatBox: true });
2223
}
2324
};

src/lib/helpers/enums.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ const knowledgeDocSource = {
101101
export const KnowledgeDocSource = Object.freeze(knowledgeDocSource);
102102

103103
const chatAction = {
104+
Open: 'open',
105+
Close: 'close',
104106
Logout: 'logout',
105107
Chat: 'chat',
106108
NewChat: 'new-chat'

src/lib/helpers/utils/chat.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,4 @@ export function addChatBoxMountEventListener(func) {
2323
func();
2424
}
2525
});
26-
}
27-
28-
/**
29-
* @param {string} chatFrameId
30-
* @param {string} text
31-
*/
32-
export function loadChatFrame(chatFrameId, text) {
33-
const chatFrame = document.getElementById(chatFrameId);
34-
if (chatFrame) {
35-
// @ts-ignore
36-
chatFrame.contentWindow.postMessage({ action: "chat", text: text }, "*");
37-
}
3826
}

src/lib/services/setting-service.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export async function getSettings() {
1515
/**
1616
*
1717
* @param {string} id
18-
* @returns {Promise<object>}
18+
* @returns {Promise<any>}
1919
*/
2020
export async function getSettingDetail(id) {
2121
let url = replaceUrl(endpoints.settingDetailUrl, {id: id});

src/routes/chat/[agentId]/[conversationId]/chat-box.svelte

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@
157157
let loadChatUtils = false;
158158
let disableSpeech = false;
159159
let isLoading = false;
160-
160+
let isCreatingNewConv = false;
161161
162162
$: {
163163
const editor = lastBotMsg?.rich_content?.editor || '';
@@ -200,35 +200,54 @@
200200
201201
window.addEventListener('message', async e => {
202202
if (e.data.action === ChatAction.Logout) {
203-
resetLocalStorage(true);
204-
return;
203+
handleLogoutAction();
204+
} else if (e.data.action === ChatAction.NewChat) {
205+
await handleNewChatAction(e);
206+
} else if (e.data.action === ChatAction.Chat) {
207+
handleChatAction(e);
205208
}
209+
});
210+
});
206211
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-
}
212+
function handleLogoutAction() {
213+
resetLocalStorage(true);
214+
}
219215
220-
if (e.data.action === ChatAction.Chat && !!e.data.text && !isThinking && !isSendingMsg) {
221-
sendChatMessage(e.data.text, e.data.data || null).then(() => {
216+
/** @param {any} e */
217+
async function handleNewChatAction(e) {
218+
if (!isCreatingNewConv && !isThinking && !isSendingMsg) {
219+
isCreatingNewConv = true;
220+
const conv = await createNewConversation();
221+
isCreatingNewConv = false;
222+
223+
if (conv && !!e.data.text) {
224+
isLoading = true;
225+
sendChatMessage(e.data.text, e.data.data || null, conv.id).then(() => {
226+
redirectToNewConversation(conv);
227+
isLoading = false;
228+
openFrame();
229+
}).catch(() => {
230+
isLoading = false;
222231
openFrame();
223232
});
224-
return;
225233
}
226-
});
227-
});
234+
}
235+
}
236+
237+
/** @param {any} e */
238+
function handleChatAction(e) {
239+
if (!!e.data.text && !isThinking && !isSendingMsg) {
240+
sendChatMessage(e.data.text, e.data.data || null).then(() => {
241+
openFrame();
242+
}).catch(() => {
243+
openFrame();
244+
});
245+
}
246+
}
228247
229248
function openFrame() {
230249
if (window.location != window.parent.location) {
231-
window.parent.postMessage({ action: "open" }, "*");
250+
window.parent.postMessage({ action: ChatAction.Open }, "*");
232251
}
233252
}
234253
@@ -749,7 +768,7 @@
749768
}
750769
});
751770
} else {
752-
window.parent.postMessage({ action: "close" }, "*");
771+
window.parent.postMessage({ action: ChatAction.Close }, "*");
753772
}
754773
}
755774

0 commit comments

Comments
 (0)