2
0

🧑‍💻 (chat) Introduce startChat and continueChat endpoints

Closes #1030
This commit is contained in:
Baptiste Arnaud
2023-11-13 15:27:36 +01:00
parent 63233eb7ee
commit 084588a086
74 changed files with 28426 additions and 645 deletions

View File

@ -4,8 +4,8 @@ import * as Sentry from '@sentry/nextjs'
import { User } from '@typebot.io/prisma'
import { NextApiRequest, NextApiResponse } from 'next'
import { getServerSession } from 'next-auth'
import { mockedUser } from '../mockedUser'
import { env } from '@typebot.io/env'
import { mockedUser } from '@typebot.io/lib/mockedUser'
export const getAuthenticatedUser = async (
req: NextApiRequest,

View File

@ -12,8 +12,6 @@ export const AudioBubbleNode = ({ url }: Props) => {
return isDefined(url) ? (
<audio src={url} controls />
) : (
<Text color={'gray.500'}>
{t('editor.blocks.bubbles.audio.node.clickToEdit.text')}
</Text>
<Text color={'gray.500'}>{t('clickToEdit')}</Text>
)
}

View File

@ -9,10 +9,6 @@ type Props = {
export const EmbedBubbleContent = ({ block }: Props) => {
const { t } = useTranslate()
if (!block.content?.url)
return (
<Text color="gray.500">
{t('editor.blocks.bubbles.embed.node.clickToEdit.text')}
</Text>
)
return <Text color="gray.500">{t('clickToEdit')}</Text>
return <Text>{t('editor.blocks.bubbles.embed.node.show.text')}</Text>
}

View File

@ -11,9 +11,7 @@ export const ImageBubbleContent = ({ block }: Props) => {
const containsVariables =
block.content?.url?.includes('{{') && block.content.url.includes('}}')
return !block.content?.url ? (
<Text color={'gray.500'}>
{t('editor.blocks.bubbles.image.node.clickToEdit.text')}
</Text>
<Text color={'gray.500'}>{t('clickToEdit')}</Text>
) : (
<Box w="full">
<Image

View File

@ -13,11 +13,7 @@ type Props = {
export const VideoBubbleContent = ({ block }: Props) => {
const { t } = useTranslate()
if (!block.content?.url || !block.content.type)
return (
<Text color="gray.500">
{t('editor.blocks.bubbles.video.node.clickToEdit.text')}
</Text>
)
return <Text color="gray.500">{t('clickToEdit')}</Text>
const containsVariables =
block.content?.url?.includes('{{') && block.content.url.includes('}}')
switch (block.content.type) {

View File

@ -32,7 +32,7 @@ test('should be configurable', async ({ page }) => {
await expect(page.getByTestId('selected-item-label').first()).toHaveText(
'My link typebot 2'
)
await page.click('input[placeholder="Select a block"]')
await page.click('input[placeholder="Select a group"]')
await page.click('text=Group #2')
await page.click('text=Preview')

View File

@ -4,7 +4,7 @@ import { useTypebot } from '@/features/editor/providers/TypebotProvider'
import { useGraph } from '@/features/graph/providers/GraphProvider'
import { useToast } from '@/hooks/useToast'
import { Standard } from '@typebot.io/nextjs'
import { ChatReply } from '@typebot.io/schemas'
import { ContinueChatResponse } from '@typebot.io/schemas'
export const WebPreview = () => {
const { typebot } = useTypebot()
@ -13,7 +13,7 @@ export const WebPreview = () => {
const { showToast } = useToast()
const handleNewLogs = (logs: ChatReply['logs']) => {
const handleNewLogs = (logs: ContinueChatResponse['logs']) => {
logs?.forEach((log) => {
showToast({
icon: <WebhookIcon />,
@ -40,8 +40,13 @@ export const WebPreview = () => {
<Standard
key={`web-preview${startPreviewAtGroup ?? ''}`}
typebot={typebot}
startGroupId={startPreviewAtGroup}
startEventId={startPreviewAtEvent}
startFrom={
startPreviewAtGroup
? { type: 'group', groupId: startPreviewAtGroup }
: startPreviewAtEvent
? { type: 'event', eventId: startPreviewAtEvent }
: undefined
}
onNewInputBlock={(block) =>
setPreviewingBlock({
id: block.id,

View File

@ -24,7 +24,7 @@ import { BuoyIcon, ExternalLinkIcon } from '@/components/icons'
export const WhatsAppPreviewInstructions = (props: StackProps) => {
const { typebot, save } = useTypebot()
const { startPreviewAtGroup } = useEditor()
const { startPreviewAtGroup, startPreviewAtEvent } = useEditor()
const [phoneNumber, setPhoneNumber] = useState(
getPhoneNumberFromLocalStorage() ?? ''
)
@ -56,7 +56,11 @@ export const WhatsAppPreviewInstructions = (props: StackProps) => {
mutate({
to: phoneNumber,
typebotId: typebot.id,
startGroupId: startPreviewAtGroup,
startFrom: startPreviewAtGroup
? { type: 'group', groupId: startPreviewAtGroup }
: startPreviewAtEvent
? { type: 'event', eventId: startPreviewAtEvent }
: undefined,
})
}

View File

@ -10,7 +10,7 @@ import { restartSession } from '@typebot.io/bot-engine/queries/restartSession'
import { sendChatReplyToWhatsApp } from '@typebot.io/bot-engine/whatsapp/sendChatReplyToWhatsApp'
import { sendWhatsAppMessage } from '@typebot.io/bot-engine/whatsapp/sendWhatsAppMessage'
import { isReadTypebotForbidden } from '../typebot/helpers/isReadTypebotForbidden'
import { SessionState } from '@typebot.io/schemas'
import { SessionState, startFromSchema } from '@typebot.io/schemas'
export const startWhatsAppPreview = authenticatedProcedure
.meta({
@ -31,7 +31,7 @@ export const startWhatsAppPreview = authenticatedProcedure
value.replace(/\s/g, '').replace(/\+/g, '').replace(/-/g, '')
),
typebotId: z.string(),
startGroupId: z.string().optional(),
startFrom: startFromSchema.optional(),
})
)
.output(
@ -39,135 +39,133 @@ export const startWhatsAppPreview = authenticatedProcedure
message: z.string(),
})
)
.mutation(
async ({ input: { to, typebotId, startGroupId }, ctx: { user } }) => {
if (
!env.WHATSAPP_PREVIEW_FROM_PHONE_NUMBER_ID ||
!env.META_SYSTEM_USER_TOKEN ||
!env.WHATSAPP_PREVIEW_TEMPLATE_NAME
)
throw new TRPCError({
code: 'BAD_REQUEST',
message:
'Missing WHATSAPP_PREVIEW_FROM_PHONE_NUMBER_ID or META_SYSTEM_USER_TOKEN or WHATSAPP_PREVIEW_TEMPLATE_NAME env variables',
})
.mutation(async ({ input: { to, typebotId, startFrom }, ctx: { user } }) => {
if (
!env.WHATSAPP_PREVIEW_FROM_PHONE_NUMBER_ID ||
!env.META_SYSTEM_USER_TOKEN ||
!env.WHATSAPP_PREVIEW_TEMPLATE_NAME
)
throw new TRPCError({
code: 'BAD_REQUEST',
message:
'Missing WHATSAPP_PREVIEW_FROM_PHONE_NUMBER_ID or META_SYSTEM_USER_TOKEN or WHATSAPP_PREVIEW_TEMPLATE_NAME env variables',
})
const existingTypebot = await prisma.typebot.findFirst({
where: {
id: typebotId,
},
select: {
id: true,
workspaceId: true,
collaborators: {
select: {
userId: true,
},
const existingTypebot = await prisma.typebot.findFirst({
where: {
id: typebotId,
},
select: {
id: true,
workspaceId: true,
collaborators: {
select: {
userId: true,
},
},
})
if (
!existingTypebot?.id ||
(await isReadTypebotForbidden(existingTypebot, user))
)
throw new TRPCError({ code: 'NOT_FOUND', message: 'Typebot not found' })
},
})
if (
!existingTypebot?.id ||
(await isReadTypebotForbidden(existingTypebot, user))
)
throw new TRPCError({ code: 'NOT_FOUND', message: 'Typebot not found' })
const sessionId = `wa-preview-${to}`
const sessionId = `wa-preview-${to}`
const existingSession = await prisma.chatSession.findFirst({
where: {
id: sessionId,
},
select: {
updatedAt: true,
state: true,
},
})
const existingSession = await prisma.chatSession.findFirst({
where: {
id: sessionId,
},
select: {
updatedAt: true,
state: true,
},
})
// For users that did not interact with the bot in the last 24 hours, we need to send a template message.
const canSendDirectMessagesToUser =
(existingSession?.updatedAt.getTime() ?? 0) >
Date.now() - 24 * 60 * 60 * 1000
// For users that did not interact with the bot in the last 24 hours, we need to send a template message.
const canSendDirectMessagesToUser =
(existingSession?.updatedAt.getTime() ?? 0) >
Date.now() - 24 * 60 * 60 * 1000
const {
newSessionState,
const {
newSessionState,
messages,
input,
clientSideActions,
logs,
visitedEdges,
} = await startSession({
version: 2,
message: undefined,
startParams: {
isOnlyRegistering: !canSendDirectMessagesToUser,
type: 'preview',
typebotId,
startFrom,
userId: user.id,
},
initialSessionState: {
whatsApp: (existingSession?.state as SessionState | undefined)
?.whatsApp,
},
})
if (canSendDirectMessagesToUser) {
await sendChatReplyToWhatsApp({
to,
typingEmulation: newSessionState.typingEmulation,
messages,
input,
clientSideActions,
logs,
visitedEdges,
} = await startSession({
version: 2,
message: undefined,
startParams: {
isOnlyRegistering: !canSendDirectMessagesToUser,
typebot: typebotId,
isPreview: true,
startGroupId,
},
userId: user.id,
initialSessionState: {
whatsApp: (existingSession?.state as SessionState | undefined)
?.whatsApp,
credentials: {
phoneNumberId: env.WHATSAPP_PREVIEW_FROM_PHONE_NUMBER_ID,
systemUserAccessToken: env.META_SYSTEM_USER_TOKEN,
},
state: newSessionState,
})
if (canSendDirectMessagesToUser) {
await sendChatReplyToWhatsApp({
await saveStateToDatabase({
clientSideActions: [],
input,
logs,
session: {
id: sessionId,
state: newSessionState,
},
visitedEdges,
})
} else {
await restartSession({
state: newSessionState,
id: sessionId,
})
try {
await sendWhatsAppMessage({
to,
typingEmulation: newSessionState.typingEmulation,
messages,
input,
clientSideActions,
message: {
type: 'template',
template: {
language: {
code: env.WHATSAPP_PREVIEW_TEMPLATE_LANG,
},
name: env.WHATSAPP_PREVIEW_TEMPLATE_NAME,
},
},
credentials: {
phoneNumberId: env.WHATSAPP_PREVIEW_FROM_PHONE_NUMBER_ID,
systemUserAccessToken: env.META_SYSTEM_USER_TOKEN,
},
state: newSessionState,
})
await saveStateToDatabase({
clientSideActions: [],
input,
logs,
session: {
id: sessionId,
state: newSessionState,
},
visitedEdges,
} catch (err) {
if (err instanceof HTTPError) console.log(err.response.body)
throw new TRPCError({
code: 'INTERNAL_SERVER_ERROR',
message: 'Request to Meta to send preview message failed',
cause: err,
})
} else {
await restartSession({
state: newSessionState,
id: sessionId,
})
try {
await sendWhatsAppMessage({
to,
message: {
type: 'template',
template: {
language: {
code: env.WHATSAPP_PREVIEW_TEMPLATE_LANG,
},
name: env.WHATSAPP_PREVIEW_TEMPLATE_NAME,
},
},
credentials: {
phoneNumberId: env.WHATSAPP_PREVIEW_FROM_PHONE_NUMBER_ID,
systemUserAccessToken: env.META_SYSTEM_USER_TOKEN,
},
})
} catch (err) {
if (err instanceof HTTPError) console.log(err.response.body)
throw new TRPCError({
code: 'INTERNAL_SERVER_ERROR',
message: 'Request to Meta to send preview message failed',
cause: err,
})
}
}
return {
message: 'success',
}
}
)
return {
message: 'success',
}
})

View File

@ -11,7 +11,7 @@ import { NextApiRequest, NextApiResponse } from 'next'
import { customAdapter } from '../../../features/auth/api/customAdapter'
import { User } from '@typebot.io/prisma'
import { getAtPath, isDefined } from '@typebot.io/lib'
import { mockedUser } from '@/features/auth/mockedUser'
import { mockedUser } from '@typebot.io/lib/mockedUser'
import { getNewUserInvitations } from '@/features/auth/helpers/getNewUserInvitations'
import { sendVerificationRequest } from '@/features/auth/helpers/sendVerificationRequest'
import { Ratelimit } from '@upstash/ratelimit'

View File

@ -624,7 +624,9 @@
"enum": [
"url",
"youtube",
"vimeo"
"vimeo",
"tiktok",
"gumlet"
]
},
"height": {
@ -634,6 +636,12 @@
},
{}
]
},
"aspectRatio": {
"type": "string"
},
"maxWidth": {
"type": "string"
}
},
"additionalProperties": false
@ -3929,7 +3937,9 @@
"enum": [
"url",
"youtube",
"vimeo"
"vimeo",
"tiktok",
"gumlet"
]
},
"height": {
@ -3939,6 +3949,12 @@
},
{}
]
},
"aspectRatio": {
"type": "string"
},
"maxWidth": {
"type": "string"
}
},
"additionalProperties": false
@ -8007,7 +8023,9 @@
"enum": [
"url",
"youtube",
"vimeo"
"vimeo",
"tiktok",
"gumlet"
]
},
"height": {
@ -8017,6 +8035,12 @@
},
{}
]
},
"aspectRatio": {
"type": "string"
},
"maxWidth": {
"type": "string"
}
},
"additionalProperties": false
@ -11708,7 +11732,9 @@
"enum": [
"url",
"youtube",
"vimeo"
"vimeo",
"tiktok",
"gumlet"
]
},
"height": {
@ -11718,6 +11744,12 @@
},
{}
]
},
"aspectRatio": {
"type": "string"
},
"maxWidth": {
"type": "string"
}
},
"additionalProperties": false
@ -15497,7 +15529,9 @@
"enum": [
"url",
"youtube",
"vimeo"
"vimeo",
"tiktok",
"gumlet"
]
},
"height": {
@ -15507,6 +15541,12 @@
},
{}
]
},
"aspectRatio": {
"type": "string"
},
"maxWidth": {
"type": "string"
}
},
"additionalProperties": false
@ -19195,7 +19235,9 @@
"enum": [
"url",
"youtube",
"vimeo"
"vimeo",
"tiktok",
"gumlet"
]
},
"height": {
@ -19205,6 +19247,12 @@
},
{}
]
},
"aspectRatio": {
"type": "string"
},
"maxWidth": {
"type": "string"
}
},
"additionalProperties": false
@ -22918,7 +22966,9 @@
"enum": [
"url",
"youtube",
"vimeo"
"vimeo",
"tiktok",
"gumlet"
]
},
"height": {
@ -22928,6 +22978,12 @@
},
{}
]
},
"aspectRatio": {
"type": "string"
},
"maxWidth": {
"type": "string"
}
},
"additionalProperties": false
@ -26647,7 +26703,9 @@
"enum": [
"url",
"youtube",
"vimeo"
"vimeo",
"tiktok",
"gumlet"
]
},
"height": {
@ -26657,6 +26715,12 @@
},
{}
]
},
"aspectRatio": {
"type": "string"
},
"maxWidth": {
"type": "string"
}
},
"additionalProperties": false
@ -30203,7 +30267,6 @@
"version",
"id",
"name",
"events",
"groups",
"edges",
"variables",
@ -30425,7 +30488,9 @@
"enum": [
"url",
"youtube",
"vimeo"
"vimeo",
"tiktok",
"gumlet"
]
},
"height": {
@ -30435,6 +30500,12 @@
},
{}
]
},
"aspectRatio": {
"type": "string"
},
"maxWidth": {
"type": "string"
}
},
"additionalProperties": false
@ -34213,7 +34284,9 @@
"enum": [
"url",
"youtube",
"vimeo"
"vimeo",
"tiktok",
"gumlet"
]
},
"height": {
@ -34223,6 +34296,12 @@
},
{}
]
},
"aspectRatio": {
"type": "string"
},
"maxWidth": {
"type": "string"
}
},
"additionalProperties": false
@ -37878,7 +37957,9 @@
"enum": [
"url",
"youtube",
"vimeo"
"vimeo",
"tiktok",
"gumlet"
]
},
"height": {
@ -37888,6 +37969,12 @@
},
{}
]
},
"aspectRatio": {
"type": "string"
},
"maxWidth": {
"type": "string"
}
},
"additionalProperties": false
@ -41678,7 +41765,9 @@
"enum": [
"url",
"youtube",
"vimeo"
"vimeo",
"tiktok",
"gumlet"
]
},
"height": {
@ -41688,6 +41777,12 @@
},
{}
]
},
"aspectRatio": {
"type": "string"
},
"maxWidth": {
"type": "string"
}
},
"additionalProperties": false
@ -45166,10 +45261,6 @@
"type": "string",
"nullable": true
},
"publicId": {
"type": "string",
"nullable": true
},
"resultsTablePreferences": {
"type": "object",
"properties": {
@ -45208,15 +45299,13 @@
"required": [
"version",
"name",
"events",
"groups",
"edges",
"variables",
"theme",
"settings",
"icon",
"folderId",
"publicId"
"folderId"
],
"additionalProperties": false
},
@ -45418,7 +45507,9 @@
"enum": [
"url",
"youtube",
"vimeo"
"vimeo",
"tiktok",
"gumlet"
]
},
"height": {
@ -45428,6 +45519,12 @@
},
{}
]
},
"aspectRatio": {
"type": "string"
},
"maxWidth": {
"type": "string"
}
},
"additionalProperties": false
@ -48816,10 +48913,6 @@
"type": "string",
"nullable": true
},
"publicId": {
"type": "string",
"nullable": true
},
"resultsTablePreferences": {
"type": "object",
"properties": {
@ -48865,8 +48958,7 @@
"theme",
"settings",
"icon",
"folderId",
"publicId"
"folderId"
],
"additionalProperties": false
}
@ -49092,7 +49184,9 @@
"enum": [
"url",
"youtube",
"vimeo"
"vimeo",
"tiktok",
"gumlet"
]
},
"height": {
@ -49102,6 +49196,12 @@
},
{}
]
},
"aspectRatio": {
"type": "string"
},
"maxWidth": {
"type": "string"
}
},
"additionalProperties": false
@ -55954,8 +56054,47 @@
"type": "string",
"minLength": 1
},
"startGroupId": {
"type": "string"
"startFrom": {
"anyOf": [
{
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": [
"group"
]
},
"groupId": {
"type": "string"
}
},
"required": [
"type",
"groupId"
],
"additionalProperties": false
},
{
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": [
"event"
]
},
"eventId": {
"type": "string"
}
},
"required": [
"type",
"eventId"
],
"additionalProperties": false
}
]
}
},
"required": [

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,79 @@
import { publicProcedure } from '@/helpers/server/trpc'
import { continueChatResponseSchema } from '@typebot.io/schemas/features/chat/schema'
import { TRPCError } from '@trpc/server'
import { getSession } from '@typebot.io/bot-engine/queries/getSession'
import { saveStateToDatabase } from '@typebot.io/bot-engine/saveStateToDatabase'
import { continueBotFlow } from '@typebot.io/bot-engine/continueBotFlow'
import { parseDynamicTheme } from '@typebot.io/bot-engine/parseDynamicTheme'
import { isDefined } from '@typebot.io/lib/utils'
import { z } from 'zod'
export const continueChat = publicProcedure
.meta({
openapi: {
method: 'POST',
path: '/v1/sessions/{sessionId}/continueChat',
summary: 'Continue chat',
description:
'To initiate a chat, do not provide a `sessionId` nor a `message`.\n\nContinue the conversation by providing the `sessionId` and the `message` that should answer the previous question.\n\nSet the `isPreview` option to `true` to chat with the non-published version of the typebot.',
},
})
.input(
z.object({
message: z.string().optional(),
sessionId: z.string(),
})
)
.output(continueChatResponseSchema)
.mutation(async ({ input: { sessionId, message } }) => {
const session = await getSession(sessionId)
if (!session) {
throw new TRPCError({
code: 'NOT_FOUND',
message: 'Session not found.',
})
}
const isSessionExpired =
session &&
isDefined(session.state.expiryTimeout) &&
session.updatedAt.getTime() + session.state.expiryTimeout < Date.now()
if (isSessionExpired)
throw new TRPCError({
code: 'NOT_FOUND',
message: 'Session expired. You need to start a new session.',
})
const {
messages,
input,
clientSideActions,
newSessionState,
logs,
lastMessageNewFormat,
visitedEdges,
} = await continueBotFlow(message, { version: 2, state: session.state })
if (newSessionState)
await saveStateToDatabase({
session: {
id: session.id,
state: newSessionState,
},
input,
logs,
clientSideActions,
visitedEdges,
})
return {
messages,
input,
clientSideActions,
dynamicTheme: parseDynamicTheme(newSessionState),
logs,
lastMessageNewFormat,
}
})

View File

@ -1,8 +1,8 @@
import { publicProcedure } from '@/helpers/server/trpc'
import {
chatReplySchema,
sendMessageInputSchema,
} from '@typebot.io/schemas/features/chat/schema'
chatReplySchema,
} from '@typebot.io/schemas/features/chat/legacy/schema'
import { TRPCError } from '@trpc/server'
import { getSession } from '@typebot.io/bot-engine/queries/getSession'
import { startSession } from '@typebot.io/bot-engine/startSession'
@ -16,10 +16,12 @@ export const sendMessageV1 = publicProcedure
.meta({
openapi: {
method: 'POST',
path: '/sendMessage',
path: '/v1/sendMessage',
summary: 'Send a message',
description:
'To initiate a chat, do not provide a `sessionId` nor a `message`.\n\nContinue the conversation by providing the `sessionId` and the `message` that should answer the previous question.\n\nSet the `isPreview` option to `true` to chat with the non-published version of the typebot.',
tags: ['Deprecated'],
deprecated: true,
},
})
.input(sendMessageInputSchema)
@ -60,8 +62,45 @@ export const sendMessageV1 = publicProcedure
visitedEdges,
} = await startSession({
version: 1,
startParams,
userId: user?.id,
startParams:
startParams.isPreview || typeof startParams.typebot !== 'string'
? {
type: 'preview',
isOnlyRegistering: startParams.isOnlyRegistering ?? false,
isStreamEnabled: startParams.isStreamEnabled,
startFrom:
'startGroupId' in startParams && startParams.startGroupId
? {
type: 'group',
groupId: startParams.startGroupId,
}
: 'startEventId' in startParams &&
startParams.startEventId
? {
type: 'event',
eventId: startParams.startEventId,
}
: undefined,
typebotId:
typeof startParams.typebot === 'string'
? startParams.typebot
: startParams.typebot.id,
typebot:
typeof startParams.typebot === 'string'
? undefined
: startParams.typebot,
message,
userId: parseUserId(user?.id),
}
: {
type: 'live',
isOnlyRegistering: startParams.isOnlyRegistering ?? false,
isStreamEnabled: startParams.isStreamEnabled,
publicId: startParams.typebot,
prefilledVariables: startParams.prefilledVariables,
resultId: startParams.resultId,
message,
},
message,
})
@ -133,3 +172,13 @@ export const sendMessageV1 = publicProcedure
}
}
)
const parseUserId = (userId?: string): string => {
if (!userId)
throw new TRPCError({
code: 'UNAUTHORIZED',
message: 'You need to be authenticated to perform this action',
})
return userId
}

