@tiledesk/tiledesk-server 2.4.99 → 2.4.101

Sign up to get free protection for your applications and to get access to all the features.
package/routes/kb.js ADDED
@@ -0,0 +1,353 @@
1
+ var express = require('express');
2
+ var { KB } = require('../models/kb_setting');
3
+ var router = express.Router();
4
+ var winston = require('../config/winston');
5
+ const openaiService = require('../services/openaiService');
6
+
7
+
8
+ router.get('/', async (req, res) => {
9
+
10
+ let project_id = req.projectid;
11
+
12
+ KB.find({ id_project: project_id }, (err, kbs) => {
13
+ if (err) {
14
+ winston.error("Find all kbs error: ", err);
15
+ return res.status(500).send({ success: false, error: err });
16
+ }
17
+
18
+ winston.debug("KBs found: ", kbs);
19
+ return res.status(200).send(kbs);
20
+ })
21
+ })
22
+
23
+ router.get('/:kb_id', async (req, res) => {
24
+
25
+ let kb_id = req.params.kb_id;
26
+
27
+ KB.findById(kb_id, (err, kb) => {
28
+ if (err) {
29
+ winston.error("Find kb by id error: ", err);
30
+ return res.status(500).send({ success: false, error: err });
31
+ }
32
+
33
+ return res.status(200).send(kb);
34
+ })
35
+ })
36
+
37
+ router.post('/', async (req, res) => {
38
+
39
+
40
+ let project_id = req.projectid;
41
+ let body = req.body;
42
+ console.log("create new kb project_id " + project_id);
43
+ console.log("create new kb body ", body);
44
+
45
+ let new_kb = {
46
+ id_project: project_id,
47
+ name: body.name,
48
+ type: body.type,
49
+ source: body.source,
50
+ content: body.content,
51
+ namespace: body.namespace
52
+ }
53
+ if (!new_kb.namespace) {
54
+ new_kb.namespace = project_id;
55
+ }
56
+ winston.debug("adding kb: ", new_kb);
57
+
58
+
59
+ KB.findOneAndUpdate({ id_project: project_id, type: 'url', source: new_kb.source }, new_kb, { upsert: true, new: true, rawResult: true }, async (err, raw) => {
60
+ if (err) {
61
+ winston.error("findOneAndUpdate with upsert error: ", err);
62
+ res.status(500).send({ success: false, error: err });
63
+ }
64
+ else {
65
+
66
+ res.status(200).send(raw);
67
+
68
+ let json = {
69
+ id: raw.value._id,
70
+ type: raw.value.type,
71
+ source: raw.value.source,
72
+ content: "",
73
+ namespace: raw.value.namespace
74
+ }
75
+
76
+ if (raw.value.content) {
77
+ json.content = raw.value.content;
78
+ }
79
+
80
+ startScrape(json).then((response) => {
81
+ winston.verbose("startScrape response: ", response);
82
+ }).catch((err) => {
83
+ winston.error("startScrape err: ", err);
84
+ })
85
+
86
+ }
87
+ })
88
+
89
+ })
90
+
91
+ router.put('/:kb_id', async (req, res) => {
92
+
93
+ let kb_id = req.params.kb_id;
94
+ winston.verbose("update kb_id " + kb_id);
95
+
96
+ let update = {};
97
+
98
+ if (req.body.name != undefined) {
99
+ update.name = req.body.name;
100
+ }
101
+
102
+ if (req.body.status != undefined) {
103
+ update.status = req.body.status;
104
+ }
105
+
106
+ update.updatedAt = new Date();
107
+ winston.debug("kb update: ", update);
108
+
109
+ KB.findByIdAndUpdate(kb_id, update, { new: true }, (err, savedKb) => {
110
+
111
+ if (err) {
112
+ winston.error("KB findByIdAndUpdate error: ", err);
113
+ return res.status(500).send({ success: false, error: err });
114
+ }
115
+
116
+ if (!savedKb) {
117
+ winston.debug("Try to updating a non-existing kb");
118
+ return res.status(400).send({ success: false, message: "Content not found" })
119
+ }
120
+
121
+ res.status(200).send(savedKb)
122
+ })
123
+
124
+ })
125
+
126
+
127
+ // PROXY PUGLIA AI V2 - START
128
+ router.post('/scrape/single', async (req, res) => {
129
+
130
+ let data = req.body;
131
+ winston.debug("/scrape/single data: ", data);
132
+
133
+ KB.findById(data.id, (err, kb) => {
134
+ if (err) {
135
+ winston.error("findById error: ", err);
136
+ return res.status(500).send({ success: false, error: err });
137
+ }
138
+
139
+ else if (!kb) {
140
+ return res.status(404).send({ success: false, error: "Unable to find the kb requested" })
141
+ }
142
+ else {
143
+
144
+ let json = {
145
+ id: kb._id,
146
+ type: kb.type,
147
+ source: kb.source,
148
+ content: kb.content,
149
+ namespace: kb.namespace
150
+ }
151
+
152
+ startScrape(json).then((response) => {
153
+ winston.verbose("startScrape response: ", response);
154
+ res.status(200).send(response);
155
+ }).catch((err) => {
156
+ winston.error("startScrape err: ", err);
157
+ res.status(500).send({ success: false, error: err });
158
+ })
159
+
160
+ }
161
+ })
162
+
163
+ })
164
+
165
+ router.post('/scrape/status', async (req, res) => {
166
+
167
+ let data = req.body;
168
+ winston.debug("/scrapeStatus req.body: ", req.body);
169
+
170
+ let returnObject = false;
171
+
172
+ if (req.query &&
173
+ req.query.returnObject &&
174
+ (req.query.returnObject === true || req.query.returnObject === true)) {
175
+ returnObject = true;
176
+ }
177
+
178
+ openaiService.scrapeStatus(data).then((response) => {
179
+
180
+ winston.debug("scrapeStatus response.data: ", response.data);
181
+
182
+ let update = {};
183
+
184
+ if (response.data.status_code) {
185
+ update.status = response.data.status_code;
186
+ }
187
+
188
+ KB.findByIdAndUpdate(data.id, update, { new: true }, (err, savedKb) => {
189
+
190
+ if (err) {
191
+ winston.verbose("Status was successfully recovered, but the update on the db failed");
192
+
193
+ if (returnObject) {
194
+ return res.status(206).send({ warning: "Unable to udpate content on db", message: "The original reply was forwarded", data: response.data });
195
+ } else {
196
+ return res.status(200).send(response.data);
197
+ }
198
+ }
199
+
200
+ if (returnObject) {
201
+ return res.status(200).send(savedKb);
202
+ } else {
203
+ return res.status(200).send(response.data);
204
+ }
205
+ })
206
+
207
+ }).catch((err) => {
208
+ winston.error("scrapeStatus err: ", err);
209
+ let status = err.response.status;
210
+ res.status(status).send({ statusText: err.response.statusText, error: err.response.data.detail });
211
+ })
212
+ })
213
+
214
+ router.post('/qa', async (req, res) => {
215
+ let data = req.body;
216
+ winston.debug("/qa data: ", data);
217
+
218
+ if (!data.gptkey) {
219
+ let gptkey = process.env.GPTKEY;
220
+ if (!gptkey) {
221
+ return res.status(403).send({ success: false, error: "GPT apikey undefined" })
222
+ }
223
+ data.gptkey = gptkey;
224
+ }
225
+
226
+ openaiService.askNamespace(data).then((resp) => {
227
+ winston.debug("qa resp: ", resp.data);
228
+ res.status(200).send(resp.data);
229
+ }).catch((err) => {
230
+ winston.error("qa err: ", err);
231
+ let status = err.response.status;
232
+ res.status(status).send({ success: false, statusText: err.response.statusText, error: err.response.data.detail });
233
+ })
234
+ })
235
+
236
+ router.delete('/delete', async (req, res) => {
237
+
238
+ let data = req.body;
239
+ winston.debug("/delete data: ", data);
240
+
241
+ openaiService.deleteIndex(data).then((resp) => {
242
+ winston.debug("delete resp: ", resp.data);
243
+ res.status(200).send(resp.data);
244
+ }).catch((err) => {
245
+ winston.error("delete err: ", err);
246
+ let status = err.response.status;
247
+ res.status(status).send({ statusText: err.response.statusText, error: err.response.data.detail });
248
+ })
249
+
250
+ })
251
+
252
+ router.delete('/deleteall', async (req, res) => {
253
+
254
+ let data = req.body;
255
+ winston.debug('/delete all data: ', data);
256
+
257
+ openaiService.deleteNamespace(data).then((resp) => {
258
+ winston.debug("delete namespace resp: ", resp.data);
259
+ res.status(200).send(resp.data);
260
+ }).catch((err) => {
261
+ winston.error("delete namespace err: ", err);
262
+ let status = err.response.status;
263
+ res.status(status).send({ statusText: err.response.statusText, error: err.response.data.detail });
264
+ })
265
+ })
266
+ // PROXY PUGLIA AI V2 - END
267
+
268
+ router.delete('/:kb_id', async (req, res) => {
269
+
270
+ let project_id = req.projectid;
271
+ let kb_id = req.params.kb_id;
272
+ winston.verbose("delete kb_id " + kb_id);
273
+
274
+ let data = {
275
+ id: kb_id,
276
+ namespace: project_id
277
+ }
278
+
279
+ openaiService.deleteIndex(data).then((resp) => {
280
+ winston.debug("delete resp: ", resp.data);
281
+
282
+ if (resp.data.success === true) {
283
+ KB.findByIdAndDelete(kb_id, (err, deletedKb) => {
284
+
285
+ if (err) {
286
+ winston.error("Delete kb error: ", err);
287
+ return res.status(500).send({ success: false, error: err });
288
+ }
289
+ res.status(200).send(deletedKb);
290
+ })
291
+
292
+ } else {
293
+ winston.info("resp.data: ", resp.data);
294
+
295
+ KB.findOneAndDelete({ _id: kb_id, status: { $in: [-1, 3, 4] } }, (err, deletedKb) => {
296
+ if (err) {
297
+ winston.error("findOneAndDelete err: ", err);
298
+ return res.status(500).send({ success: false, error: "Unable to delete the content due to an error" })
299
+ }
300
+ else if (!deletedKb) {
301
+ winston.verbose("Unable to delete the content in indexing status")
302
+ return res.status(500).send({ success: false, error: "Unable to delete the content in indexing status" })
303
+ } else {
304
+ res.status(200).send(deletedKb);
305
+ }
306
+ })
307
+ }
308
+
309
+ }).catch((err) => {
310
+ let status = err.response.status;
311
+ res.status(status).send({ success: false, statusText: err.response.statusText, error: err.response.data.detail });
312
+ })
313
+
314
+ })
315
+
316
+ async function updateStatus(id, status) {
317
+ return new Promise((resolve) => {
318
+
319
+ KB.findByIdAndUpdate(id, { status: status }, (err, updatedKb) => {
320
+ if (err) {
321
+ resolve(false)
322
+ } else {
323
+ winston.debug("updatedKb: ", updatedKb)
324
+ resolve(true);
325
+ }
326
+ })
327
+ })
328
+ }
329
+
330
+ async function startScrape(data) {
331
+
332
+ if (!data.gptkey) {
333
+ let gptkey = process.env.GPTKEY;
334
+ if (!gptkey) {
335
+ return { error: "GPT apikey undefined" }
336
+ }
337
+ data.gptkey = gptkey;
338
+ }
339
+
340
+ return new Promise((resolve, reject) => {
341
+ openaiService.singleScrape(data).then(async (resp) => {
342
+ winston.debug("singleScrape resp: ", resp.data);
343
+ let status_updated = await updateStatus(data.id, 0);
344
+ winston.verbose("status of kb " + data.id + " updated: " + status_updated);
345
+ resolve(resp.data);
346
+ }).catch((err) => {
347
+ winston.error("singleScrape err: ", err);
348
+ reject(err);
349
+ })
350
+ })
351
+ }
352
+
353
+ module.exports = router;
@@ -1,5 +1,6 @@
1
1
  var express = require('express');
