Files
9router/open-sse/handlers/imageProviders/falAi.js
T
decoluaandCursor e53ce79abb refactor(open-sse): B1 consolidate media endpoint URLs into registry
- image/embed/tts/search base URLs derive from media.*Config.baseUrl /
  searchViaChat.endpoint (single source); handlers read PROVIDER_MEDIA.
- ~18 hardcoded endpoints moved: bfl/fal/stability/runway/hf/gemini/
  cloudflare/recraft/sdwebui/comfyui/nanobanana image, voyage embed,
  gemini/openrouter tts, chatSearch endpoints.
- Byte-identical: PROVIDERS 62 + alias 90 baselines, image buildUrl outputs.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-14 13:58:12 +07:00

43 lines
1.6 KiB
JavaScript

// Fal.ai — async submit + queue polling
import { sleep, nowSec, sizeToAspectRatio, POLL_INTERVAL_MS, POLL_TIMEOUT_MS } from "./_base.js";
import { PROVIDER_MEDIA } from "../../providers/index.js";
const BASE_URL = PROVIDER_MEDIA["fal-ai"]?.imageConfig?.baseUrl;
export default {
async: true,
buildUrl: (model) => `${BASE_URL}/${model}`,
buildHeaders: (creds) => {
const key = creds?.apiKey || creds?.accessToken;
return { "Content-Type": "application/json", "Authorization": `Key ${key}` };
},
buildBody: (_model, body) => {
const req = { prompt: body.prompt, num_images: body.n || 1 };
if (body.size) req.image_size = sizeToAspectRatio(body.size);
if (body.image) req.image_url = body.image;
return req;
},
async parseResponse(response, { headers }) {
const { status_url, response_url } = await response.json();
const deadline = Date.now() + POLL_TIMEOUT_MS;
while (Date.now() < deadline) {
await sleep(POLL_INTERVAL_MS);
const r = await fetch(status_url, { headers });
if (!r.ok) throw new Error(`Fal status ${r.status}`);
const s = await r.json();
if (s.status === "COMPLETED") {
const fr = await fetch(response_url, { headers });
return await fr.json();
}
if (s.status === "FAILED") throw new Error(s.error || "Fal generation failed");
}
throw new Error("Fal polling timeout");
},
normalize: (responseBody) => {
const images = Array.isArray(responseBody.images)
? responseBody.images
: (responseBody.image ? [responseBody.image] : []);
return { created: nowSec(), data: images.map((img) => ({ url: img.url || img })) };
},
};