Skip to content

Commit 47ed5ed

Browse files
refactor: Refactor Frontend/Backend Conversation Flow for Simpler Message Processing
2 parents cada3be + f1bff72 commit 47ed5ed

File tree

14 files changed

+47
-440
lines changed

14 files changed

+47
-440
lines changed

src/App/src/App.tsx

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -234,16 +234,6 @@ const Dashboard: React.FC = () => {
234234

235235
const [ASSISTANT, TOOL, ERROR, USER] = ["assistant", "tool", "error", "user"];
236236

237-
const getLastRagResponse = (messages: ChatMessage[]) => {
238-
const lastAssistantObj = [...messages]
239-
.reverse()
240-
.find((obj) => obj.role === ASSISTANT && typeof obj.content === "string");
241-
if (typeof lastAssistantObj?.content === "string") {
242-
return lastAssistantObj.content.trim();
243-
}
244-
return null;
245-
};
246-
247237
const onSelectConversation = async (id: string) => {
248238
if (!id) {
249239
console.error("No conversation ID found");
@@ -257,10 +247,6 @@ const Dashboard: React.FC = () => {
257247
type: actionConstants.UPDATE_SELECTED_CONV_ID,
258248
payload: id,
259249
});
260-
dispatch({
261-
type: actionConstants.SET_LAST_RAG_RESPONSE,
262-
payload: null,
263-
});
264250
try {
265251
const responseMessages = await historyRead(id);
266252

@@ -273,11 +259,7 @@ const Dashboard: React.FC = () => {
273259
},
274260
});
275261
}
276-
const lastRagResponse = getLastRagResponse(responseMessages);
277-
dispatch({
278-
type: actionConstants.SET_LAST_RAG_RESPONSE,
279-
payload: lastRagResponse,
280-
});
262+
281263
} catch (error) {
282264
console.error("Error fetching conversation messages:", error);
283265
} finally {

src/App/src/api/api.ts

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,8 @@ export async function callConversationApi(
287287
"X-Ms-Client-Principal-Id": userId || "",
288288
},
289289
body: JSON.stringify({
290-
messages: options.messages,
291-
conversation_id: options.id,
292-
last_rag_response: options.last_rag_response
290+
query: options.query,
291+
conversation_id: options.id
293292
}),
294293
signal: abortSignal,
295294
});
@@ -432,42 +431,6 @@ export const historyEnsure = async (): Promise<CosmosDBHealth> => {
432431
return response;
433432
};
434433

