xinyao-assistant-sdk 1.0.1 → 1.0.11

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.
@@ -1 +1,6 @@
1
- "use strict";const t=require("event-source-polyfill");function e(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}var n,o,r={exports:{}},s={exports:{}};n="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",o={rotl:function(t,e){return t<<e|t>>>32-e},rotr:function(t,e){return t<<32-e|t>>>e},endian:function(t){if(t.constructor==Number)return 16711935&o.rotl(t,8)|4278255360&o.rotl(t,24);for(var e=0;e<t.length;e++)t[e]=o.endian(t[e]);return t},randomBytes:function(t){for(var e=[];t>0;t--)e.push(Math.floor(256*Math.random()));return e},bytesToWords:function(t){for(var e=[],n=0,o=0;n<t.length;n++,o+=8)e[o>>>5]|=t[n]<<24-o%32;return e},wordsToBytes:function(t){for(var e=[],n=0;n<32*t.length;n+=8)e.push(t[n>>>5]>>>24-n%32&255);return e},bytesToHex:function(t){for(var e=[],n=0;n<t.length;n++)e.push((t[n]>>>4).toString(16)),e.push((15&t[n]).toString(16));return e.join("")},hexToBytes:function(t){for(var e=[],n=0;n<t.length;n+=2)e.push(parseInt(t.substr(n,2),16));return e},bytesToBase64:function(t){for(var e=[],o=0;o<t.length;o+=3)for(var r=t[o]<<16|t[o+1]<<8|t[o+2],s=0;s<4;s++)8*o+6*s<=8*t.length?e.push(n.charAt(r>>>6*(3-s)&63)):e.push("=");return e.join("")},base64ToBytes:function(t){t=t.replace(/[^A-Z0-9+\/]/gi,"");for(var e=[],o=0,r=0;o<t.length;r=++o%4)0!=r&&e.push((n.indexOf(t.charAt(o-1))&Math.pow(2,-2*r+8)-1)<<2*r|n.indexOf(t.charAt(o))>>>6-2*r);return e}},s.exports=o;var i,u,a,c,h,l=s.exports,f={utf8:{stringToBytes:function(t){return f.bin.stringToBytes(unescape(encodeURIComponent(t)))},bytesToString:function(t){return decodeURIComponent(escape(f.bin.bytesToString(t)))}},bin:{stringToBytes:function(t){for(var e=[],n=0;n<t.length;n++)e.push(255&t.charCodeAt(n));return e},bytesToString:function(t){for(var e=[],n=0;n<t.length;n++)e.push(String.fromCharCode(t[n]));return e.join("")}}},g=f,p=function(t){return null!=t&&(S(t)||function(t){return"function"==typeof t.readFloatLE&&"function"==typeof t.slice&&S(t.slice(0,0))}(t)||!!t._isBuffer)};function S(t){return!!t.constructor&&"function"==typeof t.constructor.isBuffer&&t.constructor.isBuffer(t)}i=l,u=g.utf8,a=p,c=g.bin,(h=function(t,e){t.constructor==String?t=e&&"binary"===e.encoding?c.stringToBytes(t):u.stringToBytes(t):a(t)?t=Array.prototype.slice.call(t,0):Array.isArray(t)||t.constructor===Uint8Array||(t=t.toString());for(var n=i.bytesToWords(t),o=8*t.length,r=1732584193,s=-271733879,l=-1732584194,f=271733878,g=0;g<n.length;g++)n[g]=16711935&(n[g]<<8|n[g]>>>24)|4278255360&(n[g]<<24|n[g]>>>8);n[o>>>5]|=128<<o%32,n[14+(o+64>>>9<<4)]=o;var p=h._ff,S=h._gg,d=h._hh,y=h._ii;for(g=0;g<n.length;g+=16){var v=r,T=s,_=l,A=f;r=p(r,s,l,f,n[g+0],7,-680876936),f=p(f,r,s,l,n[g+1],12,-389564586),l=p(l,f,r,s,n[g+2],17,606105819),s=p(s,l,f,r,n[g+3],22,-1044525330),r=p(r,s,l,f,n[g+4],7,-176418897),f=p(f,r,s,l,n[g+5],12,1200080426),l=p(l,f,r,s,n[g+6],17,-1473231341),s=p(s,l,f,r,n[g+7],22,-45705983),r=p(r,s,l,f,n[g+8],7,1770035416),f=p(f,r,s,l,n[g+9],12,-1958414417),l=p(l,f,r,s,n[g+10],17,-42063),s=p(s,l,f,r,n[g+11],22,-1990404162),r=p(r,s,l,f,n[g+12],7,1804603682),f=p(f,r,s,l,n[g+13],12,-40341101),l=p(l,f,r,s,n[g+14],17,-1502002290),r=S(r,s=p(s,l,f,r,n[g+15],22,1236535329),l,f,n[g+1],5,-165796510),f=S(f,r,s,l,n[g+6],9,-1069501632),l=S(l,f,r,s,n[g+11],14,643717713),s=S(s,l,f,r,n[g+0],20,-373897302),r=S(r,s,l,f,n[g+5],5,-701558691),f=S(f,r,s,l,n[g+10],9,38016083),l=S(l,f,r,s,n[g+15],14,-660478335),s=S(s,l,f,r,n[g+4],20,-405537848),r=S(r,s,l,f,n[g+9],5,568446438),f=S(f,r,s,l,n[g+14],9,-1019803690),l=S(l,f,r,s,n[g+3],14,-187363961),s=S(s,l,f,r,n[g+8],20,1163531501),r=S(r,s,l,f,n[g+13],5,-1444681467),f=S(f,r,s,l,n[g+2],9,-51403784),l=S(l,f,r,s,n[g+7],14,1735328473),r=d(r,s=S(s,l,f,r,n[g+12],20,-1926607734),l,f,n[g+5],4,-378558),f=d(f,r,s,l,n[g+8],11,-2022574463),l=d(l,f,r,s,n[g+11],16,1839030562),s=d(s,l,f,r,n[g+14],23,-35309556),r=d(r,s,l,f,n[g+1],4,-1530992060),f=d(f,r,s,l,n[g+4],11,1272893353),l=d(l,f,r,s,n[g+7],16,-155497632),s=d(s,l,f,r,n[g+10],23,-1094730640),r=d(r,s,l,f,n[g+13],4,681279174),f=d(f,r,s,l,n[g+0],11,-358537222),l=d(l,f,r,s,n[g+3],16,-722521979),s=d(s,l,f,r,n[g+6],23,76029189),r=d(r,s,l,f,n[g+9],4,-640364487),f=d(f,r,s,l,n[g+12],11,-421815835),l=d(l,f,r,s,n[g+15],16,530742520),r=y(r,s=d(s,l,f,r,n[g+2],23,-995338651),l,f,n[g+0],6,-198630844),f=y(f,r,s,l,n[g+7],10,1126891415),l=y(l,f,r,s,n[g+14],15,-1416354905),s=y(s,l,f,r,n[g+5],21,-57434055),r=y(r,s,l,f,n[g+12],6,1700485571),f=y(f,r,s,l,n[g+3],10,-1894986606),l=y(l,f,r,s,n[g+10],15,-1051523),s=y(s,l,f,r,n[g+1],21,-2054922799),r=y(r,s,l,f,n[g+8],6,1873313359),f=y(f,r,s,l,n[g+15],10,-30611744),l=y(l,f,r,s,n[g+6],15,-1560198380),s=y(s,l,f,r,n[g+13],21,1309151649),r=y(r,s,l,f,n[g+4],6,-145523070),f=y(f,r,s,l,n[g+11],10,-1120210379),l=y(l,f,r,s,n[g+2],15,718787259),s=y(s,l,f,r,n[g+9],21,-343485551),r=r+v>>>0,s=s+T>>>0,l=l+_>>>0,f=f+A>>>0}return i.endian([r,s,l,f])})._ff=function(t,e,n,o,r,s,i){var u=t+(e&n|~e&o)+(r>>>0)+i;return(u<<s|u>>>32-s)+e},h._gg=function(t,e,n,o,r,s,i){var u=t+(e&o|n&~o)+(r>>>0)+i;return(u<<s|u>>>32-s)+e},h._hh=function(t,e,n,o,r,s,i){var u=t+(e^n^o)+(r>>>0)+i;return(u<<s|u>>>32-s)+e},h._ii=function(t,e,n,o,r,s,i){var u=t+(n^(e|~o))+(r>>>0)+i;return(u<<s|u>>>32-s)+e},h._blocksize=16,h._digestsize=16,r.exports=function(t,e){if(null==t)throw new Error("Illegal argument "+t);var n=i.wordsToBytes(h(t,e));return e&&e.asBytes?n:e&&e.asString?c.bytesToString(n):i.bytesToHex(n)};const d=e(r.exports);module.exports=class{constructor(t={}){this.url=t.url,this.app_key=t.app_key,this.app_secret=t.app_secret,this.onMessage=t.onMessage||(()=>{}),this.onError=t.onError||(()=>{}),this.onOpen=t.onOpen||(()=>{}),this.onAudioData=t.onAudioData||(()=>{}),this.onAudioNegotiationSuccess=t.onAudioNegotiationSuccess||(()=>{}),this.onConnectSSE=t.onConnectSSE||(()=>{}),this.eventSource=null,this.negotiationResult=null}generateAuthToken(){const t=Math.floor(Date.now()/1e3);return`${this.app_key}:${t}:${d(`${this.app_secret}:${t}`)}`}getAuthHeaders(){return{Authorization:`${this.generateAuthToken()}`,"Content-Type":"application/json"}}async post(t,e){const n=this.generateAuthToken(),o=await fetch(`${this.url}${t}`,{method:"POST",headers:{Authorization:`${n}`,"Content-Type":"application/json","User-Agent":"XY-ASSIST-SDK"},body:JSON.stringify(e)});if(!o.ok){const t=await o.text();throw new Error(`HTTP error! status: ${o.status}, message: ${t}`)}return await o.json()}async audioNegotiation(t){const e=await this.post("/api/v1/agent/ccms/assist/audio-nego",t);return this.negotiationResult=e,this.onAudioNegotiationSuccess&&this.onAudioNegotiationSuccess(e),e}connectSSEWithNegotiationResult(){if(!this.negotiationResult||!this.negotiationResult.result)throw new Error("没有有效的协商结果,请先执行音频协商");const{sse_addr:t,client_id:e}=this.negotiationResult.result;if(!t||!e)throw new Error("协商结果中缺少sse_addr或client_id");const n=`http://${t}/sse?client_id=${e}`;this.connectSSEWithURL(n),this.onConnectSSE&&this.onConnectSSE({sseUrl:n,client_id:e,sse_addr:t})}connectSSEWithURL(e){this.eventSource=new t(e),this.eventSource.onmessage=t=>{const e=JSON.parse(t.data);this.onMessage(e)},this.eventSource.onerror=t=>{this.onError(t)},this.eventSource.onopen=()=>{this.onOpen()}}connectSSE(e=!0){if(e&&this.negotiationResult&&this.negotiationResult.result&&this.negotiationResult.result.sse_addr)this.connectSSEWithNegotiationResult();else{const e=this.generateAuthToken(),n=`${this.url}/api/v1/agent/sse?auth=${e}`;this.eventSource=new t(n),this.eventSource.onmessage=t=>{const e=JSON.parse(t.data);this.onMessage(e)},this.eventSource.onerror=t=>{this.onError(t)},this.eventSource.onopen=()=>{this.onOpen()}}}disconnectSSE(){this.eventSource&&(this.eventSource.close(),this.eventSource=null)}};
1
+ "use strict";var t="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};function e(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}var n,o,r={exports:{}};
2
+ /** @license
3
+ * eventsource.js
4
+ * Available under MIT License (MIT)
5
+ * https://github.com/Yaffle/EventSource/
6
+ */n=r,o=r.exports,function(t){var e=t.setTimeout,r=t.clearTimeout,i=t.XMLHttpRequest,s=t.XDomainRequest,a=t.ActiveXObject,u=t.EventSource,c=t.document,l=t.Promise,h=t.fetch,d=t.Response,f=t.TextDecoder,p=t.TextEncoder,y=t.AbortController;if("undefined"==typeof window||void 0===c||"readyState"in c||null!=c.body||(c.readyState="loading",window.addEventListener("load",function(t){c.readyState="complete"},!1)),null==i&&null!=a&&(i=function(){return new a("Microsoft.XMLHTTP")}),null==Object.create&&(Object.create=function(t){function e(){}return e.prototype=t,new e}),Date.now||(Date.now=function(){return(new Date).getTime()}),null==y){var g=h;h=function(t,e){var n=e.signal;return g(t,{headers:e.headers,credentials:e.credentials,cache:e.cache}).then(function(t){var e=t.body.getReader();return n._reader=e,n._aborted&&n._reader.cancel(),{status:t.status,statusText:t.statusText,headers:t.headers,body:{getReader:function(){return e}}}})},y=function(){this.signal={_reader:null,_aborted:!1},this.abort=function(){null!=this.signal._reader&&this.signal._reader.cancel(),this.signal._aborted=!0}}}function v(){this.bitsNeeded=0,this.codePoint=0}v.prototype.decode=function(t){function e(t,e,n){if(1===n)return t>=128>>e&&t<<e<=2047;if(2===n)return t>=2048>>e&&t<<e<=55295||t>=57344>>e&&t<<e<=65535;if(3===n)return t>=65536>>e&&t<<e<=1114111;throw new Error}function n(t,e){if(6===t)return e>>6>15?3:e>31?2:1;if(12===t)return e>15?3:2;if(18===t)return 3;throw new Error}for(var o=65533,r="",i=this.bitsNeeded,s=this.codePoint,a=0;a<t.length;a+=1){var u=t[a];0!==i&&(u<128||u>191||!e(s<<6|63&u,i-6,n(i,s)))&&(i=0,s=o,r+=String.fromCharCode(s)),0===i?(u>=0&&u<=127?(i=0,s=u):u>=192&&u<=223?(i=6,s=31&u):u>=224&&u<=239?(i=12,s=15&u):u>=240&&u<=247?(i=18,s=7&u):(i=0,s=o),0===i||e(s,i,n(i,s))||(i=0,s=o)):(i-=6,s=s<<6|63&u),0===i&&(s<=65535?r+=String.fromCharCode(s):(r+=String.fromCharCode(55296+(s-65535-1>>10)),r+=String.fromCharCode(56320+(s-65535-1&1023))))}return this.bitsNeeded=i,this.codePoint=s,r},null!=f&&null!=p&&function(){try{return"test"===(new f).decode((new p).encode("test"),{stream:!0})}catch(t){console.debug("TextDecoder does not support streaming option. Using polyfill instead: "+t)}return!1}()||(f=v);var S=function(){};function w(t){this.withCredentials=!1,this.readyState=0,this.status=0,this.statusText="",this.responseText="",this.onprogress=S,this.onload=S,this.onerror=S,this.onreadystatechange=S,this._contentType="",this._xhr=t,this._sendTimeout=0,this._abort=S}function T(t){return t.replace(/[A-Z]/g,function(t){return String.fromCharCode(t.charCodeAt(0)+32)})}function b(t){for(var e=Object.create(null),n=t.split("\r\n"),o=0;o<n.length;o+=1){var r=n[o].split(": "),i=r.shift(),s=r.join(": ");e[T(i)]=s}this._map=e}function E(){}function _(t){this._headers=t}function C(){}function m(){this._listeners=Object.create(null)}function A(t){e(function(){throw t},0)}function x(t){this.type=t,this.target=void 0}function R(t,e){x.call(this,t),this.data=e.data,this.lastEventId=e.lastEventId}function O(t,e){x.call(this,t),this.status=e.status,this.statusText=e.statusText,this.headers=e.headers}function N(t,e){x.call(this,t),this.error=e.error}w.prototype.open=function(t,n){this._abort(!0);var o=this,s=this._xhr,a=1,u=0;this._abort=function(t){0!==o._sendTimeout&&(r(o._sendTimeout),o._sendTimeout=0),1!==a&&2!==a&&3!==a||(a=4,s.onload=S,s.onerror=S,s.onabort=S,s.onprogress=S,s.onreadystatechange=S,s.abort(),0!==u&&(r(u),u=0),t||(o.readyState=4,o.onabort(null),o.onreadystatechange())),a=0};var c=function(){if(1===a){var t=0,e="",n=void 0;if("contentType"in s)t=200,e="OK",n=s.contentType;else try{t=s.status,e=s.statusText,n=s.getResponseHeader("Content-Type")}catch(r){t=0,e="",n=void 0}0!==t&&(a=2,o.readyState=2,o.status=t,o.statusText=e,o._contentType=n,o.onreadystatechange())}},l=function(){if(c(),2===a||3===a){a=3;var t="";try{t=s.responseText}catch(e){}o.readyState=3,o.responseText=t,o.onprogress()}},h=function(t,e){if(null!=e&&null!=e.preventDefault||(e={preventDefault:S}),l(),1===a||2===a||3===a){if(a=4,0!==u&&(r(u),u=0),o.readyState=4,"load"===t)o.onload(e);else if("error"===t)o.onerror(e);else{if("abort"!==t)throw new TypeError;o.onabort(e)}o.onreadystatechange()}},d=function(){u=e(function(){d()},500),3===s.readyState&&l()};"onload"in s&&(s.onload=function(t){h("load",t)}),"onerror"in s&&(s.onerror=function(t){h("error",t)}),"onabort"in s&&(s.onabort=function(t){h("abort",t)}),"onprogress"in s&&(s.onprogress=l),"onreadystatechange"in s&&(s.onreadystatechange=function(t){!function(t){null!=s&&(4===s.readyState?"onload"in s&&"onerror"in s&&"onabort"in s||h(""===s.responseText?"error":"load",t):3===s.readyState?"onprogress"in s||l():2===s.readyState&&c())}(t)}),!("contentType"in s)&&"ontimeout"in i.prototype||(n+=(-1===n.indexOf("?")?"?":"&")+"padding=true"),s.open(t,n,!0),"readyState"in s&&(u=e(function(){d()},0))},w.prototype.abort=function(){this._abort(!1)},w.prototype.getResponseHeader=function(t){return this._contentType},w.prototype.setRequestHeader=function(t,e){var n=this._xhr;"setRequestHeader"in n&&n.setRequestHeader(t,e)},w.prototype.getAllResponseHeaders=function(){return null!=this._xhr.getAllResponseHeaders&&this._xhr.getAllResponseHeaders()||""},w.prototype.send=function(){if("ontimeout"in i.prototype&&("sendAsBinary"in i.prototype||"mozAnon"in i.prototype)||null==c||null==c.readyState||"complete"===c.readyState){var t=this._xhr;"withCredentials"in t&&(t.withCredentials=this.withCredentials);try{t.send(void 0)}catch(o){throw o}}else{var n=this;n._sendTimeout=e(function(){n._sendTimeout=0,n.send()},4)}},b.prototype.get=function(t){return this._map[T(t)]},null!=i&&null==i.HEADERS_RECEIVED&&(i.HEADERS_RECEIVED=2),E.prototype.open=function(t,e,n,o,r,s,a){t.open("GET",r);var u=0;for(var c in t.onprogress=function(){var e=t.responseText.slice(u);u+=e.length,n(e)},t.onerror=function(t){t.preventDefault(),o(new Error("NetworkError"))},t.onload=function(){o(null)},t.onabort=function(){o(null)},t.onreadystatechange=function(){if(t.readyState===i.HEADERS_RECEIVED){var n=t.status,o=t.statusText,r=t.getResponseHeader("Content-Type"),s=t.getAllResponseHeaders();e(n,o,r,new b(s))}},t.withCredentials=s,a)Object.prototype.hasOwnProperty.call(a,c)&&t.setRequestHeader(c,a[c]);return t.send(),t},_.prototype.get=function(t){return this._headers.get(t)},C.prototype.open=function(t,e,n,o,r,i,s){var a=null,u=new y,c=u.signal,d=new f;return h(r,{headers:s,credentials:i?"include":"same-origin",signal:c,cache:"no-store"}).then(function(t){return a=t.body.getReader(),e(t.status,t.statusText,t.headers.get("Content-Type"),new _(t.headers)),new l(function(t,e){var o=function(){a.read().then(function(e){if(e.done)t(void 0);else{var r=d.decode(e.value,{stream:!0});n(r),o()}}).catch(function(t){e(t)})};o()})}).catch(function(t){return"AbortError"===t.name?void 0:t}).then(function(t){o(t)}),{abort:function(){null!=a&&a.cancel(),u.abort()}}},m.prototype.dispatchEvent=function(t){t.target=this;var e=this._listeners[t.type];if(null!=e)for(var n=e.length,o=0;o<n;o+=1){var r=e[o];try{"function"==typeof r.handleEvent?r.handleEvent(t):r.call(this,t)}catch(i){A(i)}}},m.prototype.addEventListener=function(t,e){t=String(t);var n=this._listeners,o=n[t];null==o&&(o=[],n[t]=o);for(var r=!1,i=0;i<o.length;i+=1)o[i]===e&&(r=!0);r||o.push(e)},m.prototype.removeEventListener=function(t,e){t=String(t);var n=this._listeners,o=n[t];if(null!=o){for(var r=[],i=0;i<o.length;i+=1)o[i]!==e&&r.push(o[i]);0===r.length?delete n[t]:n[t]=r}},R.prototype=Object.create(x.prototype),O.prototype=Object.create(x.prototype),N.prototype=Object.create(x.prototype);var D=-1,H=-1,j=/^text\/event\-stream(;.*)?$/i,B=function(t,e){var n=null==t?e:parseInt(t,10);return n!=n&&(n=e),I(n)},I=function(t){return Math.min(Math.max(t,1e3),18e6)},M=function(t,e,n){try{"function"==typeof e&&e.call(t,n)}catch(o){A(o)}};function $(t,n){m.call(this),n=n||{},this.onopen=void 0,this.onmessage=void 0,this.onerror=void 0,this.url=void 0,this.readyState=void 0,this.withCredentials=void 0,this.headers=void 0,this._close=void 0,function(t,n,o){n=String(n);var a=Boolean(o.withCredentials),u=o.lastEventIdQueryParameterName||"lastEventId",c=I(1e3),l=B(o.heartbeatTimeout,45e3),h="",d=c,f=!1,p=0,y=o.headers||{},g=o.Transport,v=P&&null==g?void 0:new w(null!=g?new g:null!=i&&"withCredentials"in i.prototype||null==s?new i:new s),S=null!=g&&"string"!=typeof g?new g:null==v?new C:new E,T=void 0,b=0,_=D,m="",A="",x="",$="",k=0,L=0,U=0,q=function(e,n,o,r){if(0===_)if(200===e&&null!=o&&j.test(o)){_=1,f=Date.now(),d=c,t.readyState=1;var i=new O("open",{status:e,statusText:n,headers:r});t.dispatchEvent(i),M(t,t.onopen,i)}else{var s="";200!==e?(n&&(n=n.replace(/\s+/g," ")),s="EventSource's response has a status "+e+" "+n+" that is not 200. Aborting the connection."):s="EventSource's response has a Content-Type specifying an unsupported type: "+(null==o?"-":o.replace(/\s+/g," "))+". Aborting the connection.",X(),i=new O("error",{status:e,statusText:n,headers:r}),t.dispatchEvent(i),M(t,t.onerror,i),console.error(s)}},W=function(n){if(1===_){for(var o=-1,i=0;i<n.length;i+=1)(u=n.charCodeAt(i))!=="\n".charCodeAt(0)&&u!=="\r".charCodeAt(0)||(o=i);var s=(-1!==o?$:"")+n.slice(0,o+1);$=(-1===o?$:"")+n.slice(o+1),""!==n&&(f=Date.now(),p+=n.length);for(var a=0;a<s.length;a+=1){var u=s.charCodeAt(a);if(k===H&&u==="\n".charCodeAt(0))k=0;else if(k===H&&(k=0),u==="\r".charCodeAt(0)||u==="\n".charCodeAt(0)){if(0!==k){1===k&&(U=a+1);var y=s.slice(L,U-1),g=s.slice(U+(U<a&&s.charCodeAt(U)===" ".charCodeAt(0)?1:0),a);"data"===y?(m+="\n",m+=g):"id"===y?A=g:"event"===y?x=g:"retry"===y?(c=B(g,c),d=c):"heartbeatTimeout"===y&&(l=B(g,l),0!==b&&(r(b),b=e(function(){G()},l)))}if(0===k){if(""!==m){h=A,""===x&&(x="message");var v=new R(x,{data:m.slice(1),lastEventId:A});if(t.dispatchEvent(v),"open"===x?M(t,t.onopen,v):"message"===x?M(t,t.onmessage,v):"error"===x&&M(t,t.onerror,v),2===_)return}m="",x=""}k=u==="\r".charCodeAt(0)?H:0}else 0===k&&(L=a,k=1),1===k?u===":".charCodeAt(0)&&(U=a+1,k=2):2===k&&(k=3)}}},z=function(n){if(1===_||0===_){_=D,0!==b&&(r(b),b=0),b=e(function(){G()},d),d=I(Math.min(16*c,2*d)),t.readyState=0;var o=new N("error",{error:n});t.dispatchEvent(o),M(t,t.onerror,o),null!=n&&console.error(n)}},X=function(){_=2,null!=T&&(T.abort(),T=void 0),0!==b&&(r(b),b=0),t.readyState=2},G=function(){if(b=0,_===D){f=!1,p=0,b=e(function(){G()},l),_=0,m="",x="",A=h,$="",L=0,U=0,k=0;var o=n;if("data:"!==n.slice(0,5)&&"blob:"!==n.slice(0,5)&&""!==h){var r=n.indexOf("?");o=-1===r?n:n.slice(0,r+1)+n.slice(r+1).replace(/(?:^|&)([^=&]*)(?:=[^&]*)?/g,function(t,e){return e===u?"":t}),o+=(-1===n.indexOf("?")?"?":"&")+u+"="+encodeURIComponent(h)}var i=t.withCredentials,s={Accept:"text/event-stream"},a=t.headers;if(null!=a)for(var c in a)Object.prototype.hasOwnProperty.call(a,c)&&(s[c]=a[c]);try{T=S.open(v,q,W,z,o,i,s)}catch(y){throw X(),y}}else if(f||null==T){var d=Math.max((f||Date.now())+l-Date.now(),1);f=!1,b=e(function(){G()},d)}else z(new Error("No activity within "+l+" milliseconds. "+(0===_?"No response received.":p+" chars received.")+" Reconnecting.")),null!=T&&(T.abort(),T=void 0)};t.url=n,t.readyState=0,t.withCredentials=a,t.headers=y,t._close=X,G()}(this,t,n)}var P=null!=h&&null!=d&&"body"in d.prototype;$.prototype=Object.create(m.prototype),$.prototype.CONNECTING=0,$.prototype.OPEN=1,$.prototype.CLOSED=2,$.prototype.close=function(){this._close()},$.CONNECTING=0,$.OPEN=1,$.CLOSED=2,$.prototype.withCredentials=void 0;var k,L,U=u;null==i||null!=u&&"withCredentials"in u.prototype||(U=$),(k=o).EventSourcePolyfill=$,k.NativeEventSource=u,void 0!==(L=void(k.EventSource=U))&&(n.exports=L)}("undefined"==typeof globalThis?"undefined"!=typeof window?window:"undefined"!=typeof self?self:t:globalThis);var i,s,a={exports:{}},u={exports:{}};i="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",s={rotl:function(t,e){return t<<e|t>>>32-e},rotr:function(t,e){return t<<32-e|t>>>e},endian:function(t){if(t.constructor==Number)return 16711935&s.rotl(t,8)|4278255360&s.rotl(t,24);for(var e=0;e<t.length;e++)t[e]=s.endian(t[e]);return t},randomBytes:function(t){for(var e=[];t>0;t--)e.push(Math.floor(256*Math.random()));return e},bytesToWords:function(t){for(var e=[],n=0,o=0;n<t.length;n++,o+=8)e[o>>>5]|=t[n]<<24-o%32;return e},wordsToBytes:function(t){for(var e=[],n=0;n<32*t.length;n+=8)e.push(t[n>>>5]>>>24-n%32&255);return e},bytesToHex:function(t){for(var e=[],n=0;n<t.length;n++)e.push((t[n]>>>4).toString(16)),e.push((15&t[n]).toString(16));return e.join("")},hexToBytes:function(t){for(var e=[],n=0;n<t.length;n+=2)e.push(parseInt(t.substr(n,2),16));return e},bytesToBase64:function(t){for(var e=[],n=0;n<t.length;n+=3)for(var o=t[n]<<16|t[n+1]<<8|t[n+2],r=0;r<4;r++)8*n+6*r<=8*t.length?e.push(i.charAt(o>>>6*(3-r)&63)):e.push("=");return e.join("")},base64ToBytes:function(t){t=t.replace(/[^A-Z0-9+\/]/gi,"");for(var e=[],n=0,o=0;n<t.length;o=++n%4)0!=o&&e.push((i.indexOf(t.charAt(n-1))&Math.pow(2,-2*o+8)-1)<<2*o|i.indexOf(t.charAt(n))>>>6-2*o);return e}},u.exports=s;var c,l,h,d,f,p=u.exports,y={utf8:{stringToBytes:function(t){return y.bin.stringToBytes(unescape(encodeURIComponent(t)))},bytesToString:function(t){return decodeURIComponent(escape(y.bin.bytesToString(t)))}},bin:{stringToBytes:function(t){for(var e=[],n=0;n<t.length;n++)e.push(255&t.charCodeAt(n));return e},bytesToString:function(t){for(var e=[],n=0;n<t.length;n++)e.push(String.fromCharCode(t[n]));return e.join("")}}},g=y,v=function(t){return null!=t&&(S(t)||function(t){return"function"==typeof t.readFloatLE&&"function"==typeof t.slice&&S(t.slice(0,0))}(t)||!!t._isBuffer)};function S(t){return!!t.constructor&&"function"==typeof t.constructor.isBuffer&&t.constructor.isBuffer(t)}c=p,l=g.utf8,h=v,d=g.bin,(f=function(t,e){t.constructor==String?t=e&&"binary"===e.encoding?d.stringToBytes(t):l.stringToBytes(t):h(t)?t=Array.prototype.slice.call(t,0):Array.isArray(t)||t.constructor===Uint8Array||(t=t.toString());for(var n=c.bytesToWords(t),o=8*t.length,r=1732584193,i=-271733879,s=-1732584194,a=271733878,u=0;u<n.length;u++)n[u]=16711935&(n[u]<<8|n[u]>>>24)|4278255360&(n[u]<<24|n[u]>>>8);n[o>>>5]|=128<<o%32,n[14+(o+64>>>9<<4)]=o;var p=f._ff,y=f._gg,g=f._hh,v=f._ii;for(u=0;u<n.length;u+=16){var S=r,w=i,T=s,b=a;r=p(r,i,s,a,n[u+0],7,-680876936),a=p(a,r,i,s,n[u+1],12,-389564586),s=p(s,a,r,i,n[u+2],17,606105819),i=p(i,s,a,r,n[u+3],22,-1044525330),r=p(r,i,s,a,n[u+4],7,-176418897),a=p(a,r,i,s,n[u+5],12,1200080426),s=p(s,a,r,i,n[u+6],17,-1473231341),i=p(i,s,a,r,n[u+7],22,-45705983),r=p(r,i,s,a,n[u+8],7,1770035416),a=p(a,r,i,s,n[u+9],12,-1958414417),s=p(s,a,r,i,n[u+10],17,-42063),i=p(i,s,a,r,n[u+11],22,-1990404162),r=p(r,i,s,a,n[u+12],7,1804603682),a=p(a,r,i,s,n[u+13],12,-40341101),s=p(s,a,r,i,n[u+14],17,-1502002290),r=y(r,i=p(i,s,a,r,n[u+15],22,1236535329),s,a,n[u+1],5,-165796510),a=y(a,r,i,s,n[u+6],9,-1069501632),s=y(s,a,r,i,n[u+11],14,643717713),i=y(i,s,a,r,n[u+0],20,-373897302),r=y(r,i,s,a,n[u+5],5,-701558691),a=y(a,r,i,s,n[u+10],9,38016083),s=y(s,a,r,i,n[u+15],14,-660478335),i=y(i,s,a,r,n[u+4],20,-405537848),r=y(r,i,s,a,n[u+9],5,568446438),a=y(a,r,i,s,n[u+14],9,-1019803690),s=y(s,a,r,i,n[u+3],14,-187363961),i=y(i,s,a,r,n[u+8],20,1163531501),r=y(r,i,s,a,n[u+13],5,-1444681467),a=y(a,r,i,s,n[u+2],9,-51403784),s=y(s,a,r,i,n[u+7],14,1735328473),r=g(r,i=y(i,s,a,r,n[u+12],20,-1926607734),s,a,n[u+5],4,-378558),a=g(a,r,i,s,n[u+8],11,-2022574463),s=g(s,a,r,i,n[u+11],16,1839030562),i=g(i,s,a,r,n[u+14],23,-35309556),r=g(r,i,s,a,n[u+1],4,-1530992060),a=g(a,r,i,s,n[u+4],11,1272893353),s=g(s,a,r,i,n[u+7],16,-155497632),i=g(i,s,a,r,n[u+10],23,-1094730640),r=g(r,i,s,a,n[u+13],4,681279174),a=g(a,r,i,s,n[u+0],11,-358537222),s=g(s,a,r,i,n[u+3],16,-722521979),i=g(i,s,a,r,n[u+6],23,76029189),r=g(r,i,s,a,n[u+9],4,-640364487),a=g(a,r,i,s,n[u+12],11,-421815835),s=g(s,a,r,i,n[u+15],16,530742520),r=v(r,i=g(i,s,a,r,n[u+2],23,-995338651),s,a,n[u+0],6,-198630844),a=v(a,r,i,s,n[u+7],10,1126891415),s=v(s,a,r,i,n[u+14],15,-1416354905),i=v(i,s,a,r,n[u+5],21,-57434055),r=v(r,i,s,a,n[u+12],6,1700485571),a=v(a,r,i,s,n[u+3],10,-1894986606),s=v(s,a,r,i,n[u+10],15,-1051523),i=v(i,s,a,r,n[u+1],21,-2054922799),r=v(r,i,s,a,n[u+8],6,1873313359),a=v(a,r,i,s,n[u+15],10,-30611744),s=v(s,a,r,i,n[u+6],15,-1560198380),i=v(i,s,a,r,n[u+13],21,1309151649),r=v(r,i,s,a,n[u+4],6,-145523070),a=v(a,r,i,s,n[u+11],10,-1120210379),s=v(s,a,r,i,n[u+2],15,718787259),i=v(i,s,a,r,n[u+9],21,-343485551),r=r+S>>>0,i=i+w>>>0,s=s+T>>>0,a=a+b>>>0}return c.endian([r,i,s,a])})._ff=function(t,e,n,o,r,i,s){var a=t+(e&n|~e&o)+(r>>>0)+s;return(a<<i|a>>>32-i)+e},f._gg=function(t,e,n,o,r,i,s){var a=t+(e&o|n&~o)+(r>>>0)+s;return(a<<i|a>>>32-i)+e},f._hh=function(t,e,n,o,r,i,s){var a=t+(e^n^o)+(r>>>0)+s;return(a<<i|a>>>32-i)+e},f._ii=function(t,e,n,o,r,i,s){var a=t+(n^(e|~o))+(r>>>0)+s;return(a<<i|a>>>32-i)+e},f._blocksize=16,f._digestsize=16,a.exports=function(t,e){if(null==t)throw new Error("Illegal argument "+t);var n=c.wordsToBytes(f(t,e));return e&&e.asBytes?n:e&&e.asString?d.bytesToString(n):c.bytesToHex(n)};const w=e(a.exports);module.exports=class{constructor(t={}){this.url=t.url,this.app_key=t.app_key,this.app_secret=t.app_secret,this.onMessage=t.onMessage||(()=>{}),this.onError=t.onError||(()=>{}),this.onOpen=t.onOpen||(()=>{}),this.onAudioData=t.onAudioData||(()=>{}),this.onAudioNegotiationSuccess=t.onAudioNegotiationSuccess||(()=>{}),this.onConnectSSE=t.onConnectSSE||(()=>{}),this.eventSource=null,this.negotiationResult=null}generateAuthToken(){const t=Math.floor(Date.now()/1e3);return`${this.app_key}:${t}:${w(`${this.app_secret}:${t}`)}`}getAuthHeaders(){return{Authorization:`${this.generateAuthToken()}`,"Content-Type":"application/json"}}async post(t,e){const n=this.generateAuthToken(),o=await fetch(`${this.url}${t}`,{method:"POST",headers:{Authorization:`${n}`,"Content-Type":"application/json","User-Agent":"XY-ASSIST-SDK"},body:JSON.stringify(e)});if(!o.ok){const t=await o.text();throw new Error(`HTTP error! status: ${o.status}, message: ${t}`)}return await o.json()}async audioNegotiation(t){const e=await this.post("/api/v1/agent/ccms/assist/audio-nego",t);return this.negotiationResult=e,this.onAudioNegotiationSuccess&&this.onAudioNegotiationSuccess(e),e}connectSSEWithNegotiationResult(){if(!this.negotiationResult||!this.negotiationResult.result)throw new Error("没有有效的协商结果,请先执行音频协商");const{sse_addr:t,client_id:e}=this.negotiationResult.result;if(!t||!e)throw new Error("协商结果中缺少sse_addr或client_id");const n=`http://${t}/sse?client_id=${e}`;this.connectSSEWithURL(n),this.onConnectSSE&&this.onConnectSSE({sseUrl:n,client_id:e,sse_addr:t})}connectSSEWithURL(t){this.eventSource=new EventSource(t),this.eventSource.onmessage=t=>{const e=JSON.parse(t.data);this.onMessage(e)},this.eventSource.onerror=t=>{this.onError(t)},this.eventSource.onopen=()=>{this.onOpen()}}connectSSE(t=!0){if(t&&this.negotiationResult&&this.negotiationResult.result&&this.negotiationResult.result.sse_addr)this.connectSSEWithNegotiationResult();else{const t=this.generateAuthToken(),e=`${this.url}/api/v1/agent/sse?auth=${t}`;this.eventSource=new EventSource(e),this.eventSource.onmessage=t=>{const e=JSON.parse(t.data);this.onMessage(e)},this.eventSource.onerror=t=>{this.onError(t)},this.eventSource.onopen=()=>{this.onOpen()}}}disconnectSSE(){this.eventSource&&(this.eventSource.close(),this.eventSource=null)}};
@@ -1,7 +1,915 @@
1
- import EventSource from "event-source-polyfill";
1
+ var commonjsGlobal = typeof globalThis !== "undefined" ? globalThis : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : {};
2
2
  function getDefaultExportFromCjs(x) {
3
3
  return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, "default") ? x["default"] : x;
