@flaxia/node 0.7.0 → 0.7.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.
Files changed (48) hide show
  1. package/dist/assets/ai-inference.js +1 -1
  2. package/dist/assets/cc-extract.js +20 -20
  3. package/dist/assets/container.js +59 -43
  4. package/dist/assets/decision.js +43 -36
  5. package/dist/assets/embed-gemma.js +28 -20
  6. package/dist/assets/image-process.js +78 -70
  7. package/dist/assets/nudenet.js +2 -2
  8. package/dist/assets/{VectorStoreEngine.js → sharedEngine.js} +67 -41
  9. package/dist/assets/vector-embed.js +49 -36
  10. package/dist/assets/vector-query.js +4 -4
  11. package/dist/assets/vector-store.js +4 -4
  12. package/dist/assets/webgpuProbe.js +87 -47
  13. package/dist/client/SignalingClient.d.ts.map +1 -1
  14. package/dist/client/light-preloader.d.ts +8 -0
  15. package/dist/client/light-preloader.d.ts.map +1 -1
  16. package/dist/executor/WorkerPool.d.ts +6 -0
  17. package/dist/executor/WorkerPool.d.ts.map +1 -1
  18. package/dist/executor/__tests__/resultLimits.test.d.ts +2 -0
  19. package/dist/executor/__tests__/resultLimits.test.d.ts.map +1 -0
  20. package/dist/executor/container-executor.d.ts +15 -0
  21. package/dist/executor/container-executor.d.ts.map +1 -1
  22. package/dist/executor/memoryProbe.d.ts +25 -1
  23. package/dist/executor/memoryProbe.d.ts.map +1 -1
  24. package/dist/executor/resultLimits.d.ts +19 -0
  25. package/dist/executor/resultLimits.d.ts.map +1 -0
  26. package/dist/index.js +420 -396
  27. package/dist/vector-store/HNSWIndex.d.ts +4 -1
  28. package/dist/vector-store/HNSWIndex.d.ts.map +1 -1
  29. package/dist/vector-store/VectorStoreEngine.d.ts +7 -1
  30. package/dist/vector-store/VectorStoreEngine.d.ts.map +1 -1
  31. package/dist/vector-store/__tests__/sharedEngine.test.d.ts +2 -0
  32. package/dist/vector-store/__tests__/sharedEngine.test.d.ts.map +1 -0
  33. package/dist/vector-store/sharedEngine.d.ts +13 -0
  34. package/dist/vector-store/sharedEngine.d.ts.map +1 -0
  35. package/dist/worker.js +163 -160
  36. package/dist/workloads/__tests__/nudenet-session-cache.test.d.ts +2 -0
  37. package/dist/workloads/__tests__/nudenet-session-cache.test.d.ts.map +1 -0
  38. package/dist/workloads/decision.d.ts +2 -0
  39. package/dist/workloads/decision.d.ts.map +1 -1
  40. package/dist/workloads/embed-gemma.d.ts.map +1 -1
  41. package/dist/workloads/image-process.d.ts +8 -0
  42. package/dist/workloads/image-process.d.ts.map +1 -1
  43. package/dist/workloads/nudenet.d.ts.map +1 -1
  44. package/dist/workloads/vector-embed.d.ts +6 -0
  45. package/dist/workloads/vector-embed.d.ts.map +1 -1
  46. package/dist/workloads/vector-query.d.ts.map +1 -1
  47. package/dist/workloads/vector-store.d.ts.map +1 -1
  48. package/package.json +2 -2
@@ -1,5 +1,5 @@
1
1
  import { t as e } from "./chunk.js";
2
- import { L as t, y as n } from "./webgpuProbe.js";
2
+ import { B as t, S as n } from "./webgpuProbe.js";
3
3
  import { n as r, r as i, t as a } from "./transformers.web.js";
4
4
  import { a as o, i as s, n as c, o as l, r as u, s as d, t as f } from "./ai-model-registry.js";
