Skip to content

Commit 47bc1b0

Browse files
exception handling added for chart
1 parent 823fdea commit 47bc1b0

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

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

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -558,23 +558,35 @@ const Chat: React.FC<ChatProps> = ({
558558
scrollChatToBottom();
559559
} else if (isChartQuery(userMessage)) {
560560
try {
561-
562-
const splitRunningText= runningText.split("}{")
563-
let parsedChartResponse:any = {};
564-
parsedChartResponse["object"] = JSON.parse(JSON.parse("{" + splitRunningText[splitRunningText.length-1])?.choices[0]?.messages[0]?.content)?.answer;
565-
561+
const splitRunningText = runningText.split("}{");
562+
let parsedChartResponse: any = {};
563+
parsedChartResponse= JSON.parse("{" + splitRunningText[splitRunningText.length - 1]);
564+
// console.log("parsedChartResponse", parsedChartResponse);
565+
let chartResponse : any = {};
566+
try {
567+
chartResponse = JSON.parse(parsedChartResponse?.choices[0]?.messages[0]?.content)
568+
} catch (e) {
569+
chartResponse = parsedChartResponse?.choices[0]?.messages[0]?.content;
570+
}
571+
572+
573+
// console.log("chartResponse", chartResponse);
574+
if (typeof chartResponse === 'object' && chartResponse?.answer) {
575+
chartResponse = chartResponse.answer;
576+
}
577+
// console.log("chartResponse", chartResponse);
566578
if (
567-
"object" in parsedChartResponse &&
568-
parsedChartResponse?.object?.type &&
569-
parsedChartResponse?.object?.data
579+
// "object" in parsedChartResponse &&
580+
chartResponse?.type &&
581+
chartResponse?.data
570582
) {
571583
// CHART CHECKING
572584
try {
573585
const chartMessage: ChatMessage = {
574586
id: generateUUIDv4(),
575587
role: ASSISTANT,
576588
content:
577-
parsedChartResponse.object as unknown as ChartDataResponse,
589+
chartResponse as unknown as ChartDataResponse,
578590
date: new Date().toISOString(),
579591
};
580592
updatedMessages = [
@@ -608,12 +620,13 @@ const Chat: React.FC<ChatProps> = ({
608620
scrollChatToBottom();
609621
}
610622
} else if (
611-
parsedChartResponse.error ||
612-
parsedChartResponse?.object?.message
623+
parsedChartResponse?.error ||
624+
parsedChartResponse?.choices[0]?.messages[0]?.content
613625
) {
626+
console.log("parsedChartRespons 615::", parsedChartResponse);
614627
const errorMsg =
615-
parsedChartResponse.error ||
616-
parsedChartResponse?.object?.message;
628+
parsedChartResponse?.error ||
629+
parsedChartResponse?.choices[0]?.messages[0]?.content
617630
const errorMessage: ChatMessage = {
618631
id: generateUUIDv4(),
619632
role: ERROR,

0 commit comments

Comments
 (0)