@asgard-js/core 0.2.53 → 0.2.54

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/index.mjs CHANGED
@@ -25,7 +25,7 @@ function I(n, e) {
25
25
  }
26
26
  n.prototype = e === null ? Object.create(e) : (t.prototype = e.prototype, new t());
27
27
  }
28
- function Ge(n, e, t, r) {
28
+ function Fe(n, e, t, r) {
29
29
  function o(i) {
30
30
  return i instanceof t ? i : new t(function(s) {
31
31
  s(i);
@@ -147,7 +147,7 @@ function R(n, e, t) {
147
147
  function x(n) {
148
148
  return this instanceof x ? (this.v = n, this) : new x(n);
149
149
  }
150
- function Fe(n, e, t) {
150
+ function Ge(n, e, t) {
151
151
  if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
152
152
  var r = t.apply(n, e || []), o, i = [];
153
153
  return o = Object.create((typeof AsyncIterator == "function" ? AsyncIterator : Object).prototype), a("next"), a("throw"), a("return", s), o[Symbol.asyncIterator] = function() {
@@ -434,11 +434,11 @@ var We = {
434
434
  }, Z = (function() {
435
435
  return typeof Symbol == "function" && Symbol.observable || "@@observable";
436
436
  })();
437
- function G(n) {
437
+ function F(n) {
438
438
  return n;
439
439
  }
440
440
  function Xe(n) {
441
- return n.length === 0 ? G : n.length === 1 ? n[0] : function(t) {
441
+ return n.length === 0 ? F : n.length === 1 ? n[0] : function(t) {
442
442
  return n.reduce(function(r, o) {
443
443
  return o(r);
444
444
  }, t);
@@ -827,7 +827,7 @@ function _e(n) {
827
827
  return m(n?.[Te]);
828
828
  }
829
829
  function Le(n) {
830
- return Fe(this, arguments, function() {
830
+ return Ge(this, arguments, function() {
831
831
  var t, r, o, i;
832
832
  return he(this, function(s) {
833
833
  switch (s.label) {
@@ -936,7 +936,7 @@ function ht(n) {
936
936
  }
937
937
  function pt(n, e) {
938
938
  var t, r, o, i;
939
- return Ge(this, void 0, void 0, function() {
939
+ return Fe(this, void 0, void 0, function() {
940
940
  var s, a;
941
941
  return he(this, function(c) {
942
942
  switch (c.label) {
@@ -1083,7 +1083,7 @@ function Et() {
1083
1083
  function wt(n) {
1084
1084
  return n instanceof Date && !isNaN(n);
1085
1085
  }
1086
- function F(n, e) {
1086
+ function G(n, e) {
1087
1087
  return L(function(t, r) {
1088
1088
  var o = 0;
1089
1089
  t.subscribe(O(r, function(i) {
@@ -1096,7 +1096,7 @@ function It(n, e) {
1096
1096
  return Ot(e) ? n.apply(void 0, R([], M(e))) : n(e);
1097
1097
  }
1098
1098
  function Ct(n) {
1099
- return F(function(e) {
1099
+ return G(function(e) {
1100
1100
  return It(n, e);
1101
1101
  });
1102
1102
  }
@@ -1134,11 +1134,11 @@ function Rt() {
1134
1134
  return te([], t);
1135
1135
  var a = new g(Nt(i, t, s ? function(c) {
1136
1136
  return Mt(s, c);
1137
- } : G));
1137
+ } : F));
1138
1138
  return r ? a.pipe(Ct(r)) : a;
1139
1139
  }
1140
1140
  function Nt(n, e, t) {
1141
- return t === void 0 && (t = G), function(r) {
1141
+ return t === void 0 && (t = F), function(r) {
1142
1142
  ce(e, function() {
1143
1143
  for (var o = n.length, i = new Array(o), s = o, a = o, c = function(f) {
1144
1144
  ce(e, function() {
@@ -1191,7 +1191,7 @@ function Ut(n, e, t, r, o, i, s, a) {
1191
1191
  }
1192
1192
  function H(n, e, t) {
1193
1193
  return t === void 0 && (t = 1 / 0), m(e) ? H(function(r, o) {
1194
- return F(function(i, s) {
1194
+ return G(function(i, s) {
1195
1195
  return e(r, i, o, s);
1196
1196
  })(T(n(r, o)));
1197
1197
  }, t) : (typeof e == "number" && (t = e), L(function(r, o) {
@@ -1224,7 +1224,7 @@ function $t(n) {
1224
1224
  });
1225
1225
  }
1226
1226
  function jt(n) {
1227
- return F(function() {
1227
+ return G(function() {
1228
1228
  return n;
1229
1229
  });
1230
1230
  }
@@ -1240,13 +1240,13 @@ function Ht(n, e) {
1240
1240
  return t;
1241
1241
  });
1242
1242
  }
1243
- function Gt(n) {
1243
+ function Ft(n) {
1244
1244
  var e;
1245
1245
  e = {
1246
1246
  count: n
1247
1247
  };
1248
1248
  var t = e.count, r = t === void 0 ? 1 / 0 : t, o = e.delay, i = e.resetOnSuccess, s = i === void 0 ? !1 : i;
1249
- return r <= 0 ? G : L(function(a, c) {
1249
+ return r <= 0 ? F : L(function(a, c) {
1250
1250
  var u = 0, f, p = function() {
1251
1251
  var h = !1;
1252
1252
  f = a.subscribe(O(c, function(l) {
@@ -1272,7 +1272,7 @@ function Gt(n) {
1272
1272
  p();
1273
1273
  });
1274
1274
  }
1275
- function Ft(n) {
1275
+ function Gt(n) {
1276
1276
  return L(function(e, t) {
1277
1277
  T(n).subscribe(O(t, function() {
1278
1278
  return t.complete();
@@ -1486,6 +1486,10 @@ class sn {
1486
1486
  botProviderEndpoint;
1487
1487
  debugMode;
1488
1488
  destroy$ = new ee();
1489
+ closed = !1;
1490
+ detached = !1;
1491
+ detachTimer;
1492
+ inFlight = 0;
1489
1493
  sseEmitter = new Qt();
1490
1494
  transformSsePayload;
1491
1495
  customHeaders;
@@ -1538,7 +1542,7 @@ class sn {
1538
1542
  }
1539
1543
  }
1540
1544
  fetchSse(e, t) {
1541
- t?.onSseStart?.(), zt({
1545
+ t?.onSseStart?.(), this.inFlight += 1, zt({
1542
1546
  apiKey: this.apiKey,
1543
1547
  endpoint: this.endpoint,
1544
1548
  debugMode: this.debugMode,
@@ -1546,22 +1550,48 @@ class sn {
1546
1550
  customHeaders: this.customHeaders
1547
1551
  }).pipe(
1548
1552
  kt((r) => Et(r).pipe(Ht(t?.delayTime ?? 50))),
1549
- Ft(this.destroy$),
1550
- Gt(3)
1553
+ Gt(this.destroy$),
1554
+ Ft(3)
1551
1555
  ).subscribe({
1552
1556
  next: (r) => {
1553
- t?.onSseMessage?.(r), this.handleEvent(r);
1557
+ this.detached || (t?.onSseMessage?.(r), this.handleEvent(r));
1554
1558
  },
1555
1559
  error: (r) => {
1556
- t?.onSseError?.(r);
1560
+ this.detached || t?.onSseError?.(r), this.onRunSettled();
1557
1561
  },
1558
1562
  complete: () => {
1559
- t?.onSseCompleted?.();
1563
+ this.detached || t?.onSseCompleted?.(), this.onRunSettled();
1560
1564
  }
1561
1565
  });
1562
1566
  }
1567
+ /**
1568
+ * Detach the client from its owning component without aborting in-flight SSE
1569
+ * runs. Used when a chatbot unmounts (e.g. the user navigates to another
1570
+ * in-app page) but the current run should be allowed to finish on the
1571
+ * backend instead of being cut off. After detaching, the stream is still
1572
+ * drained to keep the connection open but consumer callbacks no longer fire.
1573
+ *
1574
+ * The connection cleans itself up in all cases:
1575
+ * - No run in flight → close immediately (nothing to keep alive).
1576
+ * - Runs in flight → close once they all settle (`onRunSettled`), so the
1577
+ * client never lingers past the work it is keeping alive.
1578
+ * - A run gets stuck (stream stays open, no `run.done`/error) → the safety
1579
+ * timeout force-closes it so the orphaned connection cannot leak.
1580
+ */
1581
+ detach(e) {
1582
+ if (!(this.detached || this.closed)) {
1583
+ if (this.detached = !0, this.inFlight === 0) {
1584
+ this.close();
1585
+ return;
1586
+ }
1587
+ this.detachTimer = setTimeout(() => this.close(), e.timeoutMs);
1588
+ }
1589
+ }
1590
+ onRunSettled() {
1591
+ this.inFlight = Math.max(0, this.inFlight - 1), this.detached && this.inFlight === 0 && this.close();
1592
+ }
1563
1593
  close() {
1564
- this.destroy$.next(), this.destroy$.complete();
1594
+ this.closed || (this.closed = !0, this.detachTimer && (clearTimeout(this.detachTimer), this.detachTimer = void 0), this.destroy$.next(), this.destroy$.complete());
1565
1595
  }
1566
1596
  /**
1567
1597
  * 上傳檔案到 Blob API
@@ -1787,7 +1817,7 @@ class z {
1787
1817
  }
1788
1818
  subscribe() {
1789
1819
  this.statesSubscription = Rt([this.isConnecting$, this.conversation$]).pipe(
1790
- F(([e, t]) => ({
1820
+ G(([e, t]) => ({
1791
1821
  isConnecting: e,
1792
1822
  conversation: t
1793
1823
  }))