@workglow/huggingface-inference 0.3.17 → 0.3.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/dist/ai/HfInferenceProvider.d.ts +68 -0
  2. package/dist/ai/HfInferenceProvider.d.ts.map +1 -0
  3. package/dist/ai/HfInferenceQueuedProvider.d.ts +65 -0
  4. package/dist/ai/HfInferenceQueuedProvider.d.ts.map +1 -0
  5. package/dist/ai/common/HFI_AspectRatio.d.ts +15 -0
  6. package/dist/ai/common/HFI_AspectRatio.d.ts.map +1 -0
  7. package/dist/ai/common/HFI_Capabilities.d.ts +23 -0
  8. package/dist/ai/common/HFI_Capabilities.d.ts.map +1 -0
  9. package/dist/ai/common/HFI_CapabilitySets.d.ts +25 -0
  10. package/dist/ai/common/HFI_CapabilitySets.d.ts.map +1 -0
  11. package/dist/ai/common/HFI_Client.d.ts +14 -0
  12. package/dist/ai/common/HFI_Client.d.ts.map +1 -0
  13. package/dist/ai/common/HFI_Constants.d.ts +7 -0
  14. package/dist/ai/common/HFI_Constants.d.ts.map +1 -0
  15. package/dist/ai/common/HFI_ImageEdit.d.ts +13 -0
  16. package/dist/ai/common/HFI_ImageEdit.d.ts.map +1 -0
  17. package/dist/ai/common/HFI_ImageGenerate.d.ts +13 -0
  18. package/dist/ai/common/HFI_ImageGenerate.d.ts.map +1 -0
  19. package/dist/ai/common/HFI_ImageValidation.d.ts +7 -0
  20. package/dist/ai/common/HFI_ImageValidation.d.ts.map +1 -0
  21. package/dist/ai/common/HFI_JobRunFns.d.ts +10 -0
  22. package/dist/ai/common/HFI_JobRunFns.d.ts.map +1 -0
  23. package/dist/ai/common/HFI_ModelInfo.d.ts +9 -0
  24. package/dist/ai/common/HFI_ModelInfo.d.ts.map +1 -0
  25. package/dist/ai/common/HFI_ModelSchema.d.ts +152 -0
  26. package/dist/ai/common/HFI_ModelSchema.d.ts.map +1 -0
  27. package/dist/ai/common/HFI_ModelSearch.d.ts +8 -0
  28. package/dist/ai/common/HFI_ModelSearch.d.ts.map +1 -0
  29. package/dist/ai/common/HFI_TextEmbedding.d.ts +9 -0
  30. package/dist/ai/common/HFI_TextEmbedding.d.ts.map +1 -0
  31. package/dist/ai/common/HFI_TextGeneration.d.ts +9 -0
  32. package/dist/ai/common/HFI_TextGeneration.d.ts.map +1 -0
  33. package/dist/ai/common/HFI_TextRewriter.d.ts +9 -0
  34. package/dist/ai/common/HFI_TextRewriter.d.ts.map +1 -0
  35. package/dist/ai/common/HFI_TextSummary.d.ts +9 -0
  36. package/dist/ai/common/HFI_TextSummary.d.ts.map +1 -0
  37. package/dist/ai/common/HFI_ToolCalling.d.ts +9 -0
  38. package/dist/ai/common/HFI_ToolCalling.d.ts.map +1 -0
  39. package/dist/ai/index.d.ts +37 -0
  40. package/dist/ai/index.d.ts.map +1 -0
  41. package/dist/ai/registerHfInference.d.ts +10 -0
  42. package/dist/ai/registerHfInference.d.ts.map +1 -0
  43. package/dist/ai/registerHfInferenceInline.d.ts +8 -0
  44. package/dist/ai/registerHfInferenceInline.d.ts.map +1 -0
  45. package/dist/ai/registerHfInferenceWorker.d.ts +7 -0
  46. package/dist/ai/registerHfInferenceWorker.d.ts.map +1 -0
  47. package/dist/ai/runtime.d.ts +16 -0
  48. package/dist/ai/runtime.d.ts.map +1 -0
  49. package/dist/ai-runtime.d.ts +7 -0
  50. package/dist/ai-runtime.d.ts.map +1 -0
  51. package/dist/ai-runtime.js +544 -0
  52. package/dist/ai-runtime.js.map +29 -0
  53. package/dist/ai.d.ts +7 -0
  54. package/dist/ai.d.ts.map +1 -0
  55. package/dist/ai.js +578 -0
  56. package/dist/ai.js.map +29 -0
  57. package/package.json +11 -11