View File

@ -1,8 +1,4 @@
import { publicProcedure } from '@/helpers/server/trpc'
import {
chatReplySchema,
sendMessageInputSchema,
} from '@typebot.io/schemas/features/chat/schema'
import { TRPCError } from '@trpc/server'
import { getSession } from '@typebot.io/bot-engine/queries/getSession'
import { startSession } from '@typebot.io/bot-engine/startSession'
@ -11,15 +7,21 @@ import { restartSession } from '@typebot.io/bot-engine/queries/restartSession'
import { continueBotFlow } from '@typebot.io/bot-engine/continueBotFlow'
import { parseDynamicTheme } from '@typebot.io/bot-engine/parseDynamicTheme'
import { isDefined } from '@typebot.io/lib/utils'
import {
chatReplySchema,
sendMessageInputSchema,
} from '@typebot.io/schemas/features/chat/legacy/schema'
export const sendMessageV2 = publicProcedure
.meta({
openapi: {
method: 'POST',
path: '/sendMessage',
path: '/v2/sendMessage',
summary: 'Send a message',
description:
'To initiate a chat, do not provide a `sessionId` nor a `message`.\n\nContinue the conversation by providing the `sessionId` and the `message` that should answer the previous question.\n\nSet the `isPreview` option to `true` to chat with the non-published version of the typebot.',
tags: ['Deprecated'],
deprecated: true,
},
})
.input(sendMessageInputSchema)
@ -60,8 +62,45 @@ export const sendMessageV2 = publicProcedure
visitedEdges,
} = await startSession({
version: 2,
startParams,
userId: user?.id,
startParams:
startParams.isPreview || typeof startParams.typebot !== 'string'
? {
type: 'preview',
isOnlyRegistering: startParams.isOnlyRegistering ?? false,
isStreamEnabled: startParams.isStreamEnabled,
startFrom:
'startGroupId' in startParams && startParams.startGroupId
? {
type: 'group',
groupId: startParams.startGroupId,
}
: 'startEventId' in startParams &&
startParams.startEventId
? {
type: 'event',
eventId: startParams.startEventId,
}
: undefined,
typebotId:
typeof startParams.typebot === 'string'
? startParams.typebot
: startParams.typebot.id,
typebot:
typeof startParams.typebot === 'string'
? undefined
: startParams.typebot,
message,
userId: parseUserId(user?.id),
}
: {
type: 'live',
isOnlyRegistering: startParams.isOnlyRegistering ?? false,
isStreamEnabled: startParams.isStreamEnabled,
publicId: startParams.typebot,
prefilledVariables: startParams.prefilledVariables,
resultId: startParams.resultId,
message,
},
message,
})
@ -133,3 +172,13 @@ export const sendMessageV2 = publicProcedure
}
}
)
const parseUserId = (userId?: string): string => {
if (!userId)
throw new TRPCError({
code: 'UNAUTHORIZED',
message: 'You need to be authenticated to perform this action',
})
return userId
}

