22
packages/embeds/js/src/queries/continueChatQuery.ts
Normal file
22
packages/embeds/js/src/queries/continueChatQuery.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { guessApiHost } from '@/utils/guessApiHost'
|
||||
import { isNotEmpty, sendRequest } from '@typebot.io/lib'
|
||||
import { ContinueChatResponse } from '@typebot.io/schemas'
|
||||
|
||||
export const continueChatQuery = ({
|
||||
apiHost,
|
||||
message,
|
||||
sessionId,
|
||||
}: {
|
||||
apiHost?: string
|
||||
message: string | undefined
|
||||
sessionId: string
|
||||
}) =>
|
||||
sendRequest<ContinueChatResponse>({
|
||||
method: 'POST',
|
||||
url: `${
|
||||
isNotEmpty(apiHost) ? apiHost : guessApiHost()
|
||||
}/api/v1/sessions/${sessionId}/continueChat`,
|
||||
body: {
|
||||
message,
|
||||
},
|
||||
})
|
||||
@@ -1,74 +0,0 @@
|
||||
import { BotContext, InitialChatReply } from '@/types'
|
||||
import { guessApiHost } from '@/utils/guessApiHost'
|
||||
import type {
|
||||
SendMessageInput,
|
||||
StartElementId,
|
||||
StartParams,
|
||||
} from '@typebot.io/schemas'
|
||||
import { isNotDefined, isNotEmpty, sendRequest } from '@typebot.io/lib'
|
||||
import {
|
||||
getPaymentInProgressInStorage,
|
||||
removePaymentInProgressFromStorage,
|
||||
} from '@/features/blocks/inputs/payment/helpers/paymentInProgressStorage'
|
||||
|
||||
export async function getInitialChatReplyQuery({
|
||||
typebot,
|
||||
isPreview,
|
||||
apiHost,
|
||||
prefilledVariables,
|
||||
resultId,
|
||||
stripeRedirectStatus,
|
||||
...props
|
||||
}: StartParams & {
|
||||
stripeRedirectStatus?: string
|
||||
apiHost?: string
|
||||
} & StartElementId) {
|
||||
if (isNotDefined(typebot))
|
||||
throw new Error('Typebot ID is required to get initial messages')
|
||||
|
||||
const paymentInProgressStateStr = getPaymentInProgressInStorage() ?? undefined
|
||||
const paymentInProgressState = paymentInProgressStateStr
|
||||
? (JSON.parse(paymentInProgressStateStr) as {
|
||||
sessionId: string
|
||||
typebot: BotContext['typebot']
|
||||
})
|
||||
: undefined
|
||||
if (paymentInProgressState) removePaymentInProgressFromStorage()
|
||||
const { data, error } = await sendRequest<InitialChatReply>({
|
||||
method: 'POST',
|
||||
url: `${isNotEmpty(apiHost) ? apiHost : guessApiHost()}/api/v2/sendMessage`,
|
||||
body: {
|
||||
startParams: paymentInProgressState
|
||||
? undefined
|
||||
: {
|
||||
isPreview,
|
||||
typebot,
|
||||
prefilledVariables,
|
||||
resultId,
|
||||
isStreamEnabled: true,
|
||||
startGroupId:
|
||||
'startGroupId' in props ? props.startGroupId : undefined,
|
||||
startEventId:
|
||||
'startEventId' in props ? props.startEventId : undefined,
|
||||
},
|
||||
sessionId: paymentInProgressState?.sessionId,
|
||||
message: paymentInProgressState
|
||||
? stripeRedirectStatus === 'failed'
|
||||
? 'fail'
|
||||
: 'Success'
|
||||
: undefined,
|
||||
} satisfies SendMessageInput,
|
||||
})
|
||||
|
||||
return {
|
||||
data: data
|
||||
? {
|
||||
...data,
|
||||
...(paymentInProgressState
|
||||
? { typebot: paymentInProgressState.typebot }
|
||||
: {}),
|
||||
}
|
||||
: undefined,
|
||||
error,
|
||||
}
|
||||
}
|
||||
22
packages/embeds/js/src/queries/saveClientLogsQuery.ts
Normal file
22
packages/embeds/js/src/queries/saveClientLogsQuery.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { guessApiHost } from '@/utils/guessApiHost'
|
||||
import type { ChatLog } from '@typebot.io/schemas'
|
||||
import { isNotEmpty, sendRequest } from '@typebot.io/lib'
|
||||
|
||||
export const saveClientLogsQuery = ({
|
||||
apiHost,
|
||||
sessionId,
|
||||
clientLogs,
|
||||
}: {
|
||||
apiHost?: string
|
||||
sessionId: string
|
||||
clientLogs: ChatLog[]
|
||||
}) =>
|
||||
sendRequest({
|
||||
method: 'POST',
|
||||
url: `${
|
||||
isNotEmpty(apiHost) ? apiHost : guessApiHost()
|
||||
}/api/v1/sessions/${sessionId}/clientLogs`,
|
||||
body: {
|
||||
clientLogs,
|
||||
},
|
||||
})
|
||||
@@ -1,13 +0,0 @@
|
||||
import { guessApiHost } from '@/utils/guessApiHost'
|
||||
import type { ChatReply, SendMessageInput } from '@typebot.io/schemas'
|
||||
import { isNotEmpty, sendRequest } from '@typebot.io/lib'
|
||||
|
||||
export const sendMessageQuery = ({
|
||||
apiHost,
|
||||
...body
|
||||
}: SendMessageInput & { apiHost?: string }) =>
|
||||
sendRequest<ChatReply>({
|
||||
method: 'POST',
|
||||
url: `${isNotEmpty(apiHost) ? apiHost : guessApiHost()}/api/v2/sendMessage`,
|
||||
body,
|
||||
})
|
||||
104
packages/embeds/js/src/queries/startChatQuery.ts
Normal file
104
packages/embeds/js/src/queries/startChatQuery.ts
Normal file
@@ -0,0 +1,104 @@
|
||||
import { BotContext, InitialChatReply } from '@/types'
|
||||
import { guessApiHost } from '@/utils/guessApiHost'
|
||||
import { isNotDefined, isNotEmpty, sendRequest } from '@typebot.io/lib'
|
||||
import {
|
||||
getPaymentInProgressInStorage,
|
||||
removePaymentInProgressFromStorage,
|
||||
} from '@/features/blocks/inputs/payment/helpers/paymentInProgressStorage'
|
||||
import {
|
||||
StartChatInput,
|
||||
StartFrom,
|
||||
StartPreviewChatInput,
|
||||
} from '@typebot.io/schemas'
|
||||
|
||||
export async function startChatQuery({
|
||||
typebot,
|
||||
isPreview,
|
||||
apiHost,
|
||||
prefilledVariables,
|
||||
resultId,
|
||||
stripeRedirectStatus,
|
||||
startFrom,
|
||||
}: {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
typebot: string | any
|
||||
stripeRedirectStatus?: string
|
||||
apiHost?: string
|
||||
startFrom?: StartFrom
|
||||
isPreview: boolean
|
||||
prefilledVariables?: Record<string, unknown>
|
||||
resultId?: string
|
||||
}) {
|
||||
if (isNotDefined(typebot))
|
||||
throw new Error('Typebot ID is required to get initial messages')
|
||||
|
||||
const paymentInProgressStateStr = getPaymentInProgressInStorage() ?? undefined
|
||||
const paymentInProgressState = paymentInProgressStateStr
|
||||
? (JSON.parse(paymentInProgressStateStr) as {
|
||||
sessionId: string
|
||||
typebot: BotContext['typebot']
|
||||
})
|
||||
: undefined
|
||||
if (paymentInProgressState) {
|
||||
removePaymentInProgressFromStorage()
|
||||
const { data, error } = await sendRequest<InitialChatReply>({
|
||||
method: 'POST',
|
||||
url: `${isNotEmpty(apiHost) ? apiHost : guessApiHost()}/api/v1/sessions/${
|
||||
paymentInProgressState.sessionId
|
||||
}/continueChat`,
|
||||
body: {
|
||||
message: paymentInProgressState
|
||||
? stripeRedirectStatus === 'failed'
|
||||
? 'fail'
|
||||
: 'Success'
|
||||
: undefined,
|
||||
},
|
||||
})
|
||||
return {
|
||||
data: data
|
||||
? {
|
||||
...data,
|
||||
...(paymentInProgressState
|
||||
? { typebot: paymentInProgressState.typebot }
|
||||
: {}),
|
||||
}
|
||||
: undefined,
|
||||
error,
|
||||
}
|
||||
}
|
||||
const typebotId = typeof typebot === 'string' ? typebot : typebot.id
|
||||
if (isPreview) {
|
||||
const { data, error } = await sendRequest<InitialChatReply>({
|
||||
method: 'POST',
|
||||
url: `${
|
||||
isNotEmpty(apiHost) ? apiHost : guessApiHost()
|
||||
}/api/v1/typebots/${typebotId}/preview/startChat`,
|
||||
body: {
|
||||
isStreamEnabled: true,
|
||||
startFrom,
|
||||
typebot,
|
||||
} satisfies Omit<StartPreviewChatInput, 'typebotId'>,
|
||||
})
|
||||
return {
|
||||
data,
|
||||
error,
|
||||
}
|
||||
}
|
||||
|
||||
const { data, error } = await sendRequest<InitialChatReply>({
|
||||
method: 'POST',
|
||||
url: `${
|
||||
isNotEmpty(apiHost) ? apiHost : guessApiHost()
|
||||
}/api/v1/typebots/${typebotId}/startChat`,
|
||||
body: {
|
||||
isStreamEnabled: true,
|
||||
prefilledVariables,
|
||||
resultId,
|
||||
} satisfies Omit<StartChatInput, 'publicId'>,
|
||||
})
|
||||
|
||||
return {
|
||||
data,
|
||||
error,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user