brsh 3.4.0 → 3.6.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
@@ -483,15 +483,15 @@ var u, d, f, p, m, h, g, _, v, y, b = e((() => {
483
483
  }, u.prototype.readUint32BE = u.prototype.readUInt32BE = function(e, t) {
484
484
  return e >>>= 0, t || H(e, 4, this.length), this[e] * 16777216 + (this[e + 1] << 16 | this[e + 2] << 8 | this[e + 3]);
485
485
  }, u.prototype.readBigUInt64LE = $(function(e) {
486
- e >>>= 0, X(e, "offset");
486
+ e >>>= 0, Z(e, "offset");
487
487
  let t = this[e], n = this[e + 7];
488
- (t === void 0 || n === void 0) && Z(e, this.length - 8);
488
+ (t === void 0 || n === void 0) && re(e, this.length - 8);
489
489
  let r = t + this[++e] * 2 ** 8 + this[++e] * 2 ** 16 + this[++e] * 2 ** 24, i = this[++e] + this[++e] * 2 ** 8 + this[++e] * 2 ** 16 + n * 2 ** 24;
490
490
  return BigInt(r) + (BigInt(i) << BigInt(32));
491
491
  }), u.prototype.readBigUInt64BE = $(function(e) {
492
- e >>>= 0, X(e, "offset");
492
+ e >>>= 0, Z(e, "offset");
493
493
  let t = this[e], n = this[e + 7];
494
- (t === void 0 || n === void 0) && Z(e, this.length - 8);
494
+ (t === void 0 || n === void 0) && re(e, this.length - 8);
495
495
  let r = t * 2 ** 24 + this[++e] * 2 ** 16 + this[++e] * 2 ** 8 + this[++e], i = this[++e] * 2 ** 24 + this[++e] * 2 ** 16 + this[++e] * 2 ** 8 + n;
496
496
  return (BigInt(r) << BigInt(32)) + BigInt(i);
497
497
  }), u.prototype.readIntLE = function(e, t, n) {
@@ -519,15 +519,15 @@ var u, d, f, p, m, h, g, _, v, y, b = e((() => {
519
519
  }, u.prototype.readInt32BE = function(e, t) {
520
520
  return e >>>= 0, t || H(e, 4, this.length), this[e] << 24 | this[e + 1] << 16 | this[e + 2] << 8 | this[e + 3];
521
521
  }, u.prototype.readBigInt64LE = $(function(e) {
522
- e >>>= 0, X(e, "offset");
522
+ e >>>= 0, Z(e, "offset");
523
523
  let t = this[e], n = this[e + 7];
524
- (t === void 0 || n === void 0) && Z(e, this.length - 8);
524
+ (t === void 0 || n === void 0) && re(e, this.length - 8);
525
525
  let r = this[e + 4] + this[e + 5] * 2 ** 8 + this[e + 6] * 2 ** 16 + (n << 24);
526
526
  return (BigInt(r) << BigInt(32)) + BigInt(t + this[++e] * 2 ** 8 + this[++e] * 2 ** 16 + this[++e] * 2 ** 24);
527
527
  }), u.prototype.readBigInt64BE = $(function(e) {
528
- e >>>= 0, X(e, "offset");
528
+ e >>>= 0, Z(e, "offset");
529
529
  let t = this[e], n = this[e + 7];
530
- (t === void 0 || n === void 0) && Z(e, this.length - 8);
530
+ (t === void 0 || n === void 0) && re(e, this.length - 8);
531
531
  let r = (t << 24) + this[++e] * 2 ** 16 + this[++e] * 2 ** 8 + this[++e];
532
532
  return (BigInt(r) << BigInt(32)) + BigInt(this[++e] * 2 ** 24 + this[++e] * 2 ** 16 + this[++e] * 2 ** 8 + n);
533
533
  }), u.prototype.readFloatLE = function(e, t) {
@@ -572,14 +572,14 @@ var u, d, f, p, m, h, g, _, v, y, b = e((() => {
572
572
  return e = +e, t >>>= 0, n || U(this, e, t, 4, 4294967295, 0), this[t] = e >>> 24, this[t + 1] = e >>> 16, this[t + 2] = e >>> 8, this[t + 3] = e & 255, t + 4;
573
573
  };
574
574
  function W(e, t, n, r, i) {
575
- re(t, r, i, e, n, 7);
575
+ ne(t, r, i, e, n, 7);
576
576
  let a = Number(t & BigInt(4294967295));
577
577
  e[n++] = a, a >>= 8, e[n++] = a, a >>= 8, e[n++] = a, a >>= 8, e[n++] = a;
578
578
  let o = Number(t >> BigInt(32) & BigInt(4294967295));
579
579
  return e[n++] = o, o >>= 8, e[n++] = o, o >>= 8, e[n++] = o, o >>= 8, e[n++] = o, n;
580
580
  }
581
581
  function G(e, t, n, r, i) {
582
- re(t, r, i, e, n, 7);
582
+ ne(t, r, i, e, n, 7);
583
583
  let a = Number(t & BigInt(4294967295));
584
584
  e[n + 7] = a, a >>= 8, e[n + 6] = a, a >>= 8, e[n + 5] = a, a >>= 8, e[n + 4] = a;
585
585
  let o = Number(t >> BigInt(32) & BigInt(4294967295));
@@ -631,13 +631,13 @@ var u, d, f, p, m, h, g, _, v, y, b = e((() => {
631
631
  }, u.prototype.writeFloatBE = function(e, t, n) {
632
632
  return q(this, e, t, !1, n);
633
633
  };
634
- function ee(e, t, r, i, a) {
634
+ function J(e, t, r, i, a) {
635
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 ee(this, e, t, !0, n);
638
+ return J(this, e, t, !0, n);
639
639
  }, u.prototype.writeDoubleBE = function(e, t, n) {
640
- return ee(this, e, t, !1, n);
640
+ return J(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 J = {};
672
- function Y(e, t, n) {
673
- J[e] = class extends n {
671
+ let Y = {};
672
+ function X(e, t, n) {
673
+ Y[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
- Y("ERR_BUFFER_OUT_OF_BOUNDS", function(e) {
697
+ X("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), Y("ERR_INVALID_ARG_TYPE", function(e, t) {
699
+ }, RangeError), X("ERR_INVALID_ARG_TYPE", function(e, t) {
700
700
  return `The "${e}" argument must be of type number. Received type ${typeof t}`;
701
- }, TypeError), Y("ERR_OUT_OF_RANGE", function(e, t, n) {
701
+ }, TypeError), X("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 = 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;
703
+ return Number.isInteger(n) && Math.abs(n) > 2 ** 32 ? i = ee(String(n)) : typeof n == "bigint" && (i = String(n), (n > BigInt(2) ** BigInt(32) || n < -(BigInt(2) ** BigInt(32))) && (i = ee(i)), i += "n"), r += ` It must be ${t}. Received ${i}`, r;
704
704
  }, RangeError);
705
- function te(e) {
705
+ function ee(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 ne(e, t, n) {
711
- X(t, "offset"), (e[t] === void 0 || e[t + n] === void 0) && Z(t, e.length - (n + 1));
710
+ function te(e, t, n) {
711
+ Z(t, "offset"), (e[t] === void 0 || e[t + n] === void 0) && re(t, e.length - (n + 1));
712
712
  }
713
- function re(e, t, n, r, i, a) {
713
+ function ne(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 J.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 Y.ERR_OUT_OF_RANGE("value", i, e);
717
717
  }
718
- ne(r, i, a);
718
+ te(r, i, a);
719
719
  }
720
- function X(e, t) {
721
- if (typeof e != "number") throw new J.ERR_INVALID_ARG_TYPE(t, "number", e);
720
+ function Z(e, t) {
721
+ if (typeof e != "number") throw new Y.ERR_INVALID_ARG_TYPE(t, "number", e);
722
722
  }
723
- function Z(e, t, n) {
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));
723
+ function re(e, t, n) {
724
+ throw Math.floor(e) === e ? t < 0 ? new Y.ERR_BUFFER_OUT_OF_BOUNDS() : new Y.ERR_OUT_OF_RANGE(n || "offset", `>= ${+!!n} and <= ${t}`, e) : (Z(e, n), new Y.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) {
@@ -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, K, q, J = e((() => {
1710
1710
  R = { exports: {} }, z = R.exports = {}, (function() {
1711
1711
  try {
1712
1712
  B = typeof setTimeout == "function" ? setTimeout : A;
@@ -1735,7 +1735,7 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
1735
1735
  }, z.umask = function() {
1736
1736
  return 0;
1737
1737
  }, K = R.exports, q = /* @__PURE__ */ k(K);
1738
- })), J = /* @__PURE__ */ t(((e, t) => {
1738
+ })), Y = /* @__PURE__ */ t(((e, t) => {
1739
1739
  var n = T();
1740
1740
  t.exports = class extends n {
1741
1741
  get captureInput() {
@@ -1765,8 +1765,8 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
1765
1765
  });
1766
1766
  }
1767
1767
  };
1768
- })), Y = /* @__PURE__ */ t(((e, t) => {
1769
- b(), ee();
1768
+ })), X = /* @__PURE__ */ t(((e, t) => {
1769
+ b(), J();
1770
1770
  var { BinaryFile: n, SymlinkRef: r } = O(), i = new Uint8Array([
1771
1771
  66,
1772
1772
  82,
@@ -1871,7 +1871,7 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
1871
1871
  let t = e.$c;
1872
1872
  if (m[t]) return m[t];
1873
1873
  if (f.inlineCommands && f.inlineCommands[t]) try {
1874
- let e = T(), n = J();
1874
+ let e = T(), n = Y();
1875
1875
  return Function("Command", "InteractiveCommand", `return (${f.inlineCommands[t]});`)(e, n);
1876
1876
  } catch {
1877
1877
  return null;
@@ -2036,7 +2036,7 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
2036
2036
  }
2037
2037
  }
2038
2038
  };
2039
- })), te = /* @__PURE__ */ t(((e, t) => {
2039
+ })), ee = /* @__PURE__ */ t(((e, t) => {
2040
2040
  b();
2041
2041
  var n = O(), { BinaryFile: r, SymlinkRef: i } = n;
2042
2042
  function a(e) {
@@ -2061,20 +2061,20 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
2061
2061
  t.exports = class extends n {
2062
2062
  constructor(e = "/", t = {}, n = {}, r = null) {
2063
2063
  if (super(), t instanceof Uint8Array || t instanceof ArrayBuffer) {
2064
- let { deserializeBinaryFilesystem: e } = Y(), r = e(t);
2064
+ let { deserializeBinaryFilesystem: e } = X(), r = e(t);
2065
2065
  t = r.filesystem, n = Object.assign({}, r.permissions, n);
2066
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)) {
2067
- let { deserializeBinaryFilesystem: e } = Y(), r = e(t);
2067
+ let { deserializeBinaryFilesystem: e } = X(), r = e(t);
2068
2068
  t = r.filesystem, n = Object.assign({}, r.permissions, n);
2069
2069
  }
2070
2070
  a(t), this.fileSystem = t, this.permissions = Object.assign({}, n), this.cwd = e, this._onChange = r, this._changeScheduled = !1;
2071
2071
  }
2072
2072
  toBinaryContainer(e = {}) {
2073
- let { serializeBinaryFilesystem: t } = Y();
2073
+ let { serializeBinaryFilesystem: t } = X();
2074
2074
  return t(this.fileSystem, this.permissions, e);
2075
2075
  }
2076
2076
  static fromBinaryContainer(e, t = {}) {
2077
- let { deserializeBinaryFilesystem: n } = Y();
2077
+ let { deserializeBinaryFilesystem: n } = X();
2078
2078
  return n(e, t);
2079
2079
  }
2080
2080
  getFileByPath(e) {
@@ -2252,10 +2252,10 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
2252
2252
  return null;
2253
2253
  }
2254
2254
  }, t.exports.BinaryFile = r, t.exports.SymlinkRef = i;
2255
- })), ne = /* @__PURE__ */ t(((e, t) => {
2255
+ })), te = /* @__PURE__ */ t(((e, t) => {
2256
2256
  t.exports = null;
2257
- })), re = /* @__PURE__ */ t(((e, t) => {
2258
- ee();
2257
+ })), ne = /* @__PURE__ */ t(((e, t) => {
2258
+ J();
2259
2259
  function n(e) {
2260
2260
  if (typeof e != "string") throw TypeError("Path must be a string. Received " + JSON.stringify(e));
2261
2261
  }
@@ -2422,19 +2422,76 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
2422
2422
  posix: null
2423
2423
  };
2424
2424
  a.posix = a, t.exports = a;
2425
- })), X = /* @__PURE__ */ t(((e, t) => {
2425
+ })), Z = /* @__PURE__ */ t(((e, t) => {
2426
2426
  b();
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 {
2427
+ var n = D(), { BinaryFile: r } = ee(), 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 {
2428
2428
  get name() {
2429
2429
  return "wasm";
2430
2430
  }
2431
2431
  get requiresFilesystem() {
2432
2432
  return !0;
2433
2433
  }
2434
+ get interactive() {
2435
+ if ((this.arguments || []).some((e) => e === "--help" || e === "-h" || e === "--version" || e === "-V" || e === "-v")) return !1;
2436
+ if (this._isInteractive !== void 0) return !!this._isInteractive;
2437
+ if (this._pkgMeta && this._pkgMeta.interactive !== void 0) return !!this._pkgMeta.interactive;
2438
+ let e = (this.script || this.name || "").toLowerCase();
2439
+ return e === "nano" || e.endsWith("/nano") || e.endsWith("nano.wasm");
2440
+ }
2441
+ get captureInput() {
2442
+ return this.interactive;
2443
+ }
2444
+ _enterAltScreen() {
2445
+ this._inAltScreen || (this._inAltScreen = !0, this.stdinStream && typeof this.stdinStream.setRawMode == "function" && this.stdinStream.setRawMode(!0), this.stdout && typeof this.stdout.write == "function" && this.stdout.write("\x1B[?1049h\x1B[2J\x1B[H"));
2446
+ }
2447
+ _exitAltScreen() {
2448
+ this._inAltScreen && (this._inAltScreen = !1, this.stdinStream && typeof this.stdinStream.setRawMode == "function" && this.stdinStream.setRawMode(!1), this.stdout && typeof this.stdout.write == "function" && this.stdout.write("\x1B[?1049l\x1B[?25h"));
2449
+ }
2450
+ _keyToBytes(e) {
2451
+ if (!e) return null;
2452
+ if (typeof e != "string") return e instanceof Uint8Array ? e : new Uint8Array([Number(e) & 255]);
2453
+ let t = e.match(/^\^([A-Za-z@\[\\\]^_?])$/);
2454
+ if (t) {
2455
+ let e = t[1].toUpperCase();
2456
+ return e === "?" ? new Uint8Array([127]) : new Uint8Array([e.charCodeAt(0) - 64]);
2457
+ }
2458
+ let n = {
2459
+ Enter: "\r",
2460
+ Return: "\r",
2461
+ Backspace: "",
2462
+ Tab: " ",
2463
+ Escape: "\x1B",
2464
+ ArrowUp: "\x1B[A",
2465
+ ArrowDown: "\x1B[B",
2466
+ ArrowRight: "\x1B[C",
2467
+ ArrowLeft: "\x1B[D",
2468
+ Home: "\x1B[H",
2469
+ End: "\x1B[F",
2470
+ PageUp: "\x1B[5~",
2471
+ PageDown: "\x1B[6~",
2472
+ Delete: "\x1B[3~",
2473
+ Insert: "\x1B[2~"
2474
+ };
2475
+ return n[e] ? new TextEncoder().encode(n[e]) : new TextEncoder().encode(e);
2476
+ }
2477
+ onInput(e) {
2478
+ if (!this._handlingStreamData) {
2479
+ if (typeof this._externalKeyHandler == "function") {
2480
+ this._externalKeyHandler(e);
2481
+ return;
2482
+ }
2483
+ this.stdinStream && this.stdinStream.isRaw;
2484
+ }
2485
+ }
2486
+ runCommand() {
2487
+ return super.runCommand().finally(() => {
2488
+ this._inAltScreen && this._exitAltScreen();
2489
+ });
2490
+ }
2434
2491
  run() {
2435
2492
  let e = this.script, t = (() => {
2436
2493
  try {
2437
- return ne();
2494
+ return te();
2438
2495
  } catch {
2439
2496
  return null;
2440
2497
  }
@@ -2502,29 +2559,61 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
2502
2559
  let t = {
2503
2560
  1: "",
2504
2561
  2: ""
2505
- }, n = new TextDecoder(), b = new TextEncoder(), C = null, w = (e, n) => {
2562
+ }, n = new TextDecoder(), b = new TextEncoder(), C = null, w = null, T = !1, E = 0, D = null, O = null, k = null, A = null, N = !!(this.interactive && (this.stdin === void 0 || this.stdin === null || this.stdin === this.stdinStream)), P = (e, n) => {
2563
+ if (N && (e === 1 || e === 2)) {
2564
+ if (e === 1 && this.stdout) {
2565
+ this.stdout.write(n);
2566
+ return;
2567
+ }
2568
+ if (e === 2 && this.stderr) {
2569
+ this.stderr.write(n);
2570
+ return;
2571
+ }
2572
+ }
2506
2573
  t[e] = (t[e] || "") + n;
2507
2574
  let r = t[e].split("\n");
2508
2575
  t[e] = r.pop();
2509
2576
  for (let t of r) e === 1 ? this.stdOut = t : this.stdErr = t;
2510
- }, T = () => {
2511
- t[1] &&= (this.stdOut = t[1], ""), t[2] &&= (this.stdErr = t[2], "");
2512
- }, E = typeof this.stdin == "string" ? b.encode(this.stdin) : this.stdin instanceof Uint8Array ? this.stdin : y !== void 0 && y.isBuffer(this.stdin) ? new Uint8Array(this.stdin) : new Uint8Array(), D = 0, O = this.context && this.context.fs ? this.context.fs : this.fs, k = 5, A = new Map([[3, {
2577
+ }, F = () => {
2578
+ t[1] &&= (N && this.stdout ? this.stdout.write(t[1]) : this.stdOut = t[1], ""), t[2] &&= (N && this.stderr ? this.stderr.write(t[2]) : this.stdErr = t[2], "");
2579
+ }, I = [], L = (e) => {
2580
+ if (!(!e || e.length === 0)) {
2581
+ for (let t = 0; t < e.length; t++) I.push(e[t]);
2582
+ if (O) {
2583
+ let e = O;
2584
+ O = null, e();
2585
+ }
2586
+ }
2587
+ };
2588
+ A = (e) => {
2589
+ this._handlingStreamData = !0;
2590
+ try {
2591
+ L(typeof e == "string" ? b.encode(e) : e instanceof Uint8Array ? e : y !== void 0 && y.isBuffer(e) ? new Uint8Array(e) : b.encode(String(e ?? "")));
2592
+ } finally {
2593
+ this._handlingStreamData = !1;
2594
+ }
2595
+ }, this._externalKeyHandler = (e) => {
2596
+ if (this._handlingStreamData) return;
2597
+ let t = this._keyToBytes(e);
2598
+ t && L(t);
2599
+ }, N && this.stdinStream && typeof this.stdinStream.on == "function" && this.stdinStream.on("data", A);
2600
+ let R = !N && typeof this.stdin == "string" ? b.encode(this.stdin) : !N && this.stdin instanceof Uint8Array ? this.stdin : !N && y !== void 0 && y.isBuffer(this.stdin) ? new Uint8Array(this.stdin) : new Uint8Array(), z = 0, B = this.context && this.context.fs ? this.context.fs : this.fs, V = 5, H = new Map([[3, {
2513
2601
  type: "preopen",
2514
2602
  path: "/",
2515
2603
  guestName: "/"
2516
2604
  }], [4, {
2517
2605
  type: "preopen",
2518
- path: O && O.cwd || "/",
2606
+ path: B && B.cwd || "/",
2519
2607
  guestName: "."
2520
- }]]), N = (e, t) => {
2521
- let n = A.get(e);
2608
+ }]]), U = (e, t) => {
2609
+ let n = H.get(e);
2522
2610
  if (!n || n.type !== "preopen" && n.type !== "dir") return null;
2523
- if (t.startsWith("/")) return O.absolutePath(t);
2611
+ if (t.startsWith("/")) return B.absolutePath(t);
2612
+ if (!t || t === ".") return B.absolutePath(n.path);
2524
2613
  let r = n.path.endsWith("/") ? n.path : n.path + "/";
2525
- return O.absolutePath(r + t);
2526
- }, P = (e) => {
2527
- if (!(!e || e.type !== "file" || !e.dirty || !O)) try {
2614
+ return B.absolutePath(r + t);
2615
+ }, W = (e) => {
2616
+ if (!(!e || e.type !== "file" || !e.dirty || !B)) try {
2528
2617
  let t = !0;
2529
2618
  for (let n = 0; n < e.bytes.length; n++) if (e.bytes[n] === 0) {
2530
2619
  t = !1;
@@ -2532,14 +2621,20 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
2532
2621
  }
2533
2622
  if (t) {
2534
2623
  let t = n.decode(e.bytes);
2535
- O.setFileByPath(e.path, t, !0);
2624
+ B.setFileByPath(e.path, t, !0);
2536
2625
  } else {
2537
2626
  let t = new Uint8Array(e.bytes.length);
2538
- t.set(e.bytes), O.setFileByPath(e.path, new r(t.buffer, "application/octet-stream"), !0);
2627
+ t.set(e.bytes), B.setFileByPath(e.path, new r(t.buffer, "application/octet-stream"), !0);
2539
2628
  }
2540
2629
  e.dirty = !1;
2541
2630
  } catch {}
2542
- }, F = Object.entries(this.context && this.context.vars || {}).map(([e, t]) => `${e}=${t}`), I = new Proxy({
2631
+ }, G = {
2632
+ TERM: "xterm-256color",
2633
+ COLORTERM: "truecolor",
2634
+ COLUMNS: String(this.stdout && this.stdout.columns || this.context && typeof this.context.getVar == "function" && this.context.getVar("COLUMNS") || 80),
2635
+ LINES: String(this.stdout && this.stdout.rows || this.context && typeof this.context.getVar == "function" && this.context.getVar("LINES") || 24),
2636
+ ...this.context && this.context.vars || {}
2637
+ }, K = Object.entries(G).map(([e, t]) => `${e}=${t}`), q = new Proxy({
2543
2638
  fd_write: (e, t, r, o) => {
2544
2639
  if (!C) return a;
2545
2640
  let s = new DataView(C.buffer);
@@ -2547,11 +2642,11 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
2547
2642
  let a = 0;
2548
2643
  for (let i = 0; i < r; i++) {
2549
2644
  let r = s.getUint32(t + i * 8, !0), o = s.getUint32(t + i * 8 + 4, !0);
2550
- o > 0 && w(e, n.decode(new Uint8Array(C.buffer, r, o))), a += o;
2645
+ o > 0 && P(e, n.decode(new Uint8Array(C.buffer, r, o))), a += o;
2551
2646
  }
2552
2647
  return s.setUint32(o, a, !0), i;
2553
2648
  }
2554
- let c = A.get(e);
2649
+ let c = H.get(e);
2555
2650
  if (!c || c.type !== "file") return a;
2556
2651
  let l = 0;
2557
2652
  for (let e = 0; e < r; e++) {
@@ -2571,14 +2666,36 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
2571
2666
  if (!C) return a;
2572
2667
  let o = new DataView(C.buffer);
2573
2668
  if (e === 0) {
2669
+ if (N && T) {
2670
+ if ((typeof w?.exports?.asyncify_get_state == "function" ? w.exports.asyncify_get_state() : 0) === 2) {
2671
+ w.exports.asyncify_stop_rewind();
2672
+ let e = 0;
2673
+ for (let r = 0; r < n && I.length > 0; r++) {
2674
+ let n = o.getUint32(t + r * 8, !0), i = o.getUint32(t + r * 8 + 4, !0), a = Math.min(i, I.length), s = I.splice(0, a);
2675
+ new Uint8Array(C.buffer).set(s, n), e += a;
2676
+ }
2677
+ return o.setUint32(r, e, !0), i;
2678
+ }
2679
+ if (I.length > 0) {
2680
+ let e = 0;
2681
+ for (let r = 0; r < n && I.length > 0; r++) {
2682
+ let n = o.getUint32(t + r * 8, !0), i = o.getUint32(t + r * 8 + 4, !0), a = Math.min(i, I.length), s = I.splice(0, a);
2683
+ new Uint8Array(C.buffer).set(s, n), e += a;
2684
+ }
2685
+ return o.setUint32(r, e, !0), i;
2686
+ }
2687
+ return w.exports.asyncify_start_unwind(E), D = new Promise((e) => {
2688
+ O = e;
2689
+ }), o.setUint32(r, 0, !0), i;
2690
+ }
2574
2691
  let e = 0;
2575
- for (let r = 0; r < n && D < E.length; r++) {
2576
- let n = o.getUint32(t + r * 8, !0), i = o.getUint32(t + r * 8 + 4, !0), a = Math.min(i, E.length - D);
2577
- new Uint8Array(C.buffer).set(E.slice(D, D + a), n), D += a, e += a;
2692
+ for (let r = 0; r < n && z < R.length; r++) {
2693
+ let n = o.getUint32(t + r * 8, !0), i = o.getUint32(t + r * 8 + 4, !0), a = Math.min(i, R.length - z);
2694
+ new Uint8Array(C.buffer).set(R.slice(z, z + a), n), z += a, e += a;
2578
2695
  }
2579
2696
  return o.setUint32(r, e, !0), i;
2580
2697
  }
2581
- let s = A.get(e);
2698
+ let s = H.get(e);
2582
2699
  if (!s || s.type !== "file") return a;
2583
2700
  let c = 0;
2584
2701
  for (let e = 0; e < n && s.pos < s.bytes.length; e++) {
@@ -2590,7 +2707,7 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
2590
2707
  fd_seek: (e, t, n, r, o) => {
2591
2708
  if (!C) return a;
2592
2709
  if (e <= 2) return d;
2593
- let c = A.get(e);
2710
+ let c = H.get(e);
2594
2711
  if (!c || c.type !== "file") return a;
2595
2712
  let l, u, f;
2596
2713
  typeof t == "bigint" ? (l = Number(t), u = n, f = r) : (l = t, u = r, f = o);
@@ -2603,32 +2720,32 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
2603
2720
  },
2604
2721
  fd_tell: (e, t) => {
2605
2722
  if (!C) return a;
2606
- let n = A.get(e);
2723
+ let n = H.get(e);
2607
2724
  return !n || n.type !== "file" ? a : (new DataView(C.buffer).setBigUint64(t, BigInt(n.pos), !0), i);
2608
2725
  },
2609
2726
  fd_close: (e) => {
2610
2727
  if (e <= 2) return i;
2611
- let t = A.get(e);
2612
- return t ? (P(t), A.delete(e), i) : a;
2728
+ let t = H.get(e);
2729
+ return t ? (W(t), H.delete(e), i) : a;
2613
2730
  },
2614
2731
  fd_sync: (e) => {
2615
- let t = A.get(e);
2616
- return t && P(t), i;
2732
+ let t = H.get(e);
2733
+ return t && W(t), i;
2617
2734
  },
2618
2735
  fd_datasync: (e) => {
2619
- let t = A.get(e);
2620
- return t && P(t), i;
2736
+ let t = H.get(e);
2737
+ return t && W(t), i;
2621
2738
  },
2622
2739
  fd_prestat_get: (e, t) => {
2623
2740
  if (!C) return a;
2624
- let n = A.get(e);
2741
+ let n = H.get(e);
2625
2742
  if (!n || n.type !== "preopen") return a;
2626
2743
  let r = new DataView(C.buffer), o = b.encode(n.guestName);
2627
2744
  return r.setUint8(t, 0), r.setUint32(t + 4, o.length, !0), i;
2628
2745
  },
2629
2746
  fd_prestat_dir_name: (e, t, n) => {
2630
2747
  if (!C) return a;
2631
- let r = A.get(e);
2748
+ let r = H.get(e);
2632
2749
  if (!r || r.type !== "preopen") return a;
2633
2750
  let o = b.encode(r.guestName);
2634
2751
  return new Uint8Array(C.buffer, t, Math.min(n, o.length)).set(o.subarray(0, n)), i;
@@ -2638,7 +2755,7 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
2638
2755
  let n = new DataView(C.buffer), r = f;
2639
2756
  if (e <= 2) r = p;
2640
2757
  else {
2641
- let t = A.get(e);
2758
+ let t = H.get(e);
2642
2759
  if (!t) return a;
2643
2760
  r = t.type === "preopen" || t.type === "dir" ? m : h;
2644
2761
  }
@@ -2651,22 +2768,22 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
2651
2768
  let n = new DataView(C.buffer);
2652
2769
  for (let e = 0; e < 64; e++) n.setUint8(t + e, 0);
2653
2770
  if (e <= 2) return n.setUint8(t + 16, p), i;
2654
- let r = A.get(e);
2771
+ let r = H.get(e);
2655
2772
  if (!r) return a;
2656
2773
  let o = r.type === "preopen" || r.type === "dir";
2657
2774
  return n.setUint8(t + 16, o ? m : h), n.setBigUint64(t + 24, 1n, !0), n.setBigUint64(t + 32, BigInt(r.bytes ? r.bytes.length : 0), !0), i;
2658
2775
  },
2659
2776
  path_open: (e, t, r, s, d, f, p, m, h) => {
2660
- if (!C || !O) return a;
2661
- let y = N(e, n.decode(new Uint8Array(C.buffer, r, s)));
2777
+ if (!C || !B) return a;
2778
+ let y = U(e, n.decode(new Uint8Array(C.buffer, r, s)));
2662
2779
  if (!y) return a;
2663
- let w = O.stat(y), T = w !== null;
2780
+ let w = B.stat(y), T = w !== null;
2664
2781
  if (d & v && d & g && T) return o;
2665
2782
  if (d & _) {
2666
2783
  if (!T) return l;
2667
2784
  if (!w.isDirectory) return u;
2668
- let e = k++;
2669
- return A.set(e, {
2785
+ let e = V++;
2786
+ return H.set(e, {
2670
2787
  type: "dir",
2671
2788
  path: y
2672
2789
  }), new DataView(C.buffer).setUint32(h, e, !0), i;
@@ -2675,55 +2792,68 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
2675
2792
  if (!T && !(d & g)) return l;
2676
2793
  let E = new Uint8Array(), D = !1;
2677
2794
  if (T && !(d & x)) {
2678
- let e = O.getFileByPath(y);
2795
+ let e = B.getFileByPath(y);
2679
2796
  e != null && (E = typeof e == "string" ? b.encode(e) : new Uint8Array(this._contentToBytes(e)));
2680
2797
  } else D = !0;
2681
- let j = k++;
2682
- return A.set(j, {
2798
+ let O = V++;
2799
+ return H.set(O, {
2683
2800
  type: "file",
2684
2801
  path: y,
2685
2802
  bytes: E,
2686
2803
  pos: m & S ? E.length : 0,
2687
2804
  append: !!(m & S),
2688
2805
  dirty: D
2689
- }), new DataView(C.buffer).setUint32(h, j, !0), i;
2806
+ }), new DataView(C.buffer).setUint32(h, O, !0), i;
2690
2807
  },
2691
2808
  path_filestat_get: (e, t, r, o, s) => {
2692
- if (!C || !O) return a;
2693
- let c = N(e, n.decode(new Uint8Array(C.buffer, r, o)));
2809
+ if (!C || !B) return a;
2810
+ let c = U(e, n.decode(new Uint8Array(C.buffer, r, o)));
2694
2811
  if (!c) return a;
2695
- let u = O.stat(c);
2812
+ let u = B.stat(c);
2696
2813
  if (!u) return l;
2697
2814
  let d = new DataView(C.buffer);
2698
2815
  for (let e = 0; e < 64; e++) d.setUint8(s + e, 0);
2699
- return d.setUint8(s + 16, u.isDirectory ? m : h), d.setBigUint64(s + 24, 1n, !0), d.setBigUint64(s + 32, BigInt(O.getSize(c) || 0), !0), i;
2816
+ return d.setUint8(s + 16, u.isDirectory ? m : h), d.setBigUint64(s + 24, 1n, !0), d.setBigUint64(s + 32, BigInt(B.getSize(c) || 0), !0), i;
2700
2817
  },
2701
2818
  path_create_directory: (e, t, r) => {
2702
- if (!C || !O) return a;
2703
- let o = N(e, n.decode(new Uint8Array(C.buffer, t, r)));
2819
+ if (!C || !B) return a;
2820
+ let o = U(e, n.decode(new Uint8Array(C.buffer, t, r)));
2704
2821
  if (!o) return a;
2705
2822
  try {
2706
- return O.setFileByPath(o, {}, !0), i;
2823
+ return B.setFileByPath(o, {}, !0), i;
2707
2824
  } catch {
2708
2825
  return s;
2709
2826
  }
2710
2827
  },
2711
2828
  path_unlink_file: (e, t, r) => {
2712
- if (!C || !O) return a;
2713
- let o = N(e, n.decode(new Uint8Array(C.buffer, t, r)));
2714
- if (!o || !O.exists(o)) return l;
2829
+ if (!C || !B) return a;
2830
+ let o = U(e, n.decode(new Uint8Array(C.buffer, t, r)));
2831
+ if (!o || !B.exists(o)) return l;
2715
2832
  try {
2716
- return O.deleteFileByPath(o), i;
2833
+ return B.deleteFileByPath(o), i;
2717
2834
  } catch {
2718
2835
  return s;
2719
2836
  }
2720
2837
  },
2721
2838
  path_remove_directory: (e, t, r) => {
2722
- if (!C || !O) return a;
2723
- let o = N(e, n.decode(new Uint8Array(C.buffer, t, r)));
2724
- if (!o || !O.exists(o)) return l;
2839
+ if (!C || !B) return a;
2840
+ let o = U(e, n.decode(new Uint8Array(C.buffer, t, r)));
2841
+ if (!o || !B.exists(o)) return l;
2725
2842
  try {
2726
- return O.deleteFileByPath(o), i;
2843
+ return B.deleteFileByPath(o), i;
2844
+ } catch {
2845
+ return s;
2846
+ }
2847
+ },
2848
+ path_rename: (e, t, r, o, c, u) => {
2849
+ if (!C || !B) return a;
2850
+ let d = n.decode(new Uint8Array(C.buffer, t, r)), f = n.decode(new Uint8Array(C.buffer, c, u)), p = U(e, d), m = U(o, f);
2851
+ if (!p || !m) return a;
2852
+ if (!B.exists(p)) return l;
2853
+ for (let e of H.values()) e.path === p && (W(e), e.path = m);
2854
+ try {
2855
+ let e = B.getFileByPath(p);
2856
+ return B.setFileByPath(m, e, !0), B.deleteFileByPath(p), i;
2727
2857
  } catch {
2728
2858
  return s;
2729
2859
  }
@@ -2752,16 +2882,16 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
2752
2882
  environ_sizes_get: (e, t) => {
2753
2883
  if (!C) return s;
2754
2884
  let n = new DataView(C.buffer);
2755
- n.setUint32(e, F.length, !0);
2756
- let r = F.reduce((e, t) => e + b.encode(t).length + 1, 0);
2885
+ n.setUint32(e, K.length, !0);
2886
+ let r = K.reduce((e, t) => e + b.encode(t).length + 1, 0);
2757
2887
  return n.setUint32(t, r, !0), i;
2758
2888
  },
2759
2889
  environ_get: (e, t) => {
2760
2890
  if (!C) return s;
2761
2891
  let n = new DataView(C.buffer), r = t;
2762
- for (let t = 0; t < F.length; t++) {
2892
+ for (let t = 0; t < K.length; t++) {
2763
2893
  n.setUint32(e + t * 4, r, !0);
2764
- let i = b.encode(F[t] + "\0");
2894
+ let i = b.encode(K[t] + "\0");
2765
2895
  new Uint8Array(C.buffer).set(i, r), r += i.length;
2766
2896
  }
2767
2897
  return i;
@@ -2782,7 +2912,7 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
2782
2912
  }
2783
2913
  }, { get(e, t) {
2784
2914
  return t in e ? e[t] : () => 0;
2785
- } }), L = (e) => {
2915
+ } }), J = (e) => {
2786
2916
  if (!C || !e) return "";
2787
2917
  try {
2788
2918
  let t = new DataView(C.buffer).getUint32(e - 4, !0), n = new Uint16Array(C.buffer, e, t >>> 1);
@@ -2790,33 +2920,33 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
2790
2920
  } catch {
2791
2921
  return "";
2792
2922
  }
2793
- }, R = {
2794
- wasi_snapshot_preview1: I,
2795
- wasi_unstable: I,
2923
+ }, Y = {
2924
+ wasi_snapshot_preview1: q,
2925
+ wasi_unstable: q,
2796
2926
  env: new Proxy({
2797
2927
  print: (e, t) => {
2798
- C && w(1, n.decode(new Uint8Array(C.buffer, e, t)));
2928
+ C && P(1, n.decode(new Uint8Array(C.buffer, e, t)));
2799
2929
  },
2800
2930
  eprint: (e, t) => {
2801
- C && w(2, n.decode(new Uint8Array(C.buffer, e, t)));
2931
+ C && P(2, n.decode(new Uint8Array(C.buffer, e, t)));
2802
2932
  },
2803
2933
  log: (e) => {
2804
- w(1, L(e) + "\n");
2934
+ P(1, J(e) + "\n");
2805
2935
  },
2806
2936
  logi: (e) => {
2807
- w(1, String(e) + "\n");
2937
+ P(1, String(e) + "\n");
2808
2938
  },
2809
2939
  logf: (e) => {
2810
- w(1, String(e) + "\n");
2940
+ P(1, String(e) + "\n");
2811
2941
  },
2812
2942
  trace: (e, t, ...n) => {
2813
2943
  let r = t > 0 ? " " + n.slice(0, t).join(", ") : "";
2814
- w(1, `trace: ${L(e)}${r}\n`);
2944
+ P(1, `trace: ${J(e)}${r}\n`);
2815
2945
  },
2816
2946
  seed: () => Date.now() * Math.random(),
2817
2947
  abort: (e, t, n, r) => {
2818
- let i = L(e), a = L(t);
2819
- throw (i || a) && w(2, `abort: ${i || "aborted"} at ${a || this.script}:${n || 0}:${r || 0}\n`), this.exitCode = 134, {
2948
+ let i = J(e), a = J(t);
2949
+ throw (i || a) && P(2, `abort: ${i || "aborted"} at ${a || this.script}:${n || 0}:${r || 0}\n`), this.exitCode = 134, {
2820
2950
  _wasmExit: !0,
2821
2951
  code: 134
2822
2952
  };
@@ -2832,44 +2962,66 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
2832
2962
  initial: 1,
2833
2963
  maximum: 256
2834
2964
  });
2835
- C = t, R[e.module] || (R[e.module] = {}), R[e.module][e.name] = t;
2965
+ C = t, Y[e.module] || (Y[e.module] = {}), Y[e.module][e.name] = t;
2966
+ }
2967
+ if (w = await WebAssembly.instantiate(t, Y), w.exports.memory && (C = w.exports.memory), T = !!(w.exports.asyncify_start_unwind && w.exports.asyncify_stop_unwind), T && C) {
2968
+ try {
2969
+ E = C.grow(1) * 65536;
2970
+ } catch {
2971
+ E = 16;
2972
+ }
2973
+ let e = E + 8, t = E + 65536 - 8;
2974
+ new Int32Array(C.buffer, E, 2).set([e, t]);
2836
2975
  }
2837
- let n = await WebAssembly.instantiate(t, R);
2838
- n.exports.memory && (C = n.exports.memory);
2839
- let r = n.exports._start || n.exports.__wasi_command || n.exports.main || n.exports.run;
2840
- if (r) try {
2841
- await r();
2976
+ if (N) {
2977
+ this._enterAltScreen();
2978
+ let e = this.stdout && this.stdout.columns || this.context && typeof this.context.getVar == "function" && parseInt(this.context.getVar("COLUMNS"), 10) || 80, t = this.stdout && this.stdout.rows || this.context && typeof this.context.getVar == "function" && parseInt(this.context.getVar("LINES"), 10) || 24;
2979
+ if (typeof w.exports.set_terminal_size == "function") try {
2980
+ w.exports.set_terminal_size(e, t);
2981
+ } catch {}
2982
+ k = () => {
2983
+ let e = this.stdout && this.stdout.columns || 80, t = this.stdout && this.stdout.rows || 24;
2984
+ if (typeof w?.exports?.set_terminal_size == "function") try {
2985
+ w.exports.set_terminal_size(e, t);
2986
+ } catch {}
2987
+ }, this.stdout && typeof this.stdout.on == "function" && this.stdout.on("resize", k), this.stdinStream && typeof this.stdinStream.on == "function" && this.stdinStream.on("resize", k);
2988
+ }
2989
+ let n = w.exports._start || w.exports.__wasi_command || w.exports.main || w.exports.run;
2990
+ if (n) try {
2991
+ let e = n();
2992
+ for (e && typeof e.then == "function" && (e = await e); T && typeof w.exports.asyncify_get_state == "function" && w.exports.asyncify_get_state() === 1;) w.exports.asyncify_stop_unwind(), D &&= (await D, null), w.exports.asyncify_start_rewind(E), e = n(), e && typeof e.then == "function" && (e = await e);
2842
2993
  } catch (e) {
2843
2994
  (!e || !e._wasmExit) && (this.stdErr = `${this.script}: runtime error: ${e?.message || String(e)}`, this.exitCode = 1);
2844
2995
  }
2845
2996
  else {
2846
- let e = Object.entries(n.exports).filter(([, e]) => typeof e == "function").map(([e]) => e), t = e.length > 0 ? ` (module is a library exporting: ${e.join(", ")})` : "";
2997
+ let e = Object.entries(w.exports).filter(([, e]) => typeof e == "function").map(([e]) => e), t = e.length > 0 ? ` (module is a library exporting: ${e.join(", ")})` : "";
2847
2998
  this.stdErr = `${this.script}: no CLI entry point found (expected _start, main, or run)${t}`, this.exitCode = 1;
2848
2999
  }
2849
3000
  } catch (e) {
2850
3001
  (!e || !e._wasmExit) && (this.stdErr = `${this.script}: ${e?.message || String(e)}`, this.exitCode = 1);
2851
3002
  } finally {
2852
- for (let e of A.values()) P(e);
3003
+ N && this._exitAltScreen(), N && A && this.stdinStream && typeof this.stdinStream.off == "function" && this.stdinStream.off("data", A), k && (this.stdout && typeof this.stdout.off == "function" && this.stdout.off("resize", k), this.stdinStream && typeof this.stdinStream.off == "function" && this.stdinStream.off("resize", k)), this._externalKeyHandler = null;
3004
+ for (let e of H.values()) W(e);
2853
3005
  }
2854
- T();
2855
- let z = this.arguments || [];
3006
+ F();
3007
+ let X = this.arguments || [];
2856
3008
  if (!(this.result && this.result.getStdOutput && this.result.getStdOutput().length > 0) && (this.exitCode === 0 || this.exitCode === void 0)) {
2857
3009
  let t = (this.script || "wasm").split("/").pop(), n = Object.assign({
2858
3010
  name: t,
2859
3011
  version: "wasi",
2860
3012
  description: `WebAssembly WASI binary (${this.script})`
2861
3013
  }, this._pkgMeta || {});
2862
- if (z.includes("--help") || z.includes("-h")) {
3014
+ if (X.includes("--help") || X.includes("-h")) {
2863
3015
  let r = j(e);
2864
3016
  for (let e of M(t, n, r)) this.stdOut = e;
2865
- } else (z.includes("--version") || z.includes("-V") || z.includes("-v")) && (this.stdOut = `${t} (${n.name} ${n.version} wasm)`);
3017
+ } else (X.includes("--version") || X.includes("-V") || X.includes("-v")) && (this.stdOut = `${t} (${n.name} ${n.version} wasm)`);
2866
3018
  }
2867
3019
  }
2868
3020
  }, w = /* @__PURE__ */ new Map();
2869
3021
  function T(e, t) {
2870
3022
  if (!e || !e.exists(t)) return null;
2871
3023
  if (e.isRealFilesystem) try {
2872
- let n = ne();
3024
+ let n = te();
2873
3025
  return new Uint8Array(n.readFileSync(e.absolutePath(t)));
2874
3026
  } catch {}
2875
3027
  let n = e.getFileByPath(t);
@@ -2898,7 +3050,7 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
2898
3050
  o.set(a);
2899
3051
  let s = new r(o.buffer, i);
2900
3052
  if (e.isRealFilesystem) try {
2901
- let n = ne(), r = re(), i = e.absolutePath(t);
3053
+ let n = te(), r = ne(), i = e.absolutePath(t);
2902
3054
  n.mkdirSync(r.dirname(i), { recursive: !0 }), n.writeFileSync(i, y.from(a.buffer, a.byteOffset, a.byteLength));
2903
3055
  return;
2904
3056
  } catch {
@@ -3271,8 +3423,8 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
3271
3423
  return a;
3272
3424
  }
3273
3425
  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;
3274
- })), Z = /* @__PURE__ */ t(((e, t) => {
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 {
3426
+ })), re = /* @__PURE__ */ t(((e, t) => {
3427
+ var n = Y(), { createGenericWasmCommand: r } = Z(), 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 {
3276
3428
  get requiresFilesystem() {
3277
3429
  return !0;
3278
3430
  }
@@ -3729,7 +3881,8 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
3729
3881
  cdnUrl: "https://cdn.jsdelivr.net/gh/eliotstocker/nano-wasm@main/dist/nano.wasm",
3730
3882
  wasmUrl: "https://cdn.jsdelivr.net/gh/eliotstocker/nano-wasm@main/dist/nano.wasm",
3731
3883
  type: "custom-wasm",
3732
- size: "574 KB"
3884
+ interactive: !0,
3885
+ size: "680 KB"
3733
3886
  }
3734
3887
  }, r = {
3735
3888
  sqlite: "sqlite3",
@@ -3796,7 +3949,7 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
3796
3949
  }
3797
3950
  return i;
3798
3951
  }
3799
- var { readBytesFromFs: a, writeBytesToFs: o, loadModule: s, extractHelpFromText: c, inspectWasmBytes: l, formatWasmHelp: u } = X();
3952
+ var { readBytesFromFs: a, writeBytesToFs: o, loadModule: s, extractHelpFromText: c, inspectWasmBytes: l, formatWasmHelp: u } = Z();
3800
3953
  t.exports = {
3801
3954
  formatTable: n,
3802
3955
  formatPipeList: r,
@@ -3810,7 +3963,7 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
3810
3963
  };
3811
3964
  })), oe = /* @__PURE__ */ t(((e, t) => {
3812
3965
  T();
3813
- var n = Z(), { readBytesFromFs: r, writeBytesToFs: i, loadModule: a } = ae(), o = {
3966
+ var n = re(), { readBytesFromFs: r, writeBytesToFs: i, loadModule: a } = ae(), o = {
3814
3967
  smol: "https://huggingface.co/bartowski/SmolLM2-135M-Instruct-GGUF/resolve/main/SmolLM2-135M-Instruct-Q4_K_M.gguf",
3815
3968
  smollm: "https://huggingface.co/bartowski/SmolLM2-135M-Instruct-GGUF/resolve/main/SmolLM2-135M-Instruct-Q4_K_M.gguf",
3816
3969
  smollm2: "https://huggingface.co/bartowski/SmolLM2-135M-Instruct-GGUF/resolve/main/SmolLM2-135M-Instruct-Q4_K_M.gguf",
@@ -3835,15 +3988,55 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
3835
3988
  let t = (e.file || e.url || s).split("/").pop().split("?")[0] || "model.gguf";
3836
3989
  return /\.gguf$/i.test(t) || (t += ".gguf"), `${l}/${t}`;
3837
3990
  }
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;
3991
+ function f(e) {
3992
+ 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`;
3993
+ }
3994
+ function p(e, t, n, r = 20) {
3995
+ let i = (e || "model.gguf").split("/").pop().split("?")[0] || e;
3840
3996
  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)})`;
3997
+ let e = Math.max(0, Math.min(1, t / n)), a = Math.round(e * 100), o = Math.round(e * r);
3998
+ return `\rPulling ${i}: [${"█".repeat(o) + "░".repeat(Math.max(0, r - o))}] ${String(a).padStart(3)}% (${f(t)} / ${f(n)})`;
3843
3999
  }
3844
- return `\rPulling ${a}: ${i(t)} downloaded...`;
4000
+ return `\rPulling ${i}: ${f(t)} downloaded...`;
3845
4001
  }
3846
- function p(e, t = null, n = null, r = null) {
4002
+ var m = class {
4003
+ constructor({ onRender: e = null, width: t = 20, intervalMs: n = 80, enabled: r = !0 } = {}) {
4004
+ 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;
4005
+ }
4006
+ start(e, { total: t = 0, loaded: n = 0, hint: r = "", determinate: i = null } = {}) {
4007
+ 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(() => {
4008
+ this.step++, this.render();
4009
+ }, this.intervalMs));
4010
+ }
4011
+ update({ loaded: e, total: t, hint: n } = {}) {
4012
+ !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());
4013
+ }
4014
+ formatLine() {
4015
+ let e = ((Date.now() - this.startTime) / 1e3).toFixed(1) + "s";
4016
+ if (this.isDeterminate && this.total > 0) {
4017
+ 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})` : "";
4018
+ return `${this.label}: [${i}] ${String(n).padStart(3)}% (${a}, ${e})${o}`;
4019
+ }
4020
+ 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})` : "";
4021
+ return `${this.label}: ${a} ${e}${o}`;
4022
+ }
4023
+ render() {
4024
+ !this.enabled || !this.active || typeof this.onRender != "function" || this.onRender(this.formatLine());
4025
+ }
4026
+ stopTimer() {
4027
+ this.timer !== null && (clearInterval(this.timer), this.timer = null);
4028
+ }
4029
+ finish(e = "Done") {
4030
+ if (!this.enabled || !this.active) return;
4031
+ this.stopTimer();
4032
+ 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}`;
4033
+ typeof this.onRender == "function" && this.onRender(i), this.active = !1;
4034
+ }
4035
+ clear() {
4036
+ this.stopTimer(), this.active = !1, this.enabled && typeof this.onRender == "function" && this.onRender("");
4037
+ }
4038
+ };
4039
+ function h(e, t = null, n = null, r = null) {
3847
4040
  if (t) {
3848
4041
  if (n) {
3849
4042
  let e = t.replace(/^hf:/i, "").replace(/^\/+|\/+$/g, ""), r = n.replace(/^\/+/, "");
@@ -3905,7 +4098,7 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
3905
4098
  path: i
3906
4099
  };
3907
4100
  }
3908
- async function m(e, t) {
4101
+ async function g(e, t) {
3909
4102
  let n = await fetch(e);
3910
4103
  if (!n.ok) throw Error(`Failed to fetch model from ${e}: HTTP ${n.status}`);
3911
4104
  let r = Number(n.headers.get("content-length") || 0);
@@ -3930,7 +4123,7 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
3930
4123
  let s = new Blob(a, { type: "application/octet-stream" });
3931
4124
  return s.name = e.split("/").pop().split("?")[0] || "model.gguf", s;
3932
4125
  }
3933
- async function h({ wllama: e, mod: t, modelSpec: n, downloadedBlob: r = null, targetPersistPath: a, fs: o, progressCallback: c }) {
4126
+ async function _({ wllama: e, mod: t, modelSpec: n, downloadedBlob: r = null, targetPersistPath: a, fs: o, progressCallback: c }) {
3934
4127
  if (!o || !a) return null;
3935
4128
  let l = null;
3936
4129
  if (r && typeof r.arrayBuffer == "function") l = new Uint8Array(await r.arrayBuffer());
@@ -3954,7 +4147,7 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
3954
4147
  e = r.url || r;
3955
4148
  } catch {}
3956
4149
  if (e && typeof fetch == "function") try {
3957
- let t = await m(e, c);
4150
+ let t = await g(e, c);
3958
4151
  l = new Uint8Array(await t.arrayBuffer());
3959
4152
  } catch {}
3960
4153
  }
@@ -3972,28 +4165,28 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
3972
4165
  size: l.byteLength
3973
4166
  };
3974
4167
  }
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;
4168
+ async function v(e = {}) {
4169
+ 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;
4170
+ typeof S == "function" && S("Initializing WASM runtime...");
4171
+ 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
4172
  if (f.wasmPath && l && typeof URL < "u" && typeof URL.createObjectURL == "function") {
3980
4173
  let e = r(l, f.wasmPath);
3981
- e && e.byteLength > 0 && (C = URL.createObjectURL(new Blob([e], { type: "application/wasm" })));
4174
+ e && e.byteLength > 0 && (E = URL.createObjectURL(new Blob([e], { type: "application/wasm" })));
3982
4175
  }
3983
- let w = {
3984
- default: C,
3985
- "single-thread/wllama.wasm": C,
3986
- "multi-thread/wllama.wasm": C
3987
- }, T = () => {
4176
+ let D = {
4177
+ default: E,
4178
+ "single-thread/wllama.wasm": E,
4179
+ "multi-thread/wllama.wasm": E
4180
+ }, O = () => {
3988
4181
  try {
3989
- return new x(w, {
4182
+ return new w(D, {
3990
4183
  suppressNativeLog: !0,
3991
- logger: S
4184
+ logger: T
3992
4185
  });
3993
4186
  } catch {
3994
- return new x(w, {
4187
+ return new w(D, {
3995
4188
  suppressNativeLog: !0,
3996
- logger: S,
4189
+ logger: T,
3997
4190
  cacheManager: {
3998
4191
  isSupported: () => !0,
3999
4192
  getNameFromURL: async (e) => e,
@@ -4004,135 +4197,181 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
4004
4197
  });
4005
4198
  }
4006
4199
  };
4007
- if (!g) return {
4008
- wllama: T(),
4009
- mod: b,
4010
- Wllama: x,
4200
+ if (!m) return {
4201
+ wllama: O(),
4202
+ mod: C,
4203
+ Wllama: w,
4011
4204
  modelKey: null,
4012
4205
  isCached: !1,
4013
4206
  persisted: null
4014
4207
  };
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);
4208
+ 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}`;
4209
+ if (u.has(j)) {
4210
+ let e = u.get(j);
4018
4211
  if (typeof e.isModelLoaded != "function" || e.isModelLoaded()) {
4019
- typeof y == "function" && y("Loaded model from cache.");
4212
+ typeof S == "function" && S("Loaded model from cache."), y && y.finish("Loaded model from cache");
4020
4213
  let n = null;
4021
- if ((_ || v) && l && E) if (l.exists(E)) {
4022
- let e = r(l, E);
4214
+ if ((h || v) && l && k) if (l.exists(k)) {
4215
+ let e = r(l, k);
4023
4216
  n = {
4024
- path: E,
4217
+ path: k,
4025
4218
  size: e ? e.byteLength : 0
4026
4219
  };
4027
- } else n = await h({
4220
+ } else n = await _({
4028
4221
  wllama: e,
4029
- mod: b,
4222
+ mod: C,
4030
4223
  modelSpec: t,
4031
- targetPersistPath: E,
4224
+ targetPersistPath: k,
4032
4225
  fs: l,
4033
4226
  progressCallback: o.progressCallback
4034
4227
  });
4035
4228
  return {
4036
4229
  wllama: e,
4037
- mod: b,
4038
- Wllama: x,
4039
- modelKey: O,
4230
+ mod: C,
4231
+ Wllama: w,
4232
+ modelKey: j,
4040
4233
  isCached: !0,
4041
4234
  persisted: n
4042
4235
  };
4043
4236
  }
4044
4237
  }
4045
- let k = T(), A = null, j = null;
4238
+ let M = O(), N = null, P = null, F = (e = "loading tensors") => ({
4239
+ ...o,
4240
+ progressCallback: ({ loaded: t, total: n }) => {
4241
+ y && y.update({
4242
+ loaded: t,
4243
+ total: n,
4244
+ hint: e
4245
+ }), typeof o.progressCallback == "function" && o.progressCallback({
4246
+ loaded: t,
4247
+ total: n
4248
+ });
4249
+ }
4250
+ });
4046
4251
  if (t.kind !== "fs" && l) {
4047
- let e = E && l.exists(E) ? E : D && l.exists(D) ? D : null;
4252
+ let e = k && l.exists(k) ? k : A && l.exists(A) ? A : null;
4048
4253
  if (e) {
4049
4254
  let t = r(l, e);
4050
4255
  if (t && t.byteLength > 0) {
4051
- typeof y == "function" && y(`Loading model from ${e}...`);
4256
+ typeof S == "function" && S(`Loading model from ${e}...`), y && y.start(`Loading model (${b}) weights`, {
4257
+ total: t.byteLength,
4258
+ hint: "parsing tensors & compiling WebGPU shaders"
4259
+ });
4052
4260
  let n = new Blob([t], { type: "application/octet-stream" });
4053
- n.name = e.split("/").pop() || "model.gguf", await k.loadModel([n], o), j = {
4261
+ n.name = e.split("/").pop() || "model.gguf", await M.loadModel([n], F()), P = {
4054
4262
  path: e,
4055
4263
  bytes: t
4056
- };
4264
+ }, y && y.finish("Model weights loaded");
4057
4265
  }
4058
4266
  }
4059
4267
  }
4060
- if (j) E && E !== j.path && i(l, E, j.bytes, "application/octet-stream");
4268
+ if (P) k && k !== P.path && i(l, k, P.bytes, "application/octet-stream");
4061
4269
  else if (t.kind === "fs") {
4062
- typeof y == "function" && y(`Loading model from ${t.path}...`);
4270
+ typeof S == "function" && S(`Loading model from ${t.path}...`), y && y.start(`Loading model (${b}) weights`, { hint: "reading file" });
4063
4271
  let e = r(l, t.path);
4064
4272
  if (!e) throw Error(`${t.path}: No such file or directory`);
4065
4273
  let a = new Blob([e], { type: "application/octet-stream" });
4066
4274
  a.name = t.path.split("/").pop() || "model.gguf";
4067
- let s = [a];
4275
+ let o = [a];
4068
4276
  if (n) {
4069
4277
  let e = r(l, n);
4070
4278
  if (!e) throw Error(`${n}: No such file or directory`);
4071
4279
  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})...`);
4280
+ t.name = n.split("/").pop() || "mmproj.gguf", o.push(t);
4281
+ }
4282
+ typeof S == "function" && S(`Loading model ${a.name} into memory...`), y && y.start(`Loading model (${b}) weights`, {
4283
+ total: e.byteLength,
4284
+ loaded: e.byteLength,
4285
+ hint: "parsing tensors & compiling WebGPU shaders"
4286
+ }), await M.loadModel(o, F()), y && y.finish("Model weights loaded"), k && k !== t.path && i(l, k, e, "application/octet-stream");
4287
+ } else if (t.kind === "hf-quant" && typeof M.loadModelFromHF == "function") {
4288
+ typeof S == "function" && S(`Pulling model ${t.repo} (${t.quant})...`), y && y.start(`Pulling ${b}`, { hint: "connecting to Hugging Face" });
4077
4289
  try {
4078
- await k.loadModelFromHF({
4290
+ await M.loadModelFromHF({
4079
4291
  repo: t.repo,
4080
4292
  quant: t.quant
4081
- }, o);
4293
+ }, F("downloading")), y && y.finish("Model weights loaded");
4082
4294
  } catch (e) {
4083
- if (typeof b.getHFModelSource == "function") {
4084
- let e = await b.getHFModelSource({
4295
+ if (typeof C.getHFModelSource == "function") {
4296
+ let e = await C.getHFModelSource({
4085
4297
  repo: t.repo,
4086
4298
  quant: t.quant
4087
4299
  });
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);
4300
+ typeof S == "function" && S(`Pulling model ${t.repo} (${t.quant})...`), y && y.start(`Pulling ${b}`, { hint: "downloading" }), N = await g(e.url || e, (e) => {
4301
+ y && y.update({
4302
+ loaded: e.loaded,
4303
+ total: e.total,
4304
+ hint: "downloading"
4305
+ }), typeof o.progressCallback == "function" && o.progressCallback(e);
4306
+ }), typeof S == "function" && S("Loading model into memory..."), y && y.start(`Loading model (${b}) weights`, {
4307
+ total: N.size,
4308
+ hint: "parsing tensors & compiling WebGPU shaders"
4309
+ }), await M.loadModel([N], F()), y && y.finish("Model weights loaded");
4089
4310
  } else throw e;
4090
4311
  }
4091
- } else if (t.kind === "hf-file" && typeof k.loadModelFromHF == "function") {
4092
- typeof y == "function" && y(`Pulling model ${t.repo}/${t.file}...`);
4312
+ } else if (t.kind === "hf-file" && typeof M.loadModelFromHF == "function") {
4313
+ typeof S == "function" && S(`Pulling model ${t.repo}/${t.file}...`), y && y.start(`Pulling ${b}`, { hint: "connecting to Hugging Face" });
4093
4314
  try {
4094
- await k.loadModelFromHF({
4315
+ await M.loadModelFromHF({
4095
4316
  repo: t.repo,
4096
4317
  file: t.file
4097
- }, o);
4318
+ }, F("downloading")), y && y.finish("Model weights loaded");
4098
4319
  } catch {
4099
- A = await m(t.url, o.progressCallback), typeof y == "function" && y("Loading model into memory..."), await k.loadModel([A], o);
4320
+ y && y.start(`Pulling ${b}`, { hint: "downloading" }), N = await g(t.url, (e) => {
4321
+ y && y.update({
4322
+ loaded: e.loaded,
4323
+ total: e.total,
4324
+ hint: "downloading"
4325
+ }), typeof o.progressCallback == "function" && o.progressCallback(e);
4326
+ }), typeof S == "function" && S("Loading model into memory..."), y && y.start(`Loading model (${b}) weights`, {
4327
+ total: N.size,
4328
+ hint: "parsing tensors & compiling WebGPU shaders"
4329
+ }), await M.loadModel([N], F()), y && y.finish("Model weights loaded");
4100
4330
  }
4101
4331
  } else {
4102
4332
  let e = t.url || s;
4103
- typeof y == "function" && y(`Pulling model from ${e.split("/").pop().split("?")[0] || e}...`);
4333
+ typeof S == "function" && S(`Pulling model from ${e.split("/").pop().split("?")[0] || e}...`), y && y.start(`Pulling ${b}`, { hint: "downloading" });
4104
4334
  try {
4105
- n && /^https?:\/\//i.test(n) ? await k.loadModelFromUrl({
4335
+ n && /^https?:\/\//i.test(n) ? await M.loadModelFromUrl({
4106
4336
  url: e,
4107
4337
  mmprojUrl: n
4108
- }, o) : await k.loadModelFromUrl(e, o);
4338
+ }, F("downloading")) : await M.loadModelFromUrl(e, F("downloading")), y && y.finish("Model weights loaded");
4109
4339
  } 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,
4340
+ N = await g(e, (e) => {
4341
+ y && y.update({
4342
+ loaded: e.loaded,
4343
+ total: e.total,
4344
+ hint: "downloading"
4345
+ }), typeof o.progressCallback == "function" && o.progressCallback(e);
4346
+ }), typeof S == "function" && S("Loading model into memory..."), y && y.start(`Loading model (${b}) weights`, {
4347
+ total: N.size,
4348
+ hint: "parsing tensors & compiling WebGPU shaders"
4349
+ }), await M.loadModel([N], F()), y && y.finish("Model weights loaded");
4350
+ }
4351
+ }
4352
+ typeof S == "function" && S("Model loaded.");
4353
+ let I = null;
4354
+ return P ? I = {
4355
+ path: k || P.path,
4356
+ size: P.bytes.byteLength
4357
+ } : (h || v) && l && k && t.kind !== "fs" && (I = await _({
4358
+ wllama: M,
4359
+ mod: C,
4121
4360
  modelSpec: t,
4122
- downloadedBlob: A,
4123
- targetPersistPath: E,
4361
+ downloadedBlob: N,
4362
+ targetPersistPath: k,
4124
4363
  fs: l,
4125
4364
  progressCallback: o.progressCallback
4126
- })), u.set(O, k), {
4127
- wllama: k,
4128
- mod: b,
4129
- Wllama: x,
4130
- modelKey: O,
4365
+ })), u.set(j, M), {
4366
+ wllama: M,
4367
+ mod: C,
4368
+ Wllama: w,
4369
+ modelKey: j,
4131
4370
  isCached: !1,
4132
- persisted: M
4371
+ persisted: I
4133
4372
  };
4134
4373
  }
4135
- function _(e) {
4374
+ function y(e) {
4136
4375
  if (typeof e == "string") return e;
4137
4376
  if (!e || typeof e != "object") return "";
4138
4377
  if (Array.isArray(e.choices) && e.choices.length > 0) {
@@ -4143,7 +4382,7 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
4143
4382
  }
4144
4383
  return typeof e.content == "string" ? e.content : typeof e.text == "string" ? e.text : "";
4145
4384
  }
4146
- function v(e = {}, t = {}, i = "wllama") {
4385
+ function b(e = {}, t = {}, i = "wllama") {
4147
4386
  class a extends n {
4148
4387
  static get name() {
4149
4388
  return i;
@@ -4158,19 +4397,19 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
4158
4397
  });
4159
4398
  }
4160
4399
  _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 = [];
4400
+ 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
4401
  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") {
4402
+ let h = e[o];
4403
+ if (h === "--help" || h === "-h" && o + 1 >= e.length || h === "-?") D = !0;
4404
+ else if (h === "--version" || h === "-V" || h === "-v") O = !0;
4405
+ else if (h === "--serve" || h === "--daemon") k = "serve";
4406
+ else if (h === "-q" || h === "--quiet" || h === "--no-progress") E = !1;
4407
+ else if (h === "--progress") E = !0;
4408
+ else if (h === "--persist" || h === "--save") {
4170
4409
  w = !0;
4171
4410
  let n = e[o + 1];
4172
4411
  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);
4412
+ } 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
4413
  }
4175
4414
  if (j.length > 0) {
4176
4415
  let e = j[0].toLowerCase();
@@ -4196,8 +4435,8 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
4196
4435
  let e = this.context.getVar("WLLAMA_MODEL");
4197
4436
  e && (t = e);
4198
4437
  }
4199
- let M = p(t, r, i, a), N = {
4200
- n_ctx: h,
4438
+ let M = h(t, r, i, a), N = {
4439
+ n_ctx: m,
4201
4440
  jinja: C
4202
4441
  };
4203
4442
  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 +4450,8 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
4211
4450
  maxTokens: u,
4212
4451
  temperature: d,
4213
4452
  topK: f,
4214
- topP: m,
4215
- nCtx: h,
4453
+ topP: p,
4454
+ nCtx: m,
4216
4455
  nGpuLayers: v,
4217
4456
  embeddings: y,
4218
4457
  rawCompletion: b,
@@ -4253,6 +4492,16 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
4253
4492
  _clearStatus() {
4254
4493
  !this._parsed.showProgress || this._parsed.jsonOutput || this.stderr && typeof this.stderr.write == "function" && this.stderr.write("\r\x1B[K");
4255
4494
  }
4495
+ _createTracker(e = "Loading") {
4496
+ return new m({
4497
+ enabled: !!(this._parsed.showProgress && !this._parsed.jsonOutput),
4498
+ width: 20,
4499
+ intervalMs: 80,
4500
+ onRender: (e) => {
4501
+ this.interactive ? (this._replStatus = e, this._renderRepl()) : e ? this._renderStatus(e) : this._clearStatus();
4502
+ }
4503
+ });
4504
+ }
4256
4505
  _attachProgressCallback() {
4257
4506
  if (!this._parsed.showProgress || this._parsed.modelSpec.kind === "fs") return;
4258
4507
  let e = this._parsed.model || this._parsed.modelSpec.file || this._parsed.modelSpec.repo || this._parsed.modelSpec.url || "model.gguf", t = 0;
@@ -4260,28 +4509,36 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
4260
4509
  let i = Date.now();
4261
4510
  if (t === 0 || r > 0 && n >= r || i - t >= 40) {
4262
4511
  t = i;
4263
- let a = f(e, n, r);
4512
+ let a = p(e, n, r);
4264
4513
  this.interactive ? (this._replStatus = a.replace(/^\r/, ""), this._renderRepl()) : this._parsed.subcommand === "pull" ? (this.stdOut = a, this.flush()) : this._renderStatus(a.replace(/^\r/, ""));
4265
4514
  }
4266
4515
  };
4267
4516
  }
4268
4517
  async _initWllama(n = !0) {
4269
4518
  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();
4519
+ 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) => {
4520
+ this.interactive ? (this._replStatus = e, this._renderRepl()) : this._renderStatus(e);
4521
+ };
4522
+ try {
4523
+ let { wllama: r, Wllama: s, persisted: c } = await v({
4524
+ modelSpec: this._parsed.modelSpec,
4525
+ mmprojSpec: this._parsed.mmproj,
4526
+ loadParams: this._parsed.loadParams,
4527
+ fs: this.fs,
4528
+ pkgMeta: e,
4529
+ wbtConfig: t,
4530
+ loadModel: n,
4531
+ persist: this._parsed.persist,
4532
+ persistPath: this._parsed.persistPath,
4533
+ tracker: a,
4534
+ shortLabel: i,
4535
+ isPullCommand: this._parsed.subcommand === "pull",
4536
+ onStatus: o
4537
+ });
4538
+ this._wllama = r, this._WllamaClass = s, this._persisted = c;
4539
+ } finally {
4540
+ a && a.clear(), this.interactive || this._clearStatus();
4541
+ }
4285
4542
  }
4286
4543
  async initRepl() {
4287
4544
  await this._initWllama(!0);
@@ -4340,39 +4597,46 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
4340
4597
  temperature: this._parsed.temperature,
4341
4598
  stream: n
4342
4599
  };
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));
4600
+ this._parsed.topK !== void 0 && (r.top_k = this._parsed.topK), this._parsed.topP !== void 0 && (r.top_p = this._parsed.topP);
4601
+ let i = this._createTracker();
4602
+ i.start("Running inference", { hint: "evaluating prompt & preparing KV cache" });
4603
+ try {
4604
+ if (typeof this._wllama.createChatCompletion == "function") {
4605
+ let e = await this._wllama.createChatCompletion(r);
4606
+ if (e && typeof e[Symbol.asyncIterator] == "function") {
4607
+ let n = "", r = !1;
4608
+ for await (let a of e) {
4609
+ let e = a?.choices?.[0]?.delta?.content ?? a?.choices?.[0]?.text ?? "";
4610
+ e && (r || (r = !0, i.clear(), this.interactive ? (this._replStatus = "Streaming tokens...", this._renderRepl()) : this._clearStatus()), n += e, typeof t == "function" && t(e));
4611
+ }
4612
+ return i.clear(), this.interactive || this._clearStatus(), n;
4350
4613
  }
4351
- return this.interactive || this._clearStatus(), n;
4614
+ if (i.clear(), this.interactive || this._clearStatus(), this._parsed.jsonOutput) return JSON.stringify(e);
4615
+ let n = y(e);
4616
+ return typeof t == "function" && n && t(n), n;
4352
4617
  }
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));
4618
+ let a = {
4619
+ prompt: e[e.length - 1]?.content || "",
4620
+ max_tokens: this._parsed.maxTokens,
4621
+ temperature: this._parsed.temperature,
4622
+ stream: n
4623
+ };
4624
+ this._parsed.topK !== void 0 && (a.top_k = this._parsed.topK), this._parsed.topP !== void 0 && (a.top_p = this._parsed.topP);
4625
+ let o = await this._wllama.createCompletion(a);
4626
+ if (o && typeof o[Symbol.asyncIterator] == "function") {
4627
+ let e = "", n = !1;
4628
+ for await (let r of o) {
4629
+ let a = r?.choices?.[0]?.text ?? r?.choices?.[0]?.delta?.content ?? "";
4630
+ a && (n || (n = !0, i.clear(), this.interactive ? (this._replStatus = "Streaming tokens...", this._renderRepl()) : this._clearStatus()), e += a, typeof t == "function" && t(a));
4631
+ }
4632
+ return i.clear(), this.interactive || this._clearStatus(), e;
4370
4633
  }
4371
- return this.interactive || this._clearStatus(), e;
4634
+ if (i.clear(), this.interactive || this._clearStatus(), this._parsed.jsonOutput) return JSON.stringify(o);
4635
+ let s = y(o);
4636
+ return typeof t == "function" && s && t(s), s;
4637
+ } finally {
4638
+ i.clear(), this.interactive || this._clearStatus();
4372
4639
  }
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
4640
  }
4377
4641
  _formatModelInfo() {
4378
4642
  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 +4768,17 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
4504
4768
  e = String(t);
4505
4769
  } 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
4770
  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);
4771
+ let t = this._createTracker();
4772
+ t.start("Computing embeddings", { hint: "evaluating prompt" });
4773
+ let n;
4774
+ try {
4775
+ n = await this._wllama.createEmbedding({ input: e });
4776
+ } finally {
4777
+ t.clear(), this._clearStatus();
4778
+ }
4779
+ if (this._parsed.jsonOutput) this.stdOut = JSON.stringify(n);
4509
4780
  else {
4510
- let e = t?.data?.[0]?.embedding || t?.embedding || [];
4781
+ let e = n?.data?.[0]?.embedding || n?.embedding || [];
4511
4782
  this.stdOut = JSON.stringify(e);
4512
4783
  }
4513
4784
  return;
@@ -4519,20 +4790,26 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
4519
4790
  temperature: this._parsed.temperature,
4520
4791
  stream: t
4521
4792
  };
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;
4793
+ this._parsed.topK !== void 0 && (n.top_k = this._parsed.topK), this._parsed.topP !== void 0 && (n.top_p = this._parsed.topP);
4794
+ let r = this._createTracker();
4795
+ r.start("Running inference", { hint: "evaluating prompt & preparing KV cache" });
4796
+ try {
4797
+ let e = await this._wllama.createCompletion(n), t = "";
4798
+ if (e && typeof e[Symbol.asyncIterator] == "function") {
4799
+ let n = !1;
4800
+ for await (let i of e) {
4801
+ let e = i?.choices?.[0]?.text ?? i?.choices?.[0]?.delta?.content ?? "";
4802
+ 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));
4803
+ }
4804
+ r.clear(), this._clearStatus(), n && t.length > 0 && !t.endsWith("\n") && this.stdout && typeof this.stdout.write == "function" && this.stdout.write("\r\n");
4805
+ } else r.clear(), this._clearStatus(), t = this._parsed.jsonOutput ? JSON.stringify(e) : y(e);
4806
+ t.split(/\r?\n/).forEach((e) => {
4807
+ this.stdOut = e;
4808
+ });
4809
+ return;
4810
+ } finally {
4811
+ r.clear(), this._clearStatus();
4812
+ }
4536
4813
  }
4537
4814
  let t = [];
4538
4815
  this._parsed.systemPrompt && t.push({
@@ -4559,29 +4836,29 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
4559
4836
  }
4560
4837
  return a;
4561
4838
  }
4562
- function y(e = {}, t = {}) {
4839
+ function x(e = {}, t = {}) {
4563
4840
  return [
4564
4841
  {
4565
4842
  bin: (e.bin || "wllama").toLowerCase(),
4566
- Cmd: v(e, t, "wllama")
4843
+ Cmd: b(e, t, "wllama")
4567
4844
  },
4568
4845
  {
4569
4846
  bin: "llama",
4570
- Cmd: v(e, t, "llama")
4847
+ Cmd: b(e, t, "llama")
4571
4848
  },
4572
4849
  {
4573
4850
  bin: "llama-cli",
4574
- Cmd: v(e, t, "llama-cli")
4851
+ Cmd: b(e, t, "llama-cli")
4575
4852
  }
4576
4853
  ];
4577
4854
  }
4578
- t.exports = v, t.exports.createWllamaCommand = v, t.exports.buildEntries = y, t.exports.WllamaCommand = v({
4855
+ t.exports = b, t.exports.createWllamaCommand = b, t.exports.buildEntries = x, t.exports.WllamaCommand = b({
4579
4856
  name: "wllama",
4580
4857
  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;
4858
+ }), 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
4859
  })), se = /* @__PURE__ */ t(((e, t) => {
4583
4860
  b();
4584
- 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";
4861
+ var r = E(), i = Z(), a = re(), { 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";
4585
4862
  function m(e, t, n = {}) {
4586
4863
  class r extends i {
4587
4864
  static get name() {
@@ -4593,6 +4870,12 @@ var R, z, B, V, H, U, W, G, K, q, ee = e((() => {
4593
4870
  get requiresFilesystem() {
4594
4871
  return !0;
4595
4872
  }
4873
+ get interactive() {
4874
+ return (this.arguments || []).some((e) => e === "--help" || e === "-h" || e === "--version" || e === "-V" || e === "-v") ? !1 : this._isInteractive === void 0 ? !!(n.interactive || e === "nano") : !!this._isInteractive;
4875
+ }
4876
+ get captureInput() {
4877
+ return this.interactive;
4878
+ }
4596
4879
  constructor(e, r) {
4597
4880
  super(t, e, r), this.script = t, this.arguments = e || [], this.context = r || null, r && r.fs && !this.fs && (this.fs = r.fs), this._pkgMeta = n;
4598
4881
  }