@@ -0,0 +1,8 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2025 Steven Roussey <sroussey@gmail.com>
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+ import type { AiProviderRegisterOptions } from "@workglow/ai";
7
+ export declare function registerHfInferenceInline(options?: AiProviderRegisterOptions): Promise<void>;
8
+ //# sourceMappingURL=registerHfInferenceInline.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"registerHfInferenceInline.d.ts","sourceRoot":"","sources":["../../src/ai/registerHfInferenceInline.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,cAAc,CAAC;AAM9D,wBAAsB,yBAAyB,CAC7C,OAAO,CAAC,EAAE,yBAAyB,GAClC,OAAO,CAAC,IAAI,CAAC,CAOf"}
@@ -0,0 +1,7 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2025 Steven Roussey <sroussey@gmail.com>
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+ export declare function registerHfInferenceWorker(): Promise<void>;
7
+ //# sourceMappingURL=registerHfInferenceWorker.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"registerHfInferenceWorker.d.ts","sourceRoot":"","sources":["../../src/ai/registerHfInferenceWorker.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAMH,wBAAsB,yBAAyB,IAAI,OAAO,CAAC,IAAI,CAAC,CAK/D"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2025 Steven Roussey <sroussey@gmail.com>
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+ /**
7
+ * Worker server and main-thread inline Hugging Face Inference registration (pulls in `HFI_JobRunFns`),
8
+ * plus API client helpers (`HFI_Client`).
9
+ * Import from `@workglow/huggingface-inference/ai-runtime` — not from the main `hf-inference` barrel.
10
+ *
11
+ * Use `export *` (not `export { … } from "…"`) so the Bun bundler keeps the module graph.
12
+ */
13
+ export * from "./common/HFI_Client";
14
+ export * from "./registerHfInferenceInline";
15
+ export * from "./registerHfInferenceWorker";
16
+ //# sourceMappingURL=runtime.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../../src/ai/runtime.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;;;;;GAMG;AAGH,cAAc,qBAAqB,CAAC;AACpC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,6BAA6B,CAAC"}
@@ -0,0 +1,7 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2025 Steven Roussey <sroussey@gmail.com>
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+ export * from "./ai/runtime";
7
+ //# sourceMappingURL=ai-runtime.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ai-runtime.d.ts","sourceRoot":"","sources":["../src/ai-runtime.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,cAAc,cAAc,CAAC"}
@@ -0,0 +1,544 @@
1
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
+ }) : x)(function(x) {
4
+ if (typeof require !== "undefined")
5
+ return require.apply(this, arguments);
6
+ throw Error('Dynamic require of "' + x + '" is not supported');
7
+ });
8
+
9
+ // src/ai/common/HFI_Client.ts
10
+ import { resolveApiKey } from "@workglow/ai/provider-utils";
11
+ var _loadPromise;
12
+ async function loadHfInferenceSDK() {
13
+ _loadPromise ??= import("@huggingface/inference").then((mod) => mod).catch(() => {
14
+ _loadPromise = undefined;
15
+ throw new Error("@huggingface/inference is required for Hugging Face Inference tasks. Install it with: bun add @huggingface/inference");
16
+ });
17
+ return _loadPromise;
18
+ }
19
+ async function getClient(model) {
20
+ const sdk = await loadHfInferenceSDK();
21
+ const config = model?.provider_config;
22
+ const apiKey = resolveApiKey({
23
+ config,
24
+ envVar: "HF_TOKEN",
25
+ providerLabel: "Hugging Face"
26
+ });
27
+ try {
28
+ return new sdk.InferenceClient(apiKey);
29
+ } catch (err) {
30
+ throw new Error(`Failed to create HuggingFace Inference client: ${err instanceof Error ? err.message : "unknown error"}`);
31
+ }
32
+ }
33
+ function getModelName(model) {
34
+ const name = model?.provider_config?.model_name;
35
+ if (!name) {
36
+ throw new Error("Missing model name in provider_config.model_name.");
37
+ }
38
+ return name;
39
+ }
40
+ function getProvider(model) {
41
+ return model?.provider_config?.provider;
42
+ }
43
+
44
+ // src/ai/registerHfInferenceInline.ts
45
+ import { registerProviderInline } from "@workglow/ai/provider-utils";
46
+
47
+ // src/ai/common/HFI_ImageValidation.ts
48
+ import { AiImageOutputTask, ProviderUnsupportedFeatureError } from "@workglow/ai";
49
+
50
+ // src/ai/common/HFI_AspectRatio.ts
51
+ var FLUX_DIMS = {
52
+ "1:1": { width: 1024, height: 1024 },
53
+ "16:9": { width: 1344, height: 768 },
54
+ "9:16": { width: 768, height: 1344 },
55
+ "4:3": { width: 1152, height: 896 },
56
+ "3:4": { width: 896, height: 1152 }
57
+ };
58
+ var SDXL_DIMS = {
59
+ "1:1": { width: 1024, height: 1024 },
60
+ "16:9": { width: 1280, height: 720 },
61
+ "9:16": { width: 720, height: 1280 },
62
+ "4:3": { width: 1024, height: 768 },
63
+ "3:4": { width: 768, height: 1024 }
64
+ };
65
+ var DEFAULT_DIMS = SDXL_DIMS;
66
+ var MODEL_DIM_TABLE = [
67
+ { pattern: /^black-forest-labs\/FLUX/i, dims: FLUX_DIMS },
68
+ { pattern: /^stabilityai\/.*-xl/i, dims: SDXL_DIMS }
69
+ ];
70
+ function resolveHfImageDims(modelId, aspectRatio) {
71
+ const entry = MODEL_DIM_TABLE.find((e) => e.pattern.test(modelId));
72
+ return (entry?.dims ?? DEFAULT_DIMS)[aspectRatio];
73
+ }
74
+ function isHfInpaintingModel(modelId) {
75
+ return /inpaint|kontext/i.test(modelId);
76
+ }
77
+
78
+ // src/ai/common/HFI_Constants.ts
79
+ var HF_INFERENCE = "HF_INFERENCE";
80
+
81
+ // src/ai/common/HFI_ImageValidation.ts
82
+ function registerHfImageValidator() {
83
+ AiImageOutputTask.registerProviderImageValidator(HF_INFERENCE, (taskType, input, model) => {
84
+ if (taskType !== "ImageEditTask")
85
+ return;
86
+ const modelId = model.model_id ?? "";
87
+ const modelName = model.provider_config?.model_name ?? modelId;
88
+ const additional = input["additionalImages"];
89
+ if (Array.isArray(additional) && additional.length > 0) {
90
+ throw new ProviderUnsupportedFeatureError("additionalImages", modelId, "HF Inference image-to-image only supports a single input image");
91
+ }
92
+ if (input["mask"] !== undefined && input["mask"] !== null && !isHfInpaintingModel(modelName)) {
93
+ throw new ProviderUnsupportedFeatureError("mask", modelId, "Mask is only supported on HF inpainting models (e.g., FLUX.1-Kontext-dev)");
94
+ }
95
+ });
96
+ }
97
+
98
+ // src/ai/common/HFI_CapabilitySets.ts
99
+ var HFI_TEXT_GENERATION = ["text.generation"];
100
+ var HFI_TOOL_USE = ["text.generation", "tool-use"];
101
+ var HFI_TEXT_REWRITER = ["text.rewriter"];
102
+ var HFI_TEXT_SUMMARY = ["text.summary"];
103
+ var HFI_TEXT_EMBEDDING = ["text.embedding"];
104
+ var HFI_IMAGE_GENERATION = ["image.generation"];
105
+ var HFI_IMAGE_EDITING = ["image.editing"];
106
+ var HFI_MODEL_SEARCH = ["model.search"];
107
+ var HFI_MODEL_INFO = ["model.info"];
108
+ var HFI_CAPABILITY_SETS = [
109
+ HFI_TEXT_GENERATION,
110
+ HFI_TOOL_USE,
111
+ HFI_TEXT_REWRITER,
112
+ HFI_TEXT_SUMMARY,
113
+ HFI_TEXT_EMBEDDING,
114
+ HFI_IMAGE_GENERATION,
115
+ HFI_IMAGE_EDITING,
116
+ HFI_MODEL_SEARCH,
117
+ HFI_MODEL_INFO
118
+ ];
119
+
120
+ // src/ai/common/HFI_ImageEdit.ts
121
+ import { ImageGenerationContentPolicyError, ImageGenerationProviderError } from "@workglow/ai";
122
+ import { getLogger } from "@workglow/util/worker";
123
+ import {
124
+ blobToImageValue,
125
+ imageValueToPngBytes,
126
+ modelIdForError
127
+ } from "@workglow/ai/provider-utils";
128
+ async function gpuImageToBlob(image) {
129
+ const bytes = await imageValueToPngBytes(image);
130
+ const buffer = bytes.buffer instanceof ArrayBuffer ? bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength) : new Uint8Array(bytes).buffer;
131
+ return new Blob([buffer], { type: "image/png" });
132
+ }
133
+ var HFI_ImageEdit_Stream = async (input, model, signal, emit) => {
134
+ const logger = getLogger();
135
+ const timer = `hfi:ImageEdit:${getModelName(model)}`;
136
+ logger.time(timer);
137
+ let result;
138
+ try {
139
+ const client = await getClient(model);
140
+ const modelName = getModelName(model);
141
+ const dims = resolveHfImageDims(modelName, input.aspectRatio ?? "1:1");
142
+ const inputBlob = await gpuImageToBlob(input.image);
143
+ const params = {
144
+ width: dims.width,
145
+ height: dims.height,
146
+ seed: input.seed,
147
+ negative_prompt: input.negativePrompt,
148
+ prompt: input.prompt,
149
+ ...input.providerOptions ?? {}
150
+ };
151
+ if (input.mask) {
152
+ const maskBlob = await gpuImageToBlob(input.mask);
153
+ params.mask_image = maskBlob;
154
+ }
155
+ const blob = await client.imageToImage({
156
+ model: modelName,
157
+ inputs: inputBlob,
158
+ parameters: params
159
+ }, { signal });
160
+ const image = await blobToImageValue(blob);
161
+ logger.timeEnd(timer);
162
+ result = { image };
163
+ } catch (err) {
164
+ if (err instanceof ImageGenerationProviderError || err instanceof ImageGenerationContentPolicyError)
165
+ throw err;
166
+ const msg = err instanceof Error ? err.message : "unknown error";
167
+ if (/NSFW|safety|policy/i.test(msg))
168
+ throw new ImageGenerationContentPolicyError(modelIdForError(model, "huggingface"), msg);
169
+ throw new ImageGenerationProviderError(modelIdForError(model, "huggingface"), msg, {
170
+ cause: err
171
+ });
172
+ }
173
+ if (signal.aborted)
174
+ return;
175
+ emit({ type: "finish", data: result });
176
+ };
177
+
178
+ // src/ai/common/HFI_ImageGenerate.ts
179
+ import { ImageGenerationContentPolicyError as ImageGenerationContentPolicyError2, ImageGenerationProviderError as ImageGenerationProviderError2 } from "@workglow/ai";
180
+ import { getLogger as getLogger2 } from "@workglow/util/worker";
181
+ import { blobToImageValue as blobToImageValue2, modelIdForError as modelIdForError2 } from "@workglow/ai/provider-utils";
182
+ var HFI_ImageGenerate_Stream = async (input, model, signal, emit) => {
183
+ const logger = getLogger2();
184
+ const timer = `hfi:ImageGenerate:${getModelName(model)}`;
185
+ logger.time(timer);
186
+ let result;
187
+ try {
188
+ const client = await getClient(model);
189
+ const modelName = getModelName(model);
190
+ const dims = resolveHfImageDims(modelName, input.aspectRatio ?? "1:1");
191
+ const blob = await client.textToImage({
192
+ model: modelName,
193
+ inputs: input.prompt,
194
+ parameters: {
195
+ width: dims.width,
196
+ height: dims.height,
197
+ seed: input.seed,
198
+ negative_prompt: input.negativePrompt,
199
+ ...input.providerOptions ?? {}
200
+ }
201
+ }, { outputType: "blob", signal });
202
+ const image = await blobToImageValue2(blob);
203
+ logger.timeEnd(timer);
204
+ result = { image };
205
+ } catch (err) {
206
+ if (err instanceof ImageGenerationProviderError2 || err instanceof ImageGenerationContentPolicyError2)
207
+ throw err;
208
+ const msg = err instanceof Error ? err.message : "unknown error";
209
+ if (/NSFW|safety|policy/i.test(msg))
210
+ throw new ImageGenerationContentPolicyError2(modelIdForError2(model, "huggingface"), msg);
211
+ throw new ImageGenerationProviderError2(modelIdForError2(model, "huggingface"), msg, {
212
+ cause: err
213
+ });
214
+ }
215
+ if (signal.aborted)
216
+ return;
217
+ emit({ type: "finish", data: result });
218
+ };
219
+
220
+ // src/ai/common/HFI_ModelInfo.ts
221
+ var HFI_ModelInfo = async (input, model, _signal, emit) => {
222
+ if (input.detail === "dimensions") {
223
+ const pc = model?.provider_config;
224
+ const native_dimensions = typeof pc?.native_dimensions === "number" ? pc.native_dimensions : undefined;
225
+ const mrl = typeof pc?.mrl === "boolean" ? pc.mrl : false;
226
+ emit({
227
+ type: "finish",
228
+ data: {
229
+ model: input.model,
230
+ is_local: false,
231
+ is_remote: true,
232
+ supports_browser: true,
233
+ supports_node: true,
234
+ is_cached: false,
235
+ is_loaded: false,
236
+ file_sizes: null,
237
+ ...native_dimensions !== undefined ? { native_dimensions } : {},
238
+ ...mrl ? { mrl } : {}
239
+ }
240
+ });
241
+ return;
242
+ }
243
+ emit({
244
+ type: "finish",
245
+ data: {
246
+ model: input.model,
247
+ is_local: false,
248
+ is_remote: true,
249
+ supports_browser: true,
250
+ supports_node: true,
251
+ is_cached: false,
252
+ is_loaded: false,
253
+ file_sizes: null
254
+ }
255
+ });
256
+ };
257
+
258
+ // src/ai/common/HFI_ModelSearch.ts
259
+ import {
260
+ filterLabeledModelsByQuery,
261
+ mapHfModelResult,
262
+ searchHfModels
263
+ } from "@workglow/ai/provider-utils";
264
+ var HFI_IMAGE_MODELS = [
265
+ { id: "black-forest-labs/FLUX.1-schnell", capabilities: ["image.generation"] },
266
+ { id: "black-forest-labs/FLUX.1-Kontext-dev", capabilities: ["image.editing"] }
267
+ ];
268
+ function buildFallbackResults() {
269
+ return HFI_IMAGE_MODELS.map((m) => ({
270
+ id: m.id,
271
+ label: m.id,
272
+ description: "",
273
+ record: {
274
+ model_id: m.id,
275
+ provider: HF_INFERENCE,
276
+ title: m.id.split("/").pop() ?? m.id,
277
+ description: "",
278
+ capabilities: m.capabilities,
279
+ provider_config: { model_name: m.id },
280
+ metadata: {}
281
+ },
282
+ raw: m
283
+ }));
284
+ }
285
+ var HFI_ModelSearch = async (input, _model, signal, emit) => {
286
+ const query = input.query?.trim() ?? "";
287
+ if (!input.credential_key) {
288
+ const fallback = buildFallbackResults();
289
+ const labeled = fallback.map((r) => ({ label: r.label, value: r.id }));
290
+ const filtered = filterLabeledModelsByQuery(labeled, query).map((m) => fallback.find((r) => r.id === m.value));
291
+ emit({ type: "finish", data: { results: filtered } });
292
+ return;
293
+ }
294
+ const entries = await searchHfModels(query, undefined, undefined, signal, input.credential_key);
295
+ const results = entries.map((entry) => {
296
+ const imageEntry = HFI_IMAGE_MODELS.find((m) => m.id === entry.id);
297
+ const mapped = mapHfModelResult(entry, HF_INFERENCE);
298
+ if (imageEntry) {
299
+ mapped.record.capabilities = imageEntry.capabilities;
300
+ }
301
+ return mapped;
302
+ });
303
+ emit({ type: "finish", data: { results } });
304
+ };
305
+
306
+ // src/ai/common/HFI_TextEmbedding.ts
307
+ import { getLogger as getLogger3 } from "@workglow/util/worker";
308
+ var HFI_TextEmbedding = async (input, model, signal, emit) => {
309
+ const logger = getLogger3();
310
+ const timerLabel = `hfi:TextEmbedding:${model?.provider_config?.model_name}`;
311
+ logger.time(timerLabel, { model: model?.provider_config?.model_name });
312
+ const client = await getClient(model);
313
+ const modelName = getModelName(model);
314
+ if (Array.isArray(input.text)) {
315
+ const embeddings = await Promise.all(input.text.map((text) => client.featureExtraction({
316
+ model: modelName,
317
+ inputs: text
318
+ }, { signal })));
319
+ logger.timeEnd(timerLabel, { model: model?.provider_config?.model_name, batch: true });
320
+ emit({
321
+ type: "finish",
322
+ data: {
323
+ vector: embeddings.map((embedding2) => new Float32Array(embedding2))
324
+ }
325
+ });
326
+ return;
327
+ }
328
+ const embedding = await client.featureExtraction({
329
+ model: modelName,
330
+ inputs: input.text
331
+ }, { signal });
332
+ logger.timeEnd(timerLabel, { model: model?.provider_config?.model_name });
333
+ emit({
334
+ type: "finish",
335
+ data: { vector: new Float32Array(embedding) }
336
+ });
337
+ };
338
+
339
+ // src/ai/common/HFI_TextGeneration.ts
340
+ import { toOpenAIMessages } from "@workglow/ai/worker";
341
+ var HFI_TextGeneration_Stream = async (input, model, signal, emit) => {
342
+ const client = await getClient(model);
343
+ const modelName = getModelName(model);
344
+ const provider = getProvider(model);
345
+ const unified = input;
346
+ const hasMessages = Array.isArray(unified.messages) && unified.messages.length > 0;
347
+ const messages = hasMessages ? toOpenAIMessages({
348
+ messages: unified.messages,
349
+ systemPrompt: unified.systemPrompt,
350
+ prompt: "",
351
+ tools: []
352
+ }).map((m) => ({ ...m, content: m.content ?? "" })) : [{ role: "user", content: input.prompt }];
353
+ const stream = client.chatCompletionStream({
354
+ model: modelName,
355
+ messages,
356
+ max_tokens: input.maxTokens,
357
+ temperature: input.temperature,
358
+ top_p: input.topP,
359
+ frequency_penalty: input.frequencyPenalty,
360
+ provider
361
+ }, { signal });
362
+ for await (const chunk of stream) {
363
+ const delta = chunk.choices[0]?.delta?.content ?? "";
364
+ if (delta) {
365
+ emit({ type: "text-delta", port: "text", textDelta: delta });
366
+ }
367
+ }
368
+ emit({ type: "finish", data: {} });
369
+ };
370
+
371
+ // src/ai/common/HFI_TextRewriter.ts
372
+ var HFI_TextRewriter_Stream = async (input, model, signal, emit) => {
373
+ const client = await getClient(model);
374
+ const modelName = getModelName(model);
375
+ const provider = getProvider(model);
376
+ const stream = client.chatCompletionStream({
377
+ model: modelName,
378
+ messages: [
379
+ { role: "system", content: input.prompt },
380
+ { role: "user", content: input.text }
381
+ ],
382
+ provider
383
+ }, { signal });
384
+ for await (const chunk of stream) {
385
+ const delta = chunk.choices[0]?.delta?.content ?? "";
386
+ if (delta) {
387
+ emit({ type: "text-delta", port: "text", textDelta: delta });
388
+ }
389
+ }
390
+ emit({ type: "finish", data: {} });
391
+ };
392
+
393
+ // src/ai/common/HFI_TextSummary.ts
394
+ var HFI_TextSummary_Stream = async (input, model, signal, emit) => {
395
+ const client = await getClient(model);
396
+ const modelName = getModelName(model);
397
+ const provider = getProvider(model);
398
+ const stream = client.chatCompletionStream({
399
+ model: modelName,
400
+ messages: [
401
+ { role: "system", content: "Summarize the following text concisely." },
402
+ { role: "user", content: input.text }
403
+ ],
404
+ provider
405
+ }, { signal });
406
+ for await (const chunk of stream) {
407
+ const delta = chunk.choices[0]?.delta?.content ?? "";
408
+ if (delta) {
409
+ emit({ type: "text-delta", port: "text", textDelta: delta });
410
+ }
411
+ }
412
+ emit({ type: "finish", data: {} });
413
+ };
414
+
415
+ // src/ai/common/HFI_ToolCalling.ts
416
+ import {
417
+ accumulateOpenAIStream,
418
+ buildOpenAITools,
419
+ mapOpenAIToolChoice
420
+ } from "@workglow/ai/provider-utils";
421
+ import { toOpenAIMessages as toOpenAIMessages2 } from "@workglow/ai/worker";
422
+ var HFI_ToolCalling_Stream = async (input, model, signal, emit) => {
423
+ const client = await getClient(model);
424
+ const modelName = getModelName(model);
425
+ const provider = getProvider(model);
426
+ const tools = buildOpenAITools(input.tools);
427
+ const messages = toOpenAIMessages2(input);
428
+ const toolChoice = mapOpenAIToolChoice(input.toolChoice, false);
429
+ const params = {
430
+ model: modelName,
431
+ messages,
432
+ max_tokens: input.maxTokens,
433
+ temperature: input.temperature,
434
+ provider,
435
+ stream: true
436
+ };
437
+ if (toolChoice !== "none") {
438
+ params.tools = tools;
439
+ params.tool_choice = toolChoice;
440
+ }
441
+ const stream = client.chatCompletionStream(params, { signal });
442
+ await accumulateOpenAIStream(stream, emit);
443
+ emit({ type: "finish", data: { text: "", toolCalls: [] } });
444
+ };
445
+
446
+ // src/ai/common/HFI_JobRunFns.ts
447
+ var HFI_TextGeneration_Unified = HFI_TextGeneration_Stream;
448
+ var HFI_RUN_FNS = [
449
+ { serves: HFI_TEXT_GENERATION, runFn: HFI_TextGeneration_Unified },
450
+ { serves: HFI_TOOL_USE, runFn: HFI_ToolCalling_Stream },
451
+ { serves: HFI_TEXT_REWRITER, runFn: HFI_TextRewriter_Stream },
452
+ { serves: HFI_TEXT_SUMMARY, runFn: HFI_TextSummary_Stream },
453
+ { serves: HFI_TEXT_EMBEDDING, runFn: HFI_TextEmbedding },
454
+ { serves: HFI_IMAGE_GENERATION, runFn: HFI_ImageGenerate_Stream },
455
+ { serves: HFI_IMAGE_EDITING, runFn: HFI_ImageEdit_Stream },
456
+ { serves: HFI_MODEL_SEARCH, runFn: HFI_ModelSearch },
457
+ { serves: HFI_MODEL_INFO, runFn: HFI_ModelInfo }
458
+ ];
459
+
460
+ // src/ai/HfInferenceQueuedProvider.ts
461
+ import { AiProvider } from "@workglow/ai";
462
+ import { createCloudProviderClass } from "@workglow/ai/provider-utils";
463
+
464
+ // src/ai/common/HFI_Capabilities.ts
465
+ var HFI_RUN_FN_SPECS = HFI_CAPABILITY_SETS.map((serves) => ({ serves }));
466
+ function hfInferenceWorkerRunFnSpecs() {
467
+ return HFI_RUN_FN_SPECS;
468
+ }
469
+ function inferHfInferenceCapabilities(model) {
470
+ const declared = model.capabilities ?? [];
471
+ if (declared.length > 0)
472
+ return declared;
473
+ const id = String(model.model_id ?? model.provider_config?.model_name ?? "");
474
+ const baseName = id.split("/").pop() ?? id;
475
+ if (/flux|stable-diffusion|sd-|sdxl|dall-e|kandinsky|wuerstchen|playground/i.test(baseName)) {
476
+ return ["image.generation", "image.editing", "model.info", "model.search"];
477
+ }
478
+ if (/embed|minilm|bge-|gte-|e5-/i.test(baseName)) {
479
+ return ["text.embedding", "model.info", "model.search"];
480
+ }
481
+ if (/llama|mistral|mixtral|gemma|phi|qwen|falcon|yi|deepseek|command|claude|gpt-/i.test(baseName)) {
482
+ return [
483
+ "text.generation",
484
+ "text.rewriter",
485
+ "text.summary",
486
+ "tool-use",
487
+ "model.info",
488
+ "model.search"
489
+ ];
490
+ }
491
+ return ["model.search", "model.info"];
492
+ }
493
+
494
+ // src/ai/HfInferenceQueuedProvider.ts
495
+ class HfInferenceQueuedProvider extends createCloudProviderClass(AiProvider, {
496
+ name: HF_INFERENCE,
497
+ displayName: "Hugging Face Inference"
498
+ }) {
499
+ inferCapabilities(model) {
500
+ return inferHfInferenceCapabilities(model);
501
+ }
502
+ workerRunFnSpecs() {
503
+ return hfInferenceWorkerRunFnSpecs();
504
+ }
505
+ }
506
+
507
+ // src/ai/registerHfInferenceInline.ts
508
+ async function registerHfInferenceInline(options) {
509
+ registerHfImageValidator();
510
+ await registerProviderInline(new HfInferenceQueuedProvider(HFI_RUN_FNS), "Hugging Face Inference", options);
511
+ }
512
+
513
+ // src/ai/registerHfInferenceWorker.ts
514
+ import { registerProviderWorker } from "@workglow/ai/provider-utils";
515
+
516
+ // src/ai/HfInferenceProvider.ts
517
+ import { createCloudProviderClass as createCloudProviderClass2 } from "@workglow/ai/provider-utils";
518
+ import { AiProvider as AiProvider2 } from "@workglow/ai/worker";
519
+ class HfInferenceProvider extends createCloudProviderClass2(AiProvider2, {
520
+ name: HF_INFERENCE,
521
+ displayName: "Hugging Face Inference"
522
+ }) {
523
+ inferCapabilities(model) {
524
+ return inferHfInferenceCapabilities(model);
525
+ }
526
+ workerRunFnSpecs() {
527
+ return hfInferenceWorkerRunFnSpecs();
528
+ }
529
+ }
530
+
531
+ // src/ai/registerHfInferenceWorker.ts
532
+ async function registerHfInferenceWorker() {
533
+ await registerProviderWorker((ws) => new HfInferenceProvider(HFI_RUN_FNS).registerOnWorkerServer(ws), "Hugging Face Inference");
534
+ }
535
+ export {
536
+ registerHfInferenceWorker,
537
+ registerHfInferenceInline,
538
+ loadHfInferenceSDK,
539
+ getProvider,
540
+ getModelName,
541
+ getClient
542
+ };
543
+
544
+ //# debugId=88BE26D07F1CA92964756E2164756E21