@tiledesk/tiledesk-tybot-connector 2.0.22 → 2.0.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-tybot-connector",
3
- "version": "2.0.22",
3
+ "version": "2.0.24",
4
4
  "description": "Tiledesk Tybot connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -0,0 +1,105 @@
1
+ const httpUtils = require('../utils/HttpUtils');
2
+ const winston = require('../utils/winston');
3
+ const API_ENDPOINT = process.env.API_ENDPOINT;
4
+
5
+ class KBService {
6
+
7
+ constructor() { }
8
+
9
+ async getNamespace(id_project, token, name, id) {
10
+ return new Promise((resolve) => {
11
+ const http_request = {
12
+ url: API_ENDPOINT + "/" + id_project + "/kb/namespace/all",
13
+ headers: {
14
+ 'Content-Type': 'application/json',
15
+ 'Authorization': 'JWT ' + token
16
+ },
17
+ method: "GET"
18
+ }
19
+ winston.debug("Kb HttpRequest", http_request);
20
+
21
+ httpUtils.request(
22
+ http_request, async (err, namespaces) => {
23
+ if (err) {
24
+ winston.error("Error getting namespaces:", err);
25
+ reject(err);
26
+ } else {
27
+ winston.debug("Get namespaces response:", namespaces);
28
+ if (!Array.isArray(namespaces)) {
29
+ reject(new Error('Invalid response format'));
30
+ return;
31
+ }
32
+
33
+ let namespace;
34
+ if (name) {
35
+ namespace = namespaces.find(n => n.name === name);
36
+ } else {
37
+ namespace = namespaces.find(n => n.id === id);
38
+ }
39
+ resolve(namespace || null);
40
+ }
41
+ }
42
+ )
43
+ })
44
+ }
45
+
46
+ async getKeyFromKbSettings(id_project, token) {
47
+
48
+ return new Promise((resolve) => {
49
+ const http_request = {
50
+ url: API_ENDPOINT + "/" + id_project + "/kbsettings",
51
+ headers: {
52
+ 'Content-Type': 'application/json',
53
+ 'Authorization': 'JWT ' + token
54
+ },
55
+ method: "GET"
56
+ }
57
+ winston.debug("Kb HttpRequest", http_request);
58
+
59
+ httpUtils.request(
60
+ http_request, async (err, resbody) => {
61
+ if (err) {
62
+ winston.error("Error getting kb settings:", err?.response?.data);
63
+ resolve(null);
64
+ } else {
65
+ if (!resbody || !resbody.gptkey) {
66
+ resolve(null);
67
+ } else {
68
+ resolve(resbody.gptkey);
69
+ }
70
+ }
71
+ }
72
+ )
73
+ })
74
+ }
75
+
76
+ async addUnansweredQuestion(id_project, namespace, question, token) {
77
+
78
+ const json = { namespace, question };
79
+
80
+ return new Promise((resolve, reject) => {
81
+ const http_request = {
82
+ url: API_ENDPOINT + "/" + id_project + "/kb/unanswered/",
83
+ headers: {
84
+ 'Content-Type': 'application/json',
85
+ 'Authorization': 'JWT ' + token
86
+ },
87
+ method: "POST",
88
+ json: json
89
+ }
90
+ winston.debug("Kb HttpRequest", http_request);
91
+
92
+ httpUtils.request(http_request, (err, response) => {
93
+ if (err) {
94
+ winston.error("Error adding unanswered question:", err);
95
+ reject(err);
96
+ } else {
97
+ resolve(response);
98
+ }
99
+ });
100
+ });
101
+ }
102
+ }
103
+
104
+ const kbService = new KBService();
105
+ module.exports = kbService;
@@ -0,0 +1,69 @@
1
+ const httpUtils = require('../utils/HttpUtils');
2
+ const winston = require('../utils/winston');
3
+ const API_ENDPOINT = process.env.API_ENDPOINT;
4
+
5
+ class QuotasService {
6
+
7
+ constructor() { }
8
+
9
+ async checkQuoteAvailability(id_project, token) {
10
+ return new Promise((resolve) => {
11
+
12
+ const http_request = {
13
+ url: API_ENDPOINT + "/" + id_project + "/quotes/tokens",
14
+ headers: {
15
+ 'Content-Type': 'application/json',
16
+ 'Authorization': 'JWT ' + token
17
+ },
18
+ method: "GET"
19
+ }
20
+ winston.debug("QuotasService HttpRequest", http_request);
21
+
22
+ httpUtils.request(
23
+ http_request, async (err, resbody) => {
24
+ if (err) {
25
+ winston.error("Check quote availability err: ", err);
26
+ resolve(true)
27
+ } else {
28
+ if (resbody.isAvailable === true) {
29
+ resolve(true)
30
+ } else {
31
+ resolve(false)
32
+ }
33
+ }
34
+ }
35
+ )
36
+ })
37
+ }
38
+
39
+ async updateQuote(id_project, token, tokens_usage) {
40
+ return new Promise((resolve, reject) => {
41
+
42
+ const http_request = {
43
+ url: API_ENDPOINT + "/" + id_project + "/quotes/incr/tokens",
44
+ headers: {
45
+ 'Content-Type': 'application/json',
46
+ 'Authorization': 'JWT ' + token
47
+ },
48
+ json: tokens_usage,
49
+ method: "POST"
50
+ }
51
+ winston.debug("DirAskGPTV2 update quote HttpRequest ", http_request);
52
+
53
+ httpUtils.request(
54
+ http_request, async (err, resbody) => {
55
+ if (err) {
56
+ winston.error("Increment tokens quote err: ", err);
57
+ reject(false)
58
+ } else {
59
+ resolve(true);
60
+ }
61
+ }
62
+ )
63
+ })
64
+ }
65
+
66
+ }
67
+
68
+ const quotasService = new QuotasService();
69
+ module.exports = quotasService;
@@ -11,6 +11,8 @@ const winston = require('../../utils/winston');
11
11
  const httpUtils = require("../../utils/HttpUtils");
