2
0

🧑‍💻 (chat) Introduce startChat and continueChat endpoints

Closes #1030
This commit is contained in:
Baptiste Arnaud
2023-11-13 15:27:36 +01:00
parent 63233eb7ee
commit 084588a086
74 changed files with 28426 additions and 645 deletions

View File

@@ -1,5 +1,9 @@
import { TRPCError } from '@trpc/server'
import { ChatReply, SessionState, StartElementId } from '@typebot.io/schemas'
import {
ContinueChatResponse,
SessionState,
StartFrom,
} from '@typebot.io/schemas'
import { executeGroup } from './executeGroup'
import { getNextGroup } from './getNextGroup'
import { VisitedEdge } from '@typebot.io/prisma'
@@ -7,20 +11,24 @@ import { VisitedEdge } from '@typebot.io/prisma'
type Props = {
version: 1 | 2
state: SessionState
} & StartElementId
startFrom?: StartFrom
}
export const startBotFlow = async ({
version,
state,
...props
startFrom,
}: Props): Promise<
ChatReply & { newSessionState: SessionState; visitedEdges: VisitedEdge[] }
ContinueChatResponse & {
newSessionState: SessionState
visitedEdges: VisitedEdge[]
}
> => {
let newSessionState = state
const visitedEdges: VisitedEdge[] = []
if ('startGroupId' in props) {
if (startFrom?.type === 'group') {
const group = state.typebotsQueue[0].typebot.groups.find(
(group) => group.id === props.startGroupId
(group) => group.id === startFrom.groupId
)
if (!group)
throw new TRPCError({
@@ -35,7 +43,7 @@ export const startBotFlow = async ({
}
const firstEdgeId = getFirstEdgeId({
state: newSessionState,
startEventId: 'startEventId' in props ? props.startEventId : undefined,
startEventId: startFrom?.type === 'event' ? startFrom.eventId : undefined,
})
if (!firstEdgeId) return { messages: [], newSessionState, visitedEdges: [] }
const nextGroup = await getNextGroup(newSessionState)(firstEdgeId)