Skip to content

Commit 47e7e42

Browse files
committed
lint + format
1 parent 92d3421 commit 47e7e42

File tree

14 files changed

+278
-124
lines changed

14 files changed

+278
-124
lines changed

packages/inference/src/error.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { JsonObject } from "./vendor/type-fest/basic.js";
22

3-
/**
3+
/**
44
* Base class for all inference-related errors.
55
*/
66
export abstract class HfInferenceError extends Error {
@@ -37,13 +37,15 @@ abstract class HfInferenceHttpRequestError extends HfInferenceError {
3737
super(message);
3838
this.httpRequest = {
3939
...httpRequest,
40-
...(httpRequest.headers ? {
41-
headers: {
42-
...httpRequest.headers,
43-
...("Authorization" in httpRequest.headers ? { Authorization: `Bearer [redacted]` } : undefined),
44-
/// redact authentication in the request headers
45-
},
46-
} : undefined)
40+
...(httpRequest.headers
41+
? {
42+
headers: {
43+
...httpRequest.headers,
44+
...("Authorization" in httpRequest.headers ? { Authorization: `Bearer [redacted]` } : undefined),
45+
/// redact authentication in the request headers
46+
},
47+
}
48+
: undefined),
4749
};
4850
this.httpResponse = httpResponse;
4951
}
@@ -70,12 +72,11 @@ export class HfInferenceHubApiError extends HfInferenceHttpRequestError {
7072
}
7173

7274
/**
73-
* Thrown when the inference output returned by the provider is invalid / does not match the expectations
75+
* Thrown when the inference output returned by the provider is invalid / does not match the expectations
7476
*/
7577
export class HfInferenceProviderOutputError extends HfInferenceError {
7678
constructor(message: string) {
7779
super(message);
7880
this.name = "ProviderOutputError";
7981
}
8082
}
81-

packages/inference/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export { InferenceClient, InferenceClientEndpoint, HfInference } from "./InferenceClient.js";
2-
export * from "./error.js"
2+
export * from "./error.js";
33
export * from "./types.js";
44
export * from "./tasks/index.js";
55
import * as snippets from "./snippets/index.js";

packages/inference/src/lib/getInferenceProviderMapping.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,9 @@ export async function fetchInferenceProviderMappingForModel(
3434
inferenceProviderMapping = inferenceProviderMappingCache.get(modelId)!;
3535
} else {
3636
const url = `${HF_HUB_URL}/api/models/${modelId}?expand[]=inferenceProviderMapping`;
37-
const resp = await (options?.fetch ?? fetch)(
38-
url,
39-
{
40-
headers: accessToken?.startsWith("hf_") ? { Authorization: `Bearer ${accessToken}` } : {},
41-
}
42-
);
37+
const resp = await (options?.fetch ?? fetch)(url, {
38+
headers: accessToken?.startsWith("hf_") ? { Authorization: `Bearer ${accessToken}` } : {},
39+
});
4340
if (!resp.ok) {
4441
if (resp.headers.get("Content-Type")?.startsWith("application/json")) {
4542
const error = await resp.json();

packages/inference/src/lib/getProviderHelper.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,14 @@ export function getProviderHelper(
282282
return new HFInference.HFInferenceTask();
283283
}
284284
if (!task) {
285-
throw new HfInferenceInputError("you need to provide a task name when using an external provider, e.g. 'text-to-image'");
285+
throw new HfInferenceInputError(
286+
"you need to provide a task name when using an external provider, e.g. 'text-to-image'"
287+
);
286288
}
287289
if (!(provider in PROVIDERS)) {
288-
throw new HfInferenceInputError(`Provider '${provider}' not supported. Available providers: ${Object.keys(PROVIDERS)}`);
290+
throw new HfInferenceInputError(
291+
`Provider '${provider}' not supported. Available providers: ${Object.keys(PROVIDERS)}`
292+
);
289293
}
290294
const providerTasks = PROVIDERS[provider];
291295
if (!providerTasks || !(task in providerTasks)) {

packages/inference/src/lib/makeRequestOptions.ts

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,26 +64,28 @@ export async function makeRequestOptions(
6464

6565
const inferenceProviderMapping = providerHelper.clientSideRoutingOnly
6666
? ({
67-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
68-
providerId: removeProviderPrefix(maybeModel!, provider),
69-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
70-
hfModelId: maybeModel!,
71-
status: "live",
72-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
73-
task: task!,
74-
} satisfies InferenceProviderModelMapping)
75-
: await getInferenceProviderMapping(
76-
{
77-
modelId: hfModel,
67+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
68+
providerId: removeProviderPrefix(maybeModel!, provider),
69+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
70+
hfModelId: maybeModel!,
71+
status: "live",
7872
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
7973
task: task!,
80-
provider,
81-
accessToken: args.accessToken,
82-
},
83-
{ fetch: options?.fetch }
84-
);
74+
} satisfies InferenceProviderModelMapping)
75+
: await getInferenceProviderMapping(
76+
{
77+
modelId: hfModel,
78+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
79+
task: task!,
80+
provider,
81+
accessToken: args.accessToken,
82+
},
83+
{ fetch: options?.fetch }
84+
);
8585
if (!inferenceProviderMapping) {
86-
throw new HfInferenceInputError(`We have not been able to find inference provider information for model ${hfModel}.`);
86+
throw new HfInferenceInputError(
87+
`We have not been able to find inference provider information for model ${hfModel}.`
88+
);
8789
}
8890

8991
// Use the sync version with the resolved model
@@ -210,7 +212,7 @@ async function loadTaskInfo(): Promise<Record<string, { models: { id: string }[]
210212
throw new HfInferenceHubApiError(
211213
"Failed to load tasks definitions from Hugging Face Hub.",
212214
{ url, method: "GET" },
213-
{ requestId: res.headers.get("x-request-id") ?? "", status: res.status, body: await res.text() },
215+
{ requestId: res.headers.get("x-request-id") ?? "", status: res.status, body: await res.text() }
214216
);
215217
}
216218
return await res.json();

packages/inference/src/providers/black-forest-labs.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ export class BlackForestLabsTextToImageTask extends TaskProviderHelper implement
7070
urlObj.searchParams.set("attempt", step.toString(10));
7171
const resp = await fetch(urlObj, { headers: { "Content-Type": "application/json" } });
7272
if (!resp.ok) {
73-
throw new HfInferenceProviderApiError("Failed to fetch result from black forest labs API",
73+
throw new HfInferenceProviderApiError(
74+
"Failed to fetch result from black forest labs API",
7475
{ url: urlObj.toString(), method: "GET", headers: { "Content-Type": "application/json" } },
7576
{ requestId: resp.headers.get("x-request-id") ?? "", status: resp.status, body: await resp.text() }
7677
);
@@ -95,6 +96,8 @@ export class BlackForestLabsTextToImageTask extends TaskProviderHelper implement
9596
return await image.blob();
9697
}
9798
}
98-
throw new HfInferenceProviderOutputError(`Timed out while waiting for the result from black forest labs API - aborting after 5 attempts`);
99+
throw new HfInferenceProviderOutputError(
100+
`Timed out while waiting for the result from black forest labs API - aborting after 5 attempts`
101+
);
99102
}
100103
}

packages/inference/src/providers/fal-ai.ts

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,16 @@ export class FalAITextToVideoTask extends FalAITask implements TextToVideoTaskHe
154154
}
155155
const requestId = response.request_id;
156156
if (!requestId) {
157-
throw new HfInferenceProviderOutputError("Received malformed response from Fal.ai text-to-video API: no request ID found in the response");
157+
throw new HfInferenceProviderOutputError(
158+
"Received malformed response from Fal.ai text-to-video API: no request ID found in the response"
159+
);
158160
}
159161
let status = response.status;
160162

161163
const parsedUrl = new URL(url);
162-
const baseUrl = `${parsedUrl.protocol}//${parsedUrl.host}${parsedUrl.host === "router.huggingface.co" ? "/fal-ai" : ""
163-
}`;
164+
const baseUrl = `${parsedUrl.protocol}//${parsedUrl.host}${
165+
parsedUrl.host === "router.huggingface.co" ? "/fal-ai" : ""
166+
}`;
164167

165168
// extracting the provider model id for status and result urls
166169
// from the response as it might be different from the mapped model in `url`
@@ -175,15 +178,22 @@ export class FalAITextToVideoTask extends FalAITask implements TextToVideoTaskHe
175178
const statusResponse = await fetch(statusUrl, { headers });
176179

177180
if (!statusResponse.ok) {
178-
throw new HfInferenceProviderApiError("Failed to fetch response status from fal-ai API",
181+
throw new HfInferenceProviderApiError(
182+
"Failed to fetch response status from fal-ai API",
179183
{ url: statusUrl, method: "GET" },
180-
{ requestId: statusResponse.headers.get("x-request-id") ?? "", status: statusResponse.status, body: await statusResponse.text() }
184+
{
185+
requestId: statusResponse.headers.get("x-request-id") ?? "",
186+
status: statusResponse.status,
187+
body: await statusResponse.text(),
188+
}
181189
);
182190
}
183191
try {
184192
status = (await statusResponse.json()).status;
185193
} catch (error) {
186-
throw new HfInferenceProviderOutputError("Failed to parse status response from fal-ai API: received malformed response");
194+
throw new HfInferenceProviderOutputError(
195+
"Failed to parse status response from fal-ai API: received malformed response"
196+
);
187197
}
188198
}
189199

@@ -192,7 +202,9 @@ export class FalAITextToVideoTask extends FalAITask implements TextToVideoTaskHe
192202
try {
193203
result = await resultResponse.json();
194204
} catch (error) {
195-
throw new HfInferenceProviderOutputError("Failed to parse result response from fal-ai API: received malformed response");
205+
throw new HfInferenceProviderOutputError(
206+
"Failed to parse result response from fal-ai API: received malformed response"
207+
);
196208
}
197209
if (
198210
typeof result === "object" &&
@@ -208,7 +220,9 @@ export class FalAITextToVideoTask extends FalAITask implements TextToVideoTaskHe
208220
return await urlResponse.blob();
209221
} else {
210222
throw new HfInferenceProviderOutputError(
211-
`Received malformed response from Fal.ai text-to-video API: expected { video: { url: string } } result format, got instead: ${JSON.stringify(result)}`
223+
`Received malformed response from Fal.ai text-to-video API: expected { video: { url: string } } result format, got instead: ${JSON.stringify(
224+
result
225+
)}`
212226
);
213227
}
214228
}
@@ -224,7 +238,9 @@ export class FalAIAutomaticSpeechRecognitionTask extends FalAITask implements Au
224238
const res = response as FalAIAutomaticSpeechRecognitionOutput;
225239
if (typeof res?.text !== "string") {
226240
throw new HfInferenceProviderOutputError(
227-
`Received malformed response from Fal.ai Automatic Speech Recognition API: expected { text: string } format, got instead: ${JSON.stringify(response)}`
241+
`Received malformed response from Fal.ai Automatic Speech Recognition API: expected { text: string } format, got instead: ${JSON.stringify(
242+
response
243+
)}`
228244
);
229245
}
230246
return { text: res.text };
@@ -266,22 +282,34 @@ export class FalAITextToSpeechTask extends FalAITask {
266282
const res = response as FalAITextToSpeechOutput;
267283
if (typeof res?.audio?.url !== "string") {
268284
throw new HfInferenceProviderOutputError(
269-
`Received malformed response from Fal.ai Text-to-Speech API: expected { audio: { url: string } } format, got instead: ${JSON.stringify(response)}`
285+
`Received malformed response from Fal.ai Text-to-Speech API: expected { audio: { url: string } } format, got instead: ${JSON.stringify(
286+
response
287+
)}`
270288
);
271289
}
272290
const urlResponse = await fetch(res.audio.url);
273291
if (!urlResponse.ok) {
274-
throw new HfInferenceProviderApiError(`Failed to fetch audio from ${res.audio.url}: ${urlResponse.statusText}`,
292+
throw new HfInferenceProviderApiError(
293+
`Failed to fetch audio from ${res.audio.url}: ${urlResponse.statusText}`,
275294
{ url: res.audio.url, method: "GET", headers: { "Content-Type": "application/json" } },
276-
{ requestId: urlResponse.headers.get("x-request-id") ?? "", status: urlResponse.status, body: await urlResponse.text() }
295+
{
296+
requestId: urlResponse.headers.get("x-request-id") ?? "",
297+
status: urlResponse.status,
298+
body: await urlResponse.text(),
299+
}
277300
);
278301
}
279302
try {
280303
return await urlResponse.blob();
281304
} catch (error) {
282-
throw new HfInferenceProviderApiError(`Failed to fetch audio from ${res.audio.url}: ${error instanceof Error ? error.message : String(error)}`,
305+
throw new HfInferenceProviderApiError(
306+
`Failed to fetch audio from ${res.audio.url}: ${error instanceof Error ? error.message : String(error)}`,
283307
{ url: res.audio.url, method: "GET", headers: { "Content-Type": "application/json" } },
284-
{ requestId: urlResponse.headers.get("x-request-id") ?? "", status: urlResponse.status, body: await urlResponse.text() }
308+
{
309+
requestId: urlResponse.headers.get("x-request-id") ?? "",
310+
status: urlResponse.status,
311+
body: await urlResponse.text(),
312+
}
285313
);
286314
}
287315
}

packages/inference/src/providers/featherless-ai.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ export class FeatherlessAITextGenerationTask extends BaseTextGenerationTask {
3838
...omit(params.args, ["inputs", "parameters"]),
3939
...(params.args.parameters
4040
? {
41-
max_tokens: params.args.parameters.max_new_tokens,
42-
...omit(params.args.parameters, "max_new_tokens"),
43-
}
41+
max_tokens: params.args.parameters.max_new_tokens,
42+
...omit(params.args.parameters, "max_new_tokens"),
43+
}
4444
: undefined),
4545
prompt: params.args.inputs,
4646
};

0 commit comments

Comments
 (0)