Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

fix: 图像生成tool增加image参数

openJiuwen-bot78ecb4469bf0
diff --git a/jiuwenswarm/agents/harness/common/tools/image_tools.py b/jiuwenswarm/agents/harness/common/tools/image_tools.py
index 7816090d9a..1aa4492aa7 100644
--- a/jiuwenswarm/agents/harness/common/tools/image_tools.py
+++ b/jiuwenswarm/agents/harness/common/tools/image_tools.py
@@ -67,6 +67,32 @@ class _MimeResolver:
return cls._EXT_MAP.get(ext.lower(), "image/jpeg")
+# 图片内容魔数白名单:用于校验 LLM 提供的输入确实是图片,防止任意本地
+# 文件(如 ~/.ssh/id_rsa、config.yaml)被 base64 后外发到远端图像 API。
+_IMAGE_SIGNATURES: tuple[tuple[bytes, str], ...] = (
+ (b"\x89PNG\r\n\x1a\n", "image/png"),
+ (b"\xff\xd8\xff", "image/jpeg"),
+ (b"GIF87a", "image/gif"),
+ (b"GIF89a", "image/gif"),
+ (b"BM", "image/bmp"),
+)
+
+
+def _sniff_image_mime(data: bytes) -> str | None:
+ """Sniff the real content type from magic bytes.
+
+ Returns the sniffed MIME type, or None when the bytes do not look like a
+ supported image format (PNG/JPEG/GIF/BMP/WEBP). Unlike extension-based
+ resolution, this cannot be fooled by a renamed file.
+ """
+ if data.startswith(b"RIFF") and len(data) >= 12 and data[8:12] == b"WEBP":
+ return "image/webp"
+ for sig, mime in _IMAGE_SIGNATURES:
+ if data.startswith(sig):
+ return mime
+ return None
+
+
class _RetryExecutor:
@staticmethod
async def with_backoff(
@@ -123,9 +149,9 @@ def _get_image_gen_api_credentials():
return k, b, m, p
-def _make_sandbox_error_msg() -> str:
+def _make_sandbox_error_msg(tool_name: str = "visual_question_answering") -> str:
return (
- "The visual_question_answering tool cannot access to sandbox file, "
+ f"The {tool_name} tool cannot access to sandbox file, "
"please use the local path provided by original instruction"
)
@@ -419,18 +445,168 @@ def _parse_optional_seed(inputs: dict[str, Any]) -> int | None:
return None
+def _resolve_edit_images(inputs: dict[str, Any]) -> list[str] | None:
+ """Resolve optional input image(s) for image editing (e.g. watermark removal).
+
+ Accepts a local file path, an http(s) URL, or a data: URL. A list may be
+ given for multi-image fusion (comma-joined per Huawei MaaS contract).
+ Local files are inlined as base64 data URLs because the remote API cannot
+ reach local paths. Images are sent as-is — dimension limits are the
+ service's contract and surface through its error message, so switching
+ models does not require local adjustment here.
+
+ Security: local paths and data URLs are validated by magic bytes against
+ a supported-image whitelist (PNG/JPEG/GIF/BMP/WEBP), so non-image files
+ are rejected with a clear error instead of being exfiltrated to the
+ remote image API. The MIME type comes from the sniffed content, not the
+ file extension. Sandbox-style paths get the same dedicated error message
+ as the visual_question_answering tool.
+
+ Raises ValueError on unreadable/oversized/invalid inputs.
+ Returns None when no image input was provided.
+ """
+ raw = inputs.get("image")
+ if raw is None or (isinstance(raw, str) and not raw.strip()):
+ return None
+ items = [str(s).strip() for s in raw] if isinstance(raw, (list, tuple)) else [str(raw).strip()]
+ items = [s for s in items if s]
+ if not items:
+ return None
+ resolved: list[str] = []
+ for item in items:
+ if item.startswith("data:"):
+ _, _, b64 = item.partition(",")
+ try:
+ raw_bytes = base64.b64decode(b64)
+ except Exception:
+ raw_bytes = b""
+ if _sniff_image_mime(raw_bytes) is None:
+ raise ValueError(
+ "data URL image content is not a recognized image "
+ "(PNG/JPEG/GIF/BMP/WEBP); refusing to send non-image content"
+ )
+ resolved.append(item)
+ continue
+ if item.startswith(("http://", "https://")):
+ resolved.append(item)
+ continue
+ if not os.path.isfile(item):
+ if _PathHelper.is_sandbox(item):
+ raise ValueError(_make_sandbox_error_msg("generate_image"))
+ raise ValueError(f"input image not found: {item}")
+ with open(item, "rb") as f:
+ data = f.read()
+ if len(data) > 20 * 1024 * 1024:
+ raise ValueError(f"input image too large (>20MB): {item}")
+ mime = _sniff_image_mime(data)
+ if mime is None:
+ raise ValueError(
+ f"input image is not a recognized image file "
+ f"(PNG/JPEG/GIF/BMP/WEBP): {item}"
+ )
+ resolved.append(f"data:{mime};base64,{base64.b64encode(data).decode('utf-8')}")
+ return resolved
+
+
+_MAAS_DIMENSION_ERROR_RE = re.compile(
+ r"allowed area range is:\[\s*(\d+)\s*x\s*(\d+)\s*,\s*(\d+)\s*x\s*(\d+)\s*\]"
+)
+
+
+def _try_auto_resize_from_dimension_error(
+ exc: Exception, edit_images: list[str] | None
+) -> list[str] | None:
+ """Error-driven edit-image auto-resize (no locally hard-coded limits).
+
+ When the image service rejects an edit input image for its dimension range
+ (e.g. Huawei MaaS ModelArts.81001: "allowed area range is:
+ [512x512,3072x3072]. Current area: 358 x 117"), parse the allowed range
+ from the error message — the authoritative source for the *current* model,
+ so switching models needs no local adjustment — and scale the data-URL
+ input image(s) proportionally into that range.
+
+ Returns the resized list, or None when the error is not dimension-related,
+ no input images exist, nothing needs resizing, or the images cannot be
+ resized locally (http(s) URLs / Pillow missing / decode failure).
+ """
+ if not edit_images:
+ return None
+ m = _MAAS_DIMENSION_ERROR_RE.search(str(exc))
+ if not m:
+ return None
+ lo_w, lo_h, hi_w, hi_h = (int(g) for g in m.groups())
+ min_side, max_side = min(lo_w, lo_h), max(hi_w, hi_h)
+ try:
+ from PIL import Image
+ except ImportError:
+ return None
+ import io
+
+ resized_any = False
+ out: list[str] = []
+ for item in edit_images:
+ if not item.startswith("data:"):
+ return None # http(s) URL 无法本地缩放,放弃自愈
+ _, _, b64 = item.partition(",")
+ try:
+ raw = base64.b64decode(b64)
+ with Image.open(io.BytesIO(raw)) as im:
+ w, h = im.size
+ scale = 1.0
+ if w < min_side or h < min_side:
+ scale = max(min_side / w, min_side / h)
+ if max(w, h) * scale > max_side:
+ scale = min(max_side / w, max_side / h)
+ if abs(scale - 1.0) < 1e-9:
+ out.append(item)
+ continue
+ # 单一 scale 同时作用于两边以保持长宽比;若等比缩放后仍有
+ # 边在范围外(极端长宽比,如 10000x100 → 3072x31),放弃自愈
+ # 并原样抛出服务端错误——独立钳制两边会导致拉伸失真。
+ nw = round(w * scale)
+ nh = round(h * scale)
+ if not (min_side <= nw <= max_side and min_side <= nh <= max_side):
+ logger.info(
+ "[generate_image] edit image %dx%d cannot fit "
+ "service-reported range [%d,%d] without aspect "
+ "distortion; giving up self-heal",
+ w, h, min_side, max_side,
+ )
+ return None
+ buf = io.BytesIO()
+ im.resize((nw, nh), Image.LANCZOS).save(buf, format="PNG")
+ out.append(
+ f"data:image/png;base64,{base64.b64encode(buf.getvalue()).decode('utf-8')}"
+ )
+ resized_any = True
+ logger.info(
+ "[generate_image] edit image auto-resized from %dx%d to %dx%d "
+ "per service-reported range [%d,%d]",
+ w, h, nw, nh, min_side, max_side,
+ )
+ except Exception:
+ logger.warning(
+ "[generate_image] failed to auto-resize edit image; giving up self-heal",
+ exc_info=True,
+ )
+ return None
+ if not resized_any:
+ return None
+ return out
+
+
def _build_image_gen_kwargs(
provider: str,
inputs: dict[str, Any],
*,
- size: str,
+ size: str | None,
n: int,
api_base: str = "",
+ edit_images: list[str] | None = None,
) -> dict[str, Any]:
- gen_kwargs: dict[str, Any] = {
- "size": normalize_image_size(size, provider, api_base=api_base),
- "n": n,
- }
+ gen_kwargs: dict[str, Any] = {"n": n}
+ if size:
+ gen_kwargs["size"] = normalize_image_size(size, provider, api_base=api_base)
if _is_huawei_maas_config(provider, api_base):
# Huawei MaaS: single image, b64_json only; supports seed.
gen_kwargs["n"] = 1
@@ -438,6 +614,26 @@ def _build_image_gen_kwargs(
parsed_seed = _parse_optional_seed(inputs)
if parsed_seed is not None:
gen_kwargs["seed"] = parsed_seed
+ if edit_images:
+ # qwen-image-edit requires the input image as a body-level `image`
+ # field (base64 data URLs or public URLs; comma-joined for
+ # multi-image). The OpenAI SDK has no such parameter, so forward it
+ # via extra_body, which openjiuwen passes through to
+ # images.generate. When size is omitted, the service generates at
+ # the input image's dimensions — the natural behavior for editing.
+ gen_kwargs["extra_body"] = {"image": ",".join(edit_images)}
+ if not gen_kwargs.get("size"):
+ # 显式 None:省略键会落进 openjiuwen 的 size 形参默认
+ # '1664*928'(* 分隔),华为云要求 <width>x<height> 会 400;
+ # 显式 None 让请求体不带 size 字段,服务端按输入图尺寸生成。
+ gen_kwargs["size"] = None
+ if gen_kwargs.get("size") and not re.fullmatch(r"\d+x\d+", str(gen_kwargs["size"])):
+ # 无法归一化为 <width>x<height> 的值(如 "auto")直接丢弃,避免 400。
+ logger.warning(
+ "[generate_image] dropping invalid size %r for Huawei MaaS",
+ gen_kwargs["size"],
+ )
+ gen_kwargs["size"] = None
return gen_kwargs
if not _is_dashscope_provider(provider):
@@ -700,6 +896,20 @@ async def _invoke_model_image_generation(inputs: dict[str, Any]) -> dict:
if not prompt:
return {"error": "[ERROR]: prompt cannot be empty."}
+ try:
+ edit_images = _resolve_edit_images(inputs)
+ except ValueError as exc:
+ return {"error": f"[ERROR]: {exc}"}
+ if edit_images and not _is_huawei_maas_config(provider, api_base):
+ return {
+ "error": (
+ "[ERROR]: image editing (input image) is only supported on "
+ "Huawei MaaS qwen-image-edit models with the current provider; "
+ "either remove the input image or switch the image generation "
+ "account to a Huawei MaaS endpoint."
+ )
+ }
+
size_raw = str(inputs.get("size") or "").strip()
n_raw = inputs.get("n", 1)
try:
@@ -741,9 +951,12 @@ async def _invoke_model_image_generation(inputs: dict[str, Any]) -> dict:
gen_kwargs = _build_image_gen_kwargs(
provider,
inputs,
- size=size_raw or _default_size_for_provider(provider, api_base),
+ # 编辑场景未显式指定 size 时不传 size,让服务端按输入图尺寸生成。
+ size=size_raw
+ or (None if edit_images else _default_size_for_provider(provider, api_base)),
n=n,
api_base=api_base,
+ edit_images=edit_images,
)
async def _call():
@@ -772,6 +985,21 @@ async def _invoke_model_image_generation(inputs: dict[str, Any]) -> dict:
except asyncio.CancelledError:
cancel_event.set()
raise
+ except Exception as gen_exc:
+ # 错误驱动自愈:输入图尺寸越界被服务端拒绝时(如华为云
+ # ModelArts.81001),从错误信息解析出允许范围,把 data URL 编辑图
+ # 等比缩放进范围后重试一次。范围完全来自服务端而非本地硬编码,
+ # 换模型自适应;只重试一次,失败则原样抛出。
+ resized_images = _try_auto_resize_from_dimension_error(gen_exc, edit_images)
+ if resized_images is None:
+ raise
+ edit_images = resized_images
+ gen_kwargs["extra_body"] = {"image": ",".join(edit_images)}
+ try:
+ result = await asyncio.to_thread(_run_gen)
+ except asyncio.CancelledError:
+ cancel_event.set()
+ raise
out_dir = Path(save_dir) if save_dir else None
saved_paths = await asyncio.to_thread(
@@ -790,20 +1018,89 @@ async def _invoke_model_image_generation(inputs: dict[str, Any]) -> dict:
@tool(
name="generate_image",
description=(
- "Generate an image from a text description using AI image generation models. "
- "Use this tool when the user wants to create an image based on a text prompt. "
+ "Generate an image from a text description, or edit an input image "
+ "(e.g. watermark removal, style transfer, element changes) when the "
+ "configured model supports it (Huawei MaaS qwen-image-edit). "
"Supports provider-specific options: DashScope (prompt_extend, negative_prompt, seed), "
"Huawei MaaS (seed, b64_json response), OpenAI-compatible (standard sizes). "
"Returns the paths to the saved generated image file(s)."
),
+ # NOTE: docstring 的 "Args: inputs: Dict with keys: ..." 嵌套子键无法被
+ # schema 解析器提取(LLM 只能看到 "Dict with keys:"),因此显式声明
+ # input_params,让 LLM 直接看到每个键的名称、类型与用途。
+ input_params={
+ "type": "object",
+ "properties": {
+ "inputs": {
+ "type": "object",
+ "description": "Image generation / editing parameters",
+ "properties": {
+ "prompt": {
+ "type": "string",
+ "description": (
+ "Required. Text description of the image to generate, "
+ "or the editing instruction when an input image is provided"
+ ),
+ },
+ "image": {
+ "type": ["string", "array"],
+ "items": {"type": "string"},
+ "description": (
+ "Optional input image for editing (e.g. watermark removal): "
+ "a local file path, an http(s) URL, or a data: URL; a list of "
+ "these for multi-image fusion. Huawei MaaS qwen-image-edit "
+ "models only. When editing, size defaults to the input image "
+ "dimensions unless specified"
+ ),
+ },
+ "size": {
+ "type": "string",
+ "description": 'Image size, e.g. "1024x1024", "1792x1024", "1920*1080" (DashScope uses *)',
+ },
+ "quality": {
+ "type": "string",
+ "enum": ["standard", "hd"],
+ "description": "Image quality",
+ },
+ "n": {
+ "type": "integer",
+ "description": "Number of images to generate (1-4; Huawei MaaS forces 1)",
+ },
+ "negative_prompt": {
+ "type": "string",
+ "description": "Negative prompt to exclude elements (DashScope only)",
+ },
+ "prompt_extend": {
+ "type": "boolean",
+ "description": "Whether to extend the prompt automatically (DashScope only, default True)",
+ },
+ "seed": {
+ "type": "integer",
+ "description": "Random seed for reproducibility (DashScope, Huawei MaaS)",
+ },
+ "save_dir": {
+ "type": "string",
+ "description": "Optional directory to save images (defaults to workspace/generated_images)",
+ },
+ },
+ "required": ["prompt"],
+ }
+ },
+ "required": ["inputs"],
+ },
)
async def generate_image(inputs: dict[str, Any], **kwargs) -> str:
"""
- Generate an image from text description.
+ Generate an image from text description, or edit an input image.
Args:
inputs: Dict with keys:
- prompt (required): Text description of the image to generate
+ prompt (required): Text description of the image to generate, or the
+ editing instruction when an input image is provided
+ image: Optional input image for editing (e.g. watermark removal):
+ a local file path, an http(s) URL, or a data: URL; pass a list
+ of these for multi-image fusion. Huawei MaaS qwen-image-edit
+ only. When editing, size defaults to the input image dimensions
size: Image size, e.g., "1024x1024", "1792x1024", "1920*1080" (DashScope uses *)
quality: Image quality, "standard" or "hd"
n: Number of images to generate (1-4; Huawei MaaS forces 1)
@@ -824,8 +1121,9 @@ async def generate_image(inputs: dict[str, Any], **kwargs) -> str:
_, _, model, provider = _get_image_gen_api_credentials()
logger.info(
- "[generate_image] using model: %s, provider: %s, size: %s, n: %s",
+ "[generate_image] using model: %s, provider: %s, size: %s, n: %s, edit: %s",
model, provider, inputs.get("size", ""), inputs.get("n", 1),
+ bool(inputs.get("image")),
)
try:
diff --git a/tests/unit_tests/agents/harness/common/test_image_tools_edit.py b/tests/unit_tests/agents/harness/common/test_image_tools_edit.py
new file mode 100644
index 0000000000..862a789a70
--- /dev/null
+++ b/tests/unit_tests/agents/harness/common/test_image_tools_edit.py
@@ -0,0 +1,287 @@
+# Copyright (c) Huawei Technologies Co., Ltd. 2026. All rights reserved.
+
+"""generate_image 图像编辑/自愈链路的单元测试。
+
+对应 PR 审查意见:新增 _resolve_edit_images、_try_auto_resize_from_dimension_error
+与 _build_image_gen_kwargs 编辑分支需覆盖 happy path + 异常 + 边界:
+
+- _resolve_edit_images:本地内联 / magic bytes 白名单 / 20MB 上限 / 沙箱拦截 /
+ data URL 校验 / URL 直传 / 多图 / 空与非字符串输入
+- _try_auto_resize_from_dimension_error:尺寸错误正则解析 / 等比缩放 /
+ 极端长宽比放弃 / Pillow 缺失降级 / decode 失败 / 无关错误
+- _build_image_gen_kwargs:extra_body.image 拼接 / size 语义 / n 强制
+- 工具 schema:LLM 可见性
+"""
+
+from __future__ import annotations
+
+import base64
+import io
+import sys
+
+import pytest
+from PIL import Image
+
+from jiuwenswarm.agents.harness.common.tools import image_tools as it
+
+MAAS_BASE = "https://api.modelarts-maas.com/v1"
+
+_DIM_ERROR = (
+ "Error code: 400 - {'error': {'code': 'ModelArts.81001', 'message': "
+ "'Value error, Param image Error: qwen-image-edit-2509 allowed area "
+ "range is:[512x512,3072x3072]. Current area: 358 x 117'}}"
+)
+
+
+def _png_bytes(width: int, height: int) -> bytes:
+ buf = io.BytesIO()
+ Image.new("RGB", (width, height), color=(200, 30, 30)).save(buf, format="PNG")
+ return buf.getvalue()
+
+
+def _data_url(width: int, height: int, fmt: str = "PNG") -> str:
+ buf = io.BytesIO()
+ Image.new("RGB", (width, height), color=(200, 30, 30)).save(buf, format=fmt)
+ return "data:image/png;base64," + base64.b64encode(buf.getvalue()).decode()
+
+
+def _decode_data_url(item: str) -> Image.Image:
+ assert item.startswith("data:")
+ raw = base64.b64decode(item.partition(",")[2])
+ return Image.open(io.BytesIO(raw))
+
+
+# ---------------------------------------------------------------------------
+# _resolve_edit_images:happy path
+# ---------------------------------------------------------------------------
+
+
+def test_resolve_local_file_becomes_data_url(tmp_path) -> None:
+ p = tmp_path / "watermark.png"
+ p.write_bytes(_png_bytes(358, 117))
+ resolved = it._resolve_edit_images({"image": str(p)})
+ assert resolved is not None and len(resolved) == 1
+ assert resolved[0].startswith("data:image/png;base64,")
+ assert _decode_data_url(resolved[0]).size == (358, 117)
+
+
+def test_resolve_mime_from_real_content_not_extension(tmp_path) -> None:
+ # JPEG 内容存成 .png:sniff 以真实内容为准
+ buf = io.BytesIO()
+ Image.new("RGB", (600, 600)).save(buf, format="JPEG")
+ p = tmp_path / "actually_jpeg.png"
+ p.write_bytes(buf.getvalue())
+ resolved = it._resolve_edit_images({"image": str(p)})
+ assert resolved is not None
+ assert resolved[0].startswith("data:image/jpeg;base64,")
+
+
+def test_resolve_data_url_image_passthrough() -> None:
+ item = _data_url(600, 600)
+ assert it._resolve_edit_images({"image": item}) == [item]
+
+
+def test_resolve_http_url_passthrough() -> None:
+ url = "https://example.com/photo.png"
+ assert it._resolve_edit_images({"image": url}) == [url]
+
+
+def test_resolve_multi_image_list(tmp_path) -> None:
+ p = tmp_path / "a.png"
+ p.write_bytes(_png_bytes(600, 600))
+ resolved = it._resolve_edit_images({"image": [str(p), "https://example.com/b.jpg"]})
+ assert resolved is not None and len(resolved) == 2
+ assert resolved[0].startswith("data:image/png;base64,")
+ assert resolved[1] == "https://example.com/b.jpg"
+
+
+# ---------------------------------------------------------------------------
+# _resolve_edit_images:异常与边界
+# ---------------------------------------------------------------------------
+
+
[email protected]("raw", [None, "", " ", [], [""], [" ", ""]])
+def test_resolve_empty_image_returns_none(raw) -> None:
+ assert it._resolve_edit_images({"image": raw}) is None
+
+
+def test_resolve_non_string_items_report_not_found() -> None:
+ with pytest.raises(ValueError, match="not found"):
+ it._resolve_edit_images({"image": [123, ""]})
+
+
+def test_resolve_missing_file_raises(tmp_path) -> None:
+ with pytest.raises(ValueError, match="not found"):
+ it._resolve_edit_images({"image": str(tmp_path / "missing.png")})
+
+
+def test_resolve_sandbox_path_dedicated_error() -> None:
+ # 与 visual_question_answering 的沙箱报错语义对齐
+ with pytest.raises(ValueError, match="sandbox"):
+ it._resolve_edit_images({"image": "home/user/uploaded/a.png"})
+
+
+def test_resolve_non_image_file_rejected_without_exfil(tmp_path) -> None:
+ # 伪装 .png 的文本文件(如私钥/配置)不得被内联外发
+ p = tmp_path / "evil.png"
+ p.write_bytes(b"-----BEGIN RSA PRIVATE KEY-----\nnope\n")
+ with pytest.raises(ValueError, match="not a recognized image"):
+ it._resolve_edit_images({"image": str(p)})
+
+
+def test_resolve_non_image_data_url_rejected() -> None:
+ item = "data:image/png;base64," + base64.b64encode(b"plain text").decode()
+ with pytest.raises(ValueError, match="not a recognized image"):
+ it._resolve_edit_images({"image": item})
+
+
+def test_resolve_invalid_b64_data_url_rejected() -> None:
+ # b64 解码失败同样落入白名单拒绝,不外发
+ with pytest.raises(ValueError, match="not a recognized image"):
+ it._resolve_edit_images({"image": "data:image/png;base64,@@@not-b64@@@"})
+
+
+def test_resolve_oversized_file_raises(tmp_path) -> None:
+ p = tmp_path / "big.png"
+ p.write_bytes(b"\x89PNG\r\n\x1a\n" + b"0" * (20 * 1024 * 1024 + 1))
+ with pytest.raises(ValueError, match="too large"):
+ it._resolve_edit_images({"image": str(p)})
+
+
+# ---------------------------------------------------------------------------
+# _try_auto_resize_from_dimension_error
+# ---------------------------------------------------------------------------
+
+
+def test_auto_resize_upscales_small_image() -> None:
+ out = it._try_auto_resize_from_dimension_error(
+ Exception(_DIM_ERROR), [_data_url(358, 117)]
+ )
+ assert out is not None
+ im = _decode_data_url(out[0])
+ assert im.size[0] >= 512 and im.size[1] >= 512
+ assert im.size[0] / im.size[1] == pytest.approx(358 / 117, rel=0.02)
+
+
+def test_auto_resize_downscales_large_image() -> None:
+ out = it._try_auto_resize_from_dimension_error(
+ Exception(_DIM_ERROR), [_data_url(5000, 3000)]
+ )
+ assert out is not None
+ im = _decode_data_url(out[0])
+ assert max(im.size) <= 3072
+ assert im.size[0] / im.size[1] == pytest.approx(5 / 3, rel=0.02)
+
+
+def test_auto_resize_extreme_aspect_gives_up() -> None:
+ # 10000x100 等比缩放无法落入 [512,3072]:放弃自愈,不拉伸失真
+ assert it._try_auto_resize_from_dimension_error(
+ Exception(_DIM_ERROR), [_data_url(10000, 100)]
+ ) is None
+
+
+def test_auto_resize_in_range_image_no_retry() -> None:
+ assert it._try_auto_resize_from_dimension_error(
+ Exception(_DIM_ERROR), [_data_url(2048, 1024)]
+ ) is None
+
+
+ "exc,msg",
+ [
+ (None, None),
+ (Exception("Error code: 401 - invalid api key"), "unrelated error"),
+ (Exception("some other 400"), "unrelated error"),
+ ],
+)
+def test_auto_resize_unrelated_errors_give_up(exc, msg) -> None:
+ assert it._try_auto_resize_from_dimension_error(exc, [_data_url(100, 100)]) is None
+
+
+def test_auto_resize_http_url_gives_up() -> None:
+ assert it._try_auto_resize_from_dimension_error(
+ Exception(_DIM_ERROR), ["https://example.com/a.png"]
+ ) is None
+
+
+def test_auto_resize_without_pillow_gives_up(monkeypatch) -> None:
+ # Pillow 缺失:降级放弃自愈,让服务端错误原样透传。
+ # data URL 需在禁用 PIL 前构造——Pillow 的 PNG 编码器在 save 时才
+ # 惰性 import PIL 包,构造阶段就需要可用的 PIL。
+ item = _data_url(358, 117)
+ monkeypatch.setitem(sys.modules, "PIL", None)
+ assert it._try_auto_resize_from_dimension_error(Exception(_DIM_ERROR), [item]) is None
+
+
+# ---------------------------------------------------------------------------
+# _build_image_gen_kwargs:编辑语义
+# ---------------------------------------------------------------------------
+
+
+def test_kwargs_edit_images_extra_body_and_size_none() -> None:
+ kw = it._build_image_gen_kwargs(
+ "OpenAI", {}, size=None, n=1, api_base=MAAS_BASE,
+ edit_images=["data:image/png;base64,AA==", "https://x.com/b.jpg"],
+ )
+ # openjiuwen 的 size 形参默认 '1664*928'(* 分隔)会触发华为云 400;
+ # 显式 None 让请求体省略 size 字段,服务端按输入图尺寸生成。
+ assert kw.get("size", "MISSING") is None
+ assert kw["extra_body"]["image"] == "data:image/png;base64,AA==,https://x.com/b.jpg"
+ assert kw["n"] == 1 and kw["response_format"] == "b64_json"
+
+
+def test_kwargs_edit_explicit_size_normalized() -> None:
+ kw = it._build_image_gen_kwargs(
+ "OpenAI", {}, size="1024*1024", n=1, api_base=MAAS_BASE,
+ edit_images=["data:image/png;base64,AA=="],
+ )
+ assert kw["size"] == "1024x1024"
+
+
+def test_kwargs_edit_invalid_size_dropped() -> None:
+ kw = it._build_image_gen_kwargs(
+ "OpenAI", {}, size="auto", n=1, api_base=MAAS_BASE,
+ edit_images=["data:image/png;base64,AA=="],
+ )
+ assert kw.get("size", "MISSING") is None
+
+
+def test_kwargs_text2img_default_unchanged() -> None:
+ kw = it._build_image_gen_kwargs("OpenAI", {}, size="1024x1024", n=2, api_base=MAAS_BASE)
+ assert kw["size"] == "1024x1024" and kw["n"] == 1
+ assert "extra_body" not in kw
+ kw2 = it._build_image_gen_kwargs("OpenAI", {}, size="1024x1024", n=2, api_base="")
+ assert kw2["n"] == 2
+
+
+def test_kwargs_seed_forwarded() -> None:
+ kw = it._build_image_gen_kwargs(
+ "OpenAI", {"seed": 42}, size="1024x1024", n=1, api_base=MAAS_BASE,
+ )
+ assert kw["seed"] == 42
+
+
+# ---------------------------------------------------------------------------
+# 工具 schema:LLM 必须能看到 inputs 的每个键
+# (历史缺陷:docstring 嵌套子键 "Dict with keys:" 解析丢失,LLM 只见空描述)
+# ---------------------------------------------------------------------------
+
+
+def test_tool_schema_exposes_all_inputs_keys() -> None:
+ p = it.generate_image.card.input_params
+ inner = p["properties"]["inputs"]["properties"]
+ assert sorted(inner.keys()) == sorted(
+ ["prompt", "image", "size", "quality", "n",
+ "negative_prompt", "prompt_extend", "seed", "save_dir"]
+ )
+ assert p["required"] == ["inputs"]
+ assert p["properties"]["inputs"]["required"] == ["prompt"]
+
+
+def test_tool_schema_image_param_has_full_description() -> None:
+ inner = it.generate_image.card.input_params["properties"]["inputs"]["properties"]
+ image = inner["image"]
+ assert image["type"] == ["string", "array"]
+ desc = image["description"]
+ assert "watermark removal" in desc
+ assert "local file path" in desc