feat:历史记录关于user_id限制

This commit is contained in:
liailing1026
2026-03-09 10:28:07 +08:00
parent 8926480e62
commit 14b79bc282
6 changed files with 69 additions and 19 deletions

View File

@@ -534,8 +534,8 @@ def handle_generate_base_plan_ws(data):
Request_Cache[requestIdentifier] = basePlan_withRenderSpec
# 保存到数据库
user_id = incoming_data.get("user_id", "default_user")
task_id = incoming_data.get("task_id") or str(uuid.uuid4())
user_id = incoming_data.get("user_id")
task_id = incoming_data.get("task_id")
generation_id = str(uuid.uuid4())
with get_db_context() as db:
@@ -1671,9 +1671,21 @@ def handle_get_agents_ws(data):
"""
WebSocket版本获取智能体配置
从 user_agents 数据库表读取
如果没有 user_id会生成新的 user_id 并返回
"""
request_id = data.get('id')
user_id = data.get('user_id', 'default_user')
# 前端发送的数据在 data 字段中
incoming_data = data.get('data', {})
user_id = incoming_data.get('user_id') if isinstance(incoming_data, dict) else None
# 如果没有 user_id生成新的
new_user_id = None
if not user_id:
new_user_id = str(uuid.uuid4())
user_id = new_user_id
print(f"[get_agents] 新生成 user_id: {new_user_id}")
else:
print(f"[get_agents] 接收到的 user_id: {user_id}")
try:
# 从数据库获取用户的智能体配置
@@ -1694,14 +1706,20 @@ def handle_get_agents_ws(data):
'apiModel': config.get('apiModel', ''),
})
response_data = {
'code': 200,
'content': 'get agents successfully',
'agents': agents
}
# 如果生成了新的 user_id返回给前端
if new_user_id:
response_data['user_id'] = new_user_id
emit('response', {
'id': request_id,
'status': 'success',
'data': {
'code': 200,
'content': 'get agents successfully',
'agents': agents
}
'data': response_data
})
except Exception as e:
@@ -1929,14 +1947,18 @@ def handle_get_plans(data):
WebSocket版本获取历史任务列表
"""
# socketio 会把数据包装多层:
# 前端发送: { id: 'get_plans-xxx', action: 'get_plans', data: { id: 'ws_req_xxx' } }
# 前端发送: { id: 'get_plans-xxx', action: 'get_plans', data: { id: 'ws_req_xxx', user_id: 'xxx' } }
# socketio 包装后: data = { id: 'get_plans-xxx', action: 'get_plans', data: {...} }
request_id = data.get('id') # socketio 包装的 id用于响应匹配
# 获取 user_id从 data.data 中获取,因为前端发送时会包装)
incoming_data = data.get('data', {})
user_id = incoming_data.get('user_id') if isinstance(incoming_data, dict) else None
try:
with get_db_context() as db:
# 获取最近的任务记录
tasks = MultiAgentTaskCRUD.get_recent(db, limit=50)
# 获取最近的任务记录,按 user_id 过滤
tasks = MultiAgentTaskCRUD.get_recent(db, limit=50, user_id=user_id)
# 转换为前端期望的格式
plans = []