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