@botonic/plugin-flow-builder
Version:
Botonic plugin for **Hubtype Flow Builder**: run and bridge flow-driven logic from bots on the **current** line.
110 lines • 6.08 kB
JavaScript
import { __awaiter } from "tslib";
import { applyNonPatchSessionUpdateFields, BotonicContext, BotonicContextInput, BotonicContextSecrets, BotonicContextSession, BotonicContextSettings, mergeBotonicSessionPatchSlice, mergeChatUserPatchResponseIntoSessionUser, sessionUpdateToPatchBody, userUpdateToChatUserPatchBody, } from '@botonic/core';
import { MessagingChannel } from '@botonic/shared';
import { BotonicPluginFlowBuilder, FlowBuilderAction, } from '../../index';
const DEFAULT_BOT_INTERACTION_ID = 'test-bot-interaction-id';
export function createFlowBuilderPlugin({ flow, trackEvent, getKnowledgeBaseResponse, getAiAgentResponse, inShadowing, contentFilters, disableAIAgentInFirstInteraction, }) {
const flowBuilderPlugin = new BotonicPluginFlowBuilder({
flow,
trackEvent,
getKnowledgeBaseResponse,
getAiAgentResponse,
inShadowing,
contentFilters,
disableAIAgentInFirstInteraction,
});
return flowBuilderPlugin;
}
export function createRequest({ input, plugins = {}, provider = MessagingChannel.Webchat, isFirstInteraction = false, user = {
locale: 'en',
country: 'US',
systemLocale: 'en',
}, extraData = {}, shadowing = false, hubtypeCaseId, captureUserInputId, bot_interaction_id: botInteractionId, }) {
var _a;
const sessionData = Object.assign(Object.assign({ organization: { id: 'orgIdTest', name: 'orgTest' }, bot: { id: 'bid1', name: 'Bot' }, channel: {
provider,
imp_id: 'imp-test',
unformatted_phone_number: '',
}, user: {
id: 'uid1',
provider_id: provider,
locale: user.locale,
country: user.country,
system_locale: user.systemLocale,
extra_data: extraData,
}, is_first_interaction: isFirstInteraction, is_test_integration: false, flow_thread_id: 'testFlowThreadId', shadowing }, (hubtypeCaseId !== undefined
? { hubtype_case: { id: hubtypeCaseId } }
: {})), (captureUserInputId !== undefined
? { capture_user_input_node_id: captureUserInputId }
: {}));
const contextInput = new BotonicContextInput(Object.assign(Object.assign({}, input), { bot_interaction_id: (_a = botInteractionId !== null && botInteractionId !== void 0 ? botInteractionId : input.bot_interaction_id) !== null && _a !== void 0 ? _a : DEFAULT_BOT_INTERACTION_ID }));
const contextSession = new BotonicContextSession(sessionData);
const mockHubtypeService = {
sendMessages: () => __awaiter(this, void 0, void 0, function* () { return ({}); }),
doHandoff: () => __awaiter(this, void 0, void 0, function* () { return ({}); }),
getBotSession: () => __awaiter(this, void 0, void 0, function* () { return sessionData; }),
updateBotSession: (ctx, updates) => __awaiter(this, void 0, void 0, function* () {
var _a;
applyNonPatchSessionUpdateFields(ctx.session.raw, updates);
const patch = sessionUpdateToPatchBody(updates);
if (Object.keys(patch).length > 0) {
mergeBotonicSessionPatchSlice(ctx.session.raw, patch);
}
const userBody = userUpdateToChatUserPatchBody(updates.user);
if (Object.keys(userBody).length > 0) {
mergeChatUserPatchResponseIntoSessionUser(ctx.session.raw.user, userBody);
}
Object.assign(sessionData, patch);
const updatedUser = updates.user;
if ((updatedUser === null || updatedUser === void 0 ? void 0 : updatedUser.extraData) !== undefined) {
sessionData.user.extra_data = updatedUser.extraData;
}
if ((updatedUser === null || updatedUser === void 0 ? void 0 : updatedUser.locale) !== undefined) {
sessionData.user.locale = updatedUser.locale;
}
if ((updatedUser === null || updatedUser === void 0 ? void 0 : updatedUser.country) !== undefined) {
sessionData.user.country = updatedUser.country;
}
if ((updatedUser === null || updatedUser === void 0 ? void 0 : updatedUser.systemLocale) !== undefined) {
sessionData.user.system_locale = updatedUser.systemLocale;
}
if ((updatedUser === null || updatedUser === void 0 ? void 0 : updatedUser.overrideName) !== undefined) {
sessionData.user.override_name = (_a = updatedUser.overrideName) !== null && _a !== void 0 ? _a : undefined;
}
}),
};
return new BotonicContext(mockHubtypeService, {
input: contextInput,
session: contextSession,
settings: new BotonicContextSettings({
HUBTYPE_API_URL: 'https://api.hubtype.com',
}),
secrets: new BotonicContextSecrets({
HUBTYPE_ACCESS_TOKEN: 'fake_access_token',
}),
}, plugins, 0, 0, 'inBetweenTyping');
}
export function getContentsAfterPreAndBotonicInit(botonicContext, flowBuilderPlugin) {
return __awaiter(this, void 0, void 0, function* () {
yield flowBuilderPlugin.pre(botonicContext);
const actionRequest = getActionRequest(botonicContext);
return yield FlowBuilderAction.botonicInit(actionRequest);
});
}
export function getActionRequest(botonicContext) {
return botonicContext;
}
export function createFlowBuilderPluginAndGetContents(_a) {
return __awaiter(this, arguments, void 0, function* ({ flowBuilderOptions, requestArgs, }) {
const flowBuilderPlugin = createFlowBuilderPlugin(flowBuilderOptions);
const botonicContext = createRequest(Object.assign(Object.assign({}, requestArgs), { plugins: {
flowBuilderPlugin,
} }));
const contents = yield getContentsAfterPreAndBotonicInit(botonicContext, flowBuilderPlugin);
const flowBuilderPluginPost = (botonicContext) => {
flowBuilderPlugin.post(botonicContext);
};
return { contents, botonicContext, flowBuilderPluginPost };
});
}
//# sourceMappingURL=utils.js.map