12
12
  const integrationService = require("../../services/IntegrationService");
13
13
  const { Logger } = require("../../Logger");
14
+ const kbService = require("../../services/KBService");
15
+ const quotasService = require("../../services/QuotasService");
14
16
 
15
17
  class DirAskGPTV2 {
16
18
 
@@ -169,7 +171,7 @@ class DirAskGPTV2 {
169
171
  if (!key) {
170
172
  this.logger.native("[Ask Knowledge Base] OpenAI key not found in Integration. Using shared OpenAI key");
171
173
  winston.verbose("DirAskGPTV2 - Key not found in Integrations. Searching in kb settings...");
172
- key = await this.getKeyFromKbSettings();
174
+ key = await kbService.getKeyFromKbSettings(this.projectId, this.token);
173
175
  }
174
176
 
175
177
  if (!key) {
@@ -193,7 +195,7 @@ class DirAskGPTV2 {
193
195
  }
194
196
 
195
197
  if (publicKey === true && !chunks_only) {
196
- let keep_going = await this.checkQuoteAvailability();
198
+ let keep_going = await quotasService.checkQuoteAvailability(this.projectId, this.token);
197
199
  if (keep_going === false) {
198
200
  this.logger.warn("[Ask Knowledge Base] Tokens quota exceeded. Skip the action")
199
201
  winston.verbose("DirAskGPTV2 - Quota exceeded for tokens. Skip the action")
@@ -344,7 +346,9 @@ class DirAskGPTV2 {
344
346
  tokens: resbody.prompt_token_size,
345
347
  model: json.model
346
348
  }
347
- this.updateQuote(tokens_usage);
349
+ quotasService.updateQuote(this.projectId, this.token, tokens_usage).catch((err) => {
350
+ winston.error("Error updating quota: ", err);
351
+ })
348
352
  }
349
353
 
350
354
  if (trueIntent) {
@@ -357,6 +361,10 @@ class DirAskGPTV2 {
357
361
  }
358
362
  } else {
359
363
  await this.#assignAttributes(action, answer, source);
364
+ kbService.addUnansweredQuestion(this.projectId, json.namespace, json.question, this.token).catch((err) => {
365
+ winston.error("DirAskGPTV2 - Error adding unanswered question: ", err);
366
+ this.logger.warn("[Ask Knowledge Base] Unable to add unanswered question", json.question, "to namespacae", json.namespace);
367
+ })
360
368
  if (falseIntent) {
361
369
  await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
362
370
  callback(true);
@@ -427,93 +435,6 @@ class DirAskGPTV2 {
427
435
  }
428
436
  }
429
437
 
430
- async getKeyFromKbSettings() {
431
- return new Promise((resolve) => {
432
-
433
- const KB_HTTPREQUEST = {
434
- url: this.API_ENDPOINT + "/" + this.context.projectId + "/kbsettings",
435
- headers: {
436
- 'Content-Type': 'application/json',
437
- 'Authorization': 'JWT ' + this.context.token
438
- },
439
- method: "GET"
440
- }
441
- winston.debug("DirAskGPTV2 KB HttpRequest", KB_HTTPREQUEST);
442
-
443
- httpUtils.request(
444
- KB_HTTPREQUEST, async (err, resbody) => {
445
- if (err) {
446
- winston.error("DirAskGPTV2 Get kb settings error ", err?.response?.data);
447
- resolve(null);
448
- } else {
449
- if (!resbody.gptkey) {
450
- resolve(null);
451
- } else {
452
- resolve(resbody.gptkey);
453
- }
454
- }
455
- }
456
- )
457
- })
458
- }
459
-
460
- async checkQuoteAvailability() {
461
- return new Promise((resolve) => {
462
-
463
- const HTTPREQUEST = {
464
- url: this.API_ENDPOINT + "/" + this.context.projectId + "/quotes/tokens",
465
- headers: {
466
- 'Content-Type': 'application/json',
467
- 'Authorization': 'JWT ' + this.context.token
468
- },
469
- method: "GET"
470
- }
471
- winston.debug("DirAskGPTV2 check quote availability HttpRequest", HTTPREQUEST);
472
-
473
- httpUtils.request(
474
- HTTPREQUEST, async (err, resbody) => {
475
- if (err) {
476
- winston.error("DirAskGPTV2 Check quote availability err: ", err);
477
- resolve(true)
478
- } else {
479
- if (resbody.isAvailable === true) {
480
- resolve(true)
481
- } else {
482
- resolve(false)
483
- }
484
- }
485
- }
486
- )
487
- })
488
- }
489
-
490
- async updateQuote(tokens_usage) {
491
- return new Promise((resolve, reject) => {
492
-
493
- const HTTPREQUEST = {
494
- url: this.API_ENDPOINT + "/" + this.context.projectId + "/quotes/incr/tokens",
495
- headers: {
496
- 'Content-Type': 'application/json',
497
- 'Authorization': 'JWT ' + this.context.token
498
- },
499
- json: tokens_usage,
500
- method: "POST"
501
- }
502
- winston.debug("DirAskGPTV2 update quote HttpRequest ", HTTPREQUEST);
503
-
504
- httpUtils.request(
505
- HTTPREQUEST, async (err, resbody) => {
506
- if (err) {
507
- winston.error("DirAskGPTV2 Increment tokens quote err: ", err);
508
- reject(false)
509
- } else {
510
- resolve(true);
511
- }
512
- }
513
- )
514
- })
515
- }
516
-
517
438
  /**
518
439
  * Transforms the transcirpt array in a dictionary like '0': { "question": "xxx", "answer":"xxx"}
519
440
  * merging consecutive messages with the same role in a single question or answer.
@@ -599,7 +520,7 @@ class DirAskGPTV2 {
599
520
  return new Promise((resolve) => {
600
521
  let engine = {
601
522
  name: "pinecone",
602
- type: isHybrid ? "serverless" : "pod",
523
+ type: isHybrid ? "serverless" : process.env.PINECONE_TYPE,
603
524
  apikey: "",
604
525
  vector_size: 1536,
605
526
  index_name: isHybrid ? process.env.PINECONE_INDEX_HYBRID : process.env.PINECONE_INDEX
@@ -58,6 +58,7 @@ class DirReply {
58
58
  );
59
59
 
60
60
  TiledeskChatbotUtil.replaceJSONButtons(message, requestAttributes);
61
+ TiledeskChatbotUtil.replaceJSONGalleries(message, requestAttributes);
61
62
 
62
63
  const filler = new Filler();
63
64
  // fill text attribute
@@ -75,6 +75,7 @@ class DirReplyV2 {
75
75
  // }
76
76
 
77
77
  TiledeskChatbotUtil.replaceJSONButtons(message, requestAttributes);
78
+ TiledeskChatbotUtil.replaceJSONGalleries(message, requestAttributes);
78
79
 
79
80
  try {
80
81
  // lock/unlock + no-match
@@ -307,34 +307,195 @@ class TiledeskChatbotUtil {
307
307
  let command = commands[i];
308
308
  if (command.type === 'message' && command.message) {
309
309
  if (command.message.attributes && command.message.attributes.attachment && command.message.attributes.attachment.json_buttons){
310
- // console.log("command with buttons ok:")
311
310
  let json_buttons_string = command.message.attributes.attachment.json_buttons;
312
- let json_buttons = null;
313
- let final_buttons = [];
311
+ let final_buttons = this.renderJSONButtons(json_buttons_string, flow_attributes);
312
+ // let final_buttons = [];
313
+ // try {
314
+ // // fill buttons
315
+ // const filler = new Filler();
316
+ // json_buttons_string = filler.fill(json_buttons_string, flow_attributes);
317
+ // let json_buttons = JSON.parse(json_buttons_string);
318
+ // if (Array.isArray(json_buttons)) {
319
+ // json_buttons.forEach(button => {
320
+ // if (button.value && button.type === "action" && button.action) {
321
+ // button.show_echo = true;
322
+ // final_buttons.push(button);
323
+ // }
324
+ // else if (button.value && button.type === "text") {
325
+ // button.show_echo = true;
326
+ // final_buttons.push(button);
327
+ // }
328
+ // else if (button.value && button.type === "url" && button.link) {
329
+ // button.show_echo = true;
330
+ // final_buttons.push(button);
331
+ // }
332
+ // else {
333
+ // winston.verbose("Invalid button. Skipping:", button);
334
+ // }
335
+ // });
336
+ // }
337
+
338
+ // // "buttons": [
339
+ // // {
340
+ // // "type": "action",
341
+ // // "value": "Button1", // obbligatorio sempre
342
+ // // "action": "#bb347206-d639-4926-94c9-e94930623dce", // mandatory
343
+ // // "show_echo": true, // lo inserisco sempre
344
+ // // "alias": "button1 alias"
345
+ // // },
346
+ // // {
347
+ // // "type": "text",
348
+ // // "value": "Button2 text", // obbligatorio sempre
349
+ // // "show_echo": true // lo inserisco sempre
350
+ // // },
351
+ // // {
352
+ // // "type": "url",
353
+ // // "value": "Button3 link", // obbligatorio sempre
354
+ // // "link": "http://", // obbligatorio
355
+ // // "show_echo": true // lo inserisco sempre
356
+ // // }
357
+ // // ]
358
+ // }
359
+ // catch(error) {
360
+ // winston.warn("Invalid json_buttons:", error)
361
+ // }
362
+ if (final_buttons && final_buttons.length > 0) {
363
+ command.message.attributes.attachment.buttons = final_buttons;
364
+ delete command.message.attributes.attachment.json_buttons;
365
+ }
366
+ else {
367
+ winston.verbose("Invalid json_buttons. Skipping...")
368
+ }
369
+ }
370
+ }
371
+ }
372
+ }
373
+ }
374
+ return all_buttons;
375
+ }
376
+
377
+ static renderJSONButtons(json_buttons_string, flow_attributes) {
378
+ let final_buttons = [];
379
+ try {
380
+ // fill buttons
381
+ const filler = new Filler();
382
+ json_buttons_string = filler.fill(json_buttons_string, flow_attributes);
383
+ let json_buttons = JSON.parse(json_buttons_string);
384
+ if (Array.isArray(json_buttons)) {
385
+ json_buttons.forEach(button => {
386
+ if (button.value && button.type === "action" && button.action) {
387
+ button.show_echo = true;
388
+ final_buttons.push(button);
389
+ }
390
+ else if (button.value && button.type === "text") {
391
+ button.show_echo = true;
392
+ final_buttons.push(button);
393
+ }
394
+ else if (button.value && button.type === "url" && button.link) {
395
+ button.show_echo = true;
396
+ final_buttons.push(button);
397
+ }
398
+ else {
399
+ winston.verbose("Invalid button. Skipping:", button);
400
+ }
401
+ });
402
+ }
403
+
404
+ // "buttons": [
405
+ // {
406
+ // "type": "action",
407
+ // "value": "Button1", // obbligatorio sempre
408
+ // "action": "#bb347206-d639-4926-94c9-e94930623dce", // mandatory
409
+ // "show_echo": true, // lo inserisco sempre
410
+ // "alias": "button1 alias"
411
+ // },
412
+ // {
413
+ // "type": "text",
414
+ // "value": "Button2 text", // obbligatorio sempre
415
+ // "show_echo": true // lo inserisco sempre
416
+ // },
417
+ // {
418
+ // "type": "url",
419
+ // "value": "Button3 link", // obbligatorio sempre
420
+ // "link": "http://", // obbligatorio
421
+ // "show_echo": true // lo inserisco sempre
422
+ // }
423
+ // ]
424
+ }
425
+ catch(error) {
426
+ winston.warn("Invalid json_buttons:", error)
427
+ return null;
428
+ }
429
+ return final_buttons;
430
+ }
431
+
432
+ static replaceJSONGalleries(message, flow_attributes) {
433
+ if (message.attributes && message.attributes.commands) {
434
+ let commands = message.attributes.commands;
435
+ if (commands.length > 0) {
436
+ for (let i = 0; i < commands.length; i++) {
437
+ let command = commands[i];
438
+ if (command.type === 'message' && command.message) {
439
+ if (command.message.attributes && command.message.attributes.attachment && command.message.attributes.attachment.json_gallery){
440
+ let final_gallery = [];
314
441
  try {
315
- // fill buttons
442
+ // fill previews
316
443
  const filler = new Filler();
317
- json_buttons_string = filler.fill(json_buttons_string, flow_attributes);
318
- json_buttons = JSON.parse(json_buttons_string);
319
- if (Array.isArray(json_buttons)) {
320
- json_buttons.forEach(button => {
321
- if (button.value && button.type === "action" && button.action) {
322
- button.show_echo = true;
323
- final_buttons.push(button);
324
- }
325
- else if (button.value && button.type === "text") {
326
- button.show_echo = true;
327
- final_buttons.push(button);
328
- }
329
- else if (button.value && button.type === "url" && button.link) {
330
- button.show_echo = true;
331
- final_buttons.push(button);
332
- }
333
- else {
334
- winston.verbose("Invalid button. Skipping:", button);
444
+ let json_gallery_string = command.message.attributes.attachment.json_gallery;
445
+ json_gallery_string = filler.fill(json_gallery_string, flow_attributes);
446
+ let json_gallery = JSON.parse(json_gallery_string);
447
+ if (Array.isArray(json_gallery)) {
448
+ json_gallery.forEach(el => {
449
+ if (el.buttons) {
450
+ el.buttons = TiledeskChatbotUtil.renderJSONButtons(JSON.stringify(el.buttons));
335
451
  }
452
+ final_gallery.push(el);
336
453
  });
337
454
  }
455
+ else {
456
+ winston.verbose("Invalid json_gallery.");
457
+ }
458
+ // "gallery": [
459
+ // {
460
+ // "preview": {
461
+ // "src": "https://eu.rtmv3.tiledesk.com/api/images?path=uploads%2Fusers%2F63a05d755f117f0013541383%2Fimages%2F8913ff2c-d788-45e1-ac71-ee5bae8479e2%2Fhybrid-settings.png",
462
+ // "uid": "mcamfa6s"
463
+ // },
464
+ // "title": "Title 1",
465
+ // "description": "Description 1",
466
+ // "buttons": [
467
+ // {
468
+ // "uid": "0a956f4637584ee4862360c19a161f8f",
469
+ // "type": "url",
470
+ // "value": "Prod1",
471
+ // "link": "https://URL1",
472
+ // "target": "blank",
473
+ // "action": "",
474
+ // "attributes": "",
475
+ // "show_echo": true
476
+ // },
477
+ // {
478
+ // "uid": "4a87abe3d03a4b6fbdbc3fc33c4a8430",
479
+ // "type": "action",
480
+ // "value": "Prod1.1 (connector)",
481
+ // "link": "",
482
+ // "target": "blank",
483
+ // "action": "#0f7aaefd-3147-466b-82a4-06756f36eea5",
484
+ // "attributes": "",
485
+ // "show_echo": true
486
+ // },
487
+ // {
488
+ // "uid": "31fac2c82ce24da0a2e9850a32165fe8",
489
+ // "type": "text",
490
+ // "value": "Prod1.2 (text)",
491
+ // "link": "https://url2",
492
+ // "target": "blank",
493
+ // "action": "",
494
+ // "attributes": "",
495
+ // "show_echo": true
496
+ // }
497
+ // ]
498
+ // },
338
499
 
339
500
  // "buttons": [
340
501
  // {
@@ -358,21 +519,21 @@ class TiledeskChatbotUtil {
358
519
  // ]
359
520
  }
360
521
  catch(error) {
361
- winston.warn("Invalid json_buttons:", error)
522
+ winston.warn("Error on JSON gallery parsing:", error);
362
523
  }
363
- if (final_buttons && final_buttons.length > 0) {
364
- command.message.attributes.attachment.buttons = final_buttons;
365
- delete command.message.attributes.attachment.json_buttons;
524
+ if (final_gallery && final_gallery.length > 0) {
525
+ command.message.attributes.attachment.gallery = final_gallery;
526
+ delete command.message.attributes.attachment.json_gallery;
366
527
  }
367
528
  else {
368
- winston.verbose("Invalid json_buttons. Skipping...")
529
+ winston.verbose("Invalid JSON Gallery.")
369
530
  }
370
531
  }
371
532
  }
372
533
  }
373
534
  }
374
535
  }
375
- return all_buttons;
536
+ return message;
376
537
  }
377
538
 
378
539
  static buttonByText(text, buttons) {