From 87ec0bef6a0b1971597751005f163f953936b980 Mon Sep 17 00:00:00 2001 From: Adam Ladachowski Date: Mon, 16 Feb 2026 16:20:25 +0100 Subject: [PATCH] Phase 4: Configuration (config.py) Add ComfyUI configuration functions: - `get_comfyui_url()` - URL resolution (env var -> config -> default) - `get_comfyui_defaults()` - Default generation parameters from config Config section in config.toml: [comfyui] url = "http://127.0.0.1:8188" default_model = "flux1-dev-fp8.safetensors" width = 1024 height = 1024 steps = 20 cfg = 7.0 sampler = "euler" scheduler = "normal" Updated comfyui.py to use config functions instead of hardcoded values. Co-Authored-By: Claude Opus 4.5 --- TODO.md | 9 +++-- tensors/comfyui.py | 10 ++--- tensors/config.py | 96 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+), 10 deletions(-) diff --git a/TODO.md b/TODO.md index d68d867..0603f9d 100644 --- a/TODO.md +++ b/TODO.md @@ -40,11 +40,12 @@ - [x] Step 3.3: Register router in server/__init__.py (with API key auth) ## Phase 4: Configuration (`tensors/config.py`) -- [ ] Step 4.1: Add ComfyUI config functions - - `get_comfyui_url()` - Get ComfyUI backend URL - - `get_comfyui_defaults()` - Get default generation settings +- [x] Step 4.1: Add ComfyUI config functions + - `get_comfyui_url()` - Get ComfyUI backend URL (COMFYUI_URL env -> config -> default) + - `get_comfyui_defaults()` - Get default generation settings from config - Environment variable: `COMFYUI_URL` - - Config section: `[comfyui]` + - Config section: `[comfyui]` with url, default_model, width, height, steps, cfg, sampler, scheduler + - Updated `comfyui.py` to use config functions --- diff --git a/tensors/comfyui.py b/tensors/comfyui.py index 50fe3a8..960f345 100644 --- a/tensors/comfyui.py +++ b/tensors/comfyui.py @@ -4,7 +4,6 @@ from __future__ import annotations import copy import json -import os import random import time import uuid @@ -17,19 +16,18 @@ from typing import TYPE_CHECKING, Any import httpx from rich.progress import Progress, SpinnerColumn, TextColumn +from tensors.config import get_comfyui_url + if TYPE_CHECKING: from rich.console import Console -# Default ComfyUI URL (same as comfyui_routes.py) -COMFYUI_DEFAULT_URL = "http://127.0.0.1:8188" - # Progress update throttle interval (seconds) _PROGRESS_UPDATE_INTERVAL = 0.25 def _get_comfyui_url() -> str: - """Get ComfyUI URL from environment or default.""" - return os.environ.get("COMFYUI_URL", COMFYUI_DEFAULT_URL) + """Get ComfyUI URL from config (env var -> config file -> default).""" + return get_comfyui_url() # ============================================================================ diff --git a/tensors/config.py b/tensors/config.py index 8f0b8c9..2cf9fe9 100644 --- a/tensors/config.py +++ b/tensors/config.py @@ -486,3 +486,99 @@ def get_server_api_key() -> str | None: return str(key) return None + + +# ============================================================================ +# ComfyUI Configuration +# ============================================================================ + +COMFYUI_DEFAULT_URL = "http://127.0.0.1:8188" + +# Default generation parameters +COMFYUI_DEFAULT_WIDTH = 1024 +COMFYUI_DEFAULT_HEIGHT = 1024 +COMFYUI_DEFAULT_STEPS = 20 +COMFYUI_DEFAULT_CFG = 7.0 +COMFYUI_DEFAULT_SAMPLER = "euler" +COMFYUI_DEFAULT_SCHEDULER = "normal" + + +def get_comfyui_url() -> str: + """Get the ComfyUI server URL. + + Resolution order: + 1. COMFYUI_URL environment variable + 2. config.toml [comfyui].url + 3. Default: http://127.0.0.1:8188 + + Config example: + [comfyui] + url = "http://192.168.1.100:8188" + """ + # Check environment variable first + env_url = os.environ.get("COMFYUI_URL") + if env_url: + return env_url + + # Check config file + config = load_config() + comfyui_config = config.get("comfyui", {}) + if isinstance(comfyui_config, dict): + url = comfyui_config.get("url") + if url: + return str(url) + + return COMFYUI_DEFAULT_URL + + +def get_comfyui_defaults() -> dict[str, Any]: + """Get default ComfyUI generation parameters. + + Resolution order (per parameter): + 1. config.toml [comfyui] section values + 2. Built-in defaults + + Config example: + [comfyui] + url = "http://127.0.0.1:8188" + default_model = "flux1-dev-fp8.safetensors" + width = 1024 + height = 1024 + steps = 20 + cfg = 7.0 + sampler = "euler" + scheduler = "normal" + + Returns dict with keys: model, width, height, steps, cfg, sampler, scheduler + """ + config = load_config() + comfyui_config = config.get("comfyui", {}) + + defaults: dict[str, Any] = { + "model": None, + "width": COMFYUI_DEFAULT_WIDTH, + "height": COMFYUI_DEFAULT_HEIGHT, + "steps": COMFYUI_DEFAULT_STEPS, + "cfg": COMFYUI_DEFAULT_CFG, + "sampler": COMFYUI_DEFAULT_SAMPLER, + "scheduler": COMFYUI_DEFAULT_SCHEDULER, + } + + if isinstance(comfyui_config, dict): + # Override with config values if present + if "default_model" in comfyui_config: + defaults["model"] = str(comfyui_config["default_model"]) + if "width" in comfyui_config: + defaults["width"] = int(comfyui_config["width"]) + if "height" in comfyui_config: + defaults["height"] = int(comfyui_config["height"]) + if "steps" in comfyui_config: + defaults["steps"] = int(comfyui_config["steps"]) + if "cfg" in comfyui_config: + defaults["cfg"] = float(comfyui_config["cfg"]) + if "sampler" in comfyui_config: + defaults["sampler"] = str(comfyui_config["sampler"]) + if "scheduler" in comfyui_config: + defaults["scheduler"] = str(comfyui_config["scheduler"]) + + return defaults