diff --git a/.env.example b/.env.example new file mode 100644 index 00000000..27d7e8ba --- /dev/null +++ b/.env.example @@ -0,0 +1 @@ +PYPI_TOKEN=your-pypi-token \ No newline at end of file diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 00000000..ec06b27a --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,70 @@ +name: CI + +on: + push: + branches: [ main, feat/*, fix/* ] + pull_request: + branches: [ main ] + +jobs: + validate-openapi: + name: Validate OpenAPI Specification + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Check if OpenAPI changed + id: openapi-changed + uses: tj-actions/changed-files@v44 + with: + files: openapi.yaml + + - name: Setup Node.js + if: steps.openapi-changed.outputs.any_changed == 'true' + uses: actions/setup-node@v4 + with: + node-version: '18' + + - name: Install Redoc CLI + if: steps.openapi-changed.outputs.any_changed == 'true' + run: | + npm install -g @redocly/cli + + - name: Validate OpenAPI specification + if: steps.openapi-changed.outputs.any_changed == 'true' + run: | + redocly lint openapi.yaml + + code-quality: + name: Code Quality Checks + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 # Fetch all history for proper diff + + - name: Get changed Python files + id: changed-py-files + uses: tj-actions/changed-files@v44 + with: + files: | + **/*.py + files_ignore: | + comfyui_manager/legacy/** + + - name: Setup Python + if: steps.changed-py-files.outputs.any_changed == 'true' + uses: actions/setup-python@v5 + with: + python-version: '3.9' + + - name: Install dependencies + if: steps.changed-py-files.outputs.any_changed == 'true' + run: | + pip install ruff + + - name: Run ruff linting on changed files + if: steps.changed-py-files.outputs.any_changed == 'true' + run: | + echo "Changed files: ${{ steps.changed-py-files.outputs.all_changed_files }}" + echo "${{ steps.changed-py-files.outputs.all_changed_files }}" | xargs -r ruff check \ No newline at end of file diff --git a/.github/workflows/publish-to-pypi.yml b/.github/workflows/publish-to-pypi.yml index f96c027a..88ae24a8 100644 --- a/.github/workflows/publish-to-pypi.yml +++ b/.github/workflows/publish-to-pypi.yml @@ -21,7 +21,7 @@ jobs: - name: Set up Python uses: actions/setup-python@v4 with: - python-version: '3.9' + python-version: '3.x' - name: Install build dependencies run: | @@ -31,28 +31,28 @@ jobs: - name: Get current version id: current_version run: | - CURRENT_VERSION=$(grep -oP 'version = "\K[^"]+' pyproject.toml) + CURRENT_VERSION=$(grep -oP '^version = "\K[^"]+' pyproject.toml) echo "version=$CURRENT_VERSION" >> $GITHUB_OUTPUT echo "Current version: $CURRENT_VERSION" - name: Build package run: python -m build - - name: Create GitHub Release - id: create_release - uses: softprops/action-gh-release@v2 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - files: dist/* - tag_name: v${{ steps.current_version.outputs.version }} - draft: false - prerelease: false - generate_release_notes: true + # - name: Create GitHub Release + # id: create_release + # uses: softprops/action-gh-release@v2 + # env: + # GITHUB_TOKEN: ${{ github.token }} + # with: + # files: dist/* + # tag_name: v${{ steps.current_version.outputs.version }} + # draft: false + # prerelease: false + # generate_release_notes: true - name: Publish to PyPI - uses: pypa/gh-action-pypi-publish@release/v1 + uses: pypa/gh-action-pypi-publish@76f52bc884231f62b9a034ebfe128415bbaabdfc with: password: ${{ secrets.PYPI_TOKEN }} skip-existing: true - verbose: true \ No newline at end of file + verbose: true diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml deleted file mode 100644 index 463ecca9..00000000 --- a/.github/workflows/publish.yml +++ /dev/null @@ -1,25 +0,0 @@ -name: Publish to Comfy registry -on: - workflow_dispatch: - push: - branches: - - main-blocked - paths: - - "pyproject.toml" - -permissions: - issues: write - -jobs: - publish-node: - name: Publish Custom Node to registry - runs-on: ubuntu-latest - if: ${{ github.repository_owner == 'ltdrdata' }} - steps: - - name: Check out code - uses: actions/checkout@v4 - - name: Publish Custom Node - uses: Comfy-Org/publish-node-action@v1 - with: - ## Add your own personal access token to your Github Repository secrets and reference it here. - personal_access_token: ${{ secrets.REGISTRY_ACCESS_TOKEN }} diff --git a/.gitignore b/.gitignore index 33ee743b..f00db96b 100644 --- a/.gitignore +++ b/.gitignore @@ -17,4 +17,8 @@ github-stats-cache.json pip_overrides.json *.json check2.sh -/venv/ \ No newline at end of file +/venv/ +build +dist +*.egg-info +.env \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 00000000..318b5795 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,47 @@ +## Testing Changes + +1. Activate the ComfyUI environment. + +2. Build package locally after making changes. + +```bash +# from inside the ComfyUI-Manager directory, with the ComfyUI environment activated +python -m build +``` + +3. Install the package locally in the ComfyUI environment. + +```bash +# Uninstall existing package +pip uninstall comfyui-manager + +# Install the locale package +pip install dist/comfyui-manager-*.whl +``` + +4. Start ComfyUI. + +```bash +# after navigating to the ComfyUI directory +python main.py +``` + +## Manually Publish Test Version to PyPi + +1. Set the `PYPI_TOKEN` environment variable in env file. + +2. If manually publishing, you likely want to use a release candidate version, so set the version in [pyproject.toml](pyproject.toml) to something like `0.0.1rc1`. + +3. Build the package. + +```bash +python -m build +``` + +4. Upload the package to PyPi. + +```bash +python -m twine upload dist/* --username __token__ --password $PYPI_TOKEN +``` + +5. View at https://pypi.org/project/comfyui-manager/ diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 00000000..21e7402f --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1,14 @@ +include comfyui_manager/js/* +include comfyui_manager/*.json +include comfyui_manager/glob/* +include LICENSE.txt +include README.md +include requirements.txt +include pyproject.toml +include custom-node-list.json +include extension-node-list.json +include extras.json +include github-stats.json +include model-list.json +include alter-list.json +include comfyui_manager/channels.list.template \ No newline at end of file diff --git a/README.md b/README.md index cb010dbb..e90ab1f0 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ ![menu](https://raw.githubusercontent.com/ltdrdata/ComfyUI-extension-tutorials/refs/heads/Main/ComfyUI-Manager/images/dialog.jpg) ## NOTICE +* V4.0: Modify the structure to be installable via pip instead of using git clone. * V3.16: Support for `uv` has been added. Set `use_uv` in `config.ini`. * V3.10: `double-click feature` is removed * This feature has been moved to https://github.com/ltdrdata/comfyui-connection-helper @@ -13,78 +14,26 @@ ## Installation -### Installation[method1] (General installation method: ComfyUI-Manager only) +* When installing the latest ComfyUI, it will be automatically installed as a dependency, so manual installation is no longer necessary. -To install ComfyUI-Manager in addition to an existing installation of ComfyUI, you can follow the following steps: +* Manual installation of the nightly version: + * Clone to a temporary directory (**Note:** Do **not** clone into `ComfyUI/custom_nodes`.) + ``` + git clone https://github.com/Comfy-Org/ComfyUI-Manager + ``` + * Install via pip + ``` + cd ComfyUI-Manager + pip install . + ``` -1. goto `ComfyUI/custom_nodes` dir in terminal(cmd) -2. `git clone https://github.com/ltdrdata/ComfyUI-Manager comfyui-manager` -3. Restart ComfyUI - - -### Installation[method2] (Installation for portable ComfyUI version: ComfyUI-Manager only) -1. install git -- https://git-scm.com/download/win -- standalone version -- select option: use windows default console window -2. Download [scripts/install-manager-for-portable-version.bat](https://github.com/ltdrdata/ComfyUI-Manager/raw/main/scripts/install-manager-for-portable-version.bat) into installed `"ComfyUI_windows_portable"` directory -- Don't click. Right click the link and use save as... -3. double click `install-manager-for-portable-version.bat` batch file - -![portable-install](https://raw.githubusercontent.com/ltdrdata/ComfyUI-extension-tutorials/Main/ComfyUI-Manager/images/portable-install.jpg) - - -### Installation[method3] (Installation through comfy-cli: install ComfyUI and ComfyUI-Manager at once.) -> RECOMMENDED: comfy-cli provides various features to manage ComfyUI from the CLI. - -* **prerequisite: python 3, git** - -Windows: -```commandline -python -m venv venv -venv\Scripts\activate -pip install comfy-cli -comfy install -``` - -Linux/OSX: -```commandline -python -m venv venv -. venv/bin/activate -pip install comfy-cli -comfy install -``` * See also: https://github.com/Comfy-Org/comfy-cli -### Installation[method4] (Installation for linux+venv: ComfyUI + ComfyUI-Manager) +## Front-end -To install ComfyUI with ComfyUI-Manager on Linux using a venv environment, you can follow these steps: -* **prerequisite: python-is-python3, python3-venv, git** - -1. Download [scripts/install-comfyui-venv-linux.sh](https://github.com/ltdrdata/ComfyUI-Manager/raw/main/scripts/install-comfyui-venv-linux.sh) into empty install directory -- Don't click. Right click the link and use save as... -- ComfyUI will be installed in the subdirectory of the specified directory, and the directory will contain the generated executable script. -2. `chmod +x install-comfyui-venv-linux.sh` -3. `./install-comfyui-venv-linux.sh` - -### Installation Precautions -* **DO**: `ComfyUI-Manager` files must be accurately located in the path `ComfyUI/custom_nodes/comfyui-manager` - * Installing in a compressed file format is not recommended. -* **DON'T**: Decompress directly into the `ComfyUI/custom_nodes` location, resulting in the Manager contents like `__init__.py` being placed directly in that directory. - * You have to remove all ComfyUI-Manager files from `ComfyUI/custom_nodes` -* **DON'T**: In a form where decompression occurs in a path such as `ComfyUI/custom_nodes/ComfyUI-Manager/ComfyUI-Manager`. -* **DON'T**: In a form where decompression occurs in a path such as `ComfyUI/custom_nodes/ComfyUI-Manager-main`. - * In such cases, `ComfyUI-Manager` may operate, but it won't be recognized within `ComfyUI-Manager`, and updates cannot be performed. It also poses the risk of duplicate installations. Remove it and install properly via `git clone` method. - - -You can execute ComfyUI by running either `./run_gpu.sh` or `./run_cpu.sh` depending on your system configuration. - -## Colab Notebook -This repository provides Colab notebooks that allow you to install and use ComfyUI, including ComfyUI-Manager. To use ComfyUI, [click on this link](https://colab.research.google.com/github/ltdrdata/ComfyUI-Manager/blob/main/notebooks/comfyui_colab_with_manager.ipynb). -* Support for installing ComfyUI -* Support for basic installation of ComfyUI-Manager -* Support for automatically installing dependencies of custom nodes upon restarting Colab notebooks. +* The built-in front-end of ComfyUI-Manager is the legacy front-end. The front-end for ComfyUI-Manager is now provided via [ComfyUI Frontend](https://github.com/Comfy-Org/ComfyUI_frontend). +* To enable the legacy front-end, set the environment variable `ENABLE_LEGACY_COMFYUI_MANAGER_FRONT` to `true` before running. ## How To Use @@ -266,13 +215,14 @@ The following settings are applied based on the section marked as `is_default`. downgrade_blacklist = security_level = strong|normal|normal-|weak> always_lazy_install = - network_mode = public|private|offline> + network_mode = public|private|offline|personal_cloud> ``` * network_mode: - public: An environment that uses a typical public network. - private: An environment that uses a closed network, where a private node DB is configured via `channel_url`. (Uses cache if available) - offline: An environment that does not use any external connections when using an offline network. (Uses cache if available) + - personal_cloud: Applies relaxed security features in cloud environments such as Google Colab or Runpod, where strong security is not required. ## Additional Feature @@ -363,31 +313,33 @@ When you run the `scan.sh` script: ## Security policy - * Edit `config.ini` file: add `security_level = ` - * `strong` - * doesn't allow `high` and `middle` level risky feature - * `normal` - * doesn't allow `high` level risky feature - * `middle` level risky feature is available - * `normal-` - * doesn't allow `high` level risky feature if `--listen` is specified and not starts with `127.` - * `middle` level risky feature is available - * `weak` - * all feature is available - - * `high` level risky features - * `Install via git url`, `pip install` - * Installation of custom nodes registered not in the `default channel`. - * Fix custom nodes - - * `middle` level risky features - * Uninstall/Update - * Installation of custom nodes registered in the `default channel`. - * Restore/Remove Snapshot - * Restart - - * `low` level risky features - * Update ComfyUI + +The security settings are applied based on whether the ComfyUI server's listener is non-local and whether the network mode is set to `personal_cloud`. + +* **non-local**: When the server is launched with `--listen` and is bound to a network range other than the local `127.` range, allowing remote IP access. +* **personal\_cloud**: When the `network_mode` is set to `personal_cloud`. + + +### Risky Level Table + +| Risky Level | features | +|-------------|---------------------------------------------------------------------------------------------------------------------------------------| +| high+ | * `Install via git url`, `pip install`
* Installation of nodepack registered not in the `default channel`. | +| high | * Fix nodepack | +| middle+ | * Uninstall/Update
* Installation of nodepack registered in the `default channel`.
* Restore/Remove Snapshot
* Install model | +| middle | * Restart | +| low | * Update ComfyUI | + + +### Security Level Table + +| Security Level | local | non-local (personal_cloud) | non-local (not personal_cloud) | +|----------------|--------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------|--------------------------------| +| strong | * Only `weak` level risky features are allowed | * Only `weak` level risky features are allowed | * Only `weak` level risky features are allowed | +| normal | * `high+` and `high` level risky features are not allowed
* `middle+` and `middle` level risky features are available | * `high+` and `high` level risky features are not allowed
* `middle+` and `middle` level risky features are available | * `high+`, `high` and `middle+` level risky features are not allowed
* `middle` level risky features are available +| normal- | * All features are available | * `high+` and `high` level risky features are not allowed
* `middle+` and `middle` level risky features are available | * `high+`, `high` and `middle+` level risky features are not allowed
* `middle` level risky features are available +| weak | * All features are available | * All features are available | * `high+` and `middle+` level risky features are not allowed
* `high`, `middle` and `low` level risky features are available + # Disclaimer diff --git a/__init__.py b/__init__.py deleted file mode 100644 index 65aae69c..00000000 --- a/__init__.py +++ /dev/null @@ -1,25 +0,0 @@ -""" -This file is the entry point for the ComfyUI-Manager package, handling CLI-only mode and initial setup. -""" - -import os -import sys - -cli_mode_flag = os.path.join(os.path.dirname(__file__), '.enable-cli-only-mode') - -if not os.path.exists(cli_mode_flag): - sys.path.append(os.path.join(os.path.dirname(__file__), "glob")) - import manager_server # noqa: F401 - import share_3rdparty # noqa: F401 - import cm_global - - if not cm_global.disable_front and not 'DISABLE_COMFYUI_MANAGER_FRONT' in os.environ: - WEB_DIRECTORY = "js" -else: - print("\n[ComfyUI-Manager] !! cli-only-mode is enabled !!\n") - -NODE_CLASS_MAPPINGS = {} -__all__ = ['NODE_CLASS_MAPPINGS'] - - - diff --git a/channels.list.template b/channels.list.template deleted file mode 100644 index 9a8d6877..00000000 --- a/channels.list.template +++ /dev/null @@ -1,6 +0,0 @@ -default::https://raw.githubusercontent.com/ltdrdata/ComfyUI-Manager/main -recent::https://raw.githubusercontent.com/ltdrdata/ComfyUI-Manager/main/node_db/new -legacy::https://raw.githubusercontent.com/ltdrdata/ComfyUI-Manager/main/node_db/legacy -forked::https://raw.githubusercontent.com/ltdrdata/ComfyUI-Manager/main/node_db/forked -dev::https://raw.githubusercontent.com/ltdrdata/ComfyUI-Manager/main/node_db/dev -tutorial::https://raw.githubusercontent.com/ltdrdata/ComfyUI-Manager/main/node_db/tutorial \ No newline at end of file diff --git a/cm-cli.sh b/cm-cli.sh deleted file mode 100755 index b1a21ca5..00000000 --- a/cm-cli.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/bash -python cm-cli.py $* diff --git a/glob/README.md b/comfyui_manager/README.md similarity index 75% rename from glob/README.md rename to comfyui_manager/README.md index 375b7fa7..4672e06a 100644 --- a/glob/README.md +++ b/comfyui_manager/README.md @@ -2,20 +2,16 @@ This directory contains the Python backend modules that power ComfyUI-Manager, handling the core functionality of node management, downloading, security, and server operations. -## Core Modules +## Directory Structure +- **glob/** - code for new cacheless ComfyUI-Manager +- **legacy/** - code for legacy ComfyUI-Manager +## Core Modules - **manager_core.py**: The central implementation of management functions, handling configuration, installation, updates, and node management. - **manager_server.py**: Implements server functionality and API endpoints for the web interface to interact with the backend. -- **manager_downloader.py**: Handles downloading operations for models, extensions, and other resources. -- **manager_util.py**: Provides utility functions used throughout the system. ## Specialized Modules -- **cm_global.py**: Maintains global variables and state management across the system. -- **cnr_utils.py**: Helper utilities for interacting with the custom node registry (CNR). -- **git_utils.py**: Git-specific utilities for repository operations. -- **node_package.py**: Handles the packaging and installation of node extensions. -- **security_check.py**: Implements the multi-level security system for installation safety. - **share_3rdparty.py**: Manages integration with third-party sharing platforms. ## Architecture diff --git a/comfyui_manager/__init__.py b/comfyui_manager/__init__.py new file mode 100644 index 00000000..c96a34ee --- /dev/null +++ b/comfyui_manager/__init__.py @@ -0,0 +1,104 @@ +import os +import logging +from aiohttp import web +from .common.manager_security import HANDLER_POLICY +from .common import manager_security +from comfy.cli_args import args + + +def prestartup(): + from . import prestartup_script # noqa: F401 + logging.info('[PRE] ComfyUI-Manager') + + +def start(): + logging.info('[START] ComfyUI-Manager') + from .common import cm_global # noqa: F401 + + if not args.disable_manager: + if args.enable_manager_legacy_ui: + try: + from .legacy import manager_server # noqa: F401 + from .legacy import share_3rdparty # noqa: F401 + from .legacy import manager_core as core + import nodes + + logging.info("[ComfyUI-Manager] Legacy UI is enabled.") + nodes.EXTENSION_WEB_DIRS['comfyui-manager-legacy'] = os.path.join(os.path.dirname(__file__), 'js') + except Exception as e: + print("Error enabling legacy ComfyUI Manager frontend:", e) + core = None + else: + from .glob import manager_server # noqa: F401 + from .glob import share_3rdparty # noqa: F401 + from .glob import manager_core as core + + if core is not None: + manager_security.is_personal_cloud_mode = core.get_config()['network_mode'].lower() == 'personal_cloud' + + +def should_be_disabled(fullpath:str) -> bool: + """ + 1. Disables the legacy ComfyUI-Manager. + 2. The blocklist can be expanded later based on policies. + """ + if not args.disable_manager: + # In cases where installation is done via a zip archive, the directory name may not be comfyui-manager, and it may not contain a git repository. + # It is assumed that any installed legacy ComfyUI-Manager will have at least 'comfyui-manager' in its directory name. + dir_name = os.path.basename(fullpath).lower() + if 'comfyui-manager' in dir_name: + return True + + return False + + +def get_client_ip(request): + peername = request.transport.get_extra_info("peername") + if peername is not None: + host, port = peername + return host + + return "unknown" + + +def create_middleware(): + connected_clients = set() + is_local_mode = manager_security.is_loopback(args.listen) + + @web.middleware + async def manager_middleware(request: web.Request, handler): + nonlocal connected_clients + + # security policy for remote environments + prev_client_count = len(connected_clients) + client_ip = get_client_ip(request) + connected_clients.add(client_ip) + next_client_count = len(connected_clients) + + if prev_client_count == 1 and next_client_count > 1: + manager_security.multiple_remote_alert() + + policy = manager_security.get_handler_policy(handler) + is_banned = False + + # policy check + if len(connected_clients) > 1: + if is_local_mode: + if HANDLER_POLICY.MULTIPLE_REMOTE_BAN_NON_LOCAL in policy: + is_banned = True + if HANDLER_POLICY.MULTIPLE_REMOTE_BAN_NOT_PERSONAL_CLOUD in policy: + is_banned = not manager_security.is_personal_cloud_mode + + if HANDLER_POLICY.BANNED in policy: + is_banned = True + + if is_banned: + logging.warning(f"[Manager] Banning request from {client_ip}: {request.path}") + response = web.Response(text="[Manager] This request is banned.", status=403) + else: + response: web.Response = await handler(request) + + return response + + return manager_middleware + \ No newline at end of file diff --git a/alter-list.json b/comfyui_manager/alter-list.json similarity index 100% rename from alter-list.json rename to comfyui_manager/alter-list.json diff --git a/comfyui_manager/channels.list.template b/comfyui_manager/channels.list.template new file mode 100644 index 00000000..eeeae938 --- /dev/null +++ b/comfyui_manager/channels.list.template @@ -0,0 +1,6 @@ +default::https://raw.githubusercontent.com/Comfy-Org/ComfyUI-Manager/main +recent::https://raw.githubusercontent.com/Comfy-Org/ComfyUI-Manager/main/node_db/new +legacy::https://raw.githubusercontent.com/Comfy-Org/ComfyUI-Manager/main/node_db/legacy +forked::https://raw.githubusercontent.com/Comfy-Org/ComfyUI-Manager/main/node_db/forked +dev::https://raw.githubusercontent.com/Comfy-Org/ComfyUI-Manager/main/node_db/dev +tutorial::https://raw.githubusercontent.com/Comfy-Org/ComfyUI-Manager/main/node_db/tutorial \ No newline at end of file diff --git a/snapshots/the_snapshot_files_are_located_here b/comfyui_manager/cm_cli/__init__.py similarity index 100% rename from snapshots/the_snapshot_files_are_located_here rename to comfyui_manager/cm_cli/__init__.py diff --git a/cm-cli.py b/comfyui_manager/cm_cli/__main__.py similarity index 93% rename from cm-cli.py rename to comfyui_manager/cm_cli/__main__.py index 04043423..d273bc23 100644 --- a/cm-cli.py +++ b/comfyui_manager/cm_cli/__main__.py @@ -15,31 +15,31 @@ import git import importlib -sys.path.append(os.path.dirname(__file__)) -sys.path.append(os.path.join(os.path.dirname(__file__), "glob")) - -import manager_util +from ..common import manager_util # read env vars # COMFYUI_FOLDERS_BASE_PATH is not required in cm-cli.py # `comfy_path` should be resolved before importing manager_core -comfy_path = os.environ.get('COMFYUI_PATH') -if comfy_path is None: - try: - import folder_paths - comfy_path = os.path.join(os.path.dirname(folder_paths.__file__)) - except: - print("\n[bold yellow]WARN: The `COMFYUI_PATH` environment variable is not set. Assuming `custom_nodes/ComfyUI-Manager/../../` as the ComfyUI path.[/bold yellow]", file=sys.stderr) - comfy_path = os.path.abspath(os.path.join(manager_util.comfyui_manager_path, '..', '..')) -# This should be placed here +comfy_path = os.environ.get('COMFYUI_PATH') + +if comfy_path is None: + print("[bold red]cm-cli: environment variable 'COMFYUI_PATH' is not specified.[/bold red]") + exit(-1) + sys.path.append(comfy_path) +if not os.path.exists(os.path.join(comfy_path, 'folder_paths.py')): + print("[bold red]cm-cli: '{comfy_path}' is not a valid 'COMFYUI_PATH' location.[/bold red]") + exit(-1) + + import utils.extra_config -import cm_global -import manager_core as core -from manager_core import unified_manager -import cnr_utils +from ..common import cm_global +from ..legacy import manager_core as core +from ..common import context +from ..legacy.manager_core import unified_manager +from ..common import cnr_utils comfyui_manager_path = os.path.abspath(os.path.dirname(__file__)) @@ -66,7 +66,7 @@ def check_comfyui_hash(): repo = git.Repo(comfy_path) core.comfy_ui_revision = len(list(repo.iter_commits('HEAD'))) core.comfy_ui_commit_datetime = repo.head.commit.committed_datetime - except: + except Exception: print('[bold yellow]INFO: Frozen ComfyUI mode.[/bold yellow]') core.comfy_ui_revision = 0 core.comfy_ui_commit_datetime = 0 @@ -82,7 +82,7 @@ def read_downgrade_blacklist(): try: import configparser config = configparser.ConfigParser(strict=False) - config.read(core.manager_config.path) + config.read(context.manager_config_path) default_conf = config['default'] if 'downgrade_blacklist' in default_conf: @@ -90,7 +90,7 @@ def read_downgrade_blacklist(): items = [x.strip() for x in items if x != ''] cm_global.pip_downgrade_blacklist += items cm_global.pip_downgrade_blacklist = list(set(cm_global.pip_downgrade_blacklist)) - except: + except Exception: pass @@ -105,7 +105,7 @@ class Ctx: self.no_deps = False self.mode = 'cache' self.user_directory = None - self.custom_nodes_paths = [os.path.join(core.comfy_base_path, 'custom_nodes')] + self.custom_nodes_paths = [os.path.join(context.comfy_base_path, 'custom_nodes')] self.manager_files_directory = os.path.dirname(__file__) if Ctx.folder_paths is None: @@ -143,14 +143,14 @@ class Ctx: if os.path.exists(extra_model_paths_yaml): utils.extra_config.load_extra_path_config(extra_model_paths_yaml) - core.update_user_directory(user_directory) + context.update_user_directory(user_directory) - if os.path.exists(core.manager_pip_overrides_path): - with open(core.manager_pip_overrides_path, 'r', encoding="UTF-8", errors="ignore") as json_file: + if os.path.exists(context.manager_pip_overrides_path): + with open(context.manager_pip_overrides_path, 'r', encoding="UTF-8", errors="ignore") as json_file: cm_global.pip_overrides = json.load(json_file) - if os.path.exists(core.manager_pip_blacklist_path): - with open(core.manager_pip_blacklist_path, 'r', encoding="UTF-8", errors="ignore") as f: + if os.path.exists(context.manager_pip_blacklist_path): + with open(context.manager_pip_blacklist_path, 'r', encoding="UTF-8", errors="ignore") as f: for x in f.readlines(): y = x.strip() if y != '': @@ -163,15 +163,15 @@ class Ctx: @staticmethod def get_startup_scripts_path(): - return os.path.join(core.manager_startup_script_path, "install-scripts.txt") + return os.path.join(context.manager_startup_script_path, "install-scripts.txt") @staticmethod def get_restore_snapshot_path(): - return os.path.join(core.manager_startup_script_path, "restore-snapshot.json") + return os.path.join(context.manager_startup_script_path, "restore-snapshot.json") @staticmethod def get_snapshot_path(): - return core.manager_snapshot_path + return context.manager_snapshot_path @staticmethod def get_custom_nodes_paths(): @@ -438,8 +438,11 @@ def show_list(kind, simple=False): flag = kind in ['all', 'cnr', 'installed', 'enabled'] for k, v in unified_manager.active_nodes.items(): if flag: - cnr = unified_manager.cnr_map[k] - processed[k] = "[ ENABLED ] ", cnr['name'], k, cnr['publisher']['name'], v[0] + cnr = unified_manager.cnr_map.get(k) + if cnr: + processed[k] = "[ ENABLED ] ", cnr['name'], k, cnr['publisher']['name'], v[0] + else: + processed[k] = None else: processed[k] = None @@ -459,8 +462,11 @@ def show_list(kind, simple=False): continue if flag: - cnr = unified_manager.cnr_map[k] - processed[k] = "[ DISABLED ] ", cnr['name'], k, cnr['publisher']['name'], ", ".join(list(v.keys())) + cnr = unified_manager.cnr_map.get(k) # NOTE: can this be None if removed from CNR after installed + if cnr: + processed[k] = "[ DISABLED ] ", cnr['name'], k, cnr['publisher']['name'], ", ".join(list(v.keys())) + else: + processed[k] = None else: processed[k] = None @@ -469,8 +475,11 @@ def show_list(kind, simple=False): continue if flag: - cnr = unified_manager.cnr_map[k] - processed[k] = "[ DISABLED ] ", cnr['name'], k, cnr['publisher']['name'], 'nightly' + cnr = unified_manager.cnr_map.get(k) + if cnr: + processed[k] = "[ DISABLED ] ", cnr['name'], k, cnr['publisher']['name'], 'nightly' + else: + processed[k] = None else: processed[k] = None @@ -490,9 +499,12 @@ def show_list(kind, simple=False): continue if flag: - cnr = unified_manager.cnr_map[k] - ver_spec = v['latest_version']['version'] if 'latest_version' in v else '0.0.0' - processed[k] = "[ NOT INSTALLED ] ", cnr['name'], k, cnr['publisher']['name'], ver_spec + cnr = unified_manager.cnr_map.get(k) + if cnr: + ver_spec = v['latest_version']['version'] if 'latest_version' in v else '0.0.0' + processed[k] = "[ NOT INSTALLED ] ", cnr['name'], k, cnr['publisher']['name'], ver_spec + else: + processed[k] = None else: processed[k] = None @@ -658,7 +670,7 @@ def install( cmd_ctx.set_channel_mode(channel, mode) cmd_ctx.set_no_deps(no_deps) - pip_fixer = manager_util.PIPFixer(manager_util.get_installed_packages(), comfy_path, core.manager_files_path) + pip_fixer = manager_util.PIPFixer(manager_util.get_installed_packages(), comfy_path, context.manager_files_path) for_each_nodes(nodes, act=install_node, exit_on_fail=exit_on_fail) pip_fixer.fix_broken() @@ -696,7 +708,7 @@ def reinstall( cmd_ctx.set_channel_mode(channel, mode) cmd_ctx.set_no_deps(no_deps) - pip_fixer = manager_util.PIPFixer(manager_util.get_installed_packages(), comfy_path, core.manager_files_path) + pip_fixer = manager_util.PIPFixer(manager_util.get_installed_packages(), comfy_path, context.manager_files_path) for_each_nodes(nodes, act=reinstall_node) pip_fixer.fix_broken() @@ -750,7 +762,7 @@ def update( if 'all' in nodes: asyncio.run(auto_save_snapshot()) - pip_fixer = manager_util.PIPFixer(manager_util.get_installed_packages(), comfy_path, core.manager_files_path) + pip_fixer = manager_util.PIPFixer(manager_util.get_installed_packages(), comfy_path, context.manager_files_path) for x in nodes: if x.lower() in ['comfyui', 'comfy', 'all']: @@ -851,7 +863,7 @@ def fix( if 'all' in nodes: asyncio.run(auto_save_snapshot()) - pip_fixer = manager_util.PIPFixer(manager_util.get_installed_packages(), comfy_path, core.manager_files_path) + pip_fixer = manager_util.PIPFixer(manager_util.get_installed_packages(), comfy_path, context.manager_files_path) for_each_nodes(nodes, fix_node, allow_all=True) pip_fixer.fix_broken() @@ -1128,7 +1140,7 @@ def restore_snapshot( print(f"[bold red]ERROR: `{snapshot_path}` is not exists.[/bold red]") exit(1) - pip_fixer = manager_util.PIPFixer(manager_util.get_installed_packages(), comfy_path, core.manager_files_path) + pip_fixer = manager_util.PIPFixer(manager_util.get_installed_packages(), comfy_path, context.manager_files_path) try: asyncio.run(core.restore_snapshot(snapshot_path, extras)) except Exception: @@ -1160,7 +1172,7 @@ def restore_dependencies( total = len(node_paths) i = 1 - pip_fixer = manager_util.PIPFixer(manager_util.get_installed_packages(), comfy_path, core.manager_files_path) + pip_fixer = manager_util.PIPFixer(manager_util.get_installed_packages(), comfy_path, context.manager_files_path) for x in node_paths: print("----------------------------------------------------------------------------------------------------") print(f"Restoring [{i}/{total}]: {x}") @@ -1179,7 +1191,7 @@ def post_install( ): path = os.path.expanduser(path) - pip_fixer = manager_util.PIPFixer(manager_util.get_installed_packages(), comfy_path, core.manager_files_path) + pip_fixer = manager_util.PIPFixer(manager_util.get_installed_packages(), comfy_path, context.manager_files_path) unified_manager.execute_install_script('', path, instant_execution=True) pip_fixer.fix_broken() @@ -1219,11 +1231,11 @@ def install_deps( with open(deps, 'r', encoding="UTF-8", errors="ignore") as json_file: try: json_obj = json.load(json_file) - except: + except Exception: print(f"[bold red]Invalid json file: {deps}[/bold red]") exit(1) - pip_fixer = manager_util.PIPFixer(manager_util.get_installed_packages(), comfy_path, core.manager_files_path) + pip_fixer = manager_util.PIPFixer(manager_util.get_installed_packages(), comfy_path, context.manager_files_path) for k in json_obj['custom_nodes'].keys(): state = core.simple_check_custom_node(k) if state == 'installed': @@ -1280,6 +1292,10 @@ def export_custom_node_ids( print(f"{x['id']}@unknown", file=output_file) +def main(): + app() + + if __name__ == '__main__': sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0]) sys.exit(app()) diff --git a/comfyui_manager/common/README.md b/comfyui_manager/common/README.md new file mode 100644 index 00000000..bba07e62 --- /dev/null +++ b/comfyui_manager/common/README.md @@ -0,0 +1,16 @@ +# ComfyUI-Manager: Core Backend (glob) + +This directory contains the Python backend modules that power ComfyUI-Manager, handling the core functionality of node management, downloading, security, and server operations. + +## Core Modules + +- **manager_downloader.py**: Handles downloading operations for models, extensions, and other resources. +- **manager_util.py**: Provides utility functions used throughout the system. + +## Specialized Modules + +- **cm_global.py**: Maintains global variables and state management across the system. +- **cnr_utils.py**: Helper utilities for interacting with the custom node registry (CNR). +- **git_utils.py**: Git-specific utilities for repository operations. +- **node_package.py**: Handles the packaging and installation of node extensions. +- **security_check.py**: Implements the multi-level security system for installation safety. diff --git a/comfyui_manager/common/__init__.py b/comfyui_manager/common/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/glob/cm_global.py b/comfyui_manager/common/cm_global.py similarity index 100% rename from glob/cm_global.py rename to comfyui_manager/common/cm_global.py diff --git a/glob/cnr_utils.py b/comfyui_manager/common/cnr_utils.py similarity index 96% rename from glob/cnr_utils.py rename to comfyui_manager/common/cnr_utils.py index 95d260db..1cdb8472 100644 --- a/glob/cnr_utils.py +++ b/comfyui_manager/common/cnr_utils.py @@ -6,8 +6,9 @@ import time from dataclasses import dataclass from typing import List -import manager_core -import manager_util +from . import context +from . import manager_util + import requests import toml @@ -47,9 +48,9 @@ async def _get_cnr_data(cache_mode=True, dont_wait=True): # Get ComfyUI version tag if is_desktop: # extract version from pyproject.toml instead of git tag - comfyui_ver = manager_core.get_current_comfyui_ver() or 'unknown' + comfyui_ver = context.get_current_comfyui_ver() or 'unknown' else: - comfyui_ver = manager_core.get_comfyui_tag() or 'unknown' + comfyui_ver = context.get_comfyui_tag() or 'unknown' if is_desktop: if is_windows: @@ -111,7 +112,7 @@ async def _get_cnr_data(cache_mode=True, dont_wait=True): json_obj = await fetch_all() manager_util.save_to_cache(uri, json_obj) return json_obj['nodes'] - except: + except Exception: res = {} print("Cannot connect to comfyregistry.") finally: @@ -236,7 +237,7 @@ def generate_cnr_id(fullpath, cnr_id): if not os.path.exists(cnr_id_path): with open(cnr_id_path, "w") as f: return f.write(cnr_id) - except: + except Exception: print(f"[ComfyUI Manager] unable to create file: {cnr_id_path}") @@ -246,7 +247,7 @@ def read_cnr_id(fullpath): if os.path.exists(cnr_id_path): with open(cnr_id_path) as f: return f.read().strip() - except: + except Exception: pass return None diff --git a/comfyui_manager/common/context.py b/comfyui_manager/common/context.py new file mode 100644 index 00000000..de762a28 --- /dev/null +++ b/comfyui_manager/common/context.py @@ -0,0 +1,108 @@ +import sys +import os +import logging +from . import manager_util +import toml +import git + + +# read env vars +comfy_path: str = os.environ.get('COMFYUI_PATH') +comfy_base_path = os.environ.get('COMFYUI_FOLDERS_BASE_PATH') + +if comfy_path is None: + try: + comfy_path = os.path.abspath(os.path.dirname(sys.modules['__main__'].__file__)) + os.environ['COMFYUI_PATH'] = comfy_path + except Exception: + logging.error("[ComfyUI-Manager] environment variable 'COMFYUI_PATH' is not specified.") + exit(-1) + +if comfy_base_path is None: + comfy_base_path = comfy_path + +channel_list_template_path = os.path.join(manager_util.comfyui_manager_path, 'channels.list.template') +git_script_path = os.path.join(manager_util.comfyui_manager_path, "git_helper.py") + +manager_files_path = None +manager_config_path = None +manager_channel_list_path = None +manager_startup_script_path:str = None +manager_snapshot_path = None +manager_pip_overrides_path = None +manager_pip_blacklist_path = None +manager_components_path = None +manager_batch_history_path = None + +def update_user_directory(user_dir): + global manager_files_path + global manager_config_path + global manager_channel_list_path + global manager_startup_script_path + global manager_snapshot_path + global manager_pip_overrides_path + global manager_pip_blacklist_path + global manager_components_path + global manager_batch_history_path + + manager_files_path = os.path.abspath(os.path.join(user_dir, 'default', 'ComfyUI-Manager')) + if not os.path.exists(manager_files_path): + os.makedirs(manager_files_path) + + manager_snapshot_path = os.path.join(manager_files_path, "snapshots") + if not os.path.exists(manager_snapshot_path): + os.makedirs(manager_snapshot_path) + + manager_startup_script_path = os.path.join(manager_files_path, "startup-scripts") + if not os.path.exists(manager_startup_script_path): + os.makedirs(manager_startup_script_path) + + manager_config_path = os.path.join(manager_files_path, 'config.ini') + manager_channel_list_path = os.path.join(manager_files_path, 'channels.list') + manager_pip_overrides_path = os.path.join(manager_files_path, "pip_overrides.json") + manager_pip_blacklist_path = os.path.join(manager_files_path, "pip_blacklist.list") + manager_components_path = os.path.join(manager_files_path, "components") + manager_util.cache_dir = os.path.join(manager_files_path, "cache") + manager_batch_history_path = os.path.join(manager_files_path, "batch_history") + + if not os.path.exists(manager_util.cache_dir): + os.makedirs(manager_util.cache_dir) + + if not os.path.exists(manager_batch_history_path): + os.makedirs(manager_batch_history_path) + +try: + import folder_paths + update_user_directory(folder_paths.get_user_directory()) + +except Exception: + # fallback: + # This case is only possible when running with cm-cli, and in practice, this case is not actually used. + update_user_directory(os.path.abspath(manager_util.comfyui_manager_path)) + + +def get_current_comfyui_ver(): + """ + Extract version from pyproject.toml + """ + toml_path = os.path.join(comfy_path, 'pyproject.toml') + if not os.path.exists(toml_path): + return None + else: + try: + with open(toml_path, "r", encoding="utf-8") as f: + data = toml.load(f) + + project = data.get('project', {}) + return project.get('version') + except Exception: + return None + + +def get_comfyui_tag(): + try: + with git.Repo(comfy_path) as repo: + return repo.git.describe('--tags') + except Exception: + return None + diff --git a/comfyui_manager/common/enums.py b/comfyui_manager/common/enums.py new file mode 100644 index 00000000..ed60e86b --- /dev/null +++ b/comfyui_manager/common/enums.py @@ -0,0 +1,18 @@ +import enum + +class NetworkMode(enum.Enum): + PUBLIC = "public" + PRIVATE = "private" + OFFLINE = "offline" + PERSONAL_CLOUD = "personal_cloud" + +class SecurityLevel(enum.Enum): + STRONG = "strong" + NORMAL = "normal" + NORMAL_MINUS = "normal-minus" + WEAK = "weak" + +class DBMode(enum.Enum): + LOCAL = "local" + CACHE = "cache" + REMOTE = "remote" \ No newline at end of file diff --git a/git_helper.py b/comfyui_manager/common/git_helper.py similarity index 96% rename from git_helper.py rename to comfyui_manager/common/git_helper.py index e79b43a6..5fc51478 100644 --- a/git_helper.py +++ b/comfyui_manager/common/git_helper.py @@ -15,9 +15,12 @@ comfy_path = os.environ.get('COMFYUI_PATH') git_exe_path = os.environ.get('GIT_EXE_PATH') if comfy_path is None: - print("\nWARN: The `COMFYUI_PATH` environment variable is not set. Assuming `custom_nodes/ComfyUI-Manager/../../` as the ComfyUI path.", file=sys.stderr) - comfy_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')) + print("git_helper: environment variable 'COMFYUI_PATH' is not specified.") + exit(-1) +if not os.path.exists(os.path.join(comfy_path, 'folder_paths.py')): + print("git_helper: '{comfy_path}' is not a valid 'COMFYUI_PATH' location.") + exit(-1) def download_url(url, dest_folder, filename=None): # Ensure the destination folder exists @@ -153,27 +156,27 @@ def switch_to_default_branch(repo): default_branch = repo.git.symbolic_ref(f'refs/remotes/{remote_name}/HEAD').replace(f'refs/remotes/{remote_name}/', '') repo.git.checkout(default_branch) return True - except: + except Exception: # try checkout master # try checkout main if failed try: repo.git.checkout(repo.heads.master) return True - except: + except Exception: try: if remote_name is not None: repo.git.checkout('-b', 'master', f'{remote_name}/master') return True - except: + except Exception: try: repo.git.checkout(repo.heads.main) return True - except: + except Exception: try: if remote_name is not None: repo.git.checkout('-b', 'main', f'{remote_name}/main') return True - except: + except Exception: pass print("[ComfyUI Manager] Failed to switch to the default branch") @@ -444,7 +447,7 @@ def restore_pip_snapshot(pips, options): res = 1 try: res = subprocess.check_call([sys.executable, '-m', 'pip', 'install'] + non_url) - except: + except Exception: pass # fallback @@ -453,7 +456,7 @@ def restore_pip_snapshot(pips, options): res = 1 try: res = subprocess.check_call([sys.executable, '-m', 'pip', 'install', x]) - except: + except Exception: pass if res != 0: @@ -464,7 +467,7 @@ def restore_pip_snapshot(pips, options): res = 1 try: res = subprocess.check_call([sys.executable, '-m', 'pip', 'install', x]) - except: + except Exception: pass if res != 0: @@ -475,7 +478,7 @@ def restore_pip_snapshot(pips, options): res = 1 try: res = subprocess.check_call([sys.executable, '-m', 'pip', 'install', x]) - except: + except Exception: pass if res != 0: diff --git a/glob/git_utils.py b/comfyui_manager/common/git_utils.py similarity index 100% rename from glob/git_utils.py rename to comfyui_manager/common/git_utils.py diff --git a/glob/manager_downloader.py b/comfyui_manager/common/manager_downloader.py similarity index 100% rename from glob/manager_downloader.py rename to comfyui_manager/common/manager_downloader.py diff --git a/comfyui_manager/common/manager_security.py b/comfyui_manager/common/manager_security.py new file mode 100644 index 00000000..6f0775aa --- /dev/null +++ b/comfyui_manager/common/manager_security.py @@ -0,0 +1,36 @@ +from enum import Enum + +is_personal_cloud_mode = False +handler_policy = {} + +class HANDLER_POLICY(Enum): + MULTIPLE_REMOTE_BAN_NON_LOCAL = 1 + MULTIPLE_REMOTE_BAN_NOT_PERSONAL_CLOUD = 2 + BANNED = 3 + + +def is_loopback(address): + import ipaddress + try: + return ipaddress.ip_address(address).is_loopback + except ValueError: + return False + + +def do_nothing(): + pass + + +def get_handler_policy(x): + return handler_policy.get(x) or set() + +def add_handler_policy(x, policy): + s = handler_policy.get(x) + if s is None: + s = set() + handler_policy[x] = s + + s.add(policy) + + +multiple_remote_alert = do_nothing diff --git a/glob/manager_util.py b/comfyui_manager/common/manager_util.py similarity index 99% rename from glob/manager_util.py rename to comfyui_manager/common/manager_util.py index b3db7b97..8cd7c211 100644 --- a/glob/manager_util.py +++ b/comfyui_manager/common/manager_util.py @@ -18,6 +18,7 @@ import shlex cache_lock = threading.Lock() +session_lock = threading.Lock() comfyui_manager_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) cache_dir = os.path.join(comfyui_manager_path, '.cache') # This path is also updated together in **manager_core.update_user_directory**. @@ -25,6 +26,9 @@ cache_dir = os.path.join(comfyui_manager_path, '.cache') # This path is also up use_uv = False bypass_ssl = False +def is_manager_pip_package(): + return not os.path.exists(os.path.join(comfyui_manager_path, '..', 'custom_nodes')) + def add_python_path_to_env(): if platform.system() != "Windows": sep = ':' @@ -50,7 +54,7 @@ def make_pip_cmd(cmd): # DON'T USE StrictVersion - cannot handle pre_release version # try: # from distutils.version import StrictVersion -# except: +# except Exception: # print(f"[ComfyUI-Manager] 'distutils' package not found. Activating fallback mode for compatibility.") class StrictVersion: def __init__(self, version_string): @@ -505,7 +509,7 @@ def robust_readlines(fullpath): try: with open(fullpath, "r") as f: return f.readlines() - except: + except Exception: encoding = None with open(fullpath, "rb") as f: raw_data = f.read() diff --git a/glob/node_package.py b/comfyui_manager/common/node_package.py similarity index 97% rename from glob/node_package.py rename to comfyui_manager/common/node_package.py index d199fa30..6ec53a9a 100644 --- a/glob/node_package.py +++ b/comfyui_manager/common/node_package.py @@ -3,7 +3,7 @@ from __future__ import annotations from dataclasses import dataclass import os -from git_utils import get_commit_hash +from .git_utils import get_commit_hash @dataclass diff --git a/glob/security_check.py b/comfyui_manager/common/security_check.py similarity index 99% rename from glob/security_check.py rename to comfyui_manager/common/security_check.py index 892e96a2..369daa9b 100644 --- a/glob/security_check.py +++ b/comfyui_manager/common/security_check.py @@ -2,7 +2,7 @@ import sys import subprocess import os -import manager_util +from . import manager_util def security_check(): diff --git a/custom-node-list.json b/comfyui_manager/custom-node-list.json old mode 100755 new mode 100644 similarity index 100% rename from custom-node-list.json rename to comfyui_manager/custom-node-list.json diff --git a/comfyui_manager/data_models/README.md b/comfyui_manager/data_models/README.md new file mode 100644 index 00000000..eadcfa9d --- /dev/null +++ b/comfyui_manager/data_models/README.md @@ -0,0 +1,68 @@ +# Data Models + +This directory contains Pydantic models for ComfyUI Manager, providing type safety, validation, and serialization for the API and internal data structures. + +## Overview + +- `generated_models.py` - All models auto-generated from OpenAPI spec +- `__init__.py` - Package exports for all models + +**Note**: All models are now auto-generated from the OpenAPI specification. Manual model files (`task_queue.py`, `state_management.py`) have been deprecated in favor of a single source of truth. + +## Generating Types from OpenAPI + +The state management models are automatically generated from the OpenAPI specification using `datamodel-codegen`. This ensures type safety and consistency between the API specification and the Python code. + +### Prerequisites + +Install the code generator: +```bash +pipx install datamodel-code-generator +``` + +### Generation Command + +To regenerate all models after updating the OpenAPI spec: + +```bash +datamodel-codegen \ + --use-subclass-enum \ + --field-constraints \ + --strict-types bytes \ + --use-double-quotes \ + --input openapi.yaml \ + --output comfyui_manager/data_models/generated_models.py \ + --output-model-type pydantic_v2.BaseModel +``` + +### When to Regenerate + +You should regenerate the models when: + +1. **Adding new API endpoints** that return new data structures +2. **Modifying existing schemas** in the OpenAPI specification +3. **Adding new state management features** that require new models + +### Important Notes + +- **Single source of truth**: All models are now generated from `openapi.yaml` +- **No manual models**: All previously manual models have been migrated to the OpenAPI spec +- **OpenAPI requirements**: New schemas must be referenced in API paths to be generated by datamodel-codegen +- **Validation**: Always validate the OpenAPI spec before generation: + ```bash + python3 -c "import yaml; yaml.safe_load(open('openapi.yaml'))" + ``` + +### Example: Adding New State Models + +1. Add your schema to `openapi.yaml` under `components/schemas/` +2. Reference the schema in an API endpoint response +3. Run the generation command above +4. Update `__init__.py` to export the new models +5. Import and use the models in your code + +### Troubleshooting + +- **Models not generated**: Ensure schemas are under `components/schemas/` (not `parameters/`) +- **Missing models**: Verify schemas are referenced in at least one API path +- **Import errors**: Check that new models are added to `__init__.py` exports \ No newline at end of file diff --git a/comfyui_manager/data_models/__init__.py b/comfyui_manager/data_models/__init__.py new file mode 100644 index 00000000..d0d5c182 --- /dev/null +++ b/comfyui_manager/data_models/__init__.py @@ -0,0 +1,137 @@ +""" +Data models for ComfyUI Manager. + +This package contains Pydantic models used throughout the ComfyUI Manager +for data validation, serialization, and type safety. + +All models are auto-generated from the OpenAPI specification to ensure +consistency between the API and implementation. +""" + +from .generated_models import ( + # Core Task Queue Models + QueueTaskItem, + TaskHistoryItem, + TaskStateMessage, + TaskExecutionStatus, + + # WebSocket Message Models + MessageTaskDone, + MessageTaskStarted, + MessageTaskFailed, + MessageUpdate, + ManagerMessageName, + + # State Management Models + BatchExecutionRecord, + ComfyUISystemState, + BatchOperation, + InstalledNodeInfo, + InstalledModelInfo, + ComfyUIVersionInfo, + + # Import Fail Info Models + ImportFailInfoBulkRequest, + ImportFailInfoBulkResponse, + ImportFailInfoItem, + ImportFailInfoItem1, + + # Other models + OperationType, + OperationResult, + ManagerPackInfo, + ManagerPackInstalled, + SelectedVersion, + ManagerChannel, + ManagerDatabaseSource, + ManagerPackState, + ManagerPackInstallType, + ManagerPack, + InstallPackParams, + UpdatePackParams, + UpdateAllPacksParams, + UpdateComfyUIParams, + FixPackParams, + UninstallPackParams, + DisablePackParams, + EnablePackParams, + UpdateAllQueryParams, + UpdateComfyUIQueryParams, + ComfyUISwitchVersionQueryParams, + QueueStatus, + ManagerMappings, + ModelMetadata, + NodePackageMetadata, + SnapshotItem, + Error, + InstalledPacksResponse, + HistoryResponse, + HistoryListResponse, + InstallType, + SecurityLevel, + RiskLevel, +) + +__all__ = [ + # Core Task Queue Models + "QueueTaskItem", + "TaskHistoryItem", + "TaskStateMessage", + "TaskExecutionStatus", + + # WebSocket Message Models + "MessageTaskDone", + "MessageTaskStarted", + "MessageTaskFailed", + "MessageUpdate", + "ManagerMessageName", + + # State Management Models + "BatchExecutionRecord", + "ComfyUISystemState", + "BatchOperation", + "InstalledNodeInfo", + "InstalledModelInfo", + "ComfyUIVersionInfo", + + # Import Fail Info Models + "ImportFailInfoBulkRequest", + "ImportFailInfoBulkResponse", + "ImportFailInfoItem", + "ImportFailInfoItem1", + + # Other models + "OperationType", + "OperationResult", + "ManagerPackInfo", + "ManagerPackInstalled", + "SelectedVersion", + "ManagerChannel", + "ManagerDatabaseSource", + "ManagerPackState", + "ManagerPackInstallType", + "ManagerPack", + "InstallPackParams", + "UpdatePackParams", + "UpdateAllPacksParams", + "UpdateComfyUIParams", + "FixPackParams", + "UninstallPackParams", + "DisablePackParams", + "EnablePackParams", + "UpdateAllQueryParams", + "UpdateComfyUIQueryParams", + "ComfyUISwitchVersionQueryParams", + "QueueStatus", + "ManagerMappings", + "ModelMetadata", + "NodePackageMetadata", + "SnapshotItem", + "Error", + "InstalledPacksResponse", + "HistoryResponse", + "HistoryListResponse", + "InstallType", + "SecurityLevel", + "RiskLevel", +] \ No newline at end of file diff --git a/comfyui_manager/data_models/generated_models.py b/comfyui_manager/data_models/generated_models.py new file mode 100644 index 00000000..0f3b9cfb --- /dev/null +++ b/comfyui_manager/data_models/generated_models.py @@ -0,0 +1,561 @@ +# generated by datamodel-codegen: +# filename: openapi.yaml +# timestamp: 2025-07-31T04:52:26+00:00 + +from __future__ import annotations + +from datetime import datetime +from enum import Enum +from typing import Any, Dict, List, Optional, Union + +from pydantic import BaseModel, Field, RootModel + + +class OperationType(str, Enum): + install = "install" + uninstall = "uninstall" + update = "update" + update_comfyui = "update-comfyui" + fix = "fix" + disable = "disable" + enable = "enable" + install_model = "install-model" + + +class OperationResult(str, Enum): + success = "success" + failed = "failed" + skipped = "skipped" + error = "error" + skip = "skip" + + +class TaskExecutionStatus(BaseModel): + status_str: OperationResult + completed: bool = Field(..., description="Whether the task completed") + messages: List[str] = Field(..., description="Additional status messages") + + +class ManagerMessageName(str, Enum): + cm_task_completed = "cm-task-completed" + cm_task_started = "cm-task-started" + cm_queue_status = "cm-queue-status" + + +class ManagerPackInfo(BaseModel): + id: str = Field( + ..., + description="Either github-author/github-repo or name of pack from the registry", + ) + version: str = Field(..., description="Semantic version or Git commit hash") + ui_id: Optional[str] = Field(None, description="Task ID - generated internally") + + +class ManagerPackInstalled(BaseModel): + ver: str = Field( + ..., + description="The version of the pack that is installed (Git commit hash or semantic version)", + ) + cnr_id: Optional[str] = Field( + None, description="The name of the pack if installed from the registry" + ) + aux_id: Optional[str] = Field( + None, + description="The name of the pack if installed from github (author/repo-name format)", + ) + enabled: bool = Field(..., description="Whether the pack is enabled") + + +class SelectedVersion(str, Enum): + latest = "latest" + nightly = "nightly" + + +class ManagerChannel(str, Enum): + default = "default" + recent = "recent" + legacy = "legacy" + forked = "forked" + dev = "dev" + tutorial = "tutorial" + + +class ManagerDatabaseSource(str, Enum): + remote = "remote" + local = "local" + cache = "cache" + + +class ManagerPackState(str, Enum): + installed = "installed" + disabled = "disabled" + not_installed = "not_installed" + import_failed = "import_failed" + needs_update = "needs_update" + + +class ManagerPackInstallType(str, Enum): + git_clone = "git-clone" + copy = "copy" + cnr = "cnr" + + +class SecurityLevel(str, Enum): + strong = "strong" + normal = "normal" + normal_ = "normal-" + weak = "weak" + + +class RiskLevel(str, Enum): + block = "block" + high_ = "high+" + high = "high" + middle_ = "middle+" + middle = "middle" + + +class UpdateState(Enum): + false = "false" + true = "true" + + +class ManagerPack(ManagerPackInfo): + author: Optional[str] = Field( + None, description="Pack author name or 'Unclaimed' if added via GitHub crawl" + ) + files: Optional[List[str]] = Field( + None, + description="Repository URLs for installation (typically contains one GitHub URL)", + ) + reference: Optional[str] = Field( + None, description="The type of installation reference" + ) + title: Optional[str] = Field(None, description="The display name of the pack") + cnr_latest: Optional[SelectedVersion] = None + repository: Optional[str] = Field(None, description="GitHub repository URL") + state: Optional[ManagerPackState] = None + update_state: Optional[UpdateState] = Field( + None, alias="update-state", description="Update availability status" + ) + stars: Optional[int] = Field(None, description="GitHub stars count") + last_update: Optional[datetime] = Field(None, description="Last update timestamp") + health: Optional[str] = Field(None, description="Health status of the pack") + description: Optional[str] = Field(None, description="Pack description") + trust: Optional[bool] = Field(None, description="Whether the pack is trusted") + install_type: Optional[ManagerPackInstallType] = None + + +class InstallPackParams(ManagerPackInfo): + selected_version: Union[str, SelectedVersion] = Field( + ..., description="Semantic version, Git commit hash, latest, or nightly" + ) + repository: Optional[str] = Field( + None, + description="GitHub repository URL (required if selected_version is nightly)", + ) + pip: Optional[List[str]] = Field(None, description="PyPi dependency names") + mode: ManagerDatabaseSource + channel: ManagerChannel + skip_post_install: Optional[bool] = Field( + None, description="Whether to skip post-installation steps" + ) + + +class UpdateAllPacksParams(BaseModel): + mode: Optional[ManagerDatabaseSource] = None + ui_id: Optional[str] = Field(None, description="Task ID - generated internally") + + +class UpdatePackParams(BaseModel): + node_name: str = Field(..., description="Name of the node package to update") + node_ver: Optional[str] = Field( + None, description="Current version of the node package" + ) + + +class UpdateComfyUIParams(BaseModel): + is_stable: Optional[bool] = Field( + True, + description="Whether to update to stable version (true) or nightly (false)", + ) + target_version: Optional[str] = Field( + None, + description="Specific version to switch to (for version switching operations)", + ) + + +class FixPackParams(BaseModel): + node_name: str = Field(..., description="Name of the node package to fix") + node_ver: str = Field(..., description="Version of the node package") + + +class UninstallPackParams(BaseModel): + node_name: str = Field(..., description="Name of the node package to uninstall") + is_unknown: Optional[bool] = Field( + False, description="Whether this is an unknown/unregistered package" + ) + + +class DisablePackParams(BaseModel): + node_name: str = Field(..., description="Name of the node package to disable") + is_unknown: Optional[bool] = Field( + False, description="Whether this is an unknown/unregistered package" + ) + + +class EnablePackParams(BaseModel): + cnr_id: str = Field( + ..., description="ComfyUI Node Registry ID of the package to enable" + ) + + +class UpdateAllQueryParams(BaseModel): + client_id: str = Field( + ..., description="Client identifier that initiated the request" + ) + ui_id: str = Field(..., description="Base UI identifier for task tracking") + mode: Optional[ManagerDatabaseSource] = None + + +class UpdateComfyUIQueryParams(BaseModel): + client_id: str = Field( + ..., description="Client identifier that initiated the request" + ) + ui_id: str = Field(..., description="UI identifier for task tracking") + stable: Optional[bool] = Field( + True, + description="Whether to update to stable version (true) or nightly (false)", + ) + + +class ComfyUISwitchVersionQueryParams(BaseModel): + ver: str = Field(..., description="Version to switch to") + client_id: str = Field( + ..., description="Client identifier that initiated the request" + ) + ui_id: str = Field(..., description="UI identifier for task tracking") + + +class QueueStatus(BaseModel): + total_count: int = Field( + ..., description="Total number of tasks (pending + running)" + ) + done_count: int = Field(..., description="Number of completed tasks") + in_progress_count: int = Field(..., description="Number of tasks currently running") + pending_count: Optional[int] = Field( + None, description="Number of tasks waiting to be executed" + ) + is_processing: bool = Field(..., description="Whether the task worker is active") + client_id: Optional[str] = Field( + None, description="Client ID (when filtered by client)" + ) + + +class ManagerMappings1(BaseModel): + title_aux: Optional[str] = Field(None, description="The display name of the pack") + + +class ManagerMappings( + RootModel[Optional[Dict[str, List[Union[List[str], ManagerMappings1]]]]] +): + root: Optional[Dict[str, List[Union[List[str], ManagerMappings1]]]] = Field( + None, description="Tuple of [node_names, metadata]" + ) + + +class ModelMetadata(BaseModel): + name: str = Field(..., description="Name of the model") + type: str = Field(..., description="Type of model") + base: Optional[str] = Field(None, description="Base model type") + save_path: Optional[str] = Field(None, description="Path for saving the model") + url: str = Field(..., description="Download URL") + filename: str = Field(..., description="Target filename") + ui_id: Optional[str] = Field(None, description="ID for UI reference") + + +class InstallType(str, Enum): + git = "git" + copy = "copy" + pip = "pip" + + +class NodePackageMetadata(BaseModel): + title: Optional[str] = Field(None, description="Display name of the node package") + name: Optional[str] = Field(None, description="Repository/package name") + files: Optional[List[str]] = Field(None, description="Source URLs for the package") + description: Optional[str] = Field( + None, description="Description of the node package functionality" + ) + install_type: Optional[InstallType] = Field(None, description="Installation method") + version: Optional[str] = Field(None, description="Version identifier") + id: Optional[str] = Field( + None, description="Unique identifier for the node package" + ) + ui_id: Optional[str] = Field(None, description="ID for UI reference") + channel: Optional[str] = Field(None, description="Source channel") + mode: Optional[str] = Field(None, description="Source mode") + + +class SnapshotItem(RootModel[str]): + root: str = Field(..., description="Name of the snapshot") + + +class Error(BaseModel): + error: str = Field(..., description="Error message") + + +class InstalledPacksResponse(RootModel[Optional[Dict[str, ManagerPackInstalled]]]): + root: Optional[Dict[str, ManagerPackInstalled]] = None + + +class HistoryListResponse(BaseModel): + ids: Optional[List[str]] = Field( + None, description="List of available batch history IDs" + ) + + +class InstalledNodeInfo(BaseModel): + name: str = Field(..., description="Node package name") + version: str = Field(..., description="Installed version") + repository_url: Optional[str] = Field(None, description="Git repository URL") + install_method: str = Field( + ..., description="Installation method (cnr, git, pip, etc.)" + ) + enabled: Optional[bool] = Field( + True, description="Whether the node is currently enabled" + ) + install_date: Optional[datetime] = Field( + None, description="ISO timestamp of installation" + ) + + +class InstalledModelInfo(BaseModel): + name: str = Field(..., description="Model filename") + path: str = Field(..., description="Full path to model file") + type: str = Field(..., description="Model type (checkpoint, lora, vae, etc.)") + size_bytes: Optional[int] = Field(None, description="File size in bytes", ge=0) + hash: Optional[str] = Field(None, description="Model file hash for verification") + install_date: Optional[datetime] = Field( + None, description="ISO timestamp when added" + ) + + +class ComfyUIVersionInfo(BaseModel): + version: str = Field(..., description="ComfyUI version string") + commit_hash: Optional[str] = Field(None, description="Git commit hash") + branch: Optional[str] = Field(None, description="Git branch name") + is_stable: Optional[bool] = Field( + False, description="Whether this is a stable release" + ) + last_updated: Optional[datetime] = Field( + None, description="ISO timestamp of last update" + ) + + +class BatchOperation(BaseModel): + operation_id: str = Field(..., description="Unique operation identifier") + operation_type: OperationType + target: str = Field( + ..., description="Target of the operation (node name, model name, etc.)" + ) + target_version: Optional[str] = Field( + None, description="Target version for the operation" + ) + result: OperationResult + error_message: Optional[str] = Field( + None, description="Error message if operation failed" + ) + start_time: datetime = Field( + ..., description="ISO timestamp when operation started" + ) + end_time: Optional[datetime] = Field( + None, description="ISO timestamp when operation completed" + ) + client_id: Optional[str] = Field( + None, description="Client that initiated the operation" + ) + + +class ComfyUISystemState(BaseModel): + snapshot_time: datetime = Field( + ..., description="ISO timestamp when snapshot was taken" + ) + comfyui_version: ComfyUIVersionInfo + frontend_version: Optional[str] = Field( + None, description="ComfyUI frontend version if available" + ) + python_version: str = Field(..., description="Python interpreter version") + platform_info: str = Field( + ..., description="Operating system and platform information" + ) + installed_nodes: Optional[Dict[str, InstalledNodeInfo]] = Field( + None, description="Map of installed node packages by name" + ) + installed_models: Optional[Dict[str, InstalledModelInfo]] = Field( + None, description="Map of installed models by name" + ) + manager_config: Optional[Dict[str, Any]] = Field( + None, description="ComfyUI Manager configuration settings" + ) + comfyui_root_path: Optional[str] = Field( + None, description="ComfyUI root installation directory" + ) + model_paths: Optional[Dict[str, List[str]]] = Field( + None, description="Map of model types to their configured paths" + ) + manager_version: Optional[str] = Field(None, description="ComfyUI Manager version") + security_level: Optional[SecurityLevel] = None + network_mode: Optional[str] = Field( + None, description="Network mode (online, offline, private)" + ) + cli_args: Optional[Dict[str, Any]] = Field( + None, description="Selected ComfyUI CLI arguments" + ) + custom_nodes_count: Optional[int] = Field( + None, description="Total number of custom node packages", ge=0 + ) + failed_imports: Optional[List[str]] = Field( + None, description="List of custom nodes that failed to import" + ) + pip_packages: Optional[Dict[str, str]] = Field( + None, description="Map of installed pip packages to their versions" + ) + embedded_python: Optional[bool] = Field( + None, + description="Whether ComfyUI is running from an embedded Python distribution", + ) + + +class BatchExecutionRecord(BaseModel): + batch_id: str = Field(..., description="Unique batch identifier") + start_time: datetime = Field(..., description="ISO timestamp when batch started") + end_time: Optional[datetime] = Field( + None, description="ISO timestamp when batch completed" + ) + state_before: ComfyUISystemState + state_after: Optional[ComfyUISystemState] = Field( + None, description="System state after batch execution" + ) + operations: Optional[List[BatchOperation]] = Field( + None, description="List of operations performed in this batch" + ) + total_operations: Optional[int] = Field( + 0, description="Total number of operations in batch", ge=0 + ) + successful_operations: Optional[int] = Field( + 0, description="Number of successful operations", ge=0 + ) + failed_operations: Optional[int] = Field( + 0, description="Number of failed operations", ge=0 + ) + skipped_operations: Optional[int] = Field( + 0, description="Number of skipped operations", ge=0 + ) + + +class ImportFailInfoBulkRequest(BaseModel): + cnr_ids: Optional[List[str]] = Field( + None, description="A list of CNR IDs to check." + ) + urls: Optional[List[str]] = Field( + None, description="A list of repository URLs to check." + ) + + +class ImportFailInfoItem1(BaseModel): + error: Optional[str] = None + traceback: Optional[str] = None + + +class ImportFailInfoItem(RootModel[Optional[ImportFailInfoItem1]]): + root: Optional[ImportFailInfoItem1] + + +class QueueTaskItem(BaseModel): + ui_id: str = Field(..., description="Unique identifier for the task") + client_id: str = Field(..., description="Client identifier that initiated the task") + kind: OperationType + params: Union[ + InstallPackParams, + UpdatePackParams, + UpdateAllPacksParams, + UpdateComfyUIParams, + FixPackParams, + UninstallPackParams, + DisablePackParams, + EnablePackParams, + ModelMetadata, + ] + + +class TaskHistoryItem(BaseModel): + ui_id: str = Field(..., description="Unique identifier for the task") + client_id: str = Field(..., description="Client identifier that initiated the task") + kind: str = Field(..., description="Type of task that was performed") + timestamp: datetime = Field(..., description="ISO timestamp when task completed") + result: str = Field(..., description="Task result message or details") + status: Optional[TaskExecutionStatus] = None + batch_id: Optional[str] = Field( + None, description="ID of the batch this task belongs to" + ) + end_time: Optional[datetime] = Field( + None, description="ISO timestamp when task execution ended" + ) + + +class TaskStateMessage(BaseModel): + history: Dict[str, TaskHistoryItem] = Field( + ..., description="Map of task IDs to their history items" + ) + running_queue: List[QueueTaskItem] = Field( + ..., description="Currently executing tasks" + ) + pending_queue: List[QueueTaskItem] = Field( + ..., description="Tasks waiting to be executed" + ) + installed_packs: Dict[str, ManagerPackInstalled] = Field( + ..., description="Map of currently installed node packages by name" + ) + + +class MessageTaskDone(BaseModel): + ui_id: str = Field(..., description="Task identifier") + result: str = Field(..., description="Task result message") + kind: str = Field(..., description="Type of task") + status: Optional[TaskExecutionStatus] = None + timestamp: datetime = Field(..., description="ISO timestamp when task completed") + state: TaskStateMessage + + +class MessageTaskStarted(BaseModel): + ui_id: str = Field(..., description="Task identifier") + kind: str = Field(..., description="Type of task") + timestamp: datetime = Field(..., description="ISO timestamp when task started") + state: TaskStateMessage + + +class MessageTaskFailed(BaseModel): + ui_id: str = Field(..., description="Task identifier") + error: str = Field(..., description="Error message") + kind: str = Field(..., description="Type of task") + timestamp: datetime = Field(..., description="ISO timestamp when task failed") + state: TaskStateMessage + + +class MessageUpdate( + RootModel[Union[MessageTaskDone, MessageTaskStarted, MessageTaskFailed]] +): + root: Union[MessageTaskDone, MessageTaskStarted, MessageTaskFailed] = Field( + ..., description="Union type for all possible WebSocket message updates" + ) + + +class HistoryResponse(BaseModel): + history: Optional[Dict[str, TaskHistoryItem]] = Field( + None, description="Map of task IDs to their history items" + ) + + +class ImportFailInfoBulkResponse(RootModel[Optional[Dict[str, ImportFailInfoItem]]]): + root: Optional[Dict[str, ImportFailInfoItem]] = None diff --git a/extension-node-map.json b/comfyui_manager/extension-node-map.json similarity index 100% rename from extension-node-map.json rename to comfyui_manager/extension-node-map.json diff --git a/extras.json b/comfyui_manager/extras.json similarity index 100% rename from extras.json rename to comfyui_manager/extras.json diff --git a/github-stats.json b/comfyui_manager/github-stats.json similarity index 100% rename from github-stats.json rename to comfyui_manager/github-stats.json diff --git a/comfyui_manager/glob/CLAUDE.md b/comfyui_manager/glob/CLAUDE.md new file mode 100644 index 00000000..1c96bea2 --- /dev/null +++ b/comfyui_manager/glob/CLAUDE.md @@ -0,0 +1,11 @@ +- Anytime you make a change to the data being sent or received, you should follow this process: + 1. Adjust the openapi.yaml file first + 2. Verify the syntax of the openapi.yaml file using `yaml.safe_load` + 3. Regenerate the types following the instructions in the `data_models/README.md` file + 4. Verify the new data model is generated + 5. Verify the syntax of the generated types files + 6. Run formatting and linting on the generated types files + 7. Adjust the `__init__.py` files in the `data_models` directory to match/export the new data model + 8. Only then, make the changes to the rest of the codebase + 9. Run the CI tests to verify that the changes are working +- The comfyui_manager is a python package that is used to manage the comfyui server. There are two sub-packages `glob` and `legacy`. These represent the current version (`glob`) and the previous version (`legacy`), not including common utilities and data models. When developing, we work in the `glob` package. You can ignore the `legacy` package entirely, unless you have a very good reason to research how things were done in the legacy or prior major versions of the package. But in those cases, you should just look for the sake of knowledge or reflection, not for changing code (unless explicitly asked to do so). \ No newline at end of file diff --git a/comfyui_manager/glob/__init__.py b/comfyui_manager/glob/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/comfyui_manager/glob/constants.py b/comfyui_manager/glob/constants.py new file mode 100644 index 00000000..71fcc5a4 --- /dev/null +++ b/comfyui_manager/glob/constants.py @@ -0,0 +1,55 @@ + +SECURITY_MESSAGE_MIDDLE = "ERROR: To use this action, a security_level of `normal or below` is required. Please contact the administrator.\nReference: https://github.com/ltdrdata/ComfyUI-Manager#security-policy" +SECURITY_MESSAGE_MIDDLE_P = "ERROR: To use this action, security_level must be `normal or below`, and network_mode must be set to `personal_cloud`. Please contact the administrator.\nReference: https://github.com/ltdrdata/ComfyUI-Manager#security-policy" +SECURITY_MESSAGE_NORMAL_MINUS = "ERROR: To use this feature, you must either set '--listen' to a local IP and set the security level to 'normal-' or lower, or set the security level to 'middle' or 'weak'. Please contact the administrator.\nReference: https://github.com/ltdrdata/ComfyUI-Manager#security-policy" +SECURITY_MESSAGE_GENERAL = "ERROR: This installation is not allowed in this security_level. Please contact the administrator.\nReference: https://github.com/ltdrdata/ComfyUI-Manager#security-policy" +SECURITY_MESSAGE_NORMAL_MINUS_MODEL = "ERROR: Downloading models that are not in '.safetensors' format is only allowed for models registered in the 'default' channel at this security level. If you want to download this model, set the security level to 'normal-' or lower." + + +def is_loopback(address): + import ipaddress + + try: + return ipaddress.ip_address(address).is_loopback + except ValueError: + return False + + +model_dir_name_map = { + "checkpoints": "checkpoints", + "checkpoint": "checkpoints", + "unclip": "checkpoints", + "text_encoders": "text_encoders", + "clip": "text_encoders", + "vae": "vae", + "lora": "loras", + "t2i-adapter": "controlnet", + "t2i-style": "controlnet", + "controlnet": "controlnet", + "clip_vision": "clip_vision", + "gligen": "gligen", + "upscale": "upscale_models", + "embedding": "embeddings", + "embeddings": "embeddings", + "unet": "diffusion_models", + "diffusion_model": "diffusion_models", +} + +# List of all model directory names used for checking installed models +MODEL_DIR_NAMES = [ + "checkpoints", + "loras", + "vae", + "text_encoders", + "diffusion_models", + "clip_vision", + "embeddings", + "diffusers", + "vae_approx", + "controlnet", + "gligen", + "upscale_models", + "hypernetworks", + "photomaker", + "classifiers", +] diff --git a/glob/manager_core.py b/comfyui_manager/glob/manager_core.py similarity index 94% rename from glob/manager_core.py rename to comfyui_manager/glob/manager_core.py index 6e184107..1aa262e2 100644 --- a/glob/manager_core.py +++ b/comfyui_manager/glob/manager_core.py @@ -23,7 +23,6 @@ import yaml import zipfile import traceback from concurrent.futures import ThreadPoolExecutor, as_completed -import toml orig_print = print @@ -32,18 +31,17 @@ from packaging import version import uuid -glob_path = os.path.join(os.path.dirname(__file__)) # ComfyUI-Manager/glob -sys.path.append(glob_path) - -import cm_global -import cnr_utils -import manager_util -import git_utils -import manager_downloader -from node_package import InstalledNodePackage +from ..common import cm_global +from ..common import cnr_utils +from ..common import manager_util +from ..common import git_utils +from ..common import manager_downloader +from ..common.node_package import InstalledNodePackage +from ..common.enums import NetworkMode, SecurityLevel, DBMode +from ..common import context -version_code = [3, 36] +version_code = [4, 0, 1] version_str = f"V{version_code[0]}.{version_code[1]}" + (f'.{version_code[2]}' if len(version_code) > 2 else '') @@ -58,13 +56,14 @@ class InvalidChannel(Exception): self.channel = channel super().__init__(channel) + def get_default_custom_nodes_path(): global default_custom_nodes_path if default_custom_nodes_path is None: try: import folder_paths default_custom_nodes_path = folder_paths.get_folder_paths("custom_nodes")[0] - except: + except Exception: default_custom_nodes_path = os.path.abspath(os.path.join(manager_util.comfyui_manager_path, '..')) return default_custom_nodes_path @@ -74,37 +73,11 @@ def get_custom_nodes_paths(): try: import folder_paths return folder_paths.get_folder_paths("custom_nodes") - except: + except Exception: custom_nodes_path = os.path.abspath(os.path.join(manager_util.comfyui_manager_path, '..')) return [custom_nodes_path] -def get_comfyui_tag(): - try: - repo = git.Repo(comfy_path) - return repo.git.describe('--tags') - except: - return None - - -def get_current_comfyui_ver(): - """ - Extract version from pyproject.toml - """ - toml_path = os.path.join(comfy_path, 'pyproject.toml') - if not os.path.exists(toml_path): - return None - else: - try: - with open(toml_path, "r", encoding="utf-8") as f: - data = toml.load(f) - - project = data.get('project', {}) - return project.get('version') - except: - return None - - def get_script_env(): new_env = os.environ.copy() git_exe = get_config().get('git_exe') @@ -112,10 +85,10 @@ def get_script_env(): new_env['GIT_EXE_PATH'] = git_exe if 'COMFYUI_PATH' not in new_env: - new_env['COMFYUI_PATH'] = comfy_path + new_env['COMFYUI_PATH'] = context.comfy_path if 'COMFYUI_FOLDERS_BASE_PATH' not in new_env: - new_env['COMFYUI_FOLDERS_BASE_PATH'] = comfy_path + new_env['COMFYUI_FOLDERS_BASE_PATH'] = context.comfy_path return new_env @@ -137,12 +110,12 @@ def check_invalid_nodes(): try: import folder_paths - except: + except Exception: try: - sys.path.append(comfy_path) + sys.path.append(context.comfy_path) import folder_paths - except: - raise Exception(f"Invalid COMFYUI_FOLDERS_BASE_PATH: {comfy_path}") + except Exception: + raise Exception(f"Invalid COMFYUI_FOLDERS_BASE_PATH: {context.comfy_path}") def check(root): global invalid_nodes @@ -177,75 +150,6 @@ def check_invalid_nodes(): print("\n---------------------------------------------------------------------------\n") -# read env vars -comfy_path: str = os.environ.get('COMFYUI_PATH') -comfy_base_path = os.environ.get('COMFYUI_FOLDERS_BASE_PATH') - -if comfy_path is None: - try: - import folder_paths - comfy_path = os.path.join(os.path.dirname(folder_paths.__file__)) - except: - comfy_path = os.path.abspath(os.path.join(manager_util.comfyui_manager_path, '..', '..')) - -if comfy_base_path is None: - comfy_base_path = comfy_path - - -channel_list_template_path = os.path.join(manager_util.comfyui_manager_path, 'channels.list.template') -git_script_path = os.path.join(manager_util.comfyui_manager_path, "git_helper.py") - -manager_files_path = None -manager_config_path = None -manager_channel_list_path = None -manager_startup_script_path:str = None -manager_snapshot_path = None -manager_pip_overrides_path = None -manager_pip_blacklist_path = None -manager_components_path = None - -def update_user_directory(user_dir): - global manager_files_path - global manager_config_path - global manager_channel_list_path - global manager_startup_script_path - global manager_snapshot_path - global manager_pip_overrides_path - global manager_pip_blacklist_path - global manager_components_path - - manager_files_path = os.path.abspath(os.path.join(user_dir, 'default', 'ComfyUI-Manager')) - if not os.path.exists(manager_files_path): - os.makedirs(manager_files_path) - - manager_snapshot_path = os.path.join(manager_files_path, "snapshots") - if not os.path.exists(manager_snapshot_path): - os.makedirs(manager_snapshot_path) - - manager_startup_script_path = os.path.join(manager_files_path, "startup-scripts") - if not os.path.exists(manager_startup_script_path): - os.makedirs(manager_startup_script_path) - - manager_config_path = os.path.join(manager_files_path, 'config.ini') - manager_channel_list_path = os.path.join(manager_files_path, 'channels.list') - manager_pip_overrides_path = os.path.join(manager_files_path, "pip_overrides.json") - manager_pip_blacklist_path = os.path.join(manager_files_path, "pip_blacklist.list") - manager_components_path = os.path.join(manager_files_path, "components") - manager_util.cache_dir = os.path.join(manager_files_path, "cache") - - if not os.path.exists(manager_util.cache_dir): - os.makedirs(manager_util.cache_dir) - -try: - import folder_paths - update_user_directory(folder_paths.get_user_directory()) - -except Exception: - # fallback: - # This case is only possible when running with cm-cli, and in practice, this case is not actually used. - update_user_directory(os.path.abspath(manager_util.comfyui_manager_path)) - - cached_config = None js_path = None @@ -621,7 +525,7 @@ class UnifiedManager: ver = str(manager_util.StrictVersion(info['version'])) return {'id': cnr['id'], 'cnr': cnr, 'ver': ver} else: - return None + return {'id': info['id'], 'ver': info['version']} else: return None @@ -797,7 +701,9 @@ class UnifiedManager: return latest - async def reload(self, cache_mode, dont_wait=True): + async def reload(self, cache_mode, dont_wait=True, update_cnr_map=True): + import folder_paths + self.custom_node_map_cache = {} self.cnr_inactive_nodes = {} # node_id -> node_version -> fullpath self.nightly_inactive_nodes = {} # node_id -> fullpath @@ -805,17 +711,18 @@ class UnifiedManager: self.unknown_active_nodes = {} # node_id -> repo url * fullpath self.active_nodes = {} # node_id -> node_version * fullpath - if get_config()['network_mode'] != 'public': + if get_config()['network_mode'] != 'public' or manager_util.is_manager_pip_package(): dont_wait = True - # reload 'cnr_map' and 'repo_cnr_map' - cnrs = await cnr_utils.get_cnr_data(cache_mode=cache_mode=='cache', dont_wait=dont_wait) + if update_cnr_map: + # reload 'cnr_map' and 'repo_cnr_map' + cnrs = await cnr_utils.get_cnr_data(cache_mode=cache_mode=='cache', dont_wait=dont_wait) - for x in cnrs: - self.cnr_map[x['id']] = x - if 'repository' in x: - normalized_url = git_utils.normalize_url(x['repository']) - self.repo_cnr_map[normalized_url] = x + for x in cnrs: + self.cnr_map[x['id']] = x + if 'repository' in x: + normalized_url = git_utils.normalize_url(x['repository']) + self.repo_cnr_map[normalized_url] = x # reload node status info from custom_nodes/* for custom_nodes_path in folder_paths.get_folder_paths('custom_nodes'): @@ -863,7 +770,7 @@ class UnifiedManager: if 'id' in x: if x['id'] not in res: res[x['id']] = (x, True) - except: + except Exception: logging.error(f"[ComfyUI-Manager] broken item:{x}") return res @@ -916,7 +823,7 @@ class UnifiedManager: def safe_version(ver_str): try: return version.parse(ver_str) - except: + except Exception: return version.parse("0.0.0") def execute_install_script(self, url, repo_path, instant_execution=False, lazy_mode=False, no_deps=False): @@ -930,7 +837,7 @@ class UnifiedManager: else: if os.path.exists(requirements_path) and not no_deps: print("Install: pip packages") - pip_fixer = manager_util.PIPFixer(manager_util.get_installed_packages(), comfy_path, manager_files_path) + pip_fixer = manager_util.PIPFixer(manager_util.get_installed_packages(), context.comfy_path, context.manager_files_path) lines = manager_util.robust_readlines(requirements_path) for line in lines: package_name = remap_pip_package(line.strip()) @@ -952,7 +859,7 @@ class UnifiedManager: return res def reserve_cnr_switch(self, target, zip_url, from_path, to_path, no_deps): - script_path = os.path.join(manager_startup_script_path, "install-scripts.txt") + script_path = os.path.join(context.manager_startup_script_path, "install-scripts.txt") with open(script_path, "a") as file: obj = [target, "#LAZY-CNR-SWITCH-SCRIPT", zip_url, from_path, to_path, no_deps, get_default_custom_nodes_path(), sys.executable] file.write(f"{obj}\n") @@ -1358,7 +1265,7 @@ class UnifiedManager: print(f"Download: git clone '{clone_url}'") if not instant_execution and platform.system() == 'Windows': - res = manager_funcs.run_script([sys.executable, git_script_path, "--clone", get_default_custom_nodes_path(), clone_url, repo_path], cwd=get_default_custom_nodes_path()) + res = manager_funcs.run_script([sys.executable, context.git_script_path, "--clone", get_default_custom_nodes_path(), clone_url, repo_path], cwd=get_default_custom_nodes_path()) if res != 0: return result.fail(f"Failed to clone repo: {clone_url}") else: @@ -1510,12 +1417,20 @@ class UnifiedManager: return self.unified_enable(node_id, version_spec) elif version_spec == 'unknown' or version_spec == 'nightly': + to_path = os.path.abspath(os.path.join(get_default_custom_nodes_path(), node_id)) + if version_spec == 'nightly': # disable cnr nodes if self.is_enabled(node_id, 'cnr'): self.unified_disable(node_id, False) - to_path = os.path.abspath(os.path.join(get_default_custom_nodes_path(), node_id)) + # use `repo name` as a dir name instead of `cnr id` if system added nodepack (i.e. publisher is null) + cnr = self.cnr_map.get(node_id) + + if cnr is not None and cnr.get('publisher') is None: + repo_name = os.path.basename(git_utils.normalize_url(repo_url)) + to_path = os.path.abspath(os.path.join(get_default_custom_nodes_path(), repo_name)) + res = self.repo_install(repo_url, to_path, instant_execution=instant_execution, no_deps=no_deps, return_postinstall=return_postinstall) if res.result: if version_spec == 'unknown': @@ -1576,7 +1491,7 @@ def identify_node_pack_from_path(fullpath): if github_id is None: try: github_id = os.path.basename(repo_url) - except: + except Exception: logging.warning(f"[ComfyUI-Manager] unexpected repo url: {repo_url}") github_id = module_name @@ -1631,10 +1546,10 @@ def get_channel_dict(): if channel_dict is None: channel_dict = {} - if not os.path.exists(manager_channel_list_path): - shutil.copy(channel_list_template_path, manager_channel_list_path) + if not os.path.exists(context.manager_channel_list_path): + shutil.copy(context.channel_list_template_path, context.manager_channel_list_path) - with open(manager_channel_list_path, 'r') as file: + with open(context.manager_channel_list_path, 'r') as file: channels = file.read() for x in channels.split('\n'): channel_info = x.split("::") @@ -1698,18 +1613,18 @@ def write_config(): 'db_mode': get_config()['db_mode'], } - directory = os.path.dirname(manager_config_path) + directory = os.path.dirname(context.manager_config_path) if not os.path.exists(directory): os.makedirs(directory) - with open(manager_config_path, 'w') as configfile: + with open(context.manager_config_path, 'w') as configfile: config.write(configfile) def read_config(): try: config = configparser.ConfigParser(strict=False) - config.read(manager_config_path) + config.read(context.manager_config_path) default_conf = config['default'] def get_bool(key, default_value): @@ -1722,7 +1637,7 @@ def read_config(): 'http_channel_enabled': get_bool('http_channel_enabled', False), 'preview_method': default_conf.get('preview_method', manager_funcs.get_current_preview_method()).lower(), 'git_exe': default_conf.get('git_exe', ''), - 'use_uv': get_bool('use_uv', False), + 'use_uv': get_bool('use_uv', True), 'channel_url': default_conf.get('channel_url', DEFAULT_CHANNEL), 'default_cache_as_channel_url': get_bool('default_cache_as_channel_url', False), 'share_option': default_conf.get('share_option', 'all').lower(), @@ -1734,9 +1649,9 @@ def read_config(): 'model_download_by_agent': get_bool('model_download_by_agent', False), 'downgrade_blacklist': default_conf.get('downgrade_blacklist', '').lower(), 'always_lazy_install': get_bool('always_lazy_install', False), - 'network_mode': default_conf.get('network_mode', 'public').lower(), - 'security_level': default_conf.get('security_level', 'normal').lower(), - 'db_mode': default_conf.get('db_mode', 'cache').lower(), + 'network_mode': default_conf.get('network_mode', NetworkMode.PUBLIC.value).lower(), + 'security_level': default_conf.get('security_level', SecurityLevel.NORMAL.value).lower(), + 'db_mode': default_conf.get('db_mode', DBMode.CACHE.value).lower(), } except Exception: @@ -1761,9 +1676,9 @@ def read_config(): 'model_download_by_agent': False, 'downgrade_blacklist': '', 'always_lazy_install': False, - 'network_mode': 'public', # public | private | offline - 'security_level': 'normal', # strong | normal | normal- | weak - 'db_mode': 'cache', # local | cache | remote + 'network_mode': NetworkMode.PUBLIC.value, + 'security_level': SecurityLevel.NORMAL.value, + 'db_mode': DBMode.CACHE.value, } @@ -1807,27 +1722,27 @@ def switch_to_default_branch(repo): default_branch = repo.git.symbolic_ref(f'refs/remotes/{remote_name}/HEAD').replace(f'refs/remotes/{remote_name}/', '') repo.git.checkout(default_branch) return True - except: + except Exception: # try checkout master # try checkout main if failed try: repo.git.checkout(repo.heads.master) return True - except: + except Exception: try: if remote_name is not None: repo.git.checkout('-b', 'master', f'{remote_name}/master') return True - except: + except Exception: try: repo.git.checkout(repo.heads.main) return True - except: + except Exception: try: if remote_name is not None: repo.git.checkout('-b', 'main', f'{remote_name}/main') return True - except: + except Exception: pass print("[ComfyUI Manager] Failed to switch to the default branch") @@ -1835,10 +1750,10 @@ def switch_to_default_branch(repo): def reserve_script(repo_path, install_cmds): - if not os.path.exists(manager_startup_script_path): - os.makedirs(manager_startup_script_path) + if not os.path.exists(context.manager_startup_script_path): + os.makedirs(context.manager_startup_script_path) - script_path = os.path.join(manager_startup_script_path, "install-scripts.txt") + script_path = os.path.join(context.manager_startup_script_path, "install-scripts.txt") with open(script_path, "a") as file: obj = [repo_path] + install_cmds file.write(f"{obj}\n") @@ -1878,7 +1793,7 @@ def try_install_script(url, repo_path, install_cmd, instant_execution=False): print(f"[WARN] ComfyUI-Manager: Your ComfyUI version ({comfy_ui_revision})[{comfy_ui_commit_datetime.date()}] is too old. Please update to the latest version.") print("[WARN] The extension installation feature may not work properly in the current installed ComfyUI version on Windows environment.") print("###################################################################\n\n") - except: + except Exception: pass if code != 0: @@ -1893,11 +1808,11 @@ def try_install_script(url, repo_path, install_cmd, instant_execution=False): # use subprocess to avoid file system lock by git (Windows) def __win_check_git_update(path, do_fetch=False, do_update=False): if do_fetch: - command = [sys.executable, git_script_path, "--fetch", path] + command = [sys.executable, context.git_script_path, "--fetch", path] elif do_update: - command = [sys.executable, git_script_path, "--pull", path] + command = [sys.executable, context.git_script_path, "--pull", path] else: - command = [sys.executable, git_script_path, "--check", path] + command = [sys.executable, context.git_script_path, "--check", path] new_env = get_script_env() process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=get_default_custom_nodes_path(), env=new_env) @@ -1951,7 +1866,7 @@ def __win_check_git_update(path, do_fetch=False, do_update=False): def __win_check_git_pull(path): - command = [sys.executable, git_script_path, "--pull", path] + command = [sys.executable, context.git_script_path, "--pull", path] process = subprocess.Popen(command, env=get_script_env(), cwd=get_default_custom_nodes_path()) process.wait() @@ -1967,7 +1882,7 @@ def execute_install_script(url, repo_path, lazy_mode=False, instant_execution=Fa else: if os.path.exists(requirements_path) and not no_deps: print("Install: pip packages") - pip_fixer = manager_util.PIPFixer(manager_util.get_installed_packages(), comfy_path, manager_files_path) + pip_fixer = manager_util.PIPFixer(manager_util.get_installed_packages(), context.comfy_path, context.manager_files_path) with open(requirements_path, "r") as requirements_file: for line in requirements_file: #handle comments @@ -2203,7 +2118,7 @@ async def gitclone_install(url, instant_execution=False, msg_prefix='', no_deps= clone_url = git_utils.get_url_for_clone(url) if not instant_execution and platform.system() == 'Windows': - res = manager_funcs.run_script([sys.executable, git_script_path, "--clone", get_default_custom_nodes_path(), clone_url, repo_path], cwd=get_default_custom_nodes_path()) + res = manager_funcs.run_script([sys.executable, context.git_script_path, "--clone", get_default_custom_nodes_path(), clone_url, repo_path], cwd=get_default_custom_nodes_path()) if res != 0: return result.fail(f"Failed to clone '{clone_url}' into '{repo_path}'") else: @@ -2274,7 +2189,7 @@ async def get_data_by_mode(mode, filename, channel_url=None): cache_uri = str(manager_util.simple_hash(uri))+'_'+filename cache_uri = os.path.join(manager_util.cache_dir, cache_uri) - if get_config()['network_mode'] == 'offline': + if get_config()['network_mode'] == 'offline' or manager_util.is_manager_pip_package(): # offline network mode if os.path.exists(cache_uri): json_obj = await manager_util.get_data(cache_uri) @@ -2294,7 +2209,7 @@ async def get_data_by_mode(mode, filename, channel_url=None): with open(cache_uri, "w", encoding='utf-8') as file: json.dump(json_obj, file, indent=4, sort_keys=True) except Exception as e: - print(f"[ComfyUI-Manager] Due to a network error, switching to local mode.\n=> {filename}\n=> {e}") + print(f"[ComfyUI-Manager] Due to a network error, switching to local mode.\n=> {filename} @ {channel_url}/{mode}\n=> {e}") uri = os.path.join(manager_util.comfyui_manager_path, filename) json_obj = await manager_util.get_data(uri) @@ -2365,7 +2280,7 @@ def gitclone_uninstall(files): url = url[:-1] try: for custom_nodes_dir in get_custom_nodes_paths(): - dir_name = os.path.splitext(os.path.basename(url))[0].replace(".git", "") + dir_name:str = os.path.splitext(os.path.basename(url))[0].replace(".git", "") dir_path = os.path.join(custom_nodes_dir, dir_name) # safety check @@ -2413,7 +2328,7 @@ def gitclone_set_active(files, is_disable): url = url[:-1] try: for custom_nodes_dir in get_custom_nodes_paths(): - dir_name = os.path.splitext(os.path.basename(url))[0].replace(".git", "") + dir_name:str = os.path.splitext(os.path.basename(url))[0].replace(".git", "") dir_path = os.path.join(custom_nodes_dir, dir_name) # safety check @@ -2510,7 +2425,7 @@ def update_to_stable_comfyui(repo_path): repo = git.Repo(repo_path) try: repo.git.checkout(repo.heads.master) - except: + except Exception: logging.error(f"[ComfyUI-Manager] Failed to checkout 'master' branch.\nrepo_path={repo_path}\nAvailable branches:") for branch in repo.branches: logging.error('\t'+branch.name) @@ -2533,7 +2448,7 @@ def update_to_stable_comfyui(repo_path): logging.info(f"[ComfyUI-Manager] Updating ComfyUI: {current_tag} -> {latest_tag}") repo.git.checkout(latest_tag) return 'updated', latest_tag - except: + except Exception: traceback.print_exc() return "fail", None @@ -2686,7 +2601,7 @@ async def get_current_snapshot(custom_nodes_only = False): await unified_manager.get_custom_nodes('default', 'cache') # Get ComfyUI hash - repo_path = comfy_path + repo_path = context.comfy_path comfyui_commit_hash = None if not custom_nodes_only: @@ -2731,7 +2646,7 @@ async def get_current_snapshot(custom_nodes_only = False): commit_hash = git_utils.get_commit_hash(fullpath) url = git_utils.git_url(fullpath) git_custom_nodes[url] = dict(hash=commit_hash, disabled=is_disabled) - except: + except Exception: print(f"Failed to extract snapshots for the custom node '{path}'.") elif path.endswith('.py'): @@ -2762,7 +2677,7 @@ async def save_snapshot_with_postfix(postfix, path=None, custom_nodes_only = Fal date_time_format = now.strftime("%Y-%m-%d_%H-%M-%S") file_name = f"{date_time_format}_{postfix}" - path = os.path.join(manager_snapshot_path, f"{file_name}.json") + path = os.path.join(context.manager_snapshot_path, f"{file_name}.json") else: file_name = path.replace('\\', '/').split('/')[-1] file_name = file_name.split('.')[-2] @@ -2789,7 +2704,7 @@ async def extract_nodes_from_workflow(filepath, mode='local', channel_url='defau with open(filepath, "r", encoding="UTF-8", errors="ignore") as json_file: try: workflow = json.load(json_file) - except: + except Exception: print(f"Invalid workflow file: {filepath}") exit(-1) @@ -2802,7 +2717,7 @@ async def extract_nodes_from_workflow(filepath, mode='local', channel_url='defau else: try: workflow = json.loads(img.info['workflow']) - except: + except Exception: print(f"This is not a valid .png file containing a ComfyUI workflow: {filepath}") exit(-1) @@ -3073,7 +2988,7 @@ def populate_github_stats(node_packs, json_obj_github): v['stars'] = -1 v['last_update'] = -1 v['trust'] = False - except: + except Exception: logging.error(f"[ComfyUI-Manager] DB item is broken:\n{v}") @@ -3351,12 +3266,12 @@ async def restore_snapshot(snapshot_path, git_helper_extras=None): def get_comfyui_versions(repo=None): if repo is None: - repo = git.Repo(comfy_path) + repo = git.Repo(context.comfy_path) try: remote = get_remote_name(repo) repo.remotes[remote].fetch() - except: + except Exception: logging.error("[ComfyUI-Manager] Failed to fetch ComfyUI") versions = [x.name for x in repo.tags if x.name.startswith('v')] @@ -3385,7 +3300,7 @@ def get_comfyui_versions(repo=None): def switch_comfyui(tag): - repo = git.Repo(comfy_path) + repo = git.Repo(context.comfy_path) if tag == 'nightly': repo.git.checkout('master') @@ -3425,5 +3340,5 @@ def repo_switch_commit(repo_path, commit_hash): repo.git.checkout(commit_hash) return True - except: + except Exception: return None diff --git a/comfyui_manager/glob/manager_server.py b/comfyui_manager/glob/manager_server.py new file mode 100644 index 00000000..cc06a58e --- /dev/null +++ b/comfyui_manager/glob/manager_server.py @@ -0,0 +1,2079 @@ +""" +ComfyUI Manager Server + +Main server implementation providing REST API endpoints for ComfyUI Manager functionality. +Handles task queue management, custom node operations, model installation, and system configuration. +""" + +import asyncio +import copy +import heapq +import json +import logging +import os +import platform +import re +import shutil +import subprocess # don't remove this +import sys +import threading +import traceback +import urllib.request +import uuid +import zipfile +from datetime import datetime, timedelta +from typing import Any, Optional + +import folder_paths +import latent_preview +import nodes +from aiohttp import web +from comfy.cli_args import args +from pydantic import ValidationError + +from comfyui_manager.glob.utils import ( + formatting_utils, + model_utils, + security_utils, + node_pack_utils, + environment_utils, +) + + +from server import PromptServer + +from . import manager_core as core +from ..common import manager_util +from ..common import cm_global +from ..common import manager_downloader +from ..common import context + + + +from ..data_models import ( + QueueTaskItem, + TaskHistoryItem, + TaskStateMessage, + TaskExecutionStatus, + MessageTaskDone, + MessageTaskStarted, + MessageUpdate, + ManagerMessageName, + BatchExecutionRecord, + ComfyUISystemState, + ImportFailInfoBulkRequest, + ImportFailInfoBulkResponse, + BatchOperation, + InstalledNodeInfo, + ComfyUIVersionInfo, + InstallPackParams, + UpdatePackParams, + UpdateComfyUIParams, + FixPackParams, + UninstallPackParams, + DisablePackParams, + EnablePackParams, + ModelMetadata, + OperationType, + OperationResult, + ManagerDatabaseSource, + SecurityLevel, + UpdateAllQueryParams, + UpdateComfyUIQueryParams, + ComfyUISwitchVersionQueryParams, +) + +from .constants import ( + model_dir_name_map, + SECURITY_MESSAGE_MIDDLE, + SECURITY_MESSAGE_MIDDLE_P, +) + +if not manager_util.is_manager_pip_package(): + network_mode_description = "offline" +else: + network_mode_description = core.get_config()["network_mode"] +logging.info("[ComfyUI-Manager] network_mode: " + network_mode_description) + + +MAXIMUM_HISTORY_SIZE = 10000 +routes = PromptServer.instance.routes + + +def is_loopback(address): + import ipaddress + + try: + return ipaddress.ip_address(address).is_loopback + except ValueError: + return False + + +def error_response( + status: int, message: str, error_type: Optional[str] = None +) -> web.Response: + """Create a standardized error response. + + Args: + status: HTTP status code + message: Error message + error_type: Optional error type/category + + Returns: + web.Response with JSON error body + """ + error_data = {"error": message} + if error_type: + error_data["error_type"] = error_type + + return web.json_response(error_data, status=status) + + +class ManagerFuncsInComfyUI(core.ManagerFuncs): + def get_current_preview_method(self): + if args.preview_method == latent_preview.LatentPreviewMethod.Auto: + return "auto" + elif args.preview_method == latent_preview.LatentPreviewMethod.Latent2RGB: + return "latent2rgb" + elif args.preview_method == latent_preview.LatentPreviewMethod.TAESD: + return "taesd" + else: + return "none" + + def run_script(self, cmd, cwd="."): + if len(cmd) > 0 and cmd[0].startswith("#"): + logging.error(f"[ComfyUI-Manager] Unexpected behavior: `{cmd}`") + return 0 + + process = subprocess.Popen( + cmd, + cwd=cwd, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True, + bufsize=1, + env=core.get_script_env(), + ) + + stdout_thread = threading.Thread( + target=formatting_utils.handle_stream, args=(process.stdout, "") + ) + stderr_thread = threading.Thread( + target=formatting_utils.handle_stream, args=(process.stderr, "[!]") + ) + + stdout_thread.start() + stderr_thread.start() + + stdout_thread.join() + stderr_thread.join() + + return process.wait() + + +core.manager_funcs = ManagerFuncsInComfyUI() + +from comfyui_manager.common.manager_downloader import ( + download_url, + download_url_with_agent, +) + + +class TaskQueue: + instance = None + + def __init__(self): + TaskQueue.instance = self + self.mutex = threading.RLock() + self.not_empty = threading.Condition(self.mutex) + self.current_index = 0 + self.pending_tasks = [] + self.running_tasks = {} + self.history_tasks = {} + self.task_counter = 0 + self.batch_id = None + self.batch_start_time = None + self.batch_state_before = None + self._worker_task = None + self._cleanup_performed = False + + def is_processing(self) -> bool: + """Check if the queue is currently processing tasks""" + return self._worker_task is not None and self._worker_task.is_alive() + + def start_worker(self) -> bool: + """Start the task worker if not already running. Returns True if started, False if already running.""" + if self._worker_task is not None and self._worker_task.is_alive(): + logging.debug("[ComfyUI-Manager] Worker already running, skipping start") + return False + + logging.debug("[ComfyUI-Manager] Starting task worker thread") + self._worker_task = threading.Thread(target=lambda: asyncio.run(task_worker())) + self._worker_task.start() + return True + + def get_current_state(self) -> TaskStateMessage: + return TaskStateMessage( + history=self.get_history(), + running_queue=self.get_current_queue()[0], + pending_queue=self.get_current_queue()[1], + installed_packs=core.get_installed_node_packs(), + ) + + @staticmethod + def send_queue_state_update( + msg: str, update: MessageUpdate, client_id: Optional[str] = None + ) -> None: + """Send queue state update to clients. + + Args: + msg: Message type/event name + update: Update data to send + client_id: Optional client ID. If None, broadcasts to all clients. + If provided, sends only to that specific client. + """ + PromptServer.instance.send_sync(msg, update.model_dump(mode="json"), client_id) + + def put(self, item) -> None: + """Add a task to the queue. Item can be a dict or QueueTaskItem model.""" + with self.mutex: + # Start a new batch if this is the first task after queue was empty + if ( + self.batch_id is None + and len(self.pending_tasks) == 0 + and len(self.running_tasks) == 0 + ): + self._start_new_batch() + + # Convert to Pydantic model if it's a dict + if isinstance(item, dict): + item = QueueTaskItem(**item) + + # Use current_index as priority (earlier tasks have lower numbers) + priority = self.current_index + self.current_index += 1 + + # Push tuple: (priority, task_counter, item) + # task_counter ensures stable sort for items with same priority + heapq.heappush(self.pending_tasks, (priority, self.task_counter, item)) + logging.debug( + "[ComfyUI-Manager] Task added to queue: kind=%s, ui_id=%s, client_id=%s, pending_count=%d", + item.kind, + item.ui_id, + item.client_id, + len(self.pending_tasks), + ) + self.not_empty.notify() + + def _start_new_batch(self) -> None: + """Start a new batch session for tracking operations.""" + self.batch_id = ( + f"batch_{datetime.now().strftime('%Y%m%d_%H%M%S')}_{uuid.uuid4().hex[:8]}" + ) + self.batch_start_time = datetime.now().isoformat() + self.batch_state_before = self._capture_system_state() + logging.debug("[ComfyUI-Manager] Started new batch: %s", self.batch_id) + + def get( + self, timeout: Optional[float] = None + ) -> tuple[Optional[QueueTaskItem], int]: + with self.not_empty: + while len(self.pending_tasks) == 0: + self.not_empty.wait(timeout=timeout) + if timeout is not None and len(self.pending_tasks) == 0: + logging.debug("[ComfyUI-Manager] Task queue get timed out") + return None + # Pop tuple and extract the item + priority, counter, item = heapq.heappop(self.pending_tasks) + task_index = self.task_counter + self.running_tasks[task_index] = copy.deepcopy(item) + self.task_counter += 1 + logging.debug( + "[ComfyUI-Manager] Task retrieved from queue: kind=%s, ui_id=%s, task_index=%d, running_count=%d, pending_count=%d", + item.kind, + item.ui_id, + task_index, + len(self.running_tasks), + len(self.pending_tasks), + ) + TaskQueue.send_queue_state_update( + ManagerMessageName.cm_task_started.value, + MessageTaskStarted( + ui_id=item.ui_id, + kind=item.kind, + timestamp=datetime.now(), + state=self.get_current_state(), + ), + client_id=item.client_id, # Send task started only to the client that requested it + ) + return item, task_index + + async def task_done( + self, + item: QueueTaskItem, + task_index: int, + result_msg: str, + status: Optional[TaskExecutionStatus] = None, + ) -> None: + """Mark task as completed and add to history""" + + with self.mutex: + now = datetime.now() + timestamp = now.isoformat() + + # Remove task from running_tasks using the task_index + self.running_tasks.pop(task_index, None) + logging.debug( + "[ComfyUI-Manager] Task completed: kind=%s, ui_id=%s, task_index=%d, status=%s, running_count=%d", + item.kind, + item.ui_id, + task_index, + status.status_str if status else "unknown", + len(self.running_tasks), + ) + + # Manage history size + if len(self.history_tasks) > MAXIMUM_HISTORY_SIZE: + self.history_tasks.pop(next(iter(self.history_tasks))) + + # Update history + self.history_tasks[item.ui_id] = TaskHistoryItem( + ui_id=item.ui_id, + client_id=item.client_id, + timestamp=now, + result=result_msg, + kind=item.kind, + status=status, + batch_id=self.batch_id, + end_time=now, + ) + + # Force cache refresh for successful pack-modifying operations + pack_modifying_tasks = { + OperationType.install.value, + OperationType.uninstall.value, + OperationType.enable.value, + OperationType.disable.value, + } + if ( + item.kind in pack_modifying_tasks + and status + and status.status_str == OperationResult.success.value + ): + try: + logging.debug( + "[ComfyUI-Manager] Refreshing cache after successful %s operation", + item.kind, + ) + # Force unified_manager to refresh its installed packages cache + await core.unified_manager.reload( + ManagerDatabaseSource.cache.value, + dont_wait=True, + update_cnr_map=False, + ) + except Exception as e: + logging.warning( + f"[ComfyUI-Manager] Failed to refresh cache after {item.kind}: {e}" + ) + + # Send WebSocket message indicating task is complete + TaskQueue.send_queue_state_update( + ManagerMessageName.cm_task_completed.value, + MessageTaskDone( + ui_id=item.ui_id, + result=result_msg, + kind=item.kind, + status=status, + timestamp=datetime.fromisoformat(timestamp), + state=self.get_current_state(), + ), + client_id=item.client_id, # Send completion only to the client that requested it + ) + + def get_current_queue(self) -> tuple[list[QueueTaskItem], list[QueueTaskItem]]: + """Get current running and remaining tasks""" + with self.mutex: + running = list(self.running_tasks.values()) + # Extract items from tuples, maintaining heap order + remaining = [item for priority, counter, item in sorted(self.pending_tasks)] + return running, remaining + + def get_tasks_remaining(self) -> int: + """Get number of tasks remaining""" + with self.mutex: + return len(self.pending_tasks) + len(self.running_tasks) + + def wipe_queue(self) -> None: + """Clear all task queue""" + with self.mutex: + pending_count = len(self.pending_tasks) + self.pending_tasks = [] + logging.debug( + "[ComfyUI-Manager] Queue wiped: cleared %d pending tasks", pending_count + ) + + def abort(self) -> None: + """Abort current operations""" + with self.mutex: + pending_count = len(self.pending_tasks) + running_count = len(self.running_tasks) + self.pending_tasks = [] + self.running_tasks = {} + logging.debug( + "[ComfyUI-Manager] Queue aborted: cleared %d pending and %d running tasks", + pending_count, + running_count, + ) + + def delete_history_item(self, ui_id: str) -> None: + """Delete specific task from history""" + with self.mutex: + self.history_tasks.pop(ui_id, None) + + def get_history( + self, + ui_id: Optional[str] = None, + max_items: Optional[int] = None, + offset: int = -1, + ) -> dict[str, TaskHistoryItem]: + """Get task history. If ui_id (task id) is passsed, only return that task's history item entry.""" + with self.mutex: + if ui_id is None: + out = {} + i = 0 + if offset < 0 and max_items is not None: + offset = len(self.history_tasks) - max_items + for k in self.history_tasks: + if i >= offset: + out[k] = self.history_tasks[k] + if max_items is not None and len(out) >= max_items: + break + i += 1 + return out + elif ui_id in self.history_tasks: + return self.history_tasks[ui_id] + else: + return {} + + def done_count(self) -> int: + """Get the number of completed tasks in history. + + Returns: + int: Number of tasks that have been completed and are stored in history. + Returns 0 if history_tasks is None (defensive programming). + """ + return len(self.history_tasks) if self.history_tasks is not None else 0 + + def total_count(self) -> int: + """Get the total number of tasks currently in the system (pending + running). + + Returns: + int: Combined count of pending and running tasks. + Returns 0 if either collection is None (defensive programming). + """ + return ( + len(self.pending_tasks) + len(self.running_tasks) + if self.pending_tasks is not None and self.running_tasks is not None + else 0 + ) + + def finalize(self) -> None: + """Finalize a completed task batch by saving execution history to disk. + + This method is intended to be called when the queue transitions from having + tasks to being completely empty (no pending or running tasks). It will create + a comprehensive snapshot of the ComfyUI state and all operations performed. + """ + if self.batch_id is not None: + batch_path = os.path.join( + context.manager_batch_history_path, self.batch_id + ".json" + ) + logging.debug( + "[ComfyUI-Manager] Finalizing batch: batch_id=%s, history_count=%d", + self.batch_id, + len(self.history_tasks), + ) + + try: + end_time = datetime.now().isoformat() + state_after = self._capture_system_state() + operations = self._extract_batch_operations() + + batch_record = BatchExecutionRecord( + batch_id=self.batch_id, + start_time=self.batch_start_time, + end_time=end_time, + state_before=self.batch_state_before, + state_after=state_after, + operations=operations, + total_operations=len(operations), + successful_operations=len( + [ + op + for op in operations + if op.result == OperationResult.success.value + ] + ), + failed_operations=len( + [ + op + for op in operations + if op.result == OperationResult.failed.value + ] + ), + skipped_operations=len( + [ + op + for op in operations + if op.result == OperationResult.skipped.value + ] + ), + ) + + # Save to disk + with open(batch_path, "w", encoding="utf-8") as json_file: + json.dump( + batch_record.model_dump(), json_file, indent=4, default=str + ) + + logging.debug( + "[ComfyUI-Manager] Batch history saved: batch_id=%s, path=%s, total_ops=%d, successful=%d, failed=%d, skipped=%d", + self.batch_id, + batch_path, + batch_record.total_operations, + batch_record.successful_operations, + batch_record.failed_operations, + batch_record.skipped_operations, + ) + + # Reset batch tracking + self.batch_id = None + self.batch_start_time = None + self.batch_state_before = None + + # Cleanup old batch files once per session + if not self._cleanup_performed: + self._cleanup_old_batches() + self._cleanup_performed = True + + except Exception as e: + logging.error(f"[ComfyUI-Manager] Failed to save batch history: {e}") + + def _capture_system_state(self) -> ComfyUISystemState: + """Capture current ComfyUI system state for batch record.""" + logging.debug("[ComfyUI-Manager] Capturing system state for batch record") + return ComfyUISystemState( + snapshot_time=datetime.now().isoformat(), + comfyui_version=self._get_comfyui_version_info(), + frontend_version=self._get_frontend_version(), + python_version=platform.python_version(), + platform_info=f"{platform.system()} {platform.release()} ({platform.machine()})", + installed_nodes=self._get_installed_nodes(), + comfyui_root_path=self._get_comfyui_root_path(), + model_paths=self._get_model_paths(), + manager_version=self._get_manager_version(), + security_level=self._get_security_level(), + network_mode=self._get_network_mode(), + cli_args=self._get_cli_args(), + custom_nodes_count=self._get_custom_nodes_count(), + failed_imports=self._get_failed_imports(), + pip_packages=self._get_pip_packages(), + manager_config=core.get_config(), + embedded_python=os.path.split(os.path.split(sys.executable)[0])[1] == "python_embeded", + ) + + def _get_comfyui_version_info(self) -> ComfyUIVersionInfo: + """Get ComfyUI version information.""" + try: + version_info = core.get_comfyui_versions() + current_version = version_info[1] if len(version_info) > 1 else "unknown" + return ComfyUIVersionInfo(version=current_version) + except Exception: + return ComfyUIVersionInfo(version="unknown") + + def _get_frontend_version(self) -> Optional[str]: + """Get ComfyUI frontend version.""" + try: + # Check if front-end-root is specified (overrides version) + if hasattr(args, "front_end_root") and args.front_end_root: + return f"custom-root: {args.front_end_root}" + + # Check if front-end-version is specified + if hasattr(args, "front_end_version") and args.front_end_version: + if "@" in args.front_end_version: + return args.front_end_version.split("@")[1] + else: + return args.front_end_version + + # Otherwise, check for installed package + pip_packages = self._get_pip_packages() + for package_name in ["comfyui-frontend", "comfyui_frontend"]: + if package_name in pip_packages: + return pip_packages[package_name] + + return None + except Exception: + return None + + def _get_installed_nodes(self) -> dict[str, InstalledNodeInfo]: + """Get information about installed node packages.""" + installed_nodes = {} + + try: + node_packs = core.get_installed_node_packs() + for pack_name, pack_info in node_packs.items(): + # Determine install method and repository URL + install_method = "git" if pack_info.get("aux_id") else "cnr" + repository_url = None + + if pack_info.get("aux_id"): + # It's a git-based node, construct GitHub URL + repository_url = f"https://github.com/{pack_info['aux_id']}" + + installed_nodes[pack_name] = InstalledNodeInfo( + name=pack_name, + version=pack_info.get("ver", "unknown"), + install_method=install_method, + repository_url=repository_url, + enabled=pack_info.get("enabled", True), + ) + except Exception as e: + logging.warning(f"[ComfyUI-Manager] Failed to get installed nodes: {e}") + + return installed_nodes + + def _get_comfyui_root_path(self) -> str: + """Get ComfyUI root installation directory.""" + try: + return os.path.dirname(folder_paths.__file__) + except Exception: + return None + + def _get_model_paths(self) -> dict[str, list[str]]: + """Get model paths for different model types.""" + try: + model_paths = {} + for model_type in model_dir_name_map.keys(): + try: + paths = folder_paths.get_folder_paths(model_type) + if paths: + model_paths[model_type] = paths + except Exception: + continue + return model_paths + except Exception: + return {} + + def _get_manager_version(self) -> str: + """Get ComfyUI Manager version.""" + try: + version_code = getattr(core, "version_code", [4, 0]) + return f"V{version_code[0]}.{version_code[1]}" + except Exception: + return None + + def _get_security_level(self) -> SecurityLevel: + """Get current security level.""" + try: + config = core.get_config() + level_str = config.get("security_level", "normal") + # Map the string to SecurityLevel enum + level_mapping = { + "strong": SecurityLevel.strong, + "normal": SecurityLevel.normal, + "normal-": SecurityLevel.normal_, + "weak": SecurityLevel.weak, + } + return level_mapping.get(level_str, SecurityLevel.normal) + except Exception: + return None + + def _get_network_mode(self) -> str: + """Get current network mode.""" + try: + config = core.get_config() + return config.get("network_mode", "online") + except Exception: + return None + + def _get_cli_args(self) -> dict[str, Any]: + """Get selected CLI arguments.""" + try: + cli_args = {} + if hasattr(args, "listen"): + cli_args["listen"] = args.listen + if hasattr(args, "port"): + cli_args["port"] = args.port + if hasattr(args, "preview_method"): + cli_args["preview_method"] = str(args.preview_method) + if hasattr(args, "enable_manager_legacy_ui"): + cli_args["enable_manager_legacy_ui"] = args.enable_manager_legacy_ui + if hasattr(args, "front_end_version"): + cli_args["front_end_version"] = args.front_end_version + if hasattr(args, "front_end_root"): + cli_args["front_end_root"] = args.front_end_root + return cli_args + except Exception: + return {} + + def _get_custom_nodes_count(self) -> int: + """Get total number of custom node packages.""" + try: + node_packs = core.get_installed_node_packs() + return len(node_packs) + except Exception: + return 0 + + def _get_failed_imports(self) -> list[str]: + """Get list of custom nodes that failed to import.""" + try: + # Check if the import_failed_extensions set is available + if hasattr(sys, "__comfyui_manager_import_failed_extensions"): + failed_set = getattr(sys, "__comfyui_manager_import_failed_extensions") + return list(failed_set) if failed_set else [] + return [] + except Exception: + return [] + + def _get_pip_packages(self) -> dict[str, str]: + """Get installed pip packages.""" + try: + return core.get_installed_pip_packages() + except Exception: + return {} + + def _extract_batch_operations(self) -> list[BatchOperation]: + """Extract operations from completed task history for this batch.""" + operations = [] + + try: + for ui_id, task in self.history_tasks.items(): + # Only include operations from the current batch + if task.batch_id != self.batch_id: + continue + + result_status = OperationResult.success + if task.status: + status_str = ( + task.status.status_str + if hasattr(task.status, "status_str") + else task.status.get( + "status_str", OperationResult.success.value + ) + ) + if status_str == OperationResult.error.value: + result_status = OperationResult.failed + elif status_str == OperationResult.skip.value: + result_status = OperationResult.skipped + + operation = BatchOperation( + operation_id=ui_id, + operation_type=task.kind, + target=f"task_{ui_id}", + result=result_status.value, + start_time=task.timestamp, + end_time=task.end_time, + client_id=task.client_id, + ) + operations.append(operation) + except Exception as e: + logging.warning( + f"[ComfyUI-Manager] Failed to extract batch operations: {e}" + ) + + return operations + + def _cleanup_old_batches(self) -> None: + """Clean up batch history files older than 90 days. + + This is a best-effort cleanup that silently ignores any errors + to avoid disrupting normal operations. + """ + try: + cutoff = datetime.now() - timedelta(days=16) + cutoff_timestamp = cutoff.timestamp() + + pattern = os.path.join(context.manager_batch_history_path, "batch_*.json") + removed_count = 0 + + import glob + + for file_path in glob.glob(pattern): + try: + if os.path.getmtime(file_path) < cutoff_timestamp: + os.remove(file_path) + removed_count += 1 + except Exception: + pass + + if removed_count > 0: + logging.debug( + "[ComfyUI-Manager] Cleaned up %d old batch history files", + removed_count, + ) + + except Exception: + # Silently ignore all errors - this is non-critical functionality + pass + + +task_queue = TaskQueue() + +# Preview method initialization +if args.preview_method == latent_preview.LatentPreviewMethod.NoPreviews: + environment_utils.set_preview_method(core.get_config()["preview_method"]) +else: + logging.warning( + "[ComfyUI-Manager] Since --preview-method is set, ComfyUI-Manager's preview method feature will be ignored." + ) + + +async def task_worker(): + logging.debug("[ComfyUI-Manager] Task worker started") + await core.unified_manager.reload(ManagerDatabaseSource.cache.value) + + async def do_install(params: InstallPackParams) -> str: + if not security_utils.is_allowed_security_level('middle+'): + logging.error(SECURITY_MESSAGE_MIDDLE_P) + return OperationResult.failed.value + + node_id = params.id + node_version = params.selected_version + channel = params.channel + mode = params.mode + skip_post_install = params.skip_post_install + + logging.debug( + "[ComfyUI-Manager] Installing node: id=%s, version=%s, channel=%s, mode=%s", + node_id, + node_version, + channel, + mode, + ) + + try: + node_spec = core.unified_manager.resolve_node_spec( + f"{node_id}@{node_version}" + ) + if node_spec is None: + logging.error( + f"Cannot resolve install target: '{node_id}@{node_version}'" + ) + return f"Cannot resolve install target: '{node_id}@{node_version}'" + + node_name, version_spec, is_specified = node_spec + res = await core.unified_manager.install_by_id( + node_name, + version_spec, + channel, + mode, + return_postinstall=skip_post_install, + ) # discard post install if skip_post_install mode + + if res.action not in [ + "skip", + "enable", + "install-git", + "install-cnr", + "switch-cnr", + ]: + logging.error(f"[ComfyUI-Manager] Installation failed:\n{res.msg}") + return res.msg + + elif not res.result: + logging.error(f"[ComfyUI-Manager] Installation failed:\n{res.msg}") + return res.msg + + return OperationResult.success.value + except Exception: + traceback.print_exc() + return "Installation failed" + + async def do_enable(params: EnablePackParams) -> str: + cnr_id = params.cnr_id + logging.debug("[ComfyUI-Manager] Enabling node: cnr_id=%s", cnr_id) + core.unified_manager.unified_enable(cnr_id) + return OperationResult.success.value + + async def do_update(params: UpdatePackParams) -> dict[str, str]: + node_name = params.node_name + node_ver = params.node_ver + + logging.debug( + "[ComfyUI-Manager] Updating node: name=%s, version=%s", node_name, node_ver + ) + + try: + res = core.unified_manager.unified_update(node_name, node_ver) + + if res.ver == "unknown": + url = core.unified_manager.unknown_active_nodes[node_name][0] + try: + title = os.path.basename(url) + except Exception: + title = node_name + else: + url = core.unified_manager.cnr_map[node_name].get("repository") + title = core.unified_manager.cnr_map[node_name]["name"] + + manager_util.clear_pip_cache() + + if url is not None: + base_res = {"url": url, "title": title} + else: + base_res = {"title": title} + + if res.result: + if res.action == "skip": + base_res["msg"] = OperationResult.skip.value + return base_res + else: + base_res["msg"] = OperationResult.success.value + return base_res + + base_res["msg"] = f"An error occurred while updating '{node_name}'." + logging.error( + f"\nERROR: An error occurred while updating '{node_name}'. (res.result={res.result}, res.action={res.action})" + ) + return base_res + except Exception: + traceback.print_exc() + + return {"msg": f"An error occurred while updating '{node_name}'."} + + async def do_update_comfyui(params: UpdateComfyUIParams) -> str: + try: + repo_path = os.path.dirname(folder_paths.__file__) + + # Check if this is a version switch operation + if params.target_version: + # Switch to specific version + logging.info(f"Switching ComfyUI to version: {params.target_version}") + core.switch_comfyui(params.target_version) + return f"success-switched-{params.target_version}" + else: + # Regular update operation + is_stable = params.is_stable if params.is_stable is not None else True + logging.debug( + "[ComfyUI-Manager] Updating ComfyUI: is_stable=%s, repo_path=%s", + is_stable, + repo_path, + ) + latest_tag = None + if is_stable: + res, latest_tag = core.update_to_stable_comfyui(repo_path) + else: + res = core.update_path(repo_path) + + if res == "fail": + logging.error("ComfyUI update failed") + return "fail" + elif res == "updated": + if is_stable: + logging.info("ComfyUI is updated to latest stable version.") + return "success-stable-" + latest_tag + else: + logging.info("ComfyUI is updated to latest nightly version.") + return "success-nightly" + else: # skipped + logging.info("ComfyUI is up-to-date.") + return OperationResult.skip.value + + except Exception: + traceback.print_exc() + + return "An error occurred while updating 'comfyui'." + + async def do_fix(params: FixPackParams) -> str: + if not security_utils.is_allowed_security_level('middle'): + logging.error(SECURITY_MESSAGE_MIDDLE) + return OperationResult.failed.value + + node_name = params.node_name + node_ver = params.node_ver + + try: + res = core.unified_manager.unified_fix(node_name, node_ver) + + if res.result: + return OperationResult.success.value + else: + logging.error(res.msg) + + logging.error( + f"\nERROR: An error occurred while fixing '{node_name}@{node_ver}'." + ) + except Exception: + traceback.print_exc() + + return f"An error occurred while fixing '{node_name}@{node_ver}'." + + async def do_uninstall(params: UninstallPackParams) -> str: + if not security_utils.is_allowed_security_level('middle'): + logging.error(SECURITY_MESSAGE_MIDDLE) + return OperationResult.failed.value + + node_name = params.node_name + is_unknown = params.is_unknown + + logging.debug( + "[ComfyUI-Manager] Uninstalling node: name=%s, is_unknown=%s", + node_name, + is_unknown, + ) + + try: + res = core.unified_manager.unified_uninstall(node_name, is_unknown) + + if res.result: + return OperationResult.success.value + + logging.error( + f"\nERROR: An error occurred while uninstalling '{node_name}'." + ) + except Exception: + traceback.print_exc() + + return f"An error occurred while uninstalling '{node_name}'." + + async def do_disable(params: DisablePackParams) -> str: + node_name = params.node_name + + logging.debug( + "[ComfyUI-Manager] Disabling node: name=%s, is_unknown=%s", + node_name, + params.is_unknown, + ) + + try: + res = core.unified_manager.unified_disable(node_name, params.is_unknown) + + if res: + return OperationResult.success.value + + except Exception: + traceback.print_exc() + + return f"Failed to disable: '{node_name}'" + + async def do_install_model(params: ModelMetadata) -> str: + if not security_utils.is_allowed_security_level('middle+'): + logging.error(SECURITY_MESSAGE_MIDDLE_P) + return OperationResult.failed.value + + json_data = params.model_dump() + + model_path = model_utils.get_model_path(json_data) + model_url = json_data.get("url") + + res = False + + try: + if model_path is not None: + logging.info( + f"Install model '{json_data['name']}' from '{model_url}' into '{model_path}'" + ) + + if json_data["filename"] == "": + if os.path.exists( + os.path.join(model_path, os.path.dirname(json_data["url"])) + ): + logging.error( + f"[ComfyUI-Manager] the model path already exists: {model_path}" + ) + return f"The model path already exists: {model_path}" + + logging.info( + f"[ComfyUI-Manager] Downloading '{model_url}' into '{model_path}'" + ) + manager_downloader.download_repo_in_bytes( + repo_id=model_url, local_dir=model_path + ) + + return OperationResult.success.value + + elif not core.get_config()["model_download_by_agent"] and ( + model_url.startswith("https://github.com") + or model_url.startswith("https://huggingface.co") + or model_url.startswith("https://heibox.uni-heidelberg.de") + ): + model_dir = model_utils.get_model_dir(json_data, True) + download_url(model_url, model_dir, filename=json_data["filename"]) + if model_path.endswith(".zip"): + res = core.unzip(model_path) + else: + res = True + + if res: + return OperationResult.success.value + else: + res = download_url_with_agent(model_url, model_path) + if res and model_path.endswith(".zip"): + res = core.unzip(model_path) + else: + logging.error( + f"[ComfyUI-Manager] Model installation error: invalid model type - {json_data['type']}" + ) + + if res: + return OperationResult.success.value + + except Exception as e: + logging.error(f"[ComfyUI-Manager] ERROR: {e}") + + return f"Model installation error: {model_url}" + + while True: + timeout = 4.0 + task = task_queue.get(timeout) + if task is None: + is_empty_queue = ( + task_queue.total_count() == 0 and len(task_queue.running_tasks) == 0 + ) + if is_empty_queue: + logging.debug("[ComfyUI-Manager] Queue empty - all tasks completed") + + did_complete_tasks = task_queue.done_count() > 0 + if did_complete_tasks: + logging.debug( + "[ComfyUI-Manager] Finalizing batch history with %d completed tasks", + task_queue.done_count(), + ) + task_queue.finalize() + logging.debug("[ComfyUI-Manager] Batch finalization complete") + + logging.info("\nAfter restarting ComfyUI, please refresh the browser.") + + res = {"status": "all-done"} + + # Broadcast general status updates to all clients + logging.debug("[ComfyUI-Manager] Broadcasting queue all-done status") + PromptServer.instance.send_sync("cm-queue-status", res) + + logging.debug("[ComfyUI-Manager] Task worker exiting") + return + + item, task_index = task + kind = item.kind + + logging.debug( + "[ComfyUI-Manager] Processing task: kind=%s, ui_id=%s, client_id=%s, task_index=%d", + kind, + item.ui_id, + item.client_id, + task_index, + ) + + try: + if kind == OperationType.install.value: + msg = await do_install(item.params) + elif kind == OperationType.enable.value: + msg = await do_enable(item.params) + elif kind == OperationType.install_model.value: + msg = await do_install_model(item.params) + elif kind == OperationType.update.value: + msg = await do_update(item.params) + elif kind == "update-main": + msg = await do_update(item.params) + elif kind == OperationType.update_comfyui.value: + msg = await do_update_comfyui(item.params) + elif kind == OperationType.fix.value: + msg = await do_fix(item.params) + elif kind == OperationType.uninstall.value: + msg = await do_uninstall(item.params) + elif kind == OperationType.disable.value: + msg = await do_disable(item.params) + else: + msg = "Unexpected kind: " + kind + except Exception: + msg = f"Exception: {(kind, item)}" + logging.error( + "[ComfyUI-Manager] Task execution exception: kind=%s, ui_id=%s, error=%s", + kind, + item.ui_id, + traceback.format_exc(), + ) + await task_queue.task_done( + item, + task_index, + msg, + TaskExecutionStatus( + status_str=OperationResult.error, completed=True, messages=[msg] + ), + ) + return + + # Determine status and message for task completion + if isinstance(msg, dict) and "msg" in msg: + result_msg = msg["msg"] + else: + result_msg = msg + + # Determine status + if result_msg == OperationResult.success.value: + status = TaskExecutionStatus( + status_str=OperationResult.success, completed=True, messages=[] + ) + elif result_msg == OperationResult.skip.value: + status = TaskExecutionStatus( + status_str=OperationResult.skip, completed=True, messages=[] + ) + else: + status = TaskExecutionStatus( + status_str=OperationResult.error, completed=True, messages=[result_msg] + ) + + logging.debug( + "[ComfyUI-Manager] Task execution completed: kind=%s, ui_id=%s, status=%s, result=%s", + kind, + item.ui_id, + status.status_str, + result_msg, + ) + await task_queue.task_done(item, task_index, result_msg, status) + + +@routes.post("/v2/manager/queue/task") +async def queue_task(request) -> web.Response: + """Add a new task to the processing queue. + + Accepts task data via JSON POST and adds it to the TaskQueue for processing. + The task worker will automatically pick up and process queued tasks. + + Args: + request: aiohttp request containing JSON task data + + Returns: + web.Response: HTTP 200 on successful queueing, HTTP 400 on validation error + """ + try: + json_data = await request.json() + # Validate input using Pydantic model + task_item = QueueTaskItem.model_validate(json_data) + logging.debug( + "[ComfyUI-Manager] Queueing task via API: kind=%s, ui_id=%s, client_id=%s", + task_item.kind, + task_item.ui_id, + task_item.client_id, + ) + TaskQueue.instance.put(task_item) + # maybe start worker + return web.Response(status=200) + except ValidationError as e: + logging.error(f"[ComfyUI-Manager] Invalid task data: {e}") + return web.Response(status=400, text=f"Invalid task data: {e}") + except Exception as e: + logging.error(f"[ComfyUI-Manager] Error processing task: {e}") + return web.Response(status=500, text="Internal server error") + + +@routes.get("/v2/manager/queue/history_list") +async def get_history_list(request) -> web.Response: + """Get list of available batch history files. + + Returns a list of batch history IDs sorted by modification time (newest first). + These IDs can be used with the history endpoint to retrieve detailed batch information. + + Returns: + web.Response: JSON response with 'ids' array of history file IDs + """ + history_path = context.manager_batch_history_path + + try: + files = [ + os.path.join(history_path, f) + for f in os.listdir(history_path) + if os.path.isfile(os.path.join(history_path, f)) + ] + files.sort(key=lambda x: os.path.getmtime(x), reverse=True) + history_ids = [os.path.basename(f)[:-5] for f in files] + + return web.json_response( + {"ids": list(history_ids)}, content_type="application/json" + ) + except Exception as e: + logging.error(f"[ComfyUI-Manager] /v2/manager/queue/history_list - {e}") + return web.Response(status=400) + + +@routes.get("/v2/manager/queue/history") +async def get_history(request): + """Get task history with optional client filtering. + + Query parameters: + id: Batch history ID (for file-based history) + client_id: Optional client ID to filter current session history + ui_id: Optional specific task ID to get single task history + max_items: Maximum number of items to return + offset: Offset for pagination + + Returns: + JSON with filtered history data + """ + try: + # Handle file-based batch history + if "id" in request.rel_url.query: + json_name = request.rel_url.query["id"] + ".json" + batch_path = os.path.join(context.manager_batch_history_path, json_name) + logging.debug( + "[ComfyUI-Manager] Fetching batch history: id=%s", + request.rel_url.query["id"], + ) + + with open(batch_path, "r", encoding="utf-8") as file: + json_str = file.read() + json_obj = json.loads(json_str) + return web.json_response(json_obj, content_type="application/json") + + # Handle current session history with optional filtering + client_id = request.rel_url.query.get("client_id") + ui_id = request.rel_url.query.get("ui_id") + max_items = request.rel_url.query.get("max_items") + offset = request.rel_url.query.get("offset", -1) + + logging.debug( + "[ComfyUI-Manager] Fetching history: client_id=%s, ui_id=%s, max_items=%s", + client_id, + ui_id, + max_items, + ) + + if max_items: + max_items = int(max_items) + if offset: + offset = int(offset) + + # Get history from TaskQueue + if ui_id: + history = task_queue.get_history(ui_id=ui_id) + else: + history = task_queue.get_history(max_items=max_items, offset=offset) + + # Filter by client_id if provided + if client_id and isinstance(history, dict): + filtered_history = { + task_id: task_data + for task_id, task_data in history.items() + if hasattr(task_data, "client_id") and task_data.client_id == client_id + } + history = filtered_history + + return web.json_response({"history": history}, content_type="application/json") + + except Exception as e: + logging.error(f"[ComfyUI-Manager] /v2/manager/queue/history - {e}") + + return web.Response(status=400) + + +@routes.get("/v2/customnode/getmappings") +async def fetch_customnode_mappings(request): + """ + provide unified (node -> node pack) mapping list + """ + mode = request.rel_url.query["mode"] + + nickname_mode = False + if mode == "nickname": + mode = "local" + nickname_mode = True + + json_obj = await core.get_data_by_mode(mode, "extension-node-map.json") + json_obj = core.map_to_unified_keys(json_obj) + + if nickname_mode: + json_obj = node_pack_utils.nickname_filter(json_obj) + + all_nodes = set() + patterns = [] + for k, x in json_obj.items(): + all_nodes.update(set(x[0])) + + if "nodename_pattern" in x[1]: + patterns.append((x[1]["nodename_pattern"], x[0])) + + missing_nodes = set(nodes.NODE_CLASS_MAPPINGS.keys()) - all_nodes + + for x in missing_nodes: + for pat, item in patterns: + if re.match(pat, x): + item.append(x) + + return web.json_response(json_obj, content_type="application/json") + + +@routes.get("/v2/customnode/fetch_updates") +async def fetch_updates(request): + """ + DEPRECATED: This endpoint is no longer supported. + + Repository fetching has been removed from the API. + Updates should be performed through the queue system using update operations. + """ + return web.json_response( + { + "error": "This endpoint has been deprecated", + "message": "Repository fetching is no longer supported. Please use the update operations through the queue system.", + "deprecated": True, + }, + status=410, # 410 Gone + ) + + +@routes.get("/v2/manager/queue/update_all") +async def update_all(request: web.Request) -> web.Response: + try: + # Validate query parameters using Pydantic model + query_params = UpdateAllQueryParams.model_validate(dict(request.rel_url.query)) + return await _update_all(query_params) + except ValidationError as e: + return web.json_response( + {"error": "Validation error", "details": e.errors()}, status=400 + ) + + +async def _update_all(params: UpdateAllQueryParams) -> web.Response: + if not security_utils.is_allowed_security_level("middle+"): + logging.error(SECURITY_MESSAGE_MIDDLE_P) + return web.Response(status=403) + + # Extract client info from validated params + base_ui_id = params.ui_id + client_id = params.client_id + mode = params.mode.value if params.mode else ManagerDatabaseSource.remote.value + + logging.debug( + "[ComfyUI-Manager] Update all requested: client_id=%s, base_ui_id=%s, mode=%s", + client_id, + base_ui_id, + mode, + ) + + if mode == ManagerDatabaseSource.local.value: + channel = "local" + else: + channel = core.get_config()["channel_url"] + + await core.unified_manager.reload(mode) + await core.unified_manager.get_custom_nodes(channel, mode) + + update_count = 0 + for k, v in core.unified_manager.active_nodes.items(): + if k == "comfyui-manager": + # skip updating comfyui-manager if desktop version + if os.environ.get("__COMFYUI_DESKTOP_VERSION__"): + continue + + update_task = QueueTaskItem( + kind=OperationType.update.value, + ui_id=f"{base_ui_id}_{k}", # Use client's base ui_id + node name + client_id=client_id, + params=UpdatePackParams(node_name=k, node_ver=v[0]), + ) + task_queue.put(update_task) + update_count += 1 + + for k, v in core.unified_manager.unknown_active_nodes.items(): + if k == "comfyui-manager": + # skip updating comfyui-manager if desktop version + if os.environ.get("__COMFYUI_DESKTOP_VERSION__"): + continue + + update_task = QueueTaskItem( + kind=OperationType.update.value, + ui_id=f"{base_ui_id}_{k}", # Use client's base ui_id + node name + client_id=client_id, + params=UpdatePackParams(node_name=k, node_ver="unknown"), + ) + task_queue.put(update_task) + update_count += 1 + + logging.debug( + "[ComfyUI-Manager] Update all queued %d tasks for client_id=%s", + update_count, + client_id, + ) + return web.Response(status=200) + + +@routes.get("/v2/manager/is_legacy_manager_ui") +async def is_legacy_manager_ui(request): + return web.json_response( + {"is_legacy_manager_ui": args.enable_manager_legacy_ui}, + content_type="application/json", + status=200, + ) + + +# freeze imported version +startup_time_installed_node_packs = core.get_installed_node_packs() + + +@routes.get("/v2/customnode/installed") +async def installed_list(request): + mode = request.query.get("mode", "default") + + if mode == "imported": + res = startup_time_installed_node_packs + else: + res = core.get_installed_node_packs() + + return web.json_response(res, content_type="application/json") + + +@routes.get("/v2/snapshot/getlist") +async def get_snapshot_list(request): + items = [ + f[:-5] for f in os.listdir(context.manager_snapshot_path) if f.endswith(".json") + ] + items.sort(reverse=True) + return web.json_response({"items": items}, content_type="application/json") + + +@routes.get("/v2/snapshot/remove") +async def remove_snapshot(request): + if not security_utils.is_allowed_security_level("middle"): + logging.error(SECURITY_MESSAGE_MIDDLE) + return web.Response(status=403) + + try: + target = request.rel_url.query["target"] + + path = os.path.join(context.manager_snapshot_path, f"{target}.json") + if os.path.exists(path): + os.remove(path) + + return web.Response(status=200) + except Exception: + return web.Response(status=400) + + +@routes.get("/v2/snapshot/restore") +async def restore_snapshot(request): + if not security_utils.is_allowed_security_level("middle+"): + logging.error(SECURITY_MESSAGE_MIDDLE_P) + return web.Response(status=403) + + try: + target = request.rel_url.query["target"] + + path = os.path.join(context.manager_snapshot_path, f"{target}.json") + if os.path.exists(path): + if not os.path.exists(context.manager_startup_script_path): + os.makedirs(context.manager_startup_script_path) + + target_path = os.path.join( + context.manager_startup_script_path, "restore-snapshot.json" + ) + shutil.copy(path, target_path) + + logging.info(f"Snapshot restore scheduled: `{target}`") + return web.Response(status=200) + + logging.error(f"Snapshot file not found: `{path}`") + return web.Response(status=400) + except Exception: + return web.Response(status=400) + + +@routes.get("/v2/snapshot/get_current") +async def get_current_snapshot_api(request): + try: + return web.json_response( + await core.get_current_snapshot(), content_type="application/json" + ) + except Exception: + return web.Response(status=400) + + +@routes.get("/v2/snapshot/save") +async def save_snapshot(request): + try: + await core.save_snapshot_with_postfix("snapshot") + return web.Response(status=200) + except Exception: + return web.Response(status=400) + + +def unzip_install(files): + temp_filename = "manager-temp.zip" + for url in files: + if url.endswith("/"): + url = url[:-1] + try: + headers = { + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3" + } + + req = urllib.request.Request(url, headers=headers) + response = urllib.request.urlopen(req) + data = response.read() + + with open(temp_filename, "wb") as f: + f.write(data) + + with zipfile.ZipFile(temp_filename, "r") as zip_ref: + zip_ref.extractall(core.get_default_custom_nodes_path()) + + os.remove(temp_filename) + except Exception as e: + logging.error(f"Install(unzip) error: {url} / {e}") + return False + + logging.info("Installation was successful.") + return True + + +@routes.post("/v2/customnode/import_fail_info") +async def import_fail_info(request): + try: + json_data = await request.json() + + # Basic validation - ensure we have either cnr_id or url + if not isinstance(json_data, dict): + return web.Response(status=400, text="Request body must be a JSON object") + + if "cnr_id" not in json_data and "url" not in json_data: + return web.Response( + status=400, text="Either 'cnr_id' or 'url' field is required" + ) + + if "cnr_id" in json_data: + if not isinstance(json_data["cnr_id"], str): + return web.Response(status=400, text="'cnr_id' must be a string") + module_name = core.unified_manager.get_module_name(json_data["cnr_id"]) + else: + if not isinstance(json_data["url"], str): + return web.Response(status=400, text="'url' must be a string") + module_name = core.unified_manager.get_module_name(json_data["url"]) + + if module_name is not None: + info = cm_global.error_dict.get(module_name) + if info is not None: + return web.json_response(info) + + return web.Response(status=400) + except Exception as e: + logging.error(f"[ComfyUI-Manager] Error processing import fail info: {e}") + return web.Response(status=500, text="Internal server error") + + +@routes.post("/v2/customnode/import_fail_info_bulk") +async def import_fail_info_bulk(request): + try: + json_data = await request.json() + + # Validate input using Pydantic model + request_data = ImportFailInfoBulkRequest.model_validate(json_data) + + # Ensure we have either cnr_ids or urls + if not request_data.cnr_ids and not request_data.urls: + return web.Response( + status=400, text="Either 'cnr_ids' or 'urls' field is required" + ) + + await core.unified_manager.reload('cache') + await core.unified_manager.get_custom_nodes('default', 'cache') + + results = {} + + if request_data.cnr_ids: + for cnr_id in request_data.cnr_ids: + module_name = core.unified_manager.get_module_name(cnr_id) + if module_name is not None: + info = cm_global.error_dict.get(module_name) + if info is not None: + results[cnr_id] = info + else: + results[cnr_id] = None + else: + results[cnr_id] = None + + if request_data.urls: + for url in request_data.urls: + module_name = core.unified_manager.get_module_name(url) + if module_name is not None: + info = cm_global.error_dict.get(module_name) + if info is not None: + results[url] = info + else: + results[url] = None + else: + results[url] = None + + # Create response using Pydantic model + response_data = ImportFailInfoBulkResponse(root=results) + return web.json_response(response_data.root) + except ValidationError as e: + logging.error(f"[ComfyUI-Manager] Invalid request data: {e}") + return web.Response(status=400, text=f"Invalid request data: {e}") + except Exception as e: + logging.error(f"[ComfyUI-Manager] Error processing bulk import fail info: {e}") + return web.Response(status=500, text="Internal server error") + + +@routes.get("/v2/manager/queue/reset") +async def reset_queue(request): + logging.debug("[ComfyUI-Manager] Queue reset requested") + task_queue.wipe_queue() + return web.Response(status=200) + + +@routes.get("/v2/manager/queue/status") +async def queue_count(request): + """Get current queue status with optional client filtering. + + Query parameters: + client_id: Optional client ID to filter tasks + + Returns: + JSON with queue counts and processing status + """ + client_id = request.query.get("client_id") + + if client_id: + # Filter tasks by client_id + running_client_tasks = [ + task + for task in task_queue.running_tasks.values() + if task.client_id == client_id + ] + pending_client_tasks = [ + item + for priority, counter, item in task_queue.pending_tasks + if item.client_id == client_id + ] + history_client_tasks = { + ui_id: task + for ui_id, task in task_queue.history_tasks.items() + if hasattr(task, "client_id") and task.client_id == client_id + } + + return web.json_response( + { + "client_id": client_id, + "total_count": len(pending_client_tasks) + len(running_client_tasks), + "done_count": len(history_client_tasks), + "in_progress_count": len(running_client_tasks), + "pending_count": len(pending_client_tasks), + "is_processing": len(running_client_tasks) > 0, + } + ) + else: + # Return overall status + return web.json_response( + { + "total_count": task_queue.total_count(), + "done_count": task_queue.done_count(), + "in_progress_count": len(task_queue.running_tasks), + "pending_count": len(task_queue.pending_tasks), + "is_processing": task_queue.is_processing(), + } + ) + + +@routes.get("/v2/manager/queue/start") +async def queue_start(request): + logging.debug("[ComfyUI-Manager] Queue start requested") + started = task_queue.start_worker() + + if started: + logging.debug("[ComfyUI-Manager] Queue worker started successfully") + return web.Response(status=200) # Started successfully + else: + logging.debug("[ComfyUI-Manager] Queue worker already in progress") + return web.Response(status=201) # Already in-progress + + +@routes.get("/v2/manager/queue/update_comfyui") +async def update_comfyui(request): + """Queue a ComfyUI update based on the configured update policy.""" + try: + # Validate query parameters using Pydantic model + query_params = UpdateComfyUIQueryParams.model_validate( + dict(request.rel_url.query) + ) + + # Check if stable parameter was provided, otherwise use config + if query_params.stable is None: + is_stable = core.get_config()["update_policy"] != "nightly-comfyui" + else: + is_stable = query_params.stable + + client_id = query_params.client_id + ui_id = query_params.ui_id + except ValidationError as e: + return web.json_response( + {"error": "Validation error", "details": e.errors()}, status=400 + ) + + # Create update-comfyui task + task = QueueTaskItem( + ui_id=ui_id, + client_id=client_id, + kind=OperationType.update_comfyui.value, + params=UpdateComfyUIParams(is_stable=is_stable), + ) + + task_queue.put(task) + return web.Response(status=200) + + +@routes.get("/v2/comfyui_manager/comfyui_versions") +async def comfyui_versions(request): + try: + res, current, latest = core.get_comfyui_versions() + return web.json_response( + {"versions": res, "current": current}, + status=200, + content_type="application/json", + ) + except Exception as e: + logging.error(f"ComfyUI update fail: {e}") + + return web.Response(status=400) + + +@routes.get("/v2/comfyui_manager/comfyui_switch_version") +async def comfyui_switch_version(request): + try: + # Validate query parameters using Pydantic model + query_params = ComfyUISwitchVersionQueryParams.model_validate( + dict(request.rel_url.query) + ) + + target_version = query_params.ver + client_id = query_params.client_id + ui_id = query_params.ui_id + + # Create update-comfyui task with target version + task = QueueTaskItem( + ui_id=ui_id, + client_id=client_id, + kind=OperationType.update_comfyui.value, + params=UpdateComfyUIParams(target_version=target_version), + ) + + task_queue.put(task) + return web.Response(status=200) + except ValidationError as e: + return web.json_response( + {"error": "Validation error", "details": e.errors()}, status=400 + ) + except Exception as e: + logging.error(f"ComfyUI version switch fail: {e}") + return web.Response(status=400) + + +@routes.post("/v2/manager/queue/install_model") +async def install_model(request): + try: + json_data = await request.json() + + # Validate required fields + if "client_id" not in json_data: + return web.Response(status=400, text="Missing required field: client_id") + if "ui_id" not in json_data: + return web.Response(status=400, text="Missing required field: ui_id") + + # Validate model metadata + model_data = ModelMetadata.model_validate(json_data) + + # Create install-model task with client-provided IDs + task = QueueTaskItem( + ui_id=json_data["ui_id"], + client_id=json_data["client_id"], + kind=OperationType.install_model.value, + params=model_data, + ) + + task_queue.put(task) + return web.Response(status=200) + except ValidationError as e: + logging.error(f"[ComfyUI-Manager] Invalid model data: {e}") + return web.Response(status=400, text=f"Invalid model data: {e}") + except Exception as e: + logging.error(f"[ComfyUI-Manager] Error processing model install: {e}") + return web.Response(status=500, text="Internal server error") + + +@routes.get("/v2/manager/db_mode") +async def db_mode(request): + if "value" in request.rel_url.query: + environment_utils.set_db_mode(request.rel_url.query["value"]) + core.write_config() + else: + return web.Response(text=core.get_config()["db_mode"], status=200) + + return web.Response(status=200) + + +@routes.get("/v2/manager/policy/update") +async def update_policy(request): + if "value" in request.rel_url.query: + environment_utils.set_update_policy(request.rel_url.query["value"]) + core.write_config() + else: + return web.Response(text=core.get_config()["update_policy"], status=200) + + return web.Response(status=200) + + +@routes.get("/v2/manager/channel_url_list") +async def channel_url_list(request): + channels = core.get_channel_dict() + if "value" in request.rel_url.query: + channel_url = channels.get(request.rel_url.query["value"]) + if channel_url is not None: + core.get_config()["channel_url"] = channel_url + core.write_config() + else: + selected = "custom" + selected_url = core.get_config()["channel_url"] + + for name, url in channels.items(): + if url == selected_url: + selected = name + break + + res = {"selected": selected, "list": core.get_channel_list()} + return web.json_response(res, status=200) + + return web.Response(status=200) + + +@routes.get("/v2/manager/reboot") +def restart(self): + if not security_utils.is_allowed_security_level("middle"): + logging.error(SECURITY_MESSAGE_MIDDLE) + return web.Response(status=403) + + try: + sys.stdout.close_log() + except Exception: + pass + + if "__COMFY_CLI_SESSION__" in os.environ: + with open(os.path.join(os.environ["__COMFY_CLI_SESSION__"] + ".reboot"), "w"): + pass + + print( + "\nRestarting...\n\n" + ) # This printing should not be logging - that will be ugly + exit(0) + + print( + "\nRestarting... [Legacy Mode]\n\n" + ) # This printing should not be logging - that will be ugly + + sys_argv = sys.argv.copy() + if "--windows-standalone-build" in sys_argv: + sys_argv.remove("--windows-standalone-build") + + if sys_argv[0].endswith("__main__.py"): # this is a python module + module_name = os.path.basename(os.path.dirname(sys_argv[0])) + cmds = [sys.executable, "-m", module_name] + sys_argv[1:] + elif sys.platform.startswith("win32"): + cmds = ['"' + sys.executable + '"', '"' + sys_argv[0] + '"'] + sys_argv[1:] + else: + cmds = [sys.executable] + sys_argv + + print(f"Command: {cmds}", flush=True) + + return os.execv(sys.executable, cmds) + + +@routes.get("/v2/manager/version") +async def get_version(request): + return web.Response(text=core.version_str, status=200) + + +async def _confirm_try_install(sender, custom_node_url, msg): + json_obj = await core.get_data_by_mode("default", "custom-node-list.json") + + sender = manager_util.sanitize_tag(sender) + msg = manager_util.sanitize_tag(msg) + target = core.lookup_customnode_by_url(json_obj, custom_node_url) + + if target is not None: + PromptServer.instance.send_sync( + "cm-api-try-install-customnode", + {"sender": sender, "target": target, "msg": msg}, + ) + else: + logging.error( + f"[ComfyUI Manager API] Failed to try install - Unknown custom node url '{custom_node_url}'" + ) + + +def confirm_try_install(sender, custom_node_url, msg): + asyncio.run(_confirm_try_install(sender, custom_node_url, msg)) + + +cm_global.register_api("cm.try-install-custom-node", confirm_try_install) + + +async def default_cache_update(): + core.refresh_channel_dict() + channel_url = core.get_config()["channel_url"] + + async def get_cache(filename): + try: + if core.get_config()["default_cache_as_channel_url"]: + uri = f"{channel_url}/{filename}" + else: + uri = f"{core.DEFAULT_CHANNEL}/{filename}" + + cache_uri = str(manager_util.simple_hash(uri)) + "_" + filename + cache_uri = os.path.join(manager_util.cache_dir, cache_uri) + + json_obj = await manager_util.get_data(uri, True) + + with manager_util.cache_lock: + with open(cache_uri, "w", encoding="utf-8") as file: + json.dump(json_obj, file, indent=4, sort_keys=True) + logging.debug(f"[ComfyUI-Manager] default cache updated: {uri}") + except Exception as e: + logging.error( + f"[ComfyUI-Manager] Failed to perform initial fetching '{filename}': {e}" + ) + traceback.print_exc() + + if ( + core.get_config()["network_mode"] != "offline" + and not manager_util.is_manager_pip_package() + ): + a = get_cache("custom-node-list.json") + b = get_cache("extension-node-map.json") + c = get_cache("model-list.json") + d = get_cache("alter-list.json") + e = get_cache("github-stats.json") + + await asyncio.gather(a, b, c, d, e) + + if core.get_config()["network_mode"] == "private": + logging.info( + "[ComfyUI-Manager] The private comfyregistry is not yet supported in `network_mode=private`." + ) + else: + # load at least once + await core.unified_manager.reload( + ManagerDatabaseSource.remote.value, dont_wait=False + ) + await core.unified_manager.get_custom_nodes( + channel_url, ManagerDatabaseSource.remote.value + ) + else: + await core.unified_manager.reload( + ManagerDatabaseSource.remote.value, dont_wait=False, update_cnr_map=False + ) + + logging.info("[ComfyUI-Manager] All startup tasks have been completed.") + + +threading.Thread(target=lambda: asyncio.run(default_cache_update())).start() + +if not os.path.exists(context.manager_config_path): + core.get_config() + core.write_config() + diff --git a/glob/share_3rdparty.py b/comfyui_manager/glob/share_3rdparty.py similarity index 83% rename from glob/share_3rdparty.py rename to comfyui_manager/glob/share_3rdparty.py index 837176fb..9a30619f 100644 --- a/glob/share_3rdparty.py +++ b/comfyui_manager/glob/share_3rdparty.py @@ -1,5 +1,7 @@ import mimetypes -import manager_core as core +from ..common import context +from . import manager_core as core + import os from aiohttp import web import aiohttp @@ -8,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\n") + matrix_nio_is_available = False def extract_model_file_names(json_data): @@ -53,7 +65,7 @@ def compute_sha256_checksum(filepath): return sha256.hexdigest() -@PromptServer.instance.routes.get("/manager/share_option") +@PromptServer.instance.routes.get("/v2/manager/share_option") async def share_option(request): if "value" in request.rel_url.query: core.get_config()['share_option'] = request.rel_url.query['value'] @@ -65,21 +77,21 @@ async def share_option(request): def get_openart_auth(): - if not os.path.exists(os.path.join(core.manager_files_path, ".openart_key")): + if not os.path.exists(os.path.join(context.manager_files_path, ".openart_key")): return None try: - with open(os.path.join(core.manager_files_path, ".openart_key"), "r") as f: + with open(os.path.join(context.manager_files_path, ".openart_key"), "r") as f: openart_key = f.read().strip() return openart_key if openart_key else None - except: + except Exception: return None def get_matrix_auth(): - if not os.path.exists(os.path.join(core.manager_files_path, "matrix_auth")): + if not os.path.exists(os.path.join(context.manager_files_path, "matrix_auth")): return None try: - with open(os.path.join(core.manager_files_path, "matrix_auth"), "r") as f: + with open(os.path.join(context.manager_files_path, "matrix_auth"), "r") as f: matrix_auth = f.read() homeserver, username, password = matrix_auth.strip().split("\n") if not homeserver or not username or not password: @@ -89,40 +101,40 @@ def get_matrix_auth(): "username": username, "password": password, } - except: + except Exception: return None def get_comfyworkflows_auth(): - if not os.path.exists(os.path.join(core.manager_files_path, "comfyworkflows_sharekey")): + if not os.path.exists(os.path.join(context.manager_files_path, "comfyworkflows_sharekey")): return None try: - with open(os.path.join(core.manager_files_path, "comfyworkflows_sharekey"), "r") as f: + with open(os.path.join(context.manager_files_path, "comfyworkflows_sharekey"), "r") as f: share_key = f.read() if not share_key.strip(): return None return share_key - except: + except Exception: return None def get_youml_settings(): - if not os.path.exists(os.path.join(core.manager_files_path, ".youml")): + if not os.path.exists(os.path.join(context.manager_files_path, ".youml")): return None try: - with open(os.path.join(core.manager_files_path, ".youml"), "r") as f: + with open(os.path.join(context.manager_files_path, ".youml"), "r") as f: youml_settings = f.read().strip() return youml_settings if youml_settings else None - except: + except Exception: return None def set_youml_settings(settings): - with open(os.path.join(core.manager_files_path, ".youml"), "w") as f: + with open(os.path.join(context.manager_files_path, ".youml"), "w") as f: f.write(settings) -@PromptServer.instance.routes.get("/manager/get_openart_auth") +@PromptServer.instance.routes.get("/v2/manager/get_openart_auth") async def api_get_openart_auth(request): # print("Getting stored Matrix credentials...") openart_key = get_openart_auth() @@ -131,16 +143,16 @@ async def api_get_openart_auth(request): return web.json_response({"openart_key": openart_key}) -@PromptServer.instance.routes.post("/manager/set_openart_auth") +@PromptServer.instance.routes.post("/v2/manager/set_openart_auth") async def api_set_openart_auth(request): json_data = await request.json() openart_key = json_data['openart_key'] - with open(os.path.join(core.manager_files_path, ".openart_key"), "w") as f: + with open(os.path.join(context.manager_files_path, ".openart_key"), "w") as f: f.write(openart_key) return web.Response(status=200) -@PromptServer.instance.routes.get("/manager/get_matrix_auth") +@PromptServer.instance.routes.get("/v2/manager/get_matrix_auth") async def api_get_matrix_auth(request): # print("Getting stored Matrix credentials...") matrix_auth = get_matrix_auth() @@ -149,7 +161,7 @@ async def api_get_matrix_auth(request): return web.json_response(matrix_auth) -@PromptServer.instance.routes.get("/manager/youml/settings") +@PromptServer.instance.routes.get("/v2/manager/youml/settings") async def api_get_youml_settings(request): youml_settings = get_youml_settings() if not youml_settings: @@ -157,14 +169,14 @@ async def api_get_youml_settings(request): return web.json_response(json.loads(youml_settings)) -@PromptServer.instance.routes.post("/manager/youml/settings") +@PromptServer.instance.routes.post("/v2/manager/youml/settings") async def api_set_youml_settings(request): json_data = await request.json() set_youml_settings(json.dumps(json_data)) return web.Response(status=200) -@PromptServer.instance.routes.get("/manager/get_comfyworkflows_auth") +@PromptServer.instance.routes.get("/v2/manager/get_comfyworkflows_auth") async def api_get_comfyworkflows_auth(request): # Check if the user has provided Matrix credentials in a file called 'matrix_accesstoken' # in the same directory as the ComfyUI base folder @@ -175,31 +187,39 @@ async def api_get_comfyworkflows_auth(request): return web.json_response({"comfyworkflows_sharekey": comfyworkflows_auth}) -@PromptServer.instance.routes.post("/manager/set_esheep_workflow_and_images") +@PromptServer.instance.routes.post("/v2/manager/set_esheep_workflow_and_images") async def set_esheep_workflow_and_images(request): json_data = await request.json() - with open(os.path.join(core.manager_files_path, "esheep_share_message.json"), "w", encoding='utf-8') as file: + with open(os.path.join(context.manager_files_path, "esheep_share_message.json"), "w", encoding='utf-8') as file: json.dump(json_data, file, indent=4) return web.Response(status=200) -@PromptServer.instance.routes.get("/manager/get_esheep_workflow_and_images") +@PromptServer.instance.routes.get("/v2/manager/get_esheep_workflow_and_images") async def get_esheep_workflow_and_images(request): - with open(os.path.join(core.manager_files_path, "esheep_share_message.json"), 'r', encoding='utf-8') as file: + with open(os.path.join(context.manager_files_path, "esheep_share_message.json"), 'r', encoding='utf-8') as file: data = json.load(file) 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'] password = json_data['password'] - with open(os.path.join(core.manager_files_path, "matrix_auth"), "w") as f: + with open(os.path.join(context.manager_files_path, "matrix_auth"), "w") as f: f.write("\n".join([homeserver, username, password])) def set_comfyworkflows_auth(comfyworkflows_sharekey): - with open(os.path.join(core.manager_files_path, "comfyworkflows_sharekey"), "w") as f: + with open(os.path.join(context.manager_files_path, "comfyworkflows_sharekey"), "w") as f: f.write(comfyworkflows_sharekey) @@ -211,7 +231,7 @@ def has_provided_comfyworkflows_auth(comfyworkflows_sharekey): return comfyworkflows_sharekey.strip() -@PromptServer.instance.routes.post("/manager/share") +@PromptServer.instance.routes.post("/v2/manager/share") async def share_art(request): # get json data json_data = await request.json() @@ -233,7 +253,7 @@ async def share_art(request): try: output_to_share = potential_outputs[int(selected_output_index)] - except: + except Exception: # for now, pick the first output output_to_share = potential_outputs[0] @@ -329,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/comfyui_manager/glob/utils/__init__.py b/comfyui_manager/glob/utils/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/comfyui_manager/glob/utils/environment_utils.py b/comfyui_manager/glob/utils/environment_utils.py new file mode 100644 index 00000000..abcf175b --- /dev/null +++ b/comfyui_manager/glob/utils/environment_utils.py @@ -0,0 +1,142 @@ +import os +import git +import logging +import traceback + +from comfyui_manager.common import context +import folder_paths +from comfy.cli_args import args +import latent_preview + +from comfyui_manager.glob import manager_core as core +from comfyui_manager.common import cm_global + + +comfy_ui_hash = "-" +comfyui_tag = None + + +def print_comfyui_version(): + global comfy_ui_hash + global comfyui_tag + + is_detached = False + try: + repo = git.Repo(os.path.dirname(folder_paths.__file__)) + core.comfy_ui_revision = len(list(repo.iter_commits("HEAD"))) + + comfy_ui_hash = repo.head.commit.hexsha + cm_global.variables["comfyui.revision"] = core.comfy_ui_revision + + core.comfy_ui_commit_datetime = repo.head.commit.committed_datetime + cm_global.variables["comfyui.commit_datetime"] = core.comfy_ui_commit_datetime + + is_detached = repo.head.is_detached + current_branch = repo.active_branch.name + + comfyui_tag = context.get_comfyui_tag() + + try: + if ( + not os.environ.get("__COMFYUI_DESKTOP_VERSION__") + and core.comfy_ui_commit_datetime.date() + < core.comfy_ui_required_commit_datetime.date() + ): + logging.warning( + f"\n\n## [WARN] ComfyUI-Manager: Your ComfyUI version ({core.comfy_ui_revision})[{core.comfy_ui_commit_datetime.date()}] is too old. Please update to the latest version. ##\n\n" + ) + except Exception: + pass + + # process on_revision_detected --> + if "cm.on_revision_detected_handler" in cm_global.variables: + for k, f in cm_global.variables["cm.on_revision_detected_handler"]: + try: + f(core.comfy_ui_revision) + except Exception: + logging.error(f"[ERROR] '{k}' on_revision_detected_handler") + traceback.print_exc() + + del cm_global.variables["cm.on_revision_detected_handler"] + else: + logging.warning( + "[ComfyUI-Manager] Some features are restricted due to your ComfyUI being outdated." + ) + # <-- + + if current_branch == "master": + if comfyui_tag: + logging.info( + f"### ComfyUI Version: {comfyui_tag} | Released on '{core.comfy_ui_commit_datetime.date()}'" + ) + else: + logging.info( + f"### ComfyUI Revision: {core.comfy_ui_revision} [{comfy_ui_hash[:8]}] | Released on '{core.comfy_ui_commit_datetime.date()}'" + ) + else: + if comfyui_tag: + logging.info( + f"### ComfyUI Version: {comfyui_tag} on '{current_branch}' | Released on '{core.comfy_ui_commit_datetime.date()}'" + ) + else: + logging.info( + f"### ComfyUI Revision: {core.comfy_ui_revision} on '{current_branch}' [{comfy_ui_hash[:8]}] | Released on '{core.comfy_ui_commit_datetime.date()}'" + ) + except Exception: + if is_detached: + logging.info( + f"### ComfyUI Revision: {core.comfy_ui_revision} [{comfy_ui_hash[:8]}] *DETACHED | Released on '{core.comfy_ui_commit_datetime.date()}'" + ) + else: + logging.info( + "### ComfyUI Revision: UNKNOWN (The currently installed ComfyUI is not a Git repository)" + ) + + +def set_preview_method(method): + if method == "auto": + args.preview_method = latent_preview.LatentPreviewMethod.Auto + elif method == "latent2rgb": + args.preview_method = latent_preview.LatentPreviewMethod.Latent2RGB + elif method == "taesd": + args.preview_method = latent_preview.LatentPreviewMethod.TAESD + else: + args.preview_method = latent_preview.LatentPreviewMethod.NoPreviews + + core.get_config()["preview_method"] = method + + +def set_update_policy(mode): + core.get_config()["update_policy"] = mode + + +def set_db_mode(mode): + core.get_config()["db_mode"] = mode + + +def setup_environment(): + git_exe = core.get_config()["git_exe"] + + if git_exe != "": + git.Git().update_environment(GIT_PYTHON_GIT_EXECUTABLE=git_exe) + + +def initialize_environment(): + context.comfy_path = os.path.dirname(folder_paths.__file__) + core.js_path = os.path.join(context.comfy_path, "web", "extensions") + + # Legacy database paths - kept for potential future use + # local_db_model = os.path.join(manager_util.comfyui_manager_path, "model-list.json") + # local_db_alter = os.path.join(manager_util.comfyui_manager_path, "alter-list.json") + # local_db_custom_node_list = os.path.join( + # manager_util.comfyui_manager_path, "custom-node-list.json" + # ) + # local_db_extension_node_mappings = os.path.join( + # manager_util.comfyui_manager_path, "extension-node-map.json" + # ) + + set_preview_method(core.get_config()["preview_method"]) + print_comfyui_version() + setup_environment() + + core.check_invalid_nodes() diff --git a/comfyui_manager/glob/utils/formatting_utils.py b/comfyui_manager/glob/utils/formatting_utils.py new file mode 100644 index 00000000..357112eb --- /dev/null +++ b/comfyui_manager/glob/utils/formatting_utils.py @@ -0,0 +1,60 @@ +import locale +import sys +import re + + +def handle_stream(stream, prefix): + stream.reconfigure(encoding=locale.getpreferredencoding(), errors="replace") + for msg in stream: + if ( + prefix == "[!]" + and ("it/s]" in msg or "s/it]" in msg) + and ("%|" in msg or "it [" in msg) + ): + if msg.startswith("100%"): + print("\r" + msg, end="", file=sys.stderr), + else: + print("\r" + msg[:-1], end="", file=sys.stderr), + else: + if prefix == "[!]": + print(prefix, msg, end="", file=sys.stderr) + else: + print(prefix, msg, end="") + + +def convert_markdown_to_html(input_text): + pattern_a = re.compile(r"\[a/([^]]+)]\(([^)]+)\)") + pattern_w = re.compile(r"\[w/([^]]+)]") + pattern_i = re.compile(r"\[i/([^]]+)]") + pattern_bold = re.compile(r"\*\*([^*]+)\*\*") + pattern_white = re.compile(r"%%([^*]+)%%") + + def replace_a(match): + return f"{match.group(1)}" + + def replace_w(match): + return f"

{match.group(1)}

" + + def replace_i(match): + return f"

{match.group(1)}

" + + def replace_bold(match): + return f"{match.group(1)}" + + def replace_white(match): + return f"{match.group(1)}" + + input_text = ( + input_text.replace("\\[", "[") + .replace("\\]", "]") + .replace("<", "<") + .replace(">", ">") + ) + + result_text = re.sub(pattern_a, replace_a, input_text) + result_text = re.sub(pattern_w, replace_w, result_text) + result_text = re.sub(pattern_i, replace_i, result_text) + result_text = re.sub(pattern_bold, replace_bold, result_text) + result_text = re.sub(pattern_white, replace_white, result_text) + + return result_text.replace("\n", "
") diff --git a/comfyui_manager/glob/utils/model_utils.py b/comfyui_manager/glob/utils/model_utils.py new file mode 100644 index 00000000..2225ec07 --- /dev/null +++ b/comfyui_manager/glob/utils/model_utils.py @@ -0,0 +1,161 @@ +import os +import logging +import concurrent.futures +import folder_paths + +from comfyui_manager.glob import manager_core as core +from comfyui_manager.glob.constants import model_dir_name_map, MODEL_DIR_NAMES + + +def get_model_dir(data, show_log=False): + if "download_model_base" in folder_paths.folder_names_and_paths: + models_base = folder_paths.folder_names_and_paths["download_model_base"][0][0] + else: + models_base = folder_paths.models_dir + + # NOTE: Validate to prevent path traversal. + if any(char in data["filename"] for char in {"/", "\\", ":"}): + return None + + def resolve_custom_node(save_path): + save_path = save_path[13:] # remove 'custom_nodes/' + + # NOTE: Validate to prevent path traversal. + if save_path.startswith(os.path.sep) or ":" in save_path: + return None + + repo_name = save_path.replace("\\", "/").split("/")[ + 0 + ] # get custom node repo name + + # NOTE: The creation of files within the custom node path should be removed in the future. + repo_path = core.lookup_installed_custom_nodes_legacy(repo_name) + if repo_path is not None and repo_path[0]: + # Returns the retargeted path based on the actually installed repository + return os.path.join(os.path.dirname(repo_path[1]), save_path) + else: + return None + + if data["save_path"] != "default": + if ".." in data["save_path"] or data["save_path"].startswith("/"): + if show_log: + logging.info( + f"[WARN] '{data['save_path']}' is not allowed path. So it will be saved into 'models/etc'." + ) + base_model = os.path.join(models_base, "etc") + else: + if data["save_path"].startswith("custom_nodes"): + base_model = resolve_custom_node(data["save_path"]) + if base_model is None: + if show_log: + logging.info( + f"[ComfyUI-Manager] The target custom node for model download is not installed: {data['save_path']}" + ) + return None + else: + base_model = os.path.join(models_base, data["save_path"]) + else: + model_dir_name = model_dir_name_map.get(data["type"].lower()) + if model_dir_name is not None: + base_model = folder_paths.folder_names_and_paths[model_dir_name][0][0] + else: + base_model = os.path.join(models_base, "etc") + + return base_model + + +def get_model_path(data, show_log=False): + base_model = get_model_dir(data, show_log) + if base_model is None: + return None + else: + if data["filename"] == "": + return os.path.join(base_model, os.path.basename(data["url"])) + else: + return os.path.join(base_model, data["filename"]) + + +def check_model_installed(json_obj): + def is_exists(model_dir_name, filename, url): + if filename == "": + filename = os.path.basename(url) + + dirs = folder_paths.get_folder_paths(model_dir_name) + + for x in dirs: + if os.path.exists(os.path.join(x, filename)): + return True + + return False + + total_models_files = set() + for x in MODEL_DIR_NAMES: + for y in folder_paths.get_filename_list(x): + total_models_files.add(y) + + def process_model_phase(item): + if ( + "diffusion" not in item["filename"] + and "pytorch" not in item["filename"] + and "model" not in item["filename"] + ): + # non-general name case + if item["filename"] in total_models_files: + item["installed"] = "True" + return + + if item["save_path"] == "default": + model_dir_name = model_dir_name_map.get(item["type"].lower()) + if model_dir_name is not None: + item["installed"] = str( + is_exists(model_dir_name, item["filename"], item["url"]) + ) + else: + item["installed"] = "False" + else: + model_dir_name = item["save_path"].split("/")[0] + if model_dir_name in folder_paths.folder_names_and_paths: + if is_exists(model_dir_name, item["filename"], item["url"]): + item["installed"] = "True" + + if "installed" not in item: + if item["filename"] == "": + filename = os.path.basename(item["url"]) + else: + filename = item["filename"] + + fullpath = os.path.join( + folder_paths.models_dir, item["save_path"], filename + ) + + item["installed"] = "True" if os.path.exists(fullpath) else "False" + + with concurrent.futures.ThreadPoolExecutor(8) as executor: + for item in json_obj["models"]: + executor.submit(process_model_phase, item) + + +async def check_whitelist_for_model(item): + from comfyui_manager.data_models import ManagerDatabaseSource + + json_obj = await core.get_data_by_mode(ManagerDatabaseSource.cache.value, "model-list.json") + + for x in json_obj.get("models", []): + if ( + x["save_path"] == item["save_path"] + and x["base"] == item["base"] + and x["filename"] == item["filename"] + ): + return True + + json_obj = await core.get_data_by_mode(ManagerDatabaseSource.local.value, "model-list.json") + + for x in json_obj.get("models", []): + if ( + x["save_path"] == item["save_path"] + and x["base"] == item["base"] + and x["filename"] == item["filename"] + ): + return True + + return False diff --git a/comfyui_manager/glob/utils/node_pack_utils.py b/comfyui_manager/glob/utils/node_pack_utils.py new file mode 100644 index 00000000..59ee2273 --- /dev/null +++ b/comfyui_manager/glob/utils/node_pack_utils.py @@ -0,0 +1,65 @@ +import concurrent.futures + +from comfyui_manager.glob import manager_core as core + + +def check_state_of_git_node_pack( + node_packs, do_fetch=False, do_update_check=True, do_update=False +): + if do_fetch: + print("Start fetching...", end="") + elif do_update: + print("Start updating...", end="") + elif do_update_check: + print("Start update check...", end="") + + def process_custom_node(item): + core.check_state_of_git_node_pack_single( + item, do_fetch, do_update_check, do_update + ) + + with concurrent.futures.ThreadPoolExecutor(4) as executor: + for k, v in node_packs.items(): + if v.get("active_version") in ["unknown", "nightly"]: + executor.submit(process_custom_node, v) + + if do_fetch: + print("\x1b[2K\rFetching done.") + elif do_update: + update_exists = any( + item.get("updatable", False) for item in node_packs.values() + ) + if update_exists: + print("\x1b[2K\rUpdate done.") + else: + print("\x1b[2K\rAll extensions are already up-to-date.") + elif do_update_check: + print("\x1b[2K\rUpdate check done.") + + +def nickname_filter(json_obj): + preemptions_map = {} + + for k, x in json_obj.items(): + if "preemptions" in x[1]: + for y in x[1]["preemptions"]: + preemptions_map[y] = k + elif k.endswith("/ComfyUI"): + for y in x[0]: + preemptions_map[y] = k + + updates = {} + for k, x in json_obj.items(): + removes = set() + for y in x[0]: + k2 = preemptions_map.get(y) + if k2 is not None and k != k2: + removes.add(y) + + if len(removes) > 0: + updates[k] = [y for y in x[0] if y not in removes] + + for k, v in updates.items(): + json_obj[k][0] = v + + return json_obj diff --git a/comfyui_manager/glob/utils/security_utils.py b/comfyui_manager/glob/utils/security_utils.py new file mode 100644 index 00000000..1f2ac581 --- /dev/null +++ b/comfyui_manager/glob/utils/security_utils.py @@ -0,0 +1,67 @@ +from comfyui_manager.glob import manager_core as core +from comfy.cli_args import args +from comfyui_manager.data_models import SecurityLevel, RiskLevel, ManagerDatabaseSource + + +def is_loopback(address): + import ipaddress + try: + return ipaddress.ip_address(address).is_loopback + except ValueError: + return False + + +def is_allowed_security_level(level): + is_local_mode = is_loopback(args.listen) + is_personal_cloud = core.get_config()['network_mode'].lower() == 'personal_cloud' + + if level == RiskLevel.block.value: + return False + elif level == RiskLevel.high_.value: + if is_local_mode: + return core.get_config()['security_level'] in [SecurityLevel.weak.value, SecurityLevel.normal_.value] + elif is_personal_cloud: + return core.get_config()['security_level'] == SecurityLevel.weak.value + else: + return False + elif level == RiskLevel.high.value: + if is_local_mode: + return core.get_config()['security_level'] in [SecurityLevel.weak.value, SecurityLevel.normal_.value] + else: + return core.get_config()['security_level'] == SecurityLevel.weak.value + elif level == RiskLevel.middle_.value: + if is_local_mode or is_personal_cloud: + return core.get_config()['security_level'] in [SecurityLevel.weak.value, SecurityLevel.normal.value, SecurityLevel.normal_.value] + else: + return False + elif level == RiskLevel.middle.value: + return core.get_config()['security_level'] in [SecurityLevel.weak.value, SecurityLevel.normal.value, SecurityLevel.normal_.value] + else: + return True + + +async def get_risky_level(files, pip_packages): + json_data1 = await core.get_data_by_mode(ManagerDatabaseSource.local.value, "custom-node-list.json") + json_data2 = await core.get_data_by_mode( + ManagerDatabaseSource.cache.value, + "custom-node-list.json", + channel_url="https://raw.githubusercontent.com/ltdrdata/ComfyUI-Manager/main", + ) + + all_urls = set() + for x in json_data1["custom_nodes"] + json_data2["custom_nodes"]: + all_urls.update(x.get("files", [])) + + for x in files: + if x not in all_urls: + return RiskLevel.high_.value + + all_pip_packages = set() + for x in json_data1["custom_nodes"] + json_data2["custom_nodes"]: + all_pip_packages.update(x.get("pip", [])) + + for p in pip_packages: + if p not in all_pip_packages: + return RiskLevel.block.value + + return RiskLevel.middle_.value diff --git a/js/README.md b/comfyui_manager/js/README.md similarity index 100% rename from js/README.md rename to comfyui_manager/js/README.md diff --git a/js/cm-api.js b/comfyui_manager/js/cm-api.js similarity index 90% rename from js/cm-api.js rename to comfyui_manager/js/cm-api.js index dabc6f1d..21e9f705 100644 --- a/js/cm-api.js +++ b/comfyui_manager/js/cm-api.js @@ -25,7 +25,7 @@ async function tryInstallCustomNode(event) { const res = await customConfirm(msg); if(res) { if(event.detail.target.installed == 'Disabled') { - const response = await api.fetchApi(`/customnode/toggle_active`, { + const response = await api.fetchApi(`/v2/customnode/toggle_active`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(event.detail.target) @@ -35,7 +35,7 @@ async function tryInstallCustomNode(event) { await sleep(300); app.ui.dialog.show(`Installing... '${event.detail.target.title}'`); - const response = await api.fetchApi(`/customnode/install`, { + const response = await api.fetchApi(`/v2/customnode/install`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(event.detail.target) @@ -52,7 +52,7 @@ async function tryInstallCustomNode(event) { } } - let response = await api.fetchApi("/manager/reboot"); + let response = await api.fetchApi("/v2/manager/reboot"); if(response.status == 403) { show_message('This action is not allowed with this security level configuration.'); return false; diff --git a/js/comfyui-manager.js b/comfyui_manager/js/comfyui-manager.js similarity index 94% rename from js/comfyui-manager.js rename to comfyui_manager/js/comfyui-manager.js index 6fc504b1..7baada7f 100644 --- a/js/comfyui-manager.js +++ b/comfyui_manager/js/comfyui-manager.js @@ -14,9 +14,9 @@ import { OpenArtShareDialog } from "./comfyui-share-openart.js"; import { free_models, install_pip, install_via_git_url, manager_instance, rebootAPI, setManagerInstance, show_message, customAlert, customPrompt, - infoToast, showTerminal, setNeedRestart + infoToast, showTerminal, setNeedRestart, generateUUID } from "./common.js"; -import { ComponentBuilderDialog, getPureName, load_components, set_component_policy } from "./components-manager.js"; +import { ComponentBuilderDialog, load_components, set_component_policy } from "./components-manager.js"; import { CustomNodesManager } from "./custom-nodes-manager.js"; import { ModelManager } from "./model-manager.js"; import { SnapshotManager } from "./snapshot.js"; @@ -189,8 +189,7 @@ docStyle.innerHTML = ` } `; -function is_legacy_front() { - let compareVersion = '1.2.49'; +function isBeforeFrontendVersion(compareVersion) { try { const frontendVersion = window['__COMFYUI_FRONTEND_VERSION__']; if (typeof frontendVersion !== 'string') { @@ -232,7 +231,7 @@ var restart_stop_button = null; var update_policy_combo = null; let share_option = 'all'; -var is_updating = false; +var batch_id = null; // copied style from https://github.com/pythongosssss/ComfyUI-Custom-Scripts @@ -415,7 +414,7 @@ const style = ` `; async function init_share_option() { - api.fetchApi('/manager/share_option') + api.fetchApi('/v2/manager/share_option') .then(response => response.text()) .then(data => { share_option = data || 'all'; @@ -423,7 +422,7 @@ async function init_share_option() { } async function init_notice(notice) { - api.fetchApi('/manager/notice') + api.fetchApi('/v2/manager/notice') .then(response => response.text()) .then(data => { notice.innerHTML = data; @@ -474,14 +473,19 @@ async function updateComfyUI() { let prev_text = update_comfyui_button.innerText; update_comfyui_button.innerText = "Updating ComfyUI..."; - set_inprogress_mode(); - - const response = await api.fetchApi('/manager/queue/update_comfyui'); - +// set_inprogress_mode(); showTerminal(); - is_updating = true; - await api.fetchApi('/manager/queue/start'); + batch_id = generateUUID(); + + let batch = {}; + batch['batch_id'] = batch_id; + batch['update_comfyui'] = true; + + const res = await api.fetchApi(`/v2/manager/queue/batch`, { + method: 'POST', + body: JSON.stringify(batch) + }); } function showVersionSelectorDialog(versions, current, onSelect) { @@ -612,7 +616,7 @@ async function switchComfyUI() { switch_comfyui_button.disabled = true; switch_comfyui_button.style.backgroundColor = "gray"; - let res = await api.fetchApi(`/comfyui_manager/comfyui_versions`, { cache: "no-store" }); + let res = await api.fetchApi(`/v2/comfyui_manager/comfyui_versions`, { cache: "no-store" }); switch_comfyui_button.disabled = false; switch_comfyui_button.style.backgroundColor = ""; @@ -631,14 +635,14 @@ async function switchComfyUI() { showVersionSelectorDialog(versions, obj.current, async (selected_version) => { if(selected_version == 'nightly') { update_policy_combo.value = 'nightly-comfyui'; - api.fetchApi('/manager/policy/update?value=nightly-comfyui'); + api.fetchApi('/v2/manager/policy/update?value=nightly-comfyui'); } else { update_policy_combo.value = 'stable-comfyui'; - api.fetchApi('/manager/policy/update?value=stable-comfyui'); + api.fetchApi('/v2/manager/policy/update?value=stable-comfyui'); } - let response = await api.fetchApi(`/comfyui_manager/comfyui_switch_version?ver=${selected_version}`, { cache: "no-store" }); + let response = await api.fetchApi(`/v2/comfyui_manager/comfyui_switch_version?ver=${selected_version}`, { cache: "no-store" }); if (response.status == 200) { infoToast(`ComfyUI version is switched to ${selected_version}`); } @@ -656,18 +660,17 @@ async function onQueueStatus(event) { const isElectron = 'electronAPI' in window; if(event.detail.status == 'in_progress') { - set_inprogress_mode(); +// set_inprogress_mode(); update_all_button.innerText = `in progress.. (${event.detail.done_count}/${event.detail.total_count})`; } - else if(event.detail.status == 'done') { + else if(event.detail.status == 'all-done') { reset_action_buttons(); - - if(!is_updating) { + } + else if(event.detail.status == 'batch-done') { + if(batch_id != event.detail.batch_id) { return; } - is_updating = false; - let success_list = []; let failed_list = []; let comfyui_state = null; @@ -767,41 +770,28 @@ api.addEventListener("cm-queue-status", onQueueStatus); async function updateAll(update_comfyui) { update_all_button.innerText = "Updating..."; - set_inprogress_mode(); +// set_inprogress_mode(); var mode = manager_instance.datasrc_combo.value; showTerminal(); + batch_id = generateUUID(); + + let batch = {}; if(update_comfyui) { update_all_button.innerText = "Updating ComfyUI..."; - await api.fetchApi('/manager/queue/update_comfyui'); + batch['update_comfyui'] = true; } - const response = await api.fetchApi(`/manager/queue/update_all?mode=${mode}`); + batch['update_all'] = mode; - if (response.status == 401) { - customAlert('Another task is already in progress. Please stop the ongoing task first.'); - } - else if(response.status == 200) { - is_updating = true; - await api.fetchApi('/manager/queue/start'); - } + const res = await api.fetchApi(`/v2/manager/queue/batch`, { + method: 'POST', + body: JSON.stringify(batch) + }); } -function newDOMTokenList(initialTokens) { - const tmp = document.createElement(`div`); - - const classList = tmp.classList; - if (initialTokens) { - initialTokens.forEach(token => { - classList.add(token); - }); - } - - return classList; - } - /** * Check whether the node is a potential output node (img, gif or video output) */ @@ -814,7 +804,7 @@ function restartOrStop() { rebootAPI(); } else { - api.fetchApi('/manager/queue/reset'); + api.fetchApi('/v2/manager/queue/reset'); infoToast('Cancel', 'Remaining tasks will stop after completing the current task.'); } } @@ -962,12 +952,12 @@ class ManagerMenuDialog extends ComfyDialog { this.datasrc_combo.appendChild($el('option', { value: 'local', text: 'DB: Local' }, [])); this.datasrc_combo.appendChild($el('option', { value: 'remote', text: 'DB: Channel (remote)' }, [])); - api.fetchApi('/manager/db_mode') + api.fetchApi('/v2/manager/db_mode') .then(response => response.text()) .then(data => { this.datasrc_combo.value = data; }); this.datasrc_combo.addEventListener('change', function (event) { - api.fetchApi(`/manager/db_mode?value=${event.target.value}`); + api.fetchApi(`/v2/manager/db_mode?value=${event.target.value}`); }); // preview method @@ -979,19 +969,19 @@ class ManagerMenuDialog extends ComfyDialog { preview_combo.appendChild($el('option', { value: 'latent2rgb', text: 'Preview method: Latent2RGB (fast)' }, [])); preview_combo.appendChild($el('option', { value: 'none', text: 'Preview method: None (very fast)' }, [])); - api.fetchApi('/manager/preview_method') + api.fetchApi('/v2/manager/preview_method') .then(response => response.text()) .then(data => { preview_combo.value = data; }); preview_combo.addEventListener('change', function (event) { - api.fetchApi(`/manager/preview_method?value=${event.target.value}`); + api.fetchApi(`/v2/manager/preview_method?value=${event.target.value}`); }); // channel let channel_combo = document.createElement("select"); channel_combo.setAttribute("title", "Configure the channel for retrieving data from the Custom Node list (including missing nodes) or the Model list."); channel_combo.className = "cm-menu-combo"; - api.fetchApi('/manager/channel_url_list') + api.fetchApi('/v2/manager/channel_url_list') .then(response => response.json()) .then(async data => { try { @@ -1004,7 +994,7 @@ class ManagerMenuDialog extends ComfyDialog { } channel_combo.addEventListener('change', function (event) { - api.fetchApi(`/manager/channel_url_list?value=${event.target.value}`); + api.fetchApi(`/v2/manager/channel_url_list?value=${event.target.value}`); }); channel_combo.value = data.selected; @@ -1032,7 +1022,7 @@ class ManagerMenuDialog extends ComfyDialog { share_combo.appendChild($el('option', { value: option[0], text: `Share: ${option[1]}` }, [])); } - api.fetchApi('/manager/share_option') + api.fetchApi('/v2/manager/share_option') .then(response => response.text()) .then(data => { share_combo.value = data || 'all'; @@ -1042,7 +1032,7 @@ class ManagerMenuDialog extends ComfyDialog { share_combo.addEventListener('change', function (event) { const value = event.target.value; share_option = value; - api.fetchApi(`/manager/share_option?value=${value}`); + api.fetchApi(`/v2/manager/share_option?value=${value}`); const shareButton = document.getElementById("shareButton"); if (value === 'none') { shareButton.style.display = "none"; @@ -1057,7 +1047,7 @@ class ManagerMenuDialog extends ComfyDialog { component_policy_combo.appendChild($el('option', { value: 'workflow', text: 'Component: Use workflow version' }, [])); component_policy_combo.appendChild($el('option', { value: 'higher', text: 'Component: Use higher version' }, [])); component_policy_combo.appendChild($el('option', { value: 'mine', text: 'Component: Use my version' }, [])); - api.fetchApi('/manager/policy/component') + api.fetchApi('/v2/manager/policy/component') .then(response => response.text()) .then(data => { component_policy_combo.value = data; @@ -1065,7 +1055,7 @@ class ManagerMenuDialog extends ComfyDialog { }); component_policy_combo.addEventListener('change', function (event) { - api.fetchApi(`/manager/policy/component?value=${event.target.value}`); + api.fetchApi(`/v2/manager/policy/component?value=${event.target.value}`); set_component_policy(event.target.value); }); @@ -1078,14 +1068,14 @@ class ManagerMenuDialog extends ComfyDialog { update_policy_combo.className = "cm-menu-combo"; update_policy_combo.appendChild($el('option', { value: 'stable-comfyui', text: 'Update: ComfyUI Stable Version' }, [])); update_policy_combo.appendChild($el('option', { value: 'nightly-comfyui', text: 'Update: ComfyUI Nightly Version' }, [])); - api.fetchApi('/manager/policy/update') + api.fetchApi('/v2/manager/policy/update') .then(response => response.text()) .then(data => { update_policy_combo.value = data; }); update_policy_combo.addEventListener('change', function (event) { - api.fetchApi(`/manager/policy/update?value=${event.target.value}`); + api.fetchApi(`/v2/manager/policy/update?value=${event.target.value}`); }); return [ @@ -1388,12 +1378,12 @@ class ManagerMenuDialog extends ComfyDialog { } async function getVersion() { - let version = await api.fetchApi(`/manager/version`); + let version = await api.fetchApi(`/v2/manager/version`); return await version.text(); } app.registerExtension({ - name: "Comfy.ManagerMenu", + name: "Comfy.Legacy.ManagerMenu", aboutPageBadges: [ { @@ -1524,8 +1514,6 @@ app.registerExtension({ tooltip: "Share" }).element ); - - app.menu?.settingsGroup.element.before(cmGroup.element); } catch(exception) { console.log('ComfyUI is outdated. New style menu based features are disabled.'); diff --git a/js/comfyui-share-common.js b/comfyui_manager/js/comfyui-share-common.js similarity index 97% rename from js/comfyui-share-common.js rename to comfyui_manager/js/comfyui-share-common.js index e6f3e103..0691dce9 100644 --- a/js/comfyui-share-common.js +++ b/comfyui_manager/js/comfyui-share-common.js @@ -172,7 +172,7 @@ export const shareToEsheep= () => { const nodes = app.graph._nodes const { potential_outputs, potential_output_nodes } = getPotentialOutputsAndOutputNodes(nodes); const workflow = prompt['workflow'] - api.fetchApi(`/manager/set_esheep_workflow_and_images`, { + api.fetchApi(`/v2/manager/set_esheep_workflow_and_images`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ @@ -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)"; @@ -812,7 +826,7 @@ export class ShareDialog extends ComfyDialog { // get the user's existing matrix auth and share key ShareDialog.matrix_auth = { homeserver: "matrix.org", username: "", password: "" }; try { - api.fetchApi(`/manager/get_matrix_auth`) + api.fetchApi(`/v2/manager/get_matrix_auth`) .then(response => response.json()) .then(data => { ShareDialog.matrix_auth = data; @@ -831,7 +845,7 @@ export class ShareDialog extends ComfyDialog { ShareDialog.cw_sharekey = ""; try { // console.log("Fetching comfyworkflows share key") - api.fetchApi(`/manager/get_comfyworkflows_auth`) + api.fetchApi(`/v2/manager/get_comfyworkflows_auth`) .then(response => response.json()) .then(data => { ShareDialog.cw_sharekey = data.comfyworkflows_sharekey; @@ -891,7 +905,7 @@ export class ShareDialog extends ComfyDialog { // Change the text of the share button to "Sharing..." to indicate that the share process has started this.share_button.textContent = "Sharing..."; - const response = await api.fetchApi(`/manager/share`, { + const response = await api.fetchApi(`/v2/manager/share`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ diff --git a/js/comfyui-share-copus.js b/comfyui_manager/js/comfyui-share-copus.js similarity index 100% rename from js/comfyui-share-copus.js rename to comfyui_manager/js/comfyui-share-copus.js diff --git a/js/comfyui-share-openart.js b/comfyui_manager/js/comfyui-share-openart.js similarity index 98% rename from js/comfyui-share-openart.js rename to comfyui_manager/js/comfyui-share-openart.js index 1c96a8c7..d867d4d7 100644 --- a/js/comfyui-share-openart.js +++ b/comfyui_manager/js/comfyui-share-openart.js @@ -67,7 +67,7 @@ export class OpenArtShareDialog extends ComfyDialog { async readKey() { let key = "" try { - key = await api.fetchApi(`/manager/get_openart_auth`) + key = await api.fetchApi(`/v2/manager/get_openart_auth`) .then(response => response.json()) .then(data => { return data.openart_key; @@ -82,7 +82,7 @@ export class OpenArtShareDialog extends ComfyDialog { } async saveKey(value) { - await api.fetchApi(`/manager/set_openart_auth`, { + await api.fetchApi(`/v2/manager/set_openart_auth`, { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({ @@ -399,7 +399,7 @@ export class OpenArtShareDialog extends ComfyDialog { form.append("file", uploadFile); try { const res = await this.fetchApi( - `/workflows/upload_thumbnail`, + `/v2/workflows/upload_thumbnail`, { method: "POST", body: form, @@ -459,7 +459,7 @@ export class OpenArtShareDialog extends ComfyDialog { throw new Error("Title is required"); } - const current_snapshot = await api.fetchApi(`/snapshot/get_current`) + const current_snapshot = await api.fetchApi(`/v2/snapshot/get_current`) .then(response => response.json()) .catch(error => { // console.log(error); @@ -489,7 +489,7 @@ export class OpenArtShareDialog extends ComfyDialog { try { const response = await this.fetchApi( - "/workflows/publish", + "/v2/workflows/publish", { method: "POST", headers: {"Content-Type": "application/json"}, diff --git a/js/comfyui-share-youml.js b/comfyui_manager/js/comfyui-share-youml.js similarity index 98% rename from js/comfyui-share-youml.js rename to comfyui_manager/js/comfyui-share-youml.js index efd8916f..20dc8a2e 100644 --- a/js/comfyui-share-youml.js +++ b/comfyui_manager/js/comfyui-share-youml.js @@ -179,7 +179,7 @@ export class YouMLShareDialog extends ComfyDialog { async loadToken() { let key = "" try { - const response = await api.fetchApi(`/manager/youml/settings`) + const response = await api.fetchApi(`/v2/manager/youml/settings`) const settings = await response.json() return settings.token } catch (error) { @@ -188,7 +188,7 @@ export class YouMLShareDialog extends ComfyDialog { } async saveToken(value) { - await api.fetchApi(`/manager/youml/settings`, { + await api.fetchApi(`/v2/manager/youml/settings`, { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({ @@ -380,7 +380,7 @@ export class YouMLShareDialog extends ComfyDialog { try { let snapshotData = null; try { - const snapshot = await api.fetchApi(`/snapshot/get_current`) + const snapshot = await api.fetchApi(`/v2/snapshot/get_current`) snapshotData = await snapshot.json() } catch (e) { console.error("Failed to get snapshot", e) diff --git a/js/common.js b/comfyui_manager/js/common.js similarity index 98% rename from js/common.js rename to comfyui_manager/js/common.js index 71cf58ea..ba255f79 100644 --- a/js/common.js +++ b/comfyui_manager/js/common.js @@ -172,7 +172,7 @@ export function rebootAPI() { customConfirm("Are you sure you'd like to reboot the server?").then((isConfirmed) => { if (isConfirmed) { try { - api.fetchApi("/manager/reboot"); + api.fetchApi("/v2/manager/reboot"); } catch(exception) {} } @@ -210,7 +210,7 @@ export async function install_pip(packages) { if(packages.includes('&')) app.ui.dialog.show(`Invalid PIP package enumeration: '${packages}'`); - const res = await api.fetchApi("/customnode/install/pip", { + const res = await api.fetchApi("/v2/customnode/install/pip", { method: "POST", body: packages, }); @@ -245,7 +245,7 @@ export async function install_via_git_url(url, manager_dialog) { show_message(`Wait...

Installing '${url}'`); - const res = await api.fetchApi("/customnode/install/git_url", { + const res = await api.fetchApi("/v2/customnode/install/git_url", { method: "POST", body: url, }); @@ -630,6 +630,14 @@ export function showTooltip(target, text, className = 'cn-tooltip', styleMap = { }); } +export function generateUUID() { + return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { + const r = Math.random() * 16 | 0; + const v = c === 'x' ? r : (r & 0x3 | 0x8); + return v.toString(16); + }); +} + function initTooltip () { const mouseenterHandler = (e) => { const target = e.target; diff --git a/js/components-manager.js b/comfyui_manager/js/components-manager.js similarity index 98% rename from js/components-manager.js rename to comfyui_manager/js/components-manager.js index 9244d2a4..551c11f5 100644 --- a/js/components-manager.js +++ b/comfyui_manager/js/components-manager.js @@ -64,7 +64,7 @@ function storeGroupNode(name, data, register=true) { } export async function load_components() { - let data = await api.fetchApi('/manager/component/loads', {method: "POST"}); + let data = await api.fetchApi('/v2/manager/component/loads', {method: "POST"}); let components = await data.json(); let start_time = Date.now(); @@ -222,7 +222,7 @@ async function save_as_component(node, version, author, prefix, nodename, packna pack_map[packname] = component_name; rpack_map[component_name] = subgraph; - const res = await api.fetchApi('/manager/component/save', { + const res = await api.fetchApi('/v2/manager/component/save', { method: "POST", headers: { "Content-Type": "application/json", @@ -259,7 +259,7 @@ async function import_component(component_name, component, mode) { workflow: component }; - const res = await api.fetchApi('/manager/component/save', { + const res = await api.fetchApi('/v2/manager/component/save', { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify(body) @@ -709,7 +709,7 @@ app.handleFile = handleFile; let current_component_policy = 'workflow'; try { - api.fetchApi('/manager/policy/component') + api.fetchApi('/v2/manager/policy/component') .then(response => response.text()) .then(data => { current_component_policy = data; }); } diff --git a/js/custom-nodes-manager.css b/comfyui_manager/js/custom-nodes-manager.css similarity index 100% rename from js/custom-nodes-manager.css rename to comfyui_manager/js/custom-nodes-manager.css diff --git a/js/custom-nodes-manager.js b/comfyui_manager/js/custom-nodes-manager.js similarity index 97% rename from js/custom-nodes-manager.js rename to comfyui_manager/js/custom-nodes-manager.js index a5683a2d..a9aec11a 100644 --- a/js/custom-nodes-manager.js +++ b/comfyui_manager/js/custom-nodes-manager.js @@ -7,7 +7,7 @@ import { fetchData, md5, icons, show_message, customConfirm, customAlert, customPrompt, sanitizeHTML, infoToast, showTerminal, setNeedRestart, storeColumnWidth, restoreColumnWidth, getTimeAgo, copyText, loadCss, - showPopover, hidePopover + showPopover, hidePopover, generateUUID } from "./common.js"; // https://cenfun.github.io/turbogrid/api.html @@ -66,7 +66,7 @@ export class CustomNodesManager { this.id = "cn-manager"; app.registerExtension({ - name: "Comfy.CustomNodesManager", + name: "Comfy.Legacy.CustomNodesManager", afterConfigureGraph: (missingNodeTypes) => { const item = this.getFilterItem(ShowMode.MISSING); if (item) { @@ -459,7 +459,7 @@ export class CustomNodesManager { ".cn-manager-stop": { click: () => { - api.fetchApi('/manager/queue/reset'); + api.fetchApi('/v2/manager/queue/reset'); infoToast('Cancel', 'Remaining tasks will stop after completing the current task.'); } }, @@ -635,7 +635,7 @@ export class CustomNodesManager { }; } - const response = await api.fetchApi(`/customnode/import_fail_info`, { + const response = await api.fetchApi(`/v2/customnode/import_fail_info`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(info) @@ -1244,7 +1244,7 @@ export class CustomNodesManager { async loadNodes(node_packs) { const mode = manager_instance.datasrc_combo.value; this.showStatus(`Loading node mappings (${mode}) ...`); - const res = await fetchData(`/customnode/getmappings?mode=${mode}`); + const res = await fetchData(`/v2/customnode/getmappings?mode=${mode}`); if (res.error) { console.log(res.error); return; @@ -1396,10 +1396,10 @@ export class CustomNodesManager { this.showLoading(); let res; if(is_enable) { - res = await api.fetchApi(`/customnode/disabled_versions/${node_id}`, { cache: "no-store" }); + res = await api.fetchApi(`/v2/customnode/disabled_versions/${node_id}`, { cache: "no-store" }); } else { - res = await api.fetchApi(`/customnode/versions/${node_id}`, { cache: "no-store" }); + res = await api.fetchApi(`/v2/customnode/versions/${node_id}`, { cache: "no-store" }); } this.hideLoading(); @@ -1441,13 +1441,6 @@ export class CustomNodesManager { } async installNodes(list, btn, title, selected_version) { - let stats = await api.fetchApi('/manager/queue/status'); - stats = await stats.json(); - if(stats.is_processing) { - customAlert(`[ComfyUI-Manager] There are already tasks in progress. Please try again after it is completed. (${stats.done_count}/${stats.total_count})`); - return; - } - const { target, label, mode} = btn; if(mode === "uninstall") { @@ -1474,10 +1467,10 @@ export class CustomNodesManager { let needRestart = false; let errorMsg = ""; - await api.fetchApi('/manager/queue/reset'); - let target_items = []; + let batch = {}; + for (const hash of list) { const item = this.grid.getRowItemBy("hash", hash); target_items.push(item); @@ -1519,23 +1512,11 @@ export class CustomNodesManager { api_mode = 'reinstall'; } - const res = await api.fetchApi(`/manager/queue/${api_mode}`, { - method: 'POST', - body: JSON.stringify(data) - }); - - if (res.status != 200) { - errorMsg = `'${item.title}': `; - - if(res.status == 403) { - errorMsg += `This action is not allowed with this security level configuration.\n`; - } else if(res.status == 404) { - errorMsg += `With the current security level configuration, only custom nodes from the "default channel" can be installed.\n`; - } else { - errorMsg += await res.text() + '\n'; - } - - break; + if(batch[api_mode]) { + batch[api_mode].push(data); + } + else { + batch[api_mode] = [data]; } } @@ -1552,7 +1533,24 @@ export class CustomNodesManager { } } else { - await api.fetchApi('/manager/queue/start'); + this.batch_id = generateUUID(); + batch['batch_id'] = this.batch_id; + + const res = await api.fetchApi(`/v2/manager/queue/batch`, { + method: 'POST', + body: JSON.stringify(batch) + }); + + let failed = await res.json(); + + if(failed.length > 0) { + for(let k in failed) { + let hash = failed[k]; + const item = this.grid.getRowItemBy("hash", hash); + errorMsg = `[FAIL] ${item.title}`; + } + } + this.showStop(); showTerminal(); } @@ -1560,6 +1558,9 @@ export class CustomNodesManager { async onQueueStatus(event) { let self = CustomNodesManager.instance; + // If legacy manager front is not open, return early (using new manager front) + if (self.element?.style.display === 'none') return + if(event.detail.status == 'in_progress' && event.detail.ui_target == 'nodepack_manager') { const hash = event.detail.target; @@ -1570,7 +1571,7 @@ export class CustomNodesManager { self.grid.updateCell(item, "action"); self.grid.setRowSelected(item, false); } - else if(event.detail.status == 'done') { + else if(event.detail.status == 'batch-done' && event.detail.batch_id == self.batch_id) { self.hideStop(); self.onQueueCompleted(event.detail); } @@ -1764,7 +1765,7 @@ export class CustomNodesManager { async getMissingNodesLegacy(hashMap, missing_nodes) { const mode = manager_instance.datasrc_combo.value; this.showStatus(`Loading missing nodes (${mode}) ...`); - const res = await fetchData(`/customnode/getmappings?mode=${mode}`); + const res = await fetchData(`/v2/customnode/getmappings?mode=${mode}`); if (res.error) { this.showError(`Failed to get custom node mappings: ${res.error}`); return; @@ -1879,7 +1880,7 @@ export class CustomNodesManager { async getAlternatives() { const mode = manager_instance.datasrc_combo.value; this.showStatus(`Loading alternatives (${mode}) ...`); - const res = await fetchData(`/customnode/alternatives?mode=${mode}`); + const res = await fetchData(`/v2/customnode/alternatives?mode=${mode}`); if (res.error) { this.showError(`Failed to get alternatives: ${res.error}`); return []; @@ -1927,7 +1928,7 @@ export class CustomNodesManager { infoToast('Fetching updated information. This may take some time if many custom nodes are installed.'); } - const res = await fetchData(`/customnode/getlist?mode=${mode}${skip_update}`); + const res = await fetchData(`/v2/customnode/getlist?mode=${mode}${skip_update}`); if (res.error) { this.showError("Failed to get custom node list."); this.hideLoading(); diff --git a/js/model-manager.css b/comfyui_manager/js/model-manager.css similarity index 100% rename from js/model-manager.css rename to comfyui_manager/js/model-manager.css diff --git a/js/model-manager.js b/comfyui_manager/js/model-manager.js similarity index 95% rename from js/model-manager.js rename to comfyui_manager/js/model-manager.js index 7811ab65..1752919a 100644 --- a/js/model-manager.js +++ b/comfyui_manager/js/model-manager.js @@ -3,7 +3,7 @@ import { $el } from "../../scripts/ui.js"; import { manager_instance, rebootAPI, fetchData, md5, icons, show_message, customAlert, infoToast, showTerminal, - storeColumnWidth, restoreColumnWidth, loadCss + storeColumnWidth, restoreColumnWidth, loadCss, generateUUID } from "./common.js"; import { api } from "../../scripts/api.js"; @@ -175,7 +175,7 @@ export class ModelManager { ".cmm-manager-stop": { click: () => { - api.fetchApi('/manager/queue/reset'); + api.fetchApi('/v2/manager/queue/reset'); infoToast('Cancel', 'Remaining tasks will stop after completing the current task.'); } }, @@ -435,24 +435,16 @@ export class ModelManager { } async installModels(list, btn) { - let stats = await api.fetchApi('/manager/queue/status'); - - stats = await stats.json(); - if(stats.is_processing) { - customAlert(`[ComfyUI-Manager] There are already tasks in progress. Please try again after it is completed. (${stats.done_count}/${stats.total_count})`); - return; - } - btn.classList.add("cmm-btn-loading"); this.showError(""); let needRefresh = false; let errorMsg = ""; - await api.fetchApi('/manager/queue/reset'); - let target_items = []; + let batch = {}; + for (const item of list) { this.grid.scrollRowIntoView(item); target_items.push(item); @@ -468,21 +460,12 @@ export class ModelManager { const data = item.originalData; data.ui_id = item.hash; - const res = await api.fetchApi(`/manager/queue/install_model`, { - method: 'POST', - body: JSON.stringify(data) - }); - if (res.status != 200) { - errorMsg = `'${item.name}': `; - - if(res.status == 403) { - errorMsg += `This action is not allowed with this security level configuration.\n`; - } else { - errorMsg += await res.text() + '\n'; - } - - break; + if(batch['install_model']) { + batch['install_model'].push(data); + } + else { + batch['install_model'] = [data]; } } @@ -499,7 +482,24 @@ export class ModelManager { } } else { - await api.fetchApi('/manager/queue/start'); + this.batch_id = generateUUID(); + batch['batch_id'] = this.batch_id; + + const res = await api.fetchApi(`/v2/manager/queue/batch`, { + method: 'POST', + body: JSON.stringify(batch) + }); + + let failed = await res.json(); + + if(failed.length > 0) { + for(let k in failed) { + let hash = failed[k]; + const item = self.grid.getRowItemBy("hash", hash); + errorMsg = `[FAIL] ${item.title}`; + } + } + this.showStop(); showTerminal(); } @@ -519,7 +519,7 @@ export class ModelManager { // self.grid.updateCell(item, "tg-column-select"); self.grid.updateRow(item); } - else if(event.detail.status == 'done') { + else if(event.detail.status == 'batch-done') { self.hideStop(); self.onQueueCompleted(event.detail); } @@ -645,7 +645,7 @@ export class ModelManager { const mode = manager_instance.datasrc_combo.value; - const res = await fetchData(`/externalmodel/getlist?mode=${mode}`); + const res = await fetchData(`/v2/externalmodel/getlist?mode=${mode}`); if (res.error) { this.showError("Failed to get external model list."); this.hideLoading(); diff --git a/js/node_fixer.js b/comfyui_manager/js/node_fixer.js similarity index 98% rename from js/node_fixer.js rename to comfyui_manager/js/node_fixer.js index 867a7b81..5509d1fa 100644 --- a/js/node_fixer.js +++ b/comfyui_manager/js/node_fixer.js @@ -142,7 +142,7 @@ function node_info_copy(src, dest, connect_both, copy_shape) { } app.registerExtension({ - name: "Comfy.Manager.NodeFixer", + name: "Comfy.Legacy.Manager.NodeFixer", beforeRegisterNodeDef(nodeType, nodeData, app) { addMenuHandler(nodeType, function (_, options) { options.push({ diff --git a/js/popover-helper.js b/comfyui_manager/js/popover-helper.js similarity index 100% rename from js/popover-helper.js rename to comfyui_manager/js/popover-helper.js diff --git a/js/snapshot.js b/comfyui_manager/js/snapshot.js similarity index 95% rename from js/snapshot.js rename to comfyui_manager/js/snapshot.js index 520ca615..9a5835f2 100644 --- a/js/snapshot.js +++ b/comfyui_manager/js/snapshot.js @@ -7,7 +7,7 @@ import { manager_instance, rebootAPI, show_message } from "./common.js"; async function restore_snapshot(target) { if(SnapshotManager.instance) { try { - const response = await api.fetchApi(`/snapshot/restore?target=${target}`, { cache: "no-store" }); + const response = await api.fetchApi(`/v2/snapshot/restore?target=${target}`, { cache: "no-store" }); if(response.status == 403) { show_message('This action is not allowed with this security level configuration.'); @@ -35,7 +35,7 @@ async function restore_snapshot(target) { async function remove_snapshot(target) { if(SnapshotManager.instance) { try { - const response = await api.fetchApi(`/snapshot/remove?target=${target}`, { cache: "no-store" }); + const response = await api.fetchApi(`/v2/snapshot/remove?target=${target}`, { cache: "no-store" }); if(response.status == 403) { show_message('This action is not allowed with this security level configuration.'); @@ -61,7 +61,7 @@ async function remove_snapshot(target) { async function save_current_snapshot() { try { - const response = await api.fetchApi('/snapshot/save', { cache: "no-store" }); + const response = await api.fetchApi('/v2/snapshot/save', { cache: "no-store" }); app.ui.dialog.close(); return true; } @@ -76,7 +76,7 @@ async function save_current_snapshot() { } async function getSnapshotList() { - const response = await api.fetchApi(`/snapshot/getlist`); + const response = await api.fetchApi(`/v2/snapshot/getlist`); const data = await response.json(); return data; } diff --git a/js/turbogrid.esm.js b/comfyui_manager/js/turbogrid.esm.js similarity index 100% rename from js/turbogrid.esm.js rename to comfyui_manager/js/turbogrid.esm.js diff --git a/js/workflow-metadata.js b/comfyui_manager/js/workflow-metadata.js similarity index 97% rename from js/workflow-metadata.js rename to comfyui_manager/js/workflow-metadata.js index 82dbe016..808dcf2c 100644 --- a/js/workflow-metadata.js +++ b/comfyui_manager/js/workflow-metadata.js @@ -38,7 +38,7 @@ class WorkflowMetadataExtension { * enabled is true if the node is enabled, false if it is disabled */ async getInstalledNodes() { - const res = await api.fetchApi("/customnode/installed"); + const res = await api.fetchApi("/v2/customnode/installed"); return await res.json(); } diff --git a/comfyui_manager/legacy/__init__.py b/comfyui_manager/legacy/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/comfyui_manager/legacy/manager_core.py b/comfyui_manager/legacy/manager_core.py new file mode 100644 index 00000000..52b47251 --- /dev/null +++ b/comfyui_manager/legacy/manager_core.py @@ -0,0 +1,3327 @@ +""" +description: + `manager_core` contains the core implementation of the management functions in ComfyUI-Manager. +""" + +import json +import logging +import os +import sys +import subprocess +import re +import shutil +import configparser +import platform +from datetime import datetime + +import git +from git.remote import RemoteProgress +from urllib.parse import urlparse +from tqdm.auto import tqdm +import time +import yaml +import zipfile +import traceback +from concurrent.futures import ThreadPoolExecutor, as_completed + +orig_print = print + +from rich import print +from packaging import version + +import uuid + +from ..common import cm_global +from ..common import cnr_utils +from ..common import manager_util +from ..common import git_utils +from ..common import manager_downloader +from ..common.node_package import InstalledNodePackage +from ..common.enums import NetworkMode, SecurityLevel, DBMode +from ..common import context + + +version_code = [4, 0] +version_str = f"V{version_code[0]}.{version_code[1]}" + (f'.{version_code[2]}' if len(version_code) > 2 else '') + + +DEFAULT_CHANNEL = "https://raw.githubusercontent.com/Comfy-Org/ComfyUI-Manager/main" + + +default_custom_nodes_path = None + + +class InvalidChannel(Exception): + def __init__(self, channel): + self.channel = channel + super().__init__(channel) + + +def get_default_custom_nodes_path(): + global default_custom_nodes_path + if default_custom_nodes_path is None: + try: + import folder_paths + default_custom_nodes_path = folder_paths.get_folder_paths("custom_nodes")[0] + except Exception: + default_custom_nodes_path = os.path.abspath(os.path.join(manager_util.comfyui_manager_path, '..')) + + return default_custom_nodes_path + + +def get_custom_nodes_paths(): + try: + import folder_paths + return folder_paths.get_folder_paths("custom_nodes") + except Exception: + custom_nodes_path = os.path.abspath(os.path.join(manager_util.comfyui_manager_path, '..')) + return [custom_nodes_path] + + +def get_script_env(): + new_env = os.environ.copy() + git_exe = get_config().get('git_exe') + if git_exe is not None: + new_env['GIT_EXE_PATH'] = git_exe + + if 'COMFYUI_PATH' not in new_env: + new_env['COMFYUI_PATH'] = context.comfy_path + + if 'COMFYUI_FOLDERS_BASE_PATH' not in new_env: + new_env['COMFYUI_FOLDERS_BASE_PATH'] = context.comfy_path + + return new_env + + +invalid_nodes = {} + + +def extract_base_custom_nodes_dir(x:str): + if os.path.dirname(x).endswith('.disabled'): + return os.path.dirname(os.path.dirname(x)) + elif x.endswith('.disabled'): + return os.path.dirname(x) + else: + return os.path.dirname(x) + + +def check_invalid_nodes(): + global invalid_nodes + + try: + import folder_paths + except Exception: + try: + sys.path.append(context.comfy_path) + import folder_paths + except Exception: + raise Exception(f"Invalid COMFYUI_FOLDERS_BASE_PATH: {context.comfy_path}") + + def check(root): + global invalid_nodes + + subdirs = [d for d in os.listdir(root) if os.path.isdir(os.path.join(root, d))] + for subdir in subdirs: + if subdir in ['.disabled', '__pycache__']: + continue + + package = unified_manager.installed_node_packages.get(subdir) + if not package: + continue + + if not package.isValid(): + invalid_nodes[subdir] = package.fullpath + + node_paths = folder_paths.get_folder_paths("custom_nodes") + for x in node_paths: + check(x) + + disabled_dir = os.path.join(x, '.disabled') + if os.path.exists(disabled_dir): + check(disabled_dir) + + if len(invalid_nodes): + print("\n-------------------- ComfyUI-Manager invalid nodes notice ----------------") + print("\nNodes requiring reinstallation have been detected:\n(Directly delete the corresponding path and reinstall.)\n") + + for x in invalid_nodes.values(): + print(x) + + print("\n---------------------------------------------------------------------------\n") + + +cached_config = None +js_path = None + +comfy_ui_required_revision = 1930 +comfy_ui_required_commit_datetime = datetime(2024, 1, 24, 0, 0, 0) + +comfy_ui_revision = "Unknown" +comfy_ui_commit_datetime = datetime(1900, 1, 1, 0, 0, 0) + +channel_dict = None +valid_channels = {'default', 'local'} +channel_list = None + + +def remap_pip_package(pkg): + if pkg in cm_global.pip_overrides: + res = cm_global.pip_overrides[pkg] + print(f"[ComfyUI-Manager] '{pkg}' is remapped to '{res}'") + return res + else: + return pkg + + +def is_blacklisted(name): + name = name.strip() + + pattern = r'([^<>!~=]+)([<>!~=]=?)([^ ]*)' + match = re.search(pattern, name) + + if match: + name = match.group(1) + + if name in cm_global.pip_blacklist: + return True + + if name in cm_global.pip_downgrade_blacklist: + pips = manager_util.get_installed_packages() + + if match is None: + if name in pips: + return True + elif match.group(2) in ['<=', '==', '<', '~=']: + if name in pips: + if manager_util.StrictVersion(pips[name]) >= manager_util.StrictVersion(match.group(3)): + return True + + return False + + +def is_installed(name): + name = name.strip() + + if name.startswith('#'): + return True + + pattern = r'([^<>!~=]+)([<>!~=]=?)([0-9.a-zA-Z]*)' + match = re.search(pattern, name) + + if match: + name = match.group(1) + + if name in cm_global.pip_blacklist: + return True + + if name in cm_global.pip_downgrade_blacklist: + pips = manager_util.get_installed_packages() + + if match is None: + if name in pips: + return True + elif match.group(2) in ['<=', '==', '<', '~=']: + if name in pips: + if manager_util.StrictVersion(pips[name]) >= manager_util.StrictVersion(match.group(3)): + print(f"[ComfyUI-Manager] skip black listed pip installation: '{name}'") + return True + + pkg = manager_util.get_installed_packages().get(name.lower()) + if pkg is None: + return False # update if not installed + + if match is None: + return True # don't update if version is not specified + + if match.group(2) in ['>', '>=']: + if manager_util.StrictVersion(pkg) < manager_util.StrictVersion(match.group(3)): + return False + elif manager_util.StrictVersion(pkg) > manager_util.StrictVersion(match.group(3)): + print(f"[SKIP] Downgrading pip package isn't allowed: {name.lower()} (cur={pkg})") + + if match.group(2) == '==': + if manager_util.StrictVersion(pkg) < manager_util.StrictVersion(match.group(3)): + return False + + if match.group(2) == '~=': + if manager_util.StrictVersion(pkg) == manager_util.StrictVersion(match.group(3)): + return False + + return name.lower() in manager_util.get_installed_packages() + + +def normalize_channel(channel): + if channel == 'local': + return channel + elif channel is None: + return None + elif channel.startswith('https://'): + return channel + elif channel.startswith('http://') and get_config()['http_channel_enabled'] == True: + return channel + + tmp_dict = get_channel_dict() + channel_url = tmp_dict.get(channel) + if channel_url: + return channel_url + + raise InvalidChannel(channel) + + +class ManagedResult: + def __init__(self, action): + self.action = action + self.items = [] + self.result = True + self.to_path = None + self.msg = None + self.target = None + self.postinstall = lambda: True + self.ver = None + + def append(self, item): + self.items.append(item) + + def fail(self, msg): + self.result = False + self.msg = msg + return self + + def with_target(self, target): + self.target = target + return self + + def with_msg(self, msg): + self.msg = msg + return self + + def with_postinstall(self, postinstall): + self.postinstall = postinstall + return self + + def with_ver(self, ver): + self.ver = ver + return self + + +class NormalizedKeyDict: + def __init__(self): + self._store = {} + self._key_map = {} + + def _normalize_key(self, key): + if isinstance(key, str): + return key.strip().lower() + return key + + def __setitem__(self, key, value): + norm_key = self._normalize_key(key) + self._key_map[norm_key] = key + self._store[key] = value + + def __getitem__(self, key): + norm_key = self._normalize_key(key) + original_key = self._key_map[norm_key] + return self._store[original_key] + + def __delitem__(self, key): + norm_key = self._normalize_key(key) + original_key = self._key_map.pop(norm_key) + del self._store[original_key] + + def __contains__(self, key): + return self._normalize_key(key) in self._key_map + + def get(self, key, default=None): + return self[key] if key in self else default + + def setdefault(self, key, default=None): + if key in self: + return self[key] + self[key] = default + return default + + def pop(self, key, default=None): + if key in self: + val = self[key] + del self[key] + return val + if default is not None: + return default + raise KeyError(key) + + def keys(self): + return self._store.keys() + + def values(self): + return self._store.values() + + def items(self): + return self._store.items() + + def __iter__(self): + return iter(self._store) + + def __len__(self): + return len(self._store) + + def __repr__(self): + return repr(self._store) + + def to_dict(self): + return dict(self._store) + + +class UnifiedManager: + def __init__(self): + self.installed_node_packages: dict[str, InstalledNodePackage] = {} + + self.cnr_inactive_nodes = NormalizedKeyDict() # node_id -> node_version -> fullpath + self.nightly_inactive_nodes = NormalizedKeyDict() # node_id -> fullpath + self.unknown_inactive_nodes = {} # node_id -> repo url * fullpath + self.active_nodes = NormalizedKeyDict() # node_id -> node_version * fullpath + self.unknown_active_nodes = {} # node_id -> repo url * fullpath + self.cnr_map = NormalizedKeyDict() # node_id -> cnr info + self.repo_cnr_map = {} # repo_url -> cnr info + self.custom_node_map_cache = {} # (channel, mode) -> augmented custom node list json + self.processed_install = set() + + def get_module_name(self, x): + info = self.active_nodes.get(x) + if info is None: + for url, fullpath in self.unknown_active_nodes.values(): + if url == x: + return os.path.basename(fullpath) + else: + return os.path.basename(info[1]) + + return None + + def get_cnr_by_repo(self, url): + return self.repo_cnr_map.get(git_utils.normalize_url(url)) + + def resolve_unspecified_version(self, node_name, guess_mode=None): + if guess_mode == 'active': + # priority: + # 1. CNR/nightly active nodes + # 2. unknown + # 3. Fail + + if node_name in self.cnr_map: + version_spec = self.get_from_cnr_active_nodes(node_name) + + if version_spec is None: + if node_name in self.unknown_active_nodes: + version_spec = "unknown" + else: + return None + + elif node_name in self.unknown_active_nodes: + version_spec = "unknown" + else: + return None + + elif guess_mode == 'inactive': + # priority: + # 1. CNR latest in inactive + # 2. nightly + # 3. unknown + # 4. Fail + + if node_name in self.cnr_map: + latest = self.get_from_cnr_inactive_nodes(node_name) + + if latest is not None: + version_spec = str(latest[0]) + else: + if node_name in self.nightly_inactive_nodes: + version_spec = "nightly" + else: + version_spec = "unknown" + + elif node_name in self.unknown_inactive_nodes: + version_spec = "unknown" + else: + return None + + else: + # priority: + # 1. CNR latest in world + # 2. unknown + + if node_name in self.cnr_map: + version_spec = self.cnr_map[node_name]['latest_version']['version'] + else: + version_spec = "unknown" + + return version_spec + + def resolve_node_spec(self, node_name, guess_mode=None): + """ + resolve to 'node_name, version_spec' from version string + + version string: + node_name@latest + node_name@nightly + node_name@unknown + node_name@ + node_name + + if guess_mode is 'active' or 'inactive' + return can be 'None' based on state check + otherwise + return 'unknown' version when failed to guess + """ + + spec = node_name.split('@') + + if len(spec) == 2: + node_name = spec[0] + version_spec = spec[1] + + if version_spec == 'latest': + if node_name not in self.cnr_map: + print(f"ERROR: '{node_name}' is not a CNR node.") + return None + else: + version_spec = self.cnr_map[node_name]['latest_version']['version'] + + elif guess_mode in ['active', 'inactive']: + node_name = spec[0] + version_spec = self.resolve_unspecified_version(node_name, guess_mode=guess_mode) + if version_spec is None: + return None + else: + node_name = spec[0] + version_spec = self.resolve_unspecified_version(node_name) + if version_spec is None: + return None + + return node_name, version_spec, len(spec) > 1 + + def resolve_from_path(self, fullpath): + url = git_utils.git_url(fullpath) + if url: + url = git_utils.normalize_url(url) + + cnr = self.get_cnr_by_repo(url) + commit_hash = git_utils.get_commit_hash(fullpath) + if cnr: + cnr_utils.generate_cnr_id(fullpath, cnr['id']) + return {'id': cnr['id'], 'cnr': cnr, 'ver': 'nightly', 'hash': commit_hash} + else: + url = os.path.basename(url) + if url.endswith('.git'): + url = url[:-4] + return {'id': url, 'ver': 'unknown', 'hash': commit_hash} + else: + info = cnr_utils.read_cnr_info(fullpath) + + if info: + cnr = self.cnr_map.get(info['id']) + if cnr: + # normalize version + # for example: 2.5 -> 2.5.0 + ver = str(manager_util.StrictVersion(info['version'])) + return {'id': cnr['id'], 'cnr': cnr, 'ver': ver} + else: + return {'id': info['id'], 'ver': info['version']} + else: + return None + + def update_cache_at_path(self, fullpath): + node_package = InstalledNodePackage.from_fullpath(fullpath, self.resolve_from_path) + self.installed_node_packages[node_package.id] = node_package + + if node_package.is_disabled and node_package.is_unknown: + url = git_utils.git_url(node_package.fullpath) + if url is not None: + url = git_utils.normalize_url(url) + self.unknown_inactive_nodes[node_package.id] = (url, node_package.fullpath) + + if node_package.is_disabled and node_package.is_nightly: + self.nightly_inactive_nodes[node_package.id] = node_package.fullpath + + if node_package.is_enabled and not node_package.is_unknown: + self.active_nodes[node_package.id] = node_package.version, node_package.fullpath + + if node_package.is_enabled and node_package.is_unknown: + url = git_utils.git_url(node_package.fullpath) + if url is not None: + url = git_utils.normalize_url(url) + self.unknown_active_nodes[node_package.id] = (url, node_package.fullpath) + + if node_package.is_from_cnr and node_package.is_disabled: + self.add_to_cnr_inactive_nodes(node_package.id, node_package.version, node_package.fullpath) + + def is_updatable(self, node_id): + cur_ver = self.get_cnr_active_version(node_id) + latest_ver = self.cnr_map[node_id]['latest_version']['version'] + + if cur_ver and latest_ver: + return self.safe_version(latest_ver) > self.safe_version(cur_ver) + + return False + + def fetch_or_pull_git_repo(self, is_pull=False): + updated = set() + failed = set() + + def check_update(node_name, fullpath, ver_spec): + try: + if is_pull: + is_updated, success = git_repo_update_check_with(fullpath, do_update=True) + else: + is_updated, success = git_repo_update_check_with(fullpath, do_fetch=True) + + return f"{node_name}@{ver_spec}", is_updated, success + except Exception: + traceback.print_exc() + + return f"{node_name}@{ver_spec}", False, False + + with ThreadPoolExecutor() as executor: + futures = [] + + for k, v in self.unknown_active_nodes.items(): + futures.append(executor.submit(check_update, k, v[1], 'unknown')) + + for k, v in self.active_nodes.items(): + if v[0] == 'nightly': + futures.append(executor.submit(check_update, k, v[1], 'nightly')) + + for future in as_completed(futures): + item, is_updated, success = future.result() + + if is_updated: + updated.add(item) + + if not success: + failed.add(item) + + return dict(updated=list(updated), failed=list(failed)) + + def is_enabled(self, node_id, version_spec=None): + """ + 1. true if node_id@ is enabled + 2. true if node_id@ is enabled and version_spec==None + 3. false otherwise + + remark: latest version_spec is not allowed. Must be resolved before call. + """ + if version_spec == "cnr": + return self.get_cnr_active_version(node_id) not in [None, 'nightly'] + elif version_spec == 'unknown' and self.is_unknown_active(node_id): + return True + elif version_spec is not None and self.get_cnr_active_version(node_id) == version_spec: + return True + elif version_spec is None and (node_id in self.active_nodes or node_id in self.unknown_active_nodes): + return True + return False + + def is_disabled(self, node_id, version_spec=None): + """ + 1. node_id@unknown is disabled if version_spec is @unknown + 2. node_id@nightly is disabled if version_spec is @nightly + 4. node_id@ is disabled if version_spec is not None + 5. not exists (active node_id) if version_spec is None + + remark: latest version_spec is not allowed. Must be resolved before call. + """ + if version_spec == "unknown": + return node_id in self.unknown_inactive_nodes + elif version_spec == "nightly": + return node_id in self.nightly_inactive_nodes + elif version_spec == "cnr": + res = self.cnr_inactive_nodes.get(node_id, None) + if res is None: + return False + + res = [x for x in res.keys() if x != 'nightly'] + return len(res) > 0 + elif version_spec is not None: + return version_spec in self.cnr_inactive_nodes.get(node_id, []) + + if node_id in self.nightly_inactive_nodes: + return True + elif node_id in self.unknown_inactive_nodes: + return True + + target = self.cnr_inactive_nodes.get(node_id, None) + if target is not None and target == version_spec: + return True + + return False + + def is_registered_in_cnr(self, node_id): + return node_id in self.cnr_map + + def get_cnr_active_version(self, node_id): + res = self.active_nodes.get(node_id) + if res: + return res[0] + else: + return None + + def is_unknown_active(self, node_id): + return node_id in self.unknown_active_nodes + + def add_to_cnr_inactive_nodes(self, node_id, ver, fullpath): + ver_map = self.cnr_inactive_nodes.get(node_id) + if ver_map is None: + ver_map = {} + self.cnr_inactive_nodes[node_id] = ver_map + + ver_map[ver] = fullpath + + def get_from_cnr_active_nodes(self, node_id): + ver_path = self.active_nodes.get(node_id) + if ver_path is None: + return None + + return ver_path[0] + + def get_from_cnr_inactive_nodes(self, node_id, ver=None): + ver_map = self.cnr_inactive_nodes.get(node_id) + if ver_map is None: + return None + + if ver is not None: + return ver_map.get(ver) + + latest = None + for k, v in ver_map.items(): + if latest is None: + latest = self.safe_version(k), v + continue + + cur_ver = self.safe_version(k) + if cur_ver > latest[0]: + latest = cur_ver, v + + return latest + + async def reload(self, cache_mode, dont_wait=True, update_cnr_map=True): + import folder_paths + + self.custom_node_map_cache = {} + self.cnr_inactive_nodes = {} # node_id -> node_version -> fullpath + self.nightly_inactive_nodes = {} # node_id -> fullpath + self.unknown_inactive_nodes = {} # node_id -> repo url * fullpath + self.unknown_active_nodes = {} # node_id -> repo url * fullpath + self.active_nodes = {} # node_id -> node_version * fullpath + + if get_config()['network_mode'] != 'public' or manager_util.is_manager_pip_package(): + dont_wait = True + + if update_cnr_map: + # reload 'cnr_map' and 'repo_cnr_map' + cnrs = await cnr_utils.get_cnr_data(cache_mode=cache_mode=='cache', dont_wait=dont_wait) + + for x in cnrs: + self.cnr_map[x['id']] = x + if 'repository' in x: + normalized_url = git_utils.normalize_url(x['repository']) + self.repo_cnr_map[normalized_url] = x + + # reload node status info from custom_nodes/* + for custom_nodes_path in folder_paths.get_folder_paths('custom_nodes'): + for x in os.listdir(custom_nodes_path): + fullpath = os.path.join(custom_nodes_path, x) + if os.path.isdir(fullpath): + if x not in ['__pycache__', '.disabled']: + self.update_cache_at_path(fullpath) + + # reload node status info from custom_nodes/.disabled/* + for custom_nodes_path in folder_paths.get_folder_paths('custom_nodes'): + disabled_dir = os.path.join(custom_nodes_path, '.disabled') + if os.path.exists(disabled_dir): + for x in os.listdir(disabled_dir): + fullpath = os.path.join(disabled_dir, x) + if os.path.isdir(fullpath): + self.update_cache_at_path(fullpath) + + @staticmethod + async def load_nightly(channel, mode): + if channel is None: + return {} + + res = {} + + channel_url = normalize_channel(channel) + if channel_url: + if mode not in ['remote', 'local', 'cache']: + print(f"[bold red]ERROR: Invalid mode is specified `--mode {mode}`[/bold red]", file=sys.stderr) + return {} + + # validate channel - only the channel set by the user is allowed. + if channel_url not in valid_channels: + logging.error(f'[ComfyUI-Manager] An invalid channel was used: {channel_url}') + raise InvalidChannel(channel_url) + + json_obj = await get_data_by_mode(mode, 'custom-node-list.json', channel_url=channel_url) + for x in json_obj['custom_nodes']: + try: + for y in x['files']: + if 'github.com' in y and not (y.endswith('.py') or y.endswith('.js')): + repo_name = y.split('/')[-1] + res[repo_name] = (x, False) + + if 'id' in x: + if x['id'] not in res: + res[x['id']] = (x, True) + except Exception: + logging.error(f"[ComfyUI-Manager] broken item:{x}") + + return res + + async def get_custom_nodes(self, channel, mode): + if channel is None and mode is None: + channel = 'default' + mode = 'cache' + + channel = normalize_channel(channel) + cache = self.custom_node_map_cache.get((channel, mode)) # CNR/nightly should always be based on the default channel. + + if cache is not None: + return cache + + channel = normalize_channel(channel) + nodes = await self.load_nightly(channel, mode) + + res = NormalizedKeyDict() + added_cnr = set() + for v in nodes.values(): + v = v[0] + if len(v['files']) == 1: + cnr = self.get_cnr_by_repo(v['files'][0]) + if cnr: + if 'latest_version' not in cnr: + v['cnr_latest'] = '0.0.0' + else: + v['cnr_latest'] = cnr['latest_version']['version'] + v['id'] = cnr['id'] + v['author'] = cnr['publisher']['name'] + v['title'] = cnr['name'] + v['description'] = cnr['description'] + v['health'] = '-' + if 'repository' in cnr: + v['repository'] = cnr['repository'] + added_cnr.add(cnr['id']) + node_id = v['id'] + else: + node_id = v['files'][0].split('/')[-1] + v['repository'] = v['files'][0] + res[node_id] = v + elif len(v['files']) > 1: + res[v['files'][0]] = v # A custom node composed of multiple url is treated as a single repository with one representative path + + self.custom_node_map_cache[(channel, mode)] = res + return res + + @staticmethod + def safe_version(ver_str): + try: + return version.parse(ver_str) + except Exception: + return version.parse("0.0.0") + + def execute_install_script(self, url, repo_path, instant_execution=False, lazy_mode=False, no_deps=False): + install_script_path = os.path.join(repo_path, "install.py") + requirements_path = os.path.join(repo_path, "requirements.txt") + + res = True + if lazy_mode: + install_cmd = ["#LAZY-INSTALL-SCRIPT", sys.executable] + return try_install_script(url, repo_path, install_cmd) + else: + if os.path.exists(requirements_path) and not no_deps: + print("Install: pip packages") + pip_fixer = manager_util.PIPFixer(manager_util.get_installed_packages(), context.comfy_path, context.manager_files_path) + lines = manager_util.robust_readlines(requirements_path) + for line in lines: + package_name = remap_pip_package(line.strip()) + if package_name and not package_name.startswith('#') and package_name not in self.processed_install: + self.processed_install.add(package_name) + install_cmd = manager_util.make_pip_cmd(["install", package_name]) + if package_name.strip() != "" and not package_name.startswith('#'): + res = res and try_install_script(url, repo_path, install_cmd, instant_execution=instant_execution) + + pip_fixer.fix_broken() + + if os.path.exists(install_script_path) and install_script_path not in self.processed_install: + self.processed_install.add(install_script_path) + print("Install: install script") + install_cmd = [sys.executable, "install.py"] + return res and try_install_script(url, repo_path, install_cmd, instant_execution=instant_execution) + + return res + + def reserve_cnr_switch(self, target, zip_url, from_path, to_path, no_deps): + script_path = os.path.join(context.manager_startup_script_path, "install-scripts.txt") + with open(script_path, "a") as file: + obj = [target, "#LAZY-CNR-SWITCH-SCRIPT", zip_url, from_path, to_path, no_deps, get_default_custom_nodes_path(), sys.executable] + file.write(f"{obj}\n") + + print(f"Installation reserved: {target}") + + return True + + def unified_fix(self, node_id, version_spec, instant_execution=False, no_deps=False): + """ + fix dependencies + """ + + result = ManagedResult('fix') + + if version_spec == 'unknown': + info = self.unknown_active_nodes.get(node_id) + else: + info = self.active_nodes.get(node_id) + + if info is None or not os.path.exists(info[1]): + return result.fail(f'not found: {node_id}@{version_spec}') + + self.execute_install_script(node_id, info[1], instant_execution=instant_execution, no_deps=no_deps) + + return result + + def cnr_switch_version(self, node_id, version_spec=None, instant_execution=False, no_deps=False, return_postinstall=False): + if instant_execution: + return self.cnr_switch_version_instant(node_id, version_spec, instant_execution, no_deps, return_postinstall) + else: + return self.cnr_switch_version_lazy(node_id, version_spec, no_deps, return_postinstall) + + def cnr_switch_version_lazy(self, node_id, version_spec=None, no_deps=False, return_postinstall=False): + """ + switch between cnr version (lazy mode) + """ + + result = ManagedResult('switch-cnr') + + node_info = cnr_utils.install_node(node_id, version_spec) + if node_info is None or not node_info.download_url: + return result.fail(f'not available node: {node_id}@{version_spec}') + + version_spec = node_info.version + + if self.active_nodes[node_id][0] == version_spec: + return ManagedResult('skip').with_msg("Up to date") + + zip_url = node_info.download_url + from_path = self.active_nodes[node_id][1] + target = node_id + to_path = os.path.join(get_default_custom_nodes_path(), target) + + def postinstall(): + return self.reserve_cnr_switch(target, zip_url, from_path, to_path, no_deps) + + if return_postinstall: + return result.with_postinstall(postinstall) + else: + if not postinstall(): + return result.fail(f"Failed to execute install script: {node_id}@{version_spec}") + + return result + + def cnr_switch_version_instant(self, node_id, version_spec=None, instant_execution=True, no_deps=False, return_postinstall=False): + """ + switch between cnr version + """ + + # 1. download + result = ManagedResult('switch-cnr') + + node_info = cnr_utils.install_node(node_id, version_spec) + if node_info is None or not node_info.download_url: + return result.fail(f'not available node: {node_id}@{version_spec}') + + version_spec = node_info.version + + if self.active_nodes[node_id][0] == version_spec: + return ManagedResult('skip').with_msg("Up to date") + + archive_name = f"CNR_temp_{str(uuid.uuid4())}.zip" # should be unpredictable name - security precaution + download_path = os.path.join(get_default_custom_nodes_path(), archive_name) + manager_downloader.basic_download_url(node_info.download_url, get_default_custom_nodes_path(), archive_name) + + # 2. extract files into + install_path = self.active_nodes[node_id][1] + extracted = manager_util.extract_package_as_zip(download_path, install_path) + os.remove(download_path) + + if extracted is None: + if len(os.listdir(install_path)) == 0: + shutil.rmtree(install_path) + + return result.fail(f'Empty archive file: {node_id}@{version_spec}') + + # 3. calculate garbage files (.tracking - extracted) + tracking_info_file = os.path.join(install_path, '.tracking') + prev_files = set() + with open(tracking_info_file, 'r') as f: + for line in f: + prev_files.add(line.strip()) + garbage = prev_files.difference(extracted) + garbage = [os.path.join(install_path, x) for x in garbage] + + # 4-1. remove garbage files + for x in garbage: + if os.path.isfile(x): + os.remove(x) + + # 4-2. remove garbage dir if empty + for x in garbage: + if os.path.isdir(x): + if not os.listdir(x): + os.rmdir(x) + + # 5. create .tracking file + tracking_info_file = os.path.join(install_path, '.tracking') + with open(tracking_info_file, "w", encoding='utf-8') as file: + file.write('\n'.join(list(extracted))) + + # 6. post install + result.target = version_spec + + def postinstall(): + res = self.execute_install_script(f"{node_id}@{version_spec}", install_path, instant_execution=instant_execution, no_deps=no_deps) + return res + + if return_postinstall: + return result.with_postinstall(postinstall) + else: + if not postinstall(): + return result.fail(f"Failed to execute install script: {node_id}@{version_spec}") + + return result + + def unified_enable(self, node_id: str, version_spec=None): + """ + priority if version_spec == None + 1. CNR latest in disk + 2. nightly + 3. unknown + + remark: latest version_spec is not allowed. Must be resolved before call. + """ + + result = ManagedResult('enable') + + if 'comfyui-manager' in node_id.lower(): + return result.fail(f"ignored: enabling '{node_id}'") + + if version_spec is None: + version_spec = self.resolve_unspecified_version(node_id, guess_mode='inactive') + if version is None: + return result.fail(f'Specified inactive node not exists: {node_id}') + + if self.is_enabled(node_id, version_spec): + return ManagedResult('skip').with_msg('Already enabled') + + if not self.is_disabled(node_id, version_spec): + return ManagedResult('skip').with_msg('Not installed') + + from_path = None + to_path = None + + if version_spec == 'unknown': + repo_and_path = self.unknown_inactive_nodes.get(node_id) + if repo_and_path is None: + return result.fail(f'Specified inactive node not exists: {node_id}@unknown') + from_path = repo_and_path[1] + + base_path = extract_base_custom_nodes_dir(from_path) + to_path = os.path.join(base_path, node_id) + elif version_spec == 'nightly': + self.unified_disable(node_id, False) + from_path = self.nightly_inactive_nodes.get(node_id) + if from_path is None: + return result.fail(f'Specified inactive node not exists: {node_id}@nightly') + base_path = extract_base_custom_nodes_dir(from_path) + to_path = os.path.join(base_path, node_id) + elif version_spec is not None: + self.unified_disable(node_id, False) + cnr_info = self.cnr_inactive_nodes.get(node_id) + + if cnr_info is None or len(cnr_info) == 0: + return result.fail(f'Specified inactive cnr node not exists: {node_id}') + + if version_spec == "cnr": + version_spec = next(iter(cnr_info)) + + if version_spec not in cnr_info: + return result.fail(f'Specified inactive node not exists: {node_id}@{version_spec}') + + from_path = cnr_info[version_spec] + base_path = extract_base_custom_nodes_dir(from_path) + to_path = os.path.join(base_path, node_id) + + if from_path is None or not os.path.exists(from_path): + return result.fail(f'Specified inactive node path not exists: {from_path}') + + # move from disk + shutil.move(from_path, to_path) + + # update cache + if version_spec == 'unknown': + self.unknown_active_nodes[node_id] = self.unknown_inactive_nodes[node_id][0], to_path + del self.unknown_inactive_nodes[node_id] + return result.with_target(to_path) + elif version_spec == 'nightly': + del self.nightly_inactive_nodes[node_id] + else: + del self.cnr_inactive_nodes[node_id][version_spec] + + self.active_nodes[node_id] = version_spec, to_path + return result.with_target(to_path) + + def unified_disable(self, node_id: str, is_unknown): + result = ManagedResult('disable') + + if 'comfyui-manager' in node_id.lower(): + return result.fail(f"ignored: disabling '{node_id}'") + + if is_unknown: + version_spec = 'unknown' + else: + version_spec = None + + if not self.is_enabled(node_id, version_spec): + if not self.is_disabled(node_id, version_spec): + return ManagedResult('skip').with_msg('Not installed') + else: + return ManagedResult('skip').with_msg('Already disabled') + + if is_unknown: + repo_and_path = self.unknown_active_nodes.get(node_id) + + if repo_and_path is None or not os.path.exists(repo_and_path[1]): + return result.fail(f'Specified active node not exists: {node_id}') + + base_path = extract_base_custom_nodes_dir(repo_and_path[1]) + to_path = os.path.join(base_path, '.disabled', node_id) + + shutil.move(repo_and_path[1], to_path) + result.append((repo_and_path[1], to_path)) + + self.unknown_inactive_nodes[node_id] = repo_and_path[0], to_path + del self.unknown_active_nodes[node_id] + + return result + + ver_and_path = self.active_nodes.get(node_id) + + if ver_and_path is None or not os.path.exists(ver_and_path[1]): + return result.fail(f'Specified active node not exists: {node_id}') + + base_path = extract_base_custom_nodes_dir(ver_and_path[1]) + + # NOTE: A disabled node may have multiple versions, so preserve it using the `@ suffix`. + to_path = os.path.join(base_path, '.disabled', f"{node_id}@{ver_and_path[0].replace('.', '_')}") + shutil.move(ver_and_path[1], to_path) + result.append((ver_and_path[1], to_path)) + + if ver_and_path[0] == 'nightly': + self.nightly_inactive_nodes[node_id] = to_path + else: + self.add_to_cnr_inactive_nodes(node_id, ver_and_path[0], to_path) + + del self.active_nodes[node_id] + + return result + + def unified_uninstall(self, node_id: str, is_unknown: bool): + """ + Remove whole installed custom nodes including inactive nodes + """ + result = ManagedResult('uninstall') + + if 'comfyui-manager' in node_id.lower(): + return result.fail(f"ignored: uninstalling '{node_id}'") + + if is_unknown: + # remove from actives + repo_and_path = self.unknown_active_nodes.get(node_id) + + is_removed = False + + if repo_and_path is not None and os.path.exists(repo_and_path[1]): + rmtree(repo_and_path[1]) + result.append(repo_and_path[1]) + del self.unknown_active_nodes[node_id] + + is_removed = True + + # remove from inactives + repo_and_path = self.unknown_inactive_nodes.get(node_id) + + if repo_and_path is not None and os.path.exists(repo_and_path[1]): + rmtree(repo_and_path[1]) + result.append(repo_and_path[1]) + del self.unknown_inactive_nodes[node_id] + + is_removed = True + + if is_removed: + return result + else: + return ManagedResult('skip') + + # remove from actives + ver_and_path = self.active_nodes.get(node_id) + + if ver_and_path is not None and os.path.exists(ver_and_path[1]): + try_rmtree(node_id, ver_and_path[1]) + result.items.append(ver_and_path) + del self.active_nodes[node_id] + + # remove from nightly inactives + fullpath = self.nightly_inactive_nodes.get(node_id) + if fullpath is not None and os.path.exists(fullpath): + try_rmtree(node_id, fullpath) + result.items.append(('nightly', fullpath)) + del self.nightly_inactive_nodes[node_id] + + # remove from cnr inactives + ver_map = self.cnr_inactive_nodes.get(node_id) + if ver_map is not None: + for key, fullpath in ver_map.items(): + try_rmtree(node_id, fullpath) + result.items.append((key, fullpath)) + del self.cnr_inactive_nodes[node_id] + + if len(result.items) == 0: + return ManagedResult('skip').with_msg('Not installed') + + return result + + def cnr_install(self, node_id: str, version_spec=None, instant_execution=False, no_deps=False, return_postinstall=False): + result = ManagedResult('install-cnr') + + if 'comfyui-manager' in node_id.lower(): + return result.fail(f"ignored: installing '{node_id}'") + + node_info = cnr_utils.install_node(node_id, version_spec) + if node_info is None or not node_info.download_url: + return result.fail(f'not available node: {node_id}@{version_spec}') + + archive_name = f"CNR_temp_{str(uuid.uuid4())}.zip" # should be unpredictable name - security precaution + download_path = os.path.join(get_default_custom_nodes_path(), archive_name) + + # re-download. I cannot trust existing file. + if os.path.exists(download_path): + os.remove(download_path) + + # install_path + install_path = os.path.join(get_default_custom_nodes_path(), node_id) + if os.path.exists(install_path): + return result.fail(f'Install path already exists: {install_path}') + + manager_downloader.download_url(node_info.download_url, get_default_custom_nodes_path(), archive_name) + os.makedirs(install_path, exist_ok=True) + extracted = manager_util.extract_package_as_zip(download_path, install_path) + os.remove(download_path) + result.to_path = install_path + + if extracted is None: + shutil.rmtree(install_path) + return result.fail(f'Empty archive file: {node_id}@{version_spec}') + + # create .tracking file + tracking_info_file = os.path.join(install_path, '.tracking') + with open(tracking_info_file, "w", encoding='utf-8') as file: + file.write('\n'.join(extracted)) + + result.target = version_spec + + def postinstall(): + return self.execute_install_script(node_id, install_path, instant_execution=instant_execution, no_deps=no_deps) + + if return_postinstall: + return result.with_postinstall(postinstall) + else: + if not postinstall(): + return result.fail(f"Failed to execute install script: {node_id}@{version_spec}") + + return result + + def repo_install(self, url: str, repo_path: str, instant_execution=False, no_deps=False, return_postinstall=False): + result = ManagedResult('install-git') + result.append(url) + + if 'comfyui-manager' in url.lower(): + return result.fail(f"ignored: installing '{url}'") + + if not is_valid_url(url): + return result.fail(f"Invalid git url: {url}") + + if url.endswith("/"): + url = url[:-1] + try: + # Clone the repository from the remote URL + clone_url = git_utils.get_url_for_clone(url) + print(f"Download: git clone '{clone_url}'") + + if not instant_execution and platform.system() == 'Windows': + res = manager_funcs.run_script([sys.executable, context.git_script_path, "--clone", get_default_custom_nodes_path(), clone_url, repo_path], cwd=get_default_custom_nodes_path()) + if res != 0: + return result.fail(f"Failed to clone repo: {clone_url}") + else: + repo = git.Repo.clone_from(clone_url, repo_path, recursive=True, progress=GitProgress()) + repo.git.clear_cache() + repo.close() + + def postinstall(): + return self.execute_install_script(url, repo_path, instant_execution=instant_execution, no_deps=no_deps) + + if return_postinstall: + return result.with_postinstall(postinstall) + else: + if not postinstall(): + return result.fail(f"Failed to execute install script: {url}") + + except Exception as e: + traceback.print_exc() + return result.fail(f"Install(git-clone) error[2]: {url} / {e}") + + print("Installation was successful.") + return result + + def repo_update(self, repo_path, instant_execution=False, no_deps=False, return_postinstall=False): + result = ManagedResult('update-git') + + if not os.path.exists(os.path.join(repo_path, '.git')): + return result.fail(f'Path not found: {repo_path}') + + # version check + with git.Repo(repo_path) as repo: + if repo.head.is_detached: + if not switch_to_default_branch(repo): + return result.fail(f"Failed to switch to default branch: {repo_path}") + + current_branch = repo.active_branch + branch_name = current_branch.name + + if current_branch.tracking_branch() is None: + print(f"[ComfyUI-Manager] There is no tracking branch ({current_branch})") + remote_name = get_remote_name(repo) + else: + remote_name = current_branch.tracking_branch().remote_name + + if remote_name is None: + return result.fail(f"Failed to get remote when installing: {repo_path}") + + remote = repo.remote(name=remote_name) + + try: + remote.fetch() + except Exception as e: + if 'detected dubious' in str(e): + print(f"[ComfyUI-Manager] Try fixing 'dubious repository' error on '{repo_path}' repository") + safedir_path = repo_path.replace('\\', '/') + subprocess.run(['git', 'config', '--global', '--add', 'safe.directory', safedir_path]) + try: + remote.fetch() + except Exception: + print("\n[ComfyUI-Manager] Failed to fixing repository setup. Please execute this command on cmd: \n" + "-----------------------------------------------------------------------------------------\n" + f'git config --global --add safe.directory "{safedir_path}"\n' + "-----------------------------------------------------------------------------------------\n") + + commit_hash = repo.head.commit.hexsha + if f'{remote_name}/{branch_name}' in repo.refs: + remote_commit_hash = repo.refs[f'{remote_name}/{branch_name}'].object.hexsha + else: + return result.fail(f"Not updatable branch: {branch_name}") + + if commit_hash != remote_commit_hash: + git_pull(repo_path) + + if len(repo.remotes) > 0: + url = repo.remotes[0].url + else: + url = "unknown repo" + + def postinstall(): + return self.execute_install_script(url, repo_path, instant_execution=instant_execution, no_deps=no_deps) + + if return_postinstall: + return result.with_postinstall(postinstall) + else: + if not postinstall(): + return result.fail(f"Failed to execute install script: {url}") + + return result + else: + return ManagedResult('skip').with_msg('Up to date') + + def unified_update(self, node_id, version_spec=None, instant_execution=False, no_deps=False, return_postinstall=False): + orig_print(f"\x1b[2K\rUpdating: {node_id}", end='') + + if version_spec is None: + version_spec = self.resolve_unspecified_version(node_id, guess_mode='active') + + if version_spec is None: + return ManagedResult('update').fail(f'Update not available: {node_id}@{version_spec}').with_ver(version_spec) + + if version_spec == 'nightly': + return self.repo_update(self.active_nodes[node_id][1], instant_execution=instant_execution, no_deps=no_deps, return_postinstall=return_postinstall).with_target('nightly').with_ver('nightly') + elif version_spec == 'unknown': + return self.repo_update(self.unknown_active_nodes[node_id][1], instant_execution=instant_execution, no_deps=no_deps, return_postinstall=return_postinstall).with_target('unknown').with_ver('unknown') + else: + return self.cnr_switch_version(node_id, instant_execution=instant_execution, no_deps=no_deps, return_postinstall=return_postinstall).with_ver('cnr') + + async def install_by_id(self, node_id: str, version_spec=None, channel=None, mode=None, instant_execution=False, no_deps=False, return_postinstall=False): + """ + priority if version_spec == None + 1. CNR latest + 2. unknown + + remark: latest version_spec is not allowed. Must be resolved before call. + """ + + if 'comfyui-manager' in node_id.lower(): + return ManagedResult('skip').fail(f"ignored: installing '{node_id}'") + + repo_url = None + if version_spec is None: + if self.is_enabled(node_id): + return ManagedResult('skip') + elif self.is_disabled(node_id): + return self.unified_enable(node_id) + else: + version_spec = self.resolve_unspecified_version(node_id) + + if version_spec == 'unknown' or version_spec == 'nightly': + try: + custom_nodes = await self.get_custom_nodes(channel, mode) + except InvalidChannel as e: + return ManagedResult('fail').fail(f'Invalid channel is used: {e.channel}') + + the_node = custom_nodes.get(node_id) + if the_node is not None: + if version_spec == 'unknown': + repo_url = the_node['files'][0] + else: # nightly + repo_url = the_node['repository'] + else: + result = ManagedResult('install') + return result.fail(f"Node '{node_id}@{version_spec}' not found in [{channel}, {mode}]") + + if self.is_enabled(node_id, version_spec): + return ManagedResult('skip').with_target(f"{node_id}@{version_spec}") + + elif self.is_disabled(node_id, version_spec): + return self.unified_enable(node_id, version_spec) + + elif version_spec == 'unknown' or version_spec == 'nightly': + to_path = os.path.abspath(os.path.join(get_default_custom_nodes_path(), node_id)) + + if version_spec == 'nightly': + # disable cnr nodes + if self.is_enabled(node_id, 'cnr'): + self.unified_disable(node_id, False) + + # use `repo name` as a dir name instead of `cnr id` if system added nodepack (i.e. publisher is null) + cnr = self.cnr_map.get(node_id) + + if cnr is not None and cnr.get('publisher') is None: + repo_name = os.path.basename(git_utils.normalize_url(repo_url)) + to_path = os.path.abspath(os.path.join(get_default_custom_nodes_path(), repo_name)) + + res = self.repo_install(repo_url, to_path, instant_execution=instant_execution, no_deps=no_deps, return_postinstall=return_postinstall) + if res.result: + if version_spec == 'unknown': + self.unknown_active_nodes[node_id] = repo_url, to_path + elif version_spec == 'nightly': + cnr_utils.generate_cnr_id(to_path, node_id) + self.active_nodes[node_id] = 'nightly', to_path + else: + return res + + return res.with_target(version_spec) + + if self.is_enabled(node_id, 'nightly'): + # disable nightly nodes + self.unified_disable(node_id, False) # NOTE: don't return from here + + if self.is_disabled(node_id, version_spec): + # enable and return if specified version is disabled + return self.unified_enable(node_id, version_spec) + + if self.is_disabled(node_id, "cnr"): + # enable and switch version if cnr is disabled (not specified version) + self.unified_enable(node_id, "cnr") + return self.cnr_switch_version(node_id, version_spec, no_deps=no_deps, return_postinstall=return_postinstall) + + if self.is_enabled(node_id, "cnr"): + return self.cnr_switch_version(node_id, version_spec, no_deps=no_deps, return_postinstall=return_postinstall) + + res = self.cnr_install(node_id, version_spec, instant_execution=instant_execution, no_deps=no_deps, return_postinstall=return_postinstall) + if res.result: + self.active_nodes[node_id] = version_spec, res.to_path + + return res + + +unified_manager = UnifiedManager() + + +def identify_node_pack_from_path(fullpath): + module_name = os.path.basename(fullpath) + if module_name.endswith('.git'): + module_name = module_name[:-4] + + repo_url = git_utils.git_url(fullpath) + if repo_url is None: + # cnr + cnr = cnr_utils.read_cnr_info(fullpath) + if cnr is not None: + return module_name, cnr['version'], cnr['id'], None + + return None + else: + # nightly or unknown + cnr_id = cnr_utils.read_cnr_id(fullpath) + commit_hash = git_utils.get_commit_hash(fullpath) + + github_id = git_utils.normalize_to_github_id(repo_url) + if github_id is None: + try: + github_id = os.path.basename(repo_url) + except Exception: + logging.warning(f"[ComfyUI-Manager] unexpected repo url: {repo_url}") + github_id = module_name + + if cnr_id is not None: + return module_name, commit_hash, cnr_id, github_id + else: + return module_name, commit_hash, '', github_id + + +def get_installed_node_packs(): + res = {} + + for x in get_custom_nodes_paths(): + for y in os.listdir(x): + if y == '__pycache__' or y == '.disabled': + continue + + fullpath = os.path.join(x, y) + info = identify_node_pack_from_path(fullpath) + if info is None: + continue + + is_disabled = not y.endswith('.disabled') + + res[info[0]] = { 'ver': info[1], 'cnr_id': info[2], 'aux_id': info[3], 'enabled': is_disabled } + + disabled_dirs = os.path.join(x, '.disabled') + if os.path.exists(disabled_dirs): + for y in os.listdir(disabled_dirs): + if y == '__pycache__': + continue + + fullpath = os.path.join(disabled_dirs, y) + info = identify_node_pack_from_path(fullpath) + if info is None: + continue + + res[info[0]] = { 'ver': info[1], 'cnr_id': info[2], 'aux_id': info[3], 'enabled': False } + + return res + + +def refresh_channel_dict(): + if channel_dict is None: + get_channel_dict() + + +def get_channel_dict(): + global channel_dict + global valid_channels + + if channel_dict is None: + channel_dict = {} + + if not os.path.exists(context.manager_channel_list_path): + shutil.copy(context.channel_list_template_path, context.manager_channel_list_path) + + with open(context.manager_channel_list_path, 'r') as file: + channels = file.read() + for x in channels.split('\n'): + channel_info = x.split("::") + if len(channel_info) == 2: + channel_dict[channel_info[0]] = channel_info[1] + valid_channels.add(channel_info[1]) + + return channel_dict + + +def get_channel_list(): + global channel_list + + if channel_list is None: + channel_list = [] + for k, v in get_channel_dict().items(): + channel_list.append(f"{k}::{v}") + + return channel_list + + +class ManagerFuncs: + def __init__(self): + pass + + def get_current_preview_method(self): + return "none" + + def run_script(self, cmd, cwd='.'): + if len(cmd) > 0 and cmd[0].startswith("#"): + print(f"[ComfyUI-Manager] Unexpected behavior: `{cmd}`") + return 0 + + subprocess.check_call(cmd, cwd=cwd, env=get_script_env()) + + return 0 + + +manager_funcs = ManagerFuncs() + + +def write_config(): + config = configparser.ConfigParser(strict=False) + + config['default'] = { + 'preview_method': manager_funcs.get_current_preview_method(), + 'git_exe': get_config()['git_exe'], + 'use_uv': get_config()['use_uv'], + 'channel_url': get_config()['channel_url'], + 'share_option': get_config()['share_option'], + 'bypass_ssl': get_config()['bypass_ssl'], + "file_logging": get_config()['file_logging'], + 'component_policy': get_config()['component_policy'], + 'update_policy': get_config()['update_policy'], + 'windows_selector_event_loop_policy': get_config()['windows_selector_event_loop_policy'], + 'model_download_by_agent': get_config()['model_download_by_agent'], + 'downgrade_blacklist': get_config()['downgrade_blacklist'], + 'security_level': get_config()['security_level'], + 'always_lazy_install': get_config()['always_lazy_install'], + 'network_mode': get_config()['network_mode'], + 'db_mode': get_config()['db_mode'], + } + + directory = os.path.dirname(context.manager_config_path) + if not os.path.exists(directory): + os.makedirs(directory) + + with open(context.manager_config_path, 'w') as configfile: + config.write(configfile) + + +def read_config(): + try: + config = configparser.ConfigParser(strict=False) + config.read(context.manager_config_path) + default_conf = config['default'] + + def get_bool(key, default_value): + return default_conf[key].lower() == 'true' if key in default_conf else False + + manager_util.use_uv = default_conf['use_uv'].lower() == 'true' if 'use_uv' in default_conf else False + manager_util.bypass_ssl = get_bool('bypass_ssl', False) + + return { + 'http_channel_enabled': get_bool('http_channel_enabled', False), + 'preview_method': default_conf.get('preview_method', manager_funcs.get_current_preview_method()).lower(), + 'git_exe': default_conf.get('git_exe', ''), + 'use_uv': get_bool('use_uv', True), + 'channel_url': default_conf.get('channel_url', DEFAULT_CHANNEL), + 'default_cache_as_channel_url': get_bool('default_cache_as_channel_url', False), + 'share_option': default_conf.get('share_option', 'all').lower(), + 'bypass_ssl': get_bool('bypass_ssl', False), + 'file_logging': get_bool('file_logging', True), + 'component_policy': default_conf.get('component_policy', 'workflow').lower(), + 'update_policy': default_conf.get('update_policy', 'stable-comfyui').lower(), + 'windows_selector_event_loop_policy': get_bool('windows_selector_event_loop_policy', False), + 'model_download_by_agent': get_bool('model_download_by_agent', False), + 'downgrade_blacklist': default_conf.get('downgrade_blacklist', '').lower(), + 'always_lazy_install': get_bool('always_lazy_install', False), + 'network_mode': default_conf.get('network_mode', NetworkMode.PUBLIC.value).lower(), + 'security_level': default_conf.get('security_level', SecurityLevel.NORMAL.value).lower(), + 'db_mode': default_conf.get('db_mode', DBMode.CACHE.value).lower(), + } + + except Exception: + manager_util.use_uv = False + manager_util.bypass_ssl = False + + return { + 'http_channel_enabled': False, + 'preview_method': manager_funcs.get_current_preview_method(), + 'git_exe': '', + 'use_uv': True, + 'channel_url': DEFAULT_CHANNEL, + 'default_cache_as_channel_url': False, + 'share_option': 'all', + 'bypass_ssl': manager_util.bypass_ssl, + 'file_logging': True, + 'component_policy': 'workflow', + 'update_policy': 'stable-comfyui', + 'windows_selector_event_loop_policy': False, + 'model_download_by_agent': False, + 'downgrade_blacklist': '', + 'always_lazy_install': False, + 'network_mode': NetworkMode.PUBLIC.value, + 'security_level': SecurityLevel.NORMAL.value, + 'db_mode': DBMode.CACHE.value, + } + + +def get_config(): + global cached_config + + if cached_config is None: + cached_config = read_config() + if cached_config['http_channel_enabled']: + print("[ComfyUI-Manager] Warning: http channel enabled, make sure server in secure env") + + return cached_config + + +def get_remote_name(repo): + available_remotes = [remote.name for remote in repo.remotes] + if 'origin' in available_remotes: + return 'origin' + elif 'upstream' in available_remotes: + return 'upstream' + elif len(available_remotes) > 0: + return available_remotes[0] + + if not available_remotes: + print(f"[ComfyUI-Manager] No remotes are configured for this repository: {repo.working_dir}") + else: + print(f"[ComfyUI-Manager] Available remotes in '{repo.working_dir}': ") + for remote in available_remotes: + print(f"- {remote}") + + return None + + +def switch_to_default_branch(repo): + remote_name = get_remote_name(repo) + + try: + if remote_name is None: + return False + + default_branch = repo.git.symbolic_ref(f'refs/remotes/{remote_name}/HEAD').replace(f'refs/remotes/{remote_name}/', '') + repo.git.checkout(default_branch) + return True + except Exception: + # try checkout master + # try checkout main if failed + try: + repo.git.checkout(repo.heads.master) + return True + except Exception: + try: + if remote_name is not None: + repo.git.checkout('-b', 'master', f'{remote_name}/master') + return True + except Exception: + try: + repo.git.checkout(repo.heads.main) + return True + except Exception: + try: + if remote_name is not None: + repo.git.checkout('-b', 'main', f'{remote_name}/main') + return True + except Exception: + pass + + print("[ComfyUI Manager] Failed to switch to the default branch") + return False + + +def reserve_script(repo_path, install_cmds): + if not os.path.exists(context.manager_startup_script_path): + os.makedirs(context.manager_startup_script_path) + + script_path = os.path.join(context.manager_startup_script_path, "install-scripts.txt") + with open(script_path, "a") as file: + obj = [repo_path] + install_cmds + file.write(f"{obj}\n") + + +def try_rmtree(title, fullpath): + try: + shutil.rmtree(fullpath) + except Exception as e: + logging.warning(f"[ComfyUI-Manager] An error occurred while deleting '{fullpath}', so it has been scheduled for deletion upon restart.\nEXCEPTION: {e}") + reserve_script(title, ["#LAZY-DELETE-NODEPACK", fullpath]) + + +def try_install_script(url, repo_path, install_cmd, instant_execution=False): + if not instant_execution and ( + (len(install_cmd) > 0 and install_cmd[0].startswith('#')) or platform.system() == "Windows" or get_config()['always_lazy_install'] + ): + reserve_script(repo_path, install_cmd) + return True + else: + if len(install_cmd) == 5 and install_cmd[2:4] == ['pip', 'install']: + if is_blacklisted(install_cmd[4]): + print(f"[ComfyUI-Manager] skip black listed pip installation: '{install_cmd[4]}'") + return True + elif len(install_cmd) == 6 and install_cmd[3:5] == ['pip', 'install']: # uv mode + if is_blacklisted(install_cmd[5]): + print(f"[ComfyUI-Manager] skip black listed pip installation: '{install_cmd[5]}'") + return True + + print(f"\n## ComfyUI-Manager: EXECUTE => {install_cmd}") + code = manager_funcs.run_script(install_cmd, cwd=repo_path) + + if platform.system() != "Windows": + try: + if not os.environ.get('__COMFYUI_DESKTOP_VERSION__') and comfy_ui_commit_datetime.date() < comfy_ui_required_commit_datetime.date(): + print("\n\n###################################################################") + print(f"[WARN] ComfyUI-Manager: Your ComfyUI version ({comfy_ui_revision})[{comfy_ui_commit_datetime.date()}] is too old. Please update to the latest version.") + print("[WARN] The extension installation feature may not work properly in the current installed ComfyUI version on Windows environment.") + print("###################################################################\n\n") + except Exception: + pass + + if code != 0: + if url is None: + url = os.path.dirname(repo_path) + print(f"install script failed: {url}") + return False + + return True + + +# use subprocess to avoid file system lock by git (Windows) +def __win_check_git_update(path, do_fetch=False, do_update=False): + if do_fetch: + command = [sys.executable, context.git_script_path, "--fetch", path] + elif do_update: + command = [sys.executable, context.git_script_path, "--pull", path] + else: + command = [sys.executable, context.git_script_path, "--check", path] + + new_env = get_script_env() + process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=get_default_custom_nodes_path(), env=new_env) + output, _ = process.communicate() + output = output.decode('utf-8').strip() + + if 'detected dubious' in output: + # fix and try again + safedir_path = path.replace('\\', '/') + try: + print(f"[ComfyUI-Manager] Try fixing 'dubious repository' error on '{safedir_path}' repo") + process = subprocess.Popen(['git', 'config', '--global', '--add', 'safe.directory', safedir_path], env=new_env, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + output, _ = process.communicate() + + process = subprocess.Popen(command, env=new_env, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + output, _ = process.communicate() + output = output.decode('utf-8').strip() + except Exception: + print('[ComfyUI-Manager] failed to fixing') + + if 'detected dubious' in output: + print(f'\n[ComfyUI-Manager] Failed to fixing repository setup. Please execute this command on cmd: \n' + f'-----------------------------------------------------------------------------------------\n' + f'git config --global --add safe.directory "{safedir_path}"\n' + f'-----------------------------------------------------------------------------------------\n') + + if do_update: + if "CUSTOM NODE PULL: Success" in output: + process.wait() + print(f"\x1b[2K\rUpdated: {path}") + return True, True # updated + elif "CUSTOM NODE PULL: None" in output: + process.wait() + return False, True # there is no update + else: + print(f"\x1b[2K\rUpdate error: {path}") + process.wait() + return False, False # update failed + else: + if "CUSTOM NODE CHECK: True" in output: + process.wait() + return True, True + elif "CUSTOM NODE CHECK: False" in output: + process.wait() + return False, True + else: + print(f"\x1b[2K\rFetch error: {path}") + print(f"\n{output}\n") + process.wait() + return False, True + + +def __win_check_git_pull(path): + command = [sys.executable, context.git_script_path, "--pull", path] + process = subprocess.Popen(command, env=get_script_env(), cwd=get_default_custom_nodes_path()) + process.wait() + + +def execute_install_script(url, repo_path, lazy_mode=False, instant_execution=False, no_deps=False): + # import ipdb; ipdb.set_trace() + install_script_path = os.path.join(repo_path, "install.py") + requirements_path = os.path.join(repo_path, "requirements.txt") + + if lazy_mode: + install_cmd = ["#LAZY-INSTALL-SCRIPT", sys.executable] + try_install_script(url, repo_path, install_cmd) + else: + if os.path.exists(requirements_path) and not no_deps: + print("Install: pip packages") + pip_fixer = manager_util.PIPFixer(manager_util.get_installed_packages(), context.comfy_path, context.manager_files_path) + with open(requirements_path, "r") as requirements_file: + for line in requirements_file: + #handle comments + if '#' in line: + if line.strip()[0] == '#': + print("Line is comment...skipping") + continue + else: + line = line.split('#')[0].strip() + + package_name = remap_pip_package(line.strip()) + + if package_name and not package_name.startswith('#'): + if '--index-url' in package_name: + s = package_name.split('--index-url') + install_cmd = manager_util.make_pip_cmd(["install", s[0].strip(), '--index-url', s[1].strip()]) + else: + install_cmd = manager_util.make_pip_cmd(["install", package_name]) + + if package_name.strip() != "" and not package_name.startswith('#'): + try_install_script(url, repo_path, install_cmd, instant_execution=instant_execution) + pip_fixer.fix_broken() + + if os.path.exists(install_script_path): + print("Install: install script") + install_cmd = [sys.executable, "install.py"] + try_install_script(url, repo_path, install_cmd, instant_execution=instant_execution) + + return True + + +def git_repo_update_check_with(path, do_fetch=False, do_update=False, no_deps=False): + """ + + perform update check for git custom node + and fetch or update if flag is on + + :param path: path to git custom node + :param do_fetch: do fetch during check + :param do_update: do update during check + :param no_deps: don't install dependencies + :return: update state * success + """ + if do_fetch: + orig_print(f"\x1b[2K\rFetching: {path}", end='') + elif do_update: + orig_print(f"\x1b[2K\rUpdating: {path}", end='') + + # Check if the path is a git repository + if not os.path.exists(os.path.join(path, '.git')): + raise ValueError(f'[ComfyUI-Manager] Not a valid git repository: {path}') + + if platform.system() == "Windows": + updated, success = __win_check_git_update(path, do_fetch, do_update) + if updated and success: + execute_install_script(None, path, lazy_mode=True, no_deps=no_deps) + return updated, success + else: + # Fetch the latest commits from the remote repository + repo = git.Repo(path) + + remote_name = get_remote_name(repo) + + if remote_name is None: + raise ValueError(f"No remotes are configured for this repository: {path}") + + remote = repo.remote(name=remote_name) + + if not do_update and repo.head.is_detached: + if do_fetch: + remote.fetch() + + return True, True # detached branch is treated as updatable + + if repo.head.is_detached: + if not switch_to_default_branch(repo): + raise ValueError(f"Failed to switch detached branch to default branch: {path}") + + current_branch = repo.active_branch + branch_name = current_branch.name + + # Get the current commit hash + commit_hash = repo.head.commit.hexsha + + if do_fetch or do_update: + remote.fetch() + + if do_update: + if repo.is_dirty(): + print(f"\nSTASH: '{path}' is dirty.") + repo.git.stash() + + if f'{remote_name}/{branch_name}' not in repo.refs: + if not switch_to_default_branch(repo): + raise ValueError(f"Failed to switch to default branch while updating: {path}") + + current_branch = repo.active_branch + branch_name = current_branch.name + + if f'{remote_name}/{branch_name}' in repo.refs: + remote_commit_hash = repo.refs[f'{remote_name}/{branch_name}'].object.hexsha + else: + return False, False + + if commit_hash == remote_commit_hash: + repo.close() + return False, True + + try: + remote.pull() + repo.git.submodule('update', '--init', '--recursive') + new_commit_hash = repo.head.commit.hexsha + + if commit_hash != new_commit_hash: + execute_install_script(None, path, no_deps=no_deps) + print(f"\x1b[2K\rUpdated: {path}") + return True, True + else: + return False, False + + except Exception as e: + print(f"\nUpdating failed: {path}\n{e}", file=sys.stderr) + return False, False + + if repo.head.is_detached: + repo.close() + return True, True + + # Get commit hash of the remote branch + current_branch = repo.active_branch + branch_name = current_branch.name + + if f'{remote_name}/{branch_name}' in repo.refs: + remote_commit_hash = repo.refs[f'{remote_name}/{branch_name}'].object.hexsha + else: + return True, True # Assuming there's an update if it's not the default branch. + + # Compare the commit hashes to determine if the local repository is behind the remote repository + if commit_hash != remote_commit_hash: + # Get the commit dates + commit_date = repo.head.commit.committed_datetime + remote_commit_date = repo.refs[f'{remote_name}/{branch_name}'].object.committed_datetime + + # Compare the commit dates to determine if the local repository is behind the remote repository + if commit_date < remote_commit_date: + repo.close() + return True, True + + repo.close() + + return False, True + + +class GitProgress(RemoteProgress): + def __init__(self): + super().__init__() + self.pbar = tqdm() + + def update(self, op_code, cur_count, max_count=None, message=''): + self.pbar.total = max_count + self.pbar.n = cur_count + self.pbar.pos = 0 + self.pbar.refresh() + + +def is_valid_url(url): + try: + # Check for HTTP/HTTPS URL format + result = urlparse(url) + if all([result.scheme, result.netloc]): + return True + finally: + # Check for SSH git URL format + pattern = re.compile(r"^(.+@|ssh://).+:.+$") + if pattern.match(url): + return True + return False + + +async def gitclone_install(url, instant_execution=False, msg_prefix='', no_deps=False): + await unified_manager.reload('cache') + await unified_manager.get_custom_nodes('default', 'cache') + + print(f"{msg_prefix}Install: {url}") + + result = ManagedResult('install-git') + + if not is_valid_url(url): + return result.fail(f"Invalid git url: '{url}'") + + if url.endswith("/"): + url = url[:-1] + try: + cnr = unified_manager.get_cnr_by_repo(url) + if cnr: + cnr_id = cnr['id'] + return await unified_manager.install_by_id(cnr_id, version_spec='nightly', channel='default', mode='cache') + else: + repo_name = os.path.splitext(os.path.basename(url))[0] + + # NOTE: Keep original name as possible if unknown node + # node_dir = f"{repo_name}@unknown" + node_dir = repo_name + + repo_path = os.path.join(get_default_custom_nodes_path(), node_dir) + + if os.path.exists(repo_path): + return result.fail(f"Already exists: '{repo_path}'") + + for custom_nodes_dir in get_custom_nodes_paths(): + disabled_repo_path1 = os.path.join(custom_nodes_dir, '.disabled', node_dir) + disabled_repo_path2 = os.path.join(custom_nodes_dir, repo_name+'.disabled') # old style + + if os.path.exists(disabled_repo_path1): + return result.fail(f"Already exists (disabled): '{disabled_repo_path1}'") + + if os.path.exists(disabled_repo_path2): + return result.fail(f"Already exists (disabled): '{disabled_repo_path2}'") + + print(f"CLONE into '{repo_path}'") + + # Clone the repository from the remote URL + clone_url = git_utils.get_url_for_clone(url) + + if not instant_execution and platform.system() == 'Windows': + res = manager_funcs.run_script([sys.executable, context.git_script_path, "--clone", get_default_custom_nodes_path(), clone_url, repo_path], cwd=get_default_custom_nodes_path()) + if res != 0: + return result.fail(f"Failed to clone '{clone_url}' into '{repo_path}'") + else: + repo = git.Repo.clone_from(clone_url, repo_path, recursive=True, progress=GitProgress()) + repo.git.clear_cache() + repo.close() + + execute_install_script(url, repo_path, instant_execution=instant_execution, no_deps=no_deps) + print("Installation was successful.") + return result.with_target(repo_path) + + except Exception as e: + traceback.print_exc() + print(f"Install(git-clone) error[1]: {url} / {e}", file=sys.stderr) + return result.fail(f"Install(git-clone)[1] error: {url} / {e}") + + +def git_pull(path): + # Check if the path is a git repository + if not os.path.exists(os.path.join(path, '.git')): + raise ValueError('Not a git repository') + + # Pull the latest changes from the remote repository + if platform.system() == "Windows": + return __win_check_git_pull(path) + else: + repo = git.Repo(path) + + if repo.is_dirty(): + print(f"STASH: '{path}' is dirty.") + repo.git.stash() + + if repo.head.is_detached: + if not switch_to_default_branch(repo): + raise ValueError(f"Failed to switch to default branch while pulling: {path}") + + current_branch = repo.active_branch + remote_name = current_branch.tracking_branch().remote_name + remote = repo.remote(name=remote_name) + + remote.pull() + repo.git.submodule('update', '--init', '--recursive') + + repo.close() + + return True + + +async def get_data_by_mode(mode, filename, channel_url=None): + if channel_url in get_channel_dict(): + channel_url = get_channel_dict()[channel_url] + + try: + local_uri = os.path.join(manager_util.comfyui_manager_path, filename) + + if mode == "local": + json_obj = await manager_util.get_data(local_uri) + else: + if channel_url is None: + uri = get_config()['channel_url'] + '/' + filename + else: + uri = channel_url + '/' + filename + + cache_uri = str(manager_util.simple_hash(uri))+'_'+filename + cache_uri = os.path.join(manager_util.cache_dir, cache_uri) + + if get_config()['network_mode'] == 'offline' or manager_util.is_manager_pip_package(): + # offline network mode + if os.path.exists(cache_uri): + json_obj = await manager_util.get_data(cache_uri) + else: + local_uri = os.path.join(manager_util.comfyui_manager_path, filename) + if os.path.exists(local_uri): + json_obj = await manager_util.get_data(local_uri) + else: + json_obj = {} # fallback + else: + # public network mode + if mode == "cache" and manager_util.is_file_created_within_one_day(cache_uri): + json_obj = await manager_util.get_data(cache_uri) + else: + json_obj = await manager_util.get_data(uri) + with manager_util.cache_lock: + with open(cache_uri, "w", encoding='utf-8') as file: + json.dump(json_obj, file, indent=4, sort_keys=True) + except Exception as e: + print(f"[ComfyUI-Manager] Due to a network error, switching to local mode.\n=> {filename} @ {channel_url}/{mode}\n=> {e}") + uri = os.path.join(manager_util.comfyui_manager_path, filename) + json_obj = await manager_util.get_data(uri) + + return json_obj + + +def gitclone_fix(files, instant_execution=False, no_deps=False): + print(f"Try fixing: {files}") + for url in files: + if not is_valid_url(url): + print(f"Invalid git url: '{url}'") + return False + + if url.endswith("/"): + url = url[:-1] + try: + repo_name = os.path.splitext(os.path.basename(url))[0] + repo_path = os.path.join(get_default_custom_nodes_path(), repo_name) + + if os.path.exists(repo_path+'.disabled'): + repo_path = repo_path+'.disabled' + + if not execute_install_script(url, repo_path, instant_execution=instant_execution, no_deps=no_deps): + return False + + except Exception as e: + print(f"Fix(git-clone) error: {url} / {e}", file=sys.stderr) + return False + + print(f"Attempt to fixing '{files}' is done.") + return True + + +def pip_install(packages): + install_cmd = ['#FORCE'] + manager_util.make_pip_cmd(["install", '-U']) + packages + try_install_script('pip install via manager', '..', install_cmd) + + +def rmtree(path): + retry_count = 3 + + while True: + try: + retry_count -= 1 + + if platform.system() == "Windows": + manager_funcs.run_script(['attrib', '-R', path + '\\*', '/S']) + shutil.rmtree(path) + + return True + + except Exception as ex: + print(f"ex: {ex}") + time.sleep(3) + + if retry_count < 0: + raise ex + + print(f"Uninstall retry({retry_count})") + + +def gitclone_uninstall(files): + import os + + print(f"Uninstall: {files}") + for url in files: + if url.endswith("/"): + url = url[:-1] + try: + for custom_nodes_dir in get_custom_nodes_paths(): + dir_name:str = os.path.splitext(os.path.basename(url))[0].replace(".git", "") + dir_path = os.path.join(custom_nodes_dir, dir_name) + + # safety check + if dir_path == '/' or dir_path[1:] == ":/" or dir_path == '': + print(f"Uninstall(git-clone) error: invalid path '{dir_path}' for '{url}'") + return False + + install_script_path = os.path.join(dir_path, "uninstall.py") + disable_script_path = os.path.join(dir_path, "disable.py") + if os.path.exists(install_script_path): + uninstall_cmd = [sys.executable, "uninstall.py"] + code = manager_funcs.run_script(uninstall_cmd, cwd=dir_path) + + if code != 0: + print(f"An error occurred during the execution of the uninstall.py script. Only the '{dir_path}' will be deleted.") + elif os.path.exists(disable_script_path): + disable_script = [sys.executable, "disable.py"] + code = manager_funcs.run_script(disable_script, cwd=dir_path) + if code != 0: + print(f"An error occurred during the execution of the disable.py script. Only the '{dir_path}' will be deleted.") + + if os.path.exists(dir_path): + rmtree(dir_path) + elif os.path.exists(dir_path + ".disabled"): + rmtree(dir_path + ".disabled") + except Exception as e: + print(f"Uninstall(git-clone) error: {url} / {e}", file=sys.stderr) + return False + + print("Uninstallation was successful.") + return True + + +def gitclone_set_active(files, is_disable): + import os + + if is_disable: + action_name = "Disable" + else: + action_name = "Enable" + + print(f"{action_name}: {files}") + for url in files: + if url.endswith("/"): + url = url[:-1] + try: + for custom_nodes_dir in get_custom_nodes_paths(): + dir_name:str = os.path.splitext(os.path.basename(url))[0].replace(".git", "") + dir_path = os.path.join(custom_nodes_dir, dir_name) + + # safety check + if dir_path == '/' or dir_path[1:] == ":/" or dir_path == '': + print(f"{action_name}(git-clone) error: invalid path '{dir_path}' for '{url}'") + return False + + if is_disable: + current_path = dir_path + base_path = extract_base_custom_nodes_dir(current_path) + new_path = os.path.join(base_path, ".disabled", dir_name) + + if not os.path.exists(current_path): + continue + else: + current_path1 = os.path.join(get_default_custom_nodes_path(), ".disabled", dir_name) + current_path2 = dir_path + ".disabled" + + if os.path.exists(current_path1): + current_path = current_path1 + elif os.path.exists(current_path2): + current_path = current_path2 + else: + continue + + base_path = extract_base_custom_nodes_dir(current_path) + new_path = os.path.join(base_path, dir_name) + + shutil.move(current_path, new_path) + + if is_disable: + if os.path.exists(os.path.join(new_path, "disable.py")): + disable_script = [sys.executable, "disable.py"] + try_install_script(url, new_path, disable_script) + else: + if os.path.exists(os.path.join(new_path, "enable.py")): + enable_script = [sys.executable, "enable.py"] + try_install_script(url, new_path, enable_script) + + break # for safety + + except Exception as e: + print(f"{action_name}(git-clone) error: {url} / {e}", file=sys.stderr) + return False + + print(f"{action_name} was successful.") + return True + + +def gitclone_update(files, instant_execution=False, skip_script=False, msg_prefix="", no_deps=False): + import os + + print(f"{msg_prefix}Update: {files}") + for url in files: + if url.endswith("/"): + url = url[:-1] + try: + for custom_nodes_dir in get_default_custom_nodes_path(): + repo_name = os.path.splitext(os.path.basename(url))[0].replace(".git", "") + repo_path = os.path.join(custom_nodes_dir, repo_name) + + if os.path.exists(repo_path+'.disabled'): + repo_path = repo_path+'.disabled' + + elif os.path.exists(os.path.join(get_default_custom_nodes_path(), "disabled", repo_name)): + repo_path = os.path.join(get_default_custom_nodes_path(), "disabled", repo_name) + + if not os.path.exists(repo_path): + continue + + git_pull(repo_path) + + if not skip_script: + if instant_execution: + if not execute_install_script(url, repo_path, lazy_mode=False, instant_execution=True, no_deps=no_deps): + return False + else: + if not execute_install_script(url, repo_path, lazy_mode=True, no_deps=no_deps): + return False + + break # for safety + + except Exception as e: + print(f"Update(git-clone) error: {url} / {e}", file=sys.stderr) + return False + + if not skip_script: + print("Update was successful.") + return True + + +def update_to_stable_comfyui(repo_path): + try: + repo = git.Repo(repo_path) + try: + repo.git.checkout(repo.heads.master) + except Exception: + logging.error(f"[ComfyUI-Manager] Failed to checkout 'master' branch.\nrepo_path={repo_path}\nAvailable branches:") + for branch in repo.branches: + logging.error('\t'+branch.name) + return "fail", None + + versions, current_tag, _ = get_comfyui_versions(repo) + + if len(versions) == 0 or (len(versions) == 1 and versions[0] == 'nightly'): + logging.info("[ComfyUI-Manager] Unable to update to the stable ComfyUI version.") + return "fail", None + + if versions[0] == 'nightly': + latest_tag = versions[1] + else: + latest_tag = versions[0] + + if current_tag == latest_tag: + return "skip", None + else: + logging.info(f"[ComfyUI-Manager] Updating ComfyUI: {current_tag} -> {latest_tag}") + repo.git.checkout(latest_tag) + return 'updated', latest_tag + except Exception: + traceback.print_exc() + return "fail", None + + +def update_path(repo_path, instant_execution=False, no_deps=False): + if not os.path.exists(os.path.join(repo_path, '.git')): + return "fail" + + # version check + repo = git.Repo(repo_path) + + is_switched = False + if repo.head.is_detached: + if not switch_to_default_branch(repo): + return "fail" + else: + is_switched = True + + current_branch = repo.active_branch + branch_name = current_branch.name + + if current_branch.tracking_branch() is None: + print(f"[ComfyUI-Manager] There is no tracking branch ({current_branch})") + remote_name = get_remote_name(repo) + else: + remote_name = current_branch.tracking_branch().remote_name + remote = repo.remote(name=remote_name) + + try: + remote.fetch() + except Exception as e: + if 'detected dubious' in str(e): + print(f"[ComfyUI-Manager] Try fixing 'dubious repository' error on '{repo_path}' repository") + safedir_path = repo_path.replace('\\', '/') + subprocess.run(['git', 'config', '--global', '--add', 'safe.directory', safedir_path]) + try: + remote.fetch() + except Exception: + print(f"\n[ComfyUI-Manager] Failed to fixing repository setup. Please execute this command on cmd: \n" + f"-----------------------------------------------------------------------------------------\n" + f'git config --global --add safe.directory "{safedir_path}"\n' + f"-----------------------------------------------------------------------------------------\n") + return "fail" + + commit_hash = repo.head.commit.hexsha + + if f'{remote_name}/{branch_name}' in repo.refs: + remote_commit_hash = repo.refs[f'{remote_name}/{branch_name}'].object.hexsha + else: + return "fail" + + if commit_hash != remote_commit_hash: + git_pull(repo_path) + execute_install_script("ComfyUI", repo_path, instant_execution=instant_execution, no_deps=no_deps) + return "updated" + elif is_switched: + return "updated" + else: + return "skipped" + + +def lookup_customnode_by_url(data, target): + for x in data['custom_nodes']: + if target in x['files']: + for custom_nodes_dir in get_custom_nodes_paths(): + dir_name = os.path.splitext(os.path.basename(target))[0].replace(".git", "") + dir_path = os.path.join(custom_nodes_dir, dir_name) + if os.path.exists(dir_path): + x['installed'] = 'True' + else: + disabled_path1 = os.path.join(custom_nodes_dir, '.disabled', dir_name) + disabled_path2 = dir_path + ".disabled" + + if os.path.exists(disabled_path1) or os.path.exists(disabled_path2): + x['installed'] = 'Disabled' + else: + continue + + return x + + return None + + +def lookup_installed_custom_nodes_legacy(repo_name): + base_paths = get_custom_nodes_paths() + + for base_path in base_paths: + repo_path = os.path.join(base_path, repo_name) + if os.path.exists(repo_path): + return True, repo_path + elif os.path.exists(repo_path + '.disabled'): + return False, repo_path + + return None + + +def simple_check_custom_node(url): + dir_name = os.path.splitext(os.path.basename(url))[0].replace(".git", "") + dir_path = os.path.join(get_default_custom_nodes_path(), dir_name) + if os.path.exists(dir_path): + return 'installed' + elif os.path.exists(dir_path+'.disabled'): + return 'disabled' + + return 'not-installed' + + +def check_state_of_git_node_pack_single(item, do_fetch=False, do_update_check=True, do_update=False): + if item['version'] == 'unknown': + dir_path = unified_manager.unknown_active_nodes.get(item['id'])[1] + elif item['version'] == 'nightly': + dir_path = unified_manager.active_nodes.get(item['id'])[1] + else: + # skip CNR nodes + dir_path = None + + if dir_path and os.path.exists(dir_path): + if do_update_check: + try: + update_state, success = git_repo_update_check_with(dir_path, do_fetch, do_update) + if (do_update_check or do_update) and update_state: + item['update-state'] = 'true' + elif do_update and not success: + item['update-state'] = 'fail' + except Exception: + print(f"[ComfyUI-Manager] Failed to check state of the git node pack: {dir_path}") + + +def get_installed_pip_packages(): + # extract pip package infos + cmd = manager_util.make_pip_cmd(['freeze']) + pips = subprocess.check_output(cmd, text=True).split('\n') + + res = {} + for x in pips: + if x.strip() == "": + continue + + if ' @ ' in x: + spec_url = x.split(' @ ') + res[spec_url[0]] = spec_url[1] + else: + res[x] = "" + + return res + + +async def get_current_snapshot(custom_nodes_only = False): + await unified_manager.reload('cache') + await unified_manager.get_custom_nodes('default', 'cache') + + # Get ComfyUI hash + repo_path = context.comfy_path + + comfyui_commit_hash = None + if not custom_nodes_only: + if os.path.exists(os.path.join(repo_path, '.git')): + repo = git.Repo(repo_path) + comfyui_commit_hash = repo.head.commit.hexsha + + git_custom_nodes = {} + cnr_custom_nodes = {} + file_custom_nodes = [] + + # Get custom nodes hash + for custom_nodes_dir in get_custom_nodes_paths(): + paths = os.listdir(custom_nodes_dir) + + disabled_path = os.path.join(custom_nodes_dir, '.disabled') + if os.path.exists(disabled_path): + for x in os.listdir(disabled_path): + paths.append(os.path.join(disabled_path, x)) + + for path in paths: + if path in ['.disabled', '__pycache__']: + continue + + fullpath = os.path.join(custom_nodes_dir, path) + + if os.path.isdir(fullpath): + is_disabled = path.endswith(".disabled") or os.path.basename(os.path.dirname(fullpath)) == ".disabled" + + try: + info = unified_manager.resolve_from_path(fullpath) + + if info is None: + continue + + if info['ver'] not in ['nightly', 'latest', 'unknown']: + if is_disabled: + continue # don't restore disabled state of CNR node. + + cnr_custom_nodes[info['id']] = info['ver'] + else: + commit_hash = git_utils.get_commit_hash(fullpath) + url = git_utils.git_url(fullpath) + git_custom_nodes[url] = dict(hash=commit_hash, disabled=is_disabled) + except Exception: + print(f"Failed to extract snapshots for the custom node '{path}'.") + + elif path.endswith('.py'): + is_disabled = path.endswith(".py.disabled") + filename = os.path.basename(path) + item = { + 'filename': filename, + 'disabled': is_disabled + } + + file_custom_nodes.append(item) + + pip_packages = None if custom_nodes_only else get_installed_pip_packages() + + return { + 'comfyui': comfyui_commit_hash, + 'git_custom_nodes': git_custom_nodes, + 'cnr_custom_nodes': cnr_custom_nodes, + 'file_custom_nodes': file_custom_nodes, + 'pips': pip_packages, + } + + +async def save_snapshot_with_postfix(postfix, path=None, custom_nodes_only = False): + if path is None: + now = datetime.now() + + date_time_format = now.strftime("%Y-%m-%d_%H-%M-%S") + file_name = f"{date_time_format}_{postfix}" + + path = os.path.join(context.manager_snapshot_path, f"{file_name}.json") + else: + file_name = path.replace('\\', '/').split('/')[-1] + file_name = file_name.split('.')[-2] + + snapshot = await get_current_snapshot(custom_nodes_only) + if path.endswith('.json'): + with open(path, "w") as json_file: + json.dump(snapshot, json_file, indent=4) + + return file_name + '.json' + + elif path.endswith('.yaml'): + with open(path, "w") as yaml_file: + snapshot = {'custom_nodes': snapshot} + yaml.dump(snapshot, yaml_file, allow_unicode=True) + + return path + + +async def extract_nodes_from_workflow(filepath, mode='local', channel_url='default'): + # prepare json data + workflow = None + if filepath.endswith('.json'): + with open(filepath, "r", encoding="UTF-8", errors="ignore") as json_file: + try: + workflow = json.load(json_file) + except Exception: + print(f"Invalid workflow file: {filepath}") + exit(-1) + + elif filepath.endswith('.png'): + from PIL import Image + with Image.open(filepath) as img: + if 'workflow' not in img.info: + print(f"The specified .png file doesn't have a workflow: {filepath}") + exit(-1) + else: + try: + workflow = json.loads(img.info['workflow']) + except Exception: + print(f"This is not a valid .png file containing a ComfyUI workflow: {filepath}") + exit(-1) + + if workflow is None: + print(f"Invalid workflow file: {filepath}") + exit(-1) + + # extract nodes + used_nodes = set() + + def extract_nodes(sub_workflow): + for x in sub_workflow['nodes']: + node_name = x.get('type') + + # skip virtual nodes + if node_name in ['Reroute', 'Note']: + continue + + if node_name is not None and not (node_name.startswith('workflow/') or node_name.startswith('workflow>')): + used_nodes.add(node_name) + + if 'nodes' in workflow: + extract_nodes(workflow) + + if 'extra' in workflow: + if 'groupNodes' in workflow['extra']: + for x in workflow['extra']['groupNodes'].values(): + extract_nodes(x) + + # lookup dependent custom nodes + ext_map = await get_data_by_mode(mode, 'extension-node-map.json', channel_url) + + rext_map = {} + preemption_map = {} + patterns = [] + for k, v in ext_map.items(): + if k == 'https://github.com/comfyanonymous/ComfyUI': + for x in v[0]: + if x not in preemption_map: + preemption_map[x] = [] + + preemption_map[x] = k + continue + + for x in v[0]: + if x not in rext_map: + rext_map[x] = [] + + rext_map[x].append(k) + + if 'preemptions' in v[1]: + for x in v[1]['preemptions']: + if x not in preemption_map: + preemption_map[x] = [] + + preemption_map[x] = k + + if 'nodename_pattern' in v[1]: + patterns.append((v[1]['nodename_pattern'], k)) + + # identify used extensions + used_exts = set() + unknown_nodes = set() + + for node_name in used_nodes: + ext = preemption_map.get(node_name) + + if ext is None: + ext = rext_map.get(node_name) + if ext is not None: + ext = ext[0] + + if ext is None: + for pat_ext in patterns: + if re.search(pat_ext[0], node_name): + ext = pat_ext[1] + break + + if ext == 'https://github.com/comfyanonymous/ComfyUI': + pass + elif ext is not None: + used_exts.add(ext) + else: + unknown_nodes.add(node_name) + + return used_exts, unknown_nodes + + +def unzip(model_path): + if not os.path.exists(model_path): + print(f"[ComfyUI-Manager] unzip: File not found: {model_path}") + return False + + base_dir = os.path.dirname(model_path) + filename = os.path.basename(model_path) + target_dir = os.path.join(base_dir, filename[:-4]) + + os.makedirs(target_dir, exist_ok=True) + + with zipfile.ZipFile(model_path, 'r') as zip_ref: + zip_ref.extractall(target_dir) + + # Check if there's only one directory inside the target directory + contents = os.listdir(target_dir) + if len(contents) == 1 and os.path.isdir(os.path.join(target_dir, contents[0])): + nested_dir = os.path.join(target_dir, contents[0]) + # Move each file and sub-directory in the nested directory up to the target directory + for item in os.listdir(nested_dir): + shutil.move(os.path.join(nested_dir, item), os.path.join(target_dir, item)) + # Remove the now empty nested directory + os.rmdir(nested_dir) + + os.remove(model_path) + return True + + +def map_to_unified_keys(json_obj): + res = {} + for k, v in json_obj.items(): + cnr = unified_manager.get_cnr_by_repo(k) + if cnr: + res[cnr['id']] = v + else: + res[k] = v + + return res + + +async def get_unified_total_nodes(channel, mode, regsitry_cache_mode='cache'): + await unified_manager.reload(regsitry_cache_mode) + + res = await unified_manager.get_custom_nodes(channel, mode) + + # collect pure cnr ids (i.e. not exists in custom-node-list.json) + # populate state/updatable field to non-pure cnr nodes + cnr_ids = set(unified_manager.cnr_map.keys()) + for k, v in res.items(): + # resolve cnr_id from repo url + files_in_json = v.get('files', []) + cnr_id = None + if len(files_in_json) == 1: + cnr = unified_manager.get_cnr_by_repo(files_in_json[0]) + if cnr: + cnr_id = cnr['id'] + + if cnr_id is not None: + # cnr or nightly version + cnr_ids.discard(cnr_id) + updatable = False + cnr = unified_manager.cnr_map[cnr_id] + + if cnr_id in invalid_nodes: + v['invalid-installation'] = True + + if cnr_id in unified_manager.active_nodes: + # installed + v['state'] = 'enabled' + if unified_manager.active_nodes[cnr_id][0] != 'nightly': + updatable = unified_manager.is_updatable(cnr_id) + else: + updatable = False + v['active_version'] = unified_manager.active_nodes[cnr_id][0] + v['version'] = v['active_version'] + + if cm_global.try_call(api="cm.is_import_failed_extension", name=unified_manager.active_nodes[cnr_id][1]): + v['import-fail'] = True + + elif cnr_id in unified_manager.cnr_inactive_nodes: + # disabled + v['state'] = 'disabled' + cnr_ver = unified_manager.get_from_cnr_inactive_nodes(cnr_id) + if cnr_ver is not None: + v['version'] = str(cnr_ver[0]) + else: + v['version'] = '0' + + elif cnr_id in unified_manager.nightly_inactive_nodes: + # disabled + v['state'] = 'disabled' + v['version'] = 'nightly' + else: + # not installed + v['state'] = 'not-installed' + + if 'version' not in v: + v['version'] = cnr['latest_version']['version'] + + v['update-state'] = 'true' if updatable else 'false' + else: + # unknown version + v['version'] = 'unknown' + + if unified_manager.is_enabled(k, 'unknown'): + v['state'] = 'enabled' + v['active_version'] = 'unknown' + + if cm_global.try_call(api="cm.is_import_failed_extension", name=unified_manager.unknown_active_nodes[k][1]): + v['import-fail'] = True + + elif unified_manager.is_disabled(k, 'unknown'): + v['state'] = 'disabled' + else: + v['state'] = 'not-installed' + + # add items for pure cnr nodes + if normalize_channel(channel) == DEFAULT_CHANNEL: + # Don't show CNR nodes unless default channel + for cnr_id in cnr_ids: + cnr = unified_manager.cnr_map[cnr_id] + author = cnr['publisher']['name'] + title = cnr['name'] + reference = f"https://registry.comfy.org/nodes/{cnr['id']}" + repository = cnr.get('repository', '') + install_type = "cnr" + description = cnr.get('description', '') + + ver = None + active_version = None + updatable = False + import_fail = None + if cnr_id in unified_manager.active_nodes: + # installed + state = 'enabled' + updatable = unified_manager.is_updatable(cnr_id) + active_version = unified_manager.active_nodes[cnr['id']][0] + ver = active_version + + if cm_global.try_call(api="cm.is_import_failed_extension", name=unified_manager.active_nodes[cnr_id][1]): + import_fail = True + + elif cnr['id'] in unified_manager.cnr_inactive_nodes: + # disabled + state = 'disabled' + elif cnr['id'] in unified_manager.nightly_inactive_nodes: + # disabled + state = 'disabled' + ver = 'nightly' + else: + # not installed + state = 'not-installed' + + if ver is None: + ver = cnr['latest_version']['version'] + + item = dict(author=author, title=title, reference=reference, repository=repository, install_type=install_type, + description=description, state=state, updatable=updatable, version=ver) + + if active_version: + item['active_version'] = active_version + + if import_fail: + item['import-fail'] = True + + res[cnr_id] = item + + return res + + +def populate_github_stats(node_packs, json_obj_github): + for k, v in node_packs.items(): + try: + url = v['reference'] + if url in json_obj_github: + v['stars'] = json_obj_github[url]['stars'] + v['last_update'] = json_obj_github[url]['last_update'] + v['trust'] = json_obj_github[url]['author_account_age_days'] > 600 + else: + v['stars'] = -1 + v['last_update'] = -1 + v['trust'] = False + except Exception: + logging.error(f"[ComfyUI-Manager] DB item is broken:\n{v}") + + +def populate_favorites(node_packs, json_obj_extras): + favorites = set(json_obj_extras['favorites']) + + for k, v in node_packs.items(): + if v.get('version') != 'unknown': + if k in favorites: + v['is_favorite'] = True + + +async def restore_snapshot(snapshot_path, git_helper_extras=None): + cloned_repos = [] + checkout_repos = [] + enabled_repos = [] + disabled_repos = [] + skip_node_packs = [] + switched_node_packs = [] + installed_node_packs = [] + failed = [] + + await unified_manager.reload('cache') + await unified_manager.get_custom_nodes('default', 'cache') + + cnr_repo_map = {} + for k, v in unified_manager.repo_cnr_map.items(): + cnr_repo_map[v['id']] = k + + print("Restore snapshot.") + + postinstalls = [] + + with open(snapshot_path, 'r', encoding="UTF-8") as snapshot_file: + if snapshot_path.endswith('.json'): + info = json.load(snapshot_file) + elif snapshot_path.endswith('.yaml'): + info = yaml.load(snapshot_file, Loader=yaml.SafeLoader) + info = info['custom_nodes'] + + if 'pips' in info and info['pips']: + pips = info['pips'] + else: + pips = {} + + # for cnr restore + cnr_info = info.get('cnr_custom_nodes') + if cnr_info is not None: + # disable not listed cnr nodes + todo_disable = [] + todo_checkout = [] + + for k, v in unified_manager.active_nodes.items(): + if 'comfyui-manager' in k: + continue + + if v[0] != 'nightly': + if k not in cnr_info: + todo_disable.append(k) + else: + cnr_ver = cnr_info[k] + if v[1] != cnr_ver: + todo_checkout.append((k, cnr_ver)) + else: + skip_node_packs.append(k) + + for x in todo_disable: + unified_manager.unified_disable(x, False) + disabled_repos.append(x) + + for x in todo_checkout: + ps = unified_manager.cnr_switch_version(x[0], x[1], instant_execution=True, no_deps=True, return_postinstall=False) + if ps.action == 'switch-cnr' and ps.result: + switched_node_packs.append(f"{x[0]}@{x[1]}") + elif ps.action == 'skip': + skip_node_packs.append(f"{x[0]}@{x[1]}") + elif not ps.result: + failed.append(f"{x[0]}@{x[1]}") + + # install listed cnr nodes + for k, v in cnr_info.items(): + if 'comfyui-manager' in k: + continue + + ps = await unified_manager.install_by_id(k, version_spec=v, instant_execution=True, return_postinstall=True) + if ps.action == 'install-cnr' and ps.result: + installed_node_packs.append(f"{k}@{v}") + + if ps is not None and ps.result: + if hasattr(ps, 'postinstall'): + postinstalls.append(ps.postinstall) + else: + print("cm-cli: unexpected [0001]") + + # for nightly restore + _git_info = info.get('git_custom_nodes') + git_info = {} + + # normalize github repo + for k, v in _git_info.items(): + # robust filter out comfyui-manager while restoring snapshot + if 'comfyui-manager' in k.lower(): + continue + + norm_k = git_utils.normalize_url(k) + git_info[norm_k] = v + + if git_info is not None: + todo_disable = [] + todo_enable = [] + todo_checkout = [] + processed_urls = [] + + for k, v in unified_manager.active_nodes.items(): + if 'comfyui-manager' in k: + continue + + if v[0] == 'nightly' and cnr_repo_map.get(k): + repo_url = cnr_repo_map.get(k) + normalized_url = git_utils.normalize_url(repo_url) + + if normalized_url not in git_info: + todo_disable.append(k) + else: + commit_hash = git_info[normalized_url]['hash'] + todo_checkout.append((v[1], commit_hash)) + + for k, v in unified_manager.nightly_inactive_nodes.items(): + if 'comfyui-manager' in k: + continue + + if cnr_repo_map.get(k): + repo_url = cnr_repo_map.get(k) + normalized_url = git_utils.normalize_url(repo_url) + + if normalized_url in git_info: + commit_hash = git_info[normalized_url]['hash'] + todo_enable.append((k, commit_hash)) + processed_urls.append(normalized_url) + + for x in todo_disable: + unified_manager.unified_disable(x, False) + disabled_repos.append(x) + + for x in todo_enable: + res = unified_manager.unified_enable(x[0], 'nightly') + + is_switched = False + if res and res.target: + is_switched = repo_switch_commit(res.target, x[1]) + + if is_switched: + checkout_repos.append(f"{x[0]}@{x[1]}") + else: + enabled_repos.append(x[0]) + + for x in todo_checkout: + is_switched = repo_switch_commit(x[0], x[1]) + + if is_switched: + checkout_repos.append(f"{x[0]}@{x[1]}") + + for x in git_info.keys(): + normalized_url = git_utils.normalize_url(x) + cnr = unified_manager.repo_cnr_map.get(normalized_url) + if cnr is not None: + pack_id = cnr['id'] + res = await unified_manager.install_by_id(pack_id, 'nightly', instant_execution=True, no_deps=False, return_postinstall=False) + if res.action == 'install-git' and res.result: + cloned_repos.append(pack_id) + elif res.action == 'skip': + skip_node_packs.append(pack_id) + elif not res.result: + failed.append(pack_id) + processed_urls.append(x) + + for x in processed_urls: + if x in git_info: + del git_info[x] + + # for unknown restore + todo_disable = [] + todo_enable = [] + todo_checkout = [] + processed_urls = [] + + for k2, v2 in unified_manager.unknown_active_nodes.items(): + repo_url = resolve_giturl_from_path(v2[1]) + + if repo_url is None: + continue + + normalized_url = git_utils.normalize_url(repo_url) + + if normalized_url not in git_info: + todo_disable.append(k2) + else: + commit_hash = git_info[normalized_url]['hash'] + todo_checkout.append((k2, commit_hash)) + processed_urls.append(normalized_url) + + for k2, v2 in unified_manager.unknown_inactive_nodes.items(): + repo_url = resolve_giturl_from_path(v2[1]) + + if repo_url is None: + continue + + normalized_url = git_utils.normalize_url(repo_url) + + if normalized_url in git_info: + commit_hash = git_info[normalized_url]['hash'] + todo_enable.append((k2, commit_hash)) + processed_urls.append(normalized_url) + + for x in todo_disable: + unified_manager.unified_disable(x, True) + disabled_repos.append(x) + + for x in todo_enable: + res = unified_manager.unified_enable(x[0], 'unknown') + + is_switched = False + if res and res.target: + is_switched = repo_switch_commit(res.target, x[1]) + + if is_switched: + checkout_repos.append(f"{x[0]}@{x[1]}") + else: + enabled_repos.append(x[0]) + + for x in todo_checkout: + is_switched = repo_switch_commit(x[0], x[1]) + + if is_switched: + checkout_repos.append(f"{x[0]}@{x[1]}") + else: + skip_node_packs.append(x[0]) + + for x in processed_urls: + if x in git_info: + del git_info[x] + + for repo_url in git_info.keys(): + repo_name = os.path.basename(repo_url) + if repo_name.endswith('.git'): + repo_name = repo_name[:-4] + + to_path = os.path.join(get_default_custom_nodes_path(), repo_name) + unified_manager.repo_install(repo_url, to_path, instant_execution=True, no_deps=False, return_postinstall=False) + cloned_repos.append(repo_name) + + manager_util.restore_pip_snapshot(pips, git_helper_extras) + + # print summary + for x in cloned_repos: + print(f"[ INSTALLED ] {x}") + for x in installed_node_packs: + print(f"[ INSTALLED ] {x}") + for x in checkout_repos: + print(f"[ CHECKOUT ] {x}") + for x in switched_node_packs: + print(f"[ SWITCHED ] {x}") + for x in enabled_repos: + print(f"[ ENABLED ] {x}") + for x in disabled_repos: + print(f"[ DISABLED ] {x}") + for x in skip_node_packs: + print(f"[ SKIPPED ] {x}") + for x in failed: + print(f"[ FAILED ] {x}") + + # if is_failed: + # print("[bold red]ERROR: Failed to restore snapshot.[/bold red]") + + +def get_comfyui_versions(repo=None): + if repo is None: + repo = git.Repo(context.comfy_path) + + try: + remote = get_remote_name(repo) + repo.remotes[remote].fetch() + except Exception: + logging.error("[ComfyUI-Manager] Failed to fetch ComfyUI") + + versions = [x.name for x in repo.tags if x.name.startswith('v')] + + # nearest tag + versions = sorted(versions, key=lambda v: repo.git.log('-1', '--format=%ct', v), reverse=True) + versions = versions[:4] + + current_tag = repo.git.describe('--tags') + + if current_tag not in versions: + versions = sorted(versions + [current_tag], key=lambda v: repo.git.log('-1', '--format=%ct', v), reverse=True) + versions = versions[:4] + + main_branch = repo.heads.master + latest_commit = main_branch.commit + latest_tag = repo.git.describe('--tags', latest_commit.hexsha) + + if latest_tag != versions[0]: + versions.insert(0, 'nightly') + else: + versions[0] = 'nightly' + current_tag = 'nightly' + + return versions, current_tag, latest_tag + + +def switch_comfyui(tag): + repo = git.Repo(context.comfy_path) + + if tag == 'nightly': + repo.git.checkout('master') + tracking_branch = repo.active_branch.tracking_branch() + remote_name = tracking_branch.remote_name + repo.remotes[remote_name].pull() + print("[ComfyUI-Manager] ComfyUI version is switched to the latest 'master' version") + else: + repo.git.checkout(tag) + print(f"[ComfyUI-Manager] ComfyUI version is switched to '{tag}'") + + +def resolve_giturl_from_path(fullpath): + """ + resolve giturl path of unclassified custom node based on remote url in .git/config + """ + git_config_path = os.path.join(fullpath, '.git', 'config') + + if not os.path.exists(git_config_path): + return "unknown" + + config = configparser.ConfigParser(strict=False) + config.read(git_config_path) + + for k, v in config.items(): + if k.startswith('remote ') and 'url' in v: + return v['url'].replace("git@github.com:", "https://github.com/") + + return None + + +def repo_switch_commit(repo_path, commit_hash): + try: + repo = git.Repo(repo_path) + if repo.head.commit.hexsha == commit_hash: + return False + + repo.git.checkout(commit_hash) + return True + except Exception: + return None diff --git a/glob/manager_server.py b/comfyui_manager/legacy/manager_server.py similarity index 72% rename from glob/manager_server.py rename to comfyui_manager/legacy/manager_server.py index cb3bcd92..8af46ff7 100644 --- a/glob/manager_server.py +++ b/comfyui_manager/legacy/manager_server.py @@ -14,25 +14,33 @@ import git from datetime import datetime from server import PromptServer -import manager_core as core -import manager_util -import cm_global import logging import asyncio -import queue +from collections import deque -import manager_downloader +from . import manager_core as core +from ..common import manager_util +from ..common import cm_global +from ..common import manager_downloader +from ..common import context +from ..common import manager_security logging.info(f"### Loading: ComfyUI-Manager ({core.version_str})") -logging.info("[ComfyUI-Manager] network_mode: " + core.get_config()['network_mode']) + +if not manager_util.is_manager_pip_package(): + network_mode_description = "offline" +else: + network_mode_description = core.get_config()['network_mode'] +logging.info("[ComfyUI-Manager] network_mode: " + network_mode_description) comfy_ui_hash = "-" comfyui_tag = None -SECURITY_MESSAGE_MIDDLE_OR_BELOW = "ERROR: To use this action, a security_level of `middle or below` is required. Please contact the administrator.\nReference: https://github.com/ltdrdata/ComfyUI-Manager#security-policy" -SECURITY_MESSAGE_NORMAL_MINUS = "ERROR: To use this feature, you must either set '--listen' to a local IP and set the security level to 'normal-' or lower, or set the security level to 'middle' or 'weak'. Please contact the administrator.\nReference: https://github.com/ltdrdata/ComfyUI-Manager#security-policy" -SECURITY_MESSAGE_GENERAL = "ERROR: This installation is not allowed in this security_level. Please contact the administrator.\nReference: https://github.com/ltdrdata/ComfyUI-Manager#security-policy" +SECURITY_MESSAGE_MIDDLE = "ERROR: To use this action, a security_level of `normal or below` is required. Please contact the administrator.\nReference: https://github.com/Comfy-Org/ComfyUI-Manager#security-policy" +SECURITY_MESSAGE_MIDDLE_P = "ERROR: To use this action, security_level must be `normal or below`, and network_mode must be set to `personal_cloud`. Please contact the administrator.\nReference: https://github.com/ltdrdata/ComfyUI-Manager#security-policy" +SECURITY_MESSAGE_NORMAL_MINUS = "ERROR: To use this feature, you must either set '--listen' to a local IP and set the security level to 'normal-' or lower, or set the security level to 'middle' or 'weak'. Please contact the administrator.\nReference: https://github.com/Comfy-Org/ComfyUI-Manager#security-policy" +SECURITY_MESSAGE_GENERAL = "ERROR: This installation is not allowed in this security_level. Please contact the administrator.\nReference: https://github.com/Comfy-Org/ComfyUI-Manager#security-policy" SECURITY_MESSAGE_NORMAL_MINUS_MODEL = "ERROR: Downloading models that are not in '.safetensors' format is only allowed for models registered in the 'default' channel at this security level. If you want to download this model, set the security level to 'normal-' or lower." routes = PromptServer.instance.routes @@ -87,13 +95,27 @@ model_dir_name_map = { def is_allowed_security_level(level): + is_personal_cloud = core.get_config()['network_mode'].lower() == 'personal_cloud' + if level == 'block': return False + elif level == 'high+': + if is_local_mode: + return core.get_config()['security_level'] in ['weak', 'normal-'] + elif is_personal_cloud: + return core.get_config()['security_level'] == 'weak' + else: + return False elif level == 'high': if is_local_mode: return core.get_config()['security_level'] in ['weak', 'normal-'] else: return core.get_config()['security_level'] == 'weak' + elif level == 'middle+': + if is_local_mode or is_personal_cloud: + return core.get_config()['security_level'] in ['weak', 'normal', 'normal-'] + else: + return False elif level == 'middle': return core.get_config()['security_level'] in ['weak', 'normal', 'normal-'] else: @@ -102,7 +124,7 @@ def is_allowed_security_level(level): async def get_risky_level(files, pip_packages): json_data1 = await core.get_data_by_mode('local', 'custom-node-list.json') - json_data2 = await core.get_data_by_mode('cache', 'custom-node-list.json', channel_url='https://raw.githubusercontent.com/ltdrdata/ComfyUI-Manager/main') + json_data2 = await core.get_data_by_mode('cache', 'custom-node-list.json', channel_url='https://raw.githubusercontent.com/Comfy-Org/ComfyUI-Manager/main') all_urls = set() for x in json_data1['custom_nodes'] + json_data2['custom_nodes']: @@ -110,7 +132,7 @@ async def get_risky_level(files, pip_packages): for x in files: if x not in all_urls: - return "high" + return "high+" all_pip_packages = set() for x in json_data1['custom_nodes'] + json_data2['custom_nodes']: @@ -120,7 +142,7 @@ async def get_risky_level(files, pip_packages): if p not in all_pip_packages: return "block" - return "middle" + return "middle+" class ManagerFuncsInComfyUI(core.ManagerFuncs): @@ -155,12 +177,10 @@ class ManagerFuncsInComfyUI(core.ManagerFuncs): core.manager_funcs = ManagerFuncsInComfyUI() -sys.path.append('../..') +from comfyui_manager.common.manager_downloader import download_url, download_url_with_agent -from manager_downloader import download_url, download_url_with_agent - -core.comfy_path = os.path.dirname(folder_paths.__file__) -core.js_path = os.path.join(core.comfy_path, "web", "extensions") +context.comfy_path = os.path.dirname(folder_paths.__file__) +core.js_path = os.path.join(context.comfy_path, "web", "extensions") local_db_model = os.path.join(manager_util.comfyui_manager_path, "model-list.json") local_db_alter = os.path.join(manager_util.comfyui_manager_path, "alter-list.json") @@ -181,10 +201,7 @@ def set_preview_method(method): core.get_config()['preview_method'] = method -if args.preview_method == latent_preview.LatentPreviewMethod.NoPreviews: - set_preview_method(core.get_config()['preview_method']) -else: - logging.warning("[ComfyUI-Manager] Since --preview-method is set, ComfyUI-Manager's preview method feature will be ignored.") +set_preview_method(core.get_config()['preview_method']) def set_component_policy(mode): @@ -214,12 +231,12 @@ def print_comfyui_version(): is_detached = repo.head.is_detached current_branch = repo.active_branch.name - comfyui_tag = core.get_comfyui_tag() + comfyui_tag = context.get_comfyui_tag() try: if not os.environ.get('__COMFYUI_DESKTOP_VERSION__') and core.comfy_ui_commit_datetime.date() < core.comfy_ui_required_commit_datetime.date(): logging.warning(f"\n\n## [WARN] ComfyUI-Manager: Your ComfyUI version ({core.comfy_ui_revision})[{core.comfy_ui_commit_datetime.date()}] is too old. Please update to the latest version. ##\n\n") - except: + except Exception: pass # process on_revision_detected --> @@ -246,7 +263,7 @@ def print_comfyui_version(): logging.info(f"### ComfyUI Version: {comfyui_tag} on '{current_branch}' | Released on '{core.comfy_ui_commit_datetime.date()}'") else: logging.info(f"### ComfyUI Revision: {core.comfy_ui_revision} on '{current_branch}' [{comfy_ui_hash[:8]}] | Released on '{core.comfy_ui_commit_datetime.date()}'") - except: + except Exception: if is_detached: logging.info(f"### ComfyUI Revision: {core.comfy_ui_revision} [{comfy_ui_hash[:8]}] *DETACHED | Released on '{core.comfy_ui_commit_datetime.date()}'") else: @@ -394,18 +411,77 @@ def nickname_filter(json_obj): return json_obj -task_queue = queue.Queue() -nodepack_result = {} -model_result = {} +class TaskBatch: + def __init__(self, batch_json, tasks, failed): + self.nodepack_result = {} + self.model_result = {} + self.batch_id = batch_json.get('batch_id') if batch_json is not None else None + self.batch_json = batch_json + self.tasks = tasks + self.current_index = 0 + self.stats = {} + self.failed = failed if failed is not None else set() + self.is_aborted = False + + def is_done(self): + return len(self.tasks) <= self.current_index + + def get_next(self): + if self.is_done(): + return None + + item = self.tasks[self.current_index] + self.current_index += 1 + return item + + def done_count(self): + return len(self.nodepack_result) + len(self.model_result) + + def total_count(self): + return len(self.tasks) + + def abort(self): + self.is_aborted = True + + def finalize(self): + if self.batch_id is not None: + batch_path = os.path.join(context.manager_batch_history_path, self.batch_id+".json") + json_obj = { + "batch": self.batch_json, + "nodepack_result": self.nodepack_result, + "model_result": self.model_result, + "failed": list(self.failed) + } + with open(batch_path, "w") as json_file: + json.dump(json_obj, json_file, indent=4) + + +temp_queue_batch = [] +task_batch_queue = deque() tasks_in_progress = set() task_worker_lock = threading.Lock() +aborted_batch = None + + +def finalize_temp_queue_batch(batch_json=None, failed=None): + """ + make temp_queue_batch as a batch snapshot and add to batch_queue + """ + + global temp_queue_batch + + if len(temp_queue_batch): + batch = TaskBatch(batch_json, temp_queue_batch, failed) + task_batch_queue.append(batch) + temp_queue_batch = [] + async def task_worker(): global task_queue - global nodepack_result - global model_result global tasks_in_progress + await core.unified_manager.reload('cache') + async def do_install(item) -> str: ui_id, node_spec_str, channel, mode, skip_post_install = item @@ -416,8 +492,7 @@ async def task_worker(): return f"Cannot resolve install target: '{node_spec_str}'" node_name, version_spec, is_specified = node_spec - res = await core.unified_manager.install_by_id(node_name, version_spec, channel, mode, return_postinstall=skip_post_install) - # discard post install if skip_post_install mode + res = await core.unified_manager.install_by_id(node_name, version_spec, channel, mode, return_postinstall=skip_post_install) # discard post install if skip_post_install mode if res.action not in ['skip', 'enable', 'install-git', 'install-cnr', 'switch-cnr']: logging.error(f"[ComfyUI-Manager] Installation failed:\n{res.msg}") @@ -432,6 +507,11 @@ async def task_worker(): traceback.print_exc() return f"Installation failed:\n{node_spec_str}" + async def do_enable(item) -> str: + ui_id, cnr_id = item + core.unified_manager.unified_enable(cnr_id) + return 'success' + async def do_update(item): ui_id, node_name, node_ver = item @@ -440,10 +520,7 @@ async def task_worker(): if res.ver == 'unknown': url = core.unified_manager.unknown_active_nodes[node_name][0] - try: - title = os.path.basename(url) - except Exception: - title = node_name + title = os.path.basename(url) else: url = core.unified_manager.cnr_map[node_name].get('repository') title = core.unified_manager.cnr_map[node_name]['name'] @@ -593,31 +670,45 @@ async def task_worker(): return f"Model installation error: {model_url}" - stats = {} - while True: - done_count = len(nodepack_result) + len(model_result) - total_count = done_count + task_queue.qsize() + with task_worker_lock: + if len(task_batch_queue) > 0: + cur_batch = task_batch_queue[0] + else: + logging.info("\n[ComfyUI-Manager] All tasks are completed.") + logging.info("\nAfter restarting ComfyUI, please refresh the browser.") - if task_queue.empty(): - logging.info(f"\n[ComfyUI-Manager] Queued works are completed.\n{stats}") + res = {'status': 'all-done'} - logging.info("\nAfter restarting ComfyUI, please refresh the browser.") - PromptServer.instance.send_sync("cm-queue-status", - {'status': 'done', - 'nodepack_result': nodepack_result, 'model_result': model_result, - 'total_count': total_count, 'done_count': done_count}) - nodepack_result = {} - task_queue = queue.Queue() - return # terminate worker thread + PromptServer.instance.send_sync("cm-queue-status", res) + + return + + if cur_batch.is_done(): + logging.info(f"\n[ComfyUI-Manager] A tasks batch(batch_id={cur_batch.batch_id}) is completed.\nstat={cur_batch.stats}") + + res = {'status': 'batch-done', + 'nodepack_result': cur_batch.nodepack_result, + 'model_result': cur_batch.model_result, + 'total_count': cur_batch.total_count(), + 'done_count': cur_batch.done_count(), + 'batch_id': cur_batch.batch_id, + 'remaining_batch_count': len(task_batch_queue) } + + PromptServer.instance.send_sync("cm-queue-status", res) + cur_batch.finalize() + task_batch_queue.popleft() + continue with task_worker_lock: - kind, item = task_queue.get() + kind, item = cur_batch.get_next() tasks_in_progress.add((kind, item[0])) try: if kind == 'install': msg = await do_install(item) + elif kind == 'enable': + msg = await do_enable(item) elif kind == 'install-model': msg = await do_install_model(item) elif kind == 'update': @@ -641,31 +732,131 @@ async def task_worker(): with task_worker_lock: tasks_in_progress.remove((kind, item[0])) - ui_id = item[0] - if kind == 'install-model': - model_result[ui_id] = msg - ui_target = "model_manager" - elif kind == 'update-main': - nodepack_result[ui_id] = msg - ui_target = "main" - elif kind == 'update-comfyui': - nodepack_result['comfyui'] = msg - ui_target = "main" - elif kind == 'update': - nodepack_result[ui_id] = msg['msg'] - ui_target = "nodepack_manager" - else: - nodepack_result[ui_id] = msg - ui_target = "nodepack_manager" + ui_id = item[0] + if kind == 'install-model': + cur_batch.model_result[ui_id] = msg + ui_target = "model_manager" + elif kind == 'update-main': + cur_batch.nodepack_result[ui_id] = msg + ui_target = "main" + elif kind == 'update-comfyui': + cur_batch.nodepack_result['comfyui'] = msg + ui_target = "main" + elif kind == 'update': + cur_batch.nodepack_result[ui_id] = msg['msg'] + ui_target = "nodepack_manager" + else: + cur_batch.nodepack_result[ui_id] = msg + ui_target = "nodepack_manager" - stats[kind] = stats.get(kind, 0) + 1 + cur_batch.stats[kind] = cur_batch.stats.get(kind, 0) + 1 PromptServer.instance.send_sync("cm-queue-status", - {'status': 'in_progress', 'target': item[0], 'ui_target': ui_target, - 'total_count': total_count, 'done_count': done_count}) + {'status': 'in_progress', + 'target': item[0], + 'batch_id': cur_batch.batch_id, + 'ui_target': ui_target, + 'total_count': cur_batch.total_count(), + 'done_count': cur_batch.done_count()}) -@routes.get("/customnode/getmappings") +@routes.post("/v2/manager/queue/batch") +async def queue_batch(request): + json_data = await request.json() + + failed = set() + + for k, v in json_data.items(): + if k == 'update_all': + await _update_all({'mode': v}) + + elif k == 'reinstall': + for x in v: + res = await _uninstall_custom_node(x) + if res.status != 200: + failed.add(x['id']) + else: + res = await _install_custom_node(x) + if res.status != 200: + failed.add(x['id']) + + elif k == 'install': + for x in v: + res = await _install_custom_node(x) + if res.status != 200: + failed.add(x['id']) + + elif k == 'uninstall': + for x in v: + res = await _uninstall_custom_node(x) + if res.status != 200: + failed.add(x['id']) + + elif k == 'update': + for x in v: + res = await _update_custom_node(x) + if res.status != 200: + failed.add(x['id']) + + elif k == 'update_comfyui': + await update_comfyui(None) + + elif k == 'disable': + for x in v: + await _disable_node(x) + + elif k == 'install_model': + for x in v: + res = await _install_model(x) + if res.status != 200: + failed.add(x['id']) + + elif k == 'fix': + for x in v: + res = await _fix_custom_node(x) + if res.status != 200: + failed.add(x['id']) + + with task_worker_lock: + finalize_temp_queue_batch(json_data, failed) + _queue_start() + + return web.json_response({"failed": list(failed)}, content_type='application/json') + + +@routes.get("/v2/manager/queue/history_list") +async def get_history_list(request): + history_path = context.manager_batch_history_path + + try: + files = [os.path.join(history_path, f) for f in os.listdir(history_path) if os.path.isfile(os.path.join(history_path, f))] + files.sort(key=lambda x: os.path.getmtime(x), reverse=True) + history_ids = [os.path.basename(f)[:-5] for f in files] + + return web.json_response({"ids": list(history_ids)}, content_type='application/json') + except Exception as e: + logging.error(f"[ComfyUI-Manager] /v2/manager/queue/history_list - {e}") + return web.Response(status=400) + + +@routes.get("/v2/manager/queue/history") +async def get_history(request): + try: + json_name = request.rel_url.query["id"]+'.json' + batch_path = os.path.join(context.manager_batch_history_path, json_name) + + with open(batch_path, 'r', encoding='utf-8') as file: + json_str = file.read() + json_obj = json.loads(json_str) + return web.json_response(json_obj, content_type='application/json') + + except Exception as e: + logging.error(f"[ComfyUI-Manager] /v2/manager/queue/history - {e}") + + return web.Response(status=400) + + +@routes.get("/v2/customnode/getmappings") async def fetch_customnode_mappings(request): """ provide unified (node -> node pack) mapping list @@ -701,7 +892,7 @@ async def fetch_customnode_mappings(request): return web.json_response(json_obj, content_type='application/json') -@routes.get("/customnode/fetch_updates") +@routes.get("/v2/customnode/fetch_updates") async def fetch_updates(request): try: if request.rel_url.query["mode"] == "local": @@ -723,15 +914,20 @@ async def fetch_updates(request): return web.Response(status=201) return web.Response(status=200) - except: + except Exception: traceback.print_exc() return web.Response(status=400) -@routes.get("/manager/queue/update_all") +@routes.get("/v2/manager/queue/update_all") async def update_all(request): - if not is_allowed_security_level('middle'): - logging.error(SECURITY_MESSAGE_MIDDLE_OR_BELOW) + json_data = dict(request.rel_url.query) + return await _update_all(json_data) + + +async def _update_all(json_data): + if not is_allowed_security_level('middle+'): + logging.error(SECURITY_MESSAGE_MIDDLE_P) return web.Response(status=403) with task_worker_lock: @@ -741,13 +937,13 @@ async def update_all(request): await core.save_snapshot_with_postfix('autosave') - if request.rel_url.query["mode"] == "local": + if json_data["mode"] == "local": channel = 'local' else: channel = core.get_config()['channel_url'] - await core.unified_manager.reload(request.rel_url.query["mode"]) - await core.unified_manager.get_custom_nodes(channel, request.rel_url.query["mode"]) + await core.unified_manager.reload(json_data["mode"]) + await core.unified_manager.get_custom_nodes(channel, json_data["mode"]) for k, v in core.unified_manager.active_nodes.items(): if k == 'comfyui-manager': @@ -756,7 +952,7 @@ async def update_all(request): continue update_item = k, k, v[0] - task_queue.put(("update-main", update_item)) + temp_queue_batch.append(("update-main", update_item)) for k, v in core.unified_manager.unknown_active_nodes.items(): if k == 'comfyui-manager': @@ -765,7 +961,7 @@ async def update_all(request): continue update_item = k, k, 'unknown' - task_queue.put(("update-main", update_item)) + temp_queue_batch.append(("update-main", update_item)) return web.Response(status=200) @@ -814,9 +1010,18 @@ def populate_markdown(x): x['title'] = manager_util.sanitize_tag(x['title']) +@routes.get("/v2/manager/is_legacy_manager_ui") +async def is_legacy_manager_ui(request): + return web.json_response( + {"is_legacy_manager_ui": args.enable_manager_legacy_ui}, + content_type="application/json", + status=200, + ) + + # freeze imported version startup_time_installed_node_packs = core.get_installed_node_packs() -@routes.get("/customnode/installed") +@routes.get("/v2/customnode/installed") async def installed_list(request): mode = request.query.get('mode', 'default') @@ -828,7 +1033,7 @@ async def installed_list(request): return web.json_response(res, content_type='application/json') -@routes.get("/customnode/getlist") +@routes.get("/v2/customnode/getlist") async def fetch_customnode_list(request): """ provide unified custom node list @@ -855,6 +1060,15 @@ async def fetch_customnode_list(request): for v in node_packs.values(): populate_markdown(v) + if 'comfyui-manager' in node_packs: + del node_packs['comfyui-manager'] + + if 'https://github.com/ltdrdata/ComfyUI-Manager' in node_packs: + del node_packs['https://github.com/ltdrdata/ComfyUI-Manager'] + + if 'https://github.com/Comfy-Org/ComfyUI-Manager' in node_packs: + del node_packs['https://github.com/Comfy-Org/ComfyUI-Manager'] + if channel != 'local': found = 'custom' @@ -941,7 +1155,7 @@ def check_model_installed(json_obj): executor.submit(process_model_phase, item) -@routes.get("/externalmodel/getlist") +@routes.get("/v2/externalmodel/getlist") async def fetch_externalmodel_list(request): # The model list is only allowed in the default channel, yet. json_obj = await core.get_data_by_mode(request.rel_url.query["mode"], 'model-list.json') @@ -954,46 +1168,46 @@ async def fetch_externalmodel_list(request): return web.json_response(json_obj, content_type='application/json') -@PromptServer.instance.routes.get("/snapshot/getlist") +@PromptServer.instance.routes.get("/v2/snapshot/getlist") async def get_snapshot_list(request): - items = [f[:-5] for f in os.listdir(core.manager_snapshot_path) if f.endswith('.json')] + items = [f[:-5] for f in os.listdir(context.manager_snapshot_path) if f.endswith('.json')] items.sort(reverse=True) return web.json_response({'items': items}, content_type='application/json') -@routes.get("/snapshot/remove") +@routes.get("/v2/snapshot/remove") async def remove_snapshot(request): if not is_allowed_security_level('middle'): - logging.error(SECURITY_MESSAGE_MIDDLE_OR_BELOW) + logging.error(SECURITY_MESSAGE_MIDDLE) return web.Response(status=403) try: target = request.rel_url.query["target"] - path = os.path.join(core.manager_snapshot_path, f"{target}.json") + path = os.path.join(context.manager_snapshot_path, f"{target}.json") if os.path.exists(path): os.remove(path) return web.Response(status=200) - except: + except Exception: return web.Response(status=400) -@routes.get("/snapshot/restore") +@routes.get("/v2/snapshot/restore") async def restore_snapshot(request): - if not is_allowed_security_level('middle'): - logging.error(SECURITY_MESSAGE_MIDDLE_OR_BELOW) + if not is_allowed_security_level('middle+'): + logging.error(SECURITY_MESSAGE_MIDDLE_P) return web.Response(status=403) try: target = request.rel_url.query["target"] - path = os.path.join(core.manager_snapshot_path, f"{target}.json") + path = os.path.join(context.manager_snapshot_path, f"{target}.json") if os.path.exists(path): - if not os.path.exists(core.manager_startup_script_path): - os.makedirs(core.manager_startup_script_path) + if not os.path.exists(context.manager_startup_script_path): + os.makedirs(context.manager_startup_script_path) - target_path = os.path.join(core.manager_startup_script_path, "restore-snapshot.json") + target_path = os.path.join(context.manager_startup_script_path, "restore-snapshot.json") shutil.copy(path, target_path) logging.info(f"Snapshot restore scheduled: `{target}`") @@ -1001,24 +1215,24 @@ async def restore_snapshot(request): logging.error(f"Snapshot file not found: `{path}`") return web.Response(status=400) - except: + except Exception: return web.Response(status=400) -@routes.get("/snapshot/get_current") +@routes.get("/v2/snapshot/get_current") async def get_current_snapshot_api(request): try: return web.json_response(await core.get_current_snapshot(), content_type='application/json') - except: + except Exception: return web.Response(status=400) -@routes.get("/snapshot/save") +@routes.get("/v2/snapshot/save") async def save_snapshot(request): try: await core.save_snapshot_with_postfix('snapshot') return web.Response(status=200) - except: + except Exception: return web.Response(status=400) @@ -1050,82 +1264,7 @@ def unzip_install(files): return True -def copy_install(files, js_path_name=None): - for url in files: - if url.endswith("/"): - url = url[:-1] - try: - filename = os.path.basename(url) - if url.endswith(".py"): - download_url(url, core.get_default_custom_nodes_path(), filename) - else: - path = os.path.join(core.js_path, js_path_name) if js_path_name is not None else core.js_path - if not os.path.exists(path): - os.makedirs(path) - download_url(url, path, filename) - - except Exception as e: - logging.error(f"Install(copy) error: {url} / {e}", file=sys.stderr) - return False - - logging.info("Installation was successful.") - return True - - -def copy_uninstall(files, js_path_name='.'): - for url in files: - if url.endswith("/"): - url = url[:-1] - dir_name = os.path.basename(url) - base_path = core.get_default_custom_nodes_path() if url.endswith('.py') else os.path.join(core.js_path, js_path_name) - file_path = os.path.join(base_path, dir_name) - - try: - if os.path.exists(file_path): - os.remove(file_path) - elif os.path.exists(file_path + ".disabled"): - os.remove(file_path + ".disabled") - except Exception as e: - logging.error(f"Uninstall(copy) error: {url} / {e}", file=sys.stderr) - return False - - logging.info("Uninstallation was successful.") - return True - - -def copy_set_active(files, is_disable, js_path_name='.'): - if is_disable: - action_name = "Disable" - else: - action_name = "Enable" - - for url in files: - if url.endswith("/"): - url = url[:-1] - dir_name = os.path.basename(url) - base_path = core.get_default_custom_nodes_path() if url.endswith('.py') else os.path.join(core.js_path, js_path_name) - file_path = os.path.join(base_path, dir_name) - - try: - if is_disable: - current_name = file_path - new_name = file_path + ".disabled" - else: - current_name = file_path + ".disabled" - new_name = file_path - - os.rename(current_name, new_name) - - except Exception as e: - logging.error(f"{action_name}(copy) error: {url} / {e}", file=sys.stderr) - - return False - - logging.info(f"{action_name} was successful.") - return True - - -@routes.get("/customnode/versions/{node_name}") +@routes.get("/v2/customnode/versions/{node_name}") async def get_cnr_versions(request): node_name = request.match_info.get("node_name", None) versions = core.cnr_utils.all_versions_of_node(node_name) @@ -1136,7 +1275,7 @@ async def get_cnr_versions(request): return web.Response(status=400) -@routes.get("/customnode/disabled_versions/{node_name}") +@routes.get("/v2/customnode/disabled_versions/{node_name}") async def get_disabled_versions(request): node_name = request.match_info.get("node_name", None) versions = [] @@ -1152,7 +1291,7 @@ async def get_disabled_versions(request): return web.Response(status=400) -@routes.post("/customnode/import_fail_info") +@routes.post("/v2/customnode/import_fail_info") async def import_fail_info(request): json_data = await request.json() @@ -1169,41 +1308,132 @@ async def import_fail_info(request): return web.Response(status=400) -@routes.post("/manager/queue/reinstall") +@routes.post("/v2/customnode/import_fail_info_bulk") +async def import_fail_info_bulk(request): + try: + json_data = await request.json() + + # Basic validation - ensure we have either cnr_ids or urls + if not isinstance(json_data, dict): + return web.Response(status=400, text="Request body must be a JSON object") + + if "cnr_ids" not in json_data and "urls" not in json_data: + return web.Response( + status=400, text="Either 'cnr_ids' or 'urls' field is required" + ) + + await core.unified_manager.reload('cache') + await core.unified_manager.get_custom_nodes('default', 'cache') + + results = {} + + if "cnr_ids" in json_data: + if not isinstance(json_data["cnr_ids"], list): + return web.Response(status=400, text="'cnr_ids' must be an array") + for cnr_id in json_data["cnr_ids"]: + if not isinstance(cnr_id, str): + results[cnr_id] = {"error": "cnr_id must be a string"} + continue + module_name = core.unified_manager.get_module_name(cnr_id) + if module_name is not None: + info = cm_global.error_dict.get(module_name) + if info is not None: + results[cnr_id] = info + else: + results[cnr_id] = None + else: + results[cnr_id] = None + + if "urls" in json_data: + if not isinstance(json_data["urls"], list): + return web.Response(status=400, text="'urls' must be an array") + for url in json_data["urls"]: + if not isinstance(url, str): + results[url] = {"error": "url must be a string"} + continue + module_name = core.unified_manager.get_module_name(url) + if module_name is not None: + info = cm_global.error_dict.get(module_name) + if info is not None: + results[url] = info + else: + results[url] = None + else: + results[url] = None + + return web.json_response(results) + except Exception as e: + logging.error(f"[ComfyUI-Manager] Error processing bulk import fail info: {e}") + return web.Response(status=500, text="Internal server error") + + +@routes.post("/v2/manager/queue/reinstall") async def reinstall_custom_node(request): await uninstall_custom_node(request) await install_custom_node(request) -@routes.get("/manager/queue/reset") +@routes.get("/v2/manager/queue/reset") async def reset_queue(request): - global task_queue - task_queue = queue.Queue() + global task_batch_queue + global temp_queue_batch + + with task_worker_lock: + temp_queue_batch = [] + task_batch_queue = deque() + return web.Response(status=200) -@routes.get("/manager/queue/status") +@routes.get("/v2/manager/queue/abort_current") +async def abort_queue(request): + global task_batch_queue + global temp_queue_batch + + with task_worker_lock: + temp_queue_batch = [] + if len(task_batch_queue) > 0: + task_batch_queue[0].abort() + task_batch_queue.popleft() + + return web.Response(status=200) + + +@routes.get("/v2/manager/queue/status") async def queue_count(request): global task_queue with task_worker_lock: - done_count = len(nodepack_result) + len(model_result) - in_progress_count = len(tasks_in_progress) - total_count = done_count + in_progress_count + task_queue.qsize() - is_processing = task_worker_thread is not None and task_worker_thread.is_alive() + if len(task_batch_queue) > 0: + cur_batch = task_batch_queue[0] + done_count = cur_batch.done_count() + total_count = cur_batch.total_count() + in_progress_count = len(tasks_in_progress) + is_processing = task_worker_thread is not None and task_worker_thread.is_alive() + else: + done_count = 0 + total_count = 0 + in_progress_count = 0 + is_processing = False return web.json_response({ - 'total_count': total_count, 'done_count': done_count, 'in_progress_count': in_progress_count, + 'total_count': total_count, + 'done_count': done_count, + 'in_progress_count': in_progress_count, 'is_processing': is_processing}) -@routes.post("/manager/queue/install") +@routes.post("/v2/manager/queue/install") async def install_custom_node(request): - if not is_allowed_security_level('middle'): - logging.error(SECURITY_MESSAGE_MIDDLE_OR_BELOW) - return web.Response(status=403, text="A security error has occurred. Please check the terminal logs") - json_data = await request.json() + print(f"install={json_data}") + return await _install_custom_node(json_data) + + +async def _install_custom_node(json_data): + if not is_allowed_security_level('middle+'): + logging.error(SECURITY_MESSAGE_MIDDLE_P) + return web.Response(status=403, text="A security error has occurred. Please check the terminal logs") # non-nightly cnr is safe risky_level = None @@ -1216,8 +1446,10 @@ async def install_custom_node(request): if json_data['version'] != 'unknown' and selected_version != 'unknown': if skip_post_install: if cnr_id in core.unified_manager.nightly_inactive_nodes or cnr_id in core.unified_manager.cnr_inactive_nodes: - core.unified_manager.unified_enable(cnr_id) + enable_item = json_data.get('ui_id'), cnr_id + temp_queue_batch.append(("enable", enable_item)) return web.Response(status=200) + elif selected_version is None: selected_version = 'latest' @@ -1230,9 +1462,11 @@ async def install_custom_node(request): if git_url is None: logging.error(f"[ComfyUI-Manager] Following node pack doesn't provide `nightly` version: ${git_url}") return web.Response(status=404, text=f"Following node pack doesn't provide `nightly` version: ${git_url}") + elif json_data['version'] != 'unknown' and selected_version == 'unknown': logging.error(f"[ComfyUI-Manager] Invalid installation request: {json_data}") return web.Response(status=400, text="Invalid installation request") + else: # unknown unknown_name = os.path.basename(json_data['files'][0]) @@ -1251,39 +1485,42 @@ async def install_custom_node(request): return web.Response(status=404, text="A security error has occurred. Please check the terminal logs") install_item = json_data.get('ui_id'), node_spec_str, json_data['channel'], json_data['mode'], skip_post_install - task_queue.put(("install", install_item)) + temp_queue_batch.append(("install", install_item)) return web.Response(status=200) task_worker_thread:threading.Thread = None -@routes.get("/manager/queue/start") +@routes.get("/v2/manager/queue/start") async def queue_start(request): - global nodepack_result - global model_result + with task_worker_lock: + finalize_temp_queue_batch() + return _queue_start() + +def _queue_start(): global task_worker_thread if task_worker_thread is not None and task_worker_thread.is_alive(): return web.Response(status=201) # already in-progress - nodepack_result = {} - model_result = {} - task_worker_thread = threading.Thread(target=lambda: asyncio.run(task_worker())) task_worker_thread.start() return web.Response(status=200) -@routes.post("/manager/queue/fix") +@routes.post("/v2/manager/queue/fix") async def fix_custom_node(request): + json_data = await request.json() + return await _fix_custom_node(json_data) + + +async def _fix_custom_node(json_data): if not is_allowed_security_level('middle'): logging.error(SECURITY_MESSAGE_GENERAL) return web.Response(status=403, text="A security error has occurred. Please check the terminal logs") - json_data = await request.json() - node_id = json_data.get('id') node_ver = json_data['version'] if node_ver != 'unknown': @@ -1293,14 +1530,14 @@ async def fix_custom_node(request): node_name = os.path.basename(json_data['files'][0]) update_item = json_data.get('ui_id'), node_name, json_data['version'] - task_queue.put(("fix", update_item)) + temp_queue_batch.append(("fix", update_item)) return web.Response(status=200) -@routes.post("/customnode/install/git_url") +@routes.post("/v2/customnode/install/git_url") async def install_custom_node_git_url(request): - if not is_allowed_security_level('high'): + if not is_allowed_security_level('high+'): logging.error(SECURITY_MESSAGE_NORMAL_MINUS) return web.Response(status=403) @@ -1318,9 +1555,9 @@ async def install_custom_node_git_url(request): return web.Response(status=400) -@routes.post("/customnode/install/pip") +@routes.post("/v2/customnode/install/pip") async def install_custom_node_pip(request): - if not is_allowed_security_level('high'): + if not is_allowed_security_level('high+'): logging.error(SECURITY_MESSAGE_NORMAL_MINUS) return web.Response(status=403) @@ -1330,13 +1567,16 @@ async def install_custom_node_pip(request): return web.Response(status=200) -@routes.post("/manager/queue/uninstall") +@routes.post("/v2/manager/queue/uninstall") async def uninstall_custom_node(request): - if not is_allowed_security_level('middle'): - logging.error(SECURITY_MESSAGE_MIDDLE_OR_BELOW) - return web.Response(status=403, text="A security error has occurred. Please check the terminal logs") - json_data = await request.json() + return await _uninstall_custom_node(json_data) + + +async def _uninstall_custom_node(json_data): + if not is_allowed_security_level('middle'): + logging.error(SECURITY_MESSAGE_MIDDLE) + return web.Response(status=403, text="A security error has occurred. Please check the terminal logs") node_id = json_data.get('id') if json_data['version'] != 'unknown': @@ -1348,18 +1588,21 @@ async def uninstall_custom_node(request): node_name = os.path.basename(json_data['files'][0]) uninstall_item = json_data.get('ui_id'), node_name, is_unknown - task_queue.put(("uninstall", uninstall_item)) + temp_queue_batch.append(("uninstall", uninstall_item)) return web.Response(status=200) -@routes.post("/manager/queue/update") +@routes.post("/v2/manager/queue/update") async def update_custom_node(request): - if not is_allowed_security_level('middle'): - logging.error(SECURITY_MESSAGE_MIDDLE_OR_BELOW) - return web.Response(status=403, text="A security error has occurred. Please check the terminal logs") - json_data = await request.json() + return await _update_custom_node(json_data) + + +async def _update_custom_node(json_data): + if not is_allowed_security_level('middle'): + logging.error(SECURITY_MESSAGE_MIDDLE) + return web.Response(status=403, text="A security error has occurred. Please check the terminal logs") node_id = json_data.get('id') if json_data['version'] != 'unknown': @@ -1369,19 +1612,19 @@ async def update_custom_node(request): node_name = os.path.basename(json_data['files'][0]) update_item = json_data.get('ui_id'), node_name, json_data['version'] - task_queue.put(("update", update_item)) + temp_queue_batch.append(("update", update_item)) return web.Response(status=200) -@routes.get("/manager/queue/update_comfyui") +@routes.get("/v2/manager/queue/update_comfyui") async def update_comfyui(request): is_stable = core.get_config()['update_policy'] != 'nightly-comfyui' - task_queue.put(("update-comfyui", ('comfyui', is_stable))) + temp_queue_batch.append(("update-comfyui", ('comfyui', is_stable))) return web.Response(status=200) -@routes.get("/comfyui_manager/comfyui_versions") +@routes.get("/v2/comfyui_manager/comfyui_versions") async def comfyui_versions(request): try: res, current, latest = core.get_comfyui_versions() @@ -1392,7 +1635,7 @@ async def comfyui_versions(request): return web.Response(status=400) -@routes.get("/comfyui_manager/comfyui_switch_version") +@routes.get("/v2/comfyui_manager/comfyui_switch_version") async def comfyui_switch_version(request): try: if "ver" in request.rel_url.query: @@ -1405,10 +1648,14 @@ async def comfyui_switch_version(request): return web.Response(status=400) -@routes.post("/manager/queue/disable") +@routes.post("/v2/manager/queue/disable") async def disable_node(request): json_data = await request.json() + await _disable_node(json_data) + return web.Response(status=200) + +async def _disable_node(json_data): node_id = json_data.get('id') if json_data['version'] != 'unknown': is_unknown = False @@ -1419,9 +1666,7 @@ async def disable_node(request): node_name = os.path.basename(json_data['files'][0]) update_item = json_data.get('ui_id'), node_name, is_unknown - task_queue.put(("disable", update_item)) - - return web.Response(status=200) + temp_queue_batch.append(("disable", update_item)) async def check_whitelist_for_model(item): @@ -1440,12 +1685,15 @@ async def check_whitelist_for_model(item): return False -@routes.post("/manager/queue/install_model") +@routes.post("/v2/manager/queue/install_model") async def install_model(request): json_data = await request.json() + return await _install_model(json_data) - if not is_allowed_security_level('middle'): - logging.error(SECURITY_MESSAGE_MIDDLE_OR_BELOW) + +async def _install_model(json_data): + if not is_allowed_security_level('middle+'): + logging.error(SECURITY_MESSAGE_MIDDLE_P) return web.Response(status=403, text="A security error has occurred. Please check the terminal logs") # validate request @@ -1453,7 +1701,7 @@ async def install_model(request): logging.error(f"[ComfyUI-Manager] Invalid model install request is detected: {json_data}") return web.Response(status=400, text="Invalid model install request is detected") - if not json_data['filename'].endswith('.safetensors') and not is_allowed_security_level('high'): + if not json_data['filename'].endswith('.safetensors') and not is_allowed_security_level('high+'): models_json = await core.get_data_by_mode('cache', 'model-list.json', 'default') is_belongs_to_whitelist = False @@ -1467,12 +1715,12 @@ async def install_model(request): return web.Response(status=403, text="A security error has occurred. Please check the terminal logs") install_item = json_data.get('ui_id'), json_data - task_queue.put(("install-model", install_item)) + temp_queue_batch.append(("install-model", install_item)) return web.Response(status=200) -@routes.get("/manager/preview_method") +@routes.get("/v2/manager/preview_method") async def preview_method(request): if "value" in request.rel_url.query: set_preview_method(request.rel_url.query['value']) @@ -1483,7 +1731,7 @@ async def preview_method(request): return web.Response(status=200) -@routes.get("/manager/db_mode") +@routes.get("/v2/manager/db_mode") async def db_mode(request): if "value" in request.rel_url.query: set_db_mode(request.rel_url.query['value']) @@ -1495,7 +1743,7 @@ async def db_mode(request): -@routes.get("/manager/policy/component") +@routes.get("/v2/manager/policy/component") async def component_policy(request): if "value" in request.rel_url.query: set_component_policy(request.rel_url.query['value']) @@ -1506,7 +1754,7 @@ async def component_policy(request): return web.Response(status=200) -@routes.get("/manager/policy/update") +@routes.get("/v2/manager/policy/update") async def update_policy(request): if "value" in request.rel_url.query: set_update_policy(request.rel_url.query['value']) @@ -1517,7 +1765,7 @@ async def update_policy(request): return web.Response(status=200) -@routes.get("/manager/channel_url_list") +@routes.get("/v2/manager/channel_url_list") async def channel_url_list(request): channels = core.get_channel_dict() if "value" in request.rel_url.query: @@ -1554,7 +1802,7 @@ def add_target_blank(html_text): return modified_html -@routes.get("/manager/notice") +@routes.get("/v2/manager/notice") async def get_notice(request): url = "github.com" path = "/ltdrdata/ltdrdata.github.io/wiki/News" @@ -1574,7 +1822,7 @@ async def get_notice(request): if version_tag is not None: markdown_content += f"
ComfyUI: {version_tag} [Desktop]" else: - version_tag = core.get_comfyui_tag() + version_tag = context.get_comfyui_tag() if version_tag is None: markdown_content += f"
ComfyUI: {core.comfy_ui_revision}[{comfy_ui_hash[:6]}]({core.comfy_ui_commit_datetime.date()})" else: @@ -1591,7 +1839,7 @@ async def get_notice(request): markdown_content = '

Your ComfyUI isn\'t git repo.

' + markdown_content elif core.comfy_ui_required_commit_datetime.date() > core.comfy_ui_commit_datetime.date(): markdown_content = '

Your ComfyUI is too OUTDATED!!!

' + markdown_content - except: + except Exception: pass return web.Response(text=markdown_content, status=200) @@ -1601,10 +1849,16 @@ async def get_notice(request): return web.Response(text="Unable to retrieve Notice", status=200) -@routes.get("/manager/reboot") +# legacy /manager/notice +@routes.get("/manager/notice") +async def get_notice_legacy(request): + return web.Response(text="""Starting from ComfyUI-Manager V4.0+, it should be installed via pip.

Please remove the ComfyUI-Manager installed in the 'custom_nodes' directory.
""", status=200) + + +@routes.get("/v2/manager/reboot") def restart(self): if not is_allowed_security_level('middle'): - logging.error(SECURITY_MESSAGE_MIDDLE_OR_BELOW) + logging.error(SECURITY_MESSAGE_MIDDLE) return web.Response(status=403) try: @@ -1638,22 +1892,22 @@ def restart(self): return os.execv(sys.executable, cmds) -@routes.post("/manager/component/save") +@routes.post("/v2/manager/component/save") async def save_component(request): try: data = await request.json() name = data['name'] workflow = data['workflow'] - if not os.path.exists(core.manager_components_path): - os.mkdir(core.manager_components_path) + if not os.path.exists(context.manager_components_path): + os.mkdir(context.manager_components_path) if 'packname' in workflow and workflow['packname'] != '': sanitized_name = manager_util.sanitize_filename(workflow['packname']) + '.pack' else: sanitized_name = manager_util.sanitize_filename(name) + '.json' - filepath = os.path.join(core.manager_components_path, sanitized_name) + filepath = os.path.join(context.manager_components_path, sanitized_name) components = {} if os.path.exists(filepath): with open(filepath) as f: @@ -1664,20 +1918,20 @@ async def save_component(request): with open(filepath, 'w') as f: json.dump(components, f, indent=4, sort_keys=True) return web.Response(text=filepath, status=200) - except: + except Exception: return web.Response(status=400) -@routes.post("/manager/component/loads") +@routes.post("/v2/manager/component/loads") async def load_components(request): - if os.path.exists(core.manager_components_path): + if os.path.exists(context.manager_components_path): try: - json_files = [f for f in os.listdir(core.manager_components_path) if f.endswith('.json')] - pack_files = [f for f in os.listdir(core.manager_components_path) if f.endswith('.pack')] + json_files = [f for f in os.listdir(context.manager_components_path) if f.endswith('.json')] + pack_files = [f for f in os.listdir(context.manager_components_path) if f.endswith('.pack')] components = {} for json_file in json_files + pack_files: - file_path = os.path.join(core.manager_components_path, json_file) + file_path = os.path.join(context.manager_components_path, json_file) with open(file_path, 'r') as file: try: # When there is a conflict between the .pack and the .json, the pack takes precedence and overrides. @@ -1693,7 +1947,7 @@ async def load_components(request): return web.json_response({}) -@routes.get("/manager/version") +@routes.get("/v2/manager/version") async def get_version(request): return web.Response(text=core.version_str, status=200) @@ -1757,21 +2011,23 @@ async def default_cache_update(): # load at least once await core.unified_manager.reload('remote', dont_wait=False) await core.unified_manager.get_custom_nodes(channel_url, 'remote') + else: + await core.unified_manager.reload('remote', dont_wait=False, update_cnr_map=False) logging.info("[ComfyUI-Manager] All startup tasks have been completed.") threading.Thread(target=lambda: asyncio.run(default_cache_update())).start() -if not os.path.exists(core.manager_config_path): +if not os.path.exists(context.manager_config_path): core.get_config() core.write_config() -cm_global.register_extension('ComfyUI-Manager', - {'version': core.version, - 'name': 'ComfyUI Manager', - 'nodes': {}, - 'description': 'This extension provides the ability to manage custom nodes in ComfyUI.', }) - - +# policy setup +manager_security.add_handler_policy(reinstall_custom_node, manager_security.HANDLER_POLICY.MULTIPLE_REMOTE_BAN_NOT_PERSONAL_CLOUD) +manager_security.add_handler_policy(install_custom_node, manager_security.HANDLER_POLICY.MULTIPLE_REMOTE_BAN_NOT_PERSONAL_CLOUD) +manager_security.add_handler_policy(fix_custom_node, manager_security.HANDLER_POLICY.MULTIPLE_REMOTE_BAN_NOT_PERSONAL_CLOUD) +manager_security.add_handler_policy(install_custom_node_git_url, manager_security.HANDLER_POLICY.MULTIPLE_REMOTE_BAN_NOT_PERSONAL_CLOUD) +manager_security.add_handler_policy(install_custom_node_pip, manager_security.HANDLER_POLICY.MULTIPLE_REMOTE_BAN_NOT_PERSONAL_CLOUD) +manager_security.add_handler_policy(install_model, manager_security.HANDLER_POLICY.MULTIPLE_REMOTE_BAN_NOT_PERSONAL_CLOUD) diff --git a/comfyui_manager/legacy/share_3rdparty.py b/comfyui_manager/legacy/share_3rdparty.py new file mode 100644 index 00000000..9a30619f --- /dev/null +++ b/comfyui_manager/legacy/share_3rdparty.py @@ -0,0 +1,451 @@ +import mimetypes +from ..common import context +from . import manager_core as core + +import os +from aiohttp import web +import aiohttp +import json +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\n") + matrix_nio_is_available = False + + +def extract_model_file_names(json_data): + """Extract unique file names from the input JSON data.""" + file_names = set() + model_filename_extensions = {'.safetensors', '.ckpt', '.pt', '.pth', '.bin'} + + # Recursively search for file names in the JSON data + def recursive_search(data): + if isinstance(data, dict): + for value in data.values(): + recursive_search(value) + elif isinstance(data, list): + for item in data: + recursive_search(item) + elif isinstance(data, str) and '.' in data: + file_names.add(os.path.basename(data)) # file_names.add(data) + + recursive_search(json_data) + return [f for f in list(file_names) if os.path.splitext(f)[1] in model_filename_extensions] + + +def find_file_paths(base_dir, file_names): + """Find the paths of the files in the base directory.""" + file_paths = {} + + for root, dirs, files in os.walk(base_dir): + # Exclude certain directories + dirs[:] = [d for d in dirs if d not in ['.git']] + + for file in files: + if file in file_names: + file_paths[file] = os.path.join(root, file) + return file_paths + + +def compute_sha256_checksum(filepath): + """Compute the SHA256 checksum of a file, in chunks""" + sha256 = hashlib.sha256() + with open(filepath, 'rb') as f: + for chunk in iter(lambda: f.read(4096), b''): + sha256.update(chunk) + return sha256.hexdigest() + + +@PromptServer.instance.routes.get("/v2/manager/share_option") +async def share_option(request): + if "value" in request.rel_url.query: + core.get_config()['share_option'] = request.rel_url.query['value'] + core.write_config() + else: + return web.Response(text=core.get_config()['share_option'], status=200) + + return web.Response(status=200) + + +def get_openart_auth(): + if not os.path.exists(os.path.join(context.manager_files_path, ".openart_key")): + return None + try: + with open(os.path.join(context.manager_files_path, ".openart_key"), "r") as f: + openart_key = f.read().strip() + return openart_key if openart_key else None + except Exception: + return None + + +def get_matrix_auth(): + if not os.path.exists(os.path.join(context.manager_files_path, "matrix_auth")): + return None + try: + with open(os.path.join(context.manager_files_path, "matrix_auth"), "r") as f: + matrix_auth = f.read() + homeserver, username, password = matrix_auth.strip().split("\n") + if not homeserver or not username or not password: + return None + return { + "homeserver": homeserver, + "username": username, + "password": password, + } + except Exception: + return None + + +def get_comfyworkflows_auth(): + if not os.path.exists(os.path.join(context.manager_files_path, "comfyworkflows_sharekey")): + return None + try: + with open(os.path.join(context.manager_files_path, "comfyworkflows_sharekey"), "r") as f: + share_key = f.read() + if not share_key.strip(): + return None + return share_key + except Exception: + return None + + +def get_youml_settings(): + if not os.path.exists(os.path.join(context.manager_files_path, ".youml")): + return None + try: + with open(os.path.join(context.manager_files_path, ".youml"), "r") as f: + youml_settings = f.read().strip() + return youml_settings if youml_settings else None + except Exception: + return None + + +def set_youml_settings(settings): + with open(os.path.join(context.manager_files_path, ".youml"), "w") as f: + f.write(settings) + + +@PromptServer.instance.routes.get("/v2/manager/get_openart_auth") +async def api_get_openart_auth(request): + # print("Getting stored Matrix credentials...") + openart_key = get_openart_auth() + if not openart_key: + return web.Response(status=404) + return web.json_response({"openart_key": openart_key}) + + +@PromptServer.instance.routes.post("/v2/manager/set_openart_auth") +async def api_set_openart_auth(request): + json_data = await request.json() + openart_key = json_data['openart_key'] + with open(os.path.join(context.manager_files_path, ".openart_key"), "w") as f: + f.write(openart_key) + return web.Response(status=200) + + +@PromptServer.instance.routes.get("/v2/manager/get_matrix_auth") +async def api_get_matrix_auth(request): + # print("Getting stored Matrix credentials...") + matrix_auth = get_matrix_auth() + if not matrix_auth: + return web.Response(status=404) + return web.json_response(matrix_auth) + + +@PromptServer.instance.routes.get("/v2/manager/youml/settings") +async def api_get_youml_settings(request): + youml_settings = get_youml_settings() + if not youml_settings: + return web.Response(status=404) + return web.json_response(json.loads(youml_settings)) + + +@PromptServer.instance.routes.post("/v2/manager/youml/settings") +async def api_set_youml_settings(request): + json_data = await request.json() + set_youml_settings(json.dumps(json_data)) + return web.Response(status=200) + + +@PromptServer.instance.routes.get("/v2/manager/get_comfyworkflows_auth") +async def api_get_comfyworkflows_auth(request): + # Check if the user has provided Matrix credentials in a file called 'matrix_accesstoken' + # in the same directory as the ComfyUI base folder + # print("Getting stored Comfyworkflows.com auth...") + comfyworkflows_auth = get_comfyworkflows_auth() + if not comfyworkflows_auth: + return web.Response(status=404) + return web.json_response({"comfyworkflows_sharekey": comfyworkflows_auth}) + + +@PromptServer.instance.routes.post("/v2/manager/set_esheep_workflow_and_images") +async def set_esheep_workflow_and_images(request): + json_data = await request.json() + with open(os.path.join(context.manager_files_path, "esheep_share_message.json"), "w", encoding='utf-8') as file: + json.dump(json_data, file, indent=4) + return web.Response(status=200) + + +@PromptServer.instance.routes.get("/v2/manager/get_esheep_workflow_and_images") +async def get_esheep_workflow_and_images(request): + with open(os.path.join(context.manager_files_path, "esheep_share_message.json"), 'r', encoding='utf-8') as file: + data = json.load(file) + 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'] + password = json_data['password'] + with open(os.path.join(context.manager_files_path, "matrix_auth"), "w") as f: + f.write("\n".join([homeserver, username, password])) + + +def set_comfyworkflows_auth(comfyworkflows_sharekey): + with open(os.path.join(context.manager_files_path, "comfyworkflows_sharekey"), "w") as f: + f.write(comfyworkflows_sharekey) + + +def has_provided_matrix_auth(matrix_auth): + return matrix_auth['homeserver'].strip() and matrix_auth['username'].strip() and matrix_auth['password'].strip() + + +def has_provided_comfyworkflows_auth(comfyworkflows_sharekey): + return comfyworkflows_sharekey.strip() + + +@PromptServer.instance.routes.post("/v2/manager/share") +async def share_art(request): + # get json data + json_data = await request.json() + + matrix_auth = json_data['matrix_auth'] + comfyworkflows_sharekey = json_data['cw_auth']['cw_sharekey'] + + set_matrix_auth(matrix_auth) + set_comfyworkflows_auth(comfyworkflows_sharekey) + + share_destinations = json_data['share_destinations'] + credits = json_data['credits'] + title = json_data['title'] + description = json_data['description'] + is_nsfw = json_data['is_nsfw'] + prompt = json_data['prompt'] + potential_outputs = json_data['potential_outputs'] + selected_output_index = json_data['selected_output_index'] + + try: + output_to_share = potential_outputs[int(selected_output_index)] + except Exception: + # for now, pick the first output + output_to_share = potential_outputs[0] + + assert output_to_share['type'] in ('image', 'output') + output_dir = folder_paths.get_output_directory() + + if output_to_share['type'] == 'image': + asset_filename = output_to_share['image']['filename'] + asset_subfolder = output_to_share['image']['subfolder'] + + if output_to_share['image']['type'] == 'temp': + output_dir = folder_paths.get_temp_directory() + else: + asset_filename = output_to_share['output']['filename'] + asset_subfolder = output_to_share['output']['subfolder'] + + if asset_subfolder: + asset_filepath = os.path.join(output_dir, asset_subfolder, asset_filename) + else: + asset_filepath = os.path.join(output_dir, asset_filename) + + # get the mime type of the asset + assetFileType = mimetypes.guess_type(asset_filepath)[0] + + share_website_host = "UNKNOWN" + if "comfyworkflows" in share_destinations: + share_website_host = "https://comfyworkflows.com" + share_endpoint = f"{share_website_host}/api" + + # get presigned urls + async with aiohttp.ClientSession(trust_env=True, connector=aiohttp.TCPConnector(verify_ssl=False)) as session: + async with session.post( + f"{share_endpoint}/get_presigned_urls", + json={ + "assetFileName": asset_filename, + "assetFileType": assetFileType, + "workflowJsonFileName": 'workflow.json', + "workflowJsonFileType": 'application/json', + }, + ) as resp: + assert resp.status == 200 + presigned_urls_json = await resp.json() + assetFilePresignedUrl = presigned_urls_json["assetFilePresignedUrl"] + assetFileKey = presigned_urls_json["assetFileKey"] + workflowJsonFilePresignedUrl = presigned_urls_json["workflowJsonFilePresignedUrl"] + workflowJsonFileKey = presigned_urls_json["workflowJsonFileKey"] + + # upload asset + async with aiohttp.ClientSession(trust_env=True, connector=aiohttp.TCPConnector(verify_ssl=False)) as session: + async with session.put(assetFilePresignedUrl, data=open(asset_filepath, "rb")) as resp: + assert resp.status == 200 + + # upload workflow json + async with aiohttp.ClientSession(trust_env=True, connector=aiohttp.TCPConnector(verify_ssl=False)) as session: + async with session.put(workflowJsonFilePresignedUrl, data=json.dumps(prompt['workflow']).encode('utf-8')) as resp: + assert resp.status == 200 + + model_filenames = extract_model_file_names(prompt['workflow']) + model_file_paths = find_file_paths(folder_paths.base_path, model_filenames) + + models_info = {} + for filename, filepath in model_file_paths.items(): + models_info[filename] = { + "filename": filename, + "sha256_checksum": compute_sha256_checksum(filepath), + "relative_path": os.path.relpath(filepath, folder_paths.base_path), + } + + # make a POST request to /api/upload_workflow with form data key values + async with aiohttp.ClientSession(trust_env=True, connector=aiohttp.TCPConnector(verify_ssl=False)) as session: + form = aiohttp.FormData() + if comfyworkflows_sharekey: + form.add_field("shareKey", comfyworkflows_sharekey) + form.add_field("source", "comfyui_manager") + form.add_field("assetFileKey", assetFileKey) + form.add_field("assetFileType", assetFileType) + form.add_field("workflowJsonFileKey", workflowJsonFileKey) + form.add_field("sharedWorkflowWorkflowJsonString", json.dumps(prompt['workflow'])) + form.add_field("sharedWorkflowPromptJsonString", json.dumps(prompt['output'])) + form.add_field("shareWorkflowCredits", credits) + form.add_field("shareWorkflowTitle", title) + form.add_field("shareWorkflowDescription", description) + form.add_field("shareWorkflowIsNSFW", str(is_nsfw).lower()) + form.add_field("currentSnapshot", json.dumps(await core.get_current_snapshot())) + form.add_field("modelsInfo", json.dumps(models_info)) + + async with session.post( + f"{share_endpoint}/upload_workflow", + data=form, + ) as resp: + assert resp.status == 200 + upload_workflow_json = await resp.json() + workflowId = upload_workflow_json["workflowId"] + + # check if the user has provided Matrix credentials + 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: + homeserver = 'matrix.org' + if matrix_auth: + homeserver = matrix_auth.get('homeserver', 'matrix.org') + homeserver = homeserver.replace("http://", "https://") + if not homeserver.startswith("https://"): + homeserver = "https://" + homeserver + + 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) + + # Upload asset + with open(asset_filepath, 'rb') as f: + 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 + + # 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" + if description: + text_content += f"{description}\n" + if credits: + text_content += f"\ncredits: {credits}\n" + 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({ + "comfyworkflows": { + "url": None if "comfyworkflows" not in share_destinations else f"{share_website_host}/workflows/{workflowId}", + }, + "matrix": { + "success": None if "matrix" not in share_destinations else True + } + }, content_type='application/json', status=200) diff --git a/model-list.json b/comfyui_manager/model-list.json similarity index 100% rename from model-list.json rename to comfyui_manager/model-list.json diff --git a/prestartup_script.py b/comfyui_manager/prestartup_script.py similarity index 93% rename from prestartup_script.py rename to comfyui_manager/prestartup_script.py index f3e52ea4..8e0dd8ef 100644 --- a/prestartup_script.py +++ b/comfyui_manager/prestartup_script.py @@ -12,13 +12,10 @@ import ast import logging import traceback -glob_path = os.path.join(os.path.dirname(__file__), "glob") -sys.path.append(glob_path) - -import security_check -import manager_util -import cm_global -import manager_downloader +from .common import security_check +from .common import manager_util +from .common import cm_global +from .common import manager_downloader import folder_paths manager_util.add_python_path_to_env() @@ -66,16 +63,14 @@ def is_import_failed_extension(name): comfy_path = os.environ.get('COMFYUI_PATH') comfy_base_path = os.environ.get('COMFYUI_FOLDERS_BASE_PATH') -if comfy_path is None: - # legacy env var - comfy_path = os.environ.get('COMFYUI_PATH') - if comfy_path is None: comfy_path = os.path.abspath(os.path.dirname(sys.modules['__main__'].__file__)) + os.environ['COMFYUI_PATH'] = comfy_path if comfy_base_path is None: comfy_base_path = comfy_path + sys.__comfyui_manager_register_message_collapse = register_message_collapse sys.__comfyui_manager_is_import_failed_extension = is_import_failed_extension cm_global.register_api('cm.register_message_collapse', register_message_collapse) @@ -91,9 +86,6 @@ manager_pip_blacklist_path = os.path.join(manager_files_path, "pip_blacklist.lis restore_snapshot_path = os.path.join(manager_files_path, "startup-scripts", "restore-snapshot.json") manager_config_path = os.path.join(manager_files_path, 'config.ini') -cm_cli_path = os.path.join(comfyui_manager_path, "cm-cli.py") - - default_conf = {} def read_config(): @@ -400,7 +392,11 @@ try: def emit(self, record): global is_start_mode - message = record.getMessage() + try: + message = record.getMessage() + except Exception as e: + message = f"<>: {record} - {e}" + original_stderr.write(message) if is_start_mode: match = re.search(pat_import_fail, message) @@ -443,35 +439,6 @@ except Exception as e: print(f"[ComfyUI-Manager] Logging failed: {e}") -def ensure_dependencies(): - try: - import git # noqa: F401 - import toml # noqa: F401 - import rich # noqa: F401 - import chardet # noqa: F401 - except ModuleNotFoundError: - my_path = os.path.dirname(__file__) - requirements_path = os.path.join(my_path, "requirements.txt") - - print("## ComfyUI-Manager: installing dependencies. (GitPython)") - try: - subprocess.check_output(manager_util.make_pip_cmd(['install', '-r', requirements_path])) - except subprocess.CalledProcessError: - print("## [ERROR] ComfyUI-Manager: Attempting to reinstall dependencies using an alternative method.") - try: - subprocess.check_output(manager_util.make_pip_cmd(['install', '--user', '-r', requirements_path])) - except subprocess.CalledProcessError: - print("## [ERROR] ComfyUI-Manager: Failed to install the GitPython package in the correct Python environment. Please install it manually in the appropriate environment. (You can seek help at https://app.element.io/#/room/%23comfyui_space%3Amatrix.org)") - - try: - print("## ComfyUI-Manager: installing dependencies done.") - except: - # maybe we should sys.exit() here? there is at least two screens worth of error messages still being pumped after our error messages - print("## [ERROR] ComfyUI-Manager: GitPython package seems to be installed, but failed to load somehow. Make sure you have a working git client installed") - -ensure_dependencies() - - print("** ComfyUI startup time:", current_timestamp()) print("** Platform:", platform.system()) print("** Python version:", sys.version) @@ -495,7 +462,7 @@ def read_downgrade_blacklist(): items = [x.strip() for x in items if x != ''] cm_global.pip_downgrade_blacklist += items cm_global.pip_downgrade_blacklist = list(set(cm_global.pip_downgrade_blacklist)) - except: + except Exception: pass @@ -601,7 +568,10 @@ if os.path.exists(restore_snapshot_path): if 'COMFYUI_FOLDERS_BASE_PATH' not in new_env: new_env["COMFYUI_FOLDERS_BASE_PATH"] = comfy_path - cmd_str = [sys.executable, cm_cli_path, 'restore-snapshot', restore_snapshot_path] + if 'COMFYUI_PATH' not in new_env: + new_env['COMFYUI_PATH'] = os.path.dirname(folder_paths.__file__) + + cmd_str = [sys.executable, '-m', 'comfyui_manager.cm_cli', 'restore-snapshot', restore_snapshot_path] exit_code = process_wrap(cmd_str, custom_nodes_base_path, handler=msg_capture, env=new_env) if exit_code != 0: diff --git a/notebooks/comfyui_colab_with_manager.ipynb b/notebooks/comfyui_colab_with_manager.ipynb deleted file mode 100644 index 3cfa484b..00000000 --- a/notebooks/comfyui_colab_with_manager.ipynb +++ /dev/null @@ -1,373 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "id": "aaaaaaaaaa" - }, - "source": [ - "Git clone the repo and install the requirements. (ignore the pip errors about protobuf)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "bbbbbbbbbb" - }, - "outputs": [], - "source": [ - "# #@title Environment Setup\n", - "\n", - "from pathlib import Path\n", - "\n", - "OPTIONS = {}\n", - "\n", - "USE_GOOGLE_DRIVE = True #@param {type:\"boolean\"}\n", - "UPDATE_COMFY_UI = True #@param {type:\"boolean\"}\n", - "USE_COMFYUI_MANAGER = True #@param {type:\"boolean\"}\n", - "INSTALL_CUSTOM_NODES_DEPENDENCIES = True #@param {type:\"boolean\"}\n", - "OPTIONS['USE_GOOGLE_DRIVE'] = USE_GOOGLE_DRIVE\n", - "OPTIONS['UPDATE_COMFY_UI'] = UPDATE_COMFY_UI\n", - "OPTIONS['USE_COMFYUI_MANAGER'] = USE_COMFYUI_MANAGER\n", - "OPTIONS['INSTALL_CUSTOM_NODES_DEPENDENCIES'] = INSTALL_CUSTOM_NODES_DEPENDENCIES\n", - "\n", - "current_dir = !pwd\n", - "WORKSPACE = f\"{current_dir[0]}/ComfyUI\"\n", - "\n", - "if OPTIONS['USE_GOOGLE_DRIVE']:\n", - " !echo \"Mounting Google Drive...\"\n", - " %cd /\n", - "\n", - " from google.colab import drive\n", - " drive.mount('/content/drive')\n", - "\n", - " WORKSPACE = \"/content/drive/MyDrive/ComfyUI\"\n", - " %cd /content/drive/MyDrive\n", - "\n", - "![ ! -d $WORKSPACE ] && echo -= Initial setup ComfyUI =- && git clone https://github.com/comfyanonymous/ComfyUI\n", - "%cd $WORKSPACE\n", - "\n", - "if OPTIONS['UPDATE_COMFY_UI']:\n", - " !echo -= Updating ComfyUI =-\n", - "\n", - " # Correction of the issue of permissions being deleted on Google Drive.\n", - " ![ -f \".ci/nightly/update_windows/update_comfyui_and_python_dependencies.bat\" ] && chmod 755 .ci/nightly/update_windows/update_comfyui_and_python_dependencies.bat\n", - " ![ -f \".ci/nightly/windows_base_files/run_nvidia_gpu.bat\" ] && chmod 755 .ci/nightly/windows_base_files/run_nvidia_gpu.bat\n", - " ![ -f \".ci/update_windows/update_comfyui_and_python_dependencies.bat\" ] && chmod 755 .ci/update_windows/update_comfyui_and_python_dependencies.bat\n", - " ![ -f \".ci/update_windows_cu118/update_comfyui_and_python_dependencies.bat\" ] && chmod 755 .ci/update_windows_cu118/update_comfyui_and_python_dependencies.bat\n", - " ![ -f \".ci/update_windows/update.py\" ] && chmod 755 .ci/update_windows/update.py\n", - " ![ -f \".ci/update_windows/update_comfyui.bat\" ] && chmod 755 .ci/update_windows/update_comfyui.bat\n", - " ![ -f \".ci/update_windows/README_VERY_IMPORTANT.txt\" ] && chmod 755 .ci/update_windows/README_VERY_IMPORTANT.txt\n", - " ![ -f \".ci/update_windows/run_cpu.bat\" ] && chmod 755 .ci/update_windows/run_cpu.bat\n", - " ![ -f \".ci/update_windows/run_nvidia_gpu.bat\" ] && chmod 755 .ci/update_windows/run_nvidia_gpu.bat\n", - "\n", - " !git pull\n", - "\n", - "!echo -= Install dependencies =-\n", - "!pip3 install accelerate\n", - "!pip3 install einops transformers>=4.28.1 safetensors>=0.4.2 aiohttp pyyaml Pillow scipy tqdm psutil tokenizers>=0.13.3\n", - "!pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121\n", - "!pip3 install torchsde\n", - "!pip3 install kornia>=0.7.1 spandrel soundfile sentencepiece\n", - "\n", - "if OPTIONS['USE_COMFYUI_MANAGER']:\n", - " %cd custom_nodes\n", - "\n", - " # Correction of the issue of permissions being deleted on Google Drive.\n", - " ![ -f \"ComfyUI-Manager/check.sh\" ] && chmod 755 ComfyUI-Manager/check.sh\n", - " ![ -f \"ComfyUI-Manager/scan.sh\" ] && chmod 755 ComfyUI-Manager/scan.sh\n", - " ![ -f \"ComfyUI-Manager/node_db/dev/scan.sh\" ] && chmod 755 ComfyUI-Manager/node_db/dev/scan.sh\n", - " ![ -f \"ComfyUI-Manager/node_db/tutorial/scan.sh\" ] && chmod 755 ComfyUI-Manager/node_db/tutorial/scan.sh\n", - " ![ -f \"ComfyUI-Manager/scripts/install-comfyui-venv-linux.sh\" ] && chmod 755 ComfyUI-Manager/scripts/install-comfyui-venv-linux.sh\n", - " ![ -f \"ComfyUI-Manager/scripts/install-comfyui-venv-win.bat\" ] && chmod 755 ComfyUI-Manager/scripts/install-comfyui-venv-win.bat\n", - "\n", - " ![ ! -d ComfyUI-Manager ] && echo -= Initial setup ComfyUI-Manager =- && git clone https://github.com/ltdrdata/ComfyUI-Manager\n", - " %cd ComfyUI-Manager\n", - " !git pull\n", - "\n", - "%cd $WORKSPACE\n", - "\n", - "if OPTIONS['INSTALL_CUSTOM_NODES_DEPENDENCIES']:\n", - " !echo -= Install custom nodes dependencies =-\n", - " !pip install GitPython\n", - " !python custom_nodes/ComfyUI-Manager/cm-cli.py restore-dependencies\n" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "cccccccccc" - }, - "source": [ - "Download some models/checkpoints/vae or custom comfyui nodes (uncomment the commands for the ones you want)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "dddddddddd" - }, - "outputs": [], - "source": [ - "# Checkpoints\n", - "\n", - "### SDXL\n", - "### I recommend these workflow examples: https://comfyanonymous.github.io/ComfyUI_examples/sdxl/\n", - "\n", - "#!wget -c https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0/resolve/main/sd_xl_base_1.0.safetensors -P ./models/checkpoints/\n", - "#!wget -c https://huggingface.co/stabilityai/stable-diffusion-xl-refiner-1.0/resolve/main/sd_xl_refiner_1.0.safetensors -P ./models/checkpoints/\n", - "\n", - "# SDXL ReVision\n", - "#!wget -c https://huggingface.co/comfyanonymous/clip_vision_g/resolve/main/clip_vision_g.safetensors -P ./models/clip_vision/\n", - "\n", - "# SD1.5\n", - "!wget -c https://huggingface.co/runwayml/stable-diffusion-v1-5/resolve/main/v1-5-pruned-emaonly.ckpt -P ./models/checkpoints/\n", - "\n", - "# SD2\n", - "#!wget -c https://huggingface.co/stabilityai/stable-diffusion-2-1-base/resolve/main/v2-1_512-ema-pruned.safetensors -P ./models/checkpoints/\n", - "#!wget -c https://huggingface.co/stabilityai/stable-diffusion-2-1/resolve/main/v2-1_768-ema-pruned.safetensors -P ./models/checkpoints/\n", - "\n", - "# Some SD1.5 anime style\n", - "#!wget -c https://huggingface.co/WarriorMama777/OrangeMixs/resolve/main/Models/AbyssOrangeMix2/AbyssOrangeMix2_hard.safetensors -P ./models/checkpoints/\n", - "#!wget -c https://huggingface.co/WarriorMama777/OrangeMixs/resolve/main/Models/AbyssOrangeMix3/AOM3A1_orangemixs.safetensors -P ./models/checkpoints/\n", - "#!wget -c https://huggingface.co/WarriorMama777/OrangeMixs/resolve/main/Models/AbyssOrangeMix3/AOM3A3_orangemixs.safetensors -P ./models/checkpoints/\n", - "#!wget -c https://huggingface.co/Linaqruf/anything-v3.0/resolve/main/anything-v3-fp16-pruned.safetensors -P ./models/checkpoints/\n", - "\n", - "# Waifu Diffusion 1.5 (anime style SD2.x 768-v)\n", - "#!wget -c https://huggingface.co/waifu-diffusion/wd-1-5-beta3/resolve/main/wd-illusion-fp16.safetensors -P ./models/checkpoints/\n", - "\n", - "\n", - "# unCLIP models\n", - "#!wget -c https://huggingface.co/comfyanonymous/illuminatiDiffusionV1_v11_unCLIP/resolve/main/illuminatiDiffusionV1_v11-unclip-h-fp16.safetensors -P ./models/checkpoints/\n", - "#!wget -c https://huggingface.co/comfyanonymous/wd-1.5-beta2_unCLIP/resolve/main/wd-1-5-beta2-aesthetic-unclip-h-fp16.safetensors -P ./models/checkpoints/\n", - "\n", - "\n", - "# VAE\n", - "!wget -c https://huggingface.co/stabilityai/sd-vae-ft-mse-original/resolve/main/vae-ft-mse-840000-ema-pruned.safetensors -P ./models/vae/\n", - "#!wget -c https://huggingface.co/WarriorMama777/OrangeMixs/resolve/main/VAEs/orangemix.vae.pt -P ./models/vae/\n", - "#!wget -c https://huggingface.co/hakurei/waifu-diffusion-v1-4/resolve/main/vae/kl-f8-anime2.ckpt -P ./models/vae/\n", - "\n", - "\n", - "# Loras\n", - "#!wget -c https://civitai.com/api/download/models/10350 -O ./models/loras/theovercomer8sContrastFix_sd21768.safetensors #theovercomer8sContrastFix SD2.x 768-v\n", - "#!wget -c https://civitai.com/api/download/models/10638 -O ./models/loras/theovercomer8sContrastFix_sd15.safetensors #theovercomer8sContrastFix SD1.x\n", - "#!wget -c https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0/resolve/main/sd_xl_offset_example-lora_1.0.safetensors -P ./models/loras/ #SDXL offset noise lora\n", - "\n", - "\n", - "# T2I-Adapter\n", - "#!wget -c https://huggingface.co/TencentARC/T2I-Adapter/resolve/main/models/t2iadapter_depth_sd14v1.pth -P ./models/controlnet/\n", - "#!wget -c https://huggingface.co/TencentARC/T2I-Adapter/resolve/main/models/t2iadapter_seg_sd14v1.pth -P ./models/controlnet/\n", - "#!wget -c https://huggingface.co/TencentARC/T2I-Adapter/resolve/main/models/t2iadapter_sketch_sd14v1.pth -P ./models/controlnet/\n", - "#!wget -c https://huggingface.co/TencentARC/T2I-Adapter/resolve/main/models/t2iadapter_keypose_sd14v1.pth -P ./models/controlnet/\n", - "#!wget -c https://huggingface.co/TencentARC/T2I-Adapter/resolve/main/models/t2iadapter_openpose_sd14v1.pth -P ./models/controlnet/\n", - "#!wget -c https://huggingface.co/TencentARC/T2I-Adapter/resolve/main/models/t2iadapter_color_sd14v1.pth -P ./models/controlnet/\n", - "#!wget -c https://huggingface.co/TencentARC/T2I-Adapter/resolve/main/models/t2iadapter_canny_sd14v1.pth -P ./models/controlnet/\n", - "\n", - "# T2I Styles Model\n", - "#!wget -c https://huggingface.co/TencentARC/T2I-Adapter/resolve/main/models/t2iadapter_style_sd14v1.pth -P ./models/style_models/\n", - "\n", - "# CLIPVision model (needed for styles model)\n", - "#!wget -c https://huggingface.co/openai/clip-vit-large-patch14/resolve/main/pytorch_model.bin -O ./models/clip_vision/clip_vit14.bin\n", - "\n", - "\n", - "# ControlNet\n", - "#!wget -c https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11e_sd15_ip2p_fp16.safetensors -P ./models/controlnet/\n", - "#!wget -c https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11e_sd15_shuffle_fp16.safetensors -P ./models/controlnet/\n", - "#!wget -c https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11p_sd15_canny_fp16.safetensors -P ./models/controlnet/\n", - "#!wget -c https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11f1p_sd15_depth_fp16.safetensors -P ./models/controlnet/\n", - "#!wget -c https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11p_sd15_inpaint_fp16.safetensors -P ./models/controlnet/\n", - "#!wget -c https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11p_sd15_lineart_fp16.safetensors -P ./models/controlnet/\n", - "#!wget -c https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11p_sd15_mlsd_fp16.safetensors -P ./models/controlnet/\n", - "#!wget -c https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11p_sd15_normalbae_fp16.safetensors -P ./models/controlnet/\n", - "#!wget -c https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11p_sd15_openpose_fp16.safetensors -P ./models/controlnet/\n", - "#!wget -c https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11p_sd15_scribble_fp16.safetensors -P ./models/controlnet/\n", - "#!wget -c https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11p_sd15_seg_fp16.safetensors -P ./models/controlnet/\n", - "#!wget -c https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11p_sd15_softedge_fp16.safetensors -P ./models/controlnet/\n", - "#!wget -c https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11p_sd15s2_lineart_anime_fp16.safetensors -P ./models/controlnet/\n", - "#!wget -c https://huggingface.co/comfyanonymous/ControlNet-v1-1_fp16_safetensors/resolve/main/control_v11u_sd15_tile_fp16.safetensors -P ./models/controlnet/\n", - "\n", - "# ControlNet SDXL\n", - "#!wget -c https://huggingface.co/stabilityai/control-lora/resolve/main/control-LoRAs-rank256/control-lora-canny-rank256.safetensors -P ./models/controlnet/\n", - "#!wget -c https://huggingface.co/stabilityai/control-lora/resolve/main/control-LoRAs-rank256/control-lora-depth-rank256.safetensors -P ./models/controlnet/\n", - "#!wget -c https://huggingface.co/stabilityai/control-lora/resolve/main/control-LoRAs-rank256/control-lora-recolor-rank256.safetensors -P ./models/controlnet/\n", - "#!wget -c https://huggingface.co/stabilityai/control-lora/resolve/main/control-LoRAs-rank256/control-lora-sketch-rank256.safetensors -P ./models/controlnet/\n", - "\n", - "# Controlnet Preprocessor nodes by Fannovel16\n", - "#!cd custom_nodes && git clone https://github.com/Fannovel16/comfy_controlnet_preprocessors; cd comfy_controlnet_preprocessors && python install.py\n", - "\n", - "\n", - "# GLIGEN\n", - "#!wget -c https://huggingface.co/comfyanonymous/GLIGEN_pruned_safetensors/resolve/main/gligen_sd14_textbox_pruned_fp16.safetensors -P ./models/gligen/\n", - "\n", - "\n", - "# ESRGAN upscale model\n", - "#!wget -c https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth -P ./models/upscale_models/\n", - "#!wget -c https://huggingface.co/sberbank-ai/Real-ESRGAN/resolve/main/RealESRGAN_x2.pth -P ./models/upscale_models/\n", - "#!wget -c https://huggingface.co/sberbank-ai/Real-ESRGAN/resolve/main/RealESRGAN_x4.pth -P ./models/upscale_models/\n", - "\n", - "\n" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "kkkkkkkkkkkkkkk" - }, - "source": [ - "### Run ComfyUI with cloudflared (Recommended Way)\n", - "\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "jjjjjjjjjjjjjj" - }, - "outputs": [], - "source": [ - "!wget -P ~ https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb\n", - "!dpkg -i ~/cloudflared-linux-amd64.deb\n", - "\n", - "import subprocess\n", - "import threading\n", - "import time\n", - "import socket\n", - "import urllib.request\n", - "\n", - "def iframe_thread(port):\n", - " while True:\n", - " time.sleep(0.5)\n", - " sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n", - " result = sock.connect_ex(('127.0.0.1', port))\n", - " if result == 0:\n", - " break\n", - " sock.close()\n", - " print(\"\\nComfyUI finished loading, trying to launch cloudflared (if it gets stuck here cloudflared is having issues)\\n\")\n", - "\n", - " p = subprocess.Popen([\"cloudflared\", \"tunnel\", \"--url\", \"http://127.0.0.1:{}\".format(port)], stdout=subprocess.PIPE, stderr=subprocess.PIPE)\n", - " for line in p.stderr:\n", - " l = line.decode()\n", - " if \"trycloudflare.com \" in l:\n", - " print(\"This is the URL to access ComfyUI:\", l[l.find(\"http\"):], end='')\n", - " #print(l, end='')\n", - "\n", - "\n", - "threading.Thread(target=iframe_thread, daemon=True, args=(8188,)).start()\n", - "\n", - "!python main.py --dont-print-server" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "kkkkkkkkkkkkkk" - }, - "source": [ - "### Run ComfyUI with localtunnel\n", - "\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "jjjjjjjjjjjjj" - }, - "outputs": [], - "source": [ - "!npm install -g localtunnel\n", - "\n", - "import subprocess\n", - "import threading\n", - "import time\n", - "import socket\n", - "import urllib.request\n", - "\n", - "def iframe_thread(port):\n", - " while True:\n", - " time.sleep(0.5)\n", - " sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n", - " result = sock.connect_ex(('127.0.0.1', port))\n", - " if result == 0:\n", - " break\n", - " sock.close()\n", - " print(\"\\nComfyUI finished loading, trying to launch localtunnel (if it gets stuck here localtunnel is having issues)\\n\")\n", - "\n", - " print(\"The password/enpoint ip for localtunnel is:\", urllib.request.urlopen('https://ipv4.icanhazip.com').read().decode('utf8').strip(\"\\n\"))\n", - " p = subprocess.Popen([\"lt\", \"--port\", \"{}\".format(port)], stdout=subprocess.PIPE)\n", - " for line in p.stdout:\n", - " print(line.decode(), end='')\n", - "\n", - "\n", - "threading.Thread(target=iframe_thread, daemon=True, args=(8188,)).start()\n", - "\n", - "!python main.py --dont-print-server" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "gggggggggg" - }, - "source": [ - "### Run ComfyUI with colab iframe (use only in case the previous way with localtunnel doesn't work)\n", - "\n", - "You should see the ui appear in an iframe. If you get a 403 error, it's your firefox settings or an extension that's messing things up.\n", - "\n", - "If you want to open it in another window use the link.\n", - "\n", - "Note that some UI features like live image previews won't work because the colab iframe blocks websockets." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "hhhhhhhhhh" - }, - "outputs": [], - "source": [ - "import threading\n", - "import time\n", - "import socket\n", - "def iframe_thread(port):\n", - " while True:\n", - " time.sleep(0.5)\n", - " sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n", - " result = sock.connect_ex(('127.0.0.1', port))\n", - " if result == 0:\n", - " break\n", - " sock.close()\n", - " from google.colab import output\n", - " output.serve_kernel_port_as_iframe(port, height=1024)\n", - " print(\"to open it in a window you can open this link here:\")\n", - " output.serve_kernel_port_as_window(port)\n", - "\n", - "threading.Thread(target=iframe_thread, daemon=True, args=(8188,)).start()\n", - "\n", - "!python main.py --dont-print-server" - ] - } - ], - "metadata": { - "accelerator": "GPU", - "colab": { - "provenance": [] - }, - "gpuClass": "standard", - "kernelspec": { - "display_name": "Python 3", - "name": "python3" - }, - "language_info": { - "name": "python" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/openapi.yaml b/openapi.yaml index 0446259e..90607d12 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -11,17 +11,487 @@ info: servers: - url: '/' description: Default ComfyUI server + +# Default security - can be overridden per operation +security: [] # Common API components components: schemas: - Error: + OperationType: + type: string + enum: [install, uninstall, update, update-comfyui, fix, disable, enable, install-model] + description: Type of operation or task being performed + OperationResult: + type: string + enum: [success, failed, skipped, error, skip] + description: Result status of an operation (failed/error and skipped/skip are aliases) + # Core Task Queue Models + QueueTaskItem: type: object properties: + ui_id: + type: string + description: Unique identifier for the task + client_id: + type: string + description: Client identifier that initiated the task + kind: + $ref: '#/components/schemas/OperationType' + params: + oneOf: + - $ref: '#/components/schemas/InstallPackParams' + - $ref: '#/components/schemas/UpdatePackParams' + - $ref: '#/components/schemas/UpdateAllPacksParams' + - $ref: '#/components/schemas/UpdateComfyUIParams' + - $ref: '#/components/schemas/FixPackParams' + - $ref: '#/components/schemas/UninstallPackParams' + - $ref: '#/components/schemas/DisablePackParams' + - $ref: '#/components/schemas/EnablePackParams' + - $ref: '#/components/schemas/ModelMetadata' + required: [ui_id, client_id, kind, params] + TaskHistoryItem: + type: object + properties: + ui_id: + type: string + description: Unique identifier for the task + client_id: + type: string + description: Client identifier that initiated the task + kind: + type: string + description: Type of task that was performed + timestamp: + type: string + format: date-time + description: ISO timestamp when task completed + result: + type: string + description: Task result message or details + status: + $ref: '#/components/schemas/TaskExecutionStatus' + batch_id: + type: [string, 'null'] + description: ID of the batch this task belongs to + end_time: + type: [string, 'null'] + format: date-time + description: ISO timestamp when task execution ended + required: [ui_id, client_id, kind, timestamp, result] + TaskExecutionStatus: + type: object + properties: + status_str: + $ref: '#/components/schemas/OperationResult' + completed: + type: boolean + description: Whether the task completed + messages: + type: array + items: + type: string + description: Additional status messages + required: [status_str, completed, messages] + TaskStateMessage: + type: object + properties: + history: + type: object + additionalProperties: + $ref: '#/components/schemas/TaskHistoryItem' + description: Map of task IDs to their history items + running_queue: + type: array + items: + $ref: '#/components/schemas/QueueTaskItem' + description: Currently executing tasks + pending_queue: + type: array + items: + $ref: '#/components/schemas/QueueTaskItem' + description: Tasks waiting to be executed + installed_packs: + type: object + additionalProperties: + $ref: '#/components/schemas/ManagerPackInstalled' + description: Map of currently installed node packages by name + required: [history, running_queue, pending_queue, installed_packs] + # WebSocket Message Models + ManagerMessageName: + type: string + enum: [cm-task-completed, cm-task-started, cm-queue-status] + description: WebSocket message type constants for manager events + MessageTaskDone: + type: object + properties: + ui_id: + type: string + description: Task identifier + result: + type: string + description: Task result message + kind: + type: string + description: Type of task + status: + $ref: '#/components/schemas/TaskExecutionStatus' + timestamp: + type: string + format: date-time + description: ISO timestamp when task completed + state: + $ref: '#/components/schemas/TaskStateMessage' + required: [ui_id, result, kind, timestamp, state] + MessageTaskStarted: + type: object + properties: + ui_id: + type: string + description: Task identifier + kind: + type: string + description: Type of task + timestamp: + type: string + format: date-time + description: ISO timestamp when task started + state: + $ref: '#/components/schemas/TaskStateMessage' + required: [ui_id, kind, timestamp, state] + MessageTaskFailed: + type: object + properties: + ui_id: + type: string + description: Task identifier error: type: string description: Error message - + kind: + type: string + description: Type of task + timestamp: + type: string + format: date-time + description: ISO timestamp when task failed + state: + $ref: '#/components/schemas/TaskStateMessage' + required: [ui_id, error, kind, timestamp, state] + MessageUpdate: + oneOf: + - $ref: '#/components/schemas/MessageTaskDone' + - $ref: '#/components/schemas/MessageTaskStarted' + - $ref: '#/components/schemas/MessageTaskFailed' + description: Union type for all possible WebSocket message updates + # Manager Package Models + ManagerPackInfo: + type: object + properties: + id: + type: string + description: Either github-author/github-repo or name of pack from the registry + version: + type: string + description: Semantic version or Git commit hash + ui_id: + type: string + description: Task ID - generated internally + required: [id, version] + ManagerPackInstalled: + type: object + properties: + ver: + type: string + description: The version of the pack that is installed (Git commit hash or semantic version) + cnr_id: + type: [string, 'null'] + description: The name of the pack if installed from the registry + aux_id: + type: [string, 'null'] + description: The name of the pack if installed from github (author/repo-name format) + enabled: + type: boolean + description: Whether the pack is enabled + required: [ver, enabled] + SelectedVersion: + type: string + enum: [latest, nightly] + description: Version selection for pack installation + ManagerChannel: + type: string + enum: [default, recent, legacy, forked, dev, tutorial] + description: Channel for pack sources + ManagerDatabaseSource: + type: string + enum: [remote, local, cache] + description: Source for pack information + ManagerPackState: + type: string + enum: [installed, disabled, not_installed, import_failed, needs_update] + description: Current state of a pack + ManagerPackInstallType: + type: string + enum: [git-clone, copy, cnr] + description: Type of installation used for the pack + SecurityLevel: + type: string + enum: [strong, normal, normal-, weak] + description: Security level configuration (from most to least restrictive) + RiskLevel: + type: string + enum: [block, high+, high, middle+, middle] + description: Risk classification for operations + ManagerPack: + allOf: + - $ref: '#/components/schemas/ManagerPackInfo' + - type: object + properties: + author: + type: string + description: Pack author name or 'Unclaimed' if added via GitHub crawl + files: + type: array + items: + type: string + description: Repository URLs for installation (typically contains one GitHub URL) + reference: + type: string + description: The type of installation reference + title: + type: string + description: The display name of the pack + cnr_latest: + $ref: '#/components/schemas/SelectedVersion' + repository: + type: string + description: GitHub repository URL + state: + $ref: '#/components/schemas/ManagerPackState' + update-state: + type: [string, 'null'] + enum: ['false', 'true'] + description: Update availability status + stars: + type: integer + description: GitHub stars count + last_update: + type: string + format: date-time + description: Last update timestamp + health: + type: string + description: Health status of the pack + description: + type: string + description: Pack description + trust: + type: boolean + description: Whether the pack is trusted + install_type: + $ref: '#/components/schemas/ManagerPackInstallType' + # Installation Parameters + InstallPackParams: + allOf: + - $ref: '#/components/schemas/ManagerPackInfo' + - type: object + properties: + selected_version: + oneOf: + - type: string + - $ref: '#/components/schemas/SelectedVersion' + description: Semantic version, Git commit hash, latest, or nightly + repository: + type: string + description: GitHub repository URL (required if selected_version is nightly) + pip: + type: array + items: + type: string + description: PyPi dependency names + mode: + $ref: '#/components/schemas/ManagerDatabaseSource' + channel: + $ref: '#/components/schemas/ManagerChannel' + skip_post_install: + type: boolean + description: Whether to skip post-installation steps + required: [selected_version, mode, channel] + UpdateAllPacksParams: + type: object + properties: + mode: + $ref: '#/components/schemas/ManagerDatabaseSource' + ui_id: + type: string + description: Task ID - generated internally + UpdatePackParams: + type: object + properties: + node_name: + type: string + description: Name of the node package to update + node_ver: + type: [string, 'null'] + description: Current version of the node package + required: [node_name] + UpdateComfyUIParams: + type: object + properties: + is_stable: + type: boolean + description: Whether to update to stable version (true) or nightly (false) + default: true + target_version: + type: [string, 'null'] + description: Specific version to switch to (for version switching operations) + required: [] + FixPackParams: + type: object + properties: + node_name: + type: string + description: Name of the node package to fix + node_ver: + type: string + description: Version of the node package + required: [node_name, node_ver] + UninstallPackParams: + type: object + properties: + node_name: + type: string + description: Name of the node package to uninstall + is_unknown: + type: boolean + description: Whether this is an unknown/unregistered package + default: false + required: [node_name] + DisablePackParams: + type: object + properties: + node_name: + type: string + description: Name of the node package to disable + is_unknown: + type: boolean + description: Whether this is an unknown/unregistered package + default: false + required: [node_name] + EnablePackParams: + type: object + properties: + cnr_id: + type: string + description: ComfyUI Node Registry ID of the package to enable + required: [cnr_id] + # Query Parameter Models + UpdateAllQueryParams: + type: object + properties: + client_id: + type: string + description: Client identifier that initiated the request + ui_id: + type: string + description: Base UI identifier for task tracking + mode: + $ref: '#/components/schemas/ManagerDatabaseSource' + required: [client_id, ui_id] + UpdateComfyUIQueryParams: + type: object + properties: + client_id: + type: string + description: Client identifier that initiated the request + ui_id: + type: string + description: UI identifier for task tracking + stable: + type: boolean + default: true + description: Whether to update to stable version (true) or nightly (false) + required: [client_id, ui_id] + ComfyUISwitchVersionQueryParams: + type: object + properties: + ver: + type: string + description: Version to switch to + client_id: + type: string + description: Client identifier that initiated the request + ui_id: + type: string + description: UI identifier for task tracking + required: [ver, client_id, ui_id] + # Queue Status Models + QueueStatus: + type: object + properties: + total_count: + type: integer + description: Total number of tasks (pending + running) + done_count: + type: integer + description: Number of completed tasks + in_progress_count: + type: integer + description: Number of tasks currently running + pending_count: + type: integer + description: Number of tasks waiting to be executed + is_processing: + type: boolean + description: Whether the task worker is active + client_id: + type: string + description: Client ID (when filtered by client) + required: [total_count, done_count, in_progress_count, is_processing] + # Mappings Model + ManagerMappings: + type: object + additionalProperties: + type: array + description: Tuple of [node_names, metadata] + items: + oneOf: + - type: array + items: + type: string + description: List of ComfyNode names included in the pack + - type: object + properties: + title_aux: + type: string + description: The display name of the pack + # Model Management + ModelMetadata: + type: object + properties: + name: + type: string + description: Name of the model + type: + type: string + description: Type of model + base: + type: string + description: Base model type + save_path: + type: string + description: Path for saving the model + url: + type: string + description: Download URL + filename: + type: string + description: Target filename + ui_id: + type: string + description: ID for UI reference + required: [name, type, url, filename] + # Legacy Node Package Model (for backward compatibility) NodePackageMetadata: type: object properties: @@ -58,51 +528,260 @@ components: mode: type: string description: Source mode - - ModelMetadata: + # Snapshot Models + SnapshotItem: + type: string + description: Name of the snapshot + # Error Models + Error: + type: object + properties: + error: + type: string + description: Error message + required: [error] + # Response Models + InstalledPacksResponse: + type: object + additionalProperties: + $ref: '#/components/schemas/ManagerPackInstalled' + description: Map of pack names to their installation info + HistoryResponse: + type: object + properties: + history: + type: object + additionalProperties: + $ref: '#/components/schemas/TaskHistoryItem' + description: Map of task IDs to their history items + HistoryListResponse: + type: object + properties: + ids: + type: array + items: + type: string + description: List of available batch history IDs + # State Management Models + InstalledNodeInfo: type: object properties: name: type: string - description: Name of the model - type: + description: Node package name + version: type: string - description: Type of model - base: + description: Installed version + repository_url: + type: [string, 'null'] + description: Git repository URL + install_method: type: string - description: Base model type - save_path: - type: string - description: Path for saving the model - url: - type: string - description: Download URL - filename: - type: string - description: Target filename - ui_id: - type: string - description: ID for UI reference - - SnapshotItem: - type: string - description: Name of the snapshot - - QueueStatus: + description: Installation method (cnr, git, pip, etc.) + enabled: + type: boolean + description: Whether the node is currently enabled + default: true + install_date: + type: [string, 'null'] + format: date-time + description: ISO timestamp of installation + required: [name, version, install_method] + InstalledModelInfo: type: object properties: - total_count: - type: integer - description: Total number of tasks - done_count: - type: integer - description: Number of completed tasks - in_progress_count: - type: integer - description: Number of tasks in progress - is_processing: + name: + type: string + description: Model filename + path: + type: string + description: Full path to model file + type: + type: string + description: Model type (checkpoint, lora, vae, etc.) + size_bytes: + type: [integer, 'null'] + description: File size in bytes + minimum: 0 + hash: + type: [string, 'null'] + description: Model file hash for verification + install_date: + type: [string, 'null'] + format: date-time + description: ISO timestamp when added + required: [name, path, type] + ComfyUIVersionInfo: + type: object + properties: + version: + type: string + description: ComfyUI version string + commit_hash: + type: [string, 'null'] + description: Git commit hash + branch: + type: [string, 'null'] + description: Git branch name + is_stable: type: boolean - description: Whether the queue is currently processing + description: Whether this is a stable release + default: false + last_updated: + type: [string, 'null'] + format: date-time + description: ISO timestamp of last update + required: [version] + BatchOperation: + type: object + properties: + operation_id: + type: string + description: Unique operation identifier + operation_type: + $ref: '#/components/schemas/OperationType' + target: + type: string + description: Target of the operation (node name, model name, etc.) + target_version: + type: [string, 'null'] + description: Target version for the operation + result: + $ref: '#/components/schemas/OperationResult' + error_message: + type: [string, 'null'] + description: Error message if operation failed + start_time: + type: string + format: date-time + description: ISO timestamp when operation started + end_time: + type: [string, 'null'] + format: date-time + description: ISO timestamp when operation completed + client_id: + type: [string, 'null'] + description: Client that initiated the operation + required: [operation_id, operation_type, target, result, start_time] + ComfyUISystemState: + type: object + properties: + snapshot_time: + type: string + format: date-time + description: ISO timestamp when snapshot was taken + comfyui_version: + $ref: '#/components/schemas/ComfyUIVersionInfo' + frontend_version: + type: [string, 'null'] + description: ComfyUI frontend version if available + python_version: + type: string + description: Python interpreter version + platform_info: + type: string + description: Operating system and platform information + installed_nodes: + type: object + additionalProperties: + $ref: '#/components/schemas/InstalledNodeInfo' + description: Map of installed node packages by name + installed_models: + type: object + additionalProperties: + $ref: '#/components/schemas/InstalledModelInfo' + description: Map of installed models by name + manager_config: + type: object + additionalProperties: true + description: ComfyUI Manager configuration settings + comfyui_root_path: + type: [string, 'null'] + description: ComfyUI root installation directory + model_paths: + type: object + additionalProperties: + type: array + items: + type: string + description: Map of model types to their configured paths + manager_version: + type: [string, 'null'] + description: ComfyUI Manager version + security_level: + $ref: '#/components/schemas/SecurityLevel' + network_mode: + type: [string, 'null'] + description: Network mode (online, offline, private) + cli_args: + type: object + additionalProperties: true + description: Selected ComfyUI CLI arguments + custom_nodes_count: + type: [integer, 'null'] + description: Total number of custom node packages + minimum: 0 + failed_imports: + type: array + items: + type: string + description: List of custom nodes that failed to import + pip_packages: + type: object + additionalProperties: + type: string + description: Map of installed pip packages to their versions + embedded_python: + type: [boolean, 'null'] + description: Whether ComfyUI is running from an embedded Python distribution + required: [snapshot_time, comfyui_version, python_version, platform_info] + BatchExecutionRecord: + type: object + properties: + batch_id: + type: string + description: Unique batch identifier + start_time: + type: string + format: date-time + description: ISO timestamp when batch started + end_time: + type: [string, 'null'] + format: date-time + description: ISO timestamp when batch completed + state_before: + $ref: '#/components/schemas/ComfyUISystemState' + state_after: + type: ['null'] + allOf: + - $ref: '#/components/schemas/ComfyUISystemState' + description: System state after batch execution + operations: + type: array + items: + $ref: '#/components/schemas/BatchOperation' + description: List of operations performed in this batch + total_operations: + type: integer + description: Total number of operations in batch + minimum: 0 + default: 0 + successful_operations: + type: integer + description: Number of successful operations + minimum: 0 + default: 0 + failed_operations: + type: integer + description: Number of failed operations + minimum: 0 + default: 0 + skipped_operations: + type: integer + description: Number of skipped operations + minimum: 0 + default: 0 + required: [batch_id, start_time, state_before] ImportFailInfoBulkRequest: type: object @@ -142,15 +821,13 @@ components: in: header name: Security-Level description: Security level for sensitive operations - parameters: modeParam: name: mode in: query description: Source mode (e.g., "local", "remote") schema: - type: string - enum: [local, remote, default] + $ref: '#/components/schemas/ManagerDatabaseSource' targetParam: name: target @@ -167,177 +844,213 @@ components: required: true schema: type: string - + clientIdParam: + name: client_id + in: query + description: Client ID for filtering tasks + schema: + type: string + uiIdParam: + name: ui_id + in: query + description: Specific task ID to retrieve + schema: + type: string + clientIdRequiredParam: + name: client_id + in: query + required: true + description: Required client ID that initiated the request + schema: + type: string + uiIdRequiredParam: + name: ui_id + in: query + required: true + description: Required unique task identifier + schema: + type: string + maxItemsParam: + name: max_items + in: query + description: Maximum number of items to return + schema: + type: integer + minimum: 1 + offsetParam: + name: offset + in: query + description: Offset for pagination + schema: + type: integer + minimum: 0 # API Paths paths: - # Custom Nodes Endpoints - /customnode/getmappings: - get: - summary: Get node-to-package mappings - description: Provides unified mapping between nodes and node packages - parameters: - - $ref: '#/components/parameters/modeParam' - responses: - '200': - description: Successful operation - content: - application/json: - schema: - type: object - additionalProperties: - type: array - items: - type: array - description: Mapping of node packages to node classes - - /customnode/fetch_updates: - get: - summary: Check for updates - description: Fetches updates for custom nodes - parameters: - - $ref: '#/components/parameters/modeParam' - responses: - '200': - description: No updates available - '201': - description: Updates found - '400': - description: Error occurred - - /customnode/installed: - get: - summary: Get installed custom nodes - description: Returns a list of installed node packages - parameters: - - name: mode - in: query - description: Lists mode, default or imported - schema: - type: string - enum: [default, imported] - responses: - '200': - description: Successful operation - content: - application/json: - schema: - type: object - additionalProperties: - $ref: '#/components/schemas/NodePackageMetadata' - - /customnode/getlist: - get: - summary: Get custom node list - description: Provides a list of available custom nodes - parameters: - - $ref: '#/components/parameters/modeParam' - - name: skip_update - in: query - description: Skip update check - schema: - type: boolean - responses: - '200': - description: Successful operation - content: - application/json: - schema: - type: object - properties: - channel: - type: string - node_packs: - type: object - additionalProperties: - $ref: '#/components/schemas/NodePackageMetadata' - - /customnode/alternatives: - get: - summary: Get alternative node options - description: Provides alternatives for nodes - parameters: - - $ref: '#/components/parameters/modeParam' - responses: - '200': - description: Successful operation - content: - application/json: - schema: - type: object - additionalProperties: - type: object - - /customnode/versions/{node_name}: - get: - summary: Get available versions for a node - description: Lists all available versions for a specific node - parameters: - - name: node_name - in: path - required: true - schema: - type: string - responses: - '200': - description: Successful operation - content: - application/json: - schema: - type: array - items: - type: object - properties: - version: - type: string - '400': - description: Node not found - - /customnode/disabled_versions/{node_name}: - get: - summary: Get disabled versions for a node - description: Lists all disabled versions for a specific node - parameters: - - name: node_name - in: path - required: true - schema: - type: string - responses: - '200': - description: Successful operation - content: - application/json: - schema: - type: array - items: - type: object - properties: - version: - type: string - '400': - description: Node not found - - /customnode/import_fail_info: + # Task Queue Management (v2 endpoints) + /v2/manager/queue/task: post: - summary: Get import failure information - description: Returns information about why a node failed to import + summary: Add task to queue + description: Adds a new task to the processing queue requestBody: required: true content: application/json: schema: - type: object - properties: - cnr_id: - type: string - url: - type: string + $ref: '#/components/schemas/QueueTaskItem' + examples: + install: + summary: Install a custom node + value: + ui_id: "task_123" + client_id: "client_abc" + kind: "install" + params: + id: "pythongosssss/ComfyUI-Custom-Scripts" + version: "latest" + selected_version: "latest" + mode: "remote" + channel: "default" + update: + summary: Update a custom node + value: + ui_id: "task_124" + client_id: "client_abc" + kind: "update" + params: + node_name: "ComfyUI-Custom-Scripts" + node_ver: "1.0.0" + update-all: + summary: Update all custom nodes + value: + ui_id: "task_125" + client_id: "client_abc" + kind: "update-all" + params: + mode: "remote" + update-comfyui: + summary: Update ComfyUI itself + value: + ui_id: "task_126" + client_id: "client_abc" + kind: "update-comfyui" + params: + is_stable: true + fix: + summary: Fix a custom node + value: + ui_id: "task_127" + client_id: "client_abc" + kind: "fix" + params: + node_name: "ComfyUI-Impact-Pack" + node_ver: "2.0.0" + uninstall: + summary: Uninstall a custom node + value: + ui_id: "task_128" + client_id: "client_abc" + kind: "uninstall" + params: + node_name: "ComfyUI-AnimateDiff-Evolved" + is_unknown: false + disable: + summary: Disable a custom node + value: + ui_id: "task_129" + client_id: "client_abc" + kind: "disable" + params: + node_name: "ComfyUI-Manager" + is_unknown: false + enable: + summary: Enable a custom node + value: + ui_id: "task_130" + client_id: "client_abc" + kind: "enable" + params: + cnr_id: "comfyui-manager" + install-model: + summary: Install a model + value: + ui_id: "task_131" + client_id: "client_abc" + kind: "install-model" + params: + name: "SD 1.5 Base Model" + type: "checkpoint" + base: "SD1.x" + save_path: "default" + url: "https://huggingface.co/runwayml/stable-diffusion-v1-5/resolve/main/v1-5-pruned.safetensors" + filename: "v1-5-pruned.safetensors" + responses: + '200': + description: Task queued successfully + '400': + description: Invalid task data + '500': + description: Internal server error + /v2/manager/queue/status: + get: + summary: Get queue status + description: Returns the current status of the operation queue with optional client filtering + parameters: + - $ref: '#/components/parameters/clientIdParam' responses: '200': description: Successful operation + content: + application/json: + schema: + $ref: '#/components/schemas/QueueStatus' + /v2/manager/queue/history: + get: + summary: Get task history + description: Get task history with optional filtering + parameters: + - name: id + in: query + description: Batch history ID (for file-based history) + schema: + type: string + - $ref: '#/components/parameters/clientIdParam' + - $ref: '#/components/parameters/uiIdParam' + - $ref: '#/components/parameters/maxItemsParam' + - $ref: '#/components/parameters/offsetParam' + responses: + '200': + description: Successful operation + content: + application/json: + schema: + oneOf: + - $ref: '#/components/schemas/HistoryResponse' + - type: object # File-based batch history '400': - description: No information available - + description: Error retrieving history + /v2/manager/queue/history_list: + get: + summary: Get available batch history files + description: Returns a list of batch history IDs sorted by modification time + responses: + '200': + description: Successful operation + content: + application/json: + schema: + $ref: '#/components/schemas/HistoryListResponse' + '400': + description: Error retrieving history list + /v2/manager/queue/start: + get: + summary: Start queue processing + description: Starts processing the operation queue + responses: + '200': + description: Processing started + '201': + description: Processing already in progress + /v2/customnode/import_fail_info_bulk: post: summary: Get import failure info for multiple nodes @@ -362,67 +1075,15 @@ paths: description: Bad Request. The request body is invalid. '500': description: Internal Server Error. - - /customnode/install/git_url: - post: - summary: Install custom node via Git URL - description: Installs a custom node from a Git repository URL - security: - - securityLevel: [] - requestBody: - required: true - content: - text/plain: - schema: - type: string - responses: - '200': - description: Installation successful or already installed - '400': - description: Installation failed - '403': - description: Security policy violation - - /customnode/install/pip: - post: - summary: Install custom node dependencies via pip - description: Installs Python package dependencies for custom nodes - security: - - securityLevel: [] - requestBody: - required: true - content: - text/plain: - schema: - type: string - responses: - '200': - description: Installation successful - '403': - description: Security policy violation - - # Model Management Endpoints - /externalmodel/getlist: + + /v2/manager/queue/reset: get: - summary: Get external model list - description: Provides a list of available external models - parameters: - - $ref: '#/components/parameters/modeParam' + summary: Reset queue + description: Resets the operation queue responses: '200': - description: Successful operation - content: - application/json: - schema: - type: object - properties: - models: - type: array - items: - $ref: '#/components/schemas/ModelMetadata' - - # Queue Management Endpoints - /manager/queue/update_all: + description: Queue reset successfully + /v2/manager/queue/update_all: get: summary: Update all custom nodes description: Queues update operations for all installed custom nodes @@ -430,159 +1091,30 @@ paths: - securityLevel: [] parameters: - $ref: '#/components/parameters/modeParam' + - $ref: '#/components/parameters/clientIdRequiredParam' + - $ref: '#/components/parameters/uiIdRequiredParam' responses: '200': description: Update queued successfully + '400': + description: Missing required parameters '401': description: Processing already in progress '403': description: Security policy violation - - /manager/queue/reset: - get: - summary: Reset queue - description: Resets the operation queue - responses: - '200': - description: Queue reset successfully - - /manager/queue/status: - get: - summary: Get queue status - description: Returns the current status of the operation queue - responses: - '200': - description: Successful operation - content: - application/json: - schema: - $ref: '#/components/schemas/QueueStatus' - - /manager/queue/install: - post: - summary: Install custom node - description: Queues installation of a custom node - security: - - securityLevel: [] - requestBody: - required: true - content: - application/json: - schema: - $ref: '#/components/schemas/NodePackageMetadata' - responses: - '200': - description: Installation queued successfully - '403': - description: Security policy violation - '404': - description: Target node not found or security issue - - /manager/queue/start: - get: - summary: Start queue processing - description: Starts processing the operation queue - responses: - '200': - description: Processing started - '201': - description: Processing already in progress - - /manager/queue/fix: - post: - summary: Fix custom node - description: Attempts to fix a broken custom node installation - security: - - securityLevel: [] - requestBody: - required: true - content: - application/json: - schema: - $ref: '#/components/schemas/NodePackageMetadata' - responses: - '200': - description: Fix operation queued successfully - '403': - description: Security policy violation - - /manager/queue/reinstall: - post: - summary: Reinstall custom node - description: Uninstalls and then reinstalls a custom node - security: - - securityLevel: [] - requestBody: - required: true - content: - application/json: - schema: - $ref: '#/components/schemas/NodePackageMetadata' - responses: - '200': - description: Reinstall operation queued successfully - '403': - description: Security policy violation - - /manager/queue/uninstall: - post: - summary: Uninstall custom node - description: Queues uninstallation of a custom node - security: - - securityLevel: [] - requestBody: - required: true - content: - application/json: - schema: - $ref: '#/components/schemas/NodePackageMetadata' - responses: - '200': - description: Uninstallation queued successfully - '403': - description: Security policy violation - - /manager/queue/update: - post: - summary: Update custom node - description: Queues update of a custom node - security: - - securityLevel: [] - requestBody: - required: true - content: - application/json: - schema: - $ref: '#/components/schemas/NodePackageMetadata' - responses: - '200': - description: Update queued successfully - '403': - description: Security policy violation - - /manager/queue/disable: - post: - summary: Disable custom node - description: Disables a custom node without uninstalling it - requestBody: - required: true - content: - application/json: - schema: - $ref: '#/components/schemas/NodePackageMetadata' - responses: - '200': - description: Disable operation queued successfully - - /manager/queue/update_comfyui: + /v2/manager/queue/update_comfyui: get: summary: Update ComfyUI description: Queues an update operation for ComfyUI itself + parameters: + - $ref: '#/components/parameters/clientIdRequiredParam' + - $ref: '#/components/parameters/uiIdRequiredParam' responses: '200': description: Update queued successfully - - /manager/queue/install_model: + '400': + description: Missing required parameters + /v2/manager/queue/install_model: post: summary: Install model description: Queues installation of a model @@ -601,9 +1133,74 @@ paths: description: Invalid model request '403': description: Security policy violation + # Custom Nodes Endpoints (v2) + /v2/customnode/getmappings: + get: + summary: Get node-to-package mappings + description: Provides unified mapping between nodes and node packages + parameters: + - $ref: '#/components/parameters/modeParam' + responses: + '200': + description: Successful operation + content: + application/json: + schema: + $ref: '#/components/schemas/ManagerMappings' + /v2/customnode/fetch_updates: + get: + summary: Check for updates + description: Fetches updates for custom nodes + parameters: + - $ref: '#/components/parameters/modeParam' + responses: + '200': + description: No updates available + '201': + description: Updates found + '400': + description: Error occurred - # Snapshot Management Endpoints - /snapshot/getlist: + /v2/customnode/installed: + get: + summary: Get installed custom nodes + description: Returns a list of installed node packages + parameters: + - name: mode + in: query + description: Lists mode, default or imported + schema: + type: string + enum: [default, imported] + responses: + '200': + description: Successful operation + content: + application/json: + schema: + $ref: '#/components/schemas/InstalledPacksResponse' + /v2/customnode/import_fail_info: + post: + summary: Get import failure information + description: Returns information about why a node failed to import + requestBody: + required: true + content: + application/json: + schema: + type: object + properties: + cnr_id: + type: string + url: + type: string + responses: + '200': + description: Successful operation + '400': + description: No information available + # Snapshot Management Endpoints (v2) + /v2/snapshot/getlist: get: summary: Get snapshot list description: Returns a list of available snapshots @@ -619,8 +1216,7 @@ paths: type: array items: $ref: '#/components/schemas/SnapshotItem' - - /snapshot/remove: + /v2/snapshot/remove: get: summary: Remove snapshot description: Removes a specified snapshot @@ -635,8 +1231,7 @@ paths: description: Error removing snapshot '403': description: Security policy violation - - /snapshot/restore: + /v2/snapshot/restore: get: summary: Restore snapshot description: Restores a specified snapshot @@ -651,8 +1246,7 @@ paths: description: Error restoring snapshot '403': description: Security policy violation - - /snapshot/get_current: + /v2/snapshot/get_current: get: summary: Get current snapshot description: Returns the current system state as a snapshot @@ -665,8 +1259,7 @@ paths: type: object '400': description: Error creating snapshot - - /snapshot/save: + /v2/snapshot/save: get: summary: Save snapshot description: Saves the current system state as a new snapshot @@ -675,9 +1268,8 @@ paths: description: Snapshot saved successfully '400': description: Error saving snapshot - - # ComfyUI Management Endpoints - /comfyui_manager/comfyui_versions: + # ComfyUI Management Endpoints (v2) + /v2/comfyui_manager/comfyui_versions: get: summary: Get ComfyUI versions description: Returns available and current ComfyUI versions @@ -697,57 +1289,26 @@ paths: type: string '400': description: Error retrieving versions - - /comfyui_manager/comfyui_switch_version: + /v2/comfyui_manager/comfyui_switch_version: get: summary: Switch ComfyUI version description: Switches to a specified ComfyUI version parameters: - name: ver in: query + required: true description: Target version schema: type: string + - $ref: '#/components/parameters/clientIdRequiredParam' + - $ref: '#/components/parameters/uiIdRequiredParam' responses: '200': - description: Version switch successful + description: Version switch queued successfully '400': - description: Error switching version - - /manager/reboot: - get: - summary: Reboot ComfyUI - description: Restarts the ComfyUI server - security: - - securityLevel: [] - responses: - '200': - description: Reboot initiated - '403': - description: Security policy violation - - # Configuration Endpoints - /manager/preview_method: - get: - summary: Get or set preview method - description: Gets or sets the latent preview method - parameters: - - name: value - in: query - required: false - description: New preview method - schema: - type: string - enum: [auto, latent2rgb, taesd, none] - responses: - '200': - description: Setting updated or current value returned - content: - text/plain: - schema: - type: string - - /manager/db_mode: + description: Missing required parameters or error switching version + # Configuration Endpoints (v2) + /v2/manager/db_mode: get: summary: Get or set database mode description: Gets or sets the database mode @@ -766,27 +1327,7 @@ paths: text/plain: schema: type: string - - /manager/policy/component: - get: - summary: Get or set component policy - description: Gets or sets the component policy - parameters: - - name: value - in: query - required: false - description: New component policy - schema: - type: string - responses: - '200': - description: Setting updated or current value returned - content: - text/plain: - schema: - type: string - - /manager/policy/update: + /v2/manager/policy/update: get: summary: Get or set update policy description: Gets or sets the update policy @@ -805,8 +1346,7 @@ paths: text/plain: schema: type: string - - /manager/channel_url_list: + /v2/manager/channel_url_list: get: summary: Get or set channel URL description: Gets or sets the channel URL for custom node sources @@ -836,49 +1376,18 @@ paths: type: string url: type: string - - # Component Management Endpoints - /manager/component/save: - post: - summary: Save component - description: Saves a reusable workflow component - requestBody: - required: true - content: - application/json: - schema: - type: object - properties: - name: - type: string - workflow: - type: object + /v2/manager/reboot: + get: + summary: Reboot ComfyUI + description: Restarts the ComfyUI server + security: + - securityLevel: [] responses: '200': - description: Component saved successfully - content: - text/plain: - schema: - type: string - '400': - description: Error saving component - - /manager/component/loads: - post: - summary: Load components - description: Loads all available workflow components - responses: - '200': - description: Components loaded successfully - content: - application/json: - schema: - type: object - '400': - description: Error loading components - - # Miscellaneous Endpoints - /manager/version: + description: Reboot initiated + '403': + description: Security policy violation + /v2/manager/version: get: summary: Get manager version description: Returns the current version of ComfyUI-Manager @@ -889,15 +1398,17 @@ paths: text/plain: schema: type: string - - /manager/notice: + /v2/manager/is_legacy_manager_ui: get: - summary: Get manager notice - description: Returns HTML content with notices and version information + summary: Check if legacy manager UI is enabled + description: Returns whether the legacy manager UI is enabled responses: '200': description: Successful operation content: - text/html: + application/json: schema: - type: string \ No newline at end of file + type: object + properties: + is_legacy_manager_ui: + type: boolean \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 8d369067..9f02889f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,15 +1,65 @@ +[build-system] +requires = ["setuptools >= 61.0"] +build-backend = "setuptools.build_meta" + [project] name = "comfyui-manager" +license = { text = "GPL-3.0-only" } +version = "4.0.1-beta.3" +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." -version = "3.36" -license = { file = "LICENSE.txt" } -dependencies = ["GitPython", "PyGithub", "matrix-nio", "transformers", "huggingface-hub>0.20", "typer", "rich", "typing-extensions", "toml", "uv", "chardet"] +readme = "README.md" +keywords = ["comfyui", "comfyui-manager"] + +maintainers = [ + { name = "Dr.Lt.Data", email = "dr.lt.data@gmail.com" }, + { name = "Yoland Yan", email = "yoland@comfy.org" }, + { name = "James Kwon", email = "hongilkwon316@gmail.com" }, + { name = "Robin Huang", email = "robin@comfy.org" }, +] + +classifiers = [ + "Development Status :: 4 - Beta", + "Intended Audience :: Developers", + "License :: OSI Approved :: GNU General Public License v3 (GPLv3)", +] + +dependencies = [ + "GitPython", + "PyGithub", +# "matrix-nio", + "transformers", + "huggingface-hub>0.20", + "typer", + "rich", + "typing-extensions", + "toml", + "uv", + "chardet" +] + +[project.optional-dependencies] +dev = ["pre-commit", "pytest", "ruff", "pytest-cov"] [project.urls] Repository = "https://github.com/ltdrdata/ComfyUI-Manager" -# Used by Comfy Registry https://comfyregistry.org -[tool.comfy] -PublisherId = "drltdata" -DisplayName = "ComfyUI-Manager" -Icon = "" +[tool.setuptools.packages.find] +where = ["."] +include = ["comfyui_manager*"] + +[project.scripts] +cm-cli = "comfyui_manager.cm_cli.__main__:main" + +[tool.ruff] +line-length = 120 +target-version = "py39" + +[tool.ruff.lint] +select = [ + "E4", # default + "E7", # default + "E9", # default + "F", # default + "I", # isort-like behavior (import statement sorting) +] 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 diff --git a/scanner.py b/scanner.py index c7138550..870c0721 100644 --- a/scanner.py +++ b/scanner.py @@ -94,7 +94,7 @@ def extract_nodes(code_text): return s else: return set() - except: + except Exception: return set() @@ -396,7 +396,7 @@ def update_custom_nodes(): try: download_url(url, temp_dir) - except: + except Exception: print(f"[ERROR] Cannot download '{url}'") with concurrent.futures.ThreadPoolExecutor(10) as executor: diff --git a/scripts/colab-dependencies.py b/scripts/colab-dependencies.py deleted file mode 100644 index d5a70ed6..00000000 --- a/scripts/colab-dependencies.py +++ /dev/null @@ -1,39 +0,0 @@ -import os -import subprocess - - -def get_enabled_subdirectories_with_files(base_directory): - subdirs_with_files = [] - for subdir in os.listdir(base_directory): - try: - full_path = os.path.join(base_directory, subdir) - if os.path.isdir(full_path) and not subdir.endswith(".disabled") and not subdir.startswith('.') and subdir != '__pycache__': - print(f"## Install dependencies for '{subdir}'") - requirements_file = os.path.join(full_path, "requirements.txt") - install_script = os.path.join(full_path, "install.py") - - if os.path.exists(requirements_file) or os.path.exists(install_script): - subdirs_with_files.append((full_path, requirements_file, install_script)) - except Exception as e: - print(f"EXCEPTION During Dependencies INSTALL on '{subdir}':\n{e}") - - return subdirs_with_files - - -def install_requirements(requirements_file_path): - if os.path.exists(requirements_file_path): - subprocess.run(["pip", "install", "-r", requirements_file_path]) - - -def run_install_script(install_script_path): - if os.path.exists(install_script_path): - subprocess.run(["python", install_script_path]) - - -custom_nodes_directory = "custom_nodes" -subdirs_with_files = get_enabled_subdirectories_with_files(custom_nodes_directory) - - -for subdir, requirements_file, install_script in subdirs_with_files: - install_requirements(requirements_file) - run_install_script(install_script) diff --git a/scripts/install-comfyui-venv-linux.sh b/scripts/install-comfyui-venv-linux.sh deleted file mode 100755 index 5a736ef1..00000000 --- a/scripts/install-comfyui-venv-linux.sh +++ /dev/null @@ -1,21 +0,0 @@ -git clone https://github.com/comfyanonymous/ComfyUI -cd ComfyUI/custom_nodes -git clone https://github.com/ltdrdata/ComfyUI-Manager comfyui-manager -cd .. -python -m venv venv -source venv/bin/activate -python -m pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu121 -python -m pip install -r requirements.txt -python -m pip install -r custom_nodes/comfyui-manager/requirements.txt -cd .. -echo "#!/bin/bash" > run_gpu.sh -echo "cd ComfyUI" >> run_gpu.sh -echo "source venv/bin/activate" >> run_gpu.sh -echo "python main.py --preview-method auto" >> run_gpu.sh -chmod +x run_gpu.sh - -echo "#!/bin/bash" > run_cpu.sh -echo "cd ComfyUI" >> run_cpu.sh -echo "source venv/bin/activate" >> run_cpu.sh -echo "python main.py --preview-method auto --cpu" >> run_cpu.sh -chmod +x run_cpu.sh diff --git a/scripts/install-comfyui-venv-win.bat b/scripts/install-comfyui-venv-win.bat deleted file mode 100755 index 47470098..00000000 --- a/scripts/install-comfyui-venv-win.bat +++ /dev/null @@ -1,17 +0,0 @@ -git clone https://github.com/comfyanonymous/ComfyUI -cd ComfyUI/custom_nodes -git clone https://github.com/ltdrdata/ComfyUI-Manager comfyui-manager -cd .. -python -m venv venv -call venv/Scripts/activate -python -m pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu121 -python -m pip install -r requirements.txt -python -m pip install -r custom_nodes/comfyui-manager/requirements.txt -cd .. -echo "cd ComfyUI" >> run_gpu.bat -echo "call venv/Scripts/activate" >> run_gpu.bat -echo "python main.py" >> run_gpu.bat - -echo "cd ComfyUI" >> run_cpu.bat -echo "call venv/Scripts/activate" >> run_cpu.bat -echo "python main.py --cpu" >> run_cpu.bat diff --git a/scripts/install-manager-for-portable-version.bat b/scripts/install-manager-for-portable-version.bat deleted file mode 100644 index 6eb58b74..00000000 --- a/scripts/install-manager-for-portable-version.bat +++ /dev/null @@ -1,3 +0,0 @@ -.\python_embeded\python.exe -s -m pip install gitpython -.\python_embeded\python.exe -c "import git; git.Repo.clone_from('https://github.com/ltdrdata/ComfyUI-Manager', './ComfyUI/custom_nodes/comfyui-manager')" -.\python_embeded\python.exe -m pip install -r ./ComfyUI/custom_nodes/comfyui-manager/requirements.txt