@tiledesk/tiledesk-tybot-connector 2.0.21-rc3 → 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/logs/app.log +835 -0
- package/package.json +1 -1
- package/services/{KBService.js → LLMService.js} +3 -3
- package/tiledeskChatbotPlugs/directives/DirAddKbContent.js +26 -25
- package/tiledeskChatbotPlugs/directives/DirAskGPTV2.js +22 -19
- package/tiledeskChatbotPlugs/directives/DirReplyV2.js +1 -0
- package/tiledeskChatbotPlugs/directives/DirWebResponse.js +1 -0
- package/utils/TiledeskChatbotUtil.js +222 -27
package/package.json
CHANGED
|
@@ -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
|
|
5
|
+
class LLMService {
|
|
6
6
|
|
|
7
7
|
constructor() { }
|
|
8
8
|
|
|
@@ -101,5 +101,5 @@ class KBService {
|
|
|
101
101
|
}
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
-
const
|
|
105
|
-
module.exports =
|
|
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
|
-
|
|
142
|
-
} else {
|
|
143
|
-
|
|
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
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
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
|
|
|
@@ -12,7 +12,8 @@ const httpUtils = require("../../utils/HttpUtils");
|
|
|
12
12
|
const integrationService = require("../../services/IntegrationService");
|
|
13
13
|
const { Logger } = require("../../Logger");
|
|
14
14
|
const quotasService = require("../../services/QuotasService");
|
|
15
|
-
const
|
|
15
|
+
const llmService = require("../../services/LLMService");
|
|
16
|
+
|
|
16
17
|
|
|
17
18
|
|
|
18
19
|
class DirAskGPTV2 {
|
|
@@ -175,7 +176,7 @@ class DirAskGPTV2 {
|
|
|
175
176
|
if (!key) {
|
|
176
177
|
this.logger.native("[Ask Knowledge Base] OpenAI key not found in Integration. Using shared OpenAI key");
|
|
177
178
|
winston.verbose("DirAskGPTV2 - Key not found in Integrations. Searching in kb settings...");
|
|
178
|
-
key = await
|
|
179
|
+
key = await llmService.getKeyFromKbSettings(this.projectId, this.token);
|
|
179
180
|
}
|
|
180
181
|
|
|
181
182
|
if (!key) {
|
|
@@ -210,6 +211,17 @@ class DirAskGPTV2 {
|
|
|
210
211
|
}
|
|
211
212
|
}
|
|
212
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
|
+
|
|
213
225
|
let ns;
|
|
214
226
|
|
|
215
227
|
if (action.namespaceAsName) {
|
|
@@ -240,18 +252,7 @@ class DirAskGPTV2 {
|
|
|
240
252
|
if (ns.engine) {
|
|
241
253
|
engine = ns.engine;
|
|
242
254
|
} else {
|
|
243
|
-
engine = await this.setDefaultEngine()
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
if (!namespace) {
|
|
247
|
-
this.logger.error("[Ask Knowledge Base] Namespace is undefined")
|
|
248
|
-
winston.verbose("DirAskGPTV2 - Error: namespace is undefined")
|
|
249
|
-
if (falseIntent) {
|
|
250
|
-
await this.chatbot.addParameter("flowError", "AskGPT Error: namespace is undefined");
|
|
251
|
-
await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
|
|
252
|
-
callback(true);
|
|
253
|
-
return;
|
|
254
|
-
}
|
|
255
|
+
engine = await this.setDefaultEngine(ns.hybrid);
|
|
255
256
|
}
|
|
256
257
|
|
|
257
258
|
let json = {
|
|
@@ -276,7 +277,8 @@ class DirAskGPTV2 {
|
|
|
276
277
|
json.chunks_only = chunks_only;
|
|
277
278
|
}
|
|
278
279
|
|
|
279
|
-
|
|
280
|
+
|
|
281
|
+
if (ns.hybrid === true) {
|
|
280
282
|
json.search_type = 'hybrid';
|
|
281
283
|
json.alpha = alpha;
|
|
282
284
|
}
|
|
@@ -358,7 +360,7 @@ class DirAskGPTV2 {
|
|
|
358
360
|
}
|
|
359
361
|
} else {
|
|
360
362
|
await this.#assignAttributes(action, answer, source);
|
|
361
|
-
|
|
363
|
+
llmService.addUnansweredQuestion(this.projectId, json.namespace, json.question, this.token).catch((err) => {
|
|
362
364
|
winston.error("DirAskGPTV2 - Error adding unanswered question: ", err);
|
|
363
365
|
this.logger.warn("[Ask Knowledge Base] Unable to add unanswered question", json.question, "to namespacae", json.namespace);
|
|
364
366
|
})
|
|
@@ -512,14 +514,15 @@ class DirAskGPTV2 {
|
|
|
512
514
|
})
|
|
513
515
|
}
|
|
514
516
|
|
|
515
|
-
async setDefaultEngine() {
|
|
517
|
+
async setDefaultEngine(hybrid = false) {
|
|
518
|
+
let isHybrid = hybrid === true;
|
|
516
519
|
return new Promise((resolve) => {
|
|
517
520
|
let engine = {
|
|
518
521
|
name: "pinecone",
|
|
519
|
-
type:
|
|
522
|
+
type: isHybrid ? "serverless" : "pod",
|
|
520
523
|
apikey: "",
|
|
521
524
|
vector_size: 1536,
|
|
522
|
-
index_name:
|
|
525
|
+
index_name: isHybrid ? "hybrid_index" : "standard_index"
|
|
523
526
|
}
|
|
524
527
|
resolve(engine);
|
|
525
528
|
})
|
|
@@ -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
|
-
|
|
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
|
|
448
|
+
// fill previews
|
|
314
449
|
const filler = new Filler();
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
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("
|
|
531
|
+
winston.warn("Error on JSON gallery parsing:", error);
|
|
339
532
|
}
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
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
|
|
540
|
+
winston.verbose("Invalid JSON Gallery.")
|
|
346
541
|
}
|
|
347
542
|
}
|
|
348
543
|
}
|
|
349
544
|
}
|
|
350
545
|
}
|
|
351
546
|
}
|
|
352
|
-
return
|
|
547
|
+
return message;
|
|
353
548
|
}
|
|
354
549
|
|
|
355
550
|
static buttonByText(text, buttons) {
|