2
0

🐛 (sendMessage) Correctly preprocess and parse fetched bot

This commit is contained in:
Baptiste Arnaud
2023-08-24 09:11:10 +02:00
parent ee3b94c35d
commit 06ecdf040e
15 changed files with 63 additions and 45 deletions

View File

@@ -10,6 +10,7 @@ import {
} from '@typebot.io/schemas'
export const leadGenerationTypebot: StartTypebot = {
version: null,
id: 'clckrl4q5000t3b6sabwokaar',
groups: [
{

View File

@@ -20,6 +20,7 @@ import { inputBlockSchemas } from '../blocks/schemas'
import { chatCompletionMessageSchema } from '../blocks/integrations/openai'
import { sessionStateSchema } from './sessionState'
import { dynamicThemeSchema } from './shared'
import { preprocessTypebot } from '../typebot/helpers/preprocessTypebot'
const chatSessionSchema = z.object({
id: z.string(),
@@ -84,14 +85,18 @@ const scriptToExecuteSchema = z.object({
),
})
const startTypebotSchema = typebotSchema._def.schema.pick({
id: true,
groups: true,
edges: true,
variables: true,
settings: true,
theme: true,
})
export const startTypebotSchema = z.preprocess(
preprocessTypebot,
typebotSchema._def.schema.pick({
version: true,
id: true,
groups: true,
edges: true,
variables: true,
settings: true,
theme: true,
})
)
const startParamsSchema = z.object({
typebot: startTypebotSchema

View File

@@ -1,13 +1,16 @@
import { z } from 'zod'
import { publicTypebotSchema } from '../publicTypebot'
import { preprocessTypebot } from '../typebot/helpers/preprocessTypebot'
export const typebotInSessionStateSchema = publicTypebotSchema._def.schema.pick(
{
export const typebotInSessionStateSchema = z.preprocess(
preprocessTypebot,
publicTypebotSchema._def.schema.pick({
version: true,
id: true,
groups: true,
edges: true,
variables: true,
}
})
)
export type TypebotInSession = z.infer<typeof typebotInSessionStateSchema>

View File

@@ -5,7 +5,6 @@ import {
variableSchema,
themeSchema,
settingsSchema,
typebotSchema,
} from './typebot'
import { z } from 'zod'
import { preprocessTypebot } from './typebot/helpers/preprocessTypebot'
@@ -26,13 +25,4 @@ export const publicTypebotSchema = z.preprocess(
})
) satisfies z.ZodType<PrismaPublicTypebot, z.ZodTypeDef, unknown>
const publicTypebotWithName = publicTypebotSchema._def.schema.merge(
typebotSchema._def.schema.pick({
name: true,
isArchived: true,
isClosed: true,
})
)
export type PublicTypebot = z.infer<typeof publicTypebotSchema>
export type PublicTypebotWithName = z.infer<typeof publicTypebotWithName>

View File

@@ -5,10 +5,10 @@ export const preprocessTypebot = (typebot: any) => {
if (!typebot || typebot.version === '5') return typebot
return {
...typebot,
groups: typebot.groups.map(preprocessGroup),
edges: typebot.edges?.filter(
(edge: any) => edgeSchema.safeParse(edge).success
),
groups: typebot.groups ? typebot.groups.map(preprocessGroup) : [],
edges: typebot.edges
? typebot.edges?.filter((edge: any) => edgeSchema.safeParse(edge).success)
: [],
}
}