- Updated PromptExtractor to construct workflow data with a new v2 model format, including handling of model_a and model_b blocks. - Enhanced PromptManagerAdvanced to retain Lora paths during normalization for better data integrity. - Modified RecipeBuilder to apply preferred Lora order and manage model overrides more effectively. - Improved RecipeManager to ensure proper handling of model_a and model_b outputs, including conditional logic for model retention. - Adjusted RecipeRelay to streamline model slot management and ensure compatibility with v2 structure. - Cleaned up RecipeRenderer to align with new model data handling, removing legacy references. - Removed deprecated legacy top-level keys and streamlined ensure_v2_recipe_data function for clarity and efficiency. Co-authored-by: Copilot <copilot@github.com>
169 lines
5.0 KiB
Python
169 lines
5.0 KiB
Python
"""
|
|
Utility functions for RECIPE_DATA dicts.
|
|
Runtime object keys (MODEL_A, MODEL_B, CLIP, VAE, POSITIVE, NEGATIVE, etc.)
|
|
store non-serializable ComfyUI objects and must be stripped before JSON
|
|
serialization.
|
|
"""
|
|
|
|
import json
|
|
import math
|
|
|
|
# Keys that hold runtime (non-JSON-serializable) objects/conditioning payloads.
|
|
# These should never be persisted in saved prompt/workflow metadata.
|
|
RUNTIME_OBJECT_KEYS = frozenset({
|
|
"MODEL",
|
|
"MODEL_A",
|
|
"MODEL_B",
|
|
"CLIP",
|
|
"VAE",
|
|
"LATENT",
|
|
"IMAGE",
|
|
"MASK",
|
|
"EXTRA",
|
|
"POSITIVE",
|
|
"NEGATIVE",
|
|
})
|
|
|
|
_MODEL_KEYS = ("model_a", "model_b", "model_c", "model_d")
|
|
_LEGACY_TOP_LEVEL_KEYS = {
|
|
"family",
|
|
"model_a",
|
|
"model_b",
|
|
"positive_prompt",
|
|
"negative_prompt",
|
|
"loras_a",
|
|
"loras_b",
|
|
"vae",
|
|
"clip",
|
|
"clip_type",
|
|
"loader_type",
|
|
"sampler",
|
|
"resolution",
|
|
}
|
|
|
|
|
|
def get_v2_model_block(recipe_data, model_key):
|
|
"""Return a v2 model block dict for model_key, or None if unavailable."""
|
|
if not isinstance(recipe_data, dict):
|
|
return None
|
|
if int(recipe_data.get("version", 0) or 0) < 2:
|
|
return None
|
|
models = recipe_data.get("models", {}) if isinstance(recipe_data.get("models"), dict) else {}
|
|
block = models.get(model_key)
|
|
return block if isinstance(block, dict) else None
|
|
|
|
|
|
def ensure_v2_recipe_data(recipe_data, source="RecipeData"):
|
|
"""Normalize recipe_data to strict v2 shape.
|
|
|
|
v2 contract is authoritative. Legacy top-level recipe fields are ignored.
|
|
"""
|
|
if not isinstance(recipe_data, dict):
|
|
return {
|
|
"_source": source,
|
|
"version": 2,
|
|
"models": {},
|
|
}
|
|
|
|
is_v2 = int(recipe_data.get("version", 0) or 0) >= 2 and isinstance(recipe_data.get("models"), dict)
|
|
if not is_v2:
|
|
return {
|
|
"_source": str(recipe_data.get("_source") or source),
|
|
"version": 2,
|
|
"models": {},
|
|
}
|
|
|
|
out = {
|
|
"_source": str(recipe_data.get("_source") or source),
|
|
"version": 2,
|
|
"models": {},
|
|
}
|
|
|
|
# Preserve non-legacy root keys as passthrough metadata/runtime payload.
|
|
for key, val in recipe_data.items():
|
|
if key in {"_source", "version", "models"}:
|
|
continue
|
|
if key in _LEGACY_TOP_LEVEL_KEYS:
|
|
continue
|
|
out[key] = val
|
|
|
|
if is_v2:
|
|
for mk in _MODEL_KEYS:
|
|
block = recipe_data.get("models", {}).get(mk)
|
|
if isinstance(block, dict):
|
|
out["models"][mk] = dict(block)
|
|
|
|
return out
|
|
|
|
|
|
def _strip_runtime_keys_deep(value):
|
|
"""Recursively strip runtime-only keys from dict/list containers."""
|
|
if isinstance(value, dict):
|
|
return {
|
|
k: _strip_runtime_keys_deep(v)
|
|
for k, v in value.items()
|
|
if k not in RUNTIME_OBJECT_KEYS
|
|
}
|
|
if isinstance(value, list):
|
|
return [_strip_runtime_keys_deep(v) for v in value]
|
|
if isinstance(value, tuple):
|
|
return [_strip_runtime_keys_deep(v) for v in value]
|
|
if isinstance(value, set):
|
|
return [_strip_runtime_keys_deep(v) for v in value]
|
|
return value
|
|
|
|
|
|
def strip_runtime_objects(wf):
|
|
"""Return a deep copy of wf with all runtime object keys removed.
|
|
|
|
Use before JSON serialization (saving prompts, send_sync to JS, etc.).
|
|
"""
|
|
if not isinstance(wf, dict):
|
|
return wf
|
|
return _strip_runtime_keys_deep(wf)
|
|
|
|
|
|
def _json_default(value):
|
|
"""Best-effort fallback for objects that JSON cannot encode directly."""
|
|
if isinstance(value, (set, tuple)):
|
|
return list(value)
|
|
if isinstance(value, bytes):
|
|
return value.decode("utf-8", errors="replace")
|
|
return str(value)
|
|
|
|
|
|
def to_json_safe_workflow_data(wf):
|
|
"""Return a deep JSON-safe copy of workflow data with runtime keys removed.
|
|
|
|
This is stricter than strip_runtime_objects(): it also sanitizes nested
|
|
containers and unknown object types so metadata embedding cannot fail
|
|
during Save Image serialization.
|
|
"""
|
|
if not isinstance(wf, dict):
|
|
return {}
|
|
|
|
stripped = strip_runtime_objects(wf)
|
|
|
|
# Normalize NaN/Inf because Python's json allows them by default but strict
|
|
# JSON consumers may reject them.
|
|
def _normalize_numbers(value):
|
|
if isinstance(value, float) and (math.isnan(value) or math.isinf(value)):
|
|
return None
|
|
if isinstance(value, dict):
|
|
return {k: _normalize_numbers(v) for k, v in value.items()}
|
|
if isinstance(value, list):
|
|
return [_normalize_numbers(v) for v in value]
|
|
if isinstance(value, tuple):
|
|
return [_normalize_numbers(v) for v in value]
|
|
if isinstance(value, set):
|
|
return [_normalize_numbers(v) for v in value]
|
|
return value
|
|
|
|
normalized = _normalize_numbers(stripped)
|
|
|
|
try:
|
|
return json.loads(json.dumps(normalized, default=_json_default, allow_nan=False))
|
|
except Exception:
|
|
# Last-resort safety: stringify opaque values while preserving structure.
|
|
return json.loads(json.dumps(str(normalized)))
|