wingbot 3.37.1 → 3.37.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 +1 -1
- package/src/Ai.js +2 -2
- package/src/AiMatching.js +32 -3
- package/src/BuildRouter.js +1 -1
- package/src/Processor.js +11 -5
package/package.json
CHANGED
package/src/Ai.js
CHANGED
|
@@ -49,7 +49,7 @@ let uq = 1;
|
|
|
49
49
|
*/
|
|
50
50
|
class Ai {
|
|
51
51
|
|
|
52
|
-
constructor (
|
|
52
|
+
constructor () {
|
|
53
53
|
this._keyworders = new Map();
|
|
54
54
|
|
|
55
55
|
/**
|
|
@@ -111,7 +111,7 @@ class Ai {
|
|
|
111
111
|
*
|
|
112
112
|
* @type {AiMatching}
|
|
113
113
|
*/
|
|
114
|
-
this.matcher =
|
|
114
|
+
this.matcher = new AiMatching(this);
|
|
115
115
|
}
|
|
116
116
|
|
|
117
117
|
/**
|
package/src/AiMatching.js
CHANGED
|
@@ -91,6 +91,11 @@ const COMPARE = {
|
|
|
91
91
|
* @prop {object} [state]
|
|
92
92
|
*/
|
|
93
93
|
|
|
94
|
+
/**
|
|
95
|
+
* @typedef {object} ConfidenceProvider
|
|
96
|
+
* @prop {number} confidence
|
|
97
|
+
*/
|
|
98
|
+
|
|
94
99
|
/**
|
|
95
100
|
* @class {AiMatching}
|
|
96
101
|
*
|
|
@@ -98,7 +103,11 @@ const COMPARE = {
|
|
|
98
103
|
*/
|
|
99
104
|
class AiMatching {
|
|
100
105
|
|
|
101
|
-
|
|
106
|
+
/**
|
|
107
|
+
*
|
|
108
|
+
* @param {ConfidenceProvider} ai
|
|
109
|
+
*/
|
|
110
|
+
constructor (ai = { confidence: 0.8 }) {
|
|
102
111
|
/**
|
|
103
112
|
* When the entity is optional, the final score should be little bit lower
|
|
104
113
|
* (0.001 by default)
|
|
@@ -138,6 +147,8 @@ class AiMatching {
|
|
|
138
147
|
* (1 by default)
|
|
139
148
|
*/
|
|
140
149
|
this.stateEntityScore = 1;
|
|
150
|
+
|
|
151
|
+
this._ai = ai;
|
|
141
152
|
}
|
|
142
153
|
|
|
143
154
|
get redundantHandicap () {
|
|
@@ -447,11 +458,29 @@ class AiMatching {
|
|
|
447
458
|
? score - noIntentHandicap
|
|
448
459
|
: (regexpScore + score) / 2;
|
|
449
460
|
|
|
461
|
+
const matchedEntitiesTextLength = matched.reduce((tot, entity) => (
|
|
462
|
+
typeof entity.end === 'number' && typeof entity.start === 'number' && tot !== null
|
|
463
|
+
? (tot + (entity.end - entity.start))
|
|
464
|
+
: null
|
|
465
|
+
), 0);
|
|
466
|
+
|
|
467
|
+
let finalScore = (baseScore - handicap)
|
|
468
|
+
* (this.multiMatchGain ** countOfAdditionalItems);
|
|
469
|
+
|
|
470
|
+
const textLength = req.text().length;
|
|
471
|
+
|
|
472
|
+
if (matchedEntitiesTextLength && textLength) {
|
|
473
|
+
const remainingScore = Math.max(0, Math.min(1, finalScore) - (
|
|
474
|
+
this._ai.confidence + this.redundantEntityHandicap
|
|
475
|
+
));
|
|
476
|
+
|
|
477
|
+
finalScore -= (matchedEntitiesTextLength / textLength) * remainingScore;
|
|
478
|
+
}
|
|
479
|
+
|
|
450
480
|
return {
|
|
451
481
|
intent: null,
|
|
452
482
|
entities: matched,
|
|
453
|
-
score:
|
|
454
|
-
* (this.multiMatchGain ** countOfAdditionalItems)
|
|
483
|
+
score: finalScore
|
|
455
484
|
};
|
|
456
485
|
}
|
|
457
486
|
|
package/src/BuildRouter.js
CHANGED
|
@@ -559,7 +559,7 @@ class BuildRouter extends Router {
|
|
|
559
559
|
* @param {RouteConfig} routeConfig
|
|
560
560
|
*/
|
|
561
561
|
_enabledByRouteConfig (routeConfig) {
|
|
562
|
-
return !
|
|
562
|
+
return !routeConfig || routeConfig.enabled !== false;
|
|
563
563
|
}
|
|
564
564
|
|
|
565
565
|
_joinPaths (...args) {
|
package/src/Processor.js
CHANGED
|
@@ -323,12 +323,14 @@ class Processor extends EventEmitter {
|
|
|
323
323
|
return { status: 304 };
|
|
324
324
|
}
|
|
325
325
|
|
|
326
|
-
const result = await this
|
|
326
|
+
const result = await this
|
|
327
|
+
._dispatch(message, pageId, messageSender, responderData, preloadPromise);
|
|
328
|
+
|
|
327
329
|
await preloadPromise;
|
|
328
330
|
return result;
|
|
329
331
|
}
|
|
330
332
|
|
|
331
|
-
async _dispatch (message, pageId, messageSender, responderData) {
|
|
333
|
+
async _dispatch (message, pageId, messageSender, responderData, preloadPromise) {
|
|
332
334
|
let req;
|
|
333
335
|
let res;
|
|
334
336
|
let state;
|
|
@@ -338,7 +340,7 @@ class Processor extends EventEmitter {
|
|
|
338
340
|
({
|
|
339
341
|
req, res, data, state
|
|
340
342
|
} = await this
|
|
341
|
-
._processMessage(message, pageId, messageSender, responderData,
|
|
343
|
+
._processMessage(message, pageId, messageSender, responderData, preloadPromise));
|
|
342
344
|
|
|
343
345
|
await this._emitInteractionEvent(req, messageSender, state, data);
|
|
344
346
|
|
|
@@ -447,8 +449,9 @@ class Processor extends EventEmitter {
|
|
|
447
449
|
}
|
|
448
450
|
}
|
|
449
451
|
|
|
450
|
-
async _processMessage (message, pageId, messageSender, responderData,
|
|
452
|
+
async _processMessage (message, pageId, messageSender, responderData, preloadPromise = null) {
|
|
451
453
|
let senderId = message.sender && message.sender.id;
|
|
454
|
+
const fromEvent = !!preloadPromise;
|
|
452
455
|
|
|
453
456
|
// prevent infinite cycles
|
|
454
457
|
let { _actionCount: actionCount = 0 } = responderData;
|
|
@@ -547,7 +550,10 @@ class Processor extends EventEmitter {
|
|
|
547
550
|
}
|
|
548
551
|
}
|
|
549
552
|
|
|
550
|
-
await
|
|
553
|
+
await Promise.all([
|
|
554
|
+
preloadPromise,
|
|
555
|
+
Ai.ai.preloadAi(req, res)
|
|
556
|
+
]);
|
|
551
557
|
|
|
552
558
|
// @deprecated backward compatibility
|
|
553
559
|
const aByAi = req.actionByAi();
|