@tiledesk/tiledesk-tybot-connector 2.0.21-rc4 → 2.0.21-rc9

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-rc4",
3
+ "version": "2.0.21-rc9",
4
4
  "description": "Tiledesk Tybot connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -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
 
@@ -211,6 +211,17 @@ class DirAskGPTV2 {
211
211
  }
212
212
  }
213
213
 
214
+ if (!namespace) {
215
+ this.logger.error("[Ask Knowledge Base] Namespace is undefined")
216
+ winston.verbose("DirAskGPTV2 - Error: namespace is undefined")
217
+ if (falseIntent) {
218
+ await this.chatbot.addParameter("flowError", "AskGPT Error: namespace is undefined");
219
+ await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
220
+ callback(true);
221
+ return;
222
+ }
223
+ }
224
+
214
225
  let ns;
215
226
 
216
227
  if (action.namespaceAsName) {
@@ -241,18 +252,7 @@ class DirAskGPTV2 {
241
252
  if (ns.engine) {
242
253
  engine = ns.engine;
243
254
  } else {
244
- engine = await this.setDefaultEngine()
245
- }
246
-
247
- if (!namespace) {
248
- this.logger.error("[Ask Knowledge Base] Namespace is undefined")
249
- winston.verbose("DirAskGPTV2 - Error: namespace is undefined")
250
- if (falseIntent) {
251
- await this.chatbot.addParameter("flowError", "AskGPT Error: namespace is undefined");
252
- await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
253
- callback(true);
254
- return;
255
- }
255
+ engine = await this.setDefaultEngine(ns.hybrid);
256
256
  }
257
257
 
258
258
  let json = {
@@ -277,7 +277,8 @@ class DirAskGPTV2 {
277
277
  json.chunks_only = chunks_only;
278
278
  }
279
279
 
280
- if (engine.type === 'serverless') {
280
+
281
+ if (ns.hybrid === true) {
281
282
  json.search_type = 'hybrid';
282
283
  json.alpha = alpha;
283
284
  }
@@ -513,14 +514,15 @@ class DirAskGPTV2 {
513
514
  })
514
515
  }
515
516
 
516
- async setDefaultEngine() {
517
+ async setDefaultEngine(hybrid = false) {
518
+ let isHybrid = hybrid === true;
517
519
  return new Promise((resolve) => {
518
520
  let engine = {
519
521
  name: "pinecone",
520
- type: process.env.PINECONE_TYPE,
522
+ type: isHybrid ? "serverless" : "pod",
521
523
  apikey: "",
522
524
  vector_size: 1536,
523
- index_name: process.env.PINECONE_INDEX
525
+ index_name: isHybrid ? "hybrid_index" : "standard_index"
524
526
  }
525
527
  resolve(engine);
526
528
  })
@@ -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) {