lexica-dialog-core
Version:
Core server for Lexica dialog agent.
61 lines • 2.48 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 Intent_1 = require("lexica-dialog-model/dist/Intent");
function findFallbackCommand(uniConfigs, commands) {
let fallbackCommand;
const fallbackCommands = commands
.toArray()
.map(command => {
if (!lodash_1.isNil(command.intent) && !lodash_1.isNil(command.intent.fallbackCommand)) {
return command.intent.fallbackCommand;
}
return undefined;
})
.filter(fallbackCommand => !lodash_1.isNil(fallbackCommand));
if (fallbackCommands.length > 0) {
fallbackCommand = fallbackCommands[0];
}
else if (uniConfigs.has(Api_1.RunTimeConfig.FALLBACK_COMMAND_NAME)) {
fallbackCommand = uniConfigs.get(Api_1.RunTimeConfig.FALLBACK_COMMAND_NAME).value;
}
return fallbackCommand;
}
const fallbackResponseMiddleware = async (context, next) => {
try {
await next();
}
catch (error) {
const { request, uni, uniConfigs, intentRepository, logger, rawRequest, commands } = context;
const fallbackCommand = findFallbackCommand(uniConfigs, commands);
let fallbacked = false;
logger.error(`
Return fallback message.
Raw Request: %s
Error: %s
Stack trace:
%s
`, JSON.stringify(rawRequest), JSON.stringify(error), error.stack);
if (!lodash_1.isNil(request) && !lodash_1.isNil(fallbackCommand)) {
const { locale } = request;
const intent = await intentRepository.findByUniCommandName(uni, fallbackCommand);
if (!lodash_1.isNil(intent)) {
const responses = immutable_1.List(intent.responses.map(response => Utils_1.intentResponseToBotResponse(response, immutable_1.Map(), locale)));
context.responses = immutable_1.List(responses.toArray().reduce((a, b) => a.concat(b), immutable_1.List()));
fallbacked = true;
}
}
if (!fallbacked) {
const response = {
message: "Sorry, I don't know",
type: Intent_1.ResponseType.TEXT,
};
context.responses = immutable_1.List([response]);
}
}
};
exports.default = fallbackResponseMiddleware;
//# sourceMappingURL=FallbackResponseMiddleware.js.map