brsh 3.1.4 → 3.3.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 ee(e, t, n, r, i, a) {
623
+ function K(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 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;
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;
628
628
  }
629
629
  u.prototype.writeFloatLE = function(e, t, n) {
630
- return K(this, e, t, !0, n);
630
+ return q(this, e, t, !0, n);
631
631
  }, u.prototype.writeFloatBE = function(e, t, n) {
632
- return K(this, e, t, !1, n);
632
+ return q(this, e, t, !1, n);
633
633
  };
634
- function te(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;
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;
636
636
  }
637
637
  u.prototype.writeDoubleLE = function(e, t, n) {
638
- return te(this, e, t, !0, n);
638
+ return ee(this, e, t, !0, n);
639
639
  }, u.prototype.writeDoubleBE = function(e, t, n) {
640
- return te(this, e, t, !1, n);
640
+ return ee(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;
@@ -668,9 +668,9 @@ var u, d, f, p, m, h, g, _, v, y, b = e((() => {
668
668
  }
669
669
  return this;
670
670
  };
671
- let q = {};
672
- function J(e, t, n) {
673
- q[e] = class extends n {
671
+ let J = {};
672
+ function Y(e, t, n) {
673
+ J[e] = class extends n {
674
674
  constructor() {
675
675
  super(), Object.defineProperty(this, "message", {
676
676
  value: t.apply(this, arguments),
@@ -694,34 +694,34 @@ var u, d, f, p, m, h, g, _, v, y, b = e((() => {
694
694
  }
695
695
  };
696
696
  }
697
- J("ERR_BUFFER_OUT_OF_BOUNDS", function(e) {
697
+ Y("ERR_BUFFER_OUT_OF_BOUNDS", function(e) {
698
698
  return e ? `${e} is outside of buffer bounds` : "Attempt to access memory outside buffer bounds";
699
- }, RangeError), J("ERR_INVALID_ARG_TYPE", function(e, t) {
699
+ }, RangeError), Y("ERR_INVALID_ARG_TYPE", function(e, t) {
700
700
  return `The "${e}" argument must be of type number. Received type ${typeof t}`;
701
- }, TypeError), J("ERR_OUT_OF_RANGE", function(e, t, n) {
701
+ }, TypeError), Y("ERR_OUT_OF_RANGE", function(e, t, n) {
702
702
  let r = `The value of "${e}" is out of range.`, i = n;
703
- return Number.isInteger(n) && Math.abs(n) > 2 ** 32 ? i = ne(String(n)) : typeof n == "bigint" && (i = String(n), (n > BigInt(2) ** BigInt(32) || n < -(BigInt(2) ** BigInt(32))) && (i = ne(i)), i += "n"), r += ` It must be ${t}. Received ${i}`, r;
703
+ return Number.isInteger(n) && Math.abs(n) > 2 ** 32 ? i = te(String(n)) : typeof n == "bigint" && (i = String(n), (n > BigInt(2) ** BigInt(32) || n < -(BigInt(2) ** BigInt(32))) && (i = te(i)), i += "n"), r += ` It must be ${t}. Received ${i}`, r;
704
704
  }, RangeError);
705
- function ne(e) {
705
+ function te(e) {
706
706
  let t = "", n = e.length, r = +(e[0] === "-");
707
707
  for (; n >= r + 4; n -= 3) t = `_${e.slice(n - 3, n)}${t}`;
708
708
  return `${e.slice(0, n)}${t}`;
709
709
  }
710
- function Y(e, t, n) {
710
+ function ne(e, t, n) {
711
711
  X(t, "offset"), (e[t] === void 0 || e[t + n] === void 0) && Z(t, e.length - (n + 1));
712
712
  }
713
713
  function re(e, t, n, r, i, a) {
714
714
  if (e > n || e < t) {
715
715
  let r = typeof t == "bigint" ? "n" : "", i;
716
- throw i = a > 3 ? t === 0 || t === BigInt(0) ? `>= 0${r} and < 2${r} ** ${(a + 1) * 8}${r}` : `>= -(2${r} ** ${(a + 1) * 8 - 1}${r}) and < 2 ** ${(a + 1) * 8 - 1}${r}` : `>= ${t}${r} and <= ${n}${r}`, new q.ERR_OUT_OF_RANGE("value", i, e);
716
+ throw i = a > 3 ? t === 0 || t === BigInt(0) ? `>= 0${r} and < 2${r} ** ${(a + 1) * 8}${r}` : `>= -(2${r} ** ${(a + 1) * 8 - 1}${r}) and < 2 ** ${(a + 1) * 8 - 1}${r}` : `>= ${t}${r} and <= ${n}${r}`, new J.ERR_OUT_OF_RANGE("value", i, e);
717
717
  }
718
- Y(r, i, a);
718
+ ne(r, i, a);
719
719
  }
720
720
  function X(e, t) {
721
- if (typeof e != "number") throw new q.ERR_INVALID_ARG_TYPE(t, "number", e);
721
+ if (typeof e != "number") throw new J.ERR_INVALID_ARG_TYPE(t, "number", e);
722
722
  }
723
723
  function Z(e, t, n) {
724
- throw Math.floor(e) === e ? t < 0 ? new q.ERR_BUFFER_OUT_OF_BOUNDS() : new q.ERR_OUT_OF_RANGE(n || "offset", `>= ${+!!n} and <= ${t}`, e) : (X(e, n), new q.ERR_OUT_OF_RANGE(n || "offset", "an integer", e));
724
+ throw Math.floor(e) === e ? t < 0 ? new J.ERR_BUFFER_OUT_OF_BOUNDS() : new J.ERR_OUT_OF_RANGE(n || "offset", `>= ${+!!n} and <= ${t}`, e) : (X(e, n), new J.ERR_OUT_OF_RANGE(n || "offset", "an integer", e));
725
725
  }
726
726
  let ie = /[^+/0-9A-Za-z-_]/g;
727
727
  function ae(e) {
@@ -1226,7 +1226,34 @@ var u, d, f, p, m, h, g, _, v, y, b = e((() => {
1226
1226
  this.exitCode = this.sandbox.lastCode, this.source && this.context.source(this.sandbox.context);
1227
1227
  });
1228
1228
  }
1229
+ _accumulateLines(e) {
1230
+ let t = [], n = null, r = !1, i = !1, a = !1;
1231
+ for (let o = 0; o < e.length; o++) {
1232
+ let s = e[o];
1233
+ n === null ? n = s : a ? n += s : r || i ? n += "\n" + s : n += s;
1234
+ let c = 0, l = !1, u = !1;
1235
+ for (; c < s.length;) {
1236
+ let e = s[c];
1237
+ if (r) e === "'" && (r = !1), c++;
1238
+ else if (i) e === "\\" && c + 1 < s.length ? c += 2 : e === "\\" && c + 1 === s.length ? (l = !0, c++) : (e === "\"" && (i = !1), c++);
1239
+ else {
1240
+ if (e === "#" && (c === 0 || /\s/.test(s[c - 1]))) {
1241
+ u = !0;
1242
+ break;
1243
+ }
1244
+ e === "\\" && c + 1 < s.length ? c += 2 : e === "\\" && c + 1 === s.length ? (l = !0, c++) : e === "'" ? (r = !0, c++) : (e === "\"" && (i = !0), c++);
1245
+ }
1246
+ }
1247
+ if (!r && !l && !u) {
1248
+ let e = s.match(/(\\+)\s*$/);
1249
+ e && e[1].length % 2 == 1 && (l = !0);
1250
+ }
1251
+ a = l, l && (n = n.replace(/\\\s*$/, "")), !r && !i && !l && (t.push(n), n = null, a = !1);
1252
+ }
1253
+ return n !== null && t.push(n), t;
1254
+ }
1229
1255
  _parseLines(e) {
1256
+ e = this._accumulateLines(e);
1230
1257
  let t = [], n = 0;
1231
1258
  for (; n < e.length;) {
1232
1259
  let r = e[n].trim();
@@ -1679,7 +1706,7 @@ function I(e, t) {
1679
1706
  this.fun = e, this.array = t;
1680
1707
  }
1681
1708
  function L() {}
1682
- var R, z, B, V, H, U, W, G, ee, K, te = e((() => {
1709
+ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
1683
1710
  R = { exports: {} }, z = R.exports = {}, (function() {
1684
1711
  try {
1685
1712
  B = typeof setTimeout == "function" ? setTimeout : A;
@@ -1707,8 +1734,8 @@ var R, z, B, V, H, U, W, G, ee, K, te = e((() => {
1707
1734
  throw Error("process.chdir is not supported");
1708
1735
  }, z.umask = function() {
1709
1736
  return 0;
1710
- }, ee = R.exports, K = /* @__PURE__ */ k(ee);
1711
- })), q = /* @__PURE__ */ t(((e, t) => {
1737
+ }, K = R.exports, q = /* @__PURE__ */ k(K);
1738
+ })), J = /* @__PURE__ */ t(((e, t) => {
1712
1739
  var n = T();
1713
1740
  t.exports = class extends n {
1714
1741
  get captureInput() {
@@ -1738,8 +1765,8 @@ var R, z, B, V, H, U, W, G, ee, K, te = e((() => {
1738
1765
  });
1739
1766
  }
1740
1767
  };
1741
- })), J = /* @__PURE__ */ t(((e, t) => {
1742
- b(), te();
1768
+ })), Y = /* @__PURE__ */ t(((e, t) => {
1769
+ b(), ee();
1743
1770
  var { BinaryFile: n, SymlinkRef: r } = O(), i = new Uint8Array([
1744
1771
  66,
1745
1772
  82,
@@ -1844,7 +1871,7 @@ var R, z, B, V, H, U, W, G, ee, K, te = e((() => {
1844
1871
  let t = e.$c;
1845
1872
  if (m[t]) return m[t];
1846
1873
  if (f.inlineCommands && f.inlineCommands[t]) try {
1847
- let e = T(), n = q();
1874
+ let e = T(), n = J();
1848
1875
  return Function("Command", "InteractiveCommand", `return (${f.inlineCommands[t]});`)(e, n);
1849
1876
  } catch {
1850
1877
  return null;
@@ -1876,8 +1903,8 @@ var R, z, B, V, H, U, W, G, ee, K, te = e((() => {
1876
1903
  let t = new DecompressionStream("gzip"), n = new Blob([e]).stream().pipeThrough(t), r = await new Response(n).arrayBuffer();
1877
1904
  return new Uint8Array(r);
1878
1905
  }
1879
- if (K !== void 0 && typeof K.getBuiltinModule == "function") {
1880
- let t = K.getBuiltinModule("zlib");
1906
+ if (q !== void 0 && typeof q.getBuiltinModule == "function") {
1907
+ let t = q.getBuiltinModule("zlib");
1881
1908
  if (t) return new Uint8Array(t.gunzipSync(y.from(e.buffer, e.byteOffset, e.byteLength)));
1882
1909
  }
1883
1910
  throw Error("Gzip decompression is not supported in this environment");
@@ -1896,8 +1923,8 @@ var R, z, B, V, H, U, W, G, ee, K, te = e((() => {
1896
1923
  return f(new Uint8Array(n), t);
1897
1924
  }
1898
1925
  if (typeof e == "string") {
1899
- if (!/^https?:\/\//i.test(e) && K !== void 0 && typeof K.getBuiltinModule == "function" && typeof window > "u") try {
1900
- let n = K.getBuiltinModule("fs");
1926
+ if (!/^https?:\/\//i.test(e) && q !== void 0 && typeof q.getBuiltinModule == "function" && typeof window > "u") try {
1927
+ let n = q.getBuiltinModule("fs");
1901
1928
  if (n && n.existsSync(e)) {
1902
1929
  let r = await n.promises.readFile(e);
1903
1930
  return f(new Uint8Array(r.buffer, r.byteOffset, r.byteLength), t);
@@ -2009,7 +2036,7 @@ var R, z, B, V, H, U, W, G, ee, K, te = e((() => {
2009
2036
  }
2010
2037
  }
2011
2038
  };
2012
- })), ne = /* @__PURE__ */ t(((e, t) => {
2039
+ })), te = /* @__PURE__ */ t(((e, t) => {
2013
2040
  b();
2014
2041
  var n = O(), { BinaryFile: r, SymlinkRef: i } = n;
2015
2042
  function a(e) {
@@ -2034,20 +2061,20 @@ var R, z, B, V, H, U, W, G, ee, K, te = e((() => {
2034
2061
  t.exports = class extends n {
2035
2062
  constructor(e = "/", t = {}, n = {}, r = null) {
2036
2063
  if (super(), t instanceof Uint8Array || t instanceof ArrayBuffer) {
2037
- let { deserializeBinaryFilesystem: e } = J(), r = e(t);
2064
+ let { deserializeBinaryFilesystem: e } = Y(), r = e(t);
2038
2065
  t = r.filesystem, n = Object.assign({}, r.permissions, n);
2039
2066
  } else if (t && typeof t == "object" && t.filesystem && (t.filesystem instanceof Uint8Array || typeof t.filesystem == "object") && (n = Object.assign({}, t.permissions || {}, n), t = t.filesystem, t instanceof Uint8Array || t instanceof ArrayBuffer)) {
2040
- let { deserializeBinaryFilesystem: e } = J(), r = e(t);
2067
+ let { deserializeBinaryFilesystem: e } = Y(), r = e(t);
2041
2068
  t = r.filesystem, n = Object.assign({}, r.permissions, n);
2042
2069
  }
2043
2070
  a(t), this.fileSystem = t, this.permissions = Object.assign({}, n), this.cwd = e, this._onChange = r, this._changeScheduled = !1;
2044
2071
  }
2045
2072
  toBinaryContainer(e = {}) {
2046
- let { serializeBinaryFilesystem: t } = J();
2073
+ let { serializeBinaryFilesystem: t } = Y();
2047
2074
  return t(this.fileSystem, this.permissions, e);
2048
2075
  }
2049
2076
  static fromBinaryContainer(e, t = {}) {
2050
- let { deserializeBinaryFilesystem: n } = J();
2077
+ let { deserializeBinaryFilesystem: n } = Y();
2051
2078
  return n(e, t);
2052
2079
  }
2053
2080
  getFileByPath(e) {
@@ -2225,10 +2252,10 @@ var R, z, B, V, H, U, W, G, ee, K, te = e((() => {
2225
2252
  return null;
2226
2253
  }
2227
2254
  }, t.exports.BinaryFile = r, t.exports.SymlinkRef = i;
2228
- })), Y = /* @__PURE__ */ t(((e, t) => {
2255
+ })), ne = /* @__PURE__ */ t(((e, t) => {
2229
2256
  t.exports = null;
2230
2257
  })), re = /* @__PURE__ */ t(((e, t) => {
2231
- te();
2258
+ ee();
2232
2259
  function n(e) {
2233
2260
  if (typeof e != "string") throw TypeError("Path must be a string. Received " + JSON.stringify(e));
2234
2261
  }
@@ -2266,7 +2293,7 @@ var R, z, B, V, H, U, W, G, ee, K, te = e((() => {
2266
2293
  resolve: function() {
2267
2294
  for (var e = "", t = !1, i, a = arguments.length - 1; a >= -1 && !t; a--) {
2268
2295
  var o;
2269
- 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);
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);
2270
2297
  }
2271
2298
  return e = r(e, !t), t ? e.length > 0 ? "/" + e : "/" : e.length > 0 ? e : ".";
2272
2299
  },
@@ -2397,7 +2424,7 @@ var R, z, B, V, H, U, W, G, ee, K, te = e((() => {
2397
2424
  a.posix = a, t.exports = a;
2398
2425
  })), X = /* @__PURE__ */ t(((e, t) => {
2399
2426
  b();
2400
- var n = D(), { BinaryFile: r } = ne(), i = 0, a = 8, o = 20, s = 28, c = 31, l = 44, u = 54, d = 70, f = 0, p = 2, m = 3, h = 4, g = 1, _ = 2, v = 4, x = 8, S = 1, C = class extends n {
2427
+ var n = D(), { BinaryFile: r } = te(), i = 0, a = 8, o = 20, s = 28, c = 31, l = 44, u = 54, d = 70, f = 0, p = 2, m = 3, h = 4, g = 1, _ = 2, v = 4, x = 8, S = 1, C = class extends n {
2401
2428
  get name() {
2402
2429
  return "wasm";
2403
2430
  }
@@ -2407,7 +2434,7 @@ var R, z, B, V, H, U, W, G, ee, K, te = e((() => {
2407
2434
  run() {
2408
2435
  let e = this.script, t = (() => {
2409
2436
  try {
2410
- return Y();
2437
+ return ne();
2411
2438
  } catch {
2412
2439
  return null;
2413
2440
  }
@@ -2842,7 +2869,7 @@ var R, z, B, V, H, U, W, G, ee, K, te = e((() => {
2842
2869
  function T(e, t) {
2843
2870
  if (!e || !e.exists(t)) return null;
2844
2871
  if (e.isRealFilesystem) try {
2845
- let n = Y();
2872
+ let n = ne();
2846
2873
  return new Uint8Array(n.readFileSync(e.absolutePath(t)));
2847
2874
  } catch {}
2848
2875
  let n = e.getFileByPath(t);
@@ -2871,7 +2898,7 @@ var R, z, B, V, H, U, W, G, ee, K, te = e((() => {
2871
2898
  o.set(a);
2872
2899
  let s = new r(o.buffer, i);
2873
2900
  if (e.isRealFilesystem) try {
2874
- let n = Y(), r = re(), i = e.absolutePath(t);
2901
+ let n = ne(), r = re(), i = e.absolutePath(t);
2875
2902
  n.mkdirSync(r.dirname(i), { recursive: !0 }), n.writeFileSync(i, y.from(a.buffer, a.byteOffset, a.byteLength));
2876
2903
  return;
2877
2904
  } catch {
@@ -3245,7 +3272,7 @@ var R, z, B, V, H, U, W, G, ee, K, te = e((() => {
3245
3272
  }
3246
3273
  C.readBytesFromFs = T, C.writeBytesToFs = O, C.loadModule = k, C.extractHelpFromText = A, C.inspectWasmBytes = j, C.formatWasmHelp = M, C.createGenericWasmCommand = N, C.createCommand = N, t.exports = C;
3247
3274
  })), Z = /* @__PURE__ */ t(((e, t) => {
3248
- var n = q(), { createGenericWasmCommand: r } = X(), i = "\x1B[2J\x1B[H", a = "\x1B[0m", o = "\x1B[7m", s = "\x1B[1m", c = "\x1B[2m", l = "\x1B[36m", u = "\x1B[31m", d = "\x1B[32m", f = class extends n {
3275
+ var n = J(), { createGenericWasmCommand: r } = X(), i = "\x1B[2J\x1B[H", a = "\x1B[0m", o = "\x1B[7m", s = "\x1B[1m", c = "\x1B[2m", l = "\x1B[36m", u = "\x1B[31m", d = "\x1B[32m", f = class extends n {
3249
3276
  get requiresFilesystem() {
3250
3277
  return !0;
3251
3278
  }
@@ -3304,6 +3331,20 @@ var R, z, B, V, H, U, W, G, ee, K, te = e((() => {
3304
3331
  });
3305
3332
  this._scrollOffset = 0;
3306
3333
  }
3334
+ streamReplChunk(e, t = "out") {
3335
+ if (!e) return;
3336
+ (this._replLog.length === 0 || this._replLog[this._replLog.length - 1].type !== t || !this._replStreamingActive) && (this._replLog.push({
3337
+ text: "",
3338
+ type: t
3339
+ }), this._replStreamingActive = !0);
3340
+ let n = this._replLog[this._replLog.length - 1], r = (n.text + e).split(/\r?\n/);
3341
+ n.text = r[0];
3342
+ for (let e = 1; e < r.length; e++) this._replLog.push({
3343
+ text: r[e],
3344
+ type: t
3345
+ });
3346
+ this._scrollOffset = 0, this._renderRepl();
3347
+ }
3307
3348
  onInput(e) {
3308
3349
  if (this.shouldBeInteractive()) {
3309
3350
  if (e === "^D" || e === "^X") {
@@ -3337,8 +3378,8 @@ var R, z, B, V, H, U, W, G, ee, K, te = e((() => {
3337
3378
  }
3338
3379
  let n = this._dynamicReplBuffer || e;
3339
3380
  this._dynamicReplBuffer = "", this._dynamicReplMultiline = !1, this._replBusy = !0, this._replStatus = "Running...", this._renderRepl(), Promise.resolve().then(() => this.evaluateReplLine(n)).then((e) => {
3340
- if (this._replBusy = !1, this._replStatus = "^D / .exit: Quit | ^L: Clear | Up/Down: History", e) {
3341
- if (e.clear && (this._replLog = []), Array.isArray(e.output)) for (let t of e.output) this._appendReplLog(t, "out");
3381
+ if (this._replBusy = !1, this._replStreamingActive = !1, this._replStatus = "^D / .exit: Quit | ^L: Clear | Up/Down: History", e) {
3382
+ if (e.clear && (this._replLog = []), !e.streamed) if (Array.isArray(e.output)) for (let t of e.output) this._appendReplLog(t, "out");
3342
3383
  else typeof e.output == "string" && e.output.length > 0 && this._appendReplLog(e.output, "out");
3343
3384
  if (e.error && this._appendReplLog(e.error, "err"), e.exit) {
3344
3385
  this._exitRepl();
@@ -3914,25 +3955,27 @@ var R, z, B, V, H, U, W, G, ee, K, te = e((() => {
3914
3955
  };
3915
3956
  }
3916
3957
  async function h(e = {}) {
3917
- let { modelSpec: t, mmprojSpec: n = null, loadParams: c = {}, fs: d, pkgMeta: f = {}, wbtConfig: h = {}, loadModel: g = !0, persist: _ = !1, persistPath: v = null } = e, y = await a("@wllama/wllama", f.cdnUrl, h.moduleLoader), b = y.Wllama || y.default || y, x = y.LoggerWithoutDebug || void 0, S = f.wasmUrl || s;
3958
+ let { modelSpec: t, mmprojSpec: n = null, loadParams: c = {}, fs: d, pkgMeta: f = {}, wbtConfig: h = {}, loadModel: g = !0, persist: _ = !1, persistPath: v = null, onStatus: y = null } = e;
3959
+ typeof y == "function" && y("Initializing WASM runtime...");
3960
+ let b = await a("@wllama/wllama", f.cdnUrl, h.moduleLoader), x = b.Wllama || b.default || b, S = b.LoggerWithoutDebug || void 0, C = f.wasmUrl || s;
3918
3961
  if (f.wasmPath && d && typeof URL < "u" && typeof URL.createObjectURL == "function") {
3919
3962
  let e = r(d, f.wasmPath);
3920
- e && e.byteLength > 0 && (S = URL.createObjectURL(new Blob([e], { type: "application/wasm" })));
3963
+ e && e.byteLength > 0 && (C = URL.createObjectURL(new Blob([e], { type: "application/wasm" })));
3921
3964
  }
3922
- let C = {
3923
- default: S,
3924
- "single-thread/wllama.wasm": S,
3925
- "multi-thread/wllama.wasm": S
3926
- }, w = () => {
3965
+ let w = {
3966
+ default: C,
3967
+ "single-thread/wllama.wasm": C,
3968
+ "multi-thread/wllama.wasm": C
3969
+ }, T = () => {
3927
3970
  try {
3928
- return new b(C, {
3971
+ return new x(w, {
3929
3972
  suppressNativeLog: !0,
3930
- logger: x
3973
+ logger: S
3931
3974
  });
3932
3975
  } catch {
3933
- return new b(C, {
3976
+ return new x(w, {
3934
3977
  suppressNativeLog: !0,
3935
- logger: x,
3978
+ logger: S,
3936
3979
  cacheManager: {
3937
3980
  isSupported: () => !0,
3938
3981
  getNameFromURL: async (e) => e,
@@ -3944,58 +3987,61 @@ var R, z, B, V, H, U, W, G, ee, K, te = e((() => {
3944
3987
  }
3945
3988
  };
3946
3989
  if (!g) return {
3947
- wllama: w(),
3948
- mod: y,
3949
- Wllama: b,
3990
+ wllama: T(),
3991
+ mod: b,
3992
+ Wllama: x,
3950
3993
  modelKey: null,
3951
3994
  isCached: !1,
3952
3995
  persisted: null
3953
3996
  };
3954
- let T = v || (_ && t.kind !== "fs" ? u(t) : null), E = t.kind === "fs" ? null : u(t), D = t.kind === "fs" && d && typeof d.absolutePath == "function" ? `fs:${d.absolutePath(t.path)}:gpu=${c.n_gpu_layers ?? "auto"}:emb=${!!c.embeddings}` : `${t.key}:gpu=${c.n_gpu_layers ?? "auto"}:emb=${!!c.embeddings}`;
3955
- if (l.has(D)) {
3956
- let e = l.get(D);
3997
+ let E = v || (_ && t.kind !== "fs" ? u(t) : null), D = t.kind === "fs" ? null : u(t), O = t.kind === "fs" && d && typeof d.absolutePath == "function" ? `fs:${d.absolutePath(t.path)}:gpu=${c.n_gpu_layers ?? "auto"}:emb=${!!c.embeddings}` : `${t.key}:gpu=${c.n_gpu_layers ?? "auto"}:emb=${!!c.embeddings}`;
3998
+ if (l.has(O)) {
3999
+ let e = l.get(O);
3957
4000
  if (typeof e.isModelLoaded != "function" || e.isModelLoaded()) {
4001
+ typeof y == "function" && y("Loaded model from cache.");
3958
4002
  let n = null;
3959
- if ((_ || v) && d && T) if (d.exists(T)) {
3960
- let e = r(d, T);
4003
+ if ((_ || v) && d && E) if (d.exists(E)) {
4004
+ let e = r(d, E);
3961
4005
  n = {
3962
- path: T,
4006
+ path: E,
3963
4007
  size: e ? e.byteLength : 0
3964
4008
  };
3965
4009
  } else n = await m({
3966
4010
  wllama: e,
3967
- mod: y,
4011
+ mod: b,
3968
4012
  modelSpec: t,
3969
- targetPersistPath: T,
4013
+ targetPersistPath: E,
3970
4014
  fs: d,
3971
4015
  progressCallback: c.progressCallback
3972
4016
  });
3973
4017
  return {
3974
4018
  wllama: e,
3975
- mod: y,
3976
- Wllama: b,
3977
- modelKey: D,
4019
+ mod: b,
4020
+ Wllama: x,
4021
+ modelKey: O,
3978
4022
  isCached: !0,
3979
4023
  persisted: n
3980
4024
  };
3981
4025
  }
3982
4026
  }
3983
- let O = w(), k = null, A = null;
4027
+ let k = T(), A = null, j = null;
3984
4028
  if (t.kind !== "fs" && d) {
3985
- let e = T && d.exists(T) ? T : E && d.exists(E) ? E : null;
4029
+ let e = E && d.exists(E) ? E : D && d.exists(D) ? D : null;
3986
4030
  if (e) {
3987
4031
  let t = r(d, e);
3988
4032
  if (t && t.byteLength > 0) {
4033
+ typeof y == "function" && y(`Loading model from ${e}...`);
3989
4034
  let n = new Blob([t], { type: "application/octet-stream" });
3990
- n.name = e.split("/").pop() || "model.gguf", await O.loadModel([n], c), A = {
4035
+ n.name = e.split("/").pop() || "model.gguf", await k.loadModel([n], c), j = {
3991
4036
  path: e,
3992
4037
  bytes: t
3993
4038
  };
3994
4039
  }
3995
4040
  }
3996
4041
  }
3997
- if (A) T && T !== A.path && i(d, T, A.bytes, "application/octet-stream");
4042
+ if (j) E && E !== j.path && i(d, E, j.bytes, "application/octet-stream");
3998
4043
  else if (t.kind === "fs") {
4044
+ typeof y == "function" && y(`Loading model from ${t.path}...`);
3999
4045
  let e = r(d, t.path);
4000
4046
  if (!e) throw Error(`${t.path}: No such file or directory`);
4001
4047
  let a = new Blob([e], { type: "application/octet-stream" });
@@ -4007,59 +4053,65 @@ var R, z, B, V, H, U, W, G, ee, K, te = e((() => {
4007
4053
  let t = new Blob([e], { type: "application/octet-stream" });
4008
4054
  t.name = n.split("/").pop() || "mmproj.gguf", o.push(t);
4009
4055
  }
4010
- await O.loadModel(o, c), T && T !== t.path && i(d, T, e, "application/octet-stream");
4011
- } else if (t.kind === "hf-quant" && typeof O.loadModelFromHF == "function") try {
4012
- await O.loadModelFromHF({
4013
- repo: t.repo,
4014
- quant: t.quant
4015
- }, c);
4016
- } catch (e) {
4017
- if (typeof y.getHFModelSource == "function") {
4018
- let e = await y.getHFModelSource({
4056
+ typeof y == "function" && y(`Loading model ${a.name} into memory...`), await k.loadModel(o, c), E && E !== t.path && i(d, E, e, "application/octet-stream");
4057
+ } else if (t.kind === "hf-quant" && typeof k.loadModelFromHF == "function") {
4058
+ typeof y == "function" && y(`Pulling model ${t.repo} (${t.quant})...`);
4059
+ try {
4060
+ await k.loadModelFromHF({
4019
4061
  repo: t.repo,
4020
4062
  quant: t.quant
4021
- });
4022
- k = await p(e.url || e, c.progressCallback), await O.loadModel([k], c);
4023
- } else throw e;
4024
- }
4025
- else if (t.kind === "hf-file" && typeof O.loadModelFromHF == "function") try {
4026
- await O.loadModelFromHF({
4027
- repo: t.repo,
4028
- file: t.file
4029
- }, c);
4030
- } catch {
4031
- k = await p(t.url, c.progressCallback), await O.loadModel([k], c);
4032
- }
4033
- else {
4063
+ }, c);
4064
+ } catch (e) {
4065
+ if (typeof b.getHFModelSource == "function") {
4066
+ let e = await b.getHFModelSource({
4067
+ repo: t.repo,
4068
+ quant: t.quant
4069
+ });
4070
+ typeof y == "function" && y(`Pulling model ${t.repo} (${t.quant})...`), A = await p(e.url || e, c.progressCallback), typeof y == "function" && y("Loading model into memory..."), await k.loadModel([A], c);
4071
+ } else throw e;
4072
+ }
4073
+ } else if (t.kind === "hf-file" && typeof k.loadModelFromHF == "function") {
4074
+ typeof y == "function" && y(`Pulling model ${t.repo}/${t.file}...`);
4075
+ try {
4076
+ await k.loadModelFromHF({
4077
+ repo: t.repo,
4078
+ file: t.file
4079
+ }, c);
4080
+ } catch {
4081
+ A = await p(t.url, c.progressCallback), typeof y == "function" && y("Loading model into memory..."), await k.loadModel([A], c);
4082
+ }
4083
+ } else {
4034
4084
  let e = t.url || o;
4085
+ typeof y == "function" && y(`Pulling model from ${e.split("/").pop().split("?")[0] || e}...`);
4035
4086
  try {
4036
- n && /^https?:\/\//i.test(n) ? await O.loadModelFromUrl({
4087
+ n && /^https?:\/\//i.test(n) ? await k.loadModelFromUrl({
4037
4088
  url: e,
4038
4089
  mmprojUrl: n
4039
- }, c) : await O.loadModelFromUrl(e, c);
4090
+ }, c) : await k.loadModelFromUrl(e, c);
4040
4091
  } catch {
4041
- k = await p(e, c.progressCallback), await O.loadModel([k], c);
4092
+ A = await p(e, c.progressCallback), typeof y == "function" && y("Loading model into memory..."), await k.loadModel([A], c);
4042
4093
  }
4043
4094
  }
4044
- let j = null;
4045
- return A ? j = {
4046
- path: T || A.path,
4047
- size: A.bytes.byteLength
4048
- } : (_ || v) && d && T && t.kind !== "fs" && (j = await m({
4049
- wllama: O,
4050
- mod: y,
4095
+ typeof y == "function" && y("Model loaded.");
4096
+ let M = null;
4097
+ return j ? M = {
4098
+ path: E || j.path,
4099
+ size: j.bytes.byteLength
4100
+ } : (_ || v) && d && E && t.kind !== "fs" && (M = await m({
4101
+ wllama: k,
4102
+ mod: b,
4051
4103
  modelSpec: t,
4052
- downloadedBlob: k,
4053
- targetPersistPath: T,
4104
+ downloadedBlob: A,
4105
+ targetPersistPath: E,
4054
4106
  fs: d,
4055
4107
  progressCallback: c.progressCallback
4056
- })), l.set(D, O), {
4057
- wllama: O,
4058
- mod: y,
4059
- Wllama: b,
4060
- modelKey: D,
4108
+ })), l.set(O, k), {
4109
+ wllama: k,
4110
+ mod: b,
4111
+ Wllama: x,
4112
+ modelKey: O,
4061
4113
  isCached: !1,
4062
- persisted: j
4114
+ persisted: M
4063
4115
  };
4064
4116
  }
4065
4117
  function g(e) {
@@ -4088,22 +4140,22 @@ var R, z, B, V, H, U, W, G, ee, K, te = e((() => {
4088
4140
  });
4089
4141
  }
4090
4142
  _parseArgs(e) {
4091
- let t = null, n = null, r = null, i = null, a = null, o = null, s = null, c = null, l = 256, u = .7, d, p, m = 4096, h, g, _, v = !1, y = !1, b = !1, x = !0, S = !1, C = null, w = !0, T = !1, E = !1, D = null, O = [], k = [];
4143
+ let t = null, n = null, r = null, i = null, a = null, o = null, s = null, c = null, l = 256, u = .7, d, p, m = 4096, h, g, _, v = !1, y = !1, b = !1, x = !0, S = !0, C = !1, w = null, T = !0, E = !1, D = !1, O = null, k = [], A = [];
4092
4144
  for (let f = 0; f < e.length; f++) {
4093
- let A = e[f];
4094
- if (A === "--help" || A === "-h" && f + 1 >= e.length || A === "-?") T = !0;
4095
- else if (A === "--version" || A === "-V" || A === "-v") E = !0;
4096
- else if (A === "--serve" || A === "--daemon") D = "serve";
4097
- else if (A === "-q" || A === "--quiet" || A === "--no-progress") w = !1;
4098
- else if (A === "--progress") w = !0;
4099
- else if (A === "--persist" || A === "--save") {
4100
- S = !0;
4145
+ let j = e[f];
4146
+ if (j === "--help" || j === "-h" && f + 1 >= e.length || j === "-?") E = !0;
4147
+ else if (j === "--version" || j === "-V" || j === "-v") D = !0;
4148
+ else if (j === "--serve" || j === "--daemon") O = "serve";
4149
+ else if (j === "-q" || j === "--quiet" || j === "--no-progress") T = !1;
4150
+ else if (j === "--progress") T = !0;
4151
+ else if (j === "--persist" || j === "--save") {
4152
+ C = !0;
4101
4153
  let n = e[f + 1];
4102
- 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 || f + 2 < e.length || D === "pull") && (t !== null || r !== null) && (C = e[++f]);
4103
- } else A.startsWith("--persist=") ? (S = !0, C = A.slice(10) || null) : A.startsWith("--save=") ? (S = !0, C = A.slice(7) || null) : (A === "-o" || A === "--output") && f + 1 < e.length ? (S = !0, C = e[++f]) : A.startsWith("--output=") ? (S = !0, C = A.slice(9) || null) : (A === "-m" || A === "--model" || A === "--url") && f + 1 < e.length ? t = e[++f] : A.startsWith("--model=") ? t = A.slice(8) : A === "--mmproj" && f + 1 < e.length ? n = e[++f] : (A === "--hf" || A === "-hf" || A === "--hf-repo") && f + 1 < e.length ? r = e[++f] : A === "--hf-file" && f + 1 < e.length ? i = e[++f] : (A === "--quant" || A === "--hf-quant") && f + 1 < e.length ? a = e[++f] : (A === "-p" || A === "--prompt") && f + 1 < e.length ? o = e[++f] : A.startsWith("--prompt=") ? o = A.slice(9) : (A === "-f" || A === "--file") && f + 1 < e.length ? s = e[++f] : (A === "-s" || A === "--system" || A === "--system-prompt") && f + 1 < e.length ? c = e[++f] : (A === "-n" || A === "--n-predict" || A === "--max-tokens") && f + 1 < e.length ? l = Number(e[++f]) || 256 : (A === "--temp" || A === "--temperature") && f + 1 < e.length ? u = Number(e[++f]) : (A === "--top-k" || A === "--top_k") && f + 1 < e.length ? d = Number(e[++f]) : (A === "--top-p" || A === "--top_p") && f + 1 < e.length ? p = Number(e[++f]) : (A === "-c" || A === "--ctx-size" || A === "--n-ctx") && f + 1 < e.length ? m = Number(e[++f]) || 4096 : (A === "-b" || A === "--batch-size") && f + 1 < e.length ? h = Number(e[++f]) : (A === "-t" || A === "--threads") && f + 1 < e.length ? g = Number(e[++f]) : (A === "-ngl" || A === "--n-gpu-layers" || A === "--gpu-layers") && f + 1 < e.length ? _ = Number(e[++f]) : A === "--no-gpu" || A === "--cpu" ? _ = 0 : A === "--webgpu" || A === "--gpu" ? (_ === void 0 || _ === 0) && (_ = 99) : A === "--embeddings" || A === "--embedding" || A === "--embed" ? (v = !0, D ||= "embed") : A === "--raw" || A === "--completion" ? y = !0 : A === "--json" ? b = !0 : A === "--no-jinja" ? x = !1 : A.startsWith("-") ? O.push(A) : k.push(A);
4154
+ 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 || f + 2 < e.length || O === "pull") && (t !== null || r !== null) && (w = e[++f]);
4155
+ } else j.startsWith("--persist=") ? (C = !0, w = j.slice(10) || null) : j.startsWith("--save=") ? (C = !0, w = j.slice(7) || null) : (j === "-o" || j === "--output") && f + 1 < e.length ? (C = !0, w = e[++f]) : j.startsWith("--output=") ? (C = !0, w = j.slice(9) || null) : (j === "-m" || j === "--model" || j === "--url") && f + 1 < e.length ? t = e[++f] : j.startsWith("--model=") ? t = j.slice(8) : j === "--mmproj" && f + 1 < e.length ? n = e[++f] : (j === "--hf" || j === "-hf" || j === "--hf-repo") && f + 1 < e.length ? r = e[++f] : j === "--hf-file" && f + 1 < e.length ? i = e[++f] : (j === "--quant" || j === "--hf-quant") && f + 1 < e.length ? a = e[++f] : (j === "-p" || j === "--prompt") && f + 1 < e.length ? o = e[++f] : j.startsWith("--prompt=") ? o = j.slice(9) : (j === "-f" || j === "--file") && f + 1 < e.length ? s = e[++f] : (j === "-s" || j === "--system" || j === "--system-prompt") && f + 1 < e.length ? c = e[++f] : (j === "-n" || j === "--n-predict" || j === "--max-tokens") && f + 1 < e.length ? l = Number(e[++f]) || 256 : (j === "--temp" || j === "--temperature") && f + 1 < e.length ? u = Number(e[++f]) : (j === "--top-k" || j === "--top_k") && f + 1 < e.length ? d = Number(e[++f]) : (j === "--top-p" || j === "--top_p") && f + 1 < e.length ? p = Number(e[++f]) : (j === "-c" || j === "--ctx-size" || j === "--n-ctx") && f + 1 < e.length ? m = Number(e[++f]) || 4096 : (j === "-b" || j === "--batch-size") && f + 1 < e.length ? h = Number(e[++f]) : (j === "-t" || j === "--threads") && f + 1 < e.length ? g = Number(e[++f]) : (j === "-ngl" || j === "--n-gpu-layers" || j === "--gpu-layers") && f + 1 < e.length ? _ = Number(e[++f]) : j === "--no-gpu" || j === "--cpu" ? _ = 0 : j === "--webgpu" || j === "--gpu" ? (_ === void 0 || _ === 0) && (_ = 99) : j === "--embeddings" || j === "--embedding" || j === "--embed" ? (v = !0, O ||= "embed") : j === "--raw" || j === "--completion" ? y = !0 : j === "--stream" ? x = !0 : j === "--no-stream" ? x = !1 : j === "--json" ? b = !0 : j === "--no-jinja" ? S = !1 : j.startsWith("-") ? k.push(j) : A.push(j);
4104
4156
  }
4105
- if (k.length > 0) {
4106
- let e = k[0].toLowerCase();
4157
+ if (A.length > 0) {
4158
+ let e = A[0].toLowerCase();
4107
4159
  if ([
4108
4160
  "info",
4109
4161
  "models",
@@ -4118,23 +4170,23 @@ var R, z, B, V, H, U, W, G, ee, K, te = e((() => {
4118
4170
  "pull",
4119
4171
  "download"
4120
4172
  ].includes(e)) {
4121
- let e = k.shift().toLowerCase();
4122
- e === "list" ? D = "models" : e === "embeddings" ? (D = "embed", v = !0) : e === "test-ops" ? D = "bench" : e === "completion" ? (D = "run", y = !0) : e === "download" || e === "pull" ? (D = "pull", S = !0) : D = e;
4173
+ let e = A.shift().toLowerCase();
4174
+ e === "list" ? O = "models" : e === "embeddings" ? (O = "embed", v = !0) : e === "test-ops" ? O = "bench" : e === "completion" ? (O = "run", y = !0) : e === "download" || e === "pull" ? (O = "pull", C = !0) : O = e;
4123
4175
  }
4124
4176
  }
4125
- if (k.length > 0 && (!t && !r && (D === "pull" || /\.gguf(\?.*)?$/i.test(k[0]) || /^hf:/i.test(k[0]) || /^https?:\/\//i.test(k[0])) && (t = k.shift()), D === "pull" && k.length > 0 && !C ? C = k.shift() : k.length > 0 && o === null && (o = k.join(" "))), !t && !r && this.context && typeof this.context.getVar == "function") {
4177
+ if (A.length > 0 && (!t && !r && (O === "pull" || /\.gguf(\?.*)?$/i.test(A[0]) || /^hf:/i.test(A[0]) || /^https?:\/\//i.test(A[0])) && (t = A.shift()), O === "pull" && A.length > 0 && !w ? w = A.shift() : A.length > 0 && o === null && (o = A.join(" "))), !t && !r && this.context && typeof this.context.getVar == "function") {
4126
4178
  let e = this.context.getVar("WLLAMA_MODEL");
4127
4179
  e && (t = e);
4128
4180
  }
4129
- let A = f(t, r, i, a), j = {
4181
+ let j = f(t, r, i, a), M = {
4130
4182
  n_ctx: m,
4131
- jinja: x
4183
+ jinja: S
4132
4184
  };
4133
- return _ !== void 0 && (j.n_gpu_layers = _), h !== void 0 && (j.n_batch = h), g !== void 0 && (j.n_threads = g), v && (j.embeddings = !0), {
4185
+ return _ !== void 0 && (M.n_gpu_layers = _), h !== void 0 && (M.n_batch = h), g !== void 0 && (M.n_threads = g), v && (M.embeddings = !0), {
4134
4186
  model: t,
4135
4187
  mmproj: n,
4136
- modelSpec: A,
4137
- loadParams: j,
4188
+ modelSpec: j,
4189
+ loadParams: M,
4138
4190
  prompt: o,
4139
4191
  file: s,
4140
4192
  systemPrompt: c,
@@ -4147,15 +4199,16 @@ var R, z, B, V, H, U, W, G, ee, K, te = e((() => {
4147
4199
  embeddings: v,
4148
4200
  rawCompletion: y,
4149
4201
  jsonOutput: b,
4150
- persist: S,
4151
- persistPath: C,
4152
- showProgress: w,
4153
- help: T,
4154
- version: E,
4155
- subcommand: D,
4202
+ stream: x,
4203
+ persist: C,
4204
+ persistPath: w,
4205
+ showProgress: T,
4206
+ help: E,
4207
+ version: D,
4208
+ subcommand: O,
4156
4209
  host: "127.0.0.1",
4157
4210
  port: 8080,
4158
- extraArgs: O
4211
+ extraArgs: k
4159
4212
  };
4160
4213
  }
4161
4214
  shouldBeInteractive() {
@@ -4176,12 +4229,22 @@ var R, z, B, V, H, U, W, G, ee, K, te = e((() => {
4176
4229
  ];
4177
4230
  return this._persisted && this._persisted.path && n.push(`Saved: ${this._persisted.path}`), n.push("Type a message to chat, \"/help\" for commands, or \".exit\" / Ctrl+D to quit."), n;
4178
4231
  }
4232
+ _renderStatus(e) {
4233
+ !this._parsed.showProgress || this._parsed.jsonOutput || (this.emit("wllama:status", e), this.stderr && typeof this.stderr.write == "function" && this.stderr.write(`\r\x1b[K${e}`));
4234
+ }
4235
+ _clearStatus() {
4236
+ !this._parsed.showProgress || this._parsed.jsonOutput || this.stderr && typeof this.stderr.write == "function" && this.stderr.write("\r\x1B[K");
4237
+ }
4179
4238
  _attachProgressCallback() {
4180
4239
  if (!this._parsed.showProgress || this._parsed.modelSpec.kind === "fs") return;
4181
4240
  let e = this._parsed.model || this._parsed.modelSpec.file || this._parsed.modelSpec.repo || this._parsed.modelSpec.url || "model.gguf", t = 0;
4182
4241
  this._parsed.loadParams.progressCallback = ({ loaded: n, total: r }) => {
4183
4242
  let i = Date.now();
4184
- (t === 0 || r > 0 && n >= r || i - t >= 40) && (t = i, this.stdOut = d(e, n, r), this.flush());
4243
+ if (t === 0 || r > 0 && n >= r || i - t >= 40) {
4244
+ t = i;
4245
+ let a = d(e, n, r);
4246
+ this.interactive ? (this._replStatus = a.replace(/^\r/, ""), this._renderRepl()) : this._parsed.subcommand === "pull" ? (this.stdOut = a, this.flush()) : this._renderStatus(a.replace(/^\r/, ""));
4247
+ }
4185
4248
  };
4186
4249
  }
4187
4250
  async _initWllama(n = !0) {
@@ -4195,9 +4258,12 @@ var R, z, B, V, H, U, W, G, ee, K, te = e((() => {
4195
4258
  wbtConfig: t,
4196
4259
  loadModel: n,
4197
4260
  persist: this._parsed.persist,
4198
- persistPath: this._parsed.persistPath
4261
+ persistPath: this._parsed.persistPath,
4262
+ onStatus: (e) => {
4263
+ this.interactive ? (this._replStatus = e, this._renderRepl()) : this._renderStatus(e);
4264
+ }
4199
4265
  });
4200
- this._wllama = r, this._WllamaClass = i, this._persisted = a;
4266
+ this._wllama = r, this._WllamaClass = i, this._persisted = a, this.interactive || this._clearStatus();
4201
4267
  }
4202
4268
  async initRepl() {
4203
4269
  await this._initWllama(!0);
@@ -4238,33 +4304,57 @@ var R, z, B, V, H, U, W, G, ee, K, te = e((() => {
4238
4304
  role: "user",
4239
4305
  content: t
4240
4306
  });
4241
- let e = await this._runChatTurn(this._messages);
4307
+ let e = !1, n = await this._runChatTurn(this._messages, (t) => {
4308
+ e = !0, typeof this.streamReplChunk == "function" && this.streamReplChunk(t);
4309
+ });
4242
4310
  return this._messages.push({
4243
4311
  role: "assistant",
4244
- content: e
4245
- }), { output: e.split(/\r?\n/) };
4312
+ content: n
4313
+ }), e ? { streamed: !0 } : { output: n.split(/\r?\n/) };
4246
4314
  } catch (e) {
4247
4315
  return { error: `wllama: ${e.message || e}` };
4248
4316
  }
4249
4317
  }
4250
- async _runChatTurn(e) {
4251
- let t = {
4318
+ async _runChatTurn(e, t = null) {
4319
+ let n = !!(this._parsed.stream && !this._parsed.jsonOutput), r = {
4252
4320
  messages: e,
4253
4321
  max_tokens: this._parsed.maxTokens,
4254
4322
  temperature: this._parsed.temperature,
4255
- stream: !1
4323
+ stream: n
4256
4324
  };
4257
- if (this._parsed.topK !== void 0 && (t.top_k = this._parsed.topK), this._parsed.topP !== void 0 && (t.top_p = this._parsed.topP), typeof this._wllama.createChatCompletion == "function") {
4258
- let e = await this._wllama.createChatCompletion(t);
4259
- return this._parsed.jsonOutput ? JSON.stringify(e) : g(e);
4325
+ 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") {
4326
+ let e = await this._wllama.createChatCompletion(r);
4327
+ if (e && typeof e[Symbol.asyncIterator] == "function") {
4328
+ let n = "", r = !1;
4329
+ for await (let i of e) {
4330
+ let e = i?.choices?.[0]?.delta?.content ?? i?.choices?.[0]?.text ?? "";
4331
+ e && (r || (r = !0, this.interactive ? (this._replStatus = "Streaming tokens...", this._renderRepl()) : this._clearStatus()), n += e, typeof t == "function" && t(e));
4332
+ }
4333
+ return this.interactive || this._clearStatus(), n;
4334
+ }
4335
+ if (this._parsed.jsonOutput) return this.interactive || this._clearStatus(), JSON.stringify(e);
4336
+ let n = g(e);
4337
+ return this.interactive || this._clearStatus(), typeof t == "function" && n && t(n), n;
4260
4338
  }
4261
- let n = e[e.length - 1]?.content || "", r = await this._wllama.createCompletion({
4262
- prompt: n,
4339
+ let i = {
4340
+ prompt: e[e.length - 1]?.content || "",
4263
4341
  max_tokens: this._parsed.maxTokens,
4264
4342
  temperature: this._parsed.temperature,
4265
- stream: !1
4266
- });
4267
- return this._parsed.jsonOutput ? JSON.stringify(r) : g(r);
4343
+ stream: n
4344
+ };
4345
+ this._parsed.topK !== void 0 && (i.top_k = this._parsed.topK), this._parsed.topP !== void 0 && (i.top_p = this._parsed.topP);
4346
+ let a = await this._wllama.createCompletion(i);
4347
+ if (a && typeof a[Symbol.asyncIterator] == "function") {
4348
+ let e = "", n = !1;
4349
+ for await (let r of a) {
4350
+ let i = r?.choices?.[0]?.text ?? r?.choices?.[0]?.delta?.content ?? "";
4351
+ i && (n || (n = !0, this.interactive ? (this._replStatus = "Streaming tokens...", this._renderRepl()) : this._clearStatus()), e += i, typeof t == "function" && t(i));
4352
+ }
4353
+ return this.interactive || this._clearStatus(), e;
4354
+ }
4355
+ if (this.interactive || this._clearStatus(), this._parsed.jsonOutput) return JSON.stringify(a);
4356
+ let o = g(a);
4357
+ return typeof t == "function" && o && t(o), o;
4268
4358
  }
4269
4359
  _formatModelInfo() {
4270
4360
  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 = [
@@ -4362,6 +4452,8 @@ var R, z, B, V, H, U, W, G, ee, K, te = e((() => {
4362
4452
  " -ngl, --n-gpu-layers=N Number of layers offloaded to WebGPU (default: all)",
4363
4453
  " --no-gpu, --cpu Disable WebGPU and run on WASM SIMD CPU only",
4364
4454
  " --raw, --completion Use raw text completion instead of chat completion",
4455
+ " --stream Stream tokens as they are generated (default: enabled)",
4456
+ " --no-stream Disable token streaming and wait for full response",
4365
4457
  " --embed, --embeddings Generate embeddings instead of text completion",
4366
4458
  " --json Output raw JSON response",
4367
4459
  " -h, --help Show this help message and exit",
@@ -4403,15 +4495,23 @@ var R, z, B, V, H, U, W, G, ee, K, te = e((() => {
4403
4495
  return;
4404
4496
  }
4405
4497
  if (this._parsed.rawCompletion) {
4406
- let t = {
4498
+ let t = !!(this._parsed.stream && !this._parsed.jsonOutput), n = {
4407
4499
  prompt: e,
4408
4500
  max_tokens: this._parsed.maxTokens,
4409
4501
  temperature: this._parsed.temperature,
4410
- stream: !1
4502
+ stream: t
4411
4503
  };
4412
- this._parsed.topK !== void 0 && (t.top_k = this._parsed.topK), this._parsed.topP !== void 0 && (t.top_p = this._parsed.topP);
4413
- let n = await this._wllama.createCompletion(t);
4414
- (this._parsed.jsonOutput ? JSON.stringify(n) : g(n)).split(/\r?\n/).forEach((e) => {
4504
+ 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...");
4505
+ let r = await this._wllama.createCompletion(n), i = "";
4506
+ if (r && typeof r[Symbol.asyncIterator] == "function") {
4507
+ let e = !1;
4508
+ for await (let t of r) {
4509
+ let n = t?.choices?.[0]?.text ?? t?.choices?.[0]?.delta?.content ?? "";
4510
+ 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));
4511
+ }
4512
+ this._clearStatus(), e && i.length > 0 && !i.endsWith("\n") && this.stdout && typeof this.stdout.write == "function" && this.stdout.write("\r\n");
4513
+ } else this._clearStatus(), i = this._parsed.jsonOutput ? JSON.stringify(r) : g(r);
4514
+ i.split(/\r?\n/).forEach((e) => {
4415
4515
  this.stdOut = e;
4416
4516
  });
4417
4517
  return;
@@ -4423,7 +4523,15 @@ var R, z, B, V, H, U, W, G, ee, K, te = e((() => {
4423
4523
  }), t.push({
4424
4524
  role: "user",
4425
4525
  content: e
4426
- }), (await this._runChatTurn(t)).split(/\r?\n/).forEach((e) => {
4526
+ });
4527
+ let n = !!(this._parsed.stream && !this._parsed.jsonOutput), r = "";
4528
+ if (n) {
4529
+ let e = !1;
4530
+ r = await this._runChatTurn(t, (t) => {
4531
+ e || (e = !0, this._streamedStdout = !0), this.stdout && typeof this.stdout.write == "function" && this.stdout.write(t), this.emit("token", t);
4532
+ }), e && r.length > 0 && !r.endsWith("\n") && this.stdout && typeof this.stdout.write == "function" && this.stdout.write("\r\n");
4533
+ } else r = await this._runChatTurn(t);
4534
+ r.split(/\r?\n/).forEach((e) => {
4427
4535
  this.stdOut = e;
4428
4536
  });
4429
4537
  } catch (e) {