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

@@ -115,6 +115,7 @@ export const createSpeechOpenAI = async (
])
return {
startTimeShouldBeUpdated: true,
outgoingEdgeId,
newSessionState,
}

View File

@@ -120,6 +120,7 @@ export const createChatCompletionOpenAI = async (
})
if (!chatCompletion)
return {
startTimeShouldBeUpdated: true,
outgoingEdgeId,
logs,
}
@@ -127,13 +128,16 @@ export const createChatCompletionOpenAI = async (
const totalTokens = chatCompletion.usage?.total_tokens
if (isEmpty(messageContent)) {
console.error('OpenAI block returned empty message', chatCompletion.choices)
return { outgoingEdgeId, newSessionState }
return { outgoingEdgeId, newSessionState, startTimeShouldBeUpdated: true }
}
return {
...(await resumeChatCompletion(newSessionState, {
options,
outgoingEdgeId,
logs,
})(messageContent, totalTokens)),
startTimeShouldBeUpdated: true,
}
return resumeChatCompletion(newSessionState, {
options,
outgoingEdgeId,
logs,
})(messageContent, totalTokens)
}
const isNextBubbleMessageWithAssistantMessage =

View File

@@ -30,6 +30,15 @@ type ParsedWebhook = ExecutableWebhook & {
isJson: boolean
}
export const responseDefaultTimeout = 10000
export const longRequestTimeout = 120000
const longReqTimeoutWhitelist = [
'https://api.openai.com',
'https://retune.so',
'https://www.chatbase.co',
]
export const executeWebhookBlock = async (
state: SessionState,
block: WebhookBlock | ZapierBlock | MakeComBlock | PabblyConnectBlock
@@ -64,14 +73,21 @@ export const executeWebhookBlock = async (
},
],
}
const { response: webhookResponse, logs: executeWebhookLogs } =
await executeWebhook(parsedWebhook)
return resumeWebhookExecution({
state,
block,
logs: executeWebhookLogs,
const {
response: webhookResponse,
})
logs: executeWebhookLogs,
startTimeShouldBeUpdated,
} = await executeWebhook(parsedWebhook)
return {
...resumeWebhookExecution({
state,
block,
logs: executeWebhookLogs,
response: webhookResponse,
}),
startTimeShouldBeUpdated,
}
}
const checkIfBodyIsAVariable = (body: string) => /^{{.+}}$/.test(body)
@@ -142,11 +158,19 @@ const parseWebhookAttributes =
export const executeWebhook = async (
webhook: ParsedWebhook
): Promise<{ response: WebhookResponse; logs?: ChatLog[] }> => {
): Promise<{
response: WebhookResponse
logs?: ChatLog[]
startTimeShouldBeUpdated?: boolean
}> => {
const logs: ChatLog[] = []
const { headers, url, method, basicAuth, body, isJson } = webhook
const contentType = headers ? headers['Content-Type'] : undefined
const isLongRequest = longReqTimeoutWhitelist.some((whiteListedUrl) =>
url?.includes(whiteListedUrl)
)
const request = {
url,
method: method as Method,
@@ -159,7 +183,11 @@ export const executeWebhook = async (
form:
contentType?.includes('x-www-form-urlencoded') && body ? body : undefined,
body: body && !isJson ? (body as string) : undefined,
timeout: {
response: isLongRequest ? longRequestTimeout : responseDefaultTimeout,
},
} satisfies OptionsInit
try {
const response = await got(request.url, omit(request, 'url'))
logs.push({
@@ -177,6 +205,7 @@ export const executeWebhook = async (
data: safeJsonParse(response.body).data,
},
logs,
startTimeShouldBeUpdated: isLongRequest,
}
} catch (error) {
if (error instanceof HTTPError) {
@@ -193,7 +222,7 @@ export const executeWebhook = async (
response,
},
})
return { response, logs }
return { response, logs, startTimeShouldBeUpdated: isLongRequest }
}
const response = {
statusCode: 500,
@@ -208,7 +237,7 @@ export const executeWebhook = async (
response,
},
})
return { response, logs }
return { response, logs, startTimeShouldBeUpdated: isLongRequest }
}
}

View File

@@ -108,6 +108,7 @@ export const executeZemanticAiBlock = async (
} catch (e) {
console.error(e)
return {
startTimeShouldBeUpdated: true,
outgoingEdgeId: block.outgoingEdgeId,
logs: [
{
@@ -118,7 +119,11 @@ export const executeZemanticAiBlock = async (
}
}
return { outgoingEdgeId: block.outgoingEdgeId, newSessionState }
return {
outgoingEdgeId: block.outgoingEdgeId,
newSessionState,
startTimeShouldBeUpdated: true,
}
}
const replaceTemplateVars = (