@botonic/plugin-flow-builder
Version:
Botonic plugin for **Hubtype Flow Builder**: run and bridge flow-driven logic from bots on the **current** line.
137 lines • 8.8 kB
JavaScript
import { __awaiter, __rest } from "tslib";
import { AiAgentType, EventAction, } from '@botonic/shared';
import { getCommonFlowContentEventArgsForContentId, trackEvent, } from '../tracking';
import { getFlowBuilderPlugin } from '../utils';
import { FlowAiAgent } from './flow-ai-agent';
import { FlowAiAgentBase } from './flow-ai-agent-base';
export class FlowAiAgentRouter extends FlowAiAgentBase {
constructor() {
super(...arguments);
this.availableSpecialists = [];
}
static fromHubtypeCMS(component, botonicContext) {
const newAiAgentRouter = new FlowAiAgentRouter(component.id);
newAiAgentRouter.code = component.code;
newAiAgentRouter.name = component.content.name;
newAiAgentRouter.instructions = newAiAgentRouter.replaceVariables(component.content.instructions, botonicContext);
newAiAgentRouter.model = component.content.model;
newAiAgentRouter.verbosity = component.content.verbosity;
newAiAgentRouter.inputGuardrailRules =
component.content.input_guardrail_rules || [];
newAiAgentRouter.availableSpecialists =
FlowAiAgentRouter.getAvailableSpecialistsFromHubtypeCMS(component, botonicContext);
newAiAgentRouter.followUp = component.follow_up;
return newAiAgentRouter;
}
static getAvailableSpecialistsFromHubtypeCMS(component, botonicContext) {
const flowBuilderPlugin = getFlowBuilderPlugin(botonicContext.plugins);
const cmsApi = flowBuilderPlugin.cmsApi;
return component.content.available_specialists.map((agentSlot, index) => {
const aiAgentSpecialistNode = cmsApi.getNodeById(agentSlot.target.id);
const specialist = FlowAiAgent.fromHubtypeCMS(aiAgentSpecialistNode);
const specialistName = FlowAiAgentRouter.specialistAgentBaseName(agentSlot.name, index);
const specialistDescription = agentSlot.description || `Transfer to ${specialistName}`;
return {
agent: specialist,
description: specialistDescription,
name: `transfer_to_${specialistName}`,
};
});
}
static specialistAgentBaseName(name, index) {
return (name === null || name === void 0 ? void 0 : name.trim().toLowerCase().replace(/ /g, '_')) || `ai_agent_${index}`;
}
getAIAgentResponse(botonicContext, previousContents) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
const flowBuilderPlugin = getFlowBuilderPlugin(botonicContext.plugins);
const aiAgentResponse = yield ((_a = flowBuilderPlugin.getAiAgentResponse) === null || _a === void 0 ? void 0 : _a.call(flowBuilderPlugin, botonicContext, {
type: AiAgentType.Router,
name: this.name,
instructions: this.instructions,
model: this.model,
verbosity: this.verbosity,
specialists: this.availableSpecialists.map(({ agent, description, name }) => {
var _a, _b, _c;
return ({
type: AiAgentType.Specialist,
name,
description,
instructions: agent.instructions,
model: agent.model,
verbosity: agent.verbosity,
activeTools: (_a = agent.activeTools) !== null && _a !== void 0 ? _a : [],
inputGuardrailRules: this.getActiveInputGuardrailRules(agent.inputGuardrailRules),
sourceIds: (_c = (_b = agent.sources) === null || _b === void 0 ? void 0 : _b.map(s => s.id)) !== null && _c !== void 0 ? _c : [],
});
}),
inputGuardrailRules: this.getActiveInputGuardrailRules(this.inputGuardrailRules),
previousHubtypeMessages: this.getPreviousHubtypeContents(previousContents),
}));
return aiAgentResponse;
});
}
trackAiAgentResponse(botonicContext) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
const isTransferredToSpecialist = (_b = (_a = this.aiAgentResponse) === null || _a === void 0 ? void 0 : _a.isTransferredToSpecialist) !== null && _b !== void 0 ? _b : false;
yield this.trackAIAgentRouterEvent(botonicContext, isTransferredToSpecialist);
if (isTransferredToSpecialist) {
yield this.trackAIAgentSpecialistEvent(botonicContext);
}
});
}
trackAIAgentRouterEvent(botonicContext, isTransferredToSpecialist) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r;
const { flowThreadId, flowId, flowName, flowNodeId } = yield getCommonFlowContentEventArgsForContentId(botonicContext, this.id);
const routerEvent = {
action: EventAction.AiAgentRouter,
flowThreadId,
flowId,
flowName,
flowNodeId,
flowNodeContentId: this.name,
flowNodeIsMeaningful: true,
memoryLength: (_b = (_a = this.aiAgentResponse) === null || _a === void 0 ? void 0 : _a.memoryLength) !== null && _b !== void 0 ? _b : 0,
inputMessageId: botonicContext.input.dbId,
inputGuardrailsTriggered: (_d = (_c = this.aiAgentResponse) === null || _c === void 0 ? void 0 : _c.inputGuardrailsTriggered) !== null && _d !== void 0 ? _d : [],
outputGuardrailsTriggered: (_f = (_e = this.aiAgentResponse) === null || _e === void 0 ? void 0 : _e.outputGuardrailsTriggered) !== null && _f !== void 0 ? _f : [],
exit: (_h = (_g = this.aiAgentResponse) === null || _g === void 0 ? void 0 : _g.exit) !== null && _h !== void 0 ? _h : true,
error: (_k = (_j = this.aiAgentResponse) === null || _j === void 0 ? void 0 : _j.error) !== null && _k !== void 0 ? _k : false,
startingAgentName: ((_l = this.aiAgentResponse) === null || _l === void 0 ? void 0 : _l.startingAgentName) || this.name,
lastAgentName: ((_m = this.aiAgentResponse) === null || _m === void 0 ? void 0 : _m.lastAgentName) || this.name,
availableSpecialists: (_p = (_o = this.aiAgentResponse) === null || _o === void 0 ? void 0 : _o.availableSpecialists) !== null && _p !== void 0 ? _p : [],
isTransferredToSpecialist,
toolsExecuted: (_r = (_q = this.aiAgentResponse) === null || _q === void 0 ? void 0 : _q.toolsExecuted) !== null && _r !== void 0 ? _r : [],
};
const { action } = routerEvent, routerEventArgs = __rest(routerEvent, ["action"]);
yield trackEvent(botonicContext, action, routerEventArgs);
});
}
trackAIAgentSpecialistEvent(botonicContext) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
const { flowThreadId, flowId, flowName, flowNodeId } = yield getCommonFlowContentEventArgsForContentId(botonicContext, this.id);
const specialistEvent = {
action: EventAction.AiAgent,
flowThreadId,
flowId,
flowName,
flowNodeId,
flowNodeContentId: (_b = (_a = this.aiAgentResponse) === null || _a === void 0 ? void 0 : _a.lastAgentName) !== null && _b !== void 0 ? _b : '',
flowNodeIsMeaningful: true,
toolsExecuted: (_d = (_c = this.aiAgentResponse) === null || _c === void 0 ? void 0 : _c.toolsExecuted) !== null && _d !== void 0 ? _d : [],
memoryLength: (_f = (_e = this.aiAgentResponse) === null || _e === void 0 ? void 0 : _e.memoryLength) !== null && _f !== void 0 ? _f : 0,
inputMessageId: botonicContext.input.dbId,
exit: (_h = (_g = this.aiAgentResponse) === null || _g === void 0 ? void 0 : _g.exit) !== null && _h !== void 0 ? _h : true,
inputGuardrailsTriggered: [],
outputGuardrailsTriggered: (_k = (_j = this.aiAgentResponse) === null || _j === void 0 ? void 0 : _j.outputGuardrailsTriggered) !== null && _k !== void 0 ? _k : [],
error: (_m = (_l = this.aiAgentResponse) === null || _l === void 0 ? void 0 : _l.error) !== null && _m !== void 0 ? _m : false,
};
const { action } = specialistEvent, specialistEventArgs = __rest(specialistEvent, ["action"]);
yield trackEvent(botonicContext, action, specialistEventArgs);
});
}
}
//# sourceMappingURL=flow-ai-agent-router.js.map