wingbot 3.26.0-alpha.2 → 3.26.1

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.2",
3
+ "version": "3.26.1",
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
@@ -109,7 +109,7 @@ class BotApp {
109
109
  * @returns {Promise<string>}
110
110
  */
111
111
  static signBody (body, secret, appId) {
112
- return BotApp.signBody(body, secret, appId);
112
+ return BotAppSender.signBody(body, secret, appId);
113
113
  }
114
114
 
115
115
  /**
@@ -194,8 +194,14 @@ class CustomEntityDetectionModel {
194
194
  }
195
195
  }
196
196
 
197
- _escapeRegex (string) {
198
- return string.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
197
+ _escapeRegex (string, shouldReplaceDiacritics) {
198
+ const ret = string.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
199
+
200
+ if (!shouldReplaceDiacritics) {
201
+ return ret;
202
+ }
203
+
204
+ return replaceDiacritics(ret);
199
205
  }
200
206
 
201
207
  /**
@@ -284,12 +290,12 @@ class CustomEntityDetectionModel {
284
290
  * @param {string} text
285
291
  * @param {string} [singleEntity]
286
292
  * @param {string[]} expectedEntities
293
+ * @param {DetectedEntity[]} [entities] - previously detected entities to include
287
294
  * @returns {Promise<DetectedEntity[]>}
288
295
  */
289
- async resolveEntities (text, singleEntity = null, expectedEntities = []) {
296
+ async resolveEntities (text, singleEntity = null, expectedEntities = [], entities = []) {
290
297
  const resolved = new Set();
291
298
  let missing = Array.from(this._entityDetectors.keys());
292
- const entities = [];
293
299
 
294
300
  while (missing.length !== 0) {
295
301
  let detect = [];
@@ -426,7 +432,7 @@ class CustomEntityDetectionModel {
426
432
  let replaced = source.replace(/@[A-Z0-9-]+/g, (value) => {
427
433
  const matchingEntities = entities
428
434
  .filter((e) => `@${e.entity.toUpperCase()}` === value)
429
- .map((e) => this._escapeRegex(e.text));
435
+ .map((e) => this._escapeRegex(e.text, options.replaceDiacritics));
430
436
 
431
437
  if (matchingEntities.length === 0) {
432
438
  return value;