89 lines
3.2 KiB
Python
89 lines
3.2 KiB
Python
|
||
|
||
|
||
import time
|
||
import os
|
||
|
||
class Backboard:
|
||
def __init__(self, user_uuid, username, course_id, lesson_id, root_path):
|
||
self.user_uuid = user_uuid
|
||
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):
|
||
# 检查必要的键是否存在,避免KeyError
|
||
if realtime_action.get('type') == 'config':
|
||
return
|
||
if len(self.history) > 0 and realtime_action.get('type') == 'fileEdit':
|
||
# 安全地获取filePath,并进行比较
|
||
current_file_path = realtime_action.get('filePath')
|
||
# 检查最近的历史记录是否包含filePath
|
||
if current_file_path and self.history[-1].get('filePath') == current_file_path:
|
||
# 安全地更新content,只有当content存在时才更新
|
||
if 'content' in realtime_action:
|
||
self.history[-1]['content'] = realtime_action['content']
|
||
return
|
||
self.history.append(realtime_action)
|
||
# 限制历史记录的数量
|
||
if len(self.history) > 500: # 假设最多保留500条记录
|
||
self.history.pop(0) # 移除最旧的记录
|
||
|
||
|
||
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):
|
||
return self.active_file_path
|
||
|
||
|
||
|
||
class BackBoardManager:
|
||
def __init__(self):
|
||
self.backboards = {} # user_uuid: Backboard
|
||
|
||
def add_backboard(self, user_uuid, username, course_id, lesson_id, root_path):
|
||
self.backboards[user_uuid] = Backboard(user_uuid,username, course_id, lesson_id, root_path)
|
||
|
||
def get_backboard(self, user_uuid) -> Backboard:
|
||
return self.backboards.get(user_uuid, None)
|
||
|
||
|
||
|