@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.
- package/dist/assets/ai-inference.js +1 -1
- package/dist/assets/cc-extract.js +20 -20
- package/dist/assets/container.js +59 -43
- package/dist/assets/decision.js +43 -36
- package/dist/assets/embed-gemma.js +28 -20
- package/dist/assets/image-process.js +78 -70
- package/dist/assets/nudenet.js +2 -2
- package/dist/assets/{VectorStoreEngine.js → sharedEngine.js} +67 -41
- package/dist/assets/vector-embed.js +49 -36
- package/dist/assets/vector-query.js +4 -4
- package/dist/assets/vector-store.js +4 -4
- package/dist/assets/webgpuProbe.js +87 -47
- package/dist/client/SignalingClient.d.ts.map +1 -1
- package/dist/client/light-preloader.d.ts +8 -0
- package/dist/client/light-preloader.d.ts.map +1 -1
- package/dist/executor/WorkerPool.d.ts +6 -0
- package/dist/executor/WorkerPool.d.ts.map +1 -1
- package/dist/executor/__tests__/resultLimits.test.d.ts +2 -0
- package/dist/executor/__tests__/resultLimits.test.d.ts.map +1 -0
- package/dist/executor/container-executor.d.ts +15 -0
- package/dist/executor/container-executor.d.ts.map +1 -1
- package/dist/executor/memoryProbe.d.ts +25 -1
- package/dist/executor/memoryProbe.d.ts.map +1 -1
- package/dist/executor/resultLimits.d.ts +19 -0
- package/dist/executor/resultLimits.d.ts.map +1 -0
- package/dist/index.js +420 -396
- package/dist/vector-store/HNSWIndex.d.ts +4 -1
- package/dist/vector-store/HNSWIndex.d.ts.map +1 -1
- package/dist/vector-store/VectorStoreEngine.d.ts +7 -1
- package/dist/vector-store/VectorStoreEngine.d.ts.map +1 -1
- package/dist/vector-store/__tests__/sharedEngine.test.d.ts +2 -0
- package/dist/vector-store/__tests__/sharedEngine.test.d.ts.map +1 -0
- package/dist/vector-store/sharedEngine.d.ts +13 -0
- package/dist/vector-store/sharedEngine.d.ts.map +1 -0
- package/dist/worker.js +163 -160
- package/dist/workloads/__tests__/nudenet-session-cache.test.d.ts +2 -0
- package/dist/workloads/__tests__/nudenet-session-cache.test.d.ts.map +1 -0
- package/dist/workloads/decision.d.ts +2 -0
- package/dist/workloads/decision.d.ts.map +1 -1
- package/dist/workloads/embed-gemma.d.ts.map +1 -1
- package/dist/workloads/image-process.d.ts +8 -0
- package/dist/workloads/image-process.d.ts.map +1 -1
- package/dist/workloads/nudenet.d.ts.map +1 -1
- package/dist/workloads/vector-embed.d.ts +6 -0
- package/dist/workloads/vector-embed.d.ts.map +1 -1
- package/dist/workloads/vector-query.d.ts.map +1 -1
- package/dist/workloads/vector-store.d.ts.map +1 -1
- package/package.json +2 -2
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { t as e } from "./chunk.js";
|
|
2
|
-
import {
|
|
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 {
|
|
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
|
|
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(
|
|
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(
|
|
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,
|
|
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(
|
|
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(
|
|
5073
|
-
(!
|
|
5074
|
-
let
|
|
5075
|
-
(!Array.isArray(
|
|
5076
|
-
let
|
|
5077
|
-
for (let e of
|
|
5078
|
-
if (
|
|
5079
|
-
let t =
|
|
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 (
|
|
5083
|
-
let e =
|
|
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 =
|
|
5087
|
-
return
|
|
5088
|
-
records:
|
|
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:
|
|
5090
|
+
timeoutMs: i.timeoutMs ?? 3e4
|
|
5091
5091
|
};
|
|
5092
5092
|
}
|
|
5093
5093
|
async function Ai(e, t = []) {
|
package/dist/assets/container.js
CHANGED
|
@@ -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 =
|
|
1056
|
-
function
|
|
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
|
|
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 =
|
|
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 =
|
|
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 =
|
|
1094
|
+
let t = F(e, n);
|
|
1084
1095
|
n = t.next;
|
|
1085
|
-
let r =
|
|
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 =
|
|
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 *
|
|
1094
|
-
let e = l *
|
|
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
|
|
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
|
|
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(
|
|
1136
|
+
return Math.min(j, Math.max(32, Math.floor(t)));
|
|
1126
1137
|
}
|
|
1127
|
-
function
|
|
1128
|
-
let n = t * 1024 * 1024, r =
|
|
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
|
|
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
|
|
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
|
|
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
|
-
|
|
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 (
|
|
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
|
-
|
|
1166
|
-
|
|
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 (
|
|
1176
|
-
if (!
|
|
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:
|
|
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
|
|
1195
|
-
let { image: i, command: a, memoryLimitMb: o } = t, s =
|
|
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 =
|
|
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 =
|
|
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.
|
|
1218
|
-
stderr: d.
|
|
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.
|
|
1225
|
-
stderr: d.
|
|
1240
|
+
stdout: u.text(),
|
|
1241
|
+
stderr: d.text(),
|
|
1226
1242
|
exitCode: -1
|
|
1227
1243
|
};
|
|
1228
1244
|
}
|
|
1229
|
-
},
|
|
1245
|
+
}, W = async (e, t = [], n) => {
|
|
1230
1246
|
let r = performance.now();
|
|
1231
1247
|
try {
|
|
1232
|
-
let i = await
|
|
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 {
|
|
1255
|
+
export { W as handleContainer };
|
package/dist/assets/decision.js
CHANGED
|
@@ -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 =
|
|
5
|
-
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
65
|
-
}),
|
|
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
|
|
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
|
|
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:
|
|
91
|
-
if (
|
|
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
|
|
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:
|
|
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
|
-
|
|
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
|
|
111
|
-
if (
|
|
117
|
+
let y;
|
|
118
|
+
if (g) {
|
|
112
119
|
let t = await e({
|
|
113
|
-
url:
|
|
120
|
+
url: g,
|
|
114
121
|
allowContentTypes: ["audio/*", "application/octet-stream"],
|
|
115
122
|
maxBytes: s,
|
|
116
|
-
timeoutMs:
|
|
123
|
+
timeoutMs: l
|
|
117
124
|
});
|
|
118
|
-
if (!t.ok) throw Error(`decision: audio fetch failed (${t.status}): ${
|
|
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)),
|
|
129
|
+
o.set(e.subarray(0, t)), y = o;
|
|
123
130
|
} else {
|
|
124
131
|
let e = new Int16Array(n.slice(0));
|
|
125
|
-
|
|
132
|
+
y = e.subarray(0, Math.min(e.length, i));
|
|
126
133
|
}
|
|
127
134
|
}
|
|
128
|
-
let
|
|
129
|
-
images:
|
|
130
|
-
audio:
|
|
131
|
-
}),
|
|
132
|
-
for (let [e, t] of Object.entries(
|
|
133
|
-
let
|
|
134
|
-
return console.log(`[flaxia-node] decision: done questions=${Object.keys(
|
|
135
|
-
output:
|
|
136
|
-
answers:
|
|
137
|
-
inputTokens:
|
|
138
|
-
durationMs:
|
|
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 {
|
|
149
|
+
export { g as handleDecision, f as releaseCache };
|
|
@@ -1,7 +1,15 @@
|
|
|
1
|
-
import { G as e,
|
|
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
|
-
|
|
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(
|
|
22
|
-
(!
|
|
23
|
-
let
|
|
24
|
-
(!Array.isArray(
|
|
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
|
|
27
|
-
a > 1e5 && u(`texts exceed ${
|
|
28
|
-
let o =
|
|
29
|
-
return
|
|
30
|
-
texts:
|
|
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",
|
|
39
|
-
console.log(`[flaxia-node] embed-gemma: loading model=${
|
|
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,
|
|
42
|
-
let d = await o.from_pretrained(
|
|
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(
|
|
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:
|
|
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
|
|
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:
|
|
99
|
+
model: r,
|
|
92
100
|
dimensions: a,
|
|
93
|
-
durationMs: Math.round(performance.now() -
|
|
101
|
+
durationMs: Math.round(performance.now() - n)
|
|
94
102
|
};
|
|
95
103
|
}
|
|
96
104
|
//#endregion
|