@flaxia/node 0.6.1 → 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/README.md +2 -0
- package/dist/assets/ai-inference.js +83 -183
- package/dist/assets/ai-model-registry.js +127 -0
- package/dist/assets/cc-extract.js +5166 -0
- package/dist/assets/container.js +59 -43
- package/dist/assets/decision.js +149 -0
- package/dist/assets/embed-gemma.js +105 -0
- 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/transformers.web.js +2 -2
- package/dist/assets/vector-embed.js +148 -25
- package/dist/assets/vector-query.js +4 -4
- package/dist/assets/vector-store.js +4 -4
- package/dist/assets/webgpuProbe.js +127 -70
- package/dist/client/SignalingClient.d.ts.map +1 -1
- package/dist/client/light-preloader.d.ts +14 -1
- package/dist/client/light-preloader.d.ts.map +1 -1
- package/dist/consent/notice.d.ts +3 -1
- package/dist/consent/notice.d.ts.map +1 -1
- package/dist/executor/WorkerPool.d.ts +7 -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 +357 -326
- 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 +187 -175
- package/dist/workloads/__tests__/cc-extract.test.d.ts +2 -0
- package/dist/workloads/__tests__/cc-extract.test.d.ts.map +1 -0
- package/dist/workloads/__tests__/decision.test.d.ts +2 -0
- package/dist/workloads/__tests__/decision.test.d.ts.map +1 -0
- package/dist/workloads/__tests__/embed-gemma.test.d.ts +2 -0
- package/dist/workloads/__tests__/embed-gemma.test.d.ts.map +1 -0
- 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/__tests__/wasm-threads.test.d.ts.map +1 -1
- package/dist/workloads/ai-inference.d.ts +21 -2
- package/dist/workloads/ai-inference.d.ts.map +1 -1
- package/dist/workloads/ai-model-registry.d.ts.map +1 -1
- package/dist/workloads/cc-extract.d.ts +11 -0
- package/dist/workloads/cc-extract.d.ts.map +1 -0
- package/dist/workloads/decision.d.ts +23 -0
- package/dist/workloads/decision.d.ts.map +1 -0
- package/dist/workloads/embed-gemma.d.ts +8 -0
- package/dist/workloads/embed-gemma.d.ts.map +1 -0
- 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 +16 -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 +4 -3
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 };
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { n as e } from "./egress-guard.js";
|
|
2
|
+
import { o as t } from "./ai-model-registry.js";
|
|
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 = 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
|
+
})();
|
|
11
|
+
};
|
|
12
|
+
function p(e) {
|
|
13
|
+
let t = e.decision;
|
|
14
|
+
if (!t) throw Error("decision: payload.decision is required for d1-omni");
|
|
15
|
+
let n = t.questions;
|
|
16
|
+
if (!n || typeof n != "object") throw Error("decision: questions must be an object");
|
|
17
|
+
let r = Object.keys(n);
|
|
18
|
+
if (r.length === 0 || r.length > 8) throw Error("decision: questions must hold 1..8 entries");
|
|
19
|
+
for (let [e, t] of Object.entries(n)) {
|
|
20
|
+
if (!t || typeof t != "object") throw Error(`decision: question "${e}" must be an object`);
|
|
21
|
+
if (t.type !== "noul" && t.type !== "choice" && t.type !== "score") throw Error(`decision: question "${e}" has unknown type ${JSON.stringify(t.type)}`);
|
|
22
|
+
if (typeof t.instructions != "string" || !t.instructions) throw Error(`decision: question "${e}" needs instructions`);
|
|
23
|
+
if (t.type === "choice") {
|
|
24
|
+
let n = t.criteria;
|
|
25
|
+
if (!n || typeof n != "object" || Array.isArray(n)) throw Error(`decision: question "${e}" needs criteria object`);
|
|
26
|
+
let r = Object.keys(n);
|
|
27
|
+
if (r.length < 2 || r.length > 64) throw Error(`decision: question "${e}" needs 2..64 options`);
|
|
28
|
+
for (let t of r.map((e) => n[e])) if (typeof t != "string") throw Error(`decision: question "${e}" criteria must be strings`);
|
|
29
|
+
}
|
|
30
|
+
if (t.type === "score") {
|
|
31
|
+
let n = t.criteria;
|
|
32
|
+
if (!Array.isArray(n) || n.length < 2 || n.length > 10) throw Error(`decision: question "${e}" needs 2..10 score levels`);
|
|
33
|
+
for (let t of n) if (typeof t != "string") throw Error(`decision: question "${e}" levels must be strings`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
let i = t.state ?? null;
|
|
37
|
+
if (i !== null && typeof i != "string") throw Error("decision: state must be a string or null");
|
|
38
|
+
if (typeof i == "string" && i.length > 2e4) throw Error(`decision: state exceeds ${a} characters`);
|
|
39
|
+
let o = t.imageUrls ?? [];
|
|
40
|
+
if (!Array.isArray(o) || o.length > 8) throw Error("decision: imageUrls must hold 0..8 URLs");
|
|
41
|
+
for (let e of o) if (typeof e != "string" || !e.startsWith("https://")) throw Error("decision: imageUrls must be https:// URLs");
|
|
42
|
+
let s = t.audioUrl;
|
|
43
|
+
if (s !== void 0 && (typeof s != "string" || !s.startsWith("https://"))) throw Error("decision: audioUrl must be an https:// URL");
|
|
44
|
+
if (o.length > 0 && s) throw Error("decision: pass images or audio in one call, not both");
|
|
45
|
+
if ((i === null || i === "") && o.length === 0 && !s) throw Error("decision: null state needs at least one image or an audio clip");
|
|
46
|
+
return {
|
|
47
|
+
state: i,
|
|
48
|
+
questions: n,
|
|
49
|
+
imageUrls: [...o],
|
|
50
|
+
audioUrl: s
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
async function m(e) {
|
|
54
|
+
if (u) return u;
|
|
55
|
+
if (d) return d;
|
|
56
|
+
let n = performance.now();
|
|
57
|
+
return d = (async () => {
|
|
58
|
+
let r = t(e);
|
|
59
|
+
if (r.repo !== "LiquidAI/d1-omni-600M") throw Error(`decision: model ${e} is not a d1-omni decision model`);
|
|
60
|
+
let { AutoModel: i, env: a } = await import("./transformers.web.js").then((e) => e.i);
|
|
61
|
+
a?.backends?.onnx?.wasm && (a.backends.onnx.wasm.numThreads = 1, a.backends.onnx.wasm.proxy = !1);
|
|
62
|
+
let o = await i.from_pretrained(r.repo, {
|
|
63
|
+
trust_remote_code: !0,
|
|
64
|
+
dtype: "q8",
|
|
65
|
+
revision: r.revision
|
|
66
|
+
});
|
|
67
|
+
return u = o, console.log(`[flaxia-node] decision: model ready model=${r.repo} revision=${r.revision} loadMs=${Math.round(performance.now() - n)}`), o;
|
|
68
|
+
})().catch((e) => {
|
|
69
|
+
throw d = null, console.error(`[flaxia-node] decision: model load FAILED error=${e instanceof Error ? e.message : String(e)}`), e;
|
|
70
|
+
}), d;
|
|
71
|
+
}
|
|
72
|
+
function h(e) {
|
|
73
|
+
let t = {};
|
|
74
|
+
if (typeof e != "object" || !e) return t;
|
|
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);
|
|
76
|
+
let n = e.probabilities ?? e.probs;
|
|
77
|
+
if (n && typeof n == "object" && !Array.isArray(n)) {
|
|
78
|
+
let e = {};
|
|
79
|
+
for (let [t, r] of Object.entries(n)) typeof t == "string" && typeof r == "number" && Number.isFinite(r) && (e[t] = r);
|
|
80
|
+
Object.keys(e).length > 0 && (t.probabilities = e);
|
|
81
|
+
}
|
|
82
|
+
if (Array.isArray(e.levelProbabilities)) {
|
|
83
|
+
let n = e.levelProbabilities.filter((e) => typeof e == "number" && Number.isFinite(e));
|
|
84
|
+
n.length > 0 && (t.levelProbabilities = n);
|
|
85
|
+
}
|
|
86
|
+
if (Array.isArray(e.legend)) {
|
|
87
|
+
let n = e.legend.filter((e) => typeof e == "string");
|
|
88
|
+
n.length > 0 && (t.legend = n);
|
|
89
|
+
}
|
|
90
|
+
return t;
|
|
91
|
+
}
|
|
92
|
+
async function g(t) {
|
|
93
|
+
let a = performance.now();
|
|
94
|
+
if (t.model !== n) throw Error(`decision: model must be ${n} for decision payloads, got ${JSON.stringify(t.model)}`);
|
|
95
|
+
let { state: u, questions: d, imageUrls: f, audioUrl: g } = p(t), _ = await m(t.model), v;
|
|
96
|
+
if (f.length > 0) {
|
|
97
|
+
let { loadImage: t } = await import("./transformers.web.js").then((e) => e.i);
|
|
98
|
+
v = [];
|
|
99
|
+
for (let n of f) {
|
|
100
|
+
let r = await e({
|
|
101
|
+
url: n,
|
|
102
|
+
allowContentTypes: ["image/*", "application/octet-stream"],
|
|
103
|
+
maxBytes: o,
|
|
104
|
+
timeoutMs: l
|
|
105
|
+
});
|
|
106
|
+
if (!r.ok) throw Error(`decision: image fetch failed (${r.status}): ${n}`);
|
|
107
|
+
let i = new Blob([r.bytes], { type: r.contentType || "application/octet-stream" }), a = URL.createObjectURL(i);
|
|
108
|
+
try {
|
|
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);
|
|
112
|
+
} finally {
|
|
113
|
+
URL.revokeObjectURL(a);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
let y;
|
|
118
|
+
if (g) {
|
|
119
|
+
let t = await e({
|
|
120
|
+
url: g,
|
|
121
|
+
allowContentTypes: ["audio/*", "application/octet-stream"],
|
|
122
|
+
maxBytes: s,
|
|
123
|
+
timeoutMs: l
|
|
124
|
+
});
|
|
125
|
+
if (!t.ok) throw Error(`decision: audio fetch failed (${t.status}): ${g}`);
|
|
126
|
+
let n = t.bytes.buffer.slice(t.bytes.byteOffset, t.bytes.byteOffset + t.bytes.byteLength), a = globalThis.OfflineAudioContext ?? globalThis.webkitOfflineAudioContext;
|
|
127
|
+
if (a) {
|
|
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);
|
|
129
|
+
o.set(e.subarray(0, t)), y = o;
|
|
130
|
+
} else {
|
|
131
|
+
let e = new Int16Array(n.slice(0));
|
|
132
|
+
y = e.subarray(0, Math.min(e.length, i));
|
|
133
|
+
}
|
|
134
|
+
}
|
|
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
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
//#endregion
|
|
149
|
+
export { g as handleDecision, f as releaseCache };
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { G as e, J as t, P as n, V as r, q as i } from "./webgpuProbe.js";
|
|
2
|
+
import { o as a } from "./ai-model-registry.js";
|
|
3
|
+
var o = null, s = () => {
|
|
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
|
+
})();
|
|
13
|
+
}, c = !1;
|
|
14
|
+
function l(e, t) {
|
|
15
|
+
if (c) return;
|
|
16
|
+
c = !0;
|
|
17
|
+
let n = typeof e.fetch == "function" ? e.fetch.bind(e) : globalThis.fetch.bind(globalThis);
|
|
18
|
+
e.fetch = async (e, r) => {
|
|
19
|
+
let i = new URL(String(e));
|
|
20
|
+
if (!/(^|\.)huggingface\.co$/.test(i.hostname) && !/(^|\.)hf\.co$/.test(i.hostname)) throw Error(`embed-gemma: model download blocked from ${i.hostname}`);
|
|
21
|
+
let a = await n(i, r), o = Number(a.headers.get("content-length"));
|
|
22
|
+
if (Number.isFinite(o) && o > t) throw Error(`embed-gemma: model file too large (${o} bytes > ${t})`);
|
|
23
|
+
return a;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
function u(e) {
|
|
27
|
+
throw Error(`embed-gemma: ${e}`);
|
|
28
|
+
}
|
|
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");
|
|
33
|
+
let a = 0;
|
|
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,
|
|
39
|
+
dimensions: o
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
function f() {
|
|
43
|
+
return typeof navigator < "u" && !!navigator.gpu;
|
|
44
|
+
}
|
|
45
|
+
async function p(e) {
|
|
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}`);
|
|
48
|
+
let { AutoConfig: o, AutoModel: s, AutoProcessor: c, env: u } = await import("./transformers.web.js").then((e) => e.i);
|
|
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 });
|
|
51
|
+
d.vision_config = null, d.audio_config = null;
|
|
52
|
+
let [f, p] = await Promise.all([c.from_pretrained(r.repo, { revision: r.revision }), s.from_pretrained(r.repo, {
|
|
53
|
+
config: d,
|
|
54
|
+
device: e,
|
|
55
|
+
dtype: n,
|
|
56
|
+
revision: r.revision
|
|
57
|
+
})]);
|
|
58
|
+
return console.log(`[flaxia-node] embed-gemma: model ready device=${e} dtype=${n} loadMs=${Math.round(performance.now() - t)}`), {
|
|
59
|
+
processor: f,
|
|
60
|
+
model: p,
|
|
61
|
+
device: e,
|
|
62
|
+
dtype: n
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
async function m() {
|
|
66
|
+
if (o) return o;
|
|
67
|
+
if (f()) try {
|
|
68
|
+
return o = await p("webgpu"), o;
|
|
69
|
+
} catch (e) {
|
|
70
|
+
console.warn(`[flaxia-node] embed-gemma: WebGPU load failed, falling back to WASM fp16: ${e instanceof Error ? e.message : String(e)}`);
|
|
71
|
+
}
|
|
72
|
+
return o = await p("wasm"), o;
|
|
73
|
+
}
|
|
74
|
+
function h(e, t) {
|
|
75
|
+
let n = e.slice(0, t), r = 0;
|
|
76
|
+
for (let e of n) r += e * e;
|
|
77
|
+
return r = Math.sqrt(r), r === 0 ? n : n.map((e) => e / r);
|
|
78
|
+
}
|
|
79
|
+
async function g(e, t, n) {
|
|
80
|
+
let r = await e.processor(t), i = (await e.model(r))?.sentence_embedding;
|
|
81
|
+
if (!i || !i.data) throw Error("embed-gemma: model did not return sentence_embedding");
|
|
82
|
+
let a = Array.from(i.data), o = t.length * 768;
|
|
83
|
+
if (a.length !== o) throw Error(`embed-gemma: expected ${o} embedding values, received ${a.length}`);
|
|
84
|
+
let s = [];
|
|
85
|
+
for (let e = 0; e < t.length; e += 1) {
|
|
86
|
+
let t = e * 768, r = a.slice(t, t + 768);
|
|
87
|
+
s.push(h(r, n));
|
|
88
|
+
}
|
|
89
|
+
return s;
|
|
90
|
+
}
|
|
91
|
+
async function _(e, t) {
|
|
92
|
+
let n = performance.now(), { texts: i, dimensions: a } = d(e), o = await m(), s = [];
|
|
93
|
+
for (let e = 0; e < i.length; e += 4) {
|
|
94
|
+
let n = i.slice(e, e + 4);
|
|
95
|
+
s.push(...await g(o, n, a)), await t?.();
|
|
96
|
+
}
|
|
97
|
+
return {
|
|
98
|
+
vectors: s,
|
|
99
|
+
model: r,
|
|
100
|
+
dimensions: a,
|
|
101
|
+
durationMs: Math.round(performance.now() - n)
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
//#endregion
|
|
105
|
+
export { _ as handleEmbedGemma, s as releaseCache };
|