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

refine message files #145

Merged
merged 2 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
refine message files
  • Loading branch information
Jicheng Lu committed Jun 19, 2024
commit 55ba8861e3c775e628ef91e4df25fed7f92c363e
3 changes: 2 additions & 1 deletion src/lib/helpers/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,8 @@ IRichContent.prototype.quick_replies;
* Conversation send message data
*
* @typedef {Object} MessageData
* @property {string?} [truncateMsgId] - The message id to truncate.
* @property {string?} [truncateMsgId] - The truncated message.
* @property {string?} [inputMessageId] - The input message.
* @property {string[]?} [states] - The states input by user.
* @property {Postback?} [postback] - The parent message id.
* @property {string?} [payload] - The payload message.
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 @@ -46,6 +46,7 @@ export const endpoints = {
conversationUserUrl: `${host}/conversation/{conversationId}/user`,
dialogsUrl: `${host}/conversation/{conversationId}/dialogs`,
conversationMessageDeletionUrl: `${host}/conversation/{conversationId}/message/{messageId}`,
fileUploadUrl: `${host}/agent/{agentId}/conversation/{conversationId}/upload`,

// LLM provider
llmProvidersUrl: `${host}/llm-providers`,
Expand Down
44 changes: 36 additions & 8 deletions src/lib/services/conversation-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,8 @@ export async function GetDialogs(conversationId) {
* @param {string} conversationId - The conversation id
* @param {string} message - The text message sent to CSR
* @param {import('$types').MessageData?} data - Additional data
* @param {any[]} files - The chat files
*/
export async function sendMessageToHub(agentId, conversationId, message, data = null, files = []) {
export async function sendMessageToHub(agentId, conversationId, message, data = null) {
let url = replaceUrl(endpoints.conversationMessageUrl, {
agentId: agentId,
conversationId: conversationId
Expand All @@ -98,10 +97,9 @@ export async function sendMessageToHub(agentId, conversationId, message, data =
const totalStates = !!data?.states && data?.states?.length > 0 ? [...data.states, ...userStates] : [...userStates];
const response = await axios.post(url, {
text: message,
truncateMessageId: data?.truncateMsgId,
states: totalStates,
postback: data?.postback,
files: files
input_message_id: data?.inputMessageId
});
return response.data;
}
Expand Down Expand Up @@ -130,18 +128,48 @@ function buildConversationUserStates(conversationId) {
/**
* delete a message in conversation
* @param {string} conversationId The conversation id
* @param {string} messageId The text message sent to CSR
* @param {string} messageId The target message id to delete
* @param {boolean} isNewMessage If sending a new message while deleting a message
* @returns {Promise<string>}
*/
export async function deleteConversationMessage(conversationId, messageId) {
export async function deleteConversationMessage(conversationId, messageId, isNewMessage = false) {
let url = replaceUrl(endpoints.conversationMessageDeletionUrl, {
conversationId: conversationId,
messageId: messageId
});
const response = await axios.delete(url);
return response.data;

return new Promise((resolve, reject) => {
axios.delete(url, {
data: {
is_new_message: isNewMessage
}
}).then(response => {
resolve(response.data);
}).catch(err => {
reject(err)
});
});
}


/**
* upload conversation files
* @param {string} agentId The agent id
* @param {string} converationId The conversation id
* @param {any[]} files The conversation files
* @returns {Promise<string>}
*/
export async function uploadConversationFiles(agentId, converationId, files) {
let url = replaceUrl(endpoints.fileUploadUrl, {
agentId: agentId,
conversationId: converationId
});
const response = await axios.post(url, {
files: files
});
return response.data;
}

/**
* delete a message in conversation
* @param {string} text The user input
Expand Down
56 changes: 38 additions & 18 deletions src/routes/chat/[agentId]/[conversationId]/chat-box.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
GetDialogs,
deleteConversationMessage,
getConversationFiles,
getConversationUser
getConversationUser,
uploadConversationFiles
} from '$lib/services/conversation-service.js';
import 'overlayscrollbars/overlayscrollbars.css';
import { OverlayScrollbars } from 'overlayscrollbars';
Expand Down Expand Up @@ -415,29 +416,44 @@
renewUserSentMessages(msgText);
const postback = buildPostbackMessage(dialogs, data?.payload || msgText, data?.truncateMsgId);
/** @type {import('$types').MessageData?} */
const messageData = {
let messageData = {
...data,
postback: postback
};

/** @type {any[]} */
let files = [];
if (!!!data?.truncateMsgId) {
if (!!!messageData?.inputMessageId) {
files = getChatFiles();
}
resetStorage();

return new Promise((resolve, reject) => {
sendMessageToHub(params.agentId, params.conversationId, msgText, messageData, files).then(res => {
isSendingMsg = false;
autoScrollLog = false;
resolve(res);
}).catch(err => {
isSendingMsg = false;
autoScrollLog = false;
reject(err);
if (files?.length > 0 && !!!messageData.inputMessageId) {
return new Promise((resolve, reject) => {
uploadConversationFiles(params.agentId, params.conversationId, files).then(resMessageId => {
messageData = { ...messageData, inputMessageId: resMessageId };
sendMessageToHub(params.agentId, params.conversationId, msgText, messageData).then(res => {
resolve(res);
}).catch(err => {
reject(err);
}).finally(() => {
isSendingMsg = false;
autoScrollLog = false;
});
});
});
});
} else {
return new Promise((resolve, reject) => {
sendMessageToHub(params.agentId, params.conversationId, msgText, messageData).then(res => {
resolve(res);
}).catch(err => {
reject(err);
}).finally(() => {
isSendingMsg = false;
autoScrollLog = false;
});
});
}
}

async function startListen() {
Expand Down Expand Up @@ -656,7 +672,9 @@
// @ts-ignore
}).then(async (result) => {
if (result.value) {
sendChatMessage(message?.text, { truncateMsgId: message?.message_id });
deleteConversationMessage(params.conversationId, message?.message_id, true).then(resMessageId => {
sendChatMessage(message?.text, { inputMessageId: resMessageId });
});
}
});
}
Expand Down Expand Up @@ -719,10 +737,12 @@

async function confirmEditMsg() {
isOpenEditMsgModal = false;
sendChatMessage(editText, { truncateMsgId: truncateMsgId }).then(() => {
resetEditMsg();
}).catch(() => {
resetEditMsg();
deleteConversationMessage(params.conversationId, truncateMsgId, true).then(resMessageId => {
sendChatMessage(editText, { inputMessageId: resMessageId }).then(() => {
resetEditMsg();
}).catch(() => {
resetEditMsg();
});
});
}

Expand Down