lexica-dialog-core
Version:
Core server for Lexica dialog agent.
49 lines • 2.13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const lodash_1 = require("lodash");
const immutable_1 = require("immutable");
const Api_1 = require("../Api");
const Utils_1 = require("../Utils");
const startConversationMiddleware = async (context, next) => {
try {
await next();
}
catch (error) {
const { sessionService, request, commands } = context;
if (!lodash_1.isNil(sessionService) &&
!lodash_1.isNil(request) &&
error instanceof Api_1.BotError &&
error.code === Api_1.BotErrorCode.MISSING_REQUIRED_FEATURE &&
commands.size === 1) {
const { locale } = request;
const command = commands.first();
const intent = command.intent;
const features = command.features;
const processedFeatures = command.processedFeatures;
if (!lodash_1.isNil(intent) && !lodash_1.isNil(intent.missingFeatures)) {
const missingFeatures = immutable_1.Map(intent.missingFeatures);
if (sessionService.hasConversation()) {
sessionService.updateConversationFeatures(features);
}
else {
sessionService.startConversation(intent, features);
}
const highestPriority = missingFeatures
.filter((value, key) => !features.has(lodash_1.defaultTo(key, '')))
.toList()
.sort((a, b) => a.priority - b.priority)
.first();
const responses = immutable_1.List(intent.responses.map(response => Utils_1.intentResponseToBotResponse(highestPriority.response, features, locale)));
command.botResponses = immutable_1.List(responses.toArray().reduce((a, b) => a.concat(b), immutable_1.List()));
}
else {
throw error;
}
}
else {
throw error;
}
}
};
exports.default = startConversationMiddleware;
//# sourceMappingURL=StartConversationMiddleware.js.map