Skip to content

CubeLoop — a Pythonic, async-native agent harness core designed for high performance, readability, and production-grade persistence.

feat(images): add ImagesCapabilityDescriptor + SizeSpec

xf gongc1f1c6805cfc
diff --git a/cubepi/providers/images/capability.py b/cubepi/providers/images/capability.py
new file mode 100644
index 0000000..dbe40b4
--- /dev/null
+++ b/cubepi/providers/images/capability.py
@@ -0,0 +1,54 @@
+from __future__ import annotations
+
+from typing import Any, Literal
+
+from pydantic import BaseModel, Field
+
+
+class SizeSpec(BaseModel):
+ """How the canonical ``ctx.size`` value is serialized to the wire.
+
+ - ``size_string`` → ``{"size": ctx.size}`` (OpenAI, Doubao)
+ - ``image_size_string`` → ``{"image_size": ctx.size}`` (SiliconFlow)
+ - ``width_height`` → split ``"<W>x<H>"`` into ``{"width": W, "height": H}``
+ - ``aspect_ratio`` → ``{"aspect_ratio": ctx.size}`` (Together FLUX schnell, Imagen)
+ """
+
+ kind: Literal["size_string", "image_size_string", "width_height", "aspect_ratio"]
+
+
+class ImagesCapabilityDescriptor(BaseModel):
+ """Data-level description of an OpenAI-shape image backend's wire quirks.
+
+ The descriptor is consumed by ``BaseImagesProvider._build_payload`` to
+ rename canonical CubePi fields onto whatever wire keys the backend
+ expects, and to gate ``ImagesContext`` fields the backend does not
+ support. Backends whose shape is fundamentally different (async-task
+ models like Aliyun Wanxiang, Imagen on Vertex, Stability, Replicate)
+ need a separate provider subclass; this descriptor does not try to
+ cover them.
+ """
+
+ size_spec: SizeSpec = Field(default_factory=lambda: SizeSpec(kind="size_string"))
+ count_field: str = "n"
+
+ supports_seed: bool = False
+ seed_field: str = "seed"
+
+ supports_negative_prompt: bool = False
+ negative_prompt_field: str = "negative_prompt"
+
+ supports_steps: bool = False
+ steps_field: str = "num_inference_steps"
+
+ supports_guidance: bool = False
+ guidance_field: str = "guidance_scale"
+
+ output_format_field: str | None = "output_format"
+ response_format_field: str = "response_format"
+ response_format_value: Literal["b64_json", "url"] = "b64_json"
+
+ supports_edit: bool = True
+ input_images_field: str = "image"
+
+ extra_payload: dict[str, Any] = Field(default_factory=dict)
diff --git a/tests/providers/images/test_capability.py b/tests/providers/images/test_capability.py
new file mode 100644
index 0000000..cff61b8
--- /dev/null
+++ b/tests/providers/images/test_capability.py
@@ -0,0 +1,66 @@
+from cubepi.providers.images.capability import (
+ ImagesCapabilityDescriptor,
+ SizeSpec,
+)
+
+
+def test_default_descriptor_is_openai_shape():
+ d = ImagesCapabilityDescriptor()
+ assert d.size_spec.kind == "size_string"
+ assert d.count_field == "n"
+ assert d.supports_seed is False
+ assert d.supports_negative_prompt is False
+ assert d.supports_steps is False
+ assert d.supports_guidance is False
+ assert d.output_format_field == "output_format"
+ assert d.response_format_field == "response_format"
+ assert d.response_format_value == "b64_json"
+ assert d.supports_edit is True
+ assert d.input_images_field == "image"
+ assert d.extra_payload == {}
+
+
+def test_size_spec_kinds_enum():
+ for kind in ("size_string", "image_size_string", "width_height", "aspect_ratio"):
+ s = SizeSpec(kind=kind)
+ assert s.kind == kind
+
+
+def test_descriptor_for_doubao_shape():
+ d = ImagesCapabilityDescriptor(
+ supports_seed=True,
+ extra_payload={"watermark": False},
+ )
+ assert d.supports_seed is True
+ assert d.extra_payload == {"watermark": False}
+ assert d.size_spec.kind == "size_string"
+
+
+def test_descriptor_for_siliconflow_shape():
+ d = ImagesCapabilityDescriptor(
+ size_spec=SizeSpec(kind="image_size_string"),
+ count_field="batch_size",
+ supports_seed=True,
+ supports_steps=True,
+ steps_field="num_inference_steps",
+ supports_guidance=True,
+ guidance_field="guidance_scale",
+ supports_negative_prompt=True,
+ output_format_field=None,
+ )
+ assert d.size_spec.kind == "image_size_string"
+ assert d.count_field == "batch_size"
+ assert d.steps_field == "num_inference_steps"
+ assert d.guidance_field == "guidance_scale"
+ assert d.output_format_field is None
+
+
+def test_descriptor_for_together_flux_schnell_shape():
+ d = ImagesCapabilityDescriptor(
+ size_spec=SizeSpec(kind="aspect_ratio"),
+ supports_seed=True,
+ supports_steps=True,
+ steps_field="steps",
+ )
+ assert d.size_spec.kind == "aspect_ratio"
+ assert d.steps_field == "steps"