@asgard-js/core 0.0.24 → 0.0.26

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
@@ -1,24 +1,13 @@
1
- # core
2
-
3
- This library was generated with [Nx](https://nx.dev).
4
-
5
- ## Building
6
-
7
- Run `nx build core` to build the library.
8
-
9
- ## Running unit tests
10
-
11
- Run `nx test core` to execute the unit tests via [Vitest](https://vitest.dev/).
12
1
  # AsgardJs Core
13
2
 
14
- This package contains the core functionalities of the AsgardJs SDK, providing essential tools for interacting with the Asgard AI platform.
3
+ This package contains the core functionalities of the AsgardJs SDK, providing essential tools for interacting with the Asgard AI platform through Server-Sent Events (SSE) and conversation management.
15
4
 
16
5
  ## Installation
17
6
 
18
7
  To install the core package, use the following command:
19
8
 
20
9
  ```sh
21
- yarn add asgardjs-core
10
+ yarn add @asgard-js/core
22
11
  ```
23
12
 
24
13
  ## Usage
@@ -26,49 +15,324 @@ yarn add asgardjs-core
26
15
  Here's a basic example of how to use the core package:
27
16
 
28
17
  ```javascript
29
- import { AsgardServiceClient } from 'asgardjs-core';
18
+ import { AsgardServiceClient } from '@asgard-js/core';
30
19
 
31
20
  const client = new AsgardServiceClient({
32
21
  apiKey: 'your-api-key',
33
- endpoint: 'https://api.asgard-ai.com',
22
+ botProviderEndpoint:
23
+ 'https://api.asgard-ai.com/ns/{namespace}/bot-provider/{botProviderId}',
24
+ debugMode: true, // Enable to see deprecation warnings
34
25
  });
35
26
 
36
- // Use the client to send messages
37
- client.sendMessage({
27
+ // Use the client to send messages via SSE
28
+ client.fetchSse({
38
29
  customChannelId: 'your-channel-id',
39
30
  text: 'Hello, Asgard!',
31
+ action: 'message',
32
+ });
33
+
34
+ // Listen to events
35
+ client.on('MESSAGE', (response) => {
36
+ console.log('Received message:', response);
37
+ });
38
+
39
+ client.on('DONE', (response) => {
40
+ console.log('Conversation completed:', response);
41
+ });
42
+
43
+ client.on('ERROR', (error) => {
44
+ console.error('Error occurred:', error);
45
+ });
46
+ ```
47
+
48
+ ## Migration from `endpoint` to `botProviderEndpoint`
49
+
50
+ **Important**: The `endpoint` configuration option is deprecated. Use `botProviderEndpoint` instead for simplified configuration.
51
+
52
+ ### Before (Deprecated)
53
+
54
+ ```javascript
55
+ const client = new AsgardServiceClient({
56
+ apiKey: 'your-api-key',
57
+ endpoint:
58
+ 'https://api.asgard-ai.com/ns/{namespace}/bot-provider/{botProviderId}/message/sse',
59
+ botProviderEndpoint:
60
+ 'https://api.asgard-ai.com/ns/{namespace}/bot-provider/{botProviderId}',
61
+ });
62
+ ```
63
+
64
+ ### After (Recommended)
65
+
66
+ ```javascript
67
+ const client = new AsgardServiceClient({
68
+ apiKey: 'your-api-key',
69
+ botProviderEndpoint:
70
+ 'https://api.asgard-ai.com/ns/{namespace}/bot-provider/{botProviderId}',
71
+ // SSE endpoint is automatically derived as: botProviderEndpoint + '/message/sse'
72
+ });
73
+ ```
74
+
75
+ **Benefits:**
76
+
77
+ - Simplified configuration with single endpoint
78
+ - Reduced chance of configuration errors
79
+ - Automatic endpoint derivation
80
+
81
+ **Backward Compatibility:** Existing code using `endpoint` will continue to work but may show deprecation warnings when `debugMode` is enabled.
82
+
83
+ ## API Reference
84
+
85
+ The core package exports three main classes for different levels of abstraction:
86
+
87
+ ### AsgardServiceClient
88
+
89
+ The main client class for interacting with the Asgard AI platform.
90
+
91
+ #### Constructor Options (ClientConfig)
92
+
93
+ - **apiKey**: `string` (required) - API key for authentication
94
+ - **botProviderEndpoint**: `string` (required) - Bot provider endpoint URL (SSE endpoint will be auto-derived)
95
+ - **endpoint?**: `string` (deprecated) - Legacy API endpoint URL. Use `botProviderEndpoint` instead.
96
+ - **debugMode?**: `boolean` - Enable debug mode for deprecation warnings, defaults to `false`
97
+ - **transformSsePayload?**: `(payload: FetchSsePayload) => FetchSsePayload` - SSE payload transformer
98
+ - **onRunInit?**: `InitEventHandler` - Handler for run initialization events
99
+ - **onMessage?**: `MessageEventHandler` - Handler for message events
100
+ - **onToolCall?**: `ToolCallEventHandler` - Handler for tool call events
101
+ - **onProcess?**: `ProcessEventHandler` - Handler for process events
102
+ - **onRunDone?**: `DoneEventHandler` - Handler for run completion events
103
+ - **onRunError?**: `ErrorEventHandler` - Error handler for execution errors
104
+
105
+ #### Methods
106
+
107
+ - **fetchSse(payload, options?)**: Send a message via Server-Sent Events
108
+ - **on(event, handler)**: Listen to specific SSE events
109
+ - **close()**: Close the SSE connection and cleanup resources
110
+
111
+ #### Event Types
112
+
113
+ - **INIT**: Run initialization events
114
+ - **MESSAGE**: Message events (start, delta, complete)
115
+ - **TOOL_CALL**: Tool call events (start, complete)
116
+ - **PROCESS**: Process events (start, complete)
117
+ - **DONE**: Run completion events
118
+ - **ERROR**: Error events
119
+
120
+ ### Channel
121
+
122
+ Higher-level abstraction for managing a conversation channel with reactive state management using RxJS.
123
+
124
+ #### Static Methods
125
+
126
+ - **Channel.reset(config, payload?, options?)**: `Promise<Channel>` - Create and initialize a new channel
127
+
128
+ #### Instance Methods
129
+
130
+ - **sendMessage(payload, options?)**: `Promise<void>` - Send a message through the channel
131
+ - **close()**: `void` - Close the channel and cleanup subscriptions
132
+
133
+ #### Configuration (ChannelConfig)
134
+
135
+ - **client**: `IAsgardServiceClient` - Instance of AsgardServiceClient
136
+ - **customChannelId**: `string` - Unique channel identifier
137
+ - **customMessageId?**: `string` - Optional message ID
138
+ - **conversation**: `Conversation` - Initial conversation state
139
+ - **statesObserver?**: `ObserverOrNext<ChannelStates>` - Observer for channel state changes
140
+
141
+ #### Properties
142
+
143
+ - **customChannelId**: `string` - The channel identifier
144
+ - **customMessageId?**: `string` - Optional message identifier
145
+
146
+ #### Example Usage
147
+
148
+ ```javascript
149
+ import { AsgardServiceClient, Channel, Conversation } from '@asgard-js/core';
150
+
151
+ const client = new AsgardServiceClient({
152
+ botProviderEndpoint: 'https://api.example.com/bot-provider/123',
153
+ apiKey: 'your-api-key',
154
+ });
155
+
156
+ const conversation = new Conversation({ messages: new Map() });
157
+
158
+ const channel = await Channel.reset({
159
+ client,
160
+ customChannelId: 'channel-123',
161
+ conversation,
162
+ statesObserver: (states) => {
163
+ console.log('Connection status:', states.isConnecting);
164
+ console.log('Messages:', Array.from(states.conversation.messages.values()));
165
+ },
166
+ });
167
+
168
+ // Send a message
169
+ await channel.sendMessage({ text: 'Hello, bot!' });
170
+ ```
171
+
172
+ ### Conversation
173
+
174
+ Immutable conversation state manager that handles message updates and SSE event processing.
175
+
176
+ #### Constructor
177
+
178
+ - **constructor(options)**: Initialize conversation with `{messages: Map<string, ConversationMessage> | null}`
179
+
180
+ #### Methods
181
+
182
+ - **pushMessage(message)**: `Conversation` - Add a new message (returns new instance)
183
+ - **onMessage(response)**: `Conversation` - Process SSE response and update conversation
184
+
185
+ #### Properties
186
+
187
+ - **messages**: `Map<string, ConversationMessage> | null` - Map of all messages in the conversation
188
+
189
+ #### Message Types
190
+
191
+ - **ConversationUserMessage**: User-sent messages with `text` and `time`
192
+ - **ConversationBotMessage**: Bot responses with `message`, `isTyping`, `typingText`, `eventType`
193
+ - **ConversationErrorMessage**: Error messages with `error` details
194
+
195
+ #### Example Usage
196
+
197
+ ```javascript
198
+ import { Conversation } from '@asgard-js/core';
199
+
200
+ // Create new conversation
201
+ const conversation = new Conversation({ messages: new Map() });
202
+
203
+ // Add a user message
204
+ const userMessage = {
205
+ id: 'msg-1',
206
+ type: 'user',
207
+ text: 'Hello',
208
+ time: Date.now(),
209
+ };
210
+
211
+ const updatedConversation = conversation.pushMessage(userMessage);
212
+ console.log('Messages:', Array.from(updatedConversation.messages.values()));
213
+ ```
214
+
215
+ ## Testing
216
+
217
+ The core package includes comprehensive tests using Vitest.
218
+
219
+ ### Running Tests
220
+
221
+ ```sh
222
+ # Run tests once
223
+ npm test
224
+ # or
225
+ yarn test:core
226
+
227
+ # Run tests in watch mode
228
+ npm run test:watch
229
+ # or
230
+ yarn test:core:watch
231
+
232
+ # Run tests with UI
233
+ npm run test:ui
234
+ # or
235
+ yarn test:core:ui
236
+
237
+ # Run tests with coverage
238
+ npm run test:coverage
239
+ # or
240
+ yarn test:core:coverage
241
+ ```
242
+
243
+ ### Test Structure
244
+
245
+ Tests are located alongside source files with `.spec.ts` extensions:
246
+
247
+ - `src/lib/client.spec.ts` - Tests for AsgardServiceClient including deprecation scenarios
248
+ - Test environment: jsdom with Vitest
249
+ - Coverage reports available in `test-output/vitest/coverage/`
250
+
251
+ ### Writing Tests
252
+
253
+ The package uses Vitest for testing with the following setup:
254
+
255
+ - TypeScript support
256
+ - jsdom environment for DOM APIs
257
+ - ESLint integration
258
+ - Coverage reporting with v8 provider
259
+
260
+ Example test structure:
261
+
262
+ ```javascript
263
+ import { describe, it, expect } from 'vitest';
264
+ import { AsgardServiceClient } from './client';
265
+
266
+ describe('AsgardServiceClient', () => {
267
+ it('should create client with botProviderEndpoint', () => {
268
+ const client = new AsgardServiceClient({
269
+ botProviderEndpoint: 'https://api.example.com/bot-provider/bp-123',
270
+ apiKey: 'test-key',
271
+ });
272
+
273
+ expect(client).toBeDefined();
274
+ });
40
275
  });
41
276
  ```
42
277
 
43
278
  ## Development
44
279
 
45
- To start developing the core package, follow these steps:
280
+ To develop the core package locally, follow these steps:
281
+
282
+ 1. Clone the repository and navigate to the project root directory.
46
283
 
47
- 1. Clone the repository and navigate to the `packages/core` directory.
48
284
  2. Install dependencies:
49
285
 
50
286
  ```sh
51
287
  yarn install
52
288
  ```
53
289
 
54
- 3. Build the package:
290
+ 3. Start development:
291
+
292
+ You can use the following commands to work with the core package:
55
293
 
56
294
  ```sh
57
- yarn build
295
+ # Lint the core package
296
+ yarn lint:core
297
+
298
+ # Run tests
299
+ yarn test:core
300
+
301
+ # Build the package
302
+ yarn build:core
303
+
304
+ # Watch mode for development
305
+ yarn watch:core
58
306
  ```
59
307
 
60
- 4. Run tests to ensure everything is working:
308
+ Setup your npm release token:
61
309
 
62
310
  ```sh
63
- yarn test
311
+ cd ~/
312
+ touch .npmrc
313
+ echo "//registry.npmjs.org/:_authToken={{YOUR_TOKEN}}" >> .npmrc
64
314
  ```
65
315
 
66
- 5. Start the development server:
316
+ For working with both core and React packages:
67
317
 
68
318
  ```sh
69
- yarn start
319
+ # Lint both packages
320
+ yarn lint:packages
321
+
322
+ # Test both packages
323
+ yarn test
324
+
325
+ # Build core package (required for React package)
326
+ yarn build:core
327
+ yarn build:react
328
+
329
+ # Release packages
330
+ yarn release:core # Release core package
331
+ yarn release:react # Release React package
70
332
  ```
71
333
 
334
+ All builds will be available in the `dist` directory.
335
+
72
336
  ## Contributing
73
337
 
74
338
  We welcome contributions! Please read our [contributing guide](../../CONTRIBUTING.md) to get started.
package/dist/index.cjs CHANGED
@@ -1,5 +1,5 @@
1
- "use strict";var Ue=Object.defineProperty;var Ne=(n,t,e)=>t in n?Ue(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e;var m=(n,t,e)=>Ne(n,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var N=(n=>(n.RESET_CHANNEL="RESET_CHANNEL",n.NONE="NONE",n))(N||{}),h=(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.DONE="asgard.run.done",n.ERROR="asgard.run.error",n))(h||{}),fe=(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))(fe||{}),Y=function(n,t){return Y=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,r){e.__proto__=r}||function(e,r){for(var i in r)Object.prototype.hasOwnProperty.call(r,i)&&(e[i]=r[i])},Y(n,t)};function O(n,t){if(typeof t!="function"&&t!==null)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");Y(n,t);function e(){this.constructor=n}n.prototype=t===null?Object.create(t):(e.prototype=t.prototype,new e)}function $e(n,t,e,r){function i(o){return o instanceof e?o:new e(function(s){s(o)})}return new(e||(e=Promise))(function(o,s){function c(f){try{u(r.next(f))}catch(y){s(y)}}function a(f){try{u(r.throw(f))}catch(y){s(y)}}function u(f){f.done?o(f.value):i(f.value).then(c,a)}u((r=r.apply(n,t||[])).next())})}function le(n,t){var e={label:0,sent:function(){if(o[0]&1)throw o[1];return o[1]},trys:[],ops:[]},r,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(u){return function(f){return a([u,f])}}function a(u){if(r)throw new TypeError("Generator is already executing.");for(;s&&(s=0,u[0]&&(e=0)),e;)try{if(r=1,i&&(o=u[0]&2?i.return:u[0]?i.throw||((o=i.return)&&o.call(i),0):i.next)&&!(o=o.call(i,u[1])).done)return o;switch(i=0,o&&(u=[u[0]&2,o.value]),u[0]){case 0:case 1:o=u;break;case 4:return e.label++,{value:u[1],done:!1};case 5:e.label++,i=u[1],u=[0];continue;case 7:u=e.ops.pop(),e.trys.pop();continue;default:if(o=e.trys,!(o=o.length>0&&o[o.length-1])&&(u[0]===6||u[0]===2)){e=0;continue}if(u[0]===3&&(!o||u[1]>o[0]&&u[1]<o[3])){e.label=u[1];break}if(u[0]===6&&e.label<o[1]){e.label=o[1],o=u;break}if(o&&e.label<o[2]){e.label=o[2],e.ops.push(u);break}o[2]&&e.ops.pop(),e.trys.pop();continue}u=t.call(n,e)}catch(f){u=[6,f],i=0}finally{r=o=0}if(u[0]&5)throw u[1];return{value:u[0]?u[1]:void 0,done:!0}}}function M(n){var t=typeof Symbol=="function"&&Symbol.iterator,e=t&&n[t],r=0;if(e)return e.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(t?"Object is not iterable.":"Symbol.iterator is not defined.")}function L(n,t){var e=typeof Symbol=="function"&&n[Symbol.iterator];if(!e)return n;var r=e.call(n),i,o=[],s;try{for(;(t===void 0||t-- >0)&&!(i=r.next()).done;)o.push(i.value)}catch(c){s={error:c}}finally{try{i&&!i.done&&(e=r.return)&&e.call(r)}finally{if(s)throw s.error}}return o}function R(n,t,e){if(e||arguments.length===2)for(var r=0,i=t.length,o;r<i;r++)(o||!(r in t))&&(o||(o=Array.prototype.slice.call(t,0,r)),o[r]=t[r]);return n.concat(o||Array.prototype.slice.call(t))}function P(n){return this instanceof P?(this.v=n,this):new P(n)}function Ge(n,t,e){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var r=e.apply(n,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(l){return function(v){return Promise.resolve(v).then(l,y)}}function c(l,v){r[l]&&(i[l]=function(d){return new Promise(function(S,w){o.push([l,d,S,w])>1||a(l,d)})},v&&(i[l]=v(i[l])))}function a(l,v){try{u(r[l](v))}catch(d){b(o[0][3],d)}}function u(l){l.value instanceof P?Promise.resolve(l.value.v).then(f,y):b(o[0][2],l)}function f(l){a("next",l)}function y(l){a("throw",l)}function b(l,v){l(v),o.shift(),o.length&&a(o[0][0],o[0][1])}}function Fe(n){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var t=n[Symbol.asyncIterator],e;return t?t.call(n):(n=typeof M=="function"?M(n):n[Symbol.iterator](),e={},r("next"),r("throw"),r("return"),e[Symbol.asyncIterator]=function(){return this},e);function r(o){e[o]=n[o]&&function(s){return new Promise(function(c,a){s=n[o](s),i(c,a,s.done,s.value)})}}function i(o,s,c,a){Promise.resolve(a).then(function(u){o({value:u,done:c})},s)}}function p(n){return typeof n=="function"}function de(n){var t=function(r){Error.call(r),r.stack=new Error().stack},e=n(t);return e.prototype=Object.create(Error.prototype),e.prototype.constructor=e,e}var K=de(function(n){return function(e){n(this),this.message=e?e.length+` errors occurred during unsubscription:
1
+ "use strict";var Ue=Object.defineProperty;var Ne=(n,t,e)=>t in n?Ue(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e;var m=(n,t,e)=>Ne(n,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var N=(n=>(n.RESET_CHANNEL="RESET_CHANNEL",n.NONE="NONE",n))(N||{}),h=(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.DONE="asgard.run.done",n.ERROR="asgard.run.error",n))(h||{}),fe=(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))(fe||{}),Y=function(n,t){return Y=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,r){e.__proto__=r}||function(e,r){for(var i in r)Object.prototype.hasOwnProperty.call(r,i)&&(e[i]=r[i])},Y(n,t)};function O(n,t){if(typeof t!="function"&&t!==null)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");Y(n,t);function e(){this.constructor=n}n.prototype=t===null?Object.create(t):(e.prototype=t.prototype,new e)}function $e(n,t,e,r){function i(o){return o instanceof e?o:new e(function(s){s(o)})}return new(e||(e=Promise))(function(o,s){function c(f){try{u(r.next(f))}catch(y){s(y)}}function a(f){try{u(r.throw(f))}catch(y){s(y)}}function u(f){f.done?o(f.value):i(f.value).then(c,a)}u((r=r.apply(n,t||[])).next())})}function le(n,t){var e={label:0,sent:function(){if(o[0]&1)throw o[1];return o[1]},trys:[],ops:[]},r,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(u){return function(f){return a([u,f])}}function a(u){if(r)throw new TypeError("Generator is already executing.");for(;s&&(s=0,u[0]&&(e=0)),e;)try{if(r=1,i&&(o=u[0]&2?i.return:u[0]?i.throw||((o=i.return)&&o.call(i),0):i.next)&&!(o=o.call(i,u[1])).done)return o;switch(i=0,o&&(u=[u[0]&2,o.value]),u[0]){case 0:case 1:o=u;break;case 4:return e.label++,{value:u[1],done:!1};case 5:e.label++,i=u[1],u=[0];continue;case 7:u=e.ops.pop(),e.trys.pop();continue;default:if(o=e.trys,!(o=o.length>0&&o[o.length-1])&&(u[0]===6||u[0]===2)){e=0;continue}if(u[0]===3&&(!o||u[1]>o[0]&&u[1]<o[3])){e.label=u[1];break}if(u[0]===6&&e.label<o[1]){e.label=o[1],o=u;break}if(o&&e.label<o[2]){e.label=o[2],e.ops.push(u);break}o[2]&&e.ops.pop(),e.trys.pop();continue}u=t.call(n,e)}catch(f){u=[6,f],i=0}finally{r=o=0}if(u[0]&5)throw u[1];return{value:u[0]?u[1]:void 0,done:!0}}}function M(n){var t=typeof Symbol=="function"&&Symbol.iterator,e=t&&n[t],r=0;if(e)return e.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(t?"Object is not iterable.":"Symbol.iterator is not defined.")}function L(n,t){var e=typeof Symbol=="function"&&n[Symbol.iterator];if(!e)return n;var r=e.call(n),i,o=[],s;try{for(;(t===void 0||t-- >0)&&!(i=r.next()).done;)o.push(i.value)}catch(c){s={error:c}}finally{try{i&&!i.done&&(e=r.return)&&e.call(r)}finally{if(s)throw s.error}}return o}function R(n,t,e){if(e||arguments.length===2)for(var r=0,i=t.length,o;r<i;r++)(o||!(r in t))&&(o||(o=Array.prototype.slice.call(t,0,r)),o[r]=t[r]);return n.concat(o||Array.prototype.slice.call(t))}function P(n){return this instanceof P?(this.v=n,this):new P(n)}function Ge(n,t,e){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var r=e.apply(n,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(l){return function(v){return Promise.resolve(v).then(l,y)}}function c(l,v){r[l]&&(i[l]=function(d){return new Promise(function(g,w){o.push([l,d,g,w])>1||a(l,d)})},v&&(i[l]=v(i[l])))}function a(l,v){try{u(r[l](v))}catch(d){b(o[0][3],d)}}function u(l){l.value instanceof P?Promise.resolve(l.value.v).then(f,y):b(o[0][2],l)}function f(l){a("next",l)}function y(l){a("throw",l)}function b(l,v){l(v),o.shift(),o.length&&a(o[0][0],o[0][1])}}function Fe(n){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var t=n[Symbol.asyncIterator],e;return t?t.call(n):(n=typeof M=="function"?M(n):n[Symbol.iterator](),e={},r("next"),r("throw"),r("return"),e[Symbol.asyncIterator]=function(){return this},e);function r(o){e[o]=n[o]&&function(s){return new Promise(function(c,a){s=n[o](s),i(c,a,s.done,s.value)})}}function i(o,s,c,a){Promise.resolve(a).then(function(u){o({value:u,done:c})},s)}}function p(n){return typeof n=="function"}function de(n){var t=function(r){Error.call(r),r.stack=new Error().stack},e=n(t);return e.prototype=Object.create(Error.prototype),e.prototype.constructor=e,e}var K=de(function(n){return function(e){n(this),this.message=e?e.length+` errors occurred during unsubscription:
2
2
  `+e.map(function(r,i){return i+1+") "+r.toString()}).join(`
3
- `):"",this.name="UnsubscriptionError",this.errors=e}});function $(n,t){if(n){var e=n.indexOf(t);0<=e&&n.splice(e,1)}}var k=function(){function n(t){this.initialTeardown=t,this.closed=!1,this._parentage=null,this._finalizers=null}return n.prototype.unsubscribe=function(){var t,e,r,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),a=c.next();!a.done;a=c.next()){var u=a.value;u.remove(this)}}catch(d){t={error:d}}finally{try{a&&!a.done&&(e=c.return)&&e.call(c)}finally{if(t)throw t.error}}else s.remove(this);var f=this.initialTeardown;if(p(f))try{f()}catch(d){o=d instanceof K?d.errors:[d]}var y=this._finalizers;if(y){this._finalizers=null;try{for(var b=M(y),l=b.next();!l.done;l=b.next()){var v=l.value;try{te(v)}catch(d){o=o??[],d instanceof K?o=R(R([],L(o)),L(d.errors)):o.push(d)}}}catch(d){r={error:d}}finally{try{l&&!l.done&&(i=b.return)&&i.call(b)}finally{if(r)throw r.error}}}if(o)throw new K(o)}},n.prototype.add=function(t){var e;if(t&&t!==this)if(this.closed)te(t);else{if(t instanceof n){if(t.closed||t._hasParent(this))return;t._addParent(this)}(this._finalizers=(e=this._finalizers)!==null&&e!==void 0?e:[]).push(t)}},n.prototype._hasParent=function(t){var e=this._parentage;return e===t||Array.isArray(e)&&e.includes(t)},n.prototype._addParent=function(t){var e=this._parentage;this._parentage=Array.isArray(e)?(e.push(t),e):e?[e,t]:t},n.prototype._removeParent=function(t){var e=this._parentage;e===t?this._parentage=null:Array.isArray(e)&&$(e,t)},n.prototype.remove=function(t){var e=this._finalizers;e&&$(e,t),t instanceof n&&t._removeParent(this)},n.EMPTY=function(){var t=new n;return t.closed=!0,t}(),n}(),he=k.EMPTY;function ye(n){return n instanceof k||n&&"closed"in n&&p(n.remove)&&p(n.add)&&p(n.unsubscribe)}function te(n){p(n)?n():n.unsubscribe()}var He={onUnhandledError:null,onStoppedNotification:null,Promise:void 0,useDeprecatedSynchronousErrorHandling:!1,useDeprecatedNextContext:!1},ve={setTimeout:function(n,t){for(var e=[],r=2;r<arguments.length;r++)e[r-2]=arguments[r];return setTimeout.apply(void 0,R([n,t],L(e)))},clearTimeout:function(n){var t=ve.delegate;return((t==null?void 0:t.clearTimeout)||clearTimeout)(n)},delegate:void 0};function pe(n){ve.setTimeout(function(){throw n})}function q(){}function U(n){n()}var X=function(n){O(t,n);function t(e){var r=n.call(this)||this;return r.isStopped=!1,e?(r.destination=e,ye(e)&&e.add(r)):r.destination=Ke,r}return t.create=function(e,r,i){return new W(e,r,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,n.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 n(t){this.partialObserver=t}return n.prototype.next=function(t){var e=this.partialObserver;if(e.next)try{e.next(t)}catch(r){D(r)}},n.prototype.error=function(t){var e=this.partialObserver;if(e.error)try{e.error(t)}catch(r){D(r)}else D(t)},n.prototype.complete=function(){var t=this.partialObserver;if(t.complete)try{t.complete()}catch(e){D(e)}},n}(),W=function(n){O(t,n);function t(e,r,i){var o=n.call(this)||this,s;return p(e)||!e?s={next:e??void 0,error:r??void 0,complete:i??void 0}:s=e,o.destination=new Ve(s),o}return t}(X);function D(n){pe(n)}function Be(n){throw n}var Ke={closed:!0,next:q,error:Be,complete:q},z=function(){return typeof Symbol=="function"&&Symbol.observable||"@@observable"}();function F(n){return n}function Ye(n){return n.length===0?F:n.length===1?n[0]:function(e){return n.reduce(function(r,i){return i(r)},e)}}var g=function(){function n(t){t&&(this._subscribe=t)}return n.prototype.lift=function(t){var e=new n;return e.source=this,e.operator=t,e},n.prototype.subscribe=function(t,e,r){var i=this,o=We(t)?t:new W(t,e,r);return U(function(){var s=i,c=s.operator,a=s.source;o.add(c?c.call(o,a):a?i._subscribe(o):i._trySubscribe(o))}),o},n.prototype._trySubscribe=function(t){try{return this._subscribe(t)}catch(e){t.error(e)}},n.prototype.forEach=function(t,e){var r=this;return e=ne(e),new e(function(i,o){var s=new W({next:function(c){try{t(c)}catch(a){o(a),s.unsubscribe()}},error:o,complete:i});r.subscribe(s)})},n.prototype._subscribe=function(t){var e;return(e=this.source)===null||e===void 0?void 0:e.subscribe(t)},n.prototype[z]=function(){return this},n.prototype.pipe=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return Ye(t)(this)},n.prototype.toPromise=function(t){var e=this;return t=ne(t),new t(function(r,i){var o;e.subscribe(function(s){return o=s},function(s){return i(s)},function(){return r(o)})})},n.create=function(t){return new n(t)},n}();function ne(n){var t;return(t=n??He.Promise)!==null&&t!==void 0?t:Promise}function qe(n){return n&&p(n.next)&&p(n.error)&&p(n.complete)}function We(n){return n&&n instanceof X||qe(n)&&ye(n)}function Je(n){return p(n==null?void 0:n.lift)}function C(n){return function(t){if(Je(t))return t.lift(function(e){try{return n(e,this)}catch(r){this.error(r)}});throw new TypeError("Unable to lift unknown Observable type")}}function E(n,t,e,r,i){return new Xe(n,t,e,r,i)}var Xe=function(n){O(t,n);function t(e,r,i,o,s,c){var a=n.call(this,e)||this;return a.onFinalize=s,a.shouldUnsubscribe=c,a._next=r?function(u){try{r(u)}catch(f){e.error(f)}}:n.prototype._next,a._error=o?function(u){try{o(u)}catch(f){e.error(f)}finally{this.unsubscribe()}}:n.prototype._error,a._complete=i?function(){try{i()}catch(u){e.error(u)}finally{this.unsubscribe()}}:n.prototype._complete,a}return t.prototype.unsubscribe=function(){var e;if(!this.shouldUnsubscribe||this.shouldUnsubscribe()){var r=this.closed;n.prototype.unsubscribe.call(this),!r&&((e=this.onFinalize)===null||e===void 0||e.call(this))}},t}(X),ze=de(function(n){return function(){n(this),this.name="ObjectUnsubscribedError",this.message="object unsubscribed"}}),Q=function(n){O(t,n);function t(){var e=n.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 r=new re(this,this);return r.operator=e,r},t.prototype._throwIfClosed=function(){if(this.closed)throw new ze},t.prototype.next=function(e){var r=this;U(function(){var i,o;if(r._throwIfClosed(),!r.isStopped){r.currentObservers||(r.currentObservers=Array.from(r.observers));try{for(var s=M(r.currentObservers),c=s.next();!c.done;c=s.next()){var a=c.value;a.next(e)}}catch(u){i={error:u}}finally{try{c&&!c.done&&(o=s.return)&&o.call(s)}finally{if(i)throw i.error}}}})},t.prototype.error=function(e){var r=this;U(function(){if(r._throwIfClosed(),!r.isStopped){r.hasError=r.isStopped=!0,r.thrownError=e;for(var i=r.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 r=e.observers;r.length;)r.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(),n.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 r=this,i=this,o=i.hasError,s=i.isStopped,c=i.observers;return o||s?he:(this.currentObservers=null,c.push(e),new k(function(){r.currentObservers=null,$(c,e)}))},t.prototype._checkFinalizedStatuses=function(e){var r=this,i=r.hasError,o=r.thrownError,s=r.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,r){return new re(e,r)},t}(g),re=function(n){O(t,n);function t(e,r){var i=n.call(this)||this;return i.destination=e,i.source=r,i}return t.prototype.next=function(e){var r,i;(i=(r=this.destination)===null||r===void 0?void 0:r.next)===null||i===void 0||i.call(r,e)},t.prototype.error=function(e){var r,i;(i=(r=this.destination)===null||r===void 0?void 0:r.error)===null||i===void 0||i.call(r,e)},t.prototype.complete=function(){var e,r;(r=(e=this.destination)===null||e===void 0?void 0:e.complete)===null||r===void 0||r.call(e)},t.prototype._subscribe=function(e){var r,i;return(i=(r=this.source)===null||r===void 0?void 0:r.subscribe(e))!==null&&i!==void 0?i:he},t}(Q),ie=function(n){O(t,n);function t(e){var r=n.call(this)||this;return r._value=e,r}return Object.defineProperty(t.prototype,"value",{get:function(){return this.getValue()},enumerable:!1,configurable:!0}),t.prototype._subscribe=function(e){var r=n.prototype._subscribe.call(this,e);return!r.closed&&e.next(this._value),r},t.prototype.getValue=function(){var e=this,r=e.hasError,i=e.thrownError,o=e._value;if(r)throw i;return this._throwIfClosed(),o},t.prototype.next=function(e){n.prototype.next.call(this,this._value=e)},t}(Q),Qe={now:function(){return Date.now()},delegate:void 0},Ze=function(n){O(t,n);function t(e,r){return n.call(this)||this}return t.prototype.schedule=function(e,r){return this},t}(k),oe={setInterval:function(n,t){for(var e=[],r=2;r<arguments.length;r++)e[r-2]=arguments[r];return setInterval.apply(void 0,R([n,t],L(e)))},clearInterval:function(n){return clearInterval(n)},delegate:void 0},et=function(n){O(t,n);function t(e,r){var i=n.call(this,e,r)||this;return i.scheduler=e,i.work=r,i.pending=!1,i}return t.prototype.schedule=function(e,r){var i;if(r===void 0&&(r=0),this.closed)return this;this.state=e;var o=this.id,s=this.scheduler;return o!=null&&(this.id=this.recycleAsyncId(s,o,r)),this.pending=!0,this.delay=r,this.id=(i=this.id)!==null&&i!==void 0?i:this.requestAsyncId(s,this.id,r),this},t.prototype.requestAsyncId=function(e,r,i){return i===void 0&&(i=0),oe.setInterval(e.flush.bind(e,this),i)},t.prototype.recycleAsyncId=function(e,r,i){if(i===void 0&&(i=0),i!=null&&this.delay===i&&this.pending===!1)return r;r!=null&&oe.clearInterval(r)},t.prototype.execute=function(e,r){if(this.closed)return new Error("executing a cancelled action");this.pending=!1;var i=this._execute(e,r);if(i)return i;this.pending===!1&&this.id!=null&&(this.id=this.recycleAsyncId(this.scheduler,this.id,null))},t.prototype._execute=function(e,r){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,r=e.id,i=e.scheduler,o=i.actions;this.work=this.state=this.scheduler=null,this.pending=!1,$(o,this),r!=null&&(this.id=this.recycleAsyncId(i,r,null)),this.delay=null,n.prototype.unsubscribe.call(this)}},t}(Ze),se=function(){function n(t,e){e===void 0&&(e=n.now),this.schedulerActionCtor=t,this.now=e}return n.prototype.schedule=function(t,e,r){return e===void 0&&(e=0),new this.schedulerActionCtor(this,t).schedule(r,e)},n.now=Qe.now,n}(),tt=function(n){O(t,n);function t(e,r){r===void 0&&(r=se.now);var i=n.call(this,e,r)||this;return i.actions=[],i._active=!1,i}return t.prototype.flush=function(e){var r=this.actions;if(this._active){r.push(e);return}var i;this._active=!0;do if(i=e.execute(e.state,e.delay))break;while(e=r.shift());if(this._active=!1,i){for(;e=r.shift();)e.unsubscribe();throw i}},t}(se),be=new tt(et),nt=be,rt=new g(function(n){return n.complete()});function me(n){return n&&p(n.schedule)}function ge(n){return n[n.length-1]}function it(n){return p(ge(n))?n.pop():void 0}function Se(n){return me(ge(n))?n.pop():void 0}var we=function(n){return n&&typeof n.length=="number"&&typeof n!="function"};function Ee(n){return p(n==null?void 0:n.then)}function Oe(n){return p(n[z])}function Ie(n){return Symbol.asyncIterator&&p(n==null?void 0:n[Symbol.asyncIterator])}function Ae(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 ot(){return typeof Symbol!="function"||!Symbol.iterator?"@@iterator":Symbol.iterator}var _e=ot();function xe(n){return p(n==null?void 0:n[_e])}function Te(n){return Ge(this,arguments,function(){var e,r,i,o;return le(this,function(s){switch(s.label){case 0:e=n.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 r=s.sent(),i=r.value,o=r.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 Ce(n){return p(n==null?void 0:n.getReader)}function _(n){if(n instanceof g)return n;if(n!=null){if(Oe(n))return st(n);if(we(n))return ut(n);if(Ee(n))return at(n);if(Ie(n))return Pe(n);if(xe(n))return ct(n);if(Ce(n))return ft(n)}throw Ae(n)}function st(n){return new g(function(t){var e=n[z]();if(p(e.subscribe))return e.subscribe(t);throw new TypeError("Provided object does not correctly implement Symbol.observable")})}function ut(n){return new g(function(t){for(var e=0;e<n.length&&!t.closed;e++)t.next(n[e]);t.complete()})}function at(n){return new g(function(t){n.then(function(e){t.closed||(t.next(e),t.complete())},function(e){return t.error(e)}).then(null,pe)})}function ct(n){return new g(function(t){var e,r;try{for(var i=M(n),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&&(r=i.return)&&r.call(i)}finally{if(e)throw e.error}}t.complete()})}function Pe(n){return new g(function(t){lt(n,t).catch(function(e){return t.error(e)})})}function ft(n){return Pe(Te(n))}function lt(n,t){var e,r,i,o;return $e(this,void 0,void 0,function(){var s,c;return le(this,function(a){switch(a.label){case 0:a.trys.push([0,5,6,11]),e=Fe(n),a.label=1;case 1:return[4,e.next()];case 2:if(r=a.sent(),!!r.done)return[3,4];if(s=r.value,t.next(s),t.closed)return[2];a.label=3;case 3:return[3,1];case 4:return[3,11];case 5:return c=a.sent(),i={error:c},[3,11];case 6:return a.trys.push([6,,9,10]),r&&!r.done&&(o=e.return)?[4,o.call(e)]:[3,8];case 7:a.sent(),a.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(n,t,e,r,i){r===void 0&&(r=0),i===void 0&&(i=!1);var o=t.schedule(function(){e(),i?n.add(this.schedule(null,r)):this.unsubscribe()},r);if(n.add(o),!i)return o}function Me(n,t){return t===void 0&&(t=0),C(function(e,r){e.subscribe(E(r,function(i){return A(r,n,function(){return r.next(i)},t)},function(){return A(r,n,function(){return r.complete()},t)},function(i){return A(r,n,function(){return r.error(i)},t)}))})}function Le(n,t){return t===void 0&&(t=0),C(function(e,r){r.add(n.schedule(function(){return e.subscribe(r)},t))})}function dt(n,t){return _(n).pipe(Le(t),Me(t))}function ht(n,t){return _(n).pipe(Le(t),Me(t))}function yt(n,t){return new g(function(e){var r=0;return t.schedule(function(){r===n.length?e.complete():(e.next(n[r++]),e.closed||this.schedule())})})}function vt(n,t){return new g(function(e){var r;return A(e,t,function(){r=n[_e](),A(e,t,function(){var i,o,s;try{i=r.next(),o=i.value,s=i.done}catch(c){e.error(c);return}s?e.complete():e.next(o)},0,!0)}),function(){return p(r==null?void 0:r.return)&&r.return()}})}function Re(n,t){if(!n)throw new Error("Iterable cannot be null");return new g(function(e){A(e,t,function(){var r=n[Symbol.asyncIterator]();A(e,t,function(){r.next().then(function(i){i.done?e.complete():e.next(i.value)})},0,!0)})})}function pt(n,t){return Re(Te(n),t)}function bt(n,t){if(n!=null){if(Oe(n))return dt(n,t);if(we(n))return yt(n,t);if(Ee(n))return ht(n,t);if(Ie(n))return Re(n,t);if(xe(n))return vt(n,t);if(Ce(n))return pt(n,t)}throw Ae(n)}function Z(n,t){return t?bt(n,t):_(n)}function mt(){for(var n=[],t=0;t<arguments.length;t++)n[t]=arguments[t];var e=Se(n);return Z(n,e)}function gt(n){return n instanceof Date&&!isNaN(n)}function H(n,t){return C(function(e,r){var i=0;e.subscribe(E(r,function(o){r.next(n.call(t,o,i++))}))})}var St=Array.isArray;function wt(n,t){return St(t)?n.apply(void 0,R([],L(t))):n(t)}function Et(n){return H(function(t){return wt(n,t)})}var Ot=Array.isArray,It=Object.getPrototypeOf,At=Object.prototype,_t=Object.keys;function xt(n){if(n.length===1){var t=n[0];if(Ot(t))return{args:t,keys:null};if(Tt(t)){var e=_t(t);return{args:e.map(function(r){return t[r]}),keys:e}}}return{args:n,keys:null}}function Tt(n){return n&&typeof n=="object"&&It(n)===At}function Ct(n,t){return n.reduce(function(e,r,i){return e[r]=t[i],e},{})}function Pt(){for(var n=[],t=0;t<arguments.length;t++)n[t]=arguments[t];var e=Se(n),r=it(n),i=xt(n),o=i.args,s=i.keys;if(o.length===0)return Z([],e);var c=new g(Mt(o,e,s?function(a){return Ct(s,a)}:F));return r?c.pipe(Et(r)):c}function Mt(n,t,e){return e===void 0&&(e=F),function(r){ue(t,function(){for(var i=n.length,o=new Array(i),s=i,c=i,a=function(f){ue(t,function(){var y=Z(n[f],t),b=!1;y.subscribe(E(r,function(l){o[f]=l,b||(b=!0,c--),c||r.next(e(o.slice()))},function(){--s||r.complete()}))},r)},u=0;u<i;u++)a(u)},r)}}function ue(n,t,e){n?A(e,n,t):t()}function Lt(n,t,e,r,i,o,s,c){var a=[],u=0,f=0,y=!1,b=function(){y&&!a.length&&!u&&t.complete()},l=function(d){return u<r?v(d):a.push(d)},v=function(d){u++;var S=!1;_(e(d,f++)).subscribe(E(t,function(w){t.next(w)},function(){S=!0},void 0,function(){if(S)try{u--;for(var w=function(){var x=a.shift();s||v(x)};a.length&&u<r;)w();b()}catch(x){t.error(x)}}))};return n.subscribe(E(t,l,function(){y=!0,b()})),function(){}}function G(n,t,e){return e===void 0&&(e=1/0),p(t)?G(function(r,i){return H(function(o,s){return t(r,o,i,s)})(_(n(r,i)))},e):(typeof t=="number"&&(e=t),C(function(r,i){return Lt(r,i,n,e)}))}function ke(n,t,e){n===void 0&&(n=0),e===void 0&&(e=nt);var r=-1;return t!=null&&(me(t)?e=t:r=t),new g(function(i){var o=gt(n)?+n-e.now():n;o<0&&(o=0);var s=0;return e.schedule(function(){i.closed||(i.next(s++),0<=r?this.schedule(void 0,r):i.complete())},o)})}function Rt(n,t){return p(t)?G(n,t,1):G(n,1)}function kt(n){return n<=0?function(){return rt}:C(function(t,e){var r=0;t.subscribe(E(e,function(i){++r<=n&&(e.next(i),n<=r&&e.complete())}))})}function jt(n){return H(function(){return n})}function Dt(n,t){return G(function(e,r){return _(n(e,r)).pipe(kt(1),jt(e))})}function Ut(n,t){t===void 0&&(t=be);var e=ke(n,t);return Dt(function(){return e})}function Nt(n){var t;t={count:n};var e=t.count,r=e===void 0?1/0:e,i=t.delay,o=t.resetOnSuccess,s=o===void 0?!1:o;return r<=0?F:C(function(c,a){var u=0,f,y=function(){var b=!1;f=c.subscribe(E(a,function(l){s&&(u=0),a.next(l)},void 0,function(l){if(u++<r){var v=function(){f?(f.unsubscribe(),f=null,y()):b=!0};if(i!=null){var d=typeof i=="number"?ke(i):_(i(l,u)),S=E(a,function(){S.unsubscribe(),v()},function(){a.complete()});d.subscribe(S)}else v()}else a.error(l)})),b&&(f.unsubscribe(),f=null,y())};y()})}function $t(n){return C(function(t,e){_(n).subscribe(E(e,function(){return e.complete()},q)),!e.closed&&t.subscribe(e)})}async function Gt(n,t){const e=n.getReader();let r;for(;!(r=await e.read()).done;)t(r.value)}function Ft(n){let t,e,r,i=!1;return function(s){t===void 0?(t=s,e=0,r=-1):t=Vt(t,s);const c=t.length;let a=0;for(;e<c;){i&&(t[e]===10&&(a=++e),i=!1);let u=-1;for(;e<c&&u===-1;++e)switch(t[e]){case 58:r===-1&&(r=e-a);break;case 13:i=!0;case 10:u=e;break}if(u===-1)break;n(t.subarray(a,u),r),a=e,r=-1}a===c?t=void 0:a!==0&&(t=t.subarray(a),e-=a)}}function Ht(n,t,e){let r=ae();const i=new TextDecoder;return function(s,c){if(s.length===0)e==null||e(r),r=ae();else if(c>0){const a=i.decode(s.subarray(0,c)),u=c+(s[c+1]===32?2:1),f=i.decode(s.subarray(u));switch(a){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 y=parseInt(f,10);isNaN(y)||t(r.retry=y);break}}}}function Vt(n,t){const e=new Uint8Array(n.length+t.length);return e.set(n),e.set(t,n.length),e}function ae(){return{data:"",event:"",id:"",retry:void 0}}var Bt=function(n,t){var e={};for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&t.indexOf(r)<0&&(e[r]=n[r]);if(n!=null&&typeof Object.getOwnPropertySymbols=="function")for(var i=0,r=Object.getOwnPropertySymbols(n);i<r.length;i++)t.indexOf(r[i])<0&&Object.prototype.propertyIsEnumerable.call(n,r[i])&&(e[r[i]]=n[r[i]]);return e};const J="text/event-stream",Kt=1e3,ce="last-event-id";function Yt(n,t){var{signal:e,headers:r,onopen:i,onmessage:o,onclose:s,onerror:c,openWhenHidden:a,fetch:u}=t,f=Bt(t,["signal","headers","onopen","onmessage","onclose","onerror","openWhenHidden","fetch"]);return new Promise((y,b)=>{const l=Object.assign({},r);l.accept||(l.accept=J);let v;function d(){v.abort(),document.hidden||V()}a||document.addEventListener("visibilitychange",d);let S=Kt,w=0;function x(){document.removeEventListener("visibilitychange",d),window.clearTimeout(w),v.abort()}e==null||e.addEventListener("abort",()=>{x(),y()});const je=u??window.fetch,De=i??qt;async function V(){var B;v=new AbortController;try{const j=await je(n,Object.assign(Object.assign({},f),{headers:l,signal:v.signal}));await De(j),await Gt(j.body,Ft(Ht(I=>{I?l[ce]=I:delete l[ce]},I=>{S=I},o))),s==null||s(),x(),y()}catch(j){if(!v.signal.aborted)try{const I=(B=c==null?void 0:c(j))!==null&&B!==void 0?B:S;window.clearTimeout(w),w=window.setTimeout(V,I)}catch(I){x(),b(I)}}}V()})}function qt(n){const t=n.headers.get("content-type");if(!(t!=null&&t.startsWith(J)))throw new Error(`Expected content-type to be ${J}, Actual: ${t}`)}function Wt(n){const{endpoint:t,apiKey:e,payload:r,debugMode:i}=n;return new g(o=>{const s=new AbortController,c={"Content-Type":"application/json"};e&&(c["X-API-KEY"]=e);const a=new URLSearchParams;i&&a.set("is_debug","true");const u=new URL(t);return a.toString()&&(u.search=a.toString()),Yt(u.toString(),{method:"POST",headers:c,body:r?JSON.stringify(r):void 0,signal:s.signal,openWhenHidden:!0,onopen:async f=>{f.ok||(o.error(f),s.abort())},onmessage:f=>{o.next(JSON.parse(f.data))},onclose:()=>{o.complete()},onerror:f=>{throw o.error(f),s.abort(),f}}),()=>{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(r=>r!==e)}))}remove(t){delete this.listeners[t]}emit(t,...e){this.listeners[t]&&this.listeners[t].forEach(r=>r(...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)throw new Error("endpoint must be required");this.apiKey=t.apiKey,this.endpoint=t.endpoint,this.debugMode=t.debugMode,this.transformSsePayload=t.transformSsePayload}on(t,e){this.sseEmitter.remove(t),this.sseEmitter.on(t,e)}handleEvent(t){switch(t.eventType){case h.INIT:this.sseEmitter.emit(h.INIT,t);break;case h.PROCESS_START:case h.PROCESS_COMPLETE:this.sseEmitter.emit(h.PROCESS,t);break;case h.MESSAGE_START:case h.MESSAGE_DELTA:case h.MESSAGE_COMPLETE:this.sseEmitter.emit(h.MESSAGE,t);break;case h.TOOL_CALL_START:case h.TOOL_CALL_COMPLETE:this.sseEmitter.emit(h.TOOL_CALL,t);break;case h.DONE:this.sseEmitter.emit(h.DONE,t);break;case h.ERROR:this.sseEmitter.emit(h.ERROR,t);break}}fetchSse(t,e){var r,i;(r=e==null?void 0:e.onSseStart)==null||r.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))),$t(this.destroy$),Nt(3)).subscribe({next:o=>{var s;(s=e==null?void 0:e.onSseMessage)==null||s.call(e,o),this.handleEvent(o)},error:o=>{var s;(s=e==null?void 0:e.onSseError)==null||s.call(e,o)},complete:()=>{var o;(o=e==null?void 0:e.onSseCompleted)==null||o.call(e)}})}close(){this.destroy$.next(),this.destroy$.complete()}}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,r){const i=new ee(t);try{return i.subscribe(),await i.resetChannel(e,r),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((r,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),r()}})})}resetChannel(t,e){return this.fetchSse({action:N.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 r=t.text.trim(),i=t.customMessageId??crypto.randomUUID();return this.conversation$.next(this.conversation$.value.pushMessage({type:"user",messageId:i,text:r,time:new Date})),this.fetchSse({action:N.NONE,customChannelId:this.customChannelId,customMessageId:i,payload:t==null?void 0:t.payload,text:r},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 h.MESSAGE_START:return this.onMessageStart(t);case h.MESSAGE_DELTA:return this.onMessageDelta(t);case h.MESSAGE_COMPLETE:return this.onMessageComplete(t);case h.ERROR:return this.onMessageError(t);default:return this}}onMessageStart(t){const e=t.fact.messageStart.message,r=new Map(this.messages);return r.set(e.messageId,{type:"bot",eventType:h.MESSAGE_START,isTyping:!0,typingText:"",messageId:e.messageId,message:e,time:new Date}),new T({messages:r})}onMessageDelta(t){const e=t.fact.messageDelta.message,r=new Map(this.messages),i=r.get(e.messageId);if((i==null?void 0:i.type)!=="bot")return this;const o=`${(i==null?void 0:i.typingText)??""}${e.text}`;return r.set(e.messageId,{type:"bot",eventType:h.MESSAGE_DELTA,isTyping:!0,typingText:o,messageId:e.messageId,message:e,time:new Date}),new T({messages:r})}onMessageComplete(t){const e=t.fact.messageComplete.message,r=new Map(this.messages);return r.set(e.messageId,{type:"bot",eventType:h.MESSAGE_COMPLETE,isTyping:!1,typingText:null,messageId:e.messageId,message:e,time:new Date}),new T({messages:r})}onMessageError(t){const e=crypto.randomUUID(),r=t.fact.runError.error,i=new Map(this.messages);return i.set(e,{type:"error",eventType:h.ERROR,messageId:e,error:r,time:new Date}),new T({messages:i})}}exports.AsgardServiceClient=Xt;exports.Channel=ee;exports.Conversation=T;exports.EventType=h;exports.FetchSseAction=N;exports.MessageTemplateType=fe;
3
+ `):"",this.name="UnsubscriptionError",this.errors=e}});function $(n,t){if(n){var e=n.indexOf(t);0<=e&&n.splice(e,1)}}var k=function(){function n(t){this.initialTeardown=t,this.closed=!1,this._parentage=null,this._finalizers=null}return n.prototype.unsubscribe=function(){var t,e,r,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),a=c.next();!a.done;a=c.next()){var u=a.value;u.remove(this)}}catch(d){t={error:d}}finally{try{a&&!a.done&&(e=c.return)&&e.call(c)}finally{if(t)throw t.error}}else s.remove(this);var f=this.initialTeardown;if(p(f))try{f()}catch(d){o=d instanceof K?d.errors:[d]}var y=this._finalizers;if(y){this._finalizers=null;try{for(var b=M(y),l=b.next();!l.done;l=b.next()){var v=l.value;try{te(v)}catch(d){o=o??[],d instanceof K?o=R(R([],L(o)),L(d.errors)):o.push(d)}}}catch(d){r={error:d}}finally{try{l&&!l.done&&(i=b.return)&&i.call(b)}finally{if(r)throw r.error}}}if(o)throw new K(o)}},n.prototype.add=function(t){var e;if(t&&t!==this)if(this.closed)te(t);else{if(t instanceof n){if(t.closed||t._hasParent(this))return;t._addParent(this)}(this._finalizers=(e=this._finalizers)!==null&&e!==void 0?e:[]).push(t)}},n.prototype._hasParent=function(t){var e=this._parentage;return e===t||Array.isArray(e)&&e.includes(t)},n.prototype._addParent=function(t){var e=this._parentage;this._parentage=Array.isArray(e)?(e.push(t),e):e?[e,t]:t},n.prototype._removeParent=function(t){var e=this._parentage;e===t?this._parentage=null:Array.isArray(e)&&$(e,t)},n.prototype.remove=function(t){var e=this._finalizers;e&&$(e,t),t instanceof n&&t._removeParent(this)},n.EMPTY=function(){var t=new n;return t.closed=!0,t}(),n}(),he=k.EMPTY;function ye(n){return n instanceof k||n&&"closed"in n&&p(n.remove)&&p(n.add)&&p(n.unsubscribe)}function te(n){p(n)?n():n.unsubscribe()}var He={onUnhandledError:null,onStoppedNotification:null,Promise:void 0,useDeprecatedSynchronousErrorHandling:!1,useDeprecatedNextContext:!1},ve={setTimeout:function(n,t){for(var e=[],r=2;r<arguments.length;r++)e[r-2]=arguments[r];return setTimeout.apply(void 0,R([n,t],L(e)))},clearTimeout:function(n){var t=ve.delegate;return((t==null?void 0:t.clearTimeout)||clearTimeout)(n)},delegate:void 0};function pe(n){ve.setTimeout(function(){throw n})}function q(){}function U(n){n()}var X=function(n){O(t,n);function t(e){var r=n.call(this)||this;return r.isStopped=!1,e?(r.destination=e,ye(e)&&e.add(r)):r.destination=Ke,r}return t.create=function(e,r,i){return new W(e,r,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,n.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 n(t){this.partialObserver=t}return n.prototype.next=function(t){var e=this.partialObserver;if(e.next)try{e.next(t)}catch(r){D(r)}},n.prototype.error=function(t){var e=this.partialObserver;if(e.error)try{e.error(t)}catch(r){D(r)}else D(t)},n.prototype.complete=function(){var t=this.partialObserver;if(t.complete)try{t.complete()}catch(e){D(e)}},n}(),W=function(n){O(t,n);function t(e,r,i){var o=n.call(this)||this,s;return p(e)||!e?s={next:e??void 0,error:r??void 0,complete:i??void 0}:s=e,o.destination=new Ve(s),o}return t}(X);function D(n){pe(n)}function Be(n){throw n}var Ke={closed:!0,next:q,error:Be,complete:q},z=function(){return typeof Symbol=="function"&&Symbol.observable||"@@observable"}();function F(n){return n}function Ye(n){return n.length===0?F:n.length===1?n[0]:function(e){return n.reduce(function(r,i){return i(r)},e)}}var S=function(){function n(t){t&&(this._subscribe=t)}return n.prototype.lift=function(t){var e=new n;return e.source=this,e.operator=t,e},n.prototype.subscribe=function(t,e,r){var i=this,o=We(t)?t:new W(t,e,r);return U(function(){var s=i,c=s.operator,a=s.source;o.add(c?c.call(o,a):a?i._subscribe(o):i._trySubscribe(o))}),o},n.prototype._trySubscribe=function(t){try{return this._subscribe(t)}catch(e){t.error(e)}},n.prototype.forEach=function(t,e){var r=this;return e=ne(e),new e(function(i,o){var s=new W({next:function(c){try{t(c)}catch(a){o(a),s.unsubscribe()}},error:o,complete:i});r.subscribe(s)})},n.prototype._subscribe=function(t){var e;return(e=this.source)===null||e===void 0?void 0:e.subscribe(t)},n.prototype[z]=function(){return this},n.prototype.pipe=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return Ye(t)(this)},n.prototype.toPromise=function(t){var e=this;return t=ne(t),new t(function(r,i){var o;e.subscribe(function(s){return o=s},function(s){return i(s)},function(){return r(o)})})},n.create=function(t){return new n(t)},n}();function ne(n){var t;return(t=n??He.Promise)!==null&&t!==void 0?t:Promise}function qe(n){return n&&p(n.next)&&p(n.error)&&p(n.complete)}function We(n){return n&&n instanceof X||qe(n)&&ye(n)}function Je(n){return p(n==null?void 0:n.lift)}function C(n){return function(t){if(Je(t))return t.lift(function(e){try{return n(e,this)}catch(r){this.error(r)}});throw new TypeError("Unable to lift unknown Observable type")}}function E(n,t,e,r,i){return new Xe(n,t,e,r,i)}var Xe=function(n){O(t,n);function t(e,r,i,o,s,c){var a=n.call(this,e)||this;return a.onFinalize=s,a.shouldUnsubscribe=c,a._next=r?function(u){try{r(u)}catch(f){e.error(f)}}:n.prototype._next,a._error=o?function(u){try{o(u)}catch(f){e.error(f)}finally{this.unsubscribe()}}:n.prototype._error,a._complete=i?function(){try{i()}catch(u){e.error(u)}finally{this.unsubscribe()}}:n.prototype._complete,a}return t.prototype.unsubscribe=function(){var e;if(!this.shouldUnsubscribe||this.shouldUnsubscribe()){var r=this.closed;n.prototype.unsubscribe.call(this),!r&&((e=this.onFinalize)===null||e===void 0||e.call(this))}},t}(X),ze=de(function(n){return function(){n(this),this.name="ObjectUnsubscribedError",this.message="object unsubscribed"}}),Q=function(n){O(t,n);function t(){var e=n.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 r=new re(this,this);return r.operator=e,r},t.prototype._throwIfClosed=function(){if(this.closed)throw new ze},t.prototype.next=function(e){var r=this;U(function(){var i,o;if(r._throwIfClosed(),!r.isStopped){r.currentObservers||(r.currentObservers=Array.from(r.observers));try{for(var s=M(r.currentObservers),c=s.next();!c.done;c=s.next()){var a=c.value;a.next(e)}}catch(u){i={error:u}}finally{try{c&&!c.done&&(o=s.return)&&o.call(s)}finally{if(i)throw i.error}}}})},t.prototype.error=function(e){var r=this;U(function(){if(r._throwIfClosed(),!r.isStopped){r.hasError=r.isStopped=!0,r.thrownError=e;for(var i=r.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 r=e.observers;r.length;)r.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(),n.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 r=this,i=this,o=i.hasError,s=i.isStopped,c=i.observers;return o||s?he:(this.currentObservers=null,c.push(e),new k(function(){r.currentObservers=null,$(c,e)}))},t.prototype._checkFinalizedStatuses=function(e){var r=this,i=r.hasError,o=r.thrownError,s=r.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,r){return new re(e,r)},t}(S),re=function(n){O(t,n);function t(e,r){var i=n.call(this)||this;return i.destination=e,i.source=r,i}return t.prototype.next=function(e){var r,i;(i=(r=this.destination)===null||r===void 0?void 0:r.next)===null||i===void 0||i.call(r,e)},t.prototype.error=function(e){var r,i;(i=(r=this.destination)===null||r===void 0?void 0:r.error)===null||i===void 0||i.call(r,e)},t.prototype.complete=function(){var e,r;(r=(e=this.destination)===null||e===void 0?void 0:e.complete)===null||r===void 0||r.call(e)},t.prototype._subscribe=function(e){var r,i;return(i=(r=this.source)===null||r===void 0?void 0:r.subscribe(e))!==null&&i!==void 0?i:he},t}(Q),ie=function(n){O(t,n);function t(e){var r=n.call(this)||this;return r._value=e,r}return Object.defineProperty(t.prototype,"value",{get:function(){return this.getValue()},enumerable:!1,configurable:!0}),t.prototype._subscribe=function(e){var r=n.prototype._subscribe.call(this,e);return!r.closed&&e.next(this._value),r},t.prototype.getValue=function(){var e=this,r=e.hasError,i=e.thrownError,o=e._value;if(r)throw i;return this._throwIfClosed(),o},t.prototype.next=function(e){n.prototype.next.call(this,this._value=e)},t}(Q),Qe={now:function(){return Date.now()},delegate:void 0},Ze=function(n){O(t,n);function t(e,r){return n.call(this)||this}return t.prototype.schedule=function(e,r){return this},t}(k),oe={setInterval:function(n,t){for(var e=[],r=2;r<arguments.length;r++)e[r-2]=arguments[r];return setInterval.apply(void 0,R([n,t],L(e)))},clearInterval:function(n){return clearInterval(n)},delegate:void 0},et=function(n){O(t,n);function t(e,r){var i=n.call(this,e,r)||this;return i.scheduler=e,i.work=r,i.pending=!1,i}return t.prototype.schedule=function(e,r){var i;if(r===void 0&&(r=0),this.closed)return this;this.state=e;var o=this.id,s=this.scheduler;return o!=null&&(this.id=this.recycleAsyncId(s,o,r)),this.pending=!0,this.delay=r,this.id=(i=this.id)!==null&&i!==void 0?i:this.requestAsyncId(s,this.id,r),this},t.prototype.requestAsyncId=function(e,r,i){return i===void 0&&(i=0),oe.setInterval(e.flush.bind(e,this),i)},t.prototype.recycleAsyncId=function(e,r,i){if(i===void 0&&(i=0),i!=null&&this.delay===i&&this.pending===!1)return r;r!=null&&oe.clearInterval(r)},t.prototype.execute=function(e,r){if(this.closed)return new Error("executing a cancelled action");this.pending=!1;var i=this._execute(e,r);if(i)return i;this.pending===!1&&this.id!=null&&(this.id=this.recycleAsyncId(this.scheduler,this.id,null))},t.prototype._execute=function(e,r){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,r=e.id,i=e.scheduler,o=i.actions;this.work=this.state=this.scheduler=null,this.pending=!1,$(o,this),r!=null&&(this.id=this.recycleAsyncId(i,r,null)),this.delay=null,n.prototype.unsubscribe.call(this)}},t}(Ze),se=function(){function n(t,e){e===void 0&&(e=n.now),this.schedulerActionCtor=t,this.now=e}return n.prototype.schedule=function(t,e,r){return e===void 0&&(e=0),new this.schedulerActionCtor(this,t).schedule(r,e)},n.now=Qe.now,n}(),tt=function(n){O(t,n);function t(e,r){r===void 0&&(r=se.now);var i=n.call(this,e,r)||this;return i.actions=[],i._active=!1,i}return t.prototype.flush=function(e){var r=this.actions;if(this._active){r.push(e);return}var i;this._active=!0;do if(i=e.execute(e.state,e.delay))break;while(e=r.shift());if(this._active=!1,i){for(;e=r.shift();)e.unsubscribe();throw i}},t}(se),be=new tt(et),nt=be,rt=new S(function(n){return n.complete()});function me(n){return n&&p(n.schedule)}function Se(n){return n[n.length-1]}function it(n){return p(Se(n))?n.pop():void 0}function ge(n){return me(Se(n))?n.pop():void 0}var we=function(n){return n&&typeof n.length=="number"&&typeof n!="function"};function Ee(n){return p(n==null?void 0:n.then)}function Oe(n){return p(n[z])}function Ie(n){return Symbol.asyncIterator&&p(n==null?void 0:n[Symbol.asyncIterator])}function Ae(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 ot(){return typeof Symbol!="function"||!Symbol.iterator?"@@iterator":Symbol.iterator}var _e=ot();function xe(n){return p(n==null?void 0:n[_e])}function Te(n){return Ge(this,arguments,function(){var e,r,i,o;return le(this,function(s){switch(s.label){case 0:e=n.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 r=s.sent(),i=r.value,o=r.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 Ce(n){return p(n==null?void 0:n.getReader)}function _(n){if(n instanceof S)return n;if(n!=null){if(Oe(n))return st(n);if(we(n))return ut(n);if(Ee(n))return at(n);if(Ie(n))return Pe(n);if(xe(n))return ct(n);if(Ce(n))return ft(n)}throw Ae(n)}function st(n){return new S(function(t){var e=n[z]();if(p(e.subscribe))return e.subscribe(t);throw new TypeError("Provided object does not correctly implement Symbol.observable")})}function ut(n){return new S(function(t){for(var e=0;e<n.length&&!t.closed;e++)t.next(n[e]);t.complete()})}function at(n){return new S(function(t){n.then(function(e){t.closed||(t.next(e),t.complete())},function(e){return t.error(e)}).then(null,pe)})}function ct(n){return new S(function(t){var e,r;try{for(var i=M(n),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&&(r=i.return)&&r.call(i)}finally{if(e)throw e.error}}t.complete()})}function Pe(n){return new S(function(t){lt(n,t).catch(function(e){return t.error(e)})})}function ft(n){return Pe(Te(n))}function lt(n,t){var e,r,i,o;return $e(this,void 0,void 0,function(){var s,c;return le(this,function(a){switch(a.label){case 0:a.trys.push([0,5,6,11]),e=Fe(n),a.label=1;case 1:return[4,e.next()];case 2:if(r=a.sent(),!!r.done)return[3,4];if(s=r.value,t.next(s),t.closed)return[2];a.label=3;case 3:return[3,1];case 4:return[3,11];case 5:return c=a.sent(),i={error:c},[3,11];case 6:return a.trys.push([6,,9,10]),r&&!r.done&&(o=e.return)?[4,o.call(e)]:[3,8];case 7:a.sent(),a.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(n,t,e,r,i){r===void 0&&(r=0),i===void 0&&(i=!1);var o=t.schedule(function(){e(),i?n.add(this.schedule(null,r)):this.unsubscribe()},r);if(n.add(o),!i)return o}function Me(n,t){return t===void 0&&(t=0),C(function(e,r){e.subscribe(E(r,function(i){return A(r,n,function(){return r.next(i)},t)},function(){return A(r,n,function(){return r.complete()},t)},function(i){return A(r,n,function(){return r.error(i)},t)}))})}function Le(n,t){return t===void 0&&(t=0),C(function(e,r){r.add(n.schedule(function(){return e.subscribe(r)},t))})}function dt(n,t){return _(n).pipe(Le(t),Me(t))}function ht(n,t){return _(n).pipe(Le(t),Me(t))}function yt(n,t){return new S(function(e){var r=0;return t.schedule(function(){r===n.length?e.complete():(e.next(n[r++]),e.closed||this.schedule())})})}function vt(n,t){return new S(function(e){var r;return A(e,t,function(){r=n[_e](),A(e,t,function(){var i,o,s;try{i=r.next(),o=i.value,s=i.done}catch(c){e.error(c);return}s?e.complete():e.next(o)},0,!0)}),function(){return p(r==null?void 0:r.return)&&r.return()}})}function Re(n,t){if(!n)throw new Error("Iterable cannot be null");return new S(function(e){A(e,t,function(){var r=n[Symbol.asyncIterator]();A(e,t,function(){r.next().then(function(i){i.done?e.complete():e.next(i.value)})},0,!0)})})}function pt(n,t){return Re(Te(n),t)}function bt(n,t){if(n!=null){if(Oe(n))return dt(n,t);if(we(n))return yt(n,t);if(Ee(n))return ht(n,t);if(Ie(n))return Re(n,t);if(xe(n))return vt(n,t);if(Ce(n))return pt(n,t)}throw Ae(n)}function Z(n,t){return t?bt(n,t):_(n)}function mt(){for(var n=[],t=0;t<arguments.length;t++)n[t]=arguments[t];var e=ge(n);return Z(n,e)}function St(n){return n instanceof Date&&!isNaN(n)}function H(n,t){return C(function(e,r){var i=0;e.subscribe(E(r,function(o){r.next(n.call(t,o,i++))}))})}var gt=Array.isArray;function wt(n,t){return gt(t)?n.apply(void 0,R([],L(t))):n(t)}function Et(n){return H(function(t){return wt(n,t)})}var Ot=Array.isArray,It=Object.getPrototypeOf,At=Object.prototype,_t=Object.keys;function xt(n){if(n.length===1){var t=n[0];if(Ot(t))return{args:t,keys:null};if(Tt(t)){var e=_t(t);return{args:e.map(function(r){return t[r]}),keys:e}}}return{args:n,keys:null}}function Tt(n){return n&&typeof n=="object"&&It(n)===At}function Ct(n,t){return n.reduce(function(e,r,i){return e[r]=t[i],e},{})}function Pt(){for(var n=[],t=0;t<arguments.length;t++)n[t]=arguments[t];var e=ge(n),r=it(n),i=xt(n),o=i.args,s=i.keys;if(o.length===0)return Z([],e);var c=new S(Mt(o,e,s?function(a){return Ct(s,a)}:F));return r?c.pipe(Et(r)):c}function Mt(n,t,e){return e===void 0&&(e=F),function(r){ue(t,function(){for(var i=n.length,o=new Array(i),s=i,c=i,a=function(f){ue(t,function(){var y=Z(n[f],t),b=!1;y.subscribe(E(r,function(l){o[f]=l,b||(b=!0,c--),c||r.next(e(o.slice()))},function(){--s||r.complete()}))},r)},u=0;u<i;u++)a(u)},r)}}function ue(n,t,e){n?A(e,n,t):t()}function Lt(n,t,e,r,i,o,s,c){var a=[],u=0,f=0,y=!1,b=function(){y&&!a.length&&!u&&t.complete()},l=function(d){return u<r?v(d):a.push(d)},v=function(d){u++;var g=!1;_(e(d,f++)).subscribe(E(t,function(w){t.next(w)},function(){g=!0},void 0,function(){if(g)try{u--;for(var w=function(){var x=a.shift();s||v(x)};a.length&&u<r;)w();b()}catch(x){t.error(x)}}))};return n.subscribe(E(t,l,function(){y=!0,b()})),function(){}}function G(n,t,e){return e===void 0&&(e=1/0),p(t)?G(function(r,i){return H(function(o,s){return t(r,o,i,s)})(_(n(r,i)))},e):(typeof t=="number"&&(e=t),C(function(r,i){return Lt(r,i,n,e)}))}function ke(n,t,e){n===void 0&&(n=0),e===void 0&&(e=nt);var r=-1;return t!=null&&(me(t)?e=t:r=t),new S(function(i){var o=St(n)?+n-e.now():n;o<0&&(o=0);var s=0;return e.schedule(function(){i.closed||(i.next(s++),0<=r?this.schedule(void 0,r):i.complete())},o)})}function Rt(n,t){return p(t)?G(n,t,1):G(n,1)}function kt(n){return n<=0?function(){return rt}:C(function(t,e){var r=0;t.subscribe(E(e,function(i){++r<=n&&(e.next(i),n<=r&&e.complete())}))})}function jt(n){return H(function(){return n})}function Dt(n,t){return G(function(e,r){return _(n(e,r)).pipe(kt(1),jt(e))})}function Ut(n,t){t===void 0&&(t=be);var e=ke(n,t);return Dt(function(){return e})}function Nt(n){var t;t={count:n};var e=t.count,r=e===void 0?1/0:e,i=t.delay,o=t.resetOnSuccess,s=o===void 0?!1:o;return r<=0?F:C(function(c,a){var u=0,f,y=function(){var b=!1;f=c.subscribe(E(a,function(l){s&&(u=0),a.next(l)},void 0,function(l){if(u++<r){var v=function(){f?(f.unsubscribe(),f=null,y()):b=!0};if(i!=null){var d=typeof i=="number"?ke(i):_(i(l,u)),g=E(a,function(){g.unsubscribe(),v()},function(){a.complete()});d.subscribe(g)}else v()}else a.error(l)})),b&&(f.unsubscribe(),f=null,y())};y()})}function $t(n){return C(function(t,e){_(n).subscribe(E(e,function(){return e.complete()},q)),!e.closed&&t.subscribe(e)})}async function Gt(n,t){const e=n.getReader();let r;for(;!(r=await e.read()).done;)t(r.value)}function Ft(n){let t,e,r,i=!1;return function(s){t===void 0?(t=s,e=0,r=-1):t=Vt(t,s);const c=t.length;let a=0;for(;e<c;){i&&(t[e]===10&&(a=++e),i=!1);let u=-1;for(;e<c&&u===-1;++e)switch(t[e]){case 58:r===-1&&(r=e-a);break;case 13:i=!0;case 10:u=e;break}if(u===-1)break;n(t.subarray(a,u),r),a=e,r=-1}a===c?t=void 0:a!==0&&(t=t.subarray(a),e-=a)}}function Ht(n,t,e){let r=ae();const i=new TextDecoder;return function(s,c){if(s.length===0)e==null||e(r),r=ae();else if(c>0){const a=i.decode(s.subarray(0,c)),u=c+(s[c+1]===32?2:1),f=i.decode(s.subarray(u));switch(a){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 y=parseInt(f,10);isNaN(y)||t(r.retry=y);break}}}}function Vt(n,t){const e=new Uint8Array(n.length+t.length);return e.set(n),e.set(t,n.length),e}function ae(){return{data:"",event:"",id:"",retry:void 0}}var Bt=function(n,t){var e={};for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&t.indexOf(r)<0&&(e[r]=n[r]);if(n!=null&&typeof Object.getOwnPropertySymbols=="function")for(var i=0,r=Object.getOwnPropertySymbols(n);i<r.length;i++)t.indexOf(r[i])<0&&Object.prototype.propertyIsEnumerable.call(n,r[i])&&(e[r[i]]=n[r[i]]);return e};const J="text/event-stream",Kt=1e3,ce="last-event-id";function Yt(n,t){var{signal:e,headers:r,onopen:i,onmessage:o,onclose:s,onerror:c,openWhenHidden:a,fetch:u}=t,f=Bt(t,["signal","headers","onopen","onmessage","onclose","onerror","openWhenHidden","fetch"]);return new Promise((y,b)=>{const l=Object.assign({},r);l.accept||(l.accept=J);let v;function d(){v.abort(),document.hidden||V()}a||document.addEventListener("visibilitychange",d);let g=Kt,w=0;function x(){document.removeEventListener("visibilitychange",d),window.clearTimeout(w),v.abort()}e==null||e.addEventListener("abort",()=>{x(),y()});const je=u??window.fetch,De=i??qt;async function V(){var B;v=new AbortController;try{const j=await je(n,Object.assign(Object.assign({},f),{headers:l,signal:v.signal}));await De(j),await Gt(j.body,Ft(Ht(I=>{I?l[ce]=I:delete l[ce]},I=>{g=I},o))),s==null||s(),x(),y()}catch(j){if(!v.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(n){const t=n.headers.get("content-type");if(!(t!=null&&t.startsWith(J)))throw new Error(`Expected content-type to be ${J}, Actual: ${t}`)}function Wt(n){const{endpoint:t,apiKey:e,payload:r,debugMode:i}=n;return new S(o=>{const s=new AbortController,c={"Content-Type":"application/json"};e&&(c["X-API-KEY"]=e);const a=new URLSearchParams;i&&a.set("is_debug","true");const u=new URL(t);return a.toString()&&(u.search=a.toString()),Yt(u.toString(),{method:"POST",headers:c,body:r?JSON.stringify(r):void 0,signal:s.signal,openWhenHidden:!0,onopen:async f=>{f.ok||(o.error(f),s.abort())},onmessage:f=>{o.next(JSON.parse(f.data))},onclose:()=>{o.complete()},onerror:f=>{throw o.error(f),s.abort(),f}}),()=>{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(r=>r!==e)}))}remove(t){delete this.listeners[t]}emit(t,...e){this.listeners[t]&&this.listeners[t].forEach(r=>r(...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 h.INIT:this.sseEmitter.emit(h.INIT,t);break;case h.PROCESS_START:case h.PROCESS_COMPLETE:this.sseEmitter.emit(h.PROCESS,t);break;case h.MESSAGE_START:case h.MESSAGE_DELTA:case h.MESSAGE_COMPLETE:this.sseEmitter.emit(h.MESSAGE,t);break;case h.TOOL_CALL_START:case h.TOOL_CALL_COMPLETE:this.sseEmitter.emit(h.TOOL_CALL,t);break;case h.DONE:this.sseEmitter.emit(h.DONE,t);break;case h.ERROR:this.sseEmitter.emit(h.ERROR,t);break}}fetchSse(t,e){var r,i;(r=e==null?void 0:e.onSseStart)==null||r.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))),$t(this.destroy$),Nt(3)).subscribe({next:o=>{var s;(s=e==null?void 0:e.onSseMessage)==null||s.call(e,o),this.handleEvent(o)},error:o=>{var s;(s=e==null?void 0:e.onSseError)==null||s.call(e,o)},complete:()=>{var o;(o=e==null?void 0:e.onSseCompleted)==null||o.call(e)}})}close(){this.destroy$.next(),this.destroy$.complete()}}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,r){const i=new ee(t);try{return i.subscribe(),await i.resetChannel(e,r),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((r,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),r()}})})}resetChannel(t,e){return this.fetchSse({action:N.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 r=t.text.trim(),i=t.customMessageId??crypto.randomUUID();return this.conversation$.next(this.conversation$.value.pushMessage({type:"user",messageId:i,text:r,time:new Date})),this.fetchSse({action:N.NONE,customChannelId:this.customChannelId,customMessageId:i,payload:t==null?void 0:t.payload,text:r},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 h.MESSAGE_START:return this.onMessageStart(t);case h.MESSAGE_DELTA:return this.onMessageDelta(t);case h.MESSAGE_COMPLETE:return this.onMessageComplete(t);case h.ERROR:return this.onMessageError(t);default:return this}}onMessageStart(t){const e=t.fact.messageStart.message,r=new Map(this.messages);return r.set(e.messageId,{type:"bot",eventType:h.MESSAGE_START,isTyping:!0,typingText:"",messageId:e.messageId,message:e,time:new Date}),new T({messages:r})}onMessageDelta(t){const e=t.fact.messageDelta.message,r=new Map(this.messages),i=r.get(e.messageId);if((i==null?void 0:i.type)!=="bot")return this;const o=`${(i==null?void 0:i.typingText)??""}${e.text}`;return r.set(e.messageId,{type:"bot",eventType:h.MESSAGE_DELTA,isTyping:!0,typingText:o,messageId:e.messageId,message:e,time:new Date}),new T({messages:r})}onMessageComplete(t){const e=t.fact.messageComplete.message,r=new Map(this.messages);return r.set(e.messageId,{type:"bot",eventType:h.MESSAGE_COMPLETE,isTyping:!1,typingText:null,messageId:e.messageId,message:e,time:new Date}),new T({messages:r})}onMessageError(t){const e=crypto.randomUUID(),r=t.fact.runError.error,i=new Map(this.messages);return i.set(e,{type:"error",eventType:h.ERROR,messageId:e,error:r,time:new Date}),new T({messages:i})}}exports.AsgardServiceClient=Xt;exports.Channel=ee;exports.Conversation=T;exports.EventType=h;exports.FetchSseAction=N;exports.MessageTemplateType=fe;
5
5
  //# sourceMappingURL=index.cjs.map