@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,254 +1,289 @@
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
+ var that = this;
50
+ winston.debug('message.save called');
51
+
52
+ if (!message.createdAt) {
53
+ message.createdAt = new Date();
54
+ }
55
+
56
+ let sender = message.sender;
57
+ let senderFullname = message.senderFullname;
58
+ let recipient = message.recipient;
59
+ let recipientFullname = message.recipientFullname;
60
+ let text = message.text;
61
+ let id_project = message.id_project;
62
+ let createdBy = message.createdBy;
63
+ let status = message.status;
64
+ let attributes = message.attributes;
65
+ let type = message.type;
66
+ let metadata = message.metadata;
67
+ let language = message.language;
68
+ let channel_type = message.channel_type;
69
+ let channel = message.channel;
70
+
71
+
72
+
73
+ return new Promise(function (resolve, reject) {
74
+
75
+ //let q = Project.findOne({ _id: request.id_project, status: 100 });
76
+ // Continue quotes code here (see at requestService)
77
+
78
+ if (!createdBy) {
79
+ createdBy = sender;
94
80
  }
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) {
81
+
82
+ var beforeMessage = {
83
+ sender: sender, senderFullname: senderFullname
84
+ , recipient: recipient, recipientFullname: recipientFullname
85
+ , text: text, id_project: id_project, createdBy: createdBy, status: status, attributes: attributes,
86
+ type: type, metadata: metadata, language: language, channel_type: channel_type, channel: channel
87
+ };
88
+
89
+ var messageToCreate = beforeMessage;
90
+ winston.debug('messageToCreate before', messageToCreate);
91
+ // messageEvent.emit('message.create.simple.before', {beforeMessage:beforeMessage});
92
+
93
+
94
+
95
+ messagePromiseEvent.emit('message.create.simple.before', { beforeMessage: beforeMessage }).then(results => {
96
+ winston.debug('message.create.simple.before results', results);
97
+ winston.debug('message.create.simple.before results prototype: ' + Object.prototype.toString.call(results));
98
+
99
+ if (results) {
100
+ winston.debug('message.create.simple.before results.length: ' + results.length); //TODO ELIMINA DOPO CHE CREA CRASH
101
+ }
102
+
103
+ /*
104
+ if (results ) { //NN HA MAI FUNZIONATO. LA MADIFICA DEL VALORE AVVENIVA PER PUNTATORE
105
+ winston.info('message.create.simple.before results.beforeMessage', results[0].beforeMessage);
106
+ messageToCreate = results[0].beforeMessage;
107
+ }
108
+ */
109
+
110
+ winston.debug('messageToCreate', messageToCreate);
111
+
112
+
113
+ // if (id_project) {
114
+
115
+ var newMessage = new Message({
116
+ sender: messageToCreate.sender,
117
+ senderFullname: messageToCreate.senderFullname,
118
+ recipient: messageToCreate.recipient,
119
+ recipientFullname: messageToCreate.recipientFullname, //for direct
120
+ type: messageToCreate.type,
121
+ text: messageToCreate.text,
122
+ id_project: messageToCreate.id_project,
123
+ createdBy: messageToCreate.createdBy,
124
+ updatedBy: messageToCreate.createdBy,
125
+ status: messageToCreate.status,
126
+ metadata: messageToCreate.metadata,
127
+ attributes: messageToCreate.attributes,
128
+ language: messageToCreate.language,
129
+ channel_type: messageToCreate.channel_type,
130
+ channel: messageToCreate.channel
131
+ });
132
+
133
+ // winston.debug("create new message", newMessage);
134
+
135
+ return newMessage.save(function (err, savedMessage) {
136
+ if (err) {
137
+ winston.error("Error saving the message", { err: err, message: message, newMessage: newMessage });
138
+ return reject(err);
139
+ }
140
+ winston.verbose("Message created", savedMessage.toObject());
141
+
142
+ messageEvent.emit('message.create.simple', savedMessage);
143
+ that.emitMessage(savedMessage);
144
+
145
+ let q = Project.findOne({ _id: message.id_project, status: 100 });
146
+ if (cacheEnabler.project) {
147
+ q.cache(cacheUtil.longTTL, "projects:id:" + message.id_project) //project_cache
148
+ winston.debug('project cache enabled for /project detail');
149
+ }
150
+ q.exec(async function (err, p) {
123
151
  if (err) {
124
- winston.error("Error saving the message", {err:err, message: message, newMessage: newMessage});
125
- return reject(err);
152
+ winston.error('Error getting project ', err);
153
+ }
154
+ if (!p) {
155
+ winston.warn('Project not found ');
156
+ }
157
+ //TODO REMOVE settings from project
158
+ let payload = {
159
+ project: p,
160
+ message: message
126
161
  }
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);
162
+
163
+ messageEvent.emit('message.create.quote', payload);
142
164
  });
143
-
144
-
145
-
146
- });
147
165
 
148
-
149
-
150
166
 
151
- });
167
+ // if (savedMessage.status === MessageConstants.CHAT_MESSAGE_STATUS.RECEIVED) {
168
+ // messageEvent.emit('message.received.simple', savedMessage);
169
+ // }
152
170
 
153
- };
171
+ // if (savedMessage.status === MessageConstants.CHAT_MESSAGE_STATUS.SENDING) {
172
+ // messageEvent.emit('message.sending.simple', savedMessage);
173
+ // }
154
174
 
