Skip to content

Commit

Permalink
🐛 (chat) Make sure objects are deeply parsed with variables value
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Dec 24, 2022
1 parent 449080b commit 431ad7c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ExecuteIntegrationResponse } from '@/features/chat'
import { parseVariablesInObject } from '@/features/variables'
import { deepParseVariable } from '@/features/variables'
import { GoogleAnalyticsBlock, SessionState } from 'models'

export const executeGoogleAnalyticsBlock = (
Expand All @@ -8,6 +8,6 @@ export const executeGoogleAnalyticsBlock = (
): ExecuteIntegrationResponse => ({
outgoingEdgeId: block.outgoingEdgeId,
integrations: {
googleAnalytics: parseVariablesInObject(block.options, variables),
googleAnalytics: deepParseVariable(variables)(block.options),
},
})
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { checkChatsUsage } from '@/features/usage'
import { parsePrefilledVariables } from '@/features/variables'
import {
parsePrefilledVariables,
deepParseVariable,
} from '@/features/variables'
import prisma from '@/lib/prisma'
import { publicProcedure } from '@/utils/server/trpc'
import { TRPCError } from '@trpc/server'
import { Prisma } from 'db'
import {
ChatReply,
chatReplySchema,
ChatSession,
PublicTypebot,
Expand Down Expand Up @@ -192,13 +196,13 @@ const startSession = async (startParams?: StartParams) => {
resultId: result?.id,
sessionId: session.id,
typebot: {
theme: typebot.theme,
settings: typebot.settings,
settings: deepParseVariable(typebot.variables)(typebot.settings),
theme: deepParseVariable(typebot.variables)(typebot.theme),
},
messages,
input,
logic,
}
} satisfies ChatReply
}

