/** * @fileoverview React hook for managing all chat-related state. * Centralizes useState, useRef, and useMemo hooks for the chat system, * providing a clean separation between state management and side effects. * @module components/ResearchAgent/state/chat/useChatState */ import { ChatTurn, Message } from "../../components/ChatConversation/ChatWindow"; import { ChatFile, ChatModelProvider, Section } from "../../types/chat"; /** * The complete state object for the chat system. * Contains all values managed by useState hooks. */ export interface ChatState { /** Unique identifier for the current chat session */ chatId: string | undefined; /** Whether this is a newly created chat (not loaded from storage) */ newChatCreated: boolean; /** Whether a message is currently being sent/processed */ loading: boolean; /** Whether the AI response has started appearing (for loading indicators) */ messageAppeared: boolean; /** History of conversation as [role, content] pairs for context */ chatHistory: [string, string][]; /** All messages in the current chat */ messages: Message[]; /** Files attached to the chat */ files: ChatFile[]; /** IDs of attached files for API requests */ fileIds: string[]; /** Current search/focus mode (e.g., 'webSearch', 'academic') */ focusMode: string; /** Current category for search filtering */ category: string; /** Response optimization mode (e.g., 'speed', 'quality') */ optimizationMode: string; /** Whether messages have been loaded from storage/API */ isMessagesLoaded: boolean; /** Whether the requested chat was not found */ notFound: boolean; /** Current AI model provider configuration */ chatModelProvider: ChatModelProvider; /** Whether the model configuration has been loaded */ isConfigReady: boolean; /** Whether an error occurred during initialization */ hasError: boolean; /** Whether the chat system is fully ready for use */ isReady: boolean; } /** * Collection of setter functions for updating chat state. * Includes both simple setters and React dispatch functions for complex updates. */ export interface ChatStateSetters { /** Sets the chat ID */ setChatId: (id: string | undefined) => void; /** Sets whether a new chat was created */ setNewChatCreated: (created: boolean) => void; /** Sets the loading state */ setLoading: (loading: boolean) => void; /** Sets whether the message has appeared */ setMessageAppeared: (appeared: boolean) => void; /** Sets the chat history (supports functional updates) */ setChatHistory: React.Dispatch>; /** Sets the messages array (supports functional updates) */ setMessages: React.Dispatch>; /** Sets the attached files */ setFiles: (files: ChatFile[]) => void; /** Sets the file IDs */ setFileIds: (ids: string[]) => void; /** Sets the focus mode */ setFocusMode: (mode: string) => void; /** Sets the category */ setCategory: (category: string) => void; /** Sets the optimization mode */ setOptimizationMode: (mode: string) => void; /** Sets whether messages are loaded */ setIsMessagesLoaded: (loaded: boolean) => void; /** Sets the not found state */ setNotFound: (notFound: boolean) => void; /** Sets the chat model provider */ setChatModelProvider: (provider: ChatModelProvider) => void; /** Sets whether config is ready */ setIsConfigReady: (ready: boolean) => void; /** Sets the error state */ setHasError: (hasError: boolean) => void; /** Sets the ready state */ setIsReady: (ready: boolean) => void; } /** * Return type of the useChatState hook. */ export interface UseChatStateReturn { /** All state values */ state: ChatState; /** All setter functions */ setters: ChatStateSetters; /** Ref to current messages (for use in async callbacks) */ messagesRef: React.MutableRefObject; /** Filtered list of user and assistant messages only */ chatTurns: ChatTurn[]; /** Processed sections for UI rendering */ sections: Section[]; } /** * Custom hook that manages all state for the chat system. * * This hook centralizes all useState, useRef, and useMemo hooks used by the chat, * providing a clean separation between state management and side effects. * The ChatProvider uses this hook and adds useEffect hooks for side effects. * * @param initialChatId - Optional chat ID to initialize with (from URL params) * @returns Object containing state, setters, ref, and derived values * * @example * ```typescript * function ChatProvider({ children }) { * const { state, setters, messagesRef, chatTurns, sections } = useChatState( * params.chatId * ); * * // Add effects using state and setters... * * return {children}; * } * ``` */ export declare function useChatState(initialChatId?: string): UseChatStateReturn;