* Class to manage the state and operations of the chat
| 35 | * Class to manage the state and operations of the chat |
| 36 | */ |
| 37 | class ChatManager { |
| 38 | |
| 39 | private state: ChatState; // State of the chat |
| 40 | private static instance: ChatManager | null = null; // Singleton instance of the ChatManager |
| 41 | |
| 42 | // Private constructor for singleton pattern |
| 43 | private constructor(setChatMessages: (messages: any[]) => void, setStatusMessage: (message: string) => void, setProgress: (progress: number) => void, setIsLoadingFirstMessage: (isLoading: boolean) => void) { |
| 44 | // Initialize the state |
| 45 | this.state = { |
| 46 | assistantId: null, |
| 47 | threadId: null, |
| 48 | messages: [], |
| 49 | isLoading: false, |
| 50 | error: null, |
| 51 | runId: null, |
| 52 | assistantResponseReceived: false, |
| 53 | isSending: false, |
| 54 | setChatMessages: setChatMessages, |
| 55 | setStatusMessage: setStatusMessage, |
| 56 | statusMessage: '', |
| 57 | setProgress: setProgress, |
| 58 | setIsLoadingFirstMessage: setIsLoadingFirstMessage, |
| 59 | }; |
| 60 | console.log('ChatManager initialized'); |
| 61 | } |
| 62 | |
| 63 | // Method to get the current messages |
| 64 | public getCurrentMessages(): any[] { |
| 65 | return this.state.messages; |
| 66 | } |
| 67 | |
| 68 | // Method to get the singleton instance of the ChatManager |
| 69 | public static getInstance(setChatMessages: (messages: any[]) => void, setStatusMessage: (message: string) => void, setProgress: (progress: number) => void, setIsLoadingFirstMessage: (isLoading: boolean) => void): ChatManager { // Add setIsLoadingFirstMessage here |
| 70 | if (this.instance === null) { |
| 71 | this.instance = new ChatManager(setChatMessages, setStatusMessage, setProgress, setIsLoadingFirstMessage); // And here |
| 72 | } |
| 73 | return this.instance; |
| 74 | } |
| 75 | |
| 76 | // Method to start the assistant |
| 77 | async startAssistant(assistantDetails: any, fileIds: string[], initialMessage: string): Promise<void> { |
| 78 | console.log('Starting assistant...'); |
| 79 | |
| 80 | this.state.setStatusMessage('Initializing chat assistant...'); |
| 81 | this.state.isLoading = true; |
| 82 | |
| 83 | try { |
| 84 | |
| 85 | |
| 86 | // Initialize the assistant |
| 87 | this.state.setStatusMessage('Create Assistant...'); |
| 88 | const assistantId = await initializeAssistant(assistantDetails, fileIds); |
| 89 | if (assistantId === null) { |
| 90 | throw new Error('AssistantId is null'); |
| 91 | } |
| 92 | this.state.setStatusMessage('Assistant created...'); |
| 93 | |
| 94 | // Create the chat thread |
nothing calls this directly
no outgoing calls
no test coverage detected