View File

@ -0,0 +1,63 @@
import { publicProcedure } from '@/helpers/server/trpc'
import { chatLogSchema } from '@typebot.io/schemas/features/chat/schema'
import { TRPCError } from '@trpc/server'
import { getSession } from '@typebot.io/bot-engine/queries/getSession'
import { z } from 'zod'
import { saveLogs } from '@typebot.io/bot-engine/queries/saveLogs'
import { formatLogDetails } from '@typebot.io/bot-engine/logs/helpers/formatLogDetails'
import * as Sentry from '@sentry/nextjs'
export const saveClientLogs = publicProcedure
.meta({
openapi: {
method: 'POST',
path: '/v1/sessions/{sessionId}/clientLogs',
summary: 'Save client logs',
},
})
.input(
z.object({
sessionId: z.string(),
clientLogs: z.array(chatLogSchema),
})
)
.output(z.object({ message: z.string() }))
.mutation(async ({ input: { sessionId, clientLogs } }) => {
const session = await getSession(sessionId)
if (!session) {
throw new TRPCError({
code: 'NOT_FOUND',
message: 'Session not found.',
})
}
const resultId = session.state.typebotsQueue[0].resultId
if (!resultId) {
throw new TRPCError({
code: 'NOT_FOUND',
message: 'Result not found.',
})
}
try {
await saveLogs(
clientLogs.map((log) => ({
...log,
resultId,
details: formatLogDetails(log.details),
}))
)
return {
message: 'Logs successfully saved.',
}
} catch (e) {
console.error('Failed to save logs', e)
Sentry.captureException(e)
throw new TRPCError({
code: 'INTERNAL_SERVER_ERROR',
message: 'Failed to save logs.',
})
}
})

View File

@ -0,0 +1,83 @@
import { publicProcedure } from '@/helpers/server/trpc'
import {
startChatInputSchema,
startChatResponseSchema,
} from '@typebot.io/schemas/features/chat/schema'
import { startSession } from '@typebot.io/bot-engine/startSession'
import { saveStateToDatabase } from '@typebot.io/bot-engine/saveStateToDatabase'
import { restartSession } from '@typebot.io/bot-engine/queries/restartSession'
export const startChat = publicProcedure
.meta({
openapi: {
method: 'POST',
path: '/v1/typebots/{publicId}/startChat',
summary: 'Start chat',
},
})
.input(startChatInputSchema)
.output(startChatResponseSchema)
.mutation(
async ({
input: {
message,
isOnlyRegistering,
publicId,
isStreamEnabled,
prefilledVariables,
resultId: startResultId,
},
}) => {
const {
typebot,
messages,
input,
resultId,
dynamicTheme,
logs,
clientSideActions,
newSessionState,
visitedEdges,
} = await startSession({
version: 2,
startParams: {
type: 'live',
isOnlyRegistering,
isStreamEnabled,
publicId,
prefilledVariables,
resultId: startResultId,
},
message,
})
const session = isOnlyRegistering
? await restartSession({
state: newSessionState,
})
: await saveStateToDatabase({
session: {
state: newSessionState,
},
input,
logs,
clientSideActions,
visitedEdges,
})
return {
sessionId: session.id,
typebot: {
id: typebot.id,
theme: typebot.theme,
settings: typebot.settings,
},
messages,
input,
resultId,
dynamicTheme,
logs,
clientSideActions,
}
}
)

View File

@ -0,0 +1,83 @@
import { authenticatedProcedure } from '@/helpers/server/trpc'
import {
startPreviewChatInputSchema,
startPreviewChatResponseSchema,
} from '@typebot.io/schemas/features/chat/schema'
import { startSession } from '@typebot.io/bot-engine/startSession'
import { saveStateToDatabase } from '@typebot.io/bot-engine/saveStateToDatabase'
import { restartSession } from '@typebot.io/bot-engine/queries/restartSession'
export const startChatPreview = authenticatedProcedure
.meta({
openapi: {
method: 'POST',
path: '/v1/typebots/{typebotId}/preview/startChat',
summary: 'Start preview chat',
},
})
.input(startPreviewChatInputSchema)
.output(startPreviewChatResponseSchema)
.mutation(
async ({
input: {
message,
isOnlyRegistering,
isStreamEnabled,
startFrom,
typebotId,
typebot: startTypebot,
},
ctx: { user },
}) => {
const {
typebot,
messages,
input,
dynamicTheme,
logs,
clientSideActions,
newSessionState,
visitedEdges,
} = await startSession({
version: 2,
startParams: {
type: 'preview',
isOnlyRegistering,
isStreamEnabled,
startFrom,
typebotId,
typebot: startTypebot,
userId: user.id,
},
message,
})
const session = isOnlyRegistering
? await restartSession({
state: newSessionState,
})
: await saveStateToDatabase({
session: {
state: newSessionState,
},
input,
logs,
clientSideActions,
visitedEdges,
})
return {
sessionId: session.id,
typebot: {
id: typebot.id,
theme: typebot.theme,
settings: typebot.settings,
},
messages,
input,
dynamicTheme,
logs,
clientSideActions,
}
}
)

View File

