@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.
@@ -1,254 +1,336 @@
1
1
  'use strict';
2
2
 
3
3
  var Message = require("../models/message");
4
+ var Project = require("../models/project");
4
5
  var MessageConstants = require("../models/messageConstants");
5
6
  const messageEvent = require('../event/messageEvent');
6
7
  const messagePromiseEvent = require('../event/messagePromiseEvent');
7
8
  var winston = require('../config/winston');
9
+ var cacheUtil = require("../utils/cacheUtil");
10
+ var cacheEnabler = require("../services/cacheEnabler");
8
11
 
9
12
  class MessageService {
10
13
 
11
14
 
12
- send(sender, senderFullname, recipient, text, id_project, createdBy, attributes, type, metadata, language) {
13
- return this.create(sender, senderFullname, recipient, text, id_project, createdBy, MessageConstants.CHAT_MESSAGE_STATUS.SENDING, attributes, type, metadata, language);
14
- }
15
+ send(sender, senderFullname, recipient, text, id_project, createdBy, attributes, type, metadata, language) {
16
+ return this.create(sender, senderFullname, recipient, text, id_project, createdBy, MessageConstants.CHAT_MESSAGE_STATUS.SENDING, attributes, type, metadata, language);
17
+ }
15
18
 
16
- upsert(id, sender, senderFullname, recipient, text, id_project, createdBy, status, attributes, type, metadata, language) {
17
- if (!id) {
18
- return this.create(sender, senderFullname, recipient, text, id_project, createdBy, status, attributes, type, metadata, language);
19
- } else {
20
- winston.debug("Message upsert changeStatus:"+ status);
19
+ upsert(id, sender, senderFullname, recipient, text, id_project, createdBy, status, attributes, type, metadata, language) {
20
+ if (!id) {
21
+ return this.create(sender, senderFullname, recipient, text, id_project, createdBy, status, attributes, type, metadata, language);
22
+ } else {
23
+ winston.debug("Message upsert changeStatus:" + status);
21
24
  return this.changeStatus(id, status);
22
- }
23
- }
24
-
25
- create(sender, senderFullname, recipient, text, id_project, createdBy, status, attributes, type, metadata, language, channel_type, channel) {
26
- let message = {
27
- sender: sender,
28
- senderFullname: senderFullname,
29
- recipient: recipient,
30
- text: text,
31
- id_project: id_project,
32
- createdBy: createdBy,
33
- status: status,
34
- attributes: attributes,
35
- type: type,
36
- metadata: metadata,
37
- language: language,
38
- channel_type: channel_type,
39
- channel: channel
40
- };
41
- return this.save(message);
42
- }
43
-
44
- save(message) {
45
- var that = this;
46
- winston.debug('message.save called');
47
-
48
- let sender = message.sender;
49
- let senderFullname = message.senderFullname;
50
- let recipient = message.recipient;
51
- let recipientFullname = message.recipientFullname;
52
- let text = message.text;
53
- let id_project = message.id_project;
54
- let createdBy = message.createdBy;
55
- let status = message.status;
56
- let attributes = message.attributes;
57
- let type = message.type;
58
- let metadata = message.metadata;
59
- let language = message.language;
60
- let channel_type = message.channel_type;
61
- let channel = message.channel;
62
-
63
-
64
-
65
- return new Promise(function (resolve, reject) {
66
-
67
- if (!createdBy) {
68
- createdBy = sender;
69
25
  }
70
-
71
- var beforeMessage = {sender:sender, senderFullname:senderFullname
72
- , recipient:recipient, recipientFullname: recipientFullname
73
- , text:text, id_project:id_project, createdBy:createdBy, status:status, attributes:attributes,
74
- type:type, metadata:metadata, language:language, channel_type: channel_type, channel: channel};
75
-
76
- var messageToCreate = beforeMessage;
77
- winston.debug('messageToCreate before',messageToCreate);
78
- // messageEvent.emit('message.create.simple.before', {beforeMessage:beforeMessage});
79
-
80
-
81
-
82
- messagePromiseEvent.emit('message.create.simple.before', {beforeMessage:beforeMessage}).then(results => {
83
- winston.debug('message.create.simple.before results', results);
84
- winston.debug('message.create.simple.before results prototype: ' + Object.prototype.toString.call(results));
85
-
86
- if (results) {
87
- winston.debug('message.create.simple.before results.length: '+ results.length); //TODO ELIMINA DOPO CHE CREA CRASH
88
- }
89
-
90
- /*
91
- if (results ) { //NN HA MAI FUNZIONATO. LA MADIFICA DEL VALORE AVVENIVA PER PUNTATORE
92
- winston.info('message.create.simple.before results.beforeMessage', results[0].beforeMessage);
93
- messageToCreate = results[0].beforeMessage;
26
+ }
27
+
28
+ create(sender, senderFullname, recipient, text, id_project, createdBy, status, attributes, type, metadata, language, channel_type, channel) {
29
+ let message = {
30
+ sender: sender,
31
+ senderFullname: senderFullname,
32
+ recipient: recipient,
33
+ text: text,
34
+ id_project: id_project,
35
+ createdBy: createdBy,
36
+ status: status,
37
+ attributes: attributes,
38
+ type: type,
39
+ metadata: metadata,
40
+ language: language,
41
+ channel_type: channel_type,
42
+ channel: channel
43
+ };
44
+ return this.save(message);
45
+ }
46
+
47
+ save(message) {
48
+
49
+ let total_1;
50
+ let total_e;
51
+ let total_1_end_e;
52
+ let total_2;
53
+
54
+ var that = this;
55
+ winston.debug('message.save called');
56
+
57
+ message.createdAt = new Date();
58
+
59
+ // let start1 = Date.now();
60
+ // let project;
61
+ // let payload;
62
+ // let q = Project.findOne({ _id: message.id_project, status: 100 });
63
+ // if (cacheEnabler.project) {
64
+ // q.cache(cacheUtil.longTTL, "projects:id:" + message.id_project) //project_cache
65
+ // winston.debug('project cache enabled for /project detail');
66
+ // }
67
+ // q.exec(async function (err, p) {
68
+ // if (err) {
69
+ // winston.error('Error getting project ', err);
70
+ // }
71
+ // if (!p) {
72
+ // winston.warn('Project not found ');
73
+ // }
74
+ // //TODO REMOVE settings from project
75
+ // project = p;
76
+ // payload = {
77
+ // project: project,
78
+ // message: message
79
+ // }
80
+
81
+ // let end1 = Date.now();
82
+ // total_1 = end1 - start1;
83
+
84
+ // let start_e = Date.now();
85
+ // let result = await messageEvent.emit('message.create.quote.before', payload);
86
+ // let end_e = Date.now();
87
+ // total_e = end_e - start_e;
88
+ // if (result === false) {
89
+ // winston.info("Messages limits reached for project " + project._id)
90
+ // // return false in the second phase
91
+ // }
92
+ // });
93
+
94
+ // let start2 = Date.now();
95
+ let sender = message.sender;
96
+ let senderFullname = message.senderFullname;
97
+ let recipient = message.recipient;
98
+ let recipientFullname = message.recipientFullname;
99
+ let text = message.text;
100
+ let id_project = message.id_project;
101
+ let createdBy = message.createdBy;
102
+ let status = message.status;
103
+ let attributes = message.attributes;
104
+ let type = message.type;
105
+ let metadata = message.metadata;
106
+ let language = message.language;
107
+ let channel_type = message.channel_type;
108
+ let channel = message.channel;
109
+
110
+
111
+
112
+ return new Promise(function (resolve, reject) {
113
+
114
+ if (!createdBy) {
115
+ createdBy = sender;
94
116
  }
95
- */
96
-
97
- winston.debug('messageToCreate', messageToCreate);
98
-
99
-
100
- // if (id_project) {
101
-
102
- var newMessage = new Message({
103
- sender: messageToCreate.sender,
104
- senderFullname: messageToCreate.senderFullname,
105
- recipient: messageToCreate.recipient,
106
- recipientFullname: messageToCreate.recipientFullname, //for direct
107
- type: messageToCreate.type,
108
- text: messageToCreate.text,
109
- id_project: messageToCreate.id_project,
110
- createdBy: messageToCreate.createdBy,
111
- updatedBy: messageToCreate.createdBy,
112
- status : messageToCreate.status,
113
- metadata: messageToCreate.metadata,
114
- attributes: messageToCreate.attributes,
115
- language: messageToCreate.language,
116
- channel_type: messageToCreate.channel_type,
117
- channel: messageToCreate.channel
118
- });
119
-
120
- // winston.debug("create new message", newMessage);
121
-
122
- return newMessage.save(function(err, savedMessage) {
117
+
118
+ var beforeMessage = {
119
+ sender: sender, senderFullname: senderFullname
120
+ , recipient: recipient, recipientFullname: recipientFullname
121
+ , text: text, id_project: id_project, createdBy: createdBy, status: status, attributes: attributes,
122
+ type: type, metadata: metadata, language: language, channel_type: channel_type, channel: channel
123
+ };
124
+
125
+ var messageToCreate = beforeMessage;
126
+ winston.debug('messageToCreate before', messageToCreate);
127
+ // messageEvent.emit('message.create.simple.before', {beforeMessage:beforeMessage});
128
+
129
+
130
+
131
+ messagePromiseEvent.emit('message.create.simple.before', { beforeMessage: beforeMessage }).then(results => {
132
+ winston.debug('message.create.simple.before results', results);
133
+ winston.debug('message.create.simple.before results prototype: ' + Object.prototype.toString.call(results));
134
+
135
+ if (results) {
136
+ winston.debug('message.create.simple.before results.length: ' + results.length); //TODO ELIMINA DOPO CHE CREA CRASH
137
+ }
138
+
139
+ /*
140
+ if (results ) { //NN HA MAI FUNZIONATO. LA MADIFICA DEL VALORE AVVENIVA PER PUNTATORE
141
+ winston.info('message.create.simple.before results.beforeMessage', results[0].beforeMessage);
142
+ messageToCreate = results[0].beforeMessage;
143
+ }
144
+ */
145
+
146
+ winston.debug('messageToCreate', messageToCreate);
147
+
148
+
149
+ // if (id_project) {
150
+
151
+ var newMessage = new Message({
152
+ sender: messageToCreate.sender,
153
+ senderFullname: messageToCreate.senderFullname,
154
+ recipient: messageToCreate.recipient,
155
+ recipientFullname: messageToCreate.recipientFullname, //for direct
156
+ type: messageToCreate.type,
157
+ text: messageToCreate.text,
158
+ id_project: messageToCreate.id_project,
159
+ createdBy: messageToCreate.createdBy,
160
+ updatedBy: messageToCreate.createdBy,
161
+ status: messageToCreate.status,
162
+ metadata: messageToCreate.metadata,
163
+ attributes: messageToCreate.attributes,
164
+ language: messageToCreate.language,
165
+ channel_type: messageToCreate.channel_type,
166
+ channel: messageToCreate.channel
167
+ });
168
+
169
+ // winston.debug("create new message", newMessage);
170
+
171
+ return newMessage.save(function (err, savedMessage) {
172
+ if (err) {
173
+ winston.error("Error saving the message", { err: err, message: message, newMessage: newMessage });
174
+ return reject(err);
175
+ }
176
+ winston.verbose("Message created", savedMessage.toObject());
177
+
178
+ messageEvent.emit('message.create.simple', savedMessage);
179
+ that.emitMessage(savedMessage);
180
+
181
+ let q = Project.findOne({ _id: message.id_project, status: 100 });
182
+ if (cacheEnabler.project) {
183
+ q.cache(cacheUtil.longTTL, "projects:id:" + message.id_project) //project_cache
184
+ winston.debug('project cache enabled for /project detail');
185
+ }
186
+ q.exec(async function (err, p) {
123
187
  if (err) {
124
- winston.error("Error saving the message", {err:err, message: message, newMessage: newMessage});
125
- return reject(err);
188
+ winston.error('Error getting project ', err);
189
+ }
190
+ if (!p) {
191
+ winston.warn('Project not found ');
192
+ }
193
+ //TODO REMOVE settings from project
194
+ let payload = {
195
+ project: p,
196
+ message: message
197
+ }
198
+
199
+ let result = await messageEvent.emit('message.create.quote', payload);
200
+ if (result === false) {
201
+ winston.info("Messages limits reached for project " + p._id)
202
+ // return false in the second phase
126
203
  }
127
- winston.verbose("Message created", savedMessage.toObject());
128
-
129
- messageEvent.emit('message.create.simple', savedMessage);
130
-
131
- that.emitMessage(savedMessage);
132
- // if (savedMessage.status === MessageConstants.CHAT_MESSAGE_STATUS.RECEIVED) {
133
- // messageEvent.emit('message.received.simple', savedMessage);
134
- // }
135
-
136
- // if (savedMessage.status === MessageConstants.CHAT_MESSAGE_STATUS.SENDING) {
137
- // messageEvent.emit('message.sending.simple', savedMessage);
138
- // }
139
-
140
-
141
- return resolve(savedMessage);
142
204
  });
143
-
144
-
145
-
146
- });
147
205
 
148
-
149
-
150
206
 
151
- });
207
+ // if (savedMessage.status === MessageConstants.CHAT_MESSAGE_STATUS.RECEIVED) {
208
+ // messageEvent.emit('message.received.simple', savedMessage);
209
+ // }
152
210
 
153
- };
211
+ // if (savedMessage.status === MessageConstants.CHAT_MESSAGE_STATUS.SENDING) {
212
+ // messageEvent.emit('message.sending.simple', savedMessage);
213
+ // }
154
214
 
215
+ // console.log("total 1: ", total_1);
216
+ // console.log("total e: ", total_e);
217
+ // total_1_end_e = total_1 + total_e;
218
+ // console.log("total 1 + e: ", total_1_end_e);
219
+ // console.log("total 2: ", total_2);
155
220
 
156
221
 
157
- emitMessage(message) {
158
- if (message.status === MessageConstants.CHAT_MESSAGE_STATUS.RECEIVED) {
159
- messageEvent.emit('message.received.simple', message);
160
- }
222
+ return resolve(savedMessage);
223
+ });
224
+
225
+
226
+
227
+ });
161
228
 
162
- if (message.status === MessageConstants.CHAT_MESSAGE_STATUS.SENDING) {
163
- messageEvent.emit('message.sending.simple', message);
164
- }
165
229
 
166
- if (message.status === MessageConstants.CHAT_MESSAGE_STATUS.SENT) {
167
- messageEvent.emit('message.sent.simple', message);
230
+
231
+
232
+ });
233
+
234
+ };
235
+
236
+
237
+
238
+ emitMessage(message) {
239
+ if (message.status === MessageConstants.CHAT_MESSAGE_STATUS.RECEIVED) {
240
+ messageEvent.emit('message.received.simple', message);
241
+ }
242
+
243
+ if (message.status === MessageConstants.CHAT_MESSAGE_STATUS.SENDING) {
244
+ messageEvent.emit('message.sending.simple', message);
245
+ }
246
+
247
+ if (message.status === MessageConstants.CHAT_MESSAGE_STATUS.SENT) {
248
+ messageEvent.emit('message.sent.simple', message);
249
+ }
250
+
251
+ if (message.status === MessageConstants.CHAT_MESSAGE_STATUS.DELIVERED) {
252
+ messageEvent.emit('message.delivered.simple', message);
253
+ }
168
254
  }
169
255
 
170
- if (message.status === MessageConstants.CHAT_MESSAGE_STATUS.DELIVERED) {
171
- messageEvent.emit('message.delivered.simple', message);
256
+ // TODO must update also message.attributes from chat21
257
+ // attento già scatta su chat21handler
258
+
259
+ changeStatus(message_id, newstatus) {
260
+ winston.debug("changeStatus. " + message_id + " " + newstatus);
261
+ var that = this;
262
+ return new Promise(function (resolve, reject) {
263
+ // winston.debug("request_id", request_id);
264
+ // winston.debug("newstatus", newstatus);
265
+
266
+ return Message.findByIdAndUpdate(message_id, { status: newstatus }, { new: true, upsert: false }, function (err, updatedMessage) {
267
+ if (err) {
268
+ winston.error(err);
269
+ return reject(err);
270
+ }
271
+ messageEvent.emit('message.update.simple', updatedMessage);
272
+ // winston.debug("updatedMessage", updatedMessage);
273
+
274
+ that.emitMessage(updatedMessage);
275
+ return resolve(updatedMessage);
276
+ });
277
+ });
278
+
172
279
  }
173
- }
174
-
175
- // TODO must update also message.attributes from chat21
176
- // attento già scatta su chat21handler
177
-
178
- changeStatus(message_id, newstatus) {
179
- winston.debug("changeStatus. "+message_id + " "+ newstatus);
180
- var that = this;
181
- return new Promise(function (resolve, reject) {
182
- // winston.debug("request_id", request_id);
183
- // winston.debug("newstatus", newstatus);
184
-
185
- return Message.findByIdAndUpdate(message_id, {status: newstatus}, {new: true, upsert:false}, function(err, updatedMessage) {
186
- if (err) {
187
- winston.error(err);
188
- return reject(err);
189
- }
190
- messageEvent.emit('message.update.simple',updatedMessage);
191
- // winston.debug("updatedMessage", updatedMessage);
192
280
 
193
- that.emitMessage(updatedMessage);
194
- return resolve(updatedMessage);
195
- });
196
- });
197
281
 
198
- }
199
282
 
283
+ getTranscriptByRequestId(requestid, id_project) {
284
+ winston.debug("requestid", requestid);
285
+ winston.debug("id_project", id_project);
286
+ var that = this;
287
+ return new Promise(function (resolve, reject) {
288
+ return Message.find({ "recipient": requestid, id_project: id_project }).sort({ createdAt: 'asc' }).exec(function (err, messages) {
289
+ if (err) {
290
+ winston.error("Error getting the transcript", err);
291
+ return reject(err);
292
+ }
200
293
 
294
+ winston.debug("messages", messages);
201
295
 
202
- getTranscriptByRequestId(requestid, id_project) {
203
- winston.debug("requestid", requestid);
204
- winston.debug("id_project", id_project);
205
- var that = this;
206
- return new Promise(function (resolve, reject) {
207
- return Message.find({"recipient": requestid, id_project: id_project}).sort({createdAt: 'asc'}).exec(function(err, messages) {
208
- if (err) {
209
- winston.error("Error getting the transcript", err);
210
- return reject(err);
211
- }
212
-
213
- winston.debug("messages", messages);
296
+ if (!messages) {
297
+ return resolve(messages);
298
+ }
214
299
 
215
- if(!messages){
216
- return resolve(messages);
217
- }
218
300
 
219
-
220
301
 
221
- var transcript = '';
222
- // messages.forEach(message => {
302
+ var transcript = '';
303
+ // messages.forEach(message => {
223
304
  for (var i = 0; i < messages.length; i++) {
224
305
  var message = messages[i];
225
306
  // winston.debug("message", message);
226
307
  // winston.debug("message.createdAt", message.createdAt);
227
-
228
308
 
229
- transcript = transcript +
309
+
310
+ transcript = transcript +
230
311
  message.createdAt.toLocaleString('it', { timeZone: 'UTC' }) +
231
- ' ' + message.senderFullname +
312
+ ' ' + message.senderFullname +
232
313
  ': ' + message.text;
233
314
 
234
- //not add line break for last message
235
- if (i<messages.length-1){
236
- transcript = transcript + '\r\n';
237
- }
315
+ //not add line break for last message
316
+ if (i < messages.length - 1) {
317
+ transcript = transcript + '\r\n';
318
+ }
238
319
 
239
- // winston.debug("transcript", transcript);
320
+ // winston.debug("transcript", transcript);
240
321
  }
241
- // });
322
+ // });
242
323
 
243
- // winston.debug("final transcript", transcript);
324
+ // winston.debug("final transcript", transcript);
244
325
 
245
- // each message in messages
246
- // p [#{message.createdAt.toLocaleString('it', { timeZone: 'UTC' })}] #{message.senderFullname}: #{message.text}
247
- resolve(transcript);
248
-
326
+ // each message in messages
327
+ // p [#{message.createdAt.toLocaleString('it', { timeZone: 'UTC' })}] #{message.senderFullname}: #{message.text}
328
+ resolve(transcript);
329
+
330
+ });
249
331
  });
250
- });
251
- }
332
+ }
333
+
252
334
 
253
335
 
254
336
 
@@ -257,7 +339,6 @@ class MessageService {
257
339
 
258
340
 
259
341
 
260
-
261
342
  }
262
343
 
263
344
 
@@ -1,10 +1,10 @@
1
1
  var winston = require('../config/winston');
2
2
  const axios = require("axios").default;
3
- var configGlobal = require('../config/global');
4
3
  require('dotenv').config();
5
4
 
6
5
  let openai_endpoint = process.env.OPENAI_ENDPOINT;
7
6
  let kb_endpoint = process.env.KB_ENDPOINT;
7
+ let kb_endpoint_dev = process.env.KB_ENDPOINT_DEV;
8
8
 
9
9
  class OpenaiService {
10
10
 
@@ -50,6 +50,7 @@ class OpenaiService {
50
50
  }).then((resbody) => {
51
51
  resolve(resbody);
52
52
  }).catch((err) => {
53
+ console.log("checkStatus err: ", err)
53
54
  reject(err);
54
55
  })
55
56
 
@@ -101,12 +102,12 @@ class OpenaiService {
101
102
 
102
103
  // PUGLIA AI V2
103
104
  singleScrape(data) {
104
- winston.debug("[OPENAI SERVICE] kb endpoint: ", kb_endpoint);
105
+ winston.debug("[OPENAI SERVICE] kb endpoint: ", kb_endpoint_dev);
105
106
 
106
107
  return new Promise((resolve, reject) => {
107
108
 
108
109
  axios({
109
- url: kb_endpoint + "/scrape/single",
110
+ url: kb_endpoint_dev + "/scrape/single",
110
111
  headers: {
111
112
  'Content-Type': 'application/json'
112
113
  },
@@ -122,12 +123,12 @@ class OpenaiService {
122
123
  }
123
124
 
124
125
  scrapeStatus(data) {
125
- winston.debug("[OPENAI SERVICE] kb endpoint: ", kb_endpoint);
126
+ winston.debug("[OPENAI SERVICE] kb endpoint: ", kb_endpoint_dev);
126
127
 
127
128
  return new Promise((resolve, reject) => {
128
129
 
129
130
  axios({
130
- url: kb_endpoint + "/scrape/status",
131
+ url: kb_endpoint_dev + "/scrape/status",
131
132
  headers: {
132
133
  'Content-Type': 'application/json'
133
134
  },
@@ -142,12 +143,12 @@ class OpenaiService {
142
143
  }
143
144
 
144
145
  askNamespace(data) {
145
- winston.debug("[OPENAI SERVICE] kb endpoint: ", kb_endpoint);
146
+ winston.debug("[OPENAI SERVICE] kb endpoint: ", kb_endpoint_dev);
146
147
 
147
148
  return new Promise((resolve, reject) => {
148
149
 
149
150
  axios({
150
- url: kb_endpoint + "/qa",
151
+ url: kb_endpoint_dev + "/qa",
151
152
  headers: {
152
153
  'Content-Type': 'application/json'
153
154
  },
@@ -162,14 +163,13 @@ class OpenaiService {
162
163
  })
163
164
  }
164
165
 
165
-
166
166
  deleteIndex(data) {
167
- winston.debug("[OPENAI SERVICE] kb endpoint: ", kb_endpoint);
167
+ winston.debug("[OPENAI SERVICE] kb endpoint: ", kb_endpoint_dev);
168
168
 
169
169
  return new Promise((resolve, reject) => {
170
170
 
171
171
  axios({
172
- url: kb_endpoint + "/delete/id",
172
+ url: kb_endpoint_dev + "/delete/id",
173
173
  headers: {
174
174
  'Content-Type': 'application/json'
175
175
  },
@@ -184,12 +184,12 @@ class OpenaiService {
184
184
  }
185
185
 
186
186
  deleteNamespace(data) {
187
- winston.debug("[OPENAI SERVICE] kb endpoint: ", kb_endpoint);
187
+ winston.debug("[OPENAI SERVICE] kb endpoint: ", kb_endpoint_dev);
188
188
 
189
189
  return new Promise((resolve, reject) => {
190
190
 
191
191
  axios({
192
- url: kb_endpoint + "/delete/namespace",
192
+ url: kb_endpoint_dev + "/delete/namespace",
193
193
  headers: {
194
194
  'Content-Type': 'application/json'
195
195
  },