@tiledesk/tiledesk-tybot-connector 0.5.0 → 2.0.0

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.
Files changed (100) hide show
  1. package/ExtApi.js +4 -83
  2. package/ExtUtil.js +0 -1
  3. package/Logger.js +105 -0
  4. package/TdCache.js +5 -3
  5. package/TiledeskClientTest.js +5 -28
  6. package/TiledeskExpression.js +6 -47
  7. package/{models → engine}/IntentForm.js +30 -44
  8. package/{models → engine}/IntentsMachineFactory.js +5 -4
  9. package/{models → engine}/MongodbBotsDataSource.js +31 -55
  10. package/{models → engine}/MongodbIntentsMachine.js +6 -5
  11. package/{models → engine}/TiledeskChatbot.js +82 -259
  12. package/{models → engine}/TiledeskChatbotConst.js +9 -0
  13. package/{models → engine}/TiledeskIntentsMachine.js +5 -15
  14. package/{models → engine/mock}/MockBotsDataSource.js +3 -19
  15. package/{models → engine/mock}/MockTdCache.js +0 -9
  16. package/index.js +103 -380
  17. package/logs/app.log +12723 -277
  18. package/logs/app1.log +62258 -0
  19. package/logs/app2.log +46280 -0
  20. package/logs/app3.log +31686 -0
  21. package/logs/app4.log +46163 -0
  22. package/logs/app5.log +33173 -0
  23. package/models/faq.js +2 -5
  24. package/package.json +5 -2
  25. package/{TiledeskServices → services}/AIService.js +4 -3
  26. package/{models/faqKbService.js → services/FaqKbService.js} +1 -2
  27. package/{models/faqService.js → services/FaqService.js} +4 -3
  28. package/services/IntegrationService.js +43 -0
  29. package/services/TilebotService.js +47 -0
  30. package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +49 -135
  31. package/tiledeskChatbotPlugs/FillParamsChatbotPlug.js +8 -14
  32. package/tiledeskChatbotPlugs/Filler.js +0 -1
  33. package/tiledeskChatbotPlugs/MarkbotChatbotPlug.js +12 -20
  34. package/tiledeskChatbotPlugs/MessagePipeline.js +5 -15
  35. package/tiledeskChatbotPlugs/SplitsChatbotPlug.js +4 -13
  36. package/tiledeskChatbotPlugs/TiledeskRequestVariables.js +1 -1
  37. package/tiledeskChatbotPlugs/WebhookChatbotPlug.js +21 -89
  38. package/tiledeskChatbotPlugs/directives/DEPRECATED_DirSetAttribute.js +1 -1
  39. package/tiledeskChatbotPlugs/directives/DirAddTags.js +23 -140
  40. package/tiledeskChatbotPlugs/directives/DirAiPrompt.js +42 -128
  41. package/tiledeskChatbotPlugs/directives/DirAskGPT.js +37 -127
  42. package/tiledeskChatbotPlugs/directives/DirAskGPTV2.js +52 -145
  43. package/tiledeskChatbotPlugs/directives/DirAssign.js +12 -23
  44. package/tiledeskChatbotPlugs/directives/DirAssignFromFunction.js +16 -12
  45. package/tiledeskChatbotPlugs/directives/DirAssistant.js +56 -198
  46. package/tiledeskChatbotPlugs/directives/DirBrevo.js +41 -101
  47. package/tiledeskChatbotPlugs/directives/DirCaptureUserReply.js +9 -19
  48. package/tiledeskChatbotPlugs/directives/DirClearTranscript.js +1 -1
  49. package/tiledeskChatbotPlugs/directives/DirClose.js +4 -2
  50. package/tiledeskChatbotPlugs/directives/DirCode.js +16 -23
  51. package/tiledeskChatbotPlugs/directives/DirCondition.js +19 -25
  52. package/tiledeskChatbotPlugs/directives/DirConnectBlock.js +9 -82
  53. package/tiledeskChatbotPlugs/directives/DirContactUpdate.js +10 -22
  54. package/tiledeskChatbotPlugs/directives/DirCustomerio.js +34 -89
  55. package/tiledeskChatbotPlugs/directives/DirDeflectToHelpCenter.js +17 -22
  56. package/tiledeskChatbotPlugs/directives/DirDeleteVariable.js +8 -8
  57. package/tiledeskChatbotPlugs/directives/DirDepartment.js +13 -12
  58. package/tiledeskChatbotPlugs/directives/DirDisableInputText.js +2 -6
  59. package/tiledeskChatbotPlugs/directives/DirFireTiledeskEvent.js +4 -4
  60. package/tiledeskChatbotPlugs/directives/DirForm.js +17 -24
  61. package/tiledeskChatbotPlugs/directives/DirGptTask.js +49 -139
  62. package/tiledeskChatbotPlugs/directives/DirGptTask_OLD.js +8 -53
  63. package/tiledeskChatbotPlugs/directives/DirHubspot.js +31 -91
  64. package/tiledeskChatbotPlugs/directives/DirIfOnlineAgents.js +19 -26
  65. package/tiledeskChatbotPlugs/directives/DirIfOnlineAgentsV2.js +24 -78
  66. package/tiledeskChatbotPlugs/directives/DirIfOpenHours.js +17 -68
  67. package/tiledeskChatbotPlugs/directives/DirIntent.js +6 -100
  68. package/tiledeskChatbotPlugs/directives/DirJSONCondition.js +15 -14
  69. package/tiledeskChatbotPlugs/directives/DirLockIntent.js +7 -12
  70. package/tiledeskChatbotPlugs/directives/DirMake.js +21 -53
  71. package/tiledeskChatbotPlugs/directives/DirMessage.js +35 -28
  72. package/tiledeskChatbotPlugs/directives/DirMessageToBot.js +8 -87
  73. package/tiledeskChatbotPlugs/directives/DirMoveToAgent.js +6 -4
  74. package/tiledeskChatbotPlugs/directives/DirMoveToUnassigned.js +6 -4
  75. package/tiledeskChatbotPlugs/directives/DirQapla.js +26 -120
  76. package/tiledeskChatbotPlugs/directives/DirRandomReply.js +11 -19
  77. package/tiledeskChatbotPlugs/directives/DirRemoveCurrentBot.js +4 -0
  78. package/tiledeskChatbotPlugs/directives/DirReplaceBot.js +5 -2
  79. package/tiledeskChatbotPlugs/directives/DirReplaceBotV2.js +12 -58
  80. package/tiledeskChatbotPlugs/directives/DirReplaceBotV3.js +11 -57
  81. package/tiledeskChatbotPlugs/directives/DirReply.js +36 -30
  82. package/tiledeskChatbotPlugs/directives/DirReplyV2.js +30 -72
  83. package/tiledeskChatbotPlugs/directives/DirSendEmail.js +9 -10
  84. package/tiledeskChatbotPlugs/directives/DirSendWhatsapp.js +15 -40
  85. package/tiledeskChatbotPlugs/directives/DirSetAttribute.js +8 -5
  86. package/tiledeskChatbotPlugs/directives/DirSetAttributeV2.js +25 -113
  87. package/tiledeskChatbotPlugs/directives/DirSetConversationTags.js +14 -13
  88. package/tiledeskChatbotPlugs/directives/DirUnlockIntent.js +3 -3
  89. package/tiledeskChatbotPlugs/directives/DirWait.js +5 -6
  90. package/tiledeskChatbotPlugs/directives/DirWebRequest.js +14 -49
  91. package/tiledeskChatbotPlugs/directives/DirWebRequestV2.js +22 -77
  92. package/tiledeskChatbotPlugs/directives/DirWebRequestV2_old.js +1 -1
  93. package/tiledeskChatbotPlugs/directives/DirWebResponse.js +11 -14
  94. package/tiledeskChatbotPlugs/directives/DirWhatsappByAttribute.js +13 -42
  95. package/tiledeskChatbotPlugs/directives/Directives.js +0 -3
  96. package/utils/HttpUtils.js +128 -0
  97. package/{models → utils}/TiledeskChatbotUtil.js +164 -234
  98. package/utils/winston.js +42 -0
  99. package/TiledeskServices/utils.js +0 -99
  100. /package/{models → engine/mock}/MockIntentsMachine.js +0 -0
