@tiledesk/tiledesk-server 2.4.100 → 2.4.102

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  var express = require('express');
2
2
  var { KBSettings } = require('../models/kb_setting');
3
- var { KB } = require('../models/kb_setting');
3
+ // var { KB } = require('../models/kb_setting');
4
4
  // var KB = require('../models/kb_setting')
5
5
  var router = express.Router();
6
6
  var winston = require('../config/winston');
@@ -99,75 +99,11 @@ router.delete('/:settings_id/:kb_id', async (req, res) => {
99
99
 
100
100
  })
101
101
 
102
- // PROXY PUGLIA AI V2 - START
103
- router.post('/scrape/single', async (req, res) => {
104
-
105
- let data = req.body;
106
- winston.debug("/scrape/single data: ", data);
107
-
108
- let gptkey = process.env.GPTKEY;
109
- if (!gptkey) {
110
- return res.status(403).send({ success: false, error: "GPT apikey undefined"})
111
- }
112
-
113
- data.gptkey = gptkey;
114
-
115
- openaiService.singleScrape(data).then((resp) => {
116
- winston.debug("singleScrape resp: ", resp.data);
117
- return res.status(200).send(resp.data);
118
- }).catch((err) => {
119
- winston.error("singleScrape err: ", err);
120
- let status = err.response.status;
121
- return res.status(status).send({ statusText: err.response.statusText, detail: err.response.data.detail });
122
- })
123
- })
124
-
125
- router.post('/scrape/status', async (req, res) => {
126
-
127
- let data = req.body;
128
- winston.debug("/scrapeStatus req.body: ", req.body);
129
-
130
- openaiService.scrapeStatus(data).then((response) => {
131
-
132
- winston.debug("scrapeStatus response.data: ", response.data);
133
- res.status(200).send(response.data);
134
- }).catch((err) => {
135
- winston.error("scrapeStatus err: ", err);
136
- let status = err.response.status;
137
- res.status(status).send({ statusText: err.response.statusText, detail: err.response.data.detail });
138
- })
139
- })
140
-
141
- router.post('/ask', async (req, res) => {
142
- let data = req.body;
143
- winston.debug("/qa data: ", data);
144
-
145
- if (!data.gptkey) {
146
- let gptkey = process.env.GPTKEY;
147
- if (!gptkey) {
148
- return res.status(403).send({ success: false, error: "GPT apikey undefined"})
149
- }
150
- data.gptkey = gptkey;
151
- }
152
-
153
- openaiService.askNamespace(data).then((resp) => {
154
- winston.debug("qa resp: ", resp.data);
155
- res.status(200).send(resp.data);
156
- }).catch((err) => {
157
- winston.error("qa err: ", err);
158
- let status = err.response.status;
159
- res.status(status).send({ statusText: err.response.statusText, detail: err.response.data.detail });
160
- })
161
- })
162
-
163
- // PROXY PUGLIA AI V2 - END
164
-
165
102
 
166
103
  // PROXY PUGLIA AI - START
167
104
  router.post('/qa', async (req, res) => {
168
105
  let data = req.body;
169
106
  winston.debug("/qa data: ", data);
170
- winston.info("/qa data: ", data);
171
107
 
172
108
  openaiService.ask(data).then((resp) => {
173
109
  winston.debug("qa resp: ", resp.data);
@@ -175,7 +111,6 @@ router.post('/qa', async (req, res) => {
175
111
  }).catch((err) => {
176
112
  winston.error("qa err: ", err);
177
113
  let status = err.response.status;
178
- winston.info("status on error: ", status)
179
114
  res.status(status).send({ statusText: err.response.statusText, detail: err.response.data.detail });
180
115
  })
181
116
  })
@@ -234,9 +169,9 @@ router.post('/checkstatus', async (req, res) => {
234
169
  }
235
170
 
236
171
 
237
- res.status(200).send(resp);
172
+ res.status(200).send(return_data);
238
173
  }).catch((err) => {
239
- winston.error("checkstatus err: ", err);
174
+ //winston.error("checkstatus err: ", err);
240
175
  let status = err.response.status;
241
176
  res.status(status).send({ statusText: err.response.statusText, detail: err.response.data.detail });
242
177
  })
@@ -254,14 +189,10 @@ router.post('/:settings_id', async (req, res) => {
254
189
  return res.status(500).send({ success: false, error: err});
255
190
  } else {
256
191
 
257
- let new_kb = new KB({
192
+ let new_kb = {
258
193
  name: body.name,
259
- url: body.url,
260
- source: body.source,
261
- type: body.type,
262
- content: body.content,
263
- namespace: body.namespace
264
- })
194
+ url: body.url
195
+ }
265
196
  settings.kbs.push(new_kb);
266
197
 
267
198
  KBSettings.findByIdAndUpdate( settings_id, settings, { new: true }, (err, savedSettings) => {
@@ -269,11 +200,12 @@ router.post('/:settings_id', async (req, res) => {
269
200
  winston.err("findByIdAndUpdate error: ", err);
270
201
  res.status(500).send({ success: false, error: err });
271
202
  } else {
272
- res.status(200).send(new_kb);
203
+ res.status(200).send(savedSettings);
273
204
  }
274
205
  })
275
206
  }
276
207
  })
277
208
  })
278
209
 
210
+
279
211
  module.exports = router;
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;