Skip to content

Commit 78cc02c

Browse files
authored
Merge pull request #145 from iceljc/features/refine-chat-window
refine message files
2 parents 879b33a + 41f1c59 commit 78cc02c

File tree

4 files changed

+77
-27
lines changed

4 files changed

+77
-27
lines changed

src/lib/helpers/types.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,8 @@ IRichContent.prototype.quick_replies;
408408
* Conversation send message data
409409
*
410410
* @typedef {Object} MessageData
411-
* @property {string?} [truncateMsgId] - The message id to truncate.
411+
* @property {string?} [truncateMsgId] - The truncated message.
412+
* @property {string?} [inputMessageId] - The input message.
412413
* @property {string[]?} [states] - The states input by user.
413414
* @property {Postback?} [postback] - The parent message id.
414415
* @property {string?} [payload] - The payload message.

src/lib/services/api-endpoints.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export const endpoints = {
4646
conversationUserUrl: `${host}/conversation/{conversationId}/user`,
4747
dialogsUrl: `${host}/conversation/{conversationId}/dialogs`,
4848
conversationMessageDeletionUrl: `${host}/conversation/{conversationId}/message/{messageId}`,
49+
fileUploadUrl: `${host}/agent/{agentId}/conversation/{conversationId}/upload`,
4950

5051
// LLM provider
5152
llmProvidersUrl: `${host}/llm-providers`,

src/lib/services/conversation-service.js

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,8 @@ export async function GetDialogs(conversationId) {
8787
* @param {string} conversationId - The conversation id
8888
* @param {string} message - The text message sent to CSR
8989
* @param {import('$types').MessageData?} data - Additional data
90-
* @param {any[]} files - The chat files
9190
*/
92-
export async function sendMessageToHub(agentId, conversationId, message, data = null, files = []) {
91+
export async function sendMessageToHub(agentId, conversationId, message, data = null) {
9392
let url = replaceUrl(endpoints.conversationMessageUrl, {
9493
agentId: agentId,
9594
conversationId: conversationId
@@ -98,10 +97,9 @@ export async function sendMessageToHub(agentId, conversationId, message, data =
9897
const totalStates = !!data?.states && data?.states?.length > 0 ? [...data.states, ...userStates] : [...userStates];
9998
const response = await axios.post(url, {
10099
text: message,
101-
truncateMessageId: data?.truncateMsgId,
102100
states: totalStates,
103101
postback: data?.postback,
104-
files: files
102+
input_message_id: data?.inputMessageId
105103
});
106104
return response.data;
107105
}
@@ -130,18 +128,48 @@ function buildConversationUserStates(conversationId) {
130128
/**
131129
* delete a message in conversation
132130
* @param {string} conversationId The conversation id
133-
* @param {string} messageId The text message sent to CSR
131+
* @param {string} messageId The target message id to delete
132+
* @param {boolean} isNewMessage If sending a new message while deleting a message
133+
* @returns {Promise<string>}
134134
*/
135-
export async function deleteConversationMessage(conversationId, messageId) {
135+
export async function deleteConversationMessage(conversationId, messageId, isNewMessage = false) {
136136
let url = replaceUrl(endpoints.conversationMessageDeletionUrl, {
137137
conversationId: conversationId,
138138
messageId: messageId
139139
});
140-
const response = await axios.delete(url);
141-
return response.data;
140+
141+
return new Promise((resolve, reject) => {
142+
axios.delete(url, {
143+
data: {
144+
is_new_message: isNewMessage || false
145+
}
146+
}).then(response => {
147+
resolve(response.data);
148+
}).catch(err => {
149+
reject(err)
150+
});
151+
});
142152
}
143153

144154

155+
/**
156+
* upload conversation files
157+
* @param {string} agentId The agent id
158+
* @param {string} converationId The conversation id
159+
* @param {any[]} files The conversation files
160+
* @returns {Promise<string>}
161+
*/
162+
export async function uploadConversationFiles(agentId, converationId, files) {
163+
let url = replaceUrl(endpoints.fileUploadUrl, {
164+
agentId: agentId,
165+
conversationId: converationId
166+
});
167+
const response = await axios.post(url, {
168+
files: files
169+
});
170+
return response.data;
171+
}
172+
145173
/**
146174
* delete a message in conversation
147175
* @param {string} text The user input

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

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
GetDialogs,
1717
deleteConversationMessage,
1818
getConversationFiles,
19-
getConversationUser
19+
getConversationUser,
20+
uploadConversationFiles
2021
} from '$lib/services/conversation-service.js';
2122
import 'overlayscrollbars/overlayscrollbars.css';
2223
import { OverlayScrollbars } from 'overlayscrollbars';
@@ -415,29 +416,44 @@
415416
renewUserSentMessages(msgText);
416417
const postback = buildPostbackMessage(dialogs, data?.payload || msgText, data?.truncateMsgId);
417418
/** @type {import('$types').MessageData?} */
418-
const messageData = {
419+
let messageData = {
419420
...data,
420421
postback: postback
421422
};
422423
423424
/** @type {any[]} */
424425
let files = [];
425-
if (!!!data?.truncateMsgId) {
426+
if (!!!messageData?.inputMessageId) {
426427
files = getChatFiles();
427428
}
428429
resetStorage();
429430
430-
return new Promise((resolve, reject) => {
431-
sendMessageToHub(params.agentId, params.conversationId, msgText, messageData, files).then(res => {
432-
isSendingMsg = false;
433-
autoScrollLog = false;
434-
resolve(res);
435-
}).catch(err => {
436-
isSendingMsg = false;
437-
autoScrollLog = false;
438-
reject(err);
431+
if (files?.length > 0 && !!!messageData.inputMessageId) {
432+
return new Promise((resolve, reject) => {
433+
uploadConversationFiles(params.agentId, params.conversationId, files).then(resMessageId => {
434+
messageData = { ...messageData, inputMessageId: resMessageId };
435+
sendMessageToHub(params.agentId, params.conversationId, msgText, messageData).then(res => {
436+
resolve(res);
437+
}).catch(err => {
438+
reject(err);
439+
}).finally(() => {
440+
isSendingMsg = false;
441+
autoScrollLog = false;
442+
});
443+
});
439444
});
440-
});
445+
} else {
446+
return new Promise((resolve, reject) => {
447+
sendMessageToHub(params.agentId, params.conversationId, msgText, messageData).then(res => {
448+
resolve(res);
449+
}).catch(err => {
450+
reject(err);
451+
}).finally(() => {
452+
isSendingMsg = false;
453+
autoScrollLog = false;
454+
});
455+
});
456+
}
441457
}
442458
443459
async function startListen() {
@@ -656,7 +672,9 @@
656672
// @ts-ignore
657673
}).then(async (result) => {
658674
if (result.value) {
659-
sendChatMessage(message?.text, { truncateMsgId: message?.message_id });
675+
deleteConversationMessage(params.conversationId, message?.message_id, true).then(resMessageId => {
676+
sendChatMessage(message?.text, { inputMessageId: resMessageId });
677+
});
660678
}
661679
});
662680
}
@@ -719,10 +737,12 @@
719737
720738
async function confirmEditMsg() {
721739
isOpenEditMsgModal = false;
722-
sendChatMessage(editText, { truncateMsgId: truncateMsgId }).then(() => {
723-
resetEditMsg();
724-
}).catch(() => {
725-
resetEditMsg();
740+
deleteConversationMessage(params.conversationId, truncateMsgId, true).then(resMessageId => {
741+
sendChatMessage(editText, { inputMessageId: resMessageId }).then(() => {
742+
resetEditMsg();
743+
}).catch(() => {
744+
resetEditMsg();
745+
});
726746
});
727747
}
728748

0 commit comments

Comments
 (0)