diff --git a/codeaide/logic/chat_handler.py b/codeaide/logic/chat_handler.py index 2a305ed..36d54c8 100644 --- a/codeaide/logic/chat_handler.py +++ b/codeaide/logic/chat_handler.py @@ -575,3 +575,15 @@ def start_new_session(self, chat_window): logger.info(f"New session started with ID: {self.session_id}") logger.info(f"New session directory: {self.session_dir}") + + # New method to load a previous session + def load_previous_session(self, session_id, chat_window): + logger.info(f"Loading previous session: {session_id}") + self.session_id = session_id + self.file_handler = FileHandler(session_id=session_id) + self.session_dir = self.file_handler.session_dir + + # Load chat contents + chat_window.load_chat_contents() + + logger.info(f"Loaded previous session with ID: {self.session_id}") diff --git a/codeaide/ui/chat_window.py b/codeaide/ui/chat_window.py index f135a63..5ae5d00 100644 --- a/codeaide/ui/chat_window.py +++ b/codeaide/ui/chat_window.py @@ -51,6 +51,7 @@ def __init__(self, chat_handler): self.cost_tracker = getattr(chat_handler, "cost_tracker", None) self.code_popup = None self.waiting_for_api_key = False + self.chat_contents = [] self.setup_ui() # Check API key status @@ -202,7 +203,15 @@ def add_to_chat(self, sender, message): self.chat_display.append(html_message + "
") self.chat_display.ensureCursorVisible() - self.logger.debug(f"Adding message to chat from {sender}: {message}") + self.logger.debug( + f"Adding message to chat from {sender}: {message}" + ) # Log first 50 chars + + # Add message to chat contents + self.chat_contents.append({"sender": sender, "message": message}) + + # Save chat contents + self.chat_handler.file_handler.save_chat_contents(self.chat_contents) def display_thinking(self): self.add_to_chat("AI", "Thinking... 🤔") @@ -367,10 +376,18 @@ def on_new_session_clicked(self): def clear_chat_display(self): self.chat_display.clear() - self.logger.info("Cleared chat display in UI") + self.chat_contents = [] + self.chat_handler.file_handler.save_chat_contents(self.chat_contents) + self.logger.info("Cleared chat display and contents in UI") def close_code_popup(self): if self.code_popup: self.code_popup.close() self.code_popup = None self.logger.info("Closed code pop-up") + + def load_chat_contents(self): + self.chat_contents = self.chat_handler.file_handler.load_chat_contents() + for item in self.chat_contents: + self.add_to_chat(item["sender"], item["message"]) + self.logger.info(f"Loaded {len(self.chat_contents)} messages from chat log") diff --git a/codeaide/utils/file_handler.py b/codeaide/utils/file_handler.py index d87305b..294cb5c 100644 --- a/codeaide/utils/file_handler.py +++ b/codeaide/utils/file_handler.py @@ -24,6 +24,11 @@ def __init__(self, base_dir=None, session_id=None): if self.session_dir else None ) + self.chat_window_log_file = ( + os.path.join(self.session_dir, "chat_window_log.json") + if self.session_dir + else None + ) self._ensure_output_dirs_exist() if self.session_dir: @@ -117,6 +122,30 @@ def load_chat_history(self): self.logger.error(f"Error loading chat history: {str(e)}") return [] + def save_chat_contents(self, chat_contents): + if not self.session_dir: + self.logger.error("Session directory not set. Cannot save chat contents.") + return + + try: + with open(self.chat_window_log_file, "w", encoding="utf-8") as f: + json.dump(chat_contents, f, ensure_ascii=False, indent=2) + self.logger.info(f"Chat contents saved to {self.chat_window_log_file}") + except Exception as e: + self.logger.error(f"Error saving chat contents: {str(e)}") + + def load_chat_contents(self): + if not os.path.exists(self.chat_window_log_file): + self.logger.info(f"No chat log file found at {self.chat_window_log_file}") + return [] + + try: + with open(self.chat_window_log_file, "r", encoding="utf-8") as f: + return json.load(f) + except Exception as e: + self.logger.error(f"Error loading chat contents: {str(e)}") + return [] + def set_session_id(self, session_id): self.session_id = session_id self.session_dir = os.path.join(self.output_dir, self.session_id)