wechaty-qnamaker
Version:
QnAMaker.ai Plugin for Wechaty
100 lines • 4.48 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Faq = void 0;
const wechaty_1 = require("wechaty");
const wechaty_plugin_contrib_1 = require("wechaty-plugin-contrib");
const normalize_config_1 = require("./normalize-config");
const asker_1 = require("./asker");
function Faq(config) {
wechaty_1.log.verbose('WechatyQnAMaker', 'Faq(%s)', JSON.stringify(config));
const normalizedConfigList = [];
if (!Array.isArray(config)) {
normalizedConfigList.push(normalize_config_1.normalizeConfig(config));
}
else {
normalizedConfigList.push(...config.map(c => normalize_config_1.normalizeConfig(c)));
}
return function FaqExtension(vorpal) {
wechaty_1.log.verbose('WechatyQnAMaker', 'FaqExtension(vorpal)');
vorpal
.command('faq <question...>', 'Get an answer from Frequent Asked Questions (FAQ)')
.option('-v --verbose', 'Show verbose informations')
.option('-n --number <number>', 'Show maximum <number> related answers. (default: 1)')
.action(faqAction(normalizedConfigList));
};
}
exports.Faq = Faq;
const faqAction = (configList) => {
wechaty_1.log.verbose('WechatyQnAMaker', 'Faq() faqAction("%s")', JSON.stringify(configList));
const askerDictList = configList.map(config => ({
ask: asker_1.asker(config),
matchLanguage: (typeof config.language === 'undefined')
? () => true // match all language by default
: wechaty_plugin_contrib_1.matchers.languageMatcher(config.language),
}));
const askAll = (question, dto = {}) => __awaiter(void 0, void 0, void 0, function* () {
const resultList = yield Promise.all(askerDictList
.filter(dict => dict.matchLanguage(question))
.map(dict => dict.ask(question, dto)));
return resultList
.flat()
.filter(i => i.score)
.sort((a, b) => b.score - a.score);
});
return function faqActionExector(args) {
return __awaiter(this, void 0, void 0, function* () {
wechaty_1.log.verbose('WechatyQnAMaker', 'Faq() faqAction() faqActionExecutor("%s")', JSON.stringify(args));
const options = args.options;
let question;
if (Array.isArray(args.question)) {
question = args.question.join(' ');
}
else {
question = args.question;
}
const queryDto = {
scoreThreshold: 1,
top: options.number,
};
const searchResultList = yield askAll(question, queryDto);
if (searchResultList.length <= 0) {
this.log('Sorry, I did not find any answer in my KB (Knowledge Base) for your question: "' + question + '".');
return;
}
wechaty_1.log.verbose('WechatyQnAMaker', 'Faq() faqAction() faqActionExecutor() found %s answers', searchResultList.length);
searchResultList.forEach((result, idx) => {
var _a;
if (!result.answer) {
return;
}
if (idx > ((_a = options.number) !== null && _a !== void 0 ? _a : 0)) {
return;
}
this.log(toReply(result, idx + 1, options.verbose));
});
});
};
};
function toReply(result, index, verbose = false) {
if (!verbose) {
return result.answer;
}
const list = [
`A${index}:(score:${Math.floor(result.score || 0)}) ${result.answer}`,
];
if (result.questions) {
list.unshift('');
list.unshift(`Q${index}: ` + result.questions[0]);
}
return list.join('\n');
}
//# sourceMappingURL=vorpal.js.map