2023-05-25 10:32:35 +02:00
import { z } from 'zod'
2022-11-29 10:02:40 +01:00
import {
googleAnalyticsOptionsSchema ,
2022-12-22 17:02:34 +01:00
paymentInputRuntimeOptionsSchema ,
2023-06-28 09:52:03 +02:00
pixelOptionsSchema ,
2022-12-22 17:02:34 +01:00
redirectOptionsSchema ,
2022-11-29 10:02:40 +01:00
} from './blocks'
import { publicTypebotSchema } from './publicTypebot'
2023-01-25 11:27:47 +01:00
import { logSchema , resultSchema } from './result'
2023-03-21 15:42:03 +01:00
import { listVariableValue , typebotSchema } from './typebot'
2023-01-25 11:27:47 +01:00
import {
textBubbleContentSchema ,
imageBubbleContentSchema ,
videoBubbleContentSchema ,
audioBubbleContentSchema ,
embedBubbleContentSchema ,
} from './blocks/bubbles'
2023-03-07 14:41:57 +01:00
import { answerSchema } from './answer'
2023-03-14 16:42:12 +01:00
import { BubbleBlockType } from './blocks/bubbles/enums'
2023-05-04 09:20:30 -04:00
import { inputBlockSchemas } from './blocks/schemas'
2023-05-25 10:32:35 +02:00
import { chatCompletionMessageSchema } from './blocks/integrations/openai'
2023-05-26 09:20:22 +02:00
import { executableWebhookSchema } from './webhooks'
2022-11-29 10:02:40 +01:00
const typebotInSessionStateSchema = publicTypebotSchema . pick ( {
id : true ,
groups : true ,
edges : true ,
variables : true ,
} )
2023-01-16 12:13:21 +01:00
const dynamicThemeSchema = z . object ( {
hostAvatarUrl : z.string ( ) . optional ( ) ,
guestAvatarUrl : z.string ( ) . optional ( ) ,
} )
2023-03-07 14:41:57 +01:00
const answerInSessionStateSchema = answerSchema . pick ( {
content : true ,
blockId : true ,
variableId : true ,
} )
const resultInSessionStateSchema = resultSchema
. pick ( {
variables : true ,
} )
2023-03-14 16:42:12 +01:00
. merge (
2023-03-07 14:41:57 +01:00
z . object ( {
answers : z.array ( answerInSessionStateSchema ) ,
id : z.string ( ) . optional ( ) ,
} )
)
2022-11-29 10:02:40 +01:00
export const sessionStateSchema = z . object ( {
typebot : typebotInSessionStateSchema ,
2023-01-16 12:13:21 +01:00
dynamicTheme : dynamicThemeSchema.optional ( ) ,
2022-11-29 10:02:40 +01:00
linkedTypebots : z.object ( {
typebots : z.array ( typebotInSessionStateSchema ) ,
queue : z.array ( z . object ( { edgeId : z.string ( ) , typebotId : z.string ( ) } ) ) ,
} ) ,
currentTypebotId : z.string ( ) ,
2023-03-07 14:41:57 +01:00
result : resultInSessionStateSchema ,
2022-11-29 10:02:40 +01:00
currentBlock : z
. object ( {
blockId : z.string ( ) ,
groupId : z.string ( ) ,
} )
. optional ( ) ,
2023-05-25 10:32:35 +02:00
isStreamEnabled : z.boolean ( ) . optional ( ) ,
2022-11-29 10:02:40 +01:00
} )
2023-03-07 14:41:57 +01:00
const chatSessionSchema = z . object ( {
id : z.string ( ) ,
createdAt : z.date ( ) ,
updatedAt : z.date ( ) ,
state : sessionStateSchema ,
} )
2022-11-29 10:02:40 +01:00
2022-12-22 17:02:34 +01:00
const textMessageSchema = z . object ( {
2023-03-14 16:42:12 +01:00
type : z . literal ( BubbleBlockType . TEXT ) ,
2023-04-13 17:04:21 +02:00
content : textBubbleContentSchema ,
2022-12-22 17:02:34 +01:00
} )
const imageMessageSchema = z . object ( {
type : z . enum ( [ BubbleBlockType . IMAGE ] ) ,
content : imageBubbleContentSchema ,
2022-11-29 10:02:40 +01:00
} )
2022-12-22 17:02:34 +01:00
const videoMessageSchema = z . object ( {
type : z . enum ( [ BubbleBlockType . VIDEO ] ) ,
content : videoBubbleContentSchema ,
} )
const audioMessageSchema = z . object ( {
type : z . enum ( [ BubbleBlockType . AUDIO ] ) ,
content : audioBubbleContentSchema ,
} )
const embedMessageSchema = z . object ( {
type : z . enum ( [ BubbleBlockType . EMBED ] ) ,
2023-02-19 09:53:57 +01:00
content : embedBubbleContentSchema
. omit ( {
height : true ,
} )
2023-03-14 16:42:12 +01:00
. merge ( z . object ( { height : z.number ( ) . optional ( ) } ) ) ,
2022-12-22 17:02:34 +01:00
} )
2023-01-27 10:54:59 +01:00
const chatMessageSchema = z
. object ( { id : z.string ( ) } )
. and (
2023-03-14 16:42:12 +01:00
z . discriminatedUnion ( 'type' , [
textMessageSchema ,
imageMessageSchema ,
videoMessageSchema ,
audioMessageSchema ,
embedMessageSchema ,
] )
2023-01-27 10:54:59 +01:00
)
2022-11-29 10:02:40 +01:00
2023-01-27 15:58:05 +01:00
const scriptToExecuteSchema = z . object ( {
2022-11-29 10:02:40 +01:00
content : z.string ( ) ,
args : z.array (
z . object ( {
id : z.string ( ) ,
2023-02-23 14:44:37 +01:00
value : z
. string ( )
. or ( z . number ( ) )
. or ( z . boolean ( ) )
2023-03-21 15:42:03 +01:00
. or ( listVariableValue )
2023-02-23 14:44:37 +01:00
. nullish ( ) ,
2022-11-29 10:02:40 +01:00
} )
) ,
} )
2023-01-16 12:13:21 +01:00
const startTypebotSchema = typebotSchema . pick ( {
id : true ,
groups : true ,
edges : true ,
variables : true ,
settings : true ,
theme : true ,
} )
2022-12-22 17:02:34 +01:00
const startParamsSchema = z . object ( {
2023-01-16 12:13:21 +01:00
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)).'
) ,
2022-12-22 17:02:34 +01:00
isPreview : z
. boolean ( )
. optional ( )
. describe (
2023-03-08 09:58:38 +01:00
"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 for this to work."
2022-12-22 17:02:34 +01:00
) ,
resultId : z
. string ( )
. optional ( )
. describe ( "Provide it if you'd like to overwrite an existing result." ) ,
2023-01-16 12:13:21 +01:00
startGroupId : z
. string ( )
. optional ( )
. describe ( 'Start chat from a specific group.' ) ,
prefilledVariables : z
. record ( z . unknown ( ) )
. optional ( )
. describe (
'[More info about prefilled variables.](https://docs.typebot.io/editor/variables#prefilled-variables)'
) ,
2023-05-25 10:32:35 +02:00
isStreamEnabled : z.boolean ( ) . optional ( ) ,
2022-12-22 17:02:34 +01:00
} )
2023-06-16 19:26:29 +02:00
const replyLogSchema = logSchema
. pick ( {
status : true ,
description : true ,
} )
. merge ( z . object ( { details : z.unknown ( ) . optional ( ) } ) )
2022-12-22 17:02:34 +01:00
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.'
) ,
2023-06-16 19:26:29 +02:00
clientLogs : z
. array ( replyLogSchema )
. optional ( )
. describe ( 'Logs while executing client side actions' ) ,
2022-12-22 17:02:34 +01:00
startParams : startParamsSchema.optional ( ) ,
} )
const runtimeOptionsSchema = paymentInputRuntimeOptionsSchema . optional ( )
2023-06-28 09:52:03 +02:00
const startPropsToInjectSchema = z . object ( {
googleAnalyticsId : z.string ( ) . optional ( ) ,
pixelId : z.string ( ) . optional ( ) ,
gtmId : z.string ( ) . optional ( ) ,
customHeadCode : z.string ( ) . optional ( ) ,
} )
2023-01-26 15:26:42 +01:00
const clientSideActionSchema = z
. object ( {
2023-01-27 10:54:59 +01:00
lastBubbleBlockId : z.string ( ) . optional ( ) ,
2023-01-26 15:26:42 +01:00
} )
2023-01-27 10:54:59 +01:00
. and (
z
. object ( {
2023-01-27 15:58:05 +01:00
scriptToExecute : scriptToExecuteSchema ,
2023-01-27 10:54:59 +01:00
} )
. or (
z . object ( {
redirect : redirectOptionsSchema ,
} )
)
. or (
z . object ( {
2023-01-27 15:58:05 +01:00
chatwoot : z.object ( { scriptToExecute : scriptToExecuteSchema } ) ,
2023-01-27 10:54:59 +01:00
} )
)
. or (
z . object ( {
googleAnalytics : googleAnalyticsOptionsSchema ,
} )
)
. or (
z . object ( {
wait : z.object ( {
secondsToWaitFor : z.number ( ) ,
} ) ,
} )
)
2023-04-14 12:11:42 +02:00
. or (
z . object ( {
setVariable : z.object ( { scriptToExecute : scriptToExecuteSchema } ) ,
} )
)
2023-05-25 10:32:35 +02:00
. or (
z . object ( {
streamOpenAiChatCompletion : z.object ( {
messages : z.array (
chatCompletionMessageSchema . pick ( { content : true , role : true } )
) ,
2023-07-19 11:20:44 +02:00
displayStream : z.boolean ( ) . optional ( ) ,
2023-05-25 10:32:35 +02:00
} ) ,
} )
)
2023-05-26 09:20:22 +02:00
. or (
z . object ( {
webhookToExecute : executableWebhookSchema ,
} )
)
2023-06-28 09:52:03 +02:00
. or (
z . object ( {
startPropsToInject : startPropsToInjectSchema ,
} )
)
. or (
z . object ( {
pixel : pixelOptionsSchema ,
} )
)
2023-01-26 18:23:09 +01:00
)
2023-01-26 15:26:42 +01:00
2022-11-29 10:02:40 +01:00
export const chatReplySchema = z . object ( {
2022-12-22 17:02:34 +01:00
messages : z.array ( chatMessageSchema ) ,
2023-05-04 09:20:30 -04:00
input : z
. discriminatedUnion ( 'type' , [ . . . inputBlockSchemas ] )
2022-12-22 17:02:34 +01:00
. and (
z . object ( {
prefilledValue : z.string ( ) . optional ( ) ,
runtimeOptions : runtimeOptionsSchema.optional ( ) ,
} )
)
. optional ( ) ,
2023-01-26 15:26:42 +01:00
clientSideActions : z.array ( clientSideActionSchema ) . optional ( ) ,
2022-12-22 17:02:34 +01:00
sessionId : z.string ( ) . optional ( ) ,
2023-01-16 12:13:21 +01:00
typebot : typebotSchema
. pick ( { id : true , theme : true , settings : true } )
. optional ( ) ,
2022-12-22 17:02:34 +01:00
resultId : z.string ( ) . optional ( ) ,
2023-01-16 12:13:21 +01:00
dynamicTheme : dynamicThemeSchema.optional ( ) ,
2023-01-25 11:27:47 +01:00
logs : z.array ( replyLogSchema ) . optional ( ) ,
2022-11-29 10:02:40 +01:00
} )
export type ChatSession = z . infer < typeof chatSessionSchema >
export type SessionState = z . infer < typeof sessionStateSchema >
export type TypebotInSession = z . infer < typeof typebotInSessionStateSchema >
2023-03-07 14:41:57 +01:00
export type ResultInSession = z . infer < typeof resultInSessionStateSchema >
2022-11-29 10:02:40 +01:00
export type ChatReply = z . infer < typeof chatReplySchema >
2022-12-22 17:02:34 +01:00
export type ChatMessage = z . infer < typeof chatMessageSchema >
export type SendMessageInput = z . infer < typeof sendMessageInputSchema >
2023-01-27 15:58:05 +01:00
export type ScriptToExecute = z . infer < typeof scriptToExecuteSchema >
2022-12-22 17:02:34 +01:00
export type StartParams = z . infer < typeof startParamsSchema >
export type RuntimeOptions = z . infer < typeof runtimeOptionsSchema >
2023-01-16 12:13:21 +01:00
export type StartTypebot = z . infer < typeof startTypebotSchema >
2023-01-25 11:27:47 +01:00
export type ReplyLog = z . infer < typeof replyLogSchema >
2023-06-28 09:52:03 +02:00
export type StartPropsToInject = z . infer < typeof startPropsToInjectSchema >