refactor(image_gen,browser): krea submit via shared post_json, poll-error table, catalog fetch helper; compact providers
This commit is contained in:
+10
-20
@@ -1,11 +1,10 @@
|
||||
"""Shared base for the bundled cloud-browser provider plugins.
|
||||
|
||||
Every vendor (Browserbase, Browser Use, Firecrawl) speaks the same REST shape:
|
||||
POST to create a session, one request to release it, credentials from
|
||||
:func:`agent.secret_scope.get_secret`. :class:`CloudBrowserProvider` owns that
|
||||
lifecycle; subclasses supply class attributes and small hooks. Log messages are
|
||||
rendered with the vendor label so the emitted text matches the pre-refactor
|
||||
per-vendor modules, and they are emitted on the subclass module's logger.
|
||||
POST to create a session, one request to release it. :class:`CloudBrowserProvider`
|
||||
owns that lifecycle; subclasses supply class attributes and small hooks. Log
|
||||
messages carry the vendor label and go to the subclass module's logger so the
|
||||
emitted text matches the pre-refactor per-vendor modules.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -24,11 +23,10 @@ _CLOSE_OK = {200, 201, 204}
|
||||
class CloudBrowserProvider(BrowserProvider):
|
||||
"""REST cloud-browser provider driven by class attributes + hooks.
|
||||
|
||||
Subclasses set ``provider_id`` / ``label``, ``release_method`` /
|
||||
``release_path`` (``{session_id}`` placeholder, appended to
|
||||
``config["base_url"]``), implement ``_get_config_or_none()`` and
|
||||
``_headers(config)``, and write ``create_session`` on top of
|
||||
:meth:`_post_create` / :meth:`_check_created` / :meth:`_session_name`.
|
||||
Subclasses set ``provider_id`` / ``label``, ``release_method`` / ``release_path``
|
||||
(``{session_id}`` placeholder, appended to ``config["base_url"]``), implement
|
||||
``_get_config_or_none()`` and ``_headers(config)``, and write ``create_session``
|
||||
on top of :meth:`_post_create` / :meth:`_check_created` / :meth:`_session_name`.
|
||||
"""
|
||||
|
||||
provider_id: str
|
||||
@@ -59,8 +57,6 @@ class CloudBrowserProvider(BrowserProvider):
|
||||
def is_available(self) -> bool:
|
||||
return self._get_config_or_none() is not None
|
||||
|
||||
# -- config / request hooks ------------------------------------------
|
||||
|
||||
def _get_config_or_none(self) -> Optional[Dict[str, Any]]:
|
||||
raise NotImplementedError
|
||||
|
||||
@@ -87,8 +83,6 @@ class CloudBrowserProvider(BrowserProvider):
|
||||
url = f"{config['base_url']}{self.release_path.format(session_id=session_id)}"
|
||||
return getattr(requests, self.release_method)(url, **kwargs)
|
||||
|
||||
# -- create helpers ---------------------------------------------------
|
||||
|
||||
@staticmethod
|
||||
def _session_name(task_id: str) -> str:
|
||||
return f"hermes_{task_id}_{uuid.uuid4().hex[:8]}"
|
||||
@@ -96,7 +90,7 @@ class CloudBrowserProvider(BrowserProvider):
|
||||
def _post_create(
|
||||
self, url: str, headers: Dict[str, str], payload: Dict[str, object], *, wrap_errors: bool = True
|
||||
) -> requests.Response:
|
||||
"""POST the create request; wrap network failures into RuntimeError unless
|
||||
"""POST the create request; network failures become RuntimeError unless
|
||||
the caller (managed gateway) needs the raw exception to retry."""
|
||||
try:
|
||||
return requests.post(url, headers=headers, json=payload, timeout=30)
|
||||
@@ -112,8 +106,6 @@ class CloudBrowserProvider(BrowserProvider):
|
||||
f"{response.status_code} {response.text}"
|
||||
)
|
||||
|
||||
# -- lifecycle --------------------------------------------------------
|
||||
|
||||
def close_session(self, session_id: str) -> bool:
|
||||
try:
|
||||
config = self._get_config()
|
||||
@@ -127,9 +119,7 @@ class CloudBrowserProvider(BrowserProvider):
|
||||
return True
|
||||
self._log.warning(
|
||||
self.close_fail_fmt or f"Failed to close {self.label} session %s: HTTP %s - %s",
|
||||
session_id,
|
||||
response.status_code,
|
||||
response.text[:200],
|
||||
session_id, response.status_code, response.text[:200],
|
||||
)
|
||||
return False
|
||||
except Exception as e:
|
||||
|
||||
@@ -1,13 +1,9 @@
|
||||
"""Browser Use cloud browser provider (plugin form).
|
||||
"""Browser Use cloud browser provider.
|
||||
|
||||
Browser Use is the only browser backend with dual auth: a direct
|
||||
``BROWSER_USE_API_KEY`` for self-billed users, or the managed Nous tool gateway
|
||||
(bills sessions to a Nous subscription). Dispatch order: direct key first,
|
||||
managed gateway second, unless ``tool_gateway.browser: gateway`` flips it.
|
||||
|
||||
Config: ``browser.cloud_provider: "browser-use"``; ``tool_gateway.browser``.
|
||||
Auth: ``BROWSER_USE_API_KEY`` (https://browser-use.com) or a managed Nous
|
||||
gateway entry configured via ``hermes setup``.
|
||||
The only browser backend with dual auth: a direct ``BROWSER_USE_API_KEY``
|
||||
(https://browser-use.com) or the managed Nous tool gateway (bills to a Nous
|
||||
subscription). Direct key first, managed second, unless ``tool_gateway.browser:
|
||||
gateway`` flips it. Config: ``browser.cloud_provider: "browser-use"``.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -25,7 +21,7 @@ from plugins.browser._common import CloudBrowserProvider
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Idempotency keys for managed-mode session creation: the gateway returns 409
|
||||
# "already in progress" on retried POSTs, so we forward the original key.
|
||||
# "already in progress" on retried POSTs, so the original key is forwarded.
|
||||
# Cleared on success or terminal failure.
|
||||
_pending_create_keys: Dict[str, str] = {}
|
||||
_pending_create_keys_lock = threading.Lock()
|
||||
@@ -62,12 +58,8 @@ def _should_preserve_pending_create_key(response: requests.Response) -> bool:
|
||||
payload = response.json()
|
||||
except Exception:
|
||||
return False
|
||||
if not isinstance(payload, dict):
|
||||
return False
|
||||
error = payload.get("error")
|
||||
if not isinstance(error, dict):
|
||||
return False
|
||||
return "already in progress" in str(error.get("message") or "").lower()
|
||||
error = payload.get("error") if isinstance(payload, dict) else None
|
||||
return isinstance(error, dict) and "already in progress" in str(error.get("message") or "").lower()
|
||||
|
||||
|
||||
class BrowserUseBrowserProvider(CloudBrowserProvider):
|
||||
@@ -77,16 +69,16 @@ class BrowserUseBrowserProvider(CloudBrowserProvider):
|
||||
label = "Browser Use"
|
||||
release_method = "patch"
|
||||
release_path = "/browsers/{session_id}"
|
||||
# Hidden from the hermes tools picker: the "Browser Use" row activates the
|
||||
# CLI backend (tools/browser_use_cli.py). This provider stays registered for
|
||||
# the Nous gateway path and un-migrated legacy cloud_provider configs.
|
||||
# Hidden from the picker: the "Browser Use" row activates the CLI backend
|
||||
# (tools/browser_use_cli.py). This provider stays registered for the Nous
|
||||
# gateway path and un-migrated legacy cloud_provider configs.
|
||||
setup_tag = None
|
||||
|
||||
def is_available(self) -> bool:
|
||||
return self._get_config_or_none(refresh_token=False) is not None
|
||||
|
||||
def _get_config_or_none(self, *, refresh_token: bool = True) -> Optional[Dict[str, Any]]:
|
||||
# Lazy import: managed_tool_gateway pulls in the Nous auth stack, which
|
||||
# Lazy: managed_tool_gateway pulls in the Nous auth stack, which
|
||||
# direct-API-key users never need.
|
||||
from tools.managed_tool_gateway import peek_nous_access_token, resolve_managed_tool_gateway
|
||||
from tools.tool_backend_helpers import NOUS_MANAGED_PROVIDER, read_selection
|
||||
@@ -108,8 +100,8 @@ class BrowserUseBrowserProvider(CloudBrowserProvider):
|
||||
selected = read_selection("browser")
|
||||
direct = {"api_key": api_key, "base_url": _BASE_URL, "managed_mode": False}
|
||||
|
||||
# Strict selection: "nous" (or legacy use_gateway: true) → managed gateway
|
||||
# ONLY; any other stored selection → direct key ONLY (no silent managed
|
||||
# Strict selection: "nous" (or legacy use_gateway: true) → managed ONLY;
|
||||
# any other stored selection → direct key ONLY (no silent managed
|
||||
# fallback); never-configured → direct key when present, else managed.
|
||||
if selected == NOUS_MANAGED_PROVIDER:
|
||||
return _managed_config()
|
||||
@@ -153,14 +145,12 @@ class BrowserUseBrowserProvider(CloudBrowserProvider):
|
||||
headers = self._headers(config)
|
||||
if managed_mode:
|
||||
headers["X-Idempotency-Key"] = _get_or_create_pending_create_key(task_id)
|
||||
|
||||
# Keep gateway-backed sessions short so billing authorization does not
|
||||
# default to a long Browser-Use timeout for a task-scoped browser.
|
||||
payload = (
|
||||
{"timeout": _DEFAULT_MANAGED_TIMEOUT_MINUTES, "proxyCountryCode": _DEFAULT_MANAGED_PROXY_COUNTRY_CODE}
|
||||
if managed_mode else {}
|
||||
)
|
||||
|
||||
# Managed mode propagates network errors raw so callers can retry with
|
||||
# the preserved idempotency key; direct mode wraps them.
|
||||
response = self._post_create(
|
||||
@@ -175,7 +165,6 @@ class BrowserUseBrowserProvider(CloudBrowserProvider):
|
||||
_clear_pending_create_key(task_id)
|
||||
session_name = self._session_name(task_id)
|
||||
logger.info("Created Browser Use session %s", session_name)
|
||||
|
||||
return {
|
||||
"session_name": session_name,
|
||||
"bb_session_id": session_data["id"],
|
||||
|
||||
@@ -1,13 +1,10 @@
|
||||
"""Browserbase cloud browser provider (plugin form).
|
||||
"""Browserbase cloud browser provider.
|
||||
|
||||
Direct ``BROWSERBASE_API_KEY`` + ``BROWSERBASE_PROJECT_ID`` credentials only;
|
||||
the Nous subscription routes through Browser Use instead.
|
||||
|
||||
Config: ``browser.cloud_provider: "browserbase"``. Optional knobs:
|
||||
``BROWSERBASE_BASE_URL`` (default https://api.browserbase.com),
|
||||
``BROWSERBASE_PROXIES`` (default true), ``BROWSERBASE_ADVANCED_STEALTH``
|
||||
(default false), ``BROWSERBASE_KEEP_ALIVE`` (default true),
|
||||
``BROWSERBASE_SESSION_TIMEOUT`` (seconds, integer, max 21600 = 6h).
|
||||
Direct ``BROWSERBASE_API_KEY`` + ``BROWSERBASE_PROJECT_ID`` only (the Nous
|
||||
subscription routes through Browser Use). Config: ``browser.cloud_provider:
|
||||
"browserbase"``; knobs ``BROWSERBASE_BASE_URL``, ``BROWSERBASE_PROXIES`` (true),
|
||||
``BROWSERBASE_ADVANCED_STEALTH`` (false), ``BROWSERBASE_KEEP_ALIVE`` (true),
|
||||
``BROWSERBASE_SESSION_TIMEOUT`` (seconds, max 21600).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -43,13 +40,13 @@ class BrowserbaseBrowserProvider(CloudBrowserProvider):
|
||||
def _get_config_or_none(self) -> Optional[Dict[str, Any]]:
|
||||
api_key = get_secret("BROWSERBASE_API_KEY")
|
||||
project_id = get_secret("BROWSERBASE_PROJECT_ID")
|
||||
if api_key and project_id:
|
||||
return {
|
||||
"api_key": api_key,
|
||||
"project_id": project_id,
|
||||
"base_url": os.environ.get("BROWSERBASE_BASE_URL", "https://api.browserbase.com").rstrip("/"),
|
||||
}
|
||||
return None
|
||||
if not (api_key and project_id):
|
||||
return None
|
||||
return {
|
||||
"api_key": api_key,
|
||||
"project_id": project_id,
|
||||
"base_url": os.environ.get("BROWSERBASE_BASE_URL", "https://api.browserbase.com").rstrip("/"),
|
||||
}
|
||||
|
||||
def _headers(self, config: Dict[str, Any]) -> Dict[str, str]:
|
||||
return {"Content-Type": "application/json", "X-BB-API-Key": config["api_key"]}
|
||||
@@ -62,7 +59,6 @@ class BrowserbaseBrowserProvider(CloudBrowserProvider):
|
||||
|
||||
def create_session(self, task_id: str) -> Dict[str, object]:
|
||||
config = self._get_config()
|
||||
|
||||
enable_proxies = os.environ.get("BROWSERBASE_PROXIES", "true").lower() != "false"
|
||||
enable_advanced_stealth = os.environ.get("BROWSERBASE_ADVANCED_STEALTH", "false").lower() == "true"
|
||||
enable_keep_alive = os.environ.get("BROWSERBASE_KEEP_ALIVE", "true").lower() != "false"
|
||||
@@ -88,30 +84,26 @@ class BrowserbaseBrowserProvider(CloudBrowserProvider):
|
||||
response = self._post_create(url, headers, session_config)
|
||||
|
||||
# 402 — paid features unavailable: drop keepAlive, then proxies, and retry.
|
||||
proxies_fallback = False
|
||||
keepalive_fallback = False
|
||||
if response.status_code == 402:
|
||||
if enable_keep_alive:
|
||||
keepalive_fallback = True
|
||||
logger.warning(
|
||||
"keepAlive may require paid plan (402), retrying without it. "
|
||||
"Sessions may timeout during long operations."
|
||||
)
|
||||
session_config.pop("keepAlive", None)
|
||||
response = self._post_create(url, headers, session_config)
|
||||
if response.status_code == 402 and enable_proxies:
|
||||
proxies_fallback = True
|
||||
logger.warning(
|
||||
"Proxies unavailable (402), retrying without proxies. "
|
||||
"Bot detection may be less effective."
|
||||
)
|
||||
session_config.pop("proxies", None)
|
||||
response = self._post_create(url, headers, session_config)
|
||||
proxies_fallback = keepalive_fallback = False
|
||||
if response.status_code == 402 and enable_keep_alive:
|
||||
keepalive_fallback = True
|
||||
logger.warning(
|
||||
"keepAlive may require paid plan (402), retrying without it. "
|
||||
"Sessions may timeout during long operations."
|
||||
)
|
||||
session_config.pop("keepAlive", None)
|
||||
response = self._post_create(url, headers, session_config)
|
||||
if response.status_code == 402 and enable_proxies:
|
||||
proxies_fallback = True
|
||||
logger.warning(
|
||||
"Proxies unavailable (402), retrying without proxies. Bot detection may be less effective."
|
||||
)
|
||||
session_config.pop("proxies", None)
|
||||
response = self._post_create(url, headers, session_config)
|
||||
self._check_created(response)
|
||||
|
||||
session_data = response.json()
|
||||
session_name = self._session_name(task_id)
|
||||
|
||||
features_enabled = {
|
||||
"basic_stealth": True,
|
||||
"proxies": enable_proxies and not proxies_fallback,
|
||||
@@ -121,7 +113,6 @@ class BrowserbaseBrowserProvider(CloudBrowserProvider):
|
||||
}
|
||||
feature_str = ", ".join(k for k, v in features_enabled.items() if v)
|
||||
logger.info("Created Browserbase session %s with features: %s", session_name, feature_str)
|
||||
|
||||
return {
|
||||
"session_name": session_name,
|
||||
"bb_session_id": session_data["id"],
|
||||
|
||||
@@ -1,13 +1,10 @@
|
||||
"""Firecrawl cloud browser provider (plugin form).
|
||||
|
||||
Cloud-browser path only (``/v2/browser``) — distinct from the firecrawl WEB
|
||||
plugin at ``plugins/web/firecrawl/`` (search/extract/crawl on ``/v2/*``); both
|
||||
share ``FIRECRAWL_API_KEY``.
|
||||
"""Firecrawl cloud browser provider (``/v2/browser`` only — the firecrawl WEB
|
||||
plugin under ``plugins/web/firecrawl/`` shares ``FIRECRAWL_API_KEY``).
|
||||
|
||||
Config: ``browser.cloud_provider: "firecrawl"`` (explicit selection only — not
|
||||
in the legacy auto-detect walk). Env: ``FIRECRAWL_API_KEY``
|
||||
(https://firecrawl.dev), ``FIRECRAWL_API_URL`` (default
|
||||
https://api.firecrawl.dev), ``FIRECRAWL_BROWSER_TTL`` (default 300 seconds).
|
||||
in the legacy auto-detect walk). Env: ``FIRECRAWL_API_KEY``,
|
||||
``FIRECRAWL_API_URL`` (default https://api.firecrawl.dev), ``FIRECRAWL_BROWSER_TTL``
|
||||
(default 300 seconds).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -65,11 +62,9 @@ class FirecrawlBrowserProvider(CloudBrowserProvider):
|
||||
|
||||
response = self._post_create(f"{self._api_url()}/v2/browser", self._headers(), {"ttl": ttl})
|
||||
self._check_created(response)
|
||||
|
||||
data = response.json()
|
||||
session_name = self._session_name(task_id)
|
||||
logger.info("Created Firecrawl browser session %s", session_name)
|
||||
|
||||
return {
|
||||
"session_name": session_name,
|
||||
"bb_session_id": data["id"],
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
"""Shared helpers for the bundled ``image_gen`` provider plugins.
|
||||
|
||||
Every provider under ``plugins/image_gen/`` is loaded by path (module name
|
||||
``hermes_plugins.image_gen__<name>``) and resolves this module through the
|
||||
repo root on ``sys.path`` — the same way they import ``agent.image_gen_provider``.
|
||||
Nothing here is a plugin: the scanner only looks at directories.
|
||||
Providers are loaded by path (``hermes_plugins.image_gen__<name>``) and resolve
|
||||
this module through the repo root on ``sys.path``. Not a plugin itself: the
|
||||
scanner only looks at directories.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -22,18 +21,12 @@ from agent.image_gen_provider import (
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# OpenAI-style ``size`` strings for the three semantic aspect ratios. Shared by
|
||||
# every OpenAI-compatible ``images.generations`` backend so aspect_ratio means
|
||||
# the same thing across providers.
|
||||
OPENAI_SIZES: Dict[str, str] = {
|
||||
"landscape": "1536x1024",
|
||||
"square": "1024x1024",
|
||||
"portrait": "1024x1536",
|
||||
}
|
||||
# OpenAI-style ``size`` strings for the three semantic aspect ratios, shared by
|
||||
# every OpenAI-compatible ``images.generations`` backend.
|
||||
OPENAI_SIZES: Dict[str, str] = {"landscape": "1536x1024", "square": "1024x1024", "portrait": "1024x1536"}
|
||||
|
||||
# gpt-image-2 quality tiers, exposed as three virtual model ids so the picker
|
||||
# and ``image_gen.model`` behave like any other multi-model backend. All three
|
||||
# hit the same API model with a different ``quality`` knob.
|
||||
# gpt-image-2 quality tiers as three virtual model ids (same API model,
|
||||
# different ``quality`` knob) so the picker works like any multi-model backend.
|
||||
GPT_IMAGE_2_API_MODEL = "gpt-image-2"
|
||||
GPT_IMAGE_2_DEFAULT = "gpt-image-2-medium"
|
||||
GPT_IMAGE_2_TIERS: Dict[str, Dict[str, Any]] = {
|
||||
@@ -102,11 +95,9 @@ def resolve_static_model(
|
||||
"""
|
||||
if isinstance(explicit, str) and explicit.strip() in models:
|
||||
return explicit.strip(), models[explicit.strip()]
|
||||
|
||||
env_override = os.environ.get(env_var)
|
||||
if env_override and env_override in models:
|
||||
return env_override, models[env_override]
|
||||
|
||||
cfg = load_image_gen_config() if config is None else config
|
||||
scoped = cfg.get(config_key)
|
||||
candidates = [scoped.get("model") if isinstance(scoped, dict) else None]
|
||||
@@ -115,14 +106,11 @@ def resolve_static_model(
|
||||
for candidate in candidates:
|
||||
if isinstance(candidate, str) and candidate in models:
|
||||
return candidate, models[candidate]
|
||||
|
||||
return default, models[default]
|
||||
|
||||
|
||||
def collect_source_images(
|
||||
image_url: Optional[str],
|
||||
reference_image_urls: Optional[List[str]],
|
||||
limit: Optional[int] = None,
|
||||
image_url: Optional[str], reference_image_urls: Optional[List[str]], limit: Optional[int] = None
|
||||
) -> List[str]:
|
||||
"""Primary ``image_url`` first, then normalized references, clamped to ``limit``."""
|
||||
sources: List[str] = []
|
||||
@@ -138,11 +126,8 @@ def catalog_rows(
|
||||
*,
|
||||
price: Optional[str] = None,
|
||||
) -> List[Dict[str, Any]]:
|
||||
"""Picker rows for a catalog: ``id`` plus ``fields`` read from each meta.
|
||||
|
||||
A missing ``display`` falls back to the model id, other missing fields to
|
||||
``""``; ``price`` (when given) overrides the per-model value.
|
||||
"""
|
||||
"""Picker rows: ``id`` plus ``fields`` from each meta (missing ``display`` →
|
||||
model id, other fields → ``""``); ``price`` overrides the per-model value."""
|
||||
rows = []
|
||||
for model_id, meta in models.items():
|
||||
row: Dict[str, Any] = {"id": model_id}
|
||||
@@ -159,10 +144,7 @@ def api_key_setup_schema(
|
||||
) -> Dict[str, Any]:
|
||||
"""``get_setup_schema()`` dict for a provider authenticated by one env var."""
|
||||
return {
|
||||
"name": name,
|
||||
"badge": badge,
|
||||
"tag": tag,
|
||||
"env_vars": [{"key": key, "prompt": prompt, "url": url}],
|
||||
"name": name, "badge": badge, "tag": tag, "env_vars": [{"key": key, "prompt": prompt, "url": url}],
|
||||
}
|
||||
|
||||
|
||||
@@ -178,20 +160,11 @@ def error_factory(provider: str, aspect: str, *, model: str = "", prompt: str =
|
||||
|
||||
|
||||
def prompt_required_error(provider: str, aspect: str) -> Dict[str, Any]:
|
||||
return error_response(
|
||||
error=PROMPT_REQUIRED,
|
||||
error_type="invalid_argument",
|
||||
provider=provider,
|
||||
aspect_ratio=aspect,
|
||||
)
|
||||
return error_factory(provider, aspect)(PROMPT_REQUIRED, "invalid_argument")
|
||||
|
||||
|
||||
def openai_importable() -> bool:
|
||||
try:
|
||||
import openai # noqa: F401
|
||||
except ImportError:
|
||||
return False
|
||||
return True
|
||||
return import_openai("", "")[0] is not None
|
||||
|
||||
|
||||
def import_openai(provider: str, aspect: str) -> Tuple[Any, Optional[Dict[str, Any]]]:
|
||||
@@ -199,12 +172,7 @@ def import_openai(provider: str, aspect: str) -> Tuple[Any, Optional[Dict[str, A
|
||||
try:
|
||||
import openai
|
||||
except ImportError:
|
||||
return None, error_response(
|
||||
error=OPENAI_MISSING,
|
||||
error_type="missing_dependency",
|
||||
provider=provider,
|
||||
aspect_ratio=aspect,
|
||||
)
|
||||
return None, error_factory(provider, aspect)(OPENAI_MISSING, "missing_dependency")
|
||||
return openai, None
|
||||
|
||||
|
||||
@@ -220,12 +188,11 @@ def materialize_image(
|
||||
aspect: str,
|
||||
log: logging.Logger = logger,
|
||||
) -> Tuple[Optional[str], Optional[Dict[str, Any]]]:
|
||||
"""Turn a ``(b64_json, url)`` result pair into a local image reference.
|
||||
"""``(image_ref, None)`` or ``(None, error_dict)`` for a ``(b64_json, url)`` pair.
|
||||
|
||||
Returns ``(image_ref, None)`` or ``(None, error_dict)``. Base64 is always
|
||||
cached (a write failure is an ``io_error``); a URL is cached best-effort
|
||||
because provider URLs are often ephemeral, falling back to the bare URL so
|
||||
a cache hiccup never destroys a successful generation.
|
||||
Base64 is always cached (write failure → ``io_error``); a URL is cached
|
||||
best-effort, falling back to the bare URL so a cache hiccup never destroys
|
||||
a successful generation.
|
||||
"""
|
||||
fail = error_factory(provider, aspect, model=model, prompt=prompt)
|
||||
if b64:
|
||||
@@ -243,9 +210,7 @@ def cache_url_best_effort(url: str, *, prefix: str, label: str, log: logging.Log
|
||||
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,
|
||||
)
|
||||
log.warning("%s image URL %s could not be cached (%s); falling back to bare URL.", label, url, exc)
|
||||
return url
|
||||
|
||||
|
||||
@@ -260,10 +225,9 @@ def requests_error_message(response: Any, exc: Exception) -> str:
|
||||
@dataclass
|
||||
class HttpFailure:
|
||||
"""One failed ``post_json`` attempt, pre-shaped as ``(error, error_type)``.
|
||||
|
||||
``kind`` is ``http`` / ``timeout`` / ``connection`` / ``request`` /
|
||||
``invalid_json``; ``status`` and ``response`` are set for ``http`` only.
|
||||
"""
|
||||
``kind`` ∈ http / timeout / connection / request / invalid_json; ``message``
|
||||
is the extracted HTTP error text or ``str(exc)``; ``status`` / ``response``
|
||||
are set for ``http`` only."""
|
||||
|
||||
kind: str
|
||||
error: str
|
||||
@@ -303,20 +267,21 @@ def post_json(
|
||||
"http", f"{label} image generation failed ({status}): {message}", "api_error",
|
||||
status=status, message=message, response=resp,
|
||||
)
|
||||
except requests.Timeout:
|
||||
except requests.Timeout as exc:
|
||||
return None, HttpFailure(
|
||||
"timeout", f"{label} image generation timed out ({int(read_timeout)}s)", "timeout"
|
||||
"timeout", f"{label} image generation timed out ({int(read_timeout)}s)", "timeout", message=str(exc),
|
||||
)
|
||||
except requests.ConnectionError as exc:
|
||||
return None, HttpFailure("connection", f"{label} connection error: {exc}", "connection_error")
|
||||
return None, HttpFailure(
|
||||
"connection", f"{label} connection error: {exc}", "connection_error", message=str(exc),
|
||||
)
|
||||
except requests.RequestException as exc:
|
||||
if not catch_request_exception:
|
||||
raise
|
||||
return None, HttpFailure("request", f"{label} request failed: {exc}", "api_error")
|
||||
|
||||
return None, HttpFailure("request", f"{label} request failed: {exc}", "api_error", message=str(exc))
|
||||
try:
|
||||
return response.json(), None
|
||||
except Exception as exc: # noqa: BLE001
|
||||
return None, HttpFailure(
|
||||
"invalid_json", f"{label} returned invalid JSON: {exc}", "invalid_response"
|
||||
"invalid_json", f"{label} returned invalid JSON: {exc}", "invalid_response", message=str(exc),
|
||||
)
|
||||
|
||||
@@ -1,19 +1,14 @@
|
||||
"""DeepInfra image generation backend.
|
||||
|
||||
Exposes DeepInfra's image-gen catalog (FLUX, Qwen-Image-Edit, …) through the
|
||||
OpenAI-compatible ``/v1/openai/images/generations`` endpoint.
|
||||
OpenAI-compatible ``/v1/openai/images/generations`` endpoint. Model discovery
|
||||
is fully dynamic: ``list_models()`` filters the tagged catalog via
|
||||
:func:`hermes_cli.models._fetch_deepinfra_models_by_tag` (``image-gen``), so
|
||||
no model ids are hardcoded here.
|
||||
|
||||
**Fully dynamic model discovery.** DeepInfra publishes one tagged catalog at
|
||||
``https://api.deepinfra.com/v1/openai/models?filter=true&sort_by=hermes``
|
||||
where each entry's ``metadata.tags`` declares its surface (``image-gen``
|
||||
here). ``list_models()`` filters it via
|
||||
:func:`hermes_cli.models._fetch_deepinfra_models_by_tag`, so no model ids are
|
||||
hardcoded here: new models appear in ``hermes tools`` automatically and
|
||||
retired ones disappear on the next fetch.
|
||||
|
||||
Model selection (first hit wins): ``DEEPINFRA_IMAGE_MODEL`` env →
|
||||
``image_gen.deepinfra.model`` → first model from the live catalog. When all
|
||||
three are absent ``generate()`` returns an error rather than guessing.
|
||||
Model selection: ``DEEPINFRA_IMAGE_MODEL`` env → ``image_gen.deepinfra.model``
|
||||
→ first model from the live catalog; when all are absent ``generate()`` errors
|
||||
rather than guessing.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -54,7 +49,7 @@ def _live_models() -> Optional[List[Dict[str, Any]]]:
|
||||
|
||||
|
||||
def _format_catalog_row(item: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""Format a catalog item into the picker row shape."""
|
||||
"""Picker row for a catalog item."""
|
||||
mid = item.get("id", "")
|
||||
metadata = item.get("metadata") or {}
|
||||
if not isinstance(metadata, dict):
|
||||
@@ -77,22 +72,16 @@ def _format_catalog_row(item: Dict[str, Any]) -> Dict[str, Any]:
|
||||
|
||||
|
||||
def _resolve_model(catalog: List[Dict[str, Any]], cfg: Dict[str, Any]) -> Optional[str]:
|
||||
"""Pick the model id (env > config > first live result, else None).
|
||||
|
||||
Takes the already-loaded ``image_gen.deepinfra`` config so ``generate()``
|
||||
reads config once instead of via a second ``load_config`` deepcopy.
|
||||
"""
|
||||
"""Model id: env > config > first live result, else None. Takes the loaded
|
||||
``image_gen.deepinfra`` config so ``generate()`` reads config once."""
|
||||
env_override = os.environ.get("DEEPINFRA_IMAGE_MODEL", "").strip()
|
||||
if env_override:
|
||||
return env_override
|
||||
cfg_model = cfg.get("model") if isinstance(cfg, dict) else None
|
||||
if isinstance(cfg_model, str) and cfg_model.strip():
|
||||
return cfg_model.strip()
|
||||
if catalog:
|
||||
first = catalog[0].get("id")
|
||||
if isinstance(first, str) and first:
|
||||
return first
|
||||
return None
|
||||
first = catalog[0].get("id") if catalog else None
|
||||
return first if isinstance(first, str) and first else None
|
||||
|
||||
|
||||
class DeepInfraImageGenProvider(ImageGenProvider):
|
||||
@@ -127,26 +116,18 @@ class DeepInfraImageGenProvider(ImageGenProvider):
|
||||
url="https://deepinfra.com/dash/api_keys",
|
||||
)
|
||||
|
||||
def generate(
|
||||
self,
|
||||
prompt: str,
|
||||
aspect_ratio: str = DEFAULT_ASPECT_RATIO,
|
||||
**kwargs: Any,
|
||||
) -> Dict[str, Any]:
|
||||
def generate(self, prompt: str, aspect_ratio: str = DEFAULT_ASPECT_RATIO, **kwargs: Any) -> Dict[str, Any]:
|
||||
prompt = (prompt or "").strip()
|
||||
aspect = resolve_aspect_ratio(aspect_ratio)
|
||||
fail = error_factory("deepinfra", aspect)
|
||||
|
||||
if kwargs.get("image_url") or kwargs.get("reference_image_urls"):
|
||||
return fail(
|
||||
"DeepInfra image generation is text-to-image only in this "
|
||||
"backend; image_url and reference_image_urls are unsupported.",
|
||||
"modality_unsupported", prompt=prompt,
|
||||
)
|
||||
|
||||
if not prompt:
|
||||
return prompt_required_error("deepinfra", aspect)
|
||||
|
||||
api_key = (get_secret("DEEPINFRA_API_KEY", "") or "").strip()
|
||||
if not api_key:
|
||||
return fail(
|
||||
@@ -155,7 +136,6 @@ class DeepInfraImageGenProvider(ImageGenProvider):
|
||||
"to add the key.",
|
||||
"auth_required",
|
||||
)
|
||||
|
||||
di_cfg = load_image_gen_config("deepinfra")
|
||||
model_id = _resolve_model(_live_models() or [], di_cfg)
|
||||
if not model_id:
|
||||
@@ -169,12 +149,10 @@ class DeepInfraImageGenProvider(ImageGenProvider):
|
||||
size = size_for(aspect)
|
||||
from hermes_cli.models import deepinfra_base_url
|
||||
|
||||
# OpenAI-compatible endpoint — the openai SDK supplies retry, timeout
|
||||
# and error mapping (mirrors the OpenAI image-gen plugin).
|
||||
# OpenAI-compatible endpoint — the openai SDK supplies retry, timeout and error mapping.
|
||||
openai, err = import_openai("deepinfra", aspect)
|
||||
if err:
|
||||
return err
|
||||
|
||||
fail = error_factory("deepinfra", aspect, model=model_id, prompt=prompt)
|
||||
client = openai.OpenAI(api_key=api_key, base_url=deepinfra_base_url(di_cfg))
|
||||
try:
|
||||
@@ -190,23 +168,19 @@ class DeepInfraImageGenProvider(ImageGenProvider):
|
||||
data = getattr(response, "data", None) or []
|
||||
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 so downstream
|
||||
# consumers (Telegram send_photo, browser fetch) don't get a dead link.
|
||||
# Best-effort: fall back to the bare URL if the download fails.
|
||||
# Delivery URLs are often short-lived; materialise locally, best-effort.
|
||||
try:
|
||||
image_ref = str(save_url_image(url, prefix=prefix))
|
||||
except Exception as exc:
|
||||
@@ -214,13 +188,8 @@ class DeepInfraImageGenProvider(ImageGenProvider):
|
||||
image_ref = url
|
||||
else:
|
||||
return fail("DeepInfra response contained neither b64_json nor URL", "empty_response")
|
||||
|
||||
return success_response(
|
||||
image=image_ref,
|
||||
model=model_id,
|
||||
prompt=prompt,
|
||||
aspect_ratio=aspect,
|
||||
provider="deepinfra",
|
||||
image=image_ref, model=model_id, prompt=prompt, aspect_ratio=aspect, provider="deepinfra",
|
||||
extra={"size": size},
|
||||
)
|
||||
|
||||
|
||||
@@ -1,14 +1,9 @@
|
||||
"""FAL.ai image generation backend.
|
||||
"""FAL.ai image generation backend — registration adapter.
|
||||
|
||||
Wraps the FAL catalog (FLUX 2, Z-Image, Nano Banana, GPT Image 1.5, Recraft,
|
||||
Imagen 4, Qwen, Ideogram, …) as an :class:`ImageGenProvider`.
|
||||
|
||||
The heavy lifting — model catalog, payload construction, request submission,
|
||||
managed-Nous-gateway selection, Clarity Upscaler chaining — lives in
|
||||
:mod:`tools.image_generation_tool`. This plugin reaches into that module via
|
||||
call-time indirection (``import tools.image_generation_tool as _it``) so the
|
||||
existing tests keep patching ``image_tool.*`` unchanged, and there is exactly
|
||||
one canonical FAL code path on disk — the plugin is a registration adapter.
|
||||
Catalog, payload construction, submission, managed-Nous-gateway selection and
|
||||
Clarity Upscaler chaining live in :mod:`tools.image_generation_tool`; this
|
||||
plugin reaches into it at call time (``import tools.image_generation_tool as
|
||||
_it``) so tests keep patching ``image_tool.*`` and there is one FAL code path.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -43,9 +38,9 @@ class FalImageGenProvider(ImageGenProvider):
|
||||
return "FAL.ai"
|
||||
|
||||
def is_available(self) -> bool:
|
||||
# Direct FAL_KEY or a managed Nous fal-queue origin; both checks live in
|
||||
# the legacy module so this provider tracks whatever logic ships there.
|
||||
# Direct FAL_KEY or a managed Nous fal-queue origin, per the legacy module.
|
||||
import tools.image_generation_tool as _it
|
||||
|
||||
try:
|
||||
return bool(_it.check_fal_api_key())
|
||||
except Exception: # noqa: BLE001 — never break the picker
|
||||
@@ -67,9 +62,8 @@ class FalImageGenProvider(ImageGenProvider):
|
||||
)
|
||||
|
||||
def capabilities(self) -> Dict[str, Any]:
|
||||
# Image-to-image depends on the currently selected FAL model (each entry
|
||||
# declares an edit_endpoint or not); Clarity Upscaler chains on request
|
||||
# for any model.
|
||||
# Image-to-image depends on the selected FAL model (``edit_endpoint``);
|
||||
# Clarity Upscaler chains on request for any model.
|
||||
import tools.image_generation_tool as _it
|
||||
|
||||
try:
|
||||
@@ -98,12 +92,8 @@ class FalImageGenProvider(ImageGenProvider):
|
||||
import tools.image_generation_tool as _it
|
||||
|
||||
aspect = resolve_aspect_ratio(aspect_ratio)
|
||||
passthrough = {
|
||||
key: kwargs[key] for key in _PASSTHROUGH_KWARGS
|
||||
if key in kwargs and kwargs[key] is not None
|
||||
}
|
||||
# Only forward image-to-image inputs when supplied, so a plain
|
||||
# text-to-image call delegates exactly as before (no noisy None kwargs).
|
||||
passthrough = {key: kwargs[key] for key in _PASSTHROUGH_KWARGS if kwargs.get(key) is not None}
|
||||
# Only forward image-to-image inputs when supplied (no noisy None kwargs).
|
||||
if image_url is not None:
|
||||
passthrough["image_url"] = image_url
|
||||
if reference_image_urls is not None:
|
||||
@@ -114,13 +104,8 @@ class FalImageGenProvider(ImageGenProvider):
|
||||
except Exception as exc: # noqa: BLE001 — never raise out of generate
|
||||
logger.warning("FAL image_generate_tool raised: %s", exc, exc_info=True)
|
||||
return {
|
||||
"success": False,
|
||||
"image": None,
|
||||
"error": f"FAL image generation failed: {exc}",
|
||||
"error_type": type(exc).__name__,
|
||||
"provider": "fal",
|
||||
"prompt": prompt,
|
||||
"aspect_ratio": aspect,
|
||||
"success": False, "image": None, "error": f"FAL image generation failed: {exc}",
|
||||
"error_type": type(exc).__name__, "provider": "fal", "prompt": prompt, "aspect_ratio": aspect,
|
||||
}
|
||||
|
||||
try:
|
||||
@@ -130,14 +115,11 @@ class FalImageGenProvider(ImageGenProvider):
|
||||
|
||||
if not isinstance(response, dict):
|
||||
response = {
|
||||
"success": False,
|
||||
"image": None,
|
||||
"error": "FAL pipeline returned a non-dict response",
|
||||
"success": False, "image": None, "error": "FAL pipeline returned a non-dict response",
|
||||
"error_type": "provider_contract",
|
||||
}
|
||||
|
||||
# Stamp the uniform shape declared in ``agent.image_gen_provider``; the
|
||||
# legacy pipeline resolves the model internally, so query it after the fact.
|
||||
# Stamp the uniform provider shape; the legacy pipeline resolves the
|
||||
# model internally, so query it after the fact.
|
||||
response.setdefault("provider", "fal")
|
||||
response.setdefault("prompt", prompt)
|
||||
response.setdefault("aspect_ratio", aspect)
|
||||
|
||||
@@ -15,7 +15,7 @@ from __future__ import annotations
|
||||
import logging
|
||||
import time
|
||||
import uuid
|
||||
from typing import Any, Dict, List, Optional, Tuple
|
||||
from typing import Any, Callable, Dict, List, Optional, Tuple
|
||||
|
||||
import requests
|
||||
|
||||
@@ -33,6 +33,7 @@ from plugins.image_gen._common import (
|
||||
collect_source_images,
|
||||
error_factory,
|
||||
load_image_gen_config,
|
||||
post_json,
|
||||
prompt_required_error,
|
||||
resolve_static_model,
|
||||
)
|
||||
@@ -99,6 +100,17 @@ _ENHANCE_PATH = "/generate/enhance/krea/enhance"
|
||||
_ENHANCE_SCALE_FACTOR = 2
|
||||
_USER_AGENT = "Hermes-Agent/1.0 (krea-image-gen)"
|
||||
|
||||
# Fatal poll outcome (``_poll_krea_job`` ``kind``) → (error_type, message builder).
|
||||
_POLL_FAILURES: Dict[str, Tuple[str, Callable[[str, Any], str]]] = {
|
||||
"http": ("api_error", lambda job_id, detail: f"Krea poll failed ({detail}) for job {job_id}"),
|
||||
"timeout": ("timeout", lambda job_id, detail: f"Krea poll timed out for job {job_id}: {detail}"),
|
||||
"invalid_json": ("invalid_response", lambda job_id, detail: f"Krea poll returned invalid JSON: {detail}"),
|
||||
"deadline": ("timeout", lambda job_id, detail: (
|
||||
f"Krea job {job_id} did not complete within "
|
||||
f"{int(_POLL_TIMEOUT_SECONDS)}s (last status: {detail or 'unknown'})"
|
||||
)),
|
||||
}
|
||||
|
||||
|
||||
def _load_krea_config() -> Dict[str, Any]:
|
||||
"""Read ``image_gen`` (the krea section lives under ``image_gen.krea``)."""
|
||||
@@ -418,7 +430,9 @@ class KreaImageGenProvider(ImageGenProvider):
|
||||
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"))
|
||||
style_refs = _collect_style_refs(
|
||||
image_url, reference_image_urls, kwargs.get("image_style_references")
|
||||
)
|
||||
if not prompt:
|
||||
return prompt_required_error("krea", aspect)
|
||||
|
||||
@@ -460,45 +474,39 @@ class KreaImageGenProvider(ImageGenProvider):
|
||||
)
|
||||
|
||||
# 1. Submit job.
|
||||
try:
|
||||
response = requests.post(
|
||||
f"{base_url}/generate/image/krea/krea-2/{meta['path']}",
|
||||
headers=_headers(auth_token, managed=managed is not None, json_body=True),
|
||||
json=payload, timeout=30,
|
||||
)
|
||||
response.raise_for_status()
|
||||
except requests.HTTPError as exc:
|
||||
resp = exc.response
|
||||
status = resp.status_code if resp is not None else 0
|
||||
err_msg = _submit_error_message(resp, exc)
|
||||
logger.error("Krea submit failed (%d): %s", status, err_msg)
|
||||
# Managed 4xx: the model may not be enabled/priced on the Nous
|
||||
# Portal, or the gateway's shared key hit its concurrency cap (429).
|
||||
if managed is not None and 400 <= status < 500:
|
||||
hint = (
|
||||
"Krea's shared-key concurrency cap was hit — retry shortly."
|
||||
if status == 429
|
||||
else (
|
||||
f"Model '{model_id}' may not be enabled/priced on the "
|
||||
"Nous Portal's Krea gateway. Set KREA_API_KEY to use "
|
||||
"Krea directly, or pick a different model via "
|
||||
"`hermes tools` → Image Generation."
|
||||
submit_body, failure = post_json(
|
||||
f"{base_url}/generate/image/krea/krea-2/{meta['path']}",
|
||||
headers=_headers(auth_token, managed=managed is not None, json_body=True),
|
||||
payload=payload, timeout=30, label="Krea", error_message=_submit_error_message,
|
||||
)
|
||||
if failure is not None:
|
||||
if failure.kind == "http":
|
||||
status, err_msg = failure.status, failure.message
|
||||
logger.error("Krea submit failed (%d): %s", status, err_msg)
|
||||
# Managed 4xx: the model may not be enabled/priced on the Nous
|
||||
# Portal, or the gateway's shared key hit its concurrency cap (429).
|
||||
if managed is not None and 400 <= status < 500:
|
||||
hint = (
|
||||
"Krea's shared-key concurrency cap was hit — retry shortly."
|
||||
if status == 429
|
||||
else (
|
||||
f"Model '{model_id}' may not be enabled/priced on the "
|
||||
"Nous Portal's Krea gateway. Set KREA_API_KEY to use "
|
||||
"Krea directly, or pick a different model via "
|
||||
"`hermes tools` → Image Generation."
|
||||
)
|
||||
)
|
||||
)
|
||||
return fail(
|
||||
f"Nous Subscription Krea gateway rejected '{model_id}' "
|
||||
f"(HTTP {status}): {err_msg}. {hint}",
|
||||
"api_error",
|
||||
)
|
||||
return fail(f"Krea image generation failed ({status}): {err_msg}", "api_error")
|
||||
except requests.Timeout:
|
||||
return fail("Krea submit timed out (30s)", "timeout")
|
||||
except requests.ConnectionError as exc:
|
||||
return fail(f"Krea connection error: {exc}", "connection_error")
|
||||
try:
|
||||
submit_body = response.json()
|
||||
except Exception as exc: # noqa: BLE001
|
||||
return fail(f"Krea returned invalid JSON on submit: {exc}", "invalid_response")
|
||||
return fail(
|
||||
f"Nous Subscription Krea gateway rejected '{model_id}' "
|
||||
f"(HTTP {status}): {err_msg}. {hint}",
|
||||
"api_error",
|
||||
)
|
||||
return fail(failure.error, "api_error")
|
||||
if failure.kind == "timeout":
|
||||
return fail("Krea submit timed out (30s)", "timeout")
|
||||
if failure.kind == "invalid_json":
|
||||
return fail(f"Krea returned invalid JSON on submit: {failure.message}", "invalid_response")
|
||||
return fail(failure.error, failure.error_type)
|
||||
job_id = submit_body.get("job_id")
|
||||
if not isinstance(job_id, str) or not job_id:
|
||||
return fail("Krea submit response missing job_id", "invalid_response")
|
||||
@@ -508,20 +516,9 @@ class KreaImageGenProvider(ImageGenProvider):
|
||||
poll_errors: List[Dict[str, Any]] = []
|
||||
|
||||
def poll_error(kind: str, detail: Any) -> Dict[str, Any]:
|
||||
if kind == "http":
|
||||
err = fail(f"Krea poll failed ({detail}) for job {job_id}", "api_error")
|
||||
elif kind == "timeout":
|
||||
err = fail(f"Krea poll timed out for job {job_id}: {detail}", "timeout")
|
||||
elif kind == "invalid_json":
|
||||
err = fail(f"Krea poll returned invalid JSON: {detail}", "invalid_response")
|
||||
else:
|
||||
err = fail(
|
||||
f"Krea job {job_id} did not complete within "
|
||||
f"{int(_POLL_TIMEOUT_SECONDS)}s (last status: {detail or 'unknown'})",
|
||||
"timeout",
|
||||
)
|
||||
poll_errors.append(err)
|
||||
return err
|
||||
error_type, build = _POLL_FAILURES.get(kind, _POLL_FAILURES["deadline"])
|
||||
poll_errors.append(fail(build(job_id, detail), error_type))
|
||||
return poll_errors[-1]
|
||||
|
||||
job = _poll_krea_job(base_url, auth_token, job_id, on_error=poll_error)
|
||||
if poll_errors:
|
||||
@@ -549,9 +546,13 @@ class KreaImageGenProvider(ImageGenProvider):
|
||||
upscale_requested = kwargs.get("upscale")
|
||||
if not isinstance(upscale_requested, bool):
|
||||
cfg_upscale = _krea_section().get("upscale")
|
||||
upscale_requested = cfg_upscale if isinstance(cfg_upscale, bool) else bool(meta.get("upscale", False))
|
||||
upscale_requested = (
|
||||
cfg_upscale if isinstance(cfg_upscale, bool) else bool(meta.get("upscale", False))
|
||||
)
|
||||
if upscale_requested:
|
||||
enhanced_url = _enhance_image(base_url, auth_token, result_image_url, prompt, managed=managed is not None)
|
||||
enhanced_url = _enhance_image(
|
||||
base_url, auth_token, result_image_url, prompt, managed=managed is not None,
|
||||
)
|
||||
if enhanced_url:
|
||||
result_image_url = enhanced_url
|
||||
upscaled = True
|
||||
@@ -563,7 +564,8 @@ class KreaImageGenProvider(ImageGenProvider):
|
||||
image_ref = str(save_url_image(result_image_url, prefix=f"krea_{model_id}"))
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.warning(
|
||||
"Krea image URL %s could not be cached (%s); falling back to bare URL.", result_image_url, exc,
|
||||
"Krea image URL %s could not be cached (%s); falling back to bare URL.",
|
||||
result_image_url, exc,
|
||||
)
|
||||
image_ref = result_image_url
|
||||
|
||||
|
||||
@@ -1,14 +1,10 @@
|
||||
"""Meta Model API image generation backend.
|
||||
"""Meta Model API image generation backend (``muse-image``).
|
||||
|
||||
Exposes Meta's ``muse-image`` model(s) as an :class:`ImageGenProvider`.
|
||||
The Meta Model API (https://api.meta.ai/v1) is OpenAI-compatible, so we reuse
|
||||
the OpenAI Python SDK pointed at Meta's base URL and authenticate with
|
||||
``META_MODEL_API_KEY``. Output is base64 JSON (WebP) → ``$HERMES_HOME/cache/images/``.
|
||||
|
||||
Selection precedence (first hit wins): ``model`` kwarg forwarded by the
|
||||
dispatcher (the ``hermes tools`` pick) → ``META_IMAGE_MODEL`` env →
|
||||
``image_gen.meta-ai.model`` → ``image_gen.model`` (when it's one of our IDs)
|
||||
→ :data:`DEFAULT_MODEL`.
|
||||
The Meta Model API (https://api.meta.ai/v1) is OpenAI-compatible, so the OpenAI
|
||||
SDK is pointed at Meta's base URL with ``META_MODEL_API_KEY``. Output is base64
|
||||
WebP → ``$HERMES_HOME/cache/images/``. Selection: ``model`` kwarg →
|
||||
``META_IMAGE_MODEL`` → ``image_gen.meta-ai.model`` → ``image_gen.model`` →
|
||||
:data:`DEFAULT_MODEL`.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -40,9 +36,8 @@ from plugins.image_gen._common import (
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
DEFAULT_BASE_URL = "https://api.meta.ai/v1"
|
||||
# Auth env vars, in priority order. Mirrors the bundled ``meta-ai`` chat
|
||||
# provider (plugins/model-providers/meta-ai): MODEL_API_KEY is Meta's
|
||||
# documented var; the rest are accepted aliases.
|
||||
# Auth env vars, in priority order (mirrors the ``meta-ai`` chat provider):
|
||||
# MODEL_API_KEY is Meta's documented var; the rest are accepted aliases.
|
||||
API_KEY_ENVS = ("MODEL_API_KEY", "META_API_KEY", "META_MODEL_API_KEY")
|
||||
# Primary key shown in setup prompts / error messages.
|
||||
API_KEY_ENV = "META_MODEL_API_KEY"
|
||||
@@ -51,20 +46,15 @@ BASE_URL_ENV = "META_BASE_URL"
|
||||
|
||||
|
||||
def _resolve_api_key() -> Optional[str]:
|
||||
"""First non-empty auth env var, checked in priority order."""
|
||||
for env in API_KEY_ENVS:
|
||||
val = get_secret(env)
|
||||
if val:
|
||||
return val
|
||||
return None
|
||||
"""First non-empty auth env var, in priority order."""
|
||||
return next((val for val in map(get_secret, API_KEY_ENVS) if val), None)
|
||||
|
||||
|
||||
def _resolve_base_url() -> str:
|
||||
return (os.environ.get(BASE_URL_ENV) or "").strip() or DEFAULT_BASE_URL
|
||||
|
||||
|
||||
# Catalog shown in `hermes tools` and matched against `image_gen.model`.
|
||||
# The model id is sent verbatim to the Meta Model API (`/v1/images/generations`).
|
||||
# Model ids are sent verbatim to ``/v1/images/generations``.
|
||||
_MODELS: Dict[str, Dict[str, Any]] = {
|
||||
"muse-image-1.0": {
|
||||
"display": "Muse Image 1.0",
|
||||
@@ -77,10 +67,8 @@ DEFAULT_MODEL = "muse-image-1.0"
|
||||
|
||||
|
||||
def _resolve_model(caller_model: Optional[str] = None) -> Tuple[str, Dict[str, Any]]:
|
||||
"""Return (model_id, metadata); ``caller_model`` is the dispatcher's ``model`` kwarg."""
|
||||
return resolve_static_model(
|
||||
_MODELS, DEFAULT_MODEL, env_var="META_IMAGE_MODEL", config_key="meta-ai",
|
||||
explicit=caller_model,
|
||||
_MODELS, DEFAULT_MODEL, env_var="META_IMAGE_MODEL", config_key="meta-ai", explicit=caller_model,
|
||||
)
|
||||
|
||||
|
||||
@@ -111,7 +99,7 @@ class MetaImageGenProvider(ImageGenProvider):
|
||||
)
|
||||
|
||||
def capabilities(self) -> Dict[str, Any]:
|
||||
# Text-to-image only until image-to-image is verified against the Meta endpoint.
|
||||
# Text-to-image only until image-to-image is verified against Meta.
|
||||
return {"modalities": ["text"], "max_reference_images": 0}
|
||||
|
||||
def generate(
|
||||
@@ -125,10 +113,8 @@ class MetaImageGenProvider(ImageGenProvider):
|
||||
) -> Dict[str, Any]:
|
||||
prompt = (prompt or "").strip()
|
||||
aspect = resolve_aspect_ratio(aspect_ratio)
|
||||
|
||||
if not prompt:
|
||||
return prompt_required_error("meta-ai", aspect)
|
||||
|
||||
api_key = _resolve_api_key()
|
||||
if not api_key:
|
||||
return error_factory("meta-ai", aspect)(
|
||||
@@ -140,12 +126,10 @@ class MetaImageGenProvider(ImageGenProvider):
|
||||
openai, err = import_openai("meta-ai", aspect)
|
||||
if err:
|
||||
return err
|
||||
|
||||
model_id, _meta = _resolve_model(kwargs.get("model"))
|
||||
size = size_for(aspect)
|
||||
fail = error_factory("meta-ai", aspect, model=model_id, prompt=prompt)
|
||||
client = openai.OpenAI(api_key=api_key, base_url=_resolve_base_url())
|
||||
|
||||
try:
|
||||
response = client.images.generate(model=model_id, prompt=prompt, size=size, n=1)
|
||||
except Exception as exc:
|
||||
@@ -168,20 +152,12 @@ class MetaImageGenProvider(ImageGenProvider):
|
||||
return fail("Meta response contained neither b64_json nor URL", "empty_response")
|
||||
except Exception as exc:
|
||||
return fail(f"Failed to save Meta image: {exc}", "io_error")
|
||||
|
||||
extra: Dict[str, Any] = {"size": size}
|
||||
revised_prompt = getattr(first, "revised_prompt", None)
|
||||
if revised_prompt:
|
||||
extra["revised_prompt"] = revised_prompt
|
||||
|
||||
if getattr(first, "revised_prompt", None):
|
||||
extra["revised_prompt"] = first.revised_prompt
|
||||
return success_response(
|
||||
image=image_ref,
|
||||
model=model_id,
|
||||
prompt=prompt,
|
||||
aspect_ratio=aspect,
|
||||
provider="meta-ai",
|
||||
modality="text",
|
||||
extra=extra,
|
||||
image=image_ref, model=model_id, prompt=prompt, aspect_ratio=aspect, provider="meta-ai",
|
||||
modality="text", extra=extra,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -88,7 +88,9 @@ def _summarize_error_body(body: str) -> str:
|
||||
|
||||
|
||||
def _resolve_model() -> Tuple[str, Dict[str, Any]]:
|
||||
return resolve_static_model(_MODELS, DEFAULT_MODEL, env_var="OPENAI_IMAGE_MODEL", config_key="openai-codex")
|
||||
return resolve_static_model(
|
||||
_MODELS, DEFAULT_MODEL, env_var="OPENAI_IMAGE_MODEL", config_key="openai-codex"
|
||||
)
|
||||
|
||||
|
||||
def _read_codex_access_token() -> Optional[str]:
|
||||
@@ -306,7 +308,9 @@ def _collect_image_b64(
|
||||
"Authorization": f"Bearer {token}",
|
||||
"Content-Type": "application/json",
|
||||
})
|
||||
payload = _build_responses_payload(prompt=prompt, size=size, quality=quality, input_images=input_images)
|
||||
payload = _build_responses_payload(
|
||||
prompt=prompt, size=size, quality=quality, input_images=input_images,
|
||||
)
|
||||
timeout = httpx.Timeout(300.0, connect=30.0, read=300.0, write=30.0, pool=30.0)
|
||||
|
||||
final_b64: Optional[str] = None
|
||||
@@ -409,7 +413,8 @@ class OpenAICodexImageGenProvider(ImageGenProvider):
|
||||
collected: Optional[Dict[str, str]] = None
|
||||
for attempt in range(attempts):
|
||||
collected = _collect_image_b64(
|
||||
token, prompt=prompt, size=size, quality=meta["quality"], input_images=input_images or None,
|
||||
token, prompt=prompt, size=size, quality=meta["quality"],
|
||||
input_images=input_images or None,
|
||||
)
|
||||
if collected and collected.get("source") == "final" and collected.get("b64"):
|
||||
break
|
||||
@@ -420,7 +425,8 @@ class OpenAICodexImageGenProvider(ImageGenProvider):
|
||||
else "no image_generation_call result"
|
||||
)
|
||||
logger.warning(
|
||||
"Codex image stream ended with %s (attempt %s/%s); retrying once before failing closed.",
|
||||
"Codex image stream ended with %s (attempt %s/%s); "
|
||||
"retrying once before failing closed.",
|
||||
kind, attempt + 1, attempts,
|
||||
)
|
||||
except Exception as exc:
|
||||
@@ -459,8 +465,8 @@ class OpenAICodexImageGenProvider(ImageGenProvider):
|
||||
except Exception as exc:
|
||||
return fail(f"Could not save image to cache: {exc}", "io_error")
|
||||
return success_response(
|
||||
image=str(saved_path), model=tier_id, prompt=prompt, aspect_ratio=aspect, provider="openai-codex",
|
||||
modality="image" if input_images else "text",
|
||||
image=str(saved_path), model=tier_id, prompt=prompt, aspect_ratio=aspect,
|
||||
provider="openai-codex", modality="image" if input_images else "text",
|
||||
extra={
|
||||
"size": size,
|
||||
"quality": meta["quality"],
|
||||
|
||||
@@ -1,13 +1,7 @@
|
||||
"""OpenAI image generation backend.
|
||||
|
||||
Exposes OpenAI's ``gpt-image-2`` model at three quality tiers
|
||||
(``gpt-image-2-low`` ~15s, ``-medium`` ~40s default, ``-high`` ~2min) as
|
||||
virtual model ids so the picker and ``image_gen.model`` behave like any other
|
||||
multi-model backend. Output is base64 JSON → saved under
|
||||
``$HERMES_HOME/cache/images/``.
|
||||
|
||||
Selection precedence: ``OPENAI_IMAGE_MODEL`` env → ``image_gen.openai.model``
|
||||
→ ``image_gen.model`` (when it is one of our tier ids) → :data:`DEFAULT_MODEL`.
|
||||
"""OpenAI image generation backend: ``gpt-image-2`` at three quality tiers
|
||||
(``gpt-image-2-low/-medium/-high``) exposed as virtual model ids. Output is
|
||||
base64 → ``$HERMES_HOME/cache/images/``. Selection: ``OPENAI_IMAGE_MODEL`` →
|
||||
``image_gen.openai.model`` → ``image_gen.model`` → :data:`DEFAULT_MODEL`.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -45,10 +39,7 @@ _MODELS: Dict[str, Dict[str, Any]] = dict(GPT_IMAGE_2_TIERS)
|
||||
|
||||
|
||||
def _resolve_model() -> Tuple[str, Dict[str, Any]]:
|
||||
"""Decide which tier to use and return ``(model_id, meta)``."""
|
||||
return resolve_static_model(
|
||||
_MODELS, DEFAULT_MODEL, env_var="OPENAI_IMAGE_MODEL", config_key="openai"
|
||||
)
|
||||
return resolve_static_model(_MODELS, DEFAULT_MODEL, env_var="OPENAI_IMAGE_MODEL", config_key="openai")
|
||||
|
||||
|
||||
def _load_image_bytes(ref: str) -> Tuple[bytes, str]:
|
||||
@@ -66,9 +57,7 @@ def _load_image_bytes(ref: str) -> Tuple[bytes, str]:
|
||||
import base64
|
||||
|
||||
header, _, b64 = ref.partition(",")
|
||||
ext = "png"
|
||||
if "image/" in header:
|
||||
ext = header.split("image/", 1)[1].split(";", 1)[0] or "png"
|
||||
ext = (header.split("image/", 1)[1].split(";", 1)[0] if "image/" in header else "") or "png"
|
||||
return base64.b64decode(b64), f"image.{ext}"
|
||||
# Local file path — enforce the shared credential-read guard before reading.
|
||||
from agent.file_safety import raise_if_read_blocked
|
||||
@@ -122,10 +111,8 @@ class OpenAIImageGenProvider(ImageGenProvider):
|
||||
) -> Dict[str, Any]:
|
||||
prompt = (prompt or "").strip()
|
||||
aspect = resolve_aspect_ratio(aspect_ratio)
|
||||
|
||||
if not prompt:
|
||||
return prompt_required_error("openai", aspect)
|
||||
|
||||
api_key = get_secret("OPENAI_API_KEY")
|
||||
if not api_key:
|
||||
return error_factory("openai", aspect)(
|
||||
@@ -138,7 +125,6 @@ class OpenAIImageGenProvider(ImageGenProvider):
|
||||
openai, err = import_openai("openai", aspect)
|
||||
if err:
|
||||
return err
|
||||
|
||||
tier_id, meta = _resolve_model()
|
||||
size = size_for(aspect)
|
||||
sources = collect_source_images(image_url, reference_image_urls, limit=16)
|
||||
@@ -159,15 +145,11 @@ class OpenAIImageGenProvider(ImageGenProvider):
|
||||
files.append(bio)
|
||||
except Exception as exc:
|
||||
return fail(f"Could not load source image for editing: {exc}", "io_error")
|
||||
|
||||
try:
|
||||
response = client.images.edit(
|
||||
model=API_MODEL,
|
||||
image=files if len(files) > 1 else files[0],
|
||||
prompt=prompt,
|
||||
model=API_MODEL, image=files if len(files) > 1 else files[0], prompt=prompt,
|
||||
size=size, # type: ignore[arg-type] # OPENAI_SIZES values are valid gpt-image sizes
|
||||
quality=meta["quality"],
|
||||
n=1,
|
||||
quality=meta["quality"], n=1,
|
||||
)
|
||||
except Exception as exc:
|
||||
logger.debug("OpenAI image edit failed", exc_info=True)
|
||||
@@ -186,7 +168,6 @@ class OpenAIImageGenProvider(ImageGenProvider):
|
||||
data = getattr(response, "data", None) or []
|
||||
if not data:
|
||||
return fail("OpenAI returned no image data", "empty_response")
|
||||
|
||||
first = data[0]
|
||||
image_ref, err = materialize_image(
|
||||
getattr(first, "b64_json", None), getattr(first, "url", None),
|
||||
@@ -195,20 +176,12 @@ class OpenAIImageGenProvider(ImageGenProvider):
|
||||
)
|
||||
if err:
|
||||
return err
|
||||
|
||||
extra: Dict[str, Any] = {"size": size, "quality": meta["quality"]}
|
||||
revised_prompt = getattr(first, "revised_prompt", None)
|
||||
if revised_prompt:
|
||||
extra["revised_prompt"] = revised_prompt
|
||||
|
||||
if getattr(first, "revised_prompt", None):
|
||||
extra["revised_prompt"] = first.revised_prompt
|
||||
return success_response(
|
||||
image=image_ref,
|
||||
model=tier_id,
|
||||
prompt=prompt,
|
||||
aspect_ratio=aspect,
|
||||
provider="openai",
|
||||
modality="image" if is_edit else "text",
|
||||
extra=extra,
|
||||
image=image_ref, model=tier_id, prompt=prompt, aspect_ratio=aspect, provider="openai",
|
||||
modality="image" if is_edit else "text", extra=extra,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -258,6 +258,26 @@ def _access_error_hint(
|
||||
)
|
||||
|
||||
|
||||
def _get_catalog(base_url: str, path: str, api_key: str, timeout: Any) -> List[Tuple[str, Dict[str, Any]]]:
|
||||
"""``(model_id, entry)`` pairs from ``GET {base_url}{path}``'s ``data[]``; raises on HTTP failure."""
|
||||
import requests
|
||||
|
||||
response = requests.get(
|
||||
f"{base_url}{path}",
|
||||
headers={"Authorization": f"Bearer {api_key}"} if api_key else {},
|
||||
timeout=timeout,
|
||||
)
|
||||
response.raise_for_status()
|
||||
body = response.json()
|
||||
entries = body.get("data") if isinstance(body, dict) else None
|
||||
out: List[Tuple[str, Dict[str, Any]]] = []
|
||||
for entry in entries if isinstance(entries, list) else []:
|
||||
model_id = entry.get("id") if isinstance(entry, dict) else None
|
||||
if isinstance(model_id, str) and model_id.strip():
|
||||
out.append((model_id.strip(), entry))
|
||||
return out
|
||||
|
||||
|
||||
def _fetch_catalog(
|
||||
base_url: str, api_key: str, *, path: str, meta: Dict[str, Dict[str, Any]],
|
||||
generic: str, image_output_only: bool,
|
||||
@@ -268,20 +288,8 @@ def _fetch_catalog(
|
||||
includes ``image`` and drops router pseudo-models. Curated ``meta`` wins
|
||||
for known ids; unknown ones get the API name and ``generic`` strengths.
|
||||
"""
|
||||
import requests
|
||||
|
||||
response = requests.get(
|
||||
f"{base_url}{path}",
|
||||
headers={"Authorization": f"Bearer {api_key}"} if api_key else {},
|
||||
timeout=_LIVE_TIMEOUT,
|
||||
)
|
||||
response.raise_for_status()
|
||||
out: List[Dict[str, Any]] = []
|
||||
for entry in response.json().get("data") or []:
|
||||
model_id = entry.get("id") if isinstance(entry, dict) else None
|
||||
if not isinstance(model_id, str) or not model_id.strip():
|
||||
continue
|
||||
model_id = model_id.strip()
|
||||
for model_id, entry in _get_catalog(base_url, path, api_key, _LIVE_TIMEOUT):
|
||||
arch_raw = entry.get("architecture")
|
||||
arch: Dict[str, Any] = arch_raw if isinstance(arch_raw, dict) else {}
|
||||
if image_output_only and (
|
||||
@@ -311,25 +319,13 @@ def _fetch_image_api_catalog(base_url: str, api_key: str) -> frozenset:
|
||||
call to chat-completions — guessing the other way would send a
|
||||
chat-completions id to ``/images/generations`` and 404 a working setup.
|
||||
"""
|
||||
import requests
|
||||
|
||||
cached = _CATALOG_CACHE.get(base_url)
|
||||
if cached and (time.monotonic() - cached[0]) < _CATALOG_TTL_SECONDS:
|
||||
return cached[1]
|
||||
ids: set = set()
|
||||
try:
|
||||
response = requests.get(
|
||||
f"{base_url}/images/models",
|
||||
headers={"Authorization": f"Bearer {api_key}"},
|
||||
timeout=(_IMAGE_API_CONNECT_TIMEOUT, 30.0),
|
||||
)
|
||||
response.raise_for_status()
|
||||
body = response.json()
|
||||
entries = body.get("data") if isinstance(body, dict) else None
|
||||
for entry in entries if isinstance(entries, list) else []:
|
||||
model_id = entry.get("id") if isinstance(entry, dict) else None
|
||||
if isinstance(model_id, str) and model_id.strip():
|
||||
ids.add(model_id.strip())
|
||||
catalog = _get_catalog(base_url, "/images/models", api_key, (_IMAGE_API_CONNECT_TIMEOUT, 30.0))
|
||||
ids = {model_id for model_id, _entry in catalog}
|
||||
except Exception as exc: # noqa: BLE001 - probe must never break generation
|
||||
logger.debug("image API catalog probe failed for %s: %s", base_url, exc)
|
||||
resolved = frozenset(ids)
|
||||
@@ -550,7 +546,9 @@ def _image_api_extra(
|
||||
payload: Dict[str, Any], saved: List[str], usable_refs: List[str], notes: List[str], body: Any
|
||||
) -> Dict[str, Any]:
|
||||
"""Success ``extra`` for an Image API result (knobs sent, extra images, usage)."""
|
||||
extra: Dict[str, Any] = {"endpoint": "images/generations", "exact_aspect_ratio": payload.get("aspect_ratio")}
|
||||
extra: Dict[str, Any] = {
|
||||
"endpoint": "images/generations", "exact_aspect_ratio": payload.get("aspect_ratio"),
|
||||
}
|
||||
extra.update({key: payload[key] for key in _IMAGE_API_EXTRA_KEYS if key in payload})
|
||||
if len(saved) > 1:
|
||||
extra["additional_images"] = saved[1:]
|
||||
@@ -777,7 +775,9 @@ class OpenRouterCompatImageProvider(ImageGenProvider):
|
||||
if failure.kind != "http":
|
||||
return _fail(failure.error, failure.error_type, retryable=failure.kind == "timeout")
|
||||
status, message = failure.status, failure.message
|
||||
logger.error("%s image API generation failed (%s) on %s: %s", self._name, status, model_id, message)
|
||||
logger.error(
|
||||
"%s image API generation failed (%s) on %s: %s", self._name, status, model_id, message,
|
||||
)
|
||||
# 401/403 first so one account-level rejection does not get two
|
||||
# different error_types depending on where in the chain it landed.
|
||||
if status in (401, 403):
|
||||
@@ -794,7 +794,9 @@ class OpenRouterCompatImageProvider(ImageGenProvider):
|
||||
entries = body.get("data") if isinstance(body, dict) else None
|
||||
entries = [e for e in entries if isinstance(e, dict)] if isinstance(entries, list) else []
|
||||
if not entries:
|
||||
return _fail(f"{self._display} returned no image data for '{model_id}'.", "empty_response", retryable=True)
|
||||
return _fail(
|
||||
f"{self._display} returned no image data for '{model_id}'.", "empty_response", retryable=True,
|
||||
)
|
||||
prefix = f"{self._name}_{_model_slug(model_id)}"
|
||||
try:
|
||||
saved = [p for p in (_save_image_api_entry(e, prefix) for e in entries) if p]
|
||||
@@ -833,7 +835,9 @@ class OpenRouterCompatImageProvider(ImageGenProvider):
|
||||
hint = _access_error_hint(
|
||||
self._display, model_id, self._model_env_var, failure.status, failure.message
|
||||
)
|
||||
return fail(hint or failure.error, "model_access" if hint else "api_error"), "unavailable" if hint else None
|
||||
if hint:
|
||||
return fail(hint, "model_access"), "unavailable"
|
||||
return fail(failure.error, "api_error"), None
|
||||
return fail(failure.error, failure.error_type), "timed out" if failure.kind == "timeout" else None
|
||||
|
||||
images = _extract_images(result)
|
||||
@@ -846,7 +850,8 @@ class OpenRouterCompatImageProvider(ImageGenProvider):
|
||||
first = images[0]
|
||||
try:
|
||||
if first.startswith("data:"):
|
||||
saved_path = save_b64_image(first.split(",", 1)[1] if "," in first else "", prefix=f"{self._name}_gen")
|
||||
b64 = first.split(",", 1)[1] if "," in first else ""
|
||||
saved_path = save_b64_image(b64, prefix=f"{self._name}_gen")
|
||||
else:
|
||||
saved_path = save_url_image(first, prefix=f"{self._name}_gen")
|
||||
except Exception as exc: # noqa: BLE001
|
||||
@@ -901,7 +906,10 @@ class OpenRouterCompatImageProvider(ImageGenProvider):
|
||||
for i, model_id in enumerate(model_chain):
|
||||
# An Image API model cannot be served by /chat/completions (and vice
|
||||
# versa), so this is a routing decision, not a preference.
|
||||
if self._supports_image_api and _select_surface(model_id, base_url, api_key, self._config_key) == "images":
|
||||
surface = "chat"
|
||||
if self._supports_image_api:
|
||||
surface = _select_surface(model_id, base_url, api_key, self._config_key)
|
||||
if surface == "images":
|
||||
outcome = self._generate_via_image_api(
|
||||
model_id=model_id, prompt=prompt, semantic_aspect=aspect,
|
||||
references=references, base_url=base_url, headers=headers, kwargs=kwargs,
|
||||
|
||||
@@ -278,7 +278,8 @@ class XAIImageGenProvider(ImageGenProvider):
|
||||
for index, source in enumerate(source_images):
|
||||
if source.lower().startswith(_REMOTE_PREFIXES) or Path(source).expanduser().is_file():
|
||||
continue
|
||||
field = "image_url" if index == 0 and image_url and image_url.strip() == source else "reference_image_urls"
|
||||
is_primary = index == 0 and image_url and image_url.strip() == source
|
||||
field = "image_url" if is_primary else "reference_image_urls"
|
||||
return edit_fail(
|
||||
f"{field} must be a public HTTPS URL or data URI "
|
||||
"(e.g. the `image`/`public_url` from a prior Imagine result)",
|
||||
@@ -292,7 +293,9 @@ class XAIImageGenProvider(ImageGenProvider):
|
||||
"User-Agent": hermes_xai_user_agent(),
|
||||
}
|
||||
base_url = _base_url(creds)
|
||||
storage_options = build_xai_storage_options("image_gen", filename_prefix="hermes-xai-image", extension="png")
|
||||
storage_options = build_xai_storage_options(
|
||||
"image_gen", filename_prefix="hermes-xai-image", extension="png",
|
||||
)
|
||||
storage_notice = maybe_mark_xai_storage_notice_seen("image_gen")
|
||||
storage_cfg = read_xai_imagine_storage_config("image_gen")
|
||||
|
||||
@@ -319,7 +322,9 @@ class XAIImageGenProvider(ImageGenProvider):
|
||||
payload["storage_options"] = storage_options
|
||||
|
||||
fail = error_factory(provider_name, aspect, model=model_id, prompt=prompt)
|
||||
result, failure = post_json(endpoint_url, headers=headers, payload=payload, timeout=_REQUEST_TIMEOUT, label="xAI")
|
||||
result, failure = post_json(
|
||||
endpoint_url, headers=headers, payload=payload, timeout=_REQUEST_TIMEOUT, label="xAI",
|
||||
)
|
||||
if failure:
|
||||
if failure.kind == "http":
|
||||
logger.error("xAI image gen failed (%d): %s", failure.status, failure.message)
|
||||
|
||||
Reference in New Issue
Block a user