@asgard-js/core 0.1.4 → 0.1.6-canary.1
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.cjs +4 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +877 -849
- package/dist/index.mjs.map +1 -1
- package/dist/lib/channel.d.ts.map +1 -1
- package/dist/lib/conversation.d.ts.map +1 -1
- package/package.json +3 -2
package/dist/index.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
"use strict";var
|
|
2
|
-
`+
|
|
3
|
-
`):"",this.name="UnsubscriptionError",this.errors=e}});function N(r,t){if(r){var e=r.indexOf(t);0<=e&&r.splice(e,1)}}var k=(function(){function r(t){this.initialTeardown=t,this.closed=!1,this._parentage=null,this._finalizers=null}return r.prototype.unsubscribe=function(){var t,e,n,i,o;if(!this.closed){this.closed=!0;var s=this._parentage;if(s)if(this._parentage=null,Array.isArray(s))try{for(var a=M(s),c=a.next();!c.done;c=a.next()){var u=c.value;u.remove(this)}}catch(h){t={error:h}}finally{try{c&&!c.done&&(e=a.return)&&e.call(a)}finally{if(t)throw t.error}}else s.remove(this);var l=this.initialTeardown;if(b(l))try{l()}catch(h){o=h instanceof B?h.errors:[h]}var d=this._finalizers;if(d){this._finalizers=null;try{for(var y=M(d),f=y.next();!f.done;f=y.next()){var p=f.value;try{te(p)}catch(h){o=o??[],h instanceof B?o=R(R([],L(o)),L(h.errors)):o.push(h)}}}catch(h){n={error:h}}finally{try{f&&!f.done&&(i=y.return)&&i.call(y)}finally{if(n)throw n.error}}}if(o)throw new B(o)}},r.prototype.add=function(t){var e;if(t&&t!==this)if(this.closed)te(t);else{if(t instanceof r){if(t.closed||t._hasParent(this))return;t._addParent(this)}(this._finalizers=(e=this._finalizers)!==null&&e!==void 0?e:[]).push(t)}},r.prototype._hasParent=function(t){var e=this._parentage;return e===t||Array.isArray(e)&&e.includes(t)},r.prototype._addParent=function(t){var e=this._parentage;this._parentage=Array.isArray(e)?(e.push(t),e):e?[e,t]:t},r.prototype._removeParent=function(t){var e=this._parentage;e===t?this._parentage=null:Array.isArray(e)&&N(e,t)},r.prototype.remove=function(t){var e=this._finalizers;e&&N(e,t),t instanceof r&&t._removeParent(this)},r.EMPTY=(function(){var t=new r;return t.closed=!0,t})(),r})(),he=k.EMPTY;function ve(r){return r instanceof k||r&&"closed"in r&&b(r.remove)&&b(r.add)&&b(r.unsubscribe)}function te(r){b(r)?r():r.unsubscribe()}var Fe={Promise:void 0},He={setTimeout:function(r,t){for(var e=[],n=2;n<arguments.length;n++)e[n-2]=arguments[n];return setTimeout.apply(void 0,R([r,t],L(e)))},clearTimeout:function(r){return clearTimeout(r)},delegate:void 0};function ye(r){He.setTimeout(function(){throw r})}function Y(){}function $(r){r()}var J=(function(r){I(t,r);function t(e){var n=r.call(this)||this;return n.isStopped=!1,e?(n.destination=e,ve(e)&&e.add(n)):n.destination=Be,n}return t.create=function(e,n,i){return new W(e,n,i)},t.prototype.next=function(e){this.isStopped||this._next(e)},t.prototype.error=function(e){this.isStopped||(this.isStopped=!0,this._error(e))},t.prototype.complete=function(){this.isStopped||(this.isStopped=!0,this._complete())},t.prototype.unsubscribe=function(){this.closed||(this.isStopped=!0,r.prototype.unsubscribe.call(this),this.destination=null)},t.prototype._next=function(e){this.destination.next(e)},t.prototype._error=function(e){try{this.destination.error(e)}finally{this.unsubscribe()}},t.prototype._complete=function(){try{this.destination.complete()}finally{this.unsubscribe()}},t})(k),Ve=(function(){function r(t){this.partialObserver=t}return r.prototype.next=function(t){var e=this.partialObserver;if(e.next)try{e.next(t)}catch(n){j(n)}},r.prototype.error=function(t){var e=this.partialObserver;if(e.error)try{e.error(t)}catch(n){j(n)}else j(t)},r.prototype.complete=function(){var t=this.partialObserver;if(t.complete)try{t.complete()}catch(e){j(e)}},r})(),W=(function(r){I(t,r);function t(e,n,i){var o=r.call(this)||this,s;return b(e)||!e?s={next:e??void 0,error:n??void 0,complete:i??void 0}:s=e,o.destination=new Ve(s),o}return t})(J);function j(r){ye(r)}function Ke(r){throw r}var Be={closed:!0,next:Y,error:Ke,complete:Y},z=(function(){return typeof Symbol=="function"&&Symbol.observable||"@@observable"})();function F(r){return r}function qe(r){return r.length===0?F:r.length===1?r[0]:function(e){return r.reduce(function(n,i){return i(n)},e)}}var g=(function(){function r(t){t&&(this._subscribe=t)}return r.prototype.lift=function(t){var e=new r;return e.source=this,e.operator=t,e},r.prototype.subscribe=function(t,e,n){var i=this,o=We(t)?t:new W(t,e,n);return $(function(){var s=i,a=s.operator,c=s.source;o.add(a?a.call(o,c):c?i._subscribe(o):i._trySubscribe(o))}),o},r.prototype._trySubscribe=function(t){try{return this._subscribe(t)}catch(e){t.error(e)}},r.prototype.forEach=function(t,e){var n=this;return e=re(e),new e(function(i,o){var s=new W({next:function(a){try{t(a)}catch(c){o(c),s.unsubscribe()}},error:o,complete:i});n.subscribe(s)})},r.prototype._subscribe=function(t){var e;return(e=this.source)===null||e===void 0?void 0:e.subscribe(t)},r.prototype[z]=function(){return this},r.prototype.pipe=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return qe(t)(this)},r.prototype.toPromise=function(t){var e=this;return t=re(t),new t(function(n,i){var o;e.subscribe(function(s){return o=s},function(s){return i(s)},function(){return n(o)})})},r.create=function(t){return new r(t)},r})();function re(r){var t;return(t=r??Fe.Promise)!==null&&t!==void 0?t:Promise}function Ye(r){return r&&b(r.next)&&b(r.error)&&b(r.complete)}function We(r){return r&&r instanceof J||Ye(r)&&ve(r)}function Xe(r){return b(r==null?void 0:r.lift)}function T(r){return function(t){if(Xe(t))return t.lift(function(e){try{return r(e,this)}catch(n){this.error(n)}});throw new TypeError("Unable to lift unknown Observable type")}}function E(r,t,e,n,i){return new Je(r,t,e,n,i)}var Je=(function(r){I(t,r);function t(e,n,i,o,s,a){var c=r.call(this,e)||this;return c.onFinalize=s,c.shouldUnsubscribe=a,c._next=n?function(u){try{n(u)}catch(l){e.error(l)}}:r.prototype._next,c._error=o?function(u){try{o(u)}catch(l){e.error(l)}finally{this.unsubscribe()}}:r.prototype._error,c._complete=i?function(){try{i()}catch(u){e.error(u)}finally{this.unsubscribe()}}:r.prototype._complete,c}return t.prototype.unsubscribe=function(){var e;if(!this.shouldUnsubscribe||this.shouldUnsubscribe()){var n=this.closed;r.prototype.unsubscribe.call(this),!n&&((e=this.onFinalize)===null||e===void 0||e.call(this))}},t})(J),ze=de(function(r){return function(){r(this),this.name="ObjectUnsubscribedError",this.message="object unsubscribed"}}),Q=(function(r){I(t,r);function t(){var e=r.call(this)||this;return e.closed=!1,e.currentObservers=null,e.observers=[],e.isStopped=!1,e.hasError=!1,e.thrownError=null,e}return t.prototype.lift=function(e){var n=new ne(this,this);return n.operator=e,n},t.prototype._throwIfClosed=function(){if(this.closed)throw new ze},t.prototype.next=function(e){var n=this;$(function(){var i,o;if(n._throwIfClosed(),!n.isStopped){n.currentObservers||(n.currentObservers=Array.from(n.observers));try{for(var s=M(n.currentObservers),a=s.next();!a.done;a=s.next()){var c=a.value;c.next(e)}}catch(u){i={error:u}}finally{try{a&&!a.done&&(o=s.return)&&o.call(s)}finally{if(i)throw i.error}}}})},t.prototype.error=function(e){var n=this;$(function(){if(n._throwIfClosed(),!n.isStopped){n.hasError=n.isStopped=!0,n.thrownError=e;for(var i=n.observers;i.length;)i.shift().error(e)}})},t.prototype.complete=function(){var e=this;$(function(){if(e._throwIfClosed(),!e.isStopped){e.isStopped=!0;for(var n=e.observers;n.length;)n.shift().complete()}})},t.prototype.unsubscribe=function(){this.isStopped=this.closed=!0,this.observers=this.currentObservers=null},Object.defineProperty(t.prototype,"observed",{get:function(){var e;return((e=this.observers)===null||e===void 0?void 0:e.length)>0},enumerable:!1,configurable:!0}),t.prototype._trySubscribe=function(e){return this._throwIfClosed(),r.prototype._trySubscribe.call(this,e)},t.prototype._subscribe=function(e){return this._throwIfClosed(),this._checkFinalizedStatuses(e),this._innerSubscribe(e)},t.prototype._innerSubscribe=function(e){var n=this,i=this,o=i.hasError,s=i.isStopped,a=i.observers;return o||s?he:(this.currentObservers=null,a.push(e),new k(function(){n.currentObservers=null,N(a,e)}))},t.prototype._checkFinalizedStatuses=function(e){var n=this,i=n.hasError,o=n.thrownError,s=n.isStopped;i?e.error(o):s&&e.complete()},t.prototype.asObservable=function(){var e=new g;return e.source=this,e},t.create=function(e,n){return new ne(e,n)},t})(g),ne=(function(r){I(t,r);function t(e,n){var i=r.call(this)||this;return i.destination=e,i.source=n,i}return t.prototype.next=function(e){var n,i;(i=(n=this.destination)===null||n===void 0?void 0:n.next)===null||i===void 0||i.call(n,e)},t.prototype.error=function(e){var n,i;(i=(n=this.destination)===null||n===void 0?void 0:n.error)===null||i===void 0||i.call(n,e)},t.prototype.complete=function(){var e,n;(n=(e=this.destination)===null||e===void 0?void 0:e.complete)===null||n===void 0||n.call(e)},t.prototype._subscribe=function(e){var n,i;return(i=(n=this.source)===null||n===void 0?void 0:n.subscribe(e))!==null&&i!==void 0?i:he},t})(Q),ie=(function(r){I(t,r);function t(e){var n=r.call(this)||this;return n._value=e,n}return Object.defineProperty(t.prototype,"value",{get:function(){return this.getValue()},enumerable:!1,configurable:!0}),t.prototype._subscribe=function(e){var n=r.prototype._subscribe.call(this,e);return!n.closed&&e.next(this._value),n},t.prototype.getValue=function(){var e=this,n=e.hasError,i=e.thrownError,o=e._value;if(n)throw i;return this._throwIfClosed(),o},t.prototype.next=function(e){r.prototype.next.call(this,this._value=e)},t})(Q),Qe={now:function(){return Date.now()}},Ze=(function(r){I(t,r);function t(e,n){return r.call(this)||this}return t.prototype.schedule=function(e,n){return this},t})(k),oe={setInterval:function(r,t){for(var e=[],n=2;n<arguments.length;n++)e[n-2]=arguments[n];return setInterval.apply(void 0,R([r,t],L(e)))},clearInterval:function(r){return clearInterval(r)},delegate:void 0},et=(function(r){I(t,r);function t(e,n){var i=r.call(this,e,n)||this;return i.scheduler=e,i.work=n,i.pending=!1,i}return t.prototype.schedule=function(e,n){var i;if(n===void 0&&(n=0),this.closed)return this;this.state=e;var o=this.id,s=this.scheduler;return o!=null&&(this.id=this.recycleAsyncId(s,o,n)),this.pending=!0,this.delay=n,this.id=(i=this.id)!==null&&i!==void 0?i:this.requestAsyncId(s,this.id,n),this},t.prototype.requestAsyncId=function(e,n,i){return i===void 0&&(i=0),oe.setInterval(e.flush.bind(e,this),i)},t.prototype.recycleAsyncId=function(e,n,i){if(i===void 0&&(i=0),i!=null&&this.delay===i&&this.pending===!1)return n;n!=null&&oe.clearInterval(n)},t.prototype.execute=function(e,n){if(this.closed)return new Error("executing a cancelled action");this.pending=!1;var i=this._execute(e,n);if(i)return i;this.pending===!1&&this.id!=null&&(this.id=this.recycleAsyncId(this.scheduler,this.id,null))},t.prototype._execute=function(e,n){var i=!1,o;try{this.work(e)}catch(s){i=!0,o=s||new Error("Scheduled action threw falsy error")}if(i)return this.unsubscribe(),o},t.prototype.unsubscribe=function(){if(!this.closed){var e=this,n=e.id,i=e.scheduler,o=i.actions;this.work=this.state=this.scheduler=null,this.pending=!1,N(o,this),n!=null&&(this.id=this.recycleAsyncId(i,n,null)),this.delay=null,r.prototype.unsubscribe.call(this)}},t})(Ze),se=(function(){function r(t,e){e===void 0&&(e=r.now),this.schedulerActionCtor=t,this.now=e}return r.prototype.schedule=function(t,e,n){return e===void 0&&(e=0),new this.schedulerActionCtor(this,t).schedule(n,e)},r.now=Qe.now,r})(),tt=(function(r){I(t,r);function t(e,n){n===void 0&&(n=se.now);var i=r.call(this,e,n)||this;return i.actions=[],i._active=!1,i}return t.prototype.flush=function(e){var n=this.actions;if(this._active){n.push(e);return}var i;this._active=!0;do if(i=e.execute(e.state,e.delay))break;while(e=n.shift());if(this._active=!1,i){for(;e=n.shift();)e.unsubscribe();throw i}},t})(se),pe=new tt(et),rt=pe,nt=new g(function(r){return r.complete()});function be(r){return r&&b(r.schedule)}function me(r){return r[r.length-1]}function it(r){return b(me(r))?r.pop():void 0}function ge(r){return be(me(r))?r.pop():void 0}var Se=(function(r){return r&&typeof r.length=="number"&&typeof r!="function"});function we(r){return b(r==null?void 0:r.then)}function Ee(r){return b(r[z])}function Ie(r){return Symbol.asyncIterator&&b(r==null?void 0:r[Symbol.asyncIterator])}function Oe(r){return new TypeError("You provided "+(r!==null&&typeof r=="object"?"an invalid object":"'"+r+"'")+" where a stream was expected. You can provide an Observable, Promise, ReadableStream, Array, AsyncIterable, or Iterable.")}function ot(){return typeof Symbol!="function"||!Symbol.iterator?"@@iterator":Symbol.iterator}var Ae=ot();function _e(r){return b(r==null?void 0:r[Ae])}function xe(r){return Ne(this,arguments,function(){var e,n,i,o;return fe(this,function(s){switch(s.label){case 0:e=r.getReader(),s.label=1;case 1:s.trys.push([1,,9,10]),s.label=2;case 2:return[4,C(e.read())];case 3:return n=s.sent(),i=n.value,o=n.done,o?[4,C(void 0)]:[3,5];case 4:return[2,s.sent()];case 5:return[4,C(i)];case 6:return[4,s.sent()];case 7:return s.sent(),[3,2];case 8:return[3,10];case 9:return e.releaseLock(),[7];case 10:return[2]}})})}function Pe(r){return b(r==null?void 0:r.getReader)}function x(r){if(r instanceof g)return r;if(r!=null){if(Ee(r))return st(r);if(Se(r))return at(r);if(we(r))return ct(r);if(Ie(r))return Te(r);if(_e(r))return ut(r);if(Pe(r))return lt(r)}throw Oe(r)}function st(r){return new g(function(t){var e=r[z]();if(b(e.subscribe))return e.subscribe(t);throw new TypeError("Provided object does not correctly implement Symbol.observable")})}function at(r){return new g(function(t){for(var e=0;e<r.length&&!t.closed;e++)t.next(r[e]);t.complete()})}function ct(r){return new g(function(t){r.then(function(e){t.closed||(t.next(e),t.complete())},function(e){return t.error(e)}).then(null,ye)})}function ut(r){return new g(function(t){var e,n;try{for(var i=M(r),o=i.next();!o.done;o=i.next()){var s=o.value;if(t.next(s),t.closed)return}}catch(a){e={error:a}}finally{try{o&&!o.done&&(n=i.return)&&n.call(i)}finally{if(e)throw e.error}}t.complete()})}function Te(r){return new g(function(t){ft(r,t).catch(function(e){return t.error(e)})})}function lt(r){return Te(xe(r))}function ft(r,t){var e,n,i,o;return De(this,void 0,void 0,function(){var s,a;return fe(this,function(c){switch(c.label){case 0:c.trys.push([0,5,6,11]),e=Ge(r),c.label=1;case 1:return[4,e.next()];case 2:if(n=c.sent(),!!n.done)return[3,4];if(s=n.value,t.next(s),t.closed)return[2];c.label=3;case 3:return[3,1];case 4:return[3,11];case 5:return a=c.sent(),i={error:a},[3,11];case 6:return c.trys.push([6,,9,10]),n&&!n.done&&(o=e.return)?[4,o.call(e)]:[3,8];case 7:c.sent(),c.label=8;case 8:return[3,10];case 9:if(i)throw i.error;return[7];case 10:return[7];case 11:return t.complete(),[2]}})})}function _(r,t,e,n,i){n===void 0&&(n=0),i===void 0&&(i=!1);var o=t.schedule(function(){e(),i?r.add(this.schedule(null,n)):this.unsubscribe()},n);if(r.add(o),!i)return o}function Ce(r,t){return t===void 0&&(t=0),T(function(e,n){e.subscribe(E(n,function(i){return _(n,r,function(){return n.next(i)},t)},function(){return _(n,r,function(){return n.complete()},t)},function(i){return _(n,r,function(){return n.error(i)},t)}))})}function Me(r,t){return t===void 0&&(t=0),T(function(e,n){n.add(r.schedule(function(){return e.subscribe(n)},t))})}function dt(r,t){return x(r).pipe(Me(t),Ce(t))}function ht(r,t){return x(r).pipe(Me(t),Ce(t))}function vt(r,t){return new g(function(e){var n=0;return t.schedule(function(){n===r.length?e.complete():(e.next(r[n++]),e.closed||this.schedule())})})}function yt(r,t){return new g(function(e){var n;return _(e,t,function(){n=r[Ae](),_(e,t,function(){var i,o,s;try{i=n.next(),o=i.value,s=i.done}catch(a){e.error(a);return}s?e.complete():e.next(o)},0,!0)}),function(){return b(n==null?void 0:n.return)&&n.return()}})}function Le(r,t){if(!r)throw new Error("Iterable cannot be null");return new g(function(e){_(e,t,function(){var n=r[Symbol.asyncIterator]();_(e,t,function(){n.next().then(function(i){i.done?e.complete():e.next(i.value)})},0,!0)})})}function pt(r,t){return Le(xe(r),t)}function bt(r,t){if(r!=null){if(Ee(r))return dt(r,t);if(Se(r))return vt(r,t);if(we(r))return ht(r,t);if(Ie(r))return Le(r,t);if(_e(r))return yt(r,t);if(Pe(r))return pt(r,t)}throw Oe(r)}function Z(r,t){return t?bt(r,t):x(r)}function mt(){for(var r=[],t=0;t<arguments.length;t++)r[t]=arguments[t];var e=ge(r);return Z(r,e)}function gt(r){return r instanceof Date&&!isNaN(r)}function H(r,t){return T(function(e,n){var i=0;e.subscribe(E(n,function(o){n.next(r.call(t,o,i++))}))})}var St=Array.isArray;function wt(r,t){return St(t)?r.apply(void 0,R([],L(t))):r(t)}function Et(r){return H(function(t){return wt(r,t)})}var It=Array.isArray,Ot=Object.getPrototypeOf,At=Object.prototype,_t=Object.keys;function xt(r){if(r.length===1){var t=r[0];if(It(t))return{args:t,keys:null};if(Pt(t)){var e=_t(t);return{args:e.map(function(n){return t[n]}),keys:e}}}return{args:r,keys:null}}function Pt(r){return r&&typeof r=="object"&&Ot(r)===At}function Tt(r,t){return r.reduce(function(e,n,i){return e[n]=t[i],e},{})}function Ct(){for(var r=[],t=0;t<arguments.length;t++)r[t]=arguments[t];var e=ge(r),n=it(r),i=xt(r),o=i.args,s=i.keys;if(o.length===0)return Z([],e);var a=new g(Mt(o,e,s?function(c){return Tt(s,c)}:F));return n?a.pipe(Et(n)):a}function Mt(r,t,e){return e===void 0&&(e=F),function(n){ae(t,function(){for(var i=r.length,o=new Array(i),s=i,a=i,c=function(l){ae(t,function(){var d=Z(r[l],t),y=!1;d.subscribe(E(n,function(f){o[l]=f,y||(y=!0,a--),a||n.next(e(o.slice()))},function(){--s||n.complete()}))},n)},u=0;u<i;u++)c(u)},n)}}function ae(r,t,e){r?_(e,r,t):t()}function Lt(r,t,e,n,i,o,s,a){var c=[],u=0,l=0,d=!1,y=function(){d&&!c.length&&!u&&t.complete()},f=function(h){return u<n?p(h):c.push(h)},p=function(h){u++;var S=!1;x(e(h,l++)).subscribe(E(t,function(w){t.next(w)},function(){S=!0},void 0,function(){if(S)try{u--;for(var w=function(){var P=c.shift();s||p(P)};c.length&&u<n;)w();y()}catch(P){t.error(P)}}))};return r.subscribe(E(t,f,function(){d=!0,y()})),function(){}}function G(r,t,e){return e===void 0&&(e=1/0),b(t)?G(function(n,i){return H(function(o,s){return t(n,o,i,s)})(x(r(n,i)))},e):(typeof t=="number"&&(e=t),T(function(n,i){return Lt(n,i,r,e)}))}function Re(r,t,e){r===void 0&&(r=0),e===void 0&&(e=rt);var n=-1;return t!=null&&(be(t)?e=t:n=t),new g(function(i){var o=gt(r)?+r-e.now():r;o<0&&(o=0);var s=0;return e.schedule(function(){i.closed||(i.next(s++),0<=n?this.schedule(void 0,n):i.complete())},o)})}function Rt(r,t){return b(t)?G(r,t,1):G(r,1)}function kt(r){return r<=0?function(){return nt}:T(function(t,e){var n=0;t.subscribe(E(e,function(i){++n<=r&&(e.next(i),r<=n&&e.complete())}))})}function Ut(r){return H(function(){return r})}function jt(r,t){return G(function(e,n){return x(r(e,n)).pipe(kt(1),Ut(e))})}function $t(r,t){t===void 0&&(t=pe);var e=Re(r,t);return jt(function(){return e})}function Dt(r){var t;t={count:r};var e=t.count,n=e===void 0?1/0:e,i=t.delay,o=t.resetOnSuccess,s=o===void 0?!1:o;return n<=0?F:T(function(a,c){var u=0,l,d=function(){var y=!1;l=a.subscribe(E(c,function(f){s&&(u=0),c.next(f)},void 0,function(f){if(u++<n){var p=function(){l?(l.unsubscribe(),l=null,d()):y=!0};if(i!=null){var h=typeof i=="number"?Re(i):x(i(f,u)),S=E(c,function(){S.unsubscribe(),p()},function(){c.complete()});h.subscribe(S)}else p()}else c.error(f)})),y&&(l.unsubscribe(),l=null,d())};d()})}function Nt(r){return T(function(t,e){x(r).subscribe(E(e,function(){return e.complete()},Y)),!e.closed&&t.subscribe(e)})}async function Gt(r,t){const e=r.getReader();let n;for(;!(n=await e.read()).done;)t(n.value)}function Ft(r){let t,e,n,i=!1;return function(s){t===void 0?(t=s,e=0,n=-1):t=Vt(t,s);const a=t.length;let c=0;for(;e<a;){i&&(t[e]===10&&(c=++e),i=!1);let u=-1;for(;e<a&&u===-1;++e)switch(t[e]){case 58:n===-1&&(n=e-c);break;case 13:i=!0;case 10:u=e;break}if(u===-1)break;r(t.subarray(c,u),n),c=e,n=-1}c===a?t=void 0:c!==0&&(t=t.subarray(c),e-=c)}}function Ht(r,t,e){let n=ce();const i=new TextDecoder;return function(s,a){if(s.length===0)e==null||e(n),n=ce();else if(a>0){const c=i.decode(s.subarray(0,a)),u=a+(s[a+1]===32?2:1),l=i.decode(s.subarray(u));switch(c){case"data":n.data=n.data?n.data+`
|
|
4
|
-
`+l:l;break;case"event":n.event=l;break;case"id":r(n.id=l);break;case"retry":const d=parseInt(l,10);isNaN(d)||t(n.retry=d);break}}}}function Vt(r,t){const e=new Uint8Array(r.length+t.length);return e.set(r),e.set(t,r.length),e}function ce(){return{data:"",event:"",id:"",retry:void 0}}var Kt=function(r,t){var e={};for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&t.indexOf(n)<0&&(e[n]=r[n]);if(r!=null&&typeof Object.getOwnPropertySymbols=="function")for(var i=0,n=Object.getOwnPropertySymbols(r);i<n.length;i++)t.indexOf(n[i])<0&&Object.prototype.propertyIsEnumerable.call(r,n[i])&&(e[n[i]]=r[n[i]]);return e};const X="text/event-stream",Bt=1e3,ue="last-event-id";function qt(r,t){var{signal:e,headers:n,onopen:i,onmessage:o,onclose:s,onerror:a,openWhenHidden:c,fetch:u}=t,l=Kt(t,["signal","headers","onopen","onmessage","onclose","onerror","openWhenHidden","fetch"]);return new Promise((d,y)=>{const f=Object.assign({},n);f.accept||(f.accept=X);let p;function h(){p.abort(),document.hidden||V()}c||document.addEventListener("visibilitychange",h);let S=Bt,w=0;function P(){document.removeEventListener("visibilitychange",h),window.clearTimeout(w),p.abort()}e==null||e.addEventListener("abort",()=>{P(),d()});const ke=u??window.fetch,Ue=i??Yt;async function V(){var K;p=new AbortController;try{const U=await ke(r,Object.assign(Object.assign({},l),{headers:f,signal:p.signal}));await Ue(U),await Gt(U.body,Ft(Ht(O=>{O?f[ue]=O:delete f[ue]},O=>{S=O},o))),s==null||s(),P(),d()}catch(U){if(!p.signal.aborted)try{const O=(K=a==null?void 0:a(U))!==null&&K!==void 0?K:S;window.clearTimeout(w),w=window.setTimeout(V,O)}catch(O){P(),y(O)}}}V()})}function Yt(r){const t=r.headers.get("content-type");if(!(t!=null&&t.startsWith(X)))throw new Error(`Expected content-type to be ${X}, Actual: ${t}`)}function Wt(r){const{endpoint:t,apiKey:e,payload:n,debugMode:i}=r;return new g(o=>{const s=new AbortController;let a;const c={"Content-Type":"application/json"};e&&(c["X-API-KEY"]=e);const u=new URLSearchParams;i&&u.set("is_debug","true");const l=new URL(t);return u.toString()&&(l.search=u.toString()),qt(l.toString(),{method:"POST",headers:c,body:n?JSON.stringify(n):void 0,signal:s.signal,openWhenHidden:!0,onopen:async d=>{d.ok?a=d.headers.get("X-Trace-Id")??void 0:(o.error(d),s.abort())},onmessage:d=>{const y=JSON.parse(d.data);a?y.traceId=a:y.requestId&&(y.traceId=y.requestId,a||(a=y.requestId)),o.next(y)},onclose:()=>{o.complete()},onerror:d=>{throw o.error(d),s.abort(),d}}),()=>{s.abort()}})}class Xt{constructor(){m(this,"listeners",{})}on(t,e){this.listeners=Object.assign({},this.listeners,{[t]:(this.listeners[t]??[]).concat(e)})}off(t,e){this.listeners[t]&&(this.listeners=Object.assign({},this.listeners,{[t]:(this.listeners[t]??[]).filter(n=>n!==e)}))}remove(t){delete this.listeners[t]}emit(t,...e){this.listeners[t]&&this.listeners[t].forEach(n=>n(...e))}}class Jt{constructor(t){m(this,"apiKey");m(this,"endpoint");m(this,"botProviderEndpoint");m(this,"debugMode");m(this,"destroy$",new Q);m(this,"sseEmitter",new Xt);m(this,"transformSsePayload");if(!t.endpoint&&!t.botProviderEndpoint)throw new Error("Either endpoint or botProviderEndpoint must be provided");if(this.apiKey=t.apiKey,this.debugMode=t.debugMode,this.transformSsePayload=t.transformSsePayload,this.botProviderEndpoint=t.botProviderEndpoint,!t.endpoint&&t.botProviderEndpoint){const e=t.botProviderEndpoint.replace(/\/+$/,"");this.endpoint=`${e}/message/sse`}else t.endpoint&&(this.endpoint=t.endpoint,this.debugMode&&console.warn('[AsgardServiceClient] The "endpoint" option is deprecated and will be removed in the next major version. Please use "botProviderEndpoint" instead. The SSE endpoint will be automatically derived as "${botProviderEndpoint}/message/sse".'))}on(t,e){this.sseEmitter.remove(t),this.sseEmitter.on(t,e)}handleEvent(t){switch(t.eventType){case v.INIT:this.sseEmitter.emit(v.INIT,t);break;case v.PROCESS_START:case v.PROCESS_COMPLETE:this.sseEmitter.emit(v.PROCESS,t);break;case v.MESSAGE_START:case v.MESSAGE_DELTA:case v.MESSAGE_COMPLETE:this.sseEmitter.emit(v.MESSAGE,t);break;case v.TOOL_CALL_START:case v.TOOL_CALL_COMPLETE:this.sseEmitter.emit(v.TOOL_CALL,t);break;case v.DONE:this.sseEmitter.emit(v.DONE,t);break;case v.ERROR:this.sseEmitter.emit(v.ERROR,t);break}}fetchSse(t,e){var n,i;(n=e==null?void 0:e.onSseStart)==null||n.call(e),Wt({apiKey:this.apiKey,endpoint:this.endpoint,debugMode:this.debugMode,payload:((i=this.transformSsePayload)==null?void 0:i.call(this,t))??t}).pipe(Rt(o=>mt(o).pipe($t((e==null?void 0:e.delayTime)??50))),Nt(this.destroy$),Dt(3)).subscribe({next:o=>{var s;(s=e==null?void 0:e.onSseMessage)==null||s.call(e,o),this.handleEvent(o)},error:o=>{var s;(s=e==null?void 0:e.onSseError)==null||s.call(e,o)},complete:()=>{var o;(o=e==null?void 0:e.onSseCompleted)==null||o.call(e)}})}close(){this.destroy$.next(),this.destroy$.complete()}async uploadFile(t,e){const n=this.deriveBlobEndpoint();if(!n)throw new Error("Unable to derive blob endpoint. Please provide botProviderEndpoint in config.");const i=new FormData;i.append("file",t),i.append("customChannelId",e);const o={};this.apiKey&&(o["X-API-KEY"]=this.apiKey);try{const s=await fetch(n,{method:"POST",headers:o,body:i});if(!s.ok)throw new Error(`Upload failed: ${s.status} ${s.statusText}`);const a=await s.json();return this.debugMode&&console.log("[AsgardServiceClient] File upload response:",a),a}catch(s){throw console.error("[AsgardServiceClient] File upload error:",s),s}}deriveBlobEndpoint(){if(!this.botProviderEndpoint&&!this.endpoint)return null;let t=this.botProviderEndpoint;if(!t&&this.endpoint&&(t=this.endpoint.replace("/message/sse","")),!t)return null;if(t=t.replace(/\/+$/,""),!t.includes("/generic/")){const e=t.match(/^(https?:\/\/[^/]+)(\/.*)/);if(e){const[,n,i]=e;t=`${n}/generic${i}`}}return`${t}/blob`}}class A{constructor({messages:t}){m(this,"messages",null);this.messages=t}pushMessage(t){const e=new Map(this.messages);return e.set(t.messageId,t),new A({messages:e})}onMessage(t){switch(t.eventType){case v.MESSAGE_START:return this.onMessageStart(t);case v.MESSAGE_DELTA:return this.onMessageDelta(t);case v.MESSAGE_COMPLETE:return this.onMessageComplete(t);case v.ERROR:return this.onMessageError(t);default:return this}}onMessageStart(t){const e=t.fact.messageStart.message,n=new Map(this.messages);return n.set(e.messageId,{type:"bot",eventType:v.MESSAGE_START,isTyping:!0,typingText:"",messageId:e.messageId,message:e,time:new Date,traceId:t.traceId}),new A({messages:n})}onMessageDelta(t){const e=t.fact.messageDelta.message,n=new Map(this.messages),i=n.get(e.messageId);if((i==null?void 0:i.type)!=="bot")return this;const o=`${(i==null?void 0:i.typingText)??""}${e.text}`;return n.set(e.messageId,{type:"bot",eventType:v.MESSAGE_DELTA,isTyping:!0,typingText:o,messageId:e.messageId,message:e,time:new Date,traceId:t.traceId??i.traceId}),new A({messages:n})}onMessageComplete(t){const e=t.fact.messageComplete.message,n=new Map(this.messages),i=n.get(e.messageId);return n.set(e.messageId,{type:"bot",eventType:v.MESSAGE_COMPLETE,isTyping:!1,typingText:null,messageId:e.messageId,message:e,time:new Date,traceId:t.traceId??((i==null?void 0:i.type)==="bot"?i.traceId:void 0)}),new A({messages:n})}onMessageError(t){const e=crypto.randomUUID(),n=t.fact.runError.error,i=new Map(this.messages);return i.set(e,{type:"error",eventType:v.ERROR,messageId:e,error:n,time:new Date,traceId:t.traceId}),new A({messages:i})}}class ee{constructor(t){m(this,"client");m(this,"customChannelId");m(this,"customMessageId");m(this,"isConnecting$");m(this,"conversation$");m(this,"statesObserver");m(this,"statesSubscription");m(this,"currentUserMessageId");if(!t.client)throw new Error("client must be required");if(!t.customChannelId)throw new Error("customChannelId must be required");this.client=t.client,this.customChannelId=t.customChannelId,this.customMessageId=t.customMessageId,this.isConnecting$=new ie(!1),this.conversation$=new ie(t.conversation),this.statesObserver=t.statesObserver}static async reset(t,e,n){const i=new ee(t);try{return i.subscribe(),await i.resetChannel(e,n),i}catch(o){throw i.close(),o}}subscribe(){this.statesSubscription=Ct([this.isConnecting$,this.conversation$]).pipe(H(([t,e])=>({isConnecting:t,conversation:e}))).subscribe(this.statesObserver)}fetchSse(t,e){return new Promise((n,i)=>{this.isConnecting$.next(!0),this.client.fetchSse(t,{onSseStart:e==null?void 0:e.onSseStart,onSseMessage:o=>{var s;if((s=e==null?void 0:e.onSseMessage)==null||s.call(e,o),this.currentUserMessageId&&o.traceId){const a=new Map(this.conversation$.value.messages),c=a.get(this.currentUserMessageId);c&&c.type==="user"&&(a.set(this.currentUserMessageId,{...c,traceId:o.traceId}),this.conversation$.next(new A({messages:a}))),this.currentUserMessageId=void 0}this.conversation$.next(this.conversation$.value.onMessage(o))},onSseError:o=>{var s;(s=e==null?void 0:e.onSseError)==null||s.call(e,o),this.isConnecting$.next(!1),this.currentUserMessageId=void 0,i(o)},onSseCompleted:()=>{var o;(o=e==null?void 0:e.onSseCompleted)==null||o.call(e),this.isConnecting$.next(!1),this.currentUserMessageId=void 0,n()}})})}resetChannel(t,e){return this.fetchSse({action:D.RESET_CHANNEL,customChannelId:this.customChannelId,customMessageId:this.customMessageId,text:(t==null?void 0:t.text)||"",payload:t==null?void 0:t.payload},e)}sendMessage(t,e){const n=t.text.trim(),i=t.customMessageId??crypto.randomUUID();return this.currentUserMessageId=i,this.conversation$.next(this.conversation$.value.pushMessage({type:"user",messageId:i,text:n,blobIds:t.blobIds,filePreviewUrls:t.filePreviewUrls,time:new Date})),this.fetchSse({action:D.NONE,customChannelId:this.customChannelId,customMessageId:i,payload:t==null?void 0:t.payload,text:n,blobIds:t==null?void 0:t.blobIds},e)}close(){var t;this.isConnecting$.complete(),this.conversation$.complete(),(t=this.statesSubscription)==null||t.unsubscribe()}}exports.AsgardServiceClient=Jt;exports.Channel=ee;exports.Conversation=A;exports.EventType=v;exports.FetchSseAction=D;exports.MessageTemplateType=le;
|
|
1
|
+
"use strict";var Ge=Object.defineProperty;var Fe=(r,e,t)=>e in r?Ge(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t;var m=(r,e,t)=>Fe(r,typeof e!="symbol"?e+"":e,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var N=(r=>(r.RESET_CHANNEL="RESET_CHANNEL",r.NONE="NONE",r))(N||{}),v=(r=>(r.INIT="asgard.run.init",r.PROCESS="asgard.process",r.PROCESS_START="asgard.process.start",r.PROCESS_COMPLETE="asgard.process.complete",r.MESSAGE="asgard.message",r.MESSAGE_START="asgard.message.start",r.MESSAGE_DELTA="asgard.message.delta",r.MESSAGE_COMPLETE="asgard.message.complete",r.TOOL_CALL="asgard.tool_call",r.TOOL_CALL_START="asgard.tool_call.start",r.TOOL_CALL_COMPLETE="asgard.tool_call.complete",r.DONE="asgard.run.done",r.ERROR="asgard.run.error",r))(v||{}),he=(r=>(r.TEXT="TEXT",r.HINT="HINT",r.BUTTON="BUTTON",r.IMAGE="IMAGE",r.VIDEO="VIDEO",r.AUDIO="AUDIO",r.LOCATION="LOCATION",r.CAROUSEL="CAROUSEL",r.CHART="CHART",r))(he||{}),W=function(r,e){return W=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,n){t.__proto__=n}||function(t,n){for(var i in n)Object.prototype.hasOwnProperty.call(n,i)&&(t[i]=n[i])},W(r,e)};function O(r,e){if(typeof e!="function"&&e!==null)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");W(r,e);function t(){this.constructor=r}r.prototype=e===null?Object.create(e):(t.prototype=e.prototype,new t)}function Ve(r,e,t,n){function i(o){return o instanceof t?o:new t(function(s){s(o)})}return new(t||(t=Promise))(function(o,s){function a(l){try{u(n.next(l))}catch(d){s(d)}}function c(l){try{u(n.throw(l))}catch(d){s(d)}}function u(l){l.done?o(l.value):i(l.value).then(a,c)}u((n=n.apply(r,e||[])).next())})}function ve(r,e){var t={label:0,sent:function(){if(o[0]&1)throw o[1];return o[1]},trys:[],ops:[]},n,i,o,s=Object.create((typeof Iterator=="function"?Iterator:Object).prototype);return s.next=a(0),s.throw=a(1),s.return=a(2),typeof Symbol=="function"&&(s[Symbol.iterator]=function(){return this}),s;function a(u){return function(l){return c([u,l])}}function c(u){if(n)throw new TypeError("Generator is already executing.");for(;s&&(s=0,u[0]&&(t=0)),t;)try{if(n=1,i&&(o=u[0]&2?i.return:u[0]?i.throw||((o=i.return)&&o.call(i),0):i.next)&&!(o=o.call(i,u[1])).done)return o;switch(i=0,o&&(u=[u[0]&2,o.value]),u[0]){case 0:case 1:o=u;break;case 4:return t.label++,{value:u[1],done:!1};case 5:t.label++,i=u[1],u=[0];continue;case 7:u=t.ops.pop(),t.trys.pop();continue;default:if(o=t.trys,!(o=o.length>0&&o[o.length-1])&&(u[0]===6||u[0]===2)){t=0;continue}if(u[0]===3&&(!o||u[1]>o[0]&&u[1]<o[3])){t.label=u[1];break}if(u[0]===6&&t.label<o[1]){t.label=o[1],o=u;break}if(o&&t.label<o[2]){t.label=o[2],t.ops.push(u);break}o[2]&&t.ops.pop(),t.trys.pop();continue}u=e.call(r,t)}catch(l){u=[6,l],i=0}finally{n=o=0}if(u[0]&5)throw u[1];return{value:u[0]?u[1]:void 0,done:!0}}}function R(r){var e=typeof Symbol=="function"&&Symbol.iterator,t=e&&r[e],n=0;if(t)return t.call(r);if(r&&typeof r.length=="number")return{next:function(){return r&&n>=r.length&&(r=void 0),{value:r&&r[n++],done:!r}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")}function L(r,e){var t=typeof Symbol=="function"&&r[Symbol.iterator];if(!t)return r;var n=t.call(r),i,o=[],s;try{for(;(e===void 0||e-- >0)&&!(i=n.next()).done;)o.push(i.value)}catch(a){s={error:a}}finally{try{i&&!i.done&&(t=n.return)&&t.call(n)}finally{if(s)throw s.error}}return o}function U(r,e,t){if(t||arguments.length===2)for(var n=0,i=e.length,o;n<i;n++)(o||!(n in e))&&(o||(o=Array.prototype.slice.call(e,0,n)),o[n]=e[n]);return r.concat(o||Array.prototype.slice.call(e))}function M(r){return this instanceof M?(this.v=r,this):new M(r)}function He(r,e,t){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var n=t.apply(r,e||[]),i,o=[];return i=Object.create((typeof AsyncIterator=="function"?AsyncIterator:Object).prototype),a("next"),a("throw"),a("return",s),i[Symbol.asyncIterator]=function(){return this},i;function s(f){return function(p){return Promise.resolve(p).then(f,d)}}function a(f,p){n[f]&&(i[f]=function(h){return new Promise(function(w,E){o.push([f,h,w,E])>1||c(f,h)})},p&&(i[f]=p(i[f])))}function c(f,p){try{u(n[f](p))}catch(h){y(o[0][3],h)}}function u(f){f.value instanceof M?Promise.resolve(f.value.v).then(l,d):y(o[0][2],f)}function l(f){c("next",f)}function d(f){c("throw",f)}function y(f,p){f(p),o.shift(),o.length&&c(o[0][0],o[0][1])}}function Ke(r){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var e=r[Symbol.asyncIterator],t;return e?e.call(r):(r=typeof R=="function"?R(r):r[Symbol.iterator](),t={},n("next"),n("throw"),n("return"),t[Symbol.asyncIterator]=function(){return this},t);function n(o){t[o]=r[o]&&function(s){return new Promise(function(a,c){s=r[o](s),i(a,c,s.done,s.value)})}}function i(o,s,a,c){Promise.resolve(c).then(function(u){o({value:u,done:a})},s)}}function b(r){return typeof r=="function"}function ye(r){var e=function(n){Error.call(n),n.stack=new Error().stack},t=r(e);return t.prototype=Object.create(Error.prototype),t.prototype.constructor=t,t}var q=ye(function(r){return function(t){r(this),this.message=t?t.length+` errors occurred during unsubscription:
|
|
2
|
+
`+t.map(function(n,i){return i+1+") "+n.toString()}).join(`
|
|
3
|
+
`):"",this.name="UnsubscriptionError",this.errors=t}});function G(r,e){if(r){var t=r.indexOf(e);0<=t&&r.splice(t,1)}}var k=(function(){function r(e){this.initialTeardown=e,this.closed=!1,this._parentage=null,this._finalizers=null}return r.prototype.unsubscribe=function(){var e,t,n,i,o;if(!this.closed){this.closed=!0;var s=this._parentage;if(s)if(this._parentage=null,Array.isArray(s))try{for(var a=R(s),c=a.next();!c.done;c=a.next()){var u=c.value;u.remove(this)}}catch(h){e={error:h}}finally{try{c&&!c.done&&(t=a.return)&&t.call(a)}finally{if(e)throw e.error}}else s.remove(this);var l=this.initialTeardown;if(b(l))try{l()}catch(h){o=h instanceof q?h.errors:[h]}var d=this._finalizers;if(d){this._finalizers=null;try{for(var y=R(d),f=y.next();!f.done;f=y.next()){var p=f.value;try{ne(p)}catch(h){o=o??[],h instanceof q?o=U(U([],L(o)),L(h.errors)):o.push(h)}}}catch(h){n={error:h}}finally{try{f&&!f.done&&(i=y.return)&&i.call(y)}finally{if(n)throw n.error}}}if(o)throw new q(o)}},r.prototype.add=function(e){var t;if(e&&e!==this)if(this.closed)ne(e);else{if(e instanceof r){if(e.closed||e._hasParent(this))return;e._addParent(this)}(this._finalizers=(t=this._finalizers)!==null&&t!==void 0?t:[]).push(e)}},r.prototype._hasParent=function(e){var t=this._parentage;return t===e||Array.isArray(t)&&t.includes(e)},r.prototype._addParent=function(e){var t=this._parentage;this._parentage=Array.isArray(t)?(t.push(e),t):t?[t,e]:e},r.prototype._removeParent=function(e){var t=this._parentage;t===e?this._parentage=null:Array.isArray(t)&&G(t,e)},r.prototype.remove=function(e){var t=this._finalizers;t&&G(t,e),e instanceof r&&e._removeParent(this)},r.EMPTY=(function(){var e=new r;return e.closed=!0,e})(),r})(),pe=k.EMPTY;function be(r){return r instanceof k||r&&"closed"in r&&b(r.remove)&&b(r.add)&&b(r.unsubscribe)}function ne(r){b(r)?r():r.unsubscribe()}var Be={Promise:void 0},qe={setTimeout:function(r,e){for(var t=[],n=2;n<arguments.length;n++)t[n-2]=arguments[n];return setTimeout.apply(void 0,U([r,e],L(t)))},clearTimeout:function(r){return clearTimeout(r)},delegate:void 0};function me(r){qe.setTimeout(function(){throw r})}function X(){}function D(r){r()}var Q=(function(r){O(e,r);function e(t){var n=r.call(this)||this;return n.isStopped=!1,t?(n.destination=t,be(t)&&t.add(n)):n.destination=Xe,n}return e.create=function(t,n,i){return new J(t,n,i)},e.prototype.next=function(t){this.isStopped||this._next(t)},e.prototype.error=function(t){this.isStopped||(this.isStopped=!0,this._error(t))},e.prototype.complete=function(){this.isStopped||(this.isStopped=!0,this._complete())},e.prototype.unsubscribe=function(){this.closed||(this.isStopped=!0,r.prototype.unsubscribe.call(this),this.destination=null)},e.prototype._next=function(t){this.destination.next(t)},e.prototype._error=function(t){try{this.destination.error(t)}finally{this.unsubscribe()}},e.prototype._complete=function(){try{this.destination.complete()}finally{this.unsubscribe()}},e})(k),Ye=(function(){function r(e){this.partialObserver=e}return r.prototype.next=function(e){var t=this.partialObserver;if(t.next)try{t.next(e)}catch(n){$(n)}},r.prototype.error=function(e){var t=this.partialObserver;if(t.error)try{t.error(e)}catch(n){$(n)}else $(e)},r.prototype.complete=function(){var e=this.partialObserver;if(e.complete)try{e.complete()}catch(t){$(t)}},r})(),J=(function(r){O(e,r);function e(t,n,i){var o=r.call(this)||this,s;return b(t)||!t?s={next:t??void 0,error:n??void 0,complete:i??void 0}:s=t,o.destination=new Ye(s),o}return e})(Q);function $(r){me(r)}function We(r){throw r}var Xe={closed:!0,next:X,error:We,complete:X},Z=(function(){return typeof Symbol=="function"&&Symbol.observable||"@@observable"})();function V(r){return r}function Je(r){return r.length===0?V:r.length===1?r[0]:function(t){return r.reduce(function(n,i){return i(n)},t)}}var S=(function(){function r(e){e&&(this._subscribe=e)}return r.prototype.lift=function(e){var t=new r;return t.source=this,t.operator=e,t},r.prototype.subscribe=function(e,t,n){var i=this,o=Qe(e)?e:new J(e,t,n);return D(function(){var s=i,a=s.operator,c=s.source;o.add(a?a.call(o,c):c?i._subscribe(o):i._trySubscribe(o))}),o},r.prototype._trySubscribe=function(e){try{return this._subscribe(e)}catch(t){e.error(t)}},r.prototype.forEach=function(e,t){var n=this;return t=ie(t),new t(function(i,o){var s=new J({next:function(a){try{e(a)}catch(c){o(c),s.unsubscribe()}},error:o,complete:i});n.subscribe(s)})},r.prototype._subscribe=function(e){var t;return(t=this.source)===null||t===void 0?void 0:t.subscribe(e)},r.prototype[Z]=function(){return this},r.prototype.pipe=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];return Je(e)(this)},r.prototype.toPromise=function(e){var t=this;return e=ie(e),new e(function(n,i){var o;t.subscribe(function(s){return o=s},function(s){return i(s)},function(){return n(o)})})},r.create=function(e){return new r(e)},r})();function ie(r){var e;return(e=r??Be.Promise)!==null&&e!==void 0?e:Promise}function ze(r){return r&&b(r.next)&&b(r.error)&&b(r.complete)}function Qe(r){return r&&r instanceof Q||ze(r)&&be(r)}function Ze(r){return b(r==null?void 0:r.lift)}function C(r){return function(e){if(Ze(e))return e.lift(function(t){try{return r(t,this)}catch(n){this.error(n)}});throw new TypeError("Unable to lift unknown Observable type")}}function I(r,e,t,n,i){return new et(r,e,t,n,i)}var et=(function(r){O(e,r);function e(t,n,i,o,s,a){var c=r.call(this,t)||this;return c.onFinalize=s,c.shouldUnsubscribe=a,c._next=n?function(u){try{n(u)}catch(l){t.error(l)}}:r.prototype._next,c._error=o?function(u){try{o(u)}catch(l){t.error(l)}finally{this.unsubscribe()}}:r.prototype._error,c._complete=i?function(){try{i()}catch(u){t.error(u)}finally{this.unsubscribe()}}:r.prototype._complete,c}return e.prototype.unsubscribe=function(){var t;if(!this.shouldUnsubscribe||this.shouldUnsubscribe()){var n=this.closed;r.prototype.unsubscribe.call(this),!n&&((t=this.onFinalize)===null||t===void 0||t.call(this))}},e})(Q),tt=ye(function(r){return function(){r(this),this.name="ObjectUnsubscribedError",this.message="object unsubscribed"}}),ee=(function(r){O(e,r);function e(){var t=r.call(this)||this;return t.closed=!1,t.currentObservers=null,t.observers=[],t.isStopped=!1,t.hasError=!1,t.thrownError=null,t}return e.prototype.lift=function(t){var n=new oe(this,this);return n.operator=t,n},e.prototype._throwIfClosed=function(){if(this.closed)throw new tt},e.prototype.next=function(t){var n=this;D(function(){var i,o;if(n._throwIfClosed(),!n.isStopped){n.currentObservers||(n.currentObservers=Array.from(n.observers));try{for(var s=R(n.currentObservers),a=s.next();!a.done;a=s.next()){var c=a.value;c.next(t)}}catch(u){i={error:u}}finally{try{a&&!a.done&&(o=s.return)&&o.call(s)}finally{if(i)throw i.error}}}})},e.prototype.error=function(t){var n=this;D(function(){if(n._throwIfClosed(),!n.isStopped){n.hasError=n.isStopped=!0,n.thrownError=t;for(var i=n.observers;i.length;)i.shift().error(t)}})},e.prototype.complete=function(){var t=this;D(function(){if(t._throwIfClosed(),!t.isStopped){t.isStopped=!0;for(var n=t.observers;n.length;)n.shift().complete()}})},e.prototype.unsubscribe=function(){this.isStopped=this.closed=!0,this.observers=this.currentObservers=null},Object.defineProperty(e.prototype,"observed",{get:function(){var t;return((t=this.observers)===null||t===void 0?void 0:t.length)>0},enumerable:!1,configurable:!0}),e.prototype._trySubscribe=function(t){return this._throwIfClosed(),r.prototype._trySubscribe.call(this,t)},e.prototype._subscribe=function(t){return this._throwIfClosed(),this._checkFinalizedStatuses(t),this._innerSubscribe(t)},e.prototype._innerSubscribe=function(t){var n=this,i=this,o=i.hasError,s=i.isStopped,a=i.observers;return o||s?pe:(this.currentObservers=null,a.push(t),new k(function(){n.currentObservers=null,G(a,t)}))},e.prototype._checkFinalizedStatuses=function(t){var n=this,i=n.hasError,o=n.thrownError,s=n.isStopped;i?t.error(o):s&&t.complete()},e.prototype.asObservable=function(){var t=new S;return t.source=this,t},e.create=function(t,n){return new oe(t,n)},e})(S),oe=(function(r){O(e,r);function e(t,n){var i=r.call(this)||this;return i.destination=t,i.source=n,i}return e.prototype.next=function(t){var n,i;(i=(n=this.destination)===null||n===void 0?void 0:n.next)===null||i===void 0||i.call(n,t)},e.prototype.error=function(t){var n,i;(i=(n=this.destination)===null||n===void 0?void 0:n.error)===null||i===void 0||i.call(n,t)},e.prototype.complete=function(){var t,n;(n=(t=this.destination)===null||t===void 0?void 0:t.complete)===null||n===void 0||n.call(t)},e.prototype._subscribe=function(t){var n,i;return(i=(n=this.source)===null||n===void 0?void 0:n.subscribe(t))!==null&&i!==void 0?i:pe},e})(ee),se=(function(r){O(e,r);function e(t){var n=r.call(this)||this;return n._value=t,n}return Object.defineProperty(e.prototype,"value",{get:function(){return this.getValue()},enumerable:!1,configurable:!0}),e.prototype._subscribe=function(t){var n=r.prototype._subscribe.call(this,t);return!n.closed&&t.next(this._value),n},e.prototype.getValue=function(){var t=this,n=t.hasError,i=t.thrownError,o=t._value;if(n)throw i;return this._throwIfClosed(),o},e.prototype.next=function(t){r.prototype.next.call(this,this._value=t)},e})(ee),rt={now:function(){return Date.now()}},nt=(function(r){O(e,r);function e(t,n){return r.call(this)||this}return e.prototype.schedule=function(t,n){return this},e})(k),ae={setInterval:function(r,e){for(var t=[],n=2;n<arguments.length;n++)t[n-2]=arguments[n];return setInterval.apply(void 0,U([r,e],L(t)))},clearInterval:function(r){return clearInterval(r)},delegate:void 0},it=(function(r){O(e,r);function e(t,n){var i=r.call(this,t,n)||this;return i.scheduler=t,i.work=n,i.pending=!1,i}return e.prototype.schedule=function(t,n){var i;if(n===void 0&&(n=0),this.closed)return this;this.state=t;var o=this.id,s=this.scheduler;return o!=null&&(this.id=this.recycleAsyncId(s,o,n)),this.pending=!0,this.delay=n,this.id=(i=this.id)!==null&&i!==void 0?i:this.requestAsyncId(s,this.id,n),this},e.prototype.requestAsyncId=function(t,n,i){return i===void 0&&(i=0),ae.setInterval(t.flush.bind(t,this),i)},e.prototype.recycleAsyncId=function(t,n,i){if(i===void 0&&(i=0),i!=null&&this.delay===i&&this.pending===!1)return n;n!=null&&ae.clearInterval(n)},e.prototype.execute=function(t,n){if(this.closed)return new Error("executing a cancelled action");this.pending=!1;var i=this._execute(t,n);if(i)return i;this.pending===!1&&this.id!=null&&(this.id=this.recycleAsyncId(this.scheduler,this.id,null))},e.prototype._execute=function(t,n){var i=!1,o;try{this.work(t)}catch(s){i=!0,o=s||new Error("Scheduled action threw falsy error")}if(i)return this.unsubscribe(),o},e.prototype.unsubscribe=function(){if(!this.closed){var t=this,n=t.id,i=t.scheduler,o=i.actions;this.work=this.state=this.scheduler=null,this.pending=!1,G(o,this),n!=null&&(this.id=this.recycleAsyncId(i,n,null)),this.delay=null,r.prototype.unsubscribe.call(this)}},e})(nt),ce=(function(){function r(e,t){t===void 0&&(t=r.now),this.schedulerActionCtor=e,this.now=t}return r.prototype.schedule=function(e,t,n){return t===void 0&&(t=0),new this.schedulerActionCtor(this,e).schedule(n,t)},r.now=rt.now,r})(),ot=(function(r){O(e,r);function e(t,n){n===void 0&&(n=ce.now);var i=r.call(this,t,n)||this;return i.actions=[],i._active=!1,i}return e.prototype.flush=function(t){var n=this.actions;if(this._active){n.push(t);return}var i;this._active=!0;do if(i=t.execute(t.state,t.delay))break;while(t=n.shift());if(this._active=!1,i){for(;t=n.shift();)t.unsubscribe();throw i}},e})(ce),ge=new ot(it),st=ge,at=new S(function(r){return r.complete()});function Se(r){return r&&b(r.schedule)}function we(r){return r[r.length-1]}function ct(r){return b(we(r))?r.pop():void 0}function Ee(r){return Se(we(r))?r.pop():void 0}var Ie=(function(r){return r&&typeof r.length=="number"&&typeof r!="function"});function Oe(r){return b(r==null?void 0:r.then)}function Ae(r){return b(r[Z])}function xe(r){return Symbol.asyncIterator&&b(r==null?void 0:r[Symbol.asyncIterator])}function _e(r){return new TypeError("You provided "+(r!==null&&typeof r=="object"?"an invalid object":"'"+r+"'")+" where a stream was expected. You can provide an Observable, Promise, ReadableStream, Array, AsyncIterable, or Iterable.")}function ut(){return typeof Symbol!="function"||!Symbol.iterator?"@@iterator":Symbol.iterator}var Pe=ut();function Te(r){return b(r==null?void 0:r[Pe])}function Ce(r){return He(this,arguments,function(){var t,n,i,o;return ve(this,function(s){switch(s.label){case 0:t=r.getReader(),s.label=1;case 1:s.trys.push([1,,9,10]),s.label=2;case 2:return[4,M(t.read())];case 3:return n=s.sent(),i=n.value,o=n.done,o?[4,M(void 0)]:[3,5];case 4:return[2,s.sent()];case 5:return[4,M(i)];case 6:return[4,s.sent()];case 7:return s.sent(),[3,2];case 8:return[3,10];case 9:return t.releaseLock(),[7];case 10:return[2]}})})}function Me(r){return b(r==null?void 0:r.getReader)}function P(r){if(r instanceof S)return r;if(r!=null){if(Ae(r))return lt(r);if(Ie(r))return ft(r);if(Oe(r))return dt(r);if(xe(r))return Re(r);if(Te(r))return ht(r);if(Me(r))return vt(r)}throw _e(r)}function lt(r){return new S(function(e){var t=r[Z]();if(b(t.subscribe))return t.subscribe(e);throw new TypeError("Provided object does not correctly implement Symbol.observable")})}function ft(r){return new S(function(e){for(var t=0;t<r.length&&!e.closed;t++)e.next(r[t]);e.complete()})}function dt(r){return new S(function(e){r.then(function(t){e.closed||(e.next(t),e.complete())},function(t){return e.error(t)}).then(null,me)})}function ht(r){return new S(function(e){var t,n;try{for(var i=R(r),o=i.next();!o.done;o=i.next()){var s=o.value;if(e.next(s),e.closed)return}}catch(a){t={error:a}}finally{try{o&&!o.done&&(n=i.return)&&n.call(i)}finally{if(t)throw t.error}}e.complete()})}function Re(r){return new S(function(e){yt(r,e).catch(function(t){return e.error(t)})})}function vt(r){return Re(Ce(r))}function yt(r,e){var t,n,i,o;return Ve(this,void 0,void 0,function(){var s,a;return ve(this,function(c){switch(c.label){case 0:c.trys.push([0,5,6,11]),t=Ke(r),c.label=1;case 1:return[4,t.next()];case 2:if(n=c.sent(),!!n.done)return[3,4];if(s=n.value,e.next(s),e.closed)return[2];c.label=3;case 3:return[3,1];case 4:return[3,11];case 5:return a=c.sent(),i={error:a},[3,11];case 6:return c.trys.push([6,,9,10]),n&&!n.done&&(o=t.return)?[4,o.call(t)]:[3,8];case 7:c.sent(),c.label=8;case 8:return[3,10];case 9:if(i)throw i.error;return[7];case 10:return[7];case 11:return e.complete(),[2]}})})}function _(r,e,t,n,i){n===void 0&&(n=0),i===void 0&&(i=!1);var o=e.schedule(function(){t(),i?r.add(this.schedule(null,n)):this.unsubscribe()},n);if(r.add(o),!i)return o}function Le(r,e){return e===void 0&&(e=0),C(function(t,n){t.subscribe(I(n,function(i){return _(n,r,function(){return n.next(i)},e)},function(){return _(n,r,function(){return n.complete()},e)},function(i){return _(n,r,function(){return n.error(i)},e)}))})}function Ue(r,e){return e===void 0&&(e=0),C(function(t,n){n.add(r.schedule(function(){return t.subscribe(n)},e))})}function pt(r,e){return P(r).pipe(Ue(e),Le(e))}function bt(r,e){return P(r).pipe(Ue(e),Le(e))}function mt(r,e){return new S(function(t){var n=0;return e.schedule(function(){n===r.length?t.complete():(t.next(r[n++]),t.closed||this.schedule())})})}function gt(r,e){return new S(function(t){var n;return _(t,e,function(){n=r[Pe](),_(t,e,function(){var i,o,s;try{i=n.next(),o=i.value,s=i.done}catch(a){t.error(a);return}s?t.complete():t.next(o)},0,!0)}),function(){return b(n==null?void 0:n.return)&&n.return()}})}function ke(r,e){if(!r)throw new Error("Iterable cannot be null");return new S(function(t){_(t,e,function(){var n=r[Symbol.asyncIterator]();_(t,e,function(){n.next().then(function(i){i.done?t.complete():t.next(i.value)})},0,!0)})})}function St(r,e){return ke(Ce(r),e)}function wt(r,e){if(r!=null){if(Ae(r))return pt(r,e);if(Ie(r))return mt(r,e);if(Oe(r))return bt(r,e);if(xe(r))return ke(r,e);if(Te(r))return gt(r,e);if(Me(r))return St(r,e)}throw _e(r)}function te(r,e){return e?wt(r,e):P(r)}function Et(){for(var r=[],e=0;e<arguments.length;e++)r[e]=arguments[e];var t=Ee(r);return te(r,t)}function It(r){return r instanceof Date&&!isNaN(r)}function H(r,e){return C(function(t,n){var i=0;t.subscribe(I(n,function(o){n.next(r.call(e,o,i++))}))})}var Ot=Array.isArray;function At(r,e){return Ot(e)?r.apply(void 0,U([],L(e))):r(e)}function xt(r){return H(function(e){return At(r,e)})}var _t=Array.isArray,Pt=Object.getPrototypeOf,Tt=Object.prototype,Ct=Object.keys;function Mt(r){if(r.length===1){var e=r[0];if(_t(e))return{args:e,keys:null};if(Rt(e)){var t=Ct(e);return{args:t.map(function(n){return e[n]}),keys:t}}}return{args:r,keys:null}}function Rt(r){return r&&typeof r=="object"&&Pt(r)===Tt}function Lt(r,e){return r.reduce(function(t,n,i){return t[n]=e[i],t},{})}function Ut(){for(var r=[],e=0;e<arguments.length;e++)r[e]=arguments[e];var t=Ee(r),n=ct(r),i=Mt(r),o=i.args,s=i.keys;if(o.length===0)return te([],t);var a=new S(kt(o,t,s?function(c){return Lt(s,c)}:V));return n?a.pipe(xt(n)):a}function kt(r,e,t){return t===void 0&&(t=V),function(n){ue(e,function(){for(var i=r.length,o=new Array(i),s=i,a=i,c=function(l){ue(e,function(){var d=te(r[l],e),y=!1;d.subscribe(I(n,function(f){o[l]=f,y||(y=!0,a--),a||n.next(t(o.slice()))},function(){--s||n.complete()}))},n)},u=0;u<i;u++)c(u)},n)}}function ue(r,e,t){r?_(t,r,e):e()}function jt(r,e,t,n,i,o,s,a){var c=[],u=0,l=0,d=!1,y=function(){d&&!c.length&&!u&&e.complete()},f=function(h){return u<n?p(h):c.push(h)},p=function(h){u++;var w=!1;P(t(h,l++)).subscribe(I(e,function(E){e.next(E)},function(){w=!0},void 0,function(){if(w)try{u--;for(var E=function(){var T=c.shift();s||p(T)};c.length&&u<n;)E();y()}catch(T){e.error(T)}}))};return r.subscribe(I(e,f,function(){d=!0,y()})),function(){}}function F(r,e,t){return t===void 0&&(t=1/0),b(e)?F(function(n,i){return H(function(o,s){return e(n,o,i,s)})(P(r(n,i)))},t):(typeof e=="number"&&(t=e),C(function(n,i){return jt(n,i,r,t)}))}function je(r,e,t){r===void 0&&(r=0),t===void 0&&(t=st);var n=-1;return e!=null&&(Se(e)?t=e:n=e),new S(function(i){var o=It(r)?+r-t.now():r;o<0&&(o=0);var s=0;return t.schedule(function(){i.closed||(i.next(s++),0<=n?this.schedule(void 0,n):i.complete())},o)})}function $t(r,e){return b(e)?F(r,e,1):F(r,1)}function Dt(r){return r<=0?function(){return at}:C(function(e,t){var n=0;e.subscribe(I(t,function(i){++n<=r&&(t.next(i),r<=n&&t.complete())}))})}function Nt(r){return H(function(){return r})}function Gt(r,e){return F(function(t,n){return P(r(t,n)).pipe(Dt(1),Nt(t))})}function Ft(r,e){e===void 0&&(e=ge);var t=je(r,e);return Gt(function(){return t})}function Vt(r){var e;e={count:r};var t=e.count,n=t===void 0?1/0:t,i=e.delay,o=e.resetOnSuccess,s=o===void 0?!1:o;return n<=0?V:C(function(a,c){var u=0,l,d=function(){var y=!1;l=a.subscribe(I(c,function(f){s&&(u=0),c.next(f)},void 0,function(f){if(u++<n){var p=function(){l?(l.unsubscribe(),l=null,d()):y=!0};if(i!=null){var h=typeof i=="number"?je(i):P(i(f,u)),w=I(c,function(){w.unsubscribe(),p()},function(){c.complete()});h.subscribe(w)}else p()}else c.error(f)})),y&&(l.unsubscribe(),l=null,d())};d()})}function Ht(r){return C(function(e,t){P(r).subscribe(I(t,function(){return t.complete()},X)),!t.closed&&e.subscribe(t)})}async function Kt(r,e){const t=r.getReader();let n;for(;!(n=await t.read()).done;)e(n.value)}function Bt(r){let e,t,n,i=!1;return function(s){e===void 0?(e=s,t=0,n=-1):e=Yt(e,s);const a=e.length;let c=0;for(;t<a;){i&&(e[t]===10&&(c=++t),i=!1);let u=-1;for(;t<a&&u===-1;++t)switch(e[t]){case 58:n===-1&&(n=t-c);break;case 13:i=!0;case 10:u=t;break}if(u===-1)break;r(e.subarray(c,u),n),c=t,n=-1}c===a?e=void 0:c!==0&&(e=e.subarray(c),t-=c)}}function qt(r,e,t){let n=le();const i=new TextDecoder;return function(s,a){if(s.length===0)t==null||t(n),n=le();else if(a>0){const c=i.decode(s.subarray(0,a)),u=a+(s[a+1]===32?2:1),l=i.decode(s.subarray(u));switch(c){case"data":n.data=n.data?n.data+`
|
|
4
|
+
`+l:l;break;case"event":n.event=l;break;case"id":r(n.id=l);break;case"retry":const d=parseInt(l,10);isNaN(d)||e(n.retry=d);break}}}}function Yt(r,e){const t=new Uint8Array(r.length+e.length);return t.set(r),t.set(e,r.length),t}function le(){return{data:"",event:"",id:"",retry:void 0}}var Wt=function(r,e){var t={};for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&e.indexOf(n)<0&&(t[n]=r[n]);if(r!=null&&typeof Object.getOwnPropertySymbols=="function")for(var i=0,n=Object.getOwnPropertySymbols(r);i<n.length;i++)e.indexOf(n[i])<0&&Object.prototype.propertyIsEnumerable.call(r,n[i])&&(t[n[i]]=r[n[i]]);return t};const z="text/event-stream",Xt=1e3,fe="last-event-id";function Jt(r,e){var{signal:t,headers:n,onopen:i,onmessage:o,onclose:s,onerror:a,openWhenHidden:c,fetch:u}=e,l=Wt(e,["signal","headers","onopen","onmessage","onclose","onerror","openWhenHidden","fetch"]);return new Promise((d,y)=>{const f=Object.assign({},n);f.accept||(f.accept=z);let p;function h(){p.abort(),document.hidden||K()}c||document.addEventListener("visibilitychange",h);let w=Xt,E=0;function T(){document.removeEventListener("visibilitychange",h),window.clearTimeout(E),p.abort()}t==null||t.addEventListener("abort",()=>{T(),d()});const De=u??window.fetch,Ne=i??zt;async function K(){var B;p=new AbortController;try{const j=await De(r,Object.assign(Object.assign({},l),{headers:f,signal:p.signal}));await Ne(j),await Kt(j.body,Bt(qt(A=>{A?f[fe]=A:delete f[fe]},A=>{w=A},o))),s==null||s(),T(),d()}catch(j){if(!p.signal.aborted)try{const A=(B=a==null?void 0:a(j))!==null&&B!==void 0?B:w;window.clearTimeout(E),E=window.setTimeout(K,A)}catch(A){T(),y(A)}}}K()})}function zt(r){const e=r.headers.get("content-type");if(!(e!=null&&e.startsWith(z)))throw new Error(`Expected content-type to be ${z}, Actual: ${e}`)}function Qt(r){const{endpoint:e,apiKey:t,payload:n,debugMode:i}=r;return new S(o=>{const s=new AbortController;let a;const c={"Content-Type":"application/json"};t&&(c["X-API-KEY"]=t);const u=new URLSearchParams;i&&u.set("is_debug","true");const l=new URL(e);return u.toString()&&(l.search=u.toString()),Jt(l.toString(),{method:"POST",headers:c,body:n?JSON.stringify(n):void 0,signal:s.signal,openWhenHidden:!0,onopen:async d=>{d.ok?a=d.headers.get("X-Trace-Id")??void 0:(o.error(d),s.abort())},onmessage:d=>{const y=JSON.parse(d.data);a?y.traceId=a:y.requestId&&(y.traceId=y.requestId,a||(a=y.requestId)),o.next(y)},onclose:()=>{o.complete()},onerror:d=>{throw o.error(d),s.abort(),d}}),()=>{s.abort()}})}class Zt{constructor(){m(this,"listeners",{})}on(e,t){this.listeners=Object.assign({},this.listeners,{[e]:(this.listeners[e]??[]).concat(t)})}off(e,t){this.listeners[e]&&(this.listeners=Object.assign({},this.listeners,{[e]:(this.listeners[e]??[]).filter(n=>n!==t)}))}remove(e){delete this.listeners[e]}emit(e,...t){this.listeners[e]&&this.listeners[e].forEach(n=>n(...t))}}class er{constructor(e){m(this,"apiKey");m(this,"endpoint");m(this,"botProviderEndpoint");m(this,"debugMode");m(this,"destroy$",new ee);m(this,"sseEmitter",new Zt);m(this,"transformSsePayload");if(!e.endpoint&&!e.botProviderEndpoint)throw new Error("Either endpoint or botProviderEndpoint must be provided");if(this.apiKey=e.apiKey,this.debugMode=e.debugMode,this.transformSsePayload=e.transformSsePayload,this.botProviderEndpoint=e.botProviderEndpoint,!e.endpoint&&e.botProviderEndpoint){const t=e.botProviderEndpoint.replace(/\/+$/,"");this.endpoint=`${t}/message/sse`}else e.endpoint&&(this.endpoint=e.endpoint,this.debugMode&&console.warn('[AsgardServiceClient] The "endpoint" option is deprecated and will be removed in the next major version. Please use "botProviderEndpoint" instead. The SSE endpoint will be automatically derived as "${botProviderEndpoint}/message/sse".'))}on(e,t){this.sseEmitter.remove(e),this.sseEmitter.on(e,t)}handleEvent(e){switch(e.eventType){case v.INIT:this.sseEmitter.emit(v.INIT,e);break;case v.PROCESS_START:case v.PROCESS_COMPLETE:this.sseEmitter.emit(v.PROCESS,e);break;case v.MESSAGE_START:case v.MESSAGE_DELTA:case v.MESSAGE_COMPLETE:this.sseEmitter.emit(v.MESSAGE,e);break;case v.TOOL_CALL_START:case v.TOOL_CALL_COMPLETE:this.sseEmitter.emit(v.TOOL_CALL,e);break;case v.DONE:this.sseEmitter.emit(v.DONE,e);break;case v.ERROR:this.sseEmitter.emit(v.ERROR,e);break}}fetchSse(e,t){var n,i;(n=t==null?void 0:t.onSseStart)==null||n.call(t),Qt({apiKey:this.apiKey,endpoint:this.endpoint,debugMode:this.debugMode,payload:((i=this.transformSsePayload)==null?void 0:i.call(this,e))??e}).pipe($t(o=>Et(o).pipe(Ft((t==null?void 0:t.delayTime)??50))),Ht(this.destroy$),Vt(3)).subscribe({next:o=>{var s;(s=t==null?void 0:t.onSseMessage)==null||s.call(t,o),this.handleEvent(o)},error:o=>{var s;(s=t==null?void 0:t.onSseError)==null||s.call(t,o)},complete:()=>{var o;(o=t==null?void 0:t.onSseCompleted)==null||o.call(t)}})}close(){this.destroy$.next(),this.destroy$.complete()}async uploadFile(e,t){const n=this.deriveBlobEndpoint();if(!n)throw new Error("Unable to derive blob endpoint. Please provide botProviderEndpoint in config.");const i=new FormData;i.append("file",e),i.append("customChannelId",t);const o={};this.apiKey&&(o["X-API-KEY"]=this.apiKey);try{const s=await fetch(n,{method:"POST",headers:o,body:i});if(!s.ok)throw new Error(`Upload failed: ${s.status} ${s.statusText}`);const a=await s.json();return this.debugMode&&console.log("[AsgardServiceClient] File upload response:",a),a}catch(s){throw console.error("[AsgardServiceClient] File upload error:",s),s}}deriveBlobEndpoint(){if(!this.botProviderEndpoint&&!this.endpoint)return null;let e=this.botProviderEndpoint;if(!e&&this.endpoint&&(e=this.endpoint.replace("/message/sse","")),!e)return null;if(e=e.replace(/\/+$/,""),!e.includes("/generic/")){const t=e.match(/^(https?:\/\/[^/]+)(\/.*)/);if(t){const[,n,i]=t;e=`${n}/generic${i}`}}return`${e}/blob`}}const g=[];for(let r=0;r<256;++r)g.push((r+256).toString(16).slice(1));function tr(r,e=0){return(g[r[e+0]]+g[r[e+1]]+g[r[e+2]]+g[r[e+3]]+"-"+g[r[e+4]]+g[r[e+5]]+"-"+g[r[e+6]]+g[r[e+7]]+"-"+g[r[e+8]]+g[r[e+9]]+"-"+g[r[e+10]]+g[r[e+11]]+g[r[e+12]]+g[r[e+13]]+g[r[e+14]]+g[r[e+15]]).toLowerCase()}let Y;const rr=new Uint8Array(16);function nr(){if(!Y){if(typeof crypto>"u"||!crypto.getRandomValues)throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");Y=crypto.getRandomValues.bind(crypto)}return Y(rr)}const ir=typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto),de={randomUUID:ir};function or(r,e,t){var i;r=r||{};const n=r.random??((i=r.rng)==null?void 0:i.call(r))??nr();if(n.length<16)throw new Error("Random bytes length must be >= 16");return n[6]=n[6]&15|64,n[8]=n[8]&63|128,tr(n)}function $e(r,e,t){return de.randomUUID&&!r?de.randomUUID():or(r)}class x{constructor({messages:e}){m(this,"messages",null);this.messages=e}pushMessage(e){const t=new Map(this.messages);return t.set(e.messageId,e),new x({messages:t})}onMessage(e){switch(e.eventType){case v.MESSAGE_START:return this.onMessageStart(e);case v.MESSAGE_DELTA:return this.onMessageDelta(e);case v.MESSAGE_COMPLETE:return this.onMessageComplete(e);case v.ERROR:return this.onMessageError(e);default:return this}}onMessageStart(e){const t=e.fact.messageStart.message,n=new Map(this.messages);return n.set(t.messageId,{type:"bot",eventType:v.MESSAGE_START,isTyping:!0,typingText:"",messageId:t.messageId,message:t,time:new Date,traceId:e.traceId}),new x({messages:n})}onMessageDelta(e){const t=e.fact.messageDelta.message,n=new Map(this.messages),i=n.get(t.messageId);if((i==null?void 0:i.type)!=="bot")return this;const o=`${(i==null?void 0:i.typingText)??""}${t.text}`;return n.set(t.messageId,{type:"bot",eventType:v.MESSAGE_DELTA,isTyping:!0,typingText:o,messageId:t.messageId,message:t,time:new Date,traceId:e.traceId??i.traceId}),new x({messages:n})}onMessageComplete(e){const t=e.fact.messageComplete.message,n=new Map(this.messages),i=n.get(t.messageId);return n.set(t.messageId,{type:"bot",eventType:v.MESSAGE_COMPLETE,isTyping:!1,typingText:null,messageId:t.messageId,message:t,time:new Date,traceId:e.traceId??((i==null?void 0:i.type)==="bot"?i.traceId:void 0)}),new x({messages:n})}onMessageError(e){const t=$e(),n=e.fact.runError.error,i=new Map(this.messages);return i.set(t,{type:"error",eventType:v.ERROR,messageId:t,error:n,time:new Date,traceId:e.traceId}),new x({messages:i})}}class re{constructor(e){m(this,"client");m(this,"customChannelId");m(this,"customMessageId");m(this,"isConnecting$");m(this,"conversation$");m(this,"statesObserver");m(this,"statesSubscription");m(this,"currentUserMessageId");if(!e.client)throw new Error("client must be required");if(!e.customChannelId)throw new Error("customChannelId must be required");this.client=e.client,this.customChannelId=e.customChannelId,this.customMessageId=e.customMessageId,this.isConnecting$=new se(!1),this.conversation$=new se(e.conversation),this.statesObserver=e.statesObserver}static async reset(e,t,n){const i=new re(e);try{return i.subscribe(),await i.resetChannel(t,n),i}catch(o){throw i.close(),o}}subscribe(){this.statesSubscription=Ut([this.isConnecting$,this.conversation$]).pipe(H(([e,t])=>({isConnecting:e,conversation:t}))).subscribe(this.statesObserver)}fetchSse(e,t){return new Promise((n,i)=>{this.isConnecting$.next(!0),this.client.fetchSse(e,{onSseStart:t==null?void 0:t.onSseStart,onSseMessage:o=>{var s;if((s=t==null?void 0:t.onSseMessage)==null||s.call(t,o),this.currentUserMessageId&&o.traceId){const a=new Map(this.conversation$.value.messages),c=a.get(this.currentUserMessageId);c&&c.type==="user"&&(a.set(this.currentUserMessageId,{...c,traceId:o.traceId}),this.conversation$.next(new x({messages:a}))),this.currentUserMessageId=void 0}this.conversation$.next(this.conversation$.value.onMessage(o))},onSseError:o=>{var s;(s=t==null?void 0:t.onSseError)==null||s.call(t,o),this.isConnecting$.next(!1),this.currentUserMessageId=void 0,i(o)},onSseCompleted:()=>{var o;(o=t==null?void 0:t.onSseCompleted)==null||o.call(t),this.isConnecting$.next(!1),this.currentUserMessageId=void 0,n()}})})}resetChannel(e,t){return this.fetchSse({action:N.RESET_CHANNEL,customChannelId:this.customChannelId,customMessageId:this.customMessageId,text:(e==null?void 0:e.text)||"",payload:e==null?void 0:e.payload},t)}sendMessage(e,t){const n=e.text.trim(),i=e.customMessageId??$e();return this.currentUserMessageId=i,this.conversation$.next(this.conversation$.value.pushMessage({type:"user",messageId:i,text:n,blobIds:e.blobIds,filePreviewUrls:e.filePreviewUrls,time:new Date})),this.fetchSse({action:N.NONE,customChannelId:this.customChannelId,customMessageId:i,payload:e==null?void 0:e.payload,text:n,blobIds:e==null?void 0:e.blobIds},t)}close(){var e;this.isConnecting$.complete(),this.conversation$.complete(),(e=this.statesSubscription)==null||e.unsubscribe()}}exports.AsgardServiceClient=er;exports.Channel=re;exports.Conversation=x;exports.EventType=v;exports.FetchSseAction=N;exports.MessageTemplateType=he;
|
|
5
5
|
//# sourceMappingURL=index.cjs.map
|