UNPKG

@botonic/plugin-flow-builder

Version:

Use Flow Builder to show your contents

67 lines 3.67 kB
import { __awaiter, __rest } from "tslib"; import { EventAction, } from '@botonic/core'; import { FlowAiAgent } from '../content-fields'; import { getFlowBuilderPlugin } from '../helpers'; import { trackEvent } from '../tracking'; export function getContentsByAiAgent({ cmsApi, flowBuilderPlugin, request, }) { var _a, _b, _c, _d; return __awaiter(this, void 0, void 0, function* () { const startNodeAiAgentFlow = cmsApi.getStartNodeAiAgentFlow(); const isAiAgentEnabled = cmsApi.isAiAgentEnabled(); if (!startNodeAiAgentFlow || !isAiAgentEnabled) { return []; } const contents = yield flowBuilderPlugin.getContentsByNode(startNodeAiAgentFlow); const aiAgentContent = contents.find(content => content instanceof FlowAiAgent); if (!aiAgentContent) { return []; } const activeInputGuardrailRules = ((_b = (_a = aiAgentContent.inputGuardrailRules) === null || _a === void 0 ? void 0 : _a.filter(rule => rule.is_active)) === null || _b === void 0 ? void 0 : _b.map(rule => ({ name: rule.name, description: rule.description, }))) || []; const aiAgentResponse = yield ((_c = flowBuilderPlugin.getAiAgentResponse) === null || _c === void 0 ? void 0 : _c.call(flowBuilderPlugin, request, { name: aiAgentContent.name, instructions: aiAgentContent.instructions, activeTools: aiAgentContent.activeTools, inputGuardrailRules: activeInputGuardrailRules, sourceIds: (_d = aiAgentContent.sources) === null || _d === void 0 ? void 0 : _d.map(source => source.id), })); if (!aiAgentResponse) { return []; } yield trackAiAgentResponse(aiAgentResponse, request, aiAgentContent); if (aiAgentResponse.exit) { return []; } aiAgentContent.responses = aiAgentResponse.messages; return contents; }); } function trackAiAgentResponse(aiAgentResponse, request, aiAgentContent) { var _a, _b, _c, _d; return __awaiter(this, void 0, void 0, function* () { const flowBuilderPlugin = getFlowBuilderPlugin(request.plugins); const flowId = flowBuilderPlugin.cmsApi.getNodeById(aiAgentContent.id).flow_id; const flowName = flowBuilderPlugin.getFlowName(flowId); const event = { action: EventAction.AiAgent, flowThreadId: request.session.flow_thread_id, flowId: flowId, flowName: flowName, flowNodeId: aiAgentContent.id, flowNodeContentId: aiAgentContent.name, flowNodeIsMeaningful: true, toolsExecuted: (_a = aiAgentResponse === null || aiAgentResponse === void 0 ? void 0 : aiAgentResponse.toolsExecuted) !== null && _a !== void 0 ? _a : [], memoryLength: (_b = aiAgentResponse === null || aiAgentResponse === void 0 ? void 0 : aiAgentResponse.memoryLength) !== null && _b !== void 0 ? _b : 0, inputMessageId: request.input.message_id, exit: (_c = aiAgentResponse === null || aiAgentResponse === void 0 ? void 0 : aiAgentResponse.exit) !== null && _c !== void 0 ? _c : true, inputGuardrailsTriggered: (_d = aiAgentResponse === null || aiAgentResponse === void 0 ? void 0 : aiAgentResponse.inputGuardrailsTriggered) !== null && _d !== void 0 ? _d : [], outputGuardrailsTriggered: [], error: aiAgentResponse.error, }; const { action } = event, eventArgs = __rest(event, ["action"]); yield trackEvent(request, action, eventArgs); }); } //# sourceMappingURL=ai-agent.js.map