@layercode/js-sdk 1.0.27 → 2.0.0
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.
|
@@ -3484,10 +3484,10 @@ registerProcessor('audio_processor', AudioProcessor);
|
|
|
3484
3484
|
|
|
3485
3485
|
/* eslint-env browser */
|
|
3486
3486
|
// SDK version - updated when publishing
|
|
3487
|
-
const SDK_VERSION = '
|
|
3487
|
+
const SDK_VERSION = '2.0.0';
|
|
3488
3488
|
/**
|
|
3489
3489
|
* @class LayercodeClient
|
|
3490
|
-
* @classdesc Core client for Layercode audio
|
|
3490
|
+
* @classdesc Core client for Layercode audio agent that manages audio recording, WebSocket communication, and speech processing.
|
|
3491
3491
|
*/
|
|
3492
3492
|
class LayercodeClient {
|
|
3493
3493
|
/**
|
|
@@ -3497,8 +3497,8 @@ registerProcessor('audio_processor', AudioProcessor);
|
|
|
3497
3497
|
constructor(options) {
|
|
3498
3498
|
this.deviceId = null;
|
|
3499
3499
|
this.options = {
|
|
3500
|
-
|
|
3501
|
-
|
|
3500
|
+
agentId: options.agentId,
|
|
3501
|
+
conversationId: options.conversationId || null,
|
|
3502
3502
|
authorizeSessionEndpoint: options.authorizeSessionEndpoint,
|
|
3503
3503
|
metadata: options.metadata || {},
|
|
3504
3504
|
vadResumeDelay: options.vadResumeDelay || 500,
|
|
@@ -3513,18 +3513,18 @@ registerProcessor('audio_processor', AudioProcessor);
|
|
|
3513
3513
|
onUserIsSpeakingChange: options.onUserIsSpeakingChange || (() => { }),
|
|
3514
3514
|
};
|
|
3515
3515
|
this.AMPLITUDE_MONITORING_SAMPLE_RATE = 10;
|
|
3516
|
-
this._websocketUrl = 'wss://api.layercode.com/v1/
|
|
3517
|
-
this.wavRecorder = new WavRecorder({ sampleRate: 8000 }); // TODO should be set my fetched
|
|
3516
|
+
this._websocketUrl = 'wss://api.layercode.com/v1/agents/websocket';
|
|
3517
|
+
this.wavRecorder = new WavRecorder({ sampleRate: 8000 }); // TODO should be set my fetched agent config
|
|
3518
3518
|
this.wavPlayer = new WavStreamPlayer({
|
|
3519
3519
|
finishedPlayingCallback: this._clientResponseAudioReplayFinished.bind(this),
|
|
3520
|
-
sampleRate: 16000, // TODO should be set my fetched
|
|
3520
|
+
sampleRate: 16000, // TODO should be set my fetched agent config
|
|
3521
3521
|
});
|
|
3522
3522
|
this.vad = null;
|
|
3523
3523
|
this.ws = null;
|
|
3524
3524
|
this.status = 'disconnected';
|
|
3525
3525
|
this.userAudioAmplitude = 0;
|
|
3526
3526
|
this.agentAudioAmplitude = 0;
|
|
3527
|
-
this.
|
|
3527
|
+
this.conversationId = options.conversationId || null;
|
|
3528
3528
|
this.pushToTalkActive = false;
|
|
3529
3529
|
this.pushToTalkEnabled = false;
|
|
3530
3530
|
this.canInterrupt = false;
|
|
@@ -3823,7 +3823,7 @@ registerProcessor('audio_processor', AudioProcessor);
|
|
|
3823
3823
|
}
|
|
3824
3824
|
}
|
|
3825
3825
|
/**
|
|
3826
|
-
* Connects to the Layercode
|
|
3826
|
+
* Connects to the Layercode agent and starts the audio conversation
|
|
3827
3827
|
* @async
|
|
3828
3828
|
* @returns {Promise<void>}
|
|
3829
3829
|
*/
|
|
@@ -3832,15 +3832,15 @@ registerProcessor('audio_processor', AudioProcessor);
|
|
|
3832
3832
|
this._setStatus('connecting');
|
|
3833
3833
|
// Reset turn tracking for clean start
|
|
3834
3834
|
this._resetTurnTracking();
|
|
3835
|
-
// Get
|
|
3835
|
+
// Get conversation key from server
|
|
3836
3836
|
let authorizeSessionRequestBody = {
|
|
3837
|
-
|
|
3837
|
+
agent_id: this.options.agentId,
|
|
3838
3838
|
metadata: this.options.metadata,
|
|
3839
3839
|
sdk_version: SDK_VERSION,
|
|
3840
3840
|
};
|
|
3841
|
-
// If we're reconnecting to a previous
|
|
3842
|
-
if (this.options.
|
|
3843
|
-
authorizeSessionRequestBody.
|
|
3841
|
+
// If we're reconnecting to a previous conversation, we need to include the conversation_id in the request. Otherwise we don't send conversation_id, and a new conversation will be created and the conversation_id will be returned in the response.
|
|
3842
|
+
if (this.options.conversationId) {
|
|
3843
|
+
authorizeSessionRequestBody.conversation_id = this.options.conversationId;
|
|
3844
3844
|
}
|
|
3845
3845
|
const authorizeSessionResponse = await fetch(this.options.authorizeSessionEndpoint, {
|
|
3846
3846
|
method: 'POST',
|
|
@@ -3850,10 +3850,10 @@ registerProcessor('audio_processor', AudioProcessor);
|
|
|
3850
3850
|
body: JSON.stringify(authorizeSessionRequestBody),
|
|
3851
3851
|
});
|
|
3852
3852
|
if (!authorizeSessionResponse.ok) {
|
|
3853
|
-
throw new Error(`Failed to authorize
|
|
3853
|
+
throw new Error(`Failed to authorize conversation: ${authorizeSessionResponse.statusText}`);
|
|
3854
3854
|
}
|
|
3855
3855
|
const authorizeSessionResponseBody = await authorizeSessionResponse.json();
|
|
3856
|
-
this.
|
|
3856
|
+
this.conversationId = authorizeSessionResponseBody.conversation_id; // Save the conversation_id for use in future reconnects
|
|
3857
3857
|
// Connect WebSocket
|
|
3858
3858
|
this.ws = new WebSocket(`${this._websocketUrl}?${new URLSearchParams({
|
|
3859
3859
|
client_session_key: authorizeSessionResponseBody.client_session_key,
|
|
@@ -3877,7 +3877,7 @@ registerProcessor('audio_processor', AudioProcessor);
|
|
|
3877
3877
|
this.ws.onopen = () => {
|
|
3878
3878
|
console.log('WebSocket connection established');
|
|
3879
3879
|
this._setStatus('connected');
|
|
3880
|
-
this.options.onConnect({
|
|
3880
|
+
this.options.onConnect({ conversationId: this.conversationId });
|
|
3881
3881
|
// Attempt to send ready message if recorder already started
|
|
3882
3882
|
this._sendReadyIfNeeded();
|
|
3883
3883
|
};
|
|
@@ -3900,7 +3900,7 @@ registerProcessor('audio_processor', AudioProcessor);
|
|
|
3900
3900
|
// this is to ensure that the device is initialized before the recorder is started
|
|
3901
3901
|
}
|
|
3902
3902
|
catch (error) {
|
|
3903
|
-
console.error('Error connecting to Layercode
|
|
3903
|
+
console.error('Error connecting to Layercode agent:', error);
|
|
3904
3904
|
this._setStatus('error');
|
|
3905
3905
|
this.options.onError(error instanceof Error ? error : new Error(String(error)));
|
|
3906
3906
|
throw error;
|