UNPKG

wa-chat-server

Version:

Watson Assistant powered chat server

115 lines (114 loc) 6.24 kB
"use strict"; 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.Route = void 0; class Route { constructor(newPayload, session, config, logger) { this.refuseRoute = false; this.routeContext = { target: null }; this.sharedPrefix = 'shared'; this.routePrefix = 'route'; this.paramsPrefix = 'params'; this.newPayload = newPayload; this.session = session; this.config = config; this.logger = logger; this.routeFrom = this.session.runningAssistant; if (!this.isDisamb()) { let routeTo = this.newPayload.result.context['route-target']; if (routeTo === '') { routeTo = 'MASTER'; } this.routeContext.target = routeTo; this.newPayload.result.context['route-target'] = null; delete this.session.data.assistants[this.routeFrom].context['route-target']; } else { this.refuseRoute = true; } } messagesBeforeRoute() { var _a, _b, _c, _d, _e; const arrayOfMessages = []; for (const output in (_b = (_a = this.newPayload.result) === null || _a === void 0 ? void 0 : _a.output) === null || _b === void 0 ? void 0 : _b.generic) { if (((_e = (_d = (_c = this.newPayload.result) === null || _c === void 0 ? void 0 : _c.output) === null || _d === void 0 ? void 0 : _d.generic[output]) === null || _e === void 0 ? void 0 : _e.response_type) !== 'text') { this.logger.warn('An error could occure. The chatbot, from which you were routing, has responded in a non-supported way.'); } arrayOfMessages.push(this.newPayload.result.output.generic[output]); } this.messagesBeforeRouting = arrayOfMessages; } isDisamb() { var _a, _b, _c; return ((_c = (_b = (_a = this.newPayload.result) === null || _a === void 0 ? void 0 : _a.output) === null || _b === void 0 ? void 0 : _b.generic[0]) === null || _c === void 0 ? void 0 : _c.response_type) === 'suggestion'; } route() { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d; this.session.setRunningAssistant(this.routeContext.target); this.messagesBeforeRoute(); for (const key in ((_b = (_a = this.session.data) === null || _a === void 0 ? void 0 : _a.assistants[this.routeContext.target]) === null || _b === void 0 ? void 0 : _b.context) || {}) { if (this.session.data.assistants[this.routeContext.target].context[key] === null) { delete this.session.data.assistants[this.routeContext.target].context[key]; } } this.newPayload.result.context = Object.assign(Object.assign(Object.assign({}, (((_d = (_c = this.session.data) === null || _c === void 0 ? void 0 : _c.assistants[this.routeContext.target]) === null || _d === void 0 ? void 0 : _d.context) || {})), this.newPayload.result.context), { 'incoming_route-origin': this.routeFrom }); yield this.setContext(); }); } forceIntent(forcedIntent) { this.routeContext.intent = forcedIntent; this.newPayload.result.input.intents = [{ intent: forcedIntent, confidence: 1 }]; } forceUtterance(forcedUtterance) { this.routeContext.utterance = forcedUtterance; this.newPayload.result.input.text = forcedUtterance; } forceParams(key, forcedParam) { if (!this.routeContext[this.paramsPrefix]) { this.routeContext[this.paramsPrefix] = {}; } this.routeContext[this.paramsPrefix][key] = forcedParam; this.newPayload.result.context[this.routePrefix + '-' + this.paramsPrefix + '-' + key] = forcedParam; } setContext() { return __awaiter(this, void 0, void 0, function* () { for (const key in this.newPayload.result.context) { const fragments = key.split('-'); if (!(fragments.length === 1 || key === this.routePrefix + '-target')) { if (fragments[0] === this.sharedPrefix) { this.session.data.general.shared[fragments[1]] = this.newPayload.result.context[key]; } else if (fragments[0] === this.routePrefix) { const forcedAttribute = this.newPayload.result.context[key]; if (fragments[1] == 'intent') { this.forceIntent(forcedAttribute); } else if (fragments[1] == 'utterance') { this.forceUtterance(forcedAttribute); } else if (fragments[1] == this.paramsPrefix) { this.forceParams(fragments[2], forcedAttribute); } else { this.logger.error(`A context variable ${key} found, however only '${this.routePrefix}-intent', '${this.routePrefix}-utterance', '${this.routePrefix}-target' or '${this.routePrefix}-${this.paramsPrefix}' are possible.`); } delete this.newPayload.result.context[key]; this.session.data.assistants[this.routeFrom].context[key] = null; } } } this.routeContext.shared = this.session.data.general.shared; }); } } exports.Route = Route;