wingbot 3.66.2 → 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.2",
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,23 +233,28 @@ 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
 
199
240
  if (typeof model === 'string') {
200
241
  // @ts-ignore
201
242
  modelObj = new WingbotModel({
202
- model
243
+ model,
244
+ prefix
203
245
  }, this.logger);
204
246
  } else {
205
247
  // @ts-ignore
206
248
  modelObj = model;
249
+ modelObj.prefix = prefix;
207
250
  }
208
251
 
209
252
  this._keyworders.set(prefix, modelObj);
210
253
 
211
- modelObj.wordEntityDetector = this._wordEntityDetector;
254
+ if (typeof this._wordEntityDetector === 'function') {
255
+ modelObj.wordEntityDetector = this._wordEntityDetector;
256
+ }
257
+
212
258
  for (const entityArgs of this._detectors.values()) {
213
259
  modelObj.setEntityDetector(...entityArgs);
214
260
  }
@@ -243,9 +289,15 @@ class Ai {
243
289
 
244
290
  /**
245
291
  *
246
- * @param {WordEntityDetector} wordEntityDetector
292
+ * @param {WordEntityDetector|WordEntityDetectorFactory} wordEntityDetector
247
293
  */
248
294
  setWordEntityDetector (wordEntityDetector) {
295
+ if (wordEntityDetector.length === 0) {
296
+ // @ts-ignore
297
+ this._wordEntityDetectorFactory = wordEntityDetector;
298
+ return this;
299
+ }
300
+
249
301
  this._wordEntityDetector = wordEntityDetector;
250
302
 
251
303
  for (const model of this._keyworders.values()) {
@@ -284,7 +336,7 @@ class Ai {
284
336
  *
285
337
  * @param {string} [prefix]
286
338
  */
287
- deregister (prefix = 'default') {
339
+ deregister (prefix = this.DEFAULT_PREFIX) {
288
340
  this._keyworders.delete(prefix);
289
341
  }
290
342
 
@@ -296,7 +348,7 @@ class Ai {
296
348
  * @returns {CustomEntityDetectionModel}
297
349
  * @memberOf Ai
298
350
  */
299
- getModel (prefix = 'default') {
351
+ getModel (prefix = this.DEFAULT_PREFIX) {
300
352
  const model = this._keyworders.get(prefix);
301
353
  if (!model) {
302
354
  throw new Error(`Model ${prefix} not registered yet. Register the model first.`);
@@ -590,7 +642,8 @@ class Ai {
590
642
  };
591
643
  }
592
644
 
593
- _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) {
594
647
  if (isConfident) {
595
648
  return null;
596
649
  }
@@ -625,6 +678,35 @@ class Ai {
625
678
  };
626
679
  }
627
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
+
628
710
  /**
629
711
  *
630
712
  * @param {Request} req
@@ -708,6 +790,7 @@ class Ai {
708
790
  req.intents = [];
709
791
  return;
710
792
  }
793
+
711
794
  await this._loadIntents(req, res, model);
712
795
  } else {
713
796
  req.intents = [];
@@ -732,6 +815,8 @@ class Ai {
732
815
  }
733
816
  }
734
817
 
818
+ await this.preloadDetectors();
819
+
735
820
  const texts = req.textAlternatives()
736
821
  .filter((alt) => alt.score >= this.sttScoreThreshold)
737
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
@@ -33,6 +33,7 @@ class CachedModel extends CustomEntityDetectionModel {
33
33
 
34
34
  /**
35
35
  * @param {object} options
36
+ * @param {string} [options.prefix]
36
37
  * @param {number} [options.cacheSize]
37
38
  * @param {number} [options.cachePhrasesTime]
38
39
  * @param {{ warn: Function, error: Function, log: Function }} [log]
@@ -67,6 +67,7 @@ const { iterateThroughWords } = require('../utils/ai');
67
67
  * @param {string} text
68
68
  * @param {DetectedEntity[]} entities
69
69
  * @param {number} startIndex
70
+ * @param {string} prefix
70
71
  */
71
72
 
72
73
  /**
@@ -90,12 +91,15 @@ class CustomEntityDetectionModel {
90
91
 
91
92
  /**
92
93
  * @param {object} options
94
+ * @param {string} [options.prefix]
93
95
  * @param {{ warn: Function, error: Function, log: Function }} [log]
94
96
  */
95
97
  constructor (options, log = console) {
96
98
  this._options = options;
97
99
  this._log = log;
98
100
 
101
+ this.prefix = options.prefix;
102
+
99
103
  this._entityDetectors = new Map();
100
104
 
101
105
  /**
@@ -399,7 +403,7 @@ class CustomEntityDetectionModel {
399
403
  let entities = prevEnts.slice();
400
404
  if (this.wordEntityDetector) {
401
405
  for (const [s, startIndex] of iterateThroughWords(text, this.maxWordCount)) {
402
- this.wordEntityDetector(s, entities, startIndex);
406
+ this.wordEntityDetector(s, entities, startIndex, this.prefix);
403
407
  }
404
408
  }
405
409
 
@@ -37,6 +37,7 @@ class WingbotModel extends CachedModel {
37
37
 
38
38
  /**
39
39
  * @param {object} options
40
+ * @param {string} [options.prefix]
40
41
  * @param {string} [options.serviceUrl]
41
42
  * @param {string} [options.trainingUrl]
42
43
  * @param {string} options.model