2
- var KBSettings = require('../models/kb_setting');
2
+ var { KBSettings } = require('../models/kb_setting');
3
+ // var { KB } = require('../models/kb_setting');
3
4
  // var KB = require('../models/kb_setting')
4
5
  var router = express.Router();
5
6
  var winston = require('../config/winston');
@@ -132,7 +133,7 @@ router.post('/startscrape', async (req, res) => {
132
133
 
133
134
  router.post('/checkstatus', async (req, res) => {
134
135
 
135
- // let data = req.body;
136
+ //let data = req.body;
136
137
  winston.debug("/checkstatus req.body: ", req.body);
137
138
 
138
139
  let full_url = req.body.full_url;
@@ -141,7 +142,7 @@ router.post('/checkstatus', async (req, res) => {
141
142
  }
142
143
 
143
144
  openaiService.checkStatus(data).then((resp) => {
144
- winston.debug("checkStatus resp: ", resp.data);
145
+ winston.debug("checkStatus resp: ", resp);
145
146
  winston.debug("checkStatus resp: ", resp.data);
146
147
  winston.debug("checkStatus resp: ", resp.data[full_url]);
147
148
 
@@ -170,7 +171,7 @@ router.post('/checkstatus', async (req, res) => {
170
171
 
171
172
  res.status(200).send(return_data);
172
173
  }).catch((err) => {
173
- winston.error("checkstatus err: ", err);
174
+ //winston.error("checkstatus err: ", err);
174
175
  let status = err.response.status;
175
176
  res.status(status).send({ statusText: err.response.statusText, detail: err.response.data.detail });
176
177
  })
@@ -206,4 +207,5 @@ router.post('/:settings_id', async (req, res) => {
206
207
  })
207
208
  })
208
209
 
210
+
209
211
  module.exports = router;
package/routes/openai.js CHANGED
@@ -1,30 +1,46 @@
1
1
  var express = require('express');
2
2
  var router = express.Router();
3
- var KBSettings = require('../models/kb_setting');
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;