4
4
  }
5
+ var eventsource = { exports: {} };
6
+ /** @license
7
+ * eventsource.js
8
+ * Available under MIT License (MIT)
9
+ * https://github.com/Yaffle/EventSource/
10
+ */
11
+ (function(module, exports$1) {
12
+ (function(global2) {
13
+ var setTimeout = global2.setTimeout;
14
+ var clearTimeout = global2.clearTimeout;
15
+ var XMLHttpRequest = global2.XMLHttpRequest;
16
+ var XDomainRequest = global2.XDomainRequest;
17
+ var ActiveXObject = global2.ActiveXObject;
18
+ var NativeEventSource = global2.EventSource;
19
+ var document = global2.document;
20
+ var Promise2 = global2.Promise;
21
+ var fetch2 = global2.fetch;
22
+ var Response = global2.Response;
23
+ var TextDecoder = global2.TextDecoder;
24
+ var TextEncoder = global2.TextEncoder;
25
+ var AbortController = global2.AbortController;
26
+ if (typeof window !== "undefined" && typeof document !== "undefined" && !("readyState" in document) && document.body == null) {
27
+ document.readyState = "loading";
28
+ window.addEventListener("load", function(event) {
29
+ document.readyState = "complete";
30
+ }, false);
31
+ }
32
+ if (XMLHttpRequest == null && ActiveXObject != null) {
33
+ XMLHttpRequest = function() {
34
+ return new ActiveXObject("Microsoft.XMLHTTP");
35
+ };
36
+ }
37
+ if (Object.create == void 0) {
38
+ Object.create = function(C) {
39
+ function F() {
40
+ }
41
+ F.prototype = C;
42
+ return new F();
43
+ };
44
+ }
45
+ if (!Date.now) {
46
+ Date.now = function now() {
47
+ return (/* @__PURE__ */ new Date()).getTime();
48
+ };
49
+ }
50
+ if (AbortController == void 0) {
51
+ var originalFetch2 = fetch2;
52
+ fetch2 = function(url, options) {
53
+ var signal = options.signal;
54
+ return originalFetch2(url, { headers: options.headers, credentials: options.credentials, cache: options.cache }).then(function(response) {
55
+ var reader = response.body.getReader();
56
+ signal._reader = reader;
57
+ if (signal._aborted) {
58
+ signal._reader.cancel();
59
+ }
60
+ return {
61
+ status: response.status,
62
+ statusText: response.statusText,
63
+ headers: response.headers,
64
+ body: {
65
+ getReader: function() {
66
+ return reader;
67
+ }
68
+ }
69
+ };
70
+ });
71
+ };
72
+ AbortController = function() {
73
+ this.signal = {
74
+ _reader: null,
75
+ _aborted: false
76
+ };
77
+ this.abort = function() {
78
+ if (this.signal._reader != null) {
79
+ this.signal._reader.cancel();
80
+ }
81
+ this.signal._aborted = true;
82
+ };
83
+ };
84
+ }
85
+ function TextDecoderPolyfill() {
86
+ this.bitsNeeded = 0;
87
+ this.codePoint = 0;
88
+ }
89
+ TextDecoderPolyfill.prototype.decode = function(octets) {
90
+ function valid(codePoint2, shift, octetsCount2) {
91
+ if (octetsCount2 === 1) {
92
+ return codePoint2 >= 128 >> shift && codePoint2 << shift <= 2047;
93
+ }
94
+ if (octetsCount2 === 2) {
95
+ return codePoint2 >= 2048 >> shift && codePoint2 << shift <= 55295 || codePoint2 >= 57344 >> shift && codePoint2 << shift <= 65535;
96
+ }
97
+ if (octetsCount2 === 3) {
98
+ return codePoint2 >= 65536 >> shift && codePoint2 << shift <= 1114111;
99
+ }
100
+ throw new Error();
101
+ }
102
+ function octetsCount(bitsNeeded2, codePoint2) {
103
+ if (bitsNeeded2 === 6 * 1) {
104
+ return codePoint2 >> 6 > 15 ? 3 : codePoint2 > 31 ? 2 : 1;
105
+ }
106
+ if (bitsNeeded2 === 6 * 2) {
107
+ return codePoint2 > 15 ? 3 : 2;
108
+ }
109
+ if (bitsNeeded2 === 6 * 3) {
110
+ return 3;
111
+ }
112
+ throw new Error();
113
+ }
114
+ var REPLACER = 65533;
115
+ var string = "";
116
+ var bitsNeeded = this.bitsNeeded;
117
+ var codePoint = this.codePoint;
118
+ for (var i = 0; i < octets.length; i += 1) {
119
+ var octet = octets[i];
120
+ if (bitsNeeded !== 0) {
121
+ if (octet < 128 || octet > 191 || !valid(codePoint << 6 | octet & 63, bitsNeeded - 6, octetsCount(bitsNeeded, codePoint))) {
122
+ bitsNeeded = 0;
123
+ codePoint = REPLACER;
124
+ string += String.fromCharCode(codePoint);
125
+ }
126
+ }
127
+ if (bitsNeeded === 0) {
128
+ if (octet >= 0 && octet <= 127) {
129
+ bitsNeeded = 0;
130
+ codePoint = octet;
131
+ } else if (octet >= 192 && octet <= 223) {
132
+ bitsNeeded = 6 * 1;
133
+ codePoint = octet & 31;
134
+ } else if (octet >= 224 && octet <= 239) {
135
+ bitsNeeded = 6 * 2;
136
+ codePoint = octet & 15;
137
+ } else if (octet >= 240 && octet <= 247) {
138
+ bitsNeeded = 6 * 3;
139
+ codePoint = octet & 7;
140
+ } else {
141
+ bitsNeeded = 0;
142
+ codePoint = REPLACER;
143
+ }
144
+ if (bitsNeeded !== 0 && !valid(codePoint, bitsNeeded, octetsCount(bitsNeeded, codePoint))) {
145
+ bitsNeeded = 0;
146
+ codePoint = REPLACER;
147
+ }
148
+ } else {
149
+ bitsNeeded -= 6;
150
+ codePoint = codePoint << 6 | octet & 63;
151
+ }
152
+ if (bitsNeeded === 0) {
153
+ if (codePoint <= 65535) {
154
+ string += String.fromCharCode(codePoint);
155
+ } else {
156
+ string += String.fromCharCode(55296 + (codePoint - 65535 - 1 >> 10));
157
+ string += String.fromCharCode(56320 + (codePoint - 65535 - 1 & 1023));
158
+ }
159
+ }
160
+ }
161
+ this.bitsNeeded = bitsNeeded;
162
+ this.codePoint = codePoint;
163
+ return string;
164
+ };
165
+ var supportsStreamOption = function() {
166
+ try {
167
+ return new TextDecoder().decode(new TextEncoder().encode("test"), { stream: true }) === "test";
168
+ } catch (error) {
169
+ console.debug("TextDecoder does not support streaming option. Using polyfill instead: " + error);
170
+ }
171
+ return false;
172
+ };
173
+ if (TextDecoder == void 0 || TextEncoder == void 0 || !supportsStreamOption()) {
174
+ TextDecoder = TextDecoderPolyfill;
175
+ }
176
+ var k = function() {
177
+ };
178
+ function XHRWrapper(xhr) {
179
+ this.withCredentials = false;
180
+ this.readyState = 0;
181
+ this.status = 0;
182
+ this.statusText = "";
183
+ this.responseText = "";
184
+ this.onprogress = k;
185
+ this.onload = k;
186
+ this.onerror = k;
187
+ this.onreadystatechange = k;
188
+ this._contentType = "";
189
+ this._xhr = xhr;
190
+ this._sendTimeout = 0;
191
+ this._abort = k;
192
+ }
193
+ XHRWrapper.prototype.open = function(method, url) {
194
+ this._abort(true);
195
+ var that = this;
196
+ var xhr = this._xhr;
197
+ var state = 1;
198
+ var timeout = 0;
199
+ this._abort = function(silent) {
200
+ if (that._sendTimeout !== 0) {
201
+ clearTimeout(that._sendTimeout);
202
+ that._sendTimeout = 0;
203
+ }
204
+ if (state === 1 || state === 2 || state === 3) {
205
+ state = 4;
206
+ xhr.onload = k;
207
+ xhr.onerror = k;
208
+ xhr.onabort = k;
209
+ xhr.onprogress = k;
210
+ xhr.onreadystatechange = k;
211
+ xhr.abort();
212
+ if (timeout !== 0) {
213
+ clearTimeout(timeout);
214
+ timeout = 0;
215
+ }
216
+ if (!silent) {
217
+ that.readyState = 4;
218
+ that.onabort(null);
219
+ that.onreadystatechange();
220
+ }
221
+ }
222
+ state = 0;
223
+ };
224
+ var onStart = function() {
225
+ if (state === 1) {
226
+ var status = 0;
227
+ var statusText = "";
228
+ var contentType = void 0;
229
+ if (!("contentType" in xhr)) {
230
+ try {
231
+ status = xhr.status;
232
+ statusText = xhr.statusText;
233
+ contentType = xhr.getResponseHeader("Content-Type");
234
+ } catch (error) {
235
+ status = 0;
236
+ statusText = "";
237
+ contentType = void 0;
238
+ }
239
+ } else {
240
+ status = 200;
241
+ statusText = "OK";
242
+ contentType = xhr.contentType;
243
+ }
244
+ if (status !== 0) {
245
+ state = 2;
246
+ that.readyState = 2;
247
+ that.status = status;
248
+ that.statusText = statusText;
249
+ that._contentType = contentType;
250
+ that.onreadystatechange();
251
+ }
252
+ }
253
+ };
254
+ var onProgress = function() {
255
+ onStart();
256
+ if (state === 2 || state === 3) {
257
+ state = 3;
258
+ var responseText = "";
259
+ try {
260
+ responseText = xhr.responseText;
261
+ } catch (error) {
262
+ }
263
+ that.readyState = 3;
264
+ that.responseText = responseText;
265
+ that.onprogress();
266
+ }
267
+ };
268
+ var onFinish = function(type, event) {
269
+ if (event == null || event.preventDefault == null) {
270
+ event = {
271
+ preventDefault: k
272
+ };
273
+ }
274
+ onProgress();
275
+ if (state === 1 || state === 2 || state === 3) {
276
+ state = 4;
277
+ if (timeout !== 0) {
278
+ clearTimeout(timeout);
279
+ timeout = 0;
280
+ }
281
+ that.readyState = 4;
282
+ if (type === "load") {
283
+ that.onload(event);
284
+ } else if (type === "error") {
285
+ that.onerror(event);
286
+ } else if (type === "abort") {
287
+ that.onabort(event);
288
+ } else {
289
+ throw new TypeError();
290
+ }
291
+ that.onreadystatechange();
292
+ }
293
+ };
294
+ var onReadyStateChange = function(event) {
295
+ if (xhr != void 0) {
296
+ if (xhr.readyState === 4) {
297
+ if (!("onload" in xhr) || !("onerror" in xhr) || !("onabort" in xhr)) {
298
+ onFinish(xhr.responseText === "" ? "error" : "load", event);
299
+ }
300
+ } else if (xhr.readyState === 3) {
301
+ if (!("onprogress" in xhr)) {
302
+ onProgress();
303
+ }
304
+ } else if (xhr.readyState === 2) {
305
+ onStart();
306
+ }
307
+ }
308
+ };
309
+ var onTimeout = function() {
310
+ timeout = setTimeout(function() {
311
+ onTimeout();
312
+ }, 500);
313
+ if (xhr.readyState === 3) {
314
+ onProgress();
315
+ }
316
+ };
317
+ if ("onload" in xhr) {
318
+ xhr.onload = function(event) {
319
+ onFinish("load", event);
320
+ };
321
+ }
322
+ if ("onerror" in xhr) {
323
+ xhr.onerror = function(event) {
324
+ onFinish("error", event);
325
+ };
326
+ }
327
+ if ("onabort" in xhr) {
328
+ xhr.onabort = function(event) {
329
+ onFinish("abort", event);
330
+ };
331
+ }
332
+ if ("onprogress" in xhr) {
333
+ xhr.onprogress = onProgress;
334
+ }
335
+ if ("onreadystatechange" in xhr) {
336
+ xhr.onreadystatechange = function(event) {
337
+ onReadyStateChange(event);
338
+ };
339
+ }
340
+ if ("contentType" in xhr || !("ontimeout" in XMLHttpRequest.prototype)) {
341
+ url += (url.indexOf("?") === -1 ? "?" : "&") + "padding=true";
342
+ }
343
+ xhr.open(method, url, true);
344
+ if ("readyState" in xhr) {
345
+ timeout = setTimeout(function() {
346
+ onTimeout();
347
+ }, 0);
348
+ }
349
+ };
350
+ XHRWrapper.prototype.abort = function() {
351
+ this._abort(false);
352
+ };
353
+ XHRWrapper.prototype.getResponseHeader = function(name) {
354
+ return this._contentType;
355
+ };
356
+ XHRWrapper.prototype.setRequestHeader = function(name, value) {
357
+ var xhr = this._xhr;
358
+ if ("setRequestHeader" in xhr) {
359
+ xhr.setRequestHeader(name, value);
360
+ }
361
+ };
362
+ XHRWrapper.prototype.getAllResponseHeaders = function() {
363
+ return this._xhr.getAllResponseHeaders != void 0 ? this._xhr.getAllResponseHeaders() || "" : "";
364
+ };
365
+ XHRWrapper.prototype.send = function() {
366
+ if ((!("ontimeout" in XMLHttpRequest.prototype) || !("sendAsBinary" in XMLHttpRequest.prototype) && !("mozAnon" in XMLHttpRequest.prototype)) && document != void 0 && document.readyState != void 0 && document.readyState !== "complete") {
367
+ var that = this;
368
+ that._sendTimeout = setTimeout(function() {
369
+ that._sendTimeout = 0;
370
+ that.send();
371
+ }, 4);
372
+ return;
373
+ }
374
+ var xhr = this._xhr;
375
+ if ("withCredentials" in xhr) {
376
+ xhr.withCredentials = this.withCredentials;
377
+ }
378
+ try {
379
+ xhr.send(void 0);
380
+ } catch (error1) {
381
+ throw error1;
382
+ }
383
+ };
384
+ function toLowerCase(name) {
385
+ return name.replace(/[A-Z]/g, function(c) {
386
+ return String.fromCharCode(c.charCodeAt(0) + 32);
387
+ });
388
+ }
389
+ function HeadersPolyfill(all) {
390
+ var map = /* @__PURE__ */ Object.create(null);
391
+ var array = all.split("\r\n");
392
+ for (var i = 0; i < array.length; i += 1) {
393
+ var line = array[i];
394
+ var parts = line.split(": ");
395
+ var name = parts.shift();
396
+ var value = parts.join(": ");
397
+ map[toLowerCase(name)] = value;
398
+ }
399
+ this._map = map;
400
+ }
401
+ HeadersPolyfill.prototype.get = function(name) {
402
+ return this._map[toLowerCase(name)];
403
+ };
404
+ if (XMLHttpRequest != null && XMLHttpRequest.HEADERS_RECEIVED == null) {
405
+ XMLHttpRequest.HEADERS_RECEIVED = 2;
406
+ }
407
+ function XHRTransport() {
408
+ }
409
+ XHRTransport.prototype.open = function(xhr, onStartCallback, onProgressCallback, onFinishCallback, url, withCredentials, headers) {
410
+ xhr.open("GET", url);
411
+ var offset = 0;
412
+ xhr.onprogress = function() {
413
+ var responseText = xhr.responseText;
414
+ var chunk = responseText.slice(offset);
415
+ offset += chunk.length;
416
+ onProgressCallback(chunk);
417
+ };
418
+ xhr.onerror = function(event) {
419
+ event.preventDefault();
420
+ onFinishCallback(new Error("NetworkError"));
421
+ };
422
+ xhr.onload = function() {
423
+ onFinishCallback(null);
424
+ };
425
+ xhr.onabort = function() {
426
+ onFinishCallback(null);
427
+ };
428
+ xhr.onreadystatechange = function() {
429
+ if (xhr.readyState === XMLHttpRequest.HEADERS_RECEIVED) {
430
+ var status = xhr.status;
431
+ var statusText = xhr.statusText;
432
+ var contentType = xhr.getResponseHeader("Content-Type");
433
+ var headers2 = xhr.getAllResponseHeaders();
434
+ onStartCallback(status, statusText, contentType, new HeadersPolyfill(headers2));
435
+ }
436
+ };
437
+ xhr.withCredentials = withCredentials;
438
+ for (var name in headers) {
439
+ if (Object.prototype.hasOwnProperty.call(headers, name)) {
440
+ xhr.setRequestHeader(name, headers[name]);
441
+ }
442
+ }
443
+ xhr.send();
444
+ return xhr;
445
+ };
446
+ function HeadersWrapper(headers) {
447
+ this._headers = headers;
448
+ }
449
+ HeadersWrapper.prototype.get = function(name) {
450
+ return this._headers.get(name);
451
+ };
452
+ function FetchTransport() {
453
+ }
454
+ FetchTransport.prototype.open = function(xhr, onStartCallback, onProgressCallback, onFinishCallback, url, withCredentials, headers) {
455
+ var reader = null;
456
+ var controller = new AbortController();
457
+ var signal = controller.signal;
458
+ var textDecoder = new TextDecoder();
459
+ fetch2(url, {
460
+ headers,
461
+ credentials: withCredentials ? "include" : "same-origin",
462
+ signal,
463
+ cache: "no-store"
464
+ }).then(function(response) {
465
+ reader = response.body.getReader();
466
+ onStartCallback(response.status, response.statusText, response.headers.get("Content-Type"), new HeadersWrapper(response.headers));
467
+ return new Promise2(function(resolve, reject) {
468
+ var readNextChunk = function() {
469
+ reader.read().then(function(result) {
470
+ if (result.done) {
471
+ resolve(void 0);
472
+ } else {
473
+ var chunk = textDecoder.decode(result.value, { stream: true });
474
+ onProgressCallback(chunk);
475
+ readNextChunk();
476
+ }
477
+ })["catch"](function(error) {
478
+ reject(error);
479
+ });
480
+ };
481
+ readNextChunk();
482
+ });
483
+ })["catch"](function(error) {
484
+ if (error.name === "AbortError") {
485
+ return void 0;
486
+ } else {
487
+ return error;
488
+ }
489
+ }).then(function(error) {
490
+ onFinishCallback(error);
491
+ });
492
+ return {
493
+ abort: function() {
494
+ if (reader != null) {
495
+ reader.cancel();
496
+ }
497
+ controller.abort();
498
+ }
499
+ };
500
+ };
501
+ function EventTarget() {
502
+ this._listeners = /* @__PURE__ */ Object.create(null);
503
+ }
504
+ function throwError(e) {
505
+ setTimeout(function() {
506
+ throw e;
507
+ }, 0);
508
+ }
509
+ EventTarget.prototype.dispatchEvent = function(event) {
510
+ event.target = this;
511
+ var typeListeners = this._listeners[event.type];
512
+ if (typeListeners != void 0) {
513
+ var length = typeListeners.length;
514
+ for (var i = 0; i < length; i += 1) {
515
+ var listener = typeListeners[i];
516
+ try {
517
+ if (typeof listener.handleEvent === "function") {
518
+ listener.handleEvent(event);
519
+ } else {
520
+ listener.call(this, event);
521
+ }
522
+ } catch (e) {
523
+ throwError(e);
524
+ }
525
+ }
526
+ }
527
+ };
528
+ EventTarget.prototype.addEventListener = function(type, listener) {
529
+ type = String(type);
530
+ var listeners = this._listeners;
531
+ var typeListeners = listeners[type];
532
+ if (typeListeners == void 0) {
533
+ typeListeners = [];
534
+ listeners[type] = typeListeners;
535
+ }
536
+ var found = false;
537
+ for (var i = 0; i < typeListeners.length; i += 1) {
538
+ if (typeListeners[i] === listener) {
539
+ found = true;
540
+ }
541
+ }
542
+ if (!found) {
543
+ typeListeners.push(listener);
544
+ }
545
+ };
546
+ EventTarget.prototype.removeEventListener = function(type, listener) {
547
+ type = String(type);
548
+ var listeners = this._listeners;
549
+ var typeListeners = listeners[type];
550
+ if (typeListeners != void 0) {
551
+ var filtered = [];
552
+ for (var i = 0; i < typeListeners.length; i += 1) {
553
+ if (typeListeners[i] !== listener) {
554
+ filtered.push(typeListeners[i]);
555
+ }
556
+ }
557
+ if (filtered.length === 0) {
558
+ delete listeners[type];
559
+ } else {
560
+ listeners[type] = filtered;
561
+ }
562
+ }
563
+ };
564
+ function Event(type) {
565
+ this.type = type;
566
+ this.target = void 0;
567
+ }
568
+ function MessageEvent(type, options) {
569
+ Event.call(this, type);
570
+ this.data = options.data;
571
+ this.lastEventId = options.lastEventId;
572
+ }
573
+ MessageEvent.prototype = Object.create(Event.prototype);
574
+ function ConnectionEvent(type, options) {
575
+ Event.call(this, type);
576
+ this.status = options.status;
577
+ this.statusText = options.statusText;
578
+ this.headers = options.headers;
579
+ }
580
+ ConnectionEvent.prototype = Object.create(Event.prototype);
581
+ function ErrorEvent(type, options) {
582
+ Event.call(this, type);
583
+ this.error = options.error;
584
+ }
585
+ ErrorEvent.prototype = Object.create(Event.prototype);
586
+ var WAITING = -1;
587
+ var CONNECTING = 0;
588
+ var OPEN = 1;
589
+ var CLOSED = 2;
590
+ var AFTER_CR = -1;
591
+ var FIELD_START = 0;
592
+ var FIELD = 1;
593
+ var VALUE_START = 2;
594
+ var VALUE = 3;
595
+ var contentTypeRegExp = /^text\/event\-stream(;.*)?$/i;
596
+ var MINIMUM_DURATION = 1e3;
597
+ var MAXIMUM_DURATION = 18e6;
598
+ var parseDuration = function(value, def) {
599
+ var n = value == null ? def : parseInt(value, 10);
600
+ if (n !== n) {
601
+ n = def;
602
+ }
603
+ return clampDuration(n);
604
+ };
605
+ var clampDuration = function(n) {
606
+ return Math.min(Math.max(n, MINIMUM_DURATION), MAXIMUM_DURATION);
607
+ };
608
+ var fire = function(that, f, event) {
609
+ try {
610
+ if (typeof f === "function") {
611
+ f.call(that, event);
612
+ }
613
+ } catch (e) {
614
+ throwError(e);
615
+ }
616
+ };
617
+ function EventSourcePolyfill(url, options) {
618
+ EventTarget.call(this);
619
+ options = options || {};
620
+ this.onopen = void 0;
621
+ this.onmessage = void 0;
622
+ this.onerror = void 0;
623
+ this.url = void 0;
624
+ this.readyState = void 0;
625
+ this.withCredentials = void 0;
626
+ this.headers = void 0;
627
+ this._close = void 0;
628
+ start(this, url, options);
629
+ }
630
+ function getBestXHRTransport() {
631
+ return XMLHttpRequest != void 0 && "withCredentials" in XMLHttpRequest.prototype || XDomainRequest == void 0 ? new XMLHttpRequest() : new XDomainRequest();
632
+ }
633
+ var isFetchSupported = fetch2 != void 0 && Response != void 0 && "body" in Response.prototype;
634
+ function start(es, url, options) {
635
+ url = String(url);
636
+ var withCredentials = Boolean(options.withCredentials);
637
+ var lastEventIdQueryParameterName = options.lastEventIdQueryParameterName || "lastEventId";
638
+ var initialRetry = clampDuration(1e3);
639
+ var heartbeatTimeout = parseDuration(options.heartbeatTimeout, 45e3);
640
+ var lastEventId = "";
641
+ var retry = initialRetry;
642
+ var wasActivity = false;
643
+ var textLength = 0;
644
+ var headers = options.headers || {};
645
+ var TransportOption = options.Transport;
646
+ var xhr = isFetchSupported && TransportOption == void 0 ? void 0 : new XHRWrapper(TransportOption != void 0 ? new TransportOption() : getBestXHRTransport());
647
+ var transport = TransportOption != null && typeof TransportOption !== "string" ? new TransportOption() : xhr == void 0 ? new FetchTransport() : new XHRTransport();
648
+ var abortController = void 0;
649
+ var timeout = 0;
650
+ var currentState = WAITING;
651
+ var dataBuffer = "";
652
+ var lastEventIdBuffer = "";
653
+ var eventTypeBuffer = "";
654
+ var textBuffer = "";
655
+ var state = FIELD_START;
656
+ var fieldStart = 0;
657
+ var valueStart = 0;
658
+ var onStart = function(status, statusText, contentType, headers2) {
659
+ if (currentState === CONNECTING) {
660
+ if (status === 200 && contentType != void 0 && contentTypeRegExp.test(contentType)) {
661
+ currentState = OPEN;
662
+ wasActivity = Date.now();
663
+ retry = initialRetry;
664
+ es.readyState = OPEN;
665
+ var event = new ConnectionEvent("open", {
666
+ status,
667
+ statusText,
668
+ headers: headers2
669
+ });
670
+ es.dispatchEvent(event);
671
+ fire(es, es.onopen, event);
672
+ } else {
673
+ var message = "";
674
+ if (status !== 200) {
675
+ if (statusText) {
676
+ statusText = statusText.replace(/\s+/g, " ");
677
+ }
678
+ message = "EventSource's response has a status " + status + " " + statusText + " that is not 200. Aborting the connection.";
679
+ } else {
680
+ message = "EventSource's response has a Content-Type specifying an unsupported type: " + (contentType == void 0 ? "-" : contentType.replace(/\s+/g, " ")) + ". Aborting the connection.";
681
+ }
682
+ close();
683
+ var event = new ConnectionEvent("error", {
684
+ status,
685
+ statusText,
686
+ headers: headers2
687
+ });
688
+ es.dispatchEvent(event);
689
+ fire(es, es.onerror, event);
690
+ console.error(message);
691
+ }
692
+ }
693
+ };
694
+ var onProgress = function(textChunk) {
695
+ if (currentState === OPEN) {
696
+ var n = -1;
697
+ for (var i = 0; i < textChunk.length; i += 1) {
698
+ var c = textChunk.charCodeAt(i);
699
+ if (c === "\n".charCodeAt(0) || c === "\r".charCodeAt(0)) {
700
+ n = i;
701
+ }
702
+ }
703
+ var chunk = (n !== -1 ? textBuffer : "") + textChunk.slice(0, n + 1);
704
+ textBuffer = (n === -1 ? textBuffer : "") + textChunk.slice(n + 1);
705
+ if (textChunk !== "") {
706
+ wasActivity = Date.now();
707
+ textLength += textChunk.length;
708
+ }
709
+ for (var position = 0; position < chunk.length; position += 1) {
710
+ var c = chunk.charCodeAt(position);
711
+ if (state === AFTER_CR && c === "\n".charCodeAt(0)) {
712
+ state = FIELD_START;
713
+ } else {
714
+ if (state === AFTER_CR) {
715
+ state = FIELD_START;
716
+ }
717
+ if (c === "\r".charCodeAt(0) || c === "\n".charCodeAt(0)) {
718
+ if (state !== FIELD_START) {
719
+ if (state === FIELD) {
720
+ valueStart = position + 1;
721
+ }
722
+ var field = chunk.slice(fieldStart, valueStart - 1);
723
+ var value = chunk.slice(valueStart + (valueStart < position && chunk.charCodeAt(valueStart) === " ".charCodeAt(0) ? 1 : 0), position);
724
+ if (field === "data") {
725
+ dataBuffer += "\n";
726
+ dataBuffer += value;
727
+ } else if (field === "id") {
728
+ lastEventIdBuffer = value;
729
+ } else if (field === "event") {
730
+ eventTypeBuffer = value;
731
+ } else if (field === "retry") {
732
+ initialRetry = parseDuration(value, initialRetry);
733
+ retry = initialRetry;
734
+ } else if (field === "heartbeatTimeout") {
735
+ heartbeatTimeout = parseDuration(value, heartbeatTimeout);
736
+ if (timeout !== 0) {
737
+ clearTimeout(timeout);
738
+ timeout = setTimeout(function() {
739
+ onTimeout();
740
+ }, heartbeatTimeout);
741
+ }
742
+ }
743
+ }
744
+ if (state === FIELD_START) {
745
+ if (dataBuffer !== "") {
746
+ lastEventId = lastEventIdBuffer;
747
+ if (eventTypeBuffer === "") {
748
+ eventTypeBuffer = "message";
749
+ }
750
+ var event = new MessageEvent(eventTypeBuffer, {
751
+ data: dataBuffer.slice(1),
752
+ lastEventId: lastEventIdBuffer
753
+ });
754
+ es.dispatchEvent(event);
755
+ if (eventTypeBuffer === "open") {
756
+ fire(es, es.onopen, event);
757
+ } else if (eventTypeBuffer === "message") {
758
+ fire(es, es.onmessage, event);
759
+ } else if (eventTypeBuffer === "error") {
760
+ fire(es, es.onerror, event);
761
+ }
762
+ if (currentState === CLOSED) {
763
+ return;
764
+ }
765
+ }
766
+ dataBuffer = "";
767
+ eventTypeBuffer = "";
768
+ }
769
+ state = c === "\r".charCodeAt(0) ? AFTER_CR : FIELD_START;
770
+ } else {
771
+ if (state === FIELD_START) {
772
+ fieldStart = position;
773
+ state = FIELD;
774
+ }
775
+ if (state === FIELD) {
776
+ if (c === ":".charCodeAt(0)) {
777
+ valueStart = position + 1;
778
+ state = VALUE_START;
779
+ }
780
+ } else if (state === VALUE_START) {
781
+ state = VALUE;
782
+ }
783
+ }
784
+ }
785
+ }
786
+ }
787
+ };
788
+ var onFinish = function(error) {
789
+ if (currentState === OPEN || currentState === CONNECTING) {
790
+ currentState = WAITING;
791
+ if (timeout !== 0) {
792
+ clearTimeout(timeout);
793
+ timeout = 0;
794
+ }
795
+ timeout = setTimeout(function() {
796
+ onTimeout();
797
+ }, retry);
798
+ retry = clampDuration(Math.min(initialRetry * 16, retry * 2));
799
+ es.readyState = CONNECTING;
800
+ var event = new ErrorEvent("error", { error });
801
+ es.dispatchEvent(event);
802
+ fire(es, es.onerror, event);
803
+ if (error != void 0) {
804
+ console.error(error);
805
+ }
806
+ }
807
+ };
808
+ var close = function() {
809
+ currentState = CLOSED;
810
+ if (abortController != void 0) {
811
+ abortController.abort();
812
+ abortController = void 0;
813
+ }
814
+ if (timeout !== 0) {
815
+ clearTimeout(timeout);
816
+ timeout = 0;
817
+ }
818
+ es.readyState = CLOSED;
819
+ };
820
+ var onTimeout = function() {
821
+ timeout = 0;
822
+ if (currentState !== WAITING) {
823
+ if (!wasActivity && abortController != void 0) {
824
+ onFinish(new Error("No activity within " + heartbeatTimeout + " milliseconds. " + (currentState === CONNECTING ? "No response received." : textLength + " chars received.") + " Reconnecting."));
825
+ if (abortController != void 0) {
826
+ abortController.abort();
827
+ abortController = void 0;
828
+ }
829
+ } else {
830
+ var nextHeartbeat = Math.max((wasActivity || Date.now()) + heartbeatTimeout - Date.now(), 1);
831
+ wasActivity = false;
832
+ timeout = setTimeout(function() {
833
+ onTimeout();
834
+ }, nextHeartbeat);
835
+ }
836
+ return;
837
+ }
838
+ wasActivity = false;
839
+ textLength = 0;
840
+ timeout = setTimeout(function() {
841
+ onTimeout();
842
+ }, heartbeatTimeout);
843
+ currentState = CONNECTING;
844
+ dataBuffer = "";
845
+ eventTypeBuffer = "";
846
+ lastEventIdBuffer = lastEventId;
847
+ textBuffer = "";
848
+ fieldStart = 0;
849
+ valueStart = 0;
850
+ state = FIELD_START;
851
+ var requestURL = url;
852
+ if (url.slice(0, 5) !== "data:" && url.slice(0, 5) !== "blob:") {
853
+ if (lastEventId !== "") {
854
+ var i = url.indexOf("?");
855
+ requestURL = i === -1 ? url : url.slice(0, i + 1) + url.slice(i + 1).replace(/(?:^|&)([^=&]*)(?:=[^&]*)?/g, function(p, paramName) {
856
+ return paramName === lastEventIdQueryParameterName ? "" : p;
857
+ });
858
+ requestURL += (url.indexOf("?") === -1 ? "?" : "&") + lastEventIdQueryParameterName + "=" + encodeURIComponent(lastEventId);
859
+ }
860
+ }
861
+ var withCredentials2 = es.withCredentials;
862
+ var requestHeaders = {};
863
+ requestHeaders["Accept"] = "text/event-stream";
864
+ var headers2 = es.headers;
865
+ if (headers2 != void 0) {
866
+ for (var name in headers2) {
867
+ if (Object.prototype.hasOwnProperty.call(headers2, name)) {
868
+ requestHeaders[name] = headers2[name];
869
+ }
870
+ }
871
+ }
872
+ try {
873
+ abortController = transport.open(xhr, onStart, onProgress, onFinish, requestURL, withCredentials2, requestHeaders);
874
+ } catch (error) {
875
+ close();
876
+ throw error;
877
+ }
878
+ };
879
+ es.url = url;
880
+ es.readyState = CONNECTING;
881
+ es.withCredentials = withCredentials;
882
+ es.headers = headers;
883
+ es._close = close;
884
+ onTimeout();
885
+ }
886
+ EventSourcePolyfill.prototype = Object.create(EventTarget.prototype);
887
+ EventSourcePolyfill.prototype.CONNECTING = CONNECTING;
888
+ EventSourcePolyfill.prototype.OPEN = OPEN;
889
+ EventSourcePolyfill.prototype.CLOSED = CLOSED;
890
+ EventSourcePolyfill.prototype.close = function() {
891
+ this._close();
892
+ };
893
+ EventSourcePolyfill.CONNECTING = CONNECTING;
894
+ EventSourcePolyfill.OPEN = OPEN;
895
+ EventSourcePolyfill.CLOSED = CLOSED;
896
+ EventSourcePolyfill.prototype.withCredentials = void 0;
897
+ var R = NativeEventSource;
898
+ if (XMLHttpRequest != void 0 && (NativeEventSource == void 0 || !("withCredentials" in NativeEventSource.prototype))) {
899
+ R = EventSourcePolyfill;
900
+ }
901
+ (function(factory) {
902
+ {
903
+ var v = factory(exports$1);
904
+ if (v !== void 0) module.exports = v;
905
+ }
906
+ })(function(exports2) {
907
+ exports2.EventSourcePolyfill = EventSourcePolyfill;
908
+ exports2.NativeEventSource = NativeEventSource;
909
+ exports2.EventSource = R;
910
+ });
911
+ })(typeof globalThis === "undefined" ? typeof window !== "undefined" ? window : typeof self !== "undefined" ? self : commonjsGlobal : globalThis);
912
+ })(eventsource, eventsource.exports);
5
913
  var md5$1 = { exports: {} };
