import time import os class Backboard: def __init__(self, user_id, username, course_id, lesson_id, root_path): self.user_id = user_id self.username = username self.course_id = course_id self.lesson_id = lesson_id self.root_path = root_path self.create_time = time.time() self.enter_chapter_time = time.time() self.history = [] self.file_tree = "[]" self.active_file_path = "" self.active_file_content = "" self.pasted_file_path = "" self.pasted_content = "" def next_chapter(self): self.enter_chapter_time = time.time() def get_info_prompt(self): five_history = "" for h in self.history[-5:]: five_history += h['type'] + "\n" + h.get('filePath', '') + "\n\n" endl = "\n" return (f"###Global Info:###{endl}" f"Here are some info about now user's IDE, refer to it when you need to handle some code.{endl}" f"- User's total study time is {self.get_deltatime_mmss(self.create_time, time.time())}{endl}" f"- User's current chapter study time is {self.get_deltatime_mmss(self.enter_chapter_time, time.time())}{endl}" f"- Activated file path: {self.active_file_path}{endl}" f"```{endl}" f"{self.active_file_content[-10:]}{endl}" f"```{endl}" f"- Last five action:{five_history}{endl}" f"- File tree: {self.file_tree}{endl}") # f"- Activated file content (current editing 10 lines): {endl}" def add_history(self, realtime_action): if(realtime_action['type']=='config'):return if len(self.history)!=0: if (self.history[-1]['type']=='fileEdit'): if(self.history[-1]['filePath'] == realtime_action['filePath']): self.history[-1]['content'] = realtime_action['content'] return self.history.append(realtime_action) def get_deltatime_mmss(self, start_time, end_time): elapsed_time = end_time - start_time hours = int(elapsed_time // 3600) minutes = int((elapsed_time % 3600) // 60) seconds = int(elapsed_time % 60) return f"{hours:02d}:{minutes:02d}:{seconds:02d}" def get_active_file_reletive_path(self): relative_path = os.path.relpath(self.active_file_path, self.root_path) return relative_path class BackBoardManager: def __init__(self): self.backboards = {} # user_id: Backboard def add_backboard(self, user_id, username, course_id, lesson_id, root_path): self.backboards[user_id] = Backboard(user_id,username, course_id, lesson_id, root_path) def get_backboard(self, user_id) -> Backboard: return self.backboards[user_id]