2
0

Add dynamic timeout to bot engine api

This commit is contained in:
Baptiste Arnaud
2023-12-08 13:43:58 +00:00
parent 8819e9e567
commit 957eaf33dd
16 changed files with 124 additions and 31 deletions

View File

@ -27,6 +27,9 @@ import {
} from './parseBubbleBlock'
import { InputBlockType } from '@typebot.io/schemas/features/blocks/inputs/constants'
import { VisitedEdge } from '@typebot.io/prisma'
import { env } from '@typebot.io/env'
import { TRPCError } from '@trpc/server'
import { ExecuteIntegrationResponse, ExecuteLogicResponse } from './types'
type ContextProps = {
version: 1 | 2
@ -35,6 +38,7 @@ type ContextProps = {
currentLastBubbleId?: string
firstBubbleWasStreamed?: boolean
visitedEdges: VisitedEdge[]
startTime?: number
}
export const executeGroup = async (
@ -46,6 +50,7 @@ export const executeGroup = async (
currentReply,
currentLastBubbleId,
firstBubbleWasStreamed,
startTime,
}: ContextProps
): Promise<
ContinueChatResponse & {
@ -53,6 +58,7 @@ export const executeGroup = async (
visitedEdges: VisitedEdge[]
}
> => {
let newStartTime = startTime
const messages: ContinueChatResponse['messages'] =
currentReply?.messages ?? []
let clientSideActions: ContinueChatResponse['clientSideActions'] =
@ -65,6 +71,17 @@ export const executeGroup = async (
let index = -1
for (const block of group.blocks) {
if (
newStartTime &&
env.CHAT_API_TIMEOUT &&
Date.now() - newStartTime > env.CHAT_API_TIMEOUT
) {
throw new TRPCError({
code: 'TIMEOUT',
message: `${env.CHAT_API_TIMEOUT / 1000} seconds timeout reached`,
})
}
index++
nextEdgeId = block.outgoingEdgeId
@ -93,13 +110,20 @@ export const executeGroup = async (
logs,
visitedEdges,
}
const executionResponse = isLogicBlock(block)
? await executeLogic(newSessionState)(block)
: isIntegrationBlock(block)
? await executeIntegration(newSessionState)(block)
: null
const executionResponse = (
isLogicBlock(block)
? await executeLogic(newSessionState)(block)
: isIntegrationBlock(block)
? await executeIntegration(newSessionState)(block)
: null
) as ExecuteLogicResponse | ExecuteIntegrationResponse | null
if (!executionResponse) continue
if (
'startTimeShouldBeUpdated' in executionResponse &&
executionResponse.startTimeShouldBeUpdated
)
newStartTime = Date.now()
if (executionResponse.logs)
logs = [...(logs ?? []), ...executionResponse.logs]
if (executionResponse.newSessionState)
@ -162,6 +186,7 @@ export const executeGroup = async (
logs,
},
currentLastBubbleId: lastBubbleBlockId,
startTime: newStartTime,
})
}