Unrestricted Open-source alternative to AI video platforms — Free AI image & video generation studio with 600+ models (Flux, Midjourney, Kling, Sora, Veo). No content filters. Self-hosted, MIT licensed.
JavaScript ai-art-generatorai-image-generationai-video-generationfal-ai-alternativegenerative-aiimage-to-videoinvideo-alternativekling-ailipsynclitellm-alternativemidjourney-alternativemuapiopen-sourceopenrouter-alternativeseedance2sora-alternativetext-to-imagetext-to-videouncensoredwan-video
const SUCCESS_STATUSES = new Set(["completed", "succeeded", "success"]);
const FAILURE_STATUSES = new Set(["failed", "error", "cancelled", "canceled"]);
const wait = (milliseconds) => new Promise((resolve) => setTimeout(resolve, milliseconds));
function getGenerationErrorDetail(result) {
if (typeof result?.error === "string") return result.error;
if (typeof result?.error?.message === "string") return result.error.message;
if (typeof result?.message === "string") return result.message;
return "Unknown error";
}
function createGenerationError(result, requestId) {
const error = new Error(`Generation failed: ${getGenerationErrorDetail(result)}`);
error.requestId = requestId;
error.generationResult = result;
return error;
}
export function appendGenerationRefundNotice(message, error) {
const cost = error?.generationResult?.cost;
if (cost?.refunded !== true) return message;
const credits = cost.amount_credits;
const notice = Number.isFinite(credits)
? `Refunded ${credits} credit${credits === 1 ? "" : "s"}.`
: "The generation cost was refunded.";
return `${message} ${notice}`;
}
export async function pollForGenerationResult({
baseUrl,
requestId,
apiKey,
maxAttempts = 900,
interval = 2000,
onAuthRequired,
fetchImpl = fetch,
}) {
const pollUrl = `${baseUrl}/api/v1/predictions/${requestId}/result`;
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
await wait(interval);
let response;
try {
response = await fetchImpl(pollUrl, {
headers: { "Content-Type": "application/json", "x-api-key": apiKey },
});
} catch (error) {
if (attempt === maxAttempts) throw error;
continue;
}
if (!response.ok) {
const detail = await response.text();
const error = new Error(`Poll Failed: ${response.status} - ${detail.slice(0, 100)}`);
error.requestId = requestId;
if (response.status >= 500 && attempt < maxAttempts) continue;
onAuthRequired?.(response.status, detail);
throw error;
}
let result;
try {
result = await response.json();
} catch (error) {
if (attempt === maxAttempts) throw error;
continue;
}
const status = result.status?.toLowerCase();
if (SUCCESS_STATUSES.has(status)) return result;
if (FAILURE_STATUSES.has(status)) throw createGenerationError(result, requestId);
}
const error = new Error(`Generation timed out after polling. Request ID: ${requestId}`);
error.requestId = requestId;
throw error;
}