From c4c7e683055be66388bda88adb412959d1d446cc Mon Sep 17 00:00:00 2001 From: dougollerenshaw Date: Tue, 1 Oct 2024 12:15:53 -0700 Subject: [PATCH] Improved logic for starting new session --- codeaide/logic/chat_handler.py | 18 ++++++++++++++---- codeaide/ui/chat_window.py | 19 +++++++++---------- codeaide/utils/file_handler.py | 22 ++++++++++++++++++++++ 3 files changed, 45 insertions(+), 14 deletions(-) diff --git a/codeaide/logic/chat_handler.py b/codeaide/logic/chat_handler.py index 12924c2..2a305ed 100644 --- a/codeaide/logic/chat_handler.py +++ b/codeaide/logic/chat_handler.py @@ -16,6 +16,7 @@ AI_PROVIDERS, DEFAULT_MODEL, DEFAULT_PROVIDER, + INITIAL_MESSAGE, ) from codeaide.utils.cost_tracker import CostTracker from codeaide.utils.environment_manager import EnvironmentManager @@ -42,6 +43,9 @@ def __init__(self): self.session_id = generate_session_id() self.cost_tracker = CostTracker() self.file_handler = FileHandler(session_id=self.session_id) + self.session_dir = ( + self.file_handler.session_dir + ) # Store the specific session directory self.logger = get_logger() self.conversation_history = self.file_handler.load_chat_history() self.env_manager = EnvironmentManager() @@ -57,6 +61,7 @@ def __init__(self): self.api_key_valid, self.api_key_message = self.check_api_key() self.logger.info(f"New session started with ID: {self.session_id}") + self.logger.info(f"Session directory: {self.session_dir}") def check_api_key(self): """ @@ -536,6 +541,9 @@ def set_latest_version(self, version): def start_new_session(self, chat_window): logger.info("Starting new session") + # Log the previous session path correctly + logger.info(f"Previous session path: {self.session_dir}") + # Generate new session ID new_session_id = generate_session_id() @@ -549,6 +557,7 @@ def start_new_session(self, chat_window): # Update instance variables self.session_id = new_session_id self.file_handler = new_file_handler + self.session_dir = new_file_handler.session_dir # Update the session directory # Clear conversation history self.conversation_history = [] @@ -559,9 +568,10 @@ def start_new_session(self, chat_window): # Close code pop-up if it exists chat_window.close_code_popup() - # Show confirmation message - QMessageBox.information( - chat_window, "New Session", "A new session has been started." - ) + # Add system message about previous session + system_message = f"A new session has been started. The previous chat will not be visible to the agent. Previous session data saved in: {self.session_dir}" + chat_window.add_to_chat("System", system_message) + chat_window.add_to_chat("AI", INITIAL_MESSAGE) logger.info(f"New session started with ID: {self.session_id}") + logger.info(f"New session directory: {self.session_dir}") diff --git a/codeaide/ui/chat_window.py b/codeaide/ui/chat_window.py index 89d07b2..f135a63 100644 --- a/codeaide/ui/chat_window.py +++ b/codeaide/ui/chat_window.py @@ -133,15 +133,14 @@ def setup_ui(self): self.example_button.clicked.connect(self.load_example) button_layout.addWidget(self.example_button) - self.exit_button = QPushButton("Exit", self) - self.exit_button.clicked.connect(self.on_exit) - button_layout.addWidget(self.exit_button) - - # Add New Session button self.new_session_button = QPushButton("New Session") self.new_session_button.clicked.connect(self.on_new_session_clicked) button_layout.addWidget(self.new_session_button) + self.exit_button = QPushButton("Exit", self) + self.exit_button.clicked.connect(self.on_exit) + button_layout.addWidget(self.exit_button) + main_layout.addLayout(button_layout) self.logger.info("Chat window UI initialized") @@ -351,7 +350,7 @@ def update_chat_handler(self): self.add_to_chat("System", switch_message) def on_new_session_clicked(self): - logger.info("User clicked New Session button") + self.logger.info("User clicked New Session button") reply = QMessageBox.question( self, "New Session", @@ -361,17 +360,17 @@ def on_new_session_clicked(self): ) if reply == QMessageBox.Yes: - logger.info("User confirmed starting a new session") + self.logger.info("User confirmed starting a new session") self.chat_handler.start_new_session(self) else: - logger.info("User cancelled starting a new session") + self.logger.info("User cancelled starting a new session") def clear_chat_display(self): self.chat_display.clear() - logger.info("Cleared chat display in UI") + self.logger.info("Cleared chat display in UI") def close_code_popup(self): if self.code_popup: self.code_popup.close() self.code_popup = None - logger.info("Closed code pop-up") + self.logger.info("Closed code pop-up") diff --git a/codeaide/utils/file_handler.py b/codeaide/utils/file_handler.py index 0ac1029..d87305b 100644 --- a/codeaide/utils/file_handler.py +++ b/codeaide/utils/file_handler.py @@ -124,3 +124,25 @@ def set_session_id(self, session_id): self._ensure_output_dirs_exist() setup_logger(self.session_dir) self.logger = get_logger() + + def copy_log_to_new_session(self, new_session_id): + new_session_dir = os.path.join(self.output_dir, new_session_id) + os.makedirs(new_session_dir, exist_ok=True) + + old_log_file = os.path.join(self.session_dir, "codeaide.log") + new_log_file = os.path.join(new_session_dir, "codeaide.log") + + os.makedirs(os.path.dirname(new_log_file), exist_ok=True) + + if os.path.exists(old_log_file): + shutil.copy2(old_log_file, new_log_file) + + # Append a message to the old log file + with open(old_log_file, "a") as f: + f.write("\nNew session created. Log continued in new file.\n") + + self.logger.info(f"Copied log file to new session: {new_session_id}") + else: + self.logger.warning( + f"No existing log file found to copy for new session: {new_session_id}" + )