🚸 (openai) Parse stream on client to correctly handle errors
This commit is contained in:
@@ -7,14 +7,16 @@ import { executeSetVariable } from '@/features/blocks/logic/setVariable/executeS
|
||||
import { executeWait } from '@/features/blocks/logic/wait/utils/executeWait'
|
||||
import { executeWebhook } from '@/features/blocks/integrations/webhook/executeWebhook'
|
||||
import { ClientSideActionContext } from '@/types'
|
||||
import type { ChatReply } from '@typebot.io/schemas'
|
||||
import type { ChatReply, ReplyLog } from '@typebot.io/schemas'
|
||||
|
||||
export const executeClientSideAction = async (
|
||||
clientSideAction: NonNullable<ChatReply['clientSideActions']>[0],
|
||||
context: ClientSideActionContext,
|
||||
onStreamedMessage?: (message: string) => void
|
||||
): Promise<
|
||||
{ blockedPopupUrl: string } | { replyToSend: string | undefined } | void
|
||||
| { blockedPopupUrl: string }
|
||||
| { replyToSend: string | undefined; logs?: ReplyLog[] }
|
||||
| void
|
||||
> => {
|
||||
if ('chatwoot' in clientSideAction) {
|
||||
return executeChatwoot(clientSideAction.chatwoot)
|
||||
@@ -35,11 +37,22 @@ export const executeClientSideAction = async (
|
||||
return executeSetVariable(clientSideAction.setVariable.scriptToExecute)
|
||||
}
|
||||
if ('streamOpenAiChatCompletion' in clientSideAction) {
|
||||
const text = await streamChat(context)(
|
||||
const { error, message } = await streamChat(context)(
|
||||
clientSideAction.streamOpenAiChatCompletion.messages,
|
||||
{ onStreamedMessage }
|
||||
)
|
||||
return { replyToSend: text }
|
||||
if (error)
|
||||
return {
|
||||
replyToSend: undefined,
|
||||
logs: [
|
||||
{
|
||||
status: 'error',
|
||||
description: 'Failed to stream OpenAI completion',
|
||||
details: JSON.stringify(error, null, 2),
|
||||
},
|
||||
],
|
||||
}
|
||||
return { replyToSend: message }
|
||||
}
|
||||
if ('webhookToExecute' in clientSideAction) {
|
||||
const response = await executeWebhook(clientSideAction.webhookToExecute)
|
||||
|
||||
Reference in New Issue
Block a user