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

@@ -11,6 +11,7 @@ import {
SessionState,
TypebotInSession,
Block,
SetVariableHistoryItem,
} from '@typebot.io/schemas'
import {
StartChatInput,
@@ -31,7 +32,10 @@ import { injectVariablesFromExistingResult } from '@typebot.io/variables/injectV
import { getNextGroup } from './getNextGroup'
import { upsertResult } from './queries/upsertResult'
import { continueBotFlow } from './continueBotFlow'
import { parseVariables } from '@typebot.io/variables/parseVariables'
import {
getVariablesToParseInfoInText,
parseVariables,
} from '@typebot.io/variables/parseVariables'
import { defaultSettings } from '@typebot.io/schemas/features/typebot/settings/constants'
import { IntegrationBlockType } from '@typebot.io/schemas/features/blocks/integrations/constants'
import { VisitedEdge } from '@typebot.io/prisma'
@@ -42,6 +46,9 @@ import {
defaultGuestAvatarIsEnabled,
defaultHostAvatarIsEnabled,
} from '@typebot.io/schemas/features/typebot/theme/constants'
import { BubbleBlockType } from '@typebot.io/schemas/features/blocks/bubbles/constants'
import { LogicBlockType } from '@typebot.io/schemas/features/blocks/logic/constants'
import { parseVariablesInRichText } from './parseBubbleBlock'
type StartParams =
| ({
@@ -68,6 +75,7 @@ export const startSession = async ({
Omit<StartChatResponse, 'resultId' | 'isStreamEnabled' | 'sessionId'> & {
newSessionState: SessionState
visitedEdges: VisitedEdge[]
setVariableHistory: SetVariableHistoryItem[]
resultId?: string
}
> => {
@@ -145,6 +153,8 @@ export const startSession = async ({
: typebot.theme.general?.progressBar?.isEnabled
? { totalAnswers: 0 }
: undefined,
setVariableIdsForHistory:
extractVariableIdsUsedForTranscript(typebotInSession),
...initialSessionState,
}
@@ -164,6 +174,7 @@ export const startSession = async ({
dynamicTheme: parseDynamicTheme(initialState),
messages: [],
visitedEdges: [],
setVariableHistory: [],
}
}
@@ -178,14 +189,18 @@ export const startSession = async ({
// If params has message and first block is an input block, we can directly continue the bot flow
if (message) {
const firstEdgeId = getFirstEdgeId({
state: chatReply.newSessionState,
typebot: chatReply.newSessionState.typebotsQueue[0].typebot,
startEventId:
startParams.type === 'preview' &&
startParams.startFrom?.type === 'event'
? startParams.startFrom.eventId
: undefined,
})
const nextGroup = await getNextGroup(chatReply.newSessionState)(firstEdgeId)
const nextGroup = await getNextGroup({
state: chatReply.newSessionState,
edgeId: firstEdgeId,
isOffDefaultPath: false,
})
const newSessionState = nextGroup.newSessionState
const firstBlock = nextGroup.group?.blocks.at(0)
if (firstBlock && isInputBlock(firstBlock)) {
@@ -214,6 +229,7 @@ export const startSession = async ({
newSessionState,
logs,
visitedEdges,
setVariableHistory,
} = chatReply
const clientSideActions = startFlowClientActions ?? []
@@ -268,6 +284,7 @@ export const startSession = async ({
dynamicTheme: parseDynamicTheme(newSessionState),
logs: startLogs.length > 0 ? startLogs : undefined,
visitedEdges,
setVariableHistory,
}
return {
@@ -290,6 +307,7 @@ export const startSession = async ({
dynamicTheme: parseDynamicTheme(newSessionState),
logs: startLogs.length > 0 ? startLogs : undefined,
visitedEdges,
setVariableHistory,
}
}
@@ -497,3 +515,59 @@ const convertStartTypebotToTypebotInSession = (
variables: startVariables,
events: typebot.events,
}
const extractVariableIdsUsedForTranscript = (
typebot: TypebotInSession
): string[] => {
const variableIds: Set<string> = new Set()
const parseVarParams = {
variables: typebot.variables,
takeLatestIfList: typebot.version !== '6',
}
typebot.groups.forEach((group) => {
group.blocks.forEach((block) => {
if (block.type === BubbleBlockType.TEXT) {
const { parsedVariableIds } = parseVariablesInRichText(
block.content?.richText ?? [],
parseVarParams
)
parsedVariableIds.forEach((variableId) => variableIds.add(variableId))
}
if (
block.type === BubbleBlockType.IMAGE ||
block.type === BubbleBlockType.VIDEO ||
block.type === BubbleBlockType.AUDIO
) {
if (!block.content?.url) return
const variablesInfo = getVariablesToParseInfoInText(
block.content.url,
parseVarParams
)
variablesInfo.forEach((variableInfo) =>
variableInfo.variableId
? variableIds.add(variableInfo.variableId ?? '')
: undefined
)
}
if (block.type === LogicBlockType.CONDITION) {
block.items.forEach((item) =>
item.content?.comparisons?.forEach((comparison) => {
if (comparison.variableId) variableIds.add(comparison.variableId)
if (comparison.value) {
const variableIdsInValue = getVariablesToParseInfoInText(
comparison.value,
parseVarParams
)
variableIdsInValue.forEach((variableInfo) => {
variableInfo.variableId
? variableIds.add(variableInfo.variableId)
: undefined
})
}
})
)
}
})
})
return [...variableIds]
}