Skip to content

Commit

Permalink
🐛 (openai) Fix 2 openai streaming back to back
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Oct 12, 2023
1 parent a48026c commit 42fd603
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 16 deletions.
5 changes: 4 additions & 1 deletion packages/bot-engine/continueBotFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const continueBotFlow = async (
reply: string | undefined,
{ state, version }: Params
): Promise<ChatReply & { newSessionState: SessionState }> => {
let firstBubbleWasStreamed = false
let newSessionState = { ...state }

if (!newSessionState.currentBlock) return startBotFlow({ state, version })
Expand Down Expand Up @@ -81,6 +82,7 @@ export const continueBotFlow = async (
block.type === IntegrationBlockType.OPEN_AI &&
block.options.task === 'Create chat completion'
) {
firstBubbleWasStreamed = true
if (reply) {
const result = await resumeChatCompletion(state, {
options: block.options,
Expand Down Expand Up @@ -125,7 +127,7 @@ export const continueBotFlow = async (
...group,
blocks: group.blocks.slice(blockIndex + 1),
},
{ version, state: newSessionState }
{ version, state: newSessionState, firstBubbleWasStreamed }
)
return {
...chatReply,
Expand Down Expand Up @@ -157,6 +159,7 @@ export const continueBotFlow = async (
const chatReply = await executeGroup(nextGroup.group, {
version,
state: newSessionState,
firstBubbleWasStreamed,
})

return {
Expand Down
12 changes: 11 additions & 1 deletion packages/bot-engine/executeGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,18 @@ type ContextProps = {
state: SessionState
currentReply?: ChatReply
currentLastBubbleId?: string
firstBubbleWasStreamed?: boolean
}

export const executeGroup = async (
group: Group,
{ version, state, currentReply, currentLastBubbleId }: ContextProps
{
version,
state,
currentReply,
currentLastBubbleId,
firstBubbleWasStreamed,
}: ContextProps
): Promise<ChatReply & { newSessionState: SessionState }> => {
const messages: ChatReply['messages'] = currentReply?.messages ?? []
let clientSideActions: ChatReply['clientSideActions'] =
Expand All @@ -44,10 +51,13 @@ export const executeGroup = async (

let newSessionState = state

let index = -1
for (const block of group.blocks) {
index++
nextEdgeId = block.outgoingEdgeId

if (isBubbleBlock(block)) {
if (firstBubbleWasStreamed && index === 0) continue
messages.push(
parseBubbleBlock(block, {
version,
Expand Down
2 changes: 1 addition & 1 deletion packages/embeds/js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@typebot.io/js",
"version": "0.2.5",
"version": "0.2.6",
"description": "Javascript library to display typebots on your website",
"type": "module",
"main": "dist/index.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { InputBlockType } from '@typebot.io/schemas/features/blocks/inputs/enums
import {
createEffect,
createSignal,
createUniqueId,
For,
onCleanup,
onMount,
Expand Down Expand Up @@ -109,20 +108,19 @@ export const ConversationContainer = (props: Props) => {
})()
})

const streamMessage = (content: string) => {
const streamMessage = ({ id, message }: { id: string; message: string }) => {
setIsSending(false)
const lastChunk = [...chatChunks()].pop()
if (!lastChunk) return
const id = lastChunk.streamingMessageId ?? createUniqueId()
if (!lastChunk.streamingMessageId)
if (lastChunk.streamingMessageId !== id)
setChatChunks((displayedChunks) => [
...displayedChunks,
{
messages: [],
streamingMessageId: id,
},
])
setStreamingMessage({ id, content })
setStreamingMessage({ id, content: message })
}

createEffect(() => {
Expand Down Expand Up @@ -216,9 +214,7 @@ export const ConversationContainer = (props: Props) => {
...displayedChunks,
{
input: data.input,
messages: [...chatChunks()].pop()?.streamingMessageId
? data.messages.slice(1)
: data.messages,
messages: data.messages,
clientSideActions: data.clientSideActions,
},
])
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ClientSideActionContext } from '@/types'
import { guessApiHost } from '@/utils/guessApiHost'
import { isNotEmpty } from '@typebot.io/lib/utils'
import { createUniqueId } from 'solid-js'

let abortController: AbortController | null = null
const secondsToWaitBeforeRetries = 3
Expand All @@ -13,7 +14,9 @@ export const streamChat =
content?: string | undefined
role?: 'system' | 'user' | 'assistant' | undefined
}[],
{ onMessageStream }: { onMessageStream?: (message: string) => void }
{
onMessageStream,
}: { onMessageStream?: (props: { id: string; message: string }) => void }
): Promise<{ message?: string; error?: object }> => {
try {
abortController = new AbortController()
Expand Down Expand Up @@ -64,6 +67,8 @@ export const streamChat =
const reader = res.body.getReader()
const decoder = new TextDecoder()

const id = createUniqueId()

// eslint-disable-next-line no-constant-condition
while (true) {
const { done, value } = await reader.read()
Expand All @@ -72,7 +77,7 @@ export const streamChat =
}
const chunk = decoder.decode(value)
message += chunk
if (onMessageStream) onMessageStream(message)
if (onMessageStream) onMessageStream({ id, message })
if (abortController === null) {
reader.cancel()
break
Expand Down
2 changes: 1 addition & 1 deletion packages/embeds/js/src/utils/executeClientSideActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { injectStartProps } from './injectStartProps'
type Props = {
clientSideAction: NonNullable<ChatReply['clientSideActions']>[0]
context: ClientSideActionContext
onMessageStream?: (message: string) => void
onMessageStream?: (props: { id: string; message: string }) => void
}

export const executeClientSideAction = async ({
Expand Down
2 changes: 1 addition & 1 deletion packages/embeds/nextjs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@typebot.io/nextjs",
"version": "0.2.5",
"version": "0.2.6",
"description": "Convenient library to display typebots on your Next.js website",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/embeds/react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@typebot.io/react",
"version": "0.2.5",
"version": "0.2.6",
"description": "Convenient library to display typebots on your React app",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down

2 comments on commit 42fd603

@vercel
Copy link

@vercel vercel bot commented on 42fd603 Oct 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

viewer-v2 – ./apps/viewer

bii.bj
1stop.au
houss.io
wasap.nl
x.cr8.ai
yobot.me
klujo.com
me.cr8.ai
p1314.fun
sifuim.co
wachat.io
247987.com
8jours.top
support.triplo.ai
survey.collab.day
test.eqfeqfeq.com
viewer.typebot.io
welcome.triplo.ai
www.thegymgame.it
zeropendencia.com
1988.bouclidom.com
a.onewebcenter.com
acordorenovado.com
amostra-safe.click
andreimayer.com.br
bebesemcolicas.com
bot.innovacion.fun
bot.jogodospix.com
bot.jogomilion.com
bot.lucide.contact
bot.neferlopez.com
bot.photonative.de
bot.rajatanjak.com
bot.samplehunt.com
bot.sinalcerto.com
bot.wphelpchat.com
bots.robomotion.io
brandingmkt.com.br
bt.chatgptlabs.org
cadu.uninta.edu.br
chat.daftarjer.com
chat.foxbot.online
chat.hand-made.one
chat.tuanpakya.com
chat.webisharp.com
chatbotforthat.com
descobrindotudo.me
dicanatural.online
digitalhelp.com.au
draraquelnutri.com
drcarlosyoshi.site
goalsettingbot.com
golpenuncamais.com
leads.gecoelho.com
noticiasnet.online
novoappespiao.site
omarcodosanjos.com
pant.maxbot.com.br
pantherview.cr8.ai
positivobra.com.br
rollingball.cr8.ai
ruletaiapro.online
viewer-v2-typebot-io.vercel.app
mdb.assessoria.jbatista.progenbr.com
mdb.assessoria.mauricio.progenbr.com
mdb.evento.autocadastro.progenbr.com
form.shopmercedesbenzsouthorlando.com
mdb.evento.equipeinterna.progenbr.com
bot.studiotecnicoimmobiliaremerelli.it
mdb.assessoria.boaventura.progenbr.com
mdb.assessoria.jtrebesqui.progenbr.com
pesquisa.escolamodacomproposito.com.br
anamnese.clinicaramosodontologia.com.br
gabinete.baleia.formulario.progenbr.com
mdb.assessoria.carreirinha.progenbr.com
chrome-os-inquiry-system.itschromeos.com
mdb.assessoria.paulomarques.progenbr.com
viewer-v2-git-main-typebot-io.vercel.app
main-menu-for-itschromeos.itschromeos.com
mdb.assessoria.qrcode.ademir.progenbr.com
mdb.assessoria.qrcode.arthur.progenbr.com
mdb.assessoria.qrcode.danilo.progenbr.com
mdb.assessoria.qrcode.marcao.progenbr.com
mdb.assessoria.qrcode.marcio.progenbr.com
mdb.assessoria.qrcode.aloisio.progenbr.com
mdb.assessoria.qrcode.girotto.progenbr.com
mdb.assessoria.qrcode.marinho.progenbr.com
mdb.assessoria.qrcode.rodrigo.progenbr.com
mdb.assessoria.carlosalexandre.progenbr.com
mdb.assessoria.qrcode.desideri.progenbr.com
mdb.assessoria.qrcode.fernanda.progenbr.com
mdb.assessoria.qrcode.jbatista.progenbr.com
mdb.assessoria.qrcode.mauricio.progenbr.com
mdb.assessoria.fernanda.regional.progenbr.com
mdb.assessoria.qrcode.boaventura.progenbr.com
mdb.assessoria.qrcode.jtrebesqui.progenbr.com
mdb.assessoria.qrcode.carreirinha.progenbr.com
mdb.assessoria.qrcode.paulomarques.progenbr.com
mdb.assessoria.qrcode.carlosalexandre.progenbr.com
mdb.assessoria.qrcode.fernanda.regional.progenbr.com

@vercel
Copy link

@vercel vercel bot commented on 42fd603 Oct 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

builder-v2 – ./apps/builder

app.typebot.io
builder-v2-typebot-io.vercel.app
builder-v2-git-main-typebot-io.vercel.app

Please sign in to comment.