@botonic/plugin-flow-builder
Version:
Botonic plugin for **Hubtype Flow Builder**: run and bridge flow-driven logic from bots on the **current** line.
90 lines • 4.62 kB
JavaScript
import { __awaiter, __rest } from "tslib";
import { EventAction, KnowledgebaseFailReason, } from '@botonic/shared';
import { DISABLED_MEMORY_LENGTH, FlowKnowledgeBase, } from '../content-fields';
import { getCommonFlowContentEventArgsForContentId, trackEvent, } from '../tracking';
import { inputHasTextOrTranscript, isKnowledgeBasesAllowed } from '../utils';
export function getContentsByKnowledgeBase(_a) {
return __awaiter(this, arguments, void 0, function* ({ cmsApi, flowBuilderPlugin, botonicContext, }) {
if (isKnowledgeBasesAllowed(botonicContext)) {
const startNodeKnowledgeBaseFlow = cmsApi.getStartNodeKnowledgeBaseFlow();
if (!startNodeKnowledgeBaseFlow) {
return [];
}
const contents = yield flowBuilderPlugin.getContentsByNode(startNodeKnowledgeBaseFlow);
const knowledgeBaseContent = contents.find(content => content instanceof FlowKnowledgeBase);
if (!knowledgeBaseContent) {
return contents;
}
const sourceIds = knowledgeBaseContent.sourcesData.map(source => source.id);
if (flowBuilderPlugin.getKnowledgeBaseResponse &&
inputHasTextOrTranscript(botonicContext.input) &&
sourceIds.length > 0) {
const contentsWithKnowledgeResponse = yield getContentsWithKnowledgeResponse(flowBuilderPlugin.getKnowledgeBaseResponse, botonicContext, contents, knowledgeBaseContent);
if (contentsWithKnowledgeResponse) {
return contentsWithKnowledgeResponse;
}
}
}
return [];
});
}
function getContentsWithKnowledgeResponse(getKnowledgeBaseResponse, botonicContext, contents, knowledgeBaseContent) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
const sourceIds = knowledgeBaseContent.sourcesData.map(source => source.id);
const instructions = knowledgeBaseContent.instructions;
const messageId = (_a = botonicContext.input.dbId) !== null && _a !== void 0 ? _a : '';
const memoryLength = knowledgeBaseContent.hasMemory
? knowledgeBaseContent.memoryLength
: DISABLED_MEMORY_LENGTH;
const knowledgeBaseResponse = yield getKnowledgeBaseResponse(botonicContext, sourceIds, instructions, messageId, memoryLength);
yield trackKnowledgeBase(knowledgeBaseResponse, botonicContext, knowledgeBaseContent);
if (!knowledgeBaseResponse.hasKnowledge ||
!knowledgeBaseResponse.isFaithful) {
return undefined;
}
return updateContentsWithResponse(contents, knowledgeBaseResponse);
});
}
function updateContentsWithResponse(contents, response) {
return contents.map(content => {
if (content instanceof FlowKnowledgeBase) {
content.text = response.answer;
content.inferenceId = response.inferenceId;
}
return content;
});
}
function trackKnowledgeBase(response, botonicContext, knowledgeBaseContent) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
const getKnowledgeFailReason = () => {
let knowledgebaseFailReason;
if (!response.isFaithful) {
knowledgebaseFailReason = KnowledgebaseFailReason.Hallucination;
}
if (!response.hasKnowledge) {
knowledgebaseFailReason = KnowledgebaseFailReason.NoKnowledge;
}
return knowledgebaseFailReason;
};
const { flowId, flowName, flowNodeId, flowNodeContentId } = yield getCommonFlowContentEventArgsForContentId(botonicContext, knowledgeBaseContent.id);
const event = {
action: EventAction.Knowledgebase,
knowledgebaseInferenceId: response.inferenceId,
knowledgebaseFailReason: getKnowledgeFailReason(),
knowledgebaseSourcesIds: knowledgeBaseContent.sourcesData.map(source => source.id),
knowledgebaseChunksIds: response.chunkIds,
knowledgebaseMessageId: (_a = botonicContext.input.dbId) !== null && _a !== void 0 ? _a : '',
userInput: (_b = botonicContext.input.text) !== null && _b !== void 0 ? _b : '',
flowThreadId: botonicContext.session.flowThreadId,
flowId,
flowName,
flowNodeId,
flowNodeContentId,
};
const { action } = event, eventArgs = __rest(event, ["action"]);
yield trackEvent(botonicContext, action, eventArgs);
});
}
//# sourceMappingURL=knowledge-bases.js.map