UNPKG

@botonic/plugin-flow-builder

Version:

Use Flow Builder to show your contents

71 lines 3.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getContentsByAiAgent = void 0; const tslib_1 = require("tslib"); const core_1 = require("@botonic/core"); const content_fields_1 = require("../content-fields"); const helpers_1 = require("../helpers"); const tracking_1 = require("../tracking"); function getContentsByAiAgent({ cmsApi, flowBuilderPlugin, request, }) { var _a, _b, _c, _d; return tslib_1.__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 content_fields_1.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; }); } exports.getContentsByAiAgent = getContentsByAiAgent; function trackAiAgentResponse(aiAgentResponse, request, aiAgentContent) { var _a, _b, _c, _d; return tslib_1.__awaiter(this, void 0, void 0, function* () { const flowBuilderPlugin = (0, helpers_1.getFlowBuilderPlugin)(request.plugins); const flowId = flowBuilderPlugin.cmsApi.getNodeById(aiAgentContent.id).flow_id; const flowName = flowBuilderPlugin.getFlowName(flowId); const event = { action: core_1.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 = tslib_1.__rest(event, ["action"]); yield (0, tracking_1.trackEvent)(request, action, eventArgs); }); } //# sourceMappingURL=ai-agent.js.map