@ -2,13 +2,17 @@ import { parseVariables } from '@/features/variables'
|
||||
import {
|
||||
BubbleBlock,
|
||||
BubbleBlockType,
|
||||
ChatMessageContent,
|
||||
ChatMessage,
|
||||
ChatReply,
|
||||
Group,
|
||||
InputBlock,
|
||||
InputBlockType,
|
||||
RuntimeOptions,
|
||||
SessionState,
|
||||
} from 'models'
|
||||
import {
|
||||
isBubbleBlock,
|
||||
isDefined,
|
||||
isInputBlock,
|
||||
isIntegrationBlock,
|
||||
isLogicBlock,
|
||||
@ -16,6 +20,7 @@ import {
|
||||
import { executeLogic } from './executeLogic'
|
||||
import { getNextGroup } from './getNextGroup'
|
||||
import { executeIntegration } from './executeIntegration'
|
||||
import { computePaymentInputRuntimeOptions } from '@/features/blocks/inputs/payment/api'
|
||||
|
||||
export const executeGroup =
|
||||
(state: SessionState, currentReply?: ChatReply) =>
|
||||
@ -33,17 +38,20 @@ export const executeGroup =
|
||||
nextEdgeId = block.outgoingEdgeId
|
||||
|
||||
if (isBubbleBlock(block)) {
|
||||
messages.push({
|
||||
type: block.type,
|
||||
content: parseBubbleBlockContent(newSessionState)(block),
|
||||
})
|
||||
messages.push(parseBubbleBlockContent(newSessionState)(block))
|
||||
continue
|
||||
}
|
||||
|
||||
if (isInputBlock(block))
|
||||
return {
|
||||
messages,
|
||||
input: block,
|
||||
input: {
|
||||
...block,
|
||||
runtimeOptions: await computeRuntimeOptions(newSessionState)(block),
|
||||
prefilledValue: getPrefilledInputValue(
|
||||
newSessionState.typebot.variables
|
||||
)(block),
|
||||
},
|
||||
newSessionState: {
|
||||
...newSessionState,
|
||||
currentBlock: {
|
||||
@ -53,9 +61,9 @@ export const executeGroup =
|
||||
},
|
||||
}
|
||||
const executionResponse = isLogicBlock(block)
|
||||
? await executeLogic(state)(block)
|
||||
? await executeLogic(newSessionState)(block)
|
||||
: isIntegrationBlock(block)
|
||||
? await executeIntegration(state)(block)
|
||||
? await executeIntegration(newSessionState)(block)
|
||||
: null
|
||||
|
||||
if (!executionResponse) continue
|
||||
@ -84,30 +92,50 @@ export const executeGroup =
|
||||
)
|
||||
}
|
||||
|
||||
const computeRuntimeOptions =
|
||||
(state: SessionState) =>
|
||||
(block: InputBlock): Promise<RuntimeOptions> | undefined => {
|
||||
switch (block.type) {
|
||||
case InputBlockType.PAYMENT: {
|
||||
return computePaymentInputRuntimeOptions(state)(block.options)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const parseBubbleBlockContent =
|
||||
({ typebot: { variables } }: SessionState) =>
|
||||
(block: BubbleBlock): ChatMessageContent => {
|
||||
(block: BubbleBlock): ChatMessage => {
|
||||
switch (block.type) {
|
||||
case BubbleBlockType.TEXT: {
|
||||
const plainText = parseVariables(variables)(block.content.plainText)
|
||||
const html = parseVariables(variables)(block.content.html)
|
||||
return { plainText, html }
|
||||
return { type: block.type, content: { plainText, html } }
|
||||
}
|
||||
case BubbleBlockType.IMAGE: {
|
||||
const url = parseVariables(variables)(block.content.url)
|
||||
return { url }
|
||||
return { type: block.type, content: { ...block.content, url } }
|
||||
}
|
||||
case BubbleBlockType.VIDEO: {
|
||||
const url = parseVariables(variables)(block.content.url)
|
||||
return { url }
|
||||
return { type: block.type, content: { ...block.content, url } }
|
||||
}
|
||||
case BubbleBlockType.AUDIO: {
|
||||
const url = parseVariables(variables)(block.content.url)
|
||||
return { url }
|
||||
return { type: block.type, content: { ...block.content, url } }
|
||||
}
|
||||
case BubbleBlockType.EMBED: {
|
||||
const url = parseVariables(variables)(block.content.url)
|
||||
return { url }
|
||||
return { type: block.type, content: { ...block.content, url } }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const getPrefilledInputValue =
|
||||
(variables: SessionState['typebot']['variables']) => (block: InputBlock) => {
|
||||
return (
|
||||
variables.find(
|
||||
(variable) =>
|
||||
variable.id === block.options.variableId && isDefined(variable.value)
|
||||
)?.value ?? undefined
|
||||
)
|
||||
}
|
||||
|
Reference in New Issue
Block a user