Skip to content

Commit 48f1bf3

Browse files
committed
Store user state for chatbot
1 parent 1374e2c commit 48f1bf3

File tree

5 files changed

+65
-18
lines changed

5 files changed

+65
-18
lines changed

services/web/src/components/bot/ActionProvider.jsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
*/
1515

1616
import { APIService } from "../../constants/APIConstant";
17+
import { isAccessTokenValid } from "../../utils";
18+
1719
const superagent = require("superagent");
1820

1921
class ActionProvider {
@@ -55,8 +57,8 @@ class ActionProvider {
5557
}
5658
};
5759

58-
handleInitialized = (api_key) => {
59-
if (!api_key) {
60+
handleInitialized = (apiKey, accessToken) => {
61+
if (!apiKey) {
6062
const message = this.createChatBotMessage(
6163
"Please enter a valid OpenAI API key.",
6264
{
@@ -67,14 +69,15 @@ class ActionProvider {
6769
this.addMessageToState(message);
6870
return;
6971
}
70-
localStorage.setItem("openapi_key", api_key);
71-
this.addOpenApiKeyToState(api_key);
72+
localStorage.setItem("openapi_key", apiKey);
73+
this.addOpenApiKeyToState(apiKey);
7274
const initUrl = APIService.CHATBOT_SERVICE + "genai/init";
7375
superagent
7476
.post(initUrl)
75-
.send({ openai_api_key: api_key })
77+
.send({ openai_api_key: apiKey })
7678
.set("Accept", "application/json")
7779
.set("Content-Type", "application/json")
80+
.set("Authorization", `Bearer ${accessToken}`)
7881
.end((err, res) => {
7982
if (err) {
8083
console.log(err);
@@ -102,13 +105,14 @@ class ActionProvider {
102105
return;
103106
};
104107

105-
handleChat = (message) => {
108+
handleChat = (message, accessToken) => {
106109
const chatUrl = APIService.CHATBOT_SERVICE + "genai/ask";
107110
superagent
108111
.post(chatUrl)
109112
.send({ question: message })
110113
.set("Accept", "application/json")
111114
.set("Content-Type", "application/json")
115+
.set("Authorization", `Bearer ${accessToken}`)
112116
.end((err, res) => {
113117
if (err) {
114118
console.log(err);

services/web/src/components/bot/Bot.jsx

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { APIService } from "../../constants/APIConstant";
2020
import MessageParser from "./MessageParser.jsx";
2121
import ActionProvider from "./ActionProvider.jsx";
2222
import Chatbot from "react-chatbot-kit";
23+
import { connect } from "react-redux";
2324
import { createChatBotMessage } from "react-chatbot-kit";
2425
import {
2526
PageHeader,
@@ -50,14 +51,52 @@ const PandaSvg = () => (
5051

5152
const PandaIcon = (props) => <Icon component={PandaSvg} {...props} />;
5253

53-
const ChatBotComponent = () => {
54+
const ChatBotComponent = (props) => {
5455
const [chatbotState, setChatbotState] = useState({
55-
messages: [],
5656
openapiKey: localStorage.getItem("openapi_key"),
5757
initializing: false,
5858
initializationRequired: false,
59+
accessToken: props.accessToken,
60+
isLoggedIn: props.isLoggedIn,
61+
role: props.role,
5962
});
6063

64+
useEffect(() => {
65+
const fetchInit = async () => {
66+
const stateUrl = APIService.CHATBOT_SERVICE + "genai/state";
67+
let initRequired = false;
68+
// Wait for the response
69+
await superagent
70+
.get(stateUrl)
71+
.set("Accept", "application/json")
72+
.set("Content-Type", "application/json")
73+
.then((res) => {
74+
console.log("I response:", res.body);
75+
if (res.status === 200) {
76+
if (res.body?.initialized === "true") {
77+
initRequired = false;
78+
} else {
79+
initRequired = true;
80+
}
81+
}
82+
})
83+
.catch((err) => {
84+
console.log("Error prefetch: ", err);
85+
});
86+
console.log("Initialization required:", initRequired);
87+
setChatbotState((prev) => ({
88+
...prev,
89+
initializationRequired: initRequired,
90+
}));
91+
};
92+
fetchInit();
93+
}, []);
94+
95+
const config_chatbot = {
96+
...config,
97+
state: chatbotState,
98+
};
99+
61100
const [showBot, toggleBot] = useState(false);
62101

63102
const saveMessages = (messages, HTMLString) => {
@@ -73,14 +112,15 @@ const ChatBotComponent = () => {
73112
localStorage.removeItem("chat_messages");
74113
};
75114

115+
console.log("Config state", config_chatbot);
76116
return (
77117
<Row>
78118
<Col xs={10}>
79119
<div className="app-chatbot-container">
80120
<div style={{ maxWidth: "500px" }}>
81121
{showBot && (
82122
<Chatbot
83-
config={config}
123+
config={config_chatbot}
84124
botAvator={
85125
<Icon
86126
icon={PandaIcon}
@@ -102,7 +142,7 @@ const ChatBotComponent = () => {
102142
background: "#0a5e9c",
103143
borderRadius: "0px",
104144
}}
105-
href="#"
145+
href="##"
106146
onClick={() => clearHistory()}
107147
>
108148
<DeleteOutlined />
@@ -115,7 +155,7 @@ const ChatBotComponent = () => {
115155
background: "#0a5e9c",
116156
borderRadius: "0px",
117157
}}
118-
href="#"
158+
href="##"
119159
onClick={() => toggleBot((prev) => !prev)}
120160
>
121161
<CloseSquareOutlined />

services/web/src/components/bot/MessageParser.jsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ class MessageParser {
5050
console.log("State:", this.state);
5151
console.log("Message:", message);
5252
const message_l = message.toLowerCase();
53-
if (this.state.initializationRequired === undefined) {
53+
if (this.state?.initializationRequired === undefined) {
5454
this.state.initializationRequired = await this.initializationRequired();
55-
console.log("State:", this.state);
55+
console.log("State check:", this.state);
5656
}
5757
if (message_l === "help") {
5858
this.state.initializationRequired = await this.initializationRequired();
@@ -63,6 +63,7 @@ class MessageParser {
6363
console.log("State init:", this.state);
6464
return this.actionProvider.handleInitialize(
6565
this.state.initializationRequired,
66+
this.state.accessToken,
6667
);
6768
} else if (
6869
message_l === "clear" ||
@@ -76,7 +77,7 @@ class MessageParser {
7677
return this.actionProvider.handleNotInitialized();
7778
}
7879

79-
return this.actionProvider.handleChat(message);
80+
return this.actionProvider.handleChat(message, this.state.accessToken);
8081
}
8182
}
8283

services/web/src/components/bot/config.jsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ const config = {
2525
),
2626
],
2727
botName,
28-
tate: {
29-
optionName: "",
30-
},
28+
state: {},
3129
customStyles: {
3230
botMessageBox: {
3331
backgroundColor: "#376B7E",

services/web/src/components/layout/layout.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,11 @@ const StyledComp = connect(
329329
}}
330330
/>
331331
</Switch>
332-
<ChatBotComponent />
332+
<ChatBotComponent
333+
accessToken={props.accessToken}
334+
isLoggedIn={props.isLoggedIn}
335+
role={props.role}
336+
/>
333337
</Content>
334338
</Layout>
335339
</Spin>

0 commit comments

Comments
 (0)