-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
319 lines (282 loc) · 10.1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
const express = require('express');
const cors = require('cors');
const { initializeApp } = require('firebase/app');
const { getFirestore, collection, getDocs, addDoc, getDoc, doc, setDoc} = require('firebase/firestore/lite');
const firebaseConfig = require('./config');
const { generateAnswer, translate, explainGrammar, getVocabularyFromSentence } = require('./model');
const fb_app = initializeApp(firebaseConfig);
const db = getFirestore(fb_app);
const app = express();
app.use(express.json());
app.use(cors());
/**
* all routes for
* user collection
*/
// Define the getUser function
async function getUser(db) {
const userCol = collection(db, "user");
const userSnapshot = await getDocs(userCol);
const userList = userSnapshot.docs.map((doc) => doc.id);
return userList;
}
// Define the route for getting users
app.get("/users", async (req, res) => {
try {
const userList = await getUser(db);
res.status(200).send(JSON.stringify(userList));
} catch (error) {
console.error(error);
res.status(500).json({ message: "Internal server error" });
}
});
// Define the route for getting users
app.post("/newUser", async (req, res) => {
try {
const users = collection(db, "user");
docRef = await addDoc(users, {key: "123"})
res.status(200).json({userId: docRef.id});
} catch (error) {
console.error(error);
res.status(500).json({ message: "Internal server error" });
}
});
// Get chats
app.get("/api/user/:userId/chats", async (req, res) => {
try {
const userId = req.params.userId;
const chatsRef = collection(db, "user", userId, "chats");
const querySnapshot = await getDocs(chatsRef);
const chats = querySnapshot.docs.map((doc) => doc.id);
console.log(chats);
res.json(chats);
} catch (error) {
console.log("Error getting documents: ", error);
res.status(500).send("Error getting documents");
}
});
// Get vocab list
app.get("/api/user/:userId/vocabulary", async (req, res) => {
try {
const userId = req.params.userId;
const vocabRef = collection(db, "user", userId, "vocabulary");
const querySnapshot = await getDocs(vocabRef);
const chats = querySnapshot.docs.map((doc) => doc.data());
// Combine into a single list
const result = {};
chats.forEach(element => {
result[element.key] = element.value;
});
console.log(result);
res.json(result);
} catch (error) {
console.log("Error getting documents: ", error);
res.status(500).send("Error getting documents");
}
});
// Set vocab
app.post("/api/user/:userId/vocabulary", async (req, res) => {
try {
const userId = req.params.userId;
const data = req.body;
const vocabRef = collection(db, "user", userId, "vocabulary");
vocabRefNew = await addDoc(vocabRef, {
"key": data.key,
"value": data.value
});
res.status(200).send("Success");
} catch (error) {
console.log("Error getting documents: ", error);
res.status(500).send("Error getting documents");
}
});
//add new chat
app.post("/api/user/:userId/newChat", async (req, res) => {
try {
const userId = req.params.userId;
const data = req.body;
const chatCol = collection(db, "user", userId, "chats");
chatRef = await addDoc(chatCol, {
key: data.key,
niveau: data.niveau
})
res.status(200).json({"chatId": chatRef.id});
} catch (error) {
console.error(error);
res.status(500).json({ message: "error adding chat" });
}
});
// Post chat content
app.post("/api/user/:userId/chats/:chatId/sendMessage", async (req, res) => {
try {
const data = req.body; // parsed JSON data
const userId = req.params.userId;
const chatId = req.params.chatId;
const chatRef = collection(db, "user", userId, "chats", chatId, "messages");
const messageRef = await addDoc(chatRef, {
description: "",
id: "todo: id is unecessary?",
messages: data.message,
time: Date.now(),
user: "Human"
})
res.status(200).send("Success");
} catch (error) {
console.log("Error getting documents: ", error);
res.status(500).send("Error getting documents");
}
});
// Get chat content
app.get("/api/user/:userId/chats/:chatId/messages", async (req, res) => {
try {
const userId = req.params.userId;
const chatId = req.params.chatId;
const chatsRef = collection(db, "user", userId, "chats", chatId, "messages");
const querySnapshot = await getDocs(chatsRef);
const chatContent = querySnapshot.docs.map((doc) => doc.data());
console.log(chatContent);
res.json(chatContent);
} catch (error) {
console.log("Error getting documents: ", error);
res.status(500).send("Error getting documents");
}
});
// Post chat content
app.post("/api/user/:userId/chats/:chatId/generateResponse", async (req, res) => {
try {
const userId = req.params.userId;
const chatId = req.params.chatId;
const language = req.body.language;
// Generate chat history
const messagesChat = [];
const messagesRef = collection(db, "user", userId, "chats", chatId, "messages");
const querySnapshot = await getDocs(messagesRef);
querySnapshot.docs.forEach(doc => {
const data = doc.data();
messagesChat.push({"message": data.messages, "time": data.time, "user": data.user})
});
messagesChat.sort((a, b) => a.time - b.time);
if(messagesChat[messagesChat.length - 1].user == "Ai") {
return res.status(500).send("Ai already responded!");
}
const niveau = "A1";
const partnerName = chatId == "1" ? "Jürgen" : "Laura";
// Generate response
const answer = await generateAnswer(language, niveau, partnerName, messagesChat)
console.log(answer);
// Save AI message
const messages = collection(db, "user", userId, "chats", chatId, "messages");
const messageRef = await addDoc(messages, {
description: "",
id: "todo: id is unecessary?",
messages: answer,
time: Date.now(),
user: "Ai"
})
// Return new chat
const chatsRefUpdated = collection(db, "user", userId, "chats", chatId, "messages");
const querySnapshotUpdated = await getDocs(chatsRefUpdated);
const chatContent = querySnapshotUpdated.docs.map((doc) => doc.data());
console.log(chatContent);
res.json(chatContent);
} catch (error) {
console.log("Error getting documents: ", error);
res.status(500).send("Error getting documents");
}
});
// Get chat niveau
app.get("/api/user/:userId/chats/:chatId/languageLevel", async (req, res) => {
try {
const userId = req.params.userId;
const chatId = req.params.chatId;
const chatsRef = collection(db, "user", userId, "chats");
const chatRef = doc(chatsRef, chatId);
const chatDocSnapshot = await getDoc(chatRef);
console.log(chatDocSnapshot.data().niveau);
res.json(chatDocSnapshot.data().niveau);
} catch (error) {
console.log("Error getting documents: ", error);
res.status(500).send("Error getting documents");
}
});
app.post("/api/user/:userId/chats/:chatId/explainGrammar", async (req, res) => {
try {
const userId = req.params.userId;
const chatId = req.params.chatId;
const message = req.body.message;
const outputLanguage = req.body.outputLanguage;
const explanation = await explainGrammar(message, outputLanguage)
const messages = collection(db, "user", userId, "chats", chatId, "messages");
const messageRef = await addDoc(messages, {
description: "",
id: "todo: id is unecessary?",
messages: explanation,
time: Date.now(),
user: "Ai"
})
// Return new chat
const chatsRefUpdated = collection(db, "user", userId, "chats", chatId, "messages");
const querySnapshotUpdated = await getDocs(chatsRefUpdated);
const chatContent = querySnapshotUpdated.docs.map((doc) => doc.data());
console.log(chatContent);
res.json(chatContent);
} catch (error) {
console.log("Error explaining sentence: ", error);
res.status(500).send("Error getting documents");
}
});
app.post("/api/user/:userId/chats/:chatId/translate", async (req, res) => {
try {
const userId = req.params.userId;
const chatId = req.params.chatId;
const message = req.body.message;
const outputLanguage = req.body.outputLanguage;
const translation = await translate(message, outputLanguage)
// Save AI message
const messages = collection(db, "user", userId, "chats", chatId, "messages");
const messageRef = await addDoc(messages, {
description: "",
id: "todo: id is unecessary?",
messages: translation,
time: Date.now(),
user: "Ai"
})
// Return new chat
const chatsRefUpdated = collection(db, "user", userId, "chats", chatId, "messages");
const querySnapshotUpdated = await getDocs(chatsRefUpdated);
const chatContent = querySnapshotUpdated.docs.map((doc) => doc.data());
console.log(chatContent);
res.json(chatContent);
} catch (error) {
console.log("Error translating sentence: ", error);
res.status(500).send("Error getting documents");
}
});
app.post("/api/users/:userId/saveVocabularyFromSentence", async (req, res) => {
try {
const userId = req.params.userId;
const message = req.body.message;
const outputLanguage = req.body.outputLanguage;
const vocab = await getVocabularyFromSentence(message, outputLanguage)
// const vocabRef = collection(db, "user", userId, "vocabulary");
// vocabRefNew = await addDoc(vocabRef, {
// "key": data.key,
// "value": data.value
// });
const vocabRef = collection(db, "user", userId, "vocabulary");
for (let [key, value] of Object.entries(vocab)) {
console.log(`Key: ${key}, Value: ${value}`);
await addDoc(vocabRef, {
"key": key,
"value":value
});
}
// Todo: convert data structure necessary?
res.status(200).json(vocab);
} catch (error) {
console.log("Error getting vocabulary: ", error);
res.status(500).send("Error getting documents");
}
});
const port = process.env.PORT || 8080;
app.listen(port, ()=>console.log("Up and running *4000"));