@ -1 +0,0 @@
|
||||
export { executeCode } from './utils/executeCode'
|
@ -2,10 +2,10 @@ import { parseVariables, parseCorrectValueType } from '@/features/variables'
|
||||
import { LogicState } from '@/types'
|
||||
import { sendEventToParent } from '@/utils/chat'
|
||||
import { isEmbedded } from '@/utils/helpers'
|
||||
import { CodeBlock } from 'models'
|
||||
import { ScriptBlock } from 'models'
|
||||
|
||||
export const executeCode = async (
|
||||
block: CodeBlock,
|
||||
export const executeScript = async (
|
||||
block: ScriptBlock,
|
||||
{ typebot: { variables } }: LogicState
|
||||
) => {
|
||||
if (!block.options.content) return
|
@ -1,5 +1,4 @@
|
||||
import { TypebotViewerProps } from '@/components/TypebotViewer'
|
||||
import { executeCode } from '@/features/blocks/logic/code'
|
||||
import { executeCondition } from '@/features/blocks/logic/condition'
|
||||
import { executeRedirect } from '@/features/blocks/logic/redirect'
|
||||
import { executeSetVariable } from '@/features/blocks/logic/setVariable'
|
||||
@ -8,6 +7,7 @@ import { executeWait } from '@/features/blocks/logic/wait'
|
||||
import { LinkedTypebot } from '@/providers/TypebotProvider'
|
||||
import { EdgeId, LogicState } from '@/types'
|
||||
import { LogicBlock, LogicBlockType } from 'models'
|
||||
import { executeScript } from '@/features/blocks/logic/script/executeScript'
|
||||
|
||||
export const executeLogic = async (
|
||||
block: LogicBlock,
|
||||
@ -23,8 +23,8 @@ export const executeLogic = async (
|
||||
return { nextEdgeId: executeCondition(block, context) }
|
||||
case LogicBlockType.REDIRECT:
|
||||
return { nextEdgeId: executeRedirect(block, context) }
|
||||
case LogicBlockType.CODE:
|
||||
return { nextEdgeId: await executeCode(block, context) }
|
||||
case LogicBlockType.SCRIPT:
|
||||
return { nextEdgeId: await executeScript(block, context) }
|
||||
case LogicBlockType.TYPEBOT_LINK:
|
||||
return executeTypebotLink(block, context)
|
||||
case LogicBlockType.WAIT:
|
||||
|
@ -1,6 +1,8 @@
|
||||
import { executeCode } from '@/features/blocks/logic/code'
|
||||
import type { CodeToExecute } from 'models'
|
||||
import { executeScript } from '@/features/blocks/logic/script/executeScript'
|
||||
import type { ScriptToExecute } from 'models'
|
||||
|
||||
export const executeChatwoot = (chatwoot: { codeToExecute: CodeToExecute }) => {
|
||||
executeCode(chatwoot.codeToExecute)
|
||||
export const executeChatwoot = (chatwoot: {
|
||||
scriptToExecute: ScriptToExecute
|
||||
}) => {
|
||||
executeScript(chatwoot.scriptToExecute)
|
||||
}
|
||||
|
@ -1 +0,0 @@
|
||||
export * from './utils'
|
@ -1 +0,0 @@
|
||||
export * from './executeCode'
|
@ -1,6 +1,6 @@
|
||||
import type { CodeToExecute } from 'models'
|
||||
import type { ScriptToExecute } from 'models'
|
||||
|
||||
export const executeCode = async ({ content, args }: CodeToExecute) => {
|
||||
export const executeScript = async ({ content, args }: ScriptToExecute) => {
|
||||
const func = Function(...args.map((arg) => arg.id), content)
|
||||
try {
|
||||
await func(...args.map((arg) => arg.value))
|
@ -1,7 +1,7 @@
|
||||
import { executeChatwoot } from '@/features/blocks/integrations/chatwoot'
|
||||
import { executeGoogleAnalyticsBlock } from '@/features/blocks/integrations/googleAnalytics/utils/executeGoogleAnalytics'
|
||||
import { executeCode } from '@/features/blocks/logic/code'
|
||||
import { executeRedirect } from '@/features/blocks/logic/redirect'
|
||||
import { executeScript } from '@/features/blocks/logic/script/executeScript'
|
||||
import { executeWait } from '@/features/blocks/logic/wait/utils/executeWait'
|
||||
import type { ChatReply } from 'models'
|
||||
|
||||
@ -14,8 +14,8 @@ export const executeClientSideAction = async (
|
||||
if ('googleAnalytics' in clientSideAction) {
|
||||
executeGoogleAnalyticsBlock(clientSideAction.googleAnalytics)
|
||||
}
|
||||
if ('codeToExecute' in clientSideAction) {
|
||||
await executeCode(clientSideAction.codeToExecute)
|
||||
if ('scriptToExecute' in clientSideAction) {
|
||||
await executeScript(clientSideAction.scriptToExecute)
|
||||
}
|
||||
if ('redirect' in clientSideAction) {
|
||||
executeRedirect(clientSideAction.redirect)
|
||||
|
@ -1,21 +0,0 @@
|
||||
import { z } from 'zod'
|
||||
import { blockBaseSchema } from '../baseSchemas'
|
||||
import { LogicBlockType } from './enums'
|
||||
|
||||
export const codeOptionsSchema = z.object({
|
||||
name: z.string(),
|
||||
content: z.string().optional(),
|
||||
shouldExecuteInParentContext: z.boolean().optional(),
|
||||
})
|
||||
|
||||
export const codeBlockSchema = blockBaseSchema.and(
|
||||
z.object({
|
||||
type: z.enum([LogicBlockType.CODE]),
|
||||
options: codeOptionsSchema,
|
||||
})
|
||||
)
|
||||
|
||||
export const defaultCodeOptions: CodeOptions = { name: 'Code snippet' }
|
||||
|
||||
export type CodeBlock = z.infer<typeof codeBlockSchema>
|
||||
export type CodeOptions = z.infer<typeof codeOptionsSchema>
|
@ -2,7 +2,7 @@ export enum LogicBlockType {
|
||||
SET_VARIABLE = 'Set variable',
|
||||
CONDITION = 'Condition',
|
||||
REDIRECT = 'Redirect',
|
||||
CODE = 'Code',
|
||||
SCRIPT = 'Code',
|
||||
TYPEBOT_LINK = 'Typebot link',
|
||||
WAIT = 'Wait',
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
export * from './code'
|
||||
export * from './script'
|
||||
export * from './condition'
|
||||
export * from './enums'
|
||||
export * from './logicBlock'
|
||||
|
@ -1,18 +1,18 @@
|
||||
import { z } from 'zod'
|
||||
import { codeOptionsSchema, codeBlockSchema } from './code'
|
||||
import { scriptOptionsSchema, scriptBlockSchema } from './script'
|
||||
import { conditionBlockSchema } from './condition'
|
||||
import { redirectOptionsSchema, redirectBlockSchema } from './redirect'
|
||||
import { setVariableOptionsSchema, setVariableBlockSchema } from './setVariable'
|
||||
import { typebotLinkOptionsSchema, typebotLinkBlockSchema } from './typebotLink'
|
||||
import { waitBlockSchema, waitOptionsSchema } from './wait'
|
||||
|
||||
const logicBlockOptionsSchema = codeOptionsSchema
|
||||
const logicBlockOptionsSchema = scriptOptionsSchema
|
||||
.or(redirectOptionsSchema)
|
||||
.or(setVariableOptionsSchema)
|
||||
.or(typebotLinkOptionsSchema)
|
||||
.or(waitOptionsSchema)
|
||||
|
||||
export const logicBlockSchema = codeBlockSchema
|
||||
export const logicBlockSchema = scriptBlockSchema
|
||||
.or(conditionBlockSchema)
|
||||
.or(redirectBlockSchema)
|
||||
.or(typebotLinkBlockSchema)
|
||||
|
21
packages/models/features/blocks/logic/script.ts
Normal file
21
packages/models/features/blocks/logic/script.ts
Normal file
@ -0,0 +1,21 @@
|
||||
import { z } from 'zod'
|
||||
import { blockBaseSchema } from '../baseSchemas'
|
||||
import { LogicBlockType } from './enums'
|
||||
|
||||
export const scriptOptionsSchema = z.object({
|
||||
name: z.string(),
|
||||
content: z.string().optional(),
|
||||
shouldExecuteInParentContext: z.boolean().optional(),
|
||||
})
|
||||
|
||||
export const scriptBlockSchema = blockBaseSchema.and(
|
||||
z.object({
|
||||
type: z.enum([LogicBlockType.SCRIPT]),
|
||||
options: scriptOptionsSchema,
|
||||
})
|
||||
)
|
||||
|
||||
export const defaultScriptOptions: ScriptOptions = { name: 'Script' }
|
||||
|
||||
export type ScriptBlock = z.infer<typeof scriptBlockSchema>
|
||||
export type ScriptOptions = z.infer<typeof scriptOptionsSchema>
|
@ -97,7 +97,7 @@ const chatMessageSchema = z
|
||||
.or(embedMessageSchema)
|
||||
)
|
||||
|
||||
const codeToExecuteSchema = z.object({
|
||||
const scriptToExecuteSchema = z.object({
|
||||
content: z.string(),
|
||||
args: z.array(
|
||||
z.object({
|
||||
@ -176,7 +176,7 @@ const clientSideActionSchema = z
|
||||
.and(
|
||||
z
|
||||
.object({
|
||||
codeToExecute: codeToExecuteSchema,
|
||||
scriptToExecute: scriptToExecuteSchema,
|
||||
})
|
||||
.or(
|
||||
z.object({
|
||||
@ -185,7 +185,7 @@ const clientSideActionSchema = z
|
||||
)
|
||||
.or(
|
||||
z.object({
|
||||
chatwoot: z.object({ codeToExecute: codeToExecuteSchema }),
|
||||
chatwoot: z.object({ scriptToExecute: scriptToExecuteSchema }),
|
||||
})
|
||||
)
|
||||
.or(
|
||||
@ -228,7 +228,7 @@ export type TypebotInSession = z.infer<typeof typebotInSessionStateSchema>
|
||||
export type ChatReply = z.infer<typeof chatReplySchema>
|
||||
export type ChatMessage = z.infer<typeof chatMessageSchema>
|
||||
export type SendMessageInput = z.infer<typeof sendMessageInputSchema>
|
||||
export type CodeToExecute = z.infer<typeof codeToExecuteSchema>
|
||||
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>
|
||||
|
Reference in New Issue
Block a user