@telia-ace/widget-conversational-hub 1.1.13 → 1.1.14-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.js +3 -3
  2. package/index.mjs +583 -433
  3. package/package.json +3 -3
package/index.js CHANGED
@@ -1,3 +1,3 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var _=function(r,t){return _=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,n){e.__proto__=n}||function(e,n){for(var s in n)Object.prototype.hasOwnProperty.call(n,s)&&(e[s]=n[s])},_(r,t)};function f(r,t){if(typeof t!="function"&&t!==null)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");_(r,t);function e(){this.constructor=r}r.prototype=t===null?Object.create(t):(e.prototype=t.prototype,new e)}function C(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 y(r,t){var e=typeof Symbol=="function"&&r[Symbol.iterator];if(!e)return r;var n=e.call(r),s,i=[],o;try{for(;(t===void 0||t-- >0)&&!(s=n.next()).done;)i.push(s.value)}catch(u){o={error:u}}finally{try{s&&!s.done&&(e=n.return)&&e.call(n)}finally{if(o)throw o.error}}return i}function m(r,t,e){if(e||arguments.length===2)for(var n=0,s=t.length,i;n<s;n++)(i||!(n in t))&&(i||(i=Array.prototype.slice.call(t,0,n)),i[n]=t[n]);return r.concat(i||Array.prototype.slice.call(t))}function l(r){return typeof r=="function"}function B(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 x=B(function(r){return function(e){r(this),this.message=e?e.length+` errors occurred during unsubscription:
2
- `+e.map(function(n,s){return s+1+") "+n.toString()}).join(`
3
- `):"",this.name="UnsubscriptionError",this.errors=e}});function P(r,t){if(r){var e=r.indexOf(t);0<=e&&r.splice(e,1)}}var g=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,s,i;if(!this.closed){this.closed=!0;var o=this._parentage;if(o)if(this._parentage=null,Array.isArray(o))try{for(var u=C(o),c=u.next();!c.done;c=u.next()){var w=c.value;w.remove(this)}}catch(a){t={error:a}}finally{try{c&&!c.done&&(e=u.return)&&e.call(u)}finally{if(t)throw t.error}}else o.remove(this);var M=this.initialTeardown;if(l(M))try{M()}catch(a){i=a instanceof x?a.errors:[a]}var U=this._finalizers;if(U){this._finalizers=null;try{for(var b=C(U),p=b.next();!p.done;p=b.next()){var H=p.value;try{A(H)}catch(a){i=i??[],a instanceof x?i=m(m([],y(i)),y(a.errors)):i.push(a)}}}catch(a){n={error:a}}finally{try{p&&!p.done&&(s=b.return)&&s.call(b)}finally{if(n)throw n.error}}}if(i)throw new x(i)}},r.prototype.add=function(t){var e;if(t&&t!==this)if(this.closed)A(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)&&P(e,t)},r.prototype.remove=function(t){var e=this._finalizers;e&&P(e,t),t instanceof r&&t._removeParent(this)},r.EMPTY=function(){var t=new r;return t.closed=!0,t}(),r}(),F=g.EMPTY;function N(r){return r instanceof g||r&&"closed"in r&&l(r.remove)&&l(r.add)&&l(r.unsubscribe)}function A(r){l(r)?r():r.unsubscribe()}var k={onUnhandledError:null,onStoppedNotification:null,Promise:void 0,useDeprecatedSynchronousErrorHandling:!1,useDeprecatedNextContext:!1},O={setTimeout:function(r,t){for(var e=[],n=2;n<arguments.length;n++)e[n-2]=arguments[n];var s=O.delegate;return s!=null&&s.setTimeout?s.setTimeout.apply(s,m([r,t],y(e))):setTimeout.apply(void 0,m([r,t],y(e)))},clearTimeout:function(r){var t=O.delegate;return((t==null?void 0:t.clearTimeout)||clearTimeout)(r)},delegate:void 0};function Y(r){O.setTimeout(function(){throw r})}function T(){}function v(r){r()}var z=function(r){f(t,r);function t(e){var n=r.call(this)||this;return n.isStopped=!1,e?(n.destination=e,N(e)&&e.add(n)):n.destination=G,n}return t.create=function(e,n,s){return new $(e,n,s)},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}(g),D=Function.prototype.bind;function E(r,t){return D.call(r,t)}var V=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}(),$=function(r){f(t,r);function t(e,n,s){var i=r.call(this)||this,o;if(l(e)||!e)o={next:e??void 0,error:n??void 0,complete:s??void 0};else{var u;i&&k.useDeprecatedNextContext?(u=Object.create(e),u.unsubscribe=function(){return i.unsubscribe()},o={next:e.next&&E(e.next,u),error:e.error&&E(e.error,u),complete:e.complete&&E(e.complete,u)}):o=e}return i.destination=new V(o),i}return t}(z);function d(r){Y(r)}function J(r){throw r}var G={closed:!0,next:T,error:J,complete:T},K=function(){return typeof Symbol=="function"&&Symbol.observable||"@@observable"}();function L(r){return r}function Q(r){return r.length===0?L:r.length===1?r[0]:function(e){return r.reduce(function(n,s){return s(n)},e)}}var q=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 s=this,i=Z(t)?t:new $(t,e,n);return v(function(){var o=s,u=o.operator,c=o.source;i.add(u?u.call(i,c):c?s._subscribe(i):s._trySubscribe(i))}),i},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=R(e),new e(function(s,i){var o=new $({next:function(u){try{t(u)}catch(c){i(c),o.unsubscribe()}},error:i,complete:s});n.subscribe(o)})},r.prototype._subscribe=function(t){var e;return(e=this.source)===null||e===void 0?void 0:e.subscribe(t)},r.prototype[K]=function(){return this},r.prototype.pipe=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return Q(t)(this)},r.prototype.toPromise=function(t){var e=this;return t=R(t),new t(function(n,s){var i;e.subscribe(function(o){return i=o},function(o){return s(o)},function(){return n(i)})})},r.create=function(t){return new r(t)},r}();function R(r){var t;return(t=r??k.Promise)!==null&&t!==void 0?t:Promise}function X(r){return r&&l(r.next)&&l(r.error)&&l(r.complete)}function Z(r){return r&&r instanceof z||X(r)&&N(r)}var ee=B(function(r){return function(){r(this),this.name="ObjectUnsubscribedError",this.message="object unsubscribed"}}),h=function(r){f(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 I(this,this);return n.operator=e,n},t.prototype._throwIfClosed=function(){if(this.closed)throw new ee},t.prototype.next=function(e){var n=this;v(function(){var s,i;if(n._throwIfClosed(),!n.isStopped){n.currentObservers||(n.currentObservers=Array.from(n.observers));try{for(var o=C(n.currentObservers),u=o.next();!u.done;u=o.next()){var c=u.value;c.next(e)}}catch(w){s={error:w}}finally{try{u&&!u.done&&(i=o.return)&&i.call(o)}finally{if(s)throw s.error}}}})},t.prototype.error=function(e){var n=this;v(function(){if(n._throwIfClosed(),!n.isStopped){n.hasError=n.isStopped=!0,n.thrownError=e;for(var s=n.observers;s.length;)s.shift().error(e)}})},t.prototype.complete=function(){var e=this;v(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,s=this,i=s.hasError,o=s.isStopped,u=s.observers;return i||o?F:(this.currentObservers=null,u.push(e),new g(function(){n.currentObservers=null,P(u,e)}))},t.prototype._checkFinalizedStatuses=function(e){var n=this,s=n.hasError,i=n.thrownError,o=n.isStopped;s?e.error(i):o&&e.complete()},t.prototype.asObservable=function(){var e=new q;return e.source=this,e},t.create=function(e,n){return new I(e,n)},t}(q),I=function(r){f(t,r);function t(e,n){var s=r.call(this)||this;return s.destination=e,s.source=n,s}return t.prototype.next=function(e){var n,s;(s=(n=this.destination)===null||n===void 0?void 0:n.next)===null||s===void 0||s.call(n,e)},t.prototype.error=function(e){var n,s;(s=(n=this.destination)===null||n===void 0?void 0:n.error)===null||s===void 0||s.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,s;return(s=(n=this.source)===null||n===void 0?void 0:n.subscribe(e))!==null&&s!==void 0?s:F},t}(h),W=function(r){f(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,s=e.thrownError,i=e._value;if(n)throw s;return this._throwIfClosed(),i},t.prototype.next=function(e){r.prototype.next.call(this,this._value=e)},t}(h),S=(r=>(r.User="user",r.Agent="agent",r.System="system",r))(S||{});class j{constructor(t,e,n){this.type=t,this.sender=e,this.messages=n,this.didUpdate=new h,this.timestamp=Date.now()}update(t){this.messages=[{items:t}],this.didUpdate.next(this)}}class te{constructor(t){this.name=t,this.print$=new h}print(t){const e=new j(S.Agent,{name:this.name},[{items:[["html",{content:t}]]}]);return this.print$.next(e),e}subscribe(){return this.print$.asObservable()}}class re{constructor(t){this.name=t,this.print$=new h}print(t){const e=new j(S.User,{name:this.name},[{items:[["html",{content:t}]]}]);return this.print$.next(e),e}subscribe(){return this.print$.asObservable()}}class ne{constructor(t){this.name=t,this.print$=new h}onUserSubmit(t){this.print(S.User,t.text)}print(t,e){const n=new j(t,{name:"Botsson"},[{items:[["html",{content:e}]]}]);return this.printEntry(n)}printEntry(t){return this.print$.next(t),t}createAgent(t){const e=new te(t);return e.subscribe().subscribe(n=>{this.printEntry(n)}),e}createUser(t){const e=new re(t);return e.subscribe().subscribe(n=>{this.printEntry(n)}),e}actions(){return this.print$.asObservable()}}const se=r=>{var t,e;return!!((t=r==null?void 0:r.outputPayload)!=null&&t.textOutputContent)&&((e=r==null?void 0:r.outputPayload)==null?void 0:e.isFinal)},ie=r=>{var t;return!!((t=r==null?void 0:r.outputPayload)!=null&&t.textStreamOutputContent)},oe=r=>{var t;return!!((t=r==null?void 0:r.initializeResponsePayload)!=null&&t.greetingMessage)};class ue{constructor(t){this.settings=t,this.currentSequenceNumber=1,this.isConnected$=new W(!1),this.agent$=new W({name:""}),this.messageSubject$=new h,this.systemEvents$=new h,this.isConnected=()=>this.isConnected$.asObservable(),this.agent=()=>this.agent$.asObservable(),this.messages=()=>this.messageSubject$.asObservable(),this.events=()=>this.systemEvents$.asObservable(),this.ws=new WebSocket(t.connectionUri),this.ws.onopen=e=>{this.onConnected()},this.ws.onmessage=e=>{this.onMessage(e)},this.ws.onerror=e=>{this.onError(e)},this.ws.onclose=e=>{this.onClose(e)}}sendMessage(t){this._sendMessage({sequenceNumber:this.currentSequenceNumber,inputPayload:{textInputContent:{text:t.text}}}),this.incrementSequenceNumber()}onConnected(){console.log("Connection established."),this.isConnected$.next(!0);const t={sequenceNumber:1,initializeRequestPayload:{extension:this.settings.extension}};this.systemEvents$.next({event:"connected",text:"Connection established"}),this._sendMessage(t)}_sendMessage(t){if(!this.isConnected$.value){console.warn("Message could not be sent, you are not connected.");return}this.ws.send(JSON.stringify(t))}incrementSequenceNumber(){this.currentSequenceNumber=this.currentSequenceNumber+1}onMessage(t){const e=JSON.parse(t.data);oe(e)&&e.initializeResponsePayload.greetingMessage.length>0?(this.agent$.next({name:e.initializeResponsePayload.agentName||""}),e.initializeResponsePayload.greetingMessage.length&&this.messageSubject$.next({text:e.initializeResponsePayload.greetingMessage,isComplete:!0,sequenceNumber:e.sequenceNumber,type:"text"})):ie(e)?this.messageSubject$.next({text:e.outputPayload.textStreamOutputContent.textPart||"",isComplete:!!e.outputPayload.isFinal,sequenceNumber:e.sequenceNumber,type:"stream"}):se(e)&&this.messageSubject$.next({text:e.outputPayload.textOutputContent.text,isComplete:!0,sequenceNumber:e.sequenceNumber,type:"text"})}onError(t){console.error("Connection error:",t),this.systemEvents$.next({event:"error",text:"Something went wrong"})}onClose(t){console.log("Connection closed",t),this.systemEvents$.next({event:"disconnected",text:"Connection closed"}),this.isConnected$.next(!1)}}class ce extends ne{constructor(t){super(t),this.isConnected=!1,this.connection=new ue({connectionUri:"wss://cso-ccs.labbn.se/convhub/webconnector/v2/dialog",extension:"104"}),this.lastReceivedMessageWasComplete=!0,this.lastWrittenEntry=null,this.agent=this.createAgent(""),this.user=this.createUser(""),this.connection.isConnected().subscribe(e=>{this.isConnected=e}),this.connection.agent().subscribe(e=>{this.agent.name=e.name}),this.connection.messages().subscribe(e=>{if(e.type==="stream"){let n=null;if(this.lastReceivedMessageWasComplete||!this.lastWrittenEntry)n=this.agent.print(e.text),n&&(this.lastWrittenEntry={entry:n,messages:[e]});else if(this.lastWrittenEntry){const s=this.mergeMessageParts(this.lastWrittenEntry.messages);this.updateEntry(this.lastWrittenEntry.entry,`${s}${e.text}`),this.lastWrittenEntry.messages.push(e)}this.lastReceivedMessageWasComplete=e.isComplete}else this.agent.print(e.text)}),this.connection.events().subscribe(e=>{switch(e.event){case"connected":break;case"disconnected":this.agent.print("You were disconnected from the chat session. Please reload the page to reconnect.");break}})}onUserSubmit(t){if(!this.isConnected){this.agent.print("The connection have been closed. Please reload the page to start a new session.");return}this.connection.sendMessage({text:t.text}),this.user.print(t.text)}mergeMessageParts(t){return t.length?t.map(e=>e.text).join(""):""}updateEntry(t,e){t.update([["html",{content:e}]])}}const ae=async r=>{const t=await r.container.get("conversation");t==null||t.registerProvider("ace-conversational-hub-provider",()=>new ce("ace-conversational-hub-provider"))};exports.ConversationalHubProviderPlugin=ae;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});let m;const L=new Uint8Array(16);function K(){if(!m&&(m=typeof crypto<"u"&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto),!m))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return m(L)}const a=[];for(let r=0;r<256;++r)a.push((r+256).toString(16).slice(1));function G(r,t=0){return a[r[t+0]]+a[r[t+1]]+a[r[t+2]]+a[r[t+3]]+"-"+a[r[t+4]]+a[r[t+5]]+"-"+a[r[t+6]]+a[r[t+7]]+"-"+a[r[t+8]]+a[r[t+9]]+"-"+a[r[t+10]]+a[r[t+11]]+a[r[t+12]]+a[r[t+13]]+a[r[t+14]]+a[r[t+15]]}const Q=typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto),B={randomUUID:Q};function X(r,t,e){if(B.randomUUID&&!t&&!r)return B.randomUUID();r=r||{};const n=r.random||(r.rng||K)();if(n[6]=n[6]&15|64,n[8]=n[8]&63|128,t){e=e||0;for(let i=0;i<16;++i)t[e+i]=n[i];return t}return G(n)}var O=function(r,t){return O=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])},O(r,t)};function b(r,t){if(typeof t!="function"&&t!==null)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");O(r,t);function e(){this.constructor=r}r.prototype=t===null?Object.create(t):(e.prototype=t.prototype,new e)}function U(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 w(r,t){var e=typeof Symbol=="function"&&r[Symbol.iterator];if(!e)return r;var n=e.call(r),i,s=[],o;try{for(;(t===void 0||t-- >0)&&!(i=n.next()).done;)s.push(i.value)}catch(u){o={error:u}}finally{try{i&&!i.done&&(e=n.return)&&e.call(n)}finally{if(o)throw o.error}}return s}function x(r,t,e){if(e||arguments.length===2)for(var n=0,i=t.length,s;n<i;n++)(s||!(n in t))&&(s||(s=Array.prototype.slice.call(t,0,n)),s[n]=t[n]);return r.concat(s||Array.prototype.slice.call(t))}function h(r){return typeof r=="function"}function z(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 P=z(function(r){return function(e){r(this),this.message=e?e.length+` errors occurred during unsubscription:
2
+ `+e.map(function(n,i){return i+1+") "+n.toString()}).join(`
3
+ `):"",this.name="UnsubscriptionError",this.errors=e}});function $(r,t){if(r){var e=r.indexOf(t);0<=e&&r.splice(e,1)}}var E=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,s;if(!this.closed){this.closed=!0;var o=this._parentage;if(o)if(this._parentage=null,Array.isArray(o))try{for(var u=U(o),c=u.next();!c.done;c=u.next()){var p=c.value;p.remove(this)}}catch(l){t={error:l}}finally{try{c&&!c.done&&(e=u.return)&&e.call(u)}finally{if(t)throw t.error}}else o.remove(this);var f=this.initialTeardown;if(h(f))try{f()}catch(l){s=l instanceof P?l.errors:[l]}var F=this._finalizers;if(F){this._finalizers=null;try{for(var y=U(F),v=y.next();!v.done;v=y.next()){var J=v.value;try{W(J)}catch(l){s=s??[],l instanceof P?s=x(x([],w(s)),w(l.errors)):s.push(l)}}}catch(l){n={error:l}}finally{try{v&&!v.done&&(i=y.return)&&i.call(y)}finally{if(n)throw n.error}}}if(s)throw new P(s)}},r.prototype.add=function(t){var e;if(t&&t!==this)if(this.closed)W(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)&&$(e,t)},r.prototype.remove=function(t){var e=this._finalizers;e&&$(e,t),t instanceof r&&t._removeParent(this)},r.EMPTY=function(){var t=new r;return t.closed=!0,t}(),r}(),V=E.EMPTY;function Y(r){return r instanceof E||r&&"closed"in r&&h(r.remove)&&h(r.add)&&h(r.unsubscribe)}function W(r){h(r)?r():r.unsubscribe()}var H={onUnhandledError:null,onStoppedNotification:null,Promise:void 0,useDeprecatedSynchronousErrorHandling:!1,useDeprecatedNextContext:!1},j={setTimeout:function(r,t){for(var e=[],n=2;n<arguments.length;n++)e[n-2]=arguments[n];var i=j.delegate;return i!=null&&i.setTimeout?i.setTimeout.apply(i,x([r,t],w(e))):setTimeout.apply(void 0,x([r,t],w(e)))},clearTimeout:function(r){var t=j.delegate;return((t==null?void 0:t.clearTimeout)||clearTimeout)(r)},delegate:void 0};function Z(r){j.setTimeout(function(){throw r})}function k(){}function S(r){r()}var I=function(r){b(t,r);function t(e){var n=r.call(this)||this;return n.isStopped=!1,e?(n.destination=e,Y(e)&&e.add(n)):n.destination=ne,n}return t.create=function(e,n,i){return new M(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}(E),ee=Function.prototype.bind;function C(r,t){return ee.call(r,t)}var te=function(){function r(t){this.partialObserver=t}return r.prototype.next=function(t){var e=this.partialObserver;if(e.next)try{e.next(t)}catch(n){g(n)}},r.prototype.error=function(t){var e=this.partialObserver;if(e.error)try{e.error(t)}catch(n){g(n)}else g(t)},r.prototype.complete=function(){var t=this.partialObserver;if(t.complete)try{t.complete()}catch(e){g(e)}},r}(),M=function(r){b(t,r);function t(e,n,i){var s=r.call(this)||this,o;if(h(e)||!e)o={next:e??void 0,error:n??void 0,complete:i??void 0};else{var u;s&&H.useDeprecatedNextContext?(u=Object.create(e),u.unsubscribe=function(){return s.unsubscribe()},o={next:e.next&&C(e.next,u),error:e.error&&C(e.error,u),complete:e.complete&&C(e.complete,u)}):o=e}return s.destination=new te(o),s}return t}(I);function g(r){Z(r)}function re(r){throw r}var ne={closed:!0,next:k,error:re,complete:k},ie=function(){return typeof Symbol=="function"&&Symbol.observable||"@@observable"}();function se(r){return r}function oe(r){return r.length===0?se:r.length===1?r[0]:function(e){return r.reduce(function(n,i){return i(n)},e)}}var T=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,s=ce(t)?t:new M(t,e,n);return S(function(){var o=i,u=o.operator,c=o.source;s.add(u?u.call(s,c):c?i._subscribe(s):i._trySubscribe(s))}),s},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=D(e),new e(function(i,s){var o=new M({next:function(u){try{t(u)}catch(c){s(c),o.unsubscribe()}},error:s,complete:i});n.subscribe(o)})},r.prototype._subscribe=function(t){var e;return(e=this.source)===null||e===void 0?void 0:e.subscribe(t)},r.prototype[ie]=function(){return this},r.prototype.pipe=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return oe(t)(this)},r.prototype.toPromise=function(t){var e=this;return t=D(t),new t(function(n,i){var s;e.subscribe(function(o){return s=o},function(o){return i(o)},function(){return n(s)})})},r.create=function(t){return new r(t)},r}();function D(r){var t;return(t=r??H.Promise)!==null&&t!==void 0?t:Promise}function ue(r){return r&&h(r.next)&&h(r.error)&&h(r.complete)}function ce(r){return r&&r instanceof I||ue(r)&&Y(r)}function ae(r){return h(r==null?void 0:r.lift)}function le(r){return function(t){if(ae(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 he(r,t,e,n,i){return new pe(r,t,e,n,i)}var pe=function(r){b(t,r);function t(e,n,i,s,o,u){var c=r.call(this,e)||this;return c.onFinalize=o,c.shouldUnsubscribe=u,c._next=n?function(p){try{n(p)}catch(f){e.error(f)}}:r.prototype._next,c._error=s?function(p){try{s(p)}catch(f){e.error(f)}finally{this.unsubscribe()}}:r.prototype._error,c._complete=i?function(){try{i()}catch(p){e.error(p)}finally{this.unsubscribe()}}:r.prototype._complete,c}return t.prototype.unsubscribe=function(){var e;if(!this.shouldUnsubscribe||this.shouldUnsubscribe()){var n=this.closed;r.prototype.unsubscribe.call(this),!n&&((e=this.onFinalize)===null||e===void 0||e.call(this))}},t}(I),de=z(function(r){return function(){r(this),this.name="ObjectUnsubscribedError",this.message="object unsubscribed"}}),d=function(r){b(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 N(this,this);return n.operator=e,n},t.prototype._throwIfClosed=function(){if(this.closed)throw new de},t.prototype.next=function(e){var n=this;S(function(){var i,s;if(n._throwIfClosed(),!n.isStopped){n.currentObservers||(n.currentObservers=Array.from(n.observers));try{for(var o=U(n.currentObservers),u=o.next();!u.done;u=o.next()){var c=u.value;c.next(e)}}catch(p){i={error:p}}finally{try{u&&!u.done&&(s=o.return)&&s.call(o)}finally{if(i)throw i.error}}}})},t.prototype.error=function(e){var n=this;S(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;S(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,s=i.hasError,o=i.isStopped,u=i.observers;return s||o?V:(this.currentObservers=null,u.push(e),new E(function(){n.currentObservers=null,$(u,e)}))},t.prototype._checkFinalizedStatuses=function(e){var n=this,i=n.hasError,s=n.thrownError,o=n.isStopped;i?e.error(s):o&&e.complete()},t.prototype.asObservable=function(){var e=new T;return e.source=this,e},t.create=function(e,n){return new N(e,n)},t}(T),N=function(r){b(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:V},t}(d),A=function(r){b(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,s=e._value;if(n)throw i;return this._throwIfClosed(),s},t.prototype.next=function(e){r.prototype.next.call(this,this._value=e)},t}(d),fe=new T(function(r){return r.complete()});function be(r){return r<=0?function(){return fe}:le(function(t,e){var n=0;t.subscribe(he(e,function(i){++n<=r&&(e.next(i),r<=n&&e.complete())}))})}class ve{constructor(t,e){this.id=t,this.entries$=new A([]),e.addEventListener("user-submit",n=>{if(!this.provider){console.warn("No provider activated, unable to print message to conversation.");return}this.provider.onUserSubmit(n.detail)})}write(t){this.entries$.pipe(be(1)).subscribe(e=>{this.entries$.next([...e,t])})}entries(){return this.entries$.asObservable()}setProvider(t){this.provider=t,this.provider.actions().subscribe(e=>{this.write(e)})}}class R{constructor(){this.sessions=new Map,this.registeredProviders=new Map,this.activeSessionId=null}static async getInstance(t){let e=await t.get("conversation");return e||(e=new R,await t.register("conversation",e),e)}registerProvider(t,e){this.registeredProviders.set(t,e)}createSession(t){const e=new ve(X(),t),n=this.getProviderByConfiguration(t);if(n){const i=n();e.setProvider(i)}return this.sessions.set(e.id,e),this.activeSessionId=e.id,e}getProviderByConfiguration(t){const[e]=t.properties.providers||[];if(!e){console.warn("No providers registered. You most likely forgot to add a provider to the conversation component properties.");return}return this.registeredProviders.get(e)}}var _=(r=>(r.User="user",r.Agent="agent",r.System="system",r))(_||{});class q{constructor(t,e,n){this.type=t,this.sender=e,this.messages=n,this.didUpdate=new d,this.timestamp=Date.now()}update(t){this.messages=[{items:t}],this.didUpdate.next(this)}}class ye{constructor(t){this.name=t,this.print$=new d}print(t){const e=new q(_.Agent,{name:this.name},[{items:[["html",{content:t}]]}]);return this.print$.next(e),e}subscribe(){return this.print$.asObservable()}}class me{constructor(t){this.name=t,this.print$=new d}print(t){const e=new q(_.User,{name:this.name},[{items:[["html",{content:t}]]}]);return this.print$.next(e),e}subscribe(){return this.print$.asObservable()}}class ge{constructor(t){this.name=t,this.print$=new d}onUserSubmit(t){this.print(_.User,t.text)}print(t,e){const n=new q(t,{name:"Botsson"},[{items:[["html",{content:e}]]}]);return this.printEntry(n)}printEntry(t){return this.print$.next(t),t}createAgent(t){const e=new ye(t);return e.subscribe().subscribe(n=>{this.printEntry(n)}),e}createUser(t){const e=new me(t);return e.subscribe().subscribe(n=>{this.printEntry(n)}),e}actions(){return this.print$.asObservable()}}const Se=r=>{var t,e;return!!((t=r==null?void 0:r.outputPayload)!=null&&t.textOutputContent)&&((e=r==null?void 0:r.outputPayload)==null?void 0:e.isFinal)},we=r=>{var t;return!!((t=r==null?void 0:r.outputPayload)!=null&&t.textStreamOutputContent)},xe=r=>{var t;return!!((t=r==null?void 0:r.initializeResponsePayload)!=null&&t.greetingMessage)};class Ee{constructor(t){this.settings=t,this.currentSequenceNumber=1,this.isConnected$=new A(!1),this.agent$=new A({name:""}),this.messageSubject$=new d,this.systemEvents$=new d,this.isConnected=()=>this.isConnected$.asObservable(),this.agent=()=>this.agent$.asObservable(),this.messages=()=>this.messageSubject$.asObservable(),this.events=()=>this.systemEvents$.asObservable(),this.ws=new WebSocket(t.connectionUri),this.ws.onopen=e=>{this.onConnected()},this.ws.onmessage=e=>{this.onMessage(e)},this.ws.onerror=e=>{this.onError(e)},this.ws.onclose=e=>{this.onClose(e)}}sendMessage(t){this._sendMessage({sequenceNumber:this.currentSequenceNumber,inputPayload:{textInputContent:{text:t.text}}}),this.incrementSequenceNumber()}onConnected(){console.log("Connection established."),this.isConnected$.next(!0);const t={sequenceNumber:1,initializeRequestPayload:{extension:this.settings.extension}};this.systemEvents$.next({event:"connected",text:"Connection established"}),this._sendMessage(t)}_sendMessage(t){if(!this.isConnected$.value){console.warn("Message could not be sent, you are not connected.");return}this.ws.send(JSON.stringify(t))}incrementSequenceNumber(){this.currentSequenceNumber=this.currentSequenceNumber+1}onMessage(t){const e=JSON.parse(t.data);xe(e)&&e.initializeResponsePayload.greetingMessage.length>0?(this.agent$.next({name:e.initializeResponsePayload.agentName||""}),e.initializeResponsePayload.greetingMessage.length&&this.messageSubject$.next({text:e.initializeResponsePayload.greetingMessage,isComplete:!0,sequenceNumber:e.sequenceNumber,type:"text"})):we(e)?this.messageSubject$.next({text:e.outputPayload.textStreamOutputContent.textPart||"",isComplete:!!e.outputPayload.isFinal,sequenceNumber:e.sequenceNumber,type:"stream"}):Se(e)&&this.messageSubject$.next({text:e.outputPayload.textOutputContent.text,isComplete:!0,sequenceNumber:e.sequenceNumber,type:"text"})}onError(t){console.error("Connection error:",t),this.systemEvents$.next({event:"error",text:"Something went wrong"})}onClose(t){console.log("Connection closed",t),this.systemEvents$.next({event:"disconnected",text:"Connection closed"}),this.isConnected$.next(!1)}}class _e extends ge{constructor(t){super(t),this.isConnected=!1,this.connection=new Ee({connectionUri:"wss://cso-ccs.labbn.se/convhub/webconnector/v2/dialog",extension:"104"}),this.lastReceivedMessageWasComplete=!0,this.lastWrittenEntry=null,this.agent=this.createAgent(""),this.user=this.createUser(""),this.connection.isConnected().subscribe(e=>{this.isConnected=e}),this.connection.agent().subscribe(e=>{this.agent.name=e.name}),this.connection.messages().subscribe(e=>{if(e.type==="stream"){let n=null;if(this.lastReceivedMessageWasComplete||!this.lastWrittenEntry)n=this.agent.print(e.text),n&&(this.lastWrittenEntry={entry:n,messages:[e]});else if(this.lastWrittenEntry){const i=this.mergeMessageParts(this.lastWrittenEntry.messages);this.updateEntry(this.lastWrittenEntry.entry,`${i}${e.text}`),this.lastWrittenEntry.messages.push(e)}this.lastReceivedMessageWasComplete=e.isComplete}else this.agent.print(e.text)}),this.connection.events().subscribe(e=>{switch(e.event){case"connected":break;case"disconnected":this.agent.print("You were disconnected from the chat session. Please reload the page to reconnect.");break}})}onUserSubmit(t){if(!this.isConnected){this.agent.print("The connection have been closed. Please reload the page to start a new session.");return}this.connection.sendMessage({text:t.text}),this.user.print(t.text)}mergeMessageParts(t){return t.length?t.map(e=>e.text).join(""):""}updateEntry(t,e){t.update([["html",{content:e}]])}}const Pe=async r=>{const t=await R.getInstance(r.container);t==null||t.registerProvider("ace-conversational-hub-provider",()=>new _e("ace-conversational-hub-provider"))};exports.ConversationalHubProviderPlugin=Pe;