435-
export const historyGenerate = async (
436-
options: ConversationRequest,
437-
abortSignal: AbortSignal,
438-
convId?: string
439-
): Promise<Response> => {
440-
let body;
441-
if (convId) {
442-
body = JSON.stringify({
443-
conversation_id: convId,
444-
messages: options.messages,
445-
});
446-
} else {
447-
body = JSON.stringify({
448-
messages: options.messages,
449-
});
450-
}
451-
const userId = getUserIdFromLocalStorage();
452-
const response = await fetch(`${baseURL}/history/generate`, {
453-
method: "POST",
454-
headers: {
455-
"Content-Type": "application/json",
456-
"X-Ms-Client-Principal-Id": userId || "",
457-
},
458-
body: body,
459-
signal: abortSignal,
460-
})
461-
.then((res) => {
462-
return res;
463-
})
464-
.catch((_err) => {
465-
console.error("There was an issue fetching your data.");
466-
return new Response();
467-
});
468-
return response;
469-
};
470-
471434
export const fetchCitationContent = async (body: any) => {
472435
try {
473436
const response = await fetch(`${baseURL}/api/fetch-azure-search-content`, {

src/App/src/components/Chat/Chat.tsx

Lines changed: 20 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ const Chat: React.FC<ChatProps> = ({
6363
}
6464
}, []);
6565

66-
const saveToDB = async (messages: ChatMessage[], convId: string, reqType: string = 'Text') => {
67-
if (!convId || !messages.length) {
66+
const saveToDB = async (newMessages: ChatMessage[], convId: string, reqType: string = 'Text') => {
67+
if (!convId || !newMessages.length) {
6868
return;
6969
}
7070
const isNewConversation = reqType !== 'graph' ? !state.selectedConversationId : false;
@@ -73,14 +73,14 @@ const Chat: React.FC<ChatProps> = ({
7373
payload: true,
7474
});
7575

76-
if (((reqType !== 'graph' && reqType !== 'error') && messages[messages.length - 1].role !== ERROR) && isCharthDisplayDefault ){
76+
if (((reqType !== 'graph' && reqType !== 'error') && newMessages[newMessages.length - 1].role !== ERROR) && isCharthDisplayDefault ){
7777
setIsChartLoading(true);
7878
setTimeout(()=>{
79-
makeApiRequestForChart('show in a graph by default', convId, messages[messages.length - 1].content as string)
79+
makeApiRequestForChart('show in a graph by default', convId, newMessages[newMessages.length - 1].content as string)
8080
},5000)
8181

8282
}
83-
await historyUpdate(messages, convId)
83+
await historyUpdate(newMessages, convId)
8484
.then(async (res) => {
8585
if (!res.ok) {
8686
if (!messages) {
@@ -96,7 +96,7 @@ const Chat: React.FC<ChatProps> = ({
9696
const newConversation: Conversation = {
9797
id: responseJson?.data?.conversation_id,
9898
title: responseJson?.data?.title,
99-
messages: messages,
99+
messages: state.chat.messages,
100100
date: responseJson?.data?.date,
101101
updatedAt: responseJson?.data?.date,
102102
};
@@ -218,10 +218,7 @@ const Chat: React.FC<ChatProps> = ({
218218

219219
const request: ConversationRequest = {
220220
id: conversationId,
221-
messages: [...state.chat.messages, newMessage].filter(
222-
(messageObj) => messageObj.role !== ERROR
223-
),
224-
last_rag_response: lrg
221+
query: question
225222
};
226223

227224
const streamMessage: ChatMessage = {
@@ -271,7 +268,7 @@ const Chat: React.FC<ChatProps> = ({
271268
content: errorMsg,
272269
date: new Date().toISOString(),
273270
};
274-
updatedMessages = [...state.chat.messages, newMessage, errorMessage];
271+
updatedMessages = [newMessage, errorMessage];
275272
dispatch({
276273
type: actionConstants.UPDATE_MESSAGES,
277274
payload: [errorMessage],
@@ -294,11 +291,7 @@ const Chat: React.FC<ChatProps> = ({
294291
parsedChartResponse.object as unknown as ChartDataResponse,
295292
date: new Date().toISOString(),
296293
};
297-
updatedMessages = [
298-
...state.chat.messages,
299-
newMessage,
300-
chartMessage,
301-
];
294+
updatedMessages = [newMessage, chartMessage];
302295
// Update messages with the response content
303296
dispatch({
304297
type: actionConstants.UPDATE_MESSAGES,
@@ -313,11 +306,7 @@ const Chat: React.FC<ChatProps> = ({
313306
content: "Error while generating Chart.",
314307
date: new Date().toISOString(),
315308
};
316-
updatedMessages = [
317-
...state.chat.messages,
318-
newMessage,
319-
chartMessage,
320-
];
309+
updatedMessages = [newMessage, chartMessage];
321310
dispatch({
322311
type: actionConstants.UPDATE_MESSAGES,
323312
payload: [chartMessage],
@@ -357,13 +346,9 @@ const Chat: React.FC<ChatProps> = ({
357346
console.log("Caught with an error while chat and save", e);
358347
if (abortController.signal.aborted) {
359348
if (streamMessage.content) {
360-
updatedMessages = [
361-
...state.chat.messages,
362-
newMessage,
363-
...[streamMessage],
364-
];
349+
updatedMessages = [newMessage, streamMessage];
365350
} else {
366-
updatedMessages = [...state.chat.messages, newMessage];
351+
updatedMessages = [newMessage];
367352
}
368353
console.log(
369354
"@@@ Abort Signal detected: Formed updated msgs",
@@ -429,13 +414,7 @@ const Chat: React.FC<ChatProps> = ({
429414

430415
const request: ConversationRequest = {
431416
id: conversationId,
432-
messages: [...state.chat.messages, newMessage].filter(
433-
(messageObj) => messageObj.role !== ERROR
434-
),
435-
last_rag_response:
436-
isChartQuery(userMessage) && state.chat.lastRagResponse
437-
? JSON.stringify(state.chat.lastRagResponse)
438-
: null,
417+
query: question
439418
};
440419

441420
const streamMessage: ChatMessage = {
@@ -550,7 +529,7 @@ const Chat: React.FC<ChatProps> = ({
550529
content: errorMsg,
551530
date: new Date().toISOString(),
552531
};
553-
updatedMessages = [...state.chat.messages, newMessage, errorMessage];
532+
updatedMessages = [newMessage, errorMessage];
554533
dispatch({
555534
type: actionConstants.UPDATE_MESSAGES,
556535
payload: [errorMessage],
@@ -593,11 +572,7 @@ const Chat: React.FC<ChatProps> = ({
593572
chartResponse as unknown as ChartDataResponse,
594573
date: new Date().toISOString(),
595574
};
596-
updatedMessages = [
597-
...state.chat.messages,
598-
newMessage,
599-
chartMessage,
600-
];
575+
updatedMessages = [newMessage, chartMessage];
601576
// Update messages with the response content
602577
dispatch({
603578
type: actionConstants.UPDATE_MESSAGES,
@@ -612,11 +587,7 @@ const Chat: React.FC<ChatProps> = ({
612587
content: "Error while generating Chart.",
613588
date: new Date().toISOString(),
614589
};
615-
updatedMessages = [
616-
...state.chat.messages,
617-
newMessage,
618-
chartMessage,
619-
];
590+
updatedMessages = [newMessage, chartMessage];
620591
dispatch({
621592
type: actionConstants.UPDATE_MESSAGES,
622593
payload: [chartMessage],
@@ -646,11 +617,7 @@ const Chat: React.FC<ChatProps> = ({
646617
content: errorMsg,
647618
date: new Date().toISOString(),
648619
};
649-
updatedMessages = [
650-
...state.chat.messages,
651-
newMessage,
652-
errorMessage,
653-
];
620+
updatedMessages = [newMessage, errorMessage];
654621
dispatch({
655622
type: actionConstants.UPDATE_MESSAGES,
656623
payload: [errorMessage],
@@ -661,15 +628,7 @@ const Chat: React.FC<ChatProps> = ({
661628
console.log("Error while parsing charts response", e);
662629
}
663630
} else if (!isChartResponseReceived) {
664-
dispatch({
665-
type: actionConstants.SET_LAST_RAG_RESPONSE,
666-
payload: streamMessage?.content as string,
667-
});
668-
updatedMessages = [
669-
...state.chat.messages,
670-
newMessage,
671-
...[streamMessage],
672-
];
631+
updatedMessages = [newMessage, streamMessage];
673632
}
674633
}
675634
if (updatedMessages[updatedMessages.length-1]?.role !== "error") {
@@ -679,13 +638,9 @@ const Chat: React.FC<ChatProps> = ({
679638
console.log("Caught with an error while chat and save", e);
680639
if (abortController.signal.aborted) {
681640
if (streamMessage.content) {
682-
updatedMessages = [
683-
...state.chat.messages,
684-
newMessage,
685-
...[streamMessage],
686-
];
641+
updatedMessages = [newMessage, streamMessage];
687642
} else {
688-
updatedMessages = [...state.chat.messages, newMessage];
643+
updatedMessages = [newMessage];
689644
}
690645
console.log(
691646
"@@@ Abort Signal detected: Formed updated msgs",

src/App/src/state/ActionConstants.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ export const actionConstants = {
2222
ADD_NEW_CONVERSATION_TO_CHAT_HISTORY: "ADD_NEW_CONVERSATION_TO_CHAT_HISTORY",
2323
UPDATE_APP_SPINNER_STATUS: "UPDATE_APP_SPINNER_STATUS",
2424
UPDATE_HISTORY_UPDATE_API_FLAG: "UPDATE_HISTORY_UPDATE_API_FLAG",
25-
SET_LAST_RAG_RESPONSE: "SET_LAST_RAG_RESPONSE",
2625
UPDATE_MESSAGE_BY_ID: "UPDATE_MESSAGE_BY_ID",
2726
UPDATE_STREAMING_FLAG: "UPDATE_STREAMING_FLAG",
2827
UPDATE_CHARTS_FETCHING_FLAG: "UPDATE_CHARTS_FETCHING_FLAG",

src/App/src/state/AppProvider.tsx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ export type AppState = {
2828
generatingResponse: boolean;
2929
messages: ChatMessage[];
3030
userMessage: string;
31-
lastRagResponse: string | null;
3231
isStreamingInProgress: boolean;
3332
citations: string |null;
3433
};
@@ -71,7 +70,6 @@ const initialState: AppState = {
7170
generatingResponse: false,
7271
messages: [],
7372
userMessage: "",
74-
lastRagResponse: null,
7573
citations: "",
7674
isStreamingInProgress: false,
7775
},
@@ -187,10 +185,6 @@ export type Action =
187185
type: typeof actionConstants.UPDATE_HISTORY_UPDATE_API_FLAG;
188186
payload: boolean;
189187
}
190-
| {
191-
type: typeof actionConstants.SET_LAST_RAG_RESPONSE;
192-
payload: string | null;
193-
}
194188
| {
195189
type: typeof actionConstants.UPDATE_MESSAGE_BY_ID;
196190
payload: ChatMessage;

src/App/src/state/AppReducer.tsx

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ const appReducer = (state: AppState, action: Action): AppState => {
8888
case actionConstants.NEW_CONVERSATION_START:
8989
return {
9090
...state,
91-
chat: { ...state.chat, messages: [], lastRagResponse: null },
91+
chat: { ...state.chat, messages: [] },
9292
selectedConversationId: "",
9393
generatedConversationId: generateUUIDv4(),
9494
};
@@ -199,14 +199,6 @@ const appReducer = (state: AppState, action: Action): AppState => {
199199
isHistoryUpdateAPIPending: action.payload,
200200
},
201201
};
202-
case actionConstants.SET_LAST_RAG_RESPONSE:
203-
return {
204-
...state,
205-
chat: {
206-
...state.chat,
207-
lastRagResponse: action.payload,
208-
},
209-
};
210202
case actionConstants.UPDATE_MESSAGE_BY_ID:
211203
const messageID = action.payload.id;
212204
// console.log("aaction::",action.payload)

src/App/src/types/AppTypes.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ export type ChatMessage = {
3939

4040
export type ConversationRequest = {
4141
id?: string;
42-
messages: ChatMessage[];
43-
last_rag_response: string | null;
42+
query: string;
4443
};
4544

4645
export type AskResponse = {
@@ -146,10 +145,6 @@ export type HistoryMetaData = {
146145

147146
export type ParsedChunk = {
148147
error?: string;
149-
id: string;
150-
model: string;
151-
created: number;
152-
object: string;
153148
choices: [
154149
{
155150
messages: [
@@ -158,10 +153,8 @@ export type ParsedChunk = {
158153
role: string;
159154
}
160155
];
161-
history_metadata: object;
162156
}
163157
];
164-
"apim-request-id": string;
165158
};
166159

167160
export type ToolMessageContent = {

src/api/api/api_routes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ async def conversation(request: Request):
117117
# Get the request JSON and last RAG response from the client
118118
request_json = await request.json()
119119
conversation_id = request_json.get("conversation_id")
120-
query = request_json.get("messages")[-1].get("content")
120+
query = request_json.get("query")
121121
chat_service = ChatService(request=request)
122-
result = await chat_service.stream_chat_request(request_json, conversation_id, query)
122+
result = await chat_service.stream_chat_request(conversation_id, query)
123123
track_event_if_configured(
124124
"ChatStreamSuccess",
125125
{"conversation_id": conversation_id, "query": query}

0 commit comments

Comments
 (0)