Skip to content

Commit

Permalink
♻️ Include autoContinueChat function in startBotFlow
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Dec 12, 2024
1 parent 4d7423e commit 16b7776
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 80 deletions.
22 changes: 6 additions & 16 deletions packages/bot-engine/src/apiHandlers/continueChat.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { TRPCError } from "@trpc/server";
import { BubbleBlockType } from "@typebot.io/blocks-bubbles/constants";
import { isDefined, isNotDefined } from "@typebot.io/lib/utils";
import { autoContinueChatIfStartingWithInput } from "../autoContinueChatIfStartingWithInput";
import { computeCurrentProgress } from "../computeCurrentProgress";
import { continueBotFlow } from "../continueBotFlow";
import { filterPotentiallySensitiveLogs } from "../logs/filterPotentiallySensitiveLogs";
Expand Down Expand Up @@ -53,13 +52,6 @@ export const continueChat = async ({
else corsOrigin = session.state.allowedOrigins[0];
}

const chatReply = await continueBotFlow(message, {
version: 2,
state: session.state,
startTime: Date.now(),
textBubbleContentFormat,
});

const {
messages,
input,
Expand All @@ -69,14 +61,12 @@ export const continueChat = async ({
lastMessageNewFormat,
visitedEdges,
setVariableHistory,
} = !session.state.currentBlockId
? await autoContinueChatIfStartingWithInput({
message,
chatReply,
textBubbleContentFormat,
version: 2,
})
: chatReply;
} = await continueBotFlow(message, {
version: 2,
state: session.state,
startTime: Date.now(),
textBubbleContentFormat,
});

if (newSessionState)
await saveStateToDatabase({
Expand Down
43 changes: 0 additions & 43 deletions packages/bot-engine/src/autoContinueChatIfStartingWithInput.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/bot-engine/src/continueBotFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export const continueBotFlow = async (
resetVariablesGlobals();
if (!state.currentBlockId)
return startBotFlow({
message: reply,
state: resetSessionState(state),
version,
textBubbleContentFormat,
Expand Down
61 changes: 52 additions & 9 deletions packages/bot-engine/src/startBotFlow.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import { TRPCError } from "@trpc/server";
import type { Prisma } from "@typebot.io/prisma/types";
import type { SetVariableHistoryItem } from "@typebot.io/variables/schemas";
import { continueBotFlow } from "./continueBotFlow";
import { executeGroup } from "./executeGroup";
import { getFirstEdgeId } from "./getFirstEdgeId";
import { getNextGroup } from "./getNextGroup";
import { resetGlobals } from "./globals";
import type { ContinueChatResponse, StartFrom } from "./schemas/api";
import { upsertResult } from "./queries/upsertResult";
import type { ContinueChatResponse, Message, StartFrom } from "./schemas/api";
import type { SessionState } from "./schemas/chatSession";

type ChatReply = ContinueChatResponse & {
newSessionState: SessionState;
visitedEdges: Prisma.VisitedEdge[];
setVariableHistory: SetVariableHistoryItem[];
};

type Props = {
version: 1 | 2;
message: Message | undefined;
state: SessionState;
startFrom?: StartFrom;
startTime?: number;
Expand All @@ -18,17 +27,12 @@ type Props = {

export const startBotFlow = async ({
version,
message,
state,
startFrom,
startTime,
textBubbleContentFormat,
}: Props): Promise<
ContinueChatResponse & {
newSessionState: SessionState;
visitedEdges: Prisma.VisitedEdge[];
setVariableHistory: SetVariableHistoryItem[];
}
> => {
}: Props): Promise<ChatReply> => {
resetGlobals();
let newSessionState = state;
const visitedEdges: Prisma.VisitedEdge[] = [];
Expand Down Expand Up @@ -70,12 +74,51 @@ export const startBotFlow = async ({
newSessionState = nextGroup.newSessionState;
if (!nextGroup.group)
return { messages: [], newSessionState, visitedEdges, setVariableHistory };
return executeGroup(nextGroup.group, {
const chatReply = await executeGroup(nextGroup.group, {
version,
state: newSessionState,
visitedEdges,
setVariableHistory,
startTime,
textBubbleContentFormat,
});

return autoContinueChatIfStartingWithInput({
message,
state: newSessionState,
chatReply,
textBubbleContentFormat,
version,
});
};

const autoContinueChatIfStartingWithInput = async ({
version,
message,
chatReply,
textBubbleContentFormat,
}: Props & { chatReply: ChatReply }): Promise<ChatReply> => {
console.log("HEY", message, chatReply);
if (
!message ||
chatReply.messages.length > 0 ||
(chatReply.clientSideActions?.filter((c) => c.expectsDedicatedReply)
.length ?? 0) > 0
)
return chatReply;

const resultId = chatReply.newSessionState.typebotsQueue[0].resultId;
if (resultId)
await upsertResult({
hasStarted: true,
isCompleted: false,
resultId,
typebot: chatReply.newSessionState.typebotsQueue[0].typebot,
});
return continueBotFlow(message, {
version,
state: chatReply.newSessionState,
textBubbleContentFormat: textBubbleContentFormat,
startTime: Date.now(),
});
};
17 changes: 5 additions & 12 deletions packages/bot-engine/src/startSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import {
} from "@typebot.io/variables/schemas";
import { z } from "@typebot.io/zod";
import { NodeType, parse } from "node-html-parser";
import { autoContinueChatIfStartingWithInput } from "./autoContinueChatIfStartingWithInput";
import { parseVariablesInRichText } from "./parseBubbleBlock";
import { parseDynamicTheme } from "./parseDynamicTheme";
import { findPublicTypebot } from "./queries/findPublicTypebot";
Expand Down Expand Up @@ -172,15 +171,6 @@ export const startSession = async ({
};
}

const chatReply = await startBotFlow({
version,
state: initialState,
startFrom:
startParams.type === "preview" ? startParams.startFrom : undefined,
startTime: Date.now(),
textBubbleContentFormat: startParams.textBubbleContentFormat,
});

const {
messages,
input,
Expand All @@ -189,10 +179,13 @@ export const startSession = async ({
logs,
visitedEdges,
setVariableHistory,
} = await autoContinueChatIfStartingWithInput({
} = await startBotFlow({
version,
message: startParams.message,
chatReply,
state: initialState,
startFrom:
startParams.type === "preview" ? startParams.startFrom : undefined,
startTime: Date.now(),
textBubbleContentFormat: startParams.textBubbleContentFormat,
});

Expand Down

0 comments on commit 16b7776

Please sign in to comment.