@flaxia/node 0.7.0 → 0.7.2

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 (52) hide show
  1. package/dist/assets/ai-inference.js +9 -9
  2. package/dist/assets/ai-model-registry.js +0 -5
  3. package/dist/assets/cc-extract.js +20 -20
  4. package/dist/assets/container.js +59 -43
  5. package/dist/assets/decision.js +43 -36
  6. package/dist/assets/embed-gemma.js +28 -20
  7. package/dist/assets/image-process.js +78 -70
  8. package/dist/assets/nudenet.js +2 -2
  9. package/dist/assets/{VectorStoreEngine.js → sharedEngine.js} +67 -41
  10. package/dist/assets/vector-embed.js +49 -36
  11. package/dist/assets/vector-query.js +4 -4
  12. package/dist/assets/vector-store.js +4 -4
  13. package/dist/assets/webgpuProbe.js +87 -47
  14. package/dist/client/SignalingClient.d.ts.map +1 -1
  15. package/dist/client/light-preloader.d.ts +16 -6
  16. package/dist/client/light-preloader.d.ts.map +1 -1
  17. package/dist/executor/WorkerPool.d.ts +6 -0
  18. package/dist/executor/WorkerPool.d.ts.map +1 -1
  19. package/dist/executor/__tests__/resultLimits.test.d.ts +2 -0
  20. package/dist/executor/__tests__/resultLimits.test.d.ts.map +1 -0
  21. package/dist/executor/container-executor.d.ts +15 -0
  22. package/dist/executor/container-executor.d.ts.map +1 -1
  23. package/dist/executor/memoryProbe.d.ts +25 -1
  24. package/dist/executor/memoryProbe.d.ts.map +1 -1
  25. package/dist/executor/resultLimits.d.ts +19 -0
  26. package/dist/executor/resultLimits.d.ts.map +1 -0
  27. package/dist/index.js +416 -396
  28. package/dist/vector-store/HNSWIndex.d.ts +4 -1
  29. package/dist/vector-store/HNSWIndex.d.ts.map +1 -1
  30. package/dist/vector-store/VectorStoreEngine.d.ts +7 -1
  31. package/dist/vector-store/VectorStoreEngine.d.ts.map +1 -1
  32. package/dist/vector-store/__tests__/sharedEngine.test.d.ts +2 -0
  33. package/dist/vector-store/__tests__/sharedEngine.test.d.ts.map +1 -0
  34. package/dist/vector-store/sharedEngine.d.ts +13 -0
  35. package/dist/vector-store/sharedEngine.d.ts.map +1 -0
  36. package/dist/worker.js +163 -160
  37. package/dist/workloads/__tests__/nudenet-session-cache.test.d.ts +2 -0
  38. package/dist/workloads/__tests__/nudenet-session-cache.test.d.ts.map +1 -0
  39. package/dist/workloads/ai-inference.d.ts +8 -7
  40. package/dist/workloads/ai-inference.d.ts.map +1 -1
  41. package/dist/workloads/ai-model-registry.d.ts.map +1 -1
  42. package/dist/workloads/decision.d.ts +2 -0
  43. package/dist/workloads/decision.d.ts.map +1 -1
  44. package/dist/workloads/embed-gemma.d.ts.map +1 -1
  45. package/dist/workloads/image-process.d.ts +8 -0
  46. package/dist/workloads/image-process.d.ts.map +1 -1
  47. package/dist/workloads/nudenet.d.ts.map +1 -1
  48. package/dist/workloads/vector-embed.d.ts +6 -0
  49. package/dist/workloads/vector-embed.d.ts.map +1 -1
  50. package/dist/workloads/vector-query.d.ts.map +1 -1
  51. package/dist/workloads/vector-store.d.ts.map +1 -1
  52. package/package.json +2 -2
@@ -1,22 +1,26 @@
1
1
  //#region src/workloads/image-process.ts
