__init__.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. """FAL.ai registration adapter. Catalog, payload, submission, managed-gateway selection and
  2. Clarity Upscaler chaining live in :mod:`tools.image_generation_tool`; this plugin imports it at
  3. call time so tests keep patching ``image_tool.*`` and there is one FAL code path."""
  4. from __future__ import annotations
  5. import json
  6. import logging
  7. from typing import Any, Dict, List, Optional
  8. from agent.image_gen_provider import DEFAULT_ASPECT_RATIO, resolve_aspect_ratio
  9. from plugins.image_gen._common import StaticImageGenProvider, catalog_rows
  10. logger = logging.getLogger(__name__)
  11. _PASSTHROUGH_KWARGS = (
  12. "num_inference_steps", "guidance_scale", "num_images", "output_format", "seed", "upscale",
  13. )
  14. class FalImageGenProvider(StaticImageGenProvider):
  15. """FAL.ai backend delegating to ``tools.image_generation_tool`` at call time."""
  16. provider_id = "fal"
  17. label = "FAL.ai"
  18. setup = dict(
  19. name="FAL.ai", badge="paid",
  20. tag="Pick from flux-2-klein, flux-2-pro, gpt-image, nano-banana-2, nano-banana-pro, etc. — text-to-image & image editing",
  21. key="FAL_KEY", prompt="FAL API key", url="https://fal.ai/dashboard/keys")
  22. def is_available(self) -> bool:
  23. # Direct FAL_KEY or a managed Nous fal-queue origin, per the legacy module.
  24. import tools.image_generation_tool as _it
  25. try:
  26. return bool(_it.check_fal_api_key())
  27. except Exception:
  28. return False
  29. def list_models(self) -> list[dict[str, Any]]:
  30. from tools.image_generation_catalog import FAL_MODELS
  31. return catalog_rows(FAL_MODELS)
  32. def default_model(self) -> Optional[str]:
  33. from tools.image_generation_catalog import DEFAULT_MODEL
  34. return DEFAULT_MODEL
  35. def capabilities(self) -> dict[str, Any]:
  36. # Image-to-image depends on the selected model (``edit_endpoint``); upscale works for any.
  37. import tools.image_generation_tool as _it
  38. try:
  39. _model_id, meta = _it._resolve_fal_model()
  40. except Exception:
  41. return {"modalities": ["text"], "max_reference_images": 0}
  42. if meta.get("edit_endpoint"):
  43. return {
  44. "modalities": ["text", "image"],
  45. "max_reference_images": int(meta.get("max_reference_images") or 1),
  46. "supports_upscale": True,
  47. }
  48. return {"modalities": ["text"], "max_reference_images": 0, "supports_upscale": True}
  49. def generate(
  50. self, prompt: str, aspect_ratio: str = DEFAULT_ASPECT_RATIO, *,
  51. image_url: Optional[str] = None, reference_image_urls: Optional[list[str]] = None,
  52. **kwargs: Any,
  53. ) -> dict[str, Any]:
  54. """Forward to ``image_generate_tool`` and reshape its JSON-string response into the ABC dict."""
  55. import tools.image_generation_tool as _it
  56. aspect = resolve_aspect_ratio(aspect_ratio)
  57. passthrough = {key: kwargs[key] for key in _PASSTHROUGH_KWARGS if kwargs.get(key) is not None}
  58. # Only forward image-to-image inputs when supplied (no noisy None kwargs).
  59. if image_url is not None:
  60. passthrough["image_url"] = image_url
  61. if reference_image_urls is not None:
  62. passthrough["reference_image_urls"] = reference_image_urls
  63. try:
  64. raw = _it.image_generate_tool(prompt=prompt, aspect_ratio=aspect, **passthrough)
  65. except Exception as exc:
  66. logger.warning("FAL image_generate_tool raised: %s", exc, exc_info=True)
  67. return {
  68. "success": False, "image": None, "error": f"FAL image generation failed: {exc}",
  69. "error_type": type(exc).__name__, "provider": "fal", "prompt": prompt, "aspect_ratio": aspect,
  70. }
  71. try:
  72. response = json.loads(raw) if isinstance(raw, str) else raw
  73. except Exception:
  74. response = {"success": False, "image": None, "error": "Invalid JSON from FAL pipeline"}
  75. if not isinstance(response, dict):
  76. response = {
  77. "success": False, "image": None, "error": "FAL pipeline returned a non-dict response",
  78. "error_type": "provider_contract",
  79. }
  80. # Stamp the uniform shape; the legacy pipeline resolves the model internally.
  81. response.setdefault("provider", "fal")
  82. response.setdefault("prompt", prompt)
  83. response.setdefault("aspect_ratio", aspect)
  84. if "model" not in response:
  85. try:
  86. response["model"] = _it._resolve_fal_model()[0]
  87. except Exception:
  88. pass
  89. return response
  90. def register(ctx) -> None:
  91. """Plugin entry point — wire ``FalImageGenProvider`` into the registry."""
  92. ctx.register_image_gen_provider(FalImageGenProvider())