23
packages/bot-engine/getFirstEdgeId.ts
Normal file
23
packages/bot-engine/getFirstEdgeId.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import { TRPCError } from '@trpc/server'
|
||||
import { SessionState } from '@typebot.io/schemas'
|
||||
|
||||
export const getFirstEdgeId = ({
|
||||
state,
|
||||
startEventId,
|
||||
}: {
|
||||
state: SessionState
|
||||
startEventId: string | undefined
|
||||
}) => {
|
||||
const { typebot } = state.typebotsQueue[0]
|
||||
if (startEventId) {
|
||||
const event = typebot.events?.find((e) => e.id === startEventId)
|
||||
if (!event)
|
||||
throw new TRPCError({
|
||||
code: 'BAD_REQUEST',
|
||||
message: "Start event doesn't exist",
|
||||
})
|
||||
return event.outgoingEdgeId
|
||||
}
|
||||
if (typebot.version === '6') return typebot.events[0].outgoingEdgeId
|
||||
return typebot.groups[0].blocks[0].outgoingEdgeId
|
||||
}
|
@ -7,6 +7,7 @@ import {
|
||||
import { executeGroup } from './executeGroup'
|
||||
import { getNextGroup } from './getNextGroup'
|
||||
import { VisitedEdge } from '@typebot.io/prisma'
|
||||
import { getFirstEdgeId } from './getFirstEdgeId'
|
||||
|
||||
type Props = {
|
||||
version: 1 | 2
|
||||
@ -60,24 +61,3 @@ export const startBotFlow = async ({
|
||||
startTime,
|
||||
})
|
||||
}
|
||||
|
||||
const getFirstEdgeId = ({
|
||||
state,
|
||||
startEventId,
|
||||
}: {
|
||||
state: SessionState
|
||||
startEventId: string | undefined
|
||||
}) => {
|
||||
const { typebot } = state.typebotsQueue[0]
|
||||
if (startEventId) {
|
||||
const event = typebot.events?.find((e) => e.id === startEventId)
|
||||
if (!event)
|
||||
throw new TRPCError({
|
||||
code: 'BAD_REQUEST',
|
||||
message: "Start event doesn't exist",
|
||||
})
|
||||
return event.outgoingEdgeId
|
||||
}
|
||||
if (typebot.version === '6') return typebot.events[0].outgoingEdgeId
|
||||
return typebot.groups[0].blocks[0].outgoingEdgeId
|
||||
}
|
||||
|
@ -36,6 +36,7 @@ import { IntegrationBlockType } from '@typebot.io/schemas/features/blocks/integr
|
||||
import { defaultTheme } from '@typebot.io/schemas/features/typebot/theme/constants'
|
||||
import { VisitedEdge } from '@typebot.io/prisma'
|
||||
import { env } from '@typebot.io/env'
|
||||
import { getFirstEdgeId } from './getFirstEdgeId'
|
||||
|
||||
type StartParams =
|
||||
| ({
|
||||
@ -166,9 +167,14 @@ export const startSession = async ({
|
||||
|
||||
// If params has message and first block is an input block, we can directly continue the bot flow
|
||||
if (message) {
|
||||
const firstEdgeId =
|
||||
chatReply.newSessionState.typebotsQueue[0].typebot.groups[0].blocks[0]
|
||||
.outgoingEdgeId
|
||||
const firstEdgeId = getFirstEdgeId({
|
||||
state: chatReply.newSessionState,
|
||||
startEventId:
|
||||
startParams.type === 'preview' &&
|
||||
startParams.startFrom?.type === 'event'
|
||||
? startParams.startFrom.eventId
|
||||
: undefined,
|
||||
})
|
||||
const nextGroup = await getNextGroup(chatReply.newSessionState)(firstEdgeId)
|
||||
const newSessionState = nextGroup.newSessionState
|
||||
const firstBlock = nextGroup.group?.blocks.at(0)
|
||||
|
Reference in New Issue
Block a user