Skip to content

Commit

Permalink
⚡ (bot) Use ky for queries in bot to improve reliability
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Dec 20, 2023
1 parent f2cccbd commit a653646
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 135 deletions.
3 changes: 2 additions & 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.28",
"version": "0.2.29",
"description": "Javascript library to display typebots on your website",
"type": "module",
"main": "dist/index.js",
Expand All @@ -15,6 +15,7 @@
"@stripe/stripe-js": "1.54.1",
"@udecode/plate-common": "21.1.5",
"dompurify": "3.0.6",
"ky": "^1.1.3",
"marked": "9.0.3",
"solid-element": "1.7.1",
"solid-js": "1.7.8"
Expand Down
37 changes: 28 additions & 9 deletions packages/embeds/js/src/components/Bot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { InputBlock } from '@typebot.io/schemas'
import { StartFrom } from '@typebot.io/schemas'
import { defaultTheme } from '@typebot.io/schemas/features/typebot/theme/constants'
import { clsx } from 'clsx'
import { HTTPError } from 'ky'

export type BotProps = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -52,7 +53,7 @@ export const Bot = (props: BotProps & { class?: string }) => {
typeof props.typebot === 'string' ? props.typebot : undefined
const isPreview =
typeof props.typebot !== 'string' || (props.isPreview ?? false)
const { data, error, response } = await startChatQuery({
const { data, error } = await startChatQuery({
stripeRedirectStatus: urlParams.get('redirect_status') ?? undefined,
typebot: props.typebot,
apiHost: props.apiHost,
Expand All @@ -66,24 +67,42 @@ export const Bot = (props: BotProps & { class?: string }) => {
},
startFrom: props.startFrom,
})
if (error && 'code' in error && typeof error.code === 'string') {
if (error instanceof HTTPError) {
if (isPreview) {
return setError(
new Error('An error occurred while loading the bot.', {
cause: error.message,
new Error(`An error occurred while loading the bot.`, {
cause: {
status: error.response.status,
body: await error.response.json(),
},
})
)
}
if (['BAD_REQUEST', 'FORBIDDEN'].includes(error.code))
if (error.response.status === 400 || error.response.status === 403)
return setError(new Error('This bot is now closed.'))
if (error.code === 'NOT_FOUND')
if (error.response.status === 404)
return setError(new Error("The bot you're looking for doesn't exist."))
return setError(
new Error(
`Error! Couldn't initiate the chat. (${error.response.statusText})`
)
)
}

if (!data) {
if (error) console.error(error)
console.error({ data, error, response })
return setError(new Error("Error! Couldn't initiate the chat."))
if (error) {
console.error(error)
if (isPreview) {
return setError(
new Error(`Error! Could not reach server. Check your connection.`, {
cause: error,
})
)
}
}
return setError(
new Error('Error! Could not reach server. Check your connection.')
)
}

if (data.resultId && typebotIdFromProps)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
} from '@/utils/formattedMessagesSignal'
import { InputBlockType } from '@typebot.io/schemas/features/blocks/inputs/constants'
import { saveClientLogsQuery } from '@/queries/saveClientLogsQuery'
import { HTTPError } from 'ky'

const parseDynamicTheme = (
initialTheme: Theme,
Expand Down Expand Up @@ -173,7 +174,13 @@ export const ConversationContainer = (props: Props) => {
const errorLogs = [
{
description: 'Failed to send the reply',
details: error,
details:
error instanceof HTTPError
? {
status: error.response.status,
body: await error.response.json(),
}
: error,
status: 'error',
},
]
Expand Down
2 changes: 1 addition & 1 deletion packages/embeds/js/src/components/ErrorMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const ErrorMessage = (props: Props) => {
return (
<div class="h-full flex justify-center items-center flex-col">
<p class="text-2xl text-center">{props.error.message}</p>
<p class="text-center">{props.error.cause as string}</p>
<pre>{JSON.stringify(props.error.cause, null, 2) as string}</pre>
</div>
)
}
36 changes: 24 additions & 12 deletions packages/embeds/js/src/queries/continueChatQuery.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
import { guessApiHost } from '@/utils/guessApiHost'
import { isNotEmpty, sendRequest } from '@typebot.io/lib'
import { isNotEmpty } from '@typebot.io/lib'
import { ContinueChatResponse } from '@typebot.io/schemas'
import ky from 'ky'

export const continueChatQuery = ({
export const continueChatQuery = async ({
apiHost,
message,
sessionId,
}: {
apiHost?: string
message: string | undefined
sessionId: string
}) =>
sendRequest<ContinueChatResponse>({
method: 'POST',
url: `${
isNotEmpty(apiHost) ? apiHost : guessApiHost()
}/api/v1/sessions/${sessionId}/continueChat`,
body: {
message,
},
})
}) => {
try {
const data = await ky
.post(
`${
isNotEmpty(apiHost) ? apiHost : guessApiHost()
}/api/v1/sessions/${sessionId}/continueChat`,
{
json: {
message,
},
timeout: false,
}
)
.json<ContinueChatResponse>()

return { data }
} catch (error) {
return { error }
}
}
33 changes: 0 additions & 33 deletions packages/embeds/js/src/queries/getOpenAiStreamerQuery.ts

This file was deleted.

31 changes: 19 additions & 12 deletions packages/embeds/js/src/queries/saveClientLogsQuery.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
import { guessApiHost } from '@/utils/guessApiHost'
import type { ChatLog } from '@typebot.io/schemas'
import { isNotEmpty, sendRequest } from '@typebot.io/lib'
import { isNotEmpty } from '@typebot.io/lib'
import ky from 'ky'

export const saveClientLogsQuery = ({
export const saveClientLogsQuery = async ({
apiHost,
sessionId,
clientLogs,
}: {
apiHost?: string
sessionId: string
clientLogs: ChatLog[]
}) =>
sendRequest({
method: 'POST',
url: `${
isNotEmpty(apiHost) ? apiHost : guessApiHost()
}/api/v1/sessions/${sessionId}/clientLogs`,
body: {
clientLogs,
},
})
}) => {
try {
await ky.post(
`${
isNotEmpty(apiHost) ? apiHost : guessApiHost()
}/api/v1/sessions/${sessionId}/clientLogs`,
{
json: {
clientLogs,
},
}
)
} catch (e) {
console.log(e)
}
}
Loading

3 comments on commit a653646

@vercel
Copy link

@vercel vercel bot commented on a653646 Dec 20, 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:

landing-page-v2 – ./apps/landing-page

landing-page-v2-git-main-typebot-io.vercel.app
landing-page-v2-typebot-io.vercel.app
home.typebot.io

@vercel
Copy link

@vercel vercel bot commented on a653646 Dec 20, 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

sacnext.com
team.cr8.ai
triprint.my
wolf.cr8.ai
ai.meant.com
alphamen.pro
bet7k.online
bot.afric.ai
bot.grace.bj
bot.tobb.pro
chatgov.site
cinecorn.com
ezbooking.ai
gniorder.com
help.taxt.co
kusamint.com
p18bm940.fun
psmix.online
receita.info
rhino.cr8.ai
sheep.cr8.ai
snake.cr8.ai
svhm.mprs.in
tiger.cr8.ai
video.cr8.ai
webwhats.fun
webwhats.pro
whatsapp.web
yoda.riku.ai
zebra.cr8.ai
1nkagency.com
akademicq.com
alvodelas.com
atendente.org
bemestar.club
bot.goafk.com
bot.krdfy.com
cat.hidden.sg
cgcassets.com
chat.linky.li
cnvhub.com.br
drapamela.com
event24.store
facelabko.com
filmylogy.com
gikpro.online
goldorayo.com
govsaques.com
www.classmate.africa
www.epicbrain.com.br
www.professory.co.za
www.teachmate.africa
83701274.21000000.lol
87186327.21000000.one
90945247.21000000.one
97320578.21000000.one
98650901.21000000.one
viewer-v2-typebot-io.vercel.app
mdb.assessoria.desideri.progenbr.com
mdb.assessoria.fernanda.progenbr.com
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
www.typebot.io
get-typebot.com
www.get-typebot.com

@vercel
Copy link

@vercel vercel bot commented on a653646 Dec 20, 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-git-main-typebot-io.vercel.app
builder-v2-typebot-io.vercel.app

Please sign in to comment.