wingbot 3.26.0-alpha.1 → 3.26.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wingbot",
3
- "version": "3.26.0-alpha.1",
3
+ "version": "3.26.0",
4
4
  "description": "Enterprise Messaging Bot Conversation Engine",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/AiMatching.js CHANGED
@@ -6,6 +6,18 @@
6
6
  const { replaceDiacritics } = require('./utils/tokenizer');
7
7
  const { vars } = require('./utils/stateVariables');
8
8
 
9
+ /** @typedef {import('handlebars')} Handlebars */
10
+
11
+ /** @type {Handlebars} */
12
+ let handlebars;
13
+ try {
14
+ // @ts-ignore
15
+ handlebars = module.require('handlebars');
16
+ } catch (er) {
17
+ // @ts-ignore
18
+ handlebars = { compile: (text) => () => text };
19
+ }
20
+
9
21
  const FULL_EMOJI_REGEX = /^#((?:[\u2600-\u27bf].?|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff])+)$/;
10
22
  const HAS_CLOSING_HASH = /^#(.+)#$/;
11
23
  const ENTITY_REGEX = /^@([^=><!?]+)(\?)?([!=><]{1,2})?([^=><!]+)?$/i;
@@ -46,7 +58,7 @@ const COMPARE = {
46
58
  * @prop {string} entity - the requested entity
47
59
  * @prop {boolean} [optional] - the match is optional
48
60
  * @prop {Compare} [op] - comparison operation
49
- * @prop {string[]|number[]} [compare] - value to compare with
61
+ * @prop {(string|number|Function)[]} [compare] - value to compare
50
62
  */
51
63
 
52
64
  /**
@@ -138,6 +150,17 @@ class AiMatching {
138
150
  return returnIfEmpty;
139
151
  }
140
152
 
153
+ _hbsOrFn (value, cb) {
154
+ if (typeof value === 'string' && value.match(/\{\{.+\}\}/)) {
155
+ const compiler = handlebars.compile(value);
156
+ return (data) => {
157
+ const res = compiler(data);
158
+ return cb(res);
159
+ };
160
+ }
161
+ return cb(value);
162
+ }
163
+
141
164
  _normalizeComparisonArray (compare, op) {
142
165
  const arr = Array.isArray(compare) ? compare : [compare];
143
166
 
@@ -150,7 +173,7 @@ class AiMatching {
150
173
  const [val] = arr;
151
174
 
152
175
  return [
153
- this._normalizeToNumber(val, 0)
176
+ this._hbsOrFn(val, (r) => this._normalizeToNumber(r, 0))
154
177
  ];
155
178
  }
156
179
 
@@ -158,12 +181,12 @@ class AiMatching {
158
181
  const [min, max] = arr;
159
182
 
160
183
  return [
161
- this._normalizeToNumber(min, -Infinity),
162
- this._normalizeToNumber(max, Infinity)
184
+ this._hbsOrFn(min, (r) => this._normalizeToNumber(r, -Infinity)),
185
+ this._hbsOrFn(max, (r) => this._normalizeToNumber(r, Infinity))
163
186
  ];
164
187
  }
165
188
 
166
- return arr.map((cmp) => `${cmp}`);
189
+ return arr.map((cmp) => this._hbsOrFn(cmp, (r) => `${r}`));
167
190
  }
168
191
 
169
192
  _stringOpToOperation (op) {
@@ -227,7 +250,8 @@ class AiMatching {
227
250
 
228
251
  if (rule.op === COMPARE.EQUAL
229
252
  && rule.compare
230
- && rule.compare.length === 1) {
253
+ && rule.compare.length === 1
254
+ && typeof rule.compare[0] !== 'function') {
231
255
 
232
256
  const key = `@${rule.entity}`;
233
257
  const value = rule.compare[0];
@@ -519,7 +543,7 @@ class AiMatching {
519
543
  return false;
520
544
  }
521
545
  entityExists = true;
522
- return this._entityIsMatching(wanted.op, wanted.compare, e.value);
546
+ return this._entityIsMatching(wanted.op, wanted.compare, e.value, requestState);
523
547
  });
524
548
 
525
549
  let requestEntity = requestEntities[index];
@@ -543,11 +567,13 @@ class AiMatching {
543
567
  };
544
568
 
545
569
  matching = this
546
- ._entityIsMatching(wanted.op, wanted.compare, requestEntity.value);
570
+ ._entityIsMatching(
571
+ wanted.op, wanted.compare, requestEntity.value, requestState
572
+ );
547
573
  }
548
574
  } else if (!entityExists) {
549
575
  matching = this
550
- ._entityIsMatching(wanted.op, wanted.compare, undefined);
576
+ ._entityIsMatching(wanted.op, wanted.compare, undefined, requestState);
551
577
  }
552
578
 
553
579
  if (!matching && !wanted.optional) {
@@ -600,7 +626,7 @@ class AiMatching {
600
626
  };
601
627
  }
602
628
 
603
- _entityIsMatching (op, compare, value) {
629
+ _entityIsMatching (op, compare, value, requestState) {
604
630
  const operation = op || (typeof compare !== 'undefined' ? COMPARE.EQUAL : null);
605
631
 
606
632
  if (typeof value === 'undefined') {
@@ -609,13 +635,17 @@ class AiMatching {
609
635
  : false;
610
636
  }
611
637
 
638
+ const useCmp = (compare || []).map((c) => (typeof c === 'function'
639
+ ? c(requestState)
640
+ : c));
641
+
612
642
  switch (operation) {
613
643
  case COMPARE.EQUAL:
614
- return compare.length === 0 || compare.includes(`${value}`);
644
+ return useCmp.length === 0 || useCmp.includes(`${value}`);
615
645
  case COMPARE.NOT_EQUAL:
616
- return compare.length !== 0 && !compare.includes(`${value}`);
646
+ return useCmp.length !== 0 && !useCmp.includes(`${value}`);
617
647
  case COMPARE.RANGE: {
618
- const [min, max] = compare;
648
+ const [min, max] = useCmp;
619
649
  const normalized = this._normalizeToNumber(value);
620
650
  if (normalized === null) {
621
651
  return false;
@@ -626,7 +656,7 @@ class AiMatching {
626
656
  case COMPARE.LT:
627
657
  case COMPARE.GTE:
628
658
  case COMPARE.LTE: {
629
- const [cmp] = compare;
659
+ const [cmp] = useCmp;
630
660
  const normalized = this._normalizeToNumber(value);
631
661
  if (normalized === null) {
632
662
  return false;
package/src/BotApp.js CHANGED
@@ -100,6 +100,18 @@ class BotApp {
100
100
  this._preferSynchronousResponse = preferSynchronousResponse;
101
101
  }
102
102
 
103
+ /**
104
+ * Get authorization token for wingbot orchestrator
105
+ *
106
+ * @param {string} body
107
+ * @param {string} secret
108
+ * @param {string} appId
109
+ * @returns {Promise<string>}
110
+ */
111
+ static signBody (body, secret, appId) {
112
+ return BotAppSender.signBody(body, secret, appId);
113
+ }
114
+
103
115
  /**
104
116
  * Returns processor plugin, which updates thread context automatically
105
117
  *
@@ -179,7 +179,7 @@ function getSetState (setState = {}, req, res = null, useState = null) {
179
179
  const useValue = typeof value === 'string'
180
180
  ? handlebars.compile(value)(stateData(req, res))
181
181
  .split(/(?<!\\),/g)
182
- .map((v) => v.trim())
182
+ .map((v) => v.replace(/\\,/g, ',').trim())
183
183
  : value;
184
184
  values = toArray(useValue);
185
185
  }
@@ -284,12 +284,12 @@ class CustomEntityDetectionModel {
284
284
  * @param {string} text
285
285
  * @param {string} [singleEntity]
286
286
  * @param {string[]} expectedEntities
287
+ * @param {DetectedEntity[]} [entities] - previously detected entities to include
287
288
  * @returns {Promise<DetectedEntity[]>}
288
289
  */
289
- async resolveEntities (text, singleEntity = null, expectedEntities = []) {
290
+ async resolveEntities (text, singleEntity = null, expectedEntities = [], entities = []) {
290
291
  const resolved = new Set();
291
292
  let missing = Array.from(this._entityDetectors.keys());
292
- const entities = [];
293
293
 
294
294
  while (missing.length !== 0) {
295
295
  let detect = [];