@workglow/huggingface-transformers 0.2.28
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.
- package/dist/ai-provider/HuggingFaceTransformersProvider.d.ts +28 -0
- package/dist/ai-provider/HuggingFaceTransformersProvider.d.ts.map +1 -0
- package/dist/ai-provider/HuggingFaceTransformersQueuedProvider.d.ts +29 -0
- package/dist/ai-provider/HuggingFaceTransformersQueuedProvider.d.ts.map +1 -0
- package/dist/ai-provider/common/HFT_BackgroundRemoval.d.ts +12 -0
- package/dist/ai-provider/common/HFT_BackgroundRemoval.d.ts.map +1 -0
- package/dist/ai-provider/common/HFT_Chat.d.ts +10 -0
- package/dist/ai-provider/common/HFT_Chat.d.ts.map +1 -0
- package/dist/ai-provider/common/HFT_Constants.d.ts +95 -0
- package/dist/ai-provider/common/HFT_Constants.d.ts.map +1 -0
- package/dist/ai-provider/common/HFT_CountTokens.d.ts +10 -0
- package/dist/ai-provider/common/HFT_CountTokens.d.ts.map +1 -0
- package/dist/ai-provider/common/HFT_Download.d.ts +13 -0
- package/dist/ai-provider/common/HFT_Download.d.ts.map +1 -0
- package/dist/ai-provider/common/HFT_ImageClassification.d.ts +13 -0
- package/dist/ai-provider/common/HFT_ImageClassification.d.ts.map +1 -0
- package/dist/ai-provider/common/HFT_ImageEmbedding.d.ts +12 -0
- package/dist/ai-provider/common/HFT_ImageEmbedding.d.ts.map +1 -0
- package/dist/ai-provider/common/HFT_ImageSegmentation.d.ts +12 -0
- package/dist/ai-provider/common/HFT_ImageSegmentation.d.ts.map +1 -0
- package/dist/ai-provider/common/HFT_ImageToText.d.ts +12 -0
- package/dist/ai-provider/common/HFT_ImageToText.d.ts.map +1 -0
- package/dist/ai-provider/common/HFT_InlineLifecycle.d.ts +7 -0
- package/dist/ai-provider/common/HFT_InlineLifecycle.d.ts.map +1 -0
- package/dist/ai-provider/common/HFT_JobRunFns.d.ts +19 -0
- package/dist/ai-provider/common/HFT_JobRunFns.d.ts.map +1 -0
- package/dist/ai-provider/common/HFT_ModelInfo.d.ts +9 -0
- package/dist/ai-provider/common/HFT_ModelInfo.d.ts.map +1 -0
- package/dist/ai-provider/common/HFT_ModelSchema.d.ts +368 -0
- package/dist/ai-provider/common/HFT_ModelSchema.d.ts.map +1 -0
- package/dist/ai-provider/common/HFT_ModelSearch.d.ts +8 -0
- package/dist/ai-provider/common/HFT_ModelSearch.d.ts.map +1 -0
- package/dist/ai-provider/common/HFT_ObjectDetection.d.ts +13 -0
- package/dist/ai-provider/common/HFT_ObjectDetection.d.ts.map +1 -0
- package/dist/ai-provider/common/HFT_OnnxDtypes.d.ts +27 -0
- package/dist/ai-provider/common/HFT_OnnxDtypes.d.ts.map +1 -0
- package/dist/ai-provider/common/HFT_Pipeline.d.ts +62 -0
- package/dist/ai-provider/common/HFT_Pipeline.d.ts.map +1 -0
- package/dist/ai-provider/common/HFT_Streaming.d.ts +25 -0
- package/dist/ai-provider/common/HFT_Streaming.d.ts.map +1 -0
- package/dist/ai-provider/common/HFT_StructuredGeneration.d.ts +10 -0
- package/dist/ai-provider/common/HFT_StructuredGeneration.d.ts.map +1 -0
- package/dist/ai-provider/common/HFT_TextClassification.d.ts +9 -0
- package/dist/ai-provider/common/HFT_TextClassification.d.ts.map +1 -0
- package/dist/ai-provider/common/HFT_TextEmbedding.d.ts +13 -0
- package/dist/ai-provider/common/HFT_TextEmbedding.d.ts.map +1 -0
- package/dist/ai-provider/common/HFT_TextFillMask.d.ts +9 -0
- package/dist/ai-provider/common/HFT_TextFillMask.d.ts.map +1 -0
- package/dist/ai-provider/common/HFT_TextGeneration.d.ts +14 -0
- package/dist/ai-provider/common/HFT_TextGeneration.d.ts.map +1 -0
- package/dist/ai-provider/common/HFT_TextLanguageDetection.d.ts +9 -0
- package/dist/ai-provider/common/HFT_TextLanguageDetection.d.ts.map +1 -0
- package/dist/ai-provider/common/HFT_TextNamedEntityRecognition.d.ts +9 -0
- package/dist/ai-provider/common/HFT_TextNamedEntityRecognition.d.ts.map +1 -0
- package/dist/ai-provider/common/HFT_TextOutput.d.ts +8 -0
- package/dist/ai-provider/common/HFT_TextOutput.d.ts.map +1 -0
- package/dist/ai-provider/common/HFT_TextQuestionAnswer.d.ts +14 -0
- package/dist/ai-provider/common/HFT_TextQuestionAnswer.d.ts.map +1 -0
- package/dist/ai-provider/common/HFT_TextRewriter.d.ts +14 -0
- package/dist/ai-provider/common/HFT_TextRewriter.d.ts.map +1 -0
- package/dist/ai-provider/common/HFT_TextSummary.d.ts +14 -0
- package/dist/ai-provider/common/HFT_TextSummary.d.ts.map +1 -0
- package/dist/ai-provider/common/HFT_TextTranslation.d.ts +14 -0
- package/dist/ai-provider/common/HFT_TextTranslation.d.ts.map +1 -0
- package/dist/ai-provider/common/HFT_ToolCalling.d.ts +19 -0
- package/dist/ai-provider/common/HFT_ToolCalling.d.ts.map +1 -0
- package/dist/ai-provider/common/HFT_ToolMarkup.d.ts +20 -0
- package/dist/ai-provider/common/HFT_ToolMarkup.d.ts.map +1 -0
- package/dist/ai-provider/common/HFT_Unload.d.ts +13 -0
- package/dist/ai-provider/common/HFT_Unload.d.ts.map +1 -0
- package/dist/ai-provider/index.d.ts +13 -0
- package/dist/ai-provider/index.d.ts.map +1 -0
- package/dist/ai-provider/registerHuggingFaceTransformers.d.ts +14 -0
- package/dist/ai-provider/registerHuggingFaceTransformers.d.ts.map +1 -0
- package/dist/ai-provider/registerHuggingFaceTransformersInline.d.ts +15 -0
- package/dist/ai-provider/registerHuggingFaceTransformersInline.d.ts.map +1 -0
- package/dist/ai-provider/registerHuggingFaceTransformersWorker.d.ts +7 -0
- package/dist/ai-provider/registerHuggingFaceTransformersWorker.d.ts.map +1 -0
- package/dist/ai-provider/runtime.d.ts +21 -0
- package/dist/ai-provider/runtime.d.ts.map +1 -0
- package/dist/ai-provider-runtime.d.ts +7 -0
- package/dist/ai-provider-runtime.d.ts.map +1 -0
- package/dist/ai-provider-runtime.js +2367 -0
- package/dist/ai-provider-runtime.js.map +46 -0
- package/dist/ai-provider.d.ts +7 -0
- package/dist/ai-provider.d.ts.map +1 -0
- package/dist/ai-provider.js +879 -0
- package/dist/ai-provider.js.map +17 -0
- package/package.json +60 -0
|
@@ -0,0 +1,879 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __returnValue = (v) => v;
|
|
3
|
+
function __exportSetter(name, newValue) {
|
|
4
|
+
this[name] = __returnValue.bind(null, newValue);
|
|
5
|
+
}
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, {
|
|
9
|
+
get: all[name],
|
|
10
|
+
enumerable: true,
|
|
11
|
+
configurable: true,
|
|
12
|
+
set: __exportSetter.bind(all, name)
|
|
13
|
+
});
|
|
14
|
+
};
|
|
15
|
+
var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
|
|
16
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
17
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
18
|
+
}) : x)(function(x) {
|
|
19
|
+
if (typeof require !== "undefined")
|
|
20
|
+
return require.apply(this, arguments);
|
|
21
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
// src/ai-provider/common/HFT_Pipeline.ts
|
|
25
|
+
var exports_HFT_Pipeline = {};
|
|
26
|
+
__export(exports_HFT_Pipeline, {
|
|
27
|
+
setHftSession: () => setHftSession,
|
|
28
|
+
setHftCacheDir: () => setHftCacheDir,
|
|
29
|
+
removeCachedPipeline: () => removeCachedPipeline,
|
|
30
|
+
loadTransformersSDK: () => loadTransformersSDK,
|
|
31
|
+
hasCachedPipeline: () => hasCachedPipeline,
|
|
32
|
+
getPipelineCacheKey: () => getPipelineCacheKey,
|
|
33
|
+
getPipeline: () => getPipeline,
|
|
34
|
+
getHftSession: () => getHftSession,
|
|
35
|
+
disposeHftSessionsForModel: () => disposeHftSessionsForModel,
|
|
36
|
+
deleteHftSession: () => deleteHftSession,
|
|
37
|
+
clearPipelineCache: () => clearPipelineCache,
|
|
38
|
+
HFT_NULL_PROCESSOR_PREFIX: () => HFT_NULL_PROCESSOR_PREFIX
|
|
39
|
+
});
|
|
40
|
+
import { getLogger } from "@workglow/util/worker";
|
|
41
|
+
function setHftCacheDir(dir) {
|
|
42
|
+
_cacheDir = dir;
|
|
43
|
+
if (_transformersSdk) {
|
|
44
|
+
_transformersSdk.env.cacheDir = dir;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
async function loadTransformersSDK() {
|
|
48
|
+
if (!_transformersSdk) {
|
|
49
|
+
try {
|
|
50
|
+
_transformersSdk = await import("@huggingface/transformers");
|
|
51
|
+
_transformersSdk.env.fetch = abortableFetch;
|
|
52
|
+
if (_cacheDir) {
|
|
53
|
+
_transformersSdk.env.cacheDir = _cacheDir;
|
|
54
|
+
}
|
|
55
|
+
} catch {
|
|
56
|
+
throw new Error("@huggingface/transformers is required for HuggingFace Transformers tasks. Install it with: bun add @huggingface/transformers");
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return _transformersSdk;
|
|
60
|
+
}
|
|
61
|
+
function combineAbortSignals(existingSignal, modelSignal) {
|
|
62
|
+
if (!existingSignal) {
|
|
63
|
+
return modelSignal;
|
|
64
|
+
}
|
|
65
|
+
if (!modelSignal) {
|
|
66
|
+
return existingSignal;
|
|
67
|
+
}
|
|
68
|
+
if (existingSignal.aborted || modelSignal.aborted) {
|
|
69
|
+
return AbortSignal.abort(existingSignal.reason ?? modelSignal.reason);
|
|
70
|
+
}
|
|
71
|
+
if (typeof AbortSignal.any === "function") {
|
|
72
|
+
return AbortSignal.any([existingSignal, modelSignal]);
|
|
73
|
+
}
|
|
74
|
+
const controller = new AbortController;
|
|
75
|
+
const abort = (event) => {
|
|
76
|
+
const signal = event.target;
|
|
77
|
+
controller.abort(signal.reason);
|
|
78
|
+
};
|
|
79
|
+
existingSignal.addEventListener("abort", abort, { once: true });
|
|
80
|
+
modelSignal.addEventListener("abort", abort, { once: true });
|
|
81
|
+
return controller.signal;
|
|
82
|
+
}
|
|
83
|
+
function createAbortError(signal) {
|
|
84
|
+
const reason = signal.reason;
|
|
85
|
+
if (reason instanceof Error) {
|
|
86
|
+
return reason;
|
|
87
|
+
}
|
|
88
|
+
return new Error(String(reason ?? "Fetch aborted"));
|
|
89
|
+
}
|
|
90
|
+
function wrapAbortableResponse(response, signal) {
|
|
91
|
+
if (!signal || !response.body) {
|
|
92
|
+
return response;
|
|
93
|
+
}
|
|
94
|
+
const contentLengthHeader = response.headers.get("content-length");
|
|
95
|
+
const expectedSize = contentLengthHeader && /^\d+$/.test(contentLengthHeader) ? Number.parseInt(contentLengthHeader, 10) : undefined;
|
|
96
|
+
const sourceBody = response.body;
|
|
97
|
+
let reader;
|
|
98
|
+
let abortHandler;
|
|
99
|
+
let loaded = 0;
|
|
100
|
+
const cleanup = () => {
|
|
101
|
+
if (abortHandler) {
|
|
102
|
+
signal.removeEventListener("abort", abortHandler);
|
|
103
|
+
abortHandler = undefined;
|
|
104
|
+
}
|
|
105
|
+
reader?.releaseLock();
|
|
106
|
+
};
|
|
107
|
+
const body = new ReadableStream({
|
|
108
|
+
start(controller) {
|
|
109
|
+
reader = sourceBody.getReader();
|
|
110
|
+
if (signal.aborted) {
|
|
111
|
+
controller.error(createAbortError(signal));
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
abortHandler = () => controller.error(createAbortError(signal));
|
|
115
|
+
signal.addEventListener("abort", abortHandler, { once: true });
|
|
116
|
+
},
|
|
117
|
+
async pull(controller) {
|
|
118
|
+
try {
|
|
119
|
+
if (signal.aborted) {
|
|
120
|
+
throw createAbortError(signal);
|
|
121
|
+
}
|
|
122
|
+
const { done, value } = await reader.read();
|
|
123
|
+
if (done) {
|
|
124
|
+
if (signal.aborted) {
|
|
125
|
+
throw createAbortError(signal);
|
|
126
|
+
}
|
|
127
|
+
if (expectedSize !== undefined && loaded < expectedSize) {
|
|
128
|
+
throw new Error(`Fetch ended before reading the full response body (${loaded}/${expectedSize} bytes)`);
|
|
129
|
+
}
|
|
130
|
+
cleanup();
|
|
131
|
+
controller.close();
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
loaded += value.length;
|
|
135
|
+
controller.enqueue(value);
|
|
136
|
+
} catch (error) {
|
|
137
|
+
cleanup();
|
|
138
|
+
controller.error(error);
|
|
139
|
+
}
|
|
140
|
+
},
|
|
141
|
+
cancel(reason) {
|
|
142
|
+
cleanup();
|
|
143
|
+
return sourceBody.cancel(reason);
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
return new Response(body, {
|
|
147
|
+
headers: new Headers(response.headers),
|
|
148
|
+
status: response.status,
|
|
149
|
+
statusText: response.statusText
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
function abortableFetch(url, options) {
|
|
153
|
+
let modelSignal;
|
|
154
|
+
try {
|
|
155
|
+
const pathname = new URL(url).pathname;
|
|
156
|
+
for (const [modelPath, controller] of modelAbortControllers) {
|
|
157
|
+
if (pathname.includes(`/${modelPath}/`)) {
|
|
158
|
+
modelSignal = controller.signal;
|
|
159
|
+
break;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
} catch {}
|
|
163
|
+
const combinedSignal = options?.signal ? combineAbortSignals(options.signal, modelSignal) : modelSignal;
|
|
164
|
+
return fetch(url, { ...options, ...combinedSignal ? { signal: combinedSignal } : {} }).then((response) => wrapAbortableResponse(response, combinedSignal));
|
|
165
|
+
}
|
|
166
|
+
function getHftSession(sessionId) {
|
|
167
|
+
return hftSessions.get(sessionId);
|
|
168
|
+
}
|
|
169
|
+
function setHftSession(sessionId, state) {
|
|
170
|
+
hftSessions.set(sessionId, state);
|
|
171
|
+
}
|
|
172
|
+
function disposeSessionResources(session) {
|
|
173
|
+
if (session.mode === "progressive") {
|
|
174
|
+
if (session.cache?.dispose) {
|
|
175
|
+
session.cache.dispose();
|
|
176
|
+
}
|
|
177
|
+
} else {
|
|
178
|
+
for (const tensor of Object.values(session.baseEntries)) {
|
|
179
|
+
if (tensor?.location === "gpu-buffer" && typeof tensor.dispose === "function") {
|
|
180
|
+
tensor.dispose();
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
function deleteHftSession(sessionId) {
|
|
186
|
+
const session = hftSessions.get(sessionId);
|
|
187
|
+
if (session) {
|
|
188
|
+
disposeSessionResources(session);
|
|
189
|
+
}
|
|
190
|
+
return hftSessions.delete(sessionId);
|
|
191
|
+
}
|
|
192
|
+
function disposeHftSessionsForModel(modelPath) {
|
|
193
|
+
for (const [id, state] of hftSessions) {
|
|
194
|
+
if (state.modelPath === modelPath) {
|
|
195
|
+
disposeSessionResources(state);
|
|
196
|
+
hftSessions.delete(id);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
function clearPipelineCache() {
|
|
201
|
+
pipelines.clear();
|
|
202
|
+
}
|
|
203
|
+
function hasCachedPipeline(cacheKey) {
|
|
204
|
+
return pipelines.has(cacheKey);
|
|
205
|
+
}
|
|
206
|
+
function removeCachedPipeline(cacheKey) {
|
|
207
|
+
return pipelines.delete(cacheKey);
|
|
208
|
+
}
|
|
209
|
+
function isBrowserEnv() {
|
|
210
|
+
if (typeof globalThis === "undefined")
|
|
211
|
+
return false;
|
|
212
|
+
if (typeof globalThis.window !== "undefined")
|
|
213
|
+
return true;
|
|
214
|
+
if (typeof globalThis.WorkerGlobalScope !== "undefined")
|
|
215
|
+
return true;
|
|
216
|
+
return false;
|
|
217
|
+
}
|
|
218
|
+
function getPipelineCacheKey(model) {
|
|
219
|
+
const dtype = model.provider_config.dtype || "";
|
|
220
|
+
const device = model.provider_config.device || "";
|
|
221
|
+
const revision = model.provider_config.revision || "main";
|
|
222
|
+
return `${model.provider_config.model_path}:${model.provider_config.pipeline}:${dtype}:${device}:${revision}`;
|
|
223
|
+
}
|
|
224
|
+
async function getPipeline(model, onProgress, options = {}, signal, progressScaleMax = 10) {
|
|
225
|
+
const cacheKey = getPipelineCacheKey(model);
|
|
226
|
+
if (pipelines.has(cacheKey)) {
|
|
227
|
+
getLogger().debug("HFT pipeline cache hit", { cacheKey });
|
|
228
|
+
return pipelines.get(cacheKey);
|
|
229
|
+
}
|
|
230
|
+
const inFlight = pipelineLoadPromises.get(cacheKey);
|
|
231
|
+
if (inFlight) {
|
|
232
|
+
try {
|
|
233
|
+
await inFlight;
|
|
234
|
+
} catch {}
|
|
235
|
+
const cached = pipelines.get(cacheKey);
|
|
236
|
+
if (cached)
|
|
237
|
+
return cached;
|
|
238
|
+
}
|
|
239
|
+
const loadPromise = doGetPipeline(model, onProgress, options, progressScaleMax, cacheKey, signal).finally(() => {
|
|
240
|
+
pipelineLoadPromises.delete(cacheKey);
|
|
241
|
+
});
|
|
242
|
+
pipelineLoadPromises.set(cacheKey, loadPromise);
|
|
243
|
+
return loadPromise;
|
|
244
|
+
}
|
|
245
|
+
var _transformersSdk, _cacheDir, modelAbortControllers, pipelines, hftSessions, pipelineLoadPromises, IMAGE_PIPELINE_TYPES, HFT_NULL_PROCESSOR_PREFIX = "HFT_NULL_PROCESSOR:", doGetPipeline = async (model, onProgress, options, progressScaleMax, cacheKey, signal) => {
|
|
246
|
+
let lastProgressTime = 0;
|
|
247
|
+
let pendingProgress = null;
|
|
248
|
+
let throttleTimer = null;
|
|
249
|
+
const THROTTLE_MS = 160;
|
|
250
|
+
const buildProgressDetails = (file, fileProgress, filesMap) => {
|
|
251
|
+
const details = {
|
|
252
|
+
file,
|
|
253
|
+
progress: fileProgress
|
|
254
|
+
};
|
|
255
|
+
if (filesMap && Object.keys(filesMap).length > 0) {
|
|
256
|
+
details.files = filesMap;
|
|
257
|
+
}
|
|
258
|
+
return details;
|
|
259
|
+
};
|
|
260
|
+
const sendProgress = (progress, file, fileProgress, filesMap) => {
|
|
261
|
+
const now = Date.now();
|
|
262
|
+
const timeSinceLastEvent = now - lastProgressTime;
|
|
263
|
+
const isFirst = lastProgressTime === 0;
|
|
264
|
+
const isFinal = progress >= progressScaleMax;
|
|
265
|
+
if (isFirst || isFinal) {
|
|
266
|
+
if (throttleTimer) {
|
|
267
|
+
clearTimeout(throttleTimer);
|
|
268
|
+
throttleTimer = null;
|
|
269
|
+
}
|
|
270
|
+
pendingProgress = null;
|
|
271
|
+
onProgress(Math.round(progress), "Downloading model", buildProgressDetails(file, fileProgress, filesMap));
|
|
272
|
+
lastProgressTime = now;
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
if (timeSinceLastEvent < THROTTLE_MS) {
|
|
276
|
+
pendingProgress = { progress, file, fileProgress, filesMap };
|
|
277
|
+
if (!throttleTimer) {
|
|
278
|
+
const timeRemaining = Math.max(1, THROTTLE_MS - timeSinceLastEvent);
|
|
279
|
+
throttleTimer = setTimeout(() => {
|
|
280
|
+
throttleTimer = null;
|
|
281
|
+
if (pendingProgress) {
|
|
282
|
+
const p = pendingProgress;
|
|
283
|
+
onProgress(Math.round(p.progress), "Downloading model", buildProgressDetails(p.file, p.fileProgress, p.filesMap));
|
|
284
|
+
lastProgressTime = Date.now();
|
|
285
|
+
pendingProgress = null;
|
|
286
|
+
}
|
|
287
|
+
}, timeRemaining);
|
|
288
|
+
}
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
onProgress(Math.round(progress), "Downloading model", buildProgressDetails(file, fileProgress, filesMap));
|
|
292
|
+
lastProgressTime = now;
|
|
293
|
+
pendingProgress = null;
|
|
294
|
+
};
|
|
295
|
+
const abortSignal = signal;
|
|
296
|
+
const modelPath = model.provider_config.model_path;
|
|
297
|
+
const modelController = new AbortController;
|
|
298
|
+
modelAbortControllers.set(modelPath, modelController);
|
|
299
|
+
if (abortSignal) {
|
|
300
|
+
if (abortSignal.aborted) {
|
|
301
|
+
modelController.abort();
|
|
302
|
+
} else {
|
|
303
|
+
abortSignal.addEventListener("abort", () => modelController.abort(), { once: true });
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
const progressCallback = (status) => {
|
|
307
|
+
if (abortSignal?.aborted)
|
|
308
|
+
return;
|
|
309
|
+
if (status.status === "progress_total") {
|
|
310
|
+
const totalStatus = status;
|
|
311
|
+
const scaledProgress = totalStatus.progress * progressScaleMax / 100;
|
|
312
|
+
let activeFile = "";
|
|
313
|
+
let activeFileProgress = 0;
|
|
314
|
+
const files = totalStatus.files;
|
|
315
|
+
if (files) {
|
|
316
|
+
for (const [file, info] of Object.entries(files)) {
|
|
317
|
+
if (info.loaded < info.total) {
|
|
318
|
+
activeFile = file;
|
|
319
|
+
activeFileProgress = info.total > 0 ? info.loaded / info.total * 100 : 0;
|
|
320
|
+
break;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
if (!activeFile) {
|
|
324
|
+
const fileNames = Object.keys(files);
|
|
325
|
+
if (fileNames.length > 0) {
|
|
326
|
+
activeFile = fileNames[fileNames.length - 1];
|
|
327
|
+
activeFileProgress = 100;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
sendProgress(scaledProgress, activeFile, activeFileProgress, files);
|
|
332
|
+
}
|
|
333
|
+
};
|
|
334
|
+
let device = model.provider_config.device;
|
|
335
|
+
if (isBrowserEnv()) {
|
|
336
|
+
if (device === "gpu") {
|
|
337
|
+
device = "webgpu";
|
|
338
|
+
}
|
|
339
|
+
if (device === "cpu") {
|
|
340
|
+
device = "wasm";
|
|
341
|
+
}
|
|
342
|
+
if (device !== "wasm" && device !== "webgpu") {
|
|
343
|
+
device = "wasm";
|
|
344
|
+
}
|
|
345
|
+
} else {
|
|
346
|
+
if (device === "wasm" || device === "webgpu") {
|
|
347
|
+
device = undefined;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
const dtype = model.provider_config.dtype || "";
|
|
351
|
+
const pipelineOptions = {
|
|
352
|
+
revision: model.provider_config.revision || "main",
|
|
353
|
+
...model.provider_config.use_external_data_format ? { useExternalDataFormat: model.provider_config.use_external_data_format } : {},
|
|
354
|
+
...dtype ? { dtype } : {},
|
|
355
|
+
...device ? { device } : {},
|
|
356
|
+
...options,
|
|
357
|
+
progress_callback: progressCallback
|
|
358
|
+
};
|
|
359
|
+
if (abortSignal?.aborted) {
|
|
360
|
+
modelAbortControllers.delete(modelPath);
|
|
361
|
+
throw new Error("Operation aborted before pipeline creation");
|
|
362
|
+
}
|
|
363
|
+
const pipelineType = model.provider_config.pipeline;
|
|
364
|
+
const { pipeline } = await loadTransformersSDK();
|
|
365
|
+
const logger = getLogger();
|
|
366
|
+
const pipelineTimerLabel = `hft:pipeline:${cacheKey}`;
|
|
367
|
+
logger.time(pipelineTimerLabel, { pipelineType, modelPath });
|
|
368
|
+
try {
|
|
369
|
+
const result = await pipeline(pipelineType, model.provider_config.model_path, pipelineOptions);
|
|
370
|
+
if (throttleTimer) {
|
|
371
|
+
clearTimeout(throttleTimer);
|
|
372
|
+
throttleTimer = null;
|
|
373
|
+
}
|
|
374
|
+
const finalPending = pendingProgress;
|
|
375
|
+
if (finalPending) {
|
|
376
|
+
onProgress(Math.round(finalPending.progress), "Downloading model", buildProgressDetails(finalPending.file, finalPending.fileProgress, finalPending.filesMap));
|
|
377
|
+
pendingProgress = null;
|
|
378
|
+
}
|
|
379
|
+
if (abortSignal?.aborted) {
|
|
380
|
+
logger.timeEnd(pipelineTimerLabel, { status: "aborted" });
|
|
381
|
+
throw new Error("Operation aborted after pipeline creation");
|
|
382
|
+
}
|
|
383
|
+
if (IMAGE_PIPELINE_TYPES.has(pipelineType) && result.processor == null) {
|
|
384
|
+
throw new Error(`${HFT_NULL_PROCESSOR_PREFIX} Image processor not initialized for ${pipelineType}/${modelPath}. Model cache may be incomplete.`);
|
|
385
|
+
}
|
|
386
|
+
logger.timeEnd(pipelineTimerLabel, { status: "loaded" });
|
|
387
|
+
pipelines.set(cacheKey, result);
|
|
388
|
+
return result;
|
|
389
|
+
} catch (error) {
|
|
390
|
+
logger.timeEnd(pipelineTimerLabel, { status: "error", error: String(error) });
|
|
391
|
+
if (!error?.message?.startsWith(HFT_NULL_PROCESSOR_PREFIX) && (abortSignal?.aborted || modelController.signal.aborted)) {
|
|
392
|
+
throw new Error("Pipeline download aborted");
|
|
393
|
+
}
|
|
394
|
+
throw error;
|
|
395
|
+
} finally {
|
|
396
|
+
modelAbortControllers.delete(modelPath);
|
|
397
|
+
const { random } = await loadTransformersSDK();
|
|
398
|
+
random.seed(model.provider_config.seed ?? undefined);
|
|
399
|
+
}
|
|
400
|
+
};
|
|
401
|
+
var init_HFT_Pipeline = __esm(() => {
|
|
402
|
+
modelAbortControllers = new Map;
|
|
403
|
+
pipelines = new Map;
|
|
404
|
+
hftSessions = new Map;
|
|
405
|
+
pipelineLoadPromises = new Map;
|
|
406
|
+
IMAGE_PIPELINE_TYPES = new Set([
|
|
407
|
+
"image-classification",
|
|
408
|
+
"image-segmentation",
|
|
409
|
+
"object-detection",
|
|
410
|
+
"image-to-text",
|
|
411
|
+
"image-feature-extraction",
|
|
412
|
+
"zero-shot-image-classification",
|
|
413
|
+
"depth-estimation",
|
|
414
|
+
"mask-generation"
|
|
415
|
+
]);
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
// src/ai-provider/common/HFT_Constants.ts
|
|
419
|
+
var HF_TRANSFORMERS_ONNX = "HF_TRANSFORMERS_ONNX";
|
|
420
|
+
var HF_TRANSFORMERS_ONNX_GPU = `${HF_TRANSFORMERS_ONNX}_gpu`;
|
|
421
|
+
var HF_TRANSFORMERS_ONNX_CPU = `${HF_TRANSFORMERS_ONNX}_cpu`;
|
|
422
|
+
var HTF_CACHE_NAME = "transformers-cache";
|
|
423
|
+
var QuantizationDataType = {
|
|
424
|
+
auto: "auto",
|
|
425
|
+
fp32: "fp32",
|
|
426
|
+
fp16: "fp16",
|
|
427
|
+
q8: "q8",
|
|
428
|
+
int8: "int8",
|
|
429
|
+
uint8: "uint8",
|
|
430
|
+
q4: "q4",
|
|
431
|
+
bnb4: "bnb4",
|
|
432
|
+
q4f16: "q4f16",
|
|
433
|
+
q2: "q2",
|
|
434
|
+
q2f16: "q2f16",
|
|
435
|
+
q1: "q1",
|
|
436
|
+
q1f16: "q1f16"
|
|
437
|
+
};
|
|
438
|
+
var TextPipelineUseCase = {
|
|
439
|
+
"fill-mask": "fill-mask",
|
|
440
|
+
"token-classification": "token-classification",
|
|
441
|
+
"text-generation": "text-generation",
|
|
442
|
+
"text2text-generation": "text2text-generation",
|
|
443
|
+
"text-classification": "text-classification",
|
|
444
|
+
summarization: "summarization",
|
|
445
|
+
translation: "translation",
|
|
446
|
+
"feature-extraction": "feature-extraction",
|
|
447
|
+
"zero-shot-classification": "zero-shot-classification",
|
|
448
|
+
"question-answering": "question-answering"
|
|
449
|
+
};
|
|
450
|
+
var VisionPipelineUseCase = {
|
|
451
|
+
"background-removal": "background-removal",
|
|
452
|
+
"image-segmentation": "image-segmentation",
|
|
453
|
+
"depth-estimation": "depth-estimation",
|
|
454
|
+
"image-classification": "image-classification",
|
|
455
|
+
"image-to-image": "image-to-image",
|
|
456
|
+
"image-to-text": "image-to-text",
|
|
457
|
+
"object-detection": "object-detection",
|
|
458
|
+
"image-feature-extraction": "image-feature-extraction"
|
|
459
|
+
};
|
|
460
|
+
var AudioPipelineUseCase = {
|
|
461
|
+
"audio-classification": "audio-classification",
|
|
462
|
+
"automatic-speech-recognition": "automatic-speech-recognition",
|
|
463
|
+
"text-to-speech": "text-to-speech"
|
|
464
|
+
};
|
|
465
|
+
var MultimodalPipelineUseCase = {
|
|
466
|
+
"document-question-answering": "document-question-answering",
|
|
467
|
+
"image-to-text": "image-to-text",
|
|
468
|
+
"zero-shot-audio-classification": "zero-shot-audio-classification",
|
|
469
|
+
"zero-shot-image-classification": "zero-shot-image-classification",
|
|
470
|
+
"zero-shot-object-detection": "zero-shot-object-detection"
|
|
471
|
+
};
|
|
472
|
+
var PipelineUseCase = {
|
|
473
|
+
...TextPipelineUseCase,
|
|
474
|
+
...VisionPipelineUseCase,
|
|
475
|
+
...AudioPipelineUseCase,
|
|
476
|
+
...MultimodalPipelineUseCase
|
|
477
|
+
};
|
|
478
|
+
|
|
479
|
+
// src/ai-provider/common/HFT_ModelSchema.ts
|
|
480
|
+
import { ModelConfigSchema, ModelRecordSchema } from "@workglow/ai/worker";
|
|
481
|
+
var HfTransformersOnnxModelSchema = {
|
|
482
|
+
type: "object",
|
|
483
|
+
properties: {
|
|
484
|
+
provider: {
|
|
485
|
+
const: HF_TRANSFORMERS_ONNX,
|
|
486
|
+
description: "Discriminator: ONNX runtime backend."
|
|
487
|
+
},
|
|
488
|
+
provider_config: {
|
|
489
|
+
type: "object",
|
|
490
|
+
description: "ONNX runtime-specific options.",
|
|
491
|
+
properties: {
|
|
492
|
+
pipeline: {
|
|
493
|
+
type: "string",
|
|
494
|
+
enum: Object.values(PipelineUseCase),
|
|
495
|
+
description: "Pipeline type for the ONNX model.",
|
|
496
|
+
default: "text-generation"
|
|
497
|
+
},
|
|
498
|
+
model_path: {
|
|
499
|
+
type: "string",
|
|
500
|
+
description: "Filesystem path or URI for the ONNX model."
|
|
501
|
+
},
|
|
502
|
+
revision: {
|
|
503
|
+
type: "string",
|
|
504
|
+
description: "Git revision (branch, tag, or commit hash) of the model repository.",
|
|
505
|
+
default: "main"
|
|
506
|
+
},
|
|
507
|
+
dtype: {
|
|
508
|
+
type: "string",
|
|
509
|
+
enum: Object.values(QuantizationDataType),
|
|
510
|
+
description: "Data type for the ONNX model.",
|
|
511
|
+
default: "auto"
|
|
512
|
+
},
|
|
513
|
+
device: {
|
|
514
|
+
type: "string",
|
|
515
|
+
enum: ["cpu", "gpu", "webgpu", "wasm", "metal"],
|
|
516
|
+
description: "High-level device selection.",
|
|
517
|
+
default: "webgpu"
|
|
518
|
+
},
|
|
519
|
+
execution_providers: {
|
|
520
|
+
type: "array",
|
|
521
|
+
items: { type: "string" },
|
|
522
|
+
description: "Raw ONNX Runtime execution provider identifiers.",
|
|
523
|
+
"x-ui-hidden": true
|
|
524
|
+
},
|
|
525
|
+
intra_op_num_threads: {
|
|
526
|
+
type: "integer",
|
|
527
|
+
minimum: 1
|
|
528
|
+
},
|
|
529
|
+
inter_op_num_threads: {
|
|
530
|
+
type: "integer",
|
|
531
|
+
minimum: 1
|
|
532
|
+
},
|
|
533
|
+
use_external_data_format: {
|
|
534
|
+
type: "boolean",
|
|
535
|
+
description: "Whether the model uses external data format."
|
|
536
|
+
},
|
|
537
|
+
native_dimensions: {
|
|
538
|
+
type: "integer",
|
|
539
|
+
description: "The native dimensions of the model."
|
|
540
|
+
},
|
|
541
|
+
pooling: {
|
|
542
|
+
type: "string",
|
|
543
|
+
enum: ["mean", "last_token", "cls"],
|
|
544
|
+
description: "The pooling strategy to use for the model.",
|
|
545
|
+
default: "mean"
|
|
546
|
+
},
|
|
547
|
+
normalize: {
|
|
548
|
+
type: "boolean",
|
|
549
|
+
description: "Whether the model uses normalization.",
|
|
550
|
+
default: true
|
|
551
|
+
},
|
|
552
|
+
language_style: {
|
|
553
|
+
type: "string",
|
|
554
|
+
description: "The language style of the model."
|
|
555
|
+
},
|
|
556
|
+
seed: {
|
|
557
|
+
type: "integer",
|
|
558
|
+
description: "RNG seed passed to transformers.js sampling. Omit for time-based seeding; set for reproducible generation.",
|
|
559
|
+
minimum: 0
|
|
560
|
+
},
|
|
561
|
+
mrl: {
|
|
562
|
+
type: "boolean",
|
|
563
|
+
description: "Whether the model uses matryoshka.",
|
|
564
|
+
default: false
|
|
565
|
+
}
|
|
566
|
+
},
|
|
567
|
+
required: ["model_path", "pipeline"],
|
|
568
|
+
additionalProperties: false,
|
|
569
|
+
if: {
|
|
570
|
+
properties: {
|
|
571
|
+
pipeline: {
|
|
572
|
+
const: "feature-extraction"
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
},
|
|
576
|
+
then: {
|
|
577
|
+
required: ["native_dimensions"]
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
},
|
|
581
|
+
required: ["provider", "provider_config"],
|
|
582
|
+
additionalProperties: true
|
|
583
|
+
};
|
|
584
|
+
var HfTransformersOnnxModelRecordSchema = {
|
|
585
|
+
type: "object",
|
|
586
|
+
properties: {
|
|
587
|
+
...ModelRecordSchema.properties,
|
|
588
|
+
...HfTransformersOnnxModelSchema.properties
|
|
589
|
+
},
|
|
590
|
+
required: [...ModelRecordSchema.required, ...HfTransformersOnnxModelSchema.required],
|
|
591
|
+
additionalProperties: false
|
|
592
|
+
};
|
|
593
|
+
var HfTransformersOnnxModelConfigSchema = {
|
|
594
|
+
type: "object",
|
|
595
|
+
properties: {
|
|
596
|
+
...ModelConfigSchema.properties,
|
|
597
|
+
...HfTransformersOnnxModelSchema.properties
|
|
598
|
+
},
|
|
599
|
+
required: [...ModelConfigSchema.required, ...HfTransformersOnnxModelSchema.required],
|
|
600
|
+
additionalProperties: false
|
|
601
|
+
};
|
|
602
|
+
|
|
603
|
+
// src/ai-provider/HuggingFaceTransformersProvider.ts
|
|
604
|
+
import { AiProvider } from "@workglow/ai/worker";
|
|
605
|
+
init_HFT_Pipeline();
|
|
606
|
+
|
|
607
|
+
class HuggingFaceTransformersProvider extends AiProvider {
|
|
608
|
+
name = HF_TRANSFORMERS_ONNX;
|
|
609
|
+
displayName = "Hugging Face Transformers (ONNX)";
|
|
610
|
+
isLocal = true;
|
|
611
|
+
supportsBrowser = true;
|
|
612
|
+
taskTypes = [
|
|
613
|
+
"AiChatTask",
|
|
614
|
+
"DownloadModelTask",
|
|
615
|
+
"UnloadModelTask",
|
|
616
|
+
"ModelInfoTask",
|
|
617
|
+
"CountTokensTask",
|
|
618
|
+
"TextEmbeddingTask",
|
|
619
|
+
"TextGenerationTask",
|
|
620
|
+
"TextQuestionAnswerTask",
|
|
621
|
+
"TextLanguageDetectionTask",
|
|
622
|
+
"TextClassificationTask",
|
|
623
|
+
"TextFillMaskTask",
|
|
624
|
+
"TextNamedEntityRecognitionTask",
|
|
625
|
+
"TextRewriterTask",
|
|
626
|
+
"TextSummaryTask",
|
|
627
|
+
"TextTranslationTask",
|
|
628
|
+
"ImageSegmentationTask",
|
|
629
|
+
"ImageToTextTask",
|
|
630
|
+
"BackgroundRemovalTask",
|
|
631
|
+
"ImageEmbeddingTask",
|
|
632
|
+
"ImageClassificationTask",
|
|
633
|
+
"ObjectDetectionTask",
|
|
634
|
+
"ToolCallingTask",
|
|
635
|
+
"StructuredGenerationTask",
|
|
636
|
+
"ModelSearchTask"
|
|
637
|
+
];
|
|
638
|
+
constructor(tasks, streamTasks, previewTasks) {
|
|
639
|
+
super(tasks, streamTasks, previewTasks);
|
|
640
|
+
}
|
|
641
|
+
createSession(_model) {
|
|
642
|
+
return crypto.randomUUID();
|
|
643
|
+
}
|
|
644
|
+
async disposeSession(sessionId) {
|
|
645
|
+
deleteHftSession(sessionId);
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
// src/ai-provider/HuggingFaceTransformersQueuedProvider.ts
|
|
650
|
+
import { QueuedAiProvider } from "@workglow/ai";
|
|
651
|
+
init_HFT_Pipeline();
|
|
652
|
+
var GPU_DEVICES = new Set(["webgpu", "gpu", "metal"]);
|
|
653
|
+
var HFT_CPU_QUEUE_CONCURRENCY_PRODUCTION = 4;
|
|
654
|
+
function hftIsAutomatedTestEnvironment() {
|
|
655
|
+
if (typeof process === "undefined") {
|
|
656
|
+
return false;
|
|
657
|
+
}
|
|
658
|
+
const e = process.env;
|
|
659
|
+
return e.VITEST === "true" || e.NODE_ENV === "test" || e.BUN_TEST === "1" || e.JEST_WORKER_ID !== undefined;
|
|
660
|
+
}
|
|
661
|
+
function hftDefaultCpuQueueConcurrency() {
|
|
662
|
+
return hftIsAutomatedTestEnvironment() ? 1 : HFT_CPU_QUEUE_CONCURRENCY_PRODUCTION;
|
|
663
|
+
}
|
|
664
|
+
function resolveHftCpuQueueConcurrency(concurrency, defaultCpu) {
|
|
665
|
+
if (concurrency === undefined) {
|
|
666
|
+
return defaultCpu();
|
|
667
|
+
}
|
|
668
|
+
if (typeof concurrency === "number") {
|
|
669
|
+
return defaultCpu();
|
|
670
|
+
}
|
|
671
|
+
return concurrency.cpu ?? defaultCpu();
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
class HuggingFaceTransformersQueuedProvider extends QueuedAiProvider {
|
|
675
|
+
name = HF_TRANSFORMERS_ONNX;
|
|
676
|
+
displayName = "Hugging Face Transformers (ONNX)";
|
|
677
|
+
isLocal = true;
|
|
678
|
+
supportsBrowser = true;
|
|
679
|
+
cpuStrategy;
|
|
680
|
+
taskTypes = [
|
|
681
|
+
"AiChatTask",
|
|
682
|
+
"DownloadModelTask",
|
|
683
|
+
"UnloadModelTask",
|
|
684
|
+
"ModelInfoTask",
|
|
685
|
+
"CountTokensTask",
|
|
686
|
+
"TextEmbeddingTask",
|
|
687
|
+
"TextGenerationTask",
|
|
688
|
+
"TextQuestionAnswerTask",
|
|
689
|
+
"TextLanguageDetectionTask",
|
|
690
|
+
"TextClassificationTask",
|
|
691
|
+
"TextFillMaskTask",
|
|
692
|
+
"TextNamedEntityRecognitionTask",
|
|
693
|
+
"TextRewriterTask",
|
|
694
|
+
"TextSummaryTask",
|
|
695
|
+
"TextTranslationTask",
|
|
696
|
+
"ImageSegmentationTask",
|
|
697
|
+
"ImageToTextTask",
|
|
698
|
+
"BackgroundRemovalTask",
|
|
699
|
+
"ImageEmbeddingTask",
|
|
700
|
+
"ImageClassificationTask",
|
|
701
|
+
"ObjectDetectionTask",
|
|
702
|
+
"ToolCallingTask",
|
|
703
|
+
"StructuredGenerationTask",
|
|
704
|
+
"ModelSearchTask"
|
|
705
|
+
];
|
|
706
|
+
constructor(tasks, streamTasks, previewTasks) {
|
|
707
|
+
super(tasks, streamTasks, previewTasks);
|
|
708
|
+
}
|
|
709
|
+
createSession(_model) {
|
|
710
|
+
return crypto.randomUUID();
|
|
711
|
+
}
|
|
712
|
+
async disposeSession(sessionId) {
|
|
713
|
+
deleteHftSession(sessionId);
|
|
714
|
+
}
|
|
715
|
+
async afterRegister(options) {
|
|
716
|
+
await super.afterRegister(options);
|
|
717
|
+
this.cpuStrategy = this.createQueuedStrategy(HF_TRANSFORMERS_ONNX_CPU, resolveHftCpuQueueConcurrency(options.queue?.concurrency, hftDefaultCpuQueueConcurrency), options);
|
|
718
|
+
}
|
|
719
|
+
getStrategyForModel(model) {
|
|
720
|
+
const device = model.provider_config?.device;
|
|
721
|
+
if (device && GPU_DEVICES.has(device)) {
|
|
722
|
+
return this.queuedStrategy;
|
|
723
|
+
}
|
|
724
|
+
return this.cpuStrategy;
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
// src/ai-provider/registerHuggingFaceTransformers.ts
|
|
729
|
+
import { registerProviderWithWorker } from "@workglow/ai-provider/common";
|
|
730
|
+
async function registerHuggingFaceTransformers(options) {
|
|
731
|
+
await registerProviderWithWorker(new HuggingFaceTransformersQueuedProvider, "HuggingFaceTransformers", options);
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
// src/ai-provider/common/HFT_OnnxDtypes.ts
|
|
735
|
+
var ONNX_QUANTIZATION_SUFFIX_MAPPING = {
|
|
736
|
+
fp32: "",
|
|
737
|
+
fp16: "_fp16",
|
|
738
|
+
int8: "_int8",
|
|
739
|
+
uint8: "_uint8",
|
|
740
|
+
q8: "_quantized",
|
|
741
|
+
q4: "_q4",
|
|
742
|
+
q4f16: "_q4f16",
|
|
743
|
+
bnb4: "_bnb4",
|
|
744
|
+
q2: "_q2",
|
|
745
|
+
q2f16: "_q2f16",
|
|
746
|
+
q1: "_q1",
|
|
747
|
+
q1f16: "_q1f16"
|
|
748
|
+
};
|
|
749
|
+
var SUFFIXES_LONGEST_FIRST = Object.entries(ONNX_QUANTIZATION_SUFFIX_MAPPING).filter(([, suffix]) => suffix !== "").sort((a, b) => b[1].length - a[1].length);
|
|
750
|
+
function parseOnnxQuantizations(params) {
|
|
751
|
+
const subfolder = params.subfolder ?? "onnx";
|
|
752
|
+
const prefix = subfolder + "/";
|
|
753
|
+
const stems = [];
|
|
754
|
+
for (const fp of params.filePaths) {
|
|
755
|
+
if (!fp.startsWith(prefix))
|
|
756
|
+
continue;
|
|
757
|
+
if (!fp.endsWith(".onnx"))
|
|
758
|
+
continue;
|
|
759
|
+
if (fp.endsWith(".onnx_data"))
|
|
760
|
+
continue;
|
|
761
|
+
stems.push(fp.slice(prefix.length, -".onnx".length));
|
|
762
|
+
}
|
|
763
|
+
if (stems.length === 0)
|
|
764
|
+
return [];
|
|
765
|
+
const parsed = [];
|
|
766
|
+
for (const stem of stems) {
|
|
767
|
+
let matched = false;
|
|
768
|
+
for (const [dtype, suffix] of SUFFIXES_LONGEST_FIRST) {
|
|
769
|
+
if (stem.endsWith(suffix)) {
|
|
770
|
+
parsed.push({ baseName: stem.slice(0, -suffix.length), dtype });
|
|
771
|
+
matched = true;
|
|
772
|
+
break;
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
if (!matched) {
|
|
776
|
+
parsed.push({ baseName: stem, dtype: "fp32" });
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
const allBaseNames = new Set(parsed.map((p) => p.baseName));
|
|
780
|
+
const byDtype = new Map;
|
|
781
|
+
for (const { baseName, dtype } of parsed) {
|
|
782
|
+
let set = byDtype.get(dtype);
|
|
783
|
+
if (!set) {
|
|
784
|
+
set = new Set;
|
|
785
|
+
byDtype.set(dtype, set);
|
|
786
|
+
}
|
|
787
|
+
set.add(baseName);
|
|
788
|
+
}
|
|
789
|
+
const allDtypes = Object.keys(ONNX_QUANTIZATION_SUFFIX_MAPPING);
|
|
790
|
+
return allDtypes.filter((dtype) => {
|
|
791
|
+
const set = byDtype.get(dtype);
|
|
792
|
+
return set !== undefined && set.size === allBaseNames.size;
|
|
793
|
+
});
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
// src/ai-provider/common/HFT_ToolMarkup.ts
|
|
797
|
+
function createToolCallMarkupFilter(emit) {
|
|
798
|
+
const OPEN_TAG = "<tool_call>";
|
|
799
|
+
const CLOSE_TAG = "</tool_call>";
|
|
800
|
+
let state = "text";
|
|
801
|
+
let pending = "";
|
|
802
|
+
function feed(token) {
|
|
803
|
+
if (state === "tag") {
|
|
804
|
+
pending += token;
|
|
805
|
+
const closeIdx = pending.indexOf(CLOSE_TAG);
|
|
806
|
+
if (closeIdx !== -1) {
|
|
807
|
+
const afterClose = pending.slice(closeIdx + CLOSE_TAG.length);
|
|
808
|
+
pending = "";
|
|
809
|
+
state = "text";
|
|
810
|
+
if (afterClose.length > 0) {
|
|
811
|
+
feed(afterClose);
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
return;
|
|
815
|
+
}
|
|
816
|
+
const combined = pending + token;
|
|
817
|
+
const openIdx = combined.indexOf(OPEN_TAG);
|
|
818
|
+
if (openIdx !== -1) {
|
|
819
|
+
const before = combined.slice(0, openIdx);
|
|
820
|
+
if (before.length > 0) {
|
|
821
|
+
emit(before);
|
|
822
|
+
}
|
|
823
|
+
pending = "";
|
|
824
|
+
state = "tag";
|
|
825
|
+
const afterOpen = combined.slice(openIdx + OPEN_TAG.length);
|
|
826
|
+
if (afterOpen.length > 0) {
|
|
827
|
+
feed(afterOpen);
|
|
828
|
+
}
|
|
829
|
+
return;
|
|
830
|
+
}
|
|
831
|
+
let prefixLen = 0;
|
|
832
|
+
for (let len = Math.min(combined.length, OPEN_TAG.length - 1);len >= 1; len--) {
|
|
833
|
+
if (combined.endsWith(OPEN_TAG.slice(0, len))) {
|
|
834
|
+
prefixLen = len;
|
|
835
|
+
break;
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
if (prefixLen > 0) {
|
|
839
|
+
const safe = combined.slice(0, combined.length - prefixLen);
|
|
840
|
+
if (safe.length > 0) {
|
|
841
|
+
emit(safe);
|
|
842
|
+
}
|
|
843
|
+
pending = combined.slice(combined.length - prefixLen);
|
|
844
|
+
} else {
|
|
845
|
+
if (combined.length > 0) {
|
|
846
|
+
emit(combined);
|
|
847
|
+
}
|
|
848
|
+
pending = "";
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
function flush() {
|
|
852
|
+
if (pending.length > 0 && state === "text") {
|
|
853
|
+
emit(pending);
|
|
854
|
+
pending = "";
|
|
855
|
+
}
|
|
856
|
+
pending = "";
|
|
857
|
+
state = "text";
|
|
858
|
+
}
|
|
859
|
+
return { feed, flush };
|
|
860
|
+
}
|
|
861
|
+
export {
|
|
862
|
+
registerHuggingFaceTransformers,
|
|
863
|
+
parseOnnxQuantizations,
|
|
864
|
+
createToolCallMarkupFilter,
|
|
865
|
+
QuantizationDataType,
|
|
866
|
+
PipelineUseCase,
|
|
867
|
+
ONNX_QUANTIZATION_SUFFIX_MAPPING,
|
|
868
|
+
HuggingFaceTransformersQueuedProvider,
|
|
869
|
+
HuggingFaceTransformersProvider,
|
|
870
|
+
HfTransformersOnnxModelSchema,
|
|
871
|
+
HfTransformersOnnxModelRecordSchema,
|
|
872
|
+
HfTransformersOnnxModelConfigSchema,
|
|
873
|
+
HTF_CACHE_NAME,
|
|
874
|
+
HF_TRANSFORMERS_ONNX_GPU,
|
|
875
|
+
HF_TRANSFORMERS_ONNX_CPU,
|
|
876
|
+
HF_TRANSFORMERS_ONNX
|
|
877
|
+
};
|
|
878
|
+
|
|
879
|
+
//# debugId=158703D87AE0C17864756E2164756E21
|