🚸 Improve parsing preprocessing on typebots
This commit is contained in:
@@ -80,7 +80,10 @@ const chatCompletionOptionsSchema = z
|
||||
responseMapping: z.array(
|
||||
z.object({
|
||||
id: z.string(),
|
||||
valueToExtract: z.enum(chatCompletionResponseValues),
|
||||
valueToExtract: z.preprocess(
|
||||
(val) => (!val ? 'Message content' : val),
|
||||
z.enum(chatCompletionResponseValues)
|
||||
),
|
||||
variableId: z.string().optional(),
|
||||
})
|
||||
),
|
||||
|
||||
@@ -21,7 +21,7 @@ import { BubbleBlockType } from './blocks/bubbles/enums'
|
||||
import { inputBlockSchemas } from './blocks/schemas'
|
||||
import { chatCompletionMessageSchema } from './blocks/integrations/openai'
|
||||
|
||||
const typebotInSessionStateSchema = publicTypebotSchema.pick({
|
||||
const typebotInSessionStateSchema = publicTypebotSchema._def.schema.pick({
|
||||
id: true,
|
||||
groups: true,
|
||||
edges: true,
|
||||
@@ -131,7 +131,7 @@ const scriptToExecuteSchema = z.object({
|
||||
),
|
||||
})
|
||||
|
||||
const startTypebotSchema = typebotSchema.pick({
|
||||
const startTypebotSchema = typebotSchema._def.schema.pick({
|
||||
id: true,
|
||||
groups: true,
|
||||
edges: true,
|
||||
@@ -286,7 +286,7 @@ export const chatReplySchema = z.object({
|
||||
.optional(),
|
||||
clientSideActions: z.array(clientSideActionSchema).optional(),
|
||||
sessionId: z.string().optional(),
|
||||
typebot: typebotSchema
|
||||
typebot: typebotSchema._def.schema
|
||||
.pick({ id: true, theme: true, settings: true })
|
||||
.optional(),
|
||||
resultId: z.string().optional(),
|
||||
|
||||
@@ -8,22 +8,30 @@ import {
|
||||
typebotSchema,
|
||||
} from './typebot'
|
||||
import { z } from 'zod'
|
||||
import { preprocessTypebot } from './typebot/helpers/preprocessTypebot'
|
||||
|
||||
export const publicTypebotSchema = z.object({
|
||||
id: z.string(),
|
||||
version: z.enum(['3', '4', '5']).nullable(),
|
||||
createdAt: z.date(),
|
||||
updatedAt: z.date(),
|
||||
typebotId: z.string(),
|
||||
groups: z.array(groupSchema),
|
||||
edges: z.array(edgeSchema),
|
||||
variables: z.array(variableSchema),
|
||||
theme: themeSchema,
|
||||
settings: settingsSchema,
|
||||
}) satisfies z.ZodType<PrismaPublicTypebot>
|
||||
export const publicTypebotSchema = z.preprocess(
|
||||
preprocessTypebot,
|
||||
z.object({
|
||||
id: z.string(),
|
||||
version: z.enum(['3', '4', '5']).nullable(),
|
||||
createdAt: z.date(),
|
||||
updatedAt: z.date(),
|
||||
typebotId: z.string(),
|
||||
groups: z.array(groupSchema),
|
||||
edges: z.array(edgeSchema),
|
||||
variables: z.array(variableSchema),
|
||||
theme: themeSchema,
|
||||
settings: settingsSchema,
|
||||
})
|
||||
) satisfies z.ZodType<PrismaPublicTypebot, z.ZodTypeDef, unknown>
|
||||
|
||||
const publicTypebotWithName = publicTypebotSchema.merge(
|
||||
typebotSchema.pick({ name: true, isArchived: true, isClosed: true })
|
||||
const publicTypebotWithName = publicTypebotSchema._def.schema.merge(
|
||||
typebotSchema._def.schema.pick({
|
||||
name: true,
|
||||
isArchived: true,
|
||||
isClosed: true,
|
||||
})
|
||||
)
|
||||
|
||||
export type PublicTypebot = z.infer<typeof publicTypebotSchema>
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
import { Block } from '../../blocks'
|
||||
import { Group, edgeSchema } from '../typebot'
|
||||
|
||||
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
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
const preprocessGroup = (group: Group) => ({
|
||||
...group,
|
||||
blocks: group.blocks.map((block) =>
|
||||
preprocessBlock(block, { groupId: group.id })
|
||||
),
|
||||
})
|
||||
|
||||
const preprocessBlock = (block: Block, { groupId }: { groupId: string }) => ({
|
||||
...block,
|
||||
groupId: block.groupId ?? groupId,
|
||||
})
|
||||
@@ -4,6 +4,7 @@ import { themeSchema } from './theme'
|
||||
import { variableSchema } from './variable'
|
||||
import { Typebot as TypebotPrisma } from '@typebot.io/prisma'
|
||||
import { blockSchema } from '../blocks/schemas'
|
||||
import { preprocessTypebot } from './helpers/preprocessTypebot'
|
||||
|
||||
export const groupSchema = z.object({
|
||||
id: z.string(),
|
||||
@@ -43,35 +44,38 @@ const isDomainNameWithPathNameCompatible = (str: string) =>
|
||||
str
|
||||
)
|
||||
|
||||
export const typebotSchema = z.object({
|
||||
version: z.enum(['3', '4', '5']).nullable(),
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
groups: z.array(groupSchema),
|
||||
edges: z.array(edgeSchema),
|
||||
variables: z.array(variableSchema),
|
||||
theme: themeSchema,
|
||||
selectedThemeTemplateId: z.string().nullable(),
|
||||
settings: settingsSchema,
|
||||
createdAt: z.date(),
|
||||
updatedAt: z.date(),
|
||||
icon: z.string().nullable(),
|
||||
folderId: z.string().nullable(),
|
||||
publicId: z
|
||||
.string()
|
||||
.refine((str) => /^[a-zA-Z0-9-.]+$/.test(str))
|
||||
.nullable(),
|
||||
customDomain: z
|
||||
.string()
|
||||
.refine(isDomainNameWithPathNameCompatible)
|
||||
.nullable(),
|
||||
workspaceId: z.string(),
|
||||
resultsTablePreferences: resultsTablePreferencesSchema.nullable(),
|
||||
isArchived: z.boolean(),
|
||||
isClosed: z.boolean(),
|
||||
}) satisfies z.ZodType<TypebotPrisma>
|
||||
export const typebotSchema = z.preprocess(
|
||||
preprocessTypebot,
|
||||
z.object({
|
||||
version: z.enum(['3', '4', '5']).nullable(),
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
groups: z.array(groupSchema),
|
||||
edges: z.array(edgeSchema),
|
||||
variables: z.array(variableSchema),
|
||||
theme: themeSchema,
|
||||
selectedThemeTemplateId: z.string().nullable(),
|
||||
settings: settingsSchema,
|
||||
createdAt: z.date(),
|
||||
updatedAt: z.date(),
|
||||
icon: z.string().nullable(),
|
||||
folderId: z.string().nullable(),
|
||||
publicId: z
|
||||
.string()
|
||||
.refine((str) => /^[a-zA-Z0-9-.]+$/.test(str))
|
||||
.nullable(),
|
||||
customDomain: z
|
||||
.string()
|
||||
.refine(isDomainNameWithPathNameCompatible)
|
||||
.nullable(),
|
||||
workspaceId: z.string(),
|
||||
resultsTablePreferences: resultsTablePreferencesSchema.nullable(),
|
||||
isArchived: z.boolean(),
|
||||
isClosed: z.boolean(),
|
||||
}) satisfies z.ZodType<TypebotPrisma, z.ZodTypeDef, unknown>
|
||||
)
|
||||
|
||||
export const typebotCreateSchema = typebotSchema
|
||||
export const typebotCreateSchema = typebotSchema._def.schema
|
||||
.pick({
|
||||
name: true,
|
||||
icon: true,
|
||||
|
||||
Reference in New Issue
Block a user