175
+ return resolve(savedMessage);
176
+ });
155
177
 
156
178
 
157
- emitMessage(message) {
158
- if (message.status === MessageConstants.CHAT_MESSAGE_STATUS.RECEIVED) {
159
- messageEvent.emit('message.received.simple', message);
160
- }
161
179
 
162
- if (message.status === MessageConstants.CHAT_MESSAGE_STATUS.SENDING) {
163
- messageEvent.emit('message.sending.simple', message);
164
- }
180
+ });
181
+
165
182
 
166
- if (message.status === MessageConstants.CHAT_MESSAGE_STATUS.SENT) {
167
- messageEvent.emit('message.sent.simple', message);
183
+
184
+
185
+ });
186
+
187
+ };
188
+
189
+
190
+
191
+ emitMessage(message) {
192
+ if (message.status === MessageConstants.CHAT_MESSAGE_STATUS.RECEIVED) {
193
+ messageEvent.emit('message.received.simple', message);
194
+ }
195
+
196
+ if (message.status === MessageConstants.CHAT_MESSAGE_STATUS.SENDING) {
197
+ messageEvent.emit('message.sending.simple', message);
198
+ }
199
+
200
+ if (message.status === MessageConstants.CHAT_MESSAGE_STATUS.SENT) {
201
+ messageEvent.emit('message.sent.simple', message);
202
+ }
203
+
204
+ if (message.status === MessageConstants.CHAT_MESSAGE_STATUS.DELIVERED) {
205
+ messageEvent.emit('message.delivered.simple', message);
206
+ }
168
207
  }
169
208
 
170
- if (message.status === MessageConstants.CHAT_MESSAGE_STATUS.DELIVERED) {
171
- messageEvent.emit('message.delivered.simple', message);
209
+ // TODO must update also message.attributes from chat21
210
+ // attento già scatta su chat21handler
211
+
212
+ changeStatus(message_id, newstatus) {
213
+ winston.debug("changeStatus. " + message_id + " " + newstatus);
214
+ var that = this;
215
+ return new Promise(function (resolve, reject) {
216
+ // winston.debug("request_id", request_id);
217
+ // winston.debug("newstatus", newstatus);
218
+
219
+ return Message.findByIdAndUpdate(message_id, { status: newstatus }, { new: true, upsert: false }, function (err, updatedMessage) {
220
+ if (err) {
221
+ winston.error(err);
222
+ return reject(err);
223
+ }
224
+ messageEvent.emit('message.update.simple', updatedMessage);
225
+ // winston.debug("updatedMessage", updatedMessage);
226
+
227
+ that.emitMessage(updatedMessage);
228
+ return resolve(updatedMessage);
229
+ });
230
+ });
231
+
172
232
  }
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
233
 
193
- that.emitMessage(updatedMessage);
194
- return resolve(updatedMessage);
195
- });
196
- });
197
234
 