@ -1,4 +1,3 @@
import { publicProcedure } from '@/helpers/server/trpc'
import { TRPCError } from '@trpc/server'
import { z } from 'zod'
import { getSession } from '@typebot.io/bot-engine/queries/getSession'
@ -9,12 +8,13 @@ import {
Variable,
} from '@typebot.io/schemas'
import prisma from '@typebot.io/lib/prisma'
import { authenticatedProcedure } from '@/helpers/server/trpc'
export const updateTypebotInSession = publicProcedure
export const updateTypebotInSession = authenticatedProcedure
.meta({
openapi: {
method: 'POST',
path: '/sessions/{sessionId}/updateTypebot',
path: '/v1/sessions/{sessionId}/updateTypebot',
summary: 'Update typebot in session',
description:
'Update chat session with latest typebot modifications. This is useful when you want to update the typebot in an ongoing session after making changes to it.',
@ -28,8 +28,6 @@ export const updateTypebotInSession = publicProcedure
)
.output(z.object({ message: z.literal('success') }))
.mutation(async ({ input: { sessionId }, ctx: { user } }) => {
if (!user)
throw new TRPCError({ code: 'UNAUTHORIZED', message: 'Unauthorized' })
const session = await getSession(sessionId)
if (!session)
throw new TRPCError({ code: 'NOT_FOUND', message: 'Session not found' })

View File

@ -19,10 +19,11 @@ export const getUploadUrl = publicProcedure
.meta({
openapi: {
method: 'GET',
path: '/typebots/{typebotId}/blocks/{blockId}/storage/upload-url',
path: '/v1/typebots/{typebotId}/blocks/{blockId}/storage/upload-url',
summary: 'Get upload URL for a file',
description: 'Used for the web client to get the bucket upload file.',
deprecated: true,
tags: ['Deprecated'],
},
})
.input(

View File

@ -13,7 +13,7 @@ export const generateUploadUrl = publicProcedure
.meta({
openapi: {
method: 'POST',
path: '/generate-upload-url',
path: '/v1/generate-upload-url',
summary: 'Generate upload URL',
description: 'Used to upload anything from the client to S3 bucket',
},

View File

@ -8,7 +8,7 @@ export const receiveMessage = publicProcedure
.meta({
openapi: {
method: 'POST',
path: '/workspaces/{workspaceId}/whatsapp/{credentialsId}/webhook',
path: '/v1/workspaces/{workspaceId}/whatsapp/{credentialsId}/webhook',
summary: 'Message webhook',
tags: ['WhatsApp'],
},

View File

@ -7,7 +7,7 @@ export const subscribeWebhook = publicProcedure
.meta({
openapi: {
method: 'GET',
path: '/workspaces/{workspaceId}/whatsapp/{credentialsId}/webhook',
path: '/v1/workspaces/{workspaceId}/whatsapp/{credentialsId}/webhook',
summary: 'Subscribe webhook',
tags: ['WhatsApp'],
protect: true,

View File

@ -0,0 +1,26 @@
import { sendMessageV1 } from '@/features/chat/api/legacy/sendMessageV1'
import { whatsAppRouter } from '@/features/whatsapp/api/router'
import { router } from './trpc'
import { updateTypebotInSession } from '@/features/chat/api/updateTypebotInSession'
import { getUploadUrl } from '@/features/fileUpload/api/deprecated/getUploadUrl'
import { generateUploadUrl } from '@/features/fileUpload/api/generateUploadUrl'
import { sendMessageV2 } from '@/features/chat/api/legacy/sendMessageV2'
import { continueChat } from '@/features/chat/api/continueChat'
import { saveClientLogs } from '@/features/chat/api/saveClientLogs'
import { startChat } from '@/features/chat/api/startChat'
import { startChatPreview } from '@/features/chat/api/startChatPreview'
export const appRouter = router({
sendMessageV1,
sendMessageV2,
startChat,
continueChat,
startChatPreview: startChatPreview,
getUploadUrl,
generateUploadUrl,
updateTypebotInSession,
whatsAppRouter,
saveClientLogs,
})
export type AppRouter = typeof appRouter

View File

@ -3,6 +3,8 @@ import { inferAsyncReturnType } from '@trpc/server'
import * as trpcNext from '@trpc/server/adapters/next'
import { User } from '@typebot.io/prisma'
import { NextApiRequest } from 'next'
import { mockedUser } from '@typebot.io/lib/mockedUser'
import { env } from '@typebot.io/env'
export async function createContext(opts: trpcNext.CreateNextContextOptions) {
const user = await getAuthenticatedUser(opts.req)
@ -15,6 +17,7 @@ export async function createContext(opts: trpcNext.CreateNextContextOptions) {
const getAuthenticatedUser = async (
req: NextApiRequest
): Promise<User | undefined> => {
if (env.NEXT_PUBLIC_E2E_TEST) return mockedUser
const bearerToken = extractBearerToken(req)
if (!bearerToken) return
return authenticateByToken(bearerToken)

View File

@ -1,11 +1,11 @@
import { generateOpenApiDocument } from 'trpc-openapi'
import { writeFileSync } from 'fs'
import { appRouter } from './routers/appRouterV2'
import { appRouter } from './appRouter'
const openApiDocument = generateOpenApiDocument(appRouter, {
title: 'Chat API',
version: '2.0.0',
baseUrl: 'https://typebot.io/api/v2',
version: '3.0.0',
baseUrl: 'https://typebot.io/api',
docsUrl: 'https://docs.typebot.io/api',
})

View File

@ -1,16 +0,0 @@
import { sendMessageV1 } from '@/features/chat/api/sendMessageV1'
import { whatsAppRouter } from '@/features/whatsapp/api/router'
import { router } from '../trpc'
import { updateTypebotInSession } from '@/features/chat/api/updateTypebotInSession'
import { getUploadUrl } from '@/features/fileUpload/api/deprecated/getUploadUrl'
import { generateUploadUrl } from '@/features/fileUpload/api/generateUploadUrl'
export const appRouter = router({
sendMessageV1,
getUploadUrl,
generateUploadUrl,
updateTypebotInSession,
whatsAppRouter,
})
export type AppRouter = typeof appRouter

View File

@ -1,16 +0,0 @@
import { sendMessageV2 } from '@/features/chat/api/sendMessageV2'
import { whatsAppRouter } from '@/features/whatsapp/api/router'
import { router } from '../trpc'
import { updateTypebotInSession } from '@/features/chat/api/updateTypebotInSession'
import { getUploadUrl } from '@/features/fileUpload/api/deprecated/getUploadUrl'
import { generateUploadUrl } from '@/features/fileUpload/api/generateUploadUrl'
export const appRouter = router({
sendMessageV2,
getUploadUrl,
generateUploadUrl,
updateTypebotInSession,
whatsAppRouter,
})
export type AppRouter = typeof appRouter

View File

@ -1,4 +1,4 @@
import { initTRPC } from '@trpc/server'
import { TRPCError, initTRPC } from '@trpc/server'
import { OpenApiMeta } from 'trpc-openapi'
import superjson from 'superjson'
import { Context } from './context'
@ -8,13 +8,23 @@ const t = initTRPC.context<Context>().meta<OpenApiMeta>().create({
transformer: superjson,
})
export const router = t.router
const sentryMiddleware = t.middleware(
Sentry.Handlers.trpcMiddleware({
attachRpcInput: true,
})
)
const injectUser = t.middleware(({ next, ctx }) => {
export const publicProcedure = t.procedure.use(sentryMiddleware)
const isAuthed = t.middleware(({ next, ctx }) => {
if (!ctx.user?.id) {
throw new TRPCError({
code: 'UNAUTHORIZED',
message: 'You need to be authenticated to perform this action',
})
}
return next({
ctx: {
user: ctx.user,
@ -22,10 +32,6 @@ const injectUser = t.middleware(({ next, ctx }) => {
})
})
const finalMiddleware = sentryMiddleware.unstable_pipe(injectUser)
export const middleware = t.middleware
export const router = t.router
export const publicProcedure = t.procedure.use(finalMiddleware)
export const authenticatedProcedure = t.procedure.use(
sentryMiddleware.unstable_pipe(isAuthed)
)

View File

@ -1,4 +1,4 @@
import { appRouter } from '@/helpers/server/routers/appRouterV1'
import { appRouter } from '@/helpers/server/appRouter'
import * as Sentry from '@sentry/nextjs'
import { createOpenApiNextHandler } from 'trpc-openapi'
import cors from 'nextjs-cors'

View File

@ -1,23 +0,0 @@
import { appRouter } from '@/helpers/server/routers/appRouterV2'
import * as Sentry from '@sentry/nextjs'
import { createOpenApiNextHandler } from 'trpc-openapi'
import cors from 'nextjs-cors'
import { NextApiRequest, NextApiResponse } from 'next'
import { createContext } from '@/helpers/server/context'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
await cors(req, res)
return createOpenApiNextHandler({
router: appRouter,
createContext,
onError({ error }) {
if (error.code === 'INTERNAL_SERVER_ERROR') {
Sentry.captureException(error)
console.error('Something went wrong', error)
}
},
})(req, res)
}
export default handler

View File

@ -2,7 +2,6 @@ import { getTestAsset } from '@/test/utils/playwright'
import test, { expect } from '@playwright/test'
import { createId } from '@paralleldrive/cuid2'
import prisma from '@typebot.io/lib/prisma'
import { SendMessageInput } from '@typebot.io/schemas'
import {
createWebhook,
deleteTypebots,
@ -10,6 +9,7 @@ import {
importTypebotInDatabase,
} from '@typebot.io/lib/playwright/databaseActions'
import { HttpMethod } from '@typebot.io/schemas/features/blocks/integrations/webhook/constants'
import { StartChatInput, StartPreviewChatInput } from '@typebot.io/schemas'
test.afterEach(async () => {
await deleteWebhooks(['chat-webhook-id'])
@ -40,17 +40,18 @@ test('API chat execution should work on preview bot', async ({ request }) => {
url: 'https://api.chucknorris.io/jokes/random',
})
await test.step('Start the chat', async () => {
let chatSessionId: string
await test.step('Can start and continue chat', async () => {
const { sessionId, messages, input, resultId } = await (
await request.post(`/api/v2/sendMessage`, {
await request.post(`/api/v1/typebots/${typebotId}/preview/startChat`, {
data: {
startParams: {
typebot: typebotId,
isPreview: true,
},
} satisfies SendMessageInput,
isOnlyRegistering: false,
isStreamEnabled: false,
} satisfies Omit<StartPreviewChatInput, 'typebotId'>,
})
).json()
chatSessionId = sessionId
expect(resultId).toBeUndefined()
expect(sessionId).toBeDefined()
expect(messages[0].content.richText).toStrictEqual([
@ -61,6 +62,38 @@ test('API chat execution should work on preview bot', async ({ request }) => {
])
expect(input.type).toBe('text input')
})
await test.step('Can answer Name question', async () => {
const { messages, input } = await (
await request.post(`/api/v1/sessions/${chatSessionId}/continueChat`, {
data: {
message: 'John',
},
})
).json()
expect(messages[0].content.richText).toStrictEqual([
{
children: [
{ text: 'Nice to meet you ' },
{
type: 'inline-variable',
children: [
{
type: 'p',
children: [
{
text: 'John',
},
],
},
],
},
],
type: 'p',
},
])
expect(input.type).toBe('number input')
})
})
test('API chat execution should work on published bot', async ({ request }) => {
@ -83,12 +116,11 @@ test('API chat execution should work on published bot', async ({ request }) => {
await test.step('Start the chat', async () => {
const { sessionId, messages, input, resultId } = await (
await request.post(`/api/v2/sendMessage`, {
await request.post(`/api/v1/typebots/${publicId}/startChat`, {
data: {
startParams: {
typebot: publicId,
},
} satisfies SendMessageInput,
isOnlyRegistering: false,
isStreamEnabled: false,
} satisfies Omit<StartChatInput, 'publicId'>,
})
).json()
chatSessionId = sessionId
@ -111,8 +143,8 @@ test('API chat execution should work on published bot', async ({ request }) => {
await test.step('Answer Name question', async () => {
const { messages, input } = await (
await request.post(`/api/v2/sendMessage`, {
data: { message: 'John', sessionId: chatSessionId },
await request.post(`/api/v1/sessions/${chatSessionId}/continueChat`, {
data: { message: 'John' },
})
).json()
expect(messages[0].content.richText).toStrictEqual([
@ -142,8 +174,8 @@ test('API chat execution should work on published bot', async ({ request }) => {
await test.step('Answer Age question', async () => {
const { messages, input } = await (
await request.post(`/api/v2/sendMessage`, {
data: { message: '24', sessionId: chatSessionId },
await request.post(`/api/v1/sessions/${chatSessionId}/continueChat`, {
data: { message: '24' },
})
).json()
expect(messages[0].content.richText).toStrictEqual([
@ -181,8 +213,8 @@ test('API chat execution should work on published bot', async ({ request }) => {
await test.step('Answer Rating question', async () => {
const { messages, input } = await (
await request.post(`/api/v2/sendMessage`, {
data: { message: '8', sessionId: chatSessionId },
await request.post(`/api/v1/sessions/${chatSessionId}/continueChat`, {
data: { message: '8' },
})
).json()
expect(messages[0].content.richText).toStrictEqual([
@ -196,8 +228,8 @@ test('API chat execution should work on published bot', async ({ request }) => {
await test.step('Answer Email question with wrong input', async () => {
const { messages, input } = await (
await request.post(`/api/v2/sendMessage`, {
data: { message: 'invalid email', sessionId: chatSessionId },
await request.post(`/api/v1/sessions/${chatSessionId}/continueChat`, {
data: { message: 'invalid email' },
})
).json()
expect(messages[0].content.richText).toStrictEqual([
@ -215,8 +247,8 @@ test('API chat execution should work on published bot', async ({ request }) => {
await test.step('Answer Email question with valid input', async () => {
const { messages, input } = await (
await request.post(`/api/v2/sendMessage`, {
data: { message: 'typebot@email.com', sessionId: chatSessionId },
await request.post(`/api/v1/sessions/${chatSessionId}/continueChat`, {
data: { message: 'typebot@email.com' },
})
).json()
expect(messages.length).toBe(0)
@ -225,8 +257,8 @@ test('API chat execution should work on published bot', async ({ request }) => {
await test.step('Answer URL question', async () => {
const { messages, input } = await (
await request.post(`/api/v2/sendMessage`, {
data: { message: 'https://typebot.io', sessionId: chatSessionId },
await request.post(`/api/v1/sessions/${chatSessionId}/continueChat`, {
data: { message: 'https://typebot.io' },
})
).json()
expect(messages.length).toBe(0)
@ -235,8 +267,8 @@ test('API chat execution should work on published bot', async ({ request }) => {
await test.step('Answer Buttons question with invalid choice', async () => {
const { messages } = await (
await request.post(`/api/v2/sendMessage`, {
data: { message: 'Yes', sessionId: chatSessionId },
await request.post(`/api/v1/sessions/${chatSessionId}/continueChat`, {
data: { message: 'Yes' },
})
).json()
expect(messages[0].content.richText).toStrictEqual([
@ -263,14 +295,14 @@ test('API chat execution should work on published bot', async ({ request }) => {
})
await test.step('Starting with a message when typebot starts with input should proceed', async () => {
const { messages } = await (
await request.post(`/api/v2/sendMessage`, {
data: {
message: 'Hey',
startParams: {
typebot: 'starting-with-input-public',
},
} satisfies SendMessageInput,
})
await request.post(
`/api/v1/typebots/starting-with-input-public/startChat`,
{
data: {
message: 'Hey',
} satisfies Omit<StartChatInput, 'publicId'>,
}
)
).json()
expect(messages[0].content.richText).toStrictEqual([
{

View File

@ -30,7 +30,7 @@ test('Result should be overwritten on page refresh', async ({ page }) => {
const [, response] = await Promise.all([
page.goto(`/${typebotId}-public`),
page.waitForResponse(/sendMessage/),
page.waitForResponse(/startChat/),
])
const { resultId } = await response.json()
expect(resultId).toBeDefined()
@ -38,7 +38,7 @@ test('Result should be overwritten on page refresh', async ({ page }) => {
const [, secondResponse] = await Promise.all([
page.reload(),
page.waitForResponse(/sendMessage/),
page.waitForResponse(/startChat/),
])
const { resultId: secondResultId } = await secondResponse.json()
expect(secondResultId).toBe(resultId)
@ -57,7 +57,7 @@ test.describe('Create result on page refresh enabled', () => {
])
const [, response] = await Promise.all([
page.goto(`/${typebotId}-public`),
page.waitForResponse(/sendMessage/),
page.waitForResponse(/startChat/),
])
const { resultId } = await response.json()
expect(resultId).toBeDefined()
@ -65,7 +65,7 @@ test.describe('Create result on page refresh enabled', () => {
await expect(page.getByRole('textbox')).toBeVisible()
const [, secondResponse] = await Promise.all([
page.reload(),
page.waitForResponse(/sendMessage/),
page.waitForResponse(/startChat/),
])
const { resultId: secondResultId } = await secondResponse.json()
expect(secondResultId).not.toBe(resultId)

View File

@ -1,11 +1,11 @@
import { generateOpenApiDocument } from 'trpc-openapi'
import { writeFileSync } from 'fs'
import { appRouter } from '@/helpers/server/routers/appRouterV2'
import { appRouter } from '@/helpers/server/appRouter'
const openApiDocument = generateOpenApiDocument(appRouter, {
title: 'Chat API',
version: '2.0.0',
baseUrl: 'https://typebot.io/api/v2',
version: '3.0.0',
baseUrl: 'https://typebot.io/api',
docsUrl: 'https://docs.typebot.io/api',
})

View File

@ -2,7 +2,7 @@ import {
SessionState,
GoogleSheetsGetOptions,
VariableWithValue,
ReplyLog,
ChatLog,
} from '@typebot.io/schemas'
import { isNotEmpty, byId, isDefined } from '@typebot.io/lib'
import { getAuthenticatedGoogleDoc } from './helpers/getAuthenticatedGoogleDoc'
@ -18,7 +18,7 @@ export const getRow = async (
options,
}: { outgoingEdgeId?: string; options: GoogleSheetsGetOptions }
): Promise<ExecuteIntegrationResponse> => {
const logs: ReplyLog[] = []
const logs: ChatLog[] = []
const { variables } = state.typebotsQueue[0].typebot
const { sheetId, cellsToExtract, filter, ...parsedOptions } =
deepParseVariables(variables)(options)

View File

@ -1,7 +1,7 @@
import {
SessionState,
GoogleSheetsInsertRowOptions,
ReplyLog,
ChatLog,
} from '@typebot.io/schemas'
import { parseCellValues } from './helpers/parseCellValues'
import { getAuthenticatedGoogleDoc } from './helpers/getAuthenticatedGoogleDoc'
@ -17,7 +17,7 @@ export const insertRow = async (
const { variables } = state.typebotsQueue[0].typebot
if (!options.cellsToInsert || !options.sheetId) return { outgoingEdgeId }
const logs: ReplyLog[] = []
const logs: ChatLog[] = []
const doc = await getAuthenticatedGoogleDoc({
credentialsId: options.credentialsId,

View File

@ -1,7 +1,7 @@
import {
SessionState,
GoogleSheetsUpdateRowOptions,
ReplyLog,
ChatLog,
} from '@typebot.io/schemas'
import { parseCellValues } from './helpers/parseCellValues'
import { getAuthenticatedGoogleDoc } from './helpers/getAuthenticatedGoogleDoc'
@ -28,7 +28,7 @@ export const updateRow = async (
if (!options.cellsToUpsert || !sheetId || (!referenceCell && !filter))
return { outgoingEdgeId }
const logs: ReplyLog[] = []
const logs: ChatLog[] = []
const doc = await getAuthenticatedGoogleDoc({
credentialsId: options.credentialsId,

View File

@ -1,5 +1,5 @@
import { isNotEmpty } from '@typebot.io/lib/utils'
import { ChatReply } from '@typebot.io/schemas'
import { ContinueChatResponse } from '@typebot.io/schemas'
import { OpenAIBlock } from '@typebot.io/schemas/features/blocks/integrations/openai'
import { HTTPError } from 'got'
import { ClientOptions, OpenAI } from 'openai'
@ -10,7 +10,7 @@ type Props = Pick<
> & {
apiKey: string
temperature: number | undefined
currentLogs?: ChatReply['logs']
currentLogs?: ContinueChatResponse['logs']
isRetrying?: boolean
} & Pick<NonNullable<OpenAIBlock['options']>, 'apiVersion' | 'baseUrl'>
@ -25,9 +25,9 @@ export const executeChatCompletionOpenAIRequest = async ({
currentLogs = [],
}: Props): Promise<{
chatCompletion?: OpenAI.Chat.Completions.ChatCompletion
logs?: ChatReply['logs']
logs?: ContinueChatResponse['logs']
}> => {
const logs: ChatReply['logs'] = currentLogs
const logs: ContinueChatResponse['logs'] = currentLogs
if (messages.length === 0) return { logs }
try {
const config = {

View File

@ -1,5 +1,5 @@
import { byId, isDefined } from '@typebot.io/lib'
import { ChatReply, SessionState } from '@typebot.io/schemas'
import { ContinueChatResponse, SessionState } from '@typebot.io/schemas'
import { ChatCompletionOpenAIOptions } from '@typebot.io/schemas/features/blocks/integrations/openai'
import { VariableWithUnknowValue } from '@typebot.io/schemas/features/typebot/variable'
import { updateVariablesInSession } from '../../../variables/updateVariablesInSession'
@ -14,7 +14,7 @@ export const resumeChatCompletion =
}: {
outgoingEdgeId?: string
options: ChatCompletionOpenAIOptions
logs?: ChatReply['logs']
logs?: ContinueChatResponse['logs']
}
) =>
async (message: string, totalTokens?: number) => {

View File

@ -1,7 +1,7 @@
import { DefaultBotNotificationEmail, render } from '@typebot.io/emails'
import {
AnswerInSessionState,
ReplyLog,
ChatLog,
SendEmailBlock,
SessionState,
SmtpCredentials,
@ -25,7 +25,7 @@ export const executeSendEmailBlock = async (
state: SessionState,
block: SendEmailBlock
): Promise<ExecuteIntegrationResponse> => {
const logs: ReplyLog[] = []
const logs: ChatLog[] = []
const { options } = block
const { typebot, resultId, answers } = state.typebotsQueue[0]
const isPreview = !resultId
@ -114,8 +114,8 @@ const sendEmail = async ({
typebot: TypebotInSession
answers: AnswerInSessionState[]
fileUrls?: string | string[]
}): Promise<ReplyLog[] | undefined> => {
const logs: ReplyLog[] = []
}): Promise<ChatLog[] | undefined> => {
const logs: ChatLog[] = []
const { name: replyToName } = parseEmailRecipient(replyTo)
const { host, port, isTlsEnabled, username, password, from } =

View File

@ -8,7 +8,7 @@ import {
Variable,
WebhookResponse,
KeyValue,
ReplyLog,
ChatLog,
ExecutableWebhook,
AnswerInSessionState,
} from '@typebot.io/schemas'
@ -34,7 +34,7 @@ export const executeWebhookBlock = async (
state: SessionState,
block: WebhookBlock | ZapierBlock | MakeComBlock | PabblyConnectBlock
): Promise<ExecuteIntegrationResponse> => {
const logs: ReplyLog[] = []
const logs: ChatLog[] = []
const webhook =
block.options?.webhook ??
('webhookId' in block
@ -142,8 +142,8 @@ const parseWebhookAttributes =
export const executeWebhook = async (
webhook: ParsedWebhook
): Promise<{ response: WebhookResponse; logs?: ReplyLog[] }> => {
const logs: ReplyLog[] = []
): Promise<{ response: WebhookResponse; logs?: ChatLog[] }> => {
const logs: ChatLog[] = []
const { headers, url, method, basicAuth, body, isJson } = webhook
const contentType = headers ? headers['Content-Type'] : undefined

View File

@ -2,7 +2,7 @@ import { byId } from '@typebot.io/lib'
import {
MakeComBlock,
PabblyConnectBlock,
ReplyLog,
ChatLog,
VariableWithUnknowValue,
WebhookBlock,
ZapierBlock,
@ -15,7 +15,7 @@ import { updateVariablesInSession } from '../../../variables/updateVariablesInSe
type Props = {
state: SessionState
block: WebhookBlock | ZapierBlock | MakeComBlock | PabblyConnectBlock
logs?: ReplyLog[]
logs?: ChatLog[]
response: {
statusCode: number
data?: unknown

View File

@ -3,7 +3,7 @@ import {
TypebotLinkBlock,
SessionState,
Variable,
ReplyLog,
ChatLog,
Edge,
typebotInSessionStateSchema,
TypebotInSession,
@ -19,7 +19,7 @@ export const executeTypebotLink = async (
state: SessionState,
block: TypebotLinkBlock
): Promise<ExecuteLogicResponse> => {
const logs: ReplyLog[] = []
const logs: ChatLog[] = []
const typebotId = block.options?.typebotId
if (!typebotId) {
logs.push({

View File

@ -1,7 +1,7 @@
import {
AnswerInSessionState,
Block,
ChatReply,
ContinueChatResponse,
Group,
InputBlock,
SessionState,
@ -46,7 +46,10 @@ export const continueBotFlow = async (
reply: string | undefined,
{ state, version }: Params
): Promise<
ChatReply & { newSessionState: SessionState; visitedEdges: VisitedEdge[] }
ContinueChatResponse & {
newSessionState: SessionState
visitedEdges: VisitedEdge[]
}
> => {
let firstBubbleWasStreamed = false
let newSessionState = { ...state }
@ -202,7 +205,9 @@ const saveVariableValueIfAny =
const parseRetryMessage =
(state: SessionState) =>
async (block: InputBlock): Promise<Pick<ChatReply, 'messages' | 'input'>> => {
async (
block: InputBlock
): Promise<Pick<ContinueChatResponse, 'messages' | 'input'>> => {
const retryMessage =
block.options &&
'retryMessageContent' in block.options &&

View File

@ -1,5 +1,5 @@
import {
ChatReply,
ContinueChatResponse,
Group,
InputBlock,
RuntimeOptions,
@ -31,7 +31,7 @@ import { VisitedEdge } from '@typebot.io/prisma'
type ContextProps = {
version: 1 | 2
state: SessionState
currentReply?: ChatReply
currentReply?: ContinueChatResponse
currentLastBubbleId?: string
firstBubbleWasStreamed?: boolean
visitedEdges: VisitedEdge[]
@ -48,12 +48,16 @@ export const executeGroup = async (
firstBubbleWasStreamed,
}: ContextProps
): Promise<
ChatReply & { newSessionState: SessionState; visitedEdges: VisitedEdge[] }
ContinueChatResponse & {
newSessionState: SessionState
visitedEdges: VisitedEdge[]
}
> => {
const messages: ChatReply['messages'] = currentReply?.messages ?? []
let clientSideActions: ChatReply['clientSideActions'] =
const messages: ContinueChatResponse['messages'] =
currentReply?.messages ?? []
let clientSideActions: ContinueChatResponse['clientSideActions'] =
currentReply?.clientSideActions
let logs: ChatReply['logs'] = currentReply?.logs
let logs: ContinueChatResponse['logs'] = currentReply?.logs
let nextEdgeId = null
let lastBubbleBlockId: string | undefined = currentLastBubbleId
@ -173,7 +177,7 @@ const computeRuntimeOptions =
export const parseInput =
(state: SessionState) =>
async (block: InputBlock): Promise<ChatReply['input']> => {
async (block: InputBlock): Promise<ContinueChatResponse['input']> => {
switch (block.type) {
case InputBlockType.CHOICE: {
return injectVariableValuesInButtonsInputBlock(state)(block)

View File

@ -1,5 +1,10 @@
import { parseVideoUrl } from '@typebot.io/lib/parseVideoUrl'
import { BubbleBlock, Variable, ChatReply, Typebot } from '@typebot.io/schemas'
import {
BubbleBlock,
Variable,
ContinueChatResponse,
Typebot,
} from '@typebot.io/schemas'
import { deepParseVariables } from './variables/deepParseVariables'
import { isEmpty, isNotEmpty } from '@typebot.io/lib/utils'
import {
@ -27,7 +32,7 @@ export type BubbleBlockWithDefinedContent = BubbleBlock & {
export const parseBubbleBlock = (
block: BubbleBlockWithDefinedContent,
{ version, variables, typebotVersion }: Params
): ChatReply['messages'][0] => {
): ContinueChatResponse['messages'][0] => {
switch (block.type) {
case BubbleBlockType.TEXT: {
if (version === 1)

View File

@ -1,9 +1,9 @@
import { SessionState, ChatReply } from '@typebot.io/schemas'
import { SessionState, ContinueChatResponse } from '@typebot.io/schemas'
import { parseVariables } from './variables/parseVariables'
export const parseDynamicTheme = (
state: SessionState | undefined
): ChatReply['dynamicTheme'] => {
): ContinueChatResponse['dynamicTheme'] => {
if (!state?.dynamicTheme) return
return {
hostAvatarUrl: parseVariables(state?.typebotsQueue[0].typebot.variables)(

View File

@ -1,4 +1,4 @@
import { ChatReply, ChatSession } from '@typebot.io/schemas'
import { ContinueChatResponse, ChatSession } from '@typebot.io/schemas'
import { upsertResult } from './queries/upsertResult'
import { saveLogs } from './queries/saveLogs'
import { updateSession } from './queries/updateSession'
@ -11,9 +11,9 @@ import { VisitedEdge } from '@typebot.io/prisma'
type Props = {
session: Pick<ChatSession, 'state'> & { id?: string }
input: ChatReply['input']
logs: ChatReply['logs']
clientSideActions: ChatReply['clientSideActions']
input: ContinueChatResponse['input']
logs: ContinueChatResponse['logs']
clientSideActions: ContinueChatResponse['clientSideActions']
visitedEdges: VisitedEdge[]
forceCreateSession?: boolean
}

View File

@ -1,5 +1,9 @@
import { TRPCError } from '@trpc/server'
import { ChatReply, SessionState, StartElementId } from '@typebot.io/schemas'
import {
ContinueChatResponse,
SessionState,
StartFrom,
} from '@typebot.io/schemas'
import { executeGroup } from './executeGroup'
import { getNextGroup } from './getNextGroup'
import { VisitedEdge } from '@typebot.io/prisma'
@ -7,20 +11,24 @@ import { VisitedEdge } from '@typebot.io/prisma'
type Props = {
version: 1 | 2
state: SessionState
} & StartElementId
startFrom?: StartFrom
}
export const startBotFlow = async ({
version,
state,
...props
startFrom,
}: Props): Promise<
ChatReply & { newSessionState: SessionState; visitedEdges: VisitedEdge[] }
ContinueChatResponse & {
newSessionState: SessionState
visitedEdges: VisitedEdge[]
}
> => {
let newSessionState = state
const visitedEdges: VisitedEdge[] = []
if ('startGroupId' in props) {
if (startFrom?.type === 'group') {
const group = state.typebotsQueue[0].typebot.groups.find(
(group) => group.id === props.startGroupId
(group) => group.id === startFrom.groupId
)
if (!group)
throw new TRPCError({
@ -35,7 +43,7 @@ export const startBotFlow = async ({
}
const firstEdgeId = getFirstEdgeId({
state: newSessionState,
startEventId: 'startEventId' in props ? props.startEventId : undefined,
startEventId: startFrom?.type === 'event' ? startFrom.eventId : undefined,
})
if (!firstEdgeId) return { messages: [], newSessionState, visitedEdges: [] }
const nextGroup = await getNextGroup(newSessionState)(firstEdgeId)

View File

@ -12,13 +12,13 @@ import {
Block,
} from '@typebot.io/schemas'
import {
ChatReply,
StartParams,
StartChatInput,
StartChatResponse,
StartPreviewChatInput,
StartTypebot,
startTypebotSchema,
} from '@typebot.io/schemas/features/chat/schema'
import parse, { NodeType } from 'node-html-parser'
import { env } from '@typebot.io/env'
import { parseDynamicTheme } from './parseDynamicTheme'
import { findTypebot } from './queries/findTypebot'
import { findPublicTypebot } from './queries/findPublicTypebot'
@ -36,11 +36,19 @@ import { IntegrationBlockType } from '@typebot.io/schemas/features/blocks/integr
import { defaultTheme } from '@typebot.io/schemas/features/typebot/theme/constants'
import { VisitedEdge } from '@typebot.io/prisma'
type StartParams =
| ({
type: 'preview'
userId: string
} & StartPreviewChatInput)
| ({
type: 'live'
} & StartChatInput)
type Props = {
version: 1 | 2
message: string | undefined
startParams: StartParams
userId: string | undefined
initialSessionState?: Pick<SessionState, 'whatsApp' | 'expiryTimeout'>
}
@ -48,26 +56,24 @@ export const startSession = async ({
version,
message,
startParams,
userId,
initialSessionState,
}: Props): Promise<
ChatReply & { newSessionState: SessionState; visitedEdges: VisitedEdge[] }
Omit<StartChatResponse, 'resultId'> & {
newSessionState: SessionState
visitedEdges: VisitedEdge[]
resultId?: string
}
> => {
if (!startParams)
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'StartParams are missing',
})
const typebot = await getTypebot(startParams)
const typebot = await getTypebot(startParams, userId)
const prefilledVariables = startParams.prefilledVariables
? prefillVariables(typebot.variables, startParams.prefilledVariables)
: typebot.variables
const prefilledVariables =
startParams.type === 'live' && startParams.prefilledVariables
? prefillVariables(typebot.variables, startParams.prefilledVariables)
: typebot.variables
const result = await getResult({
...startParams,
isPreview: startParams.isPreview || typeof startParams.typebot !== 'string',
resultId: startParams.type === 'live' ? startParams.resultId : undefined,
isPreview: startParams.type === 'preview',
typebotId: typebot.id,
prefilledVariables,
isRememberUserEnabled:
@ -148,11 +154,8 @@ export const startSession = async ({
let chatReply = await startBotFlow({
version,
state: initialState,
...('startGroupId' in startParams
? { startGroupId: startParams.startGroupId }
: 'startEventId' in startParams
? { startEventId: startParams.startEventId }
: {}),
startFrom:
startParams.type === 'preview' ? startParams.startFrom : undefined,
})
// If params has message and first block is an input block, we can directly continue the bot flow
@ -266,20 +269,16 @@ export const startSession = async ({
}
}
const getTypebot = async (
{ typebot, isPreview }: Pick<StartParams, 'typebot' | 'isPreview'>,
userId?: string
): Promise<StartTypebot> => {
if (typeof typebot !== 'string') return typebot
if (isPreview && !userId && !env.NEXT_PUBLIC_E2E_TEST)
throw new TRPCError({
code: 'UNAUTHORIZED',
message:
'You need to authenticate the request to start a bot in preview mode.',
})
const typebotQuery = isPreview
? await findTypebot({ id: typebot, userId })
: await findPublicTypebot({ publicId: typebot })
const getTypebot = async (startParams: StartParams): Promise<StartTypebot> => {
if (startParams.type === 'preview' && startParams.typebot)
return startParams.typebot
const typebotQuery =
startParams.type === 'preview'
? await findTypebot({
id: startParams.typebotId,
userId: startParams.userId,
})
: await findPublicTypebot({ publicId: startParams.publicId })
const parsedTypebot =
typebotQuery && 'typebot' in typebotQuery
@ -319,7 +318,9 @@ const getResult = async ({
resultId,
prefilledVariables,
isRememberUserEnabled,
}: Pick<StartParams, 'isPreview' | 'resultId'> & {
}: {
resultId: string | undefined
isPreview: boolean
typebotId: string
prefilledVariables: Variable[]
isRememberUserEnabled: boolean
@ -375,7 +376,7 @@ const parseDynamicThemeInState = (theme: Theme) => {
const parseStartClientSideAction = (
typebot: StartTypebot
): NonNullable<ChatReply['clientSideActions']>[number] | undefined => {
): NonNullable<StartChatResponse['clientSideActions']>[number] | undefined => {
const blocks = typebot.groups.flatMap<Block>((group) => group.blocks)
const pixelBlocks = (
blocks.filter(

View File

@ -1,16 +1,16 @@
import { ChatReply, SessionState } from '@typebot.io/schemas'
import { ContinueChatResponse, SessionState } from '@typebot.io/schemas'
export type EdgeId = string
export type ExecuteLogicResponse = {
outgoingEdgeId: EdgeId | undefined
newSessionState?: SessionState
} & Pick<ChatReply, 'clientSideActions' | 'logs'>
} & Pick<ContinueChatResponse, 'clientSideActions' | 'logs'>
export type ExecuteIntegrationResponse = {
outgoingEdgeId: EdgeId | undefined
newSessionState?: SessionState
} & Pick<ChatReply, 'clientSideActions' | 'logs'>
} & Pick<ContinueChatResponse, 'clientSideActions' | 'logs'>
export type ParsedReply =
| { status: 'success'; reply: string }

View File

@ -1,9 +1,9 @@
import { safeStringify } from '@typebot.io/lib/safeStringify'
import { StartParams, Variable } from '@typebot.io/schemas'
import { StartChatInput, Variable } from '@typebot.io/schemas'
export const prefillVariables = (
variables: Variable[],
prefilledVariables: NonNullable<StartParams['prefilledVariables']>
prefilledVariables: NonNullable<StartChatInput['prefilledVariables']>
): Variable[] =>
variables.map((variable) => {
const prefilledVariable = prefilledVariables[variable.name]

View File

@ -1,4 +1,4 @@
import { ButtonItem, ChatReply } from '@typebot.io/schemas'
import { ButtonItem, ContinueChatResponse } from '@typebot.io/schemas'
import { WhatsAppSendingMessage } from '@typebot.io/schemas/features/whatsapp'
import { convertRichTextToWhatsAppText } from './convertRichTextToWhatsAppText'
import { isDefined, isEmpty } from '@typebot.io/lib/utils'
@ -8,8 +8,8 @@ import { defaultPictureChoiceOptions } from '@typebot.io/schemas/features/blocks
import { defaultChoiceInputOptions } from '@typebot.io/schemas/features/blocks/inputs/choice/constants'
export const convertInputToWhatsAppMessages = (
input: NonNullable<ChatReply['input']>,
lastMessage: ChatReply['messages'][number] | undefined
input: NonNullable<ContinueChatResponse['input']>,
lastMessage: ContinueChatResponse['messages'][number] | undefined
): WhatsAppSendingMessage[] => {
const lastMessageText =
lastMessage?.type === BubbleBlockType.TEXT

View File

@ -1,4 +1,4 @@
import { ChatReply } from '@typebot.io/schemas'
import { ContinueChatResponse } from '@typebot.io/schemas'
import { WhatsAppSendingMessage } from '@typebot.io/schemas/features/whatsapp'
import { convertRichTextToWhatsAppText } from './convertRichTextToWhatsAppText'
import { isSvgSrc } from '@typebot.io/lib/utils'
@ -8,7 +8,7 @@ import { VideoBubbleContentType } from '@typebot.io/schemas/features/blocks/bubb
const mp4HttpsUrlRegex = /^https:\/\/.*\.mp4$/
export const convertMessageToWhatsAppMessage = (
message: ChatReply['messages'][number]
message: ContinueChatResponse['messages'][number]
): WhatsAppSendingMessage | undefined => {
switch (message.type) {
case BubbleBlockType.TEXT: {

View File

@ -1,4 +1,8 @@
import { ChatReply, SessionState, Settings } from '@typebot.io/schemas'
import {
ContinueChatResponse,
SessionState,
Settings,
} from '@typebot.io/schemas'
import {
WhatsAppCredentials,
WhatsAppSendingMessage,
@ -21,7 +25,7 @@ type Props = {
typingEmulation: SessionState['typingEmulation']
credentials: WhatsAppCredentials['data']
state: SessionState
} & Pick<ChatReply, 'messages' | 'input' | 'clientSideActions'>
} & Pick<ContinueChatResponse, 'messages' | 'input' | 'clientSideActions'>
export const sendChatReplyToWhatsApp = async ({
to,
@ -171,7 +175,9 @@ const getTypingDuration = ({
}
}
const isLastMessageIncludedInInput = (input: ChatReply['input']): boolean => {
const isLastMessageIncludedInInput = (
input: ContinueChatResponse['input']
): boolean => {
if (isNotDefined(input)) return false
return input.type === InputBlockType.CHOICE
}
@ -179,7 +185,9 @@ const isLastMessageIncludedInInput = (input: ChatReply['input']): boolean => {
const executeClientSideAction =
(context: { to: string; credentials: WhatsAppCredentials['data'] }) =>
async (
clientSideAction: NonNullable<ChatReply['clientSideActions']>[number]
clientSideAction: NonNullable<
ContinueChatResponse['clientSideActions']
>[number]
): Promise<{ replyToSend: string | undefined } | void> => {
if ('wait' in clientSideAction) {
await new Promise((resolve) =>

View File

@ -1,6 +1,6 @@
import prisma from '@typebot.io/lib/prisma'
import {
ChatReply,
ContinueChatResponse,
PublicTypebot,
SessionState,
Settings,
@ -20,7 +20,7 @@ import { VisitedEdge } from '@typebot.io/prisma'
type Props = {
incomingMessage?: string
workspaceId?: string
workspaceId: string
credentials: WhatsAppCredentials['data'] & Pick<WhatsAppCredentials, 'id'>
contact: NonNullable<SessionState['whatsApp']>['contact']
}
@ -31,7 +31,7 @@ export const startWhatsAppSession = async ({
credentials,
contact,
}: Props): Promise<
| (ChatReply & {
| (ContinueChatResponse & {
newSessionState: SessionState
visitedEdges: VisitedEdge[]
})
@ -89,9 +89,10 @@ export const startWhatsAppSession = async ({
version: 2,
message: incomingMessage,
startParams: {
typebot: publicTypebot.typebot.publicId as string,
type: 'live',
publicId: publicTypebot.typebot.publicId as string,
isOnlyRegistering: false,
},
userId: undefined,
initialSessionState: {
whatsApp: {
contact,

View File

@ -1,6 +1,6 @@
{
"name": "@typebot.io/js",
"version": "0.2.15",
"version": "0.2.16",
"description": "Javascript library to display typebots on your website",
"type": "module",
"main": "dist/index.js",

View File

@ -1,7 +1,7 @@
import { LiteBadge } from './LiteBadge'
import { createEffect, createSignal, onCleanup, onMount, Show } from 'solid-js'
import { isNotDefined, isNotEmpty } from '@typebot.io/lib'
import { getInitialChatReplyQuery } from '@/queries/getInitialChatReplyQuery'
import { startChatQuery } from '@/queries/startChatQuery'
import { ConversationContainer } from './ConversationContainer'
import { setIsMobile } from '@/utils/isMobileSignal'
import { BotContext, InitialChatReply, OutgoingLog } from '@/types'
@ -12,7 +12,8 @@ import {
} from '@/utils/storage'
import { setCssVariablesValue } from '@/utils/setCssVariablesValue'
import immutableCss from '../assets/immutable.css'
import { InputBlock, StartElementId } from '@typebot.io/schemas'
import { InputBlock } from '@typebot.io/schemas'
import { StartFrom } from '@typebot.io/schemas'
import { defaultTheme } from '@typebot.io/schemas/features/typebot/theme/constants'
export type BotProps = {
@ -27,7 +28,8 @@ export type BotProps = {
onInit?: () => void
onEnd?: () => void
onNewLogs?: (logs: OutgoingLog[]) => void
} & StartElementId
startFrom?: StartFrom
}
export const Bot = (props: BotProps & { class?: string }) => {
const [initialChatReply, setInitialChatReply] = createSignal<
@ -47,11 +49,13 @@ export const Bot = (props: BotProps & { class?: string }) => {
})
const typebotIdFromProps =
typeof props.typebot === 'string' ? props.typebot : undefined
const { data, error } = await getInitialChatReplyQuery({
const isPreview =
typeof props.typebot !== 'string' || (props.isPreview ?? false)
const { data, error } = await startChatQuery({
stripeRedirectStatus: urlParams.get('redirect_status') ?? undefined,
typebot: props.typebot,
apiHost: props.apiHost,
isPreview: props.isPreview ?? false,
isPreview,
resultId: isNotEmpty(props.resultId)
? props.resultId
: getExistingResultIdFromStorage(typebotIdFromProps),
@ -59,14 +63,10 @@ export const Bot = (props: BotProps & { class?: string }) => {
...prefilledVariables,
...props.prefilledVariables,
},
...('startGroupId' in props
? { startGroupId: props.startGroupId }
: 'startEventId' in props
? { startEventId: props.startEventId }
: {}),
startFrom: props.startFrom,
})
if (error && 'code' in error && typeof error.code === 'string') {
if (typeof props.typebot !== 'string' || (props.isPreview ?? false)) {
if (isPreview) {
return setError(
new Error('An error occurred while loading the bot.', {
cause: error.message,

View File

@ -1,6 +1,6 @@
import { BotContext, ChatChunk as ChatChunkType } from '@/types'
import { isMobile } from '@/utils/isMobileSignal'
import { ChatReply, Settings, Theme } from '@typebot.io/schemas'
import { ContinueChatResponse, Settings, Theme } from '@typebot.io/schemas'
import { createSignal, For, onMount, Show } from 'solid-js'
import { HostBubble } from '../bubbles/HostBubble'
import { InputChatBlock } from '../InputChatBlock'
@ -9,7 +9,7 @@ import { StreamingBubble } from '../bubbles/StreamingBubble'
import { defaultTheme } from '@typebot.io/schemas/features/typebot/theme/constants'
import { defaultSettings } from '@typebot.io/schemas/features/typebot/settings/constants'
type Props = Pick<ChatReply, 'messages' | 'input'> & {
type Props = Pick<ContinueChatResponse, 'messages' | 'input'> & {
theme: Theme
settings: Settings
inputIndex: number

View File

@ -1,8 +1,8 @@
import {
ChatReply,
ContinueChatResponse,
InputBlock,
SendMessageInput,
Theme,
ChatLog,
} from '@typebot.io/schemas'
import {
createEffect,
@ -12,7 +12,7 @@ import {
onMount,
Show,
} from 'solid-js'
import { sendMessageQuery } from '@/queries/sendMessageQuery'
import { continueChatQuery } from '@/queries/continueChatQuery'
import { ChatChunk } from './ChatChunk'
import {
BotContext,
@ -30,10 +30,11 @@ import {
setFormattedMessages,
} from '@/utils/formattedMessagesSignal'
import { InputBlockType } from '@typebot.io/schemas/features/blocks/inputs/constants'
import { saveClientLogsQuery } from '@/queries/saveClientLogsQuery'
const parseDynamicTheme = (
initialTheme: Theme,
dynamicTheme: ChatReply['dynamicTheme']
dynamicTheme: ContinueChatResponse['dynamicTheme']
): Theme => ({
...initialTheme,
chat: {
@ -74,7 +75,7 @@ export const ConversationContainer = (props: Props) => {
},
])
const [dynamicTheme, setDynamicTheme] = createSignal<
ChatReply['dynamicTheme']
ContinueChatResponse['dynamicTheme']
>(props.initialChatReply.dynamicTheme)
const [theme, setTheme] = createSignal(props.initialChatReply.typebot.theme)
const [isSending, setIsSending] = createSignal(false)
@ -136,9 +137,16 @@ export const ConversationContainer = (props: Props) => {
const sendMessage = async (
message: string | undefined,
clientLogs?: SendMessageInput['clientLogs']
clientLogs?: ChatLog[]
) => {
if (clientLogs) props.onNewLogs?.(clientLogs)
if (clientLogs) {
props.onNewLogs?.(clientLogs)
await saveClientLogsQuery({
apiHost: props.context.apiHost,
sessionId: props.initialChatReply.sessionId,
clientLogs,
})
}
setHasError(false)
const currentInputBlock = [...chatChunks()].pop()?.input
if (currentInputBlock?.id && props.onAnswer && message)
@ -153,11 +161,10 @@ export const ConversationContainer = (props: Props) => {
const longRequest = setTimeout(() => {
setIsSending(true)
}, 1000)
const { data, error } = await sendMessageQuery({
const { data, error } = await continueChatQuery({
apiHost: props.context.apiHost,
sessionId: props.initialChatReply.sessionId,
message,
clientLogs,
})
clearTimeout(longRequest)
setIsSending(false)

View File

@ -1,5 +1,5 @@
import type {
ChatReply,
ContinueChatResponse,
ChoiceInputBlock,
EmailInputBlock,
FileInputBlock,
@ -39,7 +39,7 @@ import { defaultTheme } from '@typebot.io/schemas/features/typebot/theme/constan
type Props = {
ref: HTMLDivElement | undefined
block: NonNullable<ChatReply['input']>
block: NonNullable<ContinueChatResponse['input']>
hasHostAvatar: boolean
guestAvatar?: NonNullable<Theme['chat']>['guestAvatar']
inputIndex: number
@ -113,7 +113,7 @@ export const InputChatBlock = (props: Props) => {
const Input = (props: {
context: BotContext
block: NonNullable<ChatReply['input']>
block: NonNullable<ContinueChatResponse['input']>
inputIndex: number
isInputPrefillEnabled: boolean
onSubmit: (answer: InputSubmitContent) => void
@ -252,11 +252,11 @@ const Input = (props: {
}
const isButtonsBlock = (
block: ChatReply['input']
block: ContinueChatResponse['input']
): ChoiceInputBlock | undefined =>
block?.type === InputBlockType.CHOICE ? block : undefined
const isPictureChoiceBlock = (
block: ChatReply['input']
block: ContinueChatResponse['input']
): PictureChoiceBlock | undefined =>
block?.type === InputBlockType.PICTURE_CHOICE ? block : undefined

View File

@ -10,7 +10,7 @@ export const defaultBotProps: BotProps = {
onInit: undefined,
onNewLogs: undefined,
isPreview: undefined,
startGroupId: undefined,
startFrom: undefined,
prefilledVariables: undefined,
apiHost: undefined,
resultId: undefined,

View File

@ -0,0 +1,22 @@
import { guessApiHost } from '@/utils/guessApiHost'
import { isNotEmpty, sendRequest } from '@typebot.io/lib'
import { ContinueChatResponse } from '@typebot.io/schemas'
export const continueChatQuery = ({
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,
},
})

View File

@ -1,74 +0,0 @@
import { BotContext, InitialChatReply } from '@/types'
import { guessApiHost } from '@/utils/guessApiHost'
import type {
SendMessageInput,
StartElementId,
StartParams,
} from '@typebot.io/schemas'
import { isNotDefined, isNotEmpty, sendRequest } from '@typebot.io/lib'
import {
getPaymentInProgressInStorage,
removePaymentInProgressFromStorage,
} from '@/features/blocks/inputs/payment/helpers/paymentInProgressStorage'
export async function getInitialChatReplyQuery({
typebot,
isPreview,
apiHost,
prefilledVariables,
resultId,
stripeRedirectStatus,
...props
}: StartParams & {
stripeRedirectStatus?: string
apiHost?: string
} & StartElementId) {
if (isNotDefined(typebot))
throw new Error('Typebot ID is required to get initial messages')
const paymentInProgressStateStr = getPaymentInProgressInStorage() ?? undefined
const paymentInProgressState = paymentInProgressStateStr
? (JSON.parse(paymentInProgressStateStr) as {
sessionId: string
typebot: BotContext['typebot']
})
: undefined
if (paymentInProgressState) removePaymentInProgressFromStorage()
const { data, error } = await sendRequest<InitialChatReply>({
method: 'POST',
url: `${isNotEmpty(apiHost) ? apiHost : guessApiHost()}/api/v2/sendMessage`,
body: {
startParams: paymentInProgressState
? undefined
: {
isPreview,
typebot,
prefilledVariables,
resultId,
isStreamEnabled: true,
startGroupId:
'startGroupId' in props ? props.startGroupId : undefined,
startEventId:
'startEventId' in props ? props.startEventId : undefined,
},
sessionId: paymentInProgressState?.sessionId,
message: paymentInProgressState
? stripeRedirectStatus === 'failed'
? 'fail'
: 'Success'
: undefined,
} satisfies SendMessageInput,
})
return {
data: data
? {
...data,
...(paymentInProgressState
? { typebot: paymentInProgressState.typebot }
: {}),
}
: undefined,
error,
}
}

View File

@ -0,0 +1,22 @@
import { guessApiHost } from '@/utils/guessApiHost'
import type { ChatLog } from '@typebot.io/schemas'
import { isNotEmpty, sendRequest } from '@typebot.io/lib'
export const saveClientLogsQuery = ({
apiHost,
sessionId,
clientLogs,
}: {
apiHost?: string
sessionId: string
clientLogs: ChatLog[]
}) =>
sendRequest({
method: 'POST',
url: `${
isNotEmpty(apiHost) ? apiHost : guessApiHost()
}/api/v1/sessions/${sessionId}/clientLogs`,
body: {
clientLogs,
},
})

View File

@ -1,13 +0,0 @@
import { guessApiHost } from '@/utils/guessApiHost'
import type { ChatReply, SendMessageInput } from '@typebot.io/schemas'
import { isNotEmpty, sendRequest } from '@typebot.io/lib'
export const sendMessageQuery = ({
apiHost,
...body
}: SendMessageInput & { apiHost?: string }) =>
sendRequest<ChatReply>({
method: 'POST',
url: `${isNotEmpty(apiHost) ? apiHost : guessApiHost()}/api/v2/sendMessage`,
body,
})

View File

@ -0,0 +1,104 @@
import { BotContext, InitialChatReply } from '@/types'
import { guessApiHost } from '@/utils/guessApiHost'
import { isNotDefined, isNotEmpty, sendRequest } from '@typebot.io/lib'
import {
getPaymentInProgressInStorage,
removePaymentInProgressFromStorage,
} from '@/features/blocks/inputs/payment/helpers/paymentInProgressStorage'
import {
StartChatInput,
StartFrom,
StartPreviewChatInput,
} from '@typebot.io/schemas'
export async function startChatQuery({
typebot,
isPreview,
apiHost,
prefilledVariables,
resultId,
stripeRedirectStatus,
startFrom,
}: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
typebot: string | any
stripeRedirectStatus?: string
apiHost?: string
startFrom?: StartFrom
isPreview: boolean
prefilledVariables?: Record<string, unknown>
resultId?: string
}) {
if (isNotDefined(typebot))
throw new Error('Typebot ID is required to get initial messages')
const paymentInProgressStateStr = getPaymentInProgressInStorage() ?? undefined
const paymentInProgressState = paymentInProgressStateStr
? (JSON.parse(paymentInProgressStateStr) as {
sessionId: string
typebot: BotContext['typebot']
})
: undefined
if (paymentInProgressState) {
removePaymentInProgressFromStorage()
const { data, error } = await sendRequest<InitialChatReply>({
method: 'POST',
url: `${isNotEmpty(apiHost) ? apiHost : guessApiHost()}/api/v1/sessions/${
paymentInProgressState.sessionId
}/continueChat`,
body: {
message: paymentInProgressState
? stripeRedirectStatus === 'failed'
? 'fail'
: 'Success'
: undefined,
},
})
return {
data: data
? {
...data,
...(paymentInProgressState
? { typebot: paymentInProgressState.typebot }
: {}),
}
: undefined,
error,
}
}
const typebotId = typeof typebot === 'string' ? typebot : typebot.id
if (isPreview) {
const { data, error } = await sendRequest<InitialChatReply>({
method: 'POST',
url: `${
isNotEmpty(apiHost) ? apiHost : guessApiHost()
}/api/v1/typebots/${typebotId}/preview/startChat`,
body: {
isStreamEnabled: true,
startFrom,
typebot,
} satisfies Omit<StartPreviewChatInput, 'typebotId'>,
})
return {
data,
error,
}
}
const { data, error } = await sendRequest<InitialChatReply>({
method: 'POST',
url: `${
isNotEmpty(apiHost) ? apiHost : guessApiHost()
}/api/v1/typebots/${typebotId}/startChat`,
body: {
isStreamEnabled: true,
prefilledVariables,
resultId,
} satisfies Omit<StartChatInput, 'publicId'>,
})
return {
data,
error,
}
}

View File

@ -1,4 +1,4 @@
import type { ChatReply } from '@typebot.io/schemas'
import { ContinueChatResponse, StartChatResponse } from '@typebot.io/schemas'
export type InputSubmitContent = {
label?: string
@ -13,9 +13,9 @@ export type BotContext = {
sessionId: string
}
export type InitialChatReply = ChatReply & {
typebot: NonNullable<ChatReply['typebot']>
sessionId: NonNullable<ChatReply['sessionId']>
export type InitialChatReply = StartChatResponse & {
typebot: NonNullable<StartChatResponse['typebot']>
sessionId: NonNullable<StartChatResponse['sessionId']>
}
export type OutgoingLog = {
@ -30,7 +30,7 @@ export type ClientSideActionContext = {
}
export type ChatChunk = Pick<
ChatReply,
ContinueChatResponse,
'messages' | 'input' | 'clientSideActions'
> & {
streamingMessageId?: string

View File

@ -8,11 +8,11 @@ import { executeWait } from '@/features/blocks/logic/wait/utils/executeWait'
import { executeWebhook } from '@/features/blocks/integrations/webhook/executeWebhook'
import { executePixel } from '@/features/blocks/integrations/pixel/executePixel'
import { ClientSideActionContext } from '@/types'
import type { ChatReply, ReplyLog } from '@typebot.io/schemas'
import type { ContinueChatResponse, ChatLog } from '@typebot.io/schemas'
import { injectStartProps } from './injectStartProps'
type Props = {
clientSideAction: NonNullable<ChatReply['clientSideActions']>[0]
clientSideAction: NonNullable<ContinueChatResponse['clientSideActions']>[0]
context: ClientSideActionContext
onMessageStream?: (props: { id: string; message: string }) => void
}
@ -23,7 +23,7 @@ export const executeClientSideAction = async ({
onMessageStream,
}: Props): Promise<
| { blockedPopupUrl: string }
| { replyToSend: string | undefined; logs?: ReplyLog[] }
| { replyToSend: string | undefined; logs?: ChatLog[] }
| void
> => {
if ('chatwoot' in clientSideAction) {

View File

@ -1,6 +1,6 @@
{
"name": "@typebot.io/nextjs",
"version": "0.2.15",
"version": "0.2.16",
"description": "Convenient library to display typebots on your Next.js website",
"main": "dist/index.js",
"types": "dist/index.d.ts",

View File

@ -1,6 +1,6 @@
{
"name": "@typebot.io/react",
"version": "0.2.15",
"version": "0.2.16",
"description": "Convenient library to display typebots on your React app",
"main": "dist/index.js",
"types": "dist/index.d.ts",

View File

@ -0,0 +1,123 @@
import { z } from 'zod'
import {
chatLogSchema,
chatMessageSchema,
clientSideActionSchema,
runtimeOptionsSchema,
startTypebotSchema,
} from '../schema'
import { typebotV5Schema, typebotV6Schema } from '../../typebot'
import { dynamicThemeSchema } from '../shared'
import { inputBlockSchemas } from '../../blocks'
export const startElementIdSchema = z.union([
z.object({
startGroupId: z.string().describe('Start chat from a specific group.'),
startEventId: z.never().optional(),
}),
z.object({
startEventId: z.string().describe('Start chat from a specific event.'),
startGroupId: z.never().optional(),
}),
z.object({}),
])
export type StartElementId = z.infer<typeof startElementIdSchema>
const startParamsSchema = z
.object({
typebot: startTypebotSchema
.or(z.string())
.describe(
'Either a Typebot ID or a Typebot object. If you provide a Typebot object, it will be executed in preview mode. ([How can I find my typebot ID?](https://docs.typebot.io/api#how-to-find-my-typebotid)).'
),
isPreview: z
.boolean()
.optional()
.describe(
"If set to `true`, it will start a Preview session with the unpublished bot and it won't be saved in the Results tab. You need to be authenticated with a bearer token for this to work."
),
resultId: z
.string()
.optional()
.describe("Provide it if you'd like to overwrite an existing result."),
prefilledVariables: z
.record(z.unknown())
.optional()
.describe(
'[More info about prefilled variables.](https://docs.typebot.io/editor/variables#prefilled-variables)'
),
isStreamEnabled: z
.boolean()
.optional()
.describe(
'Set this to `true` if you intend to stream OpenAI completions on a client.'
),
isOnlyRegistering: z
.boolean()
.optional()
.describe(
'If set to `true`, it will only register the session and not start the chat. This is used for other chat platform integration as it can require a session to be registered before sending the first message.'
),
})
.and(startElementIdSchema)
export const sendMessageInputSchema = z.object({
message: z
.string()
.optional()
.describe(
'The answer to the previous chat input. Do not provide it if you are starting a new chat.'
),
sessionId: z
.string()
.optional()
.describe(
'Session ID that you get from the initial chat request to a bot. If not provided, it will create a new session.'
),
startParams: startParamsSchema.optional(),
clientLogs: z
.array(chatLogSchema)
.optional()
.describe('Logs while executing client side actions'),
})
export const chatReplySchema = z.object({
messages: z.array(chatMessageSchema),
input: z
.union([
z.discriminatedUnion('type', [...inputBlockSchemas.v5]),
z.discriminatedUnion('type', [...inputBlockSchemas.v6]),
])
.and(
z.object({
prefilledValue: z.string().optional(),
runtimeOptions: runtimeOptionsSchema.optional(),
})
)
.optional(),
clientSideActions: z.array(clientSideActionSchema).optional(),
sessionId: z.string().optional(),
typebot: z
.object({
id: z.string(),
theme: z.union([
typebotV5Schema._def.schema.shape.theme,
typebotV6Schema.shape.theme,
]),
settings: z.union([
typebotV5Schema._def.schema.shape.settings,
typebotV6Schema.shape.settings,
]),
})
.optional(),
resultId: z.string().optional(),
dynamicTheme: dynamicThemeSchema.optional(),
logs: z.array(chatLogSchema).optional(),
lastMessageNewFormat: z
.string()
.optional()
.describe(
'The sent message is validated and formatted on the backend. This is set only if the message differs from the formatted version.'
),
})

View File

@ -29,6 +29,7 @@ const chatSessionSchema = z.object({
updatedAt: z.date(),
state: sessionStateSchema,
})
export type ChatSession = z.infer<typeof chatSessionSchema>
const textMessageSchema = z.object({
type: z.literal(BubbleBlockType.TEXT),
@ -59,7 +60,7 @@ const embedMessageSchema = z.object({
.merge(z.object({ height: z.number().optional() })),
})
const chatMessageSchema = z
export const chatMessageSchema = z
.object({ id: z.string() })
.and(
z.discriminatedUnion('type', [
@ -70,6 +71,7 @@ const chatMessageSchema = z
embedMessageSchema,
])
)
export type ChatMessage = z.infer<typeof chatMessageSchema>
const scriptToExecuteSchema = z.object({
content: z.string(),
@ -85,6 +87,7 @@ const scriptToExecuteSchema = z.object({
})
),
})
export type ScriptToExecute = z.infer<typeof scriptToExecuteSchema>
const startTypebotPick = {
version: true,
@ -103,87 +106,72 @@ export const startTypebotSchema = z.preprocess(
typebotV6Schema.pick(startTypebotPick),
])
)
export type StartTypebot = z.infer<typeof startTypebotSchema>
export const startElementIdSchema = z.union([
z.object({
startGroupId: z.string().describe('Start chat from a specific group.'),
startEventId: z.never().optional(),
}),
z.object({
startEventId: z.string().describe('Start chat from a specific event.'),
startGroupId: z.never().optional(),
}),
z.object({}),
])
export type StartElementId = z.infer<typeof startElementIdSchema>
const startParamsSchema = z
.object({
typebot: startTypebotSchema
.or(z.string())
.describe(
'Either a Typebot ID or a Typebot object. If you provide a Typebot object, it will be executed in preview mode. ([How can I find my typebot ID?](https://docs.typebot.io/api#how-to-find-my-typebotid)).'
),
isPreview: z
.boolean()
.optional()
.describe(
"If set to `true`, it will start a Preview session with the unpublished bot and it won't be saved in the Results tab. You need to be authenticated with a bearer token for this to work."
),
resultId: z
.string()
.optional()
.describe("Provide it if you'd like to overwrite an existing result."),
prefilledVariables: z
.record(z.unknown())
.optional()
.describe(
'[More info about prefilled variables.](https://docs.typebot.io/editor/variables#prefilled-variables)'
),
isStreamEnabled: z
.boolean()
.optional()
.describe(
'Set this to `true` if you intend to stream OpenAI completions on a client.'
),
isOnlyRegistering: z
.boolean()
.optional()
.describe(
'If set to `true`, it will only register the session and not start the chat. This is used for other chat platform integration as it can require a session to be registered before sending the first message.'
),
})
.and(startElementIdSchema)
const replyLogSchema = logSchema
export const chatLogSchema = logSchema
.pick({
status: true,
description: true,
})
.merge(z.object({ details: z.unknown().optional() }))
export type ChatLog = z.infer<typeof chatLogSchema>
export const sendMessageInputSchema = z.object({
message: z
export const startChatInputSchema = z.object({
publicId: z.string(),
isStreamEnabled: z.boolean().optional(),
message: z.string().optional(),
resultId: z
.string()
.optional()
.describe(
'The answer to the previous chat input. Do not provide it if you are starting a new chat.'
),
sessionId: z
.string()
.describe("Provide it if you'd like to overwrite an existing result."),
isOnlyRegistering: z
.boolean()
.optional()
.describe(
'Session ID that you get from the initial chat request to a bot. If not provided, it will create a new session.'
'If set to `true`, it will only register the session and not start the bot. This is used for 3rd party chat platforms as it can require a session to be registered before sending the first message.'
),
startParams: startParamsSchema.optional(),
clientLogs: z
.array(replyLogSchema)
prefilledVariables: z
.record(z.unknown())
.optional()
.describe('Logs while executing client side actions'),
.describe(
'[More info about prefilled variables.](https://docs.typebot.io/editor/variables#prefilled-variables)'
),
})
export type StartChatInput = z.infer<typeof startChatInputSchema>
const runtimeOptionsSchema = paymentInputRuntimeOptionsSchema.optional()
export const startFromSchema = z.discriminatedUnion('type', [
z.object({
type: z.literal('group'),
groupId: z.string(),
}),
z.object({
type: z.literal('event'),
eventId: z.string(),
}),
])
export type StartFrom = z.infer<typeof startFromSchema>
export const startPreviewChatInputSchema = z.object({
typebotId: z.string(),
isStreamEnabled: z.boolean().optional(),
message: z.string().optional(),
isOnlyRegistering: z
.boolean()
.optional()
.describe(
'If set to `true`, it will only register the session and not start the bot. This is used for 3rd party chat platforms as it can require a session to be registered before sending the first message.'
),
typebot: startTypebotSchema
.optional()
.describe(
'If set, it will override the typebot that is used to start the chat.'
),
startFrom: startFromSchema.optional(),
})
export type StartPreviewChatInput = z.infer<typeof startPreviewChatInputSchema>
export const runtimeOptionsSchema = paymentInputRuntimeOptionsSchema.optional()
export type RuntimeOptions = z.infer<typeof runtimeOptionsSchema>
const startPropsToInjectSchema = z.object({
googleAnalyticsId: z.string().optional(),
@ -191,8 +179,9 @@ const startPropsToInjectSchema = z.object({
gtmId: z.string().optional(),
customHeadCode: z.string().optional(),
})
export type StartPropsToInject = z.infer<typeof startPropsToInjectSchema>
const clientSideActionSchema = z
export const clientSideActionSchema = z
.object({
lastBubbleBlockId: z.string().optional(),
expectsDedicatedReply: z.boolean().optional(),
@ -271,7 +260,14 @@ export const typebotInChatReply = z.preprocess(
typebotV6Schema.pick(typebotInChatReplyPick),
])
)
export const chatReplySchema = z.object({
const chatResponseBaseSchema = z.object({
lastMessageNewFormat: z
.string()
.optional()
.describe(
'The sent message is validated and formatted on the backend. This is set only if the message differs from the formatted version.'
),
messages: z.array(chatMessageSchema),
input: z
.union([
@ -286,39 +282,30 @@ export const chatReplySchema = z.object({
)
.optional(),
clientSideActions: z.array(clientSideActionSchema).optional(),
sessionId: z.string().optional(),
typebot: z
.object({
id: z.string(),
theme: z.union([
typebotV5Schema._def.schema.shape.theme,
typebotV6Schema.shape.theme,
]),
settings: z.union([
typebotV5Schema._def.schema.shape.settings,
typebotV6Schema.shape.settings,
]),
})
.optional(),
resultId: z.string().optional(),
logs: z.array(chatLogSchema).optional(),
dynamicTheme: dynamicThemeSchema.optional(),
logs: z.array(replyLogSchema).optional(),
lastMessageNewFormat: z
.string()
.optional()
.describe(
'The sent message is validated and formatted on the backend. This is set only if the message differs from the formatted version.'
),
})
export type ChatSession = z.infer<typeof chatSessionSchema>
export const startChatResponseSchema = chatResponseBaseSchema.extend({
sessionId: z.string().optional(),
typebot: z.object({
id: z.string(),
theme: z.union([
typebotV5Schema._def.schema.shape.theme,
typebotV6Schema.shape.theme,
]),
settings: z.union([
typebotV5Schema._def.schema.shape.settings,
typebotV6Schema.shape.settings,
]),
}),
resultId: z.string().optional(),
})
export type StartChatResponse = z.infer<typeof startChatResponseSchema>
export type ChatReply = z.infer<typeof chatReplySchema>
export type ChatMessage = z.infer<typeof chatMessageSchema>
export type SendMessageInput = z.infer<typeof sendMessageInputSchema>
export type ScriptToExecute = z.infer<typeof scriptToExecuteSchema>
export type StartParams = z.infer<typeof startParamsSchema>
export type RuntimeOptions = z.infer<typeof runtimeOptionsSchema>
export type StartTypebot = z.infer<typeof startTypebotSchema>
export type ReplyLog = z.infer<typeof replyLogSchema>
export type StartPropsToInject = z.infer<typeof startPropsToInjectSchema>
export const startPreviewChatResponseSchema = startChatResponseSchema.omit({
resultId: true,
})
export const continueChatResponseSchema = chatResponseBaseSchema
export type ContinueChatResponse = z.infer<typeof continueChatResponseSchema>