in packages/inference/src/providers/fal-ai.ts [90:153]
async getResponseFromQueueApi(
response: FalAiQueueOutput,
url?: string,
headers?: Record<string, string>
): Promise<unknown> {
if (!url || !headers) {
throw new InferenceClientInputError(`URL and headers are required for ${this.task} task`);
}
const requestId = response.request_id;
if (!requestId) {
throw new InferenceClientProviderOutputError(
`Received malformed response from Fal.ai ${this.task} API: no request ID found in the response`
);
}
let status = response.status;
const parsedUrl = new URL(url);
const baseUrl = `${parsedUrl.protocol}//${parsedUrl.host}${
parsedUrl.host === "router.huggingface.co" ? "/fal-ai" : ""
}`;
// extracting the provider model id for status and result urls
// from the response as it might be different from the mapped model in `url`
const modelId = new URL(response.response_url).pathname;
const queryParams = parsedUrl.search;
const statusUrl = `${baseUrl}${modelId}/status${queryParams}`;
const resultUrl = `${baseUrl}${modelId}${queryParams}`;
while (status !== "COMPLETED") {
await delay(500);
const statusResponse = await fetch(statusUrl, { headers });
if (!statusResponse.ok) {
throw new InferenceClientProviderApiError(
"Failed to fetch response status from fal-ai API",
{ url: statusUrl, method: "GET" },
{
requestId: statusResponse.headers.get("x-request-id") ?? "",
status: statusResponse.status,
body: await statusResponse.text(),
}
);
}
try {
status = (await statusResponse.json()).status;
} catch (error) {
throw new InferenceClientProviderOutputError(
"Failed to parse status response from fal-ai API: received malformed response"
);
}
}
const resultResponse = await fetch(resultUrl, { headers });
let result: unknown;
try {
result = await resultResponse.json();
} catch (error) {
throw new InferenceClientProviderOutputError(
"Failed to parse result response from fal-ai API: received malformed response"
);
}
return result;
}