⚡ (webhook) Add client execution option
This commit is contained in:
@ -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
|
||||
}
|
@ -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',
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user