The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
feat(krea): Krea 3 edits and generates through the managed gateway, with thinking and resolution controls
Mariano Nicolini693777d4faf6
diff --git a/plugins/image_gen/krea/__init__.py b/plugins/image_gen/krea/__init__.pyindex 653afa34064..53ab9fb1b78 100644--- a/plugins/image_gen/krea/__init__.py+++ b/plugins/image_gen/krea/__init__.py@@ -1,10 +1,11 @@-"""Krea image generation backend (``Krea 2`` Medium / Large / Medium Turbo).+"""Krea image generation backend: ``Krea 3`` (generates and edits) and ``Krea 2`` Medium / Large / Medium Turbo.Krea's API is asynchronous: submit returns a ``job_id`` polled at ``GET/jobs/{job_id}``; ``generate()`` hides that (submit, poll every 2s with lightbackoff, cache the result URL locally). Selection: ``model`` kwarg →``KREA_IMAGE_MODEL`` → ``image_gen.krea.model`` → ``image_gen.model`` (when one-of our IDs) → :data:`DEFAULT_MODEL`. Docs: https://docs.krea.ai/developers/krea-2/overview+of our IDs) → :data:`DEFAULT_MODEL`. Docs: https://www.krea.ai/docs/developers/krea-3/overview+and https://docs.krea.ai/developers/krea-2/overview"""from __future__ import annotations@@ -29,25 +30,32 @@ logger = logging.getLogger(__name__)BASE_URL = "https://api.krea.ai"-# ``path`` is Krea's URL segment. ``upscale`` (Enhance pass) is opt-in for every tier:-# default-on enhance degraded output quality, and Large is 2K native anyway.+# ``path`` is Krea's submit path. ``upscale`` (Enhance pass) is opt-in for every tier:+# default-on enhance degraded output quality, and Large is 2K native anyway. ``edit``+# marks Krea 3, which edits the pictures it is given instead of copying their look._MODELS: dict[str, dict[str, Any]] = {+ "krea-3": {+ "display": "Krea 3", "speed": "~5s (1K) / ~40s (2K, high thinking)",+ "strengths": "Generates and edits existing images from instructions; keeps products, characters, logos and text consistent.",+ "price": "$0.03 (1K) / $0.06 (2K), +$0.07 per reference image, +$0.02 high thinking",+ "path": "/generate/image/krea/krea-3", "edit": True, "upscale": False,+ },"krea-2-medium": {"display": "Krea 2 Medium", "speed": "~15-25s","strengths": "Illustration, anime, painting, expressive styles. Faster + cheaper.","price": "$0.030 (text) / $0.035 (style refs) / $0.040 (moodboards)",- "path": "medium", "upscale": False,+ "path": "/generate/image/krea/krea-2/medium", "upscale": False,},"krea-2-large": {"display": "Krea 2 Large", "speed": "~25-60s","strengths": "Photorealism, raw textured looks (motion blur, grain), expressive styles.","price": "$0.060 (text) / $0.065 (style refs) / $0.070 (moodboards)",- "path": "large", "upscale": False,+ "path": "/generate/image/krea/krea-2/large", "upscale": False,},"krea-2-medium-turbo": {"display": "Krea 2 Medium Turbo", "speed": "~8-15s","strengths": "Fastest Krea 2 — medium quality at lower latency / cost.",- "price": "$0.015 (text) / $0.0175 (style refs)", "path": "medium-turbo", "upscale": False,+ "price": "$0.015 (text) / $0.0175 (style refs)", "path": "/generate/image/krea/krea-2/medium-turbo", "upscale": False,},}KREA_MODEL_IDS = frozenset(_MODELS)@@ -56,7 +64,7 @@ DEFAULT_MODEL = "krea-2-medium"# Hermes' 3 abstract ratios → Krea's enum (1:1, 4:3, 3:2, 16:9, 2.35:1, 4:5, 2:3, 9:16)._ASPECT_MAP = {"landscape": "16:9", "square": "1:1", "portrait": "9:16"}-DEFAULT_RESOLUTION = "1K" # only resolution Krea currently supports+DEFAULT_RESOLUTION = "1K" # Krea 2 is priced at 1K only; Krea 3 also takes 2K# Style refs are objects ({"url", "strength"}); bare URLs get Krea's recommended start (range -2..2)._DEFAULT_STYLE_REFERENCE_STRENGTH = 0.6_MAX_STYLE_REFERENCES = 10@@ -67,6 +75,10 @@ _MAX_LOCAL_REFERENCE_BYTES = 3 * 1024 * 1024_VALID_CREATIVITY = {"raw", "low", "medium", "high"}# Krea 2 generative sliders: integers from -100 to 100, 0 means the slider is off._K2_SLIDERS = ("intensity", "complexity", "movement")+# Krea 3 controls; the managed gateway caps its reference images at 4.+_KREA3_CONTROLS = ("thinking", "resolution")+_KREA3_RESOLUTIONS = ("1K", "2K")+_MAX_KREA3_REFERENCE_IMAGES = 4# Polling: Krea recommends 2-5s; 2s backing off to 5s (Large ~1min); ceiling = Krea's 3 min tool timeout._POLL_INITIAL_INTERVAL = 2.0@@ -327,16 +339,17 @@ def _collect_style_refs(return deduped[:_MAX_STYLE_REFERENCES]-def _inline_local_style_refs(- style_refs: list[Any], fail: ErrorFn+def _inline_local_images(+ refs: list[Any], fail: ErrorFn) -> tuple[list[Any], Optional[dict[str, Any]]]:- """Embed local image files as data URIs; URLs and data URIs pass through unchanged."""+ """Embed local image files as data URIs; URLs and data URIs pass through unchanged. A ref is+ a Krea 2 ``{url, strength}`` object or a Krea 3 source string; legacy objects pass through."""from agent.file_safety import raise_if_read_blockedinlined: list[Any] = []total_bytes = 0- for ref in style_refs:- source = ref.get("url") if isinstance(ref, dict) else None # legacy non-dict refs pass through+ for ref in refs:+ source = ref.get("url") if isinstance(ref, dict) else refif not isinstance(source, str) or source.lower().startswith(_REMOTE_REFERENCE_PREFIXES):inlined.append(ref)continue@@ -347,20 +360,21 @@ def _inline_local_style_refs(except ValueError as exc:return [], fail(str(exc), "invalid_image_url")if not path.is_file():- return [], fail(f"Style reference image not found: {source}", "invalid_image_url")+ return [], fail(f"Reference image not found: {source}", "invalid_image_url")# Read at most one byte past the remaining budget: the cap bounds what is sent, not a stat.try:with path.open("rb") as fh:data = fh.read(_MAX_LOCAL_REFERENCE_BYTES - total_bytes + 1)except OSError as exc: # removed or swapped after the is_file() check- return [], fail(f"Style reference image unreadable: {source} ({exc})", "invalid_image_url")+ return [], fail(f"Reference image unreadable: {source} ({exc})", "invalid_image_url")total_bytes += len(data)if total_bytes > _MAX_LOCAL_REFERENCE_BYTES:return [], fail(- f"Local style reference images total over {_MAX_LOCAL_REFERENCE_BYTES / 2**20:g} MB; "+ f"Local reference images total over {_MAX_LOCAL_REFERENCE_BYTES / 2**20:g} MB; ""resize them or pass public URLs", "source_too_large")mime = mimetypes.guess_type(path.name)[0] or "image/png"- inlined.append({**ref, "url": f"data:{mime};base64,{base64.b64encode(data).decode('ascii')}"})+ data_uri = f"data:{mime};base64,{base64.b64encode(data).decode('ascii')}"+ inlined.append({**ref, "url": data_uri} if isinstance(ref, dict) else data_uri)return inlined, None@@ -394,9 +408,9 @@ def _submit_job(base_url: str, auth_token: str, model_path: str, payload: dict[str, Any], managed: bool, model_id: str,fail: ErrorFn,) -> tuple[Optional[str], Optional[dict[str, Any]]]:- """POST the generation request; ``(job_id, None)`` or ``(None, error)``."""+ """POST the generation request to the model's submit path; ``(job_id, None)`` or ``(None, error)``."""submit_body, failure = post_json(- f"{base_url}/generate/image/krea/krea-2/{model_path}",+ f"{base_url}{model_path}",headers=_headers(auth_token, managed=managed, json_body=True),payload=payload, timeout=30, label="Krea", error_message=_submit_error_message)if failure is not None:@@ -455,8 +469,53 @@ def _upscale_requested(explicit: Any, meta: dict[str, Any]) -> bool:return cfg_upscale if isinstance(cfg_upscale, bool) else bool(meta.get("upscale", False))+def _krea2_payload(+ prompt: str, krea_ar: str, image_url: Optional[str], reference_image_urls: Optional[list[str]],+ kwargs: dict[str, Any], managed: bool, fail: ErrorFn,+) -> tuple[dict[str, Any], Optional[dict[str, Any]]]:+ """Krea 2 request body: the pictures given are style references whose look is copied."""+ style_refs = _collect_style_refs(image_url, reference_image_urls, kwargs.get("image_style_references"))+ payload = _build_payload(prompt, krea_ar, _resolve_creativity(kwargs.get("creativity")), style_refs, kwargs)+ # LoRAs/moodboards are rejected by the managed gateway: fail fast with guidance, not a raw 400.+ if managed:+ for what, arg in _MANAGED_UNSUPPORTED:+ if arg in payload:+ return payload, fail(+ f"Managed Krea (Nous Subscription) does not support {what}. "+ f"Set KREA_API_KEY to use Krea directly, or omit `{arg}`.",+ "unsupported_argument")+ # After the fail-fast above, so a request about to be refused never reads local files.+ if payload.get("image_style_references"):+ payload["image_style_references"], err = _inline_local_images(payload["image_style_references"], fail)+ if err is not None:+ return payload, err+ return payload, None+++def _krea3_payload(+ prompt: str, krea_ar: str, image_url: Optional[str], reference_image_urls: Optional[list[str]],+ kwargs: dict[str, Any], fail: ErrorFn,+) -> tuple[dict[str, Any], Optional[dict[str, Any]]]:+ """Krea 3 request body: the pictures given are edited; resolution and thinking mode are priced."""+ image_urls, err = _inline_local_images(+ collect_source_images(image_url, reference_image_urls, _MAX_KREA3_REFERENCE_IMAGES), fail)+ if err is not None:+ return {}, err+ resolution = kwargs.get("resolution")+ payload: dict[str, Any] = {+ "prompt": prompt, "aspect_ratio": krea_ar,+ "resolution": resolution if resolution in _KREA3_RESOLUTIONS else DEFAULT_RESOLUTION,+ "thinking_mode": "high" if kwargs.get("thinking") == "high" else "fast",+ }+ if isinstance(kwargs.get("seed"), int):+ payload["seed"] = kwargs["seed"]+ if image_urls:+ payload["image_urls"] = image_urls+ return payload, None++class KreaImageGenProvider(StaticImageGenProvider):- """Krea ``Krea 2`` foundation image model backend (Medium + Large)."""+ """Krea image backend: Krea 3 (generate and edit) and Krea 2 (Medium, Large, Medium Turbo)."""provider_id = "krea"label = "Krea"@@ -464,7 +523,7 @@ class KreaImageGenProvider(StaticImageGenProvider):default_model_id = DEFAULT_MODELsetup = dict(name="Krea", badge="paid",- tag="Krea 2 foundation model — Medium ($0.03), Large ($0.06), Medium Turbo ($0.015). Style transfer, moodboards, reference-guided generation. Direct key or managed Nous Subscription gateway.",+ tag="Krea 3 — generates and edits images ($0.03 at 1K, $0.06 at 2K) — and Krea 2 Medium ($0.03), Large ($0.06), Medium Turbo ($0.015) with style transfer and moodboards. Direct key or managed Nous Subscription gateway.",key="KREA_API_KEY", prompt="Krea API key", url="https://www.krea.ai/settings/api-tokens")def is_available(self) -> bool:@@ -472,6 +531,12 @@ class KreaImageGenProvider(StaticImageGenProvider):return bool(get_secret("KREA_API_KEY")) or _managed_krea_gateway_ready()def capabilities(self) -> dict[str, Any]:+ _, meta = _resolve_model()+ if meta.get("edit"):+ return {+ "modalities": ["text", "image"], "max_reference_images": _MAX_KREA3_REFERENCE_IMAGES,+ "supports_upscale": True, "creative_controls": list(_KREA3_CONTROLS),+ }return {"modalities": ["text", "image"], "max_reference_images": _MAX_STYLE_REFERENCES,"supports_upscale": True, "creative_controls": ["creativity", *_K2_SLIDERS],@@ -486,8 +551,6 @@ class KreaImageGenProvider(StaticImageGenProvider):prompt = (prompt or "").strip()aspect = resolve_aspect_ratio(aspect_ratio)krea_ar = _ASPECT_MAP.get(aspect, "1:1")- style_refs = _collect_style_refs(- image_url, reference_image_urls, kwargs.get("image_style_references"))if not prompt:return prompt_required_error("krea", aspect)@@ -510,23 +573,14 @@ class KreaImageGenProvider(StaticImageGenProvider):"auth_required")model_id, meta = _resolve_model(kwargs.get("model"))- creativity = _resolve_creativity(kwargs.get("creativity"))fail = error_factory("krea", aspect, model=model_id, prompt=prompt)- payload = _build_payload(prompt, krea_ar, creativity, style_refs, kwargs)-- # LoRAs/moodboards are rejected by the managed gateway: fail fast with guidance, not a raw 400.- if managed is not None:- for what, arg in _MANAGED_UNSUPPORTED:- if arg in payload:- return fail(- f"Managed Krea (Nous Subscription) does not support {what}. "- f"Set KREA_API_KEY to use Krea directly, or omit `{arg}`.",- "unsupported_argument")- # After the fail-fast above, so a request about to be refused never reads local files.- if payload.get("image_style_references"):- payload["image_style_references"], err = _inline_local_style_refs(payload["image_style_references"], fail)- if err is not None:- return err+ if meta.get("edit"):+ payload, err = _krea3_payload(prompt, krea_ar, image_url, reference_image_urls, kwargs, fail)+ else:+ payload, err = _krea2_payload(+ prompt, krea_ar, image_url, reference_image_urls, kwargs, managed is not None, fail)+ if err is not None:+ return err# 1. Submit job.job_id, err = _submit_job(@@ -574,16 +628,19 @@ class KreaImageGenProvider(StaticImageGenProvider):)image_ref = result_image_urlextra: dict[str, Any] = {- "krea_aspect_ratio": krea_ar, "resolution": DEFAULT_RESOLUTION, "creativity": creativity,- "job_id": job_id, "upscaled": upscaled,+ "krea_aspect_ratio": krea_ar, "resolution": payload["resolution"], "job_id": job_id, "upscaled": upscaled,}+ for key in ("creativity", "thinking_mode"):+ if key in payload:+ extra[key] = payload[key]if upscaled:extra["upscale_factor"] = _ENHANCE_SCALE_FACTORif isinstance(job.get("completed_at"), str):extra["completed_at"] = job["completed_at"]+ has_images = bool(payload.get("image_style_references") or payload.get("image_urls"))return success_response(image=image_ref, model=model_id, prompt=prompt, aspect_ratio=aspect, provider="krea",- modality="image" if style_refs else "text", extra=extra)+ modality="image" if has_images else "text", extra=extra)def register(ctx) -> None:diff --git a/tests/plugins/image_gen/test_krea_provider.py b/tests/plugins/image_gen/test_krea_provider.pyindex 97b0f18f1ce..ca33a7e4a2f 100644--- a/tests/plugins/image_gen/test_krea_provider.py+++ b/tests/plugins/image_gen/test_krea_provider.py@@ -85,7 +85,7 @@ class TestModelResolution:model_id, meta = _resolve_model()assert model_id == "krea-2-large"- assert meta["path"] == "large"+ assert meta["path"] == "/generate/image/krea/krea-2/large"@@ -618,7 +618,7 @@ class TestExplicitModelOverride:model_id, meta = _resolve_model("krea-2-large")assert model_id == "krea-2-large"- assert meta["path"] == "large"+ assert meta["path"] == "/generate/image/krea/krea-2/large"def test_turbo_routes_to_medium_turbo_endpoint(self):from plugins.image_gen.krea import KreaImageGenProvider@@ -750,3 +750,54 @@ class TestRegistration:provider = mock_ctx.register_image_gen_provider.call_args[0][0]assert isinstance(provider, KreaImageGenProvider)assert provider.name == "krea"+++class TestKrea3:+ def _generate(self, monkeypatch, **kwargs):+ monkeypatch.setenv("KREA_IMAGE_MODEL", "krea-3")+ from plugins.image_gen.krea import KreaImageGenProvider++ submit, poll = _submit_response(), _poll_response(_completed_job())+ with patch("plugins.image_gen.krea.requests.post", return_value=submit) as mock_post, \+ patch("plugins.image_gen.krea.requests.get", return_value=poll), \+ patch("plugins.image_gen.krea.save_url_image", return_value=Path("/tmp/x.png")), \+ patch("plugins.image_gen.krea.time.sleep"):+ result = KreaImageGenProvider().generate(prompt="make the mug red", upscale=False, **kwargs)+ return result, mock_post.call_args++ def test_submits_to_the_krea_3_path_with_fast_1k_defaults(self, monkeypatch):+ result, call = self._generate(monkeypatch)+ assert call[0][0].endswith("/generate/image/krea/krea-3")+ assert call.kwargs["json"] == {+ "prompt": "make the mug red", "aspect_ratio": "16:9", "resolution": "1K", "thinking_mode": "fast"}+ assert (result["resolution"], result["thinking_mode"]) == ("1K", "fast")+ assert "creativity" not in result++ def test_controls_and_references_reach_the_payload_and_krea_2_fields_do_not(self, monkeypatch, tmp_path):+ image = tmp_path / "mug.png"+ image.write_bytes(b"\x89PNG\r\n\x1a\nmug")+ _, call = self._generate(+ monkeypatch, image_url=str(image), reference_image_urls=["https://example.com/red.png"],+ thinking="high", resolution="2K", creativity="raw", intensity=50)+ payload = call.kwargs["json"]+ assert payload["image_urls"] == [+ "data:image/png;base64," + base64.b64encode(image.read_bytes()).decode("ascii"),+ "https://example.com/red.png"]+ assert (payload["resolution"], payload["thinking_mode"]) == ("2K", "high")+ assert not {"creativity", "intensity", "image_style_references"} & set(payload)++ def test_caps_reference_images_at_the_gateway_limit(self, monkeypatch):+ _, call = self._generate(monkeypatch, reference_image_urls=[f"https://example.com/{i}.png" for i in range(6)])+ assert len(call.kwargs["json"]["image_urls"]) == 4++ def test_capabilities_follow_the_active_model(self, monkeypatch):+ from plugins.image_gen.krea import KreaImageGenProvider++ monkeypatch.setenv("KREA_IMAGE_MODEL", "krea-3")+ krea3 = KreaImageGenProvider().capabilities()+ assert (krea3["max_reference_images"], krea3["creative_controls"]) == (4, ["thinking", "resolution"])+ assert "source_image_role" not in krea3+ monkeypatch.setenv("KREA_IMAGE_MODEL", "krea-2-medium")+ krea2 = KreaImageGenProvider().capabilities()+ assert krea2["source_image_role"] == "style"+ assert "creativity" in krea2["creative_controls"]diff --git a/tests/tools/test_image_generate_schema.py b/tests/tools/test_image_generate_schema.pyindex 8189703cbb3..e5ac0fb1d1e 100644--- a/tests/tools/test_image_generate_schema.py+++ b/tests/tools/test_image_generate_schema.py@@ -144,6 +144,21 @@ class TestDynamicParamGating(unittest.TestCase):self.assertIn("edit", fal["parameters"]["properties"]["reference_image_urls"]["description"])self.assertIn("edit", fal["parameters"]["properties"]["prompt"]["description"])+ def test_krea_3_advertises_edit_wording_and_its_own_controls(self):+ """Krea 3 edits the pictures it is given, so the edit wording applies with the plugin's+ reference cap, and the schema offers Krea 3's controls and none of Krea 2's."""+ from plugins.image_gen.krea import KreaImageGenProvider++ with patch.dict(os.environ, {"KREA_IMAGE_MODEL": "krea-3"}), \+ patch.object(ig, "_read_configured_image_provider", return_value="nous"), \+ patch.object(ig, "_read_configured_image_model", return_value="krea-3"):+ props = _build_dynamic_image_schema()["parameters"]["properties"]+ cap = KreaImageGenProvider().capabilities()["max_reference_images"]+ self.assertIs(props["image_url"], ig._IMAGE_URL_PARAM)+ self.assertEqual(props["reference_image_urls"]["maxItems"], cap)+ self.assertLessEqual({"thinking", "resolution"}, set(props))+ self.assertNotIn("creativity", props)+def test_static_schema_carries_no_capability_args(self):"""The registration-time placeholder must stay minimal — dynamicoverrides own the capability args (do-not-re-add guard)."""diff --git a/tools/image_generation_tool.py b/tools/image_generation_tool.pyindex 96280f5c64a..2ecf69b704a 100644--- a/tools/image_generation_tool.py+++ b/tools/image_generation_tool.py@@ -890,13 +890,21 @@ _IMAGE_URL_PARAM = {),}-# Creative-control vocabulary (Krea 2 today); a provider advertises the names it honors via-# ``capabilities()["creative_controls"]`` and only those reach the schema and its ``generate()``.+# Creative-control vocabulary (Krea 2 and Krea 3 today); a provider advertises the names it honors+# via ``capabilities()["creative_controls"]`` and only those reach the schema and its ``generate()``._CREATIVE_CONTROL_PARAMS = {"creativity": {"type": "string", "enum": ["raw", "low", "medium", "high"],"description": "Prompt expansion: raw (none), low, medium or high.",},+ "thinking": {+ "type": "string", "enum": ["fast", "high"],+ "description": "How much the model plans before generating: fast (default) or high for complex multi-step edits.",+ },+ "resolution": {+ "type": "string", "enum": ["1K", "2K"],+ "description": "Output size: 1K (default) or 2K when small text or fine detail matters.",+ },"intensity": {"type": "integer", "minimum": -100, "maximum": 100,"description": "Style intensity, -100 muted to 100 highly stylized. 0 neutral.",