import { Dispatch, SetStateAction, MutableRefObject } from "react"; import { ChatState } from "~/types/bot"; import { Message, ConversationStep } from "~/types/chat"; import { clearSessionStorage, clearLoadedFromStorageFlag, WidgetType, } from "~/utils/session-storage"; import { addBreadcrumb } from "~/utils/sentry"; /** * Creates a function to reset the thread ID * @param setChatState - State setter for chat state * @param widgetType - Type of widget * @returns Function to reset thread ID */ export function createResetThreadId( setChatState: Dispatch>, widgetType: WidgetType ) { return () => { setChatState((prev) => ({ ...prev, threadId: null, })); clearLoadedFromStorageFlag(widgetType); }; } /** * Creates a function to start a new conversation * @param currentAbortController - Ref to current abort controller * @param widgetType - Type of widget * @param setMessages - State setter for messages * @param setCurrentStep - State setter for conversation step * @param setIsInputDisabled - State setter for input disabled flag * @param setInputState - State setter for input state * @param setChatState - State setter for chat state * @param setShouldTrackSession - State setter for session tracking flag * @returns Function to start new conversation */ export function createStartNewConversation( currentAbortController: MutableRefObject, widgetType: WidgetType, setMessages: Dispatch>, setCurrentStep: Dispatch>, setIsInputDisabled: Dispatch>, setInputState: Dispatch< SetStateAction<"starting" | "generating" | "minimized"> >, setChatState: Dispatch>, setShouldTrackSession: Dispatch> ) { return () => { if (currentAbortController.current) { currentAbortController.current.abort(); currentAbortController.current = null; } addBreadcrumb( "New conversation started", "user-interaction.conversation_reset", "info" ); clearSessionStorage(widgetType); setMessages([]); setCurrentStep("welcome"); setIsInputDisabled(false); setInputState("starting"); setChatState({ repliedProduct: null, threadId: null, isRequestInProgress: false, }); setShouldTrackSession(true); }; }