@anam-ai/js-sdk 1.4.0-alpha.2 → 1.4.0-alpha.3
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.js +2 -2
- package/dist/main/modules/SignallingClient.d.ts +1 -1
- package/dist/main/modules/SignallingClient.js +1 -1
- package/dist/main/types/TalkMessageStream.d.ts.map +1 -1
- package/dist/main/types/TalkMessageStream.js +5 -5
- package/dist/module/AnamClient.js +2 -2
- package/dist/module/modules/SignallingClient.d.ts +1 -1
- package/dist/module/modules/SignallingClient.js +1 -1
- package/dist/module/types/TalkMessageStream.d.ts.map +1 -1
- package/dist/module/types/TalkMessageStream.js +5 -5
- package/dist/umd/anam.js +1 -1
- package/package.json +1 -1
package/dist/main/AnamClient.js
CHANGED
|
@@ -240,10 +240,10 @@ class AnamClient {
|
|
|
240
240
|
}
|
|
241
241
|
createTalkMessageStream(correlationId) {
|
|
242
242
|
if (!this.streamingClient) {
|
|
243
|
-
throw new Error('Failed to start
|
|
243
|
+
throw new Error('Failed to start talk message stream: session is not started.');
|
|
244
244
|
}
|
|
245
245
|
if (correlationId && correlationId.trim() === '') {
|
|
246
|
-
throw new Error('Failed to start
|
|
246
|
+
throw new Error('Failed to start talk message stream: correlationId is empty');
|
|
247
247
|
}
|
|
248
248
|
return this.streamingClient.startTalkMessageStream(correlationId);
|
|
249
249
|
}
|
|
@@ -19,7 +19,7 @@ export declare class SignallingClient {
|
|
|
19
19
|
sendOffer(localDescription: RTCSessionDescription): Promise<void>;
|
|
20
20
|
sendIceCandidate(candidate: RTCIceCandidate): Promise<void>;
|
|
21
21
|
private sendSignalMessage;
|
|
22
|
-
|
|
22
|
+
sendTalkMessage(payload: TalkMessageStreamPayload): Promise<void>;
|
|
23
23
|
private closeSocket;
|
|
24
24
|
private onOpen;
|
|
25
25
|
private onClose;
|
|
@@ -95,7 +95,7 @@ class SignallingClient {
|
|
|
95
95
|
this.sendingBuffer.push(message);
|
|
96
96
|
}
|
|
97
97
|
}
|
|
98
|
-
|
|
98
|
+
sendTalkMessage(payload) {
|
|
99
99
|
return __awaiter(this, void 0, void 0, function* () {
|
|
100
100
|
const chatMessage = {
|
|
101
101
|
actionType: types_1.SignalMessageAction.TALK_STREAM_INPUT,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TalkMessageStream.d.ts","sourceRoot":"","sources":["../../../src/types/TalkMessageStream.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AAGlE,OAAO,EAAE,oBAAoB,EAAE,MAAM,iCAAiC,CAAC;AACvE,OAAO,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AAE/D,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,oBAAoB,CAAuB;IACnD,OAAO,CAAC,KAAK,CAAoC;IACjD,OAAO,CAAC,aAAa,CAAS;IAC9B,OAAO,CAAC,gBAAgB,
|
|
1
|
+
{"version":3,"file":"TalkMessageStream.d.ts","sourceRoot":"","sources":["../../../src/types/TalkMessageStream.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AAGlE,OAAO,EAAE,oBAAoB,EAAE,MAAM,iCAAiC,CAAC;AACvE,OAAO,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AAE/D,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,oBAAoB,CAAuB;IACnD,OAAO,CAAC,KAAK,CAAoC;IACjD,OAAO,CAAC,aAAa,CAAS;IAC9B,OAAO,CAAC,gBAAgB,CAAmB;gBAGzC,aAAa,EAAE,MAAM,EACrB,oBAAoB,EAAE,oBAAoB,EAC1C,gBAAgB,EAAE,gBAAgB;IAYpC,OAAO,CAAC,YAAY;YAON,eAAe;IAahB,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAwB3B,kBAAkB,CAC7B,cAAc,EAAE,MAAM,EACtB,WAAW,EAAE,OAAO,GACnB,OAAO,CAAC,IAAI,CAAC;IAyBT,gBAAgB,IAAI,MAAM;IAI1B,QAAQ,IAAI,OAAO;IAOnB,QAAQ,IAAI,sBAAsB;CAG1C"}
|
|
@@ -37,11 +37,11 @@ class TalkMessageStream {
|
|
|
37
37
|
endMessage() {
|
|
38
38
|
return __awaiter(this, void 0, void 0, function* () {
|
|
39
39
|
if (this.state === TalkMessageStreamState_1.TalkMessageStreamState.ENDED) {
|
|
40
|
-
console.warn('
|
|
40
|
+
console.warn('Talk stream is already ended via end of speech. No need to call endMessage.');
|
|
41
41
|
return;
|
|
42
42
|
}
|
|
43
43
|
if (this.state !== TalkMessageStreamState_1.TalkMessageStreamState.STREAMING) {
|
|
44
|
-
console.warn('
|
|
44
|
+
console.warn('Talk stream is not active state: ' + this.state);
|
|
45
45
|
return;
|
|
46
46
|
}
|
|
47
47
|
const payload = {
|
|
@@ -50,7 +50,7 @@ class TalkMessageStream {
|
|
|
50
50
|
endOfSpeech: true,
|
|
51
51
|
correlationId: this.correlationId,
|
|
52
52
|
};
|
|
53
|
-
yield this.signallingClient.
|
|
53
|
+
yield this.signallingClient.sendTalkMessage(payload);
|
|
54
54
|
this.state = TalkMessageStreamState_1.TalkMessageStreamState.ENDED;
|
|
55
55
|
this.onDeactivate();
|
|
56
56
|
});
|
|
@@ -60,7 +60,7 @@ class TalkMessageStream {
|
|
|
60
60
|
if (this.state !== TalkMessageStreamState_1.TalkMessageStreamState.STREAMING &&
|
|
61
61
|
this.state !== TalkMessageStreamState_1.TalkMessageStreamState.UNSTARTED) {
|
|
62
62
|
// throw error
|
|
63
|
-
throw new Error('
|
|
63
|
+
throw new Error('Talk stream is not in an active state: ' + this.state);
|
|
64
64
|
}
|
|
65
65
|
const payload = {
|
|
66
66
|
content: partialMessage,
|
|
@@ -75,7 +75,7 @@ class TalkMessageStream {
|
|
|
75
75
|
this.onDeactivate();
|
|
76
76
|
}
|
|
77
77
|
// send message to signalling client
|
|
78
|
-
yield this.signallingClient.
|
|
78
|
+
yield this.signallingClient.sendTalkMessage(payload);
|
|
79
79
|
});
|
|
80
80
|
}
|
|
81
81
|
getCorrelationId() {
|
|
@@ -238,10 +238,10 @@ export default class AnamClient {
|
|
|
238
238
|
}
|
|
239
239
|
createTalkMessageStream(correlationId) {
|
|
240
240
|
if (!this.streamingClient) {
|
|
241
|
-
throw new Error('Failed to start
|
|
241
|
+
throw new Error('Failed to start talk message stream: session is not started.');
|
|
242
242
|
}
|
|
243
243
|
if (correlationId && correlationId.trim() === '') {
|
|
244
|
-
throw new Error('Failed to start
|
|
244
|
+
throw new Error('Failed to start talk message stream: correlationId is empty');
|
|
245
245
|
}
|
|
246
246
|
return this.streamingClient.startTalkMessageStream(correlationId);
|
|
247
247
|
}
|
|
@@ -19,7 +19,7 @@ export declare class SignallingClient {
|
|
|
19
19
|
sendOffer(localDescription: RTCSessionDescription): Promise<void>;
|
|
20
20
|
sendIceCandidate(candidate: RTCIceCandidate): Promise<void>;
|
|
21
21
|
private sendSignalMessage;
|
|
22
|
-
|
|
22
|
+
sendTalkMessage(payload: TalkMessageStreamPayload): Promise<void>;
|
|
23
23
|
private closeSocket;
|
|
24
24
|
private onOpen;
|
|
25
25
|
private onClose;
|
|
@@ -92,7 +92,7 @@ export class SignallingClient {
|
|
|
92
92
|
this.sendingBuffer.push(message);
|
|
93
93
|
}
|
|
94
94
|
}
|
|
95
|
-
|
|
95
|
+
sendTalkMessage(payload) {
|
|
96
96
|
return __awaiter(this, void 0, void 0, function* () {
|
|
97
97
|
const chatMessage = {
|
|
98
98
|
actionType: SignalMessageAction.TALK_STREAM_INPUT,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TalkMessageStream.d.ts","sourceRoot":"","sources":["../../../src/types/TalkMessageStream.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AAGlE,OAAO,EAAE,oBAAoB,EAAE,MAAM,iCAAiC,CAAC;AACvE,OAAO,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AAE/D,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,oBAAoB,CAAuB;IACnD,OAAO,CAAC,KAAK,CAAoC;IACjD,OAAO,CAAC,aAAa,CAAS;IAC9B,OAAO,CAAC,gBAAgB,
|
|
1
|
+
{"version":3,"file":"TalkMessageStream.d.ts","sourceRoot":"","sources":["../../../src/types/TalkMessageStream.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AAGlE,OAAO,EAAE,oBAAoB,EAAE,MAAM,iCAAiC,CAAC;AACvE,OAAO,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AAE/D,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,oBAAoB,CAAuB;IACnD,OAAO,CAAC,KAAK,CAAoC;IACjD,OAAO,CAAC,aAAa,CAAS;IAC9B,OAAO,CAAC,gBAAgB,CAAmB;gBAGzC,aAAa,EAAE,MAAM,EACrB,oBAAoB,EAAE,oBAAoB,EAC1C,gBAAgB,EAAE,gBAAgB;IAYpC,OAAO,CAAC,YAAY;YAON,eAAe;IAahB,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAwB3B,kBAAkB,CAC7B,cAAc,EAAE,MAAM,EACtB,WAAW,EAAE,OAAO,GACnB,OAAO,CAAC,IAAI,CAAC;IAyBT,gBAAgB,IAAI,MAAM;IAI1B,QAAQ,IAAI,OAAO;IAOnB,QAAQ,IAAI,sBAAsB;CAG1C"}
|
|
@@ -34,11 +34,11 @@ export class TalkMessageStream {
|
|
|
34
34
|
endMessage() {
|
|
35
35
|
return __awaiter(this, void 0, void 0, function* () {
|
|
36
36
|
if (this.state === TalkMessageStreamState.ENDED) {
|
|
37
|
-
console.warn('
|
|
37
|
+
console.warn('Talk stream is already ended via end of speech. No need to call endMessage.');
|
|
38
38
|
return;
|
|
39
39
|
}
|
|
40
40
|
if (this.state !== TalkMessageStreamState.STREAMING) {
|
|
41
|
-
console.warn('
|
|
41
|
+
console.warn('Talk stream is not active state: ' + this.state);
|
|
42
42
|
return;
|
|
43
43
|
}
|
|
44
44
|
const payload = {
|
|
@@ -47,7 +47,7 @@ export class TalkMessageStream {
|
|
|
47
47
|
endOfSpeech: true,
|
|
48
48
|
correlationId: this.correlationId,
|
|
49
49
|
};
|
|
50
|
-
yield this.signallingClient.
|
|
50
|
+
yield this.signallingClient.sendTalkMessage(payload);
|
|
51
51
|
this.state = TalkMessageStreamState.ENDED;
|
|
52
52
|
this.onDeactivate();
|
|
53
53
|
});
|
|
@@ -57,7 +57,7 @@ export class TalkMessageStream {
|
|
|
57
57
|
if (this.state !== TalkMessageStreamState.STREAMING &&
|
|
58
58
|
this.state !== TalkMessageStreamState.UNSTARTED) {
|
|
59
59
|
// throw error
|
|
60
|
-
throw new Error('
|
|
60
|
+
throw new Error('Talk stream is not in an active state: ' + this.state);
|
|
61
61
|
}
|
|
62
62
|
const payload = {
|
|
63
63
|
content: partialMessage,
|
|
@@ -72,7 +72,7 @@ export class TalkMessageStream {
|
|
|
72
72
|
this.onDeactivate();
|
|
73
73
|
}
|
|
74
74
|
// send message to signalling client
|
|
75
|
-
yield this.signallingClient.
|
|
75
|
+
yield this.signallingClient.sendTalkMessage(payload);
|
|
76
76
|
});
|
|
77
77
|
}
|
|
78
78
|
getCorrelationId() {
|
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(s,o){function r(e){try{l(i.next(e))}catch(e){o(e)}}function a(e){try{l(i.throw(e))}catch(e){o(e)}}function l(e){var t;e.done?s(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(r,a)}l((i=i.apply(e,t||[])).next())}))};Object.defineProperty(t,"__esModule",{value:!0});const s=n(958),o=n(170);t.default=class{constructor(e,t,n){this.inputAudioState={isMuted:!1},this.sessionId=null,this.streamingClient=null,this._isStreaming=!1;const i=this.validateClientConfig(e,t,n);if(i)throw new Error(i);this.sessionToken=e,this.apiKey=null==n?void 0:n.apiKey,this.personaConfig=t,this.clientOptions=n,this.publicEventEmitter=new s.PublicEventEmitter,this.internalEventEmitter=new s.InternalEventEmitter,this.apiClient=new s.CoreApiRestClient(e,null==n?void 0:n.apiKey,null==n?void 0:n.api),this.messageHistoryClient=new s.MessageHistoryClient(this.publicEventEmitter,this.internalEventEmitter)}validateClientConfig(e,t,n){if(!e&&!(null==n?void 0:n.apiKey))return"Either sessionToken or apiKey must be provided";if((null==n?void 0:n.apiKey)&&e)return"Only one of sessionToken or apiKey should be used";if(!t)return"Persona configuration must be provided";if(""===t.personaId||!t.personaId)return"Persona ID must be provided in persona configuration";if((null==n?void 0:n.voiceDetection)&&void 0!==n.voiceDetection.endOfSpeechSensitivity){if("number"!=typeof n.voiceDetection.endOfSpeechSensitivity)return"End of speech sensitivity must be a number";if(n.voiceDetection.endOfSpeechSensitivity<0||n.voiceDetection.endOfSpeechSensitivity>1)return"End of speech sensitivity must be between 0 and 1"}}buildStartSessionOptionsForClient(){var e;const t={};if((null===(e=this.clientOptions)||void 0===e?void 0:e.voiceDetection)&&(t.voiceDetection=this.clientOptions.voiceDetection),0!==Object.keys(t).length)return t}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=this.buildStartSessionOptionsForClient(),i=yield this.apiClient.startSession(t,n),{sessionId:o,clientConfig:r,engineHost:a,engineProtocol:l,signallingEndpoint:c}=i,{heartbeatIntervalSeconds:d,maxWsReconnectionAttempts:u,iceServers:h}=r;return this.streamingClient=new s.StreamingClient(o,{engine:{baseUrl:`${l}://${a}`},signalling:{heartbeatIntervalSeconds:d,maxWsReconnectionAttempts:u,url:{baseUrl:a,protocol:l,signallingPath:c}},iceServers:h,inputAudio:{inputAudioState:this.inputAudioState,userProvidedMediaStream:e}},this.publicEventEmitter,this.internalEventEmitter),this.sessionId=o,o}catch(e){throw new Error("Failed to start session")}}))}startSessionIfNeeded(e){return i(this,void 0,void 0,(function*(){if(!this.sessionId||!this.streamingClient){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(e){return i(this,void 0,void 0,(function*(){if(yield this.startSessionIfNeeded(e),this._isStreaming)throw new Error("Already streaming");return this._isStreaming=!0,new Promise((e=>{var t;const n=[];let i=!1,s=!1;this.publicEventEmitter.addListener(o.AnamEvent.VIDEO_STREAM_STARTED,(t=>{n.push(t),i=!0,s&&e(n)})),this.publicEventEmitter.addListener(o.AnamEvent.AUDIO_STREAM_STARTED,(t=>{n.push(t),s=!0,i&&e(n)})),null===(t=this.streamingClient)||void 0===t||t.startConnection()}))}))}streamToVideoAndAudioElements(e,t,n){return i(this,void 0,void 0,(function*(){if(yield this.startSessionIfNeeded(n),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()}))}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}getInputAudioState(){return this.streamingClient&&(this.inputAudioState=this.streamingClient.getInputAudioState()),this.inputAudioState}muteInputAudio(){return this.streamingClient?this.inputAudioState=this.streamingClient.muteInputAudio():this.inputAudioState=Object.assign(Object.assign({},this.inputAudioState),{isMuted:!0}),this.inputAudioState}unmuteInputAudio(){return this.streamingClient?this.inputAudioState=this.streamingClient.unmuteInputAudio():this.inputAudioState=Object.assign(Object.assign({},this.inputAudioState),{isMuted:!1}),this.inputAudioState}createTalkMessageStream(e){if(!this.streamingClient)throw new Error("Failed to start chat message stream: session is not started.");if(e&&""===e.trim())throw new Error("Failed to start chat message stream: correlationId is empty");return this.streamingClient.startTalkMessageStream(e)}addListener(e,t){this.publicEventEmitter.addListener(e,t)}removeListener(e,t){this.publicEventEmitter.removeListener(e,t)}}},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 s=i(n(816));t.createClient=(e,t,n)=>new s.default(e,t,n),t.unsafe_createClientWithApiKey=(e,t,n)=>new s.default(void 0,t,Object.assign(Object.assign({},n),{apiKey:e}))},985:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.PUBLIC_MESSAGE_ON_WEBRTC_FAILURE=t.PUBLIC_MESSAGE_ON_SIGNALLING_CLIENT_CONNECTION_FAILURE=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.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."},18:function(e,t,n){var i=this&&this.__awaiter||function(e,t,n,i){return new(n||(n=Promise))((function(s,o){function r(e){try{l(i.next(e))}catch(e){o(e)}}function a(e){try{l(i.throw(e))}catch(e){o(e)}}function l(e){var t;e.done?s(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(r,a)}l((i=i.apply(e,t||[])).next())}))};Object.defineProperty(t,"__esModule",{value:!0}),t.CoreApiRestClient=void 0;const s=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)||s.DEFAULT_API_BASE_URL,this.apiVersion=(null==n?void 0:n.apiVersion)||s.DEFAULT_API_VERSION}startSession(e,t){return i(this,void 0,void 0,(function*(){this.sessionToken||(this.sessionToken=yield this.unsafe_getSessionToken());try{const n=yield fetch(`${this.getApiUrl()}/engine/session`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.sessionToken}`},body:JSON.stringify({personaConfig:e,sessionOptions:t})});return yield n.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(s,o){function r(e){try{l(i.next(e))}catch(e){o(e)}}function a(e){try{l(i.throw(e))}catch(e){o(e)}}function l(e){var t;e.done?s(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(r,a)}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")}}))}}},259:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.InternalEventEmitter=void 0,t.InternalEventEmitter=class{constructor(){this.listeners={}}addListener(e,t){this.listeners[e]||(this.listeners[e]=new Set),this.listeners[e].add(t)}removeListener(e,t){this.listeners[e]&&this.listeners[e].delete(t)}emit(e,...t){this.listeners[e]&&this.listeners[e].forEach((e=>{e(...t)}))}}},462:(e,t,n)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.MessageHistoryClient=void 0;const i=n(170);t.MessageHistoryClient=class{constructor(e,t){this.messages=[],this.publicEventEmitter=e,this.internalEventEmitter=t,this.internalEventEmitter.addListener(i.InternalEvent.WEBRTC_CHAT_MESSAGE_RECEIVED,this.processWebRtcTextMessageEvent.bind(this))}webRtcTextMessageEventToMessageStreamEvent(e){return{id:`${e.role}::${e.message_id}`,content:e.content,role:e.role,endOfSpeech:e.end_of_speech,interrupted:e.interrupted}}processUserMessage(e){const t={id:e.id,content:e.content,role:e.role};this.messages.push(t)}processPersonaMessage(e){const t={id:e.id,content:e.content,role:e.role,interrupted:e.interrupted},n=this.messages.findIndex((e=>e.id===t.id));if(-1!==n){const e=this.messages[n];this.messages[n]=Object.assign(Object.assign({},e),{content:e.content+t.content,interrupted:e.interrupted||t.interrupted})}else this.messages.push(t)}processWebRtcTextMessageEvent(e){const t=this.webRtcTextMessageEventToMessageStreamEvent(e);switch(this.publicEventEmitter.emit(i.AnamEvent.MESSAGE_STREAM_EVENT_RECEIVED,t),t.role){case i.MessageRole.USER:this.processUserMessage(t);break;case i.MessageRole.PERSONA:this.processPersonaMessage(t)}t.endOfSpeech&&this.publicEventEmitter.emit(i.AnamEvent.MESSAGE_HISTORY_UPDATED,this.messages)}}},995:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.PublicEventEmitter=void 0,t.PublicEventEmitter=class{constructor(){this.listeners={}}addListener(e,t){this.listeners[e]||(this.listeners[e]=new Set),this.listeners[e].add(t)}removeListener(e,t){this.listeners[e]&&this.listeners[e].delete(t)}emit(e,...t){this.listeners[e]&&this.listeners[e].forEach((e=>{e(...t)}))}}},279:function(e,t,n){var i=this&&this.__awaiter||function(e,t,n,i){return new(n||(n=Promise))((function(s,o){function r(e){try{l(i.next(e))}catch(e){o(e)}}function a(e){try{l(i.throw(e))}catch(e){o(e)}}function l(e){var t;e.done?s(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(r,a)}l((i=i.apply(e,t||[])).next())}))};Object.defineProperty(t,"__esModule",{value:!0}),t.SignallingClient=void 0;const s=n(985),o=n(170);t.SignallingClient=class{constructor(e,t,n,i){var s;if(this.stopSignal=!1,this.sendingBuffer=[],this.wsConnectionAttempts=0,this.socket=null,this.heartBeatIntervalRef=null,this.publicEventEmitter=n,this.internalEventEmitter=i,!e)throw new Error("Signalling Client: sessionId is required");this.sessionId=e;const{heartbeatIntervalSeconds:o,maxWsReconnectionAttempts:r,url:a}=t;if(this.heartbeatIntervalSeconds=o||5,this.maxWsReconnectionAttempts=r||5,!a.baseUrl)throw new Error("Signalling Client: baseUrl is required");const l=`${a.protocol||"https"}://${a.baseUrl}`;this.url=new URL(l),this.url.protocol="http"===a.protocol?"ws:":"wss:",a.port&&(this.url.port=a.port),this.url.pathname=null!==(s=a.signallingPath)&&void 0!==s?s:"/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:o.SignalMessageAction.OFFER,sessionId:this.sessionId,payload:t};this.sendSignalMessage(n)}))}sendIceCandidate(e){return i(this,void 0,void 0,(function*(){const t={actionType:o.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)}sendChatMessage(e){return i(this,void 0,void 0,(function*(){const t={actionType:o.SignalMessageAction.TALK_STREAM_INPUT,sessionId:this.sessionId,payload:e};this.sendSignalMessage(t)}))}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.internalEventEmitter.emit(o.InternalEvent.WEB_SOCKET_OPEN)}catch(e){console.error("SignallingClient - onOpen: error in onOpen",e),this.publicEventEmitter.emit(o.AnamEvent.CONNECTION_CLOSED,s.PUBLIC_MESSAGE_ON_SIGNALLING_CLIENT_CONNECTION_FAILURE)}}))}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.publicEventEmitter.emit(o.AnamEvent.CONNECTION_CLOSED,s.PUBLIC_MESSAGE_ON_SIGNALLING_CLIENT_CONNECTION_FAILURE)))}))}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.internalEventEmitter.emit(o.InternalEvent.SIGNAL_MESSAGE_RECEIVED,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:o.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(s,o){function r(e){try{l(i.next(e))}catch(e){o(e)}}function a(e){try{l(i.throw(e))}catch(e){o(e)}}function l(e){var t;e.done?s(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(r,a)}l((i=i.apply(e,t||[])).next())}))};Object.defineProperty(t,"__esModule",{value:!0}),t.StreamingClient=void 0;const s=n(985),o=n(170),r=n(958),a=n(711);t.StreamingClient=class{constructor(e,t,n,i){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.inputAudioState={isMuted:!1},this.publicEventEmitter=n,this.internalEventEmitter=i;const{inputAudio:s}=t;this.inputAudioState=s.inputAudioState,t.inputAudio.userProvidedMediaStream&&(this.inputAudioStream=t.inputAudio.userProvidedMediaStream),this.internalEventEmitter.addListener(o.InternalEvent.WEB_SOCKET_OPEN,this.onSignallingClientConnected.bind(this)),this.internalEventEmitter.addListener(o.InternalEvent.SIGNAL_MESSAGE_RECEIVED,this.onSignalMessage.bind(this)),this.iceServers=t.iceServers,this.signallingClient=new r.SignallingClient(e,t.signalling,this.publicEventEmitter,this.internalEventEmitter),this.engineApiRestClient=new r.EngineApiRestClient(t.engine.baseUrl,e)}onInputAudioStateChange(e,t){e.isMuted!==t.isMuted&&(t.isMuted?this.muteAllAudioTracks():this.unmuteAllAudioTracks())}muteAllAudioTracks(){var e;null===(e=this.inputAudioStream)||void 0===e||e.getAudioTracks().forEach((e=>{e.enabled=!1}))}unmuteAllAudioTracks(){var e;null===(e=this.inputAudioStream)||void 0===e||e.getAudioTracks().forEach((e=>{e.enabled=!0}))}muteInputAudio(){const e=this.inputAudioState,t=Object.assign(Object.assign({},this.inputAudioState),{isMuted:!0});return this.inputAudioState=t,this.onInputAudioStateChange(e,t),this.inputAudioState}unmuteInputAudio(){const e=this.inputAudioState,t=Object.assign(Object.assign({},this.inputAudioState),{isMuted:!1});return this.inputAudioState=t,this.onInputAudioStateChange(e,t),this.inputAudioState}getInputAudioState(){return this.inputAudioState}getPeerConnection(){return this.peerConnection}getInputAudioStream(){return this.inputAudioStream}getVideoStream(){return this.videoStream}getAudioStream(){return this.audioStream}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(){try{if(this.peerConnection)return void console.error("StreamingClient - startConnection: peer connection already exists");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)}))}startTalkMessageStream(e){return e||(e=Math.random().toString(36).substring(2,15)),new a.TalkMessageStream(e,this.internalEventEmitter,this.signallingClient)}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 o.SignalMessageAction.ANSWER:const t=e.payload;yield this.peerConnection.setRemoteDescription(t),this.connectionReceivedAnswer=!0,this.flushRemoteIceCandidateBuffer();break;case o.SignalMessageAction.ICE_CANDIDATE:const n=e.payload,i=new RTCIceCandidate(n);this.connectionReceivedAnswer?yield this.peerConnection.addIceCandidate(i):this.remoteIceCandidateBuffer.push(i);break;case o.SignalMessageAction.END_SESSION:const s=e.payload;this.publicEventEmitter.emit(o.AnamEvent.CONNECTION_CLOSED,s),this.shutdown();break;case o.SignalMessageAction.WARNING:const r=e.payload;console.warn("Warning received from server: "+r);break;case o.SignalMessageAction.TALK_STREAM_INTERRUPTED:const a=e.payload;this.publicEventEmitter.emit(o.AnamEvent.TALK_STREAM_INTERRUPTED,a.correlationId);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)}}))}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.publicEventEmitter.emit(o.AnamEvent.CONNECTION_ESTABLISHED)}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.publicEventEmitter.emit(o.AnamEvent.CONNECTION_CLOSED,s.PUBLIC_MESSAGE_ON_WEBRTC_FAILURE)}onTrackEventHandler(e){if("video"===e.track.kind){if(this.videoStream=e.streams[0],this.publicEventEmitter.emit(o.AnamEvent.VIDEO_STREAM_STARTED,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.publicEventEmitter.emit(o.AnamEvent.VIDEO_PLAY_STARTED)}))}}else"audio"===e.track.kind&&(this.audioStream=e.streams[0],this.publicEventEmitter.emit(o.AnamEvent.AUDIO_STREAM_STARTED,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}});this.inputAudioState.isMuted&&this.muteAllAudioTracks();const e=this.inputAudioStream.getAudioTracks()[0];this.peerConnection.addTrack(e,this.inputAudioStream),this.publicEventEmitter.emit(o.AnamEvent.INPUT_AUDIO_STREAM_STARTED,this.inputAudioStream);const t=this.peerConnection.createDataChannel("chat",{ordered:!0});t.onopen=()=>{this.dataChannel=null!=t?t:null},t.onclose=()=>{},t.onmessage=e=>{const t=JSON.parse(e.data);this.internalEventEmitter.emit(o.InternalEvent.WEBRTC_CHAT_MESSAGE_RECEIVED,t)}}))}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)}}}},958:(e,t,n)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.StreamingClient=t.PublicEventEmitter=t.MessageHistoryClient=t.InternalEventEmitter=t.EngineApiRestClient=t.CoreApiRestClient=t.SignallingClient=void 0;var i=n(279);Object.defineProperty(t,"SignallingClient",{enumerable:!0,get:function(){return i.SignallingClient}});var s=n(18);Object.defineProperty(t,"CoreApiRestClient",{enumerable:!0,get:function(){return s.CoreApiRestClient}});var o=n(529);Object.defineProperty(t,"EngineApiRestClient",{enumerable:!0,get:function(){return o.EngineApiRestClient}});var r=n(259);Object.defineProperty(t,"InternalEventEmitter",{enumerable:!0,get:function(){return r.InternalEventEmitter}});var a=n(462);Object.defineProperty(t,"MessageHistoryClient",{enumerable:!0,get:function(){return a.MessageHistoryClient}});var l=n(995);Object.defineProperty(t,"PublicEventEmitter",{enumerable:!0,get:function(){return l.PublicEventEmitter}});var c=n(577);Object.defineProperty(t,"StreamingClient",{enumerable:!0,get:function(){return c.StreamingClient}})},711:function(e,t,n){var i=this&&this.__awaiter||function(e,t,n,i){return new(n||(n=Promise))((function(s,o){function r(e){try{l(i.next(e))}catch(e){o(e)}}function a(e){try{l(i.throw(e))}catch(e){o(e)}}function l(e){var t;e.done?s(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(r,a)}l((i=i.apply(e,t||[])).next())}))};Object.defineProperty(t,"__esModule",{value:!0}),t.TalkMessageStream=void 0;const s=n(170),o=n(322);t.TalkMessageStream=class{constructor(e,t,n){this.state=o.TalkMessageStreamState.UNSTARTED,this.correlationId=e,this.internalEventEmitter=t,this.signallingClient=n,this.internalEventEmitter.addListener(s.InternalEvent.SIGNAL_MESSAGE_RECEIVED,this.onSignalMessage.bind(this))}onDeactivate(){this.internalEventEmitter.removeListener(s.InternalEvent.SIGNAL_MESSAGE_RECEIVED,this.onSignalMessage.bind(this))}onSignalMessage(e){return i(this,void 0,void 0,(function*(){e.actionType===s.SignalMessageAction.TALK_STREAM_INTERRUPTED&&e.payload.correlationId===this.correlationId&&(this.state=o.TalkMessageStreamState.INTERRUPTED,this.onDeactivate())}))}endMessage(){return i(this,void 0,void 0,(function*(){if(this.state===o.TalkMessageStreamState.ENDED)return void console.warn("Chat stream is already ended via end of speech. No need to call endMessage.");if(this.state!==o.TalkMessageStreamState.STREAMING)return void console.warn("Chat stream is not active state: "+this.state);const e={content:"",startOfSpeech:!1,endOfSpeech:!0,correlationId:this.correlationId};yield this.signallingClient.sendChatMessage(e),this.state=o.TalkMessageStreamState.ENDED,this.onDeactivate()}))}streamMessageChunk(e,t){return i(this,void 0,void 0,(function*(){if(this.state!==o.TalkMessageStreamState.STREAMING&&this.state!==o.TalkMessageStreamState.UNSTARTED)throw new Error("Chat stream is not in an active state: "+this.state);const n={content:e,startOfSpeech:this.state===o.TalkMessageStreamState.UNSTARTED,endOfSpeech:t,correlationId:this.correlationId};this.state=t?o.TalkMessageStreamState.ENDED:o.TalkMessageStreamState.STREAMING,this.state===o.TalkMessageStreamState.ENDED&&this.onDeactivate(),yield this.signallingClient.sendChatMessage(n)}))}getCorrelationId(){return this.correlationId}isActive(){return this.state===o.TalkMessageStreamState.STREAMING||this.state===o.TalkMessageStreamState.UNSTARTED}getState(){return this.state}}},322:(e,t)=>{var n;Object.defineProperty(t,"__esModule",{value:!0}),t.TalkMessageStreamState=void 0,function(e){e[e.UNSTARTED=0]="UNSTARTED",e[e.STREAMING=1]="STREAMING",e[e.INTERRUPTED=2]="INTERRUPTED",e[e.ENDED=3]="ENDED"}(n||(t.TalkMessageStreamState=n={}))},878:(e,t,n)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.InternalEvent=t.AnamEvent=void 0;var i=n(21);Object.defineProperty(t,"AnamEvent",{enumerable:!0,get:function(){return i.AnamEvent}});var s=n(645);Object.defineProperty(t,"InternalEvent",{enumerable:!0,get:function(){return s.InternalEvent}})},645:(e,t)=>{var n;Object.defineProperty(t,"__esModule",{value:!0}),t.InternalEvent=void 0,function(e){e.WEB_SOCKET_OPEN="WEB_SOCKET_OPEN",e.SIGNAL_MESSAGE_RECEIVED="SIGNAL_MESSAGE_RECEIVED",e.WEBRTC_CHAT_MESSAGE_RECEIVED="WEBRTC_CHAT_MESSAGE_RECEIVED"}(n||(t.InternalEvent=n={}))},21:(e,t)=>{var n;Object.defineProperty(t,"__esModule",{value:!0}),t.AnamEvent=void 0,function(e){e.MESSAGE_HISTORY_UPDATED="MESSAGE_HISTORY_UPDATED",e.MESSAGE_STREAM_EVENT_RECEIVED="MESSAGE_STREAM_EVENT_RECEIVED",e.CONNECTION_ESTABLISHED="CONNECTION_ESTABLISHED",e.CONNECTION_CLOSED="CONNECTION_CLOSED",e.INPUT_AUDIO_STREAM_STARTED="INPUT_AUDIO_STREAM_STARTED",e.VIDEO_STREAM_STARTED="VIDEO_STREAM_STARTED",e.VIDEO_PLAY_STARTED="VIDEO_PLAY_STARTED",e.AUDIO_STREAM_STARTED="AUDIO_STREAM_STARTED",e.TALK_STREAM_INTERRUPTED="TALK_STREAM_INTERRUPTED"}(n||(t.AnamEvent=n={}))},170:(e,t,n)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.InternalEvent=t.AnamEvent=t.MessageRole=t.SignalMessageAction=void 0;var i=n(295);Object.defineProperty(t,"SignalMessageAction",{enumerable:!0,get:function(){return i.SignalMessageAction}});var s=n(822);Object.defineProperty(t,"MessageRole",{enumerable:!0,get:function(){return s.MessageRole}});var o=n(878);Object.defineProperty(t,"AnamEvent",{enumerable:!0,get:function(){return o.AnamEvent}});var r=n(878);Object.defineProperty(t,"InternalEvent",{enumerable:!0,get:function(){return r.InternalEvent}})},273:(e,t)=>{var n;Object.defineProperty(t,"__esModule",{value:!0}),t.MessageRole=void 0,function(e){e.USER="user",e.PERSONA="persona"}(n||(t.MessageRole=n={}))},822:(e,t,n)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.MessageRole=void 0;var i=n(273);Object.defineProperty(t,"MessageRole",{enumerable:!0,get:function(){return i.MessageRole}})},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",e.TALK_STREAM_INTERRUPTED="talkinputstreaminterrupted",e.TALK_STREAM_INPUT="talkstream"}(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 s=t[i];if(void 0!==s)return s.exports;var o=t[i]={exports:{}};return e[i].call(o.exports,o,o.exports,n),o.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(s,o){function r(e){try{l(i.next(e))}catch(e){o(e)}}function a(e){try{l(i.throw(e))}catch(e){o(e)}}function l(e){var t;e.done?s(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(r,a)}l((i=i.apply(e,t||[])).next())}))};Object.defineProperty(t,"__esModule",{value:!0});const s=n(958),o=n(170);t.default=class{constructor(e,t,n){this.inputAudioState={isMuted:!1},this.sessionId=null,this.streamingClient=null,this._isStreaming=!1;const i=this.validateClientConfig(e,t,n);if(i)throw new Error(i);this.sessionToken=e,this.apiKey=null==n?void 0:n.apiKey,this.personaConfig=t,this.clientOptions=n,this.publicEventEmitter=new s.PublicEventEmitter,this.internalEventEmitter=new s.InternalEventEmitter,this.apiClient=new s.CoreApiRestClient(e,null==n?void 0:n.apiKey,null==n?void 0:n.api),this.messageHistoryClient=new s.MessageHistoryClient(this.publicEventEmitter,this.internalEventEmitter)}validateClientConfig(e,t,n){if(!e&&!(null==n?void 0:n.apiKey))return"Either sessionToken or apiKey must be provided";if((null==n?void 0:n.apiKey)&&e)return"Only one of sessionToken or apiKey should be used";if(!t)return"Persona configuration must be provided";if(""===t.personaId||!t.personaId)return"Persona ID must be provided in persona configuration";if((null==n?void 0:n.voiceDetection)&&void 0!==n.voiceDetection.endOfSpeechSensitivity){if("number"!=typeof n.voiceDetection.endOfSpeechSensitivity)return"End of speech sensitivity must be a number";if(n.voiceDetection.endOfSpeechSensitivity<0||n.voiceDetection.endOfSpeechSensitivity>1)return"End of speech sensitivity must be between 0 and 1"}}buildStartSessionOptionsForClient(){var e;const t={};if((null===(e=this.clientOptions)||void 0===e?void 0:e.voiceDetection)&&(t.voiceDetection=this.clientOptions.voiceDetection),0!==Object.keys(t).length)return t}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=this.buildStartSessionOptionsForClient(),i=yield this.apiClient.startSession(t,n),{sessionId:o,clientConfig:r,engineHost:a,engineProtocol:l,signallingEndpoint:c}=i,{heartbeatIntervalSeconds:d,maxWsReconnectionAttempts:u,iceServers:h}=r;return this.streamingClient=new s.StreamingClient(o,{engine:{baseUrl:`${l}://${a}`},signalling:{heartbeatIntervalSeconds:d,maxWsReconnectionAttempts:u,url:{baseUrl:a,protocol:l,signallingPath:c}},iceServers:h,inputAudio:{inputAudioState:this.inputAudioState,userProvidedMediaStream:e}},this.publicEventEmitter,this.internalEventEmitter),this.sessionId=o,o}catch(e){throw new Error("Failed to start session")}}))}startSessionIfNeeded(e){return i(this,void 0,void 0,(function*(){if(!this.sessionId||!this.streamingClient){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(e){return i(this,void 0,void 0,(function*(){if(yield this.startSessionIfNeeded(e),this._isStreaming)throw new Error("Already streaming");return this._isStreaming=!0,new Promise((e=>{var t;const n=[];let i=!1,s=!1;this.publicEventEmitter.addListener(o.AnamEvent.VIDEO_STREAM_STARTED,(t=>{n.push(t),i=!0,s&&e(n)})),this.publicEventEmitter.addListener(o.AnamEvent.AUDIO_STREAM_STARTED,(t=>{n.push(t),s=!0,i&&e(n)})),null===(t=this.streamingClient)||void 0===t||t.startConnection()}))}))}streamToVideoAndAudioElements(e,t,n){return i(this,void 0,void 0,(function*(){if(yield this.startSessionIfNeeded(n),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()}))}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}getInputAudioState(){return this.streamingClient&&(this.inputAudioState=this.streamingClient.getInputAudioState()),this.inputAudioState}muteInputAudio(){return this.streamingClient?this.inputAudioState=this.streamingClient.muteInputAudio():this.inputAudioState=Object.assign(Object.assign({},this.inputAudioState),{isMuted:!0}),this.inputAudioState}unmuteInputAudio(){return this.streamingClient?this.inputAudioState=this.streamingClient.unmuteInputAudio():this.inputAudioState=Object.assign(Object.assign({},this.inputAudioState),{isMuted:!1}),this.inputAudioState}createTalkMessageStream(e){if(!this.streamingClient)throw new Error("Failed to start talk message stream: session is not started.");if(e&&""===e.trim())throw new Error("Failed to start talk message stream: correlationId is empty");return this.streamingClient.startTalkMessageStream(e)}addListener(e,t){this.publicEventEmitter.addListener(e,t)}removeListener(e,t){this.publicEventEmitter.removeListener(e,t)}}},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 s=i(n(816));t.createClient=(e,t,n)=>new s.default(e,t,n),t.unsafe_createClientWithApiKey=(e,t,n)=>new s.default(void 0,t,Object.assign(Object.assign({},n),{apiKey:e}))},985:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.PUBLIC_MESSAGE_ON_WEBRTC_FAILURE=t.PUBLIC_MESSAGE_ON_SIGNALLING_CLIENT_CONNECTION_FAILURE=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.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."},18:function(e,t,n){var i=this&&this.__awaiter||function(e,t,n,i){return new(n||(n=Promise))((function(s,o){function r(e){try{l(i.next(e))}catch(e){o(e)}}function a(e){try{l(i.throw(e))}catch(e){o(e)}}function l(e){var t;e.done?s(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(r,a)}l((i=i.apply(e,t||[])).next())}))};Object.defineProperty(t,"__esModule",{value:!0}),t.CoreApiRestClient=void 0;const s=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)||s.DEFAULT_API_BASE_URL,this.apiVersion=(null==n?void 0:n.apiVersion)||s.DEFAULT_API_VERSION}startSession(e,t){return i(this,void 0,void 0,(function*(){this.sessionToken||(this.sessionToken=yield this.unsafe_getSessionToken());try{const n=yield fetch(`${this.getApiUrl()}/engine/session`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.sessionToken}`},body:JSON.stringify({personaConfig:e,sessionOptions:t})});return yield n.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(s,o){function r(e){try{l(i.next(e))}catch(e){o(e)}}function a(e){try{l(i.throw(e))}catch(e){o(e)}}function l(e){var t;e.done?s(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(r,a)}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")}}))}}},259:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.InternalEventEmitter=void 0,t.InternalEventEmitter=class{constructor(){this.listeners={}}addListener(e,t){this.listeners[e]||(this.listeners[e]=new Set),this.listeners[e].add(t)}removeListener(e,t){this.listeners[e]&&this.listeners[e].delete(t)}emit(e,...t){this.listeners[e]&&this.listeners[e].forEach((e=>{e(...t)}))}}},462:(e,t,n)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.MessageHistoryClient=void 0;const i=n(170);t.MessageHistoryClient=class{constructor(e,t){this.messages=[],this.publicEventEmitter=e,this.internalEventEmitter=t,this.internalEventEmitter.addListener(i.InternalEvent.WEBRTC_CHAT_MESSAGE_RECEIVED,this.processWebRtcTextMessageEvent.bind(this))}webRtcTextMessageEventToMessageStreamEvent(e){return{id:`${e.role}::${e.message_id}`,content:e.content,role:e.role,endOfSpeech:e.end_of_speech,interrupted:e.interrupted}}processUserMessage(e){const t={id:e.id,content:e.content,role:e.role};this.messages.push(t)}processPersonaMessage(e){const t={id:e.id,content:e.content,role:e.role,interrupted:e.interrupted},n=this.messages.findIndex((e=>e.id===t.id));if(-1!==n){const e=this.messages[n];this.messages[n]=Object.assign(Object.assign({},e),{content:e.content+t.content,interrupted:e.interrupted||t.interrupted})}else this.messages.push(t)}processWebRtcTextMessageEvent(e){const t=this.webRtcTextMessageEventToMessageStreamEvent(e);switch(this.publicEventEmitter.emit(i.AnamEvent.MESSAGE_STREAM_EVENT_RECEIVED,t),t.role){case i.MessageRole.USER:this.processUserMessage(t);break;case i.MessageRole.PERSONA:this.processPersonaMessage(t)}t.endOfSpeech&&this.publicEventEmitter.emit(i.AnamEvent.MESSAGE_HISTORY_UPDATED,this.messages)}}},995:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.PublicEventEmitter=void 0,t.PublicEventEmitter=class{constructor(){this.listeners={}}addListener(e,t){this.listeners[e]||(this.listeners[e]=new Set),this.listeners[e].add(t)}removeListener(e,t){this.listeners[e]&&this.listeners[e].delete(t)}emit(e,...t){this.listeners[e]&&this.listeners[e].forEach((e=>{e(...t)}))}}},279:function(e,t,n){var i=this&&this.__awaiter||function(e,t,n,i){return new(n||(n=Promise))((function(s,o){function r(e){try{l(i.next(e))}catch(e){o(e)}}function a(e){try{l(i.throw(e))}catch(e){o(e)}}function l(e){var t;e.done?s(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(r,a)}l((i=i.apply(e,t||[])).next())}))};Object.defineProperty(t,"__esModule",{value:!0}),t.SignallingClient=void 0;const s=n(985),o=n(170);t.SignallingClient=class{constructor(e,t,n,i){var s;if(this.stopSignal=!1,this.sendingBuffer=[],this.wsConnectionAttempts=0,this.socket=null,this.heartBeatIntervalRef=null,this.publicEventEmitter=n,this.internalEventEmitter=i,!e)throw new Error("Signalling Client: sessionId is required");this.sessionId=e;const{heartbeatIntervalSeconds:o,maxWsReconnectionAttempts:r,url:a}=t;if(this.heartbeatIntervalSeconds=o||5,this.maxWsReconnectionAttempts=r||5,!a.baseUrl)throw new Error("Signalling Client: baseUrl is required");const l=`${a.protocol||"https"}://${a.baseUrl}`;this.url=new URL(l),this.url.protocol="http"===a.protocol?"ws:":"wss:",a.port&&(this.url.port=a.port),this.url.pathname=null!==(s=a.signallingPath)&&void 0!==s?s:"/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:o.SignalMessageAction.OFFER,sessionId:this.sessionId,payload:t};this.sendSignalMessage(n)}))}sendIceCandidate(e){return i(this,void 0,void 0,(function*(){const t={actionType:o.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)}sendTalkMessage(e){return i(this,void 0,void 0,(function*(){const t={actionType:o.SignalMessageAction.TALK_STREAM_INPUT,sessionId:this.sessionId,payload:e};this.sendSignalMessage(t)}))}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.internalEventEmitter.emit(o.InternalEvent.WEB_SOCKET_OPEN)}catch(e){console.error("SignallingClient - onOpen: error in onOpen",e),this.publicEventEmitter.emit(o.AnamEvent.CONNECTION_CLOSED,s.PUBLIC_MESSAGE_ON_SIGNALLING_CLIENT_CONNECTION_FAILURE)}}))}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.publicEventEmitter.emit(o.AnamEvent.CONNECTION_CLOSED,s.PUBLIC_MESSAGE_ON_SIGNALLING_CLIENT_CONNECTION_FAILURE)))}))}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.internalEventEmitter.emit(o.InternalEvent.SIGNAL_MESSAGE_RECEIVED,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:o.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(s,o){function r(e){try{l(i.next(e))}catch(e){o(e)}}function a(e){try{l(i.throw(e))}catch(e){o(e)}}function l(e){var t;e.done?s(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(r,a)}l((i=i.apply(e,t||[])).next())}))};Object.defineProperty(t,"__esModule",{value:!0}),t.StreamingClient=void 0;const s=n(985),o=n(170),r=n(958),a=n(711);t.StreamingClient=class{constructor(e,t,n,i){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.inputAudioState={isMuted:!1},this.publicEventEmitter=n,this.internalEventEmitter=i;const{inputAudio:s}=t;this.inputAudioState=s.inputAudioState,t.inputAudio.userProvidedMediaStream&&(this.inputAudioStream=t.inputAudio.userProvidedMediaStream),this.internalEventEmitter.addListener(o.InternalEvent.WEB_SOCKET_OPEN,this.onSignallingClientConnected.bind(this)),this.internalEventEmitter.addListener(o.InternalEvent.SIGNAL_MESSAGE_RECEIVED,this.onSignalMessage.bind(this)),this.iceServers=t.iceServers,this.signallingClient=new r.SignallingClient(e,t.signalling,this.publicEventEmitter,this.internalEventEmitter),this.engineApiRestClient=new r.EngineApiRestClient(t.engine.baseUrl,e)}onInputAudioStateChange(e,t){e.isMuted!==t.isMuted&&(t.isMuted?this.muteAllAudioTracks():this.unmuteAllAudioTracks())}muteAllAudioTracks(){var e;null===(e=this.inputAudioStream)||void 0===e||e.getAudioTracks().forEach((e=>{e.enabled=!1}))}unmuteAllAudioTracks(){var e;null===(e=this.inputAudioStream)||void 0===e||e.getAudioTracks().forEach((e=>{e.enabled=!0}))}muteInputAudio(){const e=this.inputAudioState,t=Object.assign(Object.assign({},this.inputAudioState),{isMuted:!0});return this.inputAudioState=t,this.onInputAudioStateChange(e,t),this.inputAudioState}unmuteInputAudio(){const e=this.inputAudioState,t=Object.assign(Object.assign({},this.inputAudioState),{isMuted:!1});return this.inputAudioState=t,this.onInputAudioStateChange(e,t),this.inputAudioState}getInputAudioState(){return this.inputAudioState}getPeerConnection(){return this.peerConnection}getInputAudioStream(){return this.inputAudioStream}getVideoStream(){return this.videoStream}getAudioStream(){return this.audioStream}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(){try{if(this.peerConnection)return void console.error("StreamingClient - startConnection: peer connection already exists");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)}))}startTalkMessageStream(e){return e||(e=Math.random().toString(36).substring(2,15)),new a.TalkMessageStream(e,this.internalEventEmitter,this.signallingClient)}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 o.SignalMessageAction.ANSWER:const t=e.payload;yield this.peerConnection.setRemoteDescription(t),this.connectionReceivedAnswer=!0,this.flushRemoteIceCandidateBuffer();break;case o.SignalMessageAction.ICE_CANDIDATE:const n=e.payload,i=new RTCIceCandidate(n);this.connectionReceivedAnswer?yield this.peerConnection.addIceCandidate(i):this.remoteIceCandidateBuffer.push(i);break;case o.SignalMessageAction.END_SESSION:const s=e.payload;this.publicEventEmitter.emit(o.AnamEvent.CONNECTION_CLOSED,s),this.shutdown();break;case o.SignalMessageAction.WARNING:const r=e.payload;console.warn("Warning received from server: "+r);break;case o.SignalMessageAction.TALK_STREAM_INTERRUPTED:const a=e.payload;this.publicEventEmitter.emit(o.AnamEvent.TALK_STREAM_INTERRUPTED,a.correlationId);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)}}))}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.publicEventEmitter.emit(o.AnamEvent.CONNECTION_ESTABLISHED)}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.publicEventEmitter.emit(o.AnamEvent.CONNECTION_CLOSED,s.PUBLIC_MESSAGE_ON_WEBRTC_FAILURE)}onTrackEventHandler(e){if("video"===e.track.kind){if(this.videoStream=e.streams[0],this.publicEventEmitter.emit(o.AnamEvent.VIDEO_STREAM_STARTED,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.publicEventEmitter.emit(o.AnamEvent.VIDEO_PLAY_STARTED)}))}}else"audio"===e.track.kind&&(this.audioStream=e.streams[0],this.publicEventEmitter.emit(o.AnamEvent.AUDIO_STREAM_STARTED,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}});this.inputAudioState.isMuted&&this.muteAllAudioTracks();const e=this.inputAudioStream.getAudioTracks()[0];this.peerConnection.addTrack(e,this.inputAudioStream),this.publicEventEmitter.emit(o.AnamEvent.INPUT_AUDIO_STREAM_STARTED,this.inputAudioStream);const t=this.peerConnection.createDataChannel("chat",{ordered:!0});t.onopen=()=>{this.dataChannel=null!=t?t:null},t.onclose=()=>{},t.onmessage=e=>{const t=JSON.parse(e.data);this.internalEventEmitter.emit(o.InternalEvent.WEBRTC_CHAT_MESSAGE_RECEIVED,t)}}))}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)}}}},958:(e,t,n)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.StreamingClient=t.PublicEventEmitter=t.MessageHistoryClient=t.InternalEventEmitter=t.EngineApiRestClient=t.CoreApiRestClient=t.SignallingClient=void 0;var i=n(279);Object.defineProperty(t,"SignallingClient",{enumerable:!0,get:function(){return i.SignallingClient}});var s=n(18);Object.defineProperty(t,"CoreApiRestClient",{enumerable:!0,get:function(){return s.CoreApiRestClient}});var o=n(529);Object.defineProperty(t,"EngineApiRestClient",{enumerable:!0,get:function(){return o.EngineApiRestClient}});var r=n(259);Object.defineProperty(t,"InternalEventEmitter",{enumerable:!0,get:function(){return r.InternalEventEmitter}});var a=n(462);Object.defineProperty(t,"MessageHistoryClient",{enumerable:!0,get:function(){return a.MessageHistoryClient}});var l=n(995);Object.defineProperty(t,"PublicEventEmitter",{enumerable:!0,get:function(){return l.PublicEventEmitter}});var c=n(577);Object.defineProperty(t,"StreamingClient",{enumerable:!0,get:function(){return c.StreamingClient}})},711:function(e,t,n){var i=this&&this.__awaiter||function(e,t,n,i){return new(n||(n=Promise))((function(s,o){function r(e){try{l(i.next(e))}catch(e){o(e)}}function a(e){try{l(i.throw(e))}catch(e){o(e)}}function l(e){var t;e.done?s(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(r,a)}l((i=i.apply(e,t||[])).next())}))};Object.defineProperty(t,"__esModule",{value:!0}),t.TalkMessageStream=void 0;const s=n(170),o=n(322);t.TalkMessageStream=class{constructor(e,t,n){this.state=o.TalkMessageStreamState.UNSTARTED,this.correlationId=e,this.internalEventEmitter=t,this.signallingClient=n,this.internalEventEmitter.addListener(s.InternalEvent.SIGNAL_MESSAGE_RECEIVED,this.onSignalMessage.bind(this))}onDeactivate(){this.internalEventEmitter.removeListener(s.InternalEvent.SIGNAL_MESSAGE_RECEIVED,this.onSignalMessage.bind(this))}onSignalMessage(e){return i(this,void 0,void 0,(function*(){e.actionType===s.SignalMessageAction.TALK_STREAM_INTERRUPTED&&e.payload.correlationId===this.correlationId&&(this.state=o.TalkMessageStreamState.INTERRUPTED,this.onDeactivate())}))}endMessage(){return i(this,void 0,void 0,(function*(){if(this.state===o.TalkMessageStreamState.ENDED)return void console.warn("Talk stream is already ended via end of speech. No need to call endMessage.");if(this.state!==o.TalkMessageStreamState.STREAMING)return void console.warn("Talk stream is not active state: "+this.state);const e={content:"",startOfSpeech:!1,endOfSpeech:!0,correlationId:this.correlationId};yield this.signallingClient.sendTalkMessage(e),this.state=o.TalkMessageStreamState.ENDED,this.onDeactivate()}))}streamMessageChunk(e,t){return i(this,void 0,void 0,(function*(){if(this.state!==o.TalkMessageStreamState.STREAMING&&this.state!==o.TalkMessageStreamState.UNSTARTED)throw new Error("Talk stream is not in an active state: "+this.state);const n={content:e,startOfSpeech:this.state===o.TalkMessageStreamState.UNSTARTED,endOfSpeech:t,correlationId:this.correlationId};this.state=t?o.TalkMessageStreamState.ENDED:o.TalkMessageStreamState.STREAMING,this.state===o.TalkMessageStreamState.ENDED&&this.onDeactivate(),yield this.signallingClient.sendTalkMessage(n)}))}getCorrelationId(){return this.correlationId}isActive(){return this.state===o.TalkMessageStreamState.STREAMING||this.state===o.TalkMessageStreamState.UNSTARTED}getState(){return this.state}}},322:(e,t)=>{var n;Object.defineProperty(t,"__esModule",{value:!0}),t.TalkMessageStreamState=void 0,function(e){e[e.UNSTARTED=0]="UNSTARTED",e[e.STREAMING=1]="STREAMING",e[e.INTERRUPTED=2]="INTERRUPTED",e[e.ENDED=3]="ENDED"}(n||(t.TalkMessageStreamState=n={}))},878:(e,t,n)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.InternalEvent=t.AnamEvent=void 0;var i=n(21);Object.defineProperty(t,"AnamEvent",{enumerable:!0,get:function(){return i.AnamEvent}});var s=n(645);Object.defineProperty(t,"InternalEvent",{enumerable:!0,get:function(){return s.InternalEvent}})},645:(e,t)=>{var n;Object.defineProperty(t,"__esModule",{value:!0}),t.InternalEvent=void 0,function(e){e.WEB_SOCKET_OPEN="WEB_SOCKET_OPEN",e.SIGNAL_MESSAGE_RECEIVED="SIGNAL_MESSAGE_RECEIVED",e.WEBRTC_CHAT_MESSAGE_RECEIVED="WEBRTC_CHAT_MESSAGE_RECEIVED"}(n||(t.InternalEvent=n={}))},21:(e,t)=>{var n;Object.defineProperty(t,"__esModule",{value:!0}),t.AnamEvent=void 0,function(e){e.MESSAGE_HISTORY_UPDATED="MESSAGE_HISTORY_UPDATED",e.MESSAGE_STREAM_EVENT_RECEIVED="MESSAGE_STREAM_EVENT_RECEIVED",e.CONNECTION_ESTABLISHED="CONNECTION_ESTABLISHED",e.CONNECTION_CLOSED="CONNECTION_CLOSED",e.INPUT_AUDIO_STREAM_STARTED="INPUT_AUDIO_STREAM_STARTED",e.VIDEO_STREAM_STARTED="VIDEO_STREAM_STARTED",e.VIDEO_PLAY_STARTED="VIDEO_PLAY_STARTED",e.AUDIO_STREAM_STARTED="AUDIO_STREAM_STARTED",e.TALK_STREAM_INTERRUPTED="TALK_STREAM_INTERRUPTED"}(n||(t.AnamEvent=n={}))},170:(e,t,n)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.InternalEvent=t.AnamEvent=t.MessageRole=t.SignalMessageAction=void 0;var i=n(295);Object.defineProperty(t,"SignalMessageAction",{enumerable:!0,get:function(){return i.SignalMessageAction}});var s=n(822);Object.defineProperty(t,"MessageRole",{enumerable:!0,get:function(){return s.MessageRole}});var o=n(878);Object.defineProperty(t,"AnamEvent",{enumerable:!0,get:function(){return o.AnamEvent}});var r=n(878);Object.defineProperty(t,"InternalEvent",{enumerable:!0,get:function(){return r.InternalEvent}})},273:(e,t)=>{var n;Object.defineProperty(t,"__esModule",{value:!0}),t.MessageRole=void 0,function(e){e.USER="user",e.PERSONA="persona"}(n||(t.MessageRole=n={}))},822:(e,t,n)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.MessageRole=void 0;var i=n(273);Object.defineProperty(t,"MessageRole",{enumerable:!0,get:function(){return i.MessageRole}})},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",e.TALK_STREAM_INTERRUPTED="talkinputstreaminterrupted",e.TALK_STREAM_INPUT="talkstream"}(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 s=t[i];if(void 0!==s)return s.exports;var o=t[i]={exports:{}};return e[i].call(o.exports,o,o.exports,n),o.exports}(440)})()));
|