@flaxia/node 0.3.0 → 0.3.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.
@@ -46,24 +46,27 @@ var r = [
46
46
  let e = {
47
47
  dtype: d.dtype ?? "q4f16",
48
48
  device: d.device
49
- };
49
+ }, t = performance.now();
50
+ console.log(`[flaxia-node] ai-inference: loading pipeline task=${c} model=${l} dtype=${e.dtype} device=${e.device ?? "wasm"}`);
50
51
  try {
51
52
  p = await n(c, l, e);
52
53
  } catch (t) {
53
54
  if (d.device && d.device !== "wasm") console.warn(`[AiInference] ${d.device} failed, falling back to wasm:`, t), e.device = "wasm", p = await n(c, l, e);
54
- else throw t;
55
+ else throw console.error(`[flaxia-node] ai-inference: pipeline load FAILED task=${c} model=${l} error=${t instanceof Error ? t.message : String(t)}`), t;
55
56
  }
56
- i.set(f, p);
57
+ console.log(`[flaxia-node] ai-inference: pipeline ready task=${c} model=${l} loadMs=${Math.round(performance.now() - t)}`), i.set(f, p);
57
58
  }
58
59
  let m = {
59
60
  max_new_tokens: d.max_new_tokens ?? 128,
60
61
  do_sample: d.do_sample ?? !1
61
62
  };
62
- return d.temperature != null && (m.temperature = d.temperature), d.top_p != null && (m.top_p = d.top_p), d.top_k != null && (m.top_k = d.top_k), d.repetition_penalty != null && (m.repetition_penalty = d.repetition_penalty), d.src_lang != null && (m.src_lang = d.src_lang), d.tgt_lang != null && (m.tgt_lang = d.tgt_lang), s && p.tokenizer && (m.streamer = new e(p.tokenizer, {
63
+ d.temperature != null && (m.temperature = d.temperature), d.top_p != null && (m.top_p = d.top_p), d.top_k != null && (m.top_k = d.top_k), d.repetition_penalty != null && (m.repetition_penalty = d.repetition_penalty), d.src_lang != null && (m.src_lang = d.src_lang), d.tgt_lang != null && (m.tgt_lang = d.tgt_lang), s && p.tokenizer && (m.streamer = new e(p.tokenizer, {
63
64
  skip_prompt: !0,
64
65
  skip_special_tokens: !0,
65
66
  callback_function: a(s, d)
66
- })), { output: await p(u, m) };
67
+ }));
68
+ let h = performance.now(), g = await p(u, m);
69
+ return console.log(`[flaxia-node] ai-inference: done task=${c} model=${l} execMs=${Math.round(performance.now() - h)} inputLen=${String(u).length}`), { output: g };
67
70
  };
68
71
  //#endregion
69
72
  export { o as handleAiInference };
@@ -1118,6 +1118,14 @@ var T = async (e) => {
1118
1118
  exitCode: -1
1119
1119
  };
1120
1120
  }
1121
- }, E = async (e) => await T(e);
1121
+ }, E = async (e) => {
1122
+ let t = performance.now();
1123
+ try {
1124
+ let n = await T(e);
1125
+ return console.log(`[flaxia-node] container: done durationMs=${Math.round(performance.now() - t)} exitCode=${n.exitCode ?? "?"}`), n;
1126
+ } catch (e) {
1127
+ throw console.error(`[flaxia-node] container: failed error=${e instanceof Error ? e.message : String(e)}`), e;
1128
+ }
1129
+ };
1122
1130
  //#endregion
1123
1131
  export { E as handleContainer };
@@ -1,22 +1,22 @@
1
1
  //#region src/workloads/image-process.ts
