wingbot 3.66.3 → 3.66.4

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.66.3",
3
+ "version": "3.66.4",
4
4
  "description": "Enterprise Messaging Bot Conversation Engine",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/Ai.js CHANGED
@@ -10,8 +10,6 @@ const { deepEqual } = require('./utils/deepMapTools');
10
10
  const systemEntities = require('./systemEntities');
11
11
  const CustomEntityDetectionModel = require('./wingbot/CustomEntityDetectionModel');
12
12
 
13
- const DEFAULT_PREFIX = 'default';
14
-
15
13
  let uq = 1;
16
14
 
17
15
  /** @typedef {import('./AiMatching').Compare} Compare */
@@ -49,9 +47,15 @@ let uq = 1;
49
47
  /** @typedef {import('./wingbot/CustomEntityDetectionModel').Phrases} Phrases */
50
48
  /** @typedef {import('./wingbot/CustomEntityDetectionModel').EntityDetector} EntityDetector */
51
49
  /** @typedef {import('./wingbot/CustomEntityDetectionModel').DetectorOptions} DetectorOptions */
50
+ /** @typedef {import('./wingbot/CustomEntityDetectionModel').Entity} Entity */
52
51
  // eslint-disable-next-line max-len
53
52
  /** @typedef {import('./wingbot/CustomEntityDetectionModel').WordEntityDetector} WordEntityDetector */
54
53
 
54
+ /**
55
+ * @callback WordEntityDetectorFactory
56
+ * @returns {Promise<WordEntityDetector>}
57
+ */
58
+
55
59
  /** @typedef {[string,EntityDetector|RegExp,DetectorOptions]} DetectorArgs */
56
60
 
57
61
  /**
@@ -76,7 +80,13 @@ class Ai {
76
80
 
77
81
  /**
78
82
  * @private
79
- * @type {WordEntityDetector}
83
+ * @type {WordEntityDetectorFactory}
84
+ */
85
+ this._wordEntityDetectorFactory = null;
86
+
87
+ /**
88
+ * @private
89
+ * @type {WordEntityDetector|Promise<WordEntityDetector>}
80
90
  */
81
91
  this._wordEntityDetector = null;
82
92
 
@@ -140,6 +150,37 @@ class Ai {
140
150
  * @type {AiMatching}
141
151
  */
142
152
  this.matcher = new AiMatching(this);
153
+
154
+ /**
155
+ * @type {string}
156
+ */
157
+ this.DEFAULT_PREFIX = 'default';
158
+ }
159
+
160
+ /**
161
+ *
162
+ * @param {string} text
163
+ * @param {string|Request} prefix
164
+ * @returns {Promise<Entity[]>}
165
+ */
166
+ async detectEntities (text, prefix = this.DEFAULT_PREFIX) {
167
+ let model;
168
+
169
+ if (typeof prefix === 'string') {
170
+ model = this._keyworders.get(prefix);
171
+ } else {
172
+ const usePrefix = this.getPrefix(this.DEFAULT_PREFIX, prefix);
173
+ model = this._keyworders.get(usePrefix);
174
+ }
175
+
176
+ if (!model) {
177
+ return [];
178
+ }
179
+
180
+ const entities = await model.resolveEntities(text);
181
+
182
+ // @ts-ignore
183
+ return entities;
143
184
  }
144
185
 
145
186
  /**
@@ -192,7 +233,7 @@ class Ai {
192
233
  * @returns {T}
193
234
  * @memberOf Ai
194
235
  */