@@ -1,12 +1,15 @@
1
1
  const axios = require("axios").default;
2
- const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
2
+ const { TiledeskChatbot } = require('../../engine/TiledeskChatbot');
3
3
  const { Filler } = require('../Filler');
4
4
  let https = require("https");
5
5
  const { DirIntent } = require("./DirIntent");
6
- const { TiledeskChatbotConst } = require("../../models/TiledeskChatbotConst");
7
- const { TiledeskChatbotUtil } = require("../../models/TiledeskChatbotUtil");
6
+ const { TiledeskChatbotConst } = require("../../engine/TiledeskChatbotConst");
7
+ const { TiledeskChatbotUtil } = require("../../utils/TiledeskChatbotUtil");
8
8
  const assert = require("assert");
9
9
  require('dotenv').config();
10
+ const winston = require('../../utils/winston');
11
+ const httpUtils = require("../../utils/HttpUtils");
12
+ const integrationService = require("../../services/IntegrationService");
10
13
 
11
14
  class DirAskGPTV2 {
12
15
 
@@ -18,19 +21,21 @@ class DirAskGPTV2 {
18
21
  this.chatbot = context.chatbot;
19
22
  this.tdcache = this.context.tdcache;
20
23
  this.requestId = this.context.requestId;
24
+ this.projectId = this.context.projectId;
25
+ this.token = this.context.token;
21
26
  this.intentDir = new DirIntent(context);
22
27
  this.API_ENDPOINT = this.context.API_ENDPOINT;
23
28
  this.log = context.log;
24
29
  }
25
30
 
26
31
  execute(directive, callback) {
27
- if (this.log) { console.log("AskGPT directive: ", directive); }
32
+ winston.debug("DirAskGPTV2 directive: ", directive);
28
33
  let action;
29
34
  if (directive.action) {
30
35
  action = directive.action;
31
36
  }
32
37
  else {
33
- console.error("Incorrect directive: ", JSON.stringify(directive));
38
+ winston.debug("DirAskGPTV2 Incorrect directive: ", directive);
34
39
  callback();
35
40
  return;
36
41
  }
@@ -40,9 +45,9 @@ class DirAskGPTV2 {
40
45
  }
41
46
 
42
47
  async go(action, callback) {
43
- if (this.log) { console.log("DirAskGPT action:", JSON.stringify(action)); }
48
+ winston.debug("DirAskGPTV2 action:", action);
44
49
  if (!this.tdcache) {
45
- console.error("Error: DirAskGPT tdcache is mandatory");
50
+ winston.error("DirAskGPTV2 Error: tdcache is mandatory");
46
51
  callback();
47
52
  return;
48
53
  }
@@ -53,12 +58,11 @@ class DirAskGPTV2 {
53
58
  let trueIntentAttributes = action.trueIntentAttributes;
54
59
  let falseIntentAttributes = action.falseIntentAttributes;
55
60
 
56
- if (this.log) {
57
- console.log("DirAskGPT trueIntent", trueIntent)
58
- console.log("DirAskGPT falseIntent", falseIntent)
59
- console.log("DirAskGPT trueIntentAttributes", trueIntentAttributes)
60
- console.log("DirAskGPT falseIntentAttributes", falseIntentAttributes)
61
- }
61
+ winston.debug("DirAskGPTV2 trueIntent", trueIntent)
62
+ winston.debug("DirAskGPTV2 falseIntent", falseIntent)
63
+ winston.debug("DirAskGPTV2 trueIntentAttributes", trueIntentAttributes)
64
+ winston.debug("DirAskGPTV2 falseIntentAttributes", falseIntentAttributes)
65
+
62
66
 
63
67
  // default values
64
68
  let answer = "No answers";
@@ -83,7 +87,7 @@ class DirAskGPTV2 {
83
87
  let source = null;
84
88
 
85
89
  if (!action.question || action.question === '') {
86
- console.error("Error: DirAskGPT question attribute is mandatory. Executing condition false...");
90
+ winston.error("DirAskGPTV2 Error: question attribute is mandatory. Executing condition false...");
87
91
  await this.#assignAttributes(action, answer, source);
88
92
  if (falseIntent) {
89
93
  await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
@@ -131,33 +135,33 @@ class DirAskGPTV2 {
131
135
  this.context.requestId,
132
136
  TiledeskChatbotConst.REQ_TRANSCRIPT_KEY
133
137
  )
134
- if (this.log) { console.log("DirAskGPT transcript string: ", transcript_string) }
138
+ winston.debug("DirAskGPTV2 transcript string: " + transcript_string)
135
139
 
136
140
  if (transcript_string) {
137
141
  transcript = await TiledeskChatbotUtil.transcriptJSON(transcript_string);
138
- if (this.log) { console.log("DirAskGPT transcript ", transcript) }
142
+ winston.debug("DirAskGPTV2 transcript ", transcript)
139
143
  } else {
140
- if (this.log) { console.log("DirAskGPT transcript_string is undefined. Skip JSON translation for chat history") }
144
+ winston.verbose("DirAskGPT transcript_string is undefined. Skip JSON translation for chat history")
141
145
  }
142
146
  }
143
147
 
144
148
  const kb_endpoint = process.env.KB_ENDPOINT_QA
145
- if (this.log) { console.log("DirAskGPT KbEndpoint URL: ", kb_endpoint); }
149
+ winston.verbose("DirAskGPTV2 KbEndpoint URL: " + kb_endpoint);
146
150
 
147
- let key = await this.getKeyFromIntegrations();
151
+ let key = await integrationService.getKeyFromIntegrations(this.projectId, 'openai', this.token);
148
152
  if (!key) {
149
- if (this.log) { console.log("DirAskGPT - Key not found in Integrations. Searching in kb settings..."); }
153
+ winston.verbose("DirAskGPTV2 - Key not found in Integrations. Searching in kb settings...");
150
154
  key = await this.getKeyFromKbSettings();
151
155
  }
152
156
 
153
157
  if (!key) {
154
- if (this.log) { console.log("DirAskGPT - Retrieve public gptkey")}
158
+ winston.verbose("DirAskGPTV2 - Retrieve public gptkey")
155
159
  key = process.env.GPTKEY;
156
160
  publicKey = true;
157
161
  }
158
162
 
159
163
  if (!key) {
160
- console.error("Error: DirAskGPT gptkey is mandatory");
164
+ winston.info("DirAskGPTV2 Error: gptkey is mandatory");
161
165
  await this.#assignAttributes(action, answer);
162
166
  if (falseIntent) {
163
167
  await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
@@ -171,7 +175,7 @@ class DirAskGPTV2 {
171
175
  if (publicKey === true) {
172
176
  let keep_going = await this.checkQuoteAvailability();
173
177
  if (keep_going === false) {
174
- if (this.log) { console.log("DirAskGPT - Quota exceeded for tokens. Skip the action")}
178
+ winston.verbose("DirAskGPTV2 - Quota exceeded for tokens. Skip the action")
175
179
  await this.chatbot.addParameter("flowError", "AskGPT Error: tokens quota exceeded");
176
180
  await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
177
181
  callback(true);
@@ -186,7 +190,7 @@ class DirAskGPTV2 {
186
190
  const filled_namespace = filler.fill(action.namespace, requestVariables)
187
191
  ns = await this.getNamespace(filled_namespace, null);
188
192
  namespace = ns?.id;
189
- if (this.log) { console.log("DirAskGPT - Retrieved namespace id from name ", namespace); }
193
+ winston.verbose("DirAskGPTV2 - Retrieved namespace id from name " + namespace);
190
194
  } else {
191
195
  ns = await this.getNamespace(null, namespace);
192
196
  }
@@ -210,7 +214,7 @@ class DirAskGPTV2 {
210
214
  }
211
215
 
212
216
  if (!namespace) {
213
- console.log("DirAskGPT - Error: namespace is undefined")
217
+ winston.verbose("DirAskGPTV2 - Error: namespace is undefined")
214
218
  if (falseIntent) {
215
219
  await this.chatbot.addParameter("flowError", "AskGPT Error: namespace is undefined");
216
220
  await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
@@ -251,7 +255,7 @@ class DirAskGPTV2 {
251
255
  json.chat_history_dict = await this.transcriptToLLM(transcript);
252
256
  }
253
257
 
254
- if (this.log) { console.log("DirAskGPT json:", json); }
258
+ winston.debug("DirAskGPTV2 json:", json);
255
259
 
256
260
  const HTTPREQUEST = {
257
261
  url: kb_endpoint + "/qa",
@@ -262,20 +266,13 @@ class DirAskGPTV2 {
262
266
  json: json,
263
267
  method: "POST"
264
268
  }
265
- if (this.log) { console.log("DirAskGPT HTTPREQUEST", HTTPREQUEST); }
269
+ winston.debug("DirAskGPTV2 HttpRequest: ", HTTPREQUEST);
266
270
 
267
- this.#myrequest(
271
+ httpUtils.request(
268
272
  HTTPREQUEST, async (err, resbody) => {
269
- if (this.log && err) {
270
- console.log("DirAskGPT error: ", err);
271
- }
272
- if (this.log) { console.log("DirAskGPT resbody:", resbody); }
273
273
 
274
- // let answer = resbody.answer;
275
- // let source = resbody.source;
276
- // await this.#assignAttributes(action, answer, source);
277
-
278
274
  if (err) {
275
+ winston.error("DirAskGPTV2 error: ", err?.respose);
279
276
  await this.#assignAttributes(action, answer, source);
280
277
  if (callback) {
281
278
  if (falseIntent) {
@@ -288,7 +285,7 @@ class DirAskGPTV2 {
288
285
  }
289
286
  }
290
287
  else if (resbody.success === true) {
291
-
288
+ winston.debug("DirAskGPTV2 resbody: ", resbody);
292
289
  await this.#assignAttributes(action, resbody.answer, resbody.source);
293
290
  if (publicKey === true) {
294
291
  let tokens_usage = {
@@ -337,7 +334,7 @@ class DirAskGPTV2 {
337
334
  })
338
335
  }
339
336
  else {
340
- if (this.log) { console.log("No trueIntentDirective specified"); }
337
+ winston.debug("DirAskGPTV2 No trueIntentDirective specified");
341
338
  if (callback) {
342
339
  callback();
343
340
  }
@@ -352,7 +349,7 @@ class DirAskGPTV2 {
352
349
  });
353
350
  }
354
351
  else {
355
- if (this.log) { console.log("No falseIntentDirective specified"); }
352
+ winston.debug("DirAskGPTV2 No falseIntentDirective specified");
356
353
  if (callback) {
357
354
  callback();
358
355
  }
@@ -361,11 +358,9 @@ class DirAskGPTV2 {
361
358
  }
362
359
 
363
360
  async #assignAttributes(action, answer, source) {
364
- if (this.log) {
365
- console.log("assignAttributes action:", action)
366
- console.log("assignAttributes answer:", answer)
367
- console.log("assignAttributes source:", source)
368
- }
361
+ winston.debug("DirAskGPTV2assignAttributes action: ", action)
362
+ winston.debug("DirAskGPTV2assignAttributes answer: ", answer)
363
+ winston.debug("DirAskGPTV2assignAttributes source: ", source)
369
364
  if (this.context.tdcache) {
370
365
  if (action.assignReplyTo && answer) {
371
366
  await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignReplyTo, answer);
@@ -373,96 +368,9 @@ class DirAskGPTV2 {
373
368
  if (action.assignSourceTo && source) {
374
369
  await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignSourceTo, source);
375
370
  }
376
- // Debug log
377
- if (this.log) {
378
- const all_parameters = await TiledeskChatbot.allParametersStatic(this.context.tdcache, this.context.requestId);
379
- for (const [key, value] of Object.entries(all_parameters)) {
380
- if (this.log) { console.log("(askgpt) request parameter:", key, "value:", value, "type:", typeof value) }
381
- }
382
- }
383
371
  }
384
372
  }
385
373
 
386
- #myrequest(options, callback) {
387
- if (this.log) {
388
- console.log("API URL:", options.url);
389
- console.log("** Options:", JSON.stringify(options));
390
- }
391
- let axios_options = {
392
- url: options.url,
393
- method: options.method,
394
- params: options.params,
395
- headers: options.headers
396
- }
397
- if (options.json !== null) {
398
- axios_options.data = options.json
399
- }
400
- if (this.log) {
401
- console.log("axios_options:", JSON.stringify(axios_options));
402
- }
403
- if (options.url.startsWith("https:")) {
404
- const httpsAgent = new https.Agent({
405
- rejectUnauthorized: false,
406
- });
407
- axios_options.httpsAgent = httpsAgent;
408
- }
409
- axios(axios_options)
410
- .then((res) => {
411
- if (this.log) {
412
- console.log("Response for url:", options.url);
413
- console.log("Response headers:\n", JSON.stringify(res.headers));
414
- }
415
- if (res && res.status == 200 && res.data) {
416
- if (callback) {
417
- callback(null, res.data);
418
- }
419
- }
420
- else {
421
- if (callback) {
422
- callback(new Error("Response status is not 200"), null);
423
- }
424
- }
425
- })
426
- .catch((error) => {
427
- console.error("(DirAskGPT) Axios error: ", JSON.stringify(error));
428
- if (callback) {
429
- callback(error, null);
430
- }
431
- });
432
- }
433
-
434
- async getKeyFromIntegrations() {
435
- return new Promise((resolve) => {
436
-
437
- const INTEGRATIONS_HTTPREQUEST = {
438
- url: this.API_ENDPOINT + "/" + this.context.projectId + "/integration/name/openai",
439
- headers: {
440
- 'Content-Type': 'application/json',
441
- 'Authorization': 'JWT ' + this.context.token
442
- },
443
- method: "GET"
444
- }
445
- if (this.log) { console.log("DirAskGPT INTEGRATIONS_HTTPREQUEST ", INTEGRATIONS_HTTPREQUEST) }
446
-
447
- this.#myrequest(
448
- INTEGRATIONS_HTTPREQUEST, async (err, integration) => {
449
- if (err) {
450
- if (this.log) { console.error("DirAskGPT Get integrations error ", err); }
451
- resolve(null);
452
- } else {
453
-
454
- if (integration &&
455
- integration.value) {
456
- resolve(integration.value.apikey)
457
- }
458
- else {
459
- resolve(null)
460
- }
461
- }
462
- })
463
- })
464
- }
465
-
466
374
  async getKeyFromKbSettings() {
467
375
  return new Promise((resolve) => {
468
376
 
@@ -474,12 +382,12 @@ class DirAskGPTV2 {
474
382
  },
475
383
  method: "GET"
476
384
  }
477
- if (this.log) { console.log("DirAskGPT KB_HTTPREQUEST", KB_HTTPREQUEST); }
385
+ winston.debug("DirAskGPTV2 KB HttpRequest", KB_HTTPREQUEST);
478
386
 
479
- this.#myrequest(
387
+ httpUtils.request(
480
388
  KB_HTTPREQUEST, async (err, resbody) => {
481
389
  if (err) {
482
- if (this.log) { console.error("DirAskGPT Get kb settings error ", err); }
390
+ winston.error("DirAskGPTV2 Get kb settings error ", err?.response?.data);
483
391
  resolve(null);
484
392
  } else {
485
393
  if (!resbody.gptkey) {
@@ -504,12 +412,12 @@ class DirAskGPTV2 {
504
412
  },
505
413
  method: "GET"
506
414
  }
507
- if (this.log) { console.log("DirAskGPT check quote availability HTTPREQUEST", HTTPREQUEST); }
415
+ winston.debug("DirAskGPTV2 check quote availability HttpRequest", HTTPREQUEST);
508
416
 
509
- this.#myrequest(
417
+ httpUtils.request(
510
418
  HTTPREQUEST, async (err, resbody) => {
511
419
  if (err) {
512
- console.error("(httprequest) DirAskGPT Check quote availability err: ", err);
420
+ winston.error("DirAskGPTV2 Check quote availability err: ", err);
513
421
  resolve(true)
514
422
  } else {
515
423
  if (resbody.isAvailable === true) {
@@ -535,15 +443,14 @@ class DirAskGPTV2 {
535
443
  json: tokens_usage,
536
444
  method: "POST"
537
445
  }
538
- if (this.log) { console.log("DirAskGPT check quote availability HTTPREQUEST", HTTPREQUEST); }
446
+ winston.debug("DirAskGPTV2 update quote HttpRequest ", HTTPREQUEST);
539
447
 
540
- this.#myrequest(
448
+ httpUtils.request(
541
449
  HTTPREQUEST, async (err, resbody) => {
542
450
  if (err) {
543
- console.error("(httprequest) DirAskGPT Increment tokens quote err: ", err);
451
+ winston.error("DirAskGPTV2 Increment tokens quote err: ", err);
544
452
  reject(false)
545
453
  } else {
546
- // console.log("(httprequest) DirAskGPT Increment token quote resbody: ", resbody);
547
454
  resolve(true);
548
455
  }
549
456
  }
@@ -609,14 +516,14 @@ class DirAskGPTV2 {
609
516
  },
610
517
  method: "GET"
611
518
  }
612
- if (this.log) { console.log("DirAskGPT get all namespaces HTTPREQUEST", HTTPREQUEST); }
613
- this.#myrequest(
519
+ winston.debug("DirAskGPTV2 get all namespaces HttpRequest", HTTPREQUEST);
520
+ httpUtils.request(
614
521
  HTTPREQUEST, async (err, namespaces) => {
615
522
  if (err) {
616
- console.error("(httprequest) DirAskGPT get all namespaces err: ", err);
523
+ winston.error("DirAskGPTV2 get all namespaces err: ", err);
617
524
  resolve(null)
618
525
  } else {
619
- if (this.log) { console.log("(httprequest) DirAskGPT get all namespaces resbody: ", namespaces); }
526
+ winston.debug("DirAskGPTV2 get all namespaces resbody: ", namespaces);
620
527
  if (name) {
621
528
  let namespace = namespaces.find(n => n.name === name);
622
529
  resolve(namespace);
@@ -1,7 +1,8 @@
1
- const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
1
+ const { TiledeskChatbot } = require('../../engine/TiledeskChatbot');
2
2
  const { TiledeskExpression } = require('../../TiledeskExpression');
3
3
  const ms = require('minimist-string');
4
4
  const { Filler } = require('../Filler');
5
+ const winston = require('../../utils/winston')
5
6
 
6
7
  class DirAssign {
7
8
 
@@ -14,6 +15,7 @@ class DirAssign {
14
15
  }
15
16
 
16
17
  execute(directive, callback) {
18
+ winston.verbose("Execute Assign directive");
17
19
  let action;
18
20
  if (directive.action) {
19
21
  action = directive.action
@@ -21,88 +23,75 @@ class DirAssign {
21
23
  else if (directive.parameter) {
22
24
  let params;
23
25
  params = this.parseParams(directive.parameter);
24
- // console.log("DirAssign params:", params);
25
26
  action = {
26
27
  expression: params.expression,
27
28
  assignTo: params.assignTo
28
29
  }
29
- // console.log("DirAssign action:", action);
30
30
  }
31
31
  else {
32
+ winston.warn("Incorrect directive: ", directive);
32
33
  callback();
33
34
  return;
34
35
  }
35
- // console.log("go DirAssign with action:", action);
36
36
  this.go(action, () => {
37
37
  callback();
38
38
  });
39
39
  }
40
40
 
41
41
  async go(action, callback) {
42
- // console.log("DirAssign action processing:", action)
43
- // const expression = action.body.expression;
44
- // const assignTo = action.body.assignTo;
42
+ winston.debug("(DirAssign) Action: ", action);
45
43
  const expression = action.expression;
46
44
  const assignTo = action.assignTo;
47
45
  if (assignTo === null || expression === null) {
48
- if (this.log) {console.log("Invalid expression or assignTo parameters");}
46
+ winston.warn("Invalid expression or assignTo parameters");
49
47
  callback();
50
48
  return;
51
49
  }
52
50
 
53
51
  if (this.context.tdcache) {
54
- if (this.log) {console.log("(DirAssign) this.requestId:", this.context.requestId);}
52
+ winston.debug("(DirAssign) this.requestId: " + this.context.requestId);
55
53
  let variables =
56
54
  await TiledeskChatbot.allParametersStatic(
57
55
  this.context.tdcache, this.context.requestId);
58
56
  // filling
59
57
  let variableName;
60
58
  const filler = new Filler();
61
- // console.log("assign variable name:", variableName);
62
59
  variableName = filler.fill(assignTo, variables);
63
- // console.log("assign variable name (after filling):", variableName);
64
60
 
65
- if (this.log) {console.log("(DirAssign) Variables:", JSON.stringify(variables));}
61
+ winston.debug("(DirAssign) Variables: ", variables);
66
62
  // const value = await new TiledeskExpression().evaluateExpression(expression, variables);
67
63
  const value = new TiledeskExpression().evaluateExpression(expression, variables);
68
- if (this.log) {console.log("(DirAssign) executed expression:", expression, "value:", value);}
64
+ winston.debug("(DirAssign) executed expression: " + expression + " value: " + value);
69
65
  await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, variableName, value);
70
66
  if (this.log) {
71
- console.log("(DirAssign) Assigned:", variableName, "=", value);
67
+ winston.debug("(DirAssign) Assigned: " + variableName + " = " + value);
72
68
  const all_parameters = await TiledeskChatbot.allParametersStatic(this.context.tdcache, this.context.requestId);
73
69
  for (const [key, value] of Object.entries(all_parameters)) {
74
70
  const value_type = typeof value;
75
- if (this.log) {console.log("(DirAssign) request parameter:", key, "value:", value, "type:", value_type)}
71
+ winston.debug("(DirAssign) request parameter: " + key + " value: " + value + " type: " + value_type)
76
72
  }
77
73
  }
78
74
 
79
75
  callback();
80
76
  }
81
77
  else {
82
- console.error("(DirAssign) No this.context.tdcache");
78
+ winston.error("(DirAssign) No this.context.tdcache");
83
79
  callback();
84
80
  }
85
81
  }
86
82
 
87
83
  // async evaluateExpression(_expression, variables) {
88
84
  // let expression = _expression.replace("$", "$data.");
89
- // console.log("Evaluating expression:", expression);
90
- // console.log("With variables:", variables);
91
85
  // const result = new TiledeskExpression().evaluate(expression, variables);
92
- // console.log("Expression result:", result);
93
86
  // return result;
94
87
  // }
95
88
 
96
89
  parseParams(directive_parameter) {
97
- // console.log("Parsing directive_parameter:", directive_parameter);
98
90
  let expression = null;
99
91
  let assignTo = null;
100
92
  const params = ms(directive_parameter);
101
- // console.log("params dirassign:", params)
102
93
  if (params.expression !== null) {
103
- // console.log("go expression:", params.expression)
104
94
  expression = params.expression
105
- // console.log("got it expression:", expression)
106
95
  }
107
96
  if (params.assignTo !== null) {
108
97
  assignTo = params.assignTo;
@@ -1,6 +1,7 @@
1
1
  const { TiledeskClient } = require('@tiledesk/tiledesk-client');
2
2
  const ms = require('minimist-string');
3
- const { TiledeskChatbot } = require('../../models/TiledeskChatbot.js');
3
+ const { TiledeskChatbot } = require('../../engine/TiledeskChatbot.js');
4
+ const winston = require('../../utils/winston')
4
5
 
5
6
  class DirAssignFromFunction {
6
7
 
@@ -23,6 +24,7 @@ class DirAssignFromFunction {
23
24
  }
24
25
 
25
26
  async execute(directive, callback) {
27
+ winston.verbose("Execute AssignFromFunction directive");
26
28
  let action;
27
29
  if (directive.action) {
28
30
  action = directive.action
@@ -36,29 +38,32 @@ class DirAssignFromFunction {
36
38
  functionName: params.functionName,
37
39
  assignTo: params.assignTo
38
40
  }
41
+ } else {
42
+ winston.warn("Incorrect directive: ", directive);
43
+ callback();
44
+ return;
39
45
  }
40
- console.log("execute assign");
41
46
  this.go(action, () => {
42
- console.log("assign end.");
43
47
  callback();
44
48
  });
45
49
  }
46
50
 
47
51
  async go(action, callback) {
48
- console.log("invoke function => assign action:", action);
52
+ winston.debug("(DirAssignFromFunction) Action: ", action);
49
53
  // const functionName = action.body.functionName;
50
54
  // const variableName = action.body.assignTo;
51
55
  const functionName = action.functionName;
52
56
  const variableName = action.assignTo;
53
- console.log("functionName:", functionName)
54
- console.log("variableName:", variableName)
57
+
58
+ winston.debug("(DirAssignFromFunction) functionName: " + functionName)
59
+ winston.debug("(DirAssignFromFunction) variableName: " + variableName)
55
60
  this.invoke(functionName, async (err, value) => {
56
61
  if (!err) {
57
62
  await TiledeskChatbot.addParameterStatic(this.tdcache, this.context.requestId, variableName, value);
58
- console.log("Assigned:", value, "to", variableName);
63
+ winston.debug("(DirAssignFromFunction) Assigned: " + value + "to" + variableName);
59
64
  }
60
65
  else {
61
- console.error("invoke() error:", err);
66
+ winston.error("(DirAssignFromFunction) invoke() error: ", err);
62
67
  }
63
68
  callback();
64
69
  });
@@ -68,7 +73,6 @@ class DirAssignFromFunction {
68
73
  switch (functionName) {
69
74
  case "openNow":
70
75
  this.tdClient.openNow((err, result) => {
71
- if (this.log) {console.log("openNow():", result);}
72
76
  if (err) {
73
77
  callback(err);
74
78
  }
@@ -82,13 +86,13 @@ class DirAssignFromFunction {
82
86
  break;
83
87
  case "availableAgents":
84
88
  this.tdClient.getProjectAvailableAgents((err, agents) => {
85
- if (this.log) {console.log("Agents on 'open'", agents);}
89
+ winston.debug("(DirAssignFromFunction) Agents on 'open' ", agents);
86
90
  if (err || !agents) {
87
- console.error("Error getting available agents in DirWhenAvailableAgents", err);
91
+ winston.error("(DirAssignFromFunction) Error getting available agents in DirWhenAvailableAgents", err);
88
92
  callback(err, 0);
89
93
  }
90
94
  else {
91
- if (this.log) {console.log("Agents count:", agents.length);}
95
+ winston.debug("(DirAssignFromFunction) Agents count: " + agents.length);
92
96
  // if (agents.length === 0) {
93
97
  // else if (agents.length > 0 && this.checkAgents) {
94
98
  callback(null, agents.length);