@tiledesk/tiledesk-tybot-connector 2.0.10-rc9 → 2.0.11
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/CHANGELOG.md +2 -27
- package/ExtApi.js +8 -1
- package/ExtUtil.js +6 -5
- package/Logger.js +17 -26
- package/TdCache copy.js +242 -0
- package/engine/IntentForm.js +1 -0
- package/engine/IntentsMachineFactory.js +4 -4
- package/engine/MongodbBotsDataSource.js +1 -0
- package/engine/MongodbIntentsMachine.js +1 -0
- package/engine/TiledeskChatbot.js +16 -11
- package/engine/TiledeskIntentsMachine.js +2 -1
- package/engine/mock/MockBotsDataSource.js +2 -1
- package/index.js +25 -22
- package/logs/app.log +43056 -4268
- package/logs/app1.log +41129 -0
- package/logs/app2.log +14567 -45594
- package/logs/app6.log +29039 -0
- package/logs/app7.log +4726 -0
- package/package.json +2 -3
- package/services/AIService.js +1 -1
- package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +18 -38
- package/tiledeskChatbotPlugs/FillParamsChatbotPlug.js +2 -1
- package/tiledeskChatbotPlugs/MarkbotChatbotPlug.js +3 -1
- package/tiledeskChatbotPlugs/SplitsChatbotPlug.js +3 -1
- package/tiledeskChatbotPlugs/WebhookChatbotPlug.js +9 -7
- package/tiledeskChatbotPlugs/directives/DEPRECATED_DirIfNotOpenHours.js +10 -3
- package/tiledeskChatbotPlugs/directives/DEPRECATED_DirIfOpenHours.js +9 -2
- package/tiledeskChatbotPlugs/directives/DirAddTags.js +4 -16
- package/tiledeskChatbotPlugs/directives/DirAiPrompt.js +0 -16
- package/tiledeskChatbotPlugs/directives/DirAskGPT.js +1 -0
- package/tiledeskChatbotPlugs/directives/DirAskGPTV2.js +4 -17
- package/tiledeskChatbotPlugs/directives/DirAssign.js +8 -5
- package/tiledeskChatbotPlugs/directives/DirAssignFromFunction.js +3 -1
- package/tiledeskChatbotPlugs/directives/DirAssistant.js +1 -0
- package/tiledeskChatbotPlugs/directives/DirBrevo.js +6 -4
- package/tiledeskChatbotPlugs/directives/DirCaptureUserReply.js +1 -0
- package/tiledeskChatbotPlugs/directives/DirClose.js +2 -0
- package/tiledeskChatbotPlugs/directives/DirCode.js +1 -0
- package/tiledeskChatbotPlugs/directives/DirCondition.js +1 -0
- package/tiledeskChatbotPlugs/directives/DirConnectBlock.js +1 -0
- package/tiledeskChatbotPlugs/directives/DirContactUpdate.js +3 -1
- package/tiledeskChatbotPlugs/directives/DirCustomerio.js +1 -0
- package/tiledeskChatbotPlugs/directives/DirDeflectToHelpCenter.js +5 -2
- package/tiledeskChatbotPlugs/directives/DirDeleteVariable.js +1 -0
- package/tiledeskChatbotPlugs/directives/DirDepartment.js +3 -1
- package/tiledeskChatbotPlugs/directives/DirFireTiledeskEvent.js +3 -1
- package/tiledeskChatbotPlugs/directives/DirForm.js +3 -1
- package/tiledeskChatbotPlugs/directives/DirGptTask.js +1 -0
- package/tiledeskChatbotPlugs/directives/DirGptTask_OLD.js +55 -36
- package/tiledeskChatbotPlugs/directives/DirHubspot.js +1 -0
- package/tiledeskChatbotPlugs/directives/DirIfOnlineAgents.js +4 -1
- package/tiledeskChatbotPlugs/directives/DirIfOnlineAgentsV2.js +4 -1
- package/tiledeskChatbotPlugs/directives/DirIfOpenHours.js +2 -0
- package/tiledeskChatbotPlugs/directives/DirIfOpenHours_OLD.js +12 -8
- package/tiledeskChatbotPlugs/directives/DirIntent.js +5 -6
- package/tiledeskChatbotPlugs/directives/DirJSONCondition.js +1 -0
- package/tiledeskChatbotPlugs/directives/DirLockIntent.js +1 -0
- package/tiledeskChatbotPlugs/directives/DirMake.js +16 -1
- package/tiledeskChatbotPlugs/directives/DirMessage.js +3 -1
- package/tiledeskChatbotPlugs/directives/DirMessageToBot.js +1 -0
- package/tiledeskChatbotPlugs/directives/DirMoveToAgent.js +4 -1
- package/tiledeskChatbotPlugs/directives/DirMoveToUnassigned.js +3 -0
- package/tiledeskChatbotPlugs/directives/DirQapla.js +1 -0
- package/tiledeskChatbotPlugs/directives/DirRandomReply.js +3 -1
- package/tiledeskChatbotPlugs/directives/DirRemoveCurrentBot.js +2 -1
- package/tiledeskChatbotPlugs/directives/DirReplaceBot.js +3 -1
- package/tiledeskChatbotPlugs/directives/DirReplaceBotV2.js +3 -1
- package/tiledeskChatbotPlugs/directives/DirReplaceBotV3.js +3 -1
- package/tiledeskChatbotPlugs/directives/DirReply.js +18 -7
- package/tiledeskChatbotPlugs/directives/DirReplyV2.js +13 -8
- package/tiledeskChatbotPlugs/directives/DirSendEmail.js +3 -1
- package/tiledeskChatbotPlugs/directives/DirSendWhatsapp.js +1 -0
- package/tiledeskChatbotPlugs/directives/DirSetAttribute.js +1 -0
- package/tiledeskChatbotPlugs/directives/DirSetAttributeV2.js +4 -1
- package/tiledeskChatbotPlugs/directives/DirSetConversationTags.js +3 -1
- package/tiledeskChatbotPlugs/directives/DirUnlockIntent.js +1 -0
- package/tiledeskChatbotPlugs/directives/DirWait.js +1 -0
- package/tiledeskChatbotPlugs/directives/DirWebRequest.js +1 -0
- package/tiledeskChatbotPlugs/directives/DirWebRequestV2.js +19 -19
- package/tiledeskChatbotPlugs/directives/DirWebRequestV2_old.js +417 -0
- package/tiledeskChatbotPlugs/directives/DirWebResponse.js +40 -6
- package/tiledeskChatbotPlugs/directives/DirWhatsappByAttribute.js +1 -0
- package/tiledeskChatbotPlugs/directives/Directives.js +0 -3
- package/utils/TiledeskChatbotUtil.js +57 -19
- package/tiledeskChatbotPlugs/directives/DirAddKbContent.js +0 -332
- package/tiledeskChatbotPlugs/directives/DirFlowLog.js +0 -88
|
@@ -252,24 +252,24 @@ class TiledeskChatbotUtil {
|
|
|
252
252
|
}
|
|
253
253
|
}
|
|
254
254
|
|
|
255
|
-
static fillCommandAttachments(command, variables) {
|
|
256
|
-
winston.debug("(TiledeskChatbotUtils) Filling command button: ", command)
|
|
255
|
+
static fillCommandAttachments(command, variables, log) {
|
|
256
|
+
if (log) { winston.debug("(TiledeskChatbotUtils) Filling command button: ", command) }
|
|
257
257
|
if (command.message && command.message.attributes && command.message.attributes.attachment && command.message.attributes.attachment.buttons && command.message.attributes.attachment.buttons.length > 0) {
|
|
258
258
|
let buttons = command.message.attributes.attachment.buttons;
|
|
259
259
|
const filler = new Filler();
|
|
260
260
|
buttons.forEach(button => {
|
|
261
261
|
if (button.link) {
|
|
262
262
|
button.link = filler.fill(button.link, variables);
|
|
263
|
-
winston.debug("(TiledeskChatbotUtils) button.link filled: " + button.link)
|
|
263
|
+
if (log) { winston.debug("(TiledeskChatbotUtils) button.link filled: " + button.link) }
|
|
264
264
|
}
|
|
265
265
|
if (button.value) {
|
|
266
266
|
button.value = filler.fill(button.value, variables);
|
|
267
|
-
winston.debug("(TiledeskChatbotUtils) button.value filled: " + button.value)
|
|
267
|
+
if (log) { winston.debug("(TiledeskChatbotUtils) button.value filled: " + button.value) }
|
|
268
268
|
}
|
|
269
269
|
});
|
|
270
270
|
}
|
|
271
271
|
else {
|
|
272
|
-
winston.debug("(TiledeskChatbotUtils) No attachments to fill in command")
|
|
272
|
+
if (log) { winston.debug("(TiledeskChatbotUtils) No attachments to fill in command") }
|
|
273
273
|
}
|
|
274
274
|
}
|
|
275
275
|
|
|
@@ -319,31 +319,55 @@ class TiledeskChatbotUtil {
|
|
|
319
319
|
json_buttons.forEach(button => {
|
|
320
320
|
if (button.value && button.type === "action" && button.action) {
|
|
321
321
|
button.show_echo = true;
|
|
322
|
+
// console.log("pushing:", button)
|
|
322
323
|
final_buttons.push(button);
|
|
323
324
|
}
|
|
324
325
|
else if (button.value && button.type === "text") {
|
|
325
326
|
button.show_echo = true;
|
|
327
|
+
// console.log("pushing:", button)
|
|
326
328
|
final_buttons.push(button);
|
|
327
329
|
}
|
|
328
330
|
else if (button.value && button.type === "url" && button.link) {
|
|
329
331
|
button.show_echo = true;
|
|
332
|
+
// console.log("pushing:", button)
|
|
330
333
|
final_buttons.push(button);
|
|
331
334
|
}
|
|
332
335
|
else {
|
|
333
|
-
|
|
336
|
+
console.log("Invalid button. Skipping:", JSON.stringify(button) );
|
|
334
337
|
}
|
|
335
338
|
});
|
|
336
339
|
}
|
|
340
|
+
|
|
341
|
+
// "buttons": [
|
|
342
|
+
// {
|
|
343
|
+
// "type": "action",
|
|
344
|
+
// "value": "Button1", // obbligatorio sempre
|
|
345
|
+
// "action": "#bb347206-d639-4926-94c9-e94930623dce", // mandatory
|
|
346
|
+
// "show_echo": true, // lo inserisco sempre
|
|
347
|
+
// "alias": "button1 alias"
|
|
348
|
+
// },
|
|
349
|
+
// {
|
|
350
|
+
// "type": "text",
|
|
351
|
+
// "value": "Button2 text", // obbligatorio sempre
|
|
352
|
+
// "show_echo": true // lo inserisco sempre
|
|
353
|
+
// },
|
|
354
|
+
// {
|
|
355
|
+
// "type": "url",
|
|
356
|
+
// "value": "Button3 link", // obbligatorio sempre
|
|
357
|
+
// "link": "http://", // obbligatorio
|
|
358
|
+
// "show_echo": true // lo inserisco sempre
|
|
359
|
+
// }
|
|
360
|
+
// ]
|
|
337
361
|
}
|
|
338
362
|
catch(error) {
|
|
339
|
-
|
|
363
|
+
console.error("Invalid json_buttons:", error)
|
|
340
364
|
}
|
|
341
365
|
if (final_buttons && final_buttons.length > 0) {
|
|
342
366
|
command.message.attributes.attachment.buttons = final_buttons;
|
|
343
367
|
delete command.message.attributes.attachment.json_buttons;
|
|
344
368
|
}
|
|
345
369
|
else {
|
|
346
|
-
|
|
370
|
+
console.log("Invalid json_buttons. Skipping")
|
|
347
371
|
}
|
|
348
372
|
}
|
|
349
373
|
}
|
|
@@ -496,7 +520,7 @@ class TiledeskChatbotUtil {
|
|
|
496
520
|
static async updateRequestAttributes(chatbot, chatbotToken, message, projectId, requestId) {
|
|
497
521
|
// update request context
|
|
498
522
|
try {
|
|
499
|
-
winston.debug("Updating request variables. Message:", message);
|
|
523
|
+
if (chatbot.log) { winston.debug("Updating request variables. Message:", message); }
|
|
500
524
|
const messageId = message._id;
|
|
501
525
|
const chat_url = `https://panel.tiledesk.com/v3/dashboard/#/project/${projectId}/wsrequest/${requestId}/messages`
|
|
502
526
|
|
|
@@ -567,12 +591,12 @@ class TiledeskChatbotUtil {
|
|
|
567
591
|
}
|
|
568
592
|
|
|
569
593
|
if (message && message.request && message.request.lead) {
|
|
570
|
-
winston.debug("(TiledeskChatbotUtil) Lead found with email: " + message.request.lead.email + " and lead.fullname " + message.request.lead.fullname);
|
|
594
|
+
if (chatbot.log) { winston.debug("(TiledeskChatbotUtil) Lead found with email: " + message.request.lead.email + " and lead.fullname " + message.request.lead.fullname); }
|
|
571
595
|
let currentLeadEmail = await chatbot.getParameter(TiledeskChatbotConst.REQ_LEAD_EMAIL_KEY);
|
|
572
|
-
winston.debug("(TiledeskChatbotUtil) You lead email from attributes: " + currentLeadEmail);
|
|
596
|
+
if (chatbot.log) { winston.debug("(TiledeskChatbotUtil) You lead email from attributes: " + currentLeadEmail); }
|
|
573
597
|
if (message.request.lead.email && !currentLeadEmail) {
|
|
574
598
|
// worth saving
|
|
575
|
-
winston.debug("(TiledeskChatbotUtil) worth saving email");
|
|
599
|
+
if (chatbot.log) { winston.debug("(TiledeskChatbotUtil) worth saving email"); }
|
|
576
600
|
try {
|
|
577
601
|
await chatbot.addParameter(TiledeskChatbotConst.REQ_LEAD_EMAIL_KEY, message.request.lead.email);
|
|
578
602
|
}
|
|
@@ -581,10 +605,10 @@ class TiledeskChatbotUtil {
|
|
|
581
605
|
}
|
|
582
606
|
}
|
|
583
607
|
let currentLeadName = await chatbot.getParameter(TiledeskChatbotConst.REQ_LEAD_USERFULLNAME_KEY);
|
|
584
|
-
winston.debug("(TiledeskChatbotUtil) You lead email from attributes: " + currentLeadEmail);
|
|
608
|
+
if (chatbot.log) { winston.debug("(TiledeskChatbotUtil) You lead email from attributes: " + currentLeadEmail); }
|
|
585
609
|
if (message.request.lead.fullname && !currentLeadName) {
|
|
586
610
|
// worth saving
|
|
587
|
-
winston.debug("(TiledeskChatbotUtil) worth saving email");
|
|
611
|
+
if (chatbot.log) { winston.debug("(TiledeskChatbotUtil) worth saving email"); }
|
|
588
612
|
try {
|
|
589
613
|
await chatbot.addParameter(TiledeskChatbotConst.REQ_LEAD_USERFULLNAME_KEY, message.request.lead.fullname);
|
|
590
614
|
}
|
|
@@ -661,10 +685,10 @@ class TiledeskChatbotUtil {
|
|
|
661
685
|
message.attributes = {}
|
|
662
686
|
}
|
|
663
687
|
message.attributes.payload = { ...message.attributes.payload, ...message.request.attributes.payload }
|
|
664
|
-
winston.debug("(TiledeskChatbotUtil) Forced Set message.attributes.payload ", message.attributes.payload);
|
|
688
|
+
if (chatbot.log) { winston.debug("(TiledeskChatbotUtil) Forced Set message.attributes.payload ", message.attributes.payload); }
|
|
665
689
|
}
|
|
666
690
|
if (message.attributes) {
|
|
667
|
-
winston.debug("(TiledeskChatbotUtil) Ok message.attributes ", message.attributes);
|
|
691
|
+
if (chatbot.log) { winston.debug("(TiledeskChatbotUtil) Ok message.attributes ", message.attributes); }
|
|
668
692
|
|
|
669
693
|
await chatbot.addParameter(TiledeskChatbotConst.REQ_END_USER_ID_KEY, message.attributes.requester_id);
|
|
670
694
|
await chatbot.addParameter(TiledeskChatbotConst.REQ_END_USER_IP_ADDRESS_KEY, message.attributes.ipAddress);
|
|
@@ -698,12 +722,12 @@ class TiledeskChatbotUtil {
|
|
|
698
722
|
|
|
699
723
|
|
|
700
724
|
const _bot = chatbot.bot; // aka FaqKB
|
|
701
|
-
winston.debug("(TiledeskChatbotUtil) Adding Globals to context: ", _bot);
|
|
725
|
+
if (chatbot.log) { winston.debug("(TiledeskChatbotUtil) Adding Globals to context: ", _bot); }
|
|
702
726
|
|
|
703
727
|
if (_bot.attributes && _bot.attributes.globals) {
|
|
704
|
-
winston.error("(TiledeskChatbotUtil) Got Globals: ", _bot.attributes.globals);
|
|
728
|
+
if (chatbot.log) { winston.error("(TiledeskChatbotUtil) Got Globals: ", _bot.attributes.globals); }
|
|
705
729
|
_bot.attributes.globals.forEach(async (global_var) => {
|
|
706
|
-
winston.error("(TiledeskChatbotUtil) Adding global: " + global_var.key + " value: " + global_var.value);
|
|
730
|
+
if (chatbot.log) { winston.error("(TiledeskChatbotUtil) Adding global: " + global_var.key + " value: " + global_var.value); }
|
|
707
731
|
await chatbot.addParameter(global_var.key, global_var.value);
|
|
708
732
|
});
|
|
709
733
|
}
|
|
@@ -716,6 +740,20 @@ class TiledeskChatbotUtil {
|
|
|
716
740
|
// }
|
|
717
741
|
// );
|
|
718
742
|
|
|
743
|
+
if (chatbot.log) {
|
|
744
|
+
if (chatbot.log) { winston.debug("(TiledeskChatbotUtil) requestId: " + requestId); }
|
|
745
|
+
if (chatbot.log) { winston.debug("(TiledeskChatbotUtil) Key: " + TiledeskChatbotConst.REQ_PROJECT_ID_KEY); }
|
|
746
|
+
|
|
747
|
+
let proj_ = await chatbot.getParameter(TiledeskChatbotConst.REQ_PROJECT_ID_KEY);
|
|
748
|
+
if (chatbot.log) { winston.debug("(TiledeskChatbotUtil) request parameter proj_: " + proj_); }
|
|
749
|
+
|
|
750
|
+
const all_parameters = await chatbot.allParameters();
|
|
751
|
+
for (const [key, value] of Object.entries(all_parameters)) {
|
|
752
|
+
const value_type = typeof value;
|
|
753
|
+
if (chatbot.log) { winston.debug("(TiledeskChatbotUtil) Request Attribute: " + key + " value: " + value + " type: " + value_type); }
|
|
754
|
+
|
|
755
|
+
}
|
|
756
|
+
}
|
|
719
757
|
} catch(error) {
|
|
720
758
|
winston.error("(TiledeskChatbotUtil) updateRequestAttributes Error: ", error);
|
|
721
759
|
process.exit(1)
|
|
@@ -1,332 +0,0 @@
|
|
|
1
|
-
const axios = require("axios").default;
|
|
2
|
-
const { TiledeskChatbot } = require('../../engine/TiledeskChatbot');
|
|
3
|
-
const { Filler } = require('../Filler');
|
|
4
|
-
let https = require("https");
|
|
5
|
-
const { DirIntent } = require("./DirIntent");
|
|
6
|
-
const { TiledeskChatbotConst } = require("../../engine/TiledeskChatbotConst");
|
|
7
|
-
const { TiledeskChatbotUtil } = require("../../utils/TiledeskChatbotUtil");
|
|
8
|
-
const assert = require("assert");
|
|
9
|
-
require('dotenv').config();
|
|
10
|
-
const winston = require('../../utils/winston');
|
|
11
|
-
const httpUtils = require("../../utils/HttpUtils");
|
|
12
|
-
const integrationService = require("../../services/IntegrationService");
|
|
13
|
-
const { Logger } = require("../../Logger");
|
|
14
|
-
|
|
15
|
-
class DirAddKbContent {
|
|
16
|
-
|
|
17
|
-
constructor(context) {
|
|
18
|
-
if (!context) {
|
|
19
|
-
throw new Error('context object is mandatory');
|
|
20
|
-
}
|
|
21
|
-
this.context = context;
|
|
22
|
-
this.chatbot = context.chatbot;
|
|
23
|
-
this.tdcache = this.context.tdcache;
|
|
24
|
-
this.requestId = this.context.requestId;
|
|
25
|
-
this.projectId = this.context.projectId;
|
|
26
|
-
this.token = this.context.token;
|
|
27
|
-
this.intentDir = new DirIntent(context);
|
|
28
|
-
this.API_ENDPOINT = this.context.API_ENDPOINT;
|
|
29
|
-
this.log = context.log;
|
|
30
|
-
this.logger = new Logger({ request_id: this.requestId, dev: this.context.supportRequest.draft });
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
execute(directive, callback) {
|
|
34
|
-
this.logger.info("Execute AskKnowledgeBase action")
|
|
35
|
-
winston.debug("DirAskGPTV2 directive: ", directive);
|
|
36
|
-
let action;
|
|
37
|
-
if (directive.action) {
|
|
38
|
-
action = directive.action;
|
|
39
|
-
}
|
|
40
|
-
else {
|
|
41
|
-
this.logger.error("Incorrect action for ", directive.name, directive)
|
|
42
|
-
winston.debug("DirAskGPTV2 Incorrect directive: ", directive);
|
|
43
|
-
callback();
|
|
44
|
-
return;
|
|
45
|
-
}
|
|
46
|
-
this.go(action, (stop) => {
|
|
47
|
-
this.logger.info("Acion AskKnowledgeBase completed");
|
|
48
|
-
callback(stop);
|
|
49
|
-
})
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
async go(action, callback) {
|
|
53
|
-
winston.debug("[DirAddKbContent] action:", action);
|
|
54
|
-
if (!this.tdcache) {
|
|
55
|
-
winston.error("[DirAddKbContent] Error: tdcache is mandatory");
|
|
56
|
-
callback();
|
|
57
|
-
return;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
let publicKey = false;
|
|
61
|
-
let type = action.type;
|
|
62
|
-
let name = action.name;
|
|
63
|
-
let content = action.content;
|
|
64
|
-
let engine;
|
|
65
|
-
|
|
66
|
-
// default values
|
|
67
|
-
let namespace = this.context.projectId;
|
|
68
|
-
|
|
69
|
-
if (action.namespace) {
|
|
70
|
-
namespace = action.namespace;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
let requestVariables = null;
|
|
74
|
-
requestVariables =
|
|
75
|
-
await TiledeskChatbot.allParametersStatic(
|
|
76
|
-
this.tdcache, this.requestId
|
|
77
|
-
);
|
|
78
|
-
|
|
79
|
-
const filler = new Filler();
|
|
80
|
-
const filled_content = filler.fill(content, requestVariables);
|
|
81
|
-
const filled_name = filler.fill(name, requestVariables);
|
|
82
|
-
|
|
83
|
-
const kb_endpoint = process.env.API_ENDPOINT;
|
|
84
|
-
winston.verbose("[DirAddKbContent] KbEndpoint URL: " + kb_endpoint);
|
|
85
|
-
|
|
86
|
-
let key = await integrationService.getKeyFromIntegrations(this.projectId, 'openai', this.token);
|
|
87
|
-
if (!key) {
|
|
88
|
-
this.logger.debug("[DirAddKbContent] OpenAI key not found in Integration. Using shared OpenAI key");
|
|
89
|
-
winston.verbose("[DirAddKbContent] - Key not found in Integrations. Searching in kb settings...");
|
|
90
|
-
key = await this.getKeyFromKbSettings();
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
if (!key) {
|
|
94
|
-
winston.verbose("[DirAddKbContent] - Retrieve public gptkey")
|
|
95
|
-
key = process.env.GPTKEY;
|
|
96
|
-
publicKey = true;
|
|
97
|
-
} else {
|
|
98
|
-
this.logger.debug("[DirAddKbContent] use your own OpenAI key")
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
if (!key) {
|
|
102
|
-
winston.info("[DirAddKbContent] Error: gptkey is mandatory");
|
|
103
|
-
await this.chatbot.addParameter("flowError", "[DirAddKbContent] Error: gptkey is mandatory");
|
|
104
|
-
callback();
|
|
105
|
-
return;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
if (publicKey === true) {
|
|
109
|
-
let keep_going = await this.checkQuoteAvailability();
|
|
110
|
-
if (keep_going === false) {
|
|
111
|
-
this.logger.warn("[DirAddKbContent] Tokens quota exceeded. Skip the action")
|
|
112
|
-
winston.verbose("[DirAddKbContent] - Quota exceeded for tokens. Skip the action")
|
|
113
|
-
await this.chatbot.addParameter("flowError", "[DirAddKbContent] Error: tokens quota exceeded");
|
|
114
|
-
callback(true);
|
|
115
|
-
return;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
let ns;
|
|
120
|
-
|
|
121
|
-
if (action.namespaceAsName) {
|
|
122
|
-
// Namespace could be an attribute
|
|
123
|
-
const filled_namespace = filler.fill(action.namespace, requestVariables)
|
|
124
|
-
this.logger.debug("[DirAddKbContent] Searching namespace by name ", filled_namespace);
|
|
125
|
-
ns = await this.getNamespace(filled_namespace, null);
|
|
126
|
-
namespace = ns?.id;
|
|
127
|
-
winston.verbose("[DirAddKbContent] - Retrieved namespace id from name " + namespace);
|
|
128
|
-
} else {
|
|
129
|
-
this.logger.debug("[DirAddKbContent] Searching namespace by id ", namespace);
|
|
130
|
-
ns = await this.getNamespace(null, namespace);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
if (!ns) {
|
|
134
|
-
this.logger.error("[DirAddKbContent] Namespace not found");
|
|
135
|
-
await this.chatbot.addParameter("flowError", "[DirAddKbContent] Error: namespace not found");
|
|
136
|
-
callback();
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
if (ns.engine) {
|
|
141
|
-
engine = ns.engine;
|
|
142
|
-
} else {
|
|
143
|
-
engine = await this.setDefaultEngine()
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
if (!namespace) {
|
|
147
|
-
this.logger.error("[DirAddKbContent] 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
|
-
let json = {
|
|
155
|
-
content: filled_content,
|
|
156
|
-
namespace: namespace,
|
|
157
|
-
type: type,
|
|
158
|
-
name: filled_name,
|
|
159
|
-
source: filled_name
|
|
160
|
-
};
|
|
161
|
-
|
|
162
|
-
winston.debug("[DirAddKbContent] json:", json);
|
|
163
|
-
|
|
164
|
-
const HTTPREQUEST = {
|
|
165
|
-
url: kb_endpoint + "/" + this.projectId + "/kb",
|
|
166
|
-
headers: {
|
|
167
|
-
'Content-Type': 'application/json',
|
|
168
|
-
'Authorization': 'JWT ' + this.context.token
|
|
169
|
-
},
|
|
170
|
-
json: json,
|
|
171
|
-
method: "POST"
|
|
172
|
-
}
|
|
173
|
-
winston.debug("[DirAddKbContent] HttpRequest: ", HTTPREQUEST);
|
|
174
|
-
|
|
175
|
-
httpUtils.request(
|
|
176
|
-
HTTPREQUEST, async (err, resbody) => {
|
|
177
|
-
|
|
178
|
-
if (err) {
|
|
179
|
-
this.logger.error("[DirAddKbContent] error: " + JSON.stringify(err?.response));
|
|
180
|
-
winston.error("[DirAddKbContent] error: ", err?.response);
|
|
181
|
-
if (callback) {
|
|
182
|
-
callback();
|
|
183
|
-
return;
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
else if (resbody.success === true) {
|
|
187
|
-
winston.debug("[DirAddKbContent] resbody: ", resbody);
|
|
188
|
-
callback();
|
|
189
|
-
return;
|
|
190
|
-
} else {
|
|
191
|
-
callback();
|
|
192
|
-
return;
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
)
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
async getKeyFromKbSettings() {
|
|
199
|
-
return new Promise((resolve) => {
|
|
200
|
-
|
|
201
|
-
const KB_HTTPREQUEST = {
|
|
202
|
-
url: this.API_ENDPOINT + "/" + this.context.projectId + "/kbsettings",
|
|
203
|
-
headers: {
|
|
204
|
-
'Content-Type': 'application/json',
|
|
205
|
-
'Authorization': 'JWT ' + this.context.token
|
|
206
|
-
},
|
|
207
|
-
method: "GET"
|
|
208
|
-
}
|
|
209
|
-
winston.debug("DirAskGPTV2 KB HttpRequest", KB_HTTPREQUEST);
|
|
210
|
-
|
|
211
|
-
httpUtils.request(
|
|
212
|
-
KB_HTTPREQUEST, async (err, resbody) => {
|
|
213
|
-
if (err) {
|
|
214
|
-
winston.error("DirAskGPTV2 Get kb settings error ", err?.response?.data);
|
|
215
|
-
resolve(null);
|
|
216
|
-
} else {
|
|
217
|
-
if (!resbody.gptkey) {
|
|
218
|
-
resolve(null);
|
|
219
|
-
} else {
|
|
220
|
-
resolve(resbody.gptkey);
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
)
|
|
225
|
-
})
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
async checkQuoteAvailability() {
|
|
229
|
-
return new Promise((resolve) => {
|
|
230
|
-
|
|
231
|
-
const HTTPREQUEST = {
|
|
232
|
-
url: this.API_ENDPOINT + "/" + this.context.projectId + "/quotes/tokens",
|
|
233
|
-
headers: {
|
|
234
|
-
'Content-Type': 'application/json',
|
|
235
|
-
'Authorization': 'JWT ' + this.context.token
|
|
236
|
-
},
|
|
237
|
-
method: "GET"
|
|
238
|
-
}
|
|
239
|
-
winston.debug("DirAskGPTV2 check quote availability HttpRequest", HTTPREQUEST);
|
|
240
|
-
|
|
241
|
-
httpUtils.request(
|
|
242
|
-
HTTPREQUEST, async (err, resbody) => {
|
|
243
|
-
if (err) {
|
|
244
|
-
winston.error("DirAskGPTV2 Check quote availability err: ", err);
|
|
245
|
-
resolve(true)
|
|
246
|
-
} else {
|
|
247
|
-
if (resbody.isAvailable === true) {
|
|
248
|
-
resolve(true)
|
|
249
|
-
} else {
|
|
250
|
-
resolve(false)
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
)
|
|
255
|
-
})
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
async updateQuote(tokens_usage) {
|
|
259
|
-
return new Promise((resolve, reject) => {
|
|
260
|
-
|
|
261
|
-
const HTTPREQUEST = {
|
|
262
|
-
url: this.API_ENDPOINT + "/" + this.context.projectId + "/quotes/incr/tokens",
|
|
263
|
-
headers: {
|
|
264
|
-
'Content-Type': 'application/json',
|
|
265
|
-
'Authorization': 'JWT ' + this.context.token
|
|
266
|
-
},
|
|
267
|
-
json: tokens_usage,
|
|
268
|
-
method: "POST"
|
|
269
|
-
}
|
|
270
|
-
winston.debug("DirAskGPTV2 update quote HttpRequest ", HTTPREQUEST);
|
|
271
|
-
|
|
272
|
-
httpUtils.request(
|
|
273
|
-
HTTPREQUEST, async (err, resbody) => {
|
|
274
|
-
if (err) {
|
|
275
|
-
winston.error("DirAskGPTV2 Increment tokens quote err: ", err);
|
|
276
|
-
reject(false)
|
|
277
|
-
} else {
|
|
278
|
-
resolve(true);
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
)
|
|
282
|
-
})
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
async getNamespace(name, id) {
|
|
286
|
-
return new Promise((resolve) => {
|
|
287
|
-
const HTTPREQUEST = {
|
|
288
|
-
url: this.API_ENDPOINT + "/" + this.context.projectId + "/kb/namespace/all",
|
|
289
|
-
headers: {
|
|
290
|
-
'Content-Type': 'application/json',
|
|
291
|
-
'Authorization': 'JWT ' + this.context.token
|
|
292
|
-
},
|
|
293
|
-
method: "GET"
|
|
294
|
-
}
|
|
295
|
-
winston.debug("DirAskGPTV2 get all namespaces HttpRequest", HTTPREQUEST);
|
|
296
|
-
httpUtils.request(
|
|
297
|
-
HTTPREQUEST, async (err, namespaces) => {
|
|
298
|
-
if (err) {
|
|
299
|
-
winston.error("DirAskGPTV2 get all namespaces err: ", err);
|
|
300
|
-
resolve(null)
|
|
301
|
-
} else {
|
|
302
|
-
winston.debug("DirAskGPTV2 get all namespaces resbody: ", namespaces);
|
|
303
|
-
if (name) {
|
|
304
|
-
let namespace = namespaces.find(n => n.name === name);
|
|
305
|
-
resolve(namespace);
|
|
306
|
-
} else {
|
|
307
|
-
let namespace = namespaces.find(n => n.id === id);
|
|
308
|
-
resolve(namespace);
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
)
|
|
314
|
-
})
|
|
315
|
-
}
|
|
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
|
-
}
|
|
329
|
-
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
module.exports = { DirAddKbContent }
|
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const { Logger } = require('../../Logger');
|
|
4
|
-
const { TiledeskChatbot } = require('../../engine/TiledeskChatbot');
|
|
5
|
-
const { Filler } = require('../Filler');
|
|
6
|
-
const winston = require('../../utils/winston');
|
|
7
|
-
|
|
8
|
-
let levels = ['error', 'warn', 'info', 'debug'];
|
|
9
|
-
|
|
10
|
-
class DirFlowLog {
|
|
11
|
-
|
|
12
|
-
constructor(context) {
|
|
13
|
-
if (!context) {
|
|
14
|
-
throw new Error('context object is mandatory.');
|
|
15
|
-
}
|
|
16
|
-
this.context = context;
|
|
17
|
-
this.chatbot = context.chatbot;
|
|
18
|
-
this.tdcache = context.tdcache;
|
|
19
|
-
this.requestId = context.requestId;
|
|
20
|
-
this.log = context.log;
|
|
21
|
-
|
|
22
|
-
this.logger = new Logger({ request_id: this.requestId, dev: this.context.supportRequest.draft });
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
execute(directive, callback) {
|
|
26
|
-
winston.verbose("Execute FlowLog directive");
|
|
27
|
-
let action;
|
|
28
|
-
if (directive.action) {
|
|
29
|
-
action = directive.action;
|
|
30
|
-
}
|
|
31
|
-
else {
|
|
32
|
-
winston.warn("DirGptTask Incorrect directive: ", directive);
|
|
33
|
-
callback();
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
this.go(action, () => {
|
|
38
|
-
callback();
|
|
39
|
-
})
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
async go(action, callback) {
|
|
43
|
-
winston.debug("(DirFlowLog) Action: ", action);
|
|
44
|
-
|
|
45
|
-
let level = action.level || 'info';
|
|
46
|
-
if (!levels.includes(level)) {
|
|
47
|
-
winston.warn("Invalid log level " + level);
|
|
48
|
-
this.logger.error("Invalid log level: " + level);
|
|
49
|
-
callback();
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
if (!action.log) {
|
|
53
|
-
winston.debug("Log text is empty");
|
|
54
|
-
callback();
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
let requestVariables = null;
|
|
58
|
-
requestVariables =
|
|
59
|
-
await TiledeskChatbot.allParametersStatic(
|
|
60
|
-
this.tdcache, this.requestId
|
|
61
|
-
);
|
|
62
|
-
|
|
63
|
-
const filler = new Filler();
|
|
64
|
-
const filled_log = filler.fill(action.log, requestVariables);
|
|
65
|
-
winston.debug("(DirFlowLog) fille log: ", filled_log);
|
|
66
|
-
|
|
67
|
-
if (level === 'error') {
|
|
68
|
-
winston.info("Adding log '" + filled_log + "' with level " + level);
|
|
69
|
-
this.logger.error(filled_log);
|
|
70
|
-
}
|
|
71
|
-
else if (level === 'warn') {
|
|
72
|
-
winston.info("Adding log '" + filled_log + "' with level " + level);
|
|
73
|
-
this.logger.warn(filled_log);
|
|
74
|
-
}
|
|
75
|
-
else if (level === 'info') {
|
|
76
|
-
winston.info("Adding log '" + filled_log + "' with level " + level);
|
|
77
|
-
this.logger.info(filled_log);
|
|
78
|
-
}
|
|
79
|
-
else if (level === 'debug') {
|
|
80
|
-
winston.info("Adding log '" + filled_log + "' with level " + level);
|
|
81
|
-
this.logger.debug(filled_log);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
callback();
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
module.exports = { DirFlowLog };
|