Skip to content

The agent that grows with you

refactor(image_gen): one managed_route() decides the managed image gateway for both dispatch and the portal label

kshitijk4poor9fb4074fd855
diff --git a/hermes_cli/nous_subscription.py b/hermes_cli/nous_subscription.py
index 0724c801b59..112ac73b113 100644
--- a/hermes_cli/nous_subscription.py
+++ b/hermes_cli/nous_subscription.py
@@ -286,19 +286,16 @@ def _web_feature(web_cfg: Dict[str, object], tool_enabled: bool, managed: bool,
def managed_image_partner(config: Dict[str, object]) -> Optional[str]:
- """Partner that serves the managed image selection (``"FAL"``, ``"Krea"`` or ``"Nous Portal"``),
- decided by the stored ``image_gen.model``; ``None`` when a direct vendor is selected, since the
- stored model then belongs to that vendor, not to a gateway."""
- from tools.image_generation_managed import FAL, KREA, PORTAL, managed_backend_for_model
+ """Partner the image request is dispatched to (``"FAL"``, ``"Krea"`` or ``"Nous Portal"``);
+ ``None`` when a direct vendor owns it. Reads the stored values the way the runtime dispatcher
+ does, so the label and the route cannot disagree."""
+ from tools.image_generation_managed import FAL, KREA, PORTAL, managed_route
section = _section(config, "image_gen")
- selected = _selected_provider(section, "provider")
- if selected not in (None, "nous"):
- return None
- backend = managed_backend_for_model(section.get("model"))
- if backend == PORTAL and selected != "nous":
- backend = FAL # an unset pick with a Portal id runs in-tree FAL (see _managed_model_plugin)
- return {FAL: "FAL", KREA: "Krea", PORTAL: "Nous Portal"}[backend]
+ 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))
def _fal_feature(key: str, tool_enabled: bool, direct: bool, managed: bool, selected: Optional[str]) -> NousFeatureState:
diff --git a/tests/hermes_cli/test_nous_subscription.py b/tests/hermes_cli/test_nous_subscription.py
index 4599ea2b62c..ecf586ece0e 100644
--- a/tests/hermes_cli/test_nous_subscription.py
+++ b/tests/hermes_cli/test_nous_subscription.py
@@ -216,12 +216,14 @@ 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
({"provider": "openai", "model": "gpt-image-2"}, None),
],
)
def test_managed_image_partner_follows_the_stored_model(image_cfg, partner):
- """The partner is the gateway the runtime actually dispatches to: the stored model decides under
- the managed pick (Portal ids only with an explicit ``nous``); a direct vendor owns its model id."""
+ """The partner is the gateway the runtime dispatcher routes to (tools.image_generation_managed.
+ managed_route): the stored model decides under the managed pick, Portal ids only with an
+ explicit ``nous``; a direct vendor owns its model id."""
assert ns.managed_image_partner({"image_gen": image_cfg}) == partner
diff --git a/tools/image_generation_managed.py b/tools/image_generation_managed.py
index cdf36135cfc..28aebf73a4d 100644
--- a/tools/image_generation_managed.py
+++ b/tools/image_generation_managed.py
@@ -47,6 +47,21 @@ 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)``.
+
+ ``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
+
+ if provider is not None and provider != NOUS_MANAGED_PROVIDER:
+ return None
+ backend = managed_backend_for_model(model_id)
+ if backend == PORTAL and provider != NOUS_MANAGED_PROVIDER:
+ return FAL
+ return backend
+
+
def _plugin_rows(name: str) -> list:
"""``list_models()`` of a registered image gen plugin; ``[]`` when unavailable."""
from tools.image_generation_tool import _get_plugin_provider
diff --git a/tools/image_generation_tool.py b/tools/image_generation_tool.py
index 6da5f1e1eeb..61218577b46 100644
--- a/tools/image_generation_tool.py
+++ b/tools/image_generation_tool.py
@@ -694,18 +694,11 @@ def _managed_model_plugin() -> Optional[tuple]:
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.
"""
- from tools.image_generation_managed import KREA, PORTAL, managed_backend_for_model
+ from tools.image_generation_managed import KREA, PORTAL, managed_route
- configured_provider = _read_configured_image_provider()
- if configured_provider is not None and configured_provider != NOUS_MANAGED_PROVIDER:
- return None
model_id = _read_configured_image_model()
- backend = managed_backend_for_model(model_id)
- if backend == KREA:
- return "krea", model_id
- if backend == PORTAL and configured_provider == NOUS_MANAGED_PROVIDER:
- return "nous", model_id
- return None
+ return {KREA: ("krea", model_id), PORTAL: ("nous", model_id)}.get(
+ managed_route(_read_configured_image_provider(), model_id))
def _maybe_route_managed_model(