Skip to content

Agent Zero AI framework

refactor: optimize WebSocket handler lifecycle and fix extension asset path handling

frdel7eb6d5b19af5
diff --git a/helpers/ws.py b/helpers/ws.py
index b6201ab07..314f197a8 100644
--- a/helpers/ws.py
+++ b/helpers/ws.py
@@ -32,7 +32,7 @@ class _SecurityContext:
_ws_contexts: dict[str, _SecurityContext] = {}
-_active_handlers: dict[str, list[tuple[str, type["WsHandler"]]]] = {}
+_active_handlers: dict[str, dict[str, "WsHandler"]] = {}
_contexts_lock = threading.Lock()
@@ -156,7 +156,7 @@ def register_ws_namespace(
cache.add(CACHE_AREA, path, handler_cls)
return handler_cls
- @socketio_server.on("connect", namespace="/ws")
+ @socketio_server.on("connect", namespace="/ws") # type: ignore
async def _on_connect(sid, environ, auth):
with webapp.request_context(environ):
origin_ok, origin_reason = validate_ws_origin(environ)
@@ -192,7 +192,7 @@ def register_ws_namespace(
if isinstance(raw, list):
handler_paths = [p for p in raw if isinstance(p, str)]
- activated: list[tuple[str, type[WsHandler]]] = []
+ activated: dict[str, WsHandler] = {}
for path in handler_paths:
try:
handler_cls = _resolve_cached(path)
@@ -203,7 +203,7 @@ def register_ws_namespace(
continue
instance = handler_cls(socketio_server, lock)
await instance.on_connect(sid)
- activated.append((path, handler_cls))
+ activated[path] = instance
except Exception as e:
PrintStyle.error(f"WS on_connect error ({path}): {format_error(e)}")
@@ -212,20 +212,19 @@ def register_ws_namespace(
return True
- @socketio_server.on("disconnect", namespace="/ws")
+ @socketio_server.on("disconnect", namespace="/ws") # type: ignore
async def _on_disconnect(sid):
with _contexts_lock:
- activated = _active_handlers.pop(sid, [])
+ activated = _active_handlers.pop(sid, {})
_ws_contexts.pop(sid, None)
- for path, handler_cls in activated:
+ for path, instance in activated.items():
try:
- instance = handler_cls(socketio_server, lock)
await instance.on_disconnect(sid)
except Exception as e:
PrintStyle.error(f"WS on_disconnect error ({path}): {format_error(e)}")
- @socketio_server.on("*", namespace="/ws")
+ @socketio_server.on("*", namespace="/ws") # type: ignore
async def _dispatch(event, sid, data):
path = event
payload = data if isinstance(data, dict) else {}
@@ -233,20 +232,20 @@ def register_ws_namespace(
try:
with _contexts_lock:
ctx = _ws_contexts.get(sid)
+ activated = _active_handlers.get(sid, {})
if ctx is None:
return {"ok": False, "error": "No security context", "code": 401}
- handler_cls = _resolve_cached(path)
- if handler_cls is None:
- return {"ok": False, "error": f"WS endpoint not found: {path}", "code": 404}
+ instance = activated.get(path)
+ if instance is None:
+ return {"ok": False, "error": f"WS endpoint not activated: {path}", "code": 404}
# Security check
- error = _check_security(handler_cls, ctx)
+ error = _check_security(type(instance), ctx)
if error is not None:
return error
- # Instantiate and process
- instance = handler_cls(socketio_server, lock)
+ # Use cached instance and process
return await instance.process(payload, sid)
except Exception as e:
diff --git a/run_ui.py b/run_ui.py
index 5772f5202..2a7bc84e2 100644
--- a/run_ui.py
+++ b/run_ui.py
@@ -149,9 +149,10 @@ async def serve_plugin_asset(plugin_name, asset_path):
@webapp.route("/extensions/webui/<path:asset_path>", methods=["GET"])
@requires_auth
async def serve_extension_asset(asset_path):
- path = files.get_abs_path("extensions/webui", asset_path)
- if not files.is_in_dir(path, "extensions/webui"):
- return Response("Access denied", 403)
+ exts = files.get_abs_path("extensions/webui")
+ path = files.get_abs_path(exts, asset_path)
+ if not files.is_in_dir(path, exts):
+ return Response(f"Access denied", 403)
return send_file(path)
diff --git a/webui/components/sidebar/top-section/quick-actions.html b/webui/components/sidebar/top-section/quick-actions.html
index 4e5a9cd60..313b8bff4 100644
--- a/webui/components/sidebar/top-section/quick-actions.html
+++ b/webui/components/sidebar/top-section/quick-actions.html
@@ -184,6 +184,7 @@
.quick-actions-dropdown {
position: fixed;
z-index: 9999;
+ margin-top: 0;
}
/* Unified scrollbar styling for dropdown */
@@ -209,7 +210,12 @@
}
/* Dropdown item icon sizing */
- .dropdown-item .material-symbols-outlined {
+ .quick-actions-dropdown .dropdown-item,
+ .quick-actions-dropdown .dropdown-item .material-symbols-outlined {
+ opacity: 1;
+ }
+
+ .quick-actions-dropdown .dropdown-item .material-symbols-outlined {
font-size: 18px;
}