mirror of
https://github.com/calesthio/OpenMontage.git
synced 2026-08-25 01:20:18 +08:00
Merge remote-tracking branch 'origin/main' into codex/repair-pr-457
# Conflicts: # tools/graphics/image_selector.py
This commit is contained in:
@@ -58,7 +58,9 @@ class ComfyUIImage(BaseTool):
|
||||
install_instructions = (
|
||||
"Start a ComfyUI server and set COMFYUI_SERVER_URL "
|
||||
"(default http://localhost:8188).\n"
|
||||
"See https://github.com/comfyanonymous/ComfyUI for setup."
|
||||
"See https://github.com/comfyanonymous/ComfyUI for setup.\n"
|
||||
"Running a separate ComfyUI instance for images? Set COMFYUI_IMAGE_SERVER_URL "
|
||||
"instead -- it takes priority over COMFYUI_SERVER_URL for this tool only."
|
||||
)
|
||||
agent_skills = ["comfyui", "flux-best-practices"]
|
||||
|
||||
@@ -133,7 +135,7 @@ class ComfyUIImage(BaseTool):
|
||||
user_visible_verification = ["Inspect generated image for quality and prompt adherence"]
|
||||
|
||||
def __init__(self) -> None:
|
||||
self._client = ComfyUIClient()
|
||||
self._client = ComfyUIClient(capability="image")
|
||||
|
||||
def get_status(self) -> ToolStatus:
|
||||
if not self._client.is_available():
|
||||
|
||||
@@ -250,6 +250,8 @@ class ImageSelector(BaseTool):
|
||||
and "model" not in adapted
|
||||
):
|
||||
adapted["model"] = adapted["model_name"]
|
||||
if "n" in adapted and "num_images" in props and "num_images" not in adapted:
|
||||
adapted["num_images"] = adapted["n"]
|
||||
|
||||
# Strip selector-only keys that downstream tools don't understand
|
||||
adapted.pop("preferred_provider", None)
|
||||
|
||||
298
tools/graphics/minimax_image.py
Normal file
298
tools/graphics/minimax_image.py
Normal file
@@ -0,0 +1,298 @@
|
||||
"""MiniMax image generation through the first-party API."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import os
|
||||
import time
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from tools.base_tool import (
|
||||
BaseTool,
|
||||
Determinism,
|
||||
ExecutionMode,
|
||||
ResourceProfile,
|
||||
RetryPolicy,
|
||||
ToolResult,
|
||||
ToolRuntime,
|
||||
ToolStability,
|
||||
ToolTier,
|
||||
)
|
||||
|
||||
|
||||
MODELS = ["image-01", "image-01-live"]
|
||||
DEFAULT_MODEL = "image-01"
|
||||
DEFAULT_REGION = "global"
|
||||
# Official global pay-as-you-go rate for image-01/image-01-live.
|
||||
PRICE_PER_IMAGE_USD = 0.0035
|
||||
REGION_BASE_URLS = {
|
||||
"global": "https://api.minimax.io",
|
||||
"global_en": "https://api.minimax.io",
|
||||
"cn": "https://api.minimaxi.com",
|
||||
"cn_zh": "https://api.minimaxi.com",
|
||||
}
|
||||
|
||||
|
||||
class MiniMaxImage(BaseTool):
|
||||
name = "minimax_image"
|
||||
version = "0.1.0"
|
||||
tier = ToolTier.GENERATE
|
||||
capability = "image_generation"
|
||||
provider = "minimax"
|
||||
stability = ToolStability.BETA
|
||||
execution_mode = ExecutionMode.SYNC
|
||||
determinism = Determinism.SEEDED
|
||||
runtime = ToolRuntime.API
|
||||
|
||||
dependencies = ["env:MINIMAX_API_KEY"]
|
||||
install_instructions = (
|
||||
"Set MINIMAX_API_KEY to your MiniMax API key. "
|
||||
"Optionally set MINIMAX_REGION to global or cn."
|
||||
)
|
||||
# MiniMax is not a FLUX model. Use the provider-neutral visual direction
|
||||
# skill until a dedicated MiniMax prompting skill is available.
|
||||
agent_skills = ["visual-style"]
|
||||
|
||||
capabilities = ["generate_image", "text_to_image"]
|
||||
supports = {
|
||||
"multiple_outputs": True,
|
||||
"aspect_ratio": True,
|
||||
"custom_dimensions": True,
|
||||
"seed": True,
|
||||
"subject_reference": True,
|
||||
"url_response": True,
|
||||
"base64_response": True,
|
||||
}
|
||||
best_for = [
|
||||
"first-party MiniMax image generation",
|
||||
"seeded multi-image generation",
|
||||
"global and mainland China API routing",
|
||||
]
|
||||
not_good_for = ["offline generation"]
|
||||
|
||||
input_schema = {
|
||||
"type": "object",
|
||||
"required": ["prompt"],
|
||||
"properties": {
|
||||
"prompt": {"type": "string", "maxLength": 1500},
|
||||
"model": {
|
||||
"type": "string",
|
||||
"enum": MODELS,
|
||||
"default": DEFAULT_MODEL,
|
||||
},
|
||||
"subject_reference": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"required": ["type", "image_file"],
|
||||
"properties": {
|
||||
"type": {"type": "string", "enum": ["character"]},
|
||||
"image_file": {"type": "string"},
|
||||
},
|
||||
},
|
||||
},
|
||||
"aspect_ratio": {
|
||||
"type": "string",
|
||||
"enum": ["1:1", "16:9", "4:3", "3:2", "2:3", "3:4", "9:16", "21:9"],
|
||||
"default": "1:1",
|
||||
},
|
||||
"width": {"type": "integer", "minimum": 512, "maximum": 2048, "multipleOf": 8},
|
||||
"height": {"type": "integer", "minimum": 512, "maximum": 2048, "multipleOf": 8},
|
||||
"response_format": {
|
||||
"type": "string",
|
||||
"enum": ["url", "base64"],
|
||||
"default": "url",
|
||||
},
|
||||
"seed": {"type": "integer"},
|
||||
"n": {"type": "integer", "minimum": 1, "maximum": 9, "default": 1},
|
||||
"prompt_optimizer": {"type": "boolean", "default": False},
|
||||
"output_path": {"type": "string"},
|
||||
},
|
||||
}
|
||||
|
||||
resource_profile = ResourceProfile(
|
||||
cpu_cores=1, ram_mb=512, vram_mb=0, disk_mb=100, network_required=True
|
||||
)
|
||||
retry_policy = RetryPolicy(
|
||||
max_retries=2, retryable_errors=["rate_limit", "timeout"]
|
||||
)
|
||||
idempotency_key_fields = [
|
||||
"prompt",
|
||||
"model",
|
||||
"subject_reference",
|
||||
"aspect_ratio",
|
||||
"width",
|
||||
"height",
|
||||
"response_format",
|
||||
"seed",
|
||||
"n",
|
||||
"prompt_optimizer",
|
||||
]
|
||||
side_effects = [
|
||||
"writes image files to output_path",
|
||||
"calls the MiniMax image generation API",
|
||||
]
|
||||
user_visible_verification = [
|
||||
"Inspect generated images for prompt adherence and visual quality"
|
||||
]
|
||||
|
||||
@staticmethod
|
||||
def _region() -> str:
|
||||
region = os.environ.get("MINIMAX_REGION", DEFAULT_REGION).strip().lower()
|
||||
return region if region in REGION_BASE_URLS else DEFAULT_REGION
|
||||
|
||||
def _base_url(self) -> str:
|
||||
override = os.environ.get("MINIMAX_BASE_URL")
|
||||
if override:
|
||||
return override.rstrip("/")
|
||||
return REGION_BASE_URLS[self._region()]
|
||||
|
||||
@staticmethod
|
||||
def _base_resp_error(data: dict[str, Any]) -> str | None:
|
||||
base_resp = data.get("base_resp") or {}
|
||||
status_code = base_resp.get("status_code")
|
||||
if status_code in (None, 0):
|
||||
return None
|
||||
status_msg = base_resp.get("status_msg") or "unknown error"
|
||||
return f"MiniMax API error {status_code}: {status_msg}"
|
||||
|
||||
@staticmethod
|
||||
def _output_paths(output_path: str | None, count: int) -> list[Path]:
|
||||
path = Path(output_path or "minimax_image.png")
|
||||
if not path.suffix:
|
||||
path = path.with_suffix(".png")
|
||||
if count == 1:
|
||||
return [path]
|
||||
return [
|
||||
path.with_name(f"{path.stem}_{index}{path.suffix}")
|
||||
for index in range(1, count + 1)
|
||||
]
|
||||
|
||||
@staticmethod
|
||||
def _build_payload(inputs: dict[str, Any]) -> dict[str, Any]:
|
||||
model = inputs.get("model", DEFAULT_MODEL)
|
||||
if model not in MODELS:
|
||||
raise ValueError(f"Unsupported MiniMax image model '{model}'.")
|
||||
|
||||
prompt = inputs.get("prompt")
|
||||
if not isinstance(prompt, str) or not prompt:
|
||||
raise ValueError("MiniMax image generation requires 'prompt'.")
|
||||
if len(prompt) > 1500:
|
||||
raise ValueError("MiniMax image prompt must not exceed 1500 characters.")
|
||||
|
||||
width = inputs.get("width")
|
||||
height = inputs.get("height")
|
||||
if (width is None) != (height is None):
|
||||
raise ValueError("MiniMax image width and height must be set together.")
|
||||
|
||||
payload: dict[str, Any] = {
|
||||
"model": model,
|
||||
"prompt": prompt,
|
||||
"response_format": inputs.get("response_format", "url"),
|
||||
"n": inputs.get("n", 1),
|
||||
"prompt_optimizer": inputs.get("prompt_optimizer", False),
|
||||
}
|
||||
for field in (
|
||||
"subject_reference",
|
||||
"aspect_ratio",
|
||||
"width",
|
||||
"height",
|
||||
"seed",
|
||||
):
|
||||
if inputs.get(field) is not None:
|
||||
payload[field] = inputs[field]
|
||||
return payload
|
||||
|
||||
@staticmethod
|
||||
def _decode_base64_image(value: str) -> bytes:
|
||||
encoded = value.split(",", 1)[1] if value.startswith("data:") else value
|
||||
return base64.b64decode(encoded)
|
||||
|
||||
@staticmethod
|
||||
def _safe_error(exc: Exception, api_key: str) -> str:
|
||||
return str(exc).replace(api_key, "[redacted]") if api_key else str(exc)
|
||||
|
||||
def estimate_cost(self, inputs: dict[str, Any]) -> float:
|
||||
return PRICE_PER_IMAGE_USD * int(inputs.get("n", 1))
|
||||
|
||||
def execute(self, inputs: dict[str, Any]) -> ToolResult:
|
||||
api_key = os.environ.get("MINIMAX_API_KEY", "")
|
||||
if not api_key:
|
||||
return ToolResult(
|
||||
success=False,
|
||||
error="MINIMAX_API_KEY not set. " + self.install_instructions,
|
||||
)
|
||||
|
||||
import requests
|
||||
|
||||
start = time.time()
|
||||
try:
|
||||
payload = self._build_payload(inputs)
|
||||
response = requests.post(
|
||||
f"{self._base_url()}/v1/image_generation",
|
||||
headers={
|
||||
"Authorization": f"Bearer {api_key}",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
json=payload,
|
||||
timeout=180,
|
||||
)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
|
||||
base_error = self._base_resp_error(data)
|
||||
if base_error:
|
||||
return ToolResult(success=False, error=base_error)
|
||||
|
||||
response_format = payload["response_format"]
|
||||
data_object = data.get("data") or {}
|
||||
image_values = data_object.get(
|
||||
"image_base64" if response_format == "base64" else "image_urls"
|
||||
) or []
|
||||
if not image_values:
|
||||
return ToolResult(
|
||||
success=False,
|
||||
error=f"MiniMax returned no {response_format} image outputs.",
|
||||
)
|
||||
|
||||
output_paths = self._output_paths(
|
||||
inputs.get("output_path"), len(image_values)
|
||||
)
|
||||
for path, value in zip(output_paths, image_values):
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
if response_format == "base64":
|
||||
path.write_bytes(self._decode_base64_image(value))
|
||||
else:
|
||||
download = requests.get(value, timeout=120)
|
||||
download.raise_for_status()
|
||||
path.write_bytes(download.content)
|
||||
except Exception as exc:
|
||||
return ToolResult(
|
||||
success=False,
|
||||
error=(
|
||||
"MiniMax image generation failed: "
|
||||
f"{self._safe_error(exc, api_key)}"
|
||||
),
|
||||
)
|
||||
|
||||
outputs = [str(path) for path in output_paths]
|
||||
return ToolResult(
|
||||
success=True,
|
||||
data={
|
||||
"provider": "minimax",
|
||||
"model": payload["model"],
|
||||
"prompt": payload["prompt"],
|
||||
"region": self._region(),
|
||||
"response_format": payload["response_format"],
|
||||
"output": outputs[0],
|
||||
"outputs": outputs,
|
||||
"images_generated": len(outputs),
|
||||
"metadata": data.get("metadata") or {},
|
||||
"request_id": data.get("id"),
|
||||
},
|
||||
artifacts=outputs,
|
||||
cost_usd=self.estimate_cost(inputs),
|
||||
duration_seconds=round(time.time() - start, 2),
|
||||
model=payload["model"],
|
||||
)
|
||||
275
tools/graphics/seedream_image.py
Normal file
275
tools/graphics/seedream_image.py
Normal file
@@ -0,0 +1,275 @@
|
||||
"""Seedream V5 image generation via fal.ai API.
|
||||
deep-thinking prompt understanding, native text in 14 languages, and precise control over dense layouts and structured designs.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import time
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from tools.base_tool import (
|
||||
BaseTool,
|
||||
Determinism,
|
||||
ExecutionMode,
|
||||
ResourceProfile,
|
||||
RetryPolicy,
|
||||
ToolResult,
|
||||
ToolRuntime,
|
||||
ToolStability,
|
||||
ToolStatus,
|
||||
ToolTier,
|
||||
)
|
||||
class SeedreamImage(BaseTool):
|
||||
name = "seedream_image"
|
||||
version = "0.1.0"
|
||||
tier = ToolTier.GENERATE
|
||||
capability = "image_generation"
|
||||
provider = "bytedance"
|
||||
stability = ToolStability.EXPERIMENTAL
|
||||
execution_mode = ExecutionMode.ASYNC
|
||||
determinism = Determinism.STOCHASTIC
|
||||
runtime = ToolRuntime.API
|
||||
|
||||
dependencies = ["env:FAL_KEY"]
|
||||
install_instructions = (
|
||||
"Set FAL_KEY to your fal.ai API key.\n"
|
||||
" Get one at https://fal.ai/dashboard/keys"
|
||||
)
|
||||
agent_skills = ["visual-style"]
|
||||
|
||||
capabilities = [
|
||||
"generate_image",
|
||||
"text_to_image",
|
||||
"structured_designs",
|
||||
"dense_layouts",
|
||||
"multi_language_text",
|
||||
]
|
||||
supports = {
|
||||
"text_rendering": True,
|
||||
"color_palette": True,
|
||||
"custom_size": True,
|
||||
"structured_designs": True,
|
||||
"dense_layouts": True,
|
||||
"multi_language_text": True,
|
||||
}
|
||||
best_for = [
|
||||
"raster brand and campaign assets",
|
||||
"images with accurate text rendering",
|
||||
"structured designs and dense layouts",
|
||||
"multi-language text rendering (14 languages)",
|
||||
]
|
||||
input_schema = {
|
||||
"type": "object",
|
||||
"required": ["prompt"],
|
||||
"properties": {
|
||||
"prompt": {"type": "string"},
|
||||
"image_size": {
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"square", "square_hd",
|
||||
"landscape_4_3", "landscape_16_9",
|
||||
"portrait_4_3", "portrait_16_9",
|
||||
"auto_1K","auto_2K"
|
||||
],
|
||||
"default": "auto_2K",
|
||||
},
|
||||
"num_images": {
|
||||
"type": "integer",
|
||||
"minimum": 1,
|
||||
"maximum": 4,
|
||||
"default": 1,
|
||||
},
|
||||
"output_format": {
|
||||
"type": "string",
|
||||
"enum": ["jpeg", "png"],
|
||||
"description": "Output image format. Use 'jpeg' for smaller file size with lossy compression (suitable for web/preview), or 'png' for lossless quality with transparency support (suitable for design assets and further editing).",
|
||||
},
|
||||
"enable_safety_checker": {
|
||||
"type": "boolean",
|
||||
"default": True,
|
||||
"description": "If set to true, the safety checker will be enabled.",
|
||||
},
|
||||
"output_path": {"type": "string"}
|
||||
},
|
||||
}
|
||||
resource_profile = ResourceProfile(
|
||||
cpu_cores=1, ram_mb=512, vram_mb=0, disk_mb=100, network_required=True
|
||||
)
|
||||
retry_policy = RetryPolicy(max_retries=2, retryable_errors=["rate_limit", "timeout"])
|
||||
idempotency_key_fields = [
|
||||
"prompt",
|
||||
"image_size",
|
||||
"output_format",
|
||||
"num_images",
|
||||
"enable_safety_checker",
|
||||
]
|
||||
side_effects = ["writes image file to output_path", "calls fal.ai queue API"]
|
||||
user_visible_verification = ["Inspect generated image for brand accuracy and text readability"]
|
||||
|
||||
def _get_api_key(self) -> str | None:
|
||||
return os.environ.get("FAL_KEY") or os.environ.get("FAL_AI_API_KEY")
|
||||
|
||||
def get_status(self) -> ToolStatus:
|
||||
if self._get_api_key():
|
||||
return ToolStatus.AVAILABLE
|
||||
return ToolStatus.UNAVAILABLE
|
||||
|
||||
def estimate_cost(self, inputs: dict[str, Any]) -> float:
|
||||
image_size = inputs.get("image_size", "auto_2K")
|
||||
num_images = inputs.get("num_images", 1)
|
||||
size_price_map = {
|
||||
"square": 0.0675,
|
||||
"square_hd": 0.135,
|
||||
"landscape_4_3": 0.0675,
|
||||
"landscape_16_9": 0.135,
|
||||
"portrait_4_3": 0.0675,
|
||||
"portrait_16_9": 0.135,
|
||||
"auto_1K": 0.0675,
|
||||
"auto_2K": 0.135,
|
||||
}
|
||||
unit_price = size_price_map.get(image_size, 0.135)
|
||||
return round(unit_price * num_images, 4)
|
||||
|
||||
@staticmethod
|
||||
def _output_paths(
|
||||
output_path: str | None, count: int, output_format: str
|
||||
) -> list[Path]:
|
||||
path = Path(output_path or f"seedream_image.{output_format}")
|
||||
if not path.suffix:
|
||||
path = path.with_suffix(f".{output_format}")
|
||||
if count == 1:
|
||||
return [path]
|
||||
return [
|
||||
path.with_name(f"{path.stem}_{index}{path.suffix}")
|
||||
for index in range(1, count + 1)
|
||||
]
|
||||
|
||||
def execute(self, inputs: dict[str, Any]) -> ToolResult:
|
||||
import requests
|
||||
|
||||
api_key = self._get_api_key()
|
||||
if not api_key:
|
||||
return ToolResult(
|
||||
success=False,
|
||||
error="FAL_KEY not set. " + self.install_instructions,
|
||||
)
|
||||
|
||||
start = time.time()
|
||||
prompt = inputs["prompt"]
|
||||
num_images = inputs.get("num_images", 1)
|
||||
if isinstance(num_images, bool) or not isinstance(num_images, int):
|
||||
return ToolResult(
|
||||
success=False, error="num_images must be an integer from 1 to 4."
|
||||
)
|
||||
if not 1 <= num_images <= 4:
|
||||
return ToolResult(
|
||||
success=False, error="num_images must be between 1 and 4."
|
||||
)
|
||||
submit_url = "https://queue.fal.run/bytedance/seedream/v5/pro/text-to-image"
|
||||
payload: dict[str, Any] = {
|
||||
"prompt": prompt,
|
||||
"image_size": inputs.get("image_size", "auto_2K"),
|
||||
"output_format": inputs.get("output_format", "jpeg"),
|
||||
"num_images": num_images,
|
||||
"enable_safety_checker": inputs.get("enable_safety_checker", True),
|
||||
}
|
||||
|
||||
try:
|
||||
headers = {
|
||||
"Authorization": f"Key {api_key}",
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
|
||||
submit_resp = requests.post(
|
||||
submit_url,
|
||||
headers=headers,
|
||||
json=payload,
|
||||
timeout=(10, 60),
|
||||
)
|
||||
submit_resp.raise_for_status()
|
||||
submit_data = submit_resp.json()
|
||||
request_id = submit_data.get("request_id")
|
||||
if not request_id:
|
||||
raise RuntimeError(
|
||||
"Seedream submit succeeded but did not return request_id"
|
||||
)
|
||||
status_url = (
|
||||
f"https://queue.fal.run/bytedance/seedream/requests/"
|
||||
f"{request_id}/status"
|
||||
)
|
||||
elapsed = 0.0
|
||||
while elapsed < 300:
|
||||
status_resp = requests.get(
|
||||
status_url,
|
||||
headers=headers,
|
||||
timeout=30,
|
||||
)
|
||||
status_resp.raise_for_status()
|
||||
status_data = status_resp.json()
|
||||
status = status_data.get("status")
|
||||
|
||||
if status == "COMPLETED":
|
||||
break
|
||||
elif status in ("FAILED", "CANCELLED"):
|
||||
error_msg = status_data.get("error", "Unknown error")
|
||||
raise RuntimeError(f"Seedream task {status}: {error_msg}")
|
||||
|
||||
time.sleep(10)
|
||||
elapsed += 10
|
||||
|
||||
if elapsed >= 300:
|
||||
raise RuntimeError(
|
||||
f"Seedream task timed out after {300}s"
|
||||
)
|
||||
|
||||
result_resp = requests.get(
|
||||
f"https://queue.fal.run/bytedance/seedream/requests/"
|
||||
f"{request_id}",
|
||||
headers=headers,
|
||||
timeout=30,
|
||||
)
|
||||
result_resp.raise_for_status()
|
||||
result_data = result_resp.json()
|
||||
|
||||
images = result_data.get("images", [])
|
||||
if not images:
|
||||
raise RuntimeError("Seedream completed but no images returned")
|
||||
|
||||
ext = inputs.get("output_format", "jpeg")
|
||||
expected_paths = self._output_paths(
|
||||
inputs.get("output_path"), len(images), ext
|
||||
)
|
||||
output_paths = []
|
||||
for img, output_path in zip(images, expected_paths):
|
||||
image_url = img.get("url")
|
||||
if not image_url:
|
||||
continue
|
||||
image_resp = requests.get(image_url, timeout=60)
|
||||
image_resp.raise_for_status()
|
||||
|
||||
output_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
output_path.write_bytes(image_resp.content)
|
||||
output_paths.append(str(output_path))
|
||||
|
||||
except Exception as e:
|
||||
return ToolResult(
|
||||
success=False,
|
||||
error=f"Seedream generation failed: {e}",
|
||||
)
|
||||
|
||||
return ToolResult(
|
||||
success=True,
|
||||
data={
|
||||
"provider": "seedream",
|
||||
"model": "seedream_v5",
|
||||
"prompt": prompt,
|
||||
"request_id": request_id,
|
||||
"image_count": len(output_paths),
|
||||
"outputs": output_paths,
|
||||
},
|
||||
artifacts=output_paths,
|
||||
cost_usd=self.estimate_cost(inputs),
|
||||
duration_seconds=round(time.time() - start, 2),
|
||||
model="fal-ai/bytedance/seedream/v5",
|
||||
)
|
||||
Reference in New Issue
Block a user