5
5
  var p = class {
@@ -1,4 +1,4 @@
1
- import { D as e, N as t, O as n, U as r, _ as i, l as a, q as o, s } from "./webgpuProbe.js";
1
+ import { A as e, I as t, K as n, X as r, b as i, f as a, j as o, u as s } from "./webgpuProbe.js";
2
2
  //#region ../../node_modules/parse5/dist/common/unicode.js
3
3
  var c = new Set([
4
4
  65534,
@@ -5045,49 +5045,49 @@ function Di(e, t) {
5045
5045
  return "";
5046
5046
  }
5047
5047
  function Oi(e, t) {
5048
- let r = ai(e), i = Si(r, (e) => e.tagName?.toLowerCase() === "meta"), a = Si(r, (e) => e.tagName?.toLowerCase() === "link"), o = $(r, "html"), s = Ei($(r, "title")) || Di(i, ["og:title", "twitter:title"]) || Ei($(r, "h1")) || t, c = Di(i, [
5048
+ let n = ai(e), r = Si(n, (e) => e.tagName?.toLowerCase() === "meta"), i = Si(n, (e) => e.tagName?.toLowerCase() === "link"), a = $(n, "html"), s = Ei($(n, "title")) || Di(r, ["og:title", "twitter:title"]) || Ei($(n, "h1")) || t, c = Di(r, [
5049
5049
  "description",
5050
5050
  "og:description",
5051
5051
  "twitter:description"
5052
- ]), l = Q(a.find((e) => (Q(e, "rel") || "").toLowerCase().split(/\s+/).includes("canonical")), "href"), u;
5052
+ ]), l = Q(i.find((e) => (Q(e, "rel") || "").toLowerCase().split(/\s+/).includes("canonical")), "href"), u;
5053
5053
  if (l) try {
5054
5054
  let e = new URL(l, t);
5055
5055
  (e.protocol === "https:" || e.protocol === "http:") && (u = e.toString());
5056
5056
  } catch {}
5057
- let d = Di(i, ["article:published_time", "datepublished"]) || Q($(r, "time"), "datetime") || "", f = $(r, "article") || $(r, "main") || Si(r, (e) => Q(e, "role")?.toLowerCase() === "main")[0] || $(r, "body") || r, p = [];
5057
+ let d = Di(r, ["article:published_time", "datepublished"]) || Q($(n, "time"), "datetime") || "", f = $(n, "article") || $(n, "main") || Si(n, (e) => Q(e, "role")?.toLowerCase() === "main")[0] || $(n, "body") || n, p = [];
5058
5058
  wi(f, p);
5059
5059
  let m = Ti(p.join(" ")), h = !1;
5060
- m.length > 16e3 && (m = m.slice(0, n), h = !0);
5060
+ m.length > 16e3 && (m = m.slice(0, o), h = !0);
5061
5061
  let g = (c || m).slice(0, 320).trim();
5062
5062
  return {
5063
5063
  title: s.slice(0, 300),
5064
5064
  text: m,
5065
5065
  excerpt: g,
5066
5066
  canonicalUrl: u,
5067
- lang: Q(o, "lang")?.trim() || void 0,
5067
+ lang: Q(a, "lang")?.trim() || void 0,
5068
5068
  publishedAt: d || void 0,
5069
5069
  truncated: h
5070
5070
  };
5071
5071
  }
5072
- function ki(n) {
5073
- (!n || typeof n != "object" || Array.isArray(n)) && Z("payload must be an object"), (typeof n.crawl != "string" || !n.crawl) && Z("payload.crawl is required");
5074
- let i = n.records;
5075
- (!Array.isArray(i) || i.length === 0 || i.length > 3) && Z("records must contain 1..3 entries");
5076
- let a = 0;
5077
- for (let e of i) (!e || typeof e != "object") && Z("record must be an object"), (typeof e.url != "string" || !e.url) && Z("record.url is required"), e.url.length > 2048 && Z(`record.url is longer than ${t} characters`), r(e.warcFilename, n.crawl) || Z("record.warcFilename is invalid or does not belong to payload.crawl"), e.fetchUrl !== void 0 && (typeof e.fetchUrl != "string" || e.fetchUrl.length > 4096) && Z(`record.fetchUrl must be a string of at most ${o} characters`), (!Number.isSafeInteger(e.offset) || e.offset < 0) && Z("record.offset is invalid"), (!Number.isSafeInteger(e.length) || e.length < 1) && Z("record.length is invalid"), a += e.length;
5078
- if (n.maxRecordBytes !== void 0) {
5079
- let t = n.maxRecordBytes;
5072
+ function ki(i) {
5073
+ (!i || typeof i != "object" || Array.isArray(i)) && Z("payload must be an object"), (typeof i.crawl != "string" || !i.crawl) && Z("payload.crawl is required");
5074
+ let a = i.records;
5075
+ (!Array.isArray(a) || a.length === 0 || a.length > 3) && Z("records must contain 1..3 entries");
5076
+ let o = 0;
5077
+ for (let e of a) (!e || typeof e != "object") && Z("record must be an object"), (typeof e.url != "string" || !e.url) && Z("record.url is required"), e.url.length > 2048 && Z(`record.url is longer than ${t} characters`), n(e.warcFilename, i.crawl) || Z("record.warcFilename is invalid or does not belong to payload.crawl"), e.fetchUrl !== void 0 && (typeof e.fetchUrl != "string" || e.fetchUrl.length > 4096) && Z(`record.fetchUrl must be a string of at most ${r} characters`), (!Number.isSafeInteger(e.offset) || e.offset < 0) && Z("record.offset is invalid"), (!Number.isSafeInteger(e.length) || e.length < 1) && Z("record.length is invalid"), o += e.length;
5078
+ if (i.maxRecordBytes !== void 0) {
5079
+ let t = i.maxRecordBytes;
5080
5080
  (!Number.isSafeInteger(t) || t < 1 || t > 2097152) && Z(`maxRecordBytes must be within 1..${e}`);
5081
5081
  }
5082
- if (n.timeoutMs !== void 0) {
5083
- let e = n.timeoutMs;
5082
+ if (i.timeoutMs !== void 0) {
5083
+ let e = i.timeoutMs;
5084
5084
  (!Number.isSafeInteger(e) || e < 1e3 || e > 6e4) && Z(`timeoutMs must be within 1000..${s}`);
5085
5085
  }
5086
- let c = n.maxRecordBytes ?? 2097152;
5087
- return a > 4194304 && Z("records exceed the total byte cap"), {
5088
- records: i,
5086
+ let c = i.maxRecordBytes ?? 2097152;
5087
+ return o > 4194304 && Z("records exceed the total byte cap"), {
5088
+ records: a,
5089
5089
  maxRecordBytes: c,
5090
- timeoutMs: n.timeoutMs ?? 3e4
5090
+ timeoutMs: i.timeoutMs ?? 3e4
5091
5091
  };
5092
5092
  }
5093
5093
  async function Ai(e, t = []) {
@@ -1052,8 +1052,19 @@ var v = class extends g {
1052
1052
  super(), this.parent = null, t instanceof Array ? this.contents = new Map(t) : this.contents = t;
1053
1053
  for (let t of this.contents.values()) t instanceof e && (t.parent = this);
1054
1054
  }
1055
- }, w = [], T = 128 * 1024 * 1024, E = ["application/wasm", "application/octet-stream"], D = ["application/octet-stream"], O = 4096, k = 4096, A = 65536;
1056
- function j(e, n = w) {
1055
+ }, w = [], T = 128 * 1024 * 1024, E = ["application/wasm", "application/octet-stream"], D = ["application/octet-stream"], O = 4096, k = 1e6;
1056
+ function A() {
1057
+ let e = [], t = new TextDecoder(), n = 0, r = !1;
1058
+ return {
1059
+ write: (i) => {
1060
+ let a = t.decode(i), o = k - n;
1061
+ return o > 0 && (e.push(a.length > o ? a.slice(0, o) : a), n += Math.min(a.length, o)), a.length > o && (r = !0), i.length;
1062
+ },
1063
+ text: () => e.join("") + (r ? "\n[truncated]" : "")
1064
+ };
1065
+ }
1066
+ var j = 4096, M = 65536;
1067
+ function N(e, n = w) {
1057
1068
  let r;
1058
1069
  try {
1059
1070
  r = new URL(e);
@@ -1065,33 +1076,33 @@ function j(e, n = w) {
1065
1076
  if (!r.pathname.endsWith(".wasm")) throw Error(`Image URL must point to a .wasm file: ${r.pathname}`);
1066
1077
  return t(r);
1067
1078
  }
1068
- function M(e) {
1079
+ function P(e) {
1069
1080
  if (e.byteLength < 8) throw Error("Invalid WASM image: truncated header");
1070
1081
  if (e[0] !== 0 || e[1] !== 97 || e[2] !== 115 || e[3] !== 109) throw Error("Invalid WASM image: bad magic number");
1071
1082
  if (new DataView(e.buffer, e.byteOffset, e.byteLength).getUint32(4, !0) !== 1) throw Error("Unsupported WASM image version");
1072
1083
  let t = 8;
1073
1084
  for (; t < e.byteLength;) {
1074
- let n = e[t++], r = N(e, t);
1085
+ let n = e[t++], r = F(e, t);
1075
1086
  t = r.next;
1076
1087
  let i = t + r.value;
1077
1088
  if (i > e.byteLength) throw Error("Invalid WASM image: truncated section");
1078
1089
  if (n === 5) {
1079
- let n = t, r = N(e, n);
1090
+ let n = t, r = F(e, n);
1080
1091
  n = r.next;
1081
1092
  let a = 0, o = null;
1082
1093
  for (let t = 0; t < r.value; t++) {
1083
- let t = N(e, n);
1094
+ let t = F(e, n);
1084
1095
  n = t.next;
1085
- let r = N(e, n);
1096
+ let r = F(e, n);
1086
1097
  n = r.next;
1087
1098
  let s = (t.value & 2) != 0, c = (t.value & 1) != 0 || s, l = null;
1088
1099
  if (c) {
1089
- let t = N(e, n);
1100
+ let t = F(e, n);
1090
1101
  n = t.next, l = t.value;
1091
1102
  }
1092
1103
  if (n > i) throw Error("Invalid WASM image: malformed memory section");
1093
- if (a = Math.max(a, r.value * A), l !== null) {
1094
- let e = l * A;
1104
+ if (a = Math.max(a, r.value * M), l !== null) {
1105
+ let e = l * M;
1095
1106
  o = o === null ? e : Math.max(o, e);
1096
1107
  }
1097
1108
  }
@@ -1107,7 +1118,7 @@ function M(e) {
1107
1118
  maximumBytes: null
1108
1119
  };
1109
1120
  }
1110
- function N(e, t) {
1121
+ function F(e, t) {
1111
1122
  let n = 0, r = 0, i = t;
1112
1123
  for (;;) {
1113
1124
  if (i >= e.byteLength) throw Error("Invalid WASM image: truncated LEB128 value");
@@ -1119,22 +1130,22 @@ function N(e, t) {
1119
1130
  if (r += 7, r > 28) throw Error("Invalid WASM image: oversized LEB128 value");
1120
1131
  }
1121
1132
  }
1122
- function P(e) {
1133
+ function I(e) {
1123
1134
  let t = e ?? 512;
1124
1135
  if (!Number.isFinite(t)) throw Error(`memoryLimitMb must be a finite number, got ${String(e)}`);
1125
- return Math.min(k, Math.max(32, Math.floor(t)));
1136
+ return Math.min(j, Math.max(32, Math.floor(t)));
1126
1137
  }
1127
- function F(e, t) {
1128
- let n = t * 1024 * 1024, r = M(e);
1138
+ function L(e, t) {
1139
+ let n = t * 1024 * 1024, r = P(e);
1129
1140
  if (r.initialBytes > n) throw Error(`Container image declares ${Math.round(r.initialBytes / 1024 / 1024)}MB of initial memory, above memoryLimitMb=${t}`);
1130
1141
  if (r.maximumBytes === null) throw Error("Container image has no declared maximum memory; memoryLimitMb cannot be enforced");
1131
1142
  if (r.maximumBytes > n) throw Error(`Container image may grow to ${Math.round(r.maximumBytes / 1024 / 1024)}MB, above memoryLimitMb=${t}`);
1132
1143
  return r;
1133
1144
  }
1134
- function I(e) {
1145
+ function R(e) {
1135
1146
  if (!e || e.length > 255 || e.startsWith("/") || e.includes("\\") || /[\u0000-\u001f\u007f]/.test(e) || e.split("/").some((e) => !e || e === "." || e === "..")) throw Error(`Invalid container input mount name: ${e}`);
1136
1147
  }
1137
- function L(e, n) {
1148
+ function z(e, n) {
1138
1149
  let r;
1139
1150
  try {
1140
1151
  r = new URL(e);
@@ -1146,12 +1157,23 @@ function L(e, n) {
1146
1157
  if (!n.includes(r.origin)) throw Error(`Container file-source origin not allowed: ${r.origin}`);
1147
1158
  return t(r);
1148
1159
  }
1149
- function R(e) {
1160
+ function B(e) {
1150
1161
  if (!e || typeof e != "object" || Array.isArray(e)) return !1;
1151
1162
  let t = e;
1152
1163
  return typeof t.url == "string" && t.url.length <= 2048 && typeof t.token == "string" && t.token.length <= 4096 && /^[A-Za-z0-9_-]+\.[a-f0-9]{64}$/.test(t.token) && Number.isSafeInteger(t.size) && t.size >= 0 && t.size <= 26214400 && typeof t.sha256 == "string" && /^[a-f0-9]{64}$/.test(t.sha256);
1153
1164
  }
1154
- async function z(t, n = []) {
1165
+ function V(e, t) {
1166
+ let n;
1167
+ try {
1168
+ n = atob(t);
1169
+ } catch {
1170
+ throw Error(`Container file ${e} is not valid base64`);
1171
+ }
1172
+ let r = new Uint8Array(n.length);
1173
+ for (let e = 0; e < n.length; e++) r[e] = n.charCodeAt(e);
1174
+ return r;
1175
+ }
1176
+ async function H(t, n = []) {
1155
1177
  let r = t.files ?? {}, i = t.fileSources ?? {};
1156
1178
  if (!r || typeof r != "object" || Array.isArray(r)) throw Error("Container files must be a filename-to-base64 map");
1157
1179
  if (!i || typeof i != "object" || Array.isArray(i)) throw Error("Container fileSources must be a filename-to-source map");
@@ -1159,25 +1181,19 @@ async function z(t, n = []) {
1159
1181
  if (a.length + o.length > O) throw Error(`Container input file count exceeds ${O}`);
1160
1182
  let s = /* @__PURE__ */ new Map(), c = 0;
1161
1183
  for (let [e, t] of a) {
1162
- if (I(e), typeof t != "string") throw Error(`Container file ${e} must be base64 text`);
1184
+ if (R(e), typeof t != "string") throw Error(`Container file ${e} must be base64 text`);
1163
1185
  if (t.length > 8738134 * 4) throw Error(`Container file ${e} exceeds the 25 MiB input limit`);
1164
- let n;
1165
- try {
1166
- n = atob(t);
1167
- } catch {
1168
- throw Error(`Container file ${e} is not valid base64`);
1169
- }
1170
- let r = Uint8Array.from(n, (e) => e.charCodeAt(0));
1171
- if (c += r.byteLength, c > 26214400) throw Error("Container input files exceed the 25 MiB limit");
1172
- s.set(e, r);
1186
+ let n = V(e, t);
1187
+ if (c += n.byteLength, c > 26214400) throw Error("Container input files exceed the 25 MiB limit");
1188
+ s.set(e, n);
1173
1189
  }
1174
1190
  for (let [t, r] of o) {
1175
- if (I(t), s.has(t)) throw Error(`Container input file is defined twice: ${t}`);
1176
- if (!R(r)) throw Error(`Container file source ${t} is malformed`);
1191
+ if (R(t), s.has(t)) throw Error(`Container input file is defined twice: ${t}`);
1192
+ if (!B(r)) throw Error(`Container file source ${t} is malformed`);
1177
1193
  let i = r;
1178
1194
  if (c += i.size, c > 26214400) throw Error("Container input files exceed the 25 MiB limit");
1179
1195
  let a = await e({
1180
- url: L(i.url, n),
1196
+ url: z(i.url, n),
1181
1197
  allowContentTypes: D,
1182
1198
  maxBytes: Math.max(1, i.size),
1183
1199
  timeoutMs: 6e4,
@@ -1191,10 +1207,10 @@ async function z(t, n = []) {
1191
1207
  }
1192
1208
  return s;
1193
1209
  }
1194
- var B = async (t, n = [], r = w) => {
1195
- let { image: i, command: a, memoryLimitMb: o } = t, s = j(i, r), c = P(o), l = await z(t, n);
1210
+ var U = async (t, n = [], r = w) => {
1211
+ let { image: i, command: a, memoryLimitMb: o } = t, s = N(i, r), c = I(o), l = await H(t, n);
1196
1212
  console.log(`Starting container: ${i} with command: ${a.join(" ")}`);
1197
- let u = [], d = [], f = { write: (e) => (u.push(new TextDecoder().decode(e)), e.length) }, p = { write: (e) => (d.push(new TextDecoder().decode(e)), e.length) }, m = /* @__PURE__ */ new Map();
1213
+ let u = A(), d = A(), f = { write: u.write }, p = { write: d.write }, m = /* @__PURE__ */ new Map();
1198
1214
  for (let [e, t] of l) m.set(e, new x(t));
1199
1215
  let g = new b("/", m), _ = new h(a, [], [
1200
1216
  new x(new Uint8Array()),
@@ -1207,33 +1223,33 @@ var B = async (t, n = [], r = w) => {
1207
1223
  maxBytes: T
1208
1224
  });
1209
1225
  if (!v.ok) throw Error(`Failed to download container image: ${s.toString()} (HTTP ${v.status})`);
1210
- let y = v.bytes, S = F(y, c);
1226
+ let y = v.bytes, S = L(y, c);
1211
1227
  console.log(`[flaxia-node] container: memory cap ${c}MB (declared initial=${Math.round(S.initialBytes / 1024 / 1024)}MB max=${S.maximumBytes === null ? "unbounded" : `${Math.round(S.maximumBytes / 1024 / 1024)}MB`})`);
1212
1228
  let { instance: C } = await WebAssembly.instantiate(y.slice().buffer, { wasi_snapshot_preview1: _.wasiImport });
1213
1229
  try {
1214
1230
  let e = _.start(C);
1215
1231
  return {
1216
1232
  files: {},
1217
- stdout: u.join(""),
1218
- stderr: d.join(""),
1233
+ stdout: u.text(),
1234
+ stderr: d.text(),
1219
1235
  exitCode: e
1220
1236
  };
1221
1237
  } catch {
1222
1238
  return {
1223
1239
  files: {},
1224
- stdout: u.join(""),
1225
- stderr: d.join(""),
1240
+ stdout: u.text(),
1241
+ stderr: d.text(),
1226
1242
  exitCode: -1
1227
1243
  };
1228
1244
  }
1229
- }, V = async (e, t = [], n) => {
1245
+ }, W = async (e, t = [], n) => {
1230
1246
  let r = performance.now();
1231
1247
  try {
1232
- let i = await B(e, t, n);
1248
+ let i = await U(e, t, n);
1233
1249
  return console.log(`[flaxia-node] container: done durationMs=${Math.round(performance.now() - r)} exitCode=${i.exitCode ?? "?"}`), i;
1234
1250
  } catch (e) {
1235
1251
  throw console.error(`[flaxia-node] container: failed error=${e instanceof Error ? e.message : String(e)}`), e;
1236
1252
  }
1237
1253
  };
1238
1254
  //#endregion
1239
- export { V as handleContainer };
1255
+ export { W as handleContainer };
@@ -1,10 +1,15 @@
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/decision.ts
4
- var n = "LiquidAI/d1-omni-600M", r = 16e3, i = r * 30, a = 2e4, o = 8 * 1024 * 1024, s = 8 * 1024 * 1024, c = 2e4, l = null, u = null, d = () => {
5
- l = null, u = null;
4
+ var n = "LiquidAI/d1-omni-600M", r = 16e3, i = r * 30, a = 2e4, o = 8 * 1024 * 1024, s = 8 * 1024 * 1024, c = 16 * 1024 * 1024, l = 2e4, u = null, d = null, f = () => {
5
+ let e = u;
6
+ u = null, d = null, e && (async () => {
7
+ try {
8
+ e.dispose?.();
9
+ } catch {}
10
+ })();
6
11
  };
7
- function f(e) {
12
+ function p(e) {
8
13
  let t = e.decision;
9
14
  if (!t) throw Error("decision: payload.decision is required for d1-omni");
10
15
  let n = t.questions;
@@ -45,11 +50,11 @@ function f(e) {
45
50
  audioUrl: s
46
51
  };
47
52
  }
48
- async function p(e) {
49
- if (l) return l;
53
+ async function m(e) {
50
54
  if (u) return u;
55
+ if (d) return d;
51
56
  let n = performance.now();
52
- return u = (async () => {
57
+ return d = (async () => {
53
58
  let r = t(e);
54
59
  if (r.repo !== "LiquidAI/d1-omni-600M") throw Error(`decision: model ${e} is not a d1-omni decision model`);
55
60
  let { AutoModel: i, env: a } = await import("./transformers.web.js").then((e) => e.i);
@@ -59,12 +64,12 @@ async function p(e) {
59
64
  dtype: "q8",
60
65
  revision: r.revision
61
66
  });
62
- return l = o, console.log(`[flaxia-node] decision: model ready model=${r.repo} revision=${r.revision} loadMs=${Math.round(performance.now() - n)}`), o;
67
+ return u = o, console.log(`[flaxia-node] decision: model ready model=${r.repo} revision=${r.revision} loadMs=${Math.round(performance.now() - n)}`), o;
63
68
  })().catch((e) => {
64
- throw u = null, console.error(`[flaxia-node] decision: model load FAILED error=${e instanceof Error ? e.message : String(e)}`), e;
65
- }), u;
69
+ throw d = null, console.error(`[flaxia-node] decision: model load FAILED error=${e instanceof Error ? e.message : String(e)}`), e;
70
+ }), d;
66
71
  }
67
- function m(e) {
72
+ function h(e) {
68
73
  let t = {};
69
74
  if (typeof e != "object" || !e) return t;
70
75
  typeof e.noul == "number" && (t.noul = e.noul), typeof e.choice == "string" && (t.choice = e.choice), typeof e.confidence == "number" && (t.confidence = e.confidence), typeof e.score == "number" && (t.score = e.score);
@@ -84,59 +89,61 @@ function m(e) {
84
89
  }
85
90
  return t;
86
91
  }
87
- async function h(t) {
92
+ async function g(t) {
88
93
  let a = performance.now();
89
94
  if (t.model !== n) throw Error(`decision: model must be ${n} for decision payloads, got ${JSON.stringify(t.model)}`);
90
- let { state: l, questions: u, imageUrls: d, audioUrl: h } = f(t), g = await p(t.model), _;
91
- if (d.length > 0) {
95
+ let { state: u, questions: d, imageUrls: f, audioUrl: g } = p(t), _ = await m(t.model), v;
96
+ if (f.length > 0) {
92
97
  let { loadImage: t } = await import("./transformers.web.js").then((e) => e.i);
93
- _ = [];
94
- for (let n of d) {
98
+ v = [];
99
+ for (let n of f) {
95
100
  let r = await e({
96
101
  url: n,
97
102
  allowContentTypes: ["image/*", "application/octet-stream"],
98
103
  maxBytes: o,
99
- timeoutMs: c
104
+ timeoutMs: l
100
105
  });
101
106
  if (!r.ok) throw Error(`decision: image fetch failed (${r.status}): ${n}`);
102
107
  let i = new Blob([r.bytes], { type: r.contentType || "application/octet-stream" }), a = URL.createObjectURL(i);
103
108
  try {
104
- _.push(await t(a));
109
+ let e = await t(a);
110
+ if (!Number.isSafeInteger(e.width) || !Number.isSafeInteger(e.height) || e.width < 1 || e.height < 1 || e.width * e.height > 16777216) throw Error(`decision: image ${n} decodes to ${e.width}x${e.height}, above the ${c} pixel limit`);
111
+ v.push(e);
105
112
  } finally {
106
113
  URL.revokeObjectURL(a);
107
114
  }
108
115
  }
109
116
  }
110
- let v;
111
- if (h) {
117
+ let y;
118
+ if (g) {
112
119
  let t = await e({
113
- url: h,
120
+ url: g,
114
121
  allowContentTypes: ["audio/*", "application/octet-stream"],
115
122
  maxBytes: s,
116
- timeoutMs: c
123
+ timeoutMs: l
117
124
  });
118
- if (!t.ok) throw Error(`decision: audio fetch failed (${t.status}): ${h}`);
125
+ if (!t.ok) throw Error(`decision: audio fetch failed (${t.status}): ${g}`);
119
126
  let n = t.bytes.buffer.slice(t.bytes.byteOffset, t.bytes.byteOffset + t.bytes.byteLength), a = globalThis.OfflineAudioContext ?? globalThis.webkitOfflineAudioContext;
120
127
  if (a) {
121
128
  let e = (await new a(1, r * 30, r).decodeAudioData(n.slice(0))).getChannelData(0), t = Math.min(e.length, i), o = new Float32Array(t);
122
- o.set(e.subarray(0, t)), v = o;
129
+ o.set(e.subarray(0, t)), y = o;
123
130
  } else {
124
131
  let e = new Int16Array(n.slice(0));
125
- v = e.subarray(0, Math.min(e.length, i));
132
+ y = e.subarray(0, Math.min(e.length, i));
126
133
  }
127
134
  }
128
- let y = g.system_one(l, u, {
129
- images: _?.length ? _ : void 0,
130
- audio: v
131
- }), b = Math.round(performance.now() - a), x = {}, S = y?.answers ?? {};
132
- for (let [e, t] of Object.entries(S)) x[e] = m(t);
133
- let C = typeof y?.usage?.input_tokens == "number" ? y.usage.input_tokens : void 0;
134
- return console.log(`[flaxia-node] decision: done questions=${Object.keys(x).length} durationMs=${b}`), {
135
- output: y,
136
- answers: x,
137
- inputTokens: C,
138
- durationMs: b
135
+ let b = _.system_one(u, d, {
136
+ images: v?.length ? v : void 0,
137
+ audio: y
138
+ }), x = Math.round(performance.now() - a), S = {}, C = b?.answers ?? {};
139
+ for (let [e, t] of Object.entries(C)) S[e] = h(t);
140
+ let w = typeof b?.usage?.input_tokens == "number" ? b.usage.input_tokens : void 0;
141
+ return console.log(`[flaxia-node] decision: done questions=${Object.keys(S).length} durationMs=${x}`), {
142
+ output: b,
143
+ answers: S,
144
+ inputTokens: w,
145
+ durationMs: x
139
146
  };
140
147
  }
141
148
  //#endregion
142
- export { h as handleDecision, d as releaseCache };
149
+ export { g as handleDecision, f as releaseCache };
@@ -1,7 +1,15 @@
1
- import { G as e, H as t, R as n, W as r, j as i } from "./webgpuProbe.js";
1
+ import { G as e, J as t, P as n, V as r, q as i } from "./webgpuProbe.js";
2
2
  import { o as a } from "./ai-model-registry.js";
3
3
  var o = null, s = () => {
4
- o = null;
4
+ let e = o;
5
+ o = null, e && (async () => {
6
+ try {
7
+ e.model?.dispose?.();
8
+ } catch {}
9
+ try {
10
+ e.processor?.dispose?.();
11
+ } catch {}
12
+ })();
5
13
  }, c = !1;
6
14
  function l(e, t) {
7
15
  if (c) return;
@@ -18,16 +26,16 @@ function l(e, t) {
18
26
  function u(e) {
19
27
  throw Error(`embed-gemma: ${e}`);
20
28
  }
21
- function d(n) {
22
- (!n || typeof n != "object" || Array.isArray(n)) && u("payload must be an object");
23
- let r = n.texts;
24
- (!Array.isArray(r) || r.length === 0 || r.length > 32) && u("texts must contain 1..32 strings");
29
+ function d(r) {
30
+ (!r || typeof r != "object" || Array.isArray(r)) && u("payload must be an object");
31
+ let i = r.texts;
32
+ (!Array.isArray(i) || i.length === 0 || i.length > 32) && u("texts must contain 1..32 strings");
25
33
  let a = 0;
26
- for (let e of r) (typeof e != "string" || e.length === 0) && u("each text must be a non-empty string"), e.length > 4e3 && u(`each text must be at most ${i} characters`), a += e.length;
27
- a > 1e5 && u(`texts exceed ${e} characters in total`);
28
- let o = n.dimensions ?? 768;
29
- return t(o) || u("dimensions must be one of 768, 512, 256, 128"), {
30
- texts: r,
34
+ for (let e of i) (typeof e != "string" || e.length === 0) && u("each text must be a non-empty string"), e.length > 4e3 && u(`each text must be at most ${n} characters`), a += e.length;
35
+ a > 1e5 && u(`texts exceed ${t} characters in total`);
36
+ let o = r.dimensions ?? 768;
37
+ return e(o) || u("dimensions must be one of 768, 512, 256, 128"), {
38
+ texts: i,
31
39
  dimensions: o
32
40
  };
33
41
  }
@@ -35,17 +43,17 @@ function f() {
35
43
  return typeof navigator < "u" && !!navigator.gpu;
36
44
  }
37
45
  async function p(e) {
38
- let t = performance.now(), n = e === "webgpu" ? "q8" : "fp16", i = a(r);
39
- console.log(`[flaxia-node] embed-gemma: loading model=${i.repo} revision=${i.revision} device=${e} dtype=${n}`);
46
+ let t = performance.now(), n = e === "webgpu" ? "q8" : "fp16", r = a(i);
47
+ console.log(`[flaxia-node] embed-gemma: loading model=${r.repo} revision=${r.revision} device=${e} dtype=${n}`);
40
48
  let { AutoConfig: o, AutoModel: s, AutoProcessor: c, env: u } = await import("./transformers.web.js").then((e) => e.i);
41
- u?.backends?.onnx?.wasm && (u.backends.onnx.wasm.numThreads = 1, u.backends.onnx.wasm.proxy = !1), l(u, i.maxBytes);
42
- let d = await o.from_pretrained(i.repo, { revision: i.revision });
49
+ u?.backends?.onnx?.wasm && (u.backends.onnx.wasm.numThreads = 1, u.backends.onnx.wasm.proxy = !1), l(u, r.maxBytes);
50
+ let d = await o.from_pretrained(r.repo, { revision: r.revision });
43
51
  d.vision_config = null, d.audio_config = null;
44
- let [f, p] = await Promise.all([c.from_pretrained(i.repo, { revision: i.revision }), s.from_pretrained(i.repo, {
52
+ let [f, p] = await Promise.all([c.from_pretrained(r.repo, { revision: r.revision }), s.from_pretrained(r.repo, {
45
53
  config: d,
46
54
  device: e,
47
55
  dtype: n,
48
- revision: i.revision
56
+ revision: r.revision
49
57
  })]);
50
58
  return console.log(`[flaxia-node] embed-gemma: model ready device=${e} dtype=${n} loadMs=${Math.round(performance.now() - t)}`), {
51
59
  processor: f,
@@ -81,16 +89,16 @@ async function g(e, t, n) {
81
89
  return s;
82
90
  }
83
91
  async function _(e, t) {
84
- let r = performance.now(), { texts: i, dimensions: a } = d(e), o = await m(), s = [];
92
+ let n = performance.now(), { texts: i, dimensions: a } = d(e), o = await m(), s = [];
85
93
  for (let e = 0; e < i.length; e += 4) {
86
94
  let n = i.slice(e, e + 4);
87
95
  s.push(...await g(o, n, a)), await t?.();
88
96
  }
89
97
  return {
90
98
  vectors: s,
91
- model: n,
99
+ model: r,
92
100
  dimensions: a,
93
- durationMs: Math.round(performance.now() - r)
101
+ durationMs: Math.round(performance.now() - n)
94
102
  };
95
103
  }
96
104
  //#endregion