wingbot 3.74.8 → 3.75.9-alpha.2

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/src/Processor.js CHANGED
@@ -13,6 +13,7 @@ const ReturnSender = require('./ReturnSender');
13
13
  const { prepareState, mergeState, isUserInteraction } = require('./utils/stateVariables');
14
14
  const LLM = require('./LLM');
15
15
  const LLMMockProvider = require('./LLMMockProvider');
16
+ const LLMDispatcher = require('./LLMDispatcher');
16
17
 
17
18
  /** @typedef {import('./wingbot/CustomEntityDetectionModel').Intent} Intent */
18
19
  /** @typedef {import('./ReducerWrapper')} ReducerWrapper */
@@ -140,9 +141,9 @@ const LLMMockProvider = require('./LLMMockProvider');
140
141
  */
141
142
 
142
143
  /**
143
- * @template {ReducerWrapper|Router|BuildRouter} T
144
+ * @template {ReducerWrapper|Router|BuildRouter} R
144
145
  * @callback Preloader
145
- * @param {T} router
146
+ * @param {R} router
146
147
  * @param {Ai} ai
147
148
  * @returns {Promise<void>}
148
149
  */
@@ -268,12 +269,11 @@ class Processor extends EventEmitter {
268
269
  }
269
270
 
270
271
  if (dispatchSync) {
271
- let previousAction;
272
272
  return Promise.resolve(data)
273
273
  .then((resolvedData) => {
274
274
  let reduceResult;
275
275
  Object.assign(resolvedData, { _localpostback: true });
276
- previousAction = req.setAction(action, resolvedData);
276
+ req.dispatcher.overrideAction({ action, data: resolvedData });
277
277
  if (typeof this.reducer === 'function') {
278
278
  // @ts-ignore
279
279
  reduceResult = this.reducer(req, res, postBack);
@@ -283,7 +283,7 @@ class Processor extends EventEmitter {
283
283
  return reduceResult;
284
284
  })
285
285
  .then((reduceResult) => {
286
- req.setAction(previousAction);
286
+ req.dispatcher.overrideAction(null);
287
287
  return reduceResult;
288
288
  });
289
289
  }
@@ -407,7 +407,7 @@ class Processor extends EventEmitter {
407
407
 
408
408
  /** @type {LLMGlobalConfig} */
409
409
  const llmConfiguration = {
410
- provider: new LLMMockProvider(),
410
+ provider: new LLMMockProvider(message.llmMocks),
411
411
  ...this.options.llm
412
412
  };
413
413
 
@@ -533,10 +533,11 @@ class Processor extends EventEmitter {
533
533
  ? Request.text('none', text)
534
534
  : text;
535
535
  // @ts-ignore
536
- const req = new Request(request, { lang }, pageId, this.reducer.globalIntents);
537
-
538
- await Ai.ai.preloadAi(req);
539
-
536
+ const dispatcher = new LLMDispatcher(Ai.ai, this.reducer.routing, {
537
+ log: this.options.log
538
+ });
539
+ const req = new Request(request, { lang }, pageId, dispatcher);
540
+ await dispatcher.dispatch(req);
540
541
  const actions = req.aiActions();
541
542
 
542
543
  if (actions.length === 0 && allowEmptyAction && req.intents.length > 0) {
@@ -555,6 +556,7 @@ class Processor extends EventEmitter {
555
556
  ];
556
557
  }
557
558
 
559
+ // @ts-ignore
558
560
  return actions
559
561
  .map((a) => ({
560
562
  ...a,
@@ -638,13 +640,14 @@ class Processor extends EventEmitter {
638
640
  }
639
641
  }
640
642
 
641
- // @ts-ignore
643
+ const dispatcher = new LLMDispatcher(Ai.ai, this.reducer.routing, llm, {
644
+ log: this.options.log
645
+ });
642
646
  req = new Request(
643
647
  message,
644
648
  state,
645
649
  pageId,
646
- // @ts-ignore
647
- this.reducer.globalIntents,
650
+ dispatcher,
648
651
  {
649
652
  apiUrl: this.options.apiUrl,
650
653
  secret: this.options.secret,
@@ -719,6 +722,7 @@ class Processor extends EventEmitter {
719
722
  senderMeta,
720
723
  llm
721
724
  );
725
+ llm.setReqRes(req, res);
722
726
  const postBack = this._createPostBack(postbackAcumulator, req, res, features);
723
727
 
724
728
  let continueDispatching = true;
@@ -738,7 +742,7 @@ class Processor extends EventEmitter {
738
742
 
739
743
  await Promise.all([
740
744
  preloadPromise,
741
- Ai.ai.preloadAi(req, res)
745
+ dispatcher.dispatch(req, res)
742
746
  ]);
743
747
 
744
748
  // @deprecated backward compatibility
@@ -4,6 +4,11 @@
4
4
  'use strict';
5
5
 
6
6
  const EventEmitter = require('events');
7
+ const LLMDispatcher = require('./LLMDispatcher');
8
+
9
+ /** @typedef {import('./Responder')} Responder */
10
+ /** @typedef {import('./Request')} Request */
11
+ /** @typedef {import('./LLMDispatcher').Routing} Routing */
7
12
 
8
13
  /**
9
14
  * Solution for catching events. This is useful for analytics.
@@ -39,6 +44,25 @@ class ReducerWrapper extends EventEmitter {
39
44
  this.processMessage = null;
40
45
 
41
46
  this.setMaxListeners(Infinity);
47
+
48
+ this.globalIntents = new Map();
49
+
50
+ /** @type {Routing} */
51
+ this._routing = null;
52
+ }
53
+
54
+ resetRouter () {
55
+ this._routing = null;
56
+ }
57
+
58
+ /**
59
+ * @returns {Routing}
60
+ */
61
+ get routing () {
62
+ if (this._routing === null) {
63
+ this._routing = LLMDispatcher.prepareRouting(this.globalIntents);
64
+ }
65
+ return this._routing;
42
66
  }
43
67
 
44
68
  /**