update templates for monorepo

This commit is contained in:
bymyself 2025-11-16 00:21:19 -08:00
parent a1a70362ca
commit 9a67426b3a
3 changed files with 95 additions and 9 deletions

View File

@ -10,7 +10,8 @@ import importlib
from dataclasses import dataclass from dataclasses import dataclass
from functools import cached_property from functools import cached_property
from pathlib import Path from pathlib import Path
from typing import TypedDict, Optional from typing import Dict, TypedDict, Optional
from aiohttp import web
from importlib.metadata import version from importlib.metadata import version
import requests import requests
@ -257,7 +258,57 @@ comfyui-frontend-package is not installed.
sys.exit(-1) sys.exit(-1)
@classmethod @classmethod
def templates_path(cls) -> str: def template_asset_map(cls) -> Optional[Dict[str, str]]:
"""Return a mapping of template asset names to their absolute paths."""
try:
from comfyui_workflow_templates import (
get_asset_path,
iter_templates,
)
except ImportError:
logging.error(
f"""
********** ERROR ***********
comfyui-workflow-templates is not installed.
{frontend_install_warning_message()}
********** ERROR ***********
""".strip()
)
return None
try:
template_entries = list(iter_templates())
except Exception as exc:
logging.error(f"Failed to enumerate workflow templates: {exc}")
return None
asset_map: Dict[str, str] = {}
try:
for entry in template_entries:
for asset in entry.assets:
asset_map[asset.filename] = get_asset_path(
entry.template_id, asset.filename
)
except Exception as exc:
logging.error(f"Failed to resolve template asset paths: {exc}")
return None
if not asset_map:
logging.error("No workflow template assets found. Did the packages install correctly?")
return None
return asset_map
@classmethod
def templates_path(cls) -> Optional[Dict[str, str]]:
return cls.template_asset_map()
@classmethod
def legacy_templates_path(cls) -> Optional[str]:
"""Return the legacy templates directory shipped inside the meta package."""
try: try:
import comfyui_workflow_templates import comfyui_workflow_templates
@ -276,6 +327,7 @@ comfyui-workflow-templates is not installed.
********** ERROR *********** ********** ERROR ***********
""".strip() """.strip()
) )
return None
@classmethod @classmethod
def embedded_docs_path(cls) -> str: def embedded_docs_path(cls) -> str:
@ -392,3 +444,17 @@ comfyui-workflow-templates is not installed.
logging.info("Falling back to the default frontend.") logging.info("Falling back to the default frontend.")
check_frontend_version() check_frontend_version()
return cls.default_frontend_path() return cls.default_frontend_path()
@classmethod
def template_asset_handler(cls):
assets = cls.template_asset_map()
if not assets:
return None
async def serve_template(request: web.Request) -> web.StreamResponse:
rel_path = request.match_info.get("path", "")
target = assets.get(rel_path)
if target is None:
raise web.HTTPNotFound()
return web.FileResponse(target)
return serve_template

View File

@ -1,5 +1,5 @@
comfyui-frontend-package==1.28.8 comfyui-frontend-package==1.28.8
comfyui-workflow-templates==0.2.11 comfyui-workflow-templates==0.3.1
comfyui-embedded-docs==0.3.1 comfyui-embedded-docs==0.3.1
torch torch
torchsde torchsde

View File

@ -29,7 +29,7 @@ import comfy.model_management
from comfy_api import feature_flags from comfy_api import feature_flags
import node_helpers import node_helpers
from comfyui_version import __version__ from comfyui_version import __version__
from app.frontend_management import FrontendManager from app.frontend_management import FrontendManager, parse_version
from comfy_api.internal import _ComfyNodeInternal from comfy_api.internal import _ComfyNodeInternal
from app.user_manager import UserManager from app.user_manager import UserManager
@ -847,11 +847,31 @@ class PromptServer():
for name, dir in nodes.EXTENSION_WEB_DIRS.items(): for name, dir in nodes.EXTENSION_WEB_DIRS.items():
self.app.add_routes([web.static('/extensions/' + name, dir)]) self.app.add_routes([web.static('/extensions/' + name, dir)])
workflow_templates_path = FrontendManager.templates_path() installed_templates_version = FrontendManager.get_installed_templates_version()
use_legacy_templates = True
if installed_templates_version:
try:
use_legacy_templates = (
parse_version(installed_templates_version)
< parse_version("0.3.0")
)
except Exception as exc:
logging.warning(
"Unable to parse templates version '%s': %s",
installed_templates_version,
exc,
)
if use_legacy_templates:
workflow_templates_path = FrontendManager.legacy_templates_path()
if workflow_templates_path: if workflow_templates_path:
self.app.add_routes([ self.app.add_routes([
web.static('/templates', workflow_templates_path) web.static('/templates', workflow_templates_path)
]) ])
else:
handler = FrontendManager.template_asset_handler()
if handler:
self.app.router.add_get("/templates/{path:.*}", handler)
# Serve embedded documentation from the package # Serve embedded documentation from the package
embedded_docs_path = FrontendManager.embedded_docs_path() embedded_docs_path = FrontendManager.embedded_docs_path()