2023-09-20 15:26:52 +02:00
|
|
|
import { ExecuteLogicResponse } from '../../../types'
|
2023-04-14 12:11:42 +02:00
|
|
|
import { ScriptBlock, SessionState, Variable } from '@typebot.io/schemas'
|
2023-12-13 10:22:02 +01:00
|
|
|
import { extractVariablesFromText } from '@typebot.io/variables/extractVariablesFromText'
|
|
|
|
|
import { parseGuessedValueType } from '@typebot.io/variables/parseGuessedValueType'
|
|
|
|
|
import { parseVariables } from '@typebot.io/variables/parseVariables'
|
2024-01-22 09:22:28 +01:00
|
|
|
import { defaultScriptOptions } from '@typebot.io/schemas/features/blocks/logic/script/constants'
|
|
|
|
|
import { executeFunction } from '@typebot.io/variables/executeFunction'
|
|
|
|
|
import { updateVariablesInSession } from '@typebot.io/variables/updateVariablesInSession'
|
2022-11-29 10:02:40 +01:00
|
|
|
|
2024-01-22 09:22:28 +01:00
|
|
|
export const executeScript = async (
|
2023-08-24 07:48:30 +02:00
|
|
|
state: SessionState,
|
2023-05-26 09:20:22 +02:00
|
|
|
block: ScriptBlock
|
2024-01-22 09:22:28 +01:00
|
|
|
): Promise<ExecuteLogicResponse> => {
|
2023-08-24 07:48:30 +02:00
|
|
|
const { variables } = state.typebotsQueue[0].typebot
|
2023-11-08 15:34:16 +01:00
|
|
|
if (!block.options?.content || state.whatsApp)
|
2023-08-29 10:01:28 +02:00
|
|
|
return { outgoingEdgeId: block.outgoingEdgeId }
|
2022-11-29 10:02:40 +01:00
|
|
|
|
2024-01-22 09:22:28 +01:00
|
|
|
const isExecutedOnClient =
|
|
|
|
|
block.options.isExecutedOnClient ?? defaultScriptOptions.isExecutedOnClient
|
|
|
|
|
|
|
|
|
|
if (!isExecutedOnClient) {
|
|
|
|
|
const { newVariables, error } = await executeFunction({
|
|
|
|
|
variables,
|
|
|
|
|
body: block.options.content,
|
|
|
|
|
})
|
|
|
|
|
|
2024-05-15 14:24:55 +02:00
|
|
|
const updateVarResults = newVariables
|
|
|
|
|
? updateVariablesInSession({
|
|
|
|
|
newVariables,
|
|
|
|
|
state,
|
|
|
|
|
currentBlockId: block.id,
|
|
|
|
|
})
|
|
|
|
|
: undefined
|
|
|
|
|
|
|
|
|
|
let newSessionState = state
|
|
|
|
|
|
|
|
|
|
if (updateVarResults) {
|
|
|
|
|
newSessionState = updateVarResults.updatedState
|
|
|
|
|
}
|
2024-01-22 09:22:28 +01:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
outgoingEdgeId: block.outgoingEdgeId,
|
|
|
|
|
logs: error ? [{ status: 'error', description: error }] : [],
|
|
|
|
|
newSessionState,
|
2024-05-15 14:24:55 +02:00
|
|
|
newSetVariableHistory: updateVarResults?.newSetVariableHistory,
|
2024-01-22 09:22:28 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-14 12:11:42 +02:00
|
|
|
const scriptToExecute = parseScriptToExecuteClientSideAction(
|
|
|
|
|
variables,
|
2022-11-29 10:02:40 +01:00
|
|
|
block.options.content
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
outgoingEdgeId: block.outgoingEdgeId,
|
2023-01-26 15:26:42 +01:00
|
|
|
clientSideActions: [
|
|
|
|
|
{
|
2023-12-22 09:13:53 +01:00
|
|
|
type: 'scriptToExecute',
|
2023-04-14 12:11:42 +02:00
|
|
|
scriptToExecute: scriptToExecute,
|
2022-11-29 10:02:40 +01:00
|
|
|
},
|
2023-01-26 15:26:42 +01:00
|
|
|
],
|
2022-11-29 10:02:40 +01:00
|
|
|
}
|
|
|
|
|
}
|
2023-04-14 12:11:42 +02:00
|
|
|
|
|
|
|
|
export const parseScriptToExecuteClientSideAction = (
|
|
|
|
|
variables: Variable[],
|
|
|
|
|
contentToEvaluate: string
|
|
|
|
|
) => {
|
|
|
|
|
const content = parseVariables(variables, { fieldToParse: 'id' })(
|
|
|
|
|
contentToEvaluate
|
|
|
|
|
)
|
|
|
|
|
const args = extractVariablesFromText(variables)(contentToEvaluate).map(
|
|
|
|
|
(variable) => ({
|
|
|
|
|
id: variable.id,
|
|
|
|
|
value: parseGuessedValueType(variable.value),
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
return {
|
|
|
|
|
content,
|
|
|
|
|
args,
|
|
|
|
|
}
|
|
|
|
|
}
|