From 37e901bc30d4d2eb06eddd0c35fabeaf25033cba Mon Sep 17 00:00:00 2001 From: donnyjang Date: Mon, 5 Jan 2026 16:50:56 -0700 Subject: [PATCH] refactored workflow parameters to include a component property for the frontend, updated docs --- DOCUMENTATION.md | 186 ++++++++++++++++++++++++++++++++++------ parameters.py | 45 ++++++++-- schema_generator.py | 53 +++++++++--- workflows/basic_sdxl.py | 4 +- 4 files changed, 236 insertions(+), 52 deletions(-) diff --git a/DOCUMENTATION.md b/DOCUMENTATION.md index e52e9dc..23ba65b 100644 --- a/DOCUMENTATION.md +++ b/DOCUMENTATION.md @@ -1,6 +1,6 @@ # ImageLab External API Documentation -**Version:** 2.0 +**Version:** 2.1 **Base URL:** `http://127.0.0.1:8188` REST API endpoints for executing ComfyScript workflows with real-time progress monitoring. @@ -27,11 +27,20 @@ REST API endpoints for executing ComfyScript workflows with real-time progress m The ImageLab External API extends ComfyUI with: - **Workflow Execution**: Execute ComfyScript-based workflows via REST API -- **YAML Schema System**: Auto-generated UI schemas from Pydantic models +- **YAML Schema System**: Auto-generated UI schemas from Pydantic models with type/component separation - **Resource Discovery**: List and index models with CivitAI metadata - **Progress Broadcasting**: Real-time updates via WebSocket binary events - **Hot Reloading**: Workflows reload from disk on each execution +### Schema Design + +The YAML schema separates concerns: + +- **`type`**: Data type (int, float, str, bool, enum, object) +- **`component`**: Optional UI component hint (seed, prompt, sampler, etc.) + +This allows the frontend to use generic components based on type while supporting custom UI for specific fields. + ### Technology Stack | Component | Technology | @@ -162,7 +171,8 @@ Get the UI parameter schema for a workflow. Returns resolved options from `optio "parameters": [ { "name": "prompt", - "type": "text", + "type": "str", + "component": "prompt", "category": "prompts", "multiline": true, "required": true, @@ -174,6 +184,7 @@ Get the UI parameter schema for a workflow. Returns resolved options from `optio { "name": "sampler", "type": "enum", + "component": "sampler", "options_source": "samplers", "category": "parameters", "default": "euler", @@ -187,6 +198,28 @@ Get the UI parameter schema for a workflow. Returns resolved options from `optio "metadata": null } ] + }, + { + "name": "seed", + "type": "int", + "component": "seed", + "category": "parameters", + "required": true, + "label": "Seed" + }, + { + "name": "loras", + "type": "object", + "multiple": true, + "component": "loras", + "options_source": "loras", + "category": "models", + "default": [], + "label": "Loras", + "params": [ + { "name": "strength_model", "type": "float", "default": 1.0 }, + { "name": "strength_clip", "type": "float", "default": 1.0 } + ] } ] } @@ -538,39 +571,62 @@ UI parameter schemas are auto-generated from Pydantic `WorkflowParams` models us ### Annotated Types (parameters.py) +The `component` property specifies the UI component to render (optional). The `type` is inferred from the Python type and metadata. + ```python from typing import Annotated, List from pydantic import Field -# Text fields +# Text fields - component specifies custom UI Prompt = Annotated[str, Field( - json_schema_extra={"ui_type": "text", "category": "prompts", "multiline": True, "rows": 4} + json_schema_extra={"component": "prompt", "category": "prompts", "multiline": True, "rows": 4} )] +# Generated: type: str, component: prompt -# Special types +# Special types - int with custom UI component Seed = Annotated[int, Field( - json_schema_extra={"ui_type": "seed", "category": "parameters"} + json_schema_extra={"component": "seed", "category": "parameters"} )] +# Generated: type: int, component: seed -# Enum types (options resolved from resources) +# Enum types - options_source triggers type: enum Sampler = Annotated[str, Field( - json_schema_extra={"ui_type": "enum", "options_source": "samplers", "category": "parameters"} + json_schema_extra={"component": "sampler", "options_source": "samplers", "category": "parameters"} )] +# Generated: type: enum, component: sampler, options_source: samplers -# Model selectors +# Model selectors - multiple selection from options_source Checkpoint = Annotated[List[str], Field( - json_schema_extra={"ui_type": "model", "options_source": "checkpoints", "category": "models"} + json_schema_extra={"component": "checkpoints", "options_source": "checkpoints", "category": "models"} )] +# Generated: type: enum, multiple: true, component: checkpoints -# Dimension fields +# Dimension fields - no component = generic slider Width = Annotated[int, Field( ge=64, le=2048, multiple_of=8, - json_schema_extra={"category": "composition"} + json_schema_extra={"component": "dimensions", "category": "composition"} )] +# Generated: type: int, component: dimensions, min: 64, max: 2048, step: 8 ``` +### Type Inference Rules + +The `type` property represents the **data type**, inferred automatically: + +| Condition | Inferred `type` | +| ---------------------------- | ---------------------------- | +| Has `options_source` | `enum` | +| Has `params` (complex model) | `object` | +| Python `int` | `int` | +| Python `float` | `float` | +| Python `str` (no options) | `str` | +| Python `bool` | `bool` | +| Python `List[T]` | type of T + `multiple: true` | + ### YAML Format +The schema separates **data type** (`type`) from **UI component** (`component`): + ```yaml name: Basic SDXL Text to Image description: Workflow with SDXL presets for quick iteration. @@ -579,26 +635,76 @@ model_types: - SDXL parameters: + # Text field with custom component - name: prompt - type: text + type: str # Data type + component: prompt # UI component (optional) category: prompts multiline: true required: true label: Your Creative Prompt rows: 4 placeholder: Describe your image... - description: Enter a detailed description + # Seed - int with custom component + - name: seed + type: int # Data type + component: seed # Custom UI component + category: parameters + required: true + label: Seed + + # Enum - selection from options_source - name: sampler - type: enum + type: enum # Selection type (has options_source) + component: sampler # UI component (optional) options_source: samplers category: parameters default: euler label: Sampler searchable: true + # Multiple selection + - name: checkpoints + type: enum + multiple: true # List[str] -> multiple selections + component: checkpoints + options_source: checkpoints + category: models + required: true + label: Checkpoints + + # Complex type with params + - name: loras + type: object # Complex type (has params) + multiple: true + component: loras + options_source: loras + category: models + default: [] + label: Loras + params: + - name: strength_model + type: float + default: 1.0 + - name: strength_clip + type: float + default: 1.0 + + # Plain int with constraints (no custom component) + - name: steps + type: int + category: parameters + min: 1 + max: 150 + default: 20 + label: Steps + # No component = frontend uses generic SliderField + + # Dimensions with custom component - name: width type: int + component: dimensions category: composition min: 64 max: 2048 @@ -619,23 +725,47 @@ When regenerating YAML schemas: **Updated** (from code): -- `type`, `default`, `required` +- `type`, `component`, `default`, `required` - `min`, `max`, `step` - `category`, `options_source` - `multiple`, `params`, `multiline` -### Parameter Types +### Data Types (`type`) -| Type | Description | Properties | -| ------------------- | ------------------- | ---------------------------------- | -| `text` | Text input | `multiline`, `rows`, `placeholder` | -| `int` | Integer | `min`, `max`, `step` | -| `float` | Decimal | `min`, `max`, `step` | -| `bool` | Boolean toggle | - | -| `enum` | Dropdown | `options_source`, `searchable` | -| `seed` | Random seed | - | -| `model` | Model selector | `options_source`, `multiple` | -| `model_with_params` | Model with settings | `options_source`, `params` | +The `type` property represents the semantic data type: + +| Type | Description | Properties | +| -------- | ----------------------- | ------------------------------ | +| `str` | Text/string | `multiline`, `rows` | +| `int` | Integer | `min`, `max`, `step` | +| `float` | Decimal | `min`, `max`, `step` | +| `bool` | Boolean toggle | - | +| `enum` | Selection from list | `options_source`, `searchable` | +| `object` | Complex with parameters | `options_source`, `params` | + +### UI Components (`component`) + +The `component` property is an **optional** UI hint for custom rendering: + +| Component | Description | +| ----------------- | ----------------------------------------- | +| `prompt` | Multi-line text area for prompts | +| `negative_prompt` | Multi-line text area for negative prompts | +| `seed` | Seed input with randomize button | +| `sampler` | Sampler dropdown with search | +| `scheduler` | Scheduler dropdown | +| `checkpoints` | Model multi-select with metadata | +| `loras` | LoRA selector with strength sliders | +| `dimensions` | Width/Height linked inputs | + +**Component Resolution (Frontend):** + +| Priority | Check | Example | +| -------- | ------------------------- | ------------------------------------------ | +| 1 | `component` property | `component: "seed"` → SeedField | +| 2 | `options_source` property | `options_source: "samplers"` → SelectField | +| 3 | `type` + constraints | `type: "int"` + min/max → SliderField | +| 4 | `type` only | `type: "bool"` → ToggleField | ### options_source Values diff --git a/parameters.py b/parameters.py index d1fc441..922793c 100644 --- a/parameters.py +++ b/parameters.py @@ -8,41 +8,52 @@ from typing import Annotated, List, Optional # ============================================================================= # Annotated Types for UI Schema Generation # ============================================================================= +# +# Each type specifies: +# - component: The frontend UI component to render (optional, for custom UX) +# - category: Which tab/section to display the field in +# - options_source: For selections, where to get the options from +# +# If no component is specified, frontend uses generic components based on type. # Text fields Prompt = Annotated[str, Field( - json_schema_extra={"ui_type": "text", "category": "prompts", "multiline": True, "rows": 4} + json_schema_extra={"component": "prompt", "category": "prompts", "multiline": True, "rows": 4} )] NegativePrompt = Annotated[str, Field( - json_schema_extra={"ui_type": "text", "category": "prompts", "multiline": True, "rows": 2} + json_schema_extra={"component": "negative_prompt", "category": "prompts", "multiline": True, "rows": 2} )] # Special types Seed = Annotated[int, Field( - json_schema_extra={"ui_type": "seed", "category": "parameters"} + json_schema_extra={"component": "seed", "category": "parameters"} )] -# Enum types +# Enum types (selections from options_source) Sampler = Annotated[str, Field( - json_schema_extra={"ui_type": "enum", "options_source": "samplers", "category": "parameters"} + json_schema_extra={"component": "sampler", "options_source": "samplers", "category": "parameters"} )] Scheduler = Annotated[str, Field( - json_schema_extra={"ui_type": "enum", "options_source": "schedulers", "category": "parameters"} + json_schema_extra={"component": "scheduler", "options_source": "schedulers", "category": "parameters"} )] # Model selectors Checkpoint = Annotated[List[str], Field( - json_schema_extra={"ui_type": "model", "options_source": "checkpoints", "category": "models"} + json_schema_extra={"component": "checkpoints", "options_source": "checkpoints", "category": "models"} )] +# Loras - uses Lora class defined below +# The actual type is defined after Lora class, see Loras below + # Dimension fields (uses Pydantic's built-in constraints) +# No component specified - frontend uses generic slider based on type + constraints Width = Annotated[int, Field( ge=64, le=2048, multiple_of=8, - json_schema_extra={"category": "composition"} + json_schema_extra={"component": "dimensions", "category": "composition"} )] Height = Annotated[int, Field( ge=64, le=2048, multiple_of=8, - json_schema_extra={"category": "composition"} + json_schema_extra={"component": "dimensions", "category": "composition"} )] # ============================================================================= @@ -75,4 +86,20 @@ class Controlnet(BaseModel): disabled: bool = False parameters: ControlnetParameters = ControlnetParameters() +# ============================================================================= +# Annotated List Types (for model_with_params with category) +# ============================================================================= + +# Loras - List of Lora models with parameters +Loras = Annotated[List[Lora], Field( + default=[], + json_schema_extra={"component": "loras", "options_source": "loras", "category": "models"} +)] + +# Controlnets - List of Controlnet models with parameters +Controlnets = Annotated[List[Controlnet], Field( + default=[], + json_schema_extra={"component": "controlnets", "options_source": "controlnets", "category": "models"} +)] + # Add more shared types as needed diff --git a/schema_generator.py b/schema_generator.py index b0c45f4..3be3539 100644 --- a/schema_generator.py +++ b/schema_generator.py @@ -26,6 +26,7 @@ PRESERVE_PROPERTIES = { # Properties to update from Pydantic model UPDATE_PROPERTIES = { "type", + "component", # NEW: explicit UI component "default", "required", "min", @@ -140,7 +141,7 @@ def get_model_params_schema(field_type: type) -> List[Dict[str, Any]]: elif field_annotation is bool: param["type"] = "bool" elif field_annotation is str: - param["type"] = "text" + param["type"] = "str" # Get default if field_info.default is not None: @@ -176,7 +177,15 @@ def extract_field_schema( field_info: FieldInfo, field_type: type ) -> Dict[str, Any]: - """Extract schema from a Pydantic field.""" + """Extract schema from a Pydantic field. + + Type inference rules: + 1. If has options_source -> type: enum (selection from list) + 2. If has params (model_with_params) -> type: object + 3. Otherwise infer from Python type (int, float, str, bool) + + Component is optional - frontend falls back to generic rendering based on type. + """ schema: Dict[str, Any] = { "name": field_name, "label": camel_to_title(field_name), @@ -191,8 +200,11 @@ def extract_field_schema( else: extra = dict(field_info.json_schema_extra) - # Get ui_type from extra, or infer from Python type - ui_type = extra.pop("ui_type", None) + # Extract component (NEW: explicit UI component key) + component = extra.pop("component", None) + + # Extract options_source early - needed for type inference + options_source = extra.get("options_source") # Handle List types origin = get_origin(field_type) @@ -203,15 +215,26 @@ def extract_field_schema( if args: inner_type = args[0] - # Check if it's a model with parameters + # Infer type based on priority: + # 1. model_with_params (has params) + # 2. enum (has options_source) + # 3. primitive type (int, float, str, bool) + if is_model_with_params(field_type): - schema["type"] = "model_with_params" - schema["options_source"] = infer_options_source(field_name, field_type) + # Complex type with embedded parameters + schema["type"] = "object" + inferred_source = infer_options_source(field_name, field_type) + if not options_source: + schema["options_source"] = inferred_source + # For model_with_params, use options_source as component if not specified + if not component: + component = inferred_source or field_name schema["params"] = get_model_params_schema(field_type) - elif ui_type: - schema["type"] = ui_type + elif options_source: + # Selection from a list of options + schema["type"] = "enum" else: - # Infer type from Python type + # Primitive type - infer from Python type if inner_type is bool: schema["type"] = "bool" elif inner_type is int: @@ -219,11 +242,15 @@ def extract_field_schema( elif inner_type is float: schema["type"] = "float" elif inner_type is str: - schema["type"] = "text" + schema["type"] = "str" else: - schema["type"] = "text" # Default fallback + schema["type"] = "str" # Default fallback - # Copy extra properties (category, multiline, rows, options_source, etc.) + # Add component if specified (optional UI hint) + if component: + schema["component"] = component + + # Copy remaining extra properties (category, multiline, rows, options_source, etc.) for key, value in extra.items(): schema[key] = value diff --git a/workflows/basic_sdxl.py b/workflows/basic_sdxl.py index 6a9063a..94433b9 100644 --- a/workflows/basic_sdxl.py +++ b/workflows/basic_sdxl.py @@ -15,7 +15,7 @@ sys.path.insert(0, os.path.dirname(os.path.dirname(__file__))) from base import BaseWorkflow from parameters import ( Prompt, NegativePrompt, Checkpoint, Seed, Sampler, Scheduler, - Width, Height, Lora + Width, Height, Loras ) # ============================================ @@ -42,7 +42,7 @@ class WorkflowParams(BaseModel): prompt: Prompt negativePrompt: NegativePrompt = "text, watermark" checkpoints: Checkpoint - loras: List[Lora] = [] # Generator infers params from Lora class + loras: Loras # Uses shared type with category: models sampler: Sampler = "euler" scheduler: Scheduler = "normal" seed: Seed # Required, no default