diff --git a/packages/bot-engine/src/blocks/inputs/payment/computePaymentInputRuntimeOptions.ts b/packages/bot-engine/src/blocks/inputs/payment/computePaymentInputRuntimeOptions.ts index ecb864c1d..3f1583693 100644 --- a/packages/bot-engine/src/blocks/inputs/payment/computePaymentInputRuntimeOptions.ts +++ b/packages/bot-engine/src/blocks/inputs/payment/computePaymentInputRuntimeOptions.ts @@ -4,37 +4,55 @@ import type { PaymentInputBlock, PaymentInputRuntimeOptions, } from "@typebot.io/blocks-inputs/payment/schema"; -import type { SessionState } from "@typebot.io/chat-session/schemas"; import { decrypt } from "@typebot.io/credentials/decrypt"; import { getCredentials } from "@typebot.io/credentials/getCredentials"; import type { StripeCredentials } from "@typebot.io/credentials/schemas"; import type { SessionStore } from "@typebot.io/runtime-session-store"; import { parseVariables } from "@typebot.io/variables/parseVariables"; +import type { Variable } from "@typebot.io/variables/schemas"; import Stripe from "stripe"; export const computePaymentInputRuntimeOptions = ( options: PaymentInputBlock["options"], - { sessionStore, state }: { sessionStore: SessionStore; state: SessionState }, -) => createStripePaymentIntent(options, { sessionStore, state }); + { + sessionStore, + variables, + isPreview, + workspaceId, + }: { + sessionStore: SessionStore; + variables: Variable[]; + isPreview: boolean; + workspaceId: string; + }, +) => + createStripePaymentIntent(options, { + sessionStore, + variables, + isPreview, + workspaceId, + }); const createStripePaymentIntent = async ( options: PaymentInputBlock["options"], - { sessionStore, state }: { sessionStore: SessionStore; state: SessionState }, + { + sessionStore, + variables, + isPreview, + workspaceId, + }: { + sessionStore: SessionStore; + variables: Variable[]; + isPreview: boolean; + workspaceId: string; + }, ): Promise => { - const { - resultId, - typebot: { variables }, - } = state.typebotsQueue[0]; - const isPreview = !resultId; if (!options?.credentialsId) throw new TRPCError({ code: "BAD_REQUEST", message: "Missing credentialsId", }); - const stripeKeys = await getStripeInfo( - options.credentialsId, - state.workspaceId, - ); + const stripeKeys = await getStripeInfo(options.credentialsId, workspaceId); if (!stripeKeys) throw new TRPCError({ code: "NOT_FOUND", diff --git a/packages/bot-engine/src/continueBotFlow.ts b/packages/bot-engine/src/continueBotFlow.ts index 62aa57442..898997647 100644 --- a/packages/bot-engine/src/continueBotFlow.ts +++ b/packages/bot-engine/src/continueBotFlow.ts @@ -27,7 +27,7 @@ import type { ForgedBlock } from "@typebot.io/forge-repository/schemas"; import { getBlockById } from "@typebot.io/groups/helpers/getBlockById"; import type { Group } from "@typebot.io/groups/schemas"; import { parseUnknownError } from "@typebot.io/lib/parseUnknownError"; -import { byId, isDefined } from "@typebot.io/lib/utils"; +import { byId, isDefined, isNotDefined } from "@typebot.io/lib/utils"; import type { AnswerInSessionState } from "@typebot.io/results/schemas/answers"; import type { SessionStore } from "@typebot.io/runtime-session-store"; import { defaultSystemMessages } from "@typebot.io/settings/constants"; @@ -624,7 +624,12 @@ const parseRetryMessage = async ( }, }, ], - input: await formatInputForChatResponse(block, { state, sessionStore }), + input: await formatInputForChatResponse(block, { + variables: state.typebotsQueue[0].typebot.variables, + isPreview: isNotDefined(state.typebotsQueue[0].resultId), + workspaceId: state.workspaceId, + sessionStore, + }), }; }; diff --git a/packages/bot-engine/src/formatInputForChatResponse.ts b/packages/bot-engine/src/formatInputForChatResponse.ts index 81f67d1f2..da14e4aea 100644 --- a/packages/bot-engine/src/formatInputForChatResponse.ts +++ b/packages/bot-engine/src/formatInputForChatResponse.ts @@ -4,9 +4,9 @@ import type { ContinueChatResponse, RuntimeOptions, } from "@typebot.io/chat-api/schemas"; -import type { SessionState } from "@typebot.io/chat-session/schemas"; import type { SessionStore } from "@typebot.io/runtime-session-store"; import { deepParseVariables } from "@typebot.io/variables/deepParseVariables"; +import type { Variable } from "@typebot.io/variables/schemas"; import { injectVariableValuesInCardsBlock } from "./blocks/cards/injectVariableValuesInCardsBlock"; import { injectVariableValuesInButtonsInputBlock } from "./blocks/inputs/buttons/injectVariableValuesInButtonsInputBlock"; import { parseDateInput } from "./blocks/inputs/date/parseDateInput"; @@ -16,18 +16,28 @@ import { getPrefilledInputValue } from "./getPrefilledValue"; export const formatInputForChatResponse = async ( block: InputBlock, - { state, sessionStore }: { state: SessionState; sessionStore: SessionStore }, + { + variables, + sessionStore, + isPreview, + workspaceId, + }: { + variables: Variable[]; + sessionStore: SessionStore; + isPreview: boolean; + workspaceId: string; + }, ): Promise => { switch (block.type) { case InputBlockType.CHOICE: { return injectVariableValuesInButtonsInputBlock(block, { - state, + variables, sessionStore, }); } case InputBlockType.PICTURE_CHOICE: { return injectVariableValuesInPictureChoiceBlock(block, { - variables: state.typebotsQueue[0].typebot.variables, + variables, sessionStore, }); } @@ -35,19 +45,17 @@ export const formatInputForChatResponse = async ( return deepParseVariables( { ...block, - prefilledValue: getPrefilledInputValue( - state.typebotsQueue[0].typebot.variables, - )(block), + prefilledValue: getPrefilledInputValue(variables)(block), }, { - variables: state.typebotsQueue[0].typebot.variables, + variables, sessionStore, }, ); } case InputBlockType.DATE: { return parseDateInput(block, { - variables: state.typebotsQueue[0].typebot.variables, + variables, sessionStore, }); } @@ -55,19 +63,17 @@ export const formatInputForChatResponse = async ( return deepParseVariables( { ...block, - prefilledValue: getPrefilledInputValue( - state.typebotsQueue[0].typebot.variables, - )(block), + prefilledValue: getPrefilledInputValue(variables)(block), }, { - variables: state.typebotsQueue[0].typebot.variables, + variables, sessionStore, }, ); } case InputBlockType.CARDS: { return injectVariableValuesInCardsBlock(block, { - variables: state.typebotsQueue[0].typebot.variables, + variables, sessionStore, }); } @@ -77,14 +83,14 @@ export const formatInputForChatResponse = async ( ...block, runtimeOptions: await computeRuntimeOptions(block, { sessionStore, - state, + variables, + isPreview, + workspaceId, }), - prefilledValue: getPrefilledInputValue( - state.typebotsQueue[0].typebot.variables, - )(block), + prefilledValue: getPrefilledInputValue(variables)(block), }, { - variables: state.typebotsQueue[0].typebot.variables, + variables, sessionStore, }, ); @@ -94,13 +100,25 @@ export const formatInputForChatResponse = async ( const computeRuntimeOptions = ( block: InputBlock, - { sessionStore, state }: { sessionStore: SessionStore; state: SessionState }, + { + sessionStore, + variables, + isPreview, + workspaceId, + }: { + sessionStore: SessionStore; + variables: Variable[]; + isPreview: boolean; + workspaceId: string; + }, ): Promise | undefined => { switch (block.type) { case InputBlockType.PAYMENT: { return computePaymentInputRuntimeOptions(block.options, { sessionStore, - state, + variables, + isPreview, + workspaceId, }); } } diff --git a/packages/bot-engine/src/walkFlowForward.ts b/packages/bot-engine/src/walkFlowForward.ts index 7b51ee1de..589a1dac4 100644 --- a/packages/bot-engine/src/walkFlowForward.ts +++ b/packages/bot-engine/src/walkFlowForward.ts @@ -246,7 +246,9 @@ const executeGroup = async ( return { messages, input: await formatInputForChatResponse(block, { - state: newSessionState, + variables: newSessionState.typebotsQueue[0].typebot.variables, + isPreview: isNotDefined(newSessionState.typebotsQueue[0].resultId), + workspaceId: newSessionState.workspaceId, sessionStore, }), newSessionState: {