---
name: imagine
description: Generate an image with Flux 2 Klein via ComfyUI and save the visual workflow for learning.
allowed-tools: Read, Write, Bash, Glob, AskUserQuestion
argument-hint: "[description]"
---

# /imagine — ComfyUI Image Generation

Generate an image with Flux 2 Klein, submit via API, and save the workflow so it appears in ComfyUI's visual canvas for learning and iteration.

## Usage

```
/imagine a woman in mechanic overalls on a spaceship
/imagine jinx looking scared, close-up, dramatic lighting
/imagine wide shot of a dark corridor, rust-punk, amber lights
/imagine --seed 42 --size 1024x1024 a man in a leather jacket
```

## Parameters

| Parameter | Default | Options |
|-----------|---------|---------|
| `--seed` | random | Any integer |
| `--size` | 768x1024 | WxH (e.g., 1024x1024, 1024x768, 768x1344) |
| `--steps` | 4 | 1-8 (Klein is optimized for 4) |
| `--prefix` | comfyui | Filename prefix for saved image |

## Execution Steps

### 1. Parse the input

Extract the description and any flags (--seed, --size, --steps, --prefix).

If no description provided, ask the user what they want to generate.

### 2. Enhance the prompt

Take the user's natural description and expand it into a production-quality prompt:
- Add cinematic modifiers (lighting, depth of field, lens type) if not specified
- Add quality terms (photorealistic, 4k detail, film grain) if not specified
- Keep the user's creative intent - don't override their specifics
- Show the user the enhanced prompt before generating

### 3. Build and submit the workflow

Build a ComfyUI API workflow with these nodes:
- **UnetLoaderGGUF** → `flux-2-klein-9b-Q8_0.gguf`
- **CLIPLoader** → `qwen_3_8b_fp8mixed.safetensors`, type `flux2`
- **VAELoader** → `flux2-vae.safetensors`
- **CLIPTextEncode** (positive) → the enhanced prompt
- **CLIPTextEncode** (negative) → empty string
- **EmptyLatentImage** → width/height from --size
- **KSampler** → seed, steps (default 4), cfg 1.0, euler, simple
- **VAEDecode** → decode latent to image
- **SaveImage** → save with prefix

Submit via POST to `http://127.0.0.1:8188/prompt`

### 4. Save visual workflow

Save a ComfyUI UI-format JSON to `~/ComfyUI/user/default/workflows/` so the user can:
- See it in the Workflows menu (may need to hit refresh)
- View and understand every node connection
- Modify the prompt or settings directly in the canvas
- Re-queue from the UI

The workflow JSON must include:
- Node positions (laid out left-to-right for readability)
- `widgets_values` matching the API parameters
- Links array connecting all nodes
- Proper output/input slot definitions

Name the workflow file: `imagine_{prefix}_{seed}.json`

### 5. Wait for result

Poll `http://127.0.0.1:8188/history/{prompt_id}` every 15 seconds until status is `success` or 3 minutes have passed.

### 6. Show the result

Read the output image using the Read tool (it supports images).

Provide a brief critique:
- What worked well
- What could be improved
- Suggest specific prompt tweaks if the user wants to iterate

### 7. Report to user

Output:
```
Image: ~/ComfyUI/output/{filename}
Workflow: ~/ComfyUI/user/default/workflows/imagine_{prefix}_{seed}.json
Seed: {seed}
Prompt: {the enhanced prompt}

To iterate: modify the prompt in ComfyUI and hit Queue,
or run /imagine again with tweaks.
```

## Workflow Node Layout

Arrange nodes left-to-right in this order for readability:

```
Column 1 (x=50):     CLIP Loader, GGUF Loader, VAE Loader
Column 2 (x=450):    Positive Prompt, Negative Prompt, Empty Latent
Column 3 (x=950):    KSampler
Column 4 (x=1350):   VAE Decode, Save Image
```

## ComfyUI Connectivity Check

Before submitting, verify ComfyUI is running:
```
curl -s http://127.0.0.1:8188/system_stats
```

If not running, tell the user:
```
ComfyUI isn't running. Start it with:
  cd ~/ComfyUI && ./start_comfyui.sh
```

## Error Handling

- If ComfyUI returns node_errors, report the specific error and suggest fixes
- If the workflow times out (3 min), check ComfyUI terminal for OOM or other errors
- If models aren't found, check ~/ComfyUI/models/ paths

## Tips for Good Prompts

- **Character shots**: Name, expression, clothing, action, camera angle, lighting
- **Environments**: Location, atmosphere, color palette, time of day, mood
- **Action shots**: Subject, action, motion blur, dynamic angle, intensity
- **Close-ups**: Feature focus, skin detail, emotion, eye direction, bokeh
- Always specify lighting style (dramatic, soft, amber, neon, etc.)
