Skip to content

The agent that grows with you

refactor(image_gen/deepinfra): route b64/url materialisation through shared materialize_image (on_url_fail hook)

Tekniumf2e8ecaf527d
diff --git a/plugins/image_gen/_common.py b/plugins/image_gen/_common.py
index 8c927426a0b..385baf3f009 100644
--- a/plugins/image_gen/_common.py
+++ b/plugins/image_gen/_common.py
@@ -203,7 +203,7 @@ def import_openai(provider: str, aspect: str) -> Tuple[Any, Optional[Dict[str, A
def materialize_image(
b64: Optional[str], url: Optional[str], *, prefix: str, label: str, provider: str, model: str,
- prompt: str, aspect: str, log: logging.Logger = logger,
+ prompt: str, aspect: str, log: logging.Logger = logger, on_url_fail: Optional[Callable[[Exception], None]] = None,
) -> Tuple[Optional[str], Optional[Dict[str, Any]]]:
"""``(image_ref, None)`` or ``(None, error_dict)`` for a ``(b64_json, url)`` pair.
@@ -218,16 +218,22 @@ def materialize_image(
except Exception as exc: # noqa: BLE001
return None, fail(f"Could not save image to cache: {exc}", "io_error")
if url:
- return cache_url_best_effort(url, prefix=prefix, label=label, log=log), None
+ return cache_url_best_effort(url, prefix=prefix, label=label, log=log, on_fail=on_url_fail), None
return None, fail(f"{label} response contained neither b64_json nor URL", "empty_response")
-def cache_url_best_effort(url: str, *, prefix: str, label: str, log: logging.Logger = logger) -> str:
- """Cache ``url`` locally; on failure warn and return the bare URL."""
+def cache_url_best_effort(
+ url: str, *, prefix: str, label: str, log: logging.Logger = logger,
+ on_fail: Optional[Callable[[Exception], None]] = None,
+) -> str:
+ """Cache ``url`` locally; on failure warn (or call ``on_fail``) and return the bare URL."""
try:
return str(save_url_image(url, prefix=prefix))
except Exception as exc: # noqa: BLE001
- log.warning("%s image URL %s could not be cached (%s); falling back to bare URL.", label, url, exc)
+ if on_fail is not None:
+ on_fail(exc)
+ else:
+ log.warning("%s image URL %s could not be cached (%s); falling back to bare URL.", label, url, exc)
return url
diff --git a/plugins/image_gen/deepinfra/__init__.py b/plugins/image_gen/deepinfra/__init__.py
index fa0ef284d87..8a1016a9042 100644
--- a/plugins/image_gen/deepinfra/__init__.py
+++ b/plugins/image_gen/deepinfra/__init__.py
@@ -18,10 +18,9 @@ import os
from typing import Any, Dict, List, Optional
from agent.secret_scope import get_secret
-from agent.image_gen_provider import (
- DEFAULT_ASPECT_RATIO, resolve_aspect_ratio, save_b64_image, save_url_image, success_response)
+from agent.image_gen_provider import DEFAULT_ASPECT_RATIO, resolve_aspect_ratio, success_response
from plugins.image_gen._common import (
- StaticImageGenProvider, error_factory, import_openai, load_image_gen_config,
+ StaticImageGenProvider, error_factory, import_openai, load_image_gen_config, materialize_image,
prompt_required_error, size_for)
logger = logging.getLogger(__name__)
@@ -40,13 +39,10 @@ def _live_models() -> Optional[List[Dict[str, Any]]]:
def _format_catalog_row(item: Dict[str, Any]) -> Dict[str, Any]:
"""Picker row for a catalog item."""
mid = item.get("id", "")
- metadata = item.get("metadata") or {}
- if not isinstance(metadata, dict):
- metadata = {}
+ metadata = item.get("metadata")
+ metadata = metadata if isinstance(metadata, dict) else {}
row: Dict[str, Any] = {
- "id": mid,
- "display": mid.split("/", 1)[-1] if "/" in mid else mid,
- "strengths": metadata.get("description", ""),
+ "id": mid, "display": mid.split("/", 1)[-1], "strengths": metadata.get("description", ""),
}
pricing = metadata.get("pricing")
if isinstance(pricing, dict) and pricing.get("per_image_unit") is not None:
@@ -147,25 +143,17 @@ class DeepInfraImageGenProvider(StaticImageGenProvider):
if not data:
return fail("DeepInfra returned no image data", "empty_response")
first = data[0]
- b64 = getattr(first, "b64_json", None)
- url = getattr(first, "url", None)
# Drop the ``vendor/`` prefix and any colons so the saved filename
- # stays a single path component on every OS.
- prefix = f"deepinfra_{model_id.split('/', 1)[-1].replace(':', '_')}"
- if b64:
- try:
- image_ref = str(save_b64_image(b64, prefix=prefix))
- except Exception as exc:
- return fail(f"Could not save image to cache: {exc}", "io_error")
- elif url:
- # Delivery URLs are often short-lived; materialise locally, best-effort.
- try:
- image_ref = str(save_url_image(url, prefix=prefix))
- except Exception as exc:
- logger.debug("DeepInfra: caching delivery URL failed (%s); returning URL", exc)
- image_ref = url
- else:
- return fail("DeepInfra response contained neither b64_json nor URL", "empty_response")
+ # stays a single path component on every OS. Delivery URLs are often
+ # short-lived; materialise locally, best-effort.
+ image_ref, err = materialize_image(
+ getattr(first, "b64_json", None), getattr(first, "url", None),
+ prefix=f"deepinfra_{model_id.split('/', 1)[-1].replace(':', '_')}", label="DeepInfra",
+ provider="deepinfra", model=model_id, prompt=prompt, aspect=aspect,
+ on_url_fail=lambda exc: logger.debug("DeepInfra: caching delivery URL failed (%s); returning URL", exc),
+ )
+ if err:
+ return err
return success_response(
image=image_ref, model=model_id, prompt=prompt, aspect_ratio=aspect, provider="deepinfra",
extra={"size": size})