mergeok
This commit is contained in:
@@ -25,6 +25,12 @@ class ASEngineClient:
|
||||
# 存储路由和对应的回调函数
|
||||
self.routes: Dict[str, Dict[str, Callable]] = {}
|
||||
|
||||
self._on_connect_callback = None
|
||||
self._on_connect_callback_entry=None
|
||||
self._on_connect_callback_initData=None
|
||||
|
||||
|
||||
|
||||
# 注册基础事件处理函数
|
||||
self._register_base_events()
|
||||
|
||||
@@ -36,7 +42,9 @@ class ASEngineClient:
|
||||
self.connected = True
|
||||
self.sid = self.sio.get_sid(namespace=self.namespace)
|
||||
print(f"Connected to server. SID: {self.sid}")
|
||||
|
||||
if self._on_connect_callback is not None:
|
||||
self._on_connect_callback(self._on_connect_callback_entry, self._on_connect_callback_initData)
|
||||
|
||||
# 断开连接事件
|
||||
@self.sio.on('disconnect', namespace=self.namespace)
|
||||
def on_disconnect():
|
||||
@@ -55,6 +63,14 @@ class ASEngineClient:
|
||||
def on_response(data):
|
||||
print(f"Received response: {data}")
|
||||
|
||||
def register_on_connect_entry(self, callback: Callable, entry: Any, init_data: Any)->bool:
|
||||
if not callable(callback):
|
||||
return False
|
||||
self._on_connect_callback = callback
|
||||
self._on_connect_callback_entry=entry
|
||||
self._on_connect_callback_initData=init_data
|
||||
|
||||
|
||||
def register_route_with_entry(self, route: str,
|
||||
callback: Optional[Callable] = None,
|
||||
entry: Any = None,
|
||||
@@ -76,6 +92,7 @@ class ASEngineClient:
|
||||
|
||||
@self.sio.on(route, namespace=self.namespace)
|
||||
def on_route_message(data, _route=route):
|
||||
if not self.connected: return
|
||||
print(f"Received message on route '{_route}': {data}")
|
||||
route_info = self.routes.get(_route)
|
||||
if not route_info:
|
||||
@@ -138,7 +155,8 @@ class ASEngineClient:
|
||||
# 等待连接建立
|
||||
start_time = time.time()
|
||||
while not self.connected and (time.time() - start_time) < timeout:
|
||||
time.sleep(0.1)
|
||||
print(f"Connecting To {self.server_url}")
|
||||
time.sleep(0.2)
|
||||
return self.connected
|
||||
except Exception as e:
|
||||
print(f"Connection failed: {e}")
|
||||
@@ -263,8 +281,9 @@ class ASEngineClient:
|
||||
|
||||
|
||||
class HSAEngineClient(ASEngineClient):
|
||||
def __init__(self, server_url: str, namespace: str, id:str):
|
||||
def __init__(self, server_url: str, namespace: str, id:str, chatmanager):
|
||||
super().__init__(server_url, namespace, id)
|
||||
self.chatmanager = chatmanager
|
||||
def loadmarkdown(self, fmd, fmdp, fsp):
|
||||
self.send_text('markdown-in', fmd, self.id)
|
||||
self.send_text('markdown-prompt-in', fmdp, self.id)
|
||||
|
||||
@@ -2,6 +2,7 @@ import signal
|
||||
import sys
|
||||
import os
|
||||
import time
|
||||
import re
|
||||
import uuid
|
||||
current_directory = os.path.dirname(os.path.abspath(__file__))
|
||||
sys.path.append(current_directory)
|
||||
@@ -29,11 +30,25 @@ class ChatManager:
|
||||
_procs: Dict[str, ProcRecord] = {}
|
||||
_lock = threading.RLock()
|
||||
def __init__(self):
|
||||
self.chapter_chain_now = -1
|
||||
self.ase_client = None
|
||||
self.app = None
|
||||
self.socketio = None
|
||||
self.user_uuid = None
|
||||
self.raw_markdown = None
|
||||
self.raw_markdown_prompts = None
|
||||
self.raw_score_prompts = None
|
||||
self.material_id = None
|
||||
self.chapter_name = None
|
||||
self.lesson_name = None
|
||||
self.chapter_chain = None
|
||||
self.function_manager = FunctionManager()
|
||||
self.bb = None
|
||||
self.chat_historys = None
|
||||
pass
|
||||
|
||||
|
||||
def init(self,user_uuid,ase_client,raw_markdown,raw_markdown_prompts,raw_score_prompts,max_iter = 5, app=None, socketio=None):
|
||||
self.chapter_chain_now = -1
|
||||
def init(self,user_uuid,ase_client,raw_markdown,raw_markdown_prompts,raw_score_prompts,material_id,chapter_name, lesson_name, max_iter = 5, app=None, socketio=None):
|
||||
self.ase_client = ase_client
|
||||
self.app = app
|
||||
self.socketio = socketio
|
||||
@@ -41,9 +56,12 @@ class ChatManager:
|
||||
self.raw_markdown = raw_markdown
|
||||
self.raw_markdown_prompts = raw_markdown_prompts
|
||||
self.raw_score_prompts = raw_score_prompts
|
||||
self.material_id = material_id
|
||||
self.chapter_name = chapter_name
|
||||
self.lesson_name = lesson_name
|
||||
self.chapter_chain = load_chapters(raw_markdown, raw_markdown_prompts, raw_score_prompts)
|
||||
self.function_manager = FunctionManager()
|
||||
self.bb = None
|
||||
self.chat_historys = []
|
||||
|
||||
def disconnect(self, uuid):
|
||||
try:
|
||||
@@ -53,17 +71,29 @@ class ChatManager:
|
||||
print("ERROR: disconnect",e)
|
||||
print("disconnect success stop code-server success")
|
||||
|
||||
def load_code_in_markdown(self):
|
||||
nowchapter_content = self.chapter_chain[self.chapter_chain_now].chapter
|
||||
code_texts = extract_code_blocks(nowchapter_content)
|
||||
for code_text in code_texts:
|
||||
self.vscode_ws.emit('code-file', {'type':'create', 'data':code_text, 'chapter_title': self.chapter_chain[self.chapter_chain_now].title}, room=self.user_uuid ,namespace='/vscode')
|
||||
|
||||
|
||||
def next_chapter(self):
|
||||
assert self.chapter_chain_now + 1 < len(self.chapter_chain), "chapter_chain_now out of range"
|
||||
self.chapter_chain_now += 1
|
||||
self.bb.next_chapter()
|
||||
self.focus_chapter(self.chapter_chain_now)
|
||||
|
||||
|
||||
def focus_chapter(self, chapter_index):
|
||||
assert chapter_index < len(self.chapter_chain), "chapter_index out of range"
|
||||
self.chapter_chain_now = chapter_index
|
||||
self.chapter_chain[chapter_index].Focus()
|
||||
self.ase_client.loadmarkdown(self.chapter_chain[chapter_index].chapter, self.chapter_chain[chapter_index].chapter_prompt, self.chapter_chain[chapter_index].score_prompt)
|
||||
|
||||
def load_next_chapter(self):
|
||||
print(f"now load next chapter markdown {self.chapter_chain_now}")
|
||||
self.ase_client.loadmarkdown(self.chapter_chain[self.chapter_chain_now].chapter, self.chapter_chain[self.chapter_chain_now].chapter_prompt, self.chapter_chain[self.chapter_chain_now].score_prompt)
|
||||
self.load_code_in_markdown()
|
||||
|
||||
|
||||
def upload_bb(self):
|
||||
self.ase_client.send_text('backboard-in', self.bb.get_info_prompt())
|
||||
@@ -256,4 +286,21 @@ class ChatManager:
|
||||
for k in keys:
|
||||
self.stop(k)
|
||||
|
||||
|
||||
|
||||
def extract_code_blocks(text):
|
||||
"""
|
||||
从文本中提取所有用```标记的代码段(包括标记本身)
|
||||
|
||||
参数:
|
||||
text: 包含代码段的原始文本
|
||||
|
||||
返回:
|
||||
提取到的代码段列表,每个元素是一个完整的代码块(包括```标记)
|
||||
"""
|
||||
# 正则表达式匹配```开始和结束的代码块
|
||||
# 考虑可能的语言指定(如```python)
|
||||
pattern = r'```.*?```'
|
||||
# 使用DOTALL模式让.匹配包括换行符在内的所有字符
|
||||
code_blocks = re.findall(pattern, text, re.DOTALL)
|
||||
|
||||
return code_blocks
|
||||
Reference in New Issue
Block a user