wingbot 3.67.2 → 3.67.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.67.2",
3
+ "version": "3.67.4",
4
4
  "description": "Enterprise Messaging Bot Conversation Engine",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -165,7 +165,7 @@ class BuildRouter extends Router {
165
165
  * @constructor
166
166
  * @param {BotConfig|Block} block
167
167
  * @param {Plugins} plugins - custom code blocks resource
168
- * @param {BuildRouterContext<C>} context - the building context
168
+ * @param {BuildRouterContext<C>|Promise<BuildRouterContext<C>>} context - the building context
169
169
  * @param {fetch} [fetchFn] - override a request function
170
170
  * @example
171
171
  *
@@ -190,15 +190,24 @@ class BuildRouter extends Router {
190
190
  * module.exports = bot;
191
191
  */
192
192
  constructor (block, plugins = new Plugins(), context = {}, fetchFn = fetch) {
193
- super(context.configuration);
193
+ super(
194
+ context instanceof Promise
195
+ ? context.then((c) => c.configuration)
196
+ : context.configuration
197
+ );
194
198
 
195
199
  this._validateBlock(block);
196
200
 
197
201
  this._plugins = plugins;
198
202
 
199
- /** @type {BotContext<C>} */
203
+ /** @type {BotContext<C>|Promise<BuildRouterContext<C>>} */
200
204
  this._context = context;
201
205
 
206
+ /** @type {BotContext<C>} */
207
+ this._resolvedContext = context instanceof Promise
208
+ ? null
209
+ : context;
210
+
202
211
  /** @type {LinksMap} */
203
212
  this._linksMap = new Map();
204
213
 
@@ -218,7 +227,10 @@ class BuildRouter extends Router {
218
227
 
219
228
  this._loadBotAuthorization = 'token' in block ? block.token : null;
220
229
 
221
- this._configStorage = context.configStorage;
230
+ /** @type {ConfigStorage|Promise<ConfigStorage>} */
231
+ this._configStorage = context instanceof Promise
232
+ ? context.then((c) => c.configStorage)
233
+ : context.configStorage;
222
234
 
223
235
  this._runningReqs = [];
224
236
 
@@ -227,7 +239,7 @@ class BuildRouter extends Router {
227
239
  /**
228
240
  * Timeout, when the router is not checking for new configuration
229
241
  *
230
- * @prop {number}
242
+ * @type {number}
231
243
  */
232
244
  this.keepConfigFor = 60000;
233
245
 
@@ -315,16 +327,40 @@ class BuildRouter extends Router {
315
327
  return;
316
328
  }
317
329
 
318
- if (!this._configStorage) {
319
- // not need to wait for existing requests, there are no existing ones
330
+ /** @type {ConfigStorage} */
331
+ let configStorage;
332
+ let snapshot;
333
+ /** @type {BotContext<C>} */
334
+ let context;
335
+
336
+ if (this._context instanceof Promise || this._configStorage instanceof Promise) {
337
+ [
338
+ configStorage,
339
+ snapshot,
340
+ context
341
+ ] = await Promise.all([
342
+ Promise.resolve(this._configStorage),
343
+ this.loadBot(),
344
+ this._context
345
+ ]);
346
+
347
+ this._context = context;
348
+ this._resolvedContext = context;
349
+ this._configStorage = configStorage;
350
+ } else {
351
+ configStorage = this._configStorage;
352
+ context = this._context;
353
+ }
320
354
 
321
- let botLoaded = false;
355
+ if (!configStorage) {
356
+ // not need to wait for existing requests, there are no existing ones
322
357
  try {
323
- const snapshot = await this.loadBot();
324
- botLoaded = true;
358
+ if (!snapshot) {
359
+ snapshot = await this.loadBot();
360
+ }
325
361
  this.buildWithSnapshot(snapshot.blocks);
326
362
  } catch (e) {
327
- if (this._configTs > 0 && !botLoaded) {
363
+ if (this._configTs > 0 && !snapshot) {
328
364
  // mute
329
365
  // eslint-disable-next-line no-console
330
366
  console.info('Loading new state failed - recovering', e);
@@ -335,26 +371,29 @@ class BuildRouter extends Router {
335
371
 
336
372
  return;
337
373
  }
374
+
338
375
  try {
339
376
  // check for current TS
340
- const ts = await this._configStorage.getConfigTimestamp();
377
+ const ts = await configStorage.getConfigTimestamp();
341
378
 
342
379
  if (ts <= this._configTs && this._configTs !== 0 && ts !== 0) {
343
380
  // do not update, when there is no better configuration
344
381
  return;
345
382
  }
346
383
 
347
- let snapshot;
384
+ if (snapshot) {
385
+ snapshot = await configStorage.updateConfig(snapshot);
386
+ }
348
387
 
349
- if (ts !== 0) {
388
+ if (ts !== 0 && !snapshot) {
350
389
  // probably someone has updated the configuration
351
- snapshot = await this._configStorage.getConfig();
390
+ snapshot = await configStorage.getConfig();
352
391
  }
353
392
 
354
393
  if (!snapshot || typeof snapshot !== 'object' || !Array.isArray(snapshot.blocks)) {
355
394
  // there is no configuration, load it from server
356
395
  snapshot = await this.loadBot();
357
- snapshot = await this._configStorage.updateConfig(snapshot);
396
+ snapshot = await configStorage.updateConfig(snapshot);
358
397
  }
359
398
 
360
399
  // wait for running request
@@ -362,7 +401,7 @@ class BuildRouter extends Router {
362
401
 
363
402
  this.buildWithSnapshot(snapshot.blocks, snapshot.timestamp, snapshot.lastmod);
364
403
  } catch (e) {
365
- await this._configStorage.invalidateConfig();
404
+ await configStorage.invalidateConfig();
366
405
  throw e;
367
406
  }
368
407
  }
@@ -428,7 +467,7 @@ class BuildRouter extends Router {
428
467
  buildWithSnapshot (blocks, setConfigTimestamp = Number.MAX_SAFE_INTEGER, lastmod = '-') {
429
468
  this._validateBlocks(blocks);
430
469
 
431
- Object.assign(this._context, { blocks });
470
+ Object.assign(this._resolvedContext, { blocks });
432
471
 
433
472
  const rootBlock = blocks.find((block) => block.isRoot);
434
473
 
@@ -482,8 +521,8 @@ class BuildRouter extends Router {
482
521
  blockName, blockType, isRoot, staticBlockId
483
522
  } = block;
484
523
 
485
- this._context = {
486
- ...this._context, blockName, blockType, isRoot, staticBlockId, BuildRouter
524
+ this._resolvedContext = {
525
+ ...this._resolvedContext, blockName, blockType, isRoot, staticBlockId, BuildRouter
487
526
  };
488
527
 
489
528
  this._linksMap = this._createLinksMap(block);
@@ -544,7 +583,7 @@ class BuildRouter extends Router {
544
583
  .filter((route) => !route.isResponder)
545
584
  .forEach((route) => linksMap.set(route.id, route.path));
546
585
 
547
- const { linksMap: prevLinksMap } = this._context;
586
+ const { linksMap: prevLinksMap } = this._resolvedContext;
548
587
 
549
588
  if (prevLinksMap) {
550
589
  for (const [from, to] of prevLinksMap.entries()) {
@@ -612,7 +651,7 @@ class BuildRouter extends Router {
612
651
  if (!staticBlockId) {
613
652
  return null;
614
653
  }
615
- const nestedBlock = (this._context.blocks || [])
654
+ const nestedBlock = (this._resolvedContext.blocks || [])
616
655
  .find((b) => b.staticBlockId === staticBlockId);
617
656
 
618
657
  if (!nestedBlock || nestedBlock.disabled) {
@@ -627,7 +666,7 @@ class BuildRouter extends Router {
627
666
  * @returns {RouteConfig}
628
667
  */
629
668
  _getRouteConfig (route) {
630
- const { path: ctxPath, routeConfigs } = this._context;
669
+ const { path: ctxPath, routeConfigs } = this._resolvedContext;
631
670
  if (!routeConfigs || !route.path || route.isFallback) {
632
671
  return null;
633
672
  }
@@ -810,10 +849,15 @@ class BuildRouter extends Router {
810
849
  const lastMessageIndex = this._lastMessageIndex(resolvers);
811
850
  const lastIndex = resolvers.length - 1;
812
851
 
852
+ /** @type {C} */
853
+ const configuration = this._configuration instanceof Promise
854
+ ? null
855
+ : this._configuration;
856
+
813
857
  return resolvers.map((resolver, i) => {
814
858
 
815
859
  const context = {
816
- ...this._context,
860
+ ...this._resolvedContext,
817
861
  isLastIndex: lastIndex === i && !buildInfo.expectedToAddResolver,
818
862
  isLastMessage: lastMessageIndex === i,
819
863
  router: this,
@@ -823,7 +867,7 @@ class BuildRouter extends Router {
823
867
  isResponder,
824
868
  expectedPath,
825
869
  routeId: id,
826
- configuration: this._configuration,
870
+ configuration,
827
871
  resolverId: resolver.id
828
872
  };
829
873
 
@@ -832,7 +876,7 @@ class BuildRouter extends Router {
832
876
  // @ts-ignore
833
877
  if (typeof resFn.configuration === 'undefined') {
834
878
  // @ts-ignore
835
- resFn.configuration = this._configuration;
879
+ resFn.configuration = configuration;
836
880
  }
837
881
  return resFn;
838
882
  });
package/src/Processor.js CHANGED
@@ -607,6 +607,9 @@ class Processor extends EventEmitter {
607
607
  let configuration = {};
608
608
  if ('getConfiguration' in this.reducer) {
609
609
  configuration = this.reducer.getConfiguration();
610
+ if (configuration instanceof Promise) {
611
+ configuration = await configuration;
612
+ }
610
613
  }
611
614
 
612
615
  // @ts-ignore
package/src/Request.js CHANGED
@@ -189,22 +189,22 @@ class Request {
189
189
  : event.message.attachments)) || [];
190
190
 
191
191
  /**
192
- * @prop {number|null} timestamp
192
+ * @type {number|null} timestamp
193
193
  */
194
194
  this.timestamp = event.timestamp || Date.now();
195
195
 
196
196
  /**
197
- * @prop {string} senderId sender.id from the event
197
+ * @type {string} senderId sender.id from the event
198
198
  */
199
199
  this.senderId = (event.sender && event.sender.id) || null;
200
200
 
201
201
  /**
202
- * @prop {string} recipientId recipient.id from the event
202
+ * @type {string} recipientId recipient.id from the event
203
203
  */
204
204
  this.recipientId = event.recipient && event.recipient.id;
205
205
 
206
206
  /**
207
- * @prop {string} pageId page identifier from the event
207
+ * @type {string} pageId page identifier from the event
208
208
  */
209
209
  this.pageId = pageId;
210
210
 
@@ -214,29 +214,29 @@ class Request {
214
214
  this.state = state;
215
215
 
216
216
  /**
217
- * @prop {string[]} features supported messaging features
217
+ * @type {string[]} features supported messaging features
218
218
  */
219
219
  this.features = Array.isArray(event.features)
220
220
  ? event.features
221
221
  : getDefaultFeatureList();
222
222
 
223
223
  /**
224
- * @prop {string[]} state list of subscribed tags
224
+ * @type {string[]} state list of subscribed tags
225
225
  */
226
226
  this.subscribtions = [];
227
227
 
228
228
  /**
229
- * @prop {Entity[]} entities list of entities
229
+ * @type {Entity[]} entities list of entities
230
230
  */
231
231
  this.entities = [];
232
232
 
233
233
  /**
234
- * @prop {Intent[]} intents list of resolved intents
234
+ * @type {Intent[]} intents list of resolved intents
235
235
  */
236
236
  this.intents = [];
237
237
 
238
238
  /**
239
- * @prop {Action}
239
+ * @type {Action}
240
240
  * @private
241
241
  */
242
242
  this._action = undefined;
package/src/Router.js CHANGED
@@ -99,14 +99,19 @@ function defaultPathContext () {
99
99
  class Router extends ReducerWrapper {
100
100
 
101
101
  /**
102
- * @param {C} [configuration]
102
+ * @param {C|Promise<C>} [configuration]
103
103
  */
104
104
  constructor (configuration = null) {
105
105
  super();
106
106
 
107
- /** @type {C} */
107
+ /** @type {C|Promise<C>} */
108
108
  // @ts-ignore
109
- this._configuration = configuration || {};
109
+ this._configuration = configuration instanceof Promise
110
+ ? configuration.then((c) => {
111
+ this._configuration = c;
112
+ return c;
113
+ })
114
+ : configuration || {};
110
115
 
111
116
  this._routes = [];
112
117
 
@@ -114,15 +119,17 @@ class Router extends ReducerWrapper {
114
119
  }
115
120
 
116
121
  /**
117
- * @returns {C}
122
+ * @returns {C|null}
118
123
  */
119
124
  get configuration () {
120
- return this._configuration;
125
+ return this._configuration instanceof Promise
126
+ ? null
127
+ : this._configuration;
121
128
  }
122
129
 
123
130
  /**
124
131
  *
125
- * @returns {C}
132
+ * @returns {C|Promise<C>}
126
133
  */
127
134
  getConfiguration () {
128
135
  return this._configuration;
@@ -131,10 +138,13 @@ class Router extends ReducerWrapper {
131
138
  /**
132
139
  *
133
140
  * @param {Partial<C>} c
134
- * @returns {C}
141
+ * @returns {Promise<C>}
135
142
  */
136
- updateConfiguration (c) {
137
- return Object.assign(this._configuration, c);
143
+ async updateConfiguration (c) {
144
+ const cfg = this._configuration instanceof Promise
145
+ ? await this._configuration
146
+ : this._configuration;
147
+ return Object.assign(cfg, c);
138
148
  }
139
149
 
140
150
  _normalizePath (path) {
@@ -405,7 +405,27 @@ class CustomEntityDetectionModel {
405
405
  if (this.wordEntityDetector) {
406
406
  for (const [s, startIndex] of iterateThroughWords(text, this.maxWordCount)) {
407
407
  const ents = this.wordEntityDetector(s, prevEnts, startIndex, this.prefix);
408
- entities.push(...ents);
408
+
409
+ const byEntity = new Map();
410
+
411
+ for (const entity of ents) {
412
+ let list;
413
+ if (byEntity.has(entity.entity)) {
414
+ list = byEntity.get(entity.entity);
415
+ } else {
416
+ list = [];
417
+ byEntity.set(entity.entity, list);
418
+ }
419
+ list.push({
420
+ text: s,
421
+ ...entity
422
+ });
423
+ }
424
+
425
+ const normalized = Array.from(byEntity.entries())
426
+ .flatMap(([e, list]) => this._normalizeResult(list, e, s, startIndex, text));
427
+
428
+ entities.push(...normalized);
409
429
  }
410
430
  }
411
431