@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/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
- console.log("### --> body: ", body);
18
+ KBSettings.findOne({ id_project: project_id }, async (err, kbSettings) => {
13
19
 
14
- KBSettings.findOne({ id_project: project_id }, (err, kbSettings) => {
15
- console.log("kbSettings: ", kbSettings);
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
- let gptkey = kbSettings.gptkey;
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
- // winston.debug("completions response: ", response);
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;
@@ -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;