2
0

(setVariable) Add Transcription system var (#1507)

Closes #1484
This commit is contained in:
Baptiste Arnaud
2024-05-15 14:24:55 +02:00
committed by GitHub
parent ec7ff8d9ca
commit 40f21203b5
102 changed files with 2911 additions and 986 deletions

View File

@ -4,6 +4,7 @@ import {
InputBlock,
RuntimeOptions,
SessionState,
SetVariableHistoryItem,
} from '@typebot.io/schemas'
import { isNotEmpty } from '@typebot.io/lib'
import {
@ -21,16 +22,16 @@ import { injectVariableValuesInPictureChoiceBlock } from './blocks/inputs/pictur
import { getPrefilledInputValue } from './getPrefilledValue'
import { parseDateInput } from './blocks/inputs/date/parseDateInput'
import { deepParseVariables } from '@typebot.io/variables/deepParseVariables'
import {
BubbleBlockWithDefinedContent,
parseBubbleBlock,
} from './parseBubbleBlock'
import { InputBlockType } from '@typebot.io/schemas/features/blocks/inputs/constants'
import { VisitedEdge } from '@typebot.io/prisma'
import { env } from '@typebot.io/env'
import { TRPCError } from '@trpc/server'
import { ExecuteIntegrationResponse, ExecuteLogicResponse } from './types'
import { createId } from '@paralleldrive/cuid2'
import {
BubbleBlockWithDefinedContent,
parseBubbleBlock,
} from './parseBubbleBlock'
type ContextProps = {
version: 1 | 2
@ -39,6 +40,7 @@ type ContextProps = {
currentLastBubbleId?: string
firstBubbleWasStreamed?: boolean
visitedEdges: VisitedEdge[]
setVariableHistory: SetVariableHistoryItem[]
startTime?: number
}
@ -48,6 +50,7 @@ export const executeGroup = async (
version,
state,
visitedEdges,
setVariableHistory,
currentReply,
currentLastBubbleId,
firstBubbleWasStreamed,
@ -56,6 +59,7 @@ export const executeGroup = async (
): Promise<
ContinueChatResponse & {
newSessionState: SessionState
setVariableHistory: SetVariableHistoryItem[]
visitedEdges: VisitedEdge[]
}
> => {
@ -70,6 +74,7 @@ export const executeGroup = async (
let newSessionState = state
let isNextEdgeOffDefaultPath = false
let index = -1
for (const block of group.blocks) {
if (
@ -110,6 +115,7 @@ export const executeGroup = async (
clientSideActions,
logs,
visitedEdges,
setVariableHistory,
}
const executionResponse = (
isLogicBlock(block)
@ -120,6 +126,29 @@ export const executeGroup = async (
) as ExecuteLogicResponse | ExecuteIntegrationResponse | null
if (!executionResponse) continue
if (
executionResponse.newSetVariableHistory &&
executionResponse.newSetVariableHistory?.length > 0
) {
if (!newSessionState.typebotsQueue[0].resultId)
newSessionState = {
...newSessionState,
previewMetadata: {
...newSessionState.previewMetadata,
setVariableHistory: (
newSessionState.previewMetadata?.setVariableHistory ?? []
).concat(
executionResponse.newSetVariableHistory.map((item) => ({
blockId: item.blockId,
variableId: item.variableId,
value: item.value,
}))
),
},
}
else setVariableHistory.push(...executionResponse.newSetVariableHistory)
}
if (
'startTimeShouldBeUpdated' in executionResponse &&
executionResponse.startTimeShouldBeUpdated
@ -165,33 +194,55 @@ export const executeGroup = async (
clientSideActions,
logs,
visitedEdges,
setVariableHistory,
}
}
}
if (executionResponse.outgoingEdgeId) {
isNextEdgeOffDefaultPath =
block.outgoingEdgeId !== executionResponse.outgoingEdgeId
nextEdgeId = executionResponse.outgoingEdgeId
break
}
}
if (!nextEdgeId && newSessionState.typebotsQueue.length === 1)
return { messages, newSessionState, clientSideActions, logs, visitedEdges }
return {
messages,
newSessionState,
clientSideActions,
logs,
visitedEdges,
setVariableHistory,
}
const nextGroup = await getNextGroup(newSessionState)(nextEdgeId ?? undefined)
const nextGroup = await getNextGroup({
state: newSessionState,
edgeId: nextEdgeId ?? undefined,
isOffDefaultPath: isNextEdgeOffDefaultPath,
})
newSessionState = nextGroup.newSessionState
if (nextGroup.visitedEdge) visitedEdges.push(nextGroup.visitedEdge)
if (!nextGroup.group) {
return { messages, newSessionState, clientSideActions, logs, visitedEdges }
return {
messages,
newSessionState,
clientSideActions,
logs,
visitedEdges,
setVariableHistory,
}
}
return executeGroup(nextGroup.group, {
version,
state: newSessionState,
visitedEdges,
setVariableHistory,
currentReply: {
messages,
clientSideActions,