@flaxia/node 0.5.1 → 0.6.1

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.
@@ -1,28 +1,39 @@
1
- import { j as e } from "./webgpuProbe.js";
2
- import { TextStreamer as t, env as n, pipeline as r } 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";
3
4
  //#region src/workloads/ai-model-registry.ts
4
- var i = 512 * 1024 * 1024, a = 4 * 1024 ** 3, o = new Map(Object.entries({
5
+ var o = 512 * 1024 * 1024, s = 4 * 1024 ** 3, c = new Map(Object.entries({
5
6
  "Xenova/bert-base-multilingual-uncased-sentiment": {
6
7
  repo: "Xenova/bert-base-multilingual-uncased-sentiment",
7
8
  revision: "main",
8
- maxBytes: i
9
+ maxBytes: o
9
10
  },
10
11
  "onnx-community/Qwen3-Embedding-0.6B-ONNX": {
11
12
  repo: "onnx-community/Qwen3-Embedding-0.6B-ONNX",
12
13
  revision: "main",
13
- maxBytes: i
14
+ maxBytes: o
15
+ },
16
+ "Xenova/m2m100_418M": {
17
+ repo: "Xenova/m2m100_418M",
18
+ revision: "main",
19
+ maxBytes: o
20
+ },
21
+ "TeamFlaxia/plamo-3-translate-0.8b-onnx": {
22
+ repo: "TeamFlaxia/plamo-3-translate-0.8b-onnx",
23
+ revision: "1c8417ee1a72442440ad515b02f99186c81fd08a",
24
+ maxBytes: 2 * 1024 ** 3
14
25
  }
15
- }).map(([e, t]) => [e, { ...t }])), s = [
26
+ }).map(([e, t]) => [e, { ...t }])), l = [
16
27
  "wasm",
17
28
  "cpu",
18
29
  "webgpu"
19
- ], c = [
30
+ ], u = [
20
31
  "q4",
21
32
  "q4f16",
22
33
  "q8",
23
34
  "int8",
24
35
  "uint8"
25
- ], l = {
36
+ ], d = {
26
37
  max_new_tokens: {
27
38
  min: 1,
28
39
  max: 1024
@@ -43,45 +54,50 @@ var i = 512 * 1024 * 1024, a = 4 * 1024 ** 3, o = new Map(Object.entries({
43
54
  min: 0,
44
55
  max: 4
45
56
  }
46
- }, u = 1e5;
47
- function d() {
48
- return [...o.keys()];
57
+ }, f = 1e5;
58
+ function p() {
59
+ return [...c.keys()];
49
60
  }
50
- function f(e) {
61
+ function m(e) {
51
62
  if (typeof e != "string" || !e) throw Error(`ai-inference: model must be a non-empty string, got ${JSON.stringify(e)}`);
52
- let t = o.get(e);
53
- if (!t) throw Error(`ai-inference: model not allowed: ${e} (allowed: ${d().join(", ") || "none"})`);
63
+ let t = c.get(e);
64
+ if (!t) throw Error(`ai-inference: model not allowed: ${e} (allowed: ${p().join(", ") || "none"})`);
54
65
  return t;
55
66
  }
56
- function p(e) {
67
+ function h(e) {
57
68
  if (e != null) {
58
- if (typeof e != "string" || !s.includes(e)) throw Error(`ai-inference: device not allowed: ${JSON.stringify(e)} (allowed: ${s.join(", ")})`);
69
+ if (typeof e != "string" || !l.includes(e)) throw Error(`ai-inference: device not allowed: ${JSON.stringify(e)} (allowed: ${l.join(", ")})`);
59
70
  return e;
60
71
  }
61
72
  }
62
- function m(e) {
73
+ function g(e) {
63
74
  if (e != null) {
64
- if (typeof e != "string" || !c.includes(e)) throw Error(`ai-inference: dtype not allowed: ${JSON.stringify(e)} (allowed: ${c.join(", ")})`);
75
+ if (typeof e != "string" || !u.includes(e)) throw Error(`ai-inference: dtype not allowed: ${JSON.stringify(e)} (allowed: ${u.join(", ")})`);
65
76
  return e;
66
77
  }
67
78
  }
68
- function h(e) {
79
+ function _(e) {
80
+ if (e == null) return !1;
81
+ if (typeof e != "boolean") throw Error(`ai-inference: option ondeviceOnly must be a boolean, got ${JSON.stringify(e)}`);
82
+ return e;
83
+ }
84
+ function v(e) {
69
85
  if (typeof e == "string") {
70
- if (e.length > 1e5) throw Error(`ai-inference: input exceeds ${u} characters`);
86
+ if (e.length > 1e5) throw Error(`ai-inference: input exceeds ${f} characters`);
71
87
  return e;
72
88
  }
73
89
  if (Array.isArray(e)) {
74
90
  if (e.length === 0 || e.length > 64) throw Error("ai-inference: input array must hold 1..64 items");
75
91
  for (let t of e) {
76
92
  if (typeof t != "string") throw Error("ai-inference: input array items must be strings");
77
- if (t.length > 1e5) throw Error(`ai-inference: input exceeds ${u} characters`);
93
+ if (t.length > 1e5) throw Error(`ai-inference: input exceeds ${f} characters`);
78
94
  }
79
95
  return e;
80
96
  }
81
97
  throw Error("ai-inference: input must be a string or an array of strings");
82
98
  }
83
- function g(e, t) {
84
- for (let [n, r] of Object.entries(l)) {
99
+ function y(e, t) {
100
+ for (let [n, r] of Object.entries(d)) {
85
101
  let i = t[n];
86
102
  if (i != null) {
87
103
  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)}`);
@@ -100,18 +116,18 @@ function g(e, t) {
100
116
  }
101
117
  }
102
118
  }
103
- var _ = class {
119
+ var b = class {
104
120
  constructor(e = {}) {
105
121
  this.options = e, this.entries = /* @__PURE__ */ new Map();
106
122
  }
107
- async acquire(t, n, r, i) {
108
- let a = this.entries.get(t);
109
- if (a?.state === "ready" && a.references === 0 && a.expiresAt <= this.now() && (await this.disposeEntry(a), a = this.entries.get(t)), a?.state === "failed" && (this.entries.delete(t), a = void 0), a || (await this.evictExpired(), a = this.entries.get(t), a)) return this.acquireEntry(a);
123
+ async acquire(e, t, r, i) {
124
+ let a = this.entries.get(e);
125
+ 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);
110
126
  let o = Math.max(1, Math.floor(this.options.capacity ?? 2));
111
- if (this.entries.size >= o && await this.makeRoom(), a = this.entries.get(t), a) return this.acquireEntry(a);
127
+ if (this.entries.size >= o && await this.makeRoom(), a = this.entries.get(e), a) return this.acquireEntry(a);
112
128
  let s = this.now(), c = {
113
- key: t,
114
- descriptor: { ...n },
129
+ key: e,
130
+ descriptor: { ...t },
115
131
  state: "loading",
116
132
  references: 1,
117
133
  updatedAt: s,
@@ -120,15 +136,15 @@ var _ = class {
120
136
  promise: Promise.resolve(void 0),
121
137
  dispose: i
122
138
  };
123
- this.entries.set(t, c), c.promise = (async () => {
139
+ this.entries.set(e, c), c.promise = (async () => {
124
140
  try {
125
141
  let e = await r();
126
142
  return c.value = e.value, c.descriptor = {
127
143
  ...c.descriptor,
128
144
  ...e.descriptor
129
145
  }, c.state === "loading" && (c.state = "ready"), c.updatedAt = this.now(), c.expiresAt = this.now() + this.ttlMs(), e.value;
130
- } catch (t) {
131
- throw c.state = "failed", c.updatedAt = this.now(), c.expiresAt = this.now() + Math.min(this.ttlMs(), e), t;
146
+ } catch (e) {
147
+ throw c.state = "failed", c.updatedAt = this.now(), c.expiresAt = this.now() + Math.min(this.ttlMs(), n), e;
132
148
  }
133
149
  })();
134
150
  try {
@@ -149,11 +165,11 @@ var _ = class {
149
165
  throw e.references = Math.max(0, e.references - 1), t;
150
166
  }
151
167
  }
152
- getSnapshot(t = this.now()) {
168
+ getSnapshot(e = this.now()) {
153
169
  return {
154
170
  version: 1,
155
- sampledAt: t,
156
- expiresAt: t + e,
171
+ sampledAt: e,
172
+ expiresAt: e + n,
157
173
  entries: [...this.entries.values()].slice(0, 8).map((e) => ({
158
174
  ...e.descriptor,
159
175
  state: e.state,
@@ -220,7 +236,7 @@ var _ = class {
220
236
  return Math.max(1, this.options.ttlMs ?? 18e5);
221
237
  }
222
238
  };
223
- function v(e) {
239
+ function x(e) {
224
240
  return JSON.stringify([
225
241
  e.task,
226
242
  e.modelId,
@@ -232,7 +248,24 @@ function v(e) {
232
248
  }
233
249
  //#endregion
234
250
  //#region src/workloads/ai-inference.ts
235
- var y = [
251
+ var S = /* @__PURE__ */ e({
252
+ DEFAULT_AI_MODEL_MAX_BYTES: () => o,
253
+ LIGHT_INFERENCE_PLAMO_TRANSLATE_DTYPE: () => M,
254
+ LIGHT_INFERENCE_PLAMO_TRANSLATE_MODEL: () => A,
255
+ LIGHT_INFERENCE_PLAMO_TRANSLATE_TASK: () => j,
256
+ LIGHT_INFERENCE_TRANSLATE_DTYPE: () => k,
257
+ LIGHT_INFERENCE_TRANSLATE_MODEL: () => D,
258
+ LIGHT_INFERENCE_TRANSLATE_TASK: () => O,
259
+ MAX_AI_INPUT_CHARS: () => f,
260
+ MAX_AI_INPUT_ITEMS: () => 64,
261
+ MAX_PENDING_TOKEN_CALLBACKS: () => 64,
262
+ MAX_PENDING_TOKEN_CHARS: () => T,
263
+ getModelCacheSnapshot: () => P,
264
+ handleAiInference: () => L,
265
+ isAiModelPreloaded: () => E,
266
+ preloadAiModel: () => R,
267
+ releaseCache: () => N
268
+ }), C = [
236
269
  "text-classification",
237
270
  "token-classification",
238
271
  "question-answering",
@@ -258,9 +291,9 @@ var y = [
258
291
  "depth-estimation",
259
292
  "feature-extraction",
260
293
  "image-feature-extraction"
261
- ], b = new _(), x = () => {
262
- b.releaseAll();
263
- }, S = () => b.getSnapshot(), C = (e, t) => {
294
+ ], w = new b(), T = 256 * 1024, E = (e) => P().entries.some((t) => t.modelId === e && t.state === "ready"), D = "Xenova/m2m100_418M", O = "translation", k = "q4f16", A = "TeamFlaxia/plamo-3-translate-0.8b-onnx", j = "text-generation", M = "q4f16", N = () => {
295
+ w.releaseAll();
296
+ }, P = () => w.getSnapshot(), F = (e, t) => {
264
297
  let n = "", r = null, i = Promise.resolve(), a = 0, o = 0, s, c = !1, l = !1, u = (t) => {
265
298
  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;
266
299
  a++, o += t.length, i = i.then(() => {
@@ -299,74 +332,95 @@ var y = [
299
332
  cancel: () => p(!1)
300
333
  };
301
334
  };
302
- function w() {
335
+ function I() {
303
336
  let e = globalThis.fetch.bind(globalThis);
304
- n.fetch = async (t, n) => {
337
+ r.fetch = async (t, n) => {
305
338
  let r = new URL(String(t));
306
339
  if (!/(^|\.)huggingface\.co$/.test(r.hostname) && !/(^|\.)hf\.co$/.test(r.hostname)) throw Error(`ai-inference: model download blocked from ${r.hostname}`);
307
- let i = await e(r, n), o = Number(i.headers.get("content-length"));
308
- if (Number.isFinite(o) && o > a) throw Error(`ai-inference: model file too large (${o} bytes > ${a})`);
340
+ let i = await e(r, n), a = Number(i.headers.get("content-length"));
341
+ if (Number.isFinite(a) && a > s) throw Error(`ai-inference: model file too large (${a} bytes > ${s})`);
309
342
  return i;
310
343
  };
311
344
  }
312
- w(), n.backends?.onnx?.wasm && (n.backends.onnx.wasm.numThreads = 1, n.backends.onnx.wasm.proxy = !1);
313
- var T = async (e, n) => {
314
- let { task: i, model: a, input: o, options: s = {} } = e;
315
- if (!y.includes(i)) throw Error(`Invalid or unsupported task: ${i}. Supported tasks are: ${y.join(", ")}`);
316
- let c = f(a), l = p(s.device), u = m(s.dtype), d = h(o), _ = {};
317
- g(_, s);
318
- let x = u ?? "q4f16", S = l ?? "wasm", w = v({
319
- task: i,
320
- modelId: a,
321
- revision: c.revision,
322
- dtype: x,
323
- backend: S
324
- }), T = performance.now(), E = await b.acquire(w, {
325
- modelId: a,
326
- revision: c.revision,
327
- dtype: x,
328
- backend: S
345
+ I(), r.backends?.onnx?.wasm && (r.backends.onnx.wasm.numThreads = 1, r.backends.onnx.wasm.proxy = !1);
346
+ var L = async (e, n) => {
347
+ let { task: r, model: o, input: s, options: c = {} } = e;
348
+ if (!C.includes(r)) throw Error(`Invalid or unsupported task: ${r}. Supported tasks are: ${C.join(", ")}`);
349
+ let l = m(o), u = h(c.device), d = g(c.dtype), f = _(c.ondeviceOnly), p = v(s), b = {};
350
+ if (y(b, c), f && !E(o)) throw new t(`ondeviceOnly: model ${o} is not preloaded on this device`, "preloading");
351
+ let S = d ?? "q4f16", T = u ?? "wasm", D = x({
352
+ task: r,
353
+ modelId: o,
354
+ revision: l.revision,
355
+ dtype: S,
356
+ backend: T
357
+ }), O = performance.now(), k = await w.acquire(D, {
358
+ modelId: o,
359
+ revision: l.revision,
360
+ dtype: S,
361
+ backend: T
329
362
  }, async () => {
330
363
  let e = {
331
- dtype: x,
332
- device: l,
333
- revision: c.revision
364
+ dtype: S,
365
+ device: u,
366
+ revision: l.revision
334
367
  };
335
- console.log(`[flaxia-node] ai-inference: loading pipeline task=${i} model=${c.repo} revision=${c.revision} dtype=${e.dtype} device=${l ?? "wasm"}`);
368
+ console.log(`[flaxia-node] ai-inference: loading pipeline task=${r} model=${l.repo} revision=${l.revision} dtype=${e.dtype} device=${u ?? "wasm"}`);
336
369
  let t;
337
370
  try {
338
- t = await r(i, c.repo, e);
371
+ t = await i(r, l.repo, e);
339
372
  } catch (n) {
340
- if (l && l !== "wasm") console.warn(`[AiInference] ${l} failed, falling back to wasm:`, n), e.device = "wasm", t = await r(i, c.repo, e);
341
- else throw console.error(`[flaxia-node] ai-inference: pipeline load FAILED task=${i} model=${c.repo} error=${n instanceof Error ? n.message : String(n)}`), n;
373
+ if (u && u !== "wasm") console.warn(`[AiInference] ${u} failed, falling back to wasm:`, n), e.device = "wasm", t = await i(r, l.repo, e);
374
+ 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;
342
375
  }
343
- return console.log(`[flaxia-node] ai-inference: pipeline ready task=${i} model=${c.repo} loadMs=${Math.round(performance.now() - T)}`), {
376
+ return console.log(`[flaxia-node] ai-inference: pipeline ready task=${r} model=${l.repo} loadMs=${Math.round(performance.now() - O)}`), {
344
377
  value: t,
345
378
  descriptor: { backend: e.device ?? "wasm" }
346
379
  };
347
- }, (e) => e.dispose?.()), D = E.value;
380
+ }, (e) => e.dispose?.()), A = k.value;
348
381
  try {
349
382
  let e = {
350
- max_new_tokens: _.max_new_tokens ?? 128,
351
- do_sample: _.do_sample ?? !1
383
+ max_new_tokens: b.max_new_tokens ?? 128,
384
+ do_sample: b.do_sample ?? !1
352
385
  };
353
- for (let [t, n] of Object.entries(_)) t === "max_new_tokens" || t === "do_sample" || (e[t] = n);
354
- let r = n && D.tokenizer ? C(n, s) : null;
355
- r && (e.streamer = new t(D.tokenizer, {
386
+ for (let [t, n] of Object.entries(b)) t === "max_new_tokens" || t === "do_sample" || (e[t] = n);
387
+ let t = n && A.tokenizer ? F(n, c) : null;
388
+ t && (e.streamer = new a(A.tokenizer, {
356
389
  skip_prompt: !0,
357
390
  skip_special_tokens: !0,
358
- callback_function: r.callback
391
+ callback_function: t.callback
359
392
  }));
360
- let a = performance.now(), o;
393
+ let i = performance.now(), o;
361
394
  try {
362
- o = await D(d, e);
395
+ o = await A(p, e);
363
396
  } catch (e) {
364
- throw /out of memory|oom|allocation failed|could not allocate/i.test(e instanceof Error ? e.message : String(e)) && E.invalidate(), r && await r.cancel(), e;
397
+ 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;
365
398
  }
366
- return await r?.finish(), console.log(`[flaxia-node] ai-inference: done task=${i} model=${c.repo} execMs=${Math.round(performance.now() - a)} inputLen=${String(d).length}`), { output: o };
399
+ 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 };
367
400
  } finally {
368
- await E.release();
401
+ await k.release();
369
402
  }
403
+ }, R = async (e) => {
404
+ let { task: t, model: n } = e, r = m(n), a = h(e.device), o = g(e.dtype) ?? "q4f16", s = a ?? "wasm", c = x({
405
+ task: t,
406
+ modelId: n,
407
+ revision: r.revision,
408
+ dtype: o,
409
+ backend: s
410
+ });
411
+ await (await w.acquire(c, {
412
+ modelId: n,
413
+ revision: r.revision,
414
+ dtype: o,
415
+ backend: s
416
+ }, async () => (console.log(`[flaxia-node] ai-inference: preloading task=${t} model=${r.repo}`), {
417
+ value: await i(t, r.repo, {
418
+ dtype: o,
419
+ device: a,
420
+ revision: r.revision
421
+ }),
422
+ descriptor: { backend: s }
423
+ }), (e) => e.dispose?.())).release();
370
424
  };
371
425
  //#endregion
372
- export { S as getModelCacheSnapshot, T as handleAiInference, x as releaseCache };
426
+ export { D as a, E as c, k as i, R as l, A as n, O as o, j as r, S as s, M 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" });