@flaxia/node 0.5.0 → 0.6.0

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 (54) hide show
  1. package/dist/assets/ai-inference.js +288 -86
  2. package/dist/assets/transformers.web.js +2 -2
  3. package/dist/assets/vector-embed.js +1 -1
  4. package/dist/assets/webgpuProbe.js +365 -0
  5. package/dist/client/SignalingClient.d.ts.map +1 -1
  6. package/dist/client/__tests__/light-preloader.test.d.ts +2 -0
  7. package/dist/client/__tests__/light-preloader.test.d.ts.map +1 -0
  8. package/dist/client/__tests__/mobile-lifecycle.test.d.ts +2 -0
  9. package/dist/client/__tests__/mobile-lifecycle.test.d.ts.map +1 -0
  10. package/dist/client/light-preloader.d.ts +47 -0
  11. package/dist/client/light-preloader.d.ts.map +1 -0
  12. package/dist/client/mobile-lifecycle.d.ts +20 -0
  13. package/dist/client/mobile-lifecycle.d.ts.map +1 -0
  14. package/dist/executor/WorkerPool.d.ts +7 -1
  15. package/dist/executor/WorkerPool.d.ts.map +1 -1
  16. package/dist/executor/__tests__/resourceReporter.test.d.ts +2 -0
  17. package/dist/executor/__tests__/resourceReporter.test.d.ts.map +1 -0
  18. package/dist/executor/memoryProbe.d.ts +5 -0
  19. package/dist/executor/memoryProbe.d.ts.map +1 -1
  20. package/dist/executor/resourceReporter.d.ts +15 -0
  21. package/dist/executor/resourceReporter.d.ts.map +1 -0
  22. package/dist/executor/webgpuProbe.d.ts +16 -5
  23. package/dist/executor/webgpuProbe.d.ts.map +1 -1
  24. package/dist/index.d.ts +2 -0
  25. package/dist/index.d.ts.map +1 -1
  26. package/dist/index.js +1093 -354
  27. package/dist/swarm/__tests__/peer-quality.test.d.ts +2 -0
  28. package/dist/swarm/__tests__/peer-quality.test.d.ts.map +1 -0
  29. package/dist/swarm/__tests__/peer-transport.test.d.ts +2 -0
  30. package/dist/swarm/__tests__/peer-transport.test.d.ts.map +1 -0
  31. package/dist/swarm/adapter.d.ts +1 -1
  32. package/dist/swarm/adapter.d.ts.map +1 -1
  33. package/dist/swarm/controller.d.ts +4 -0
  34. package/dist/swarm/controller.d.ts.map +1 -1
  35. package/dist/swarm/messages.d.ts +2 -2
  36. package/dist/swarm/messages.d.ts.map +1 -1
  37. package/dist/swarm/peer-quality.d.ts +14 -0
  38. package/dist/swarm/peer-quality.d.ts.map +1 -0
  39. package/dist/swarm/peer-transport.d.ts +81 -0
  40. package/dist/swarm/peer-transport.d.ts.map +1 -0
  41. package/dist/swarm/runtime.d.ts +5 -1
  42. package/dist/swarm/runtime.d.ts.map +1 -1
  43. package/dist/swarm-engine.js +39 -20
  44. package/dist/worker.js +317 -185
  45. package/dist/workloads/__tests__/model-pipeline-cache.test.d.ts +2 -0
  46. package/dist/workloads/__tests__/model-pipeline-cache.test.d.ts.map +1 -0
  47. package/dist/workloads/ai-inference.d.ts +27 -0
  48. package/dist/workloads/ai-inference.d.ts.map +1 -1
  49. package/dist/workloads/ai-model-registry.d.ts +6 -0
  50. package/dist/workloads/ai-model-registry.d.ts.map +1 -1
  51. package/dist/workloads/model-pipeline-cache.d.ts +48 -0
  52. package/dist/workloads/model-pipeline-cache.d.ts.map +1 -0
  53. package/package.json +2 -2
  54. package/dist/assets/dist.js +0 -209
@@ -1,27 +1,34 @@
1
- import { TextStreamer as e, env as t, pipeline as n } from "./transformers.web.js";
1
+ import { t as e } from "./chunk.js";
2
+ import { E as t, x as n } from "./webgpuProbe.js";
3
+ import { n as r, r as i, t as a } from "./transformers.web.js";
2
4
  //#region src/workloads/ai-model-registry.ts
