2023-01-26 15:26:42 +01:00
|
|
|
import { executeChatwoot } from '@/features/blocks/integrations/chatwoot'
|
|
|
|
|
import { executeGoogleAnalyticsBlock } from '@/features/blocks/integrations/googleAnalytics/utils/executeGoogleAnalytics'
|
|
|
|
|
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-25 10:32:35 +02:00
|
|
|
import { getOpenAiStreamerQuery } from '@/queries/getOpenAiStreamerQuery'
|
2023-03-15 08:35:16 +01:00
|
|
|
import type { ChatReply } from '@typebot.io/schemas'
|
2023-01-26 15:26:42 +01:00
|
|
|
|
2023-05-25 10:32:35 +02:00
|
|
|
type ClientSideActionContext = {
|
|
|
|
|
apiHost?: string
|
|
|
|
|
sessionId: string
|
|
|
|
|
}
|
|
|
|
|
|
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-01-26 15:26:42 +01:00
|
|
|
}
|
2023-05-25 10:32:35 +02:00
|
|
|
|
|
|
|
|
const streamChat =
|
|
|
|
|
(context: ClientSideActionContext) =>
|
|
|
|
|
async (
|
|
|
|
|
messages: {
|
|
|
|
|
content?: string | undefined
|
|
|
|
|
role?: 'system' | 'user' | 'assistant' | undefined
|
|
|
|
|
}[],
|
|
|
|
|
{ onStreamedMessage }: { onStreamedMessage?: (message: string) => void }
|
|
|
|
|
) => {
|
|
|
|
|
const data = await getOpenAiStreamerQuery(context)(messages)
|
|
|
|
|
|
|
|
|
|
if (!data) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const reader = data.getReader()
|
|
|
|
|
const decoder = new TextDecoder()
|
|
|
|
|
let done = false
|
|
|
|
|
|
|
|
|
|
let message = ''
|
|
|
|
|
while (!done) {
|
|
|
|
|
const { value, done: doneReading } = await reader.read()
|
|
|
|
|
done = doneReading
|
|
|
|
|
const chunkValue = decoder.decode(value)
|
|
|
|
|
message += chunkValue
|
|
|
|
|
onStreamedMessage?.(message)
|
|
|
|
|
}
|
|
|
|
|
return message
|
|
|
|
|
}
|