2
2
  var e = async (e) => {
3
- let { operation: t, imageBase64: n, mimeType: r, options: i } = e, a = await (await fetch(`data:${r};base64,${n}`)).blob(), o = await createImageBitmap(a), s = new OffscreenCanvas(i.width || o.width, i.height || o.height), c = s.getContext("2d");
4
- if (!c) throw Error("Failed to get OffscreenCanvas context");
5
- t === "grayscale" && (c.filter = "grayscale(100%)"), c.drawImage(o, 0, 0, s.width, s.height);
6
- let l = `image/${i.outputFormat || r.split("/")[1]}`, u = await s.convertToBlob({
7
- type: l,
3
+ let { operation: t, imageBase64: n, mimeType: r, options: i } = e, a = performance.now(), o = Math.round(n.length * .75), s = await (await fetch(`data:${r};base64,${n}`)).blob(), c = await createImageBitmap(s), l = new OffscreenCanvas(i.width || c.width, i.height || c.height), u = l.getContext("2d");
4
+ if (!u) throw Error("Failed to get OffscreenCanvas context");
5
+ t === "grayscale" && (u.filter = "grayscale(100%)"), u.drawImage(c, 0, 0, l.width, l.height);
6
+ let d = i.outputFormat || r.split("/")[1], f = `image/${d}`, p = await l.convertToBlob({
7
+ type: f,
8
8
  quality: i.quality || .8
9
- }), d = new FileReader();
10
- return {
11
- imageBase64: await new Promise((e, t) => {
12
- d.onloadend = () => {
13
- let t = d.result.split(",")[1];
14
- e(t);
15
- }, d.onerror = t, d.readAsDataURL(u);
16
- }),
17
- mimeType: l,
18
- originalSizeBytes: a.size,
19
- resultSizeBytes: u.size
9
+ }), m = new FileReader(), h = await new Promise((e, t) => {
10
+ m.onloadend = () => {
11
+ let t = m.result.split(",")[1];
12
+ e(t);
13
+ }, m.onerror = t, m.readAsDataURL(p);
14
+ });
15
+ return console.log(`[flaxia-node] image-process: done operation=${t} output=${d} inputBytes=${o} resultBytes=${p.size} durationMs=${Math.round(performance.now() - a)}`), {
16
+ imageBase64: h,
17
+ mimeType: f,
18
+ originalSizeBytes: s.size,
19
+ resultSizeBytes: p.size
20
20
  };
21
21
  };
22
22
  //#endregion
@@ -13953,10 +13953,14 @@ var Ed = /* @__PURE__ */ new Map();
13953
13953
  function Dd(e) {
13954
13954
  let t = Ed.get(e);
13955
13955
  return t || (t = (async () => {
13956
- let t = await fetch(e);
13957
- if (!t.ok) throw Error(`Failed to download ONNX model: ${e} (HTTP ${t.status})`);
13958
- let n = await t.arrayBuffer();
13959
- return Se.create(n, { executionProviders: ["wasm"] });
13956
+ let t = e.split("/").pop() ?? e, n = performance.now();
13957
+ console.log(`[flaxia-node] nudenet: downloading model ${t}`);
13958
+ let r = await fetch(e);
13959
+ if (!r.ok) throw Error(`Failed to download ONNX model: ${e} (HTTP ${r.status})`);
13960
+ let i = await r.arrayBuffer();
13961
+ console.log(`[flaxia-node] nudenet: model loaded ${t} bytes=${i.byteLength} downloadMs=${Math.round(performance.now() - n)}`);
13962
+ let a = performance.now(), o = await Se.create(i, { executionProviders: ["wasm"] });
13963
+ return console.log(`[flaxia-node] nudenet: session ready ${t} sessionMs=${Math.round(performance.now() - a)}`), o;
13960
13964
  })(), Ed.set(e, t)), t;
13961
13965
  }
13962
13966
  async function Od(e) {
@@ -14025,16 +14029,18 @@ var jd = async (e) => {
14025
14029
  Td();
14026
14030
  let n = e.topK ?? 100, r = e.iouThreshold ?? .45, i = e.scoreThreshold ?? .25, a = await Od(e);
14027
14031
  try {
14028
- let { tensor: e, ratio: o } = kd(a), [s, c] = await Promise.all([Dd(xd), Dd(Sd)]), { output0: l } = await s.run({ images: e }), u = new pe("float32", new Float32Array([
14032
+ let { tensor: e, ratio: o } = kd(a), s = performance.now(), [c, l] = await Promise.all([Dd(xd), Dd(Sd)]);
14033
+ console.log(`[flaxia-node] nudenet: sessions ready loadMs=${Math.round(performance.now() - s)}`);
14034
+ let u = performance.now(), { output0: d } = await c.run({ images: e }), f = new pe("float32", new Float32Array([
14029
14035
  n,
14030
14036
  r,
14031
14037
  i
14032
- ]), [3]), { selected: d } = await c.run({
14033
- detection: l,
14034
- config: u
14035
- });
14036
- return {
14037
- detections: Ad(d, o),
14038
+ ]), [3]), { selected: p } = await l.run({
14039
+ detection: d,
14040
+ config: f
14041
+ }), m = Ad(p, o);
14042
+ return console.log(`[flaxia-node] nudenet: done detections=${m.length} inferMs=${Math.round(performance.now() - u)} totalMs=${Math.round(performance.now() - t)}`), {
14043
+ detections: m,
14038
14044
  durationMs: Date.now() - t
14039
14045
  };
14040
14046
  } finally {
@@ -3,18 +3,25 @@ var e = null;
3
3
  async function t(t) {
4
4
  let n = performance.now();
5
5
  if (!e) {
6
- let { pipeline: t } = await import("./transformers.web.js");
7
- e = await t("feature-extraction", "onnx-community/Qwen3-Embedding-0.6B-ONNX", { device: "wasm" });
6
+ let t = performance.now();
7
+ console.log("[flaxia-node] vector-embed: loading pipeline model=onnx-community/Qwen3-Embedding-0.6B-ONNX device=wasm");
8
+ let { pipeline: n } = await import("./transformers.web.js");
9
+ try {
10
+ e = await n("feature-extraction", "onnx-community/Qwen3-Embedding-0.6B-ONNX", { device: "wasm" });
11
+ } catch (e) {
12
+ throw console.error(`[flaxia-node] vector-embed: pipeline load FAILED error=${e instanceof Error ? e.message : String(e)}`), e;
13
+ }
14
+ console.log(`[flaxia-node] vector-embed: pipeline ready loadMs=${Math.round(performance.now() - t)}`);
8
15
  }
9
- let r = await e(t.text, {
16
+ let r = performance.now(), i = await e(t.text, {
10
17
  pooling: "mean",
11
18
  normalize: !0
12
- }), i = Array.from(r.data), a = performance.now() - n;
13
- return {
14
- vector: i,
19
+ }), a = Array.from(i.data), o = performance.now() - n;
20
+ return console.log(`[flaxia-node] vector-embed: done dims=${a.length} modelMs=${Math.round(performance.now() - r)} totalMs=${Math.round(o)} textLen=${t.text.length}`), {
21
+ vector: a,
15
22
  model: "Qwen/Qwen3-Embedding-0.6B",
16
23
  dimensions: 1024,
17
- durationMs: Math.round(a)
24
+ durationMs: Math.round(o)
18
25
  };
19
26
  }
20
27
  //#endregion
@@ -5,7 +5,13 @@ async function n() {
5
5
  return t || (t = new e(), await t.initialize()), t;
6
6
  }
7
7
  async function r(e) {
8
- return (await n()).query(e);
8
+ let t = await n(), r = performance.now();
9
+ try {
10
+ let n = await t.query(e);
11
+ return console.log(`[flaxia-node] vector-query: done durationMs=${Math.round(performance.now() - r)}`), n;
12
+ } catch (e) {
13
+ throw console.error(`[flaxia-node] vector-query: failed error=${e instanceof Error ? e.message : String(e)}`), e;
14
+ }
9
15
  }
10
16
  //#endregion
11
17
  export { r as handleVectorQuery };
@@ -5,7 +5,13 @@ async function n() {
5
5
  return t || (t = new e(), await t.initialize()), t;
6
6
  }
7
7
  async function r(e) {
8
- return (await n()).store(e);
8
+ let t = await n(), r = performance.now();
9
+ try {
10
+ let n = await t.store(e);
11
+ return console.log(`[flaxia-node] vector-store: done durationMs=${Math.round(performance.now() - r)}`), n;
12
+ } catch (e) {
13
+ throw console.error(`[flaxia-node] vector-store: failed error=${e instanceof Error ? e.message : String(e)}`), e;
14
+ }
9
15
  }
10
16
  //#endregion
11
17
  export { r as handleVectorStore };
@@ -1 +1 @@
1
- {"version":3,"file":"SignalingClient.d.ts","sourceRoot":"","sources":["../../src/client/SignalingClient.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE5D,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,YAAY,CAAC;IACvB,OAAO,EAAE,OAAO,CAAC;IACjB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AA8ND,eAAO,MAAM,cAAc,GAAI,QAAQ,UAAU,SAehD,CAAC"}
1
+ {"version":3,"file":"SignalingClient.d.ts","sourceRoot":"","sources":["../../src/client/SignalingClient.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAK5D,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,YAAY,CAAC;IACvB,OAAO,EAAE,OAAO,CAAC;IACjB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAsPD,eAAO,MAAM,cAAc,GAAI,QAAQ,UAAU,SAehD,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"WorkerPool.d.ts","sourceRoot":"","sources":["../../src/executor/WorkerPool.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAWhD;;;;;;;;;;GAUG;AACH,qBAAa,UAAU;IACrB,OAAO,CAAC,MAAM,CAAuB;IACrC,OAAO,CAAC,gBAAgB,CAAS;IACjC,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,KAAK,CAAyB;IACtC,OAAO,CAAC,MAAM,CAA2B;IACzC,OAAO,CAAC,YAAY,CAAK;gBAEb,SAAS,CAAC,EAAE,MAAM,EAAE,SAAS,SAAS;IAMlD,OAAO,CAAC,UAAU;IAYlB,GAAG,CACD,EAAE,EAAE,MAAM,EACV,QAAQ,EAAE,YAAY,EACtB,OAAO,EAAE,OAAO,EAChB,SAAS,CAAC,EAAE,MAAM,EAClB,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,EACjC,MAAM,CAAC,EAAE;QAAE,UAAU,CAAC,EAAE,MAAM,CAAA;KAAE,GAC/B,OAAO,CAAC,OAAO,CAAC;IAenB,OAAO,CAAC,OAAO;IA2Ef,OAAO,CAAC,iBAAiB;IAWzB,OAAO,CAAC,OAAO;IAKf,OAAO,CAAC,aAAa;IAQrB,SAAS;IAaT,MAAM;IAMN,IAAI,WAAW,IAAI,MAAM,CAExB;CACF"}
1
+ {"version":3,"file":"WorkerPool.d.ts","sourceRoot":"","sources":["../../src/executor/WorkerPool.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAahD;;;;;;;;;;GAUG;AACH,qBAAa,UAAU;IACrB,OAAO,CAAC,MAAM,CAAuB;IACrC,OAAO,CAAC,gBAAgB,CAAS;IACjC,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,KAAK,CAAyB;IACtC,OAAO,CAAC,MAAM,CAA2B;IACzC,OAAO,CAAC,YAAY,CAAK;gBAEb,SAAS,CAAC,EAAE,MAAM,EAAE,SAAS,SAAS;IAMlD,OAAO,CAAC,UAAU;IAYlB,GAAG,CACD,EAAE,EAAE,MAAM,EACV,QAAQ,EAAE,YAAY,EACtB,OAAO,EAAE,OAAO,EAChB,SAAS,CAAC,EAAE,MAAM,EAClB,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,EACjC,MAAM,CAAC,EAAE;QAAE,UAAU,CAAC,EAAE,MAAM,CAAA;KAAE,GAC/B,OAAO,CAAC,OAAO,CAAC;IAgBnB,OAAO,CAAC,OAAO;IAiFf,OAAO,CAAC,iBAAiB;IAYzB,OAAO,CAAC,OAAO;IAKf,OAAO,CAAC,aAAa;IAQrB,SAAS;IAaT,MAAM;IAMN,IAAI,WAAW,IAAI,MAAM,CAExB;CACF"}
package/dist/index.js CHANGED
@@ -49,7 +49,7 @@ var e = class {
49
49
  return !0;
50
50
  }, a = () => {
51
51
  localStorage.setItem(t, "true"), localStorage.setItem(n, String(Date.now() + r));
52
- }, o = class {
52
+ }, o = (...e) => console.log("[flaxia-node]", ...e), s = class {
53
53
  constructor(e, t = 3e5) {
54
54
  this.worker = null, this.queue = [], this.active = null, this._lastCpuLoad = 0, this.workerUrl = e || "/worker.js", this.defaultTimeoutMs = t, this.initWorker();
55
55
  }
@@ -63,63 +63,66 @@ var e = class {
63
63
  }, this.worker = e;
64
64
  }
65
65
  run(e, t, n, r, i, a) {
66
- return new Promise((o, s) => {
66
+ return new Promise((s, c) => {
67
67
  if (!this.worker) {
68
- s(/* @__PURE__ */ Error("Worker not available"));
68
+ c(/* @__PURE__ */ Error("Worker not available"));
69
69
  return;
70
70
  }
71
- let c = () => this.execute(e, t, n, r, i, a, o, s);
72
- this.active ? this.queue.push(c) : c();
71
+ let l = () => this.execute(e, t, n, r, i, a, s, c);
72
+ this.active ? (o(`task queued id=${e} workload=${t} (active task running)`), this.queue.push(l)) : l();
73
73
  });
74
74
  }
75
- execute(e, t, n, r, i, a, o, s) {
76
- let c = this.worker;
77
- if (!c) {
78
- s(/* @__PURE__ */ Error("Worker not available")), this.dequeue();
75
+ execute(e, t, n, r, i, a, s, c) {
76
+ let l = this.worker;
77
+ if (!l) {
78
+ c(/* @__PURE__ */ Error("Worker not available")), this.dequeue();
79
79
  return;
80
80
  }
81
- let l = r ?? this.defaultTimeoutMs, u = !1, d = (e) => {
82
- if (u) return;
83
- u = !0;
81
+ let u = r ?? this.defaultTimeoutMs, d = !1, f = performance.now();
82
+ o(`task start id=${e} workload=${t} timeoutMs=${u}`);
83
+ let p = (e) => {
84
+ if (d) return;
85
+ d = !0;
84
86
  let t = this.active;
85
87
  t && (clearTimeout(t.timer), t.removeListener()), this.active = null, e(), this.dequeue();
86
- }, f = (t) => {
87
- let { id: n, type: r, result: a, error: c, token: l, cpuLoad: u } = t.data;
88
- if (n === e) {
89
- if (r === "token") {
90
- i?.(l);
88
+ }, m = (n) => {
89
+ let { id: r, type: a, result: l, error: u, token: d, cpuLoad: m } = n.data;
90
+ if (r === e) {
91
+ if (a === "token") {
92
+ i?.(d);
91
93
  return;
92
94
  }
93
- if (r === "heartbeat") {
94
- typeof u == "number" && Number.isFinite(u) && (this._lastCpuLoad = Math.min(1, Math.max(0, u)));
95
+ if (a === "heartbeat") {
96
+ typeof m == "number" && Number.isFinite(m) && (this._lastCpuLoad = Math.min(1, Math.max(0, m)));
95
97
  return;
96
98
  }
97
- d(r === "done" ? () => o(a) : r === "error" ? () => s(Error(c)) : () => s(/* @__PURE__ */ Error(`Unknown message type: ${String(r)}`)));
99
+ a === "done" ? (o(`task done id=${e} workload=${t} durationMs=${Math.round(performance.now() - f)}`), p(() => s(l))) : a === "error" ? (o(`task failed id=${e} workload=${t} error=${u}`), p(() => c(Error(u)))) : (o(`task unknown-message id=${e} type=${String(a)}`), p(() => c(/* @__PURE__ */ Error(`Unknown message type: ${String(a)}`))));
98
100
  }
99
- }, p = () => c.removeEventListener("message", f), m = setTimeout(() => {
100
- this.cleanupWorker(), d(() => s(/* @__PURE__ */ Error("TIMEOUT")));
101
- }, l);
101
+ }, h = () => l.removeEventListener("message", m), g = setTimeout(() => {
102
+ o(`task timeout id=${e} workload=${t} (worker terminated after ${u}ms)`), this.cleanupWorker(), p(() => c(/* @__PURE__ */ Error("TIMEOUT")));
103
+ }, u);
102
104
  this.active = {
103
105
  id: e,
104
- resolve: o,
105
- reject: s,
106
- timer: m,
107
- worker: c,
108
- removeListener: p
109
- }, c.addEventListener("message", f);
106
+ resolve: s,
107
+ reject: c,
108
+ timer: g,
109
+ worker: l,
110
+ removeListener: h
111
+ }, l.addEventListener("message", m);
110
112
  try {
111
- c.postMessage({
113
+ l.postMessage({
112
114
  id: e,
113
115
  workload: t,
114
116
  payload: n,
115
- timeoutMs: l,
117
+ timeoutMs: u,
116
118
  config: a
117
119
  });
118
120
  } catch (e) {
119
- d(() => s(e instanceof Error ? e : Error(String(e))));
121
+ p(() => c(e instanceof Error ? e : Error(String(e))));
120
122
  }
121
123
  }
122
124
  handleWorkerError(e) {
125
+ o(`worker error error=${e.message}`);
123
126
  let t = this.active;
124
127
  this.cleanupWorker(), t && (clearTimeout(t.timer), this.active = null, t.reject(e), this.dequeue());
125
128
  }
@@ -139,13 +142,13 @@ var e = class {
139
142
  get lastCpuLoad() {
140
143
  return this._lastCpuLoad;
141
144
  }
142
- }, s = "flaxia_node_id", c = "flaxia_node_token", l = class {
145
+ }, c = (...e) => console.log("[flaxia-node]", ...e), l = (...e) => console.error("[flaxia-node]", ...e), u = "flaxia_node_id", d = "flaxia_node_token", f = class {
143
146
  constructor(e, t, n) {
144
147
  this.config = e, this.workerPool = t, this.nodeId = n, this.ws = null, this.reconnectAttempts = 0, this.MAX_RECONNECT_DELAY = 3e4, this.destroyed = !1, this.suspended = !1, this.visibilityHandler = null;
145
148
  }
146
149
  async connect() {
147
150
  if (this.destroyed) return;
148
- if (this.ws) {
151
+ if (c(`connect nodeId=${this.nodeId}`), this.ws) {
149
152
  let e = this.ws;
150
153
  e.onclose = null;
151
154
  try {
@@ -156,14 +159,14 @@ var e = class {
156
159
  this.setupVisibilityHandler();
157
160
  let e = await this.obtainToken();
158
161
  if (!e) {
159
- this.scheduleReconnect();
162
+ l("token acquisition failed; scheduling reconnect"), this.scheduleReconnect();
160
163
  return;
161
164
  }
162
165
  let t = this.config.orchestratorUrl.replace(/\/+$/, "").replace(/^http/, "ws"), n = new URL(`${t}/crowd/signal`);
163
166
  n.searchParams.set("token", e.token);
164
167
  let r = new WebSocket(n.toString());
165
168
  this.ws = r, r.onopen = () => {
166
- this.reconnectAttempts = 0;
169
+ this.reconnectAttempts = 0, c(`signal connected ${n.toString()}`);
167
170
  }, r.onmessage = async (e) => {
168
171
  let t;
169
172
  try {
@@ -178,17 +181,22 @@ var e = class {
178
181
  });
179
182
  return;
180
183
  }
181
- if (t.type === "task") try {
182
- await this.handleTask(t);
183
- } catch (e) {
184
- this.send({
185
- type: "error",
186
- taskId: String(t.taskId ?? ""),
187
- error: e instanceof Error ? e.message : String(e)
188
- });
184
+ if (t.type === "task") {
185
+ let e = t;
186
+ c(`task received taskId=${e.taskId} workload=${e.workload} timeoutMs=${e.timeoutMs ?? "default"}`);
187
+ try {
188
+ await this.handleTask(e);
189
+ } catch (n) {
190
+ let r = n instanceof Error ? n.message : String(n);
191
+ l(`task failed taskId=${e.taskId} workload=${e.workload} error=${r}`), this.send({
192
+ type: "error",
193
+ taskId: String(t.taskId ?? ""),
194
+ error: r
195
+ });
196
+ }
189
197
  }
190
198
  }, r.onclose = () => {
191
- r.onclose = null, this.ws === r && (this.ws = null, !this.destroyed && this.scheduleReconnect());
199
+ r.onclose = null, this.ws === r && (this.ws = null, !this.destroyed && (c(`signal disconnected; scheduling reconnect attempt=${this.reconnectAttempts + 1}`), this.scheduleReconnect()));
192
200
  };
193
201
  }
194
202
  scheduleReconnect() {
@@ -198,7 +206,7 @@ var e = class {
198
206
  }, e);
199
207
  }
200
208
  disconnect() {
201
- this.destroyed = !0, this.suspended = !1, this.removeVisibilityHandler();
209
+ c(`disconnect nodeId=${this.nodeId}`), this.destroyed = !0, this.suspended = !1, this.removeVisibilityHandler();
202
210
  try {
203
211
  this.ws?.close();
204
212
  } catch {}
@@ -206,7 +214,7 @@ var e = class {
206
214
  }
207
215
  suspend() {
208
216
  if (!(this.suspended || this.destroyed)) {
209
- if (this.suspended = !0, this.ws) {
217
+ if (c("suspended (visibility or manual)"), this.suspended = !0, this.ws) {
210
218
  let e = this.ws;
211
219
  e.onclose = null;
212
220
  try {
@@ -218,14 +226,14 @@ var e = class {
218
226
  }
219
227
  }
220
228
  resume() {
221
- !this.suspended || this.destroyed || (this.suspended = !1, this.workerPool.resume(), this.connect());
229
+ !this.suspended || this.destroyed || (c("resumed"), this.suspended = !1, this.workerPool.resume(), this.connect());
222
230
  }
223
231
  async obtainToken() {
224
232
  try {
225
- let e = localStorage.getItem(c);
233
+ let e = localStorage.getItem(d);
226
234
  if (e) try {
227
235
  let t = JSON.parse(e);
228
- if (t.token && t.nodeId && t.expiresAt > Date.now() + 6e4) return t;
236
+ if (t.token && t.nodeId && t.expiresAt > Date.now() + 6e4) return c(`token cached nodeId=${t.nodeId} expiresIn=${Math.round((t.expiresAt - Date.now()) / 1e3)}s`), t;
229
237
  } catch {}
230
238
  let t = this.config.orchestratorUrl.replace(/\/+$/, ""), n = await fetch(`${t}/crowd/nodes/register`, {
231
239
  method: "POST",
@@ -236,22 +244,23 @@ var e = class {
236
244
  capabilities: this.config.capabilities ?? ["ai-inference", "image-process"]
237
245
  })
238
246
  });
239
- if (!n.ok) return null;
247
+ if (!n.ok) return l(`node register failed HTTP ${n.status} (${t}/crowd/nodes/register)`), null;
240
248
  let r = await n.json();
241
- if (!r.token) return null;
249
+ if (!r.token) return l("node register returned no token"), null;
250
+ c(`node registered nodeId=${this.nodeId} expiresIn=${Math.round((r.expiresAt - Date.now()) / 1e3)}s`);
242
251
  try {
243
- localStorage.setItem(c, JSON.stringify(r));
252
+ localStorage.setItem(d, JSON.stringify(r));
244
253
  } catch {}
245
254
  return r;
246
- } catch {
247
- return null;
255
+ } catch (e) {
256
+ return l("node register threw", e instanceof Error ? e.message : e), null;
248
257
  }
249
258
  }
250
259
  send(e) {
251
260
  this.ws && this.ws.readyState === WebSocket.OPEN && this.ws.send(JSON.stringify(e));
252
261
  }
253
262
  async handleTask(e) {
254
- let t = e.timeoutMs ? e.timeoutMs + 3e4 : void 0, n = await this.workerPool.run(e.taskId, e.workload, e.payload, t, (t) => {
263
+ let t = e.timeoutMs ? e.timeoutMs + 3e4 : void 0, n = performance.now(), r = await this.workerPool.run(e.taskId, e.workload, e.payload, t, (t) => {
255
264
  this.send({
256
265
  type: "progress",
257
266
  taskId: e.taskId,
@@ -261,8 +270,8 @@ var e = class {
261
270
  this.send({
262
271
  type: "result",
263
272
  taskId: e.taskId,
264
- payload: n
265
- });
273
+ payload: r
274
+ }), c(`task result sent taskId=${e.taskId} workload=${e.workload} durationMs=${Math.round(performance.now() - n)}`);
266
275
  }
267
276
  setupVisibilityHandler() {
268
277
  this.removeVisibilityHandler(), this.visibilityHandler = () => {
@@ -272,22 +281,22 @@ var e = class {
272
281
  removeVisibilityHandler() {
273
282
  this.visibilityHandler &&= (document.removeEventListener("visibilitychange", this.visibilityHandler), null);
274
283
  }
275
- }, u = "__flaxia_node_signal_client", d = (e) => {
276
- let t = window[u];
284
+ }, p = "__flaxia_node_signal_client", m = (e) => {
285
+ let t = window[p];
277
286
  t && t.disconnect();
278
- let n = new URL("./worker.js", "" + import.meta.url).href, r = new o(n), i = localStorage.getItem(s);
279
- i || (i = crypto.randomUUID(), localStorage.setItem(s, i));
280
- let a = new l(e, r, i);
281
- window[u] = a, a.connect();
282
- }, f = (t) => {
287
+ let n = new URL("./worker.js", "" + import.meta.url).href, r = new s(n), i = localStorage.getItem(u);
288
+ i || (i = crypto.randomUUID(), localStorage.setItem(u, i));
289
+ let a = new f(e, r, i);
290
+ window[p] = a, a.connect();
291
+ }, h = (t) => {
283
292
  if (i()) {
284
- d(t);
293
+ m(t);
285
294
  return;
286
295
  }
287
296
  let n = document.createElement("div");
288
297
  n.id = "flaxia-consent-container", document.body.appendChild(n), new e(n, t.consent, () => {
289
- a(), n.remove(), d(t);
298
+ a(), n.remove(), m(t);
290
299
  });
291
300
  };
292
301
  //#endregion
293
- export { f as initFlaxiaNode };
302
+ export { h as initFlaxiaNode };
package/dist/worker.js CHANGED
@@ -41,9 +41,9 @@ var t = class {
41
41
  }
42
42
  }, n = null;
43
43
  self.onmessage = async (e) => {
44
- let { id: r, workload: i, payload: a, config: o } = e.data;
44
+ let { id: r, workload: i, payload: a, config: o } = e.data, s = performance.now();
45
45
  try {
46
- n ||= new t(o?.maxCpuLoad), await n.waitForSlot();
46
+ n ||= new t(o?.maxCpuLoad), console.log(`[flaxia-node:worker] task start id=${r} workload=${i}`), await n.waitForSlot();
47
47
  let e = setInterval(() => {
48
48
  self.postMessage({
49
49
  id: r,
@@ -94,15 +94,16 @@ self.onmessage = async (e) => {
94
94
  id: r,
95
95
  type: "done",
96
96
  result: e
97
- });
97
+ }), console.log(`[flaxia-node:worker] task done id=${r} workload=${i} durationMs=${Math.round(performance.now() - s)}`);
98
98
  } finally {
99
99
  clearInterval(e);
100
100
  }
101
101
  } catch (e) {
102
- self.postMessage({
102
+ let t = e instanceof Error ? e.message : String(e);
103
+ console.error(`[flaxia-node:worker] task error id=${r} workload=${i} error=${t}`), self.postMessage({
103
104
  id: r,
104
105
  type: "error",
105
- error: e instanceof Error ? e.message : String(e)
106
+ error: t
106
107
  });
107
108
  }
108
109
  };
@@ -1 +1 @@
1
- {"version":3,"file":"ai-inference.d.ts","sourceRoot":"","sources":["../../src/workloads/ai-inference.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,kBAAkB,EAAE,iBAAiB,EAAsB,MAAM,aAAa,CAAC;AAwC7F,eAAO,MAAM,iBAAiB,GAC5B,SAAS,kBAAkB,EAC3B,UAAU,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,KAChC,OAAO,CAAC,iBAAiB,CA0D3B,CAAC"}
1
+ {"version":3,"file":"ai-inference.d.ts","sourceRoot":"","sources":["../../src/workloads/ai-inference.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,kBAAkB,EAAE,iBAAiB,EAAsB,MAAM,aAAa,CAAC;AAwC7F,eAAO,MAAM,iBAAiB,GAC5B,SAAS,kBAAkB,EAC3B,UAAU,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,KAChC,OAAO,CAAC,iBAAiB,CAsE3B,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"container.d.ts","sourceRoot":"","sources":["../../src/workloads/container.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAErE,eAAO,MAAM,eAAe,GAAU,SAAS,gBAAgB,KAAG,OAAO,CAAC,eAAe,CAExF,CAAC"}
1
+ {"version":3,"file":"container.d.ts","sourceRoot":"","sources":["../../src/workloads/container.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAErE,eAAO,MAAM,eAAe,GAAU,SAAS,gBAAgB,KAAG,OAAO,CAAC,eAAe,CAYxF,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"image-process.d.ts","sourceRoot":"","sources":["../../src/workloads/image-process.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAE3E,eAAO,MAAM,kBAAkB,GAAU,SAAS,mBAAmB,KAAG,OAAO,CAAC,kBAAkB,CAmDjG,CAAC"}
1
+ {"version":3,"file":"image-process.d.ts","sourceRoot":"","sources":["../../src/workloads/image-process.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAE3E,eAAO,MAAM,kBAAkB,GAAU,SAAS,mBAAmB,KAAG,OAAO,CAAC,kBAAkB,CAwDjG,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"nudenet.d.ts","sourceRoot":"","sources":["../../src/workloads/nudenet.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAoB,cAAc,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAoKnF,eAAO,MAAM,aAAa,GAAU,SAAS,cAAc,KAAG,OAAO,CAAC,aAAa,CAuBlF,CAAC"}
1
+ {"version":3,"file":"nudenet.d.ts","sourceRoot":"","sources":["../../src/workloads/nudenet.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAoB,cAAc,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AA+KnF,eAAO,MAAM,aAAa,GAAU,SAAS,cAAc,KAAG,OAAO,CAAC,aAAa,CA6BlF,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"vector-embed.d.ts","sourceRoot":"","sources":["../../src/workloads/vector-embed.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAIzE,wBAAsB,iBAAiB,CAAC,OAAO,EAAE,kBAAkB,GAAG,OAAO,CAAC,iBAAiB,CAAC,CA0B/F"}
1
+ {"version":3,"file":"vector-embed.d.ts","sourceRoot":"","sources":["../../src/workloads/vector-embed.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAIzE,wBAAsB,iBAAiB,CAAC,OAAO,EAAE,kBAAkB,GAAG,OAAO,CAAC,iBAAiB,CAAC,CA2C/F"}
@@ -1 +1 @@
1
- {"version":3,"file":"vector-query.d.ts","sourceRoot":"","sources":["../../src/workloads/vector-query.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAazE,wBAAsB,iBAAiB,CAAC,OAAO,EAAE,kBAAkB,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAG/F"}
1
+ {"version":3,"file":"vector-query.d.ts","sourceRoot":"","sources":["../../src/workloads/vector-query.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAazE,wBAAsB,iBAAiB,CAAC,OAAO,EAAE,kBAAkB,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAW/F"}
@@ -1 +1 @@
1
- {"version":3,"file":"vector-store.d.ts","sourceRoot":"","sources":["../../src/workloads/vector-store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAazE,wBAAsB,iBAAiB,CAAC,OAAO,EAAE,kBAAkB,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAG/F;AAED,wBAAsB,4BAA4B,CAAC,UAAU,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAGtG"}
1
+ {"version":3,"file":"vector-store.d.ts","sourceRoot":"","sources":["../../src/workloads/vector-store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAazE,wBAAsB,iBAAiB,CAAC,OAAO,EAAE,kBAAkB,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAW/F;AAED,wBAAsB,4BAA4B,CAAC,UAAU,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAGtG"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flaxia/node",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "./dist/index.d.ts",