|
| 1 | +"""Image generation and editing tool. |
| 2 | +
|
| 3 | +Generates or edits images using the configured image provider. |
| 4 | +
|
| 5 | +If the current conversation contains images (sent by the user via any channel), |
| 6 | +they are automatically available via the ``_incoming_image_paths`` extension and |
| 7 | +used as input images when the ``images`` parameter is omitted. |
| 8 | +
|
| 9 | +Flow: |
| 10 | + 1. User sends a message — optionally with one or more images + caption. |
| 11 | + 2. Agent calls ``imagegen`` with the caption as ``prompt``. |
| 12 | + 3. Tool picks up any incoming image paths automatically. |
| 13 | + 4. Provider generates or edits the image. |
| 14 | + 5. Result is sent back to the user and the output path is returned. |
| 15 | +""" |
| 16 | + |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +import uuid |
| 20 | +from datetime import datetime |
| 21 | +from pathlib import Path |
| 22 | +from typing import Optional |
| 23 | + |
| 24 | +from pydantic import BaseModel, Field |
| 25 | + |
| 26 | +from operator_use.bus.views import ImagePart, OutgoingMessage, TextPart |
| 27 | +from operator_use.tools import Tool, ToolResult |
| 28 | + |
| 29 | + |
| 30 | +class ImageGen(BaseModel): |
| 31 | + prompt: str = Field( |
| 32 | + description=( |
| 33 | + "Text description of the image to generate, or the edit/modification to apply " |
| 34 | + "when input images are provided (e.g. 'make it look like a watercolour painting', " |
| 35 | + "'add a sunset sky', 'remove the background')." |
| 36 | + ) |
| 37 | + ) |
| 38 | + images: Optional[list[str]] = Field( |
| 39 | + default=None, |
| 40 | + description=( |
| 41 | + "Optional list of input image file paths to edit or use as references. " |
| 42 | + "If omitted, any images the user sent in the current message are used automatically. " |
| 43 | + "Pass an empty list [] to force pure text-to-image generation even when the user sent images." |
| 44 | + ), |
| 45 | + ) |
| 46 | + output_path: Optional[str] = Field( |
| 47 | + default=None, |
| 48 | + description=( |
| 49 | + "Where to save the generated image. " |
| 50 | + "If omitted, a unique file is created inside the agent workspace." |
| 51 | + ), |
| 52 | + ) |
| 53 | + caption: Optional[str] = Field( |
| 54 | + default=None, |
| 55 | + description="Optional caption to send alongside the generated image.", |
| 56 | + ) |
| 57 | + send_result: bool = Field( |
| 58 | + default=True, |
| 59 | + description="If True (default), send the generated image back to the user automatically.", |
| 60 | + ) |
| 61 | + |
| 62 | + |
| 63 | +@Tool( |
| 64 | + name="imagegen", |
| 65 | + description=( |
| 66 | + "Generate or edit an image using the configured image provider.\n\n" |
| 67 | + "Generation: call with just a prompt to create an image from scratch.\n" |
| 68 | + "Editing: provide input images (or let the tool pick up images the user sent) " |
| 69 | + "and a prompt describing the edit — e.g. 'make it a pencil sketch', " |
| 70 | + "'add a rainbow', 'change the background to a forest'.\n\n" |
| 71 | + "The generated image is sent back to the user automatically (send_result=True). " |
| 72 | + "The output file path is always returned so you can reference or share it further." |
| 73 | + ), |
| 74 | + model=ImageGen, |
| 75 | +) |
| 76 | +async def imagegen( |
| 77 | + prompt: str, |
| 78 | + images: list[str] | None = None, |
| 79 | + output_path: str | None = None, |
| 80 | + caption: str | None = None, |
| 81 | + send_result: bool = True, |
| 82 | + **kwargs, |
| 83 | +) -> ToolResult: |
| 84 | + provider = kwargs.get("_image_provider") |
| 85 | + if provider is None: |
| 86 | + return ToolResult.error_result( |
| 87 | + "No image provider is configured. " |
| 88 | + "Enable one under 'image' in config (e.g. provider: openai, model: dall-e-3)." |
| 89 | + ) |
| 90 | + |
| 91 | + # Resolve input images: explicit list → incoming message images → None (pure generation) |
| 92 | + # Passing [] explicitly skips auto-detection and forces generation. |
| 93 | + if images is None: |
| 94 | + images = kwargs.get("_incoming_image_paths") or None |
| 95 | + elif images == []: |
| 96 | + images = None |
| 97 | + |
| 98 | + # Build output path inside workspace if not provided |
| 99 | + if not output_path: |
| 100 | + workspace: Path | None = kwargs.get("_workspace") |
| 101 | + if workspace: |
| 102 | + gen_dir = workspace / "generated" |
| 103 | + else: |
| 104 | + import tempfile |
| 105 | + gen_dir = Path(tempfile.gettempdir()) / "operator_generated" |
| 106 | + gen_dir.mkdir(parents=True, exist_ok=True) |
| 107 | + ts = datetime.now().strftime("%Y%m%d_%H%M%S") |
| 108 | + output_path = str(gen_dir / f"{ts}_{uuid.uuid4().hex[:8]}.png") |
| 109 | + |
| 110 | + # Generate / edit |
| 111 | + try: |
| 112 | + await provider.agenerate(prompt, output_path, images=images) |
| 113 | + except Exception as e: |
| 114 | + return ToolResult.error_result(f"Image generation failed: {type(e).__name__}: {e}") |
| 115 | + |
| 116 | + # Send result back to user |
| 117 | + if send_result: |
| 118 | + bus = kwargs.get("_bus") |
| 119 | + channel = kwargs.get("_channel") |
| 120 | + chat_id = kwargs.get("_chat_id") |
| 121 | + account_id = kwargs.get("_account_id", "") |
| 122 | + |
| 123 | + if bus and channel and chat_id: |
| 124 | + parts: list = [ImagePart(paths=[output_path])] |
| 125 | + if caption: |
| 126 | + parts.append(TextPart(content=caption)) |
| 127 | + await bus.publish_outgoing( |
| 128 | + OutgoingMessage( |
| 129 | + channel=channel, |
| 130 | + chat_id=chat_id, |
| 131 | + account_id=account_id, |
| 132 | + parts=parts, |
| 133 | + ) |
| 134 | + ) |
| 135 | + |
| 136 | + action = "edited" if images else "generated" |
| 137 | + return ToolResult.success_result(f"Image {action} and saved to: {output_path}") |
0 commit comments