import { BotContext } from '@/types' import { guessApiHost } from '@/utils/guessApiHost' import { isNotDefined, isNotEmpty } from '@indite.io/lib' import { getPaymentInProgressInStorage, removePaymentInProgressFromStorage, } from '@/features/blocks/inputs/payment/helpers/paymentInProgressStorage' import { ContinueChatResponse, StartChatInput, StartChatResponse, StartFrom, StartPreviewChatInput, } from '@indite.io/schemas' import ky from 'ky' import { CorsError } from '@/utils/CorsError' type Props = { // eslint-disable-next-line @typescript-eslint/no-explicit-any bot: string | any stripeRedirectStatus?: string apiHost?: string source?: string startFrom?: StartFrom isPreview: boolean prefilledVariables?: Record resultId?: string sessionId?: string } export async function startChatQuery({ bot, isPreview, apiHost, source, prefilledVariables, resultId, stripeRedirectStatus, startFrom, sessionId, }: Props) { if (isNotDefined(bot)) throw new Error('Bot ID is required to get initial messages') console.log('source-start', source) const paymentInProgressStateStr = getPaymentInProgressInStorage() ?? undefined const paymentInProgressState = paymentInProgressStateStr ? (JSON.parse(paymentInProgressStateStr) as { sessionId: string bot: BotContext['bot'] }) : undefined if (paymentInProgressState) { removePaymentInProgressFromStorage() try { const data = await ky .post( `${isNotEmpty(apiHost) ? apiHost : guessApiHost()}/api/v1/sessions/${ paymentInProgressState.sessionId }/continueChat`, { json: { message: paymentInProgressState ? stripeRedirectStatus === 'failed' ? 'fail' : 'Success' : undefined, }, timeout: false, } ) .json() return { data: { ...data, ...paymentInProgressState, } satisfies StartChatResponse, } } catch (error) { return { error } } } const botId = typeof bot === 'string' ? bot : bot.id if (isPreview) { try { const data = await ky .post( `${ isNotEmpty(apiHost) ? apiHost : guessApiHost() }/api/v1/bots/${botId}/preview/startChat`, { json: { isStreamEnabled: true, startFrom, bot, source, prefilledVariables, sessionId, } satisfies Omit< StartPreviewChatInput, 'botId' | 'isOnlyRegistering' | 'textBubbleContentFormat' >, timeout: false, } ) .json() return { data } } catch (error) { return { error } } } try { const iframeReferrerOrigin = parent !== window && isNotEmpty(document.referrer) ? new URL(document.referrer).origin : undefined const response = await ky.post( `${ isNotEmpty(apiHost) ? apiHost : guessApiHost() }/api/v1/bots/${botId}/startChat`, { headers: { 'x-bot-iframe-referrer-origin': iframeReferrerOrigin, }, json: { isStreamEnabled: true, prefilledVariables, resultId, source: source ? source : 'web', isOnlyRegistering: false, } satisfies Omit< StartChatInput, 'publicId' | 'textBubbleContentFormat' >, timeout: false, } ) const corsAllowOrigin = response.headers.get('access-control-allow-origin') if ( iframeReferrerOrigin && corsAllowOrigin && corsAllowOrigin !== '*' && !iframeReferrerOrigin.includes(corsAllowOrigin) ) throw new CorsError(corsAllowOrigin) return { data: await response.json() } } catch (error) { return { error } } }