Skip to content

plugin-flow-builder: create utils folder and split in diferent files#3194

Merged
Iru89 merged 3 commits intorefactor-track-nodes-and-do-handoff-or-actionsfrom
refactor-utils-folder
Mar 30, 2026
Merged

plugin-flow-builder: create utils folder and split in diferent files#3194
Iru89 merged 3 commits intorefactor-track-nodes-and-do-handoff-or-actionsfrom
refactor-utils-folder

Conversation

@Iru89
Copy link
Copy Markdown
Contributor

@Iru89 Iru89 commented Mar 27, 2026

Description

This PR refactors the internal architecture of botonic-plugin-flow-builder by reorganising utility helpers into a dedicated utils/ folder, splitting the monolithic action orchestration logic into smaller focused modules, and replacing ActionRequest with BotContext across the board. It also introduces an abstract processContent method on every content field so that side effects (tracking, handoff, bot actions, AI agent resolution) are encapsulated inside each content type rather than centralised in FlowBuilderAction.

Context

The previous code mixed orchestration (which content to fetch) with rendering side effects (tracking events, executing handoffs, resolving AI agent responses) inside action/index.tsx. This made it hard to follow the execution flow, reuse individual pieces, and unit-test them in isolation. Additionally, helper functions were scattered across helpers.ts and ad-hoc files with no clear grouping strategy. The refactor creates a clean separation of concerns aligned with the existing content-field model.

Approach taken / Explain the design

1. New src/utils/ folder

Four focused utility modules replace the old helpers.ts catch-all:

File Responsibility
utils/get-flow-builder-plugin.ts Retrieves the BotonicPluginFlowBuilder instance from the plugin registry. Previously lived in helpers.ts.
utils/ai-agent.ts splitAiAgentContents() helper + HubtypeAssistantContent adapter class that converts FlowContent objects to plain-text strings for AI assistant context.
utils/input.ts Input-type guards (inputHasTextOrTranscript, getTextOrTranscript) and NLU shadowing checks (isKeywordsAllowed, isSmartIntentsAllowed, isKnowledgeBasesAllowed).
utils/token.ts resolveGetAccessToken() — selects the correct token-retrieval strategy depending on NODE_ENV.

2. action/context.ts — shared context object

Extracts the FlowBuilderContext interface (cmsApi, flowBuilderPlugin, request, resolvedLocale, contentID) and its factory function getFlowBuilderActionContext() into a standalone module. Every action module now receives a typed context instead of constructing it inline.

3. action/get-contents.ts — content-routing orchestrator

Moves the getContents() routing logic (first interaction, payload, AI agent, knowledge base, fallback) out of action/index.tsx into its own module. FlowBuilderAction.botonicInit now simply delegates to getContents() + prepareContentsToRender().

4. action/ai-agent-from-user-input.ts — replaces action/ai-agent/index.ts

The old getContentsByAiAgent function (and its internal structured-output wiring) is replaced by a thin getContentsByAiAgentFromUserInput that delegates the heavy lifting to the FlowAiAgent content field directly. The deleted folder action/ai-agent/ is no longer needed.

5. Abstract processContent on ContentFieldsBase

A new abstract method processContent(botContext: BotContext): Promise<void> is added to the base class. Each content field implements it to perform its own side effects (track event, execute handoff, run bot action, resolve AI agent response). FlowBuilderAction.prepareContentsToRender now iterates over filtered contents and calls content.processContent(), making trackAllContents and doHandoffAndBotActions unnecessary.

6. FlowAiAgent becomes self-contained

All AI agent inference logic previously scattered in action/ai-agent/index.ts is moved into FlowAiAgent:

  • resolveAIAgentResponse(botContext, previousContents?) — orchestrates inference, tracking, message parsing, and JSX generation.
  • getAIAgentResponse(botContext, previousContents?) — calls the underlying inference API.
  • Internal state changed from a single responses array to aiAgentResponse, messages, and jsxElements for clearer lifecycle semantics.

7. ActionRequestBotContext

Throughout ContentFieldsBase, FlowBuilderAction, and every content field, ActionRequest (from @botonic/react) is replaced by BotContext (from @botonic/core) to reduce the dependency on the React layer in pure-logic modules.

8. WhatsApp Button List — toBotonic rename

toBotonic functions in flow-whatsapp-button-list-section.tsx and flow-whatsapp-button-list-row.tsx are renamed to be consistent with the rest of the content field naming convention, and a new whatsapp-button-list/index.ts barrel is added.

@Iru89 Iru89 self-assigned this Mar 27, 2026
@chatgpt-codex-connector
Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Credits must be used to enable repository wide code reviews.

@Iru89 Iru89 force-pushed the refactor-utils-folder branch from 131622f to d9f832c Compare March 27, 2026 10:31
@Iru89 Iru89 requested review from asastre and vanbasten17 March 27, 2026 10:36
@Iru89 Iru89 merged commit 5946f45 into refactor-track-nodes-and-do-handoff-or-actions Mar 30, 2026
4 checks passed
@Iru89 Iru89 deleted the refactor-utils-folder branch March 30, 2026 09:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants