@tiledesk/tiledesk-server 2.4.100 → 2.4.101
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +4 -0
- package/app.js +45 -9
- package/event/emailEvent.js +13 -0
- package/event/integrationEvent.js +13 -0
- package/models/integrations.js +23 -0
- package/package.json +1 -1
- package/pubmodules/cache/mongoose-cachegoose-fn.js +12 -0
- package/pubmodules/emailNotification/requestNotification.js +1 -0
- package/routes/auth.js +14 -5
- package/routes/email.js +4 -2
- package/routes/faq.js +1 -0
- package/routes/integration.js +199 -0
- package/routes/kbsettings.js +8 -76
- package/routes/openai.js +52 -14
- package/routes/project.js +3 -4
- package/routes/quotes.js +52 -0
- package/routes/request.js +515 -499
- package/routes/users.js +5 -1
- package/services/QuoteManager.js +317 -0
- package/services/cacheEnabler.js +5 -0
- package/services/emailService.js +610 -586
- package/services/messageService.js +283 -202
- package/services/openaiService.js +12 -12
- package/services/requestService.js +1764 -1421
- package/services/trainingService.js +6 -2
- package/test/messageService.js +154 -92
- package/test/mock/MockTdCache.js +46 -0
- package/test/mock/emailMock.js +9 -0
- package/test/mock/messageMock.js +46 -0
- package/test/mock/projectMock.js +171 -0
- package/test/mock/requestMock.js +127 -0
- package/test/quoteManager.js +282 -0
- package/test/requestRoute.js +1 -1
- package/test/requestService.js +1196 -1079
- package/utils/TdCache.js +253 -0
package/routes/openai.js
CHANGED
@@ -3,28 +3,44 @@ var router = express.Router();
|
|
3
3
|
var { KBSettings } = require('../models/kb_setting');
|
4
4
|
var openaiService = require('../services/openaiService');
|
5
5
|
var winston = require('../config/winston');
|
6
|
+
const { QuoteManager } = require('../services/QuoteManager');
|
6
7
|
|
7
8
|
router.post('/', async (req, res) => {
|
8
9
|
|
9
10
|
let project_id = req.projectid;
|
10
11
|
let body = req.body;
|
12
|
+
let usePublicKey = false;
|
13
|
+
let publicKey = process.env.GPTKEY;
|
14
|
+
let gptkey = null;
|
15
|
+
let obj = { createdAt: new Date() };
|
16
|
+
let quoteManager = req.app.get('quote_manager');
|
11
17
|
|
12
|
-
|
18
|
+
KBSettings.findOne({ id_project: project_id }, async (err, kbSettings) => {
|
13
19
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
if (!kbSettings) {
|
18
|
-
return res.status(400).send({ success: false, message: "Missing gptkey parameter (settings not exist)" })
|
20
|
+
if (err) {
|
21
|
+
usePublicKey = true;
|
22
|
+
gptkey = publicKey;
|
19
23
|
}
|
20
24
|
|
21
|
-
|
25
|
+
if (kbSettings && kbSettings.gptkey) {
|
26
|
+
gptkey = kbSettings.gptkey;
|
27
|
+
} else {
|
28
|
+
usePublicKey = true;
|
29
|
+
gptkey = publicKey;
|
30
|
+
}
|
22
31
|
|
23
32
|
if (!gptkey) {
|
24
|
-
return res.status(400).send({ success: false, message: "Missing gptkey parameter" })
|
33
|
+
return res.status(400).send({ success: false, message: "Missing gptkey parameter" });
|
34
|
+
}
|
35
|
+
|
36
|
+
if (usePublicKey === true) {
|
37
|
+
let isAvailable = await quoteManager.checkQuote(req.project, obj, 'tokens');
|
38
|
+
if (isAvailable === false) {
|
39
|
+
return res.status(403).send("Tokens quota exceeded")
|
40
|
+
}
|
25
41
|
}
|
42
|
+
|
26
43
|
|
27
|
-
// attua modifiche
|
28
44
|
let json = {
|
29
45
|
"model": body.model,
|
30
46
|
"messages": [
|
@@ -43,20 +59,42 @@ router.post('/', async (req, res) => {
|
|
43
59
|
message.content = body.context;
|
44
60
|
json.messages.unshift(message);
|
45
61
|
}
|
46
|
-
console.log("openai preview --> json: ", json);
|
47
62
|
|
48
|
-
openaiService.completions(json, gptkey).then((response) => {
|
49
|
-
|
63
|
+
openaiService.completions(json, gptkey).then(async (response) => {
|
64
|
+
let data = { createdAt: new Date(), tokens: response.data.usage.total_tokens }
|
65
|
+
if (usePublicKey === true) {
|
66
|
+
let incremented_key = await quoteManager.incrementTokenCount(req.project, data);
|
67
|
+
winston.verbose("Tokens quota incremented for key " + incremented_key);
|
68
|
+
}
|
50
69
|
res.status(200).send(response.data);
|
70
|
+
|
51
71
|
}).catch((err) => {
|
52
|
-
console.log("err: ", err);
|
53
72
|
// winston.error("completions error: ", err);
|
54
73
|
res.status(500).send(err)
|
55
74
|
})
|
56
|
-
|
57
75
|
})
|
58
76
|
})
|
59
77
|
|
78
|
+
router.post('/quotes', async (req, res) => {
|
79
|
+
|
80
|
+
let project = req.project;
|
81
|
+
|
82
|
+
let body = req.body;
|
83
|
+
body.createdAt = new Date(body.createdAt);
|
84
|
+
|
85
|
+
let redis_client = req.app.get('redis_client');
|
86
|
+
if (!redis_client) {
|
87
|
+
return res.status(400).send({ error: "Redis not ready"});
|
88
|
+
}
|
89
|
+
|
90
|
+
let quoteManager = req.app.get('quote_manager');
|
91
|
+
|
92
|
+
let incremented_key = await quoteManager.incrementTokenCount(req.project, req.body);
|
93
|
+
let quote = await quoteManager.getCurrentQuote(req.project, req.body, 'tokens');
|
94
|
+
|
95
|
+
res.status(200).send({ message: "value incremented for key " + incremented_key, key: incremented_key, currentQuote: quote });
|
96
|
+
})
|
97
|
+
|
60
98
|
// router.get('/', async (req, res) => {
|
61
99
|
|
62
100
|
// let project_id = req.projectid;
|
package/routes/project.js
CHANGED
@@ -233,10 +233,9 @@ router.put('/:projectid', [passport.authenticate(['basic', 'jwt'], { session: fa
|
|
233
233
|
update.bannedUsers = req.body.bannedUsers;
|
234
234
|
}
|
235
235
|
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
236
|
+
if (req.body.profile != undefined) {
|
237
|
+
update.profile = req.body.profile;
|
238
|
+
}
|
240
239
|
|
241
240
|
// if (req.body.defaultLanguage!=undefined) {
|
242
241
|
// update.defaultLanguage = req.body.defaultLanguage;
|
package/routes/quotes.js
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
var express = require('express');
|
2
|
+
var router = express.Router();
|
3
|
+
const { QuoteManager } = require('../services/QuoteManager');
|
4
|
+
let winston = require('../config/winston');
|
5
|
+
|
6
|
+
|
7
|
+
router.post('/', async (req, res) => {
|
8
|
+
|
9
|
+
let date = req.body.date;
|
10
|
+
|
11
|
+
let obj = { createdAt: new Date() };
|
12
|
+
if (date) {
|
13
|
+
obj.createdAt = new Date(date)
|
14
|
+
}
|
15
|
+
|
16
|
+
let quoteManager = req.app.get('quote_manager');
|
17
|
+
|
18
|
+
// check if project is not null/undefined
|
19
|
+
let quotes = await quoteManager.getAllQuotes(req.project, obj);
|
20
|
+
|
21
|
+
winston.debug("quotes: ", quotes);
|
22
|
+
res.status(200).send({ message: 'ok', quotes: quotes });
|
23
|
+
|
24
|
+
})
|
25
|
+
|
26
|
+
router.get('/:type', async (req, res) => {
|
27
|
+
|
28
|
+
let type = req.params.type;
|
29
|
+
let obj = { createdAt: new Date() };
|
30
|
+
|
31
|
+
let quoteManager = req.app.get('quote_manager');
|
32
|
+
let isAvailable = await quoteManager.checkQuote(req.project, obj, type);
|
33
|
+
|
34
|
+
winston.debug("is " + type + " available: ", isAvailable);
|
35
|
+
res.status(200).send({ isAvailable: isAvailable })
|
36
|
+
|
37
|
+
})
|
38
|
+
|
39
|
+
router.post('/incr/:type', async (req, res) => {
|
40
|
+
|
41
|
+
let type = req.params.type;
|
42
|
+
let body = req.body;
|
43
|
+
body.createdAt = new Date();
|
44
|
+
|
45
|
+
let quoteManager = req.app.get('quote_manager');
|
46
|
+
let incremented_key = await quoteManager.incrementTokenCount(req.project, req.body);
|
47
|
+
let quote = await quoteManager.getCurrentQuote(req.project, req.body, type);
|
48
|
+
|
49
|
+
res.status(200).send({ message: "value incremented for key " + incremented_key, key: incremented_key, currentQuote: quote });
|
50
|
+
})
|
51
|
+
|
52
|
+
module.exports = router;
|