UNPKG

@botonic/plugin-flow-builder

Version:

Use Flow Builder to show your contents

68 lines 3.11 kB
import { __awaiter } from "tslib"; import { MAIN_FLOW_NAME } from '../constants'; import { FlowBotAction } from '../content-fields'; import { inputHasTextData } from '../utils'; import { getContentsByKnowledgeBase } from './knowledge-bases'; import { getContentsByPayload } from './payload'; export function getContentsByFirstInteraction(context) { return __awaiter(this, void 0, void 0, function* () { const { flowBuilderPlugin, request } = context; const firstInteractionContents = yield flowBuilderPlugin.getStartContents(); // If the first interaction has a FlowBotAction, it should be the last content // and avoid to render the match with keywords,intents or knowledge base if (firstInteractionContents.at(-1) instanceof FlowBotAction) { return firstInteractionContents; } if (request.input.nluResolution || inputHasTextData(request.input)) { const contentsByUserInput = yield getContentsByUserInput(context); return [...firstInteractionContents, ...contentsByUserInput]; } return firstInteractionContents; }); } function getContentsByUserInput({ cmsApi, flowBuilderPlugin, request, resolvedLocale, }) { var _a; return __awaiter(this, void 0, void 0, function* () { const payloadByNlu = (_a = request.input.nluResolution) === null || _a === void 0 ? void 0 : _a.payload; if (payloadByNlu) { request.input.payload = payloadByNlu; const conversationStartId = getConversationStartId(cmsApi); if (request.input.payload === conversationStartId) { return []; } const contentsByKeywordsOrIntents = yield getContentsByPayload({ cmsApi, flowBuilderPlugin, request, resolvedLocale, }); const hasRepeatedContent = yield checkRepeatedContents(flowBuilderPlugin, contentsByKeywordsOrIntents); if (hasRepeatedContent) { return []; } if (contentsByKeywordsOrIntents.length > 0) { return contentsByKeywordsOrIntents; } } return yield getContentsByKnowledgeBase({ cmsApi, flowBuilderPlugin, request, resolvedLocale, }); }); } function getConversationStartId(cmsApi) { var _a; const conversationStartId = (_a = cmsApi.flow.flows.find(flow => flow.name === MAIN_FLOW_NAME)) === null || _a === void 0 ? void 0 : _a.start_node_id; return conversationStartId; } function checkRepeatedContents(flowBuilderPlugin, contentsByKeywordsOrIntents) { return __awaiter(this, void 0, void 0, function* () { const startContents = yield flowBuilderPlugin.getStartContents(); const contentIds = new Set(contentsByKeywordsOrIntents.map(content => content.id)); const hasRepeatedContent = startContents.some(content => contentIds.has(content.id)); return hasRepeatedContent; }); } //# sourceMappingURL=first-interaction.js.map