@tiledesk/tiledesk-tybot-connector 2.0.21-rc1 → 2.0.21-rc10

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.21-rc1",
3
+ "version": "2.0.21-rc10",
4
4
  "description": "Tiledesk Tybot connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -2,7 +2,7 @@ const httpUtils = require('../utils/HttpUtils');
2
2
  const winston = require('../utils/winston');
3
3
  const API_ENDPOINT = process.env.API_ENDPOINT;
4
4
 
5
- class KbService {
5
+ class LLMService {
6
6
 
7
7
  constructor() { }
8
8
 
@@ -45,7 +45,7 @@ class KbService {
45
45
 
46
46
  async getKeyFromKbSettings(id_project, token) {
47
47
 
48
- return new Promise((resolve, reject) => {
48
+ return new Promise((resolve) => {
49
49
  const http_request = {
50
50
  url: API_ENDPOINT + "/" + id_project + "/kbsettings",
51
51
  headers: {
@@ -59,8 +59,8 @@ class KbService {
59
59
  httpUtils.request(
60
60
  http_request, async (err, resbody) => {
61
61
  if (err) {
62
- winston.error("Error getting kb settings:", err);
63
- reject(err);
62
+ winston.error("Error getting kb settings:", err?.response?.data);
63
+ resolve(null);
64
64
  } else {
65
65
  if (!resbody || !resbody.gptkey) {
66
66
  resolve(null);
@@ -101,5 +101,5 @@ class KbService {
101
101
  }
102
102
  }
103
103
 
104
- const kbService = new KbService();
105
- module.exports = kbService;
104
+ const llmService = new LLMService();
105
+ module.exports = llmService;
@@ -116,6 +116,14 @@ class DirAddKbContent {
116
116
  }
117
117
  }
118
118
 
119
+ if (!namespace) {
120
+ this.logger.error("[Add to KnwoledgeBase] Namespace is undefined")
121
+ winston.verbose("[DirAddKbContent] - Error: namespace is undefined")
122
+ await this.chatbot.addParameter("flowError", "[DirAddKbContent] Error: namespace is undefined");
123
+ callback(true);
124
+ return;
125
+ }
126
+
119
127
  let ns;
120
128
 
121
129
  if (action.namespaceAsName) {
@@ -137,20 +145,12 @@ class DirAddKbContent {
137
145
  return;
138
146
  }
139
147
 
140
- if (ns.engine) {
141
- engine = ns.engine;
142
- } else {
143
- engine = await this.setDefaultEngine()
144
- }
148
+ // if (ns.engine) {
149
+ // engine = ns.engine;
150
+ // } else {
151
+ // engine = await this.setDefaultEngine(ns.hybrid);
152
+ // }
145
153
 
146
- if (!namespace) {
147
- this.logger.error("[Add to KnwoledgeBase] Namespace is undefined")
148
- winston.verbose("[DirAddKbContent] - Error: namespace is undefined")
149
- await this.chatbot.addParameter("flowError", "[DirAddKbContent] Error: namespace is undefined");
150
- callback(true);
151
- return;
152
- }
153
-
154
154
  let json = {
155
155
  content: filled_content,
156
156
  namespace: namespace,
@@ -314,18 +314,19 @@ class DirAddKbContent {
314
314
  })
315
315
  }
316
316
 
317
- async setDefaultEngine() {
318
- return new Promise((resolve) => {
319
- let engine = {
320
- name: "pinecone",
321
- type: process.env.PINECONE_TYPE,
322
- apikey: "",
323
- vector_size: 1536,
324
- index_name: process.env.PINECONE_INDEX
325
- }
326
- resolve(engine);
327
- })
328
- }
317
+ // async setDefaultEngine() {
318
+ // let isHybrid = hybrid === true;
319
+ // return new Promise((resolve) => {
320
+ // let engine = {
321
+ // name: "pinecone",
322
+ // type: isHybrid ? "serverless" : "pod",
323
+ // apikey: "",
324
+ // vector_size: 1536,
325
+ // index_name: isHybrid ? "hybrid_index" : "standard_index"
326
+ // }
327
+ // resolve(engine);
328
+ // })
329
+ // }
329
330
 
330
331
  }
331
332
 
@@ -11,8 +11,10 @@ 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
14
  const quotasService = require("../../services/QuotasService");
15
+ const llmService = require("../../services/LLMService");
16
+
17
+
16
18
 
17
19
  class DirAskGPTV2 {
18
20
 
@@ -167,14 +169,11 @@ class DirAskGPTV2 {
167
169
  }
168
170
  }
169
171
 
170
- const kb_endpoint = process.env.KB_ENDPOINT_QA
171
- winston.verbose("DirAskGPTV2 KbEndpoint URL: " + kb_endpoint);
172
-
173
172
  let key = await integrationService.getKeyFromIntegrations(this.projectId, 'openai', this.token);
174
173
  if (!key) {
175
174
  this.logger.native("[Ask Knowledge Base] OpenAI key not found in Integration. Using shared OpenAI key");
176
175
  winston.verbose("DirAskGPTV2 - Key not found in Integrations. Searching in kb settings...");
177
- key = await kbService.getKeyFromKbSettings(this.projectId, this.token);
176
+ key = await llmService.getKeyFromKbSettings(this.projectId, this.token);
178
177
  }
179
178
 
180
179
  if (!key) {
@@ -209,6 +208,17 @@ class DirAskGPTV2 {
209
208
  }
210
209
  }
211
210
 
211
+ if (!namespace) {
212
+ this.logger.error("[Ask Knowledge Base] Namespace is undefined")
213
+ winston.verbose("DirAskGPTV2 - Error: namespace is undefined")
214
+ if (falseIntent) {
215
+ await this.chatbot.addParameter("flowError", "AskGPT Error: namespace is undefined");
216
+ await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
217
+ callback(true);
218
+ return;
219
+ }
220
+ }
221
+
212
222
  let ns;
213
223
 
214
224
  if (action.namespaceAsName) {
@@ -239,18 +249,7 @@ class DirAskGPTV2 {
239
249
  if (ns.engine) {
240
250
  engine = ns.engine;
241
251
  } else {
242
- engine = await this.setDefaultEngine()
243
- }
244
-
245
- if (!namespace) {
246
- this.logger.error("[Ask Knowledge Base] Namespace is undefined")
247
- winston.verbose("DirAskGPTV2 - Error: namespace is undefined")
248
- if (falseIntent) {
249
- await this.chatbot.addParameter("flowError", "AskGPT Error: namespace is undefined");
250
- await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
251
- callback(true);
252
- return;
253
- }
252
+ engine = await this.setDefaultEngine(ns.hybrid);
254
253
  }
255
254
 
256
255
  let json = {
@@ -275,7 +274,8 @@ class DirAskGPTV2 {
275
274
  json.chunks_only = chunks_only;
276
275
  }
277
276
 
278
- if (engine.type === 'serverless') {
277
+
278
+ if (ns.hybrid === true) {
279
279
  json.search_type = 'hybrid';
280
280
  json.alpha = alpha;
281
281
  }
@@ -296,6 +296,12 @@ class DirAskGPTV2 {
296
296
 
297
297
  winston.debug("DirAskGPTV2 json:", json);
298
298
 
299
+ let kb_endpoint = process.env.KB_ENDPOINT_QA;
300
+ if (ns.hybrid === true) {
301
+ kb_endpoint = process.env.KB_ENDPOINT_QA_GPU;
302
+ }
303
+ winston.verbose("DirAskGPTV2 KbEndpoint URL: " + kb_endpoint);
304
+
299
305
  const HTTPREQUEST = {
300
306
  url: kb_endpoint + "/qa",
301
307
  headers: {
@@ -357,7 +363,7 @@ class DirAskGPTV2 {
357
363
  }
358
364
  } else {
359
365
  await this.#assignAttributes(action, answer, source);
360
- kbService.addUnansweredQuestion(this.projectId, json.namespace, json.question, this.token).catch((err) => {
366
+ llmService.addUnansweredQuestion(this.projectId, json.namespace, json.question, this.token).catch((err) => {
361
367
  winston.error("DirAskGPTV2 - Error adding unanswered question: ", err);
362
368
  this.logger.warn("[Ask Knowledge Base] Unable to add unanswered question", json.question, "to namespacae", json.namespace);
363
369
  })
@@ -511,14 +517,15 @@ class DirAskGPTV2 {
511
517
  })
512
518
  }
513
519
 
514
- async setDefaultEngine() {
520
+ async setDefaultEngine(hybrid = false) {
521
+ let isHybrid = hybrid === true;
515
522
  return new Promise((resolve) => {
516
523
  let engine = {
517
524
  name: "pinecone",
518
- type: process.env.PINECONE_TYPE,
525
+ type: isHybrid ? "serverless" : "pod",
519
526
  apikey: "",
520
527
  vector_size: 1536,
521
- index_name: process.env.PINECONE_INDEX
528
+ index_name: isHybrid ? "hybrid_index" : "standard_index"
522
529
  }
523
530
  resolve(engine);
524
531
  })
@@ -19,8 +19,6 @@ class DirReply {
19
19
  this.tdcache = context.tdcache;
20
20
  this.log = context.log;
21
21
  this.API_ENDPOINT = context.API_ENDPOINT;
22
-
23
- console.log("context.supportRequest: ", context.supportRequest);
24
22
 
25
23
  this.logger = new Logger({ request_id: this.requestId, dev: this.context.supportRequest?.draft, intent_id: this.context.reply?.attributes?.intent_info?.intent_id });
26
24
  this.tdClient = new TiledeskClient({ projectId: this.context.projectId, token: this.context.token, APIURL: this.API_ENDPOINT, APIKEY: "___", log: this.log });
@@ -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
@@ -90,6 +90,7 @@ class DirWebResponse {
90
90
  }
91
91
  catch (err) {
92
92
  winston.error("Error parsing webRequest jsonBody: " + JSON.stringify(jsonBody) + "\nError: " + JSON.stringify(err));
93
+ this.logger.error("[Web Response] Error parsing webRequest jsonBody ", jsonBody)
93
94
  reject("Error parsing jsonBody");
94
95
  }
95
96
  }
@@ -307,49 +307,244 @@ class TiledeskChatbotUtil {
307
307
  if (command.message.attributes && command.message.attributes.attachment && command.message.attributes.attachment.json_buttons){
308
308
  // console.log("command with buttons ok:")
309
309
  let json_buttons_string = command.message.attributes.attachment.json_buttons;
310
- let json_buttons = null;
311
- let final_buttons = [];
310
+ console.log("json_buttons_string:", json_buttons_string)
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
+ console.log("replaceJSONGalleries...");
434
+ if (message.attributes && message.attributes.commands) {
435
+ console.log("message.attributes && message.attributes.commands...");
436
+ let commands = message.attributes.commands;
437
+ if (commands.length > 0) {
438
+ console.log("commands.length > 0");
439
+ for (let i = 0; i < commands.length; i++) {
440
+ let command = commands[i];
441
+ console.log("command:", JSON.stringify(command));
442
+ if (command.type === 'message' && command.message) {
443
+ console.log("command.type === 'message' && command.message");
444
+ if (command.message.attributes && command.message.attributes.attachment && command.message.attributes.attachment.json_gallery){
445
+ console.log("command with json_galley")
446
+ let final_gallery = [];
312
447
  try {
313
- // fill buttons
448
+ // fill previews
314
449
  const filler = new Filler();
315
- json_buttons_string = filler.fill(json_buttons_string, flow_attributes);
316
- json_buttons = JSON.parse(json_buttons_string);
317
- if (Array.isArray(json_buttons)) {
318
- json_buttons.forEach(button => {
319
- if (button.value && button.type === "action" && button.action) {
320
- button.show_echo = true;
321
- final_buttons.push(button);
322
- }
323
- else if (button.value && button.type === "text") {
324
- button.show_echo = true;
325
- final_buttons.push(button);
326
- }
327
- else if (button.value && button.type === "url" && button.link) {
328
- button.show_echo = true;
329
- final_buttons.push(button);
330
- }
331
- else {
332
- winston.verbose("Invalid button. Skipping:", button);
450
+ let json_gallery_string = command.message.attributes.attachment.json_gallery;
451
+ console.log("gallerystring is:", json_gallery_string)
452
+ json_gallery_string = filler.fill(json_gallery_string, flow_attributes);
453
+ let json_gallery = JSON.parse(json_gallery_string);
454
+ if (Array.isArray(json_gallery)) {
455
+ console.log("is gallery")
456
+ json_gallery.forEach(el => {
457
+ if (el.buttons) {
458
+ el.buttons = TiledeskChatbotUtil.renderJSONButtons(JSON.stringify(el.buttons));
333
459
  }
460
+ final_gallery.push(el);
334
461
  });
462
+ console.log("final: ", final_gallery)
335
463
  }
464
+ else {
465
+ winston.verbose("Invalid json_gallery.");
466
+ }
467
+ // "gallery": [
468
+ // {
469
+ // "preview": {
470
+ // "src": "https://eu.rtmv3.tiledesk.com/api/images?path=uploads%2Fusers%2F63a05d755f117f0013541383%2Fimages%2F8913ff2c-d788-45e1-ac71-ee5bae8479e2%2Fhybrid-settings.png",
471
+ // "uid": "mcamfa6s"
472
+ // },
473
+ // "title": "Title 1",
474
+ // "description": "Description 1",
475
+ // "buttons": [
476
+ // {
477
+ // "uid": "0a956f4637584ee4862360c19a161f8f",
478
+ // "type": "url",
479
+ // "value": "Prod1",
480
+ // "link": "https://URL1",
481
+ // "target": "blank",
482
+ // "action": "",
483
+ // "attributes": "",
484
+ // "show_echo": true
485
+ // },
486
+ // {
487
+ // "uid": "4a87abe3d03a4b6fbdbc3fc33c4a8430",
488
+ // "type": "action",
489
+ // "value": "Prod1.1 (connector)",
490
+ // "link": "",
491
+ // "target": "blank",
492
+ // "action": "#0f7aaefd-3147-466b-82a4-06756f36eea5",
493
+ // "attributes": "",
494
+ // "show_echo": true
495
+ // },
496
+ // {
497
+ // "uid": "31fac2c82ce24da0a2e9850a32165fe8",
498
+ // "type": "text",
499
+ // "value": "Prod1.2 (text)",
500
+ // "link": "https://url2",
501
+ // "target": "blank",
502
+ // "action": "",
503
+ // "attributes": "",
504
+ // "show_echo": true
505
+ // }
506
+ // ]
507
+ // },
508
+
509
+ // "buttons": [
510
+ // {
511
+ // "type": "action",
512
+ // "value": "Button1", // obbligatorio sempre
513
+ // "action": "#bb347206-d639-4926-94c9-e94930623dce", // mandatory
514
+ // "show_echo": true, // lo inserisco sempre
515
+ // "alias": "button1 alias"
516
+ // },
517
+ // {
518
+ // "type": "text",
519
+ // "value": "Button2 text", // obbligatorio sempre
520
+ // "show_echo": true // lo inserisco sempre
521
+ // },
522
+ // {
523
+ // "type": "url",
524
+ // "value": "Button3 link", // obbligatorio sempre
525
+ // "link": "http://", // obbligatorio
526
+ // "show_echo": true // lo inserisco sempre
527
+ // }
528
+ // ]
336
529
  }
337
530
  catch(error) {
338
- winston.warn("Invalid json_buttons:", error)
531
+ winston.warn("Error on JSON gallery parsing:", error);
339
532
  }
340
- if (final_buttons && final_buttons.length > 0) {
341
- command.message.attributes.attachment.buttons = final_buttons;
342
- delete command.message.attributes.attachment.json_buttons;
533
+ console.log("final gallery...", final_gallery)
534
+ if (final_gallery && final_gallery.length > 0) {
535
+ console.log("updating with final gallery...", final_gallery)
536
+ command.message.attributes.attachment.gallery = final_gallery;
537
+ delete command.message.attributes.attachment.json_gallery;
343
538
  }
344
539
  else {
345
- winston.verbose("Invalid json_buttons. Skipping...")
540
+ winston.verbose("Invalid JSON Gallery.")
346
541
  }
347
542
  }
348
543
  }
349
544
  }
350
545
  }
351
546
  }
352
- return all_buttons;
547
+ return message;
353
548
  }
354
549
 
355
550
  static buttonByText(text, buttons) {