198
- }
199
235
 
236
+ getTranscriptByRequestId(requestid, id_project) {
237
+ winston.debug("requestid", requestid);
238
+ winston.debug("id_project", id_project);
239
+ var that = this;
240
+ return new Promise(function (resolve, reject) {
241
+ return Message.find({ "recipient": requestid, id_project: id_project }).sort({ createdAt: 'asc' }).exec(function (err, messages) {
242
+ if (err) {
243
+ winston.error("Error getting the transcript", err);
244
+ return reject(err);
245
+ }
200
246
 
247
+ winston.debug("messages", messages);
201
248
 
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);
249
+ if (!messages) {
250
+ return resolve(messages);
251
+ }
214
252
 
215
- if(!messages){
216
- return resolve(messages);
217
- }
218
253
 
219
-
220
254
 
221
- var transcript = '';
222
- // messages.forEach(message => {
255
+ var transcript = '';
256
+ // messages.forEach(message => {
223
257
  for (var i = 0; i < messages.length; i++) {
224
258
  var message = messages[i];
225
259
  // winston.debug("message", message);
226
260
  // winston.debug("message.createdAt", message.createdAt);
227
-
228
261
 
229
- transcript = transcript +
262
+
263
+ transcript = transcript +
230
264
  message.createdAt.toLocaleString('it', { timeZone: 'UTC' }) +
231
- ' ' + message.senderFullname +
265
+ ' ' + message.senderFullname +
232
266
  ': ' + message.text;
233
267
 
234
- //not add line break for last message
235
- if (i<messages.length-1){
236
- transcript = transcript + '\r\n';
237
- }
268
+ //not add line break for last message
269
+ if (i < messages.length - 1) {
270
+ transcript = transcript + '\r\n';
271
+ }
238
272
 
239
- // winston.debug("transcript", transcript);
273
+ // winston.debug("transcript", transcript);
240
274
  }
241
- // });
275
+ // });
242
276
 
243
- // winston.debug("final transcript", transcript);
277
+ // winston.debug("final transcript", transcript);
244
278
 
245
- // each message in messages
246
- // p [#{message.createdAt.toLocaleString('it', { timeZone: 'UTC' })}] #{message.senderFullname}: #{message.text}
247
- resolve(transcript);
248
-
279
+ // each message in messages
280
+ // p [#{message.createdAt.toLocaleString('it', { timeZone: 'UTC' })}] #{message.senderFullname}: #{message.text}
281
+ resolve(transcript);
282
+
283
+ });
249
284
  });
250
- });
251
- }
285
+ }
286
+
252
287
 
253
288
 
254
289
 
@@ -257,7 +292,6 @@ class MessageService {
257
292
 
258
293
 
259
294
 
260
-
261
295
  }
262
296
 
263
297
 
@@ -1,17 +1,17 @@
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
 
11
11
  // OPEN AI
