Skip to content

The agent that grows with you

refactor(image_gen): managed_route() owns stored-value normalisation; unreadable Krea refs return an error

kshitijk4poor62c42e9db9cd
diff --git a/hermes_cli/nous_subscription.py b/hermes_cli/nous_subscription.py
index 112ac73b113..6fe87cd3d47 100644
--- a/hermes_cli/nous_subscription.py
+++ b/hermes_cli/nous_subscription.py
@@ -292,10 +292,8 @@ def managed_image_partner(config: Dict[str, object]) -> Optional[str]:
from tools.image_generation_managed import FAL, KREA, PORTAL, managed_route
section = _section(config, "image_gen")
- provider, model = (
- value.strip() if isinstance(value, str) and value.strip() else None
- for value in (section.get("provider"), section.get("model")))
- return {FAL: "FAL", KREA: "Krea", PORTAL: "Nous Portal"}.get(managed_route(provider, model))
+ return {FAL: "FAL", KREA: "Krea", PORTAL: "Nous Portal"}.get(
+ managed_route(section.get("provider"), section.get("model")))
def _fal_feature(key: str, tool_enabled: bool, direct: bool, managed: bool, selected: Optional[str]) -> NousFeatureState:
diff --git a/plugins/image_gen/krea/__init__.py b/plugins/image_gen/krea/__init__.py
index ee0d60cd77a..2118d6c985b 100644
--- a/plugins/image_gen/krea/__init__.py
+++ b/plugins/image_gen/krea/__init__.py
@@ -337,8 +337,11 @@ def _inline_local_style_refs(
if not path.is_file():
return [], fail(f"Style 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.
- with path.open("rb") as fh:
- data = fh.read(_MAX_LOCAL_REFERENCE_BYTES - total_bytes + 1)
+ 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")
total_bytes += len(data)
if total_bytes > _MAX_LOCAL_REFERENCE_BYTES:
return [], fail(
diff --git a/tests/hermes_cli/test_nous_subscription.py b/tests/hermes_cli/test_nous_subscription.py
index ecf586ece0e..605132fb731 100644
--- a/tests/hermes_cli/test_nous_subscription.py
+++ b/tests/hermes_cli/test_nous_subscription.py
@@ -216,7 +216,7 @@ def test_logged_in_entitled_account_yields_a_state_for_every_feature(monkeypatch
({"model": FAL_DEFAULT_MODEL}, "FAL"),
({"provider": "nous", "model": "openai/gpt-image-2"}, "Nous Portal"),
({"model": "openai/gpt-image-2"}, "FAL"),
- ({"use_gateway": True, "model": "openai/gpt-image-2"}, "FAL"), # the dispatcher ignores legacy use_gateway
+ ({"use_gateway": True, "model": "openai/gpt-image-2"}, "FAL"), # managed-model routing ignores legacy use_gateway
({"provider": "openai", "model": "gpt-image-2"}, None),
],
)
diff --git a/tools/image_generation_managed.py b/tools/image_generation_managed.py
index 28aebf73a4d..118cda40242 100644
--- a/tools/image_generation_managed.py
+++ b/tools/image_generation_managed.py
@@ -17,6 +17,8 @@ from __future__ import annotations
from typing import Any, Dict, Optional, Tuple
+from tools.tool_backend_helpers import NOUS_MANAGED_PROVIDER
+
FAL, KREA, PORTAL = "fal", "krea", "portal"
# Portal ids that are the same model as a FAL or Krea catalog entry. FAL wins (it is the
@@ -47,13 +49,13 @@ def managed_backend_for_model(model_id: Optional[str]) -> str:
return PORTAL
-def managed_route(provider: Optional[str], model_id: Optional[str]) -> Optional[str]:
- """Gateway a request is dispatched to for the stored ``(image_gen.provider, image_gen.model)``.
+def managed_route(provider: Any, model_id: Any) -> Optional[str]:
+ """Gateway a request is dispatched to for the stored ``image_gen.provider`` / ``image_gen.model``
+ (raw config values; blank or non-string reads as unset).
``None`` when a direct/BYO provider owns the request. A Portal id reaches the Portal only under
an explicit ``nous`` pick; with the provider unset it stays on the in-tree FAL path."""
- from tools.tool_backend_helpers import NOUS_MANAGED_PROVIDER
-
+ provider, model_id = (v.strip() if isinstance(v, str) and v.strip() else None for v in (provider, model_id))
if provider is not None and provider != NOUS_MANAGED_PROVIDER:
return None
backend = managed_backend_for_model(model_id)
diff --git a/tools/image_generation_tool.py b/tools/image_generation_tool.py
index 61218577b46..431aa6d38a8 100644
--- a/tools/image_generation_tool.py
+++ b/tools/image_generation_tool.py
@@ -688,12 +688,8 @@ def _normalize_krea_model(model_id: Optional[str]) -> Optional[str]:
def _managed_model_plugin() -> Optional[tuple]:
- """``(plugin_name, model_id)`` when the managed selection stores a Krea or Portal model, else ``None``.
-
- The managed row writes ``provider: nous`` for three gateways; the model id says which. FAL
- models (and an unset model) return ``None`` so the in-tree FAL path handles them. Only the
- ``nous``/unset selection qualifies — a direct/BYO provider pick dispatches normally.
- """
+ """``(plugin_name, model_id)`` when the stored selection routes to the Krea or Portal gateway
+ (rule: :func:`tools.image_generation_managed.managed_route`), else ``None`` for the FAL path."""
from tools.image_generation_managed import KREA, PORTAL, managed_route
model_id = _read_configured_image_model()