Skip to content

Commit

Permalink
Merge pull request #232 from iceljc/features/refine-chat-window
Browse files Browse the repository at this point in the history
prevent duplicate chat
  • Loading branch information
iceljc authored Oct 1, 2024
2 parents ad86b5b + e1503bf commit 1222445
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 39 deletions.
5 changes: 3 additions & 2 deletions src/lib/common/LiveChatEntry.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import { getSettingDetail } from '$lib/services/setting-service';
import { chatBotStore } from '$lib/helpers/store';
import { CHAT_FRAME_ID } from '$lib/helpers/constants';
import { ChatAction } from '$lib/helpers/enums';
let chatUrl = PUBLIC_LIVECHAT_HOST;
Expand All @@ -15,9 +16,9 @@
// Handle event from iframe
window.onmessage = async function(e) {
if (e.data.action == 'close') {
if (e.data.action == ChatAction.Close) {
chatBotStore.set({ showChatBox: false });
} else if (e.data.action == 'open') {
} else if (e.data.action == ChatAction.Open) {
chatBotStore.set({ showChatBox: true });
}
};
Expand Down
2 changes: 2 additions & 0 deletions src/lib/helpers/enums.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ const knowledgeDocSource = {
export const KnowledgeDocSource = Object.freeze(knowledgeDocSource);

const chatAction = {
Open: 'open',
Close: 'close',
Logout: 'logout',
Chat: 'chat',
NewChat: 'new-chat'
Expand Down
12 changes: 0 additions & 12 deletions src/lib/helpers/utils/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,4 @@ export function addChatBoxMountEventListener(func) {
func();
}
});
}

/**
* @param {string} chatFrameId
* @param {string} text
*/
export function loadChatFrame(chatFrameId, text) {
const chatFrame = document.getElementById(chatFrameId);
if (chatFrame) {
// @ts-ignore
chatFrame.contentWindow.postMessage({ action: "chat", text: text }, "*");
}
}
2 changes: 1 addition & 1 deletion src/lib/services/setting-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export async function getSettings() {
/**
*
* @param {string} id
* @returns {Promise<object>}
* @returns {Promise<any>}
*/
export async function getSettingDetail(id) {
let url = replaceUrl(endpoints.settingDetailUrl, {id: id});
Expand Down
71 changes: 47 additions & 24 deletions src/routes/chat/[agentId]/[conversationId]/chat-box.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
let loadChatUtils = false;
let disableSpeech = false;
let isLoading = false;
let isCreatingNewConv = false;
$: {
const editor = lastBotMsg?.rich_content?.editor || '';
Expand Down Expand Up @@ -198,37 +198,58 @@
refresh();
autoScrollLog = false;
window.addEventListener('message', async e => {
window.addEventListener('message', e => {
if (e.data.action === ChatAction.Logout) {
resetLocalStorage(true);
return;
handleLogoutAction();
} else if (e.data.action === ChatAction.NewChat) {
handleNewChatAction(e);
} else if (e.data.action === ChatAction.Chat) {
handleChatAction(e);
}
});
});
if (e.data.action === ChatAction.NewChat) {
const conv = await createNewConversation();
if (!!e.data.text && !isThinking && !isSendingMsg) {
function handleLogoutAction() {
resetLocalStorage(true);
}
/** @param {any} e */
function handleNewChatAction(e) {
if (!isCreatingNewConv && !isThinking && !isSendingMsg) {
isCreatingNewConv = true;
createNewConversation().then(conv => {
isCreatingNewConv = false;
if (conv && !!e.data.text) {
isLoading = true;
sendChatMessage(e.data.text, e.data.data || null, conv.id).then(() => {
redirectToNewConversation(conv);
isLoading = false;
openFrame();
}).catch(() => {
isLoading = false;
openFrame();
});
}
return;
}
}).catch(() => {
isCreatingNewConv = false;
});
}
}
if (e.data.action === ChatAction.Chat && !!e.data.text && !isThinking && !isSendingMsg) {
sendChatMessage(e.data.text, e.data.data || null).then(() => {
openFrame();
});
return;
}
});
});
/** @param {any} e */
function handleChatAction(e) {
if (!!e.data.text && !isThinking && !isSendingMsg) {
sendChatMessage(e.data.text, e.data.data || null).then(() => {
openFrame();
}).catch(() => {
openFrame();
});
}
}
function openFrame() {
if (window.location != window.parent.location) {
window.parent.postMessage({ action: "open" }, "*");
window.parent.postMessage({ action: ChatAction.Open }, "*");
}
}
Expand Down Expand Up @@ -489,6 +510,8 @@
autoScrollLog = true;
clearInstantLogs();
renewUserSentMessages(msgText);
const agentId = params.agentId;
const convId = conversationId || params.conversationId;
let postback = data?.postback;
if (!postback) {
Expand All @@ -511,7 +534,7 @@
if (files?.length > 0 && !!!messageData.inputMessageId) {
const filePayload = buildFilePayload(files);
return new Promise((resolve, reject) => {
uploadConversationFiles(params.agentId, conversationId || params.conversationId, files).then(resMessageId => {
uploadConversationFiles(agentId, convId, files).then(resMessageId => {
messageData = { ...messageData, inputMessageId: resMessageId };
if (!!filePayload) {
messageData = {
Expand All @@ -524,7 +547,7 @@
};
}
sendMessageToHub(params.agentId, conversationId || params.conversationId, msgText, messageData).then(res => {
sendMessageToHub(agentId, convId, msgText, messageData).then(res => {
resolve(res);
}).catch(err => {
reject(err);
Expand All @@ -537,7 +560,7 @@
} else {
return new Promise((resolve, reject) => {
if (!!messageData?.inputMessageId) {
getConversationFiles(params.conversationId, messageData.inputMessageId, FileSourceType.User).then(retFiles => {
getConversationFiles(convId, messageData.inputMessageId, FileSourceType.User).then(retFiles => {
const filePayload = buildFilePayload(retFiles);
if (!!filePayload) {
messageData = {
Expand All @@ -550,7 +573,7 @@
};
}
sendMessageToHub(params.agentId, conversationId || params.conversationId, msgText, messageData).then(res => {
sendMessageToHub(agentId, convId, msgText, messageData).then(res => {
resolve(res);
}).catch(err => {
reject(err);
Expand All @@ -560,7 +583,7 @@
});
});
} else {
sendMessageToHub(params.agentId, conversationId || params.conversationId, msgText, messageData).then(res => {
sendMessageToHub(agentId, convId, msgText, messageData).then(res => {
resolve(res);
}).catch(err => {
reject(err);
Expand Down Expand Up @@ -749,7 +772,7 @@
}
});
} else {
window.parent.postMessage({ action: "close" }, "*");
window.parent.postMessage({ action: ChatAction.Close }, "*");
}
}
Expand Down

0 comments on commit 1222445

Please sign in to comment.