2
0

(webhook) Add client execution option

This commit is contained in:
Baptiste Arnaud
2023-05-26 09:20:22 +02:00
parent 084a17ffc8
commit 75f9da0a4f
23 changed files with 426 additions and 306 deletions

View File

@ -0,0 +1,32 @@
import { getOpenAiStreamerQuery } from '@/queries/getOpenAiStreamerQuery'
import { ClientSideActionContext } from '@/types'
export 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
}

View File

@ -0,0 +1,23 @@
import { ExecutableWebhook } from '@typebot.io/schemas'
export const executeWebhook = async (
webhookToExecute: ExecutableWebhook
): Promise<string> => {
const { url, method, body, headers } = webhookToExecute
try {
const response = await fetch(url, {
method,
body: method !== 'GET' && body ? JSON.stringify(body) : undefined,
headers,
})
const statusCode = response.status
const data = await response.json()
return JSON.stringify({ statusCode, data })
} catch (error) {
console.error(error)
return JSON.stringify({
statusCode: 500,
data: 'An error occured while executing the webhook on the client',
})
}
}