Files
craft-ai-integrations/docs
Tex Jerniganandscratch 323b2a3d9f Generative fill: FLUX.1 Fill and Gemini for image.inpaint (#120)
* feat(ai-bfl): FLUX.1 Fill [pro] for image.inpaint (Generative fill)

Adds the flux-pro-1.0-fill model to craft-ai-bfl for image.inpaint with
source, mask and prompt, and lists BFL as a hosted route for the
generative-fill tool.

- Mask: white (or, for a mask with transparency, transparent) marks the
  area to fill; BFL is always sent a binary 8-bit grayscale PNG, white =
  fill, as its API documents. An empty mask, a non-PNG mask or a mask that
  differs in size from a PNG source is refused before sending. Our own
  bound: 8,192 px a side, 32 MP (BFL documents none).
- Options: steps (15-50), guidance (1.5-100), seed, safety_tolerance
  (0-6), prompt_upsampling, output_format (png/jpeg/webp; png default),
  range-checked; anything else is Unsupported.
- Result: downloaded as soon as the task is Ready (BFL links last ten
  minutes), only from https *.bfl.ai, without the key, no redirects,
  bounded to 32 MiB, type taken from the bytes, returned inline. A dropped
  download is a Network error the next poll retries; an expired link, a
  non-BFL host or a non-image fails the job.
- Errors: 429 now carries Retry-After; 402 is Quota ("out of credits");
  401/403 Auth; moderation statuses fail the job with "content moderated";
  "No Card Generated" is a failure instead of an unknown status.
- Regional addresses api.eu.bfl.ai and api.us.bfl.ai are accepted.

FLUX.1 Expand is not mapped: it takes per-side pixel counts that
image.outpaint does not carry.

Tested against scripted servers only (conformance lifecycle, cancel,
unknown job and reject checks included); no real BFL call was made.

* fix(ai-bfl): poll BFL's polling_url and stop waiting on a lost task

BFL's docs say a task submitted to the global api.bfl.ai must be read at
the polling_url from the submit reply, because the global address routes
to several clusters. Reading {base}/get_result instead could answer
"Task not found" forever, which was treated as pending.

- Every BFL job (FLUX 3 and FLUX.1 Fill) is polled at polling_url when it
  is https on bfl.ai or a subdomain, default port, no user-info; the key
  goes nowhere else and redirects are still not followed. Otherwise the
  configured {base}/get_result?id= is used. A loopback API (tests) only
  accepts its own origin.
- "Task not found" (status, or a 404 from get_result) fails the job with a
  Provider error once it has lasted 10 polls in a row and 60 seconds
  (BflConfig::not_found_grace). Any other answer resets the count.

Scripted-server tests cover the polling_url being used, foreign and plain
http polling URLs ignored without the key reaching them, and the
not-found timeout and reset.

* feat(ai-gemini): Gemini API image models for image.inpaint (Generative fill)

Adds craft-ai-gemini: Google's Gemini API (generativelanguage.googleapis.com,
an AI Studio key; not Vertex) through the Interactions endpoint, serving
image.inpaint with source, mask and prompt. Registered in the factory as
"gemini", with a "Google Gemini" preset and a hosted route for the
generative-fill tool.

- Models: Nano Banana 2.1 (gemini-nano-banana-2.1, default), Pro, 2 and 2
  Lite; any well-formed gemini-*image* id is also accepted at submit.
- Mask: Gemini takes none. The craft mask (white, or transparent when it
  has transparency, = fill) is sent as a second picture, an opaque binary
  grayscale PNG, white = fill, with an instruction explaining it and saying
  the area may already be blurred out as a placeholder. Prompt-guided
  editing: the model may change pixels outside the mask, so the caller
  keeps only the masked part. An empty prompt asks for a removal fill.
- Output: JPEG (the only format the endpoint returns), nearest supported
  aspect ratio, 1K/2K/4K from the picture's size or the image_size option;
  Lite stays at 1K. Anything else in params is Unsupported.
- Errors: 401/403 and a 400 about the API key are Auth, 402 Quota, 429
  RateLimited with Retry-After, Google's message kept (key scrubbed, cut to
  300 chars). A text-only answer fails the job with the model's words.
- Key in x-goog-api-key only, official address or loopback only, no
  redirects, replies bounded to 48 MiB. Testing a connection lists models
  (free). Each job runs on its own thread so it can be polled and
  cancelled; a cancelled request may still be billed.

Tested against scripted servers only (conformance lifecycle, cancel,
unknown job and reject checks included); no real Gemini call was made.

* feat(ai-client): Plan::provider, for adapter-specific params

* refactor(ai-media): one mask reader for the inpainting adapters

FLUX Fill and Gemini fill each carried the same craft-mask reader, size
check and grayscale PNG writer (SonarCloud: 9.1% duplication on new
code). They now share craft_ai_media::mask (Bounds, inline, png_size,
binary, encode_gray); each adapter keeps only its own limits and error
wording. Behaviour is unchanged and the existing mask tests pass as they
were.

* refactor(ai-media): Bounds names its service and checks sizes

The size check, its error and the input lookup were still the same few
lines in FLUX Fill and Gemini fill. Bounds now carries the service name
and a check() with the error a person reads, and mask::inline takes the
Bounds, so each adapter keeps only its limits.

* fix(ai-gemini): retain terminal jobs until their state is delivered

* fix(ai-features): fill notes say what was run live; Gemini's warning in its preset

The OpenAI and FLUX.1 Fill options said "Built and tested offline only",
but both have been used live from PhotoCraft. Gemini's warning that it
may change the picture outside the selection lived only in the cloud
option's note, which setup and the picker don't show; its preset note
now says it too.

* fix(ai-gemini): hold a permit for the life of each worker thread

MAX_RUNNING counted jobs that were not terminal, but cancel makes a job terminal while its request keeps running. Repeating submit and cancel could put more than 32 requests in flight, for up to the request timeout each.

submit now takes a permit before it accepts a job, and the worker thread holds it until it exits. A worker that never starts gives it back too. No live call was made: the regression test runs against a local listener.

---------

Co-authored-by: scratch <s@s>
2026-11-06 21:03:24 +00:00
..