Skip to content

Commit f668be0

Browse files
authored
Merge pull request #354 from iceljc/features/add-stream-chat
Features/add stream chat
2 parents eee0ded + e4a399a commit f668be0

File tree

10 files changed

+226
-96
lines changed

10 files changed

+226
-96
lines changed

.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ PUBLIC_LIVECHAT_VOICE_ENABLED=false
77
PUBLIC_LIVECHAT_SPEAKER_ENABLED=false
88
PUBLIC_LIVECHAT_FILES_ENABLED=false
99
PUBLIC_LIVECHAT_ENABLE_TRAINING=false
10+
PUBLIC_LIVECHAT_STREAM_ENABLED=false
1011
PUBLIC_HOME_IMAGE=images/megamenu-img.png
1112
PUBLIC_HOME_SLOGAN=A central workspace for building, testing and evaluating your AI Agents.
1213
PUBLIC_LOGO_URL=images/logo.png

src/lib/helpers/constants.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ export const ADMIN_ROLES = [
2020

2121
export const BOT_SENDERS = [
2222
UserRole.Assistant,
23-
UserRole.Bot
23+
UserRole.Bot,
24+
UserRole.System
2425
];
2526

2627
export const TEXT_EDITORS = [

src/lib/helpers/types/conversationTypes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ IRichContent.prototype.quick_replies;
152152
* @property {Date} created_at - The message sent time.
153153
* @property {boolean} has_message_files
154154
* @property {boolean} is_chat_message
155+
* @property {boolean} is_streaming
155156
*/
156157

157158
/**

src/lib/helpers/utils/common.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,20 @@ export function removeDuplicates(arr, key) {
8383
/**
8484
* @param {(string | null)[]} args
8585
*/
86-
export const classnames = (...args) => args.filter(Boolean).join(' ');
86+
export function classnames(...args) {
87+
return args.filter(Boolean).join(' ');
88+
}
8789

90+
/**
91+
* @param {number} milliseconds
92+
*/
93+
export function delay(milliseconds = 100) {
94+
return new Promise(resolve => {
95+
setTimeout(() => {
96+
resolve('');
97+
}, milliseconds);
98+
});
99+
};
88100

89101
/**
90102
* @param {{ page: any, pageSize: any }} args

src/lib/services/api-endpoints.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export const endpoints = {
5959
// conversation
6060
conversationInitUrl: `${host}/conversation/{agentId}`,
6161
conversationMessageUrl: `${host}/conversation/{agentId}/{conversationId}`,
62+
6263
notificationUrl: `${host}/conversation/{conversationId}/notification`,
6364
conversationsUrl: `${host}/conversations`,
6465
conversationCountUrl: `${host}/conversations/count`,

src/lib/services/conversation-service.js

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,17 @@ export async function getConversations(filter) {
5454
* @param {string} conversationId
5555
* @param {string} messageId
5656
* @param {string} source
57+
* @returns {Promise<any[]>}
5758
*/
5859
export async function getConversationFiles(conversationId, messageId, source) {
5960
const url = replaceUrl(endpoints.conversationAttachmentUrl, { conversationId: conversationId, messageId: messageId, source: source });
60-
const response = await axios.get(url);
61-
return response?.data;
61+
return new Promise((resolve, reject) => {
62+
axios.get(url).then(response => {
63+
resolve(response?.data);
64+
}).catch(err => {
65+
reject(err);
66+
});
67+
});
6268
}
6369

6470
/**
@@ -102,15 +108,21 @@ export async function sendMessageToHub(agentId, conversationId, text, data = nul
102108
});
103109
const userStates = buildConversationUserStates(conversationId);
104110
const totalStates = !!data?.states && data?.states?.length > 0 ? [...data.states, ...userStates] : [...userStates];
105-
const response = await axios.post(url, {
106-
text: text,
107-
states: totalStates,
108-
postback: data?.postback,
109-
input_message_id: data?.inputMessageId
111+
return new Promise((resolve, reject) => {
112+
axios.post(url, {
113+
text: text,
114+
states: totalStates,
115+
postback: data?.postback,
116+
input_message_id: data?.inputMessageId
117+
}).then(response => {
118+
resolve(response?.data);
119+
}).catch(err => {
120+
reject(err);
121+
});
110122
});
111-
return response.data;
112123
}
113124

125+
114126
/**
115127
* pin a conversation to dashboard
116128
* @param {string} agentId - The agent id
@@ -253,14 +265,18 @@ export async function updateConversationMessage(conversationId, request) {
253265
* @returns {Promise<string>}
254266
*/
255267
export async function uploadConversationFiles(agentId, converationId, files) {
256-
let url = replaceUrl(endpoints.fileUploadUrl, {
268+
const url = replaceUrl(endpoints.fileUploadUrl, {
257269
agentId: agentId,
258270
conversationId: converationId
259271
});
260-
const response = await axios.post(url, {
261-
files: files
272+
273+
return new Promise((resolve, reject) => {
274+
axios.post(url, { files: files }).then(response => {
275+
resolve(response?.data);
276+
}).catch(err => {
277+
reject(err);
278+
});
262279
});
263-
return response.data;
264280
}
265281

266282
/**

src/lib/services/signalr-service.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ export const signalr = {
4242
/** @type {import('$conversationTypes').OnConversationMessageDeleted} */
4343
onConversationMessageDeleted: () => {},
4444

45+
/** @type {import('$conversationTypes').OnMessageReceived} */
46+
beforeReceiveLlmStreamMessage: () => {},
47+
/** @type {import('$conversationTypes').OnMessageReceived} */
48+
onReceiveLlmStreamMessage: async () => {},
49+
/** @type {import('$conversationTypes').OnMessageReceived} */
50+
afterReceiveLlmStreamMessage: () => {},
51+
4552
// start the connection
4653
/** @param {string} conversationId */
4754
async start(conversationId) {
@@ -142,6 +149,24 @@ export const signalr = {
142149
this.onConversationMessageDeleted(data);
143150
}
144151
});
152+
153+
connection.on('BeforeReceiveLlmStreamMessage', data => {
154+
if (conversationId === data?.conversation_id) {
155+
this.beforeReceiveLlmStreamMessage(data);
156+
}
157+
});
158+
159+
connection.on('OnReceiveLlmStreamMessage', data => {
160+
if (conversationId === data?.conversation_id) {
161+
this.onReceiveLlmStreamMessage(data);
162+
}
163+
});
164+
165+
connection.on('AfterReceiveLlmStreamMessage', data => {
166+
if (conversationId === data?.conversation_id) {
167+
this.afterReceiveLlmStreamMessage(data);
168+
}
169+
});
145170
},
146171

147172
// stop the connection

0 commit comments

Comments
 (0)