wingbot 3.26.0-alpha.4 → 3.26.3
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
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
|
|
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.
|
|
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.
|
|
162
|
-
this.
|
|
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) => `${
|
|
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];
|
|
@@ -379,13 +403,25 @@ class AiMatching {
|
|
|
379
403
|
score: regexpScore - handicap
|
|
380
404
|
};
|
|
381
405
|
}
|
|
406
|
+
|
|
407
|
+
let useState;
|
|
408
|
+
if (stateless || intents.length === 0) {
|
|
409
|
+
useState = Object.entries(req.state)
|
|
410
|
+
.reduce((o, [k, v]) => {
|
|
411
|
+
if (k.startsWith('@')) {
|
|
412
|
+
return o;
|
|
413
|
+
}
|
|
414
|
+
return Object.assign(o, { [k]: v });
|
|
415
|
+
}, {});
|
|
416
|
+
} else {
|
|
417
|
+
useState = req.state;
|
|
418
|
+
}
|
|
419
|
+
|
|
382
420
|
const { score, handicap, matched } = this
|
|
383
421
|
._entityMatching(
|
|
384
422
|
entities,
|
|
385
423
|
reqEntities,
|
|
386
|
-
|
|
387
|
-
? {}
|
|
388
|
-
: req.state
|
|
424
|
+
useState
|
|
389
425
|
);
|
|
390
426
|
|
|
391
427
|
const allOptional = entities.every((e) => e.optional);
|
|
@@ -519,7 +555,7 @@ class AiMatching {
|
|
|
519
555
|
return false;
|
|
520
556
|
}
|
|
521
557
|
entityExists = true;
|
|
522
|
-
return this._entityIsMatching(wanted.op, wanted.compare, e.value);
|
|
558
|
+
return this._entityIsMatching(wanted.op, wanted.compare, e.value, requestState);
|
|
523
559
|
});
|
|
524
560
|
|
|
525
561
|
let requestEntity = requestEntities[index];
|
|
@@ -543,11 +579,13 @@ class AiMatching {
|
|
|
543
579
|
};
|
|
544
580
|
|
|
545
581
|
matching = this
|
|
546
|
-
._entityIsMatching(
|
|
582
|
+
._entityIsMatching(
|
|
583
|
+
wanted.op, wanted.compare, requestEntity.value, requestState
|
|
584
|
+
);
|
|
547
585
|
}
|
|
548
586
|
} else if (!entityExists) {
|
|
549
587
|
matching = this
|
|
550
|
-
._entityIsMatching(wanted.op, wanted.compare, undefined);
|
|
588
|
+
._entityIsMatching(wanted.op, wanted.compare, undefined, requestState);
|
|
551
589
|
}
|
|
552
590
|
|
|
553
591
|
if (!matching && !wanted.optional) {
|
|
@@ -600,7 +638,7 @@ class AiMatching {
|
|
|
600
638
|
};
|
|
601
639
|
}
|
|
602
640
|
|
|
603
|
-
_entityIsMatching (op, compare, value) {
|
|
641
|
+
_entityIsMatching (op, compare, value, requestState) {
|
|
604
642
|
const operation = op || (typeof compare !== 'undefined' ? COMPARE.EQUAL : null);
|
|
605
643
|
|
|
606
644
|
if (typeof value === 'undefined') {
|
|
@@ -609,13 +647,17 @@ class AiMatching {
|
|
|
609
647
|
: false;
|
|
610
648
|
}
|
|
611
649
|
|
|
650
|
+
const useCmp = (compare || []).map((c) => (typeof c === 'function'
|
|
651
|
+
? c(requestState)
|
|
652
|
+
: c));
|
|
653
|
+
|
|
612
654
|
switch (operation) {
|
|
613
655
|
case COMPARE.EQUAL:
|
|
614
|
-
return
|
|
656
|
+
return useCmp.length === 0 || useCmp.includes(`${value}`);
|
|
615
657
|
case COMPARE.NOT_EQUAL:
|
|
616
|
-
return
|
|
658
|
+
return useCmp.length !== 0 && !useCmp.includes(`${value}`);
|
|
617
659
|
case COMPARE.RANGE: {
|
|
618
|
-
const [min, max] =
|
|
660
|
+
const [min, max] = useCmp;
|
|
619
661
|
const normalized = this._normalizeToNumber(value);
|
|
620
662
|
if (normalized === null) {
|
|
621
663
|
return false;
|
|
@@ -626,7 +668,7 @@ class AiMatching {
|
|
|
626
668
|
case COMPARE.LT:
|
|
627
669
|
case COMPARE.GTE:
|
|
628
670
|
case COMPARE.LTE: {
|
|
629
|
-
const [cmp] =
|
|
671
|
+
const [cmp] = useCmp;
|
|
630
672
|
const normalized = this._normalizeToNumber(value);
|
|
631
673
|
if (normalized === null) {
|
|
632
674
|
return false;
|
|
@@ -194,8 +194,14 @@ class CustomEntityDetectionModel {
|
|
|
194
194
|
}
|
|
195
195
|
}
|
|
196
196
|
|
|
197
|
-
_escapeRegex (string) {
|
|
198
|
-
|
|
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
|
/**
|
|
@@ -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;
|