change clear to sync

This commit is contained in:
CakeCN
2025-11-18 23:32:41 +08:00
parent 5e6e040753
commit 6c48d79076

View File

@@ -1,8 +1,10 @@
# myapp/sockets/namespaces.py # myapp/sockets/namespaces.py
import json import json
import time import time
from flask import current_app, request, session import requests
from flask import current_app, request, session, url_for
from flask_socketio import Namespace, join_room, leave_room, emit from flask_socketio import Namespace, join_room, leave_room, emit
from ..config import Config
from ..function_tools import judge from ..function_tools import judge
from ..function_tools import next_chapter from ..function_tools import next_chapter
@@ -209,9 +211,33 @@ class AgentNamespace(Namespace):
print(f"on_connect_to_ase {client_entry}, {namespace_entry}, {init_data}") print(f"on_connect_to_ase {client_entry}, {namespace_entry}, {init_data}")
namespace_entry, ase_client = namespace_entry namespace_entry, ase_client = namespace_entry
namespace_entry.emit('open-iframe',"", room=init_data['room'], namespace='/agent') namespace_entry.emit('open-iframe',"", room=init_data['room'], namespace='/agent')
# 将异步emit改为同步requests.post请求
try:
# 构建请求URL使用配置中的ASE_ENGINE_URL
base_url = current_app.config['ASE_ENGINE_URL']
clear_url = f"{base_url}/clear_user_session"
# 准备请求参数
payload = {
'namespace_url': current_app.config['ASE_ENGINE_URL_TOKEN'], # WebSocket命名空间URL
'user_id': init_data['room'], # 用户ID对应session_info.id
'clear_type': 'all' # 清理类型,默认为全部
}
# 添加token认证头
headers = {
'Content-Type': 'application/json'
}
# 发送同步POST请求
response = requests.post(clear_url, json=payload, headers=headers)
response.raise_for_status() # 检查请求是否成功
print(f"Clear user session successful: {response.json()}")
except Exception as e:
print(f"Error clearing user session: {e}")
ase_client.chatmanager.load_next_chapter() ase_client.chatmanager.load_next_chapter()
ase_client.send_text("chapter-start", "") ase_client.send_text("chapter-start", "")
ase_client.send_text("clear","")
namespace_entry.emit('message', "Multi-Agents服务已连接", room=init_data['room'], namespace='/agent') namespace_entry.emit('message', "Multi-Agents服务已连接", room=init_data['room'], namespace='/agent')
#接受消息回来直接让message监听发送 #接受消息回来直接让message监听发送