调整onlogin 代码量

This commit is contained in:
CakeCN
2025-12-11 17:28:20 +08:00
parent 7300b304e3
commit 77cd53c241
2 changed files with 377 additions and 115 deletions

View File

@@ -2,6 +2,27 @@ import requests
from flask_socketio import Namespace
def clear_user_session(namespace_entry, user_id):
'''
快速清理用户会话的函数
'''
try:
base_url = namespace_entry.app.config['ASE_ENGINE_URL']
clear_url = f"{base_url}/clear_user_session"
payload = {
'namespace_url': namespace_entry.app.config['ASE_ENGINE_URL_TOKEN'], # WebSocket命名空间URL
'user_id': user_id, # 用户ID对应session_info.id
'clear_type': 'all' # 清理类型,默认为全部
}
headers = {
'Content-Type': 'application/json'
}
response = requests.post(clear_url, json=payload, headers=headers)
response.raise_for_status() # 检查请求是否成功
except Exception as e:
print(f"Error clearing user session: {e}")
def on_connect_to_ase(client, entry, init_data, **kwargs):
'''
此时刚刚建立好aseclient连接并向前端发送打开code-server-like指令。
@@ -10,24 +31,12 @@ def on_connect_to_ase(client, entry, init_data, **kwargs):
namespace_entry.emit('open-iframe',"", room=init_data['room'], namespace='/agent')
namespace_entry.emit('message', "Agents服务已连接, 加载教案中", room=init_data['room'], namespace='/agent')
if init_data.get("restart", False):
try:
base_url = namespace_entry.app.config['ASE_ENGINE_URL']
clear_url = f"{base_url}/clear_user_session"
payload = {
'namespace_url': namespace_entry.app.config['ASE_ENGINE_URL_TOKEN'], # WebSocket命名空间URL
'user_id': init_data['room'], # 用户ID对应session_info.id
'clear_type': 'all' # 清理类型,默认为全部
}
headers = {
'Content-Type': 'application/json'
}
response = requests.post(clear_url, json=payload, headers=headers)
response.raise_for_status() # 检查请求是否成功
except Exception as e:
print(f"Error clearing user session: {e}")
ase_client.chatmanager.load_now_chapter()
ase_client.send_text("chapter-start", "")
clear_user_session(namespace_entry, init_data['room'])
restart = init_data.get("restart", False)
if restart:
ase_client.chatmanager.load_now_chapter()
ase_client.send_text("chapter-start", "")
namespace_entry.emit('message', "教案加载完毕", room=init_data['room'], namespace='/agent')