| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- """FAL.ai registration adapter. Catalog, payload, submission, managed-gateway selection and
- Clarity Upscaler chaining live in :mod:`tools.image_generation_tool`; this plugin imports it at
- call time so tests keep patching ``image_tool.*`` and there is one FAL code path."""
- from __future__ import annotations
- import json
- import logging
- from typing import Any, Dict, List, Optional
- from agent.image_gen_provider import DEFAULT_ASPECT_RATIO, resolve_aspect_ratio
- from plugins.image_gen._common import StaticImageGenProvider, catalog_rows
- logger = logging.getLogger(__name__)
- _PASSTHROUGH_KWARGS = (
- "num_inference_steps", "guidance_scale", "num_images", "output_format", "seed", "upscale",
- )
- class FalImageGenProvider(StaticImageGenProvider):
- """FAL.ai backend delegating to ``tools.image_generation_tool`` at call time."""
- provider_id = "fal"
- label = "FAL.ai"
- setup = dict(
- name="FAL.ai", badge="paid",
- tag="Pick from flux-2-klein, flux-2-pro, gpt-image, nano-banana-2, nano-banana-pro, etc. — text-to-image & image editing",
- key="FAL_KEY", prompt="FAL API key", url="https://fal.ai/dashboard/keys")
- def is_available(self) -> bool:
- # 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:
- return False
- def list_models(self) -> list[dict[str, Any]]:
- from tools.image_generation_catalog import FAL_MODELS
- return catalog_rows(FAL_MODELS)
- def default_model(self) -> Optional[str]:
- from tools.image_generation_catalog import DEFAULT_MODEL
- return DEFAULT_MODEL
- def capabilities(self) -> dict[str, Any]:
- # Image-to-image depends on the selected model (``edit_endpoint``); upscale works for any.
- import tools.image_generation_tool as _it
- try:
- _model_id, meta = _it._resolve_fal_model()
- except Exception:
- return {"modalities": ["text"], "max_reference_images": 0}
- if meta.get("edit_endpoint"):
- return {
- "modalities": ["text", "image"],
- "max_reference_images": int(meta.get("max_reference_images") or 1),
- "supports_upscale": True,
- }
- return {"modalities": ["text"], "max_reference_images": 0, "supports_upscale": True}
- def generate(
- self, prompt: str, aspect_ratio: str = DEFAULT_ASPECT_RATIO, *,
- image_url: Optional[str] = None, reference_image_urls: Optional[list[str]] = None,
- **kwargs: Any,
- ) -> dict[str, Any]:
- """Forward to ``image_generate_tool`` and reshape its JSON-string response into the ABC dict."""
- import tools.image_generation_tool as _it
- aspect = resolve_aspect_ratio(aspect_ratio)
- 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:
- passthrough["reference_image_urls"] = reference_image_urls
- try:
- raw = _it.image_generate_tool(prompt=prompt, aspect_ratio=aspect, **passthrough)
- except Exception as exc:
- 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,
- }
- try:
- response = json.loads(raw) if isinstance(raw, str) else raw
- except Exception:
- response = {"success": False, "image": None, "error": "Invalid JSON from FAL pipeline"}
- if not isinstance(response, dict):
- response = {
- "success": False, "image": None, "error": "FAL pipeline returned a non-dict response",
- "error_type": "provider_contract",
- }
- # Stamp the uniform shape; the legacy pipeline resolves the model internally.
- response.setdefault("provider", "fal")
- response.setdefault("prompt", prompt)
- response.setdefault("aspect_ratio", aspect)
- if "model" not in response:
- try:
- response["model"] = _it._resolve_fal_model()[0]
- except Exception:
- pass
- return response
- def register(ctx) -> None:
- """Plugin entry point — wire ``FalImageGenProvider`` into the registry."""
- ctx.register_image_gen_provider(FalImageGenProvider())
|