@asgard-js/core 0.0.44-canary.2 → 0.0.44-canary.4

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 CHANGED
@@ -7,7 +7,7 @@ This package contains the core functionalities of the AsgardJs SDK, providing es
7
7
  To install the core package, use the following command:
8
8
 
9
9
  ```sh
10
- yarn add @asgard-js/core
10
+ npm install @asgard-js/core
11
11
  ```
12
12
 
13
13
  ## Usage
@@ -31,6 +31,30 @@ client.fetchSse({
31
31
  action: 'message',
32
32
  });
33
33
 
34
+ // Upload files (optional, requires uploadFile method)
35
+ if (client.uploadFile) {
36
+ const fileInput = document.querySelector('input[type="file"]');
37
+ const file = fileInput.files[0];
38
+
39
+ try {
40
+ const uploadResponse = await client.uploadFile(file, 'your-channel-id');
41
+
42
+ if (uploadResponse.isSuccess && uploadResponse.data[0]) {
43
+ const blobId = uploadResponse.data[0].blobId;
44
+
45
+ // Send message with uploaded file
46
+ client.fetchSse({
47
+ customChannelId: 'your-channel-id',
48
+ text: 'Here is my image:',
49
+ action: 'message',
50
+ blobIds: [blobId]
51
+ });
52
+ }
53
+ } catch (error) {
54
+ console.error('File upload failed:', error);
55
+ }
56
+ }
57
+
34
58
  // Listen to events
