brsh 3.4.0 → 3.5.0

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/wbt.es.js CHANGED
@@ -620,24 +620,24 @@ var u, d, f, p, m, h, g, _, v, y, b = e((() => {
620
620
  }), u.prototype.writeBigInt64BE = $(function(e, t = 0) {
621
621
  return G(this, e, t, -BigInt("0x8000000000000000"), BigInt("0x7fffffffffffffff"));
622
622
  });
623
- function K(e, t, n, r, i, a) {
623
+ function ee(e, t, n, r, i, a) {
624
624
  if (n + r > e.length || n < 0) throw RangeError("Index out of range");
625
625
  }
626
- function q(e, t, r, i, a) {
627
- return t = +t, r >>>= 0, a || K(e, t, r, 4), n.write(e, t, r, i, 23, 4), r + 4;
626
+ function K(e, t, r, i, a) {
627
+ return t = +t, r >>>= 0, a || ee(e, t, r, 4), n.write(e, t, r, i, 23, 4), r + 4;
628
628
  }
629
629
  u.prototype.writeFloatLE = function(e, t, n) {
630
- return q(this, e, t, !0, n);
630
+ return K(this, e, t, !0, n);
631
631
  }, u.prototype.writeFloatBE = function(e, t, n) {
632
- return q(this, e, t, !1, n);
632
+ return K(this, e, t, !1, n);
633
633
  };
634
- function ee(e, t, r, i, a) {
635
- return t = +t, r >>>= 0, a || K(e, t, r, 8), n.write(e, t, r, i, 52, 8), r + 8;
634
+ function q(e, t, r, i, a) {
635
+ return t = +t, r >>>= 0, a || ee(e, t, r, 8), n.write(e, t, r, i, 52, 8), r + 8;
636
636
  }
637
637
  u.prototype.writeDoubleLE = function(e, t, n) {
638
- return ee(this, e, t, !0, n);
638
+ return q(this, e, t, !0, n);
639
639
  }, u.prototype.writeDoubleBE = function(e, t, n) {
640
- return ee(this, e, t, !1, n);
640
+ return q(this, e, t, !1, n);
641
641
  }, u.prototype.copy = function(e, t, n, r) {
642
642
  if (!u.isBuffer(e)) throw TypeError("argument should be a Buffer");
643
643
  if (n ||= 0, !r && r !== 0 && (r = this.length), t >= e.length && (t = e.length), t ||= 0, r > 0 && r < n && (r = n), r === n || e.length === 0 || this.length === 0) return 0;
@@ -1706,7 +1706,7 @@ function I(e, t) {
1706
1706
  this.fun = e, this.array = t;
1707
1707
  }
1708
1708
  function L() {}
1709
- var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
1709
+ var R, z, B, V, H, U, W, G, ee, K, q = e((() => {
1710
1710
  R = { exports: {} }, z = R.exports = {}, (function() {
1711
1711
  try {
1712
1712
  B = typeof setTimeout == "function" ? setTimeout : A;
@@ -1734,7 +1734,7 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
1734
1734
  throw Error("process.chdir is not supported");
1735
1735
  }, z.umask = function() {
1736
1736
  return 0;
1737
- }, K = R.exports, q = /* @__PURE__ */ k(K);
1737
+ }, ee = R.exports, K = /* @__PURE__ */ k(ee);
1738
1738
  })), J = /* @__PURE__ */ t(((e, t) => {
1739
1739
  var n = T();
1740
1740
  t.exports = class extends n {
@@ -1766,7 +1766,7 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
1766
1766
  }
1767
1767
  };
1768
1768
  })), Y = /* @__PURE__ */ t(((e, t) => {
1769
- b(), ee();
1769
+ b(), q();
1770
1770
  var { BinaryFile: n, SymlinkRef: r } = O(), i = new Uint8Array([
1771
1771
  66,
1772
1772
  82,
@@ -1903,8 +1903,8 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
1903
1903
  let t = new DecompressionStream("gzip"), n = new Blob([e]).stream().pipeThrough(t), r = await new Response(n).arrayBuffer();
1904
1904
  return new Uint8Array(r);
1905
1905
  }
1906
- if (q !== void 0 && typeof q.getBuiltinModule == "function") {
1907
- let t = q.getBuiltinModule("zlib");
1906
+ if (K !== void 0 && typeof K.getBuiltinModule == "function") {
1907
+ let t = K.getBuiltinModule("zlib");
1908
1908
  if (t) return new Uint8Array(t.gunzipSync(y.from(e.buffer, e.byteOffset, e.byteLength)));
1909
1909
  }
1910
1910
  throw Error("Gzip decompression is not supported in this environment");
@@ -1923,8 +1923,8 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
1923
1923
  return f(new Uint8Array(n), t);
1924
1924
  }
1925
1925
  if (typeof e == "string") {
1926
- if (!/^https?:\/\//i.test(e) && q !== void 0 && typeof q.getBuiltinModule == "function" && typeof window > "u") try {
1927
- let n = q.getBuiltinModule("fs");
1926
+ if (!/^https?:\/\//i.test(e) && K !== void 0 && typeof K.getBuiltinModule == "function" && typeof window > "u") try {
1927
+ let n = K.getBuiltinModule("fs");
1928
1928
  if (n && n.existsSync(e)) {
1929
1929
  let r = await n.promises.readFile(e);
1930
1930
  return f(new Uint8Array(r.buffer, r.byteOffset, r.byteLength), t);
@@ -2255,7 +2255,7 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
2255
2255
  })), ne = /* @__PURE__ */ t(((e, t) => {
2256
2256
  t.exports = null;
2257
2257
  })), re = /* @__PURE__ */ t(((e, t) => {
2258
- ee();
2258
+ q();
2259
2259
  function n(e) {
2260
2260
  if (typeof e != "string") throw TypeError("Path must be a string. Received " + JSON.stringify(e));
2261
2261
  }
@@ -2293,7 +2293,7 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
2293
2293
  resolve: function() {
2294
2294
  for (var e = "", t = !1, i, a = arguments.length - 1; a >= -1 && !t; a--) {
2295
2295
  var o;
2296
- a >= 0 ? o = arguments[a] : (i === void 0 && (i = q.cwd()), o = i), n(o), o.length !== 0 && (e = o + "/" + e, t = o.charCodeAt(0) === 47);
2296
+ a >= 0 ? o = arguments[a] : (i === void 0 && (i = K.cwd()), o = i), n(o), o.length !== 0 && (e = o + "/" + e, t = o.charCodeAt(0) === 47);
2297
2297
  }
2298
2298
  return e = r(e, !t), t ? e.length > 0 ? "/" + e : "/" : e.length > 0 ? e : ".";
2299
2299
  },
@@ -3835,15 +3835,55 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
3835
3835
  let t = (e.file || e.url || s).split("/").pop().split("?")[0] || "model.gguf";
3836
3836
  return /\.gguf$/i.test(t) || (t += ".gguf"), `${l}/${t}`;
3837
3837
  }
3838
- function f(e, t, n, r = 20) {
3839
- let i = (e) => e >= 1024 * 1024 ? `${(e / (1024 * 1024)).toFixed(1)} MB` : e >= 1024 ? `${(e / (1024 * 1024)).toFixed(1)} KB` : `${e} B`, a = (e || "model.gguf").split("/").pop().split("?")[0] || e;
3838
+ function f(e) {
3839
+ return !e || e <= 0 ? "0 B" : e >= 1024 * 1024 * 1024 ? `${(e / (1024 * 1024 * 1024)).toFixed(1)} GB` : e >= 1024 * 1024 ? `${(e / (1024 * 1024)).toFixed(1)} MB` : e >= 1024 ? `${(e / 1024).toFixed(1)} KB` : `${e} B`;
3840
+ }
3841
+ function p(e, t, n, r = 20) {
3842
+ let i = (e || "model.gguf").split("/").pop().split("?")[0] || e;
3840
3843
  if (n > 0) {
3841
- let e = Math.max(0, Math.min(1, t / n)), o = Math.round(e * 100), s = Math.round(e * r);
3842
- return `\rPulling ${a}: [${"█".repeat(s) + "░".repeat(Math.max(0, r - s))}] ${String(o).padStart(3)}% (${i(t)} / ${i(n)})`;
3844
+ let e = Math.max(0, Math.min(1, t / n)), a = Math.round(e * 100), o = Math.round(e * r);
3845
+ return `\rPulling ${i}: [${"█".repeat(o) + "░".repeat(Math.max(0, r - o))}] ${String(a).padStart(3)}% (${f(t)} / ${f(n)})`;
3843
3846
  }
3844
- return `\rPulling ${a}: ${i(t)} downloaded...`;
3847
+ return `\rPulling ${i}: ${f(t)} downloaded...`;
3845
3848
  }
3846
- function p(e, t = null, n = null, r = null) {
3849
+ var m = class {
3850
+ constructor({ onRender: e = null, width: t = 20, intervalMs: n = 80, enabled: r = !0 } = {}) {
3851
+ this.onRender = e, this.width = t, this.intervalMs = n, this.enabled = r, this.label = "", this.total = 0, this.loaded = 0, this.hint = "", this.startTime = 0, this.step = 0, this.timer = null, this.active = !1, this.isDeterminate = !1;
3852
+ }
3853
+ start(e, { total: t = 0, loaded: n = 0, hint: r = "", determinate: i = null } = {}) {
3854
+ this.enabled && (this.stopTimer(), this.label = e || "Loading", this.total = t || 0, this.loaded = n || 0, this.hint = r || "", this.startTime = Date.now(), this.step = 0, this.active = !0, this.isDeterminate = i === null ? this.total > 0 && this.loaded > 0 : i, this.render(), this.timer = setInterval(() => {
3855
+ this.step++, this.render();
3856
+ }, this.intervalMs));
3857
+ }
3858
+ update({ loaded: e, total: t, hint: n } = {}) {
3859
+ !this.enabled || !this.active || (typeof t == "number" && t > 0 && (this.total = t, this.isDeterminate = !0), typeof e == "number" && e >= 0 && (this.loaded = e, this.total > 0 && (this.isDeterminate = !0)), typeof n == "string" && (this.hint = n), this.render());
3860
+ }
3861
+ formatLine() {
3862
+ let e = ((Date.now() - this.startTime) / 1e3).toFixed(1) + "s";
3863
+ if (this.isDeterminate && this.total > 0) {
3864
+ let t = Math.max(0, Math.min(1, this.loaded / this.total)), n = Math.round(t * 100), r = Math.round(t * this.width), i = "█".repeat(r) + "░".repeat(Math.max(0, this.width - r)), a = `${f(this.loaded)} / ${f(this.total)}`, o = this.hint ? ` (${this.hint})` : "";
3865
+ return `${this.label}: [${i}] ${String(n).padStart(3)}% (${a}, ${e})${o}`;
3866
+ }
3867
+ let t = Math.max(1, this.width - 5), n = t * 2, r = this.step % n, i = r < t ? r : n - r, a = `[${"░".repeat(i)}${"█".repeat(5)}${"░".repeat(Math.max(0, this.width - i - 5))}]`, o = this.hint ? ` (${this.hint})` : "";
3868
+ return `${this.label}: ${a} ${e}${o}`;
3869
+ }
3870
+ render() {
3871
+ !this.enabled || !this.active || typeof this.onRender != "function" || this.onRender(this.formatLine());
3872
+ }
3873
+ stopTimer() {
3874
+ this.timer !== null && (clearInterval(this.timer), this.timer = null);
3875
+ }
3876
+ finish(e = "Done") {
3877
+ if (!this.enabled || !this.active) return;
3878
+ this.stopTimer();
3879
+ let t = ((Date.now() - this.startTime) / 1e3).toFixed(1) + "s", n = "█".repeat(this.width), r = this.total > 0 ? `, ${f(this.total)}` : "", i = `${this.label}: [${n}] 100% (${t}${r}) — ${e}`;
3880
+ typeof this.onRender == "function" && this.onRender(i), this.active = !1;
3881
+ }
3882
+ clear() {
3883
+ this.stopTimer(), this.active = !1, this.enabled && typeof this.onRender == "function" && this.onRender("");
3884
+ }
3885
+ };
3886
+ function h(e, t = null, n = null, r = null) {
3847
3887
  if (t) {
3848
3888
  if (n) {
3849
3889
  let e = t.replace(/^hf:/i, "").replace(/^\/+|\/+$/g, ""), r = n.replace(/^\/+/, "");
@@ -3905,7 +3945,7 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
3905
3945
  path: i
3906
3946
  };
3907
3947
  }
3908
- async function m(e, t) {
3948
+ async function g(e, t) {
3909
3949
  let n = await fetch(e);
3910
3950
  if (!n.ok) throw Error(`Failed to fetch model from ${e}: HTTP ${n.status}`);
3911
3951
  let r = Number(n.headers.get("content-length") || 0);
@@ -3930,7 +3970,7 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
3930
3970
  let s = new Blob(a, { type: "application/octet-stream" });
3931
3971
  return s.name = e.split("/").pop().split("?")[0] || "model.gguf", s;
3932
3972
  }
3933
- async function h({ wllama: e, mod: t, modelSpec: n, downloadedBlob: r = null, targetPersistPath: a, fs: o, progressCallback: c }) {
3973
+ async function _({ wllama: e, mod: t, modelSpec: n, downloadedBlob: r = null, targetPersistPath: a, fs: o, progressCallback: c }) {
3934
3974
  if (!o || !a) return null;
3935
3975
  let l = null;
3936
3976
  if (r && typeof r.arrayBuffer == "function") l = new Uint8Array(await r.arrayBuffer());
@@ -3954,7 +3994,7 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
3954
3994
  e = r.url || r;
3955
3995
  } catch {}
3956
3996
  if (e && typeof fetch == "function") try {
3957
- let t = await m(e, c);
3997
+ let t = await g(e, c);
3958
3998
  l = new Uint8Array(await t.arrayBuffer());
3959
3999
  } catch {}
3960
4000
  }
@@ -3972,28 +4012,28 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
3972
4012
  size: l.byteLength
3973
4013
  };
3974
4014
  }
3975
- async function g(e = {}) {
3976
- let { modelSpec: t, mmprojSpec: n = null, loadParams: o = {}, fs: l, pkgMeta: f = {}, wbtConfig: p = {}, loadModel: g = !0, persist: _ = !1, persistPath: v = null, onStatus: y = null } = e;
3977
- typeof y == "function" && y("Initializing WASM runtime...");
3978
- let b = await a("@wllama/wllama", f.cdnUrl, p.moduleLoader), x = b.Wllama || b.default || b, S = b.LoggerWithoutDebug || void 0, C = f.wasmUrl || c;
4015
+ async function v(e = {}) {
4016
+ let { modelSpec: t, mmprojSpec: n = null, loadParams: o = {}, fs: l, pkgMeta: f = {}, wbtConfig: p = {}, loadModel: m = !0, persist: h = !1, persistPath: v = null, tracker: y = null, shortLabel: b = "model.gguf", isPullCommand: x = !1, onStatus: S = null } = e;
4017
+ typeof S == "function" && S("Initializing WASM runtime...");
4018
+ let C = await a("@wllama/wllama", f.cdnUrl, p.moduleLoader), w = C.Wllama || C.default || C, T = C.LoggerWithoutDebug || void 0, E = f.wasmUrl || c;
3979
4019
  if (f.wasmPath && l && typeof URL < "u" && typeof URL.createObjectURL == "function") {
3980
4020
  let e = r(l, f.wasmPath);
3981
- e && e.byteLength > 0 && (C = URL.createObjectURL(new Blob([e], { type: "application/wasm" })));
4021
+ e && e.byteLength > 0 && (E = URL.createObjectURL(new Blob([e], { type: "application/wasm" })));
3982
4022
  }
3983
- let w = {
3984
- default: C,
3985
- "single-thread/wllama.wasm": C,
3986
- "multi-thread/wllama.wasm": C
3987
- }, T = () => {
4023
+ let D = {
4024
+ default: E,
4025
+ "single-thread/wllama.wasm": E,
4026
+ "multi-thread/wllama.wasm": E
4027
+ }, O = () => {
3988
4028
  try {
3989
- return new x(w, {
4029
+ return new w(D, {
3990
4030
  suppressNativeLog: !0,
3991
- logger: S
4031
+ logger: T
3992
4032
  });
3993
4033
  } catch {
3994
- return new x(w, {
4034
+ return new w(D, {
3995
4035
  suppressNativeLog: !0,
3996
- logger: S,
4036
+ logger: T,
3997
4037
  cacheManager: {
3998
4038
  isSupported: () => !0,
3999
4039
  getNameFromURL: async (e) => e,
@@ -4004,135 +4044,181 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
4004
4044
  });
4005
4045
  }
4006
4046
  };
4007
- if (!g) return {
4008
- wllama: T(),
4009
- mod: b,
4010
- Wllama: x,
4047
+ if (!m) return {
4048
+ wllama: O(),
4049
+ mod: C,
4050
+ Wllama: w,
4011
4051
  modelKey: null,
4012
4052
  isCached: !1,
4013
4053
  persisted: null
4014
4054
  };
4015
- let E = v || (_ && t.kind !== "fs" ? d(t) : null), D = t.kind === "fs" ? null : d(t), O = t.kind === "fs" && l && typeof l.absolutePath == "function" ? `fs:${l.absolutePath(t.path)}:gpu=${o.n_gpu_layers ?? "auto"}:emb=${!!o.embeddings}` : `${t.key}:gpu=${o.n_gpu_layers ?? "auto"}:emb=${!!o.embeddings}`;
4016
- if (u.has(O)) {
4017
- let e = u.get(O);
4055
+ let k = v || (h && t.kind !== "fs" ? d(t) : null), A = t.kind === "fs" ? null : d(t), j = t.kind === "fs" && l && typeof l.absolutePath == "function" ? `fs:${l.absolutePath(t.path)}:gpu=${o.n_gpu_layers ?? "auto"}:emb=${!!o.embeddings}` : `${t.key}:gpu=${o.n_gpu_layers ?? "auto"}:emb=${!!o.embeddings}`;
4056
+ if (u.has(j)) {
4057
+ let e = u.get(j);
4018
4058
  if (typeof e.isModelLoaded != "function" || e.isModelLoaded()) {
4019
- typeof y == "function" && y("Loaded model from cache.");
4059
+ typeof S == "function" && S("Loaded model from cache."), y && y.finish("Loaded model from cache");
4020
4060
  let n = null;
4021
- if ((_ || v) && l && E) if (l.exists(E)) {
4022
- let e = r(l, E);
4061
+ if ((h || v) && l && k) if (l.exists(k)) {
4062
+ let e = r(l, k);
4023
4063
  n = {
4024
- path: E,
4064
+ path: k,
4025
4065
  size: e ? e.byteLength : 0
4026
4066
  };
4027
- } else n = await h({
4067
+ } else n = await _({
4028
4068
  wllama: e,
4029
- mod: b,
4069
+ mod: C,
4030
4070
  modelSpec: t,
4031
- targetPersistPath: E,
4071
+ targetPersistPath: k,
4032
4072
  fs: l,
4033
4073
  progressCallback: o.progressCallback
4034
4074
  });
4035
4075
  return {
4036
4076
  wllama: e,
4037
- mod: b,
4038
- Wllama: x,
4039
- modelKey: O,
4077
+ mod: C,
4078
+ Wllama: w,
4079
+ modelKey: j,
4040
4080
  isCached: !0,
4041
4081
  persisted: n
4042
4082
  };
4043
4083
  }
4044
4084
  }
4045
- let k = T(), A = null, j = null;
4085
+ let M = O(), N = null, P = null, F = (e = "loading tensors") => ({
4086
+ ...o,
4087
+ progressCallback: ({ loaded: t, total: n }) => {
4088
+ y && y.update({
4089
+ loaded: t,
4090
+ total: n,
4091
+ hint: e
4092
+ }), typeof o.progressCallback == "function" && o.progressCallback({
4093
+ loaded: t,
4094
+ total: n
4095
+ });
4096
+ }
4097
+ });
4046
4098
  if (t.kind !== "fs" && l) {
4047
- let e = E && l.exists(E) ? E : D && l.exists(D) ? D : null;
4099
+ let e = k && l.exists(k) ? k : A && l.exists(A) ? A : null;
4048
4100
  if (e) {
4049
4101
  let t = r(l, e);
4050
4102
  if (t && t.byteLength > 0) {
4051
- typeof y == "function" && y(`Loading model from ${e}...`);
4103
+ typeof S == "function" && S(`Loading model from ${e}...`), y && y.start(`Loading model (${b}) weights`, {
4104
+ total: t.byteLength,
4105
+ hint: "parsing tensors & compiling WebGPU shaders"
4106
+ });
4052
4107
  let n = new Blob([t], { type: "application/octet-stream" });
4053
- n.name = e.split("/").pop() || "model.gguf", await k.loadModel([n], o), j = {
4108
+ n.name = e.split("/").pop() || "model.gguf", await M.loadModel([n], F()), P = {
4054
4109
  path: e,
4055
4110
  bytes: t
4056
- };
4111
+ }, y && y.finish("Model weights loaded");
4057
4112
  }
4058
4113
  }
4059
4114
  }
4060
- if (j) E && E !== j.path && i(l, E, j.bytes, "application/octet-stream");
4115
+ if (P) k && k !== P.path && i(l, k, P.bytes, "application/octet-stream");
4061
4116
  else if (t.kind === "fs") {
4062
- typeof y == "function" && y(`Loading model from ${t.path}...`);
4117
+ typeof S == "function" && S(`Loading model from ${t.path}...`), y && y.start(`Loading model (${b}) weights`, { hint: "reading file" });
4063
4118
  let e = r(l, t.path);
4064
4119
  if (!e) throw Error(`${t.path}: No such file or directory`);
4065
4120
  let a = new Blob([e], { type: "application/octet-stream" });
4066
4121
  a.name = t.path.split("/").pop() || "model.gguf";
4067
- let s = [a];
4122
+ let o = [a];
4068
4123
  if (n) {
4069
4124
  let e = r(l, n);
4070
4125
  if (!e) throw Error(`${n}: No such file or directory`);
4071
4126
  let t = new Blob([e], { type: "application/octet-stream" });
4072
- t.name = n.split("/").pop() || "mmproj.gguf", s.push(t);
4073
- }
4074
- typeof y == "function" && y(`Loading model ${a.name} into memory...`), await k.loadModel(s, o), E && E !== t.path && i(l, E, e, "application/octet-stream");
4075
- } else if (t.kind === "hf-quant" && typeof k.loadModelFromHF == "function") {
4076
- typeof y == "function" && y(`Pulling model ${t.repo} (${t.quant})...`);
4127
+ t.name = n.split("/").pop() || "mmproj.gguf", o.push(t);
4128
+ }
4129
+ typeof S == "function" && S(`Loading model ${a.name} into memory...`), y && y.start(`Loading model (${b}) weights`, {
4130
+ total: e.byteLength,
4131
+ loaded: e.byteLength,
4132
+ hint: "parsing tensors & compiling WebGPU shaders"
4133
+ }), await M.loadModel(o, F()), y && y.finish("Model weights loaded"), k && k !== t.path && i(l, k, e, "application/octet-stream");
4134
+ } else if (t.kind === "hf-quant" && typeof M.loadModelFromHF == "function") {
4135
+ typeof S == "function" && S(`Pulling model ${t.repo} (${t.quant})...`), y && y.start(`Pulling ${b}`, { hint: "connecting to Hugging Face" });
4077
4136
  try {
4078
- await k.loadModelFromHF({
4137
+ await M.loadModelFromHF({
4079
4138
  repo: t.repo,
4080
4139
  quant: t.quant
4081
- }, o);
4140
+ }, F("downloading")), y && y.finish("Model weights loaded");
4082
4141
  } catch (e) {
4083
- if (typeof b.getHFModelSource == "function") {
4084
- let e = await b.getHFModelSource({
4142
+ if (typeof C.getHFModelSource == "function") {
4143
+ let e = await C.getHFModelSource({
4085
4144
  repo: t.repo,
4086
4145
  quant: t.quant
4087
4146
  });
4088
- typeof y == "function" && y(`Pulling model ${t.repo} (${t.quant})...`), A = await m(e.url || e, o.progressCallback), typeof y == "function" && y("Loading model into memory..."), await k.loadModel([A], o);
4147
+ typeof S == "function" && S(`Pulling model ${t.repo} (${t.quant})...`), y && y.start(`Pulling ${b}`, { hint: "downloading" }), N = await g(e.url || e, (e) => {
4148
+ y && y.update({
4149
+ loaded: e.loaded,
4150
+ total: e.total,
4151
+ hint: "downloading"
4152
+ }), typeof o.progressCallback == "function" && o.progressCallback(e);
4153
+ }), typeof S == "function" && S("Loading model into memory..."), y && y.start(`Loading model (${b}) weights`, {
4154
+ total: N.size,
4155
+ hint: "parsing tensors & compiling WebGPU shaders"
4156
+ }), await M.loadModel([N], F()), y && y.finish("Model weights loaded");
4089
4157
  } else throw e;
4090
4158
  }
4091
- } else if (t.kind === "hf-file" && typeof k.loadModelFromHF == "function") {
4092
- typeof y == "function" && y(`Pulling model ${t.repo}/${t.file}...`);
4159
+ } else if (t.kind === "hf-file" && typeof M.loadModelFromHF == "function") {
4160
+ typeof S == "function" && S(`Pulling model ${t.repo}/${t.file}...`), y && y.start(`Pulling ${b}`, { hint: "connecting to Hugging Face" });
4093
4161
  try {
4094
- await k.loadModelFromHF({
4162
+ await M.loadModelFromHF({
4095
4163
  repo: t.repo,
4096
4164
  file: t.file
4097
- }, o);
4165
+ }, F("downloading")), y && y.finish("Model weights loaded");
4098
4166
  } catch {
4099
- A = await m(t.url, o.progressCallback), typeof y == "function" && y("Loading model into memory..."), await k.loadModel([A], o);
4167
+ y && y.start(`Pulling ${b}`, { hint: "downloading" }), N = await g(t.url, (e) => {
4168
+ y && y.update({
4169
+ loaded: e.loaded,
4170
+ total: e.total,
4171
+ hint: "downloading"
4172
+ }), typeof o.progressCallback == "function" && o.progressCallback(e);
4173
+ }), typeof S == "function" && S("Loading model into memory..."), y && y.start(`Loading model (${b}) weights`, {
4174
+ total: N.size,
4175
+ hint: "parsing tensors & compiling WebGPU shaders"
4176
+ }), await M.loadModel([N], F()), y && y.finish("Model weights loaded");
4100
4177
  }
4101
4178
  } else {
4102
4179
  let e = t.url || s;
4103
- typeof y == "function" && y(`Pulling model from ${e.split("/").pop().split("?")[0] || e}...`);
4180
+ typeof S == "function" && S(`Pulling model from ${e.split("/").pop().split("?")[0] || e}...`), y && y.start(`Pulling ${b}`, { hint: "downloading" });
4104
4181
  try {
4105
- n && /^https?:\/\//i.test(n) ? await k.loadModelFromUrl({
4182
+ n && /^https?:\/\//i.test(n) ? await M.loadModelFromUrl({
4106
4183
  url: e,
4107
4184
  mmprojUrl: n
4108
- }, o) : await k.loadModelFromUrl(e, o);
4185
+ }, F("downloading")) : await M.loadModelFromUrl(e, F("downloading")), y && y.finish("Model weights loaded");
4109
4186
  } catch {
4110
- A = await m(e, o.progressCallback), typeof y == "function" && y("Loading model into memory..."), await k.loadModel([A], o);
4111
- }
4112
- }
4113
- typeof y == "function" && y("Model loaded.");
4114
- let M = null;
4115
- return j ? M = {
4116
- path: E || j.path,
4117
- size: j.bytes.byteLength
4118
- } : (_ || v) && l && E && t.kind !== "fs" && (M = await h({
4119
- wllama: k,
4120
- mod: b,
4187
+ N = await g(e, (e) => {
4188
+ y && y.update({
4189
+ loaded: e.loaded,
4190
+ total: e.total,
4191
+ hint: "downloading"
4192
+ }), typeof o.progressCallback == "function" && o.progressCallback(e);
4193
+ }), typeof S == "function" && S("Loading model into memory..."), y && y.start(`Loading model (${b}) weights`, {
4194
+ total: N.size,
4195
+ hint: "parsing tensors & compiling WebGPU shaders"
4196
+ }), await M.loadModel([N], F()), y && y.finish("Model weights loaded");
4197
+ }
4198
+ }
4199
+ typeof S == "function" && S("Model loaded.");
4200
+ let I = null;
4201
+ return P ? I = {
4202
+ path: k || P.path,
4203
+ size: P.bytes.byteLength
4204
+ } : (h || v) && l && k && t.kind !== "fs" && (I = await _({
4205
+ wllama: M,
4206
+ mod: C,
4121
4207
  modelSpec: t,
4122
- downloadedBlob: A,
4123
- targetPersistPath: E,
4208
+ downloadedBlob: N,
4209
+ targetPersistPath: k,
4124
4210
  fs: l,
4125
4211
  progressCallback: o.progressCallback
4126
- })), u.set(O, k), {
4127
- wllama: k,
4128
- mod: b,
4129
- Wllama: x,
4130
- modelKey: O,
4212
+ })), u.set(j, M), {
4213
+ wllama: M,
4214
+ mod: C,
4215
+ Wllama: w,
4216
+ modelKey: j,
4131
4217
  isCached: !1,
4132
- persisted: M
4218
+ persisted: I
4133
4219
  };
4134
4220
  }
4135
- function _(e) {
4221
+ function y(e) {
4136
4222
  if (typeof e == "string") return e;
4137
4223
  if (!e || typeof e != "object") return "";
4138
4224
  if (Array.isArray(e.choices) && e.choices.length > 0) {
@@ -4143,7 +4229,7 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
4143
4229
  }
4144
4230
  return typeof e.content == "string" ? e.content : typeof e.text == "string" ? e.text : "";
4145
4231
  }
4146
- function v(e = {}, t = {}, i = "wllama") {
4232
+ function b(e = {}, t = {}, i = "wllama") {
4147
4233
  class a extends n {
4148
4234
  static get name() {
4149
4235
  return i;
@@ -4158,19 +4244,19 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
4158
4244
  });
4159
4245
  }
4160
4246
  _parseArgs(e) {
4161
- let t = null, n = null, r = null, i = null, a = null, s = null, c = null, l = null, u = 256, d = .7, f, m, h = 4096, g, _, v, y = !1, b = !1, x = !1, S = !0, C = !0, w = !1, T = null, E = !0, D = !1, O = !1, k = null, A = [], j = [];
4247
+ let t = null, n = null, r = null, i = null, a = null, s = null, c = null, l = null, u = 256, d = .7, f, p, m = 4096, g, _, v, y = !1, b = !1, x = !1, S = !0, C = !0, w = !1, T = null, E = !0, D = !1, O = !1, k = null, A = [], j = [];
4162
4248
  for (let o = 0; o < e.length; o++) {
4163
- let p = e[o];
4164
- if (p === "--help" || p === "-h" && o + 1 >= e.length || p === "-?") D = !0;
4165
- else if (p === "--version" || p === "-V" || p === "-v") O = !0;
4166
- else if (p === "--serve" || p === "--daemon") k = "serve";
4167
- else if (p === "-q" || p === "--quiet" || p === "--no-progress") E = !1;
4168
- else if (p === "--progress") E = !0;
4169
- else if (p === "--persist" || p === "--save") {
4249
+ let h = e[o];
4250
+ if (h === "--help" || h === "-h" && o + 1 >= e.length || h === "-?") D = !0;
4251
+ else if (h === "--version" || h === "-V" || h === "-v") O = !0;
4252
+ else if (h === "--serve" || h === "--daemon") k = "serve";
4253
+ else if (h === "-q" || h === "--quiet" || h === "--no-progress") E = !1;
4254
+ else if (h === "--progress") E = !0;
4255
+ else if (h === "--persist" || h === "--save") {
4170
4256
  w = !0;
4171
4257
  let n = e[o + 1];
4172
4258
  n && !n.startsWith("-") && !/^https?:\/\//i.test(n) && !/^hf:/i.test(n) && (n.startsWith("/") || n.startsWith("./") || n.startsWith("../") || /\.gguf$/i.test(n)) && (t !== null || r !== null || o + 2 < e.length || k === "pull") && (t !== null || r !== null) && (T = e[++o]);
4173
- } else p.startsWith("--persist=") ? (w = !0, T = p.slice(10) || null) : p.startsWith("--save=") ? (w = !0, T = p.slice(7) || null) : (p === "-o" || p === "--output") && o + 1 < e.length ? (w = !0, T = e[++o]) : p.startsWith("--output=") ? (w = !0, T = p.slice(9) || null) : (p === "-m" || p === "--model" || p === "--url") && o + 1 < e.length ? t = e[++o] : p.startsWith("--model=") ? t = p.slice(8) : p === "--mmproj" && o + 1 < e.length ? n = e[++o] : (p === "--hf" || p === "-hf" || p === "--hf-repo") && o + 1 < e.length ? r = e[++o] : p === "--hf-file" && o + 1 < e.length ? i = e[++o] : (p === "--quant" || p === "--hf-quant") && o + 1 < e.length ? a = e[++o] : (p === "-p" || p === "--prompt") && o + 1 < e.length ? s = e[++o] : p.startsWith("--prompt=") ? s = p.slice(9) : (p === "-f" || p === "--file") && o + 1 < e.length ? c = e[++o] : (p === "-s" || p === "--system" || p === "--system-prompt") && o + 1 < e.length ? l = e[++o] : (p === "-n" || p === "--n-predict" || p === "--max-tokens") && o + 1 < e.length ? u = Number(e[++o]) || 256 : (p === "--temp" || p === "--temperature") && o + 1 < e.length ? d = Number(e[++o]) : (p === "--top-k" || p === "--top_k") && o + 1 < e.length ? f = Number(e[++o]) : (p === "--top-p" || p === "--top_p") && o + 1 < e.length ? m = Number(e[++o]) : (p === "-c" || p === "--ctx-size" || p === "--n-ctx") && o + 1 < e.length ? h = Number(e[++o]) || 4096 : (p === "-b" || p === "--batch-size") && o + 1 < e.length ? g = Number(e[++o]) : (p === "-t" || p === "--threads") && o + 1 < e.length ? _ = Number(e[++o]) : (p === "-ngl" || p === "--n-gpu-layers" || p === "--gpu-layers") && o + 1 < e.length ? v = Number(e[++o]) : p === "--no-gpu" || p === "--cpu" ? v = 0 : p === "--webgpu" || p === "--gpu" ? (v === void 0 || v === 0) && (v = 99) : p === "--embeddings" || p === "--embedding" || p === "--embed" ? (y = !0, k ||= "embed") : p === "--raw" || p === "--completion" ? b = !0 : p === "--stream" ? S = !0 : p === "--no-stream" ? S = !1 : p === "--json" ? x = !0 : p === "--no-jinja" ? C = !1 : p.startsWith("-") ? A.push(p) : j.push(p);
4259
+ } else h.startsWith("--persist=") ? (w = !0, T = h.slice(10) || null) : h.startsWith("--save=") ? (w = !0, T = h.slice(7) || null) : (h === "-o" || h === "--output") && o + 1 < e.length ? (w = !0, T = e[++o]) : h.startsWith("--output=") ? (w = !0, T = h.slice(9) || null) : (h === "-m" || h === "--model" || h === "--url") && o + 1 < e.length ? t = e[++o] : h.startsWith("--model=") ? t = h.slice(8) : h === "--mmproj" && o + 1 < e.length ? n = e[++o] : (h === "--hf" || h === "-hf" || h === "--hf-repo") && o + 1 < e.length ? r = e[++o] : h === "--hf-file" && o + 1 < e.length ? i = e[++o] : (h === "--quant" || h === "--hf-quant") && o + 1 < e.length ? a = e[++o] : (h === "-p" || h === "--prompt") && o + 1 < e.length ? s = e[++o] : h.startsWith("--prompt=") ? s = h.slice(9) : (h === "-f" || h === "--file") && o + 1 < e.length ? c = e[++o] : (h === "-s" || h === "--system" || h === "--system-prompt") && o + 1 < e.length ? l = e[++o] : (h === "-n" || h === "--n-predict" || h === "--max-tokens") && o + 1 < e.length ? u = Number(e[++o]) || 256 : (h === "--temp" || h === "--temperature") && o + 1 < e.length ? d = Number(e[++o]) : (h === "--top-k" || h === "--top_k") && o + 1 < e.length ? f = Number(e[++o]) : (h === "--top-p" || h === "--top_p") && o + 1 < e.length ? p = Number(e[++o]) : (h === "-c" || h === "--ctx-size" || h === "--n-ctx") && o + 1 < e.length ? m = Number(e[++o]) || 4096 : (h === "-b" || h === "--batch-size") && o + 1 < e.length ? g = Number(e[++o]) : (h === "-t" || h === "--threads") && o + 1 < e.length ? _ = Number(e[++o]) : (h === "-ngl" || h === "--n-gpu-layers" || h === "--gpu-layers") && o + 1 < e.length ? v = Number(e[++o]) : h === "--no-gpu" || h === "--cpu" ? v = 0 : h === "--webgpu" || h === "--gpu" ? (v === void 0 || v === 0) && (v = 99) : h === "--embeddings" || h === "--embedding" || h === "--embed" ? (y = !0, k ||= "embed") : h === "--raw" || h === "--completion" ? b = !0 : h === "--stream" ? S = !0 : h === "--no-stream" ? S = !1 : h === "--json" ? x = !0 : h === "--no-jinja" ? C = !1 : h.startsWith("-") ? A.push(h) : j.push(h);
4174
4260
  }
4175
4261
  if (j.length > 0) {
4176
4262
  let e = j[0].toLowerCase();
@@ -4196,8 +4282,8 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
4196
4282
  let e = this.context.getVar("WLLAMA_MODEL");
4197
4283
  e && (t = e);
4198
4284
  }
4199
- let M = p(t, r, i, a), N = {
4200
- n_ctx: h,
4285
+ let M = h(t, r, i, a), N = {
4286
+ n_ctx: m,
4201
4287
  jinja: C
4202
4288
  };
4203
4289
  return v !== void 0 && (N.n_gpu_layers = v), g !== void 0 && (N.n_batch = g), _ !== void 0 && (N.n_threads = _), y && (N.embeddings = !0), {
@@ -4211,8 +4297,8 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
4211
4297
  maxTokens: u,
4212
4298
  temperature: d,
4213
4299
  topK: f,
4214
- topP: m,
4215
- nCtx: h,
4300
+ topP: p,
4301
+ nCtx: m,
4216
4302
  nGpuLayers: v,
4217
4303
  embeddings: y,
4218
4304
  rawCompletion: b,
@@ -4253,6 +4339,16 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
4253
4339
  _clearStatus() {
4254
4340
  !this._parsed.showProgress || this._parsed.jsonOutput || this.stderr && typeof this.stderr.write == "function" && this.stderr.write("\r\x1B[K");
4255
4341
  }
4342
+ _createTracker(e = "Loading") {
4343
+ return new m({
4344
+ enabled: !!(this._parsed.showProgress && !this._parsed.jsonOutput),
4345
+ width: 20,
4346
+ intervalMs: 80,
4347
+ onRender: (e) => {
4348
+ this.interactive ? (this._replStatus = e, this._renderRepl()) : e ? this._renderStatus(e) : this._clearStatus();
4349
+ }
4350
+ });
4351
+ }
4256
4352
  _attachProgressCallback() {
4257
4353
  if (!this._parsed.showProgress || this._parsed.modelSpec.kind === "fs") return;
4258
4354
  let e = this._parsed.model || this._parsed.modelSpec.file || this._parsed.modelSpec.repo || this._parsed.modelSpec.url || "model.gguf", t = 0;
@@ -4260,28 +4356,36 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
4260
4356
  let i = Date.now();
4261
4357
  if (t === 0 || r > 0 && n >= r || i - t >= 40) {
4262
4358
  t = i;
4263
- let a = f(e, n, r);
4359
+ let a = p(e, n, r);
4264
4360
  this.interactive ? (this._replStatus = a.replace(/^\r/, ""), this._renderRepl()) : this._parsed.subcommand === "pull" ? (this.stdOut = a, this.flush()) : this._renderStatus(a.replace(/^\r/, ""));
4265
4361
  }
4266
4362
  };
4267
4363
  }
4268
4364
  async _initWllama(n = !0) {
4269
4365
  n && this._attachProgressCallback();
4270
- let { wllama: r, Wllama: i, persisted: a } = await g({
4271
- modelSpec: this._parsed.modelSpec,
4272
- mmprojSpec: this._parsed.mmproj,
4273
- loadParams: this._parsed.loadParams,
4274
- fs: this.fs,
4275
- pkgMeta: e,
4276
- wbtConfig: t,
4277
- loadModel: n,
4278
- persist: this._parsed.persist,
4279
- persistPath: this._parsed.persistPath,
4280
- onStatus: (e) => {
4281
- this.interactive ? (this._replStatus = e, this._renderRepl()) : this._renderStatus(e);
4282
- }
4283
- });
4284
- this._wllama = r, this._WllamaClass = i, this._persisted = a, this.interactive || this._clearStatus();
4366
+ let r = this._parsed.model || this._parsed.modelSpec.file || this._parsed.modelSpec.repo || this._parsed.modelSpec.url || "model.gguf", i = r.split("/").pop().split("?")[0] || r, a = this._createTracker(), o = (e) => {
4367
+ this.interactive ? (this._replStatus = e, this._renderRepl()) : this._renderStatus(e);
4368
+ };
4369
+ try {
4370
+ let { wllama: r, Wllama: s, persisted: c } = await v({
4371
+ modelSpec: this._parsed.modelSpec,
4372
+ mmprojSpec: this._parsed.mmproj,
4373
+ loadParams: this._parsed.loadParams,
4374
+ fs: this.fs,
4375
+ pkgMeta: e,
4376
+ wbtConfig: t,
4377
+ loadModel: n,
4378
+ persist: this._parsed.persist,
4379
+ persistPath: this._parsed.persistPath,
4380
+ tracker: a,
4381
+ shortLabel: i,
4382
+ isPullCommand: this._parsed.subcommand === "pull",
4383
+ onStatus: o
4384
+ });
4385
+ this._wllama = r, this._WllamaClass = s, this._persisted = c;
4386
+ } finally {
4387
+ a && a.clear(), this.interactive || this._clearStatus();
4388
+ }
4285
4389
  }
4286
4390
  async initRepl() {
4287
4391
  await this._initWllama(!0);
@@ -4340,39 +4444,46 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
4340
4444
  temperature: this._parsed.temperature,
4341
4445
  stream: n
4342
4446
  };
4343
- if (this._parsed.topK !== void 0 && (r.top_k = this._parsed.topK), this._parsed.topP !== void 0 && (r.top_p = this._parsed.topP), this.interactive ? (this._replStatus = "Running inference...", this._renderRepl()) : this._renderStatus("Running inference..."), typeof this._wllama.createChatCompletion == "function") {
4344
- let e = await this._wllama.createChatCompletion(r);
4345
- if (e && typeof e[Symbol.asyncIterator] == "function") {
4346
- let n = "", r = !1;
4347
- for await (let i of e) {
4348
- let e = i?.choices?.[0]?.delta?.content ?? i?.choices?.[0]?.text ?? "";
4349
- e && (r || (r = !0, this.interactive ? (this._replStatus = "Streaming tokens...", this._renderRepl()) : this._clearStatus()), n += e, typeof t == "function" && t(e));
4447
+ this._parsed.topK !== void 0 && (r.top_k = this._parsed.topK), this._parsed.topP !== void 0 && (r.top_p = this._parsed.topP);
4448
+ let i = this._createTracker();
4449
+ i.start("Running inference", { hint: "evaluating prompt & preparing KV cache" });
4450
+ try {
4451
+ if (typeof this._wllama.createChatCompletion == "function") {
4452
+ let e = await this._wllama.createChatCompletion(r);
4453
+ if (e && typeof e[Symbol.asyncIterator] == "function") {
4454
+ let n = "", r = !1;
4455
+ for await (let a of e) {
4456
+ let e = a?.choices?.[0]?.delta?.content ?? a?.choices?.[0]?.text ?? "";
4457
+ e && (r || (r = !0, i.clear(), this.interactive ? (this._replStatus = "Streaming tokens...", this._renderRepl()) : this._clearStatus()), n += e, typeof t == "function" && t(e));
4458
+ }
4459
+ return i.clear(), this.interactive || this._clearStatus(), n;
4350
4460
  }
4351
- return this.interactive || this._clearStatus(), n;
4461
+ if (i.clear(), this.interactive || this._clearStatus(), this._parsed.jsonOutput) return JSON.stringify(e);
4462
+ let n = y(e);
4463
+ return typeof t == "function" && n && t(n), n;
4352
4464
  }
4353
- if (this._parsed.jsonOutput) return this.interactive || this._clearStatus(), JSON.stringify(e);
4354
- let n = _(e);
4355
- return this.interactive || this._clearStatus(), typeof t == "function" && n && t(n), n;
4356
- }
4357
- let i = {
4358
- prompt: e[e.length - 1]?.content || "",
4359
- max_tokens: this._parsed.maxTokens,
4360
- temperature: this._parsed.temperature,
4361
- stream: n
4362
- };
4363
- this._parsed.topK !== void 0 && (i.top_k = this._parsed.topK), this._parsed.topP !== void 0 && (i.top_p = this._parsed.topP);
4364
- let a = await this._wllama.createCompletion(i);
4365
- if (a && typeof a[Symbol.asyncIterator] == "function") {
4366
- let e = "", n = !1;
4367
- for await (let r of a) {
4368
- let i = r?.choices?.[0]?.text ?? r?.choices?.[0]?.delta?.content ?? "";
4369
- i && (n || (n = !0, this.interactive ? (this._replStatus = "Streaming tokens...", this._renderRepl()) : this._clearStatus()), e += i, typeof t == "function" && t(i));
4465
+ let a = {
4466
+ prompt: e[e.length - 1]?.content || "",
4467
+ max_tokens: this._parsed.maxTokens,
4468
+ temperature: this._parsed.temperature,
4469
+ stream: n
4470
+ };
4471
+ this._parsed.topK !== void 0 && (a.top_k = this._parsed.topK), this._parsed.topP !== void 0 && (a.top_p = this._parsed.topP);
4472
+ let o = await this._wllama.createCompletion(a);
4473
+ if (o && typeof o[Symbol.asyncIterator] == "function") {
4474
+ let e = "", n = !1;
4475
+ for await (let r of o) {
4476
+ let a = r?.choices?.[0]?.text ?? r?.choices?.[0]?.delta?.content ?? "";
4477
+ a && (n || (n = !0, i.clear(), this.interactive ? (this._replStatus = "Streaming tokens...", this._renderRepl()) : this._clearStatus()), e += a, typeof t == "function" && t(a));
4478
+ }
4479
+ return i.clear(), this.interactive || this._clearStatus(), e;
4370
4480
  }
4371
- return this.interactive || this._clearStatus(), e;
4481
+ if (i.clear(), this.interactive || this._clearStatus(), this._parsed.jsonOutput) return JSON.stringify(o);
4482
+ let s = y(o);
4483
+ return typeof t == "function" && s && t(s), s;
4484
+ } finally {
4485
+ i.clear(), this.interactive || this._clearStatus();
4372
4486
  }
4373
- if (this.interactive || this._clearStatus(), this._parsed.jsonOutput) return JSON.stringify(a);
4374
- let o = _(a);
4375
- return typeof t == "function" && o && t(o), o;
4376
4487
  }
4377
4488
  _formatModelInfo() {
4378
4489
  let t = this._wllama && typeof this._wllama.isSupportWebGPU == "function" ? this._wllama.isSupportWebGPU() : !!(typeof navigator < "u" && navigator.gpu), n = this._wllama && typeof this._wllama.isMultithread == "function" ? this._wllama.isMultithread() : !1, r = this._wllama && typeof this._wllama.getNumThreads == "function" ? this._wllama.getNumThreads() : 1, i = this._WllamaClass && typeof this._WllamaClass.getLibllamaVersion == "function" ? this._WllamaClass.getLibllamaVersion() : "b6327", a = this._wllama && typeof this._wllama.getModelMetadata == "function" ? this._wllama.getModelMetadata() : null, o = [
@@ -4504,10 +4615,17 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
4504
4615
  e = String(t);
4505
4616
  } else this._parsed.prompt === null ? this.stdin !== void 0 && this.stdin !== null && String(this.stdin).trim().length > 0 && (e = String(this.stdin).trim()) : e = this._parsed.prompt;
4506
4617
  if (this._parsed.embeddings || this._parsed.subcommand === "embed") {
4507
- let t = await this._wllama.createEmbedding({ input: e });
4508
- if (this._parsed.jsonOutput) this.stdOut = JSON.stringify(t);
4618
+ let t = this._createTracker();
4619
+ t.start("Computing embeddings", { hint: "evaluating prompt" });
4620
+ let n;
4621
+ try {
4622
+ n = await this._wllama.createEmbedding({ input: e });
4623
+ } finally {
4624
+ t.clear(), this._clearStatus();
4625
+ }
4626
+ if (this._parsed.jsonOutput) this.stdOut = JSON.stringify(n);
4509
4627
  else {
4510
- let e = t?.data?.[0]?.embedding || t?.embedding || [];
4628
+ let e = n?.data?.[0]?.embedding || n?.embedding || [];
4511
4629
  this.stdOut = JSON.stringify(e);
4512
4630
  }
4513
4631
  return;
@@ -4519,20 +4637,26 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
4519
4637
  temperature: this._parsed.temperature,
4520
4638
  stream: t
4521
4639
  };
4522
- this._parsed.topK !== void 0 && (n.top_k = this._parsed.topK), this._parsed.topP !== void 0 && (n.top_p = this._parsed.topP), this._renderStatus("Running inference...");
4523
- let r = await this._wllama.createCompletion(n), i = "";
4524
- if (r && typeof r[Symbol.asyncIterator] == "function") {
4525
- let e = !1;
4526
- for await (let t of r) {
4527
- let n = t?.choices?.[0]?.text ?? t?.choices?.[0]?.delta?.content ?? "";
4528
- n && (e || (e = !0, this._streamedStdout = !0, this._clearStatus()), i += n, this.stdout && typeof this.stdout.write == "function" && this.stdout.write(n), this.emit("token", n));
4529
- }
4530
- this._clearStatus(), e && i.length > 0 && !i.endsWith("\n") && this.stdout && typeof this.stdout.write == "function" && this.stdout.write("\r\n");
4531
- } else this._clearStatus(), i = this._parsed.jsonOutput ? JSON.stringify(r) : _(r);
4532
- i.split(/\r?\n/).forEach((e) => {
4533
- this.stdOut = e;
4534
- });
4535
- return;
4640
+ this._parsed.topK !== void 0 && (n.top_k = this._parsed.topK), this._parsed.topP !== void 0 && (n.top_p = this._parsed.topP);
4641
+ let r = this._createTracker();
4642
+ r.start("Running inference", { hint: "evaluating prompt & preparing KV cache" });
4643
+ try {
4644
+ let e = await this._wllama.createCompletion(n), t = "";
4645
+ if (e && typeof e[Symbol.asyncIterator] == "function") {
4646
+ let n = !1;
4647
+ for await (let i of e) {
4648
+ let e = i?.choices?.[0]?.text ?? i?.choices?.[0]?.delta?.content ?? "";
4649
+ e && (n || (n = !0, this._streamedStdout = !0, r.clear(), this._clearStatus()), t += e, this.stdout && typeof this.stdout.write == "function" && this.stdout.write(e), this.emit("token", e));
4650
+ }
4651
+ r.clear(), this._clearStatus(), n && t.length > 0 && !t.endsWith("\n") && this.stdout && typeof this.stdout.write == "function" && this.stdout.write("\r\n");
4652
+ } else r.clear(), this._clearStatus(), t = this._parsed.jsonOutput ? JSON.stringify(e) : y(e);
4653
+ t.split(/\r?\n/).forEach((e) => {
4654
+ this.stdOut = e;
4655
+ });
4656
+ return;
4657
+ } finally {
4658
+ r.clear(), this._clearStatus();
4659
+ }
4536
4660
  }
4537
4661
  let t = [];
4538
4662
  this._parsed.systemPrompt && t.push({
@@ -4559,26 +4683,26 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
4559
4683
  }
4560
4684
  return a;
4561
4685
  }
4562
- function y(e = {}, t = {}) {
4686
+ function x(e = {}, t = {}) {
4563
4687
  return [
4564
4688
  {
4565
4689
  bin: (e.bin || "wllama").toLowerCase(),
4566
- Cmd: v(e, t, "wllama")
4690
+ Cmd: b(e, t, "wllama")
4567
4691
  },
4568
4692
  {
4569
4693
  bin: "llama",
4570
- Cmd: v(e, t, "llama")
4694
+ Cmd: b(e, t, "llama")
4571
4695
  },
4572
4696
  {
4573
4697
  bin: "llama-cli",
4574
- Cmd: v(e, t, "llama-cli")
4698
+ Cmd: b(e, t, "llama-cli")
4575
4699
  }
4576
4700
  ];
4577
4701
  }
4578
- t.exports = v, t.exports.createWllamaCommand = v, t.exports.buildEntries = y, t.exports.WllamaCommand = v({
4702
+ t.exports = b, t.exports.createWllamaCommand = b, t.exports.buildEntries = x, t.exports.WllamaCommand = b({
4579
4703
  name: "wllama",
4580
4704
  version: "3.8.1"
4581
- }), t.exports.wllamaInstances = u, t.exports.getDefaultPersistPath = d, t.exports.DEFAULT_PERSIST_DIR = l, t.exports.resolveModelSpec = p, t.exports.formatDownloadProgress = f, t.exports.DEFAULT_MODEL_URL = s, t.exports.MODEL_ALIASES = o;
4705
+ }), t.exports.wllamaInstances = u, t.exports.getDefaultPersistPath = d, t.exports.DEFAULT_PERSIST_DIR = l, t.exports.resolveModelSpec = h, t.exports.formatDownloadProgress = p, t.exports.DEFAULT_MODEL_URL = s, t.exports.MODEL_ALIASES = o;
4582
4706
  })), se = /* @__PURE__ */ t(((e, t) => {
4583
4707
  b();
4584
4708
  var r = E(), i = X(), a = Z(), { DEFAULT_REGISTRY: o, resolvePackageName: s } = ie(), c = oe(), { writeBytesToFs: l, createGenericWasmCommand: u } = i, { createReplWasmCommand: d } = a, f = "/var/lib/wbt/installed.json", p = "/var/cache/wbt";