2023-03-15 12:21:52 +01:00
|
|
|
import { ExecuteLogicResponse } from '@/features/chat/types'
|
|
|
|
|
import { extractVariablesFromText } from '@/features/variables/extractVariablesFromText'
|
|
|
|
|
import { parseGuessedValueType } from '@/features/variables/parseGuessedValueType'
|
|
|
|
|
import { parseVariables } from '@/features/variables/parseVariables'
|
2023-03-15 08:35:16 +01:00
|
|
|
import { ScriptBlock, SessionState } from '@typebot.io/schemas'
|
2022-11-29 10:02:40 +01:00
|
|
|
|
2023-01-27 15:58:05 +01:00
|
|
|
export const executeScript = (
|
2022-11-29 10:02:40 +01:00
|
|
|
{ typebot: { variables } }: SessionState,
|
2023-01-27 15:58:05 +01:00
|
|
|
block: ScriptBlock,
|
2023-01-27 10:54:59 +01:00
|
|
|
lastBubbleBlockId?: string
|
2022-11-29 10:02:40 +01:00
|
|
|
): ExecuteLogicResponse => {
|
|
|
|
|
if (!block.options.content) return { outgoingEdgeId: block.outgoingEdgeId }
|
|
|
|
|
|
|
|
|
|
const content = parseVariables(variables, { fieldToParse: 'id' })(
|
|
|
|
|
block.options.content
|
|
|
|
|
)
|
|
|
|
|
const args = extractVariablesFromText(variables)(block.options.content).map(
|
|
|
|
|
(variable) => ({
|
|
|
|
|
id: variable.id,
|
2023-03-15 12:21:52 +01:00
|
|
|
value: parseGuessedValueType(variable.value),
|
2022-11-29 10:02:40 +01:00
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
outgoingEdgeId: block.outgoingEdgeId,
|
2023-01-26 15:26:42 +01:00
|
|
|
clientSideActions: [
|
|
|
|
|
{
|
2023-01-27 15:58:05 +01:00
|
|
|
scriptToExecute: {
|
2023-01-26 15:26:42 +01:00
|
|
|
content,
|
|
|
|
|
args,
|
|
|
|
|
},
|
2023-01-27 10:54:59 +01:00
|
|
|
lastBubbleBlockId,
|
2022-11-29 10:02:40 +01:00
|
|
|
},
|
2023-01-26 15:26:42 +01:00
|
|
|
],
|
2022-11-29 10:02:40 +01:00
|
|
|
}
|
|
|
|
|
}
|