@botonic/plugin-flow-builder
Version:
Use Flow Builder to show your contents
76 lines • 3.44 kB
JavaScript
import { __awaiter } from "tslib";
import { NluType } from '@botonic/core';
import axios from 'axios';
import { getFlowBuilderPlugin } from '../helpers';
import { EventAction, trackEvent } from '../tracking';
export class SmartIntentsApi {
constructor(cmsApi, currentRequest, smartIntentsConfig, flowId) {
this.cmsApi = cmsApi;
this.currentRequest = currentRequest;
this.smartIntentsConfig = smartIntentsConfig;
this.flowId = flowId;
}
getNodeByInput() {
return __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: NluType.SmartIntent,
matchedValue: smartIntentNode.content.title,
payload: targetPayload,
};
yield trackEvent(this.currentRequest, 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 __awaiter(this, void 0, void 0, function* () {
const pluginFlowBuilder = getFlowBuilderPlugin(this.currentRequest.plugins);
const token = pluginFlowBuilder.getAccessToken(this.currentRequest.session);
return yield axios({
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,
});
});
}
}
//# sourceMappingURL=smart-intent.js.map