From 00fb9c88e1a7127f059270968fb37e5b2064da0f Mon Sep 17 00:00:00 2001 From: "Dr.Lt.Data" Date: Fri, 22 Aug 2025 20:43:19 +0900 Subject: [PATCH] modified: remove `matrix-nio` dependency from the `requirements.txt` modified: The matrix share feature is now only available when the `matrix-nio` dependency is installed. If `matrix-nio` is not installed: 1. Apply a strikethrough to the matrix checkbox text in the share UI and display a tooltip. 2. A warning is logged at startup indicating that `matrix-nio` is missing, along with the installation command. fixed: Corrected an issue where PR #2025 was merged into draft-v4 but applied only to `legacy/..` and not to `glob/..` --- comfyui_manager/glob/share_3rdparty.py | 101 +++++++++++++++++---- comfyui_manager/js/comfyui-share-common.js | 14 +++ comfyui_manager/legacy/share_3rdparty.py | 22 ++++- pyproject.toml | 4 +- requirements.txt | 2 +- 5 files changed, 119 insertions(+), 24 deletions(-) diff --git a/comfyui_manager/glob/share_3rdparty.py b/comfyui_manager/glob/share_3rdparty.py index 80ed1211..87416566 100644 --- a/comfyui_manager/glob/share_3rdparty.py +++ b/comfyui_manager/glob/share_3rdparty.py @@ -11,6 +11,15 @@ import hashlib import folder_paths from server import PromptServer import logging +import sys + + +try: + from nio import AsyncClient, LoginResponse, UploadResponse + matrix_nio_is_available = True +except Exception: + logging.warning(f"[ComfyUI-Manager] The matrix sharing feature has been disabled because the `matrix-nio` dependency is not installed.\n\tTo use this feature, please run the following command:\n\t{sys.executable} -m pip install matrix-nio") + matrix_nio_is_available = False def extract_model_file_names(json_data): @@ -193,6 +202,14 @@ async def get_esheep_workflow_and_images(request): return web.Response(status=200, text=json.dumps(data)) +@PromptServer.instance.routes.get("/v2/manager/get_matrix_dep_status") +async def get_matrix_dep_status(request): + if matrix_nio_is_available: + return web.Response(status=200, text='available') + else: + return web.Response(status=200, text='unavailable') + + def set_matrix_auth(json_data): homeserver = json_data['homeserver'] username = json_data['username'] @@ -332,15 +349,12 @@ async def share_art(request): workflowId = upload_workflow_json["workflowId"] # check if the user has provided Matrix credentials - if "matrix" in share_destinations: + if matrix_nio_is_available and "matrix" in share_destinations: comfyui_share_room_id = '!LGYSoacpJPhIfBqVfb:matrix.org' filename = os.path.basename(asset_filepath) content_type = assetFileType try: - from matrix_client.api import MatrixHttpApi - from matrix_client.client import MatrixClient - homeserver = 'matrix.org' if matrix_auth: homeserver = matrix_auth.get('homeserver', 'matrix.org') @@ -348,20 +362,35 @@ async def share_art(request): if not homeserver.startswith("https://"): homeserver = "https://" + homeserver - client = MatrixClient(homeserver) - try: - token = client.login(username=matrix_auth['username'], password=matrix_auth['password']) - if not token: - return web.json_response({"error": "Invalid Matrix credentials."}, content_type='application/json', status=400) - except Exception: + client = AsyncClient(homeserver, matrix_auth['username']) + + # Login + login_resp = await client.login(matrix_auth['password']) + if not isinstance(login_resp, LoginResponse) or not login_resp.access_token: + await client.close() return web.json_response({"error": "Invalid Matrix credentials."}, content_type='application/json', status=400) - matrix = MatrixHttpApi(homeserver, token=token) + # Upload asset with open(asset_filepath, 'rb') as f: - mxc_url = matrix.media_upload(f.read(), content_type, filename=filename)['content_uri'] + upload_resp, _maybe_keys = await client.upload(f, content_type=content_type, filename=filename) + asset_data = f.seek(0) or f.read() # get size for info below + if not isinstance(upload_resp, UploadResponse) or not upload_resp.content_uri: + await client.close() + return web.json_response({"error": "Failed to upload asset to Matrix."}, content_type='application/json', status=500) + mxc_url = upload_resp.content_uri - workflow_json_mxc_url = matrix.media_upload(prompt['workflow'], 'application/json', filename='workflow.json')['content_uri'] + # Upload workflow JSON + import io + workflow_json_bytes = json.dumps(prompt['workflow']).encode('utf-8') + workflow_io = io.BytesIO(workflow_json_bytes) + upload_workflow_resp, _maybe_keys = await client.upload(workflow_io, content_type='application/json', filename='workflow.json') + workflow_io.seek(0) + if not isinstance(upload_workflow_resp, UploadResponse) or not upload_workflow_resp.content_uri: + await client.close() + return web.json_response({"error": "Failed to upload workflow to Matrix."}, content_type='application/json', status=500) + workflow_json_mxc_url = upload_workflow_resp.content_uri + # Send text message text_content = "" if title: text_content += f"{title}\n" @@ -369,11 +398,47 @@ async def share_art(request): text_content += f"{description}\n" if credits: text_content += f"\ncredits: {credits}\n" - matrix.send_message(comfyui_share_room_id, text_content) - matrix.send_content(comfyui_share_room_id, mxc_url, filename, 'm.image') - matrix.send_content(comfyui_share_room_id, workflow_json_mxc_url, 'workflow.json', 'm.file') - except Exception: - logging.exception("An error occurred") + await client.room_send( + room_id=comfyui_share_room_id, + message_type="m.room.message", + content={"msgtype": "m.text", "body": text_content} + ) + + # Send image + await client.room_send( + room_id=comfyui_share_room_id, + message_type="m.room.message", + content={ + "msgtype": "m.image", + "body": filename, + "url": mxc_url, + "info": { + "mimetype": content_type, + "size": len(asset_data) + } + } + ) + + # Send workflow JSON file + await client.room_send( + room_id=comfyui_share_room_id, + message_type="m.room.message", + content={ + "msgtype": "m.file", + "body": "workflow.json", + "url": workflow_json_mxc_url, + "info": { + "mimetype": "application/json", + "size": len(workflow_json_bytes) + } + } + ) + + await client.close() + + except: + import traceback + traceback.print_exc() return web.json_response({"error": "An error occurred when sharing your art to Matrix."}, content_type='application/json', status=500) return web.json_response({ diff --git a/comfyui_manager/js/comfyui-share-common.js b/comfyui_manager/js/comfyui-share-common.js index 9b570b8a..0691dce9 100644 --- a/comfyui_manager/js/comfyui-share-common.js +++ b/comfyui_manager/js/comfyui-share-common.js @@ -552,6 +552,20 @@ export class ShareDialog extends ComfyDialog { this.matrix_destination_checkbox.style.color = "var(--fg-color)"; this.matrix_destination_checkbox.checked = this.share_option === 'matrix'; //true; + try { + api.fetchApi(`/v2/manager/get_matrix_dep_status`) + .then(response => response.text()) + .then(data => { + if(data == 'unavailable') { + matrix_destination_checkbox_text.style.textDecoration = "line-through"; + this.matrix_destination_checkbox.disabled = true; + this.matrix_destination_checkbox.title = "It has been disabled because the 'matrix-nio' dependency is not installed. Please install this dependency to use the matrix sharing feature."; + matrix_destination_checkbox_text.title = "It has been disabled because the 'matrix-nio' dependency is not installed. Please install this dependency to use the matrix sharing feature."; + } + }) + .catch(error => {}); + } catch (error) {} + this.comfyworkflows_destination_checkbox = $el("input", { type: 'checkbox', id: "comfyworkflows_destination" }, []) const comfyworkflows_destination_checkbox_text = $el("label", {}, [" ComfyWorkflows.com"]) this.comfyworkflows_destination_checkbox.style.color = "var(--fg-color)"; diff --git a/comfyui_manager/legacy/share_3rdparty.py b/comfyui_manager/legacy/share_3rdparty.py index e5938333..87416566 100644 --- a/comfyui_manager/legacy/share_3rdparty.py +++ b/comfyui_manager/legacy/share_3rdparty.py @@ -10,6 +10,16 @@ import hashlib import folder_paths from server import PromptServer +import logging +import sys + + +try: + from nio import AsyncClient, LoginResponse, UploadResponse + matrix_nio_is_available = True +except Exception: + logging.warning(f"[ComfyUI-Manager] The matrix sharing feature has been disabled because the `matrix-nio` dependency is not installed.\n\tTo use this feature, please run the following command:\n\t{sys.executable} -m pip install matrix-nio") + matrix_nio_is_available = False def extract_model_file_names(json_data): @@ -192,6 +202,14 @@ async def get_esheep_workflow_and_images(request): return web.Response(status=200, text=json.dumps(data)) +@PromptServer.instance.routes.get("/v2/manager/get_matrix_dep_status") +async def get_matrix_dep_status(request): + if matrix_nio_is_available: + return web.Response(status=200, text='available') + else: + return web.Response(status=200, text='unavailable') + + def set_matrix_auth(json_data): homeserver = json_data['homeserver'] username = json_data['username'] @@ -331,14 +349,12 @@ async def share_art(request): workflowId = upload_workflow_json["workflowId"] # check if the user has provided Matrix credentials - if "matrix" in share_destinations: + if matrix_nio_is_available and "matrix" in share_destinations: comfyui_share_room_id = '!LGYSoacpJPhIfBqVfb:matrix.org' filename = os.path.basename(asset_filepath) content_type = assetFileType try: - from nio import AsyncClient, LoginResponse, UploadResponse - homeserver = 'matrix.org' if matrix_auth: homeserver = matrix_auth.get('homeserver', 'matrix.org') diff --git a/pyproject.toml b/pyproject.toml index bcdb0421..e7c56860 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta" [project] name = "comfyui-manager" license = { text = "GPL-3.0-only" } -version = "4.0.0-beta.12" +version = "4.0.0-beta.13" requires-python = ">= 3.9" description = "ComfyUI-Manager provides features to install and manage custom nodes for ComfyUI, as well as various functionalities to assist with ComfyUI." readme = "README.md" @@ -27,7 +27,7 @@ classifiers = [ dependencies = [ "GitPython", "PyGithub", - "matrix-client==0.4.0", +# "matrix-nio", "transformers", "huggingface-hub>0.20", "typer", diff --git a/requirements.txt b/requirements.txt index 08372455..2ba553e8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,6 @@ GitPython PyGithub -matrix-nio +# matrix-nio transformers huggingface-hub>0.20 typer