forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
545 lines (448 loc) · 20.1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
var _ = require('lodash');
var util = require('util');
var builder = require('botbuilder');
var inspector = require('schema-inspector');
var Promise = require('bluebird');
var BuiltInTypes = require('./builtin');
var Status = {
NoActionRecognized: 'NoActionRecognized',
Fulfilled: 'Fulfilled',
MissingParameters: 'MissingParameters',
ContextSwitch: 'ContextSwitch'
};
/*
* API
*/
module.exports = {
Status: Status,
BuiltInTypes: BuiltInTypes,
evaluate: evaluate,
bindToBotDialog: bindToBotDialog
};
var EmptyActionModel = {
status: Status.NoActionRecognized,
intentName: null,
result: null,
userInput: null,
currentParameter: null,
parameters: {},
parameterErrors: []
};
function evaluate(modelUrl, actions, currentActionModel, userInput, onContextCreationHandler) {
if (!modelUrl) {
throw new Error('modelUrl not set');
}
actions.forEach(validateAction);
onContextCreationHandler = validateContextCreationHandler(onContextCreationHandler);
return new Promise(function (resolve, reject) {
var actionModel = _.merge({}, EmptyActionModel, currentActionModel);
if (actionModel.status === Status.ContextSwitch) {
// confirming switch context
if (actionModel.confirmSwitch) {
// re-write current model
actionModel.intentName = actionModel.contextSwitchData.intentName;
actionModel.parameters = actionModel.contextSwitchData.parameters;
}
// force continue with validation
actionModel.contextSwitchData = null;
actionModel.currentParameter = null;
actionModel.userInput = null;
}
// normalize input
actionModel.userInput = userInput ? userInput.trim() : null;
// cleanup from previous runs
delete actionModel.subcontextResult;
switch (actionModel.status) {
case Status.NoActionRecognized:
// First time input, resolve to action
builder.LuisRecognizer.recognize(actionModel.userInput, modelUrl, (err, intents, entities) => {
if (err) {
return reject(err);
}
var action = chooseBestIntentAction(intents, actions);
if (action) {
// Populate action parameters with LUIS entities
actionModel.intentName = action.intentName;
// Contextual action? Populate with root action
if (action.parentAction && !actionModel.contextModel) {
popupateContextParent(actionModel, action, actions);
}
// extract parameters from entities
actionModel.parameters = extractParametersFromEntities(action.schema, entities);
var next = function () {
// Run validation
tryExecute(action, actionModel)
.then(resolve)
.catch(reject);
};
if (action.parentAction) {
// Invoke custom onContextCreationHandler, may inject more parameters to contextModel (the parent's actionModel)
// Wait for onContextCreation handler's callback to continue execution
onContextCreationHandler(action.parentAction, actionModel.contextModel, next);
} else {
next();
}
} else {
// No action recognized
actionModel.status = Status.NoActionRecognized;
resolve(actionModel);
}
});
break;
case Status.MissingParameters:
case Status.ContextSwitch:
var action = _.find(actions, function (action) { return actionModel.intentName === action.intentName; });
if (actionModel.userInput) {
// Filling for a missing parameter
builder.LuisRecognizer.recognize(actionModel.userInput, modelUrl, (err, intents, entities) => {
if (err) {
return reject(err);
}
var newAction = chooseBestIntentAction(intents, actions, action);
if (newAction && newAction.intentName !== action.intentName) {
if (newAction.parentAction === action) {
// context action (sub action), replace action & model and continue
actionModel = _.merge({}, EmptyActionModel, {
contextModel: actionModel,
intentName: newAction.intentName
});
actionModel.parameters = [];
action = newAction;
} else if (equalsTrue(action.confirmOnContextSwitch, true)) {
// new context switch
actionModel.status = Status.ContextSwitch;
actionModel.contextSwitchData = {
intentName: newAction.intentName,
parameters: extractParametersFromEntities(newAction.schema, entities)
};
// prompt
var currentActionName = action.friendlyName || action.intentName;
var newActionName = newAction.friendlyName || newAction.intentName;
actionModel.contextSwitchPrompt = util.format('Do you want to discard the current action \'%s\' and start the with \'%s\' action?', currentActionName, newActionName);
// return and wait for context switch confirmation
return resolve(actionModel);
} else {
// switch to new context and continue with evaluation
action = newAction;
actionModel.intentName = newAction.intentName;
actionModel.currentParameter = null;
}
}
var parameters = extractParametersFromEntities(action.schema, entities, actionModel);
// merge new identified parameters from entites
actionModel.parameters = _.merge({}, actionModel.parameters, parameters);
// Run validation
tryExecute(action, actionModel)
.then(resolve)
.catch(reject);
});
} else {
// Run validation with current model
tryExecute(action, actionModel)
.then(resolve)
.catch(reject);
}
break;
default:
reject('Unknown action.status "' + actionModel.status + '"');
}
});
}
/*
* Bot Stuff
*/
function bindToBotDialog(bot, intentDialog, modelUrl, actions, options) {
if (!bot) {
throw new Error('bot is required');
}
if (!intentDialog) {
throw new Error('intentDialog is required');
}
if (!modelUrl) {
throw new Error('ModelUrl is required');
}
options = options || {};
// enable bot persistence (used for storing actionModel in privateConversationData)
bot.set('persistConversationData', true);
// register dialog for handling input evaluation
bot.library(createBotLibrary(modelUrl, actions, options));
// Register each LuisActions with the intentDialog
_.forEach(actions, function (action) {
intentDialog.matches(action.intentName, createBotAction(action, modelUrl));
});
}
function createBotLibrary(modelUrl, actions, options) {
var defaultReplyHandler = typeof options.defaultReply === 'function' ? options.defaultReply : function (session) { session.endDialog('Sorry, I couldn\'t understart that.'); };
var fulfillReplyHandler = typeof options.fulfillReply === 'function' ? options.fulfillReply : function (session, actionModel) { session.endDialog(actionModel.result.toString()); };
var onContextCreationHandler = validateContextCreationHandler(options.onContextCreation);
var lib = new builder.Library('LuisActions');
lib.dialog('Evaluate', new builder.SimpleDialog(function (session, args) {
var actionModel = null;
var action = null;
if (args && args.intents) {
// Coming from a matched intent
action = chooseBestIntentAction(args.intents, actions);
if (!action) {
return defaultReplyHandler(session);
}
actionModel = _.merge({}, EmptyActionModel, {
intentName: action.intentName
});
// Contextual action? Populate with root action
if (action.parentAction && !actionModel.contextModel) {
popupateContextParent(actionModel, action, actions);
}
// Extract parameters from entities/luisresult
actionModel.parameters = extractParametersFromEntities(action.schema, args.entities);
if (action.parentAction) {
// Invoke custom onContextCreationHandler, may inject more parameters to contextModel (the parent's actionModel)
// Wait for onContextCreation handler's callback to continue execution
return onContextCreationHandler(action.parentAction, actionModel.contextModel, next, session);
}
} else {
actionModel = session.privateConversationData['luisaction.model'];
}
next();
function next() {
if (!actionModel) {
return defaultReplyHandler(session);
}
action = actions.find(a => a.intentName === actionModel.intentName);
if (!action) {
return defaultReplyHandler(session);
}
var operation = null;
if (actionModel.status === Status.ContextSwitch && args.response === true) {
// confirming context switch
actionModel.confirmSwitch = true;
operation = evaluate(modelUrl, actions, actionModel);
} else if (args && args.response && actionModel.currentParameter) {
// try evaluate new parameter
operation = evaluate(modelUrl, actions, actionModel, args.response);
} else {
// try validate with current parameters
operation = tryExecute(action, actionModel);
}
operation.then(actionModel => {
session.privateConversationData['luisaction.model'] = actionModel;
if (actionModel.subcontextResult) {
session.send(actionModel.subcontextResult.toString());
}
switch (actionModel.status) {
case Status.MissingParameters:
// Prompt for first missing parameter
var errors = actionModel.parameterErrors;
var firstError = _.first(errors);
// set current parameter name to help recognizer which parameter to match
actionModel.currentParameter = firstError.parameterName;
session.privateConversationData['luisaction.model'] = actionModel;
builder.Prompts.text(session, firstError.message);
break;
case Status.ContextSwitch:
// Prompt for context switch
var prompt = actionModel.contextSwitchPrompt;
session.privateConversationData['luisaction.model'] = actionModel;
builder.Prompts.confirm(session, prompt, { listStyle: builder.ListStyle.button });
break;
case Status.Fulfilled:
// Action fulfilled
// TODO: Allow external handler
delete session.privateConversationData['luisaction.model'];
fulfillReplyHandler(session, actionModel);
break;
}
}).catch((err) => {
// error ocurred
session.endDialog('Error: %s', err);
});
}
}));
return lib;
}
function createBotAction(action, modelUrl) {
validateAction(action);
// trigger evaluation dialog
return function (session, dialogArgs) {
session.beginDialog('LuisActions:Evaluate', dialogArgs);
};
}
/*
* Helpers
*/
function chooseBestIntentAction(intents, actions, currentAction) {
var intent = _.maxBy(intents, function (intent) { return intent.score; });
var action = _.find(actions, function (action) { return intent && intent.intent === action.intentName; });
// ignore context actions that do not belong to current action
if (action && currentAction && action.parentAction && action.parentAction !== currentAction) {
return null;
}
// ignore context action that do not allow execution without context
if (action && action.parentAction && (!equalsTrue(action.canExecuteWithoutContext, true) && action.parentAction !== currentAction)) {
return null;
}
return action;
}
function extractParametersFromEntities(schema, entities, actionModel) {
// when evaluating a specific parameter, try matching it by its custom type, then name and finally builin type
if (actionModel && actionModel.currentParameter && schema[actionModel.currentParameter]) {
var currentParameterSchema = schema[actionModel.currentParameter];
var entity = null;
// find by custom attrib
if (currentParameterSchema.customType) {
entity = entities.find(e => e.type === currentParameterSchema.customType);
}
// find by name
if (!entity) {
entity = entities.find(e => e.type === actionModel.currentParameter);
}
// find by builtin
if (!entity && currentParameterSchema.builtInType) {
entity = entities.find(e => e.type === currentParameterSchema.builtInType);
}
// if no entity recognized then try to assign user's input
if (!entity) {
entity = { entity: actionModel.userInput };
}
entity = _.merge({}, entity, { type: actionModel.currentParameter });
entities = entities.concat([entity]);
}
// resolve complete parameters from entities
entities = crossMatchEntities(entities);
// merge entities into parameters obj
var parameters = _.reduce(entities, function (merged, entity) {
merged[entity.type] = entity.entity;
return merged;
}, {});
// validate and remove those parameters marked as invalid
var schemaObject = wrap(schema);
inspector.sanitize(schemaObject, parameters);
var result = inspector.validate(schemaObject, parameters);
if (!result.valid) {
var invalidParameterNames = result.error.map(getParameterName);
parameters = _.omit(parameters, invalidParameterNames);
}
return parameters;
}
function tryExecute(action, actionModel) {
return new Promise(function (resolve, reject) {
try {
validate(action.schema, actionModel.parameters,
(parameters, errors) => {
actionModel.status = Status.MissingParameters;
actionModel.parameters = parameters;
actionModel.parameterErrors = errors;
resolve(actionModel);
},
(completeParameters) => {
// fulfill and return response to callback
var parentContext = actionModel.contextModel;
action.fulfill(completeParameters, (fulfillResult) => {
actionModel.status = Status.Fulfilled;
actionModel.result = fulfillResult;
actionModel.parameters = completeParameters;
actionModel.parameterErrors = [];
if (actionModel.contextModel) {
// switch back to context dialog
actionModel.contextModel.subcontextResult = actionModel.result;
actionModel = actionModel.contextModel;
tryExecute(action.parentAction, actionModel)
.then(resolve)
.catch(reject);
} else {
resolve(actionModel);
}
}, parentContext ? parentContext.parameters : {});
});
} catch (err) {
reject(err);
}
});
}
function validate(schema, parameters, onValidationErrors, onValidationPass) {
var schemaObject = wrap(schema);
inspector.sanitize(schemaObject, parameters);
var result = inspector.validate(schemaObject, parameters);
if (result.valid) {
onValidationPass(parameters);
} else {
var errors = result.error.map(function (fieldError) {
var parameterName = getParameterName(fieldError);
var errorMessage = schema[parameterName].message;
return {
parameterName: parameterName,
message: errorMessage
};
});
onValidationErrors(parameters, errors);
}
}
function popupateContextParent(actionModel, currentAction) {
if (!currentAction.parentAction) {
return actionModel;
}
actionModel.contextModel = _.merge({}, EmptyActionModel, {
intentName: currentAction.parentAction.intentName,
status: Status.MissingParameters
});
actionModel.parameters = {};
actionModel.parameterErrors = [];
actionModel.result = null;
return actionModel;
}
function crossMatchEntities(entities) {
// Group detected entities by origin input
var groups = _.groupBy(entities, function (entity) {
return entity.entity;
});
var result = [];
_.forOwn(groups, function (matches, entity) {
if (matches.length > 1) {
var entityTarget = matches.find((e) => e.type.indexOf('builtin.') === -1);
var entityWithValue = matches.find((e) => e.resolution);
if (entityWithValue) {
var resolution = entityWithValue.resolution;
entityTarget.entity = resolution[_.keys(resolution)[0]];
}
if (entityTarget) {
result.push(entityTarget);
}
} else {
result.push(matches[0]);
}
});
return result;
}
function getParameterName(fieldError) {
return _.last(fieldError.property.split('.'));
}
function wrap(propertiesSchema) {
return {
type: 'object',
properties: propertiesSchema
};
}
function equalsTrue(value, valueForUndefined) {
if (value === undefined || value === null) {
return valueForUndefined === true;
}
return value === true;
}
function validateContextCreationHandler(callback) {
return typeof callback === 'function'
? callback
: function (action, actionModel, next) { next(); };
}
function validateAction(action) {
if (typeof action.intentName !== 'string') {
throw new Error('actionModel.intentName requires a string');
}
if (typeof action.friendlyName !== 'string') {
throw new Error('actionModel.friendlyName requires a string');
}
if (typeof action.schema !== 'object') {
throw new Error('actionModel.schema requires a schema of properties');
}
if (typeof action.fulfill !== 'function') {
throw new Error('actionModel.fulfill should be a function');
}
}