tools.generate_image module

Generate images via native Gemini API and send to the current channel.

async tools.generate_image.run(prompt, aspect_ratio='16:9', model=None, ctx=None)[source]

Generate an image from a prompt and post it to the current channel.

Entry point for the generate_image tool. It resolves a Gemini key with _resolve_api_key (preferring the user’s own), and – for users on the shared fallback key – enforces the _IMAGE_DAILY_LIMIT daily quota via _default_key_image_quota_applies and check_default_key_limit (both backed by Redis through manage_api_keys), returning the IMAGE_RATE_LIMIT_ERROR JSON when exhausted. It then calls _call_gemini_native to render the image, normalises the bytes to PNG via the nested _convert_to_png on asyncio.to_thread, and uploads the result to the channel through ctx.adapter.send_file, recording it in ctx.sent_files. On a successful shared-key generation it bumps the usage counter with increment_default_key_usage. Failures are caught and returned as a JSON error object.

Dispatched by tool_loader.py, which imports this module and resolves run via getattr(module, "run") to register it under TOOL_NAME (“generate_image”).

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

  • aspect_ratio (str) – Requested aspect ratio (default "16:9"), passed through to _call_gemini_native.

  • model (str | None) – Optional Gemini image model id; defaults are applied downstream.

  • ctx (ToolContext | None) – The current ToolContext; its adapter is required to send the image and its user_id / redis drive quota enforcement. When ctx or ctx.adapter is missing the call returns an error.

Return type:

str

Returns:

A JSON string with success: True plus filename, a result message, and (when available) file_url on success; otherwise a JSON object with an error describing a quota block, an empty result, or a raised exception.