const getResult = async ({
Expand Down
41 changes: 6 additions & 35 deletions apps/viewer/src/features/chat/api/utils/executeGroup.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { parseVariables } from '@/features/variables'
import { deepParseVariable } from '@/features/variables'
import {
BubbleBlock,
BubbleBlockType,
ChatMessage,
ChatReply,
Group,
InputBlock,
Expand Down Expand Up @@ -38,20 +35,22 @@ export const executeGroup =
nextEdgeId = block.outgoingEdgeId

if (isBubbleBlock(block)) {
messages.push(parseBubbleBlockContent(newSessionState)(block))
messages.push(
deepParseVariable(newSessionState.typebot.variables)(block)
)
continue
}

if (isInputBlock(block))
return {
messages,
input: {
input: deepParseVariable(newSessionState.typebot.variables)({
...block,
runtimeOptions: await computeRuntimeOptions(newSessionState)(block),
prefilledValue: getPrefilledInputValue(
newSessionState.typebot.variables
)(block),
},
}),
newSessionState: {
...newSessionState,
currentBlock: {
Expand Down Expand Up @@ -102,34 +101,6 @@ const computeRuntimeOptions =
}
}

const parseBubbleBlockContent =
({ typebot: { variables } }: SessionState) =>
(block: BubbleBlock): ChatMessage => {
switch (block.type) {
case BubbleBlockType.TEXT: {
const plainText = parseVariables(variables)(block.content.plainText)
const html = parseVariables(variables)(block.content.html)
return { type: block.type, content: { plainText, html } }
}
case BubbleBlockType.IMAGE: {
const url = parseVariables(variables)(block.content.url)
return { type: block.type, content: { ...block.content, url } }
}
case BubbleBlockType.VIDEO: {
const url = parseVariables(variables)(block.content.url)
return { type: block.type, content: { ...block.content, url } }
}
case BubbleBlockType.AUDIO: {
const url = parseVariables(variables)(block.content.url)
return { type: block.type, content: { ...block.content, url } }
}
case BubbleBlockType.EMBED: {
const url = parseVariables(variables)(block.content.url)
return { type: block.type, content: { ...block.content, url } }
}
}
}

const getPrefilledInputValue =
(variables: SessionState['typebot']['variables']) => (block: InputBlock) => {
return (
Expand Down
39 changes: 25 additions & 14 deletions apps/viewer/src/features/variables/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,31 @@ const jsonParse = (str: string) =>
.replace(/"/g, `\\"`)
.replace(/\\[^n"]/g, `\\\\ `)

export const parseVariablesInObject = (
object: { [key: string]: string | number },
variables: Variable[]
) =>
Object.keys(object).reduce((newObj, key) => {
const currentValue = object[key]
return {
...newObj,
[key]:
typeof currentValue === 'string'
? parseVariables(variables)(currentValue)
: currentValue,
}
}, {})
export const deepParseVariable =
(variables: Variable[]) =>
<T extends Record<string, unknown>>(object: T): T =>
Object.keys(object).reduce<T>((newObj, key) => {
const currentValue = object[key]

if (typeof currentValue === 'string')
return { ...newObj, [key]: parseVariables(variables)(currentValue) }

if (currentValue instanceof Object && currentValue.constructor === Object)
return {
...newObj,
[key]: deepParseVariable(variables)(
currentValue as Record<string, unknown>
),
}

if (currentValue instanceof Array)
return {
...newObj,
[key]: currentValue.map(deepParseVariable(variables)),
}

return { ...newObj, [key]: currentValue }
}, {} as T)

export const parsePrefilledVariables = (
variables: Typebot['variables'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const ChoiceForm = (props: Props) => {
>
{item.content}
</button>
{props.inputIndex === 0 && (
{props.inputIndex === 0 && props.block.items.length === 1 && (
<span class="flex h-3 w-3 absolute top-0 right-0 -mt-1 -mr-1 ping">
<span class="animate-ping absolute inline-flex h-full w-full rounded-full brightness-225 opacity-75" />
<span class="relative inline-flex rounded-full h-3 w-3 brightness-200" />
Expand Down

5 comments on commit 431ad7c

@vercel
Copy link

@vercel vercel bot commented on 431ad7c Dec 24, 2022

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

@vercel
Copy link

@vercel vercel bot commented on 431ad7c Dec 24, 2022

Choose a reason for hiding this comment

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

@vercel
Copy link

@vercel vercel bot commented on 431ad7c Dec 24, 2022

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-alpha – ./apps/viewer

bot.artiweb.app
bot.devitus.com
bot.tc-mail.com
chat.sureb4.com
eventhub.com.au
games.klujo.com
sakuranembro.it
typebot.aloe.do
bot.piccinato.co
bot.sv-energy.it
botc.ceox.com.br
clo.closeer.work
cockroach.cr8.ai
faqs.nigerias.io
form.syncwin.com
kw.wpwakanda.com
pant.maxbot.com.br
positivobra.com.br
survey.digienge.io
this-is-a-test.com
zap.techadviser.in
bot.digitalbled.com
bot.eventhub.com.au
bot.jepierre.com.br
bot.winglabs.com.br
carsalesenquiry.com
chatbot.repplai.com
demo.botscientis.us
forms.webisharp.com
kbsub.wpwakanda.com
live.botscientis.us
mentoria.omelhor.vc
nutrisamirbayde.com
order.maitempah.com
quest.wpwakanda.com
survey1.digienge.io
test.botscientis.us
typebot.stillio.com
wordsandimagery.com
bium.gratirabbit.com
bot.ansuraniphone.my
bot.cotemeuplano.com
bot.leadbooster.help
chat.hayurihijab.com
chatbee.agfunnel.com
click.sevenoways.com
connect.growthguy.in
kuiz.sistemniaga.com
offer.botscientis.us
sellmycarglasgow.com
talkbot.agfunnel.com
tenorioadvogados.com
uppity.wpwakanda.com
abutton.wpwakanda.com
acelera.maxbot.com.br
aidigitalmarketing.kr
bbutton.wpwakanda.com
bot.incusservices.com
bot.meuesocial.com.br
bot.ramonmatos.com.br
bot.cabinrentalagency.com
bot.fusionstarreviews.com
boyfriend-breakup.riku.ai
brigadeirosemdrama.com.br
chat.ertcrebateportal.com
chat.thisiscrushhouse.com
sellmyharleylouisiana.com
verfica.botmachine.com.br
configurator.bouclidom.com
help.atlasoutfittersk9.com
ted.meujalecobrasil.com.br
type.dericsoncalari.com.br
chatbot.berbelanjabiz.trade
designguide.techyscouts.com
presente.empresarias.com.mx
sell.sellthemotorhome.co.uk
anamnese.odontopavani.com.br
austin.channelautomation.com
bot.marketingplusmindset.com
piazzatorre.barrettamario.it
requests.swamprecordsgnv.com
type.cookieacademyonline.com
bot.brigadeirosemdrama.com.br
onboarding.libertydreamcare.ie
type.talitasouzamarques.com.br
agendamento.sergiolimajr.com.br
anamnese.clinicamegasjdr.com.br
bookings.littlepartymonkeys.com
bot.comercializadoraomicron.com
elevateyourmind.groovepages.com
yourfeedback.comebackreward.com
personal-trainer.barrettamario.it
preagendamento.sergiolimajr.com.br
studiotecnicoimmobiliaremerelli.it
download.thailandmicespecialist.com
register.thailandmicespecialist.com
viewer-v2-alpha-typebot-io.vercel.app
pesquisa.escolamodacomproposito.com.br
anamnese.clinicaramosodontologia.com.br
viewer-v2-alpha-git-main-typebot-io.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 431ad7c Dec 24, 2022

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:

docs – ./apps/docs

docs-git-main-typebot-io.vercel.app
docs.typebot.io
docs-typebot-io.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 431ad7c Dec 24, 2022

Choose a reason for hiding this comment

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

Please sign in to comment.