35
59
  client.on('MESSAGE', (response) => {
36
60
  console.log('Received message:', response);
@@ -105,6 +129,7 @@ The main client class for interacting with the Asgard AI platform.
105
129
  #### Methods
106
130
 
107
131
  - **fetchSse(payload, options?)**: Send a message via Server-Sent Events
132
+ - **uploadFile(file, customChannelId)**: Upload file to Blob API and return BlobUploadResponse
108
133
  - **on(event, handler)**: Listen to specific SSE events
109
134
  - **close()**: Close the SSE connection and cleanup resources
110
135
 
@@ -212,6 +237,29 @@ const updatedConversation = conversation.pushMessage(userMessage);
212
237
  console.log('Messages:', Array.from(updatedConversation.messages.values()));
213
238
  ```
214
239
 
240
+
241
+ ### File Upload API
242
+
243
+ The core package includes file upload capabilities for sending images through the chatbot.
244
+
245
+ ```typescript
246
+ // Upload file and send message with attachment
247
+ const uploadResponse = await client.uploadFile(file, customChannelId);
248
+
249
+ if (uploadResponse.isSuccess && uploadResponse.data[0]) {
250
+ const blobId = uploadResponse.data[0].blobId;
251
+
252
+ client.fetchSse({
253
+ customChannelId: 'your-channel-id',
254
+ text: 'Here is my image',
255
+ action: 'message',
256
+ blobIds: [blobId]
257
+ });
258
+ }
259
+ ```
260
+
261
+ **Note**: `uploadFile` is optional - check `client.uploadFile` exists before use. Supports JPEG, PNG, GIF, WebP up to 20MB.
262
+
215
263
  ### Authentication Types
216
264
 
217
265
  The core package includes authentication-related types for dynamic API key management:
@@ -256,16 +304,16 @@ The core package includes comprehensive tests using Vitest.
256
304
 
257
305
  ```sh
258
306
  # Run tests once
259
- yarn test:core
307
+ npm run test:core
260
308
 
261
309
  # Run tests in watch mode
262
- yarn test:core:watch
310
+ npm run test:core:watch
263
311
 
264
312
  # Run tests with UI
265
- yarn test:core:ui
313
+ npm run test:core:ui
266
314
 
267
315
  # Run tests with coverage
268
- yarn test:core:coverage
316
+ npm run test:core:coverage
269
317
  ```
270
318
 
271
319
  ### Test Structure
@@ -312,7 +360,7 @@ To develop the core package locally, follow these steps:
312
360
  2. Install dependencies:
313
361
 
314
362
  ```sh
315
- yarn install
363
+ npm install
316
364
  ```
317
365
 
318
366
  3. Start development:
@@ -321,19 +369,19 @@ You can use the following commands to work with the core package:
321
369
 
322
370
  ```sh
323
371
  # Lint the core package
324
- yarn lint:core
372
+ npm run lint:core
325
373
 
326
374
  # Run tests
327
- yarn test:core
375
+ npm run test:core
328
376
 
329
377
  # Build the package
330
- yarn build:core
378
+ npm run build:core
331
379
 
332
380
  # Watch mode for development
333
- yarn watch:core
381
+ npm run watch:core
334
382
  ```
335
383
 
336
- Setup your npm registry token for yarn publishing:
384
+ Setup your npm registry token for npm publishing:
337
385
 
338
386
  ```sh
339
387
  cd ~/
@@ -345,18 +393,18 @@ For working with both core and React packages:
345
393
 
346
394
  ```sh
347
395
  # Lint both packages
348
- yarn lint:packages
396
+ npm run lint:packages
349
397
 
350
398
  # Test both packages
351
- yarn test
399
+ npm test
352
400
 
353
401
  # Build core package (required for React package)
354
- yarn build:core
355
- yarn build:react
402
+ npm run build:core
403
+ npm run build:react
356
404
 
357
405
  # Release packages
358
- yarn release:core # Release core package
359
- yarn release:react # Release React package
406
+ npm run release:core # Release core package
407
+ npm run release:react # Release React package
360
408
  ```
361
409
 
362
410
  All builds will be available in the `dist` directory.
package/dist/index.cjs CHANGED
@@ -1,5 +1,5 @@
1
- "use strict";var De=Object.defineProperty;var Ue=(r,t,e)=>t in r?De(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e;var m=(r,t,e)=>Ue(r,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var $=(r=>(r.RESET_CHANNEL="RESET_CHANNEL",r.NONE="NONE",r))($||{}),d=(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))(d||{}),le=(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))(le||{}),Y=function(r,t){return Y=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])},Y(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");Y(r,t);function e(){this.constructor=r}r.prototype=t===null?Object.create(t):(e.prototype=t.prototype,new e)}function $e(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 c(l){try{a(n.next(l))}catch(v){s(v)}}function u(l){try{a(n.throw(l))}catch(v){s(v)}}function a(l){l.done?o(l.value):i(l.value).then(c,u)}a((n=n.apply(r,t||[])).next())})}function fe(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=c(0),s.throw=c(1),s.return=c(2),typeof Symbol=="function"&&(s[Symbol.iterator]=function(){return this}),s;function c(a){return function(l){return u([a,l])}}function u(a){if(n)throw new TypeError("Generator is already executing.");for(;s&&(s=0,a[0]&&(e=0)),e;)try{if(n=1,i&&(o=a[0]&2?i.return:a[0]?i.throw||((o=i.return)&&o.call(i),0):i.next)&&!(o=o.call(i,a[1])).done)return o;switch(i=0,o&&(a=[a[0]&2,o.value]),a[0]){case 0:case 1:o=a;break;case 4:return e.label++,{value:a[1],done:!1};case 5:e.label++,i=a[1],a=[0];continue;case 7:a=e.ops.pop(),e.trys.pop();continue;default:if(o=e.trys,!(o=o.length>0&&o[o.length-1])&&(a[0]===6||a[0]===2)){e=0;continue}if(a[0]===3&&(!o||a[1]>o[0]&&a[1]<o[3])){e.label=a[1];break}if(a[0]===6&&e.label<o[1]){e.label=o[1],o=a;break}if(o&&e.label<o[2]){e.label=o[2],e.ops.push(a);break}o[2]&&e.ops.pop(),e.trys.pop();continue}a=t.call(r,e)}catch(l){a=[6,l],i=0}finally{n=o=0}if(a[0]&5)throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}}function M(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 L(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(c){s={error:c}}finally{try{i&&!i.done&&(e=n.return)&&e.call(n)}finally{if(s)throw s.error}}return o}function R(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 P(r){return this instanceof P?(this.v=r,this):new P(r)}function Ne(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),c("next"),c("throw"),c("return",s),i[Symbol.asyncIterator]=function(){return this},i;function s(f){return function(y){return Promise.resolve(y).then(f,v)}}function c(f,y){n[f]&&(i[f]=function(h){return new Promise(function(g,w){o.push([f,h,g,w])>1||u(f,h)})},y&&(i[f]=y(i[f])))}function u(f,y){try{a(n[f](y))}catch(h){b(o[0][3],h)}}function a(f){f.value instanceof P?Promise.resolve(f.value.v).then(l,v):b(o[0][2],f)}function l(f){u("next",f)}function v(f){u("throw",f)}function b(f,y){f(y),o.shift(),o.length&&u(o[0][0],o[0][1])}}function Ge(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 M=="function"?M(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(c,u){s=r[o](s),i(c,u,s.done,s.value)})}}function i(o,s,c,u){Promise.resolve(u).then(function(a){o({value:a,done:c})},s)}}function p(r){return typeof r=="function"}function he(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 K=he(function(r){return function(e){r(this),this.message=e?e.length+` errors occurred during unsubscription:
1
+ "use strict";var $e=Object.defineProperty;var Ue=(r,t,e)=>t in r?$e(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e;var m=(r,t,e)=>Ue(r,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var D=(r=>(r.RESET_CHANNEL="RESET_CHANNEL",r.NONE="NONE",r))(D||{}),d=(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))(d||{}),le=(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))(le||{}),Y=function(r,t){return Y=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])},Y(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");Y(r,t);function e(){this.constructor=r}r.prototype=t===null?Object.create(t):(e.prototype=t.prototype,new e)}function De(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 c(l){try{a(n.next(l))}catch(v){s(v)}}function u(l){try{a(n.throw(l))}catch(v){s(v)}}function a(l){l.done?o(l.value):i(l.value).then(c,u)}a((n=n.apply(r,t||[])).next())})}function fe(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=c(0),s.throw=c(1),s.return=c(2),typeof Symbol=="function"&&(s[Symbol.iterator]=function(){return this}),s;function c(a){return function(l){return u([a,l])}}function u(a){if(n)throw new TypeError("Generator is already executing.");for(;s&&(s=0,a[0]&&(e=0)),e;)try{if(n=1,i&&(o=a[0]&2?i.return:a[0]?i.throw||((o=i.return)&&o.call(i),0):i.next)&&!(o=o.call(i,a[1])).done)return o;switch(i=0,o&&(a=[a[0]&2,o.value]),a[0]){case 0:case 1:o=a;break;case 4:return e.label++,{value:a[1],done:!1};case 5:e.label++,i=a[1],a=[0];continue;case 7:a=e.ops.pop(),e.trys.pop();continue;default:if(o=e.trys,!(o=o.length>0&&o[o.length-1])&&(a[0]===6||a[0]===2)){e=0;continue}if(a[0]===3&&(!o||a[1]>o[0]&&a[1]<o[3])){e.label=a[1];break}if(a[0]===6&&e.label<o[1]){e.label=o[1],o=a;break}if(o&&e.label<o[2]){e.label=o[2],e.ops.push(a);break}o[2]&&e.ops.pop(),e.trys.pop();continue}a=t.call(r,e)}catch(l){a=[6,l],i=0}finally{n=o=0}if(a[0]&5)throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}}function M(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 L(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(c){s={error:c}}finally{try{i&&!i.done&&(e=n.return)&&e.call(n)}finally{if(s)throw s.error}}return o}function R(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 C(r){return this instanceof C?(this.v=r,this):new C(r)}function Ne(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),c("next"),c("throw"),c("return",s),i[Symbol.asyncIterator]=function(){return this},i;function s(f){return function(y){return Promise.resolve(y).then(f,v)}}function c(f,y){n[f]&&(i[f]=function(h){return new Promise(function(S,w){o.push([f,h,S,w])>1||u(f,h)})},y&&(i[f]=y(i[f])))}function u(f,y){try{a(n[f](y))}catch(h){b(o[0][3],h)}}function a(f){f.value instanceof C?Promise.resolve(f.value.v).then(l,v):b(o[0][2],f)}function l(f){u("next",f)}function v(f){u("throw",f)}function b(f,y){f(y),o.shift(),o.length&&u(o[0][0],o[0][1])}}function Ge(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 M=="function"?M(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(c,u){s=r[o](s),i(c,u,s.done,s.value)})}}function i(o,s,c,u){Promise.resolve(u).then(function(a){o({value:a,done:c})},s)}}function p(r){return typeof r=="function"}function he(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 B=he(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
- `):"",this.name="UnsubscriptionError",this.errors=e}});function N(r,t){if(r){var e=r.indexOf(t);0<=e&&r.splice(e,1)}}var k=(function(){function r(t){this.initialTeardown=t,this.closed=!1,this._parentage=null,this._finalizers=null}return r.prototype.unsubscribe=function(){var t,e,n,i,o;if(!this.closed){this.closed=!0;var s=this._parentage;if(s)if(this._parentage=null,Array.isArray(s))try{for(var c=M(s),u=c.next();!u.done;u=c.next()){var a=u.value;a.remove(this)}}catch(h){t={error:h}}finally{try{u&&!u.done&&(e=c.return)&&e.call(c)}finally{if(t)throw t.error}}else s.remove(this);var l=this.initialTeardown;if(p(l))try{l()}catch(h){o=h instanceof K?h.errors:[h]}var v=this._finalizers;if(v){this._finalizers=null;try{for(var b=M(v),f=b.next();!f.done;f=b.next()){var y=f.value;try{te(y)}catch(h){o=o??[],h instanceof K?o=R(R([],L(o)),L(h.errors)):o.push(h)}}}catch(h){n={error:h}}finally{try{f&&!f.done&&(i=b.return)&&i.call(b)}finally{if(n)throw n.error}}}if(o)throw new K(o)}},r.prototype.add=function(t){var e;if(t&&t!==this)if(this.closed)te(t);else{if(t instanceof r){if(t.closed||t._hasParent(this))return;t._addParent(this)}(this._finalizers=(e=this._finalizers)!==null&&e!==void 0?e:[]).push(t)}},r.prototype._hasParent=function(t){var e=this._parentage;return e===t||Array.isArray(e)&&e.includes(t)},r.prototype._addParent=function(t){var e=this._parentage;this._parentage=Array.isArray(e)?(e.push(t),e):e?[e,t]:t},r.prototype._removeParent=function(t){var e=this._parentage;e===t?this._parentage=null:Array.isArray(e)&&N(e,t)},r.prototype.remove=function(t){var e=this._finalizers;e&&N(e,t),t instanceof r&&t._removeParent(this)},r.EMPTY=(function(){var t=new r;return t.closed=!0,t})(),r})(),de=k.EMPTY;function ve(r){return r instanceof k||r&&"closed"in r&&p(r.remove)&&p(r.add)&&p(r.unsubscribe)}function te(r){p(r)?r():r.unsubscribe()}var Fe={Promise:void 0},He={setTimeout:function(r,t){for(var e=[],n=2;n<arguments.length;n++)e[n-2]=arguments[n];return setTimeout.apply(void 0,R([r,t],L(e)))},clearTimeout:function(r){return clearTimeout(r)},delegate:void 0};function ye(r){He.setTimeout(function(){throw r})}function q(){}function U(r){r()}var X=(function(r){O(t,r);function t(e){var n=r.call(this)||this;return n.isStopped=!1,e?(n.destination=e,ve(e)&&e.add(n)):n.destination=Ke,n}return t.create=function(e,n,i){return new W(e,n,i)},t.prototype.next=function(e){this.isStopped||this._next(e)},t.prototype.error=function(e){this.isStopped||(this.isStopped=!0,this._error(e))},t.prototype.complete=function(){this.isStopped||(this.isStopped=!0,this._complete())},t.prototype.unsubscribe=function(){this.closed||(this.isStopped=!0,r.prototype.unsubscribe.call(this),this.destination=null)},t.prototype._next=function(e){this.destination.next(e)},t.prototype._error=function(e){try{this.destination.error(e)}finally{this.unsubscribe()}},t.prototype._complete=function(){try{this.destination.complete()}finally{this.unsubscribe()}},t})(k),Ve=(function(){function r(t){this.partialObserver=t}return r.prototype.next=function(t){var e=this.partialObserver;if(e.next)try{e.next(t)}catch(n){D(n)}},r.prototype.error=function(t){var e=this.partialObserver;if(e.error)try{e.error(t)}catch(n){D(n)}else D(t)},r.prototype.complete=function(){var t=this.partialObserver;if(t.complete)try{t.complete()}catch(e){D(e)}},r})(),W=(function(r){O(t,r);function t(e,n,i){var o=r.call(this)||this,s;return p(e)||!e?s={next:e??void 0,error:n??void 0,complete:i??void 0}:s=e,o.destination=new Ve(s),o}return t})(X);function D(r){ye(r)}function Be(r){throw r}var Ke={closed:!0,next:q,error:Be,complete:q},z=(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 S=(function(){function r(t){t&&(this._subscribe=t)}return r.prototype.lift=function(t){var e=new r;return e.source=this,e.operator=t,e},r.prototype.subscribe=function(t,e,n){var i=this,o=We(t)?t:new W(t,e,n);return U(function(){var s=i,c=s.operator,u=s.source;o.add(c?c.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=re(e),new e(function(i,o){var s=new W({next:function(c){try{t(c)}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[z]=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=re(t),new t(function(n,i){var o;e.subscribe(function(s){return o=s},function(s){return i(s)},function(){return n(o)})})},r.create=function(t){return new r(t)},r})();function re(r){var t;return(t=r??Fe.Promise)!==null&&t!==void 0?t:Promise}function qe(r){return r&&p(r.next)&&p(r.error)&&p(r.complete)}function We(r){return r&&r instanceof X||qe(r)&&ve(r)}function Je(r){return p(r==null?void 0:r.lift)}function C(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 E(r,t,e,n,i){return new Xe(r,t,e,n,i)}var Xe=(function(r){O(t,r);function t(e,n,i,o,s,c){var u=r.call(this,e)||this;return u.onFinalize=s,u.shouldUnsubscribe=c,u._next=n?function(a){try{n(a)}catch(l){e.error(l)}}:r.prototype._next,u._error=o?function(a){try{o(a)}catch(l){e.error(l)}finally{this.unsubscribe()}}:r.prototype._error,u._complete=i?function(){try{i()}catch(a){e.error(a)}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})(X),ze=he(function(r){return function(){r(this),this.name="ObjectUnsubscribedError",this.message="object unsubscribed"}}),Q=(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 ne(this,this);return n.operator=e,n},t.prototype._throwIfClosed=function(){if(this.closed)throw new ze},t.prototype.next=function(e){var n=this;U(function(){var i,o;if(n._throwIfClosed(),!n.isStopped){n.currentObservers||(n.currentObservers=Array.from(n.observers));try{for(var s=M(n.currentObservers),c=s.next();!c.done;c=s.next()){var u=c.value;u.next(e)}}catch(a){i={error:a}}finally{try{c&&!c.done&&(o=s.return)&&o.call(s)}finally{if(i)throw i.error}}}})},t.prototype.error=function(e){var n=this;U(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;U(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,c=i.observers;return o||s?de:(this.currentObservers=null,c.push(e),new k(function(){n.currentObservers=null,N(c,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 S;return e.source=this,e},t.create=function(e,n){return new ne(e,n)},t})(S),ne=(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:de},t})(Q),ie=(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})(Q),Qe={now:function(){return Date.now()}},Ze=(function(r){O(t,r);function t(e,n){return r.call(this)||this}return t.prototype.schedule=function(e,n){return this},t})(k),oe={setInterval:function(r,t){for(var e=[],n=2;n<arguments.length;n++)e[n-2]=arguments[n];return setInterval.apply(void 0,R([r,t],L(e)))},clearInterval:function(r){return clearInterval(r)},delegate:void 0},et=(function(r){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),oe.setInterval(e.flush.bind(e,this),i)},t.prototype.recycleAsyncId=function(e,n,i){if(i===void 0&&(i=0),i!=null&&this.delay===i&&this.pending===!1)return n;n!=null&&oe.clearInterval(n)},t.prototype.execute=function(e,n){if(this.closed)return new Error("executing a cancelled action");this.pending=!1;var i=this._execute(e,n);if(i)return i;this.pending===!1&&this.id!=null&&(this.id=this.recycleAsyncId(this.scheduler,this.id,null))},t.prototype._execute=function(e,n){var i=!1,o;try{this.work(e)}catch(s){i=!0,o=s||new Error("Scheduled action threw falsy error")}if(i)return this.unsubscribe(),o},t.prototype.unsubscribe=function(){if(!this.closed){var e=this,n=e.id,i=e.scheduler,o=i.actions;this.work=this.state=this.scheduler=null,this.pending=!1,N(o,this),n!=null&&(this.id=this.recycleAsyncId(i,n,null)),this.delay=null,r.prototype.unsubscribe.call(this)}},t})(Ze),se=(function(){function r(t,e){e===void 0&&(e=r.now),this.schedulerActionCtor=t,this.now=e}return r.prototype.schedule=function(t,e,n){return e===void 0&&(e=0),new this.schedulerActionCtor(this,t).schedule(n,e)},r.now=Qe.now,r})(),tt=(function(r){O(t,r);function t(e,n){n===void 0&&(n=se.now);var i=r.call(this,e,n)||this;return i.actions=[],i._active=!1,i}return t.prototype.flush=function(e){var n=this.actions;if(this._active){n.push(e);return}var i;this._active=!0;do if(i=e.execute(e.state,e.delay))break;while(e=n.shift());if(this._active=!1,i){for(;e=n.shift();)e.unsubscribe();throw i}},t})(se),pe=new tt(et),rt=pe,nt=new S(function(r){return r.complete()});function be(r){return r&&p(r.schedule)}function me(r){return r[r.length-1]}function it(r){return p(me(r))?r.pop():void 0}function Se(r){return be(me(r))?r.pop():void 0}var ge=(function(r){return r&&typeof r.length=="number"&&typeof r!="function"});function we(r){return p(r==null?void 0:r.then)}function Ee(r){return p(r[z])}function Oe(r){return Symbol.asyncIterator&&p(r==null?void 0:r[Symbol.asyncIterator])}function Ie(r){return new TypeError("You provided "+(r!==null&&typeof r=="object"?"an invalid object":"'"+r+"'")+" where a stream was expected. You can provide an Observable, Promise, ReadableStream, Array, AsyncIterable, or Iterable.")}function ot(){return typeof Symbol!="function"||!Symbol.iterator?"@@iterator":Symbol.iterator}var Ae=ot();function _e(r){return p(r==null?void 0:r[Ae])}function xe(r){return Ne(this,arguments,function(){var e,n,i,o;return fe(this,function(s){switch(s.label){case 0:e=r.getReader(),s.label=1;case 1:s.trys.push([1,,9,10]),s.label=2;case 2:return[4,P(e.read())];case 3:return n=s.sent(),i=n.value,o=n.done,o?[4,P(void 0)]:[3,5];case 4:return[2,s.sent()];case 5:return[4,P(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 p(r==null?void 0:r.getReader)}function _(r){if(r instanceof S)return r;if(r!=null){if(Ee(r))return st(r);if(ge(r))return at(r);if(we(r))return ut(r);if(Oe(r))return Ce(r);if(_e(r))return ct(r);if(Te(r))return lt(r)}throw Ie(r)}function st(r){return new S(function(t){var e=r[z]();if(p(e.subscribe))return e.subscribe(t);throw new TypeError("Provided object does not correctly implement Symbol.observable")})}function at(r){return new S(function(t){for(var e=0;e<r.length&&!t.closed;e++)t.next(r[e]);t.complete()})}function ut(r){return new S(function(t){r.then(function(e){t.closed||(t.next(e),t.complete())},function(e){return t.error(e)}).then(null,ye)})}function ct(r){return new S(function(t){var e,n;try{for(var i=M(r),o=i.next();!o.done;o=i.next()){var s=o.value;if(t.next(s),t.closed)return}}catch(c){e={error:c}}finally{try{o&&!o.done&&(n=i.return)&&n.call(i)}finally{if(e)throw e.error}}t.complete()})}function Ce(r){return new S(function(t){ft(r,t).catch(function(e){return t.error(e)})})}function lt(r){return Ce(xe(r))}function ft(r,t){var e,n,i,o;return $e(this,void 0,void 0,function(){var s,c;return fe(this,function(u){switch(u.label){case 0:u.trys.push([0,5,6,11]),e=Ge(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 c=u.sent(),i={error:c},[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 A(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 Pe(r,t){return t===void 0&&(t=0),C(function(e,n){e.subscribe(E(n,function(i){return A(n,r,function(){return n.next(i)},t)},function(){return A(n,r,function(){return n.complete()},t)},function(i){return A(n,r,function(){return n.error(i)},t)}))})}function Me(r,t){return t===void 0&&(t=0),C(function(e,n){n.add(r.schedule(function(){return e.subscribe(n)},t))})}function ht(r,t){return _(r).pipe(Me(t),Pe(t))}function dt(r,t){return _(r).pipe(Me(t),Pe(t))}function vt(r,t){return new S(function(e){var n=0;return t.schedule(function(){n===r.length?e.complete():(e.next(r[n++]),e.closed||this.schedule())})})}function yt(r,t){return new S(function(e){var n;return A(e,t,function(){n=r[Ae](),A(e,t,function(){var i,o,s;try{i=n.next(),o=i.value,s=i.done}catch(c){e.error(c);return}s?e.complete():e.next(o)},0,!0)}),function(){return p(n==null?void 0:n.return)&&n.return()}})}function Le(r,t){if(!r)throw new Error("Iterable cannot be null");return new S(function(e){A(e,t,function(){var n=r[Symbol.asyncIterator]();A(e,t,function(){n.next().then(function(i){i.done?e.complete():e.next(i.value)})},0,!0)})})}function pt(r,t){return Le(xe(r),t)}function bt(r,t){if(r!=null){if(Ee(r))return ht(r,t);if(ge(r))return vt(r,t);if(we(r))return dt(r,t);if(Oe(r))return Le(r,t);if(_e(r))return yt(r,t);if(Te(r))return pt(r,t)}throw Ie(r)}function Z(r,t){return t?bt(r,t):_(r)}function mt(){for(var r=[],t=0;t<arguments.length;t++)r[t]=arguments[t];var e=Se(r);return Z(r,e)}function St(r){return r instanceof Date&&!isNaN(r)}function H(r,t){return C(function(e,n){var i=0;e.subscribe(E(n,function(o){n.next(r.call(t,o,i++))}))})}var gt=Array.isArray;function wt(r,t){return gt(t)?r.apply(void 0,R([],L(t))):r(t)}function Et(r){return H(function(t){return wt(r,t)})}var Ot=Array.isArray,It=Object.getPrototypeOf,At=Object.prototype,_t=Object.keys;function xt(r){if(r.length===1){var t=r[0];if(Ot(t))return{args:t,keys:null};if(Tt(t)){var e=_t(t);return{args:e.map(function(n){return t[n]}),keys:e}}}return{args:r,keys:null}}function Tt(r){return r&&typeof r=="object"&&It(r)===At}function Ct(r,t){return r.reduce(function(e,n,i){return e[n]=t[i],e},{})}function Pt(){for(var r=[],t=0;t<arguments.length;t++)r[t]=arguments[t];var e=Se(r),n=it(r),i=xt(r),o=i.args,s=i.keys;if(o.length===0)return Z([],e);var c=new S(Mt(o,e,s?function(u){return Ct(s,u)}:F));return n?c.pipe(Et(n)):c}function Mt(r,t,e){return e===void 0&&(e=F),function(n){ae(t,function(){for(var i=r.length,o=new Array(i),s=i,c=i,u=function(l){ae(t,function(){var v=Z(r[l],t),b=!1;v.subscribe(E(n,function(f){o[l]=f,b||(b=!0,c--),c||n.next(e(o.slice()))},function(){--s||n.complete()}))},n)},a=0;a<i;a++)u(a)},n)}}function ae(r,t,e){r?A(e,r,t):t()}function Lt(r,t,e,n,i,o,s,c){var u=[],a=0,l=0,v=!1,b=function(){v&&!u.length&&!a&&t.complete()},f=function(h){return a<n?y(h):u.push(h)},y=function(h){a++;var g=!1;_(e(h,l++)).subscribe(E(t,function(w){t.next(w)},function(){g=!0},void 0,function(){if(g)try{a--;for(var w=function(){var x=u.shift();s||y(x)};u.length&&a<n;)w();b()}catch(x){t.error(x)}}))};return r.subscribe(E(t,f,function(){v=!0,b()})),function(){}}function G(r,t,e){return e===void 0&&(e=1/0),p(t)?G(function(n,i){return H(function(o,s){return t(n,o,i,s)})(_(r(n,i)))},e):(typeof t=="number"&&(e=t),C(function(n,i){return Lt(n,i,r,e)}))}function Re(r,t,e){r===void 0&&(r=0),e===void 0&&(e=rt);var n=-1;return t!=null&&(be(t)?e=t:n=t),new S(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 Rt(r,t){return p(t)?G(r,t,1):G(r,1)}function kt(r){return r<=0?function(){return nt}:C(function(t,e){var n=0;t.subscribe(E(e,function(i){++n<=r&&(e.next(i),r<=n&&e.complete())}))})}function jt(r){return H(function(){return r})}function Dt(r,t){return G(function(e,n){return _(r(e,n)).pipe(kt(1),jt(e))})}function Ut(r,t){t===void 0&&(t=pe);var e=Re(r,t);return Dt(function(){return e})}function $t(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:C(function(c,u){var a=0,l,v=function(){var b=!1;l=c.subscribe(E(u,function(f){s&&(a=0),u.next(f)},void 0,function(f){if(a++<n){var y=function(){l?(l.unsubscribe(),l=null,v()):b=!0};if(i!=null){var h=typeof i=="number"?Re(i):_(i(f,a)),g=E(u,function(){g.unsubscribe(),y()},function(){u.complete()});h.subscribe(g)}else y()}else u.error(f)})),b&&(l.unsubscribe(),l=null,v())};v()})}function Nt(r){return C(function(t,e){_(r).subscribe(E(e,function(){return e.complete()},q)),!e.closed&&t.subscribe(e)})}async function Gt(r,t){const e=r.getReader();let n;for(;!(n=await e.read()).done;)t(n.value)}function Ft(r){let t,e,n,i=!1;return function(s){t===void 0?(t=s,e=0,n=-1):t=Vt(t,s);const c=t.length;let u=0;for(;e<c;){i&&(t[e]===10&&(u=++e),i=!1);let a=-1;for(;e<c&&a===-1;++e)switch(t[e]){case 58:n===-1&&(n=e-u);break;case 13:i=!0;case 10:a=e;break}if(a===-1)break;r(t.subarray(u,a),n),u=e,n=-1}u===c?t=void 0:u!==0&&(t=t.subarray(u),e-=u)}}function Ht(r,t,e){let n=ue();const i=new TextDecoder;return function(s,c){if(s.length===0)e==null||e(n),n=ue();else if(c>0){const u=i.decode(s.subarray(0,c)),a=c+(s[c+1]===32?2:1),l=i.decode(s.subarray(a));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 v=parseInt(l,10);isNaN(v)||t(n.retry=v);break}}}}function Vt(r,t){const e=new Uint8Array(r.length+t.length);return e.set(r),e.set(t,r.length),e}function ue(){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 J="text/event-stream",Kt=1e3,ce="last-event-id";function Yt(r,t){var{signal:e,headers:n,onopen:i,onmessage:o,onclose:s,onerror:c,openWhenHidden:u,fetch:a}=t,l=Bt(t,["signal","headers","onopen","onmessage","onclose","onerror","openWhenHidden","fetch"]);return new Promise((v,b)=>{const f=Object.assign({},n);f.accept||(f.accept=J);let y;function h(){y.abort(),document.hidden||V()}u||document.addEventListener("visibilitychange",h);let g=Kt,w=0;function x(){document.removeEventListener("visibilitychange",h),window.clearTimeout(w),y.abort()}e==null||e.addEventListener("abort",()=>{x(),v()});const ke=a??window.fetch,je=i??qt;async function V(){var B;y=new AbortController;try{const j=await ke(r,Object.assign(Object.assign({},l),{headers:f,signal:y.signal}));await je(j),await Gt(j.body,Ft(Ht(I=>{I?f[ce]=I:delete f[ce]},I=>{g=I},o))),s==null||s(),x(),v()}catch(j){if(!y.signal.aborted)try{const I=(B=c==null?void 0:c(j))!==null&&B!==void 0?B:g;window.clearTimeout(w),w=window.setTimeout(V,I)}catch(I){x(),b(I)}}}V()})}function qt(r){const t=r.headers.get("content-type");if(!(t!=null&&t.startsWith(J)))throw new Error(`Expected content-type to be ${J}, Actual: ${t}`)}function Wt(r){const{endpoint:t,apiKey:e,payload:n,debugMode:i}=r;return new S(o=>{const s=new AbortController,c={"Content-Type":"application/json"};e&&(c["X-API-KEY"]=e);const u=new URLSearchParams;i&&u.set("is_debug","true");const a=new URL(t);return u.toString()&&(a.search=u.toString()),Yt(a.toString(),{method:"POST",headers:c,body:n?JSON.stringify(n):void 0,signal:s.signal,openWhenHidden:!0,onopen:async l=>{l.ok||(o.error(l),s.abort())},onmessage:l=>{o.next(JSON.parse(l.data))},onclose:()=>{o.complete()},onerror:l=>{throw o.error(l),s.abort(),l}}),()=>{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 Xt{constructor(t){m(this,"apiKey");m(this,"endpoint");m(this,"debugMode");m(this,"destroy$",new Q);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,!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 d.INIT:this.sseEmitter.emit(d.INIT,t);break;case d.PROCESS_START:case d.PROCESS_COMPLETE:this.sseEmitter.emit(d.PROCESS,t);break;case d.MESSAGE_START:case d.MESSAGE_DELTA:case d.MESSAGE_COMPLETE:this.sseEmitter.emit(d.MESSAGE,t);break;case d.TOOL_CALL_START:case d.TOOL_CALL_COMPLETE:this.sseEmitter.emit(d.TOOL_CALL,t);break;case d.DONE:this.sseEmitter.emit(d.DONE,t);break;case d.ERROR:this.sseEmitter.emit(d.ERROR,t);break}}fetchSse(t,e){var n,i;(n=e==null?void 0:e.onSseStart)==null||n.call(e),Wt({apiKey:this.apiKey,endpoint:this.endpoint,debugMode:this.debugMode,payload:((i=this.transformSsePayload)==null?void 0:i.call(this,t))??t}).pipe(Rt(o=>mt(o).pipe(Ut((e==null?void 0:e.delayTime)??50))),Nt(this.destroy$),$t(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()}}class ee{constructor(t){m(this,"client");m(this,"customChannelId");m(this,"customMessageId");m(this,"isConnecting$");m(this,"conversation$");m(this,"statesObserver");m(this,"statesSubscription");if(!t.client)throw new Error("client must be required");if(!t.customChannelId)throw new Error("customChannelId must be required");this.client=t.client,this.customChannelId=t.customChannelId,this.customMessageId=t.customMessageId,this.isConnecting$=new ie(!1),this.conversation$=new ie(t.conversation),this.statesObserver=t.statesObserver}static async reset(t,e,n){const i=new ee(t);try{return i.subscribe(),await i.resetChannel(e,n),i}catch(o){throw i.close(),o}}subscribe(){this.statesSubscription=Pt([this.isConnecting$,this.conversation$]).pipe(H(([t,e])=>({isConnecting:t,conversation:e}))).subscribe(this.statesObserver)}fetchSse(t,e){return new Promise((n,i)=>{this.isConnecting$.next(!0),this.client.fetchSse(t,{onSseStart:e==null?void 0:e.onSseStart,onSseMessage:o=>{var s;(s=e==null?void 0:e.onSseMessage)==null||s.call(e,o),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),i(o)},onSseCompleted:()=>{var o;(o=e==null?void 0:e.onSseCompleted)==null||o.call(e),this.isConnecting$.next(!1),n()}})})}resetChannel(t,e){return this.fetchSse({action:$.RESET_CHANNEL,customChannelId:this.customChannelId,customMessageId:this.customMessageId,text:(t==null?void 0:t.text)||"",payload:t==null?void 0:t.payload},e)}sendMessage(t,e){const n=t.text.trim(),i=t.customMessageId??crypto.randomUUID();return this.conversation$.next(this.conversation$.value.pushMessage({type:"user",messageId:i,text:n,time:new Date})),this.fetchSse({action:$.NONE,customChannelId:this.customChannelId,customMessageId:i,payload:t==null?void 0:t.payload,text:n},e)}close(){var t;this.isConnecting$.complete(),this.conversation$.complete(),(t=this.statesSubscription)==null||t.unsubscribe()}}class T{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 T({messages:e})}onMessage(t){switch(t.eventType){case d.MESSAGE_START:return this.onMessageStart(t);case d.MESSAGE_DELTA:return this.onMessageDelta(t);case d.MESSAGE_COMPLETE:return this.onMessageComplete(t);case d.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:d.MESSAGE_START,isTyping:!0,typingText:"",messageId:e.messageId,message:e,time:new Date}),new T({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:d.MESSAGE_DELTA,isTyping:!0,typingText:o,messageId:e.messageId,message:e,time:new Date}),new T({messages:n})}onMessageComplete(t){const e=t.fact.messageComplete.message,n=new Map(this.messages);return n.set(e.messageId,{type:"bot",eventType:d.MESSAGE_COMPLETE,isTyping:!1,typingText:null,messageId:e.messageId,message:e,time:new Date}),new T({messages:n})}onMessageError(t){const e=crypto.randomUUID(),n=t.fact.runError.error,i=new Map(this.messages);return i.set(e,{type:"error",eventType:d.ERROR,messageId:e,error:n,time:new Date}),new T({messages:i})}}exports.AsgardServiceClient=Xt;exports.Channel=ee;exports.Conversation=T;exports.EventType=d;exports.FetchSseAction=$;exports.MessageTemplateType=le;
3
+ `):"",this.name="UnsubscriptionError",this.errors=e}});function N(r,t){if(r){var e=r.indexOf(t);0<=e&&r.splice(e,1)}}var k=(function(){function r(t){this.initialTeardown=t,this.closed=!1,this._parentage=null,this._finalizers=null}return r.prototype.unsubscribe=function(){var t,e,n,i,o;if(!this.closed){this.closed=!0;var s=this._parentage;if(s)if(this._parentage=null,Array.isArray(s))try{for(var c=M(s),u=c.next();!u.done;u=c.next()){var a=u.value;a.remove(this)}}catch(h){t={error:h}}finally{try{u&&!u.done&&(e=c.return)&&e.call(c)}finally{if(t)throw t.error}}else s.remove(this);var l=this.initialTeardown;if(p(l))try{l()}catch(h){o=h instanceof B?h.errors:[h]}var v=this._finalizers;if(v){this._finalizers=null;try{for(var b=M(v),f=b.next();!f.done;f=b.next()){var y=f.value;try{te(y)}catch(h){o=o??[],h instanceof B?o=R(R([],L(o)),L(h.errors)):o.push(h)}}}catch(h){n={error:h}}finally{try{f&&!f.done&&(i=b.return)&&i.call(b)}finally{if(n)throw n.error}}}if(o)throw new B(o)}},r.prototype.add=function(t){var e;if(t&&t!==this)if(this.closed)te(t);else{if(t instanceof r){if(t.closed||t._hasParent(this))return;t._addParent(this)}(this._finalizers=(e=this._finalizers)!==null&&e!==void 0?e:[]).push(t)}},r.prototype._hasParent=function(t){var e=this._parentage;return e===t||Array.isArray(e)&&e.includes(t)},r.prototype._addParent=function(t){var e=this._parentage;this._parentage=Array.isArray(e)?(e.push(t),e):e?[e,t]:t},r.prototype._removeParent=function(t){var e=this._parentage;e===t?this._parentage=null:Array.isArray(e)&&N(e,t)},r.prototype.remove=function(t){var e=this._finalizers;e&&N(e,t),t instanceof r&&t._removeParent(this)},r.EMPTY=(function(){var t=new r;return t.closed=!0,t})(),r})(),de=k.EMPTY;function ve(r){return r instanceof k||r&&"closed"in r&&p(r.remove)&&p(r.add)&&p(r.unsubscribe)}function te(r){p(r)?r():r.unsubscribe()}var Fe={Promise:void 0},He={setTimeout:function(r,t){for(var e=[],n=2;n<arguments.length;n++)e[n-2]=arguments[n];return setTimeout.apply(void 0,R([r,t],L(e)))},clearTimeout:function(r){return clearTimeout(r)},delegate:void 0};function ye(r){He.setTimeout(function(){throw r})}function q(){}function U(r){r()}var J=(function(r){O(t,r);function t(e){var n=r.call(this)||this;return n.isStopped=!1,e?(n.destination=e,ve(e)&&e.add(n)):n.destination=Be,n}return t.create=function(e,n,i){return new W(e,n,i)},t.prototype.next=function(e){this.isStopped||this._next(e)},t.prototype.error=function(e){this.isStopped||(this.isStopped=!0,this._error(e))},t.prototype.complete=function(){this.isStopped||(this.isStopped=!0,this._complete())},t.prototype.unsubscribe=function(){this.closed||(this.isStopped=!0,r.prototype.unsubscribe.call(this),this.destination=null)},t.prototype._next=function(e){this.destination.next(e)},t.prototype._error=function(e){try{this.destination.error(e)}finally{this.unsubscribe()}},t.prototype._complete=function(){try{this.destination.complete()}finally{this.unsubscribe()}},t})(k),Ve=(function(){function r(t){this.partialObserver=t}return r.prototype.next=function(t){var e=this.partialObserver;if(e.next)try{e.next(t)}catch(n){$(n)}},r.prototype.error=function(t){var e=this.partialObserver;if(e.error)try{e.error(t)}catch(n){$(n)}else $(t)},r.prototype.complete=function(){var t=this.partialObserver;if(t.complete)try{t.complete()}catch(e){$(e)}},r})(),W=(function(r){O(t,r);function t(e,n,i){var o=r.call(this)||this,s;return p(e)||!e?s={next:e??void 0,error:n??void 0,complete:i??void 0}:s=e,o.destination=new Ve(s),o}return t})(J);function $(r){ye(r)}function Ke(r){throw r}var Be={closed:!0,next:q,error:Ke,complete:q},z=(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 g=(function(){function r(t){t&&(this._subscribe=t)}return r.prototype.lift=function(t){var e=new r;return e.source=this,e.operator=t,e},r.prototype.subscribe=function(t,e,n){var i=this,o=We(t)?t:new W(t,e,n);return U(function(){var s=i,c=s.operator,u=s.source;o.add(c?c.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=re(e),new e(function(i,o){var s=new W({next:function(c){try{t(c)}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[z]=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=re(t),new t(function(n,i){var o;e.subscribe(function(s){return o=s},function(s){return i(s)},function(){return n(o)})})},r.create=function(t){return new r(t)},r})();function re(r){var t;return(t=r??Fe.Promise)!==null&&t!==void 0?t:Promise}function qe(r){return r&&p(r.next)&&p(r.error)&&p(r.complete)}function We(r){return r&&r instanceof J||qe(r)&&ve(r)}function Xe(r){return p(r==null?void 0:r.lift)}function T(r){return function(t){if(Xe(t))return t.lift(function(e){try{return r(e,this)}catch(n){this.error(n)}});throw new TypeError("Unable to lift unknown Observable type")}}function E(r,t,e,n,i){return new Je(r,t,e,n,i)}var Je=(function(r){O(t,r);function t(e,n,i,o,s,c){var u=r.call(this,e)||this;return u.onFinalize=s,u.shouldUnsubscribe=c,u._next=n?function(a){try{n(a)}catch(l){e.error(l)}}:r.prototype._next,u._error=o?function(a){try{o(a)}catch(l){e.error(l)}finally{this.unsubscribe()}}:r.prototype._error,u._complete=i?function(){try{i()}catch(a){e.error(a)}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})(J),ze=he(function(r){return function(){r(this),this.name="ObjectUnsubscribedError",this.message="object unsubscribed"}}),Q=(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 ne(this,this);return n.operator=e,n},t.prototype._throwIfClosed=function(){if(this.closed)throw new ze},t.prototype.next=function(e){var n=this;U(function(){var i,o;if(n._throwIfClosed(),!n.isStopped){n.currentObservers||(n.currentObservers=Array.from(n.observers));try{for(var s=M(n.currentObservers),c=s.next();!c.done;c=s.next()){var u=c.value;u.next(e)}}catch(a){i={error:a}}finally{try{c&&!c.done&&(o=s.return)&&o.call(s)}finally{if(i)throw i.error}}}})},t.prototype.error=function(e){var n=this;U(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;U(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,c=i.observers;return o||s?de:(this.currentObservers=null,c.push(e),new k(function(){n.currentObservers=null,N(c,e)}))},t.prototype._checkFinalizedStatuses=function(e){var n=this,i=n.hasError,o=n.thrownError,s=n.isStopped;i?e.error(o):s&&e.complete()},t.prototype.asObservable=function(){var e=new g;return e.source=this,e},t.create=function(e,n){return new ne(e,n)},t})(g),ne=(function(r){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:de},t})(Q),ie=(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})(Q),Qe={now:function(){return Date.now()}},Ze=(function(r){O(t,r);function t(e,n){return r.call(this)||this}return t.prototype.schedule=function(e,n){return this},t})(k),oe={setInterval:function(r,t){for(var e=[],n=2;n<arguments.length;n++)e[n-2]=arguments[n];return setInterval.apply(void 0,R([r,t],L(e)))},clearInterval:function(r){return clearInterval(r)},delegate:void 0},et=(function(r){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),oe.setInterval(e.flush.bind(e,this),i)},t.prototype.recycleAsyncId=function(e,n,i){if(i===void 0&&(i=0),i!=null&&this.delay===i&&this.pending===!1)return n;n!=null&&oe.clearInterval(n)},t.prototype.execute=function(e,n){if(this.closed)return new Error("executing a cancelled action");this.pending=!1;var i=this._execute(e,n);if(i)return i;this.pending===!1&&this.id!=null&&(this.id=this.recycleAsyncId(this.scheduler,this.id,null))},t.prototype._execute=function(e,n){var i=!1,o;try{this.work(e)}catch(s){i=!0,o=s||new Error("Scheduled action threw falsy error")}if(i)return this.unsubscribe(),o},t.prototype.unsubscribe=function(){if(!this.closed){var e=this,n=e.id,i=e.scheduler,o=i.actions;this.work=this.state=this.scheduler=null,this.pending=!1,N(o,this),n!=null&&(this.id=this.recycleAsyncId(i,n,null)),this.delay=null,r.prototype.unsubscribe.call(this)}},t})(Ze),se=(function(){function r(t,e){e===void 0&&(e=r.now),this.schedulerActionCtor=t,this.now=e}return r.prototype.schedule=function(t,e,n){return e===void 0&&(e=0),new this.schedulerActionCtor(this,t).schedule(n,e)},r.now=Qe.now,r})(),tt=(function(r){O(t,r);function t(e,n){n===void 0&&(n=se.now);var i=r.call(this,e,n)||this;return i.actions=[],i._active=!1,i}return t.prototype.flush=function(e){var n=this.actions;if(this._active){n.push(e);return}var i;this._active=!0;do if(i=e.execute(e.state,e.delay))break;while(e=n.shift());if(this._active=!1,i){for(;e=n.shift();)e.unsubscribe();throw i}},t})(se),pe=new tt(et),rt=pe,nt=new g(function(r){return r.complete()});function be(r){return r&&p(r.schedule)}function me(r){return r[r.length-1]}function it(r){return p(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 p(r==null?void 0:r.then)}function Ee(r){return p(r[z])}function Oe(r){return Symbol.asyncIterator&&p(r==null?void 0:r[Symbol.asyncIterator])}function Ie(r){return new TypeError("You provided "+(r!==null&&typeof r=="object"?"an invalid object":"'"+r+"'")+" where a stream was expected. You can provide an Observable, Promise, ReadableStream, Array, AsyncIterable, or Iterable.")}function ot(){return typeof Symbol!="function"||!Symbol.iterator?"@@iterator":Symbol.iterator}var Ae=ot();function _e(r){return p(r==null?void 0:r[Ae])}function xe(r){return Ne(this,arguments,function(){var e,n,i,o;return fe(this,function(s){switch(s.label){case 0:e=r.getReader(),s.label=1;case 1:s.trys.push([1,,9,10]),s.label=2;case 2:return[4,C(e.read())];case 3:return n=s.sent(),i=n.value,o=n.done,o?[4,C(void 0)]:[3,5];case 4:return[2,s.sent()];case 5:return[4,C(i)];case 6:return[4,s.sent()];case 7:return s.sent(),[3,2];case 8:return[3,10];case 9:return e.releaseLock(),[7];case 10:return[2]}})})}function Pe(r){return p(r==null?void 0:r.getReader)}function _(r){if(r instanceof g)return r;if(r!=null){if(Ee(r))return st(r);if(Se(r))return at(r);if(we(r))return ut(r);if(Oe(r))return Te(r);if(_e(r))return ct(r);if(Pe(r))return lt(r)}throw Ie(r)}function st(r){return new g(function(t){var e=r[z]();if(p(e.subscribe))return e.subscribe(t);throw new TypeError("Provided object does not correctly implement Symbol.observable")})}function at(r){return new g(function(t){for(var e=0;e<r.length&&!t.closed;e++)t.next(r[e]);t.complete()})}function ut(r){return new g(function(t){r.then(function(e){t.closed||(t.next(e),t.complete())},function(e){return t.error(e)}).then(null,ye)})}function ct(r){return new g(function(t){var e,n;try{for(var i=M(r),o=i.next();!o.done;o=i.next()){var s=o.value;if(t.next(s),t.closed)return}}catch(c){e={error:c}}finally{try{o&&!o.done&&(n=i.return)&&n.call(i)}finally{if(e)throw e.error}}t.complete()})}function Te(r){return new g(function(t){ft(r,t).catch(function(e){return t.error(e)})})}function lt(r){return Te(xe(r))}function ft(r,t){var e,n,i,o;return De(this,void 0,void 0,function(){var s,c;return fe(this,function(u){switch(u.label){case 0:u.trys.push([0,5,6,11]),e=Ge(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 c=u.sent(),i={error:c},[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 A(r,t,e,n,i){n===void 0&&(n=0),i===void 0&&(i=!1);var o=t.schedule(function(){e(),i?r.add(this.schedule(null,n)):this.unsubscribe()},n);if(r.add(o),!i)return o}function Ce(r,t){return t===void 0&&(t=0),T(function(e,n){e.subscribe(E(n,function(i){return A(n,r,function(){return n.next(i)},t)},function(){return A(n,r,function(){return n.complete()},t)},function(i){return A(n,r,function(){return n.error(i)},t)}))})}function Me(r,t){return t===void 0&&(t=0),T(function(e,n){n.add(r.schedule(function(){return e.subscribe(n)},t))})}function ht(r,t){return _(r).pipe(Me(t),Ce(t))}function dt(r,t){return _(r).pipe(Me(t),Ce(t))}function vt(r,t){return new g(function(e){var n=0;return t.schedule(function(){n===r.length?e.complete():(e.next(r[n++]),e.closed||this.schedule())})})}function yt(r,t){return new g(function(e){var n;return A(e,t,function(){n=r[Ae](),A(e,t,function(){var i,o,s;try{i=n.next(),o=i.value,s=i.done}catch(c){e.error(c);return}s?e.complete():e.next(o)},0,!0)}),function(){return p(n==null?void 0:n.return)&&n.return()}})}function Le(r,t){if(!r)throw new Error("Iterable cannot be null");return new g(function(e){A(e,t,function(){var n=r[Symbol.asyncIterator]();A(e,t,function(){n.next().then(function(i){i.done?e.complete():e.next(i.value)})},0,!0)})})}function pt(r,t){return Le(xe(r),t)}function bt(r,t){if(r!=null){if(Ee(r))return ht(r,t);if(Se(r))return vt(r,t);if(we(r))return dt(r,t);if(Oe(r))return Le(r,t);if(_e(r))return yt(r,t);if(Pe(r))return pt(r,t)}throw Ie(r)}function Z(r,t){return t?bt(r,t):_(r)}function mt(){for(var r=[],t=0;t<arguments.length;t++)r[t]=arguments[t];var e=ge(r);return Z(r,e)}function gt(r){return r instanceof Date&&!isNaN(r)}function H(r,t){return T(function(e,n){var i=0;e.subscribe(E(n,function(o){n.next(r.call(t,o,i++))}))})}var St=Array.isArray;function wt(r,t){return St(t)?r.apply(void 0,R([],L(t))):r(t)}function Et(r){return H(function(t){return wt(r,t)})}var Ot=Array.isArray,It=Object.getPrototypeOf,At=Object.prototype,_t=Object.keys;function xt(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"&&It(r)===At}function Tt(r,t){return r.reduce(function(e,n,i){return e[n]=t[i],e},{})}function Ct(){for(var r=[],t=0;t<arguments.length;t++)r[t]=arguments[t];var e=ge(r),n=it(r),i=xt(r),o=i.args,s=i.keys;if(o.length===0)return Z([],e);var c=new g(Mt(o,e,s?function(u){return Tt(s,u)}:F));return n?c.pipe(Et(n)):c}function Mt(r,t,e){return e===void 0&&(e=F),function(n){ae(t,function(){for(var i=r.length,o=new Array(i),s=i,c=i,u=function(l){ae(t,function(){var v=Z(r[l],t),b=!1;v.subscribe(E(n,function(f){o[l]=f,b||(b=!0,c--),c||n.next(e(o.slice()))},function(){--s||n.complete()}))},n)},a=0;a<i;a++)u(a)},n)}}function ae(r,t,e){r?A(e,r,t):t()}function Lt(r,t,e,n,i,o,s,c){var u=[],a=0,l=0,v=!1,b=function(){v&&!u.length&&!a&&t.complete()},f=function(h){return a<n?y(h):u.push(h)},y=function(h){a++;var S=!1;_(e(h,l++)).subscribe(E(t,function(w){t.next(w)},function(){S=!0},void 0,function(){if(S)try{a--;for(var w=function(){var x=u.shift();s||y(x)};u.length&&a<n;)w();b()}catch(x){t.error(x)}}))};return r.subscribe(E(t,f,function(){v=!0,b()})),function(){}}function G(r,t,e){return e===void 0&&(e=1/0),p(t)?G(function(n,i){return H(function(o,s){return t(n,o,i,s)})(_(r(n,i)))},e):(typeof t=="number"&&(e=t),T(function(n,i){return Lt(n,i,r,e)}))}function Re(r,t,e){r===void 0&&(r=0),e===void 0&&(e=rt);var n=-1;return t!=null&&(be(t)?e=t:n=t),new g(function(i){var o=gt(r)?+r-e.now():r;o<0&&(o=0);var s=0;return e.schedule(function(){i.closed||(i.next(s++),0<=n?this.schedule(void 0,n):i.complete())},o)})}function Rt(r,t){return p(t)?G(r,t,1):G(r,1)}function kt(r){return r<=0?function(){return nt}:T(function(t,e){var n=0;t.subscribe(E(e,function(i){++n<=r&&(e.next(i),r<=n&&e.complete())}))})}function jt(r){return H(function(){return r})}function $t(r,t){return G(function(e,n){return _(r(e,n)).pipe(kt(1),jt(e))})}function Ut(r,t){t===void 0&&(t=pe);var e=Re(r,t);return $t(function(){return e})}function Dt(r){var t;t={count:r};var e=t.count,n=e===void 0?1/0:e,i=t.delay,o=t.resetOnSuccess,s=o===void 0?!1:o;return n<=0?F:T(function(c,u){var a=0,l,v=function(){var b=!1;l=c.subscribe(E(u,function(f){s&&(a=0),u.next(f)},void 0,function(f){if(a++<n){var y=function(){l?(l.unsubscribe(),l=null,v()):b=!0};if(i!=null){var h=typeof i=="number"?Re(i):_(i(f,a)),S=E(u,function(){S.unsubscribe(),y()},function(){u.complete()});h.subscribe(S)}else y()}else u.error(f)})),b&&(l.unsubscribe(),l=null,v())};v()})}function Nt(r){return T(function(t,e){_(r).subscribe(E(e,function(){return e.complete()},q)),!e.closed&&t.subscribe(e)})}async function Gt(r,t){const e=r.getReader();let n;for(;!(n=await e.read()).done;)t(n.value)}function Ft(r){let t,e,n,i=!1;return function(s){t===void 0?(t=s,e=0,n=-1):t=Vt(t,s);const c=t.length;let u=0;for(;e<c;){i&&(t[e]===10&&(u=++e),i=!1);let a=-1;for(;e<c&&a===-1;++e)switch(t[e]){case 58:n===-1&&(n=e-u);break;case 13:i=!0;case 10:a=e;break}if(a===-1)break;r(t.subarray(u,a),n),u=e,n=-1}u===c?t=void 0:u!==0&&(t=t.subarray(u),e-=u)}}function Ht(r,t,e){let n=ue();const i=new TextDecoder;return function(s,c){if(s.length===0)e==null||e(n),n=ue();else if(c>0){const u=i.decode(s.subarray(0,c)),a=c+(s[c+1]===32?2:1),l=i.decode(s.subarray(a));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 v=parseInt(l,10);isNaN(v)||t(n.retry=v);break}}}}function Vt(r,t){const e=new Uint8Array(r.length+t.length);return e.set(r),e.set(t,r.length),e}function ue(){return{data:"",event:"",id:"",retry:void 0}}var Kt=function(r,t){var e={};for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&t.indexOf(n)<0&&(e[n]=r[n]);if(r!=null&&typeof Object.getOwnPropertySymbols=="function")for(var i=0,n=Object.getOwnPropertySymbols(r);i<n.length;i++)t.indexOf(n[i])<0&&Object.prototype.propertyIsEnumerable.call(r,n[i])&&(e[n[i]]=r[n[i]]);return e};const X="text/event-stream",Bt=1e3,ce="last-event-id";function Yt(r,t){var{signal:e,headers:n,onopen:i,onmessage:o,onclose:s,onerror:c,openWhenHidden:u,fetch:a}=t,l=Kt(t,["signal","headers","onopen","onmessage","onclose","onerror","openWhenHidden","fetch"]);return new Promise((v,b)=>{const f=Object.assign({},n);f.accept||(f.accept=X);let y;function h(){y.abort(),document.hidden||V()}u||document.addEventListener("visibilitychange",h);let S=Bt,w=0;function x(){document.removeEventListener("visibilitychange",h),window.clearTimeout(w),y.abort()}e==null||e.addEventListener("abort",()=>{x(),v()});const ke=a??window.fetch,je=i??qt;async function V(){var K;y=new AbortController;try{const j=await ke(r,Object.assign(Object.assign({},l),{headers:f,signal:y.signal}));await je(j),await Gt(j.body,Ft(Ht(I=>{I?f[ce]=I:delete f[ce]},I=>{S=I},o))),s==null||s(),x(),v()}catch(j){if(!y.signal.aborted)try{const I=(K=c==null?void 0:c(j))!==null&&K!==void 0?K:S;window.clearTimeout(w),w=window.setTimeout(V,I)}catch(I){x(),b(I)}}}V()})}function qt(r){const t=r.headers.get("content-type");if(!(t!=null&&t.startsWith(X)))throw new Error(`Expected content-type to be ${X}, Actual: ${t}`)}function Wt(r){const{endpoint:t,apiKey:e,payload:n,debugMode:i}=r;return new g(o=>{const s=new AbortController,c={"Content-Type":"application/json"};e&&(c["X-API-KEY"]=e);const u=new URLSearchParams;i&&u.set("is_debug","true");const a=new URL(t);return u.toString()&&(a.search=u.toString()),Yt(a.toString(),{method:"POST",headers:c,body:n?JSON.stringify(n):void 0,signal:s.signal,openWhenHidden:!0,onopen:async l=>{l.ok||(o.error(l),s.abort())},onmessage:l=>{o.next(JSON.parse(l.data))},onclose:()=>{o.complete()},onerror:l=>{throw o.error(l),s.abort(),l}}),()=>{s.abort()}})}class Xt{constructor(){m(this,"listeners",{})}on(t,e){this.listeners=Object.assign({},this.listeners,{[t]:(this.listeners[t]??[]).concat(e)})}off(t,e){this.listeners[t]&&(this.listeners=Object.assign({},this.listeners,{[t]:(this.listeners[t]??[]).filter(n=>n!==e)}))}remove(t){delete this.listeners[t]}emit(t,...e){this.listeners[t]&&this.listeners[t].forEach(n=>n(...e))}}class Jt{constructor(t){m(this,"apiKey");m(this,"endpoint");m(this,"botProviderEndpoint");m(this,"debugMode");m(this,"destroy$",new Q);m(this,"sseEmitter",new Xt);m(this,"transformSsePayload");if(!t.endpoint&&!t.botProviderEndpoint)throw new Error("Either endpoint or botProviderEndpoint must be provided");if(this.apiKey=t.apiKey,this.debugMode=t.debugMode,this.transformSsePayload=t.transformSsePayload,this.botProviderEndpoint=t.botProviderEndpoint,!t.endpoint&&t.botProviderEndpoint){const e=t.botProviderEndpoint.replace(/\/+$/,"");this.endpoint=`${e}/message/sse`}else t.endpoint&&(this.endpoint=t.endpoint,this.debugMode&&console.warn('[AsgardServiceClient] The "endpoint" option is deprecated and will be removed in the next major version. Please use "botProviderEndpoint" instead. The SSE endpoint will be automatically derived as "${botProviderEndpoint}/message/sse".'))}on(t,e){this.sseEmitter.remove(t),this.sseEmitter.on(t,e)}handleEvent(t){switch(t.eventType){case d.INIT:this.sseEmitter.emit(d.INIT,t);break;case d.PROCESS_START:case d.PROCESS_COMPLETE:this.sseEmitter.emit(d.PROCESS,t);break;case d.MESSAGE_START:case d.MESSAGE_DELTA:case d.MESSAGE_COMPLETE:this.sseEmitter.emit(d.MESSAGE,t);break;case d.TOOL_CALL_START:case d.TOOL_CALL_COMPLETE:this.sseEmitter.emit(d.TOOL_CALL,t);break;case d.DONE:this.sseEmitter.emit(d.DONE,t);break;case d.ERROR:this.sseEmitter.emit(d.ERROR,t);break}}fetchSse(t,e){var n,i;(n=e==null?void 0:e.onSseStart)==null||n.call(e),Wt({apiKey:this.apiKey,endpoint:this.endpoint,debugMode:this.debugMode,payload:((i=this.transformSsePayload)==null?void 0:i.call(this,t))??t}).pipe(Rt(o=>mt(o).pipe(Ut((e==null?void 0:e.delayTime)??50))),Nt(this.destroy$),Dt(3)).subscribe({next:o=>{var s;(s=e==null?void 0:e.onSseMessage)==null||s.call(e,o),this.handleEvent(o)},error:o=>{var s;(s=e==null?void 0:e.onSseError)==null||s.call(e,o)},complete:()=>{var o;(o=e==null?void 0:e.onSseCompleted)==null||o.call(e)}})}close(){this.destroy$.next(),this.destroy$.complete()}async uploadFile(t,e){const n=this.deriveBlobEndpoint();if(!n)throw new Error("Unable to derive blob endpoint. Please provide botProviderEndpoint in config.");const i=new FormData;i.append("file",t),i.append("customChannelId",e);const o={};this.apiKey&&(o["X-API-KEY"]=this.apiKey);try{const s=await fetch(n,{method:"POST",headers:o,body:i});if(!s.ok)throw new Error(`Upload failed: ${s.status} ${s.statusText}`);const c=await s.json();return this.debugMode&&console.log("[AsgardServiceClient] File upload response:",c),c}catch(s){throw console.error("[AsgardServiceClient] File upload error:",s),s}}deriveBlobEndpoint(){if(!this.botProviderEndpoint&&!this.endpoint)return null;let t=this.botProviderEndpoint;if(!t&&this.endpoint&&(t=this.endpoint.replace("/message/sse","")),!t)return null;if(t=t.replace(/\/+$/,""),!t.includes("/generic/")){const e=t.match(/^(https?:\/\/[^/]+)(\/.*)/);if(e){const[,n,i]=e;t=`${n}/generic${i}`}}return`${t}/blob`}}class ee{constructor(t){m(this,"client");m(this,"customChannelId");m(this,"customMessageId");m(this,"isConnecting$");m(this,"conversation$");m(this,"statesObserver");m(this,"statesSubscription");if(!t.client)throw new Error("client must be required");if(!t.customChannelId)throw new Error("customChannelId must be required");this.client=t.client,this.customChannelId=t.customChannelId,this.customMessageId=t.customMessageId,this.isConnecting$=new ie(!1),this.conversation$=new ie(t.conversation),this.statesObserver=t.statesObserver}static async reset(t,e,n){const i=new ee(t);try{return i.subscribe(),await i.resetChannel(e,n),i}catch(o){throw i.close(),o}}subscribe(){this.statesSubscription=Ct([this.isConnecting$,this.conversation$]).pipe(H(([t,e])=>({isConnecting:t,conversation:e}))).subscribe(this.statesObserver)}fetchSse(t,e){return new Promise((n,i)=>{this.isConnecting$.next(!0),this.client.fetchSse(t,{onSseStart:e==null?void 0:e.onSseStart,onSseMessage:o=>{var s;(s=e==null?void 0:e.onSseMessage)==null||s.call(e,o),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),i(o)},onSseCompleted:()=>{var o;(o=e==null?void 0:e.onSseCompleted)==null||o.call(e),this.isConnecting$.next(!1),n()}})})}resetChannel(t,e){return this.fetchSse({action:D.RESET_CHANNEL,customChannelId:this.customChannelId,customMessageId:this.customMessageId,text:(t==null?void 0:t.text)||"",payload:t==null?void 0:t.payload},e)}sendMessage(t,e){const n=t.text.trim(),i=t.customMessageId??crypto.randomUUID();return this.conversation$.next(this.conversation$.value.pushMessage({type:"user",messageId:i,text:n,blobIds:t.blobIds,filePreviewUrls:t.filePreviewUrls,time:new Date})),this.fetchSse({action:D.NONE,customChannelId:this.customChannelId,customMessageId:i,payload:t==null?void 0:t.payload,text:n,blobIds:t==null?void 0:t.blobIds},e)}close(){var t;this.isConnecting$.complete(),this.conversation$.complete(),(t=this.statesSubscription)==null||t.unsubscribe()}}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 d.MESSAGE_START:return this.onMessageStart(t);case d.MESSAGE_DELTA:return this.onMessageDelta(t);case d.MESSAGE_COMPLETE:return this.onMessageComplete(t);case d.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:d.MESSAGE_START,isTyping:!0,typingText:"",messageId:e.messageId,message:e,time:new Date}),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:d.MESSAGE_DELTA,isTyping:!0,typingText:o,messageId:e.messageId,message:e,time:new Date}),new P({messages:n})}onMessageComplete(t){const e=t.fact.messageComplete.message,n=new Map(this.messages);return n.set(e.messageId,{type:"bot",eventType:d.MESSAGE_COMPLETE,isTyping:!1,typingText:null,messageId:e.messageId,message:e,time:new Date}),new P({messages:n})}onMessageError(t){const e=crypto.randomUUID(),n=t.fact.runError.error,i=new Map(this.messages);return i.set(e,{type:"error",eventType:d.ERROR,messageId:e,error:n,time:new Date}),new P({messages:i})}}exports.AsgardServiceClient=Jt;exports.Channel=ee;exports.Conversation=P;exports.EventType=d;exports.FetchSseAction=D;exports.MessageTemplateType=le;
5
5
  //# sourceMappingURL=index.cjs.map