@workglow/huggingface-inference 0.3.15 → 0.3.17

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