Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { FlowAiAgent, type FlowContent } from '../../content-fields'
import type { FlowBuilderContext } from '../index'
import type { FlowContent } from '../content-fields/index'
import { splitAiAgentContents } from '../utils/ai-agent'
import type { FlowBuilderContext } from './context'

export async function getContentsByAiAgent({
export async function getContentsByAiAgentFromUserInput({
cmsApi,
flowBuilderPlugin,
request,
Expand Down Expand Up @@ -32,24 +33,3 @@ export async function getContentsByAiAgent({

return contents
}

interface AiAgentContentAndContentsBeforeAiAgent {
aiAgentContent: FlowAiAgent
contentsBeforeAiAgent: FlowContent[]
}

export function splitAiAgentContents(
contents: FlowContent[]
): AiAgentContentAndContentsBeforeAiAgent | undefined {
const aiAgentIndex = contents.findIndex(
content => content instanceof FlowAiAgent
)
if (aiAgentIndex < 0) {
return undefined
}

const aiAgentContent = contents[aiAgentIndex] as FlowAiAgent
const contentsBeforeAiAgent = contents.slice(0, aiAgentIndex)

return { aiAgentContent, contentsBeforeAiAgent }
}
29 changes: 29 additions & 0 deletions packages/botonic-plugin-flow-builder/src/action/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { BotContext } from '@botonic/core'

import type { FlowBuilderApi } from '../api'
import type BotonicPluginFlowBuilder from '../index'
import { getFlowBuilderPlugin } from '../utils/get-flow-builder-plugin'

export interface FlowBuilderContext {
cmsApi: FlowBuilderApi
flowBuilderPlugin: BotonicPluginFlowBuilder
request: BotContext
resolvedLocale: string
contentID?: string
}

export function getFlowBuilderActionContext(
request: BotContext,
contentID?: string
): FlowBuilderContext {
const flowBuilderPlugin = getFlowBuilderPlugin(request.plugins)
const cmsApi = flowBuilderPlugin.cmsApi
const resolvedLocale = flowBuilderPlugin.cmsApi.getResolvedLocale()
return {
cmsApi,
flowBuilderPlugin,
request,
resolvedLocale,
contentID,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
getCommonFlowContentEventArgsForContentId,
trackEvent,
} from '../tracking'
import type { FlowBuilderContext } from './index'
import type { FlowBuilderContext } from './context'

export async function getContentsByFallback({
cmsApi,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import type { FlowBuilderApi } from '../api'
import { MAIN_FLOW_NAME } from '../constants'
import { FlowBotAction, type FlowContent } from '../content-fields'
import type BotonicPluginFlowBuilder from '../index'
import { inputHasTextOrTranscript } from '../utils'
import { getContentsByAiAgent } from './ai-agent'
import type { FlowBuilderContext } from './index'
import { inputHasTextOrTranscript } from '../utils/input'
import { getContentsByAiAgentFromUserInput } from './ai-agent-from-user-input'
import type { FlowBuilderContext } from './context'
import { getContentsByKnowledgeBase } from './knowledge-bases'
import { getContentsByPayload } from './payload'

Expand Down Expand Up @@ -100,7 +100,7 @@ async function getContentsByUserInput(
}

if (!flowBuilderPlugin.disableAIAgentInFirstInteraction) {
const contentsByAiAgent = await getContentsByAiAgent(context)
const contentsByAiAgent = await getContentsByAiAgentFromUserInput(context)
return contentsByAiAgent
}

Expand Down
51 changes: 51 additions & 0 deletions packages/botonic-plugin-flow-builder/src/action/get-contents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { type BotContext, INPUT } from '@botonic/core'
import { EMPTY_PAYLOAD } from '../constants'
import type { FlowContent } from '../content-fields'
import { inputHasTextOrTranscript } from '../utils/input'
import { getContentsByAiAgentFromUserInput } from './ai-agent-from-user-input'
import { getFlowBuilderActionContext } from './context'
import { getContentsByFallback } from './fallback'
import { getContentsByFirstInteraction } from './first-interaction'
import { getContentsByKnowledgeBase } from './knowledge-bases'
import { getContentsByPayload } from './payload'

export async function getContents(
botContext: BotContext,
contentID?: string
): Promise<FlowContent[]> {
const context = getFlowBuilderActionContext(botContext, contentID)

if (botContext.session.is_first_interaction) {
return await getContentsByFirstInteraction(context)
}
// TODO: Add needed logic when we can define contents for multi locale queue position message
if (botContext.input.type === INPUT.EVENT_QUEUE_POSITION_CHANGED) {
return []
}

if (botContext.input.payload?.startsWith(EMPTY_PAYLOAD)) {
botContext.input.payload = undefined
}

if (botContext.input.payload || contentID) {
const contentsByPayload = await getContentsByPayload(context)
if (contentsByPayload.length > 0) {
return contentsByPayload
}

return await getContentsByFallback(context)
}

if (inputHasTextOrTranscript(botContext.input)) {
const aiAgentContents = await getContentsByAiAgentFromUserInput(context)
if (aiAgentContents.length > 0) {
return aiAgentContents
}
const knowledgeBaseContents = await getContentsByKnowledgeBase(context)
if (knowledgeBaseContents.length > 0) {
return knowledgeBaseContents
}
}

return await getContentsByFallback(context)
}
Loading
Loading