Skip to content

Commit ae57db0

Browse files
committed
Chatbot typescript
1 parent 0bf05d9 commit ae57db0

12 files changed

Lines changed: 5967 additions & 7675 deletions

File tree

services/web/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ RUN npm install --silent
2121
COPY ./src /app/src
2222
COPY ./public /app/public
2323
RUN cp ./src/config.js.template ./src/config.js
24-
RUN NODE_OPTIONS=--max_old_space_size=2048 npm run build
24+
RUN NODE_OPTIONS=--max_old_space_size=8192 npm run build
2525
RUN ls /app/build
2626

2727
# Main Image

services/web/package-lock.json

Lines changed: 5790 additions & 7211 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

services/web/package.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"name": "crapi-web",
33
"version": "0.1.0",
4-
"proxy": "http://localhost:30080",
4+
"proxy": "http://crapi.allvapps:30080",
55
"private": true,
66
"dependencies": {
7-
"@ant-design/icons": "^4.8.3",
87
"@ant-design/cssinjs": "^1.21.1",
8+
"@ant-design/icons": "^4.8.3",
99
"@ant-design/pro-components": "^2.7.15",
1010
"@reduxjs/toolkit": "^2.2.7",
1111
"@testing-library/jest-dom": "^5.17.0",
@@ -21,8 +21,8 @@
2121
"crypto-js": "^4.2.0",
2222
"jsonwebtoken": "^9.0.2",
2323
"prop-types": "^15.8.1",
24-
"react-chatbot-kit": "^2.2.2",
2524
"react": "^18.3.0",
25+
"react-chatbot-kit": "^2.2.2",
2626
"react-dom": "^18.3.0",
2727
"react-linkify": "^1.0.0-alpha",
2828
"react-redux": "^9.1.2",
@@ -32,9 +32,9 @@
3232
"redux": "^5.0.1",
3333
"redux-persist": "^6.0.0",
3434
"redux-saga": "^1.3.0",
35+
"source-map-loader": "^5.0.0",
3536
"styled-components": "^6.1.8",
3637
"superagent": "^8.1.2",
37-
"source-map-loader": "^5.0.0",
3838
"ts-loader": "^9.5.1",
3939
"typescript": "^4.9.5",
4040
"web-vitals": "^2.1.4"
@@ -68,9 +68,11 @@
6868
"devDependencies": {
6969
"@babel/cli": "^7.24.1",
7070
"@babel/core": "^7.24.4",
71-
"@babel/preset-react": "^7.24.1",
7271
"@babel/plugin-proposal-private-property-in-object": "7.21.11",
72+
"@babel/preset-react": "^7.24.1",
73+
"@types/superagent": "^8.1.9",
7374
"copy-webpack-plugin": "^6.3.2",
75+
"eslint-config-react-app": "^7.0.1",
7476
"prettier": "3.3.3"
7577
},
7678
"browser": {

services/web/src/components/bot/ActionProvider.jsx renamed to services/web/src/components/bot/ActionProvider.tsx

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,52 @@
1414
*/
1515

1616
import { APIService } from "../../constants/APIConstant";
17-
import { isAccessTokenValid } from "../../utils";
17+
// import { isAccessTokenValid } from "../../utils";
18+
import superagent from "superagent";
1819

19-
const superagent = require("superagent");
20+
interface ChatBotMessage {
21+
id: string;
22+
message: string;
23+
loading?: boolean;
24+
terminateLoading?: boolean;
25+
}
26+
27+
interface State {
28+
messages: ChatBotMessage[];
29+
openapiKey: string | null;
30+
initializing: boolean;
31+
initializationRequired: boolean;
32+
}
33+
34+
type SetStateFunc = (stateUpdater: (state: State) => State) => void;
2035

2136
class ActionProvider {
22-
constructor(createChatBotMessage, setStateFunc, createClientMessage) {
37+
private createChatBotMessage: (message: string, options?: Partial<ChatBotMessage>) => ChatBotMessage;
38+
private setState: SetStateFunc;
39+
private createClientMessage: (message: string) => ChatBotMessage;
40+
41+
constructor(
42+
createChatBotMessage: (message: string, options?: Partial<ChatBotMessage>) => ChatBotMessage,
43+
setStateFunc: SetStateFunc,
44+
createClientMessage: (message: string) => ChatBotMessage
45+
) {
2346
this.createChatBotMessage = createChatBotMessage;
2447
this.setState = setStateFunc;
2548
this.createClientMessage = createClientMessage;
2649
}
27-
handleNotInitialized = () => {
50+
51+
handleNotInitialized = (): void => {
2852
const message = this.createChatBotMessage(
2953
"To initialize the chatbot, please type init and press enter.",
3054
{
3155
loading: true,
3256
terminateLoading: true,
33-
},
57+
}
3458
);
3559
this.addMessageToState(message);
3660
};
3761

38-
handleInitialize = (initRequired) => {
62+
handleInitialize = (initRequired: boolean): void => {
3963
console.log("Initialization required:", initRequired);
4064
if (initRequired) {
4165
this.addOpenApiKeyToState(null);
@@ -45,7 +69,7 @@ class ActionProvider {
4569
{
4670
loading: true,
4771
terminateLoading: true,
48-
},
72+
}
4973
);
5074
this.addMessageToState(message);
5175
} else {
@@ -57,14 +81,14 @@ class ActionProvider {
5781
}
5882
};
5983

60-
handleInitialized = (apiKey, accessToken) => {
84+
handleInitialized = (apiKey: string | null, accessToken: string): void => {
6185
if (!apiKey) {
6286
const message = this.createChatBotMessage(
6387
"Please enter a valid OpenAI API key.",
6488
{
6589
loading: true,
6690
terminateLoading: true,
67-
},
91+
}
6892
);
6993
this.addMessageToState(message);
7094
return;
@@ -86,7 +110,7 @@ class ActionProvider {
86110
{
87111
loading: true,
88112
terminateLoading: true,
89-
},
113+
}
90114
);
91115
this.addMessageToState(errormessage);
92116
return;
@@ -97,15 +121,14 @@ class ActionProvider {
97121
{
98122
loading: true,
99123
terminateLoading: true,
100-
},
124+
}
101125
);
102126
this.addMessageToState(successmessage);
103127
this.addInitializedToState();
104128
});
105-
return;
106129
};
107130

108-
handleChat = (message, accessToken) => {
131+
handleChat = (message: string, accessToken: string): void => {
109132
const chatUrl = APIService.CHATBOT_SERVICE + "genai/ask";
110133
superagent
111134
.post(chatUrl)
@@ -121,7 +144,7 @@ class ActionProvider {
121144
{
122145
loading: true,
123146
terminateLoading: true,
124-
},
147+
}
125148
);
126149
this.addMessageToState(errormessage);
127150
return;
@@ -132,19 +155,18 @@ class ActionProvider {
132155
terminateLoading: true,
133156
});
134157
this.addMessageToState(successmessage);
135-
return;
136158
});
137159
};
138160

139-
handleHelp = (initRequired) => {
161+
handleHelp = (initRequired: boolean): void => {
140162
console.log("Initialization required:", initRequired);
141163
if (initRequired) {
142164
const message = this.createChatBotMessage(
143165
"To initialize the chatbot, please type init and press enter. To clear the chat context, type clear or reset and press enter.",
144166
{
145167
loading: true,
146168
terminateLoading: true,
147-
},
169+
}
148170
);
149171
this.addMessageToState(message);
150172
} else {
@@ -153,13 +175,13 @@ class ActionProvider {
153175
{
154176
loading: true,
155177
terminateLoading: true,
156-
},
178+
}
157179
);
158180
this.addMessageToState(message);
159181
}
160182
};
161183

162-
handleResetContext = (accessToken) => {
184+
handleResetContext = (accessToken: string): void => {
163185
localStorage.removeItem("chat_messages");
164186
this.clearMessages();
165187
const resetUrl = APIService.CHATBOT_SERVICE + "genai/reset";
@@ -176,7 +198,7 @@ class ActionProvider {
176198
{
177199
loading: true,
178200
terminateLoading: true,
179-
},
201+
}
180202
);
181203
this.addMessageToState(errormessage);
182204
return;
@@ -187,44 +209,43 @@ class ActionProvider {
187209
{
188210
loading: true,
189211
terminateLoading: true,
190-
},
212+
}
191213
);
192214
this.addMessageToState(successmessage);
193215
this.addInitializedToState();
194216
});
195-
return;
196217
};
197218

198-
addMessageToState = (message) => {
219+
addMessageToState = (message: ChatBotMessage): void => {
199220
this.setState((state) => ({
200221
...state,
201222
messages: [...state.messages, message],
202223
}));
203224
};
204225

205-
addOpenApiKeyToState = (api_key) => {
226+
addOpenApiKeyToState = (api_key: string | null): void => {
206227
this.setState((state) => ({
207228
...state,
208229
openapiKey: api_key,
209230
}));
210231
};
211232

212-
addInitializingToState = () => {
233+
addInitializingToState = (): void => {
213234
this.setState((state) => ({
214235
...state,
215236
initializing: true,
216237
}));
217238
};
218239

219-
addInitializedToState = () => {
240+
addInitializedToState = (): void => {
220241
this.setState((state) => ({
221242
...state,
222243
initializing: false,
223244
initializationRequired: false,
224245
}));
225246
};
226247

227-
clearMessages = () => {
248+
clearMessages = (): void => {
228249
this.setState((state) => ({
229250
...state,
230251
messages: [],

0 commit comments

Comments
 (0)