|
| 1 | +import { fetchUrlToDataURI } from "@/server/lib/util"; |
| 2 | +import type { AiProvider, ApiProviderSettings, ApiProviderSettingsItem } from "../types/provider"; |
| 3 | +import { type ProviderSettingsType, chooseAblility, doParseSettings, findModel } from "../types/provider"; |
| 4 | + |
| 5 | +const fluxSettingsSchema = [ |
| 6 | + { |
| 7 | + key: "apiKey", |
| 8 | + type: "password", |
| 9 | + required: true, |
| 10 | + }, |
| 11 | +] as const satisfies ApiProviderSettingsItem[]; |
| 12 | + |
| 13 | +// Automatically generate type from schema |
| 14 | +export type FluxSettings = ProviderSettingsType<typeof fluxSettingsSchema>; |
| 15 | + |
| 16 | +interface FluxSubmitResponse { |
| 17 | + id: string; |
| 18 | + polling_url: string; |
| 19 | +} |
| 20 | + |
| 21 | +interface FluxPollResponse { |
| 22 | + id: string; |
| 23 | + status: "Pending" | "Running" | "Ready" | "Error" | "Failed"; |
| 24 | + result?: { |
| 25 | + sample: string; |
| 26 | + }; |
| 27 | + error?: string; |
| 28 | +} |
| 29 | + |
| 30 | +const Flux: AiProvider = { |
| 31 | + id: "flux", |
| 32 | + name: "Flux", |
| 33 | + supportCors: false, |
| 34 | + enabledByDefault: true, |
| 35 | + settings: fluxSettingsSchema, |
| 36 | + models: [ |
| 37 | + { |
| 38 | + id: "flux-kontext-max", |
| 39 | + name: "FLUX.1 Kontext [max]", |
| 40 | + ability: "i2i", |
| 41 | + enabledByDefault: true, |
| 42 | + }, |
| 43 | + { |
| 44 | + id: "flux-kontext-pro", |
| 45 | + name: "FLUX.1 Kontext [pro]", |
| 46 | + ability: "i2i", |
| 47 | + enabledByDefault: true, |
| 48 | + }, |
| 49 | + { |
| 50 | + id: "flux-pro-1.1-ultra", |
| 51 | + name: "FLUX1.1 [pro] Ultra", |
| 52 | + ability: "t2i", |
| 53 | + enabledByDefault: true, |
| 54 | + }, |
| 55 | + { |
| 56 | + id: "flux-pro-1.1", |
| 57 | + name: "FLUX1.1 [pro]", |
| 58 | + ability: "t2i", |
| 59 | + enabledByDefault: true, |
| 60 | + }, |
| 61 | + { |
| 62 | + id: "flux-pro", |
| 63 | + name: "FLUX.1 [pro]", |
| 64 | + ability: "t2i", |
| 65 | + enabledByDefault: true, |
| 66 | + }, |
| 67 | + { |
| 68 | + id: "flux-dev", |
| 69 | + name: "FLUX.1 [dev]", |
| 70 | + ability: "t2i", |
| 71 | + enabledByDefault: true, |
| 72 | + }, |
| 73 | + ], |
| 74 | + parseSettings: <FluxSettings>(settings: ApiProviderSettings) => { |
| 75 | + return doParseSettings(settings, fluxSettingsSchema) as FluxSettings; |
| 76 | + }, |
| 77 | + generate: async (request, settings) => { |
| 78 | + const { apiKey } = Flux.parseSettings<FluxSettings>(settings); |
| 79 | + |
| 80 | + const model = findModel(Flux, request.modelId); |
| 81 | + const genType = chooseAblility(request, model.ability); |
| 82 | + |
| 83 | + const requestBody: any = { |
| 84 | + prompt: request.prompt, |
| 85 | + }; |
| 86 | + if (genType === "i2i" && request.images?.[0]) { |
| 87 | + requestBody.image_url = request.images[0]; |
| 88 | + } |
| 89 | + |
| 90 | + const submitResponse = await fetch(`https://api.bfl.ai/v1/${request.modelId}`, { |
| 91 | + method: "POST", |
| 92 | + headers: { |
| 93 | + accept: "application/json", |
| 94 | + "x-key": apiKey, |
| 95 | + "Content-Type": "application/json", |
| 96 | + }, |
| 97 | + body: JSON.stringify(requestBody), |
| 98 | + }); |
| 99 | + |
| 100 | + if (!submitResponse.ok) { |
| 101 | + if (submitResponse.status === 403) { |
| 102 | + return { |
| 103 | + errorReason: "CONFIG_ERROR", |
| 104 | + images: [], |
| 105 | + }; |
| 106 | + } |
| 107 | + throw new Error(`Flux API error: ${submitResponse.status} ${submitResponse.statusText}`); |
| 108 | + } |
| 109 | + |
| 110 | + const submitData: FluxSubmitResponse = await submitResponse.json(); |
| 111 | + const { id: requestId, polling_url: pollingUrl } = submitData; |
| 112 | + |
| 113 | + let attempts = 0; |
| 114 | + const maxAttempts = 120; |
| 115 | + |
| 116 | + while (attempts < maxAttempts) { |
| 117 | + await new Promise((resolve) => setTimeout(resolve, 500)); |
| 118 | + attempts++; |
| 119 | + |
| 120 | + const pollUrl = new URL(pollingUrl); |
| 121 | + pollUrl.searchParams.set("id", requestId); |
| 122 | + |
| 123 | + const pollResponse = await fetch(pollUrl.toString(), { |
| 124 | + method: "GET", |
| 125 | + headers: { |
| 126 | + accept: "application/json", |
| 127 | + "x-key": apiKey, |
| 128 | + }, |
| 129 | + }); |
| 130 | + |
| 131 | + if (!pollResponse.ok) { |
| 132 | + throw new Error(`Flux polling error: ${pollResponse.status} ${pollResponse.statusText}`); |
| 133 | + } |
| 134 | + |
| 135 | + const pollData: FluxPollResponse = await pollResponse.json(); |
| 136 | + |
| 137 | + if (pollData.status === "Ready" && pollData.result?.sample) { |
| 138 | + try { |
| 139 | + const imageDataUri = await fetchUrlToDataURI(pollData.result.sample); |
| 140 | + return { |
| 141 | + images: [imageDataUri], |
| 142 | + }; |
| 143 | + } catch (error) { |
| 144 | + console.error("Flux image fetch error:", error); |
| 145 | + return { |
| 146 | + images: [], |
| 147 | + }; |
| 148 | + } |
| 149 | + } else if (pollData.status === "Error" || pollData.status === "Failed") { |
| 150 | + throw new Error(`Flux generation failed: ${pollData.error || "Unknown error"}`); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + throw new Error("Flux generation timeout - exceeded maximum polling attempts"); |
| 155 | + }, |
| 156 | +}; |
| 157 | + |
| 158 | +export default Flux; |
0 commit comments