async getResponse()

in packages/inference/src/providers/black-forest-labs.ts [64:106]


	async getResponse(
		response: BlackForestLabsResponse,
		url?: string,
		headers?: HeadersInit,
		outputType?: "url" | "blob"
	): Promise<string | Blob> {
		const urlObj = new URL(response.polling_url);
		for (let step = 0; step < 5; step++) {
			await delay(1000);
			console.debug(`Polling Black Forest Labs API for the result... ${step + 1}/5`);
			urlObj.searchParams.set("attempt", step.toString(10));
			const resp = await fetch(urlObj, { headers: { "Content-Type": "application/json" } });
			if (!resp.ok) {
				throw new InferenceClientProviderApiError(
					"Failed to fetch result from black forest labs API",
					{ url: urlObj.toString(), method: "GET", headers: { "Content-Type": "application/json" } },
					{ requestId: resp.headers.get("x-request-id") ?? "", status: resp.status, body: await resp.text() }
				);
			}
			const payload = await resp.json();
			if (
				typeof payload === "object" &&
				payload &&
				"status" in payload &&
				typeof payload.status === "string" &&
				payload.status === "Ready" &&
				"result" in payload &&
				typeof payload.result === "object" &&
				payload.result &&
				"sample" in payload.result &&
				typeof payload.result.sample === "string"
			) {
				if (outputType === "url") {
					return payload.result.sample;
				}
				const image = await fetch(payload.result.sample);
				return await image.blob();
			}
		}
		throw new InferenceClientProviderOutputError(
			`Timed out while waiting for the result from black forest labs API - aborting after 5 attempts`
		);
	}