Skip to content

Commit

Permalink
Added a log file containing the chat window history
Browse files Browse the repository at this point in the history
  • Loading branch information
dougollerenshaw committed Oct 1, 2024
1 parent c4c7e68 commit 5abc035
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
12 changes: 12 additions & 0 deletions codeaide/logic/chat_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
21 changes: 19 additions & 2 deletions codeaide/ui/chat_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -202,7 +203,15 @@ def add_to_chat(self, sender, message):
self.chat_display.append(html_message + "<br>")
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... 🤔")
Expand Down Expand Up @@ -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")
29 changes: 29 additions & 0 deletions codeaide/utils/file_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 5abc035

Please sign in to comment.