2
0

♻️ Introduce typebot v6 with events (#1013)

Closes #885
This commit is contained in:
Baptiste Arnaud
2023-11-08 15:34:16 +01:00
committed by GitHub
parent 68e4fc71fb
commit 35300eaf34
634 changed files with 58971 additions and 31449 deletions

View File

@@ -2,7 +2,6 @@ import {
ChatReply,
Group,
InputBlock,
InputBlockType,
RuntimeOptions,
SessionState,
} from '@typebot.io/schemas'
@@ -22,7 +21,12 @@ import { injectVariableValuesInPictureChoiceBlock } from './blocks/inputs/pictur
import { getPrefilledInputValue } from './getPrefilledValue'
import { parseDateInput } from './blocks/inputs/date/parseDateInput'
import { deepParseVariables } from './variables/deepParseVariables'
import { parseBubbleBlock } from './parseBubbleBlock'
import {
BubbleBlockWithDefinedContent,
parseBubbleBlock,
} from './parseBubbleBlock'
import { InputBlockType } from '@typebot.io/schemas/features/blocks/inputs/constants'
import { VisitedEdge } from '@typebot.io/prisma'
type ContextProps = {
version: 1 | 2
@@ -30,6 +34,7 @@ type ContextProps = {
currentReply?: ChatReply
currentLastBubbleId?: string
firstBubbleWasStreamed?: boolean
visitedEdges: VisitedEdge[]
}
export const executeGroup = async (
@@ -37,11 +42,14 @@ export const executeGroup = async (
{
version,
state,
visitedEdges,
currentReply,
currentLastBubbleId,
firstBubbleWasStreamed,
}: ContextProps
): Promise<ChatReply & { newSessionState: SessionState }> => {
): Promise<
ChatReply & { newSessionState: SessionState; visitedEdges: VisitedEdge[] }
> => {
const messages: ChatReply['messages'] = currentReply?.messages ?? []
let clientSideActions: ChatReply['clientSideActions'] =
currentReply?.clientSideActions
@@ -57,11 +65,12 @@ export const executeGroup = async (
nextEdgeId = block.outgoingEdgeId
if (isBubbleBlock(block)) {
if (firstBubbleWasStreamed && index === 0) continue
if (!block.content || (firstBubbleWasStreamed && index === 0)) continue
messages.push(
parseBubbleBlock(block, {
parseBubbleBlock(block as BubbleBlockWithDefinedContent, {
version,
variables: newSessionState.typebotsQueue[0].typebot.variables,
typebotVersion: newSessionState.typebotsQueue[0].typebot.version,
})
)
lastBubbleBlockId = block.id
@@ -74,13 +83,11 @@ export const executeGroup = async (
input: await parseInput(newSessionState)(block),
newSessionState: {
...newSessionState,
currentBlock: {
groupId: group.id,
blockId: block.id,
},
currentBlockId: block.id,
},
clientSideActions,
logs,
visitedEdges,
}
const executionResponse = isLogicBlock(block)
? await executeLogic(newSessionState)(block)
@@ -113,13 +120,11 @@ export const executeGroup = async (
messages,
newSessionState: {
...newSessionState,
currentBlock: {
groupId: group.id,
blockId: block.id,
},
currentBlockId: block.id,
},
clientSideActions,
logs,
visitedEdges,
}
}
}
@@ -131,19 +136,22 @@ export const executeGroup = async (
}
if (!nextEdgeId && newSessionState.typebotsQueue.length === 1)
return { messages, newSessionState, clientSideActions, logs }
return { messages, newSessionState, clientSideActions, logs, visitedEdges }
const nextGroup = await getNextGroup(newSessionState)(nextEdgeId ?? undefined)
newSessionState = nextGroup.newSessionState
if (nextGroup.visitedEdge) visitedEdges.push(nextGroup.visitedEdge)
if (!nextGroup.group) {
return { messages, newSessionState, clientSideActions, logs }
return { messages, newSessionState, clientSideActions, logs, visitedEdges }
}
return executeGroup(nextGroup.group, {
version,
state: newSessionState,
visitedEdges,
currentReply: {
messages,
clientSideActions,
@@ -188,14 +196,14 @@ export const parseInput =
...parsedBlock,
options: {
...parsedBlock.options,
min: isNotEmpty(parsedBlock.options.min as string)
? Number(parsedBlock.options.min)
min: isNotEmpty(parsedBlock.options?.min as string)
? Number(parsedBlock.options?.min)
: undefined,
max: isNotEmpty(parsedBlock.options.max as string)
? Number(parsedBlock.options.max)
max: isNotEmpty(parsedBlock.options?.max as string)
? Number(parsedBlock.options?.max)
: undefined,
step: isNotEmpty(parsedBlock.options.step as string)
? Number(parsedBlock.options.step)
step: isNotEmpty(parsedBlock.options?.step as string)
? Number(parsedBlock.options?.step)
: undefined,
},
}