@asgard-js/core 0.2.53 → 0.2.55

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
@@ -9,23 +9,23 @@ class de extends Error {
9
9
  function on(n) {
10
10
  return n instanceof de;
11
11
  }
12
- var $ = /* @__PURE__ */ ((n) => (n.RESET_CHANNEL = "RESET_CHANNEL", n.NONE = "NONE", n.RESPONSE_TOOL_CALL_CONSENT = "RESPONSE_TOOL_CALL_CONSENT", n))($ || {}), d = /* @__PURE__ */ ((n) => (n.INIT = "asgard.run.init", n.PROCESS = "asgard.process", n.PROCESS_START = "asgard.process.start", n.PROCESS_COMPLETE = "asgard.process.complete", n.MESSAGE = "asgard.message", n.MESSAGE_START = "asgard.message.start", n.MESSAGE_DELTA = "asgard.message.delta", n.MESSAGE_COMPLETE = "asgard.message.complete", n.TOOL_CALL = "asgard.tool_call", n.TOOL_CALL_START = "asgard.tool_call.start", n.TOOL_CALL_COMPLETE = "asgard.tool_call.complete", n.TOOL_CALL_CONSENT = "asgard.tool_call.consent", n.DONE = "asgard.run.done", n.ERROR = "asgard.run.error", n))(d || {}), De = /* @__PURE__ */ ((n) => (n.ALLOW_ONCE = "ALLOW_ONCE", n.ALLOW_ALWAYS = "ALLOW_ALWAYS", n.DENY_ONCE = "DENY_ONCE", n))(De || {}), He = /* @__PURE__ */ ((n) => (n.TEXT = "TEXT", n.HINT = "HINT", n.BUTTON = "BUTTON", n.IMAGE = "IMAGE", n.VIDEO = "VIDEO", n.AUDIO = "AUDIO", n.LOCATION = "LOCATION", n.CAROUSEL = "CAROUSEL", n.CHART = "CHART", n.TABLE = "TABLE", n.ATTACHMENT = "ATTACHMENT", n))(He || {}), B = function(n, e) {
13
- return B = Object.setPrototypeOf || { __proto__: [] } instanceof Array && function(t, r) {
12
+ var k = /* @__PURE__ */ ((n) => (n.RESET_CHANNEL = "RESET_CHANNEL", n.NONE = "NONE", n.RESPONSE_TOOL_CALL_CONSENT = "RESPONSE_TOOL_CALL_CONSENT", n))(k || {}), d = /* @__PURE__ */ ((n) => (n.INIT = "asgard.run.init", n.PROCESS = "asgard.process", n.PROCESS_START = "asgard.process.start", n.PROCESS_COMPLETE = "asgard.process.complete", n.MESSAGE = "asgard.message", n.MESSAGE_START = "asgard.message.start", n.MESSAGE_DELTA = "asgard.message.delta", n.MESSAGE_COMPLETE = "asgard.message.complete", n.TOOL_CALL = "asgard.tool_call", n.TOOL_CALL_START = "asgard.tool_call.start", n.TOOL_CALL_COMPLETE = "asgard.tool_call.complete", n.TOOL_CALL_CONSENT = "asgard.tool_call.consent", n.DONE = "asgard.run.done", n.ERROR = "asgard.run.error", n))(d || {}), je = /* @__PURE__ */ ((n) => (n.ALLOW_ONCE = "ALLOW_ONCE", n.ALLOW_ALWAYS = "ALLOW_ALWAYS", n.DENY_ONCE = "DENY_ONCE", n))(je || {}), He = /* @__PURE__ */ ((n) => (n.TEXT = "TEXT", n.HINT = "HINT", n.BUTTON = "BUTTON", n.IMAGE = "IMAGE", n.VIDEO = "VIDEO", n.AUDIO = "AUDIO", n.LOCATION = "LOCATION", n.CAROUSEL = "CAROUSEL", n.CHART = "CHART", n.TABLE = "TABLE", n.ATTACHMENT = "ATTACHMENT", n))(He || {}), q = function(n, e) {
13
+ return q = Object.setPrototypeOf || { __proto__: [] } instanceof Array && function(t, r) {
14
14
  t.__proto__ = r;
15
15
  } || function(t, r) {
16
16
  for (var o in r) Object.prototype.hasOwnProperty.call(r, o) && (t[o] = r[o]);
17
- }, B(n, e);
17
+ }, q(n, e);
18
18
  };
19
19
  function I(n, e) {
20
20
  if (typeof e != "function" && e !== null)
21
21
  throw new TypeError("Class extends value " + String(e) + " is not a constructor or null");
22
- B(n, e);
22
+ q(n, e);
23
23
  function t() {
24
24
  this.constructor = n;
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() {
@@ -160,8 +160,8 @@ function Fe(n, e, t) {
160
160
  }
161
161
  function a(l, y) {
162
162
  r[l] && (o[l] = function(v) {
163
- return new Promise(function(E, w) {
164
- i.push([l, v, E, w]) > 1 || c(l, v);
163
+ return new Promise(function(w, E) {
164
+ i.push([l, v, w, E]) > 1 || c(l, v);
165
165
  });
166
166
  }, y && (o[l] = y(o[l])));
167
167
  }
@@ -185,7 +185,7 @@ function Fe(n, e, t) {
185
185
  l(y), i.shift(), i.length && c(i[0][0], i[0][1]);
186
186
  }
187
187
  }
188
- function Ve(n) {
188
+ function Ke(n) {
189
189
  if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
190
190
  var e = n[Symbol.asyncIterator], t;
191
191
  return e ? e.call(n) : (n = typeof P == "function" ? P(n) : n[Symbol.iterator](), t = {}, r("next"), r("throw"), r("return"), t[Symbol.asyncIterator] = function() {
@@ -213,7 +213,7 @@ function pe(n) {
213
213
  }, t = n(e);
214
214
  return t.prototype = Object.create(Error.prototype), t.prototype.constructor = t, t;
215
215
  }
216
- var q = pe(function(n) {
216
+ var B = pe(function(n) {
217
217
  return function(t) {
218
218
  n(this), this.message = t ? t.length + ` errors occurred during unsubscription:
219
219
  ` + t.map(function(r, o) {
@@ -222,7 +222,7 @@ var q = pe(function(n) {
222
222
  `) : "", this.name = "UnsubscriptionError", this.errors = t;
223
223
  };
224
224
  });
225
- function D(n, e) {
225
+ function j(n, e) {
226
226
  if (n) {
227
227
  var t = n.indexOf(e);
228
228
  0 <= t && n.splice(t, 1);
@@ -260,7 +260,7 @@ var N = (function() {
260
260
  try {
261
261
  f();
262
262
  } catch (v) {
263
- i = v instanceof q ? v.errors : [v];
263
+ i = v instanceof B ? v.errors : [v];
264
264
  }
265
265
  var p = this._finalizers;
266
266
  if (p) {
@@ -271,7 +271,7 @@ var N = (function() {
271
271
  try {
272
272
  ne(y);
273
273
  } catch (v) {
274
- i = i ?? [], v instanceof q ? i = R(R([], M(i)), M(v.errors)) : i.push(v);
274
+ i = i ?? [], v instanceof B ? i = R(R([], M(i)), M(v.errors)) : i.push(v);
275
275
  }
276
276
  }
277
277
  } catch (v) {
@@ -285,7 +285,7 @@ var N = (function() {
285
285
  }
286
286
  }
287
287
  if (i)
288
- throw new q(i);
288
+ throw new B(i);
289
289
  }
290
290
  }, n.prototype.add = function(e) {
291
291
  var t;
@@ -308,10 +308,10 @@ var N = (function() {
308
308
  this._parentage = Array.isArray(t) ? (t.push(e), t) : t ? [t, e] : e;
309
309
  }, n.prototype._removeParent = function(e) {
310
310
  var t = this._parentage;
311
- t === e ? this._parentage = null : Array.isArray(t) && D(t, e);
311
+ t === e ? this._parentage = null : Array.isArray(t) && j(t, e);
312
312
  }, n.prototype.remove = function(e) {
313
313
  var t = this._finalizers;
314
- t && D(t, e), e instanceof n && e._removeParent(this);
314
+ t && j(t, e), e instanceof n && e._removeParent(this);
315
315
  }, n.EMPTY = (function() {
316
316
  var e = new n();
317
317
  return e.closed = !0, e;
@@ -323,9 +323,9 @@ function ye(n) {
323
323
  function ne(n) {
324
324
  m(n) ? n() : n.unsubscribe();
325
325
  }
326
- var Ye = {
326
+ var Ve = {
327
327
  Promise: void 0
328
- }, qe = {
328
+ }, Be = {
329
329
  setTimeout: function(n, e) {
330
330
  for (var t = [], r = 2; r < arguments.length; r++)
331
331
  t[r - 2] = arguments[r];
@@ -337,13 +337,13 @@ var Ye = {
337
337
  delegate: void 0
338
338
  };
339
339
  function me(n) {
340
- qe.setTimeout(function() {
340
+ Be.setTimeout(function() {
341
341
  throw n;
342
342
  });
343
343
  }
344
344
  function W() {
345
345
  }
346
- function j(n) {
346
+ function D(n) {
347
347
  n();
348
348
  }
349
349
  var Q = (function(n) {
@@ -377,7 +377,7 @@ var Q = (function(n) {
377
377
  this.unsubscribe();
378
378
  }
379
379
  }, e;
380
- })(N), Ke = (function() {
380
+ })(N), Ye = (function() {
381
381
  function n(e) {
382
382
  this.partialObserver = e;
383
383
  }
@@ -387,7 +387,7 @@ var Q = (function(n) {
387
387
  try {
388
388
  t.next(e);
389
389
  } catch (r) {
390
- k(r);
390
+ $(r);
391
391
  }
392
392
  }, n.prototype.error = function(e) {
393
393
  var t = this.partialObserver;
@@ -395,17 +395,17 @@ var Q = (function(n) {
395
395
  try {
396
396
  t.error(e);
397
397
  } catch (r) {
398
- k(r);
398
+ $(r);
399
399
  }
400
400
  else
401
- k(e);
401
+ $(e);
402
402
  }, n.prototype.complete = function() {
403
403
  var e = this.partialObserver;
404
404
  if (e.complete)
405
405
  try {
406
406
  e.complete();
407
407
  } catch (t) {
408
- k(t);
408
+ $(t);
409
409
  }
410
410
  }, n;
411
411
  })(), X = (function(n) {
@@ -416,29 +416,29 @@ var Q = (function(n) {
416
416
  next: t ?? void 0,
417
417
  error: r ?? void 0,
418
418
  complete: o ?? void 0
419
- } : s = t, i.destination = new Ke(s), i;
419
+ } : s = t, i.destination = new Ye(s), i;
420
420
  }
421
421
  return e;
422
422
  })(Q);
423
- function k(n) {
423
+ function $(n) {
424
424
  me(n);
425
425
  }
426
- function Be(n) {
426
+ function qe(n) {
427
427
  throw n;
428
428
  }
429
429
  var We = {
430
430
  closed: !0,
431
431
  next: W,
432
- error: Be,
432
+ error: qe,
433
433
  complete: W
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);
@@ -452,8 +452,8 @@ var g = (function() {
452
452
  var t = new n();
453
453
  return t.source = this, t.operator = e, t;
454
454
  }, n.prototype.subscribe = function(e, t, r) {
455
- var o = this, i = ze(e) ? e : new X(e, t, r);
456
- return j(function() {
455
+ var o = this, i = Je(e) ? e : new X(e, t, r);
456
+ return D(function() {
457
457
  var s = o, a = s.operator, c = s.source;
458
458
  i.add(a ? a.call(i, c) : c ? o._subscribe(i) : o._trySubscribe(i));
459
459
  }), i;
@@ -506,13 +506,13 @@ var g = (function() {
506
506
  })();
507
507
  function re(n) {
508
508
  var e;
509
- return (e = n ?? Ye.Promise) !== null && e !== void 0 ? e : Promise;
509
+ return (e = n ?? Ve.Promise) !== null && e !== void 0 ? e : Promise;
510
510
  }
511
- function Je(n) {
511
+ function ze(n) {
512
512
  return n && m(n.next) && m(n.error) && m(n.complete);
513
513
  }
514
- function ze(n) {
515
- return n && n instanceof Q || Je(n) && ye(n);
514
+ function Je(n) {
515
+ return n && n instanceof Q || ze(n) && ye(n);
516
516
  }
517
517
  function Qe(n) {
518
518
  return m(n?.lift);
@@ -586,7 +586,7 @@ var Ze = (function(n) {
586
586
  throw new et();
587
587
  }, e.prototype.next = function(t) {
588
588
  var r = this;
589
- j(function() {
589
+ D(function() {
590
590
  var o, i;
591
591
  if (r._throwIfClosed(), !r.isStopped) {
592
592
  r.currentObservers || (r.currentObservers = Array.from(r.observers));
@@ -608,7 +608,7 @@ var Ze = (function(n) {
608
608
  });
609
609
  }, e.prototype.error = function(t) {
610
610
  var r = this;
611
- j(function() {
611
+ D(function() {
612
612
  if (r._throwIfClosed(), !r.isStopped) {
613
613
  r.hasError = r.isStopped = !0, r.thrownError = t;
614
614
  for (var o = r.observers; o.length; )
@@ -617,7 +617,7 @@ var Ze = (function(n) {
617
617
  });
618
618
  }, e.prototype.complete = function() {
619
619
  var t = this;
620
- j(function() {
620
+ D(function() {
621
621
  if (t._throwIfClosed(), !t.isStopped) {
622
622
  t.isStopped = !0;
623
623
  for (var r = t.observers; r.length; )
@@ -640,7 +640,7 @@ var Ze = (function(n) {
640
640
  }, e.prototype._innerSubscribe = function(t) {
641
641
  var r = this, o = this, i = o.hasError, s = o.isStopped, a = o.observers;
642
642
  return i || s ? ve : (this.currentObservers = null, a.push(t), new N(function() {
643
- r.currentObservers = null, D(a, t);
643
+ r.currentObservers = null, j(a, t);
644
644
  }));
645
645
  }, e.prototype._checkFinalizedStatuses = function(t) {
646
646
  var r = this, o = r.hasError, i = r.thrownError, s = r.isStopped;
@@ -754,7 +754,7 @@ var Ze = (function(n) {
754
754
  }, e.prototype.unsubscribe = function() {
755
755
  if (!this.closed) {
756
756
  var t = this, r = t.id, o = t.scheduler, i = o.actions;
757
- this.work = this.state = this.scheduler = null, this.pending = !1, D(i, this), r != null && (this.id = this.recycleAsyncId(o, r, null)), this.delay = null, n.prototype.unsubscribe.call(this);
757
+ this.work = this.state = this.scheduler = null, this.pending = !1, j(i, this), r != null && (this.id = this.recycleAsyncId(o, r, null)), this.delay = null, n.prototype.unsubscribe.call(this);
758
758
  }
759
759
  }, e;
760
760
  })(nt), ae = (function() {
@@ -801,10 +801,10 @@ function Se(n) {
801
801
  function at(n) {
802
802
  return m(Se(n)) ? n.pop() : void 0;
803
803
  }
804
- function Ee(n) {
804
+ function we(n) {
805
805
  return ge(Se(n)) ? n.pop() : void 0;
806
806
  }
807
- var we = (function(n) {
807
+ var Ee = (function(n) {
808
808
  return n && typeof n.length == "number" && typeof n != "function";
809
809
  });
810
810
  function Oe(n) {
@@ -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) {
@@ -866,7 +866,7 @@ function T(n) {
866
866
  if (n != null) {
867
867
  if (Ie(n))
868
868
  return ut(n);
869
- if (we(n))
869
+ if (Ee(n))
870
870
  return lt(n);
871
871
  if (Oe(n))
872
872
  return ft(n);
@@ -936,12 +936,12 @@ 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) {
943
943
  case 0:
944
- c.trys.push([0, 5, 6, 11]), t = Ve(n), c.label = 1;
944
+ c.trys.push([0, 5, 6, 11]), t = Ke(n), c.label = 1;
945
945
  case 1:
946
946
  return [4, t.next()];
947
947
  case 2:
@@ -1058,7 +1058,7 @@ function St(n, e) {
1058
1058
  if (n != null) {
1059
1059
  if (Ie(n))
1060
1060
  return vt(n, e);
1061
- if (we(n))
1061
+ if (Ee(n))
1062
1062
  return mt(n, e);
1063
1063
  if (Oe(n))
1064
1064
  return yt(n, e);
@@ -1074,16 +1074,16 @@ function St(n, e) {
1074
1074
  function te(n, e) {
1075
1075
  return e ? St(n, e) : T(n);
1076
1076
  }
1077
- function Et() {
1077
+ function wt() {
1078
1078
  for (var n = [], e = 0; e < arguments.length; e++)
1079
1079
  n[e] = arguments[e];
1080
- var t = Ee(n);
1080
+ var t = we(n);
1081
1081
  return te(n, t);
1082
1082
  }
1083
- function wt(n) {
1083
+ function Et(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
  }
@@ -1129,16 +1129,16 @@ function Mt(n, e) {
1129
1129
  function Rt() {
1130
1130
  for (var n = [], e = 0; e < arguments.length; e++)
1131
1131
  n[e] = arguments[e];
1132
- var t = Ee(n), r = at(n), o = xt(n), i = o.args, s = o.keys;
1132
+ var t = we(n), r = at(n), o = xt(n), i = o.args, s = o.keys;
1133
1133
  if (i.length === 0)
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() {
@@ -1164,20 +1164,20 @@ function Ut(n, e, t, r, o, i, s, a) {
1164
1164
  return u < r ? y(v) : c.push(v);
1165
1165
  }, y = function(v) {
1166
1166
  u++;
1167
- var E = !1;
1168
- T(t(v, f++)).subscribe(O(e, function(w) {
1169
- e.next(w);
1167
+ var w = !1;
1168
+ T(t(v, f++)).subscribe(O(e, function(E) {
1169
+ e.next(E);
1170
1170
  }, function() {
1171
- E = !0;
1171
+ w = !0;
1172
1172
  }, void 0, function() {
1173
- if (E)
1173
+ if (w)
1174
1174
  try {
1175
1175
  u--;
1176
- for (var w = function() {
1176
+ for (var E = function() {
1177
1177
  var _ = c.shift();
1178
1178
  s || y(_);
1179
1179
  }; c.length && u < r; )
1180
- w();
1180
+ E();
1181
1181
  h();
1182
1182
  } catch (_) {
1183
1183
  e.error(_);
@@ -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) {
@@ -1202,7 +1202,7 @@ function Ue(n, e, t) {
1202
1202
  n === void 0 && (n = 0), t === void 0 && (t = it);
1203
1203
  var r = -1;
1204
1204
  return e != null && (ge(e) ? t = e : r = e), new g(function(o) {
1205
- var i = wt(n) ? +n - t.now() : n;
1205
+ var i = Et(n) ? +n - t.now() : n;
1206
1206
  i < 0 && (i = 0);
1207
1207
  var s = 0;
1208
1208
  return t.schedule(function() {
@@ -1210,10 +1210,10 @@ function Ue(n, e, t) {
1210
1210
  }, i);
1211
1211
  });
1212
1212
  }
1213
- function kt(n, e) {
1213
+ function $t(n, e) {
1214
1214
  return m(e) ? H(n, e, 1) : H(n, 1);
1215
1215
  }
1216
- function $t(n) {
1216
+ function kt(n) {
1217
1217
  return n <= 0 ? function() {
1218
1218
  return st;
1219
1219
  } : L(function(e, t) {
@@ -1223,30 +1223,30 @@ function $t(n) {
1223
1223
  }));
1224
1224
  });
1225
1225
  }
1226
- function jt(n) {
1227
- return F(function() {
1226
+ function Dt(n) {
1227
+ return G(function() {
1228
1228
  return n;
1229
1229
  });
1230
1230
  }
1231
- function Dt(n, e) {
1231
+ function jt(n, e) {
1232
1232
  return H(function(t, r) {
1233
- return T(n(t, r)).pipe($t(1), jt(t));
1233
+ return T(n(t, r)).pipe(kt(1), Dt(t));
1234
1234
  });
1235
1235
  }
1236
1236
  function Ht(n, e) {
1237
1237
  e === void 0 && (e = be);
1238
1238
  var t = Ue(n, e);
1239
- return Dt(function() {
1239
+ return jt(function() {
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) {
@@ -1257,12 +1257,12 @@ function Gt(n) {
1257
1257
  f ? (f.unsubscribe(), f = null, p()) : h = !0;
1258
1258
  };
1259
1259
  if (o != null) {
1260
- var v = typeof o == "number" ? Ue(o) : T(o(l, u)), E = O(c, function() {
1261
- E.unsubscribe(), y();
1260
+ var v = typeof o == "number" ? Ue(o) : T(o(l, u)), w = O(c, function() {
1261
+ w.unsubscribe(), y();
1262
1262
  }, function() {
1263
1263
  c.complete();
1264
1264
  });
1265
- v.subscribe(E);
1265
+ v.subscribe(w);
1266
1266
  } else
1267
1267
  y();
1268
1268
  } else
@@ -1272,23 +1272,23 @@ 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();
1279
1279
  }, W)), !t.closed && e.subscribe(t);
1280
1280
  });
1281
1281
  }
1282
- async function Vt(n, e) {
1282
+ async function Kt(n, e) {
1283
1283
  const t = n.getReader();
1284
1284
  let r;
1285
1285
  for (; !(r = await t.read()).done; )
1286
1286
  e(r.value);
1287
1287
  }
1288
- function Yt(n) {
1288
+ function Vt(n) {
1289
1289
  let e, t, r, o = !1;
1290
1290
  return function(s) {
1291
- e === void 0 ? (e = s, t = 0, r = -1) : e = Kt(e, s);
1291
+ e === void 0 ? (e = s, t = 0, r = -1) : e = Yt(e, s);
1292
1292
  const a = e.length;
1293
1293
  let c = 0;
1294
1294
  for (; t < a; ) {
@@ -1312,7 +1312,7 @@ function Yt(n) {
1312
1312
  c === a ? e = void 0 : c !== 0 && (e = e.subarray(c), t -= c);
1313
1313
  };
1314
1314
  }
1315
- function qt(n, e, t) {
1315
+ function Bt(n, e, t) {
1316
1316
  let r = ue();
1317
1317
  const o = new TextDecoder();
1318
1318
  return function(s, a) {
@@ -1339,7 +1339,7 @@ function qt(n, e, t) {
1339
1339
  }
1340
1340
  };
1341
1341
  }
1342
- function Kt(n, e) {
1342
+ function Yt(n, e) {
1343
1343
  const t = new Uint8Array(n.length + e.length);
1344
1344
  return t.set(n), t.set(e, n.length), t;
1345
1345
  }
@@ -1351,7 +1351,7 @@ function ue() {
1351
1351
  retry: void 0
1352
1352
  };
1353
1353
  }
1354
- var Bt = function(n, e) {
1354
+ var qt = function(n, e) {
1355
1355
  var t = {};
1356
1356
  for (var r in n) Object.prototype.hasOwnProperty.call(n, r) && e.indexOf(r) < 0 && (t[r] = n[r]);
1357
1357
  if (n != null && typeof Object.getOwnPropertySymbols == "function")
@@ -1359,54 +1359,54 @@ var Bt = function(n, e) {
1359
1359
  e.indexOf(r[o]) < 0 && Object.prototype.propertyIsEnumerable.call(n, r[o]) && (t[r[o]] = n[r[o]]);
1360
1360
  return t;
1361
1361
  };
1362
- const J = "text/event-stream", Wt = 1e3, le = "last-event-id";
1362
+ const z = "text/event-stream", Wt = 1e3, le = "last-event-id";
1363
1363
  function Xt(n, e) {
1364
- var { signal: t, headers: r, onopen: o, onmessage: i, onclose: s, onerror: a, openWhenHidden: c, fetch: u } = e, f = Bt(e, ["signal", "headers", "onopen", "onmessage", "onclose", "onerror", "openWhenHidden", "fetch"]);
1364
+ var { signal: t, headers: r, onopen: o, onmessage: i, onclose: s, onerror: a, openWhenHidden: c, fetch: u } = e, f = qt(e, ["signal", "headers", "onopen", "onmessage", "onclose", "onerror", "openWhenHidden", "fetch"]);
1365
1365
  return new Promise((p, h) => {
1366
1366
  const l = Object.assign({}, r);
1367
- l.accept || (l.accept = J);
1367
+ l.accept || (l.accept = z);
1368
1368
  let y;
1369
1369
  function v() {
1370
- y.abort(), document.hidden || V();
1370
+ y.abort(), document.hidden || K();
1371
1371
  }
1372
1372
  c || document.addEventListener("visibilitychange", v);
1373
- let E = Wt, w = 0;
1373
+ let w = Wt, E = 0;
1374
1374
  function _() {
1375
- document.removeEventListener("visibilitychange", v), window.clearTimeout(w), y.abort();
1375
+ document.removeEventListener("visibilitychange", v), window.clearTimeout(E), y.abort();
1376
1376
  }
1377
1377
  t?.addEventListener("abort", () => {
1378
1378
  _(), p();
1379
1379
  });
1380
- const $e = u ?? window.fetch, je = o ?? Jt;
1381
- async function V() {
1382
- var Y;
1380
+ const ke = u ?? window.fetch, De = o ?? zt;
1381
+ async function K() {
1382
+ var V;
1383
1383
  y = new AbortController();
1384
1384
  try {
1385
- const U = await $e(n, Object.assign(Object.assign({}, f), { headers: l, signal: y.signal }));
1386
- await je(U), await Vt(U.body, Yt(qt((C) => {
1385
+ const U = await ke(n, Object.assign(Object.assign({}, f), { headers: l, signal: y.signal }));
1386
+ await De(U), await Kt(U.body, Vt(Bt((C) => {
1387
1387
  C ? l[le] = C : delete l[le];
1388
1388
  }, (C) => {
1389
- E = C;
1389
+ w = C;
1390
1390
  }, i))), s?.(), _(), p();
1391
1391
  } catch (U) {
1392
1392
  if (!y.signal.aborted)
1393
1393
  try {
1394
- const C = (Y = a?.(U)) !== null && Y !== void 0 ? Y : E;
1395
- window.clearTimeout(w), w = window.setTimeout(V, C);
1394
+ const C = (V = a?.(U)) !== null && V !== void 0 ? V : w;
1395
+ window.clearTimeout(E), E = window.setTimeout(K, C);
1396
1396
  } catch (C) {
1397
1397
  _(), h(C);
1398
1398
  }
1399
1399
  }
1400
1400
  }
1401
- V();
1401
+ K();
1402
1402
  });
1403
1403
  }
1404
- function Jt(n) {
1404
+ function zt(n) {
1405
1405
  const e = n.headers.get("content-type");
1406
- if (!e?.startsWith(J))
1407
- throw new Error(`Expected content-type to be ${J}, Actual: ${e}`);
1406
+ if (!e?.startsWith(z))
1407
+ throw new Error(`Expected content-type to be ${z}, Actual: ${e}`);
1408
1408
  }
1409
- function zt(n) {
1409
+ function Jt(n) {
1410
1410
  const { endpoint: e, apiKey: t, payload: r, debugMode: o, customHeaders: i } = n;
1411
1411
  return new g((s) => {
1412
1412
  const a = new AbortController();
@@ -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,30 +1542,56 @@ class sn {
1538
1542
  }
1539
1543
  }
1540
1544
  fetchSse(e, t) {
1541
- t?.onSseStart?.(), zt({
1545
+ t?.onSseStart?.(), this.inFlight += 1, Jt({
1542
1546
  apiKey: this.apiKey,
1543
1547
  endpoint: this.endpoint,
1544
1548
  debugMode: this.debugMode,
1545
1549
  payload: this.transformSsePayload?.(e) ?? e,
1546
1550
  customHeaders: this.customHeaders
1547
1551
  }).pipe(
1548
- kt((r) => Et(r).pipe(Ht(t?.delayTime ?? 50))),
1549
- Ft(this.destroy$),
1550
- Gt(3)
1552
+ $t((r) => wt(r).pipe(Ht(t?.delayTime ?? 50))),
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
@@ -1591,14 +1621,45 @@ class sn {
1591
1621
  throw console.error("[AsgardServiceClient] File upload error:", s), s;
1592
1622
  }
1593
1623
  }
1624
+ /**
1625
+ * 下載 channel sandbox 工作目錄裡的檔案(cwd:// URI action)。
1626
+ * 呼叫 Edge Server GET <base>/cwd/download?custom_channel_id=xxx&relative_path=xxx 取回 binary。
1627
+ */
1628
+ async downloadCwdFile(e, t) {
1629
+ const r = this.getBaseEndpoint();
1630
+ if (!r)
1631
+ throw new Error("Unable to derive cwd download endpoint. Please provide botProviderEndpoint in config.");
1632
+ const o = `custom_channel_id=${encodeURIComponent(t)}&relative_path=${encodeURIComponent(e)}`, i = `${r}/cwd/download?${o}`, s = {
1633
+ ...this.customHeaders
1634
+ };
1635
+ this.apiKey && (s["X-API-KEY"] = this.apiKey);
1636
+ try {
1637
+ const a = await fetch(i, {
1638
+ method: "GET",
1639
+ headers: s
1640
+ });
1641
+ if (!a.ok)
1642
+ throw new Error(`CWD download failed: ${a.status} ${a.statusText}`);
1643
+ const c = await a.blob(), u = e.split("/").pop() || "download";
1644
+ return this.debugMode && console.log("[AsgardServiceClient] CWD download response:", { filename: u, size: c.size }), { blob: c, filename: u };
1645
+ } catch (a) {
1646
+ throw console.error("[AsgardServiceClient] CWD download error:", a), a;
1647
+ }
1648
+ }
1594
1649
  /**
1595
1650
  * 從 botProviderEndpoint 衍生 Blob API endpoint
1596
1651
  */
1597
1652
  deriveBlobEndpoint() {
1598
- if (!this.botProviderEndpoint && !this.endpoint)
1599
- return null;
1653
+ const e = this.getBaseEndpoint();
1654
+ return e ? `${e}/blob` : null;
1655
+ }
1656
+ /**
1657
+ * 衍生 bot provider 的 base endpoint(不含子路徑)。
1658
+ * 優先用 botProviderEndpoint;若只有 deprecated 的 endpoint 則反推(移除 /message/sse)。已去除尾部斜線。
1659
+ */
1660
+ getBaseEndpoint() {
1600
1661
  let e = this.botProviderEndpoint;
1601
- return !e && this.endpoint && (e = this.endpoint.replace("/message/sse", "")), e ? (e = e.replace(/\/+$/, ""), `${e}/blob`) : null;
1662
+ return !e && this.endpoint && (e = this.endpoint.replace("/message/sse", "")), e ? e.replace(/\/+$/, "") : null;
1602
1663
  }
1603
1664
  }
1604
1665
  const b = [];
@@ -1607,15 +1668,15 @@ for (let n = 0; n < 256; ++n)
1607
1668
  function Zt(n, e = 0) {
1608
1669
  return (b[n[e + 0]] + b[n[e + 1]] + b[n[e + 2]] + b[n[e + 3]] + "-" + b[n[e + 4]] + b[n[e + 5]] + "-" + b[n[e + 6]] + b[n[e + 7]] + "-" + b[n[e + 8]] + b[n[e + 9]] + "-" + b[n[e + 10]] + b[n[e + 11]] + b[n[e + 12]] + b[n[e + 13]] + b[n[e + 14]] + b[n[e + 15]]).toLowerCase();
1609
1670
  }
1610
- let K;
1671
+ let Y;
1611
1672
  const en = new Uint8Array(16);
1612
1673
  function tn() {
1613
- if (!K) {
1674
+ if (!Y) {
1614
1675
  if (typeof crypto > "u" || !crypto.getRandomValues)
1615
1676
  throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");
1616
- K = crypto.getRandomValues.bind(crypto);
1677
+ Y = crypto.getRandomValues.bind(crypto);
1617
1678
  }
1618
- return K(en);
1679
+ return Y(en);
1619
1680
  }
1620
1681
  const nn = typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto), fe = { randomUUID: nn };
1621
1682
  function rn(n, e, t) {
@@ -1625,7 +1686,7 @@ function rn(n, e, t) {
1625
1686
  throw new Error("Random bytes length must be >= 16");
1626
1687
  return r[6] = r[6] & 15 | 64, r[8] = r[8] & 63 | 128, Zt(r);
1627
1688
  }
1628
- function ke(n, e, t) {
1689
+ function $e(n, e, t) {
1629
1690
  return fe.randomUUID && !n ? fe.randomUUID() : rn(n);
1630
1691
  }
1631
1692
  class S {
@@ -1706,7 +1767,7 @@ class S {
1706
1767
  }), new S({ messages: r, pendingConsent: this.pendingConsent });
1707
1768
  }
1708
1769
  onMessageError(e) {
1709
- const t = ke(), r = e.fact.runError.error, o = new Map(this.messages);
1770
+ const t = $e(), r = e.fact.runError.error, o = new Map(this.messages);
1710
1771
  return o.set(t, {
1711
1772
  type: "error",
1712
1773
  eventType: d.ERROR,
@@ -1752,7 +1813,7 @@ class S {
1752
1813
  return new S({ messages: this.messages, pendingConsent: t });
1753
1814
  }
1754
1815
  }
1755
- class z {
1816
+ class J {
1756
1817
  client;
1757
1818
  customChannelId;
1758
1819
  customMessageId;
@@ -1774,11 +1835,11 @@ class z {
1774
1835
  this.client = e.client, this.customChannelId = e.customChannelId, this.customMessageId = e.customMessageId, this.isConnecting$ = new ie(!1), this.conversation$ = new ie(e.conversation), this.statesObserver = e.statesObserver;
1775
1836
  }
1776
1837
  static create(e) {
1777
- const t = new z(e);
1838
+ const t = new J(e);
1778
1839
  return t.subscribe(), t;
1779
1840
  }
1780
1841
  static async reset(e, t, r) {
1781
- const o = new z(e);
1842
+ const o = new J(e);
1782
1843
  try {
1783
1844
  return o.subscribe(), await o.resetChannel(t, r), o;
1784
1845
  } catch (i) {
@@ -1787,7 +1848,7 @@ class z {
1787
1848
  }
1788
1849
  subscribe() {
1789
1850
  this.statesSubscription = Rt([this.isConnecting$, this.conversation$]).pipe(
1790
- F(([e, t]) => ({
1851
+ G(([e, t]) => ({
1791
1852
  isConnecting: e,
1792
1853
  conversation: t
1793
1854
  }))
@@ -1833,7 +1894,7 @@ class z {
1833
1894
  resetChannel(e, t) {
1834
1895
  return this.fetchSse(
1835
1896
  {
1836
- action: $.RESET_CHANNEL,
1897
+ action: k.RESET_CHANNEL,
1837
1898
  customChannelId: this.customChannelId,
1838
1899
  customMessageId: this.customMessageId,
1839
1900
  text: e?.text || "",
@@ -1843,7 +1904,7 @@ class z {
1843
1904
  );
1844
1905
  }
1845
1906
  sendMessage(e, t) {
1846
- const r = e.text.trim(), o = e.customMessageId ?? ke();
1907
+ const r = e.text.trim(), o = e.customMessageId ?? $e();
1847
1908
  return this.currentUserMessageId = o, this.lastSentMessageId = o, this.conversation$.next(
1848
1909
  this.conversation$.value.pushMessage({
1849
1910
  type: "user",
@@ -1856,7 +1917,7 @@ class z {
1856
1917
  })
1857
1918
  ), this.fetchSse(
1858
1919
  {
1859
- action: $.NONE,
1920
+ action: k.NONE,
1860
1921
  customChannelId: this.customChannelId,
1861
1922
  customMessageId: o,
1862
1923
  payload: this.resolvePayload(e?.payload),
@@ -1869,7 +1930,7 @@ class z {
1869
1930
  replyToolCallConsents(e, t, r) {
1870
1931
  return this.conversation$.next(this.conversation$.value.clearPendingConsent()), this.fetchSse(
1871
1932
  {
1872
- action: $.RESPONSE_TOOL_CALL_CONSENT,
1933
+ action: k.RESPONSE_TOOL_CALL_CONSENT,
1873
1934
  customChannelId: this.customChannelId,
1874
1935
  customMessageId: this.lastSentMessageId ?? this.customMessageId,
1875
1936
  payload: this.resolvePayload(r),
@@ -1885,13 +1946,13 @@ class z {
1885
1946
  }
1886
1947
  export {
1887
1948
  sn as AsgardServiceClient,
1888
- z as Channel,
1949
+ J as Channel,
1889
1950
  S as Conversation,
1890
1951
  d as EventType,
1891
- $ as FetchSseAction,
1952
+ k as FetchSseAction,
1892
1953
  de as HttpError,
1893
1954
  He as MessageTemplateType,
1894
- De as ToolCallConsentResult,
1955
+ je as ToolCallConsentResult,
1895
1956
  on as isHttpError
1896
1957
  };
1897
1958
  //# sourceMappingURL=index.mjs.map