tools.comfyui_generate_image module

Image generation via Civitai API.

Generates AI images using Civitai’s generation service and returns the image file path or download URL.

async tools.comfyui_generate_image.run(prompt, negative_prompt=None, width=1024, height=1024, steps=30, cfg=4.0, seed=None, scheduler='DPM2MKarras', model_urn=None, clip_skip=2, ctx=None)[source]

Generate one or more AI images via Civitai and return their file paths.

Entry point for the civitai_generate_image tool. It resolves a Civitai API token (per-user via tools.manage_api_keys.get_user_api_key when ctx has Redis and a user id, otherwise the environment token), temporarily installs it into os.environ so the civitai-py SDK can pick it up, clamps and normalizes the generation parameters (mapping scheduler aliases via SCHEDULER_MAP, capping width/height to the generator’s 1024 contract, defaulting the model to DEFAULT_MODEL_URN, randomizing the seed when unset), submits the job with retries through civitai.image.create, waits for it with _poll_job_status(), downloads each result with _download_image(), and writes them to per-image temp files. The original CIVITAI_API_TOKEN is always restored in a finally block.

Dispatched by tool_loader.py as the civitai_generate_image handler (located via getattr(module, "run")); not called directly elsewhere. Side effects include the temporary os.environ token swap, the Civitai HTTP calls, and writing PNG files into freshly created temp directories.

Parameters:
  • prompt (str) – Text description of the image to generate (required).

  • negative_prompt (str) – Optional description of what to avoid.

  • width (int) – Requested width in pixels (clamped to 64-2048; the API call is additionally capped at 1024).

  • height (int) – Requested height in pixels (clamped to 64-2048; API-capped at 1024).

  • steps (int) – Sampling steps (clamped to 1-100).

  • cfg (float) – CFG (classifier-free guidance) scale (1.0-20.0).

  • seed (int) – Random seed; a random value is chosen when None.

  • scheduler (str) – Sampling scheduler name or alias (mapped via SCHEDULER_MAP).

  • model_urn (str) – Optional Civitai model URN; defaults to DEFAULT_MODEL_URN.

  • clip_skip (int) – Number of CLIP layers to skip.

  • ctx (ToolContext | None) – Tool execution context; supplies Redis and the user id used to resolve a per-user API key.

Returns:

A JSON object with success, the saved files list, the seed, and the effective parameters on success, or an {"error": ...} object on missing prompt, missing key, API failure, or download failure.

Return type:

str