Skip to content

Commit

Permalink
Improved logic for starting new session
Browse files Browse the repository at this point in the history
  • Loading branch information
dougollerenshaw committed Oct 1, 2024
1 parent f958f50 commit c4c7e68
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 14 deletions.
18 changes: 14 additions & 4 deletions codeaide/logic/chat_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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):
"""
Expand Down Expand Up @@ -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()

Expand All @@ -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 = []
Expand All @@ -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}")
19 changes: 9 additions & 10 deletions codeaide/ui/chat_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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",
Expand All @@ -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")
22 changes: 22 additions & 0 deletions codeaide/utils/file_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
)

0 comments on commit c4c7e68

Please sign in to comment.