diff --git a/api/app.py b/api/app.py index b0f09b9405..6271c60c0b 100644 --- a/api/app.py +++ b/api/app.py @@ -1,6 +1,5 @@ import os import sys -import logging def is_db_command(): diff --git a/api/controllers/console/app/online_user.py b/api/controllers/console/app/online_user.py index c85f045e9b..6f05aae5c5 100644 --- a/api/controllers/console/app/online_user.py +++ b/api/controllers/console/app/online_user.py @@ -1,20 +1,21 @@ import json -from flask_login import current_user, login_required from flask import request +from flask_login import current_user, login_required -from extensions.ext_socketio import ext_socketio from extensions.ext_redis import redis_client +from extensions.ext_socketio import ext_socketio -@ext_socketio.on('user_connect') + +@ext_socketio.on("user_connect") @login_required def handle_user_connect(data): """ Handle user connect event, check login and get user info. """ - + sid = request.sid - workflow_id = data.get('workflow_id') + workflow_id = data.get("workflow_id") old_info_json = redis_client.hget(f"workflow_online_users:{workflow_id}", current_user.id) if old_info_json: @@ -27,25 +28,16 @@ def handle_user_connect(data): "user_id": current_user.id, "username": getattr(current_user, "username", ""), "avatar": getattr(current_user, "avatar", ""), - "sid": sid - } + "sid": sid, + } - redis_client.hset( - f"workflow_online_users:{workflow_id}", - current_user.id, - json.dumps(user_info) - ) + redis_client.hset(f"workflow_online_users:{workflow_id}", current_user.id, json.dumps(user_info)) - redis_client.set( - f"ws_sid_map:{sid}", - json.dumps({ - "workflow_id": workflow_id, - "user_id": current_user.id - }) - ) - return {'msg': 'connected', 'user_id': current_user.id, 'sid': sid} + redis_client.set(f"ws_sid_map:{sid}", json.dumps({"workflow_id": workflow_id, "user_id": current_user.id})) + return {"msg": "connected", "user_id": current_user.id, "sid": sid} -@ext_socketio.on('disconnect') + +@ext_socketio.on("disconnect") def handle_disconnect(): """ Handle user disconnect event, remove user from workflow's online user list. @@ -55,6 +47,6 @@ def handle_disconnect(): if mapping: data = json.loads(mapping) workflow_id = data["workflow_id"] - user_id = data["user_id"] + user_id = data["user_id"] redis_client.hdel(f"workflow_online_users:{workflow_id}", user_id) - redis_client.delete(f"ws_sid_map:{sid}") \ No newline at end of file + redis_client.delete(f"ws_sid_map:{sid}") diff --git a/api/extensions/ext_socketio.py b/api/extensions/ext_socketio.py index b29e4b25ce..3f5f5db976 100644 --- a/api/extensions/ext_socketio.py +++ b/api/extensions/ext_socketio.py @@ -7,4 +7,4 @@ ext_socketio = SocketIO() def init_app(app: DifyApp): - ext_socketio.init_app(app, async_mode='gevent', cors_allowed_origins=dify_config.CONSOLE_CORS_ALLOW_ORIGINS) \ No newline at end of file + ext_socketio.init_app(app, async_mode="gevent", cors_allowed_origins=dify_config.CONSOLE_CORS_ALLOW_ORIGINS) diff --git a/api/uv.lock b/api/uv.lock index 261e3684e8..c98b8b7df8 100644 --- a/api/uv.lock +++ b/api/uv.lock @@ -1247,6 +1247,7 @@ dependencies = [ { name = "flask-socketio" }, { name = "flask-sqlalchemy" }, { name = "gevent" }, + { name = "gevent-websocket" }, { name = "gmpy2" }, { name = "google-api-core" }, { name = "google-api-python-client" }, @@ -1430,6 +1431,7 @@ requires-dist = [ { name = "flask-socketio", specifier = "~=5.5.1" }, { name = "flask-sqlalchemy", specifier = "~=3.1.1" }, { name = "gevent", specifier = "~=24.11.1" }, + { name = "gevent-websocket", specifier = "~=0.10.1" }, { name = "gmpy2", specifier = "~=2.2.1" }, { name = "google-api-core", specifier = "==2.18.0" }, { name = "google-api-python-client", specifier = "==2.90.0" }, @@ -1968,6 +1970,18 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/11/b2/5d20664ef6a077bec9f27f7a7ee761edc64946d0b1e293726a3d074a9a18/gevent-24.11.1-cp312-cp312-win_amd64.whl", hash = "sha256:68bee86b6e1c041a187347ef84cf03a792f0b6c7238378bf6ba4118af11feaae", size = 1541631 }, ] +[[package]] +name = "gevent-websocket" +version = "0.10.1" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "gevent" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/98/d2/6fa19239ff1ab072af40ebf339acd91fb97f34617c2ee625b8e34bf42393/gevent-websocket-0.10.1.tar.gz", hash = "sha256:7eaef32968290c9121f7c35b973e2cc302ffb076d018c9068d2f5ca8b2d85fb0", size = 18366 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/7b/84/2dc373eb6493e00c884cc11e6c059ec97abae2678d42f06bf780570b0193/gevent_websocket-0.10.1-py3-none-any.whl", hash = "sha256:17b67d91282f8f4c973eba0551183fc84f56f1c90c8f6b6b30256f31f66f5242", size = 22987 }, +] + [[package]] name = "gitdb" version = "4.0.12"