@anam-ai/js-sdk 1.1.0-alpha.1 → 1.1.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/main/AnamClient.d.ts +2 -2
- package/dist/main/AnamClient.d.ts.map +1 -1
- package/dist/main/AnamClient.js +8 -8
- package/dist/main/AnamClient.js.map +1 -1
- package/dist/module/AnamClient.d.ts +2 -2
- package/dist/module/AnamClient.d.ts.map +1 -1
- package/dist/module/AnamClient.js +8 -8
- package/dist/module/AnamClient.js.map +1 -1
- package/dist/umd/anam.js +1 -1
- package/package.json +1 -1
|
@@ -11,8 +11,8 @@ export default class AnamClient {
|
|
|
11
11
|
constructor(sessionToken: string | undefined, personaConfig: PersonaConfig, options?: AnamClientOptions);
|
|
12
12
|
private startSession;
|
|
13
13
|
private startSessionIfNeeded;
|
|
14
|
-
stream(callbacks?: ConnectionCallbacks,
|
|
15
|
-
streamToVideoAndAudioElements(videoElementId: string, audioElementId: string, callbacks?: ConnectionCallbacks,
|
|
14
|
+
stream(callbacks?: ConnectionCallbacks, userProvidedAudioStream?: MediaStream): Promise<MediaStream[]>;
|
|
15
|
+
streamToVideoAndAudioElements(videoElementId: string, audioElementId: string, callbacks?: ConnectionCallbacks, userProvidedMediaStream?: MediaStream): Promise<void>;
|
|
16
16
|
talk(content: string): Promise<void>;
|
|
17
17
|
sendDataMessage(message: string): void;
|
|
18
18
|
stopStreaming(): Promise<void>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AnamClient.d.ts","sourceRoot":"","sources":["../../src/AnamClient.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,mBAAmB,EACnB,aAAa,EAEd,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AAE9D,MAAM,CAAC,OAAO,OAAO,UAAU;IAC7B,SAAS,CAAC,YAAY,EAAE,MAAM,GAAG,SAAS,CAAC;IAC3C,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AE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAe1C,eAAe,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAQhC,aAAa,IAAI,OAAO,CAAC,IAAI,CAAC;IASpC,WAAW,IAAI,OAAO;IAItB,gBAAgB,CAAC,aAAa,EAAE,aAAa,GAAG,IAAI;IAIpD,gBAAgB,IAAI,aAAa,GAAG,SAAS;CAGrD"}
|
package/dist/main/AnamClient.js
CHANGED
|
@@ -24,10 +24,10 @@ class AnamClient {
|
|
|
24
24
|
this.personaConfig = personaConfig;
|
|
25
25
|
this.apiClient = new CoreApiRestClient_1.CoreApiRestClient(sessionToken, options === null || options === void 0 ? void 0 : options.apiKey, options === null || options === void 0 ? void 0 : options.api);
|
|
26
26
|
}
|
|
27
|
-
startSession(
|
|
27
|
+
startSession(userProvidedAudioStream) {
|
|
28
28
|
return __awaiter(this, void 0, void 0, function* () {
|
|
29
29
|
try {
|
|
30
|
-
const config =
|
|
30
|
+
const config = this.personaConfig;
|
|
31
31
|
if (!config) {
|
|
32
32
|
throw new Error('A default persona configuration has not been set and no persona configuration was provided');
|
|
33
33
|
}
|
|
@@ -59,12 +59,12 @@ class AnamClient {
|
|
|
59
59
|
}
|
|
60
60
|
});
|
|
61
61
|
}
|
|
62
|
-
startSessionIfNeeded(
|
|
62
|
+
startSessionIfNeeded(userProvidedMediaStream) {
|
|
63
63
|
return __awaiter(this, void 0, void 0, function* () {
|
|
64
64
|
if (!this.sessionId || !this.streamingClient) {
|
|
65
65
|
console.warn('StreamToVideoAndAudioElements: session is not started. starting a new session');
|
|
66
66
|
try {
|
|
67
|
-
yield this.startSession(
|
|
67
|
+
yield this.startSession(userProvidedMediaStream);
|
|
68
68
|
}
|
|
69
69
|
catch (error) {
|
|
70
70
|
throw new Error('StreamToVideoAndAudioElements: Failed to start session');
|
|
@@ -76,8 +76,8 @@ class AnamClient {
|
|
|
76
76
|
});
|
|
77
77
|
}
|
|
78
78
|
stream() {
|
|
79
|
-
return __awaiter(this, arguments, void 0, function* (callbacks = {},
|
|
80
|
-
yield this.startSessionIfNeeded(
|
|
79
|
+
return __awaiter(this, arguments, void 0, function* (callbacks = {}, userProvidedAudioStream) {
|
|
80
|
+
yield this.startSessionIfNeeded(userProvidedAudioStream);
|
|
81
81
|
if (this._isStreaming) {
|
|
82
82
|
throw new Error('Already streaming');
|
|
83
83
|
}
|
|
@@ -108,8 +108,8 @@ class AnamClient {
|
|
|
108
108
|
});
|
|
109
109
|
}
|
|
110
110
|
streamToVideoAndAudioElements(videoElementId_1, audioElementId_1) {
|
|
111
|
-
return __awaiter(this, arguments, void 0, function* (videoElementId, audioElementId, callbacks = {},
|
|
112
|
-
yield this.startSessionIfNeeded(
|
|
111
|
+
return __awaiter(this, arguments, void 0, function* (videoElementId, audioElementId, callbacks = {}, userProvidedMediaStream) {
|
|
112
|
+
yield this.startSessionIfNeeded(userProvidedMediaStream);
|
|
113
113
|
if (this._isStreaming) {
|
|
114
114
|
throw new Error('Already streaming');
|
|
115
115
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AnamClient.js","sourceRoot":"","sources":["../../src/AnamClient.ts"],"names":[],"mappings":";;;;;;;;;;;AAAA,mEAAgE;AAChE,+DAA4D;AAQ5D,MAAqB,UAAU;IAU7B,YACE,YAAgC,EAChC,aAA4B,EAC5B,OAA2B;QARrB,cAAS,GAAkB,IAAI,CAAC;QAChC,oBAAe,GAA2B,IAAI,CAAC;QAE/C,iBAAY,GAAG,KAAK,CAAC;QAO3B,IAAI,CAAC,YAAY,IAAI,CAAC,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,MAAM,CAAA,EAAE,CAAC;YACtC,MAAM,IAAI,KAAK,CAAC,gDAAgD,CAAC,CAAC;QACpE,CAAC;QACD,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;QACjC,IAAI,CAAC,MAAM,GAAG,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,MAAM,CAAC;QAC9B,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CAAC,SAAS,GAAG,IAAI,CAAC;gBACtB,IAAI,CAAC,YAAY,GAAG,KAAK,CAAC;YAC5B,CAAC;QACH,CAAC;KAAA;IAEM,WAAW;QAChB,OAAO,IAAI,CAAC,YAAY,CAAC;IAC3B,CAAC;IAEM,gBAAgB,CAAC,aAA4B;QAClD,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;IACrC,CAAC;IAEM,gBAAgB;QACrB,OAAO,IAAI,CAAC,aAAa,CAAC;IAC5B,CAAC;CACF;AAxMD,6BAwMC"}
|
|
@@ -11,8 +11,8 @@ export default class AnamClient {
|
|
|
11
11
|
constructor(sessionToken: string | undefined, personaConfig: PersonaConfig, options?: AnamClientOptions);
|
|
12
12
|
private startSession;
|
|
13
13
|
private startSessionIfNeeded;
|
|
14
|
-
stream(callbacks?: ConnectionCallbacks,
|
|
15
|
-
streamToVideoAndAudioElements(videoElementId: string, audioElementId: string, callbacks?: ConnectionCallbacks,
|
|
14
|
+
stream(callbacks?: ConnectionCallbacks, userProvidedAudioStream?: MediaStream): Promise<MediaStream[]>;
|
|
15
|
+
streamToVideoAndAudioElements(videoElementId: string, audioElementId: string, callbacks?: ConnectionCallbacks, userProvidedMediaStream?: MediaStream): Promise<void>;
|
|
16
16
|
talk(content: string): Promise<void>;
|
|
17
17
|
sendDataMessage(message: string): void;
|
|
18
18
|
stopStreaming(): Promise<void>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AnamClient.d.ts","sourceRoot":"","sources":["../../src/AnamClient.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,mBAAmB,EACnB,aAAa,EAEd,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AAE9D,MAAM,CAAC,OAAO,OAAO,UAAU;IAC7B,SAAS,CAAC,YAAY,EAAE,MAAM,GAAG,SAAS,CAAC;IAC3C,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC;IAErC,OAA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,OAAO,EAAE,MAAM,GAAG,IAAI;IAQhC,aAAa,IAAI,OAAO,CAAC,IAAI,CAAC;IASpC,WAAW,IAAI,OAAO;IAItB,gBAAgB,CAAC,aAAa,EAAE,aAAa,GAAG,IAAI;IAIpD,gBAAgB,IAAI,aAAa,GAAG,SAAS;CAGrD"}
|
|
@@ -22,10 +22,10 @@ export default class AnamClient {
|
|
|
22
22
|
this.personaConfig = personaConfig;
|
|
23
23
|
this.apiClient = new CoreApiRestClient(sessionToken, options === null || options === void 0 ? void 0 : options.apiKey, options === null || options === void 0 ? void 0 : options.api);
|
|
24
24
|
}
|
|
25
|
-
startSession(
|
|
25
|
+
startSession(userProvidedAudioStream) {
|
|
26
26
|
return __awaiter(this, void 0, void 0, function* () {
|
|
27
27
|
try {
|
|
28
|
-
const config =
|
|
28
|
+
const config = this.personaConfig;
|
|
29
29
|
if (!config) {
|
|
30
30
|
throw new Error('A default persona configuration has not been set and no persona configuration was provided');
|
|
31
31
|
}
|
|
@@ -57,12 +57,12 @@ export default class AnamClient {
|
|
|
57
57
|
}
|
|
58
58
|
});
|
|
59
59
|
}
|
|
60
|
-
startSessionIfNeeded(
|
|
60
|
+
startSessionIfNeeded(userProvidedMediaStream) {
|
|
61
61
|
return __awaiter(this, void 0, void 0, function* () {
|
|
62
62
|
if (!this.sessionId || !this.streamingClient) {
|
|
63
63
|
console.warn('StreamToVideoAndAudioElements: session is not started. starting a new session');
|
|
64
64
|
try {
|
|
65
|
-
yield this.startSession(
|
|
65
|
+
yield this.startSession(userProvidedMediaStream);
|
|
66
66
|
}
|
|
67
67
|
catch (error) {
|
|
68
68
|
throw new Error('StreamToVideoAndAudioElements: Failed to start session');
|
|
@@ -74,8 +74,8 @@ export default class AnamClient {
|
|
|
74
74
|
});
|
|
75
75
|
}
|
|
76
76
|
stream() {
|
|
77
|
-
return __awaiter(this, arguments, void 0, function* (callbacks = {},
|
|
78
|
-
yield this.startSessionIfNeeded(
|
|
77
|
+
return __awaiter(this, arguments, void 0, function* (callbacks = {}, userProvidedAudioStream) {
|
|
78
|
+
yield this.startSessionIfNeeded(userProvidedAudioStream);
|
|
79
79
|
if (this._isStreaming) {
|
|
80
80
|
throw new Error('Already streaming');
|
|
81
81
|
}
|
|
@@ -106,8 +106,8 @@ export default class AnamClient {
|
|
|
106
106
|
});
|
|
107
107
|
}
|
|
108
108
|
streamToVideoAndAudioElements(videoElementId_1, audioElementId_1) {
|
|
109
|
-
return __awaiter(this, arguments, void 0, function* (videoElementId, audioElementId, callbacks = {},
|
|
110
|
-
yield this.startSessionIfNeeded(
|
|
109
|
+
return __awaiter(this, arguments, void 0, function* (videoElementId, audioElementId, callbacks = {}, userProvidedMediaStream) {
|
|
110
|
+
yield this.startSessionIfNeeded(userProvidedMediaStream);
|
|
111
111
|
if (this._isStreaming) {
|
|
112
112
|
throw new Error('Already streaming');
|
|
113
113
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AnamClient.js","sourceRoot":"","sources":["../../src/AnamClient.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,6BAA6B,CAAC;AAChE,OAAO,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC;AAQ5D,MAAM,CAAC,OAAO,OAAO,UAAU;IAU7B,YACE,YAAgC,EAChC,aAA4B,EAC5B,OAA2B;QARrB,cAAS,GAAkB,IAAI,CAAC;QAChC,oBAAe,GAA2B,IAAI,CAAC;QAE/C,iBAAY,GAAG,KAAK,CAAC;QAO3B,IAAI,CAAC,YAAY,IAAI,CAAC,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,MAAM,CAAA,EAAE,CAAC;YACtC,MAAM,IAAI,KAAK,CAAC,gDAAgD,CAAC,CAAC;QACpE,CAAC;QACD,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;QACjC,IAAI,CAAC,MAAM,GAAG,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,MAAM,CAAC;QAC9B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,eAAe,CAAC,cAAc,EAAE,CAAC;gBACtC,IAAI,CAAC,eAAe,GAAG,IAAI,CAAC;gBAC5B,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;gBACtB,IAAI,CAAC,YAAY,GAAG,KAAK,CAAC;YAC5B,CAAC;QACH,CAAC;KAAA;IAEM,WAAW;QAChB,OAAO,IAAI,CAAC,YAAY,CAAC;IAC3B,CAAC;IAEM,gBAAgB,CAAC,aAA4B;QAClD,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;IACrC,CAAC;IAEM,gBAAgB;QACrB,OAAO,IAAI,CAAC,aAAa,CAAC;IAC5B,CAAC;CACF"}
|
package/dist/umd/anam.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.anam=t():e.anam=t()}(self,(()=>(()=>{"use strict";var e={816:function(e,t,n){var i=this&&this.__awaiter||function(e,t,n,i){return new(n||(n=Promise))((function(o,s){function a(e){try{l(i.next(e))}catch(e){s(e)}}function r(e){try{l(i.throw(e))}catch(e){s(e)}}function l(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(a,r)}l((i=i.apply(e,t||[])).next())}))};Object.defineProperty(t,"__esModule",{value:!0});const o=n(18),s=n(577);t.default=class{constructor(e,t,n){if(this.sessionId=null,this.streamingClient=null,this._isStreaming=!1,!e&&!(null==n?void 0:n.apiKey))throw new Error("Either sessionToken or apiKey must be provided");this.sessionToken=e,this.apiKey=null==n?void 0:n.apiKey,this.personaConfig=t,this.apiClient=new o.CoreApiRestClient(e,null==n?void 0:n.apiKey,null==n?void 0:n.api)}startSession(e,t){return i(this,void 0,void 0,(function*(){try{const n=e||this.personaConfig;if(!n)throw new Error("A default persona configuration has not been set and no persona configuration was provided");const i=yield this.apiClient.startSession(n),{sessionId:o,clientConfig:a,engineHost:r,engineProtocol:l,signallingEndpoint:c}=i,{heartbeatIntervalSeconds:d,maxWsReconnectionAttempts:h,iceServers:u}=a;return this.streamingClient=new s.StreamingClient(o,{engine:{baseUrl:`${l}://${r}`},signalling:{heartbeatIntervalSeconds:d,maxWsReconnectionAttempts:h,url:{baseUrl:r,protocol:l,signallingPath:c}},iceServers:u,userProvidedMediaStream:t}),this.sessionId=o,o}catch(e){throw new Error("Failed to start session")}}))}startSessionIfNeeded(e,t){return i(this,void 0,void 0,(function*(){if(!this.sessionId||!this.streamingClient){console.warn("StreamToVideoAndAudioElements: session is not started. starting a new session");try{yield this.startSession(e,t)}catch(e){throw new Error("StreamToVideoAndAudioElements: Failed to start session")}if(!this.sessionId||!this.streamingClient)throw new Error("StreamToVideoAndAudioElements: session Id or streaming client is not available after starting session")}}))}stream(){return i(this,arguments,void 0,(function*(e={},t,n){if(yield this.startSessionIfNeeded(t,n),this._isStreaming)throw new Error("Already streaming");return this._isStreaming=!0,new Promise((t=>{var n,i,o;const s=[];let a=!1,r=!1;null===(n=this.streamingClient)||void 0===n||n.setOnVideoStreamStartCallback((e=>{s.push(e),a=!0,r&&t(s)})),null===(i=this.streamingClient)||void 0===i||i.setOnAudioStreamStartCallback((e=>{s.push(e),r=!0,a&&t(s)})),null===(o=this.streamingClient)||void 0===o||o.startConnection(e)}))}))}streamToVideoAndAudioElements(e,t){return i(this,arguments,void 0,(function*(e,t,n={},i,o){if(yield this.startSessionIfNeeded(i,o),this._isStreaming)throw new Error("Already streaming");if(this._isStreaming=!0,!this.streamingClient)throw new Error("Failed to stream: streaming client is not available");this.streamingClient.setMediaStreamTargetsById(e,t),this.streamingClient.startConnection(n)}))}talk(e){return i(this,void 0,void 0,(function*(){if(!this.streamingClient)throw new Error("Failed to send talk command: session is not started. Have you called startSession?");if(!this._isStreaming)throw new Error("Failed to send talk command: not currently streaming. Have you called stream?");yield this.streamingClient.sendTalkCommand(e)}))}sendDataMessage(e){if(!this.streamingClient)throw new Error("Failed to send message: session is not started.");this.streamingClient.sendDataMessage(e)}stopStreaming(){return i(this,void 0,void 0,(function*(){this.streamingClient&&(this.streamingClient.stopConnection(),this.streamingClient=null,this.sessionId=null,this._isStreaming=!1)}))}isStreaming(){return this._isStreaming}setPersonaConfig(e){this.personaConfig=e}getPersonaConfig(){return this.personaConfig}}},440:function(e,t,n){var i=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0}),t.unsafe_createClientWithApiKey=t.createClient=void 0;const o=i(n(816));t.createClient=(e,t,n)=>new o.default(e,t,n),t.unsafe_createClientWithApiKey=(e,t,n)=>new o.default(void 0,t,Object.assign(Object.assign({},n),{apiKey:e}))},985:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.DEFAULT_ICE_SERVERS=t.PUBLIC_MESSAGE_ON_WEBRTC_FAILURE=t.PUBLIC_MESSAGE_ON_SIGNALLING_CLIENT_CONNECTION_FAILURE=t.DEFAULT_ENGINE_BASE_URL=t.DEFAULT_API_VERSION=t.DEFAULT_API_BASE_URL=t.DEFAULT_HEADERS=void 0,t.DEFAULT_HEADERS={"Content-Type":"application/json"},t.DEFAULT_API_BASE_URL="https://api.anam.ai",t.DEFAULT_API_VERSION="/v1",t.DEFAULT_ENGINE_BASE_URL="http://localhost:8081",t.PUBLIC_MESSAGE_ON_SIGNALLING_CLIENT_CONNECTION_FAILURE="There was a problem connecting to our servers. Please try again.",t.PUBLIC_MESSAGE_ON_WEBRTC_FAILURE="There was an issue connecting to our servers. Please try again.",t.DEFAULT_ICE_SERVERS=[{urls:"stun:stun.relay.metered.ca:80"}]},18:function(e,t,n){var i=this&&this.__awaiter||function(e,t,n,i){return new(n||(n=Promise))((function(o,s){function a(e){try{l(i.next(e))}catch(e){s(e)}}function r(e){try{l(i.throw(e))}catch(e){s(e)}}function l(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(a,r)}l((i=i.apply(e,t||[])).next())}))};Object.defineProperty(t,"__esModule",{value:!0}),t.CoreApiRestClient=void 0;const o=n(985);t.CoreApiRestClient=class{constructor(e,t,n){if(!e&&!t)throw new Error("Either sessionToken or apiKey must be provided");this.sessionToken=e||null,this.apiKey=t||null,this.baseUrl=(null==n?void 0:n.baseUrl)||o.DEFAULT_API_BASE_URL,this.apiVersion=(null==n?void 0:n.apiVersion)||o.DEFAULT_API_VERSION}startSession(e){return i(this,void 0,void 0,(function*(){this.sessionToken||(this.sessionToken=yield this.unsafe_getSessionToken());try{const t=yield fetch(`${this.getApiUrl()}/engine/session`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.sessionToken}`},body:JSON.stringify({personaConfig:e})});return yield t.json()}catch(e){throw new Error("Failed to start session")}}))}unsafe_getSessionToken(){return i(this,void 0,void 0,(function*(){if(console.warn("Using unsecure method. This method should not be used in production."),!this.apiKey)throw new Error("No apiKey provided");try{const e=yield fetch(`${this.getApiUrl()}/auth/session-token`,{method:"GET",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`}});return(yield e.json()).sessionToken}catch(e){throw new Error("Failed to get session token")}}))}getApiUrl(){return`${this.baseUrl}${this.apiVersion}`}}},529:function(e,t){var n=this&&this.__awaiter||function(e,t,n,i){return new(n||(n=Promise))((function(o,s){function a(e){try{l(i.next(e))}catch(e){s(e)}}function r(e){try{l(i.throw(e))}catch(e){s(e)}}function l(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(a,r)}l((i=i.apply(e,t||[])).next())}))};Object.defineProperty(t,"__esModule",{value:!0}),t.EngineApiRestClient=void 0,t.EngineApiRestClient=class{constructor(e,t){this.baseUrl=e,this.sessionId=t}sendTalkCommand(e){return n(this,void 0,void 0,(function*(){try{const t=yield fetch(`${this.baseUrl}/talk?session_id=${this.sessionId}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({content:e})});if(!t.ok)throw new Error(`Failed to send talk command: ${t.status} ${t.statusText}`)}catch(e){throw console.error(e),new Error("EngineApiRestClient - sendTalkCommand: Failed to send talk command")}}))}}},279:function(e,t,n){var i=this&&this.__awaiter||function(e,t,n,i){return new(n||(n=Promise))((function(o,s){function a(e){try{l(i.next(e))}catch(e){s(e)}}function r(e){try{l(i.throw(e))}catch(e){s(e)}}function l(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(a,r)}l((i=i.apply(e,t||[])).next())}))};Object.defineProperty(t,"__esModule",{value:!0}),t.SignallingClient=t.DEFATULT_OPTIONS=void 0;const o=n(985),s=n(170);t.DEFATULT_OPTIONS={heartbeatIntervalSeconds:5,maxWsReconnectionAttempts:5,url:{baseUrl:o.DEFAULT_ENGINE_BASE_URL,protocol:"ws"}},t.SignallingClient=class{constructor(e,n=t.DEFATULT_OPTIONS,i,o,s){var a;if(this.stopSignal=!1,this.sendingBuffer=[],this.wsConnectionAttempts=0,this.socket=null,this.heartBeatIntervalRef=null,!e)throw new Error("Signalling Client: sessionId is required");this.sessionId=e,i&&(this.onSignalMessageReceivedCallback=i),o&&(this.onClientConnectedCallback=o),s&&(this.onClientConnectionFailureCallback=s);const{heartbeatIntervalSeconds:r,maxWsReconnectionAttempts:l,url:c}=n;if(this.heartbeatIntervalSeconds=r||5,this.maxWsReconnectionAttempts=l||5,!c.baseUrl)throw new Error("Signalling Client: baseUrl is required");const d=`${c.protocol||"https"}://${c.baseUrl}`;this.url=new URL(d),this.url.protocol="http"===c.protocol?"ws:":"wss:",c.port&&(this.url.port=c.port),this.url.pathname=null!==(a=c.signallingPath)&&void 0!==a?a:"/ws",this.url.searchParams.append("session_id",e)}stop(){this.stopSignal=!0,this.closeSocket()}connect(){return this.socket=new WebSocket(this.url.href),this.socket.onopen=this.onOpen.bind(this),this.socket.onclose=this.onClose.bind(this),this.socket.onerror=this.onError.bind(this),this.socket}sendOffer(e){return i(this,void 0,void 0,(function*(){const t={connectionDescription:e,userUid:this.sessionId},n={actionType:s.SignalMessageAction.OFFER,sessionId:this.sessionId,payload:t};this.sendSignalMessage(n)}))}sendIceCandidate(e){return i(this,void 0,void 0,(function*(){const t={actionType:s.SignalMessageAction.ICE_CANDIDATE,sessionId:this.sessionId,payload:e.toJSON()};this.sendSignalMessage(t)}))}sendSignalMessage(e){var t;if((null===(t=this.socket)||void 0===t?void 0:t.readyState)===WebSocket.OPEN)try{this.socket.send(JSON.stringify(e))}catch(e){console.error("SignallingClient - sendSignalMessage: error sending message",e)}else this.sendingBuffer.push(e)}closeSocket(){this.socket&&(this.socket.close(),this.socket=null),this.heartBeatIntervalRef&&(clearInterval(this.heartBeatIntervalRef),this.heartBeatIntervalRef=null)}onOpen(){return i(this,void 0,void 0,(function*(){if(!this.socket)throw new Error("SignallingClient - onOpen: socket is null");try{this.wsConnectionAttempts=0,this.flushSendingBuffer(),this.socket.onmessage=this.onMessage.bind(this),this.startSendingHeartBeats(),this.onClientConnectedCallback&&(yield this.onClientConnectedCallback())}catch(e){console.error("SignallingClient - onOpen: error in onOpen",e),this.onClientConnectionFailureCallback&&this.onClientConnectionFailureCallback()}}))}onClose(){return i(this,void 0,void 0,(function*(){this.wsConnectionAttempts+=1,this.stopSignal||(this.wsConnectionAttempts<=this.maxWsReconnectionAttempts?(this.socket=null,setTimeout((()=>{this.connect()}),100*this.wsConnectionAttempts)):(this.heartBeatIntervalRef&&(clearInterval(this.heartBeatIntervalRef),this.heartBeatIntervalRef=null),this.onClientConnectionFailureCallback&&this.onClientConnectionFailureCallback()))}))}onError(e){this.stopSignal||console.error("SignallingClient - onError: ",e)}flushSendingBuffer(){const e=[];this.sendingBuffer.length>0&&this.sendingBuffer.forEach((t=>{var n;(null===(n=this.socket)||void 0===n?void 0:n.readyState)===WebSocket.OPEN?this.socket.send(JSON.stringify(t)):e.push(t)})),this.sendingBuffer=e}onMessage(e){return i(this,void 0,void 0,(function*(){const t=JSON.parse(e.data);this.onSignalMessageReceivedCallback&&(yield this.onSignalMessageReceivedCallback(t))}))}startSendingHeartBeats(){if(!this.socket)throw new Error("SignallingClient - startSendingHeartBeats: socket is null");this.heartBeatIntervalRef&&console.warn("SignallingClient - startSendingHeartBeats: heartbeat interval already set");const e=1e3*this.heartbeatIntervalSeconds,t={actionType:s.SignalMessageAction.HEARTBEAT,sessionId:this.sessionId,payload:""},n=JSON.stringify(t);this.heartBeatIntervalRef=setInterval((()=>{var e;this.stopSignal||(null===(e=this.socket)||void 0===e?void 0:e.readyState)===WebSocket.OPEN&&this.socket.send(n)}),e)}}},577:function(e,t,n){var i=this&&this.__awaiter||function(e,t,n,i){return new(n||(n=Promise))((function(o,s){function a(e){try{l(i.next(e))}catch(e){s(e)}}function r(e){try{l(i.throw(e))}catch(e){s(e)}}function l(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(a,r)}l((i=i.apply(e,t||[])).next())}))};Object.defineProperty(t,"__esModule",{value:!0}),t.StreamingClient=void 0;const o=n(985),s=n(295),a=n(529),r=n(279),l={engine:{baseUrl:o.DEFAULT_ENGINE_BASE_URL},signalling:r.DEFATULT_OPTIONS,iceServers:o.DEFAULT_ICE_SERVERS};t.StreamingClient=class{constructor(e,t=l){this.iceServers=o.DEFAULT_ICE_SERVERS,this.peerConnection=null,this.connectionReceivedAnswer=!1,this.remoteIceCandidateBuffer=[],this.inputAudioStream=null,this.dataChannel=null,this.videoElement=null,this.videoStream=null,this.audioElement=null,this.audioStream=null,this.iceServers=t.iceServers||o.DEFAULT_ICE_SERVERS,this.signallingClient=new r.SignallingClient(e,t.signalling,this.onSignalMessage.bind(this),this.onSignallingClientConnected.bind(this),this.onSignallingClientFailed.bind(this)),this.engineApiRestClient=new a.EngineApiRestClient(t.engine.baseUrl,e),t.userProvidedMediaStream&&(this.inputAudioStream=t.userProvidedMediaStream)}getPeerConnection(){return this.peerConnection}getInputAudioStream(){return this.inputAudioStream}getVideoStream(){return this.videoStream}getAudioStream(){return this.audioStream}setOnVideoStreamStartCallback(e){this.onVideoStreamStartCallback=e}setOnAudioStreamStartCallback(e){this.onAudioStreamStartCallback=e}sendDataMessage(e){this.dataChannel&&"open"===this.dataChannel.readyState&&this.dataChannel.send(e)}setMediaStreamTargetsById(e,t){if(e){const t=document.getElementById(e);if(!t)throw new Error(`StreamingClient: video element with id ${e} not found`);this.videoElement=t}if(t){const e=document.getElementById(t);if(!e)throw new Error(`StreamingClient: audio element with id ${t} not found`);this.audioElement=e}}startConnection(e){try{if(this.peerConnection)return void console.error("StreamingClient - startConnection: peer connection already exists");this.setConnectionCallbacks(e),this.signallingClient.connect()}catch(e){this.handleWebrtcFailure(e)}}stopConnection(){this.shutdown()}sendTalkCommand(e){return i(this,void 0,void 0,(function*(){if(!this.peerConnection)throw new Error("StreamingClient - sendTalkCommand: peer connection is null");yield this.engineApiRestClient.sendTalkCommand(e)}))}setConnectionCallbacks({onReceiveMessageCallback:e,onConnectionEstablishedCallback:t,onConnectionClosedCallback:n,onInputAudioStreamStartCallback:i,onVideoStreamStartCallback:o,onVideoPlayStartedCallback:s,onAudioStreamStartCallback:a}){e&&(this.onReceiveMessageCallback=e),t&&(this.onConnectionEstablishedCallback=t),n&&(this.onConnectionClosedCallback=n),i&&(this.onInputAudioStreamStartCallback=i),o&&(this.onVideoStreamStartCallback=o),s&&(this.onVideoPlayStartedCallback=s),a&&(this.onAudioStreamStartCallback=a)}initPeerConnection(){return i(this,void 0,void 0,(function*(){this.peerConnection=new RTCPeerConnection({iceServers:this.iceServers}),this.peerConnection.onicecandidate=this.onIceCandidate.bind(this),this.peerConnection.oniceconnectionstatechange=this.onIceConnectionStateChange.bind(this),this.peerConnection.onconnectionstatechange=this.onConnectionStateChange.bind(this),this.peerConnection.addEventListener("track",this.onTrackEventHandler.bind(this)),yield this.setupDataChannels(),this.peerConnection.addTransceiver("video",{direction:"recvonly"}),this.peerConnection.addTransceiver("audio",{direction:"sendrecv"})}))}onSignalMessage(e){return i(this,void 0,void 0,(function*(){if(this.peerConnection)switch(e.actionType){case s.SignalMessageAction.ANSWER:const t=e.payload;yield this.peerConnection.setRemoteDescription(t),this.connectionReceivedAnswer=!0,this.flushRemoteIceCandidateBuffer();break;case s.SignalMessageAction.ICE_CANDIDATE:const n=e.payload,i=new RTCIceCandidate(n);this.connectionReceivedAnswer?yield this.peerConnection.addIceCandidate(i):this.remoteIceCandidateBuffer.push(i);break;case s.SignalMessageAction.END_SESSION:const o=e.payload;this.onConnectionClosedCallback&&this.onConnectionClosedCallback(o),this.shutdown();break;case s.SignalMessageAction.WARNING:const a=e.payload;console.warn("Warning received from server: "+a);break;default:console.error("StreamingClient - onSignalMessage: unknown signal message action type",e)}else console.error("StreamingClient - onSignalMessage: peerConnection is not initialized")}))}onSignallingClientConnected(){return i(this,void 0,void 0,(function*(){if(!this.peerConnection)try{yield this.initPeerConnectionAndSendOffer()}catch(e){console.error("StreamingClient - onSignallingClientConnected: Error initializing peer connection",e),this.handleWebrtcFailure(e)}}))}onSignallingClientFailed(){console.error("StreamingClient - onSignallingClientFailed: signalling client failed"),this.onConnectionClosedCallback&&this.onConnectionClosedCallback(o.PUBLIC_MESSAGE_ON_SIGNALLING_CLIENT_CONNECTION_FAILURE)}flushRemoteIceCandidateBuffer(){this.remoteIceCandidateBuffer.forEach((e=>{var t;null===(t=this.peerConnection)||void 0===t||t.addIceCandidate(e)})),this.remoteIceCandidateBuffer=[]}onIceCandidate(e){e.candidate&&this.signallingClient.sendIceCandidate(e.candidate)}onIceConnectionStateChange(){var e,t;"connected"!==(null===(e=this.peerConnection)||void 0===e?void 0:e.iceConnectionState)&&"completed"!==(null===(t=this.peerConnection)||void 0===t?void 0:t.iceConnectionState)||this.onConnectionEstablishedCallback&&this.onConnectionEstablishedCallback()}onConnectionStateChange(){var e;"closed"===(null===(e=this.peerConnection)||void 0===e?void 0:e.connectionState)&&(console.error("StreamingClient - onConnectionStateChange: Connection closed"),this.handleWebrtcFailure("The connection to our servers was lost. Please try again."))}handleWebrtcFailure(e){console.error("StreamingClient - handleWebrtcFailure: ",e);try{this.stopConnection()}catch(e){console.error("StreamingClient - handleWebrtcFailure: error stopping connection",e)}this.onConnectionClosedCallback&&this.onConnectionClosedCallback(o.PUBLIC_MESSAGE_ON_WEBRTC_FAILURE)}onTrackEventHandler(e){if("video"===e.track.kind){if(this.videoStream=e.streams[0],this.onVideoStreamStartCallback&&this.onVideoStreamStartCallback(this.videoStream),this.videoElement){this.videoElement.srcObject=this.videoStream;const e=this.videoElement.requestVideoFrameCallback((()=>{var t;null===(t=this.videoElement)||void 0===t||t.cancelVideoFrameCallback(e),this.onVideoPlayStartedCallback&&this.onVideoPlayStartedCallback()}))}}else"audio"===e.track.kind&&(this.audioStream=e.streams[0],this.onAudioStreamStartCallback&&this.onAudioStreamStartCallback(this.audioStream),this.audioElement&&(this.audioElement.srcObject=this.audioStream))}setupDataChannels(){return i(this,void 0,void 0,(function*(){if(!this.peerConnection)return void console.error("StreamingClient - setupDataChannels: peer connection is not initialized");if(this.inputAudioStream){if(!this.inputAudioStream.getAudioTracks().length)throw new Error("StreamingClient - setupDataChannels: user provided stream does not have audio tracks")}else this.inputAudioStream=yield navigator.mediaDevices.getUserMedia({audio:{echoCancellation:!0}});const e=this.inputAudioStream.getAudioTracks()[0];this.peerConnection.addTrack(e,this.inputAudioStream),this.onInputAudioStreamStartCallback&&this.onInputAudioStreamStartCallback(this.inputAudioStream);const t=this.peerConnection.createDataChannel("chat",{ordered:!0});t.onopen=()=>{this.dataChannel=null!=t?t:null},t.onclose=()=>{},t.onmessage=e=>{this.onReceiveMessageCallback&&this.onReceiveMessageCallback(JSON.parse(e.data))}}))}initPeerConnectionAndSendOffer(){return i(this,void 0,void 0,(function*(){if(yield this.initPeerConnection(),!this.peerConnection)return void console.error("StreamingClient - initPeerConnectionAndSendOffer: peer connection is not initialized");const e=yield this.peerConnection.createOffer();if(yield this.peerConnection.setLocalDescription(e),!this.peerConnection.localDescription)throw new Error("StreamingClient - initPeerConnectionAndSendOffer: local description is null");yield this.signallingClient.sendOffer(this.peerConnection.localDescription)}))}shutdown(){try{this.inputAudioStream&&this.inputAudioStream.getTracks().forEach((e=>{e.stop()})),this.inputAudioStream=null}catch(e){console.error("StreamingClient - shutdown: error stopping input audio stream",e)}try{this.signallingClient.stop()}catch(e){console.error("StreamingClient - shutdown: error stopping signallilng",e)}try{this.peerConnection&&"closed"!==this.peerConnection.connectionState&&(this.peerConnection.onconnectionstatechange=null,this.peerConnection.close(),this.peerConnection=null)}catch(e){console.error("StreamingClient - shutdown: error closing peer connection",e)}}}},170:(e,t,n)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.SignalMessageAction=void 0;var i=n(295);Object.defineProperty(t,"SignalMessageAction",{enumerable:!0,get:function(){return i.SignalMessageAction}})},656:(e,t)=>{var n;Object.defineProperty(t,"__esModule",{value:!0}),t.SignalMessageAction=void 0,function(e){e.OFFER="offer",e.ANSWER="answer",e.ICE_CANDIDATE="icecandidate",e.END_SESSION="endsession",e.HEARTBEAT="heartbeat",e.WARNING="warning"}(n||(t.SignalMessageAction=n={}))},295:(e,t,n)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.SignalMessageAction=void 0;var i=n(656);Object.defineProperty(t,"SignalMessageAction",{enumerable:!0,get:function(){return i.SignalMessageAction}})}},t={};return function n(i){var o=t[i];if(void 0!==o)return o.exports;var s=t[i]={exports:{}};return e[i].call(s.exports,s,s.exports,n),s.exports}(440)})()));
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.anam=t():e.anam=t()}(self,(()=>(()=>{"use strict";var e={816:function(e,t,n){var i=this&&this.__awaiter||function(e,t,n,i){return new(n||(n=Promise))((function(o,s){function a(e){try{l(i.next(e))}catch(e){s(e)}}function r(e){try{l(i.throw(e))}catch(e){s(e)}}function l(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(a,r)}l((i=i.apply(e,t||[])).next())}))};Object.defineProperty(t,"__esModule",{value:!0});const o=n(18),s=n(577);t.default=class{constructor(e,t,n){if(this.sessionId=null,this.streamingClient=null,this._isStreaming=!1,!e&&!(null==n?void 0:n.apiKey))throw new Error("Either sessionToken or apiKey must be provided");this.sessionToken=e,this.apiKey=null==n?void 0:n.apiKey,this.personaConfig=t,this.apiClient=new o.CoreApiRestClient(e,null==n?void 0:n.apiKey,null==n?void 0:n.api)}startSession(e){return i(this,void 0,void 0,(function*(){try{const t=this.personaConfig;if(!t)throw new Error("A default persona configuration has not been set and no persona configuration was provided");const n=yield this.apiClient.startSession(t),{sessionId:i,clientConfig:o,engineHost:a,engineProtocol:r,signallingEndpoint:l}=n,{heartbeatIntervalSeconds:c,maxWsReconnectionAttempts:d,iceServers:h}=o;return this.streamingClient=new s.StreamingClient(i,{engine:{baseUrl:`${r}://${a}`},signalling:{heartbeatIntervalSeconds:c,maxWsReconnectionAttempts:d,url:{baseUrl:a,protocol:r,signallingPath:l}},iceServers:h,userProvidedMediaStream:e}),this.sessionId=i,i}catch(e){throw new Error("Failed to start session")}}))}startSessionIfNeeded(e){return i(this,void 0,void 0,(function*(){if(!this.sessionId||!this.streamingClient){console.warn("StreamToVideoAndAudioElements: session is not started. starting a new session");try{yield this.startSession(e)}catch(e){throw new Error("StreamToVideoAndAudioElements: Failed to start session")}if(!this.sessionId||!this.streamingClient)throw new Error("StreamToVideoAndAudioElements: session Id or streaming client is not available after starting session")}}))}stream(){return i(this,arguments,void 0,(function*(e={},t){if(yield this.startSessionIfNeeded(t),this._isStreaming)throw new Error("Already streaming");return this._isStreaming=!0,new Promise((t=>{var n,i,o;const s=[];let a=!1,r=!1;null===(n=this.streamingClient)||void 0===n||n.setOnVideoStreamStartCallback((e=>{s.push(e),a=!0,r&&t(s)})),null===(i=this.streamingClient)||void 0===i||i.setOnAudioStreamStartCallback((e=>{s.push(e),r=!0,a&&t(s)})),null===(o=this.streamingClient)||void 0===o||o.startConnection(e)}))}))}streamToVideoAndAudioElements(e,t){return i(this,arguments,void 0,(function*(e,t,n={},i){if(yield this.startSessionIfNeeded(i),this._isStreaming)throw new Error("Already streaming");if(this._isStreaming=!0,!this.streamingClient)throw new Error("Failed to stream: streaming client is not available");this.streamingClient.setMediaStreamTargetsById(e,t),this.streamingClient.startConnection(n)}))}talk(e){return i(this,void 0,void 0,(function*(){if(!this.streamingClient)throw new Error("Failed to send talk command: session is not started. Have you called startSession?");if(!this._isStreaming)throw new Error("Failed to send talk command: not currently streaming. Have you called stream?");yield this.streamingClient.sendTalkCommand(e)}))}sendDataMessage(e){if(!this.streamingClient)throw new Error("Failed to send message: session is not started.");this.streamingClient.sendDataMessage(e)}stopStreaming(){return i(this,void 0,void 0,(function*(){this.streamingClient&&(this.streamingClient.stopConnection(),this.streamingClient=null,this.sessionId=null,this._isStreaming=!1)}))}isStreaming(){return this._isStreaming}setPersonaConfig(e){this.personaConfig=e}getPersonaConfig(){return this.personaConfig}}},440:function(e,t,n){var i=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0}),t.unsafe_createClientWithApiKey=t.createClient=void 0;const o=i(n(816));t.createClient=(e,t,n)=>new o.default(e,t,n),t.unsafe_createClientWithApiKey=(e,t,n)=>new o.default(void 0,t,Object.assign(Object.assign({},n),{apiKey:e}))},985:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.DEFAULT_ICE_SERVERS=t.PUBLIC_MESSAGE_ON_WEBRTC_FAILURE=t.PUBLIC_MESSAGE_ON_SIGNALLING_CLIENT_CONNECTION_FAILURE=t.DEFAULT_ENGINE_BASE_URL=t.DEFAULT_API_VERSION=t.DEFAULT_API_BASE_URL=t.DEFAULT_HEADERS=void 0,t.DEFAULT_HEADERS={"Content-Type":"application/json"},t.DEFAULT_API_BASE_URL="https://api.anam.ai",t.DEFAULT_API_VERSION="/v1",t.DEFAULT_ENGINE_BASE_URL="http://localhost:8081",t.PUBLIC_MESSAGE_ON_SIGNALLING_CLIENT_CONNECTION_FAILURE="There was a problem connecting to our servers. Please try again.",t.PUBLIC_MESSAGE_ON_WEBRTC_FAILURE="There was an issue connecting to our servers. Please try again.",t.DEFAULT_ICE_SERVERS=[{urls:"stun:stun.relay.metered.ca:80"}]},18:function(e,t,n){var i=this&&this.__awaiter||function(e,t,n,i){return new(n||(n=Promise))((function(o,s){function a(e){try{l(i.next(e))}catch(e){s(e)}}function r(e){try{l(i.throw(e))}catch(e){s(e)}}function l(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(a,r)}l((i=i.apply(e,t||[])).next())}))};Object.defineProperty(t,"__esModule",{value:!0}),t.CoreApiRestClient=void 0;const o=n(985);t.CoreApiRestClient=class{constructor(e,t,n){if(!e&&!t)throw new Error("Either sessionToken or apiKey must be provided");this.sessionToken=e||null,this.apiKey=t||null,this.baseUrl=(null==n?void 0:n.baseUrl)||o.DEFAULT_API_BASE_URL,this.apiVersion=(null==n?void 0:n.apiVersion)||o.DEFAULT_API_VERSION}startSession(e){return i(this,void 0,void 0,(function*(){this.sessionToken||(this.sessionToken=yield this.unsafe_getSessionToken());try{const t=yield fetch(`${this.getApiUrl()}/engine/session`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.sessionToken}`},body:JSON.stringify({personaConfig:e})});return yield t.json()}catch(e){throw new Error("Failed to start session")}}))}unsafe_getSessionToken(){return i(this,void 0,void 0,(function*(){if(console.warn("Using unsecure method. This method should not be used in production."),!this.apiKey)throw new Error("No apiKey provided");try{const e=yield fetch(`${this.getApiUrl()}/auth/session-token`,{method:"GET",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`}});return(yield e.json()).sessionToken}catch(e){throw new Error("Failed to get session token")}}))}getApiUrl(){return`${this.baseUrl}${this.apiVersion}`}}},529:function(e,t){var n=this&&this.__awaiter||function(e,t,n,i){return new(n||(n=Promise))((function(o,s){function a(e){try{l(i.next(e))}catch(e){s(e)}}function r(e){try{l(i.throw(e))}catch(e){s(e)}}function l(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(a,r)}l((i=i.apply(e,t||[])).next())}))};Object.defineProperty(t,"__esModule",{value:!0}),t.EngineApiRestClient=void 0,t.EngineApiRestClient=class{constructor(e,t){this.baseUrl=e,this.sessionId=t}sendTalkCommand(e){return n(this,void 0,void 0,(function*(){try{const t=yield fetch(`${this.baseUrl}/talk?session_id=${this.sessionId}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({content:e})});if(!t.ok)throw new Error(`Failed to send talk command: ${t.status} ${t.statusText}`)}catch(e){throw console.error(e),new Error("EngineApiRestClient - sendTalkCommand: Failed to send talk command")}}))}}},279:function(e,t,n){var i=this&&this.__awaiter||function(e,t,n,i){return new(n||(n=Promise))((function(o,s){function a(e){try{l(i.next(e))}catch(e){s(e)}}function r(e){try{l(i.throw(e))}catch(e){s(e)}}function l(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(a,r)}l((i=i.apply(e,t||[])).next())}))};Object.defineProperty(t,"__esModule",{value:!0}),t.SignallingClient=t.DEFATULT_OPTIONS=void 0;const o=n(985),s=n(170);t.DEFATULT_OPTIONS={heartbeatIntervalSeconds:5,maxWsReconnectionAttempts:5,url:{baseUrl:o.DEFAULT_ENGINE_BASE_URL,protocol:"ws"}},t.SignallingClient=class{constructor(e,n=t.DEFATULT_OPTIONS,i,o,s){var a;if(this.stopSignal=!1,this.sendingBuffer=[],this.wsConnectionAttempts=0,this.socket=null,this.heartBeatIntervalRef=null,!e)throw new Error("Signalling Client: sessionId is required");this.sessionId=e,i&&(this.onSignalMessageReceivedCallback=i),o&&(this.onClientConnectedCallback=o),s&&(this.onClientConnectionFailureCallback=s);const{heartbeatIntervalSeconds:r,maxWsReconnectionAttempts:l,url:c}=n;if(this.heartbeatIntervalSeconds=r||5,this.maxWsReconnectionAttempts=l||5,!c.baseUrl)throw new Error("Signalling Client: baseUrl is required");const d=`${c.protocol||"https"}://${c.baseUrl}`;this.url=new URL(d),this.url.protocol="http"===c.protocol?"ws:":"wss:",c.port&&(this.url.port=c.port),this.url.pathname=null!==(a=c.signallingPath)&&void 0!==a?a:"/ws",this.url.searchParams.append("session_id",e)}stop(){this.stopSignal=!0,this.closeSocket()}connect(){return this.socket=new WebSocket(this.url.href),this.socket.onopen=this.onOpen.bind(this),this.socket.onclose=this.onClose.bind(this),this.socket.onerror=this.onError.bind(this),this.socket}sendOffer(e){return i(this,void 0,void 0,(function*(){const t={connectionDescription:e,userUid:this.sessionId},n={actionType:s.SignalMessageAction.OFFER,sessionId:this.sessionId,payload:t};this.sendSignalMessage(n)}))}sendIceCandidate(e){return i(this,void 0,void 0,(function*(){const t={actionType:s.SignalMessageAction.ICE_CANDIDATE,sessionId:this.sessionId,payload:e.toJSON()};this.sendSignalMessage(t)}))}sendSignalMessage(e){var t;if((null===(t=this.socket)||void 0===t?void 0:t.readyState)===WebSocket.OPEN)try{this.socket.send(JSON.stringify(e))}catch(e){console.error("SignallingClient - sendSignalMessage: error sending message",e)}else this.sendingBuffer.push(e)}closeSocket(){this.socket&&(this.socket.close(),this.socket=null),this.heartBeatIntervalRef&&(clearInterval(this.heartBeatIntervalRef),this.heartBeatIntervalRef=null)}onOpen(){return i(this,void 0,void 0,(function*(){if(!this.socket)throw new Error("SignallingClient - onOpen: socket is null");try{this.wsConnectionAttempts=0,this.flushSendingBuffer(),this.socket.onmessage=this.onMessage.bind(this),this.startSendingHeartBeats(),this.onClientConnectedCallback&&(yield this.onClientConnectedCallback())}catch(e){console.error("SignallingClient - onOpen: error in onOpen",e),this.onClientConnectionFailureCallback&&this.onClientConnectionFailureCallback()}}))}onClose(){return i(this,void 0,void 0,(function*(){this.wsConnectionAttempts+=1,this.stopSignal||(this.wsConnectionAttempts<=this.maxWsReconnectionAttempts?(this.socket=null,setTimeout((()=>{this.connect()}),100*this.wsConnectionAttempts)):(this.heartBeatIntervalRef&&(clearInterval(this.heartBeatIntervalRef),this.heartBeatIntervalRef=null),this.onClientConnectionFailureCallback&&this.onClientConnectionFailureCallback()))}))}onError(e){this.stopSignal||console.error("SignallingClient - onError: ",e)}flushSendingBuffer(){const e=[];this.sendingBuffer.length>0&&this.sendingBuffer.forEach((t=>{var n;(null===(n=this.socket)||void 0===n?void 0:n.readyState)===WebSocket.OPEN?this.socket.send(JSON.stringify(t)):e.push(t)})),this.sendingBuffer=e}onMessage(e){return i(this,void 0,void 0,(function*(){const t=JSON.parse(e.data);this.onSignalMessageReceivedCallback&&(yield this.onSignalMessageReceivedCallback(t))}))}startSendingHeartBeats(){if(!this.socket)throw new Error("SignallingClient - startSendingHeartBeats: socket is null");this.heartBeatIntervalRef&&console.warn("SignallingClient - startSendingHeartBeats: heartbeat interval already set");const e=1e3*this.heartbeatIntervalSeconds,t={actionType:s.SignalMessageAction.HEARTBEAT,sessionId:this.sessionId,payload:""},n=JSON.stringify(t);this.heartBeatIntervalRef=setInterval((()=>{var e;this.stopSignal||(null===(e=this.socket)||void 0===e?void 0:e.readyState)===WebSocket.OPEN&&this.socket.send(n)}),e)}}},577:function(e,t,n){var i=this&&this.__awaiter||function(e,t,n,i){return new(n||(n=Promise))((function(o,s){function a(e){try{l(i.next(e))}catch(e){s(e)}}function r(e){try{l(i.throw(e))}catch(e){s(e)}}function l(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(a,r)}l((i=i.apply(e,t||[])).next())}))};Object.defineProperty(t,"__esModule",{value:!0}),t.StreamingClient=void 0;const o=n(985),s=n(295),a=n(529),r=n(279),l={engine:{baseUrl:o.DEFAULT_ENGINE_BASE_URL},signalling:r.DEFATULT_OPTIONS,iceServers:o.DEFAULT_ICE_SERVERS};t.StreamingClient=class{constructor(e,t=l){this.iceServers=o.DEFAULT_ICE_SERVERS,this.peerConnection=null,this.connectionReceivedAnswer=!1,this.remoteIceCandidateBuffer=[],this.inputAudioStream=null,this.dataChannel=null,this.videoElement=null,this.videoStream=null,this.audioElement=null,this.audioStream=null,this.iceServers=t.iceServers||o.DEFAULT_ICE_SERVERS,this.signallingClient=new r.SignallingClient(e,t.signalling,this.onSignalMessage.bind(this),this.onSignallingClientConnected.bind(this),this.onSignallingClientFailed.bind(this)),this.engineApiRestClient=new a.EngineApiRestClient(t.engine.baseUrl,e),t.userProvidedMediaStream&&(this.inputAudioStream=t.userProvidedMediaStream)}getPeerConnection(){return this.peerConnection}getInputAudioStream(){return this.inputAudioStream}getVideoStream(){return this.videoStream}getAudioStream(){return this.audioStream}setOnVideoStreamStartCallback(e){this.onVideoStreamStartCallback=e}setOnAudioStreamStartCallback(e){this.onAudioStreamStartCallback=e}sendDataMessage(e){this.dataChannel&&"open"===this.dataChannel.readyState&&this.dataChannel.send(e)}setMediaStreamTargetsById(e,t){if(e){const t=document.getElementById(e);if(!t)throw new Error(`StreamingClient: video element with id ${e} not found`);this.videoElement=t}if(t){const e=document.getElementById(t);if(!e)throw new Error(`StreamingClient: audio element with id ${t} not found`);this.audioElement=e}}startConnection(e){try{if(this.peerConnection)return void console.error("StreamingClient - startConnection: peer connection already exists");this.setConnectionCallbacks(e),this.signallingClient.connect()}catch(e){this.handleWebrtcFailure(e)}}stopConnection(){this.shutdown()}sendTalkCommand(e){return i(this,void 0,void 0,(function*(){if(!this.peerConnection)throw new Error("StreamingClient - sendTalkCommand: peer connection is null");yield this.engineApiRestClient.sendTalkCommand(e)}))}setConnectionCallbacks({onReceiveMessageCallback:e,onConnectionEstablishedCallback:t,onConnectionClosedCallback:n,onInputAudioStreamStartCallback:i,onVideoStreamStartCallback:o,onVideoPlayStartedCallback:s,onAudioStreamStartCallback:a}){e&&(this.onReceiveMessageCallback=e),t&&(this.onConnectionEstablishedCallback=t),n&&(this.onConnectionClosedCallback=n),i&&(this.onInputAudioStreamStartCallback=i),o&&(this.onVideoStreamStartCallback=o),s&&(this.onVideoPlayStartedCallback=s),a&&(this.onAudioStreamStartCallback=a)}initPeerConnection(){return i(this,void 0,void 0,(function*(){this.peerConnection=new RTCPeerConnection({iceServers:this.iceServers}),this.peerConnection.onicecandidate=this.onIceCandidate.bind(this),this.peerConnection.oniceconnectionstatechange=this.onIceConnectionStateChange.bind(this),this.peerConnection.onconnectionstatechange=this.onConnectionStateChange.bind(this),this.peerConnection.addEventListener("track",this.onTrackEventHandler.bind(this)),yield this.setupDataChannels(),this.peerConnection.addTransceiver("video",{direction:"recvonly"}),this.peerConnection.addTransceiver("audio",{direction:"sendrecv"})}))}onSignalMessage(e){return i(this,void 0,void 0,(function*(){if(this.peerConnection)switch(e.actionType){case s.SignalMessageAction.ANSWER:const t=e.payload;yield this.peerConnection.setRemoteDescription(t),this.connectionReceivedAnswer=!0,this.flushRemoteIceCandidateBuffer();break;case s.SignalMessageAction.ICE_CANDIDATE:const n=e.payload,i=new RTCIceCandidate(n);this.connectionReceivedAnswer?yield this.peerConnection.addIceCandidate(i):this.remoteIceCandidateBuffer.push(i);break;case s.SignalMessageAction.END_SESSION:const o=e.payload;this.onConnectionClosedCallback&&this.onConnectionClosedCallback(o),this.shutdown();break;case s.SignalMessageAction.WARNING:const a=e.payload;console.warn("Warning received from server: "+a);break;default:console.error("StreamingClient - onSignalMessage: unknown signal message action type",e)}else console.error("StreamingClient - onSignalMessage: peerConnection is not initialized")}))}onSignallingClientConnected(){return i(this,void 0,void 0,(function*(){if(!this.peerConnection)try{yield this.initPeerConnectionAndSendOffer()}catch(e){console.error("StreamingClient - onSignallingClientConnected: Error initializing peer connection",e),this.handleWebrtcFailure(e)}}))}onSignallingClientFailed(){console.error("StreamingClient - onSignallingClientFailed: signalling client failed"),this.onConnectionClosedCallback&&this.onConnectionClosedCallback(o.PUBLIC_MESSAGE_ON_SIGNALLING_CLIENT_CONNECTION_FAILURE)}flushRemoteIceCandidateBuffer(){this.remoteIceCandidateBuffer.forEach((e=>{var t;null===(t=this.peerConnection)||void 0===t||t.addIceCandidate(e)})),this.remoteIceCandidateBuffer=[]}onIceCandidate(e){e.candidate&&this.signallingClient.sendIceCandidate(e.candidate)}onIceConnectionStateChange(){var e,t;"connected"!==(null===(e=this.peerConnection)||void 0===e?void 0:e.iceConnectionState)&&"completed"!==(null===(t=this.peerConnection)||void 0===t?void 0:t.iceConnectionState)||this.onConnectionEstablishedCallback&&this.onConnectionEstablishedCallback()}onConnectionStateChange(){var e;"closed"===(null===(e=this.peerConnection)||void 0===e?void 0:e.connectionState)&&(console.error("StreamingClient - onConnectionStateChange: Connection closed"),this.handleWebrtcFailure("The connection to our servers was lost. Please try again."))}handleWebrtcFailure(e){console.error("StreamingClient - handleWebrtcFailure: ",e);try{this.stopConnection()}catch(e){console.error("StreamingClient - handleWebrtcFailure: error stopping connection",e)}this.onConnectionClosedCallback&&this.onConnectionClosedCallback(o.PUBLIC_MESSAGE_ON_WEBRTC_FAILURE)}onTrackEventHandler(e){if("video"===e.track.kind){if(this.videoStream=e.streams[0],this.onVideoStreamStartCallback&&this.onVideoStreamStartCallback(this.videoStream),this.videoElement){this.videoElement.srcObject=this.videoStream;const e=this.videoElement.requestVideoFrameCallback((()=>{var t;null===(t=this.videoElement)||void 0===t||t.cancelVideoFrameCallback(e),this.onVideoPlayStartedCallback&&this.onVideoPlayStartedCallback()}))}}else"audio"===e.track.kind&&(this.audioStream=e.streams[0],this.onAudioStreamStartCallback&&this.onAudioStreamStartCallback(this.audioStream),this.audioElement&&(this.audioElement.srcObject=this.audioStream))}setupDataChannels(){return i(this,void 0,void 0,(function*(){if(!this.peerConnection)return void console.error("StreamingClient - setupDataChannels: peer connection is not initialized");if(this.inputAudioStream){if(!this.inputAudioStream.getAudioTracks().length)throw new Error("StreamingClient - setupDataChannels: user provided stream does not have audio tracks")}else this.inputAudioStream=yield navigator.mediaDevices.getUserMedia({audio:{echoCancellation:!0}});const e=this.inputAudioStream.getAudioTracks()[0];this.peerConnection.addTrack(e,this.inputAudioStream),this.onInputAudioStreamStartCallback&&this.onInputAudioStreamStartCallback(this.inputAudioStream);const t=this.peerConnection.createDataChannel("chat",{ordered:!0});t.onopen=()=>{this.dataChannel=null!=t?t:null},t.onclose=()=>{},t.onmessage=e=>{this.onReceiveMessageCallback&&this.onReceiveMessageCallback(JSON.parse(e.data))}}))}initPeerConnectionAndSendOffer(){return i(this,void 0,void 0,(function*(){if(yield this.initPeerConnection(),!this.peerConnection)return void console.error("StreamingClient - initPeerConnectionAndSendOffer: peer connection is not initialized");const e=yield this.peerConnection.createOffer();if(yield this.peerConnection.setLocalDescription(e),!this.peerConnection.localDescription)throw new Error("StreamingClient - initPeerConnectionAndSendOffer: local description is null");yield this.signallingClient.sendOffer(this.peerConnection.localDescription)}))}shutdown(){try{this.inputAudioStream&&this.inputAudioStream.getTracks().forEach((e=>{e.stop()})),this.inputAudioStream=null}catch(e){console.error("StreamingClient - shutdown: error stopping input audio stream",e)}try{this.signallingClient.stop()}catch(e){console.error("StreamingClient - shutdown: error stopping signallilng",e)}try{this.peerConnection&&"closed"!==this.peerConnection.connectionState&&(this.peerConnection.onconnectionstatechange=null,this.peerConnection.close(),this.peerConnection=null)}catch(e){console.error("StreamingClient - shutdown: error closing peer connection",e)}}}},170:(e,t,n)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.SignalMessageAction=void 0;var i=n(295);Object.defineProperty(t,"SignalMessageAction",{enumerable:!0,get:function(){return i.SignalMessageAction}})},656:(e,t)=>{var n;Object.defineProperty(t,"__esModule",{value:!0}),t.SignalMessageAction=void 0,function(e){e.OFFER="offer",e.ANSWER="answer",e.ICE_CANDIDATE="icecandidate",e.END_SESSION="endsession",e.HEARTBEAT="heartbeat",e.WARNING="warning"}(n||(t.SignalMessageAction=n={}))},295:(e,t,n)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.SignalMessageAction=void 0;var i=n(656);Object.defineProperty(t,"SignalMessageAction",{enumerable:!0,get:function(){return i.SignalMessageAction}})}},t={};return function n(i){var o=t[i];if(void 0!==o)return o.exports;var s=t[i]={exports:{}};return e[i].call(s.exports,s,s.exports,n),s.exports}(440)})()));
|