🐛 Refactor edge ID retrieval logic in computeResultTranscript to handle cards input and block items more effectively

This commit is contained in:
Baptiste Arnaud 2025-08-28 11:02:00 +02:00
parent d68104d28d
commit c8cfa6a66e
No known key found for this signature in database

View File

@ -1,9 +1,11 @@
import { BubbleBlockType } from "@typebot.io/blocks-bubbles/constants";
import { isBubbleBlock, isInputBlock } from "@typebot.io/blocks-core/helpers";
import { defaultChoiceInputOptions } from "@typebot.io/blocks-inputs/choice/constants";
import {
blockHasItems,
isBubbleBlock,
isCardsInput,
isInputBlock,
} from "@typebot.io/blocks-core/helpers";
import { InputBlockType } from "@typebot.io/blocks-inputs/constants";
import { defaultPictureChoiceOptions } from "@typebot.io/blocks-inputs/pictureChoice/constants";
import type { InputBlock } from "@typebot.io/blocks-inputs/schema";
import { LogicBlockType } from "@typebot.io/blocks-logic/constants";
import type { ContinueChatResponse } from "@typebot.io/chat-api/schemas";
import type { TypebotInSession } from "@typebot.io/chat-session/schemas";
@ -13,10 +15,8 @@ import { createId } from "@typebot.io/lib/createId";
import type { Answer } from "@typebot.io/results/schemas/answers";
import type { SessionStore } from "@typebot.io/runtime-session-store";
import type { Edge } from "@typebot.io/typebot/schemas/edge";
import { parseVariables } from "@typebot.io/variables/parseVariables";
import type { Variable } from "@typebot.io/variables/schemas";
import type { SetVariableHistoryItem } from "@typebot.io/variables/schemas";
import { executeAbTest } from "./blocks/logic/abTest/executeAbTest";
import { isTypebotInSessionAtLeastV6 } from "./helpers/isTypebotInSessionAtLeastV6";
import {
type BubbleBlockWithDefinedContent,
@ -250,13 +250,33 @@ const executeGroup = ({
: answer.content,
});
const outgoing = getOutgoingEdgeId(block, {
answer: answer.content,
variables: typebot.variables,
sessionStore,
});
if (outgoing.isOffDefaultPath) visitedEdges.next();
nextEdgeId = outgoing.edgeId;
const nextVisitedEdge = visitedEdges.peek();
// Check if the next visited edge matches an non default outgoing edge
if (nextVisitedEdge) {
if (isCardsInput(block)) {
for (const item of block.items) {
if (!item.paths) continue;
for (const path of item.paths) {
if (path.outgoingEdgeId === nextVisitedEdge) {
nextEdgeId = path.outgoingEdgeId;
visitedEdges.next();
break;
}
}
}
}
if (blockHasItems(block)) {
for (const item of block.items) {
if (item.outgoingEdgeId !== nextVisitedEdge) continue;
nextEdgeId = item.outgoingEdgeId;
visitedEdges.next();
break;
}
}
}
if (!nextEdgeId && block.outgoingEdgeId)
nextEdgeId = block.outgoingEdgeId;
}
// ──────────────────────────────────────────────────────────── Condition
else if (block.type === LogicBlockType.CONDITION) {
@ -426,52 +446,3 @@ const convertChatMessageToTranscriptMessage = (
}
}
};
const getOutgoingEdgeId = (
block: InputBlock,
{
answer,
variables,
sessionStore,
}: {
answer: string | undefined;
variables: Variable[];
sessionStore: SessionStore;
},
): { edgeId: string | undefined; isOffDefaultPath: boolean } => {
if (
block.type === InputBlockType.CHOICE &&
!(
block.options?.isMultipleChoice ??
defaultChoiceInputOptions.isMultipleChoice
) &&
answer
) {
const matchedItem = block.items.find(
(item) =>
parseVariables(item.content, {
variables,
sessionStore,
}).normalize() === answer.normalize(),
);
if (matchedItem?.outgoingEdgeId)
return { edgeId: matchedItem.outgoingEdgeId, isOffDefaultPath: true };
}
if (
block.type === InputBlockType.PICTURE_CHOICE &&
!(
block.options?.isMultipleChoice ??
defaultPictureChoiceOptions.isMultipleChoice
) &&
answer
) {
const matchedItem = block.items.find(
(item) =>
parseVariables(item.title, { variables, sessionStore }).normalize() ===
answer.normalize(),
);
if (matchedItem?.outgoingEdgeId)
return { edgeId: matchedItem.outgoingEdgeId, isOffDefaultPath: true };
}
return { edgeId: block.outgoingEdgeId, isOffDefaultPath: false };
};