Skip to content

Commit 6738b43

Browse files
added session id
1 parent a71d00c commit 6738b43

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

frontend/src/components/Chatbot.tsx

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,31 @@ import ChatBotUserAvatar from '../assets/images/chatbot-user.png';
55
import ChatBotAvatar from '../assets/images/chatbot-ai.png';
66
import { ChatbotProps } from '../types';
77
import { useCredentials } from '../context/UserCredentials';
8-
import { useFileContext } from '../context/UsersFiles';
98
import chatBotAPI from '../services/QnaAPI';
9+
import { v4 as uuidv4 } from 'uuid';
10+
1011

1112
export default function Chatbot(props: ChatbotProps) {
1213
const { messages: listMessages, setMessages: setListMessages } = props;
1314
const [inputMessage, setInputMessage] = useState('');
1415
const formattedTextStyle = { color: 'rgb(var(--theme-palette-discovery-bg-strong))' };
1516
const [loading, setLoading] = useState<boolean>(false);
1617
const { userCredentials } = useCredentials();
17-
const { model } = useFileContext();
1818
const messagesEndRef = useRef<HTMLDivElement>(null);
19+
const [sessionId, setSessionId] = useState<string>(sessionStorage.getItem("session_id") ?? "")
1920

2021
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
2122
setInputMessage(e.target.value);
2223
};
2324

25+
useEffect(() => {
26+
if (!sessionStorage.getItem("session_id")) {
27+
const id = uuidv4();
28+
setSessionId(id);
29+
sessionStorage.setItem("session_id", id);
30+
}
31+
}, [])
32+
2433
const simulateTypingEffect = (responseText: string, index = 0) => {
2534
if (index < responseText.length) {
2635
const nextIndex = index + 1;
@@ -72,7 +81,7 @@ export default function Chatbot(props: ChatbotProps) {
7281
setLoading(true);
7382
setInputMessage('');
7483
simulateTypingEffect(' ');
75-
const chatresponse = await chatBotAPI(userCredentials, model, inputMessage);
84+
const chatresponse = await chatBotAPI(userCredentials, inputMessage, sessionId);
7685
chatbotReply = chatresponse?.data?.message;
7786
simulateTypingEffect(chatbotReply);
7887
setLoading(false);
@@ -131,14 +140,12 @@ export default function Chatbot(props: ChatbotProps) {
131140
<Widget
132141
header=''
133142
isElevated={true}
134-
className={`p-4 self-start ${
135-
chat.user === 'chatbot' ? 'n-bg-palette-neutral-bg-strong' : 'n-bg-palette-primary-bg-weak'
136-
}`}
143+
className={`p-4 self-start ${chat.user === 'chatbot' ? 'n-bg-palette-neutral-bg-strong' : 'n-bg-palette-primary-bg-weak'
144+
}`}
137145
>
138146
<div
139-
className={`${
140-
loading && index === listMessages.length - 1 && chat.user == 'chatbot' ? 'loader' : ''
141-
}`}
147+
className={`${loading && index === listMessages.length - 1 && chat.user == 'chatbot' ? 'loader' : ''
148+
}`}
142149
>
143150
{chat.message.split(/`(.+?)`/).map((part, index) =>
144151
index % 2 === 1 ? (

frontend/src/services/QnaAPI.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import axios from 'axios';
22
import { url } from '../utils/Utils';
33

4-
const chatBotAPI = async (userCredentials: any, model: string, question: string) => {
4+
const chatBotAPI = async (userCredentials: any, question: string, session_id: string) => {
55
try {
66
const formData = new FormData();
77
formData.append('uri', userCredentials?.uri ?? '');
88
formData.append('database', userCredentials?.database ?? '');
99
formData.append('userName', userCredentials?.userName ?? '');
1010
formData.append('password', userCredentials?.password ?? '');
11-
formData.append('model', model);
1211
formData.append('question', question);
12+
formData.append("session_id", session_id)
1313
const response: any = await axios.post(`${url()}/chat_bot`, formData, {
1414
headers: {
1515
'Content-Type': 'multipart/form-data',

0 commit comments

Comments
 (0)