6
914
  var crypt = { exports: {} };
7
915
  (function() {
@@ -1 +1,6 @@
1
- !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("event-source-polyfill")):"function"==typeof define&&define.amd?define(["event-source-polyfill"],e):(t="undefined"!=typeof globalThis?globalThis:t||self).AgentAssistantSDK=e(t.EventSource)}(this,function(t){"use strict";function e(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}var n,o,r={exports:{}},s={exports:{}};n="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",o={rotl:function(t,e){return t<<e|t>>>32-e},rotr:function(t,e){return t<<32-e|t>>>e},endian:function(t){if(t.constructor==Number)return 16711935&o.rotl(t,8)|4278255360&o.rotl(t,24);for(var e=0;e<t.length;e++)t[e]=o.endian(t[e]);return t},randomBytes:function(t){for(var e=[];t>0;t--)e.push(Math.floor(256*Math.random()));return e},bytesToWords:function(t){for(var e=[],n=0,o=0;n<t.length;n++,o+=8)e[o>>>5]|=t[n]<<24-o%32;return e},wordsToBytes:function(t){for(var e=[],n=0;n<32*t.length;n+=8)e.push(t[n>>>5]>>>24-n%32&255);return e},bytesToHex:function(t){for(var e=[],n=0;n<t.length;n++)e.push((t[n]>>>4).toString(16)),e.push((15&t[n]).toString(16));return e.join("")},hexToBytes:function(t){for(var e=[],n=0;n<t.length;n+=2)e.push(parseInt(t.substr(n,2),16));return e},bytesToBase64:function(t){for(var e=[],o=0;o<t.length;o+=3)for(var r=t[o]<<16|t[o+1]<<8|t[o+2],s=0;s<4;s++)8*o+6*s<=8*t.length?e.push(n.charAt(r>>>6*(3-s)&63)):e.push("=");return e.join("")},base64ToBytes:function(t){t=t.replace(/[^A-Z0-9+\/]/gi,"");for(var e=[],o=0,r=0;o<t.length;r=++o%4)0!=r&&e.push((n.indexOf(t.charAt(o-1))&Math.pow(2,-2*r+8)-1)<<2*r|n.indexOf(t.charAt(o))>>>6-2*r);return e}},s.exports=o;var i,u,a,c,h,f=s.exports,l={utf8:{stringToBytes:function(t){return l.bin.stringToBytes(unescape(encodeURIComponent(t)))},bytesToString:function(t){return decodeURIComponent(escape(l.bin.bytesToString(t)))}},bin:{stringToBytes:function(t){for(var e=[],n=0;n<t.length;n++)e.push(255&t.charCodeAt(n));return e},bytesToString:function(t){for(var e=[],n=0;n<t.length;n++)e.push(String.fromCharCode(t[n]));return e.join("")}}},g=l,p=function(t){return null!=t&&(d(t)||function(t){return"function"==typeof t.readFloatLE&&"function"==typeof t.slice&&d(t.slice(0,0))}(t)||!!t._isBuffer)};function d(t){return!!t.constructor&&"function"==typeof t.constructor.isBuffer&&t.constructor.isBuffer(t)}i=f,u=g.utf8,a=p,c=g.bin,(h=function(t,e){t.constructor==String?t=e&&"binary"===e.encoding?c.stringToBytes(t):u.stringToBytes(t):a(t)?t=Array.prototype.slice.call(t,0):Array.isArray(t)||t.constructor===Uint8Array||(t=t.toString());for(var n=i.bytesToWords(t),o=8*t.length,r=1732584193,s=-271733879,f=-1732584194,l=271733878,g=0;g<n.length;g++)n[g]=16711935&(n[g]<<8|n[g]>>>24)|4278255360&(n[g]<<24|n[g]>>>8);n[o>>>5]|=128<<o%32,n[14+(o+64>>>9<<4)]=o;var p=h._ff,d=h._gg,S=h._hh,y=h._ii;for(g=0;g<n.length;g+=16){var v=r,T=s,A=f,_=l;r=p(r,s,f,l,n[g+0],7,-680876936),l=p(l,r,s,f,n[g+1],12,-389564586),f=p(f,l,r,s,n[g+2],17,606105819),s=p(s,f,l,r,n[g+3],22,-1044525330),r=p(r,s,f,l,n[g+4],7,-176418897),l=p(l,r,s,f,n[g+5],12,1200080426),f=p(f,l,r,s,n[g+6],17,-1473231341),s=p(s,f,l,r,n[g+7],22,-45705983),r=p(r,s,f,l,n[g+8],7,1770035416),l=p(l,r,s,f,n[g+9],12,-1958414417),f=p(f,l,r,s,n[g+10],17,-42063),s=p(s,f,l,r,n[g+11],22,-1990404162),r=p(r,s,f,l,n[g+12],7,1804603682),l=p(l,r,s,f,n[g+13],12,-40341101),f=p(f,l,r,s,n[g+14],17,-1502002290),r=d(r,s=p(s,f,l,r,n[g+15],22,1236535329),f,l,n[g+1],5,-165796510),l=d(l,r,s,f,n[g+6],9,-1069501632),f=d(f,l,r,s,n[g+11],14,643717713),s=d(s,f,l,r,n[g+0],20,-373897302),r=d(r,s,f,l,n[g+5],5,-701558691),l=d(l,r,s,f,n[g+10],9,38016083),f=d(f,l,r,s,n[g+15],14,-660478335),s=d(s,f,l,r,n[g+4],20,-405537848),r=d(r,s,f,l,n[g+9],5,568446438),l=d(l,r,s,f,n[g+14],9,-1019803690),f=d(f,l,r,s,n[g+3],14,-187363961),s=d(s,f,l,r,n[g+8],20,1163531501),r=d(r,s,f,l,n[g+13],5,-1444681467),l=d(l,r,s,f,n[g+2],9,-51403784),f=d(f,l,r,s,n[g+7],14,1735328473),r=S(r,s=d(s,f,l,r,n[g+12],20,-1926607734),f,l,n[g+5],4,-378558),l=S(l,r,s,f,n[g+8],11,-2022574463),f=S(f,l,r,s,n[g+11],16,1839030562),s=S(s,f,l,r,n[g+14],23,-35309556),r=S(r,s,f,l,n[g+1],4,-1530992060),l=S(l,r,s,f,n[g+4],11,1272893353),f=S(f,l,r,s,n[g+7],16,-155497632),s=S(s,f,l,r,n[g+10],23,-1094730640),r=S(r,s,f,l,n[g+13],4,681279174),l=S(l,r,s,f,n[g+0],11,-358537222),f=S(f,l,r,s,n[g+3],16,-722521979),s=S(s,f,l,r,n[g+6],23,76029189),r=S(r,s,f,l,n[g+9],4,-640364487),l=S(l,r,s,f,n[g+12],11,-421815835),f=S(f,l,r,s,n[g+15],16,530742520),r=y(r,s=S(s,f,l,r,n[g+2],23,-995338651),f,l,n[g+0],6,-198630844),l=y(l,r,s,f,n[g+7],10,1126891415),f=y(f,l,r,s,n[g+14],15,-1416354905),s=y(s,f,l,r,n[g+5],21,-57434055),r=y(r,s,f,l,n[g+12],6,1700485571),l=y(l,r,s,f,n[g+3],10,-1894986606),f=y(f,l,r,s,n[g+10],15,-1051523),s=y(s,f,l,r,n[g+1],21,-2054922799),r=y(r,s,f,l,n[g+8],6,1873313359),l=y(l,r,s,f,n[g+15],10,-30611744),f=y(f,l,r,s,n[g+6],15,-1560198380),s=y(s,f,l,r,n[g+13],21,1309151649),r=y(r,s,f,l,n[g+4],6,-145523070),l=y(l,r,s,f,n[g+11],10,-1120210379),f=y(f,l,r,s,n[g+2],15,718787259),s=y(s,f,l,r,n[g+9],21,-343485551),r=r+v>>>0,s=s+T>>>0,f=f+A>>>0,l=l+_>>>0}return i.endian([r,s,f,l])})._ff=function(t,e,n,o,r,s,i){var u=t+(e&n|~e&o)+(r>>>0)+i;return(u<<s|u>>>32-s)+e},h._gg=function(t,e,n,o,r,s,i){var u=t+(e&o|n&~o)+(r>>>0)+i;return(u<<s|u>>>32-s)+e},h._hh=function(t,e,n,o,r,s,i){var u=t+(e^n^o)+(r>>>0)+i;return(u<<s|u>>>32-s)+e},h._ii=function(t,e,n,o,r,s,i){var u=t+(n^(e|~o))+(r>>>0)+i;return(u<<s|u>>>32-s)+e},h._blocksize=16,h._digestsize=16,r.exports=function(t,e){if(null==t)throw new Error("Illegal argument "+t);var n=i.wordsToBytes(h(t,e));return e&&e.asBytes?n:e&&e.asString?c.bytesToString(n):i.bytesToHex(n)};const S=e(r.exports);return class{constructor(t={}){this.url=t.url,this.app_key=t.app_key,this.app_secret=t.app_secret,this.onMessage=t.onMessage||(()=>{}),this.onError=t.onError||(()=>{}),this.onOpen=t.onOpen||(()=>{}),this.onAudioData=t.onAudioData||(()=>{}),this.onAudioNegotiationSuccess=t.onAudioNegotiationSuccess||(()=>{}),this.onConnectSSE=t.onConnectSSE||(()=>{}),this.eventSource=null,this.negotiationResult=null}generateAuthToken(){const t=Math.floor(Date.now()/1e3);return`${this.app_key}:${t}:${S(`${this.app_secret}:${t}`)}`}getAuthHeaders(){return{Authorization:`${this.generateAuthToken()}`,"Content-Type":"application/json"}}async post(t,e){const n=this.generateAuthToken(),o=await fetch(`${this.url}${t}`,{method:"POST",headers:{Authorization:`${n}`,"Content-Type":"application/json","User-Agent":"XY-ASSIST-SDK"},body:JSON.stringify(e)});if(!o.ok){const t=await o.text();throw new Error(`HTTP error! status: ${o.status}, message: ${t}`)}return await o.json()}async audioNegotiation(t){const e=await this.post("/api/v1/agent/ccms/assist/audio-nego",t);return this.negotiationResult=e,this.onAudioNegotiationSuccess&&this.onAudioNegotiationSuccess(e),e}connectSSEWithNegotiationResult(){if(!this.negotiationResult||!this.negotiationResult.result)throw new Error("没有有效的协商结果,请先执行音频协商");const{sse_addr:t,client_id:e}=this.negotiationResult.result;if(!t||!e)throw new Error("协商结果中缺少sse_addr或client_id");const n=`http://${t}/sse?client_id=${e}`;this.connectSSEWithURL(n),this.onConnectSSE&&this.onConnectSSE({sseUrl:n,client_id:e,sse_addr:t})}connectSSEWithURL(e){this.eventSource=new t(e),this.eventSource.onmessage=t=>{const e=JSON.parse(t.data);this.onMessage(e)},this.eventSource.onerror=t=>{this.onError(t)},this.eventSource.onopen=()=>{this.onOpen()}}connectSSE(e=!0){if(e&&this.negotiationResult&&this.negotiationResult.result&&this.negotiationResult.result.sse_addr)this.connectSSEWithNegotiationResult();else{const e=this.generateAuthToken(),n=`${this.url}/api/v1/agent/sse?auth=${e}`;this.eventSource=new t(n),this.eventSource.onmessage=t=>{const e=JSON.parse(t.data);this.onMessage(e)},this.eventSource.onerror=t=>{this.onError(t)},this.eventSource.onopen=()=>{this.onOpen()}}}disconnectSSE(){this.eventSource&&(this.eventSource.close(),this.eventSource=null)}}});
1
+ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).AgentAssistantSDK=e()}(this,function(){"use strict";var t="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};function e(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}var n,o,r={exports:{}};
2
+ /** @license
3
+ * eventsource.js
4
+ * Available under MIT License (MIT)
5
+ * https://github.com/Yaffle/EventSource/
6
+ */n=r,o=r.exports,function(t){var e=t.setTimeout,r=t.clearTimeout,i=t.XMLHttpRequest,s=t.XDomainRequest,a=t.ActiveXObject,u=t.EventSource,c=t.document,l=t.Promise,h=t.fetch,d=t.Response,f=t.TextDecoder,p=t.TextEncoder,y=t.AbortController;if("undefined"==typeof window||void 0===c||"readyState"in c||null!=c.body||(c.readyState="loading",window.addEventListener("load",function(t){c.readyState="complete"},!1)),null==i&&null!=a&&(i=function(){return new a("Microsoft.XMLHTTP")}),null==Object.create&&(Object.create=function(t){function e(){}return e.prototype=t,new e}),Date.now||(Date.now=function(){return(new Date).getTime()}),null==y){var g=h;h=function(t,e){var n=e.signal;return g(t,{headers:e.headers,credentials:e.credentials,cache:e.cache}).then(function(t){var e=t.body.getReader();return n._reader=e,n._aborted&&n._reader.cancel(),{status:t.status,statusText:t.statusText,headers:t.headers,body:{getReader:function(){return e}}}})},y=function(){this.signal={_reader:null,_aborted:!1},this.abort=function(){null!=this.signal._reader&&this.signal._reader.cancel(),this.signal._aborted=!0}}}function v(){this.bitsNeeded=0,this.codePoint=0}v.prototype.decode=function(t){function e(t,e,n){if(1===n)return t>=128>>e&&t<<e<=2047;if(2===n)return t>=2048>>e&&t<<e<=55295||t>=57344>>e&&t<<e<=65535;if(3===n)return t>=65536>>e&&t<<e<=1114111;throw new Error}function n(t,e){if(6===t)return e>>6>15?3:e>31?2:1;if(12===t)return e>15?3:2;if(18===t)return 3;throw new Error}for(var o=65533,r="",i=this.bitsNeeded,s=this.codePoint,a=0;a<t.length;a+=1){var u=t[a];0!==i&&(u<128||u>191||!e(s<<6|63&u,i-6,n(i,s)))&&(i=0,s=o,r+=String.fromCharCode(s)),0===i?(u>=0&&u<=127?(i=0,s=u):u>=192&&u<=223?(i=6,s=31&u):u>=224&&u<=239?(i=12,s=15&u):u>=240&&u<=247?(i=18,s=7&u):(i=0,s=o),0===i||e(s,i,n(i,s))||(i=0,s=o)):(i-=6,s=s<<6|63&u),0===i&&(s<=65535?r+=String.fromCharCode(s):(r+=String.fromCharCode(55296+(s-65535-1>>10)),r+=String.fromCharCode(56320+(s-65535-1&1023))))}return this.bitsNeeded=i,this.codePoint=s,r},null!=f&&null!=p&&function(){try{return"test"===(new f).decode((new p).encode("test"),{stream:!0})}catch(t){console.debug("TextDecoder does not support streaming option. Using polyfill instead: "+t)}return!1}()||(f=v);var S=function(){};function w(t){this.withCredentials=!1,this.readyState=0,this.status=0,this.statusText="",this.responseText="",this.onprogress=S,this.onload=S,this.onerror=S,this.onreadystatechange=S,this._contentType="",this._xhr=t,this._sendTimeout=0,this._abort=S}function T(t){return t.replace(/[A-Z]/g,function(t){return String.fromCharCode(t.charCodeAt(0)+32)})}function b(t){for(var e=Object.create(null),n=t.split("\r\n"),o=0;o<n.length;o+=1){var r=n[o].split(": "),i=r.shift(),s=r.join(": ");e[T(i)]=s}this._map=e}function E(){}function _(t){this._headers=t}function m(){}function C(){this._listeners=Object.create(null)}function A(t){e(function(){throw t},0)}function x(t){this.type=t,this.target=void 0}function R(t,e){x.call(this,t),this.data=e.data,this.lastEventId=e.lastEventId}function O(t,e){x.call(this,t),this.status=e.status,this.statusText=e.statusText,this.headers=e.headers}function N(t,e){x.call(this,t),this.error=e.error}w.prototype.open=function(t,n){this._abort(!0);var o=this,s=this._xhr,a=1,u=0;this._abort=function(t){0!==o._sendTimeout&&(r(o._sendTimeout),o._sendTimeout=0),1!==a&&2!==a&&3!==a||(a=4,s.onload=S,s.onerror=S,s.onabort=S,s.onprogress=S,s.onreadystatechange=S,s.abort(),0!==u&&(r(u),u=0),t||(o.readyState=4,o.onabort(null),o.onreadystatechange())),a=0};var c=function(){if(1===a){var t=0,e="",n=void 0;if("contentType"in s)t=200,e="OK",n=s.contentType;else try{t=s.status,e=s.statusText,n=s.getResponseHeader("Content-Type")}catch(r){t=0,e="",n=void 0}0!==t&&(a=2,o.readyState=2,o.status=t,o.statusText=e,o._contentType=n,o.onreadystatechange())}},l=function(){if(c(),2===a||3===a){a=3;var t="";try{t=s.responseText}catch(e){}o.readyState=3,o.responseText=t,o.onprogress()}},h=function(t,e){if(null!=e&&null!=e.preventDefault||(e={preventDefault:S}),l(),1===a||2===a||3===a){if(a=4,0!==u&&(r(u),u=0),o.readyState=4,"load"===t)o.onload(e);else if("error"===t)o.onerror(e);else{if("abort"!==t)throw new TypeError;o.onabort(e)}o.onreadystatechange()}},d=function(){u=e(function(){d()},500),3===s.readyState&&l()};"onload"in s&&(s.onload=function(t){h("load",t)}),"onerror"in s&&(s.onerror=function(t){h("error",t)}),"onabort"in s&&(s.onabort=function(t){h("abort",t)}),"onprogress"in s&&(s.onprogress=l),"onreadystatechange"in s&&(s.onreadystatechange=function(t){!function(t){null!=s&&(4===s.readyState?"onload"in s&&"onerror"in s&&"onabort"in s||h(""===s.responseText?"error":"load",t):3===s.readyState?"onprogress"in s||l():2===s.readyState&&c())}(t)}),!("contentType"in s)&&"ontimeout"in i.prototype||(n+=(-1===n.indexOf("?")?"?":"&")+"padding=true"),s.open(t,n,!0),"readyState"in s&&(u=e(function(){d()},0))},w.prototype.abort=function(){this._abort(!1)},w.prototype.getResponseHeader=function(t){return this._contentType},w.prototype.setRequestHeader=function(t,e){var n=this._xhr;"setRequestHeader"in n&&n.setRequestHeader(t,e)},w.prototype.getAllResponseHeaders=function(){return null!=this._xhr.getAllResponseHeaders&&this._xhr.getAllResponseHeaders()||""},w.prototype.send=function(){if("ontimeout"in i.prototype&&("sendAsBinary"in i.prototype||"mozAnon"in i.prototype)||null==c||null==c.readyState||"complete"===c.readyState){var t=this._xhr;"withCredentials"in t&&(t.withCredentials=this.withCredentials);try{t.send(void 0)}catch(o){throw o}}else{var n=this;n._sendTimeout=e(function(){n._sendTimeout=0,n.send()},4)}},b.prototype.get=function(t){return this._map[T(t)]},null!=i&&null==i.HEADERS_RECEIVED&&(i.HEADERS_RECEIVED=2),E.prototype.open=function(t,e,n,o,r,s,a){t.open("GET",r);var u=0;for(var c in t.onprogress=function(){var e=t.responseText.slice(u);u+=e.length,n(e)},t.onerror=function(t){t.preventDefault(),o(new Error("NetworkError"))},t.onload=function(){o(null)},t.onabort=function(){o(null)},t.onreadystatechange=function(){if(t.readyState===i.HEADERS_RECEIVED){var n=t.status,o=t.statusText,r=t.getResponseHeader("Content-Type"),s=t.getAllResponseHeaders();e(n,o,r,new b(s))}},t.withCredentials=s,a)Object.prototype.hasOwnProperty.call(a,c)&&t.setRequestHeader(c,a[c]);return t.send(),t},_.prototype.get=function(t){return this._headers.get(t)},m.prototype.open=function(t,e,n,o,r,i,s){var a=null,u=new y,c=u.signal,d=new f;return h(r,{headers:s,credentials:i?"include":"same-origin",signal:c,cache:"no-store"}).then(function(t){return a=t.body.getReader(),e(t.status,t.statusText,t.headers.get("Content-Type"),new _(t.headers)),new l(function(t,e){var o=function(){a.read().then(function(e){if(e.done)t(void 0);else{var r=d.decode(e.value,{stream:!0});n(r),o()}}).catch(function(t){e(t)})};o()})}).catch(function(t){return"AbortError"===t.name?void 0:t}).then(function(t){o(t)}),{abort:function(){null!=a&&a.cancel(),u.abort()}}},C.prototype.dispatchEvent=function(t){t.target=this;var e=this._listeners[t.type];if(null!=e)for(var n=e.length,o=0;o<n;o+=1){var r=e[o];try{"function"==typeof r.handleEvent?r.handleEvent(t):r.call(this,t)}catch(i){A(i)}}},C.prototype.addEventListener=function(t,e){t=String(t);var n=this._listeners,o=n[t];null==o&&(o=[],n[t]=o);for(var r=!1,i=0;i<o.length;i+=1)o[i]===e&&(r=!0);r||o.push(e)},C.prototype.removeEventListener=function(t,e){t=String(t);var n=this._listeners,o=n[t];if(null!=o){for(var r=[],i=0;i<o.length;i+=1)o[i]!==e&&r.push(o[i]);0===r.length?delete n[t]:n[t]=r}},R.prototype=Object.create(x.prototype),O.prototype=Object.create(x.prototype),N.prototype=Object.create(x.prototype);var D=-1,j=-1,H=/^text\/event\-stream(;.*)?$/i,B=function(t,e){var n=null==t?e:parseInt(t,10);return n!=n&&(n=e),I(n)},I=function(t){return Math.min(Math.max(t,1e3),18e6)},M=function(t,e,n){try{"function"==typeof e&&e.call(t,n)}catch(o){A(o)}};function $(t,n){C.call(this),n=n||{},this.onopen=void 0,this.onmessage=void 0,this.onerror=void 0,this.url=void 0,this.readyState=void 0,this.withCredentials=void 0,this.headers=void 0,this._close=void 0,function(t,n,o){n=String(n);var a=Boolean(o.withCredentials),u=o.lastEventIdQueryParameterName||"lastEventId",c=I(1e3),l=B(o.heartbeatTimeout,45e3),h="",d=c,f=!1,p=0,y=o.headers||{},g=o.Transport,v=P&&null==g?void 0:new w(null!=g?new g:null!=i&&"withCredentials"in i.prototype||null==s?new i:new s),S=null!=g&&"string"!=typeof g?new g:null==v?new m:new E,T=void 0,b=0,_=D,C="",A="",x="",$="",k=0,L=0,U=0,q=function(e,n,o,r){if(0===_)if(200===e&&null!=o&&H.test(o)){_=1,f=Date.now(),d=c,t.readyState=1;var i=new O("open",{status:e,statusText:n,headers:r});t.dispatchEvent(i),M(t,t.onopen,i)}else{var s="";200!==e?(n&&(n=n.replace(/\s+/g," ")),s="EventSource's response has a status "+e+" "+n+" that is not 200. Aborting the connection."):s="EventSource's response has a Content-Type specifying an unsupported type: "+(null==o?"-":o.replace(/\s+/g," "))+". Aborting the connection.",X(),i=new O("error",{status:e,statusText:n,headers:r}),t.dispatchEvent(i),M(t,t.onerror,i),console.error(s)}},W=function(n){if(1===_){for(var o=-1,i=0;i<n.length;i+=1)(u=n.charCodeAt(i))!=="\n".charCodeAt(0)&&u!=="\r".charCodeAt(0)||(o=i);var s=(-1!==o?$:"")+n.slice(0,o+1);$=(-1===o?$:"")+n.slice(o+1),""!==n&&(f=Date.now(),p+=n.length);for(var a=0;a<s.length;a+=1){var u=s.charCodeAt(a);if(k===j&&u==="\n".charCodeAt(0))k=0;else if(k===j&&(k=0),u==="\r".charCodeAt(0)||u==="\n".charCodeAt(0)){if(0!==k){1===k&&(U=a+1);var y=s.slice(L,U-1),g=s.slice(U+(U<a&&s.charCodeAt(U)===" ".charCodeAt(0)?1:0),a);"data"===y?(C+="\n",C+=g):"id"===y?A=g:"event"===y?x=g:"retry"===y?(c=B(g,c),d=c):"heartbeatTimeout"===y&&(l=B(g,l),0!==b&&(r(b),b=e(function(){G()},l)))}if(0===k){if(""!==C){h=A,""===x&&(x="message");var v=new R(x,{data:C.slice(1),lastEventId:A});if(t.dispatchEvent(v),"open"===x?M(t,t.onopen,v):"message"===x?M(t,t.onmessage,v):"error"===x&&M(t,t.onerror,v),2===_)return}C="",x=""}k=u==="\r".charCodeAt(0)?j:0}else 0===k&&(L=a,k=1),1===k?u===":".charCodeAt(0)&&(U=a+1,k=2):2===k&&(k=3)}}},z=function(n){if(1===_||0===_){_=D,0!==b&&(r(b),b=0),b=e(function(){G()},d),d=I(Math.min(16*c,2*d)),t.readyState=0;var o=new N("error",{error:n});t.dispatchEvent(o),M(t,t.onerror,o),null!=n&&console.error(n)}},X=function(){_=2,null!=T&&(T.abort(),T=void 0),0!==b&&(r(b),b=0),t.readyState=2},G=function(){if(b=0,_===D){f=!1,p=0,b=e(function(){G()},l),_=0,C="",x="",A=h,$="",L=0,U=0,k=0;var o=n;if("data:"!==n.slice(0,5)&&"blob:"!==n.slice(0,5)&&""!==h){var r=n.indexOf("?");o=-1===r?n:n.slice(0,r+1)+n.slice(r+1).replace(/(?:^|&)([^=&]*)(?:=[^&]*)?/g,function(t,e){return e===u?"":t}),o+=(-1===n.indexOf("?")?"?":"&")+u+"="+encodeURIComponent(h)}var i=t.withCredentials,s={Accept:"text/event-stream"},a=t.headers;if(null!=a)for(var c in a)Object.prototype.hasOwnProperty.call(a,c)&&(s[c]=a[c]);try{T=S.open(v,q,W,z,o,i,s)}catch(y){throw X(),y}}else if(f||null==T){var d=Math.max((f||Date.now())+l-Date.now(),1);f=!1,b=e(function(){G()},d)}else z(new Error("No activity within "+l+" milliseconds. "+(0===_?"No response received.":p+" chars received.")+" Reconnecting.")),null!=T&&(T.abort(),T=void 0)};t.url=n,t.readyState=0,t.withCredentials=a,t.headers=y,t._close=X,G()}(this,t,n)}var P=null!=h&&null!=d&&"body"in d.prototype;$.prototype=Object.create(C.prototype),$.prototype.CONNECTING=0,$.prototype.OPEN=1,$.prototype.CLOSED=2,$.prototype.close=function(){this._close()},$.CONNECTING=0,$.OPEN=1,$.CLOSED=2,$.prototype.withCredentials=void 0;var k,L,U=u;null==i||null!=u&&"withCredentials"in u.prototype||(U=$),(k=o).EventSourcePolyfill=$,k.NativeEventSource=u,void 0!==(L=void(k.EventSource=U))&&(n.exports=L)}("undefined"==typeof globalThis?"undefined"!=typeof window?window:"undefined"!=typeof self?self:t:globalThis);var i,s,a={exports:{}},u={exports:{}};i="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",s={rotl:function(t,e){return t<<e|t>>>32-e},rotr:function(t,e){return t<<32-e|t>>>e},endian:function(t){if(t.constructor==Number)return 16711935&s.rotl(t,8)|4278255360&s.rotl(t,24);for(var e=0;e<t.length;e++)t[e]=s.endian(t[e]);return t},randomBytes:function(t){for(var e=[];t>0;t--)e.push(Math.floor(256*Math.random()));return e},bytesToWords:function(t){for(var e=[],n=0,o=0;n<t.length;n++,o+=8)e[o>>>5]|=t[n]<<24-o%32;return e},wordsToBytes:function(t){for(var e=[],n=0;n<32*t.length;n+=8)e.push(t[n>>>5]>>>24-n%32&255);return e},bytesToHex:function(t){for(var e=[],n=0;n<t.length;n++)e.push((t[n]>>>4).toString(16)),e.push((15&t[n]).toString(16));return e.join("")},hexToBytes:function(t){for(var e=[],n=0;n<t.length;n+=2)e.push(parseInt(t.substr(n,2),16));return e},bytesToBase64:function(t){for(var e=[],n=0;n<t.length;n+=3)for(var o=t[n]<<16|t[n+1]<<8|t[n+2],r=0;r<4;r++)8*n+6*r<=8*t.length?e.push(i.charAt(o>>>6*(3-r)&63)):e.push("=");return e.join("")},base64ToBytes:function(t){t=t.replace(/[^A-Z0-9+\/]/gi,"");for(var e=[],n=0,o=0;n<t.length;o=++n%4)0!=o&&e.push((i.indexOf(t.charAt(n-1))&Math.pow(2,-2*o+8)-1)<<2*o|i.indexOf(t.charAt(n))>>>6-2*o);return e}},u.exports=s;var c,l,h,d,f,p=u.exports,y={utf8:{stringToBytes:function(t){return y.bin.stringToBytes(unescape(encodeURIComponent(t)))},bytesToString:function(t){return decodeURIComponent(escape(y.bin.bytesToString(t)))}},bin:{stringToBytes:function(t){for(var e=[],n=0;n<t.length;n++)e.push(255&t.charCodeAt(n));return e},bytesToString:function(t){for(var e=[],n=0;n<t.length;n++)e.push(String.fromCharCode(t[n]));return e.join("")}}},g=y,v=function(t){return null!=t&&(S(t)||function(t){return"function"==typeof t.readFloatLE&&"function"==typeof t.slice&&S(t.slice(0,0))}(t)||!!t._isBuffer)};function S(t){return!!t.constructor&&"function"==typeof t.constructor.isBuffer&&t.constructor.isBuffer(t)}c=p,l=g.utf8,h=v,d=g.bin,(f=function(t,e){t.constructor==String?t=e&&"binary"===e.encoding?d.stringToBytes(t):l.stringToBytes(t):h(t)?t=Array.prototype.slice.call(t,0):Array.isArray(t)||t.constructor===Uint8Array||(t=t.toString());for(var n=c.bytesToWords(t),o=8*t.length,r=1732584193,i=-271733879,s=-1732584194,a=271733878,u=0;u<n.length;u++)n[u]=16711935&(n[u]<<8|n[u]>>>24)|4278255360&(n[u]<<24|n[u]>>>8);n[o>>>5]|=128<<o%32,n[14+(o+64>>>9<<4)]=o;var p=f._ff,y=f._gg,g=f._hh,v=f._ii;for(u=0;u<n.length;u+=16){var S=r,w=i,T=s,b=a;r=p(r,i,s,a,n[u+0],7,-680876936),a=p(a,r,i,s,n[u+1],12,-389564586),s=p(s,a,r,i,n[u+2],17,606105819),i=p(i,s,a,r,n[u+3],22,-1044525330),r=p(r,i,s,a,n[u+4],7,-176418897),a=p(a,r,i,s,n[u+5],12,1200080426),s=p(s,a,r,i,n[u+6],17,-1473231341),i=p(i,s,a,r,n[u+7],22,-45705983),r=p(r,i,s,a,n[u+8],7,1770035416),a=p(a,r,i,s,n[u+9],12,-1958414417),s=p(s,a,r,i,n[u+10],17,-42063),i=p(i,s,a,r,n[u+11],22,-1990404162),r=p(r,i,s,a,n[u+12],7,1804603682),a=p(a,r,i,s,n[u+13],12,-40341101),s=p(s,a,r,i,n[u+14],17,-1502002290),r=y(r,i=p(i,s,a,r,n[u+15],22,1236535329),s,a,n[u+1],5,-165796510),a=y(a,r,i,s,n[u+6],9,-1069501632),s=y(s,a,r,i,n[u+11],14,643717713),i=y(i,s,a,r,n[u+0],20,-373897302),r=y(r,i,s,a,n[u+5],5,-701558691),a=y(a,r,i,s,n[u+10],9,38016083),s=y(s,a,r,i,n[u+15],14,-660478335),i=y(i,s,a,r,n[u+4],20,-405537848),r=y(r,i,s,a,n[u+9],5,568446438),a=y(a,r,i,s,n[u+14],9,-1019803690),s=y(s,a,r,i,n[u+3],14,-187363961),i=y(i,s,a,r,n[u+8],20,1163531501),r=y(r,i,s,a,n[u+13],5,-1444681467),a=y(a,r,i,s,n[u+2],9,-51403784),s=y(s,a,r,i,n[u+7],14,1735328473),r=g(r,i=y(i,s,a,r,n[u+12],20,-1926607734),s,a,n[u+5],4,-378558),a=g(a,r,i,s,n[u+8],11,-2022574463),s=g(s,a,r,i,n[u+11],16,1839030562),i=g(i,s,a,r,n[u+14],23,-35309556),r=g(r,i,s,a,n[u+1],4,-1530992060),a=g(a,r,i,s,n[u+4],11,1272893353),s=g(s,a,r,i,n[u+7],16,-155497632),i=g(i,s,a,r,n[u+10],23,-1094730640),r=g(r,i,s,a,n[u+13],4,681279174),a=g(a,r,i,s,n[u+0],11,-358537222),s=g(s,a,r,i,n[u+3],16,-722521979),i=g(i,s,a,r,n[u+6],23,76029189),r=g(r,i,s,a,n[u+9],4,-640364487),a=g(a,r,i,s,n[u+12],11,-421815835),s=g(s,a,r,i,n[u+15],16,530742520),r=v(r,i=g(i,s,a,r,n[u+2],23,-995338651),s,a,n[u+0],6,-198630844),a=v(a,r,i,s,n[u+7],10,1126891415),s=v(s,a,r,i,n[u+14],15,-1416354905),i=v(i,s,a,r,n[u+5],21,-57434055),r=v(r,i,s,a,n[u+12],6,1700485571),a=v(a,r,i,s,n[u+3],10,-1894986606),s=v(s,a,r,i,n[u+10],15,-1051523),i=v(i,s,a,r,n[u+1],21,-2054922799),r=v(r,i,s,a,n[u+8],6,1873313359),a=v(a,r,i,s,n[u+15],10,-30611744),s=v(s,a,r,i,n[u+6],15,-1560198380),i=v(i,s,a,r,n[u+13],21,1309151649),r=v(r,i,s,a,n[u+4],6,-145523070),a=v(a,r,i,s,n[u+11],10,-1120210379),s=v(s,a,r,i,n[u+2],15,718787259),i=v(i,s,a,r,n[u+9],21,-343485551),r=r+S>>>0,i=i+w>>>0,s=s+T>>>0,a=a+b>>>0}return c.endian([r,i,s,a])})._ff=function(t,e,n,o,r,i,s){var a=t+(e&n|~e&o)+(r>>>0)+s;return(a<<i|a>>>32-i)+e},f._gg=function(t,e,n,o,r,i,s){var a=t+(e&o|n&~o)+(r>>>0)+s;return(a<<i|a>>>32-i)+e},f._hh=function(t,e,n,o,r,i,s){var a=t+(e^n^o)+(r>>>0)+s;return(a<<i|a>>>32-i)+e},f._ii=function(t,e,n,o,r,i,s){var a=t+(n^(e|~o))+(r>>>0)+s;return(a<<i|a>>>32-i)+e},f._blocksize=16,f._digestsize=16,a.exports=function(t,e){if(null==t)throw new Error("Illegal argument "+t);var n=c.wordsToBytes(f(t,e));return e&&e.asBytes?n:e&&e.asString?d.bytesToString(n):c.bytesToHex(n)};const w=e(a.exports);return class{constructor(t={}){this.url=t.url,this.app_key=t.app_key,this.app_secret=t.app_secret,this.onMessage=t.onMessage||(()=>{}),this.onError=t.onError||(()=>{}),this.onOpen=t.onOpen||(()=>{}),this.onAudioData=t.onAudioData||(()=>{}),this.onAudioNegotiationSuccess=t.onAudioNegotiationSuccess||(()=>{}),this.onConnectSSE=t.onConnectSSE||(()=>{}),this.eventSource=null,this.negotiationResult=null}generateAuthToken(){const t=Math.floor(Date.now()/1e3);return`${this.app_key}:${t}:${w(`${this.app_secret}:${t}`)}`}getAuthHeaders(){return{Authorization:`${this.generateAuthToken()}`,"Content-Type":"application/json"}}async post(t,e){const n=this.generateAuthToken(),o=await fetch(`${this.url}${t}`,{method:"POST",headers:{Authorization:`${n}`,"Content-Type":"application/json","User-Agent":"XY-ASSIST-SDK"},body:JSON.stringify(e)});if(!o.ok){const t=await o.text();throw new Error(`HTTP error! status: ${o.status}, message: ${t}`)}return await o.json()}async audioNegotiation(t){const e=await this.post("/api/v1/agent/ccms/assist/audio-nego",t);return this.negotiationResult=e,this.onAudioNegotiationSuccess&&this.onAudioNegotiationSuccess(e),e}connectSSEWithNegotiationResult(){if(!this.negotiationResult||!this.negotiationResult.result)throw new Error("没有有效的协商结果,请先执行音频协商");const{sse_addr:t,client_id:e}=this.negotiationResult.result;if(!t||!e)throw new Error("协商结果中缺少sse_addr或client_id");const n=`http://${t}/sse?client_id=${e}`;this.connectSSEWithURL(n),this.onConnectSSE&&this.onConnectSSE({sseUrl:n,client_id:e,sse_addr:t})}connectSSEWithURL(t){this.eventSource=new EventSource(t),this.eventSource.onmessage=t=>{const e=JSON.parse(t.data);this.onMessage(e)},this.eventSource.onerror=t=>{this.onError(t)},this.eventSource.onopen=()=>{this.onOpen()}}connectSSE(t=!0){if(t&&this.negotiationResult&&this.negotiationResult.result&&this.negotiationResult.result.sse_addr)this.connectSSEWithNegotiationResult();else{const t=this.generateAuthToken(),e=`${this.url}/api/v1/agent/sse?auth=${t}`;this.eventSource=new EventSource(e),this.eventSource.onmessage=t=>{const e=JSON.parse(t.data);this.onMessage(e)},this.eventSource.onerror=t=>{this.onError(t)},this.eventSource.onopen=()=>{this.onOpen()}}}disconnectSSE(){this.eventSource&&(this.eventSource.close(),this.eventSource=null)}}});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xinyao-assistant-sdk",
3
- "version": "1.0.1",
3
+ "version": "1.0.11",
4
4
  "type": "module",
5
5
  "description": "坐席辅助SDK - 提供音频协商和SSE连接功能",
6
6
  "main": "./dist/xinyao-assistant-sdk.cjs.js",
@@ -1,4 +1,4 @@
1
- import EventSource from 'event-source-polyfill';
1
+ import { EventSourcePolyfill } from 'event-source-polyfill';
2
2
  import md5 from 'md5';
3
3
 
4
4
  class AgentAssistantSDK {