All files / src/stores chat.ts

98.87% Statements 88/89
100% Branches 27/27
92.3% Functions 24/26
98.87% Lines 88/89

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125            1x       1x 6x   6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x   1x   5x   5x   5x   5x 16x       16x 4x 4x   16x 16x   5x 23x 23x 23x     5x 3x   5x 2x   5x   5x 1x   5x 1x   5x 1x 1x 1x 1x 1x 1x 1x     5x   5x 1x   5x 1x   5x   5x     5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x   5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x  
import type { Message } from '@copilotkit/runtime-client-gql';
 
// Types
import { ChatState, ChatSlice } from '@/types';
 
// Utils
import { loadChatState, persistChatState } from '@/utils';
 
export type ChatHandler = (state: ChatState) => void;
 
export const createInitialChatState = (): ChatSlice => {
  const initialChatState = loadChatState();
 
  return {
    ...initialChatState,
    setChatMessages: (_chatMessages: Message[]) => {},
    setChatLoading: (_loading: boolean) => {},
    setFetched: (_fetched: boolean) => {},
    setChatInitialized: (_isChatInitialized: boolean) => {},
    setChatResetFlag: (_shouldResetChat: boolean) => {},
    resetChat: () => {},
    setInputValue: (_value: string) => {},
    resetChatInput: () => {},
    setTextareaCursorPosition: (_position: number) => {},
    setTextareaScrollTop: (_scrollTop: number) => {},
    setSuggestionsLoaded: (_loaded: boolean) => {},
  };
};
 
export const chatStore = (() => {
  // Initial placeholders for actions
  let state: ChatSlice = createInitialChatState();
 
  const listeners = new Set<ChatHandler>();
 
  const getState = () => state;
 
  const setState = (partial: Partial<ChatState>) => {
    state = { ...state, ...partial } as ChatSlice;
    // Always persist chat messages to localStorage
    // This ensures messages are saved even if token isn't available yet
    // Also persist when messages are cleared (empty array) to update storage
    if (partial.chatMessages !== undefined) {
      persistChatState(state);
    }
    // Always notify listeners so React components update
    listeners.forEach((listener) => listener(state));
  };
 
  const subscribe = (listener: ChatHandler) => {
    listeners.add(listener);
    return () => listeners.delete(listener);
  };
 
  // Chat actions
  const setChatMessages = (chatMessages: Message[]) =>
    setState({ chatMessages });
 
  const setChatLoading = (loading: boolean) =>
    setState({ isChatLoading: loading });
 
  const setFetched = (fetched: boolean) => setState({ isFetched: fetched });
 
  const setChatInitialized = (isChatInitialized: boolean) =>
    setState({ isChatInitialized });
 
  const setChatResetFlag = (shouldResetChat: boolean) =>
    setState({ shouldResetChat });
 
  const resetChat = () =>
    setState({
      chatMessages: [],
      isChatLoading: false,
      isFetched: false,
      isChatInitialized: false,
      inputValue: '',
    });
 
  // Chat input actions
  const setInputValue = (value: string) => setState({ inputValue: value });
 
  const setTextareaCursorPosition = (value: number) =>
    setState({ textareaCursorPosition: value });
 
  const setTextareaScrollTop = (value: number) =>
    setState({ textareaScrollTop: value });
 
  const resetChatInput = () => setState({ inputValue: '' });
 
  const setSuggestionsLoaded = (loaded: boolean) =>
    setState({ hasLoadedSuggestions: loaded });
 
  state = {
    ...state,
    setChatMessages,
    setChatLoading,
    setFetched,
    setChatInitialized,
    setChatResetFlag,
    resetChat,
    setInputValue,
    resetChatInput,
    setTextareaCursorPosition,
    setTextareaScrollTop,
    setSuggestionsLoaded,
  };
 
  return {
    getState,
    setState,
    subscribe,
    setChatMessages,
    setChatLoading,
    setFetched,
    setChatInitialized,
    setChatResetFlag,
    resetChat,
    setInputValue,
    resetChatInput,
    setTextareaCursorPosition,
    setTextareaScrollTop,
    setSuggestionsLoaded,
  };
})();