12
12
  completions(data, gptkey) {
13
13
 
14
- winston.debug("[OPENAI SERVICE] openai endpoint: ", openai_endpoint);
14
+ winston.debug("[OPENAI SERVICE] openai endpoint: " + openai_endpoint);
15
15
 
16
16
  return new Promise((resolve, reject) => {
17
17
 
@@ -36,7 +36,7 @@ class OpenaiService {
36
36
 
37
37
  // PUGLIA AI
38
38
  checkStatus(data) {
39
- winston.debug("[OPENAI SERVICE] kb endpoint: ", kb_endpoint);
39
+ winston.debug("[OPENAI SERVICE] kb endpoint: " + kb_endpoint);
40
40
 
41
41
  return new Promise((resolve, reject) => {
42
42
 
@@ -57,7 +57,7 @@ class OpenaiService {
57
57
  }
58
58
 
59
59
  startScrape(data) {
60
- winston.debug("[OPENAI SERVICE] kb endpoint: ", kb_endpoint);
60
+ winston.debug("[OPENAI SERVICE] kb endpoint: " + kb_endpoint);
61
61
 
62
62
  return new Promise((resolve, reject) => {
63
63
 
@@ -78,7 +78,7 @@ class OpenaiService {
78
78
  }
79
79
 
80
80
  ask(data) {
81
- winston.debug("[OPENAI SERVICE] kb endpoint: ", kb_endpoint);
81
+ winston.debug("[OPENAI SERVICE] kb endpoint: " + kb_endpoint);
82
82
 
83
83
  return new Promise((resolve, reject) => {
84
84
 
@@ -92,7 +92,6 @@ class OpenaiService {
92
92
  }).then((resbody) => {
93
93
  resolve(resbody);
94
94
  }).catch((err) => {
95
- console.log("err: ", err);
96
95
  reject(err);
97
96
  })
98
97
 
@@ -101,12 +100,12 @@ class OpenaiService {
101
100
 
102
101
  // PUGLIA AI V2
103
102
  singleScrape(data) {
104
- winston.debug("[OPENAI SERVICE] kb endpoint: ", kb_endpoint);
103
+ winston.debug("[OPENAI SERVICE] kb endpoint: " + kb_endpoint_dev);
105
104
 
106
105
  return new Promise((resolve, reject) => {
107
106
 
108
107
  axios({
109
- url: kb_endpoint + "/scrape/single",
108
+ url: kb_endpoint_dev + "/scrape/single",
110
109
  headers: {
111
110
  'Content-Type': 'application/json'
112
111
  },
@@ -122,12 +121,12 @@ class OpenaiService {
122
121
  }
123
122
 
124
123
  scrapeStatus(data) {
125
- winston.debug("[OPENAI SERVICE] kb endpoint: ", kb_endpoint);
124
+ winston.debug("[OPENAI SERVICE] kb endpoint: " + kb_endpoint_dev);
126
125
 
127
126
  return new Promise((resolve, reject) => {
128
127
 
129
128
  axios({
130
- url: kb_endpoint + "/scrape/status",
129
+ url: kb_endpoint_dev + "/scrape/status",
131
130
  headers: {
132
131
  'Content-Type': 'application/json'
133
132
  },
@@ -142,12 +141,12 @@ class OpenaiService {
142
141
  }
143
142
 
144
143
  askNamespace(data) {
145
- winston.debug("[OPENAI SERVICE] kb endpoint: ", kb_endpoint);
144
+ winston.debug("[OPENAI SERVICE] kb endpoint: " + kb_endpoint_dev);
146
145
 
147
146
  return new Promise((resolve, reject) => {
148
147
 
149
148
  axios({
150
- url: kb_endpoint + "/qa",
149
+ url: kb_endpoint_dev + "/qa",
151
150
  headers: {
152
151
  'Content-Type': 'application/json'
153
152
  },
@@ -162,14 +161,13 @@ class OpenaiService {
162
161
  })
163
162
  }
164
163
 
165
-
166
164
  deleteIndex(data) {
167
- winston.debug("[OPENAI SERVICE] kb endpoint: ", kb_endpoint);
165
+ winston.debug("[OPENAI SERVICE] kb endpoint: " + kb_endpoint_dev);
168
166
 
169
167
  return new Promise((resolve, reject) => {
170
168
 
171
169
  axios({
172
- url: kb_endpoint + "/delete/id",
170
+ url: kb_endpoint_dev + "/delete/id",
173
171
  headers: {
174
172
  'Content-Type': 'application/json'
175
173
  },
@@ -184,12 +182,12 @@ class OpenaiService {
184
182
  }
185
183
 
186
184
  deleteNamespace(data) {
187
- winston.debug("[OPENAI SERVICE] kb endpoint: ", kb_endpoint);
185
+ winston.debug("[OPENAI SERVICE] kb endpoint: " + kb_endpoint_dev);
188
186
 
189
187
  return new Promise((resolve, reject) => {
190
188
 
191
189
  axios({
192
- url: kb_endpoint + "/delete/namespace",
190
+ url: kb_endpoint_dev + "/delete/namespace",
193
191
  headers: {
194
192
  'Content-Type': 'application/json'
195
193
  },