@asgard-js/core 0.1.11-canary.1 → 0.1.12
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 +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -0
- package/dist/index.mjs.map +1 -1
- package/dist/lib/channel.d.ts +1 -0
- package/dist/lib/channel.d.ts.map +1 -1
- package/dist/types/channel.d.ts +1 -0
- package/dist/types/channel.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
(function(g,w){typeof exports=="object"&&typeof module<"u"?w(exports):typeof define=="function"&&define.amd?define(["exports"],w):(g=typeof globalThis<"u"?globalThis:g||self,w(g["@asgard-js/core"]={}))})(this,(function(g){"use strict";var ir=Object.defineProperty;var or=(g,w,h)=>w in g?ir(g,w,{enumerable:!0,configurable:!0,writable:!0,value:h}):g[w]=h;var m=(g,w,h)=>or(g,typeof w!="symbol"?w+"":w,h);var w=(r=>(r.RESET_CHANNEL="RESET_CHANNEL",r.NONE="NONE",r))(w||{}),h=(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))(h||{}),ie=(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))(ie||{}),B=function(r,t){return B=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,n){e.__proto__=n}||function(e,n){for(var i in n)Object.prototype.hasOwnProperty.call(n,i)&&(e[i]=n[i])},B(r,t)};function O(r,t){if(typeof t!="function"&&t!==null)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");B(r,t);function e(){this.constructor=r}r.prototype=t===null?Object.create(t):(e.prototype=t.prototype,new e)}function Ne(r,t,e,n){function i(o){return o instanceof e?o:new e(function(s){s(o)})}return new(e||(e=Promise))(function(o,s){function a(l){try{c(n.next(l))}catch(d){s(d)}}function u(l){try{c(n.throw(l))}catch(d){s(d)}}function c(l){l.done?o(l.value):i(l.value).then(a,u)}c((n=n.apply(r,t||[])).next())})}function oe(r,t){var e={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(c){return function(l){return u([c,l])}}function u(c){if(n)throw new TypeError("Generator is already executing.");for(;s&&(s=0,c[0]&&(e=0)),e;)try{if(n=1,i&&(o=c[0]&2?i.return:c[0]?i.throw||((o=i.return)&&o.call(i),0):i.next)&&!(o=o.call(i,c[1])).done)return o;switch(i=0,o&&(c=[c[0]&2,o.value]),c[0]){case 0:case 1:o=c;break;case 4:return e.label++,{value:c[1],done:!1};case 5:e.label++,i=c[1],c=[0];continue;case 7:c=e.ops.pop(),e.trys.pop();continue;default:if(o=e.trys,!(o=o.length>0&&o[o.length-1])&&(c[0]===6||c[0]===2)){e=0;continue}if(c[0]===3&&(!o||c[1]>o[0]&&c[1]<o[3])){e.label=c[1];break}if(c[0]===6&&e.label<o[1]){e.label=o[1],o=c;break}if(o&&e.label<o[2]){e.label=o[2],e.ops.push(c);break}o[2]&&e.ops.pop(),e.trys.pop();continue}c=t.call(r,e)}catch(l){c=[6,l],i=0}finally{n=o=0}if(c[0]&5)throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}}function L(r){var t=typeof Symbol=="function"&&Symbol.iterator,e=t&&r[t],n=0;if(e)return e.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(t?"Object is not iterable.":"Symbol.iterator is not defined.")}function k(r,t){var e=typeof Symbol=="function"&&r[Symbol.iterator];if(!e)return r;var n=e.call(r),i,o=[],s;try{for(;(t===void 0||t-- >0)&&!(i=n.next()).done;)o.push(i.value)}catch(a){s={error:a}}finally{try{i&&!i.done&&(e=n.return)&&e.call(n)}finally{if(s)throw s.error}}return o}function j(r,t,e){if(e||arguments.length===2)for(var n=0,i=t.length,o;n<i;n++)(o||!(n in t))&&(o||(o=Array.prototype.slice.call(t,0,n)),o[n]=t[n]);return r.concat(o||Array.prototype.slice.call(t))}function U(r){return this instanceof U?(this.v=r,this):new U(r)}function Ge(r,t,e){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var n=e.apply(r,t||[]),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(v){return new Promise(function(I,x){o.push([f,v,I,x])>1||u(f,v)})},p&&(i[f]=p(i[f])))}function u(f,p){try{c(n[f](p))}catch(v){y(o[0][3],v)}}function c(f){f.value instanceof U?Promise.resolve(f.value.v).then(l,d):y(o[0][2],f)}function l(f){u("next",f)}function d(f){u("throw",f)}function y(f,p){f(p),o.shift(),o.length&&u(o[0][0],o[0][1])}}function Fe(r){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var t=r[Symbol.asyncIterator],e;return t?t.call(r):(r=typeof L=="function"?L(r):r[Symbol.iterator](),e={},n("next"),n("throw"),n("return"),e[Symbol.asyncIterator]=function(){return this},e);function n(o){e[o]=r[o]&&function(s){return new Promise(function(a,u){s=r[o](s),i(a,u,s.done,s.value)})}}function i(o,s,a,u){Promise.resolve(u).then(function(c){o({value:c,done:a})},s)}}typeof SuppressedError=="function"&&SuppressedError;function b(r){return typeof r=="function"}function se(r){var t=function(n){Error.call(n),n.stack=new Error().stack},e=r(t);return e.prototype=Object.create(Error.prototype),e.prototype.constructor=e,e}var q=se(function(r){return function(e){r(this),this.message=e?e.length+` errors occurred during unsubscription:
|
|
2
2
|
`+e.map(function(n,i){return i+1+") "+n.toString()}).join(`
|
|
3
3
|
`):"",this.name="UnsubscriptionError",this.errors=e}});function D(r,t){if(r){var e=r.indexOf(t);0<=e&&r.splice(e,1)}}var $=(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=L(s),u=a.next();!u.done;u=a.next()){var c=u.value;c.remove(this)}}catch(v){t={error:v}}finally{try{u&&!u.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(v){o=v instanceof q?v.errors:[v]}var d=this._finalizers;if(d){this._finalizers=null;try{for(var y=L(d),f=y.next();!f.done;f=y.next()){var p=f.value;try{ce(p)}catch(v){o=o??[],v instanceof q?o=j(j([],k(o)),k(v.errors)):o.push(v)}}}catch(v){n={error:v}}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(t){var e;if(t&&t!==this)if(this.closed)ce(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)&&D(e,t)},r.prototype.remove=function(t){var e=this._finalizers;e&&D(e,t),t instanceof r&&t._removeParent(this)},r.EMPTY=(function(){var t=new r;return t.closed=!0,t})(),r})(),ae=$.EMPTY;function ue(r){return r instanceof $||r&&"closed"in r&&b(r.remove)&&b(r.add)&&b(r.unsubscribe)}function ce(r){b(r)?r():r.unsubscribe()}var Ve={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,j([r,t],k(e)))},clearTimeout:function(r){return clearTimeout(r)},delegate:void 0};function le(r){He.setTimeout(function(){throw r})}function Y(){}function N(r){r()}var W=(function(r){O(t,r);function t(e){var n=r.call(this)||this;return n.isStopped=!1,e?(n.destination=e,ue(e)&&e.add(n)):n.destination=qe,n}return t.create=function(e,n,i){return new X(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})($),Ke=(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){G(n)}},r.prototype.error=function(t){var e=this.partialObserver;if(e.error)try{e.error(t)}catch(n){G(n)}else G(t)},r.prototype.complete=function(){var t=this.partialObserver;if(t.complete)try{t.complete()}catch(e){G(e)}},r})(),X=(function(r){O(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 Ke(s),o}return t})(W);function G(r){le(r)}function Be(r){throw r}var qe={closed:!0,next:Y,error:Be,complete:Y},J=(function(){return typeof Symbol=="function"&&Symbol.observable||"@@observable"})();function F(r){return r}function Ye(r){return r.length===0?F:r.length===1?r[0]:function(e){return r.reduce(function(n,i){return i(n)},e)}}var E=(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=Xe(t)?t:new X(t,e,n);return N(function(){var s=i,a=s.operator,u=s.source;o.add(a?a.call(o,u):u?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=fe(e),new e(function(i,o){var s=new X({next:function(a){try{t(a)}catch(u){o(u),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[J]=function(){return this},r.prototype.pipe=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return Ye(t)(this)},r.prototype.toPromise=function(t){var e=this;return t=fe(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 fe(r){var t;return(t=r??Ve.Promise)!==null&&t!==void 0?t:Promise}function We(r){return r&&b(r.next)&&b(r.error)&&b(r.complete)}function Xe(r){return r&&r instanceof W||We(r)&&ue(r)}function Je(r){return b(r==null?void 0:r.lift)}function M(r){return function(t){if(Je(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 A(r,t,e,n,i){return new ze(r,t,e,n,i)}var ze=(function(r){O(t,r);function t(e,n,i,o,s,a){var u=r.call(this,e)||this;return u.onFinalize=s,u.shouldUnsubscribe=a,u._next=n?function(c){try{n(c)}catch(l){e.error(l)}}:r.prototype._next,u._error=o?function(c){try{o(c)}catch(l){e.error(l)}finally{this.unsubscribe()}}:r.prototype._error,u._complete=i?function(){try{i()}catch(c){e.error(c)}finally{this.unsubscribe()}}:r.prototype._complete,u}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})(W),Qe=se(function(r){return function(){r(this),this.name="ObjectUnsubscribedError",this.message="object unsubscribed"}}),z=(function(r){O(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 de(this,this);return n.operator=e,n},t.prototype._throwIfClosed=function(){if(this.closed)throw new Qe},t.prototype.next=function(e){var n=this;N(function(){var i,o;if(n._throwIfClosed(),!n.isStopped){n.currentObservers||(n.currentObservers=Array.from(n.observers));try{for(var s=L(n.currentObservers),a=s.next();!a.done;a=s.next()){var u=a.value;u.next(e)}}catch(c){i={error:c}}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;N(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;N(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?ae:(this.currentObservers=null,a.push(e),new $(function(){n.currentObservers=null,D(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 E;return e.source=this,e},t.create=function(e,n){return new de(e,n)},t})(E),de=(function(r){O(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:ae},t})(z),he=(function(r){O(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})(z),Ze={now:function(){return Date.now()}},et=(function(r){O(t,r);function t(e,n){return r.call(this)||this}return t.prototype.schedule=function(e,n){return this},t})($),ve={setInterval:function(r,t){for(var e=[],n=2;n<arguments.length;n++)e[n-2]=arguments[n];return setInterval.apply(void 0,j([r,t],k(e)))},clearInterval:function(r){return clearInterval(r)},delegate:void 0},tt=(function(r){O(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),ve.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&&ve.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,D(o,this),n!=null&&(this.id=this.recycleAsyncId(i,n,null)),this.delay=null,r.prototype.unsubscribe.call(this)}},t})(et),ye=(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=Ze.now,r})(),rt=(function(r){O(t,r);function t(e,n){n===void 0&&(n=ye.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})(ye),pe=new rt(tt),nt=pe,it=new E(function(r){return r.complete()});function be(r){return r&&b(r.schedule)}function me(r){return r[r.length-1]}function ot(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[J])}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 st(){return typeof Symbol!="function"||!Symbol.iterator?"@@iterator":Symbol.iterator}var Ae=st();function xe(r){return b(r==null?void 0:r[Ae])}function _e(r){return Ge(this,arguments,function(){var e,n,i,o;return oe(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,U(e.read())];case 3:return n=s.sent(),i=n.value,o=n.done,o?[4,U(void 0)]:[3,5];case 4:return[2,s.sent()];case 5:return[4,U(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 Te(r){return b(r==null?void 0:r.getReader)}function _(r){if(r instanceof E)return r;if(r!=null){if(Ee(r))return at(r);if(Se(r))return ut(r);if(we(r))return ct(r);if(Ie(r))return Pe(r);if(xe(r))return lt(r);if(Te(r))return ft(r)}throw Oe(r)}function at(r){return new E(function(t){var e=r[J]();if(b(e.subscribe))return e.subscribe(t);throw new TypeError("Provided object does not correctly implement Symbol.observable")})}function ut(r){return new E(function(t){for(var e=0;e<r.length&&!t.closed;e++)t.next(r[e]);t.complete()})}function ct(r){return new E(function(t){r.then(function(e){t.closed||(t.next(e),t.complete())},function(e){return t.error(e)}).then(null,le)})}function lt(r){return new E(function(t){var e,n;try{for(var i=L(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 Pe(r){return new E(function(t){dt(r,t).catch(function(e){return t.error(e)})})}function ft(r){return Pe(_e(r))}function dt(r,t){var e,n,i,o;return Ne(this,void 0,void 0,function(){var s,a;return oe(this,function(u){switch(u.label){case 0:u.trys.push([0,5,6,11]),e=Fe(r),u.label=1;case 1:return[4,e.next()];case 2:if(n=u.sent(),!!n.done)return[3,4];if(s=n.value,t.next(s),t.closed)return[2];u.label=3;case 3:return[3,1];case 4:return[3,11];case 5:return a=u.sent(),i={error:a},[3,11];case 6:return u.trys.push([6,,9,10]),n&&!n.done&&(o=e.return)?[4,o.call(e)]:[3,8];case 7:u.sent(),u.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 T(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),M(function(e,n){e.subscribe(A(n,function(i){return T(n,r,function(){return n.next(i)},t)},function(){return T(n,r,function(){return n.complete()},t)},function(i){return T(n,r,function(){return n.error(i)},t)}))})}function Me(r,t){return t===void 0&&(t=0),M(function(e,n){n.add(r.schedule(function(){return e.subscribe(n)},t))})}function ht(r,t){return _(r).pipe(Me(t),Ce(t))}function vt(r,t){return _(r).pipe(Me(t),Ce(t))}function yt(r,t){return new E(function(e){var n=0;return t.schedule(function(){n===r.length?e.complete():(e.next(r[n++]),e.closed||this.schedule())})})}function pt(r,t){return new E(function(e){var n;return T(e,t,function(){n=r[Ae](),T(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 Re(r,t){if(!r)throw new Error("Iterable cannot be null");return new E(function(e){T(e,t,function(){var n=r[Symbol.asyncIterator]();T(e,t,function(){n.next().then(function(i){i.done?e.complete():e.next(i.value)})},0,!0)})})}function bt(r,t){return Re(_e(r),t)}function mt(r,t){if(r!=null){if(Ee(r))return ht(r,t);if(Se(r))return yt(r,t);if(we(r))return vt(r,t);if(Ie(r))return Re(r,t);if(xe(r))return pt(r,t);if(Te(r))return bt(r,t)}throw Oe(r)}function Q(r,t){return t?mt(r,t):_(r)}function gt(){for(var r=[],t=0;t<arguments.length;t++)r[t]=arguments[t];var e=ge(r);return Q(r,e)}function St(r){return r instanceof Date&&!isNaN(r)}function V(r,t){return M(function(e,n){var i=0;e.subscribe(A(n,function(o){n.next(r.call(t,o,i++))}))})}var wt=Array.isArray;function Et(r,t){return wt(t)?r.apply(void 0,j([],k(t))):r(t)}function It(r){return V(function(t){return Et(r,t)})}var Ot=Array.isArray,At=Object.getPrototypeOf,xt=Object.prototype,_t=Object.keys;function Tt(r){if(r.length===1){var t=r[0];if(Ot(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"&&At(r)===xt}function Ct(r,t){return r.reduce(function(e,n,i){return e[n]=t[i],e},{})}function Mt(){for(var r=[],t=0;t<arguments.length;t++)r[t]=arguments[t];var e=ge(r),n=ot(r),i=Tt(r),o=i.args,s=i.keys;if(o.length===0)return Q([],e);var a=new E(Rt(o,e,s?function(u){return Ct(s,u)}:F));return n?a.pipe(It(n)):a}function Rt(r,t,e){return e===void 0&&(e=F),function(n){Le(t,function(){for(var i=r.length,o=new Array(i),s=i,a=i,u=function(l){Le(t,function(){var d=Q(r[l],t),y=!1;d.subscribe(A(n,function(f){o[l]=f,y||(y=!0,a--),a||n.next(e(o.slice()))},function(){--s||n.complete()}))},n)},c=0;c<i;c++)u(c)},n)}}function Le(r,t,e){r?T(e,r,t):t()}function Lt(r,t,e,n,i,o,s,a){var u=[],c=0,l=0,d=!1,y=function(){d&&!u.length&&!c&&t.complete()},f=function(v){return c<n?p(v):u.push(v)},p=function(v){c++;var I=!1;_(e(v,l++)).subscribe(A(t,function(x){t.next(x)},function(){I=!0},void 0,function(){if(I)try{c--;for(var x=function(){var R=u.shift();s||p(R)};u.length&&c<n;)x();y()}catch(R){t.error(R)}}))};return r.subscribe(A(t,f,function(){d=!0,y()})),function(){}}function H(r,t,e){return e===void 0&&(e=1/0),b(t)?H(function(n,i){return V(function(o,s){return t(n,o,i,s)})(_(r(n,i)))},e):(typeof t=="number"&&(e=t),M(function(n,i){return Lt(n,i,r,e)}))}function Ue(r,t,e){r===void 0&&(r=0),e===void 0&&(e=nt);var n=-1;return t!=null&&(be(t)?e=t:n=t),new E(function(i){var o=St(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 Ut(r,t){return b(t)?H(r,t,1):H(r,1)}function kt(r){return r<=0?function(){return it}:M(function(t,e){var n=0;t.subscribe(A(e,function(i){++n<=r&&(e.next(i),r<=n&&e.complete())}))})}function jt(r){return V(function(){return r})}function $t(r,t){return H(function(e,n){return _(r(e,n)).pipe(kt(1),jt(e))})}function Dt(r,t){t===void 0&&(t=pe);var e=Ue(r,t);return $t(function(){return e})}function Nt(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:M(function(a,u){var c=0,l,d=function(){var y=!1;l=a.subscribe(A(u,function(f){s&&(c=0),u.next(f)},void 0,function(f){if(c++<n){var p=function(){l?(l.unsubscribe(),l=null,d()):y=!0};if(i!=null){var v=typeof i=="number"?Ue(i):_(i(f,c)),I=A(u,function(){I.unsubscribe(),p()},function(){u.complete()});v.subscribe(I)}else p()}else u.error(f)})),y&&(l.unsubscribe(),l=null,d())};d()})}function Gt(r){return M(function(t,e){_(r).subscribe(A(e,function(){return e.complete()},Y)),!e.closed&&t.subscribe(e)})}async function Ft(r,t){const e=r.getReader();let n;for(;!(n=await e.read()).done;)t(n.value)}function Vt(r){let t,e,n,i=!1;return function(s){t===void 0?(t=s,e=0,n=-1):t=Kt(t,s);const a=t.length;let u=0;for(;e<a;){i&&(t[e]===10&&(u=++e),i=!1);let c=-1;for(;e<a&&c===-1;++e)switch(t[e]){case 58:n===-1&&(n=e-u);break;case 13:i=!0;case 10:c=e;break}if(c===-1)break;r(t.subarray(u,c),n),u=e,n=-1}u===a?t=void 0:u!==0&&(t=t.subarray(u),e-=u)}}function Ht(r,t,e){let n=ke();const i=new TextDecoder;return function(s,a){if(s.length===0)e==null||e(n),n=ke();else if(a>0){const u=i.decode(s.subarray(0,a)),c=a+(s[a+1]===32?2:1),l=i.decode(s.subarray(c));switch(u){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 Kt(r,t){const e=new Uint8Array(r.length+t.length);return e.set(r),e.set(t,r.length),e}function ke(){return{data:"",event:"",id:"",retry:void 0}}var Bt=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 Z="text/event-stream",qt=1e3,je="last-event-id";function Yt(r,t){var{signal:e,headers:n,onopen:i,onmessage:o,onclose:s,onerror:a,openWhenHidden:u,fetch:c}=t,l=Bt(t,["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 v(){p.abort(),document.hidden||re()}u||document.addEventListener("visibilitychange",v);let I=qt,x=0;function R(){document.removeEventListener("visibilitychange",v),window.clearTimeout(x),p.abort()}e==null||e.addEventListener("abort",()=>{R(),d()});const rr=c??window.fetch,nr=i??Wt;async function re(){var ne;p=new AbortController;try{const K=await rr(r,Object.assign(Object.assign({},l),{headers:f,signal:p.signal}));await nr(K),await Ft(K.body,Vt(Ht(C=>{C?f[je]=C:delete f[je]},C=>{I=C},o))),s==null||s(),R(),d()}catch(K){if(!p.signal.aborted)try{const C=(ne=a==null?void 0:a(K))!==null&&ne!==void 0?ne:I;window.clearTimeout(x),x=window.setTimeout(re,C)}catch(C){R(),y(C)}}}re()})}function Wt(r){const t=r.headers.get("content-type");if(!(t!=null&&t.startsWith(Z)))throw new Error(`Expected content-type to be ${Z}, Actual: ${t}`)}function Xt(r){const{endpoint:t,apiKey:e,payload:n,debugMode:i}=r;return new E(o=>{const s=new AbortController;let a;const u={"Content-Type":"application/json"};e&&(u["X-API-KEY"]=e);const c=new URLSearchParams;i&&c.set("is_debug","true");const l=new URL(t);return c.toString()&&(l.search=c.toString()),Yt(l.toString(),{method:"POST",headers:u,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 Jt{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 zt{constructor(t){m(this,"apiKey");m(this,"endpoint");m(this,"botProviderEndpoint");m(this,"debugMode");m(this,"destroy$",new z);m(this,"sseEmitter",new Jt);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 h.INIT:this.sseEmitter.emit(h.INIT,t);break;case h.PROCESS_START:case h.PROCESS_COMPLETE:this.sseEmitter.emit(h.PROCESS,t);break;case h.MESSAGE_START:case h.MESSAGE_DELTA:case h.MESSAGE_COMPLETE:this.sseEmitter.emit(h.MESSAGE,t);break;case h.TOOL_CALL_START:case h.TOOL_CALL_COMPLETE:this.sseEmitter.emit(h.TOOL_CALL,t);break;case h.DONE:this.sseEmitter.emit(h.DONE,t);break;case h.ERROR:this.sseEmitter.emit(h.ERROR,t);break}}fetchSse(t,e){var n,i;(n=e==null?void 0:e.onSseStart)==null||n.call(e),Xt({apiKey:this.apiKey,endpoint:this.endpoint,debugMode:this.debugMode,payload:((i=this.transformSsePayload)==null?void 0:i.call(this,t))??t}).pipe(Ut(o=>gt(o).pipe(Dt((e==null?void 0:e.delayTime)??50))),Gt(this.destroy$),Nt(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`}}const S=[];for(let r=0;r<256;++r)S.push((r+256).toString(16).slice(1));function Qt(r,t=0){return(S[r[t+0]]+S[r[t+1]]+S[r[t+2]]+S[r[t+3]]+"-"+S[r[t+4]]+S[r[t+5]]+"-"+S[r[t+6]]+S[r[t+7]]+"-"+S[r[t+8]]+S[r[t+9]]+"-"+S[r[t+10]]+S[r[t+11]]+S[r[t+12]]+S[r[t+13]]+S[r[t+14]]+S[r[t+15]]).toLowerCase()}let ee;const Zt=new Uint8Array(16);function er(){if(!ee){if(typeof crypto>"u"||!crypto.getRandomValues)throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");ee=crypto.getRandomValues.bind(crypto)}return ee(Zt)}const $e={randomUUID:typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto)};function tr(r,t,e){var i;r=r||{};const n=r.random??((i=r.rng)==null?void 0:i.call(r))??er();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,Qt(n)}function De(r,t,e){return $e.randomUUID&&!r?$e.randomUUID():tr(r)}class P{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 P({messages:e})}onMessage(t){switch(t.eventType){case h.MESSAGE_START:return this.onMessageStart(t);case h.MESSAGE_DELTA:return this.onMessageDelta(t);case h.MESSAGE_COMPLETE:return this.onMessageComplete(t);case h.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:h.MESSAGE_START,isTyping:!0,typingText:"",messageId:e.messageId,message:e,time:new Date,traceId:t.traceId}),new P({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:h.MESSAGE_DELTA,isTyping:!0,typingText:o,messageId:e.messageId,message:e,time:new Date,traceId:t.traceId??i.traceId}),new P({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:h.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 P({messages:n})}onMessageError(t){const e=De(),n=t.fact.runError.error,i=new Map(this.messages);return i.set(e,{type:"error",eventType:h.ERROR,messageId:e,error:n,time:new Date,traceId:t.traceId}),new P({messages:i})}}class te{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 he(!1),this.conversation$=new he(t.conversation),this.statesObserver=t.statesObserver}static async reset(t,e,n){const i=new te(t);try{return i.subscribe(),await i.resetChannel(e,n),i}catch(o){throw i.close(),o}}subscribe(){this.statesSubscription=Mt([this.isConnecting$,this.conversation$]).pipe(V(([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),u=a.get(this.currentUserMessageId);u&&u.type==="user"&&(a.set(this.currentUserMessageId,{...u,traceId:o.traceId}),this.conversation$.next(new P({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:w.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??De();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:w.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()}}g.AsgardServiceClient=zt,g.Channel=te,g.Conversation=P,g.EventType=h,g.FetchSseAction=w,g.MessageTemplateType=ie,Object.defineProperty(g,Symbol.toStringTag,{value:"Module"})}));
|
|
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 Kt(r,t){const e=new Uint8Array(r.length+t.length);return e.set(r),e.set(t,r.length),e}function ke(){return{data:"",event:"",id:"",retry:void 0}}var Bt=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 Z="text/event-stream",qt=1e3,je="last-event-id";function Yt(r,t){var{signal:e,headers:n,onopen:i,onmessage:o,onclose:s,onerror:a,openWhenHidden:u,fetch:c}=t,l=Bt(t,["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 v(){p.abort(),document.hidden||re()}u||document.addEventListener("visibilitychange",v);let I=qt,x=0;function R(){document.removeEventListener("visibilitychange",v),window.clearTimeout(x),p.abort()}e==null||e.addEventListener("abort",()=>{R(),d()});const rr=c??window.fetch,nr=i??Wt;async function re(){var ne;p=new AbortController;try{const K=await rr(r,Object.assign(Object.assign({},l),{headers:f,signal:p.signal}));await nr(K),await Ft(K.body,Vt(Ht(C=>{C?f[je]=C:delete f[je]},C=>{I=C},o))),s==null||s(),R(),d()}catch(K){if(!p.signal.aborted)try{const C=(ne=a==null?void 0:a(K))!==null&&ne!==void 0?ne:I;window.clearTimeout(x),x=window.setTimeout(re,C)}catch(C){R(),y(C)}}}re()})}function Wt(r){const t=r.headers.get("content-type");if(!(t!=null&&t.startsWith(Z)))throw new Error(`Expected content-type to be ${Z}, Actual: ${t}`)}function Xt(r){const{endpoint:t,apiKey:e,payload:n,debugMode:i}=r;return new E(o=>{const s=new AbortController;let a;const u={"Content-Type":"application/json"};e&&(u["X-API-KEY"]=e);const c=new URLSearchParams;i&&c.set("is_debug","true");const l=new URL(t);return c.toString()&&(l.search=c.toString()),Yt(l.toString(),{method:"POST",headers:u,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 Jt{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 zt{constructor(t){m(this,"apiKey");m(this,"endpoint");m(this,"botProviderEndpoint");m(this,"debugMode");m(this,"destroy$",new z);m(this,"sseEmitter",new Jt);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 h.INIT:this.sseEmitter.emit(h.INIT,t);break;case h.PROCESS_START:case h.PROCESS_COMPLETE:this.sseEmitter.emit(h.PROCESS,t);break;case h.MESSAGE_START:case h.MESSAGE_DELTA:case h.MESSAGE_COMPLETE:this.sseEmitter.emit(h.MESSAGE,t);break;case h.TOOL_CALL_START:case h.TOOL_CALL_COMPLETE:this.sseEmitter.emit(h.TOOL_CALL,t);break;case h.DONE:this.sseEmitter.emit(h.DONE,t);break;case h.ERROR:this.sseEmitter.emit(h.ERROR,t);break}}fetchSse(t,e){var n,i;(n=e==null?void 0:e.onSseStart)==null||n.call(e),Xt({apiKey:this.apiKey,endpoint:this.endpoint,debugMode:this.debugMode,payload:((i=this.transformSsePayload)==null?void 0:i.call(this,t))??t}).pipe(Ut(o=>gt(o).pipe(Dt((e==null?void 0:e.delayTime)??50))),Gt(this.destroy$),Nt(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`}}const S=[];for(let r=0;r<256;++r)S.push((r+256).toString(16).slice(1));function Qt(r,t=0){return(S[r[t+0]]+S[r[t+1]]+S[r[t+2]]+S[r[t+3]]+"-"+S[r[t+4]]+S[r[t+5]]+"-"+S[r[t+6]]+S[r[t+7]]+"-"+S[r[t+8]]+S[r[t+9]]+"-"+S[r[t+10]]+S[r[t+11]]+S[r[t+12]]+S[r[t+13]]+S[r[t+14]]+S[r[t+15]]).toLowerCase()}let ee;const Zt=new Uint8Array(16);function er(){if(!ee){if(typeof crypto>"u"||!crypto.getRandomValues)throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");ee=crypto.getRandomValues.bind(crypto)}return ee(Zt)}const $e={randomUUID:typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto)};function tr(r,t,e){var i;r=r||{};const n=r.random??((i=r.rng)==null?void 0:i.call(r))??er();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,Qt(n)}function De(r,t,e){return $e.randomUUID&&!r?$e.randomUUID():tr(r)}class P{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 P({messages:e})}onMessage(t){switch(t.eventType){case h.MESSAGE_START:return this.onMessageStart(t);case h.MESSAGE_DELTA:return this.onMessageDelta(t);case h.MESSAGE_COMPLETE:return this.onMessageComplete(t);case h.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:h.MESSAGE_START,isTyping:!0,typingText:"",messageId:e.messageId,message:e,time:new Date,traceId:t.traceId}),new P({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:h.MESSAGE_DELTA,isTyping:!0,typingText:o,messageId:e.messageId,message:e,time:new Date,traceId:t.traceId??i.traceId}),new P({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:h.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 P({messages:n})}onMessageError(t){const e=De(),n=t.fact.runError.error,i=new Map(this.messages);return i.set(e,{type:"error",eventType:h.ERROR,messageId:e,error:n,time:new Date,traceId:t.traceId}),new P({messages:i})}}class te{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 he(!1),this.conversation$=new he(t.conversation),this.statesObserver=t.statesObserver}static async reset(t,e,n){const i=new te(t);try{return i.subscribe(),await i.resetChannel(e,n),i}catch(o){throw i.close(),o}}subscribe(){this.statesSubscription=Mt([this.isConnecting$,this.conversation$]).pipe(V(([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),u=a.get(this.currentUserMessageId);u&&u.type==="user"&&(a.set(this.currentUserMessageId,{...u,traceId:o.traceId}),this.conversation$.next(new P({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:w.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??De();return this.currentUserMessageId=i,this.conversation$.next(this.conversation$.value.pushMessage({type:"user",messageId:i,text:n,blobIds:t.blobIds,filePreviewUrls:t.filePreviewUrls,documentNames:t.documentNames,time:new Date})),this.fetchSse({action:w.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()}}g.AsgardServiceClient=zt,g.Channel=te,g.Conversation=P,g.EventType=h,g.FetchSseAction=w,g.MessageTemplateType=ie,Object.defineProperty(g,Symbol.toStringTag,{value:"Module"})}));
|
|
5
5
|
//# sourceMappingURL=index.js.map
|