2
0

🐛 Fix delete session with client side actions

This commit is contained in:
Baptiste Arnaud
2023-08-29 12:19:50 +02:00
parent b852b4af0b
commit 013c7a6265
10 changed files with 55 additions and 26 deletions

View File

@@ -17,6 +17,9 @@ export const sendWhatsAppInitialMessage = authenticatedProcedure
async ({ input: { to, typebotId, startGroupId }, ctx: { user } }) => { async ({ input: { to, typebotId, startGroupId }, ctx: { user } }) => {
const apiToken = await prisma.apiToken.findFirst({ const apiToken = await prisma.apiToken.findFirst({
where: { ownerId: user.id }, where: { ownerId: user.id },
select: {
token: true,
},
}) })
if (!apiToken) if (!apiToken)
throw new TRPCError({ throw new TRPCError({

View File

@@ -10,7 +10,7 @@ import {
Text, Text,
} from '@chakra-ui/react' } from '@chakra-ui/react'
import { runtimes } from '../data' import { runtimes } from '../data'
import { getFeatureFlags } from '@/features/telemetry/posthog' import { isWhatsAppAvailable } from '@/features/telemetry/posthog'
type Runtime = (typeof runtimes)[number] type Runtime = (typeof runtimes)[number]
@@ -38,9 +38,7 @@ export const RuntimeMenu = ({ selectedRuntime, onSelectRuntime }: Props) => {
{runtimes {runtimes
.filter((runtime) => runtime.name !== selectedRuntime.name) .filter((runtime) => runtime.name !== selectedRuntime.name)
.filter((runtime) => .filter((runtime) =>
runtime.name === 'WhatsApp' runtime.name === 'WhatsApp' ? isWhatsAppAvailable() : true
? getFeatureFlags().includes('whatsApp')
: true
) )
.map((runtime) => ( .map((runtime) => (
<MenuItem <MenuItem

View File

@@ -42,7 +42,7 @@ import { NextjsModal } from './modals/Nextjs/NextjsModal'
import { WhatsAppLogo } from '@/components/logos/WhatsAppLogo' import { WhatsAppLogo } from '@/components/logos/WhatsAppLogo'
import { WhatsAppModal } from './modals/WhatsAppModal/WhatsAppModal' import { WhatsAppModal } from './modals/WhatsAppModal/WhatsAppModal'
import { ParentModalProvider } from '@/features/graph/providers/ParentModalProvider' import { ParentModalProvider } from '@/features/graph/providers/ParentModalProvider'
import { getFeatureFlags } from '@/features/telemetry/posthog' import { isWhatsAppAvailable } from '@/features/telemetry/posthog'
export type ModalProps = { export type ModalProps = {
publicId: string publicId: string
@@ -84,7 +84,7 @@ export const EmbedButton = ({
export const integrationsList = [ export const integrationsList = [
(props: Pick<ModalProps, 'publicId' | 'isPublished'>) => { (props: Pick<ModalProps, 'publicId' | 'isPublished'>) => {
if (getFeatureFlags().includes('whatsApp')) if (isWhatsAppAvailable())
return ( return (
<ParentModalProvider> <ParentModalProvider>
<EmbedButton <EmbedButton

View File

@@ -24,11 +24,13 @@ export const identifyUser = (userId: string) => {
posthog.identify(userId) posthog.identify(userId)
} }
export const getFeatureFlags = () => { export const isWhatsAppAvailable = () => {
return posthog.__loaded && if (!env.NEXT_PUBLIC_POSTHOG_KEY || !posthog) return true
posthog.isFeatureEnabled('whatsApp', { send_event: false }) const isWhatsAppEnabled = posthog.getFeatureFlag('whatsApp', {
? ['whatsApp'] send_event: false,
: [] })
if (isWhatsAppEnabled === undefined) return true
return posthog.__loaded && isWhatsAppEnabled
} }
export { posthog } export { posthog }

View File

@@ -9,6 +9,7 @@ import {
chatReplySchema, chatReplySchema,
sendMessageInputSchema, sendMessageInputSchema,
} from '@typebot.io/schemas/features/chat/schema' } from '@typebot.io/schemas/features/chat/schema'
import { TRPCError } from '@trpc/server'
export const sendMessage = publicProcedure export const sendMessage = publicProcedure
.meta({ .meta({
@@ -30,6 +31,11 @@ export const sendMessage = publicProcedure
const session = sessionId ? await getSession(sessionId) : null const session = sessionId ? await getSession(sessionId) : null
if (!session) { if (!session) {
if (!startParams)
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'Missing startParams',
})
const { const {
typebot, typebot,
messages, messages,
@@ -39,7 +45,7 @@ export const sendMessage = publicProcedure
logs, logs,
clientSideActions, clientSideActions,
newSessionState, newSessionState,
} = await startSession(startParams, user?.id) } = await startSession({ startParams, userId: user?.id })
const allLogs = clientLogs ? [...(logs ?? []), ...clientLogs] : logs const allLogs = clientLogs ? [...(logs ?? []), ...clientLogs] : logs

View File

@@ -22,7 +22,10 @@ export const saveStateToDatabase = async ({
clientSideActions, clientSideActions,
}: Props) => { }: Props) => {
const containsSetVariableClientSideAction = clientSideActions?.some( const containsSetVariableClientSideAction = clientSideActions?.some(
(action) => 'setVariable' in action (action) =>
'setVariable' in action ||
'webhookToExecute' in action ||
'streamOpenAiChatCompletion' in action
) )
const isCompleted = Boolean(!input && !containsSetVariableClientSideAction) const isCompleted = Boolean(!input && !containsSetVariableClientSideAction)

View File

@@ -27,10 +27,15 @@ import parse, { NodeType } from 'node-html-parser'
import { parseDynamicTheme } from './parseDynamicTheme' import { parseDynamicTheme } from './parseDynamicTheme'
import { env } from '@typebot.io/env' import { env } from '@typebot.io/env'
export const startSession = async ( type Props = {
startParams?: StartParams, startParams: StartParams
userId?: string userId: string | undefined
): Promise<ChatReply & { newSessionState: SessionState }> => { }
export const startSession = async ({
startParams,
userId,
}: Props): Promise<ChatReply & { newSessionState: SessionState }> => {
if (!startParams) if (!startParams)
throw new TRPCError({ throw new TRPCError({
code: 'BAD_REQUEST', code: 'BAD_REQUEST',

View File

@@ -70,10 +70,13 @@ export const startWhatsAppPreview = publicProcedure
const { newSessionState, messages, input, clientSideActions, logs } = const { newSessionState, messages, input, clientSideActions, logs } =
await startSession({ await startSession({
startParams: {
isOnlyRegistering: !canSendDirectMessagesToUser, isOnlyRegistering: !canSendDirectMessagesToUser,
typebot: typebotId, typebot: typebotId,
isPreview: true, isPreview: true,
startGroupId, startGroupId,
},
userId: user.id,
}) })
if (canSendDirectMessagesToUser) { if (canSendDirectMessagesToUser) {

View File

@@ -84,7 +84,10 @@ export const startWhatsAppSession = async ({
if (credentials.phoneNumberId !== phoneNumberId) return if (credentials.phoneNumberId !== phoneNumberId) return
const session = await startSession({ const session = await startSession({
startParams: {
typebot: publicTypebot.typebot.publicId as string, typebot: publicTypebot.typebot.publicId as string,
},
userId: undefined,
}) })
return { return {

View File

@@ -21,12 +21,18 @@ const getAuthenticatedUser = async (
} }
const authenticateByToken = async ( const authenticateByToken = async (
apiToken: string token: string
): Promise<User | undefined> => { ): Promise<User | undefined> => {
if (typeof window !== 'undefined') return if (typeof window !== 'undefined') return
return (await prisma.user.findFirst({ const apiToken = await prisma.apiToken.findFirst({
where: { apiTokens: { some: { token: apiToken } } }, where: {
})) as User token,
},
select: {
owner: true,
},
})
return apiToken?.owner
} }
const extractBearerToken = (req: NextApiRequest) => const extractBearerToken = (req: NextApiRequest) =>