195
- register (model, prefix = 'default') {
236
+ register (model, prefix = this.DEFAULT_PREFIX) {
196
237
  /** @type {T} */
197
238
  let modelObj;
198
239
 
@@ -210,7 +251,10 @@ class Ai {
210
251
 
211
252
  this._keyworders.set(prefix, modelObj);
212
253
 
213
- modelObj.wordEntityDetector = this._wordEntityDetector;
254
+ if (typeof this._wordEntityDetector === 'function') {
255
+ modelObj.wordEntityDetector = this._wordEntityDetector;
256
+ }
257
+
214
258
  for (const entityArgs of this._detectors.values()) {
215
259
  modelObj.setEntityDetector(...entityArgs);
216
260
  }
@@ -245,9 +289,15 @@ class Ai {
245
289
 
246
290
  /**
247
291
  *
248
- * @param {WordEntityDetector} wordEntityDetector
292
+ * @param {WordEntityDetector|WordEntityDetectorFactory} wordEntityDetector
249
293
  */
250
294
  setWordEntityDetector (wordEntityDetector) {
295
+ if (wordEntityDetector.length === 0) {
296
+ // @ts-ignore
297
+ this._wordEntityDetectorFactory = wordEntityDetector;
298
+ return this;
299
+ }
300
+
251
301
  this._wordEntityDetector = wordEntityDetector;
252
302
 
253
303
  for (const model of this._keyworders.values()) {
@@ -286,7 +336,7 @@ class Ai {
286
336
  *
287
337
  * @param {string} [prefix]
288
338
  */
289
- deregister (prefix = 'default') {
339
+ deregister (prefix = this.DEFAULT_PREFIX) {
290
340
  this._keyworders.delete(prefix);
291
341
  }
292
342
 
@@ -298,7 +348,7 @@ class Ai {
298
348
  * @returns {CustomEntityDetectionModel}
299
349
  * @memberOf Ai
300
350
  */
301
- getModel (prefix = 'default') {
351
+ getModel (prefix = this.DEFAULT_PREFIX) {
302
352
  const model = this._keyworders.get(prefix);
303
353
  if (!model) {
304
354
  throw new Error(`Model ${prefix} not registered yet. Register the model first.`);
@@ -592,7 +642,8 @@ class Ai {
592
642
  };
593
643
  }
594
644
 
595
- _getModelForRequest (req, isConfident = req.isConfidentInput(), defaultModel = DEFAULT_PREFIX) {
645
+ // eslint-disable-next-line max-len
646
+ _getModelForRequest (req, isConfident = req.isConfidentInput(), defaultModel = this.DEFAULT_PREFIX) {
596
647
  if (isConfident) {
597
648
  return null;
598
649
  }
@@ -627,6 +678,35 @@ class Ai {
627
678
  };
628
679
  }
629
680
 
681
+ /**
682
+ *
683
+ * @returns {Promise}
684
+ */
685
+ preloadDetectors () {
686
+ if (this._wordEntityDetectorFactory === null || this._wordEntityDetector) {
687
+ return Promise.resolve();
688
+ }
689
+
690
+ const promise = this._wordEntityDetectorFactory()
691
+ .then((detector) => {
692
+ this._wordEntityDetector = detector;
693
+ for (const model of this._keyworders.values()) {
694
+ model.wordEntityDetector = detector;
695
+ }
696
+ return detector;
697
+ })
698
+ .catch((e) => {
699
+ // eslint-disable-next-line no-console
700
+ console.error('AI.preloadDetectors FAILED', e);
701
+ this._wordEntityDetector = null;
702
+ });
703
+
704
+ // @ts-ignore
705
+ this._wordEntityDetector = promise;
706
+
707
+ return promise;
708
+ }
709
+
630
710
  /**
631
711
  *
632
712
  * @param {Request} req
@@ -710,6 +790,7 @@ class Ai {
710
790
  req.intents = [];
711
791
  return;
712
792
  }
793
+
713
794
  await this._loadIntents(req, res, model);
714
795
  } else {
715
796
  req.intents = [];
@@ -734,6 +815,8 @@ class Ai {
734
815
  }
735
816
  }
736
817
 
818
+ await this.preloadDetectors();
819
+
737
820
  const texts = req.textAlternatives()
738
821
  .filter((alt) => alt.score >= this.sttScoreThreshold)
739
822
  .slice(0, this.sttMaxAlternatives);
package/src/Processor.js CHANGED
@@ -333,6 +333,7 @@ class Processor extends EventEmitter {
333
333
 
334
334
  async _preload () {
335
335
  return Promise.all([
336
+ Ai.ai.preloadDetectors(),
336
337
  // @ts-ignore
337
338
  this.reducer && typeof this.reducer.preload === 'function'
338
339
  // @ts-ignore