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'
|
2023-06-28 09:52:03 +02:00
|
|
|
import { executePixel } from '@/features/blocks/integrations/pixel/executePixel'
|
2023-05-26 09:20:22 +02:00
|
|
|
import { ClientSideActionContext } from '@/types'
|
2023-06-16 19:26:29 +02:00
|
|
|
import type { ChatReply, ReplyLog } from '@typebot.io/schemas'
|
2023-06-28 09:52:03 +02:00
|
|
|
import { injectStartProps } from './injectStartProps'
|
2023-01-26 15:26:42 +01:00
|
|
|
|
2023-07-19 11:20:44 +02:00
|
|
|
type Props = {
|
|
|
|
|
clientSideAction: NonNullable<ChatReply['clientSideActions']>[0]
|
|
|
|
|
context: ClientSideActionContext
|
|
|
|
|
onMessageStream?: (message: string) => void
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const executeClientSideAction = async ({
|
|
|
|
|
clientSideAction,
|
|
|
|
|
context,
|
|
|
|
|
onMessageStream,
|
|
|
|
|
}: Props): Promise<
|
2023-06-16 19:26:29 +02:00
|
|
|
| { blockedPopupUrl: string }
|
|
|
|
|
| { replyToSend: string | undefined; logs?: ReplyLog[] }
|
|
|
|
|
| void
|
2023-04-14 12:11:42 +02:00
|
|
|
> => {
|
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) {
|
2023-06-16 19:26:29 +02:00
|
|
|
const { error, message } = await streamChat(context)(
|
2023-05-25 10:32:35 +02:00
|
|
|
clientSideAction.streamOpenAiChatCompletion.messages,
|
2023-07-19 11:20:44 +02:00
|
|
|
{
|
|
|
|
|
onMessageStream: clientSideAction.streamOpenAiChatCompletion
|
|
|
|
|
.displayStream
|
|
|
|
|
? onMessageStream
|
|
|
|
|
: undefined,
|
|
|
|
|
}
|
2023-05-25 10:32:35 +02:00
|
|
|
)
|
2023-06-16 19:26:29 +02:00
|
|
|
if (error)
|
|
|
|
|
return {
|
|
|
|
|
replyToSend: undefined,
|
|
|
|
|
logs: [
|
|
|
|
|
{
|
|
|
|
|
status: 'error',
|
|
|
|
|
description: 'Failed to stream OpenAI completion',
|
|
|
|
|
details: JSON.stringify(error, null, 2),
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
return { replyToSend: message }
|
2023-05-25 10:32:35 +02:00
|
|
|
}
|
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-06-28 09:52:03 +02:00
|
|
|
if ('startPropsToInject' in clientSideAction) {
|
|
|
|
|
return injectStartProps(clientSideAction.startPropsToInject)
|
|
|
|
|
}
|
|
|
|
|
if ('pixel' in clientSideAction) {
|
|
|
|
|
return executePixel(clientSideAction.pixel)
|
|
|
|
|
}
|
2023-05-26 09:20:22 +02:00
|
|
|
}
|