3
- var r = 512 * 1024 * 1024, i = 4 * 1024 ** 3, a = new Map(Object.entries({
5
+ var o = 512 * 1024 * 1024, s = 4 * 1024 ** 3, c = new Map(Object.entries({
4
6
  "Xenova/bert-base-multilingual-uncased-sentiment": {
5
7
  repo: "Xenova/bert-base-multilingual-uncased-sentiment",
6
8
  revision: "main",
7
- maxBytes: r
9
+ maxBytes: o
8
10
  },
9
11
  "onnx-community/Qwen3-Embedding-0.6B-ONNX": {
10
12
  repo: "onnx-community/Qwen3-Embedding-0.6B-ONNX",
11
13
  revision: "main",
12
- maxBytes: r
14
+ maxBytes: o
15
+ },
16
+ "Xenova/m2m100_418M": {
17
+ repo: "Xenova/m2m100_418M",
18
+ revision: "main",
19
+ maxBytes: o
13
20
  }
14
- }).map(([e, t]) => [e, { ...t }])), o = [
21
+ }).map(([e, t]) => [e, { ...t }])), l = [
15
22
  "wasm",
16
23
  "cpu",
17
24
  "webgpu"
18
- ], s = [
25
+ ], u = [
19
26
  "q4",
20
27
  "q4f16",
21
28
  "q8",
22
29
  "int8",
23
30
  "uint8"
24
- ], c = {
31
+ ], d = {
25
32
  max_new_tokens: {
26
33
  min: 1,
27
34
  max: 1024
@@ -42,45 +49,50 @@ var r = 512 * 1024 * 1024, i = 4 * 1024 ** 3, a = new Map(Object.entries({
42
49
  min: 0,
43
50
  max: 4
44
51
  }
45
- }, l = 1e5;
46
- function u() {
47
- return [...a.keys()];
52
+ }, f = 1e5;
53
+ function p() {
54
+ return [...c.keys()];
48
55
  }
49
- function d(e) {
56
+ function m(e) {
50
57
  if (typeof e != "string" || !e) throw Error(`ai-inference: model must be a non-empty string, got ${JSON.stringify(e)}`);
51
- let t = a.get(e);
52
- if (!t) throw Error(`ai-inference: model not allowed: ${e} (allowed: ${u().join(", ") || "none"})`);
58
+ let t = c.get(e);
59
+ if (!t) throw Error(`ai-inference: model not allowed: ${e} (allowed: ${p().join(", ") || "none"})`);
53
60
  return t;
54
61
  }
55
- function f(e) {
62
+ function h(e) {
56
63
  if (e != null) {
57
- if (typeof e != "string" || !o.includes(e)) throw Error(`ai-inference: device not allowed: ${JSON.stringify(e)} (allowed: ${o.join(", ")})`);
64
+ if (typeof e != "string" || !l.includes(e)) throw Error(`ai-inference: device not allowed: ${JSON.stringify(e)} (allowed: ${l.join(", ")})`);
58
65
  return e;
59
66
  }
60
67
  }
61
- function p(e) {
68
+ function g(e) {
62
69
  if (e != null) {
63
- if (typeof e != "string" || !s.includes(e)) throw Error(`ai-inference: dtype not allowed: ${JSON.stringify(e)} (allowed: ${s.join(", ")})`);
70
+ if (typeof e != "string" || !u.includes(e)) throw Error(`ai-inference: dtype not allowed: ${JSON.stringify(e)} (allowed: ${u.join(", ")})`);
64
71
  return e;
65
72
  }
66
73
  }
67
- function m(e) {
74
+ function _(e) {
75
+ if (e == null) return !1;
76
+ if (typeof e != "boolean") throw Error(`ai-inference: option ondeviceOnly must be a boolean, got ${JSON.stringify(e)}`);
77
+ return e;
78
+ }
79
+ function v(e) {
68
80
  if (typeof e == "string") {
69
- if (e.length > 1e5) throw Error(`ai-inference: input exceeds ${l} characters`);
81
+ if (e.length > 1e5) throw Error(`ai-inference: input exceeds ${f} characters`);
70
82
  return e;
71
83
  }
72
84
  if (Array.isArray(e)) {
73
85
  if (e.length === 0 || e.length > 64) throw Error("ai-inference: input array must hold 1..64 items");
74
86
  for (let t of e) {
75
87
  if (typeof t != "string") throw Error("ai-inference: input array items must be strings");
76
- if (t.length > 1e5) throw Error(`ai-inference: input exceeds ${l} characters`);
88
+ if (t.length > 1e5) throw Error(`ai-inference: input exceeds ${f} characters`);
77
89
  }
78
90
  return e;
79
91
  }
80
92
  throw Error("ai-inference: input must be a string or an array of strings");
81
93
  }
82
- function h(e, t) {
83
- for (let [n, r] of Object.entries(c)) {
94
+ function y(e, t) {
95
+ for (let [n, r] of Object.entries(d)) {
84
96
  let i = t[n];
85
97
  if (i != null) {
86
98
  if (typeof i != "number" || !Number.isFinite(i) || i < r.min || i > r.max) throw Error(`ai-inference: option ${n} must be a number within ${r.min}..${r.max}, got ${JSON.stringify(i)}`);
@@ -99,9 +111,153 @@ function h(e, t) {
99
111
  }
100
112
  }
101
113
  }
114
+ var b = class {
115
+ constructor(e = {}) {
116
+ this.options = e, this.entries = /* @__PURE__ */ new Map();
117
+ }
118
+ async acquire(e, t, r, i) {
119
+ let a = this.entries.get(e);
120
+ if (a?.state === "ready" && a.references === 0 && a.expiresAt <= this.now() && (await this.disposeEntry(a), a = this.entries.get(e)), a?.state === "failed" && (this.entries.delete(e), a = void 0), a || (await this.evictExpired(), a = this.entries.get(e), a)) return this.acquireEntry(a);
121
+ let o = Math.max(1, Math.floor(this.options.capacity ?? 2));
122
+ if (this.entries.size >= o && await this.makeRoom(), a = this.entries.get(e), a) return this.acquireEntry(a);
123
+ let s = this.now(), c = {
124
+ key: e,
125
+ descriptor: { ...t },
126
+ state: "loading",
127
+ references: 1,
128
+ updatedAt: s,
129
+ lastUsedAt: s,
130
+ expiresAt: s + this.ttlMs(),
131
+ promise: Promise.resolve(void 0),
132
+ dispose: i
133
+ };
134
+ this.entries.set(e, c), c.promise = (async () => {
135
+ try {
136
+ let e = await r();
137
+ return c.value = e.value, c.descriptor = {
138
+ ...c.descriptor,
139
+ ...e.descriptor
140
+ }, c.state === "loading" && (c.state = "ready"), c.updatedAt = this.now(), c.expiresAt = this.now() + this.ttlMs(), e.value;
141
+ } catch (e) {
142
+ throw c.state = "failed", c.updatedAt = this.now(), c.expiresAt = this.now() + Math.min(this.ttlMs(), n), e;
143
+ }
144
+ })();
145
+ try {
146
+ let e = await c.promise;
147
+ return this.createLease(c, e);
148
+ } catch (e) {
149
+ throw c.references = Math.max(0, c.references - 1), e;
150
+ }
151
+ }
152
+ async acquireEntry(e) {
153
+ if (e.state === "failed") throw this.entries.delete(e.key), Error("model cache entry failed before it could be acquired");
154
+ if (e.state === "evicting") throw Error("model cache entry is being evicted");
155
+ e.references++, e.lastUsedAt = this.now(), e.expiresAt = this.now() + this.ttlMs();
156
+ try {
157
+ let t = await e.promise;
158
+ return this.createLease(e, t);
159
+ } catch (t) {
160
+ throw e.references = Math.max(0, e.references - 1), t;
161
+ }
162
+ }
163
+ getSnapshot(e = this.now()) {
164
+ return {
165
+ version: 1,
166
+ sampledAt: e,
167
+ expiresAt: e + n,
168
+ entries: [...this.entries.values()].slice(0, 8).map((e) => ({
169
+ ...e.descriptor,
170
+ state: e.state,
171
+ referenceCount: e.references
172
+ }))
173
+ };
174
+ }
175
+ async releaseAll() {
176
+ let e = [];
177
+ for (let t of this.entries.values()) {
178
+ if (t.state === "failed") {
179
+ this.entries.delete(t.key);
180
+ continue;
181
+ }
182
+ t.state = "evicting", t.updatedAt = this.now(), t.references === 0 && t.value !== void 0 && e.push(this.disposeEntry(t));
183
+ }
184
+ await Promise.all(e);
185
+ }
186
+ createLease(e, t) {
187
+ let n = !1;
188
+ return {
189
+ value: t,
190
+ updateDescriptor: (t) => {
191
+ n || (e.descriptor = {
192
+ ...e.descriptor,
193
+ ...t
194
+ }, e.updatedAt = this.now());
195
+ },
196
+ invalidate: () => {
197
+ n || (e.state = "evicting", e.updatedAt = this.now());
198
+ },
199
+ release: async () => {
200
+ n || (n = !0, e.references = Math.max(0, e.references - 1), e.lastUsedAt = this.now(), e.updatedAt = this.now(), e.references === 0 && e.state === "evicting" ? await this.disposeEntry(e) : e.references === 0 && e.state === "ready" && (e.expiresAt = this.now() + this.ttlMs()));
201
+ }
202
+ };
203
+ }
204
+ async evictExpired() {
205
+ let e = this.now();
206
+ for (let t of [...this.entries.values()]) t.state === "failed" && t.expiresAt <= e ? this.entries.delete(t.key) : t.state === "ready" && t.references === 0 && t.expiresAt <= e && await this.disposeEntry(t);
207
+ }
208
+ async makeRoom() {
209
+ let e = Math.max(1, Math.floor(this.options.capacity ?? 2));
210
+ for (; this.entries.size >= e;) {
211
+ let e = [...this.entries.values()].filter((e) => e.references === 0 && (e.state === "failed" || e.state === "ready")).sort((e, t) => Number(t.state === "failed") - Number(e.state === "failed") || e.lastUsedAt - t.lastUsedAt)[0];
212
+ if (!e) throw Error("model cache capacity is occupied by active entries");
213
+ e.state === "failed" ? this.entries.delete(e.key) : await this.disposeEntry(e);
214
+ }
215
+ }
216
+ async disposeEntry(e) {
217
+ if (e.references > 0 || e.state === "loading") return;
218
+ e.state = "evicting", e.updatedAt = this.now();
219
+ let t = e.value;
220
+ e.value = void 0;
221
+ try {
222
+ t !== void 0 && await e.dispose(t);
223
+ } finally {
224
+ this.entries.get(e.key) === e && this.entries.delete(e.key);
225
+ }
226
+ }
227
+ now() {
228
+ return this.options.now?.() ?? Date.now();
229
+ }
230
+ ttlMs() {
231
+ return Math.max(1, this.options.ttlMs ?? 18e5);
232
+ }
233
+ };
234
+ function x(e) {
235
+ return JSON.stringify([
236
+ e.task,
237
+ e.modelId,
238
+ e.revision,
239
+ e.dtype,
240
+ e.backend,
241
+ e.layerRange ?? null
242
+ ]);
243
+ }
102
244
  //#endregion
103
245
  //#region src/workloads/ai-inference.ts
104
- var g = [
246
+ var S = /* @__PURE__ */ e({
247
+ DEFAULT_AI_MODEL_MAX_BYTES: () => o,
248
+ LIGHT_INFERENCE_TRANSLATE_DTYPE: () => k,
249
+ LIGHT_INFERENCE_TRANSLATE_MODEL: () => D,
250
+ LIGHT_INFERENCE_TRANSLATE_TASK: () => O,
251
+ MAX_AI_INPUT_CHARS: () => f,
252
+ MAX_AI_INPUT_ITEMS: () => 64,
253
+ MAX_PENDING_TOKEN_CALLBACKS: () => 64,
254
+ MAX_PENDING_TOKEN_CHARS: () => T,
255
+ getModelCacheSnapshot: () => j,
256
+ handleAiInference: () => P,
257
+ isAiModelPreloaded: () => E,
258
+ preloadAiModel: () => F,
259
+ releaseCache: () => A
260
+ }), C = [
105
261
  "text-classification",
106
262
  "token-classification",
107
263
  "question-answering",
@@ -127,90 +283,136 @@ var g = [
127
283
  "depth-estimation",
128
284
  "feature-extraction",
129
285
  "image-feature-extraction"
130
- ], _ = /* @__PURE__ */ new Map(), v = 2, y = () => {
131
- _.clear();
132
- }, b = (e, t) => {
133
- let n = "", r = null, i = Promise.resolve(), a, o = !1, s = !1, c = (t) => {
134
- i = i.then(() => {
135
- if (!o) return e(t);
286
+ ], w = new b(), T = 256 * 1024, E = (e) => j().entries.some((t) => t.modelId === e && t.state === "ready"), D = "Xenova/m2m100_418M", O = "translation", k = "q4f16", A = () => {
287
+ w.releaseAll();
288
+ }, j = () => w.getSnapshot(), M = (e, t) => {
289
+ let n = "", r = null, i = Promise.resolve(), a = 0, o = 0, s, c = !1, l = !1, u = (t) => {
290
+ if (a >= 64 || o + t.length > 262144) throw c = !0, s = /* @__PURE__ */ Error("ai-inference token consumer is too slow; generation stopped at the bounded stream buffer"), s;
291
+ a++, o += t.length, i = i.then(() => {
292
+ if (!c) return e(t);
136
293
  }).then(() => void 0, (e) => {
137
- o = !0, a = e;
294
+ c = !0, s = e;
295
+ }).finally(() => {
296
+ a--, o -= t.length;
138
297
  });
139
- }, l = () => {
298
+ }, d = () => {
140
299
  r !== null && clearTimeout(r), r = null;
141
300
  let e = n;
142
- n = "", e && !o && c(e);
143
- }, u = (e) => {
144
- if (!s) {
145
- if (o) throw a;
301
+ n = "", e && !c && u(e);
302
+ }, f = (e) => {
303
+ if (!l) {
304
+ if (c) throw s;
146
305
  if (!t.tokenBuffer) {
147
- c(e);
306
+ u(e);
148
307
  return;
149
308
  }
150
- n += e, r === null && (r = setTimeout(l, t.tokenBufferIntervalMs ?? 50));
309
+ if (n.length + e.length > 262144) throw c = !0, s = /* @__PURE__ */ Error("ai-inference token consumer is too slow; generation stopped at the bounded stream buffer"), s;
310
+ n += e, r === null && (r = setTimeout(() => {
311
+ try {
312
+ d();
313
+ } catch (e) {
314
+ c = !0, s = e;
315
+ }
316
+ }, t.tokenBufferIntervalMs ?? 50));
151
317
  }
152
- }, d = async (e) => {
153
- if (s = !0, r !== null && clearTimeout(r), r = null, e && !o ? l() : n = "", await i, o) throw a;
318
+ }, p = async (e) => {
319
+ if (l = !0, r !== null && clearTimeout(r), r = null, e && !c ? d() : n = "", await i, c) throw s;
154
320
  };
155
321
  return {
156
- callback: u,
157
- finish: () => d(!0),
158
- cancel: () => d(!1)
322
+ callback: f,
323
+ finish: () => p(!0),
324
+ cancel: () => p(!1)
159
325
  };
160
326
  };
161
- function x() {
327
+ function N() {
162
328
  let e = globalThis.fetch.bind(globalThis);
163
- t.fetch = async (t, n) => {
329
+ r.fetch = async (t, n) => {
164
330
  let r = new URL(String(t));
165
331
  if (!/(^|\.)huggingface\.co$/.test(r.hostname) && !/(^|\.)hf\.co$/.test(r.hostname)) throw Error(`ai-inference: model download blocked from ${r.hostname}`);
166
- let a = await e(r, n), o = Number(a.headers.get("content-length"));
167
- if (Number.isFinite(o) && o > i) throw Error(`ai-inference: model file too large (${o} bytes > ${i})`);
168
- return a;
332
+ let i = await e(r, n), a = Number(i.headers.get("content-length"));
333
+ if (Number.isFinite(a) && a > s) throw Error(`ai-inference: model file too large (${a} bytes > ${s})`);
334
+ return i;
169
335
  };
170
336
  }
171
- x(), t.backends?.onnx?.wasm && (t.backends.onnx.wasm.numThreads = 1, t.backends.onnx.wasm.proxy = !1);
172
- var S = async (t, r) => {
173
- let { task: i, model: a, input: o, options: s = {} } = t;
174
- if (!g.includes(i)) throw Error(`Invalid or unsupported task: ${i}. Supported tasks are: ${g.join(", ")}`);
175
- let c = d(a), l = f(s.device), u = p(s.dtype), y = m(o), x = {};
176
- h(x, s);
177
- let S = `${i}:${c.repo}:${u ?? "q4f16"}:${l ?? "wasm"}`, C = _.get(S);
178
- if (!C) {
179
- if (_.size >= v) {
180
- let e = _.keys().next().value;
181
- _.delete(e);
182
- }
337
+ N(), r.backends?.onnx?.wasm && (r.backends.onnx.wasm.numThreads = 1, r.backends.onnx.wasm.proxy = !1);
338
+ var P = async (e, n) => {
339
+ let { task: r, model: o, input: s, options: c = {} } = e;
340
+ if (!C.includes(r)) throw Error(`Invalid or unsupported task: ${r}. Supported tasks are: ${C.join(", ")}`);
341
+ let l = m(o), u = h(c.device), d = g(c.dtype), f = _(c.ondeviceOnly), p = v(s), b = {};
342
+ if (y(b, c), f && !E(o)) throw new t(`ondeviceOnly: model ${o} is not preloaded on this device`, "preloading");
343
+ let S = d ?? "q4f16", T = u ?? "wasm", D = x({
344
+ task: r,
345
+ modelId: o,
346
+ revision: l.revision,
347
+ dtype: S,
348
+ backend: T
349
+ }), O = performance.now(), k = await w.acquire(D, {
350
+ modelId: o,
351
+ revision: l.revision,
352
+ dtype: S,
353
+ backend: T
354
+ }, async () => {
183
355
  let e = {
184
- dtype: u ?? "q4f16",
185
- device: l
186
- }, t = performance.now();
187
- console.log(`[flaxia-node] ai-inference: loading pipeline task=${i} model=${c.repo} revision=${c.revision} dtype=${e.dtype} device=${l ?? "wasm"}`);
356
+ dtype: S,
357
+ device: u,
358
+ revision: l.revision
359
+ };
360
+ console.log(`[flaxia-node] ai-inference: loading pipeline task=${r} model=${l.repo} revision=${l.revision} dtype=${e.dtype} device=${u ?? "wasm"}`);
361
+ let t;
188
362
  try {
189
- C = await n(i, c.repo, e);
190
- } catch (t) {
191
- if (l && l !== "wasm") console.warn(`[AiInference] ${l} failed, falling back to wasm:`, t), e.device = "wasm", C = await n(i, c.repo, e);
192
- else throw console.error(`[flaxia-node] ai-inference: pipeline load FAILED task=${i} model=${c.repo} error=${t instanceof Error ? t.message : String(t)}`), t;
363
+ t = await i(r, l.repo, e);
364
+ } catch (n) {
365
+ if (u && u !== "wasm") console.warn(`[AiInference] ${u} failed, falling back to wasm:`, n), e.device = "wasm", t = await i(r, l.repo, e);
366
+ else throw console.error(`[flaxia-node] ai-inference: pipeline load FAILED task=${r} model=${l.repo} error=${n instanceof Error ? n.message : String(n)}`), n;
193
367
  }
194
- console.log(`[flaxia-node] ai-inference: pipeline ready task=${i} model=${c.repo} loadMs=${Math.round(performance.now() - t)}`), _.set(S, C);
195
- }
196
- let w = {
197
- max_new_tokens: x.max_new_tokens ?? 128,
198
- do_sample: x.do_sample ?? !1
199
- };
200
- for (let [e, t] of Object.entries(x)) e === "max_new_tokens" || e === "do_sample" || (w[e] = t);
201
- let T = r && C.tokenizer ? b(r, s) : null;
202
- T && (w.streamer = new e(C.tokenizer, {
203
- skip_prompt: !0,
204
- skip_special_tokens: !0,
205
- callback_function: T.callback
206
- }));
207
- let E = performance.now(), D;
368
+ return console.log(`[flaxia-node] ai-inference: pipeline ready task=${r} model=${l.repo} loadMs=${Math.round(performance.now() - O)}`), {
369
+ value: t,
370
+ descriptor: { backend: e.device ?? "wasm" }
371
+ };
372
+ }, (e) => e.dispose?.()), A = k.value;
208
373
  try {
209
- D = await C(y, w);
210
- } catch (e) {
211
- throw T && await T.cancel(), e;
374
+ let e = {
375
+ max_new_tokens: b.max_new_tokens ?? 128,
376
+ do_sample: b.do_sample ?? !1
377
+ };
378
+ for (let [t, n] of Object.entries(b)) t === "max_new_tokens" || t === "do_sample" || (e[t] = n);
379
+ let t = n && A.tokenizer ? M(n, c) : null;
380
+ t && (e.streamer = new a(A.tokenizer, {
381
+ skip_prompt: !0,
382
+ skip_special_tokens: !0,
383
+ callback_function: t.callback
384
+ }));
385
+ let i = performance.now(), o;
386
+ try {
387
+ o = await A(p, e);
388
+ } catch (e) {
389
+ throw /out of memory|oom|allocation failed|could not allocate/i.test(e instanceof Error ? e.message : String(e)) && k.invalidate(), t && await t.cancel(), e;
390
+ }
391
+ return await t?.finish(), console.log(`[flaxia-node] ai-inference: done task=${r} model=${l.repo} execMs=${Math.round(performance.now() - i)} inputLen=${String(p).length}`), { output: o };
392
+ } finally {
393
+ await k.release();
212
394
  }
213
- return await T?.finish(), console.log(`[flaxia-node] ai-inference: done task=${i} model=${c.repo} execMs=${Math.round(performance.now() - E)} inputLen=${String(y).length}`), { output: D };
395
+ }, F = async (e) => {
396
+ let { task: t, model: n } = e, r = m(n), a = h(e.device), o = g(e.dtype) ?? "q4f16", s = a ?? "wasm", c = x({
397
+ task: t,
398
+ modelId: n,
399
+ revision: r.revision,
400
+ dtype: o,
401
+ backend: s
402
+ });
403
+ await (await w.acquire(c, {
404
+ modelId: n,
405
+ revision: r.revision,
406
+ dtype: o,
407
+ backend: s
408
+ }, async () => (console.log(`[flaxia-node] ai-inference: preloading task=${t} model=${r.repo}`), {
409
+ value: await i(t, r.repo, {
410
+ dtype: o,
411
+ device: a,
412
+ revision: r.revision
413
+ }),
414
+ descriptor: { backend: s }
415
+ }), (e) => e.dispose?.())).release();
214
416
  };
215
417
  //#endregion
216
- export { S as handleAiInference, y as releaseCache };
418
+ export { E as a, S as i, D as n, F as o, O as r, k as t };
@@ -1,4 +1,4 @@
1
1
  [diffend] Oversized file quarantined before diffing.
2
2
  name: package/dist/assets/transformers.web.js
3
- size: 63498194 bytes
4
- sha256: ebb50a0da2a27878c81b3cd7b045df58263d18a5dbb9fb357e48edf8ed35b25f
3
+ size: 63528578 bytes
4
+ sha256: c4ea4c2cc11efabbcbd71ff8631ac3d08593a8cde1a4a2a64518ac81f837f4b0
@@ -7,7 +7,7 @@ async function n(t) {
7
7
  if (!e) {
8
8
  let t = performance.now();
9
9
  console.log("[flaxia-node] vector-embed: loading pipeline model=onnx-community/Qwen3-Embedding-0.6B-ONNX device=wasm");
10
- let { pipeline: n, env: r } = await import("./transformers.web.js");
10
+ let { pipeline: n, env: r } = await import("./transformers.web.js").then((e) => e.i);
11
11
  r?.backends?.onnx?.wasm && (r.backends.onnx.wasm.numThreads = 1, r.backends.onnx.wasm.proxy = !1);
12
12
  try {
13
13
  e = await n("feature-extraction", "onnx-community/Qwen3-Embedding-0.6B-ONNX", { device: "wasm" });