UNPKG

@botonic/plugin-flow-builder

Version:

Use Flow Builder to show your contents

80 lines 3.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SmartIntentsApi = void 0; const tslib_1 = require("tslib"); const core_1 = require("@botonic/core"); const axios_1 = tslib_1.__importDefault(require("axios")); const helpers_1 = require("../helpers"); const tracking_1 = require("../tracking"); class SmartIntentsApi { constructor(cmsApi, currentRequest, smartIntentsConfig, flowId) { this.cmsApi = cmsApi; this.currentRequest = currentRequest; this.smartIntentsConfig = smartIntentsConfig; this.flowId = flowId; } getNodeByInput() { return tslib_1.__awaiter(this, void 0, void 0, function* () { if (!this.currentRequest.input.data) return undefined; const smartIntentNodes = this.cmsApi.getSmartIntentNodes(); if (!smartIntentNodes.length) return undefined; const params = { bot_id: this.currentRequest.session.bot.id, text: this.currentRequest.input.data, num_smart_intents_to_use: this.smartIntentsConfig.numSmartIntentsToUse, use_latest: this.resolveUseLatest(), }; try { const response = yield this.getInference(params); const smartIntentNode = smartIntentNodes.find(smartIntentNode => smartIntentNode.content.title === response.data.smart_intent_title); if (smartIntentNode) { const targetPayload = this.cmsApi.getPayload(smartIntentNode.target); this.currentRequest.input.nluResolution = { type: core_1.NluType.SmartIntent, matchedValue: smartIntentNode.content.title, payload: targetPayload, }; yield (0, tracking_1.trackEvent)(this.currentRequest, tracking_1.EventAction.IntentSmart, { nluIntentSmartTitle: response.data.smart_intent_title, nluIntentSmartNumUsed: response.data.smart_intents_used.length, nluIntentSmartMessageId: this.currentRequest.input.message_id, userInput: this.currentRequest.input.data, flowThreadId: this.currentRequest.session.flow_thread_id, flowId: smartIntentNode.flow_id, flowNodeId: smartIntentNode.id, }); return smartIntentNode; } } catch (e) { console.error(e); } return undefined; }); } resolveUseLatest() { if (this.currentRequest.session.is_test_integration) return false; return this.smartIntentsConfig.useLatest; } getInference(inferenceParams) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const pluginFlowBuilder = (0, helpers_1.getFlowBuilderPlugin)(this.currentRequest.plugins); const token = pluginFlowBuilder.getAccessToken(this.currentRequest.session); return yield (0, axios_1.default)({ method: 'POST', url: `${process.env.HUBTYPE_API_URL}/external/v2/ai/smart_intents/inference/`, headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json', }, data: inferenceParams, timeout: 10000, }); }); } } exports.SmartIntentsApi = SmartIntentsApi; //# sourceMappingURL=smart-intent.js.map