2
- var e = 8192, t = 32 * 1024 * 1024, n = 16 * 1024 * 1024;
3
- function r(e, t) {
2
+ var e = 8192, t = 32 * 1024 * 1024, n = 16 * 1024 * 1024, r = 8 * 1024 * 1024, i = [
3
+ "png",
4
+ "jpeg",
5
+ "webp"
6
+ ];
7
+ function a(e, t) {
4
8
  return e[t] * 16777216 + (e[t + 1] << 16) + (e[t + 2] << 8) + e[t + 3] >>> 0;
5
9
  }
6
- function i(e, t) {
10
+ function o(e, t) {
7
11
  return e[t] + e[t + 1] * 256 + e[t + 2] * 65536 + e[t + 3] * 16777216 >>> 0;
8
12
  }
9
- function a(e, t) {
13
+ function s(e, t) {
10
14
  return e[t] * 256 + e[t + 1];
11
15
  }
12
- function o(e, t, n) {
16
+ function c(e, t, n) {
13
17
  return t + n.length <= e.length && n.every((n, r) => e[t + r] === n);
14
18
  }
15
- function s() {
19
+ function l() {
16
20
  throw Error("image-process: unknown, truncated or inconsistent image header");
17
21
  }
18
- function c(e) {
19
- if (o(e, 0, [
22
+ function u(e) {
23
+ if (c(e, 0, [
20
24
  137,
21
25
  80,
22
26
  78,
@@ -25,7 +29,7 @@ function c(e) {
25
29
  10,
26
30
  26,
27
31
  10
28
- ])) return (e.length < 33 || r(e, 8) !== 13 || !o(e, 12, [
32
+ ])) return (e.length < 33 || a(e, 8) !== 13 || !c(e, 12, [
29
33
  73,
30
34
  72,
31
35
  68,
@@ -41,21 +45,21 @@ function c(e) {
41
45
  2,
42
46
  4,
43
47
  8
44
- ].includes(e[24]) || [4, 6].includes(e[25]) && [8, 16].includes(e[24])) || e[26] !== 0 || e[27] !== 0 || e[28] > 1) && s(), {
45
- width: r(e, 16),
46
- height: r(e, 20),
48
+ ].includes(e[24]) || [4, 6].includes(e[25]) && [8, 16].includes(e[24])) || e[26] !== 0 || e[27] !== 0 || e[28] > 1) && l(), {
49
+ width: a(e, 16),
50
+ height: a(e, 20),
47
51
  format: "png"
48
52
  };
49
- if (o(e, 0, [255, 216])) {
53
+ if (c(e, 0, [255, 216])) {
50
54
  let t = 2;
51
55
  for (; t < e.length;) {
52
- for (e[t++] !== 255 && s(); t < e.length && e[t] === 255;) t++;
53
- t >= e.length && s();
56
+ for (e[t++] !== 255 && l(); t < e.length && e[t] === 255;) t++;
57
+ t >= e.length && l();
54
58
  let n = e[t++];
55
- if ((n === 0 || n === 216 || n === 217 || n === 218) && s(), n === 1 || n >= 208 && n <= 215) continue;
56
- t + 2 > e.length && s();
57
- let r = a(e, t);
58
- if ((r < 2 || t + r > e.length) && s(), [
59
+ if ((n === 0 || n === 216 || n === 217 || n === 218) && l(), n === 1 || n >= 208 && n <= 215) continue;
60
+ t + 2 > e.length && l();
61
+ let r = s(e, t);
62
+ if ((r < 2 || t + r > e.length) && l(), [
59
63
  192,
60
64
  193,
61
65
  194,
@@ -69,120 +73,124 @@ function c(e) {
69
73
  205,
70
74
  206,
71
75
  207
72
- ].includes(n)) return (r < 8 || e[t + 2] === 0 || e[t + 7] === 0 || r !== 8 + e[t + 7] * 3) && s(), {
73
- width: a(e, t + 5),
74
- height: a(e, t + 3),
76
+ ].includes(n)) return (r < 8 || e[t + 2] === 0 || e[t + 7] === 0 || r !== 8 + e[t + 7] * 3) && l(), {
77
+ width: s(e, t + 5),
78
+ height: s(e, t + 3),
75
79
  format: "jpeg"
76
80
  };
77
81
  t += r;
78
82
  }
79
- s();
83
+ l();
80
84
  }
81
- if (o(e, 0, [
85
+ if (c(e, 0, [
82
86
  82,
83
87
  73,
84
88
  70,
85
89
  70
86
- ]) && o(e, 8, [
90
+ ]) && c(e, 8, [
87
91
  87,
88
92
  69,
89
93
  66,
90
94
  80
91
95
  ])) {
92
- (e.length < 20 || i(e, 4) !== e.length - 8) && s();
93
- let t = i(e, 16);
94
- if ((t > e.length - 20 || t % 2 + t + 20 > e.length) && s(), o(e, 12, [
96
+ (e.length < 20 || o(e, 4) !== e.length - 8) && l();
97
+ let t = o(e, 16);
98
+ if ((t > e.length - 20 || t % 2 + t + 20 > e.length) && l(), c(e, 12, [
95
99
  86,
96
100
  80,
97
101
  56,
98
102
  88
99
- ])) return (t !== 10 || e[20] & 193 || e[21] !== 0 || e[22] !== 0 || e[23] !== 0) && s(), {
103
+ ])) return (t !== 10 || e[20] & 193 || e[21] !== 0 || e[22] !== 0 || e[23] !== 0) && l(), {
100
104
  width: 1 + e[24] + (e[25] << 8) + (e[26] << 16),
101
105
  height: 1 + e[27] + (e[28] << 8) + (e[29] << 16),
102
106
  format: "webp"
103
107
  };
104
- if (o(e, 12, [
108
+ if (c(e, 12, [
105
109
  86,
106
110
  80,
107
111
  56,
108
112
  76
109
113
  ])) {
110
- (t < 5 || e[20] !== 47 || e[24] & 240) && s();
111
- let n = i(e, 21);
114
+ (t < 5 || e[20] !== 47 || e[24] & 240) && l();
115
+ let n = o(e, 21);
112
116
  return {
113
117
  width: (n & 16383) + 1,
114
118
  height: (n >>> 14 & 16383) + 1,
115
119
  format: "webp"
116
120
  };
117
121
  }
118
- if (o(e, 12, [
122
+ if (c(e, 12, [
119
123
  86,
120
124
  80,
121
125
  56,
122
126
  32
123
- ])) return (t < 10 || e[20] & 1 || !o(e, 23, [
127
+ ])) return (t < 10 || e[20] & 1 || !c(e, 23, [
124
128
  157,
125
129
  1,
126
130
  42
127
- ])) && s(), {
131
+ ])) && l(), {
128
132
  width: (e[27] << 8 | e[26]) & 16383,
129
133
  height: (e[29] << 8 | e[28]) & 16383,
130
134
  format: "webp"
131
135
  };
132
- s();
136
+ l();
133
137
  }
134
- s();
138
+ l();
135
139
  }
136
- function l({ width: n, height: r }) {
140
+ function d({ width: n, height: r }) {
137
141
  if (!Number.isSafeInteger(n) || !Number.isSafeInteger(r) || n < 1 || r < 1 || n > 8192 || r > 8192 || n * r > 33554432) throw Error(`image-process: source dimensions ${n}x${r} exceed image limits (${e}px side, ${t} pixels)`);
138
142
  }
139
- function u(t, n) {
143
+ function f(t, n) {
140
144
  if (t == null) return null;
141
145
  if (typeof t != "number" || !Number.isInteger(t) || t <= 0) throw Error(`image-process: options.${n} must be a positive integer, got ${JSON.stringify(t)}`);
142
146
  if (t > 8192) throw Error(`image-process: options.${n} ${t} exceeds the ${e}px limit`);
143
147
  return t;
144
148
  }
145
- function d(e, n) {
146
- let r = u(e.width, "width") ?? n.width, i = u(e.height, "height") ?? n.height, a = {
149
+ async function p(e) {
150
+ let t = new Uint8Array(await e.arrayBuffer()), n = "", r = 32768;
151
+ for (let e = 0; e < t.length; e += r) n += String.fromCharCode.apply(null, t.subarray(e, e + r));
152
+ return btoa(n);
153
+ }
154
+ function m(e, n) {
155
+ let r = f(e.width, "width") ?? n.width, i = f(e.height, "height") ?? n.height, a = {
147
156
  width: Math.max(1, Math.min(r, n.width)),
148
157
  height: Math.max(1, Math.min(i, n.height))
149
158
  };
150
159
  if (a.width * a.height > 33554432) throw Error(`image-process: canvas ${a.width}x${a.height} exceeds the ${t} pixel limit`);
151
160
  return a;
152
161
  }
153
- var f = async (e) => {
154
- let { operation: t, imageBase64: r, mimeType: i, options: a } = e, o = performance.now();
155
- if (typeof r != "string" || r.length > 22369624 || !/^[A-Za-z0-9+/]*={0,2}$/.test(r) || r.length % 4 != 0) throw Error(`image-process: invalid base64 or image exceeds ${n} byte limit`);
156
- let s = r.endsWith("==") ? 2 : +!!r.endsWith("="), u = r.length / 4 * 3 - s;
157
- if (u > 16777216) throw Error(`image-process: image exceeds ${n} byte limit`);
158
- let f = await (await fetch(`data:${i};base64,${r}`)).blob();
159
- if (f.size > 16777216) throw Error(`image-process: image exceeds ${n} byte limit`);
160
- let p = c(new Uint8Array(await f.arrayBuffer()));
161
- if (l(p), i !== `image/${p.format}`) throw Error(`image-process: declared MIME ${i} does not match ${p.format} image`);
162
- let m = await createImageBitmap(f);
162
+ var h = async (e) => {
163
+ let { operation: t, imageBase64: a, mimeType: o, options: s } = e, c = performance.now();
164
+ if (typeof a != "string" || a.length > 22369624 || !/^[A-Za-z0-9+/]*={0,2}$/.test(a) || a.length % 4 != 0) throw Error(`image-process: invalid base64 or image exceeds ${n} byte limit`);
165
+ let l = a.endsWith("==") ? 2 : +!!a.endsWith("="), f = a.length / 4 * 3 - l;
166
+ if (f > 16777216) throw Error(`image-process: image exceeds ${n} byte limit`);
167
+ let h = await (await fetch(`data:${o};base64,${a}`)).blob();
168
+ if (h.size > 16777216) throw Error(`image-process: image exceeds ${n} byte limit`);
169
+ let g = u(new Uint8Array(await h.arrayBuffer()));
170
+ if (d(g), o !== `image/${g.format}`) throw Error(`image-process: declared MIME ${o} does not match ${g.format} image`);
171
+ let _ = await createImageBitmap(h);
163
172
  try {
164
- if (m.width !== p.width || m.height !== p.height) throw Error("image-process: decoded image dimensions differ from image header");
165
- let e = d(a, m), n = new OffscreenCanvas(e.width, e.height), r = n.getContext("2d");
166
- if (!r) throw Error("Failed to get OffscreenCanvas context");
167
- t === "grayscale" && (r.filter = "grayscale(100%)"), r.drawImage(m, 0, 0, n.width, n.height);
168
- let s = a.outputFormat || i.split("/")[1], c = `image/${s}`, l = await n.convertToBlob({
169
- type: c,
170
- quality: a.quality || .8
171
- }), h = new FileReader(), g = await new Promise((e, t) => {
172
- h.onloadend = () => {
173
- let t = h.result.split(",")[1];
174
- e(t);
175
- }, h.onerror = t, h.readAsDataURL(l);
173
+ if (_.width !== g.width || _.height !== g.height) throw Error("image-process: decoded image dimensions differ from image header");
174
+ let e = m(s, _), n = new OffscreenCanvas(e.width, e.height), a = n.getContext("2d");
175
+ if (!a) throw Error("Failed to get OffscreenCanvas context");
176
+ t === "grayscale" && (a.filter = "grayscale(100%)"), a.drawImage(_, 0, 0, n.width, n.height);
177
+ let l = s.outputFormat ?? o.split("/")[1];
178
+ if (typeof l != "string" || !i.includes(l)) throw Error(`image-process: unsupported output format ${JSON.stringify(l)} (allowed: ${i.join(", ")})`);
179
+ let u = typeof s.quality == "number" && s.quality > 0 && s.quality <= 1 ? s.quality : .8, d = `image/${l}`, v = await n.convertToBlob({
180
+ type: d,
181
+ quality: u
176
182
  });
177
- return console.log(`[flaxia-node] image-process: done operation=${t} output=${s} size=${e.width}x${e.height} inputBytes=${u} resultBytes=${l.size} durationMs=${Math.round(performance.now() - o)}`), {
178
- imageBase64: g,
179
- mimeType: c,
180
- originalSizeBytes: f.size,
181
- resultSizeBytes: l.size
183
+ if (v.size > 8388608) throw Error(`image-process: encoded output ${v.size} bytes exceeds the ${r} byte limit`);
184
+ let y = await p(v);
185
+ return console.log(`[flaxia-node] image-process: done operation=${t} output=${l} size=${e.width}x${e.height} inputBytes=${f} resultBytes=${v.size} durationMs=${Math.round(performance.now() - c)}`), {
186
+ imageBase64: y,
187
+ mimeType: d,
188
+ originalSizeBytes: h.size,
189
+ resultSizeBytes: v.size
182
190
  };
183
191
  } finally {
184
- m.close();
192
+ _.close();
185
193
  }
186
194
  };
187
195
  //#endregion
188
- export { f as handleImageProcess };
196
+ export { h as handleImageProcess };
@@ -1,4 +1,4 @@
1
1
  [diffend] Oversized file quarantined before diffing.
2
2
  name: package/dist/assets/nudenet.js
3
- size: 70464329 bytes
4
- sha256: d6b0662a4da0668cefa3583cd693e3652bec3a34a59cc459b7a66bb01a2060b0
3
+ size: 70464735 bytes
4
+ sha256: 59843438bd324c58dbd0e2b98b5ca53e6fcdc54b919901efa77ce981778e818c
@@ -1,5 +1,5 @@
1
1
  //#region src/vector-store/HNSWIndex.ts
2
- var e = 1e5, t = 1024, n = class {
2
+ var e = 2e4, t = 1024, n = class {
3
3
  constructor(e, t = "cosine", n = 16, r = 200) {
4
4
  this.dimensions = e, this.metric = t, this.M = n, this.efConstruction = r, this.nodes = /* @__PURE__ */ new Map(), this.docIdMap = /* @__PURE__ */ new Map(), this.enterPoint = null, this.nextNodeId = 0, this.maxLevel = 0;
5
5
  }
@@ -7,7 +7,7 @@ var e = 1e5, t = 1024, n = class {
7
7
  return this.dimensions;
8
8
  }
9
9
  insert(t, n) {
10
- if (this.nodes.size >= 1e5) throw Error(`HNSW index is full (${e} nodes); refusing to insert more`);
10
+ if (this.nodes.size >= 2e4) throw Error(`HNSW index is full (${e} nodes); refusing to insert more`);
11
11
  if (!(n instanceof Float32Array) || n.length !== this.dimensions) throw Error(`HNSW insert: vector must be a Float32Array of ${this.dimensions} dimensions, got ${n?.length ?? "none"}`);
12
12
  let r = this.nextNodeId++, i = this.randomLevel(), a = {
13
13
  id: r,
@@ -37,7 +37,7 @@ var e = 1e5, t = 1024, n = class {
37
37
  for (let e = Math.min(i, this.maxLevel); e >= 0; e--) {
38
38
  let t = this.searchLayer(n, o, e, this.efConstruction), i = this.selectNeighbors(t, this.M), a = o.neighbors.get(e) || [];
39
39
  for (let e of i) a.includes(e.nodeId) || a.push(e.nodeId);
40
- o.neighbors.set(e, a);
40
+ o.neighbors.set(e, a.slice(-this.M));
41
41
  for (let t of i) {
42
42
  let n = this.nodes.get(t.nodeId);
43
43
  if (n) {
@@ -74,31 +74,33 @@ var e = 1e5, t = 1024, n = class {
74
74
  let i = new Set([t.id]), a = [{
75
75
  nodeId: t.id,
76
76
  distance: this.distance(e, t.vector)
77
- }], o = [...a], s = /* @__PURE__ */ new Map();
78
- for (s.set(t.id, a[0].distance); a.length > 0;) {
77
+ }], o = [a[0]], s = a[0].distance;
78
+ for (; a.length > 0;) {
79
79
  let t = 0;
80
80
  for (let e = 1; e < a.length; e++) a[e].distance < a[t].distance && (t = e);
81
- let c = a[t], l = o.length > 0 ? Math.max(...o.map((e) => e.distance)) : Infinity;
82
- if (c.distance > l && o.length >= r) break;
81
+ let c = a[t];
82
+ if (c.distance > s && o.length >= r) break;
83
83
  a.splice(t, 1);
84
- let u = this.nodes.get(c.nodeId);
85
- if (!u) continue;
86
- let d = u.neighbors.get(n) || [];
87
- for (let t of d) {
84
+ let l = this.nodes.get(c.nodeId);
85
+ if (!l) continue;
86
+ let u = l.neighbors.get(n) || [];
87
+ for (let t of u) {
88
88
  if (i.has(t)) continue;
89
89
  i.add(t);
90
90
  let n = this.nodes.get(t);
91
91
  if (!n) continue;
92
92
  let c = this.distance(e, n.vector);
93
- s.set(t, c);
94
- let l = o.length > 0 ? Math.max(...o.map((e) => e.distance)) : Infinity;
95
- (o.length < r || c < l) && (a.push({
96
- nodeId: t,
97
- distance: c
98
- }), o.push({
99
- nodeId: t,
100
- distance: c
101
- }), o.length > r && (o.sort((e, t) => t.distance - e.distance), o.pop()));
93
+ if (o.length < r || c < s) {
94
+ let e = {
95
+ nodeId: t,
96
+ distance: c
97
+ };
98
+ if (a.push(e), o.push(e), c > s && (s = c), o.length > r) {
99
+ let e = 0;
100
+ for (let t = 1; t < o.length; t++) o[t].distance > o[e].distance && (e = t);
101
+ o.splice(e, 1), s = o.length > 0 ? o.reduce((e, t) => t.distance > e ? t.distance : e, -Infinity) : Infinity;
102
+ }
103
+ }
102
104
  }
103
105
  }
104
106
  return o.sort((e, t) => e.distance - t.distance), o.slice(0, r);
@@ -142,7 +144,7 @@ var e = 1e5, t = 1024, n = class {
142
144
  }
143
145
  importNodes(n, r) {
144
146
  if (!Array.isArray(n)) throw Error("HNSW import: nodes must be an array");
145
- if (n.length > 1e5) throw Error(`HNSW import: ${n.length} nodes exceed the ${e} quota`);
147
+ if (n.length > 2e4) throw Error(`HNSW import: ${n.length} nodes exceed the ${e} quota`);
146
148
  let i = /* @__PURE__ */ new Set();
147
149
  for (let e of n) {
148
150
  if (!e || typeof e != "object") throw Error("HNSW import: node entry must be an object");
@@ -221,13 +223,20 @@ function f(e, t, n) {
221
223
  a.onsuccess = () => r(), a.onerror = () => i(a.error);
222
224
  });
223
225
  }
224
- function p(e, t) {
226
+ function p(e, t, n) {
227
+ return new Promise((r, i) => {
228
+ let a = e.transaction(t, "readwrite"), o = a.objectStore(t);
229
+ for (let e of n) o.put(e);
230
+ a.oncomplete = () => r(), a.onerror = () => i(a.error);
231
+ });
232
+ }
233
+ function m(e, t) {
225
234
  return new Promise((n, r) => {
226
235
  let i = u(e, t).getAll();
227
236
  i.onsuccess = () => n(i.result), i.onerror = () => r(i.error);
228
237
  });
229
238
  }
230
- var m = class {
239
+ var h = class {
231
240
  constructor() {
232
241
  this.db = null, this.hnsw = null, this.shardInfo = null, this.saveCounter = 0;
233
242
  }
@@ -304,31 +313,48 @@ var m = class {
304
313
  getVectorCount() {
305
314
  return this.hnsw?.size() || 0;
306
315
  }
316
+ close() {
317
+ this.hnsw = null, this.db &&= (this.db.close(), null), this.shardInfo = null, this.saveCounter = 0;
318
+ }
307
319
  async loadGraphFromDB() {
308
- let e = await p(this.db, "hnsw-graph");
320
+ let e = await m(this.db, "hnsw-graph");
309
321
  if (e.length === 0) return;
310
322
  if (e.length > a) throw Error(`vector-store: persisted graph holds ${e.length} nodes, above the ${a} quota`);
311
- let t = /* @__PURE__ */ new Map(), n = [];
312
- for (let r of e) {
313
- let e = await d(this.db, "vectors", r.docId);
314
- e && e.vector && t.set(r.nodeId, e.vector instanceof Float32Array ? e.vector : new Float32Array(e.vector)), n.push({
315
- id: r.nodeId,
316
- docId: r.docId,
317
- level: r.level,
318
- neighbors: r.neighbors
319
- });
323
+ let t = /* @__PURE__ */ new Map(), n = [], r = await m(this.db, "vectors"), i = /* @__PURE__ */ new Map();
324
+ for (let t of e) i.set(t.docId, t.nodeId);
325
+ for (let e of r) {
326
+ let n = i.get(e.docId);
327
+ n !== void 0 && t.set(n, e.vector instanceof Float32Array ? e.vector : new Float32Array(e.vector));
320
328
  }
329
+ for (let t of e) n.push({
330
+ id: t.nodeId,
331
+ docId: t.docId,
332
+ level: t.level,
333
+ neighbors: t.neighbors
334
+ });
321
335
  this.hnsw.importNodes(n, t);
322
336
  }
323
337
  async saveGraphSnapshot() {
324
- let e = this.hnsw.exportNodes();
325
- for (let [t, n] of e) await f(this.db, "hnsw-graph", {
326
- nodeId: t,
327
- docId: n.docId,
328
- level: n.level,
329
- neighbors: n.neighbors
338
+ let e = this.hnsw.exportNodes(), t = [];
339
+ for (let [n, r] of e) t.push({
340
+ nodeId: n,
341
+ docId: r.docId,
342
+ level: r.level,
343
+ neighbors: r.neighbors
330
344
  });
345
+ await p(this.db, "hnsw-graph", t);
346
+ }
347
+ }, g = null;
348
+ function _() {
349
+ if (!g) {
350
+ let e = new h();
351
+ return e.initialize().then(() => (g = e, e));
331
352
  }
332
- };
353
+ return Promise.resolve(g);
354
+ }
355
+ function v() {
356
+ let e = g;
357
+ g = null, e?.close();
358
+ }
333
359
  //#endregion
334
- export { m as t };
360
+ export { v as n, _ as t };
@@ -1,12 +1,20 @@
1
1
  import { n as e } from "./egress-guard.js";
2
2
  import { o as t } from "./ai-model-registry.js";
3
3
  //#region src/workloads/vector-embed.ts
4
- var n = "onnx-community/embeddinggemma-2-ONNX", r = 768, i = 16e3, a = 5 * 1024 * 1024, o = 25 * 1024 * 1024, s = 50 * 1024 * 1024, c = 3e4, l = 1e5, u = null, d = null, f = () => {
5
- u = null, d = null;
6
- }, p = !1;
7
- function m(e, t) {
8
- if (p) return;
9
- p = !0;
4
+ var n = "onnx-community/embeddinggemma-2-ONNX", r = 768, i = 16e3, a = 5 * 1024 * 1024, o = 25 * 1024 * 1024, s = 50 * 1024 * 1024, c = 16 * 1024 * 1024, l = 30 * i, u = 3e4, d = 1e5, f = null, p = null, m = () => {
5
+ let e = f;
6
+ f = null, p = null, e && (async () => {
7
+ try {
8
+ e.model?.dispose?.();
9
+ } catch {}
10
+ try {
11
+ e.processor?.dispose?.();
12
+ } catch {}
13
+ })();
14
+ }, h = !1;
15
+ function g(e, t) {
16
+ if (h) return;
17
+ h = !0;
10
18
  let n = typeof e.fetch == "function" ? e.fetch.bind(e) : globalThis.fetch.bind(globalThis);
11
19
  e.fetch = async (e, r) => {
12
20
  let i = new URL(String(e));
@@ -16,15 +24,15 @@ function m(e, t) {
16
24
  return a;
17
25
  };
18
26
  }
19
- async function h() {
20
- if (u) return u;
21
- if (d) return d;
27
+ async function _() {
28
+ if (f) return f;
29
+ if (p) return p;
22
30
  let e = performance.now();
23
- return d = (async () => {
31
+ return p = (async () => {
24
32
  let r = t(n);
25
33
  console.log(`[flaxia-node] vector-embed: loading model=${r.repo} revision=${r.revision} device=wasm dtype=fp16`);
26
34
  let { AutoModel: i, AutoProcessor: a, env: o } = await import("./transformers.web.js").then((e) => e.i);
27
- o?.backends?.onnx?.wasm && (o.backends.onnx.wasm.numThreads = 1, o.backends.onnx.wasm.proxy = !1), m(o, r.maxBytes);
35
+ o?.backends?.onnx?.wasm && (o.backends.onnx.wasm.numThreads = 1, o.backends.onnx.wasm.proxy = !1), g(o, r.maxBytes);
28
36
  let [s, c] = await Promise.all([a.from_pretrained(r.repo, { revision: r.revision }), i.from_pretrained(r.repo, {
29
37
  device: "wasm",
30
38
  dtype: "fp16",
@@ -33,14 +41,14 @@ async function h() {
33
41
  processor: s,
34
42
  model: c
35
43
  };
36
- return u = l, console.log(`[flaxia-node] vector-embed: model ready loadMs=${Math.round(performance.now() - e)}`), l;
44
+ return f = l, console.log(`[flaxia-node] vector-embed: model ready loadMs=${Math.round(performance.now() - e)}`), l;
37
45
  })().catch((e) => {
38
- throw d = null, console.error(`[flaxia-node] vector-embed: model load FAILED error=${e instanceof Error ? e.message : String(e)}`), e;
39
- }), d;
46
+ throw p = null, console.error(`[flaxia-node] vector-embed: model load FAILED error=${e instanceof Error ? e.message : String(e)}`), e;
47
+ }), p;
40
48
  }
41
- function g(e) {
49
+ function v(e) {
42
50
  let t = typeof e.text == "string" ? e.text : "";
43
- if (t.length > l) throw Error(`vector-embed: text exceeds ${l} characters`);
51
+ if (t.length > d) throw Error(`vector-embed: text exceeds ${d} characters`);
44
52
  let n = e.imageUrls ?? [];
45
53
  if (!Array.isArray(n) || n.length > 29) throw Error("vector-embed: imageUrls must hold 0..29 URLs");
46
54
  for (let e of n) if (typeof e != "string" || !e.startsWith("https://")) throw Error("vector-embed: imageUrls must be https:// URLs");
@@ -56,60 +64,65 @@ function g(e) {
56
64
  videoUrl: i
57
65
  };
58
66
  }
59
- function _(e, t) {
67
+ function y(e, t) {
60
68
  let n = e, r = (e) => n.split(e).length - 1;
61
69
  return t.images > r("<|image|>") && (n += " <|image|>".repeat(t.images - r("<|image|>"))), t.videos > r("<|video|>") && (n += " <|video|>".repeat(t.videos - r("<|video|>"))), t.audio > r("<|audio|>") && (n += " <|audio|>".repeat(t.audio - r("<|audio|>"))), n;
62
70
  }
63
- async function v(t) {
71
+ async function b(t) {
64
72
  let n = await e({
65
73
  url: t,
66
74
  allowContentTypes: ["image/*", "application/octet-stream"],
67
75
  maxBytes: a,
68
- timeoutMs: c
76
+ timeoutMs: u
69
77
  });
70
78
  if (!n.ok) throw Error(`vector-embed: image fetch failed (${n.status}): ${t}`);
71
79
  let r = new Blob([n.bytes], { type: n.contentType || "application/octet-stream" }), i = URL.createObjectURL(r);
72
80
  try {
73
- let { loadImage: e } = await import("./transformers.web.js").then((e) => e.i);
74
- return await e(i);
81
+ let { loadImage: e } = await import("./transformers.web.js").then((e) => e.i), n = await e(i);
82
+ if (!Number.isSafeInteger(n.width) || !Number.isSafeInteger(n.height) || n.width < 1 || n.height < 1 || n.width * n.height > 16777216) throw Error(`vector-embed: image ${t} decodes to ${n.width}x${n.height}, above the ${c} pixel limit`);
83
+ return n;
75
84
  } finally {
76
85
  URL.revokeObjectURL(i);
77
86
  }
78
87
  }
79
- async function y(t) {
88
+ async function x(t) {
80
89
  let n = await e({
81
90
  url: t,
82
91
  allowContentTypes: ["audio/*", "application/octet-stream"],
83
92
  maxBytes: o,
84
- timeoutMs: c
93
+ timeoutMs: u
85
94
  });
86
95
  if (!n.ok) throw Error(`vector-embed: audio fetch failed (${n.status}): ${t}`);
87
96
  let r = new Blob([n.bytes], { type: n.contentType || "application/octet-stream" }), a = URL.createObjectURL(r);
88
97
  try {
89
- let { loadAudio: e } = await import("./transformers.web.js").then((e) => e.i);
90
- return await e(a, i);
98
+ let { loadAudio: e } = await import("./transformers.web.js").then((e) => e.i), n = await e(a, i);
99
+ if (!n || n.length > 48e4) throw Error(`vector-embed: audio ${t} decodes to ${n?.length ?? 0} samples, above the ${l} sample limit`);
100
+ return n;
91
101
  } finally {
92
102
  URL.revokeObjectURL(a);
93
103
  }
94
104
  }
95
- async function b(t) {
105
+ async function S(t) {
96
106
  let n = await e({
97
107
  url: t,
98
108
  allowContentTypes: ["video/*", "application/octet-stream"],
99
109
  maxBytes: s,
100
- timeoutMs: c
110
+ timeoutMs: u
101
111
  });
102
112
  if (!n.ok) throw Error(`vector-embed: video fetch failed (${n.status}): ${t}`);
103
113
  let r = new Blob([n.bytes], { type: n.contentType || "application/octet-stream" }), i = URL.createObjectURL(r);
104
114
  try {
105
115
  let { loadVideo: e } = await import("./transformers.web.js").then((e) => e.i);
106
- return await e(i, { fps: 1 });
116
+ return await e(i, {
117
+ fps: 1,
118
+ num_frames: 16
119
+ });
107
120
  } finally {
108
121
  URL.revokeObjectURL(i);
109
122
  }
110
123
  }
111
- async function x(e) {
112
- let t = performance.now(), { text: i, imageUrls: a, audioUrl: o, videoUrl: s } = g(e), c = await h(), l = performance.now();
124
+ async function C(e) {
125
+ let t = performance.now(), { text: i, imageUrls: a, audioUrl: o, videoUrl: s } = v(e), c = await _(), l = performance.now();
113
126
  if (!(a.length > 0 || o || s)) {
114
127
  let e = await c.processor(i), a = (await c.model(e))?.sentence_embedding;
115
128
  if (!a || !a.data) throw Error("vector-embed: model did not return sentence_embedding");
@@ -122,14 +135,14 @@ async function x(e) {
122
135
  };
123
136
  }
124
137
  let u = [];
125
- for (let e of a) u.push(await v(e));
126
- let d = o ? await y(o) : null, f = s ? await b(s) : null, p = _(i || " ", {
138
+ for (let e of a) u.push(await b(e));
139
+ let d = o ? await x(o) : null, f = s ? await S(s) : null, p = y(i || " ", {
127
140
  images: u.length,
128
141
  videos: +!!f,
129
142
  audio: +!!d
130
- }), m = u.length === 0 ? null : u.length === 1 ? u[0] : u, x = await c.processor(p, m, d, f), S = (await c.model(x))?.sentence_embedding;
131
- if (!S || !S.data) throw Error("vector-embed: model did not return sentence_embedding");
132
- let C = Array.from(S.data), w = Math.round(performance.now() - t);
143
+ }), m = u.length === 0 ? null : u.length === 1 ? u[0] : u, h = await c.processor(p, m, d, f), g = (await c.model(h))?.sentence_embedding;
144
+ if (!g || !g.data) throw Error("vector-embed: model did not return sentence_embedding");
145
+ let C = Array.from(g.data), w = Math.round(performance.now() - t);
133
146
  return console.log(`[flaxia-node] vector-embed: done dims=${C.length} modelMs=${Math.round(performance.now() - l)} totalMs=${w} textLen=${i.length} images=${a.length} audio=${+!!o} video=${+!!s}`), {
134
147
  vector: C,
135
148
  model: n,
@@ -138,4 +151,4 @@ async function x(e) {
138
151
  };
139
152
  }
140
153
  //#endregion
141
- export { x as handleVectorEmbed, f as releaseCache };
154
+ export { C as handleVectorEmbed, m as releaseCache };
@@ -1,10 +1,10 @@
1
- import { t as e } from "./VectorStoreEngine.js";
1
+ import { n as e, t } from "./sharedEngine.js";
2
2
  //#region src/workloads/vector-query.ts
3
- var t = null, n = () => {
4
- t = null;
3
+ var n = () => {
4
+ e();
5
5
  };
6
6
  async function r() {
7
- return t || (t = new e(), await t.initialize()), t;
7
+ return t();
8
8
  }
9
9
  async function i(e) {
10
10
  let t = await r(), n = performance.now();
@@ -1,10 +1,10 @@
1
- import { t as e } from "./VectorStoreEngine.js";
1
+ import { n as e, t } from "./sharedEngine.js";
2
2
  //#region src/workloads/vector-store.ts
3
- var t = null, n = () => {
4
- t = null;
3
+ var n = () => {
4
+ e();
5
5
  };
6
6
  async function r() {
7
- return t || (t = new e(), await t.initialize()), t;
7
+ return t();
8
8
  }
9
9
  async function i(e) {
10
10
  let t = await r(), n = performance.now();