From 19eba2b27b0808c605ead562f4f589caa08f3824 Mon Sep 17 00:00:00 2001 From: pradeepjha-microsoft Date: Thu, 30 Jan 2025 19:32:07 +0530 Subject: [PATCH] update history method changes --- ClientAdvisor/App/frontend/src/api/api.ts | 54 +++++++++++++++-------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/ClientAdvisor/App/frontend/src/api/api.ts b/ClientAdvisor/App/frontend/src/api/api.ts index b59dceb0..acd20736 100644 --- a/ClientAdvisor/App/frontend/src/api/api.ts +++ b/ClientAdvisor/App/frontend/src/api/api.ts @@ -201,31 +201,47 @@ export const selectUser = async (options: ClientIdRequest): Promise => return new Response(null, { status: 500, statusText: 'Internal Server Error' }); } }; - +function isLastObjectNotEmpty(arr:any) + { + if (arr.length === 0) return false; + // Handle empty array case + const lastObj = arr[arr.length - 1]; + return Object.keys(lastObj).length > 0; + } export const historyUpdate = async (messages: ChatMessage[], convId: string): Promise => { - const response = await fetch('/history/update', { - method: 'POST', - body: JSON.stringify({ - conversation_id: convId, - messages: messages - }), - headers: { - 'Content-Type': 'application/json' - } - }) - .then(async res => { - return res + if(isLastObjectNotEmpty(messages)){ + const response = await fetch('/history/update', { + method: 'POST', + body: JSON.stringify({ + conversation_id: convId, + messages: messages + }), + headers: { + 'Content-Type': 'application/json' + } }) - .catch(_err => { - console.error('There was an issue fetching your data.') + .then(async res => { + return res + }) + .catch(_err => { + console.error('There was an issue fetching your data.') + const errRes: Response = { + ...new Response(), + ok: false, + status: 500 + } + return errRes + }) + return response + } + else{ const errRes: Response = { ...new Response(), ok: false, status: 500 } - return errRes - }) - return response + return errRes + } } export const historyDelete = async (convId: string): Promise => { @@ -425,4 +441,4 @@ export const historyMessageFeedback = async (messageId: string, feedback: string // const data = await response.text(); // console.log('Response:', data); -// }; \ No newline at end of file +// };