2023-01-26 15:26:42 +01:00
|
|
|
import { executeChatwoot } from '@/features/blocks/integrations/chatwoot'
|
|
|
|
|
import { executeGoogleAnalyticsBlock } from '@/features/blocks/integrations/googleAnalytics/utils/executeGoogleAnalytics'
|
2023-05-26 09:20:22 +02:00
|
|
|
import { streamChat } from '@/features/blocks/integrations/openai/streamChat'
|
2023-01-26 15:26:42 +01:00
|
|
|
import { executeRedirect } from '@/features/blocks/logic/redirect'
|
2023-01-27 15:58:05 +01:00
|
|
|
import { executeScript } from '@/features/blocks/logic/script/executeScript'
|
2023-04-14 12:11:42 +02:00
|
|
|
import { executeSetVariable } from '@/features/blocks/logic/setVariable/executeSetVariable'
|
2023-01-26 18:23:09 +01:00
|
|
|
import { executeWait } from '@/features/blocks/logic/wait/utils/executeWait'
|
2023-05-26 09:20:22 +02:00
|
|
|
import { executeWebhook } from '@/features/blocks/integrations/webhook/executeWebhook'
|
|
|
|
|
import { ClientSideActionContext } from '@/types'
|
2023-03-15 08:35:16 +01:00
|
|
|
import type { ChatReply } from '@typebot.io/schemas'
|
2023-01-26 15:26:42 +01:00
|
|
|
|
|
|
|
|
export const executeClientSideAction = async (
|
2023-05-25 10:32:35 +02:00
|
|
|
clientSideAction: NonNullable<ChatReply['clientSideActions']>[0],
|
|
|
|
|
context: ClientSideActionContext,
|
|
|
|
|
onStreamedMessage?: (message: string) => void
|
2023-04-14 12:11:42 +02:00
|
|
|
): Promise<
|
|
|
|
|
{ blockedPopupUrl: string } | { replyToSend: string | undefined } | void
|
|
|
|
|
> => {
|
2023-01-26 15:26:42 +01:00
|
|
|
if ('chatwoot' in clientSideAction) {
|
2023-02-20 08:36:48 +01:00
|
|
|
return executeChatwoot(clientSideAction.chatwoot)
|
2023-01-26 15:26:42 +01:00
|
|
|
}
|
|
|
|
|
if ('googleAnalytics' in clientSideAction) {
|
2023-02-20 08:36:48 +01:00
|
|
|
return executeGoogleAnalyticsBlock(clientSideAction.googleAnalytics)
|
2023-01-26 15:26:42 +01:00
|
|
|
}
|
2023-01-27 15:58:05 +01:00
|
|
|
if ('scriptToExecute' in clientSideAction) {
|
2023-02-20 08:36:48 +01:00
|
|
|
return executeScript(clientSideAction.scriptToExecute)
|
2023-01-26 15:26:42 +01:00
|
|
|
}
|
|
|
|
|
if ('redirect' in clientSideAction) {
|
2023-02-20 08:36:48 +01:00
|
|
|
return executeRedirect(clientSideAction.redirect)
|
2023-01-26 15:26:42 +01:00
|
|
|
}
|
2023-01-26 18:23:09 +01:00
|
|
|
if ('wait' in clientSideAction) {
|
2023-02-20 08:36:48 +01:00
|
|
|
return executeWait(clientSideAction.wait)
|
2023-01-26 18:23:09 +01:00
|
|
|
}
|
2023-04-14 12:11:42 +02:00
|
|
|
if ('setVariable' in clientSideAction) {
|
|
|
|
|
return executeSetVariable(clientSideAction.setVariable.scriptToExecute)
|
|
|
|
|
}
|
2023-05-25 10:32:35 +02:00
|
|
|
if ('streamOpenAiChatCompletion' in clientSideAction) {
|
|
|
|
|
const text = await streamChat(context)(
|
|
|
|
|
clientSideAction.streamOpenAiChatCompletion.messages,
|
|
|
|
|
{ onStreamedMessage }
|
|
|
|
|
)
|
|
|
|
|
return { replyToSend: text }
|
|
|
|
|
}
|
2023-05-26 09:20:22 +02:00
|
|
|
if ('webhookToExecute' in clientSideAction) {
|
|
|
|
|
const response = await executeWebhook(clientSideAction.webhookToExecute)
|
|
|
|
|
return { replyToSend: response }
|
2023-05-25 10:32:35 +02:00
|
|
|
}
|
2023-05-26 09:20:22 +02:00
|
|
|
}
|