Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add notification #238

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 42 additions & 5 deletions src/lib/common/DialogModal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
/** @type {boolean} */
export let isOpen;

/** @type {boolean} */
export let closeable = false;

/** @type {string} */
export let size = 'xl';

/** @type {string} */
/** @type {string | any} */
export let title;

/** @type {string} */
Expand All @@ -28,6 +31,9 @@
/** @type {() => void} */
export let cancel = () => {};

/** @type {() => void} */
export let close = () => {};

/** @type {boolean} */
export let disableConfirmBtn = false;

Expand All @@ -43,13 +49,44 @@
cancel && cancel();
}

/** @param {any} e */
function handleClose(e) {
e.preventDefault();
close && close();
}
</script>


<Modal class={className} fade size={size} isOpen={isOpen} toggle={() => toggleModal()}>
{#if !!title}
<ModalHeader>{title}</ModalHeader>
{/if}
<Modal
class={`dialog-modal-container ${className}`}
fade
size={size}
isOpen={isOpen}
toggle={() => toggleModal()}
unmountOnClose
>
<ModalHeader>
<div class="dialog-modal-header">
{#if !!title}
<div class="header-title">
<slot name='title-icon'/>
<div>{title}</div>
</div>
{/if}
{#if closeable}
<div class="header-close">
<!-- svelte-ignore a11y-no-static-element-interactions -->
<!-- svelte-ignore a11y-click-events-have-key-events -->
<div
class="clickable"
on:click={e => handleClose(e)}
>
<i class="mdi mdi-close" />
</div>
</div>
{/if}
</div>
</ModalHeader>
<ModalBody>
<slot />
</ModalBody>
Expand Down
2 changes: 1 addition & 1 deletion src/lib/common/LiveChatEntry.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
showChatBox = false;
} else if (e.data.action == ChatAction.Open) {
// showChatBox = true;
} else if (e.data.action == ChatAction.ReceiveMsg && !showChatBox) {
} else if (e.data.action == ChatAction.ReceiveNotification && !showChatBox) {
receivedMsg = e.data?.data?.rich_content?.message?.text || e.data?.data?.text || '';
showBubbleMsg = true;
wave();
Expand Down
2 changes: 1 addition & 1 deletion src/lib/common/StateModal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@
}
</script>

<Modal class={className} fade size={size} isOpen={isOpen} toggle={() => toggleModal && toggleModal()}>
<Modal class={className} fade size={size} isOpen={isOpen} unmountOnClose toggle={() => toggleModal && toggleModal()}>
<ModalHeader>{title}</ModalHeader>
<ModalBody>
<Form class="state-form">
Expand Down
5 changes: 3 additions & 2 deletions src/lib/helpers/enums.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ const contentLogSource = {
Prompt: "prompt",
FunctionCall: "function call",
AgentResponse: "agent response",
HardRule: "hard rule"
HardRule: "hard rule",
Notification: "notification"
};
export const ContentLogSource = Object.freeze(contentLogSource);

Expand Down Expand Up @@ -106,6 +107,6 @@ const chatAction = {
Logout: 'logout',
Chat: 'chat',
NewChat: 'new-chat',
ReceiveMsg: 'receive-msg'
ReceiveNotification: 'receive-notification'
};
export const ChatAction = Object.freeze(chatAction);
24 changes: 23 additions & 1 deletion src/lib/scss/custom/components/_chat.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
$bubble-chat-theme-color: rgba($primary, 80%);

.dialog-modal-container {
.modal-title {
width: 100%;
}

.dialog-modal-header {
display: flex;
justify-content: space-between;

.header-title {
display: flex;
gap: 8px;
}
}
}


.chat-util-common {
display: block;
position: absolute;
Expand Down Expand Up @@ -45,7 +62,12 @@ $bubble-chat-theme-color: rgba($primary, 80%);
font-size: 30px;
}


.chat-notification {
min-height: 20px;
max-height: 150px;
overflow-y: auto;
scrollbar-width: none;
}

.chat-bubble-container {
display: flex;
Expand Down
1 change: 1 addition & 0 deletions src/lib/services/api-endpoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const endpoints = {
// conversation
conversationInitUrl: `${host}/conversation/{agentId}`,
conversationMessageUrl: `${host}/conversation/{agentId}/{conversationId}`,
notificationUrl: `${host}/conversation/{conversationId}/notification`,
conversationsUrl: `${host}/conversations`,
conversationCountUrl: `${host}/conversations/count`,
conversationDeletionUrl: `${host}/conversation/{conversationId}`,
Expand Down
18 changes: 18 additions & 0 deletions src/lib/services/conversation-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,24 @@ export async function sendMessageToHub(agentId, conversationId, text, data = nul
}


/**
* send a notification to conversation
* @param {string} conversationId - The conversation id
* @param {string} text - The text message sent to CSR
* @param {import('$conversationTypes').MessageData?} data - Additional data
*/
export async function sendNotification(conversationId, text, data = null) {
let url = replaceUrl(endpoints.notificationUrl, {
conversationId: conversationId
});
const response = await axios.post(url, {
text: text,
states: [],
postback: data?.postback
});
return response.data;
}

/**
* @param {string} conversationId
*/
Expand Down
10 changes: 10 additions & 0 deletions src/lib/services/signalr-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export const signalr = {
/** @type {import('$conversationTypes').OnMessageReceived} */
onMessageReceivedFromAssistant: () => {},

/** @type {import('$conversationTypes').OnMessageReceived} */
onNotificationGenerated: () => {},

/** @type {import('$conversationTypes').OnConversationContentLogReceived} */
onConversationContentLogGenerated: () => {},

Expand Down Expand Up @@ -93,6 +96,13 @@ export const signalr = {
}
});

connection.on('OnNotificationGenerated', (json) => {
const message = JSON.parse(json);
if (conversationId === message?.conversation_id) {
this.onNotificationGenerated(message);
}
});

connection.on('OnConversationContentLogGenerated', (log) => {
const jsonLog = JSON.parse(log);
if (conversationId === jsonLog?.conversation_id) {
Expand Down
46 changes: 43 additions & 3 deletions src/routes/chat/[agentId]/[conversationId]/chat-box.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
let truncateMsgId = "";
let indication = "";
let mode = '';
let notificationText = '';

/** @type {number} */
let messageInputTimeout;
Expand Down Expand Up @@ -158,6 +159,7 @@
let disableSpeech = false;
let isLoading = false;
let isCreatingNewConv = false;
let isDisplayNotification = false;

$: {
const editor = lastBotMsg?.rich_content?.editor || '';
Expand All @@ -184,6 +186,7 @@
signalr.onMessageReceivedFromClient = onMessageReceivedFromClient;
signalr.onMessageReceivedFromCsr = onMessageReceivedFromCsr;
signalr.onMessageReceivedFromAssistant = onMessageReceivedFromAssistant;
signalr.onNotificationGenerated = onNotificationGenerated;
signalr.onConversationContentLogGenerated = onConversationContentLogGenerated;
signalr.onConversationStateLogGenerated = onConversationStateLogGenerated;
signalr.onStateChangeGenerated = onStateChangeGenerated;
Expand Down Expand Up @@ -255,9 +258,9 @@
}

/** @param {import('$conversationTypes').ChatResponseModel} message */
function sendReceivedMessage(message) {
function sendReceivedNotification(message) {
if (isFrame) {
window.parent.postMessage({ action: ChatAction.ReceiveMsg, data: message }, "*");
window.parent.postMessage({ action: ChatAction.ReceiveNotification, data: message }, "*");
}
}

Expand Down Expand Up @@ -431,10 +434,21 @@
...message,
is_chat_message: true
});
sendReceivedMessage(message);
refresh();
}

/** @param {import('$conversationTypes').ChatResponseModel} message */
function onNotificationGenerated(message) {
notificationText = message?.rich_content?.message?.text || message.text || '';
isDisplayNotification = true;
setTimeout(() => {
isDisplayNotification = false;
notificationText = '';
}, notificationText?.length > 200 ? 8000 : 3000);

sendReceivedNotification(message);
}

/** @param {import('$conversationTypes').ConversationContentLogModel} log */
function onConversationContentLogGenerated(log) {
if (!isLoadPersistLog) return;
Expand Down Expand Up @@ -1094,6 +1108,13 @@
};
sendChatMessage(text, data);
}

function toggleNotificationModal() {
isDisplayNotification = !isDisplayNotification;
if (!isDisplayNotification) {
notificationText = '';
}
}
</script>


Expand All @@ -1103,6 +1124,25 @@
<Loader size={35} />
{/if}

<DialogModal
title={'Notification'}
size={'md'}
isOpen={isDisplayNotification}
closeable
toggleModal={() => toggleNotificationModal()}
confirmBtnText={''}
cancelBtnText={''}
close={() => toggleNotificationModal()}
>
<div slot='title-icon' class="color: text-warning">
<i class="mdi mdi-bell-ring" />
</div>
<div class="chat-notification">
{notificationText}
</div>
</DialogModal>


<DialogModal
title={'Edit message'}
size={'md'}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
const includedSources = [
ContentLogSource.Prompt,
ContentLogSource.AgentResponse,
ContentLogSource.FunctionCall
ContentLogSource.FunctionCall,
ContentLogSource.Notification
];

$: {
if (data.source === ContentLogSource.AgentResponse) {
if (data.source === ContentLogSource.AgentResponse || data.source === ContentLogSource.Notification) {
logDisplayStyle = 'border border-secondary';
logTextStyle = 'text-info';
} else if (data.source === ContentLogSource.FunctionCall) {
Expand Down
Loading
Loading