image_generation_managed.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. """Managed ("Nous Subscription") image generation: one picker row, one model catalog.
  2. Three gateways sit behind the single stored selection ``image_gen.provider: nous``; the stored
  3. ``image_gen.model`` decides which one serves a request:
  4. * ``fal`` — the FAL managed gateway, every id in the in-tree ``FAL_MODELS`` catalog;
  5. * ``krea`` — the Krea managed gateway, the ``plugins/image_gen/krea`` model ids;
  6. * ``portal`` — Nous Portal chat-completions image models (``plugins/image_gen/openrouter``'s
  7. ``nous`` provider), anything else.
  8. Before this module the second and third gateways each had their own picker row that also
  9. wrote ``provider: nous`` — every managed row read "active" at once and picking the Portal row
  10. silently generated on FAL.
  11. """
  12. from __future__ import annotations
  13. from typing import Any, Dict, Optional, Tuple
  14. from tools.tool_backend_helpers import NOUS_MANAGED_PROVIDER
  15. FAL, KREA, PORTAL = "fal", "krea", "portal"
  16. # Portal ids that are the same model as a FAL or Krea catalog entry. FAL wins (it is the
  17. # gateway the free tool pool funds); Krea 2 goes through its native gateway, never FAL or the
  18. # Portal. Only cross-id duplicates live here — identical ids collapse by key.
  19. _PORTAL_DUPLICATE_OF = {
  20. "openai/gpt-image-2": "fal-ai/gpt-image-2",
  21. "google/gemini-3-pro-image": "fal-ai/nano-banana-pro",
  22. "google/gemini-3.1-flash-image": "fal-ai/nano-banana-2",
  23. "google/gemini-3.1-flash-lite-image": "google/nano-banana-2-lite",
  24. "krea/krea-2-medium": "krea-2-medium",
  25. "krea/krea-2-medium-turbo": "krea-2-medium-turbo",
  26. "krea/krea-2-large": "krea-2-large",
  27. "krea/krea-3": "krea-3",
  28. }
  29. _FAL_KREA_PREFIX = "fal-ai/krea/"
  30. def managed_backend_for_model(model_id: Optional[str]) -> str:
  31. """Gateway that serves ``model_id`` under the managed selection (unset → FAL default)."""
  32. from plugins.image_gen.krea import KREA_MODEL_IDS
  33. from tools.image_generation_catalog import FAL_MODELS
  34. candidate = model_id.strip() if isinstance(model_id, str) else ""
  35. if not candidate or candidate in FAL_MODELS:
  36. return FAL
  37. if candidate in KREA_MODEL_IDS:
  38. return KREA
  39. return PORTAL
  40. def managed_route(provider: Any, model_id: Any) -> Optional[str]:
  41. """Gateway a request is dispatched to for the stored ``image_gen.provider`` / ``image_gen.model``
  42. (raw config values; blank or non-string reads as unset).
  43. ``None`` when a direct/BYO provider owns the request. A Portal id reaches the Portal only under
  44. an explicit ``nous`` pick; with the provider unset it stays on the in-tree FAL path."""
  45. provider, model_id = (v.strip() if isinstance(v, str) and v.strip() else None for v in (provider, model_id))
  46. if provider is not None and provider != NOUS_MANAGED_PROVIDER:
  47. return None
  48. backend = managed_backend_for_model(model_id)
  49. if backend == PORTAL and provider != NOUS_MANAGED_PROVIDER:
  50. return FAL
  51. return backend
  52. def _plugin_rows(name: str) -> list:
  53. """``list_models()`` of a registered image gen plugin; ``[]`` when unavailable."""
  54. from tools.image_generation_tool import _get_plugin_provider
  55. try:
  56. provider = _get_plugin_provider(name)
  57. return list(provider.list_models() or []) if provider is not None else []
  58. except Exception:
  59. return []
  60. def managed_image_catalog(
  61. *, include_krea: bool = True, include_portal: bool = True,
  62. ) -> tuple[dict[str, dict[str, Any]], str]:
  63. """``({model_id: metadata}, default_model)`` for the managed row's model picker.
  64. FAL catalog first (minus the Krea-on-FAL entries), then native Krea, then Portal models
  65. that are not another entry in disguise. Every row carries ``backend`` so callers can hide
  66. the gateways an account is not entitled to.
  67. """
  68. from tools.image_generation_catalog import DEFAULT_MODEL, FAL_MODELS
  69. catalog: dict[str, dict[str, Any]] = {
  70. mid: {**meta, "backend": FAL} for mid, meta in FAL_MODELS.items()
  71. if not mid.startswith(_FAL_KREA_PREFIX)}
  72. if include_krea:
  73. for row in _plugin_rows("krea"):
  74. catalog[row["id"]] = {**row, "backend": KREA}
  75. if include_portal:
  76. for row in _plugin_rows("nous"):
  77. mid = row["id"]
  78. if mid in catalog or _PORTAL_DUPLICATE_OF.get(mid) in catalog:
  79. continue
  80. catalog[mid] = {**row, "backend": PORTAL}
  81. return catalog, DEFAULT_MODEL