UNPKG

@botonic/plugin-flow-builder

Version:

Botonic plugin for **Hubtype Flow Builder**: run and bridge flow-driven logic from bots on the **current** line.

188 lines 8.33 kB
import { WhatsappHeaderType } from '@botonic/shared'; import { HtButtonStyle } from '../content-fields/hubtype-fields'; import { FlowAiAgent, FlowAiAgentRouter, FlowCarousel, FlowImage, FlowText, FlowVideo, FlowWhatsappButtonList, FlowWhatsappCtaUrlButtonNode, FlowWhatsappTemplate, } from '../content-fields/index'; export function splitAiAgentContents(contents) { const routerIndex = contents.findIndex(content => content instanceof FlowAiAgentRouter); if (routerIndex >= 0) { return { aiAgentRouterContent: contents[routerIndex], contentsBeforeAiAgentRouter: contents.slice(0, routerIndex), }; } const aiAgentIndex = contents.findIndex(content => content instanceof FlowAiAgent); if (aiAgentIndex < 0) { return undefined; } return { aiAgentContent: contents[aiAgentIndex], contentsBeforeAiAgent: contents.slice(0, aiAgentIndex), }; } // biome-ignore lint/complexity/noStaticOnlyClass: namespace-like adapter with private static helpers export class HubtypeAssistantContent { static adapt(content) { if (content instanceof FlowText) { return HubtypeAssistantContent.formatFlowTextContent(content); } if (content instanceof FlowCarousel) { return HubtypeAssistantContent.formatCarouselContent(content); } if (content instanceof FlowImage) { return content.src ? `[Image]\n${content.src}` : '[Image]'; } if (content instanceof FlowVideo) { return content.src ? `[Video]\n${content.src}` : '[Video]'; } if (content instanceof FlowWhatsappButtonList) { return HubtypeAssistantContent.formatWhatsappButtonListContent(content); } if (content instanceof FlowWhatsappCtaUrlButtonNode) { return HubtypeAssistantContent.formatWhatsappCtaContent(content); } if (content instanceof FlowWhatsappTemplate) { return HubtypeAssistantContent.formatWhatsappTemplateContent(content); } return ''; } static quickReplyLabel(button) { var _a; const buttonText = (_a = button.text) === null || _a === void 0 ? void 0 : _a.trim(); if (buttonText) { return buttonText; } return button.payload || 'Option'; } static persistentButtonLabel(button) { var _a; return ((_a = button.text) === null || _a === void 0 ? void 0 : _a.trim()) || 'Button'; } static carouselButtonLabel(button) { var _a; return ((_a = button.text) === null || _a === void 0 ? void 0 : _a.trim()) || 'Action'; } static formatFlowTextContent(text) { var _a, _b; const body = (_b = (_a = text.text) === null || _a === void 0 ? void 0 : _a.trim()) !== null && _b !== void 0 ? _b : ''; const buttons = text.buttons; if (!body && buttons.length === 0) { return ''; } if (buttons.length === 0) { return text.text; } const isQuickReply = text.buttonStyle === HtButtonStyle.QUICK_REPLY; const blockTitle = isQuickReply ? 'Quick replies:' : 'Buttons:'; const lines = buttons.map((button, i) => { const label = isQuickReply ? HubtypeAssistantContent.quickReplyLabel(button) : HubtypeAssistantContent.persistentButtonLabel(button); return ` [${i + 1}] ${label}`; }); const block = [blockTitle, ...lines].join('\n'); if (!body) { return block; } return `${body}\n\n${block}`; } static formatCarouselContent(carousel) { var _a; const elements = carousel.elements; const n = elements.length; const mainText = ((_a = carousel.whatsappText) !== null && _a !== void 0 ? _a : '').trim(); if (n === 0) { return mainText; } let header; if (!mainText) { header = `Carousel displayed with ${n} items:`; } else if (mainText.endsWith(':')) { header = mainText; } else { header = `${mainText}\nCarousel with ${n} items:`; } const itemLines = elements.map((element, index) => { var _a, _b; const title = ((_a = element.title) === null || _a === void 0 ? void 0 : _a.trim()) || 'Item'; const subtitle = (_b = element.subtitle) === null || _b === void 0 ? void 0 : _b.trim(); const subtitlePart = subtitle ? ` - ${subtitle}` : ''; const btnLabels = []; if (element.button) { btnLabels.push(HubtypeAssistantContent.carouselButtonLabel(element.button)); } const btnPart = btnLabels.length > 0 ? ` [${btnLabels.join(', ')}]` : ''; return ` ${index + 1}. "${title}"${subtitlePart}${btnPart}`; }); return [header, ...itemLines].join('\n'); } static formatWhatsappButtonListContent(list) { var _a, _b; const body = (_b = (_a = list.text) === null || _a === void 0 ? void 0 : _a.trim()) !== null && _b !== void 0 ? _b : ''; const hasSections = list.sections.some(s => s.rows.length > 0); if (!body && !hasSections) { return '[WhatsApp Button List]'; } const parts = []; if (body) { parts.push(body); } if (hasSections) { const sectionBlocks = list.sections.map(section => { var _a; const rowLines = section.rows.map(row => { var _a, _b, _c, _d; const rowTitle = (_b = (_a = row.title) === null || _a === void 0 ? void 0 : _a.trim()) !== null && _b !== void 0 ? _b : ''; const rowDescription = (_d = (_c = row.description) === null || _c === void 0 ? void 0 : _c.trim()) !== null && _d !== void 0 ? _d : ''; if (!rowTitle && !rowDescription) { return ' - '; } if (!rowTitle) { return ` - ${rowDescription}`; } if (!rowDescription) { return ` - ${rowTitle}`; } return ` - ${rowTitle}: ${rowDescription}`; }); const rowsText = rowLines.join('\n'); const sectionTitle = (_a = section.title) === null || _a === void 0 ? void 0 : _a.trim(); if (sectionTitle) { return `${sectionTitle}:\n${rowsText}`; } return rowsText; }); parts.push(sectionBlocks.join('\n')); } return parts.join('\n\n'); } static formatWhatsappCtaContent(cta) { var _a, _b, _c, _d, _e, _f, _g, _h; const hasStructuredData = (cta.headerType === WhatsappHeaderType.Text && !!((_a = cta.header) === null || _a === void 0 ? void 0 : _a.trim())) || ((_b = cta.text) === null || _b === void 0 ? void 0 : _b.trim()) || ((_c = cta.footer) === null || _c === void 0 ? void 0 : _c.trim()) || ((_d = cta.displayText) === null || _d === void 0 ? void 0 : _d.trim()) || cta.url; if (!hasStructuredData) { return ((_e = cta.text) === null || _e === void 0 ? void 0 : _e.trim()) || '[WhatsApp CTA Button]'; } const lines = []; if (cta.headerType === WhatsappHeaderType.Text && ((_f = cta.header) === null || _f === void 0 ? void 0 : _f.trim())) { lines.push(cta.header.trim()); } if ((_g = cta.text) === null || _g === void 0 ? void 0 : _g.trim()) { lines.push(cta.text.trim()); } if ((_h = cta.footer) === null || _h === void 0 ? void 0 : _h.trim()) { lines.push(cta.footer.trim()); } const ctaLine = `[${cta.displayText || ''}]${cta.url ? ` (${cta.url})` : ''}`; lines.push(ctaLine); return lines.join('\n'); } static formatWhatsappTemplateContent(template) { const { name, language } = template.htWhatsappTemplate; return `WhatsApp Template: ${name} (${language})`; } } //# sourceMappingURL=ai-agent.js.map