Skip to content

Commit

Permalink
⚡ (wait) Add pause option on Wait block
Browse files Browse the repository at this point in the history
Closes #751
  • Loading branch information
baptisteArno committed Sep 4, 2023
1 parent 66dc570 commit 111fb32
Show file tree
Hide file tree
Showing 16 changed files with 672 additions and 589 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { Stack } from '@chakra-ui/react'
import {
Accordion,
AccordionButton,
AccordionIcon,
AccordionItem,
AccordionPanel,
Stack,
} from '@chakra-ui/react'
import { WaitOptions } from '@typebot.io/schemas'
import React from 'react'
import { TextInput } from '@/components/inputs'
import { SwitchWithLabel } from '@/components/inputs/SwitchWithLabel'

type Props = {
options: WaitOptions
Expand All @@ -13,6 +21,10 @@ export const WaitSettings = ({ options, onOptionsChange }: Props) => {
onOptionsChange({ ...options, secondsToWaitFor })
}

const updateShouldPause = (shouldPause: boolean) => {
onOptionsChange({ ...options, shouldPause })
}

return (
<Stack spacing={4}>
<TextInput
Expand All @@ -21,6 +33,22 @@ export const WaitSettings = ({ options, onOptionsChange }: Props) => {
onChange={handleSecondsChange}
placeholder="0"
/>
<Accordion allowToggle>
<AccordionItem>
<AccordionButton justifyContent="space-between">
Advanced
<AccordionIcon />
</AccordionButton>
<AccordionPanel py="4">
<SwitchWithLabel
label="Pause the flow"
moreInfoContent="When enabled, the flow is paused until the client sends another message. This is automatic on the web bot."
initialValue={options.shouldPause ?? false}
onCheckChange={updateShouldPause}
/>
</AccordionPanel>
</AccordionItem>
</Accordion>
</Stack>
)
}
6 changes: 6 additions & 0 deletions apps/docs/docs/editor/blocks/logic/wait.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ This can be useful if you want the bot to emphasize on what's been said or to wa
:::caution
This should be used wisely. If you want the bot to write slower or faster in a more general sense, you need to check the [Typing emulation settings](/editor/settings#typing-emulation)
:::

## Pause the flow

You can enable the "Pause the flow" option if you ever need to mark a pause in the flow.

Under the hood, typebot always compute all the blocks between each input blocks. But sometimes you may want to display some messages before a long-running action like a slow webhook request.
21 changes: 21 additions & 0 deletions apps/docs/openapi/builder/_spec_.json
Original file line number Diff line number Diff line change
Expand Up @@ -2088,6 +2088,9 @@
"properties": {
"secondsToWaitFor": {
"type": "string"
},
"shouldPause": {
"type": "boolean"
}
},
"additionalProperties": false
Expand Down Expand Up @@ -6381,6 +6384,9 @@
"properties": {
"secondsToWaitFor": {
"type": "string"
},
"shouldPause": {
"type": "boolean"
}
},
"additionalProperties": false
Expand Down Expand Up @@ -10309,6 +10315,9 @@
"properties": {
"secondsToWaitFor": {
"type": "string"
},
"shouldPause": {
"type": "boolean"
}
},
"additionalProperties": false
Expand Down Expand Up @@ -14377,6 +14386,9 @@
"properties": {
"secondsToWaitFor": {
"type": "string"
},
"shouldPause": {
"type": "boolean"
}
},
"additionalProperties": false
Expand Down Expand Up @@ -18325,6 +18337,9 @@
"properties": {
"secondsToWaitFor": {
"type": "string"
},
"shouldPause": {
"type": "boolean"
}
},
"additionalProperties": false
Expand Down Expand Up @@ -22328,6 +22343,9 @@
"properties": {
"secondsToWaitFor": {
"type": "string"
},
"shouldPause": {
"type": "boolean"
}
},
"additionalProperties": false
Expand Down Expand Up @@ -26394,6 +26412,9 @@
"properties": {
"secondsToWaitFor": {
"type": "string"
},
"shouldPause": {
"type": "boolean"
}
},
"additionalProperties": false
Expand Down
6 changes: 6 additions & 0 deletions apps/docs/openapi/chat/_spec_.json
Original file line number Diff line number Diff line change
Expand Up @@ -1671,6 +1671,9 @@
"properties": {
"secondsToWaitFor": {
"type": "string"
},
"shouldPause": {
"type": "boolean"
}
},
"additionalProperties": false
Expand Down Expand Up @@ -5204,6 +5207,9 @@
"properties": {
"lastBubbleBlockId": {
"type": "string"
},
"expectsDedicatedReply": {
"type": "boolean"
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export const createChatCompletionOpenAI = async (
assistantMessageVariableName
),
},
expectsDedicatedReply: true,
},
],
outgoingEdgeId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export const executeWebhookBlock = async (
clientSideActions: [
{
webhookToExecute: parsedWebhook,
expectsDedicatedReply: true,
},
],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const executeSetVariable = (
setVariable: {
scriptToExecute,
},
expectsDedicatedReply: true,
},
],
}
Expand Down
1 change: 1 addition & 0 deletions apps/viewer/src/features/blocks/logic/wait/executeWait.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const executeWait = (
? [
{
wait: { secondsToWaitFor: parsedSecondsToWaitFor },
expectsDedicatedReply: block.options.shouldPause,
},
]
: undefined,
Expand Down
16 changes: 3 additions & 13 deletions apps/viewer/src/features/chat/helpers/continueBotFlow.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { TRPCError } from '@trpc/server'
import {
AnswerInSessionState,
Block,
BubbleBlockType,
ChatReply,
InputBlock,
InputBlockType,
IntegrationBlockType,
LogicBlockType,
SessionState,
SetVariableBlock,
WebhookBlock,
defaultPaymentInputOptions,
invalidEmailDefaultRetryMessage,
} from '@typebot.io/schemas'
Expand All @@ -21,7 +19,6 @@ import { formatPhoneNumber } from '@/features/blocks/inputs/phone/formatPhoneNum
import { validateUrl } from '@/features/blocks/inputs/url/validateUrl'
import { updateVariables } from '@/features/variables/updateVariables'
import { parseVariables } from '@/features/variables/parseVariables'
import { OpenAIBlock } from '@typebot.io/schemas/features/blocks/integrations/openai'
import { resumeChatCompletion } from '@/features/blocks/integrations/openai/resumeChatCompletion'
import { resumeWebhookExecution } from '@/features/blocks/integrations/webhook/resumeWebhookExecution'
import { upsertAnswer } from '../queries/upsertAnswer'
Expand Down Expand Up @@ -80,11 +77,7 @@ export const continueBotFlow =
})(reply)
newSessionState = result.newSessionState
}
} else if (!isInputBlock(block))
throw new TRPCError({
code: 'INTERNAL_SERVER_ERROR',
message: 'Current block is not an input block',
})
}

let formattedReply: string | undefined

Expand Down Expand Up @@ -276,10 +269,7 @@ const setNewAnswerInState =

const getOutgoingEdgeId =
(state: Pick<SessionState, 'typebotsQueue'>) =>
(
block: InputBlock | SetVariableBlock | OpenAIBlock | WebhookBlock,
reply: string | undefined
) => {
(block: Block, reply: string | undefined) => {
const variables = state.typebotsQueue[0].typebot.variables
if (
block.type === InputBlockType.CHOICE &&
Expand Down
5 changes: 1 addition & 4 deletions apps/viewer/src/features/chat/helpers/executeGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,7 @@ export const executeGroup =
]
if (
executionResponse.clientSideActions?.find(
(action) =>
'setVariable' in action ||
'streamOpenAiChatCompletion' in action ||
'webhookToExecute' in action
(action) => action.expectsDedicatedReply
)
) {
return {
Expand Down
5 changes: 1 addition & 4 deletions apps/viewer/src/features/chat/helpers/saveStateToDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ export const saveStateToDatabase = async ({
clientSideActions,
}: Props) => {
const containsSetVariableClientSideAction = clientSideActions?.some(
(action) =>
'setVariable' in action ||
'webhookToExecute' in action ||
'streamOpenAiChatCompletion' in action
(action) => action.expectsDedicatedReply
)

const isCompleted = Boolean(!input && !containsSetVariableClientSideAction)
Expand Down
5 changes: 1 addition & 4 deletions apps/viewer/src/features/chat/helpers/startSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,7 @@ export const startSession = async ({
}

const clientSideActionsNeedSessionId = clientSideActions?.some(
(action) =>
'setVariable' in action ||
'streamOpenAiChatCompletion' in action ||
'webhookToExecute' in action
(action) => action.expectsDedicatedReply
)

if (!input && !clientSideActionsNeedSessionId)
Expand Down
5 changes: 4 additions & 1 deletion packages/embeds/js/src/utils/executeClientSideActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ export const executeClientSideAction = async ({
return executeRedirect(clientSideAction.redirect)
}
if ('wait' in clientSideAction) {
return executeWait(clientSideAction.wait)
await executeWait(clientSideAction.wait)
return clientSideAction.expectsDedicatedReply
? { replyToSend: undefined }
: undefined
}
if ('setVariable' in clientSideAction) {
return executeSetVariable(clientSideAction.setVariable.scriptToExecute)
Expand Down
1 change: 1 addition & 0 deletions packages/schemas/features/blocks/logic/wait.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { LogicBlockType } from './enums'

export const waitOptionsSchema = z.object({
secondsToWaitFor: z.string().optional(),
shouldPause: z.boolean().optional(),
})

export const waitBlockSchema = blockBaseSchema.merge(
Expand Down
1 change: 1 addition & 0 deletions packages/schemas/features/chat/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ const startPropsToInjectSchema = z.object({
const clientSideActionSchema = z
.object({
lastBubbleBlockId: z.string().optional(),
expectsDedicatedReply: z.boolean().optional(),
})
.and(
z
Expand Down
Loading

4 comments on commit 111fb32

@vercel
Copy link

@vercel vercel bot commented on 111fb32 Sep 4, 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

kusamint.com
psmix.online
receita.info
rhino.cr8.ai
sheep.cr8.ai
snake.cr8.ai
nutriandreia.shop
ov1.wpwakanda.com
ov2.wpwakanda.com
ov3.wpwakanda.com
pcb.drapamela.com
softwarelucra.com
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
amancarseat.online
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.samplehunt.com
bot.sinalcerto.com
bot.wphelpchat.com
bots.robomotion.io
cadu.uninta.edu.br
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
leads.gecoelho.com
noticiasnet.online
novoappespiao.site
omarcodosanjos.com
pant.maxbot.com.br
pantherview.cr8.ai
positivobra.com.br
rollingball.cr8.ai
speciallife.com.br
sub.yolozeeeer.com
viewer-v2-typebot-io.vercel.app
mdb.assessoria.girotto.progenbr.com
mdb.assessoria.marinho.progenbr.com
mdb.assessoria.rodrigo.progenbr.com
register.thailandmicespecialist.com
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

@vercel
Copy link

@vercel vercel bot commented on 111fb32 Sep 4, 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

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

@vercel
Copy link

@vercel vercel bot commented on 111fb32 Sep 4, 2023

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 111fb32 Sep 4, 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:

docs – ./